2 * Copyright (c) 2000-2005 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
34 #include "xfs_types.h"
38 #include "xfs_trans.h"
43 #include "xfs_dmapi.h"
44 #include "xfs_mount.h"
45 #include "xfs_bmap_btree.h"
46 #include "xfs_alloc_btree.h"
47 #include "xfs_ialloc_btree.h"
48 #include "xfs_dir_sf.h"
49 #include "xfs_dir2_sf.h"
50 #include "xfs_attr_sf.h"
51 #include "xfs_dinode.h"
52 #include "xfs_inode.h"
53 #include "xfs_inode_item.h"
54 #include "xfs_btree.h"
55 #include "xfs_error.h"
56 #include "xfs_alloc.h"
57 #include "xfs_ialloc.h"
58 #include "xfs_fsops.h"
59 #include "xfs_itable.h"
60 #include "xfs_trans_space.h"
61 #include "xfs_rtalloc.h"
65 * File system operations
74 geo->blocksize = mp->m_sb.sb_blocksize;
75 geo->rtextsize = mp->m_sb.sb_rextsize;
76 geo->agblocks = mp->m_sb.sb_agblocks;
77 geo->agcount = mp->m_sb.sb_agcount;
78 geo->logblocks = mp->m_sb.sb_logblocks;
79 geo->sectsize = mp->m_sb.sb_sectsize;
80 geo->inodesize = mp->m_sb.sb_inodesize;
81 geo->imaxpct = mp->m_sb.sb_imax_pct;
82 geo->datablocks = mp->m_sb.sb_dblocks;
83 geo->rtblocks = mp->m_sb.sb_rblocks;
84 geo->rtextents = mp->m_sb.sb_rextents;
85 geo->logstart = mp->m_sb.sb_logstart;
86 ASSERT(sizeof(geo->uuid)==sizeof(mp->m_sb.sb_uuid));
87 memcpy(geo->uuid, &mp->m_sb.sb_uuid, sizeof(mp->m_sb.sb_uuid));
88 if (new_version >= 2) {
89 geo->sunit = mp->m_sb.sb_unit;
90 geo->swidth = mp->m_sb.sb_width;
92 if (new_version >= 3) {
93 geo->version = XFS_FSOP_GEOM_VERSION;
95 (XFS_SB_VERSION_HASATTR(&mp->m_sb) ?
96 XFS_FSOP_GEOM_FLAGS_ATTR : 0) |
97 (XFS_SB_VERSION_HASNLINK(&mp->m_sb) ?
98 XFS_FSOP_GEOM_FLAGS_NLINK : 0) |
99 (XFS_SB_VERSION_HASQUOTA(&mp->m_sb) ?
100 XFS_FSOP_GEOM_FLAGS_QUOTA : 0) |
101 (XFS_SB_VERSION_HASALIGN(&mp->m_sb) ?
102 XFS_FSOP_GEOM_FLAGS_IALIGN : 0) |
103 (XFS_SB_VERSION_HASDALIGN(&mp->m_sb) ?
104 XFS_FSOP_GEOM_FLAGS_DALIGN : 0) |
105 (XFS_SB_VERSION_HASSHARED(&mp->m_sb) ?
106 XFS_FSOP_GEOM_FLAGS_SHARED : 0) |
107 (XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb) ?
108 XFS_FSOP_GEOM_FLAGS_EXTFLG : 0) |
109 (XFS_SB_VERSION_HASDIRV2(&mp->m_sb) ?
110 XFS_FSOP_GEOM_FLAGS_DIRV2 : 0) |
111 (XFS_SB_VERSION_HASSECTOR(&mp->m_sb) ?
112 XFS_FSOP_GEOM_FLAGS_SECTOR : 0) |
113 (XFS_SB_VERSION_HASATTR2(&mp->m_sb) ?
114 XFS_FSOP_GEOM_FLAGS_ATTR2 : 0);
115 geo->logsectsize = XFS_SB_VERSION_HASSECTOR(&mp->m_sb) ?
116 mp->m_sb.sb_logsectsize : BBSIZE;
117 geo->rtsectsize = mp->m_sb.sb_blocksize;
118 geo->dirblocksize = mp->m_dirblksize;
120 if (new_version >= 4) {
122 (XFS_SB_VERSION_HASLOGV2(&mp->m_sb) ?
123 XFS_FSOP_GEOM_FLAGS_LOGV2 : 0);
124 geo->logsunit = mp->m_sb.sb_logsunit;
130 xfs_growfs_data_private(
131 xfs_mount_t *mp, /* mount point for filesystem */
132 xfs_growfs_data_t *in) /* growfs data input struct */
138 xfs_extlen_t tmpsize;
139 xfs_alloc_rec_t *arec;
140 xfs_btree_sblock_t *block;
145 xfs_agnumber_t nagcount;
146 xfs_agnumber_t nagimax = 0;
147 xfs_rfsblock_t nb, nb_mod;
149 xfs_rfsblock_t nfree;
150 xfs_agnumber_t oagcount;
157 if (nb < mp->m_sb.sb_dblocks || pct < 0 || pct > 100)
158 return XFS_ERROR(EINVAL);
159 dpct = pct - mp->m_sb.sb_imax_pct;
160 error = xfs_read_buf(mp, mp->m_ddev_targp,
161 XFS_FSB_TO_BB(mp, nb) - XFS_FSS_TO_BB(mp, 1),
162 XFS_FSS_TO_BB(mp, 1), 0, &bp);
168 new = nb; /* use new as a temporary here */
169 nb_mod = do_div(new, mp->m_sb.sb_agblocks);
170 nagcount = new + (nb_mod != 0);
171 if (nb_mod && nb_mod < XFS_MIN_AG_BLOCKS) {
173 nb = nagcount * mp->m_sb.sb_agblocks;
174 if (nb < mp->m_sb.sb_dblocks)
175 return XFS_ERROR(EINVAL);
177 new = nb - mp->m_sb.sb_dblocks;
178 oagcount = mp->m_sb.sb_agcount;
179 if (nagcount > oagcount) {
180 down_write(&mp->m_peraglock);
181 mp->m_perag = kmem_realloc(mp->m_perag,
182 sizeof(xfs_perag_t) * nagcount,
183 sizeof(xfs_perag_t) * oagcount,
185 memset(&mp->m_perag[oagcount], 0,
186 (nagcount - oagcount) * sizeof(xfs_perag_t));
187 mp->m_flags |= XFS_MOUNT_32BITINODES;
188 nagimax = xfs_initialize_perag(mp, nagcount);
189 up_write(&mp->m_peraglock);
191 tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFS);
192 if ((error = xfs_trans_reserve(tp, XFS_GROWFS_SPACE_RES(mp),
193 XFS_GROWDATA_LOG_RES(mp), 0, 0, 0))) {
194 xfs_trans_cancel(tp, 0);
199 for (agno = nagcount - 1; agno >= oagcount; agno--, new -= agsize) {
201 * AG freelist header block
203 bp = xfs_buf_get(mp->m_ddev_targp,
204 XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
205 XFS_FSS_TO_BB(mp, 1), 0);
206 agf = XFS_BUF_TO_AGF(bp);
207 memset(agf, 0, mp->m_sb.sb_sectsize);
208 INT_SET(agf->agf_magicnum, ARCH_CONVERT, XFS_AGF_MAGIC);
209 INT_SET(agf->agf_versionnum, ARCH_CONVERT, XFS_AGF_VERSION);
210 INT_SET(agf->agf_seqno, ARCH_CONVERT, agno);
211 if (agno == nagcount - 1)
214 (agno * (xfs_rfsblock_t)mp->m_sb.sb_agblocks);
216 agsize = mp->m_sb.sb_agblocks;
217 INT_SET(agf->agf_length, ARCH_CONVERT, agsize);
218 INT_SET(agf->agf_roots[XFS_BTNUM_BNOi], ARCH_CONVERT,
220 INT_SET(agf->agf_roots[XFS_BTNUM_CNTi], ARCH_CONVERT,
222 INT_SET(agf->agf_levels[XFS_BTNUM_BNOi], ARCH_CONVERT, 1);
223 INT_SET(agf->agf_levels[XFS_BTNUM_CNTi], ARCH_CONVERT, 1);
224 agf->agf_flfirst = 0;
225 INT_SET(agf->agf_fllast, ARCH_CONVERT, XFS_AGFL_SIZE(mp) - 1);
226 agf->agf_flcount = 0;
227 tmpsize = agsize - XFS_PREALLOC_BLOCKS(mp);
228 INT_SET(agf->agf_freeblks, ARCH_CONVERT, tmpsize);
229 INT_SET(agf->agf_longest, ARCH_CONVERT, tmpsize);
230 error = xfs_bwrite(mp, bp);
235 * AG inode header block
237 bp = xfs_buf_get(mp->m_ddev_targp,
238 XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
239 XFS_FSS_TO_BB(mp, 1), 0);
240 agi = XFS_BUF_TO_AGI(bp);
241 memset(agi, 0, mp->m_sb.sb_sectsize);
242 INT_SET(agi->agi_magicnum, ARCH_CONVERT, XFS_AGI_MAGIC);
243 INT_SET(agi->agi_versionnum, ARCH_CONVERT, XFS_AGI_VERSION);
244 INT_SET(agi->agi_seqno, ARCH_CONVERT, agno);
245 INT_SET(agi->agi_length, ARCH_CONVERT, agsize);
247 INT_SET(agi->agi_root, ARCH_CONVERT, XFS_IBT_BLOCK(mp));
248 INT_SET(agi->agi_level, ARCH_CONVERT, 1);
249 agi->agi_freecount = 0;
250 INT_SET(agi->agi_newino, ARCH_CONVERT, NULLAGINO);
251 INT_SET(agi->agi_dirino, ARCH_CONVERT, NULLAGINO);
252 for (bucket = 0; bucket < XFS_AGI_UNLINKED_BUCKETS; bucket++)
253 INT_SET(agi->agi_unlinked[bucket], ARCH_CONVERT,
255 error = xfs_bwrite(mp, bp);
260 * BNO btree root block
262 bp = xfs_buf_get(mp->m_ddev_targp,
263 XFS_AGB_TO_DADDR(mp, agno, XFS_BNO_BLOCK(mp)),
264 BTOBB(mp->m_sb.sb_blocksize), 0);
265 block = XFS_BUF_TO_SBLOCK(bp);
266 memset(block, 0, mp->m_sb.sb_blocksize);
267 INT_SET(block->bb_magic, ARCH_CONVERT, XFS_ABTB_MAGIC);
269 INT_SET(block->bb_numrecs, ARCH_CONVERT, 1);
270 INT_SET(block->bb_leftsib, ARCH_CONVERT, NULLAGBLOCK);
271 INT_SET(block->bb_rightsib, ARCH_CONVERT, NULLAGBLOCK);
272 arec = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize, xfs_alloc,
273 block, 1, mp->m_alloc_mxr[0]);
274 INT_SET(arec->ar_startblock, ARCH_CONVERT,
275 XFS_PREALLOC_BLOCKS(mp));
276 INT_SET(arec->ar_blockcount, ARCH_CONVERT,
277 agsize - INT_GET(arec->ar_startblock, ARCH_CONVERT));
278 error = xfs_bwrite(mp, bp);
283 * CNT btree root block
285 bp = xfs_buf_get(mp->m_ddev_targp,
286 XFS_AGB_TO_DADDR(mp, agno, XFS_CNT_BLOCK(mp)),
287 BTOBB(mp->m_sb.sb_blocksize), 0);
288 block = XFS_BUF_TO_SBLOCK(bp);
289 memset(block, 0, mp->m_sb.sb_blocksize);
290 INT_SET(block->bb_magic, ARCH_CONVERT, XFS_ABTC_MAGIC);
292 INT_SET(block->bb_numrecs, ARCH_CONVERT, 1);
293 INT_SET(block->bb_leftsib, ARCH_CONVERT, NULLAGBLOCK);
294 INT_SET(block->bb_rightsib, ARCH_CONVERT, NULLAGBLOCK);
295 arec = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize, xfs_alloc,
296 block, 1, mp->m_alloc_mxr[0]);
297 INT_SET(arec->ar_startblock, ARCH_CONVERT,
298 XFS_PREALLOC_BLOCKS(mp));
299 INT_SET(arec->ar_blockcount, ARCH_CONVERT,
300 agsize - INT_GET(arec->ar_startblock, ARCH_CONVERT));
301 nfree += INT_GET(arec->ar_blockcount, ARCH_CONVERT);
302 error = xfs_bwrite(mp, bp);
307 * INO btree root block
309 bp = xfs_buf_get(mp->m_ddev_targp,
310 XFS_AGB_TO_DADDR(mp, agno, XFS_IBT_BLOCK(mp)),
311 BTOBB(mp->m_sb.sb_blocksize), 0);
312 block = XFS_BUF_TO_SBLOCK(bp);
313 memset(block, 0, mp->m_sb.sb_blocksize);
314 INT_SET(block->bb_magic, ARCH_CONVERT, XFS_IBT_MAGIC);
316 block->bb_numrecs = 0;
317 INT_SET(block->bb_leftsib, ARCH_CONVERT, NULLAGBLOCK);
318 INT_SET(block->bb_rightsib, ARCH_CONVERT, NULLAGBLOCK);
319 error = xfs_bwrite(mp, bp);
324 xfs_trans_agblocks_delta(tp, nfree);
326 * There are new blocks in the old last a.g.
330 * Change the agi length.
332 error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
337 agi = XFS_BUF_TO_AGI(bp);
338 INT_MOD(agi->agi_length, ARCH_CONVERT, new);
339 ASSERT(nagcount == oagcount ||
340 INT_GET(agi->agi_length, ARCH_CONVERT) ==
341 mp->m_sb.sb_agblocks);
342 xfs_ialloc_log_agi(tp, bp, XFS_AGI_LENGTH);
346 error = xfs_alloc_read_agf(mp, tp, agno, 0, &bp);
351 agf = XFS_BUF_TO_AGF(bp);
352 INT_MOD(agf->agf_length, ARCH_CONVERT, new);
353 ASSERT(INT_GET(agf->agf_length, ARCH_CONVERT) ==
354 INT_GET(agi->agi_length, ARCH_CONVERT));
356 * Free the new space.
358 error = xfs_free_extent(tp, XFS_AGB_TO_FSB(mp, agno,
359 INT_GET(agf->agf_length, ARCH_CONVERT) - new), new);
364 if (nagcount > oagcount)
365 xfs_trans_mod_sb(tp, XFS_TRANS_SB_AGCOUNT, nagcount - oagcount);
366 if (nb > mp->m_sb.sb_dblocks)
367 xfs_trans_mod_sb(tp, XFS_TRANS_SB_DBLOCKS,
368 nb - mp->m_sb.sb_dblocks);
370 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, nfree);
372 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IMAXPCT, dpct);
373 error = xfs_trans_commit(tp, 0, NULL);
377 /* New allocation groups fully initialized, so update mount struct */
379 mp->m_maxagi = nagimax;
380 if (mp->m_sb.sb_imax_pct) {
381 __uint64_t icount = mp->m_sb.sb_dblocks * mp->m_sb.sb_imax_pct;
383 mp->m_maxicount = icount << mp->m_sb.sb_inopblog;
386 for (agno = 1; agno < nagcount; agno++) {
387 error = xfs_read_buf(mp, mp->m_ddev_targp,
388 XFS_AGB_TO_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
389 XFS_FSS_TO_BB(mp, 1), 0, &bp);
391 xfs_fs_cmn_err(CE_WARN, mp,
392 "error %d reading secondary superblock for ag %d",
396 sbp = XFS_BUF_TO_SBP(bp);
397 xfs_xlatesb(sbp, &mp->m_sb, -1, XFS_SB_ALL_BITS);
399 * If we get an error writing out the alternate superblocks,
400 * just issue a warning and continue. The real work is
401 * already done and committed.
403 if (!(error = xfs_bwrite(mp, bp))) {
406 xfs_fs_cmn_err(CE_WARN, mp,
407 "write error %d updating secondary superblock for ag %d",
409 break; /* no point in continuing */
415 xfs_trans_cancel(tp, XFS_TRANS_ABORT);
420 xfs_growfs_log_private(
421 xfs_mount_t *mp, /* mount point for filesystem */
422 xfs_growfs_log_t *in) /* growfs log input struct */
427 if (nb < XFS_MIN_LOG_BLOCKS || nb < XFS_B_TO_FSB(mp, XFS_MIN_LOG_BYTES))
428 return XFS_ERROR(EINVAL);
429 if (nb == mp->m_sb.sb_logblocks &&
430 in->isint == (mp->m_sb.sb_logstart != 0))
431 return XFS_ERROR(EINVAL);
433 * Moving the log is hard, need new interfaces to sync
434 * the log first, hold off all activity while moving it.
435 * Can have shorter or longer log in the same space,
436 * or transform internal to external log or vice versa.
438 return XFS_ERROR(ENOSYS);
442 * protected versions of growfs function acquire and release locks on the mount
443 * point - exported through ioctls: XFS_IOC_FSGROWFSDATA, XFS_IOC_FSGROWFSLOG,
451 xfs_growfs_data_t *in)
454 if (!cpsema(&mp->m_growlock))
455 return XFS_ERROR(EWOULDBLOCK);
456 error = xfs_growfs_data_private(mp, in);
457 vsema(&mp->m_growlock);
464 xfs_growfs_log_t *in)
467 if (!cpsema(&mp->m_growlock))
468 return XFS_ERROR(EWOULDBLOCK);
469 error = xfs_growfs_log_private(mp, in);
470 vsema(&mp->m_growlock);
475 * exported through ioctl XFS_IOC_FSCOUNTS
481 xfs_fsop_counts_t *cnt)
486 cnt->freedata = mp->m_sb.sb_fdblocks;
487 cnt->freertx = mp->m_sb.sb_frextents;
488 cnt->freeino = mp->m_sb.sb_ifree;
489 cnt->allocino = mp->m_sb.sb_icount;
490 XFS_SB_UNLOCK(mp, s);
495 * exported through ioctl XFS_IOC_SET_RESBLKS & XFS_IOC_GET_RESBLKS
497 * xfs_reserve_blocks is called to set m_resblks
498 * in the in-core mount table. The number of unused reserved blocks
499 * is kept in m_resbls_avail.
501 * Reserve the requested number of blocks if available. Otherwise return
502 * as many as possible to satisfy the request. The actual number
503 * reserved are returned in outval
505 * A null inval pointer indicates that only the current reserved blocks
506 * available should be returned no settings are changed.
513 xfs_fsop_resblks_t *outval)
515 __int64_t lcounter, delta;
519 /* If inval is null, report current values and return */
521 if (inval == (__uint64_t *)NULL) {
522 outval->resblks = mp->m_resblks;
523 outval->resblks_avail = mp->m_resblks_avail;
531 * If our previous reservation was larger than the current value,
532 * then move any unused blocks back to the free pool.
535 if (mp->m_resblks > request) {
536 lcounter = mp->m_resblks_avail - request;
537 if (lcounter > 0) { /* release unused blocks */
538 mp->m_sb.sb_fdblocks += lcounter;
539 mp->m_resblks_avail -= lcounter;
541 mp->m_resblks = request;
543 delta = request - mp->m_resblks;
544 lcounter = mp->m_sb.sb_fdblocks - delta;
546 /* We can't satisfy the request, just get what we can */
547 mp->m_resblks += mp->m_sb.sb_fdblocks;
548 mp->m_resblks_avail += mp->m_sb.sb_fdblocks;
549 mp->m_sb.sb_fdblocks = 0;
551 mp->m_sb.sb_fdblocks = lcounter;
552 mp->m_resblks = request;
553 mp->m_resblks_avail += delta;
557 outval->resblks = mp->m_resblks;
558 outval->resblks_avail = mp->m_resblks_avail;
559 XFS_SB_UNLOCK(mp, s);
569 case XFS_FSOP_GOING_FLAGS_DEFAULT: {
570 struct vfs *vfsp = XFS_MTOVFS(mp);
571 struct super_block *sb = freeze_bdev(vfsp->vfs_super->s_bdev);
574 xfs_force_shutdown(mp, XFS_FORCE_UMOUNT);
575 thaw_bdev(sb->s_bdev, sb);
580 case XFS_FSOP_GOING_FLAGS_LOGFLUSH:
581 xfs_force_shutdown(mp, XFS_FORCE_UMOUNT);
583 case XFS_FSOP_GOING_FLAGS_NOLOGFLUSH:
584 xfs_force_shutdown(mp, XFS_FORCE_UMOUNT|XFS_LOG_IO_ERROR);
587 return XFS_ERROR(EINVAL);