2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_types.h"
24 #include "xfs_trans.h"
29 #include "xfs_dmapi.h"
30 #include "xfs_mount.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_alloc_btree.h"
33 #include "xfs_ialloc_btree.h"
34 #include "xfs_dir_sf.h"
35 #include "xfs_dir2_sf.h"
36 #include "xfs_attr_sf.h"
37 #include "xfs_dinode.h"
38 #include "xfs_inode.h"
39 #include "xfs_btree.h"
40 #include "xfs_ialloc.h"
41 #include "xfs_alloc.h"
42 #include "xfs_rtalloc.h"
44 #include "xfs_error.h"
46 #include "xfs_quota.h"
47 #include "xfs_fsops.h"
49 STATIC void xfs_mount_log_sbunit(xfs_mount_t *, __int64_t);
50 STATIC int xfs_uuid_mount(xfs_mount_t *);
51 STATIC void xfs_uuid_unmount(xfs_mount_t *mp);
52 STATIC void xfs_unmountfs_wait(xfs_mount_t *);
56 STATIC void xfs_icsb_destroy_counters(xfs_mount_t *);
57 STATIC void xfs_icsb_balance_counter(xfs_mount_t *, xfs_sb_field_t, int);
58 STATIC void xfs_icsb_sync_counters(xfs_mount_t *);
59 STATIC int xfs_icsb_modify_counters(xfs_mount_t *, xfs_sb_field_t,
61 STATIC int xfs_icsb_modify_counters_locked(xfs_mount_t *, xfs_sb_field_t,
66 #define xfs_icsb_destroy_counters(mp) do { } while (0)
67 #define xfs_icsb_balance_counter(mp, a, b) do { } while (0)
68 #define xfs_icsb_sync_counters(mp) do { } while (0)
69 #define xfs_icsb_modify_counters(mp, a, b, c) do { } while (0)
70 #define xfs_icsb_modify_counters_locked(mp, a, b, c) do { } while (0)
76 short type; /* 0 = integer
77 * 1 = binary / string (no translation)
80 { offsetof(xfs_sb_t, sb_magicnum), 0 },
81 { offsetof(xfs_sb_t, sb_blocksize), 0 },
82 { offsetof(xfs_sb_t, sb_dblocks), 0 },
83 { offsetof(xfs_sb_t, sb_rblocks), 0 },
84 { offsetof(xfs_sb_t, sb_rextents), 0 },
85 { offsetof(xfs_sb_t, sb_uuid), 1 },
86 { offsetof(xfs_sb_t, sb_logstart), 0 },
87 { offsetof(xfs_sb_t, sb_rootino), 0 },
88 { offsetof(xfs_sb_t, sb_rbmino), 0 },
89 { offsetof(xfs_sb_t, sb_rsumino), 0 },
90 { offsetof(xfs_sb_t, sb_rextsize), 0 },
91 { offsetof(xfs_sb_t, sb_agblocks), 0 },
92 { offsetof(xfs_sb_t, sb_agcount), 0 },
93 { offsetof(xfs_sb_t, sb_rbmblocks), 0 },
94 { offsetof(xfs_sb_t, sb_logblocks), 0 },
95 { offsetof(xfs_sb_t, sb_versionnum), 0 },
96 { offsetof(xfs_sb_t, sb_sectsize), 0 },
97 { offsetof(xfs_sb_t, sb_inodesize), 0 },
98 { offsetof(xfs_sb_t, sb_inopblock), 0 },
99 { offsetof(xfs_sb_t, sb_fname[0]), 1 },
100 { offsetof(xfs_sb_t, sb_blocklog), 0 },
101 { offsetof(xfs_sb_t, sb_sectlog), 0 },
102 { offsetof(xfs_sb_t, sb_inodelog), 0 },
103 { offsetof(xfs_sb_t, sb_inopblog), 0 },
104 { offsetof(xfs_sb_t, sb_agblklog), 0 },
105 { offsetof(xfs_sb_t, sb_rextslog), 0 },
106 { offsetof(xfs_sb_t, sb_inprogress), 0 },
107 { offsetof(xfs_sb_t, sb_imax_pct), 0 },
108 { offsetof(xfs_sb_t, sb_icount), 0 },
109 { offsetof(xfs_sb_t, sb_ifree), 0 },
110 { offsetof(xfs_sb_t, sb_fdblocks), 0 },
111 { offsetof(xfs_sb_t, sb_frextents), 0 },
112 { offsetof(xfs_sb_t, sb_uquotino), 0 },
113 { offsetof(xfs_sb_t, sb_gquotino), 0 },
114 { offsetof(xfs_sb_t, sb_qflags), 0 },
115 { offsetof(xfs_sb_t, sb_flags), 0 },
116 { offsetof(xfs_sb_t, sb_shared_vn), 0 },
117 { offsetof(xfs_sb_t, sb_inoalignmt), 0 },
118 { offsetof(xfs_sb_t, sb_unit), 0 },
119 { offsetof(xfs_sb_t, sb_width), 0 },
120 { offsetof(xfs_sb_t, sb_dirblklog), 0 },
121 { offsetof(xfs_sb_t, sb_logsectlog), 0 },
122 { offsetof(xfs_sb_t, sb_logsectsize),0 },
123 { offsetof(xfs_sb_t, sb_logsunit), 0 },
124 { offsetof(xfs_sb_t, sb_features2), 0 },
125 { sizeof(xfs_sb_t), 0 }
129 * Return a pointer to an initialized xfs_mount structure.
136 mp = kmem_zalloc(sizeof(xfs_mount_t), KM_SLEEP);
138 if (xfs_icsb_init_counters(mp)) {
139 mp->m_flags |= XFS_MOUNT_NO_PERCPU_SB;
142 AIL_LOCKINIT(&mp->m_ail_lock, "xfs_ail");
143 spinlock_init(&mp->m_sb_lock, "xfs_sb");
144 mutex_init(&mp->m_ilock);
145 initnsema(&mp->m_growlock, 1, "xfs_grow");
147 * Initialize the AIL.
149 xfs_trans_ail_init(mp);
151 atomic_set(&mp->m_active_trans, 0);
157 * Free up the resources associated with a mount structure. Assume that
158 * the structure was initially zeroed, so we can tell which fields got
174 for (agno = 0; agno < mp->m_maxagi; agno++)
175 if (mp->m_perag[agno].pagb_list)
176 kmem_free(mp->m_perag[agno].pagb_list,
177 sizeof(xfs_perag_busy_t) *
179 kmem_free(mp->m_perag,
180 sizeof(xfs_perag_t) * mp->m_sb.sb_agcount);
183 AIL_LOCK_DESTROY(&mp->m_ail_lock);
184 spinlock_destroy(&mp->m_sb_lock);
185 mutex_destroy(&mp->m_ilock);
186 freesema(&mp->m_growlock);
190 if (mp->m_fsname != NULL)
191 kmem_free(mp->m_fsname, mp->m_fsname_len);
192 if (mp->m_rtname != NULL)
193 kmem_free(mp->m_rtname, strlen(mp->m_rtname) + 1);
194 if (mp->m_logname != NULL)
195 kmem_free(mp->m_logname, strlen(mp->m_logname) + 1);
198 struct vfs *vfsp = XFS_MTOVFS(mp);
200 bhv_remove_all_vfsops(vfsp, 0);
201 VFS_REMOVEBHV(vfsp, &mp->m_bhv);
204 xfs_icsb_destroy_counters(mp);
205 kmem_free(mp, sizeof(xfs_mount_t));
210 * Check the validity of the SB found.
213 xfs_mount_validate_sb(
218 * If the log device and data device have the
219 * same device number, the log is internal.
220 * Consequently, the sb_logstart should be non-zero. If
221 * we have a zero sb_logstart in this case, we may be trying to mount
222 * a volume filesystem in a non-volume manner.
224 if (sbp->sb_magicnum != XFS_SB_MAGIC) {
225 cmn_err(CE_WARN, "XFS: bad magic number");
226 return XFS_ERROR(EWRONGFS);
229 if (!XFS_SB_GOOD_VERSION(sbp)) {
230 cmn_err(CE_WARN, "XFS: bad version");
231 return XFS_ERROR(EWRONGFS);
235 sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
237 "XFS: filesystem is marked as having an external log; "
238 "specify logdev on the\nmount command line.");
239 XFS_CORRUPTION_ERROR("xfs_mount_validate_sb(1)",
240 XFS_ERRLEVEL_HIGH, mp, sbp);
241 return XFS_ERROR(EFSCORRUPTED);
245 sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
247 "XFS: filesystem is marked as having an internal log; "
248 "don't specify logdev on\nthe mount command line.");
249 XFS_CORRUPTION_ERROR("xfs_mount_validate_sb(2)",
250 XFS_ERRLEVEL_HIGH, mp, sbp);
251 return XFS_ERROR(EFSCORRUPTED);
255 * More sanity checking. These were stolen directly from
259 sbp->sb_agcount <= 0 ||
260 sbp->sb_sectsize < XFS_MIN_SECTORSIZE ||
261 sbp->sb_sectsize > XFS_MAX_SECTORSIZE ||
262 sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG ||
263 sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG ||
264 sbp->sb_blocksize < XFS_MIN_BLOCKSIZE ||
265 sbp->sb_blocksize > XFS_MAX_BLOCKSIZE ||
266 sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG ||
267 sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
268 sbp->sb_inodesize < XFS_DINODE_MIN_SIZE ||
269 sbp->sb_inodesize > XFS_DINODE_MAX_SIZE ||
270 (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE) ||
271 (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) ||
272 sbp->sb_imax_pct > 100)) {
273 cmn_err(CE_WARN, "XFS: SB sanity check 1 failed");
274 XFS_CORRUPTION_ERROR("xfs_mount_validate_sb(3)",
275 XFS_ERRLEVEL_LOW, mp, sbp);
276 return XFS_ERROR(EFSCORRUPTED);
280 * Sanity check AG count, size fields against data size field
283 sbp->sb_dblocks == 0 ||
285 (xfs_drfsbno_t)sbp->sb_agcount * sbp->sb_agblocks ||
286 sbp->sb_dblocks < (xfs_drfsbno_t)(sbp->sb_agcount - 1) *
287 sbp->sb_agblocks + XFS_MIN_AG_BLOCKS)) {
288 cmn_err(CE_WARN, "XFS: SB sanity check 2 failed");
289 XFS_ERROR_REPORT("xfs_mount_validate_sb(4)",
290 XFS_ERRLEVEL_LOW, mp);
291 return XFS_ERROR(EFSCORRUPTED);
294 ASSERT(PAGE_SHIFT >= sbp->sb_blocklog);
295 ASSERT(sbp->sb_blocklog >= BBSHIFT);
297 #if XFS_BIG_BLKNOS /* Limited by ULONG_MAX of page cache index */
299 (sbp->sb_dblocks >> (PAGE_SHIFT - sbp->sb_blocklog)) > ULONG_MAX ||
300 (sbp->sb_rblocks >> (PAGE_SHIFT - sbp->sb_blocklog)) > ULONG_MAX)) {
301 #else /* Limited by UINT_MAX of sectors */
303 (sbp->sb_dblocks << (sbp->sb_blocklog - BBSHIFT)) > UINT_MAX ||
304 (sbp->sb_rblocks << (sbp->sb_blocklog - BBSHIFT)) > UINT_MAX)) {
307 "XFS: File system is too large to be mounted on this system.");
308 return XFS_ERROR(E2BIG);
311 if (unlikely(sbp->sb_inprogress)) {
312 cmn_err(CE_WARN, "XFS: file system busy");
313 XFS_ERROR_REPORT("xfs_mount_validate_sb(5)",
314 XFS_ERRLEVEL_LOW, mp);
315 return XFS_ERROR(EFSCORRUPTED);
319 * Version 1 directory format has never worked on Linux.
321 if (unlikely(!XFS_SB_VERSION_HASDIRV2(sbp))) {
323 "XFS: Attempted to mount file system using version 1 directory format");
324 return XFS_ERROR(ENOSYS);
328 * Until this is fixed only page-sized or smaller data blocks work.
330 if (unlikely(sbp->sb_blocksize > PAGE_SIZE)) {
332 "XFS: Attempted to mount file system with blocksize %d bytes",
335 "XFS: Only page-sized (%ld) or less blocksizes currently work.",
337 return XFS_ERROR(ENOSYS);
344 xfs_initialize_perag(
347 xfs_agnumber_t agcount)
349 xfs_agnumber_t index, max_metadata;
353 xfs_sb_t *sbp = &mp->m_sb;
354 xfs_ino_t max_inum = XFS_MAXINUMBER_32;
356 /* Check to see if the filesystem can overflow 32 bit inodes */
357 agino = XFS_OFFBNO_TO_AGINO(mp, sbp->sb_agblocks - 1, 0);
358 ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino);
360 /* Clear the mount flag if no inode can overflow 32 bits
361 * on this filesystem, or if specifically requested..
363 if ((vfs->vfs_flag & VFS_32BITINODES) && ino > max_inum) {
364 mp->m_flags |= XFS_MOUNT_32BITINODES;
366 mp->m_flags &= ~XFS_MOUNT_32BITINODES;
369 /* If we can overflow then setup the ag headers accordingly */
370 if (mp->m_flags & XFS_MOUNT_32BITINODES) {
371 /* Calculate how much should be reserved for inodes to
372 * meet the max inode percentage.
374 if (mp->m_maxicount) {
377 icount = sbp->sb_dblocks * sbp->sb_imax_pct;
379 icount += sbp->sb_agblocks - 1;
380 do_div(icount, sbp->sb_agblocks);
381 max_metadata = icount;
383 max_metadata = agcount;
385 for (index = 0; index < agcount; index++) {
386 ino = XFS_AGINO_TO_INO(mp, index, agino);
387 if (ino > max_inum) {
392 /* This ag is prefered for inodes */
393 pag = &mp->m_perag[index];
394 pag->pagi_inodeok = 1;
395 if (index < max_metadata)
396 pag->pagf_metadata = 1;
399 /* Setup default behavior for smaller filesystems */
400 for (index = 0; index < agcount; index++) {
401 pag = &mp->m_perag[index];
402 pag->pagi_inodeok = 1;
411 * data - on disk version of sb
413 * dir - conversion direction: <0 - convert sb to buf
414 * >0 - convert buf to sb
415 * fields - which fields to copy (bitmask)
436 buf_ptr = (xfs_caddr_t)data;
437 mem_ptr = (xfs_caddr_t)sb;
440 f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
441 first = xfs_sb_info[f].offset;
442 size = xfs_sb_info[f + 1].offset - first;
444 ASSERT(xfs_sb_info[f].type == 0 || xfs_sb_info[f].type == 1);
446 if (size == 1 || xfs_sb_info[f].type == 1) {
448 memcpy(mem_ptr + first, buf_ptr + first, size);
450 memcpy(buf_ptr + first, mem_ptr + first, size);
455 INT_XLATE(*(__uint16_t*)(buf_ptr+first),
456 *(__uint16_t*)(mem_ptr+first),
460 INT_XLATE(*(__uint32_t*)(buf_ptr+first),
461 *(__uint32_t*)(mem_ptr+first),
465 INT_XLATE(*(__uint64_t*)(buf_ptr+first),
466 *(__uint64_t*)(mem_ptr+first), dir, ARCH_CONVERT);
473 fields &= ~(1LL << f);
480 * Does the initial read of the superblock.
483 xfs_readsb(xfs_mount_t *mp)
485 unsigned int sector_size;
486 unsigned int extra_flags;
491 ASSERT(mp->m_sb_bp == NULL);
492 ASSERT(mp->m_ddev_targp != NULL);
495 * Allocate a (locked) buffer to hold the superblock.
496 * This will be kept around at all times to optimize
497 * access to the superblock.
499 sector_size = xfs_getsize_buftarg(mp->m_ddev_targp);
500 extra_flags = XFS_BUF_LOCK | XFS_BUF_MANAGE | XFS_BUF_MAPPED;
502 bp = xfs_buf_read_flags(mp->m_ddev_targp, XFS_SB_DADDR,
503 BTOBB(sector_size), extra_flags);
504 if (!bp || XFS_BUF_ISERROR(bp)) {
505 cmn_err(CE_WARN, "XFS: SB read failed");
506 error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM;
509 ASSERT(XFS_BUF_ISBUSY(bp));
510 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
513 * Initialize the mount structure from the superblock.
514 * But first do some basic consistency checking.
516 sbp = XFS_BUF_TO_SBP(bp);
517 xfs_xlatesb(XFS_BUF_PTR(bp), &(mp->m_sb), 1, XFS_SB_ALL_BITS);
519 error = xfs_mount_validate_sb(mp, &(mp->m_sb));
521 cmn_err(CE_WARN, "XFS: SB validate failed");
526 * We must be able to do sector-sized and sector-aligned IO.
528 if (sector_size > mp->m_sb.sb_sectsize) {
530 "XFS: device supports only %u byte sectors (not %u)",
531 sector_size, mp->m_sb.sb_sectsize);
537 * If device sector size is smaller than the superblock size,
538 * re-read the superblock so the buffer is correctly sized.
540 if (sector_size < mp->m_sb.sb_sectsize) {
541 XFS_BUF_UNMANAGE(bp);
543 sector_size = mp->m_sb.sb_sectsize;
544 bp = xfs_buf_read_flags(mp->m_ddev_targp, XFS_SB_DADDR,
545 BTOBB(sector_size), extra_flags);
546 if (!bp || XFS_BUF_ISERROR(bp)) {
547 cmn_err(CE_WARN, "XFS: SB re-read failed");
548 error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM;
551 ASSERT(XFS_BUF_ISBUSY(bp));
552 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
555 xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
556 xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
557 xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
561 ASSERT(XFS_BUF_VALUSEMA(bp) > 0);
566 XFS_BUF_UNMANAGE(bp);
576 * Mount initialization code establishing various mount
577 * fields from the superblock associated with the given
581 xfs_mount_common(xfs_mount_t *mp, xfs_sb_t *sbp)
585 mp->m_agfrotor = mp->m_agirotor = 0;
586 spinlock_init(&mp->m_agirotor_lock, "m_agirotor_lock");
587 mp->m_maxagi = mp->m_sb.sb_agcount;
588 mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
589 mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
590 mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
591 mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
592 mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
593 mp->m_litino = sbp->sb_inodesize -
594 ((uint)sizeof(xfs_dinode_core_t) + (uint)sizeof(xfs_agino_t));
595 mp->m_blockmask = sbp->sb_blocksize - 1;
596 mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
597 mp->m_blockwmask = mp->m_blockwsize - 1;
598 INIT_LIST_HEAD(&mp->m_del_inodes);
601 * Setup for attributes, in case they get created.
602 * This value is for inodes getting attributes for the first time,
603 * the per-inode value is for old attribute values.
605 ASSERT(sbp->sb_inodesize >= 256 && sbp->sb_inodesize <= 2048);
606 switch (sbp->sb_inodesize) {
608 mp->m_attroffset = XFS_LITINO(mp) -
609 XFS_BMDR_SPACE_CALC(MINABTPTRS);
614 mp->m_attroffset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
619 ASSERT(mp->m_attroffset < XFS_LITINO(mp));
621 for (i = 0; i < 2; i++) {
622 mp->m_alloc_mxr[i] = XFS_BTREE_BLOCK_MAXRECS(sbp->sb_blocksize,
624 mp->m_alloc_mnr[i] = XFS_BTREE_BLOCK_MINRECS(sbp->sb_blocksize,
627 for (i = 0; i < 2; i++) {
628 mp->m_bmap_dmxr[i] = XFS_BTREE_BLOCK_MAXRECS(sbp->sb_blocksize,
630 mp->m_bmap_dmnr[i] = XFS_BTREE_BLOCK_MINRECS(sbp->sb_blocksize,
633 for (i = 0; i < 2; i++) {
634 mp->m_inobt_mxr[i] = XFS_BTREE_BLOCK_MAXRECS(sbp->sb_blocksize,
636 mp->m_inobt_mnr[i] = XFS_BTREE_BLOCK_MINRECS(sbp->sb_blocksize,
640 mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
641 mp->m_ialloc_inos = (int)MAX((__uint16_t)XFS_INODES_PER_CHUNK,
643 mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog;
648 * This function does the following on an initial mount of a file system:
649 * - reads the superblock from disk and init the mount struct
650 * - if we're a 32-bit kernel, do a size check on the superblock
651 * so we don't mount terabyte filesystems
652 * - init mount struct realtime fields
653 * - allocate inode hash table for fs
654 * - init directory manager
655 * - perform recovery and init the log manager
664 xfs_sb_t *sbp = &(mp->m_sb);
667 int readio_log, writeio_log;
670 __int64_t update_flags;
671 uint quotamount, quotaflags;
673 int uuid_mounted = 0;
676 if (mp->m_sb_bp == NULL) {
677 if ((error = xfs_readsb(mp))) {
681 xfs_mount_common(mp, sbp);
684 * Check if sb_agblocks is aligned at stripe boundary
685 * If sb_agblocks is NOT aligned turn off m_dalign since
686 * allocator alignment is within an ag, therefore ag has
687 * to be aligned at stripe boundary.
690 if (mp->m_dalign && !(mfsi_flags & XFS_MFSI_SECOND)) {
692 * If stripe unit and stripe width are not multiples
693 * of the fs blocksize turn off alignment.
695 if ((BBTOB(mp->m_dalign) & mp->m_blockmask) ||
696 (BBTOB(mp->m_swidth) & mp->m_blockmask)) {
697 if (mp->m_flags & XFS_MOUNT_RETERR) {
699 "XFS: alignment check 1 failed");
700 error = XFS_ERROR(EINVAL);
703 mp->m_dalign = mp->m_swidth = 0;
706 * Convert the stripe unit and width to FSBs.
708 mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign);
709 if (mp->m_dalign && (sbp->sb_agblocks % mp->m_dalign)) {
710 if (mp->m_flags & XFS_MOUNT_RETERR) {
711 error = XFS_ERROR(EINVAL);
714 xfs_fs_cmn_err(CE_WARN, mp,
715 "stripe alignment turned off: sunit(%d)/swidth(%d) incompatible with agsize(%d)",
716 mp->m_dalign, mp->m_swidth,
721 } else if (mp->m_dalign) {
722 mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth);
724 if (mp->m_flags & XFS_MOUNT_RETERR) {
725 xfs_fs_cmn_err(CE_WARN, mp,
726 "stripe alignment turned off: sunit(%d) less than bsize(%d)",
729 error = XFS_ERROR(EINVAL);
737 * Update superblock with new values
740 if (XFS_SB_VERSION_HASDALIGN(sbp)) {
741 if (sbp->sb_unit != mp->m_dalign) {
742 sbp->sb_unit = mp->m_dalign;
743 update_flags |= XFS_SB_UNIT;
745 if (sbp->sb_width != mp->m_swidth) {
746 sbp->sb_width = mp->m_swidth;
747 update_flags |= XFS_SB_WIDTH;
750 } else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN &&
751 XFS_SB_VERSION_HASDALIGN(&mp->m_sb)) {
752 mp->m_dalign = sbp->sb_unit;
753 mp->m_swidth = sbp->sb_width;
756 xfs_alloc_compute_maxlevels(mp);
757 xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
758 xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
759 xfs_ialloc_compute_maxlevels(mp);
761 if (sbp->sb_imax_pct) {
764 /* Make sure the maximum inode count is a multiple of the
765 * units we allocate inodes in.
768 icount = sbp->sb_dblocks * sbp->sb_imax_pct;
770 do_div(icount, mp->m_ialloc_blks);
771 mp->m_maxicount = (icount * mp->m_ialloc_blks) <<
776 mp->m_maxioffset = xfs_max_file_offset(sbp->sb_blocklog);
779 * XFS uses the uuid from the superblock as the unique
780 * identifier for fsid. We can not use the uuid from the volume
781 * since a single partition filesystem is identical to a single
782 * partition volume/filesystem.
784 if ((mfsi_flags & XFS_MFSI_SECOND) == 0 &&
785 (mp->m_flags & XFS_MOUNT_NOUUID) == 0) {
786 if (xfs_uuid_mount(mp)) {
787 error = XFS_ERROR(EINVAL);
791 ret64 = uuid_hash64(&sbp->sb_uuid);
792 memcpy(&vfsp->vfs_fsid, &ret64, sizeof(ret64));
796 * Set the default minimum read and write sizes unless
797 * already specified in a mount option.
798 * We use smaller I/O sizes when the file system
799 * is being used for NFS service (wsync mount option).
801 if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)) {
802 if (mp->m_flags & XFS_MOUNT_WSYNC) {
803 readio_log = XFS_WSYNC_READIO_LOG;
804 writeio_log = XFS_WSYNC_WRITEIO_LOG;
806 readio_log = XFS_READIO_LOG_LARGE;
807 writeio_log = XFS_WRITEIO_LOG_LARGE;
810 readio_log = mp->m_readio_log;
811 writeio_log = mp->m_writeio_log;
815 * Set the number of readahead buffers to use based on
816 * physical memory size.
818 if (xfs_physmem <= 4096) /* <= 16MB */
819 mp->m_nreadaheads = XFS_RW_NREADAHEAD_16MB;
820 else if (xfs_physmem <= 8192) /* <= 32MB */
821 mp->m_nreadaheads = XFS_RW_NREADAHEAD_32MB;
823 mp->m_nreadaheads = XFS_RW_NREADAHEAD_K32;
824 if (sbp->sb_blocklog > readio_log) {
825 mp->m_readio_log = sbp->sb_blocklog;
827 mp->m_readio_log = readio_log;
829 mp->m_readio_blocks = 1 << (mp->m_readio_log - sbp->sb_blocklog);
830 if (sbp->sb_blocklog > writeio_log) {
831 mp->m_writeio_log = sbp->sb_blocklog;
833 mp->m_writeio_log = writeio_log;
835 mp->m_writeio_blocks = 1 << (mp->m_writeio_log - sbp->sb_blocklog);
838 * Set the inode cluster size based on the physical memory
839 * size. This may still be overridden by the file system
840 * block size if it is larger than the chosen cluster size.
842 if (xfs_physmem <= btoc(32 * 1024 * 1024)) { /* <= 32 MB */
843 mp->m_inode_cluster_size = XFS_INODE_SMALL_CLUSTER_SIZE;
845 mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE;
848 * Set whether we're using inode alignment.
850 if (XFS_SB_VERSION_HASALIGN(&mp->m_sb) &&
851 mp->m_sb.sb_inoalignmt >=
852 XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size))
853 mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1;
855 mp->m_inoalign_mask = 0;
857 * If we are using stripe alignment, check whether
858 * the stripe unit is a multiple of the inode alignment
860 if (mp->m_dalign && mp->m_inoalign_mask &&
861 !(mp->m_dalign & mp->m_inoalign_mask))
862 mp->m_sinoalign = mp->m_dalign;
866 * Check that the data (and log if separate) are an ok size.
868 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
869 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
870 cmn_err(CE_WARN, "XFS: size check 1 failed");
871 error = XFS_ERROR(E2BIG);
874 error = xfs_read_buf(mp, mp->m_ddev_targp,
875 d - XFS_FSS_TO_BB(mp, 1),
876 XFS_FSS_TO_BB(mp, 1), 0, &bp);
880 cmn_err(CE_WARN, "XFS: size check 2 failed");
881 if (error == ENOSPC) {
882 error = XFS_ERROR(E2BIG);
887 if (((mfsi_flags & XFS_MFSI_CLIENT) == 0) &&
888 mp->m_logdev_targp != mp->m_ddev_targp) {
889 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
890 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) {
891 cmn_err(CE_WARN, "XFS: size check 3 failed");
892 error = XFS_ERROR(E2BIG);
895 error = xfs_read_buf(mp, mp->m_logdev_targp,
896 d - XFS_FSB_TO_BB(mp, 1),
897 XFS_FSB_TO_BB(mp, 1), 0, &bp);
901 cmn_err(CE_WARN, "XFS: size check 3 failed");
902 if (error == ENOSPC) {
903 error = XFS_ERROR(E2BIG);
910 * Initialize realtime fields in the mount structure
912 if ((error = xfs_rtmount_init(mp))) {
913 cmn_err(CE_WARN, "XFS: RT mount failed");
918 * For client case we are done now
920 if (mfsi_flags & XFS_MFSI_CLIENT) {
925 * Copies the low order bits of the timestamp and the randomly
926 * set "sequence" number out of a UUID.
928 uuid_getnodeuniq(&sbp->sb_uuid, mp->m_fixedfsid);
931 * The vfs structure needs to have a file system independent
932 * way of checking for the invariant file system ID. Since it
933 * can't look at mount structures it has a pointer to the data
934 * in the mount structure.
936 * File systems that don't support user level file handles (i.e.
937 * all of them except for XFS) will leave vfs_altfsid as NULL.
939 vfsp->vfs_altfsid = (xfs_fsid_t *)mp->m_fixedfsid;
940 mp->m_dmevmask = 0; /* not persistent; set after each mount */
943 * Select the right directory manager.
946 XFS_SB_VERSION_HASDIRV2(&mp->m_sb) ?
951 * Initialize directory manager's entries.
956 * Initialize the attribute manager's entries.
958 mp->m_attr_magicpct = (mp->m_sb.sb_blocksize * 37) / 100;
961 * Initialize the precomputed transaction reservations values.
966 * Allocate and initialize the inode hash table for this
973 * Allocate and initialize the per-ag data.
975 init_rwsem(&mp->m_peraglock);
977 kmem_zalloc(sbp->sb_agcount * sizeof(xfs_perag_t), KM_SLEEP);
979 mp->m_maxagi = xfs_initialize_perag(vfsp, mp, sbp->sb_agcount);
982 * log's mount-time initialization. Perform 1st part recovery if needed
984 if (likely(sbp->sb_logblocks > 0)) { /* check for volume case */
985 error = xfs_log_mount(mp, mp->m_logdev_targp,
986 XFS_FSB_TO_DADDR(mp, sbp->sb_logstart),
987 XFS_FSB_TO_BB(mp, sbp->sb_logblocks));
989 cmn_err(CE_WARN, "XFS: log mount failed");
992 } else { /* No log has been defined */
993 cmn_err(CE_WARN, "XFS: no log defined");
994 XFS_ERROR_REPORT("xfs_mountfs_int(1)", XFS_ERRLEVEL_LOW, mp);
995 error = XFS_ERROR(EFSCORRUPTED);
1000 * Get and sanity-check the root inode.
1001 * Save the pointer to it in the mount structure.
1003 error = xfs_iget(mp, NULL, sbp->sb_rootino, 0, XFS_ILOCK_EXCL, &rip, 0);
1005 cmn_err(CE_WARN, "XFS: failed to read root inode");
1009 ASSERT(rip != NULL);
1010 rvp = XFS_ITOV(rip);
1012 if (unlikely((rip->i_d.di_mode & S_IFMT) != S_IFDIR)) {
1013 cmn_err(CE_WARN, "XFS: corrupted root inode");
1014 prdev("Root inode %llu is not a directory",
1015 mp->m_ddev_targp, (unsigned long long)rip->i_ino);
1016 xfs_iunlock(rip, XFS_ILOCK_EXCL);
1017 XFS_ERROR_REPORT("xfs_mountfs_int(2)", XFS_ERRLEVEL_LOW,
1019 error = XFS_ERROR(EFSCORRUPTED);
1022 mp->m_rootip = rip; /* save it */
1024 xfs_iunlock(rip, XFS_ILOCK_EXCL);
1027 * Initialize realtime inode pointers in the mount structure
1029 if ((error = xfs_rtmount_inodes(mp))) {
1031 * Free up the root inode.
1033 cmn_err(CE_WARN, "XFS: failed to read RT inodes");
1038 * If fs is not mounted readonly, then update the superblock
1039 * unit and width changes.
1041 if (update_flags && !(vfsp->vfs_flag & VFS_RDONLY))
1042 xfs_mount_log_sbunit(mp, update_flags);
1045 * Initialise the XFS quota management subsystem for this mount
1047 if ((error = XFS_QM_INIT(mp, "amount, "aflags)))
1051 * Finish recovering the file system. This part needed to be
1052 * delayed until after the root and real-time bitmap inodes
1053 * were consistently read in.
1055 error = xfs_log_mount_finish(mp, mfsi_flags);
1057 cmn_err(CE_WARN, "XFS: log mount finish failed");
1062 * Complete the quota initialisation, post-log-replay component.
1064 if ((error = XFS_QM_MOUNT(mp, quotamount, quotaflags, mfsi_flags)))
1071 * Free up the root inode.
1075 xfs_log_unmount_dealloc(mp);
1079 for (agno = 0; agno < sbp->sb_agcount; agno++)
1080 if (mp->m_perag[agno].pagb_list)
1081 kmem_free(mp->m_perag[agno].pagb_list,
1082 sizeof(xfs_perag_busy_t) * XFS_PAGB_NUM_SLOTS);
1083 kmem_free(mp->m_perag, sbp->sb_agcount * sizeof(xfs_perag_t));
1088 xfs_uuid_unmount(mp);
1096 * This flushes out the inodes,dquots and the superblock, unmounts the
1097 * log and makes sure that incore structures are freed.
1100 xfs_unmountfs(xfs_mount_t *mp, struct cred *cr)
1102 struct vfs *vfsp = XFS_MTOVFS(mp);
1103 #if defined(DEBUG) || defined(INDUCE_IO_ERROR)
1109 XFS_QM_DQPURGEALL(mp, XFS_QMOPT_QUOTALL | XFS_QMOPT_UMOUNTING);
1112 * Flush out the log synchronously so that we know for sure
1113 * that nothing is pinned. This is important because bflush()
1114 * will skip pinned buffers.
1116 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
1118 xfs_binval(mp->m_ddev_targp);
1119 if (mp->m_rtdev_targp) {
1120 xfs_binval(mp->m_rtdev_targp);
1123 xfs_unmountfs_writesb(mp);
1125 xfs_unmountfs_wait(mp); /* wait for async bufs */
1127 xfs_log_unmount(mp); /* Done! No more fs ops. */
1132 * All inodes from this mount point should be freed.
1134 ASSERT(mp->m_inodes == NULL);
1136 xfs_unmountfs_close(mp, cr);
1137 if ((mp->m_flags & XFS_MOUNT_NOUUID) == 0)
1138 xfs_uuid_unmount(mp);
1140 #if defined(DEBUG) || defined(INDUCE_IO_ERROR)
1142 * clear all error tags on this filesystem
1144 memcpy(&fsid, &vfsp->vfs_fsid, sizeof(int64_t));
1145 xfs_errortag_clearall_umount(fsid, mp->m_fsname, 0);
1148 xfs_mount_free(mp, 1);
1153 xfs_unmountfs_close(xfs_mount_t *mp, struct cred *cr)
1155 if (mp->m_logdev_targp != mp->m_ddev_targp)
1156 xfs_free_buftarg(mp->m_logdev_targp, 1);
1157 if (mp->m_rtdev_targp)
1158 xfs_free_buftarg(mp->m_rtdev_targp, 1);
1159 xfs_free_buftarg(mp->m_ddev_targp, 0);
1163 xfs_unmountfs_wait(xfs_mount_t *mp)
1165 if (mp->m_logdev_targp != mp->m_ddev_targp)
1166 xfs_wait_buftarg(mp->m_logdev_targp);
1167 if (mp->m_rtdev_targp)
1168 xfs_wait_buftarg(mp->m_rtdev_targp);
1169 xfs_wait_buftarg(mp->m_ddev_targp);
1173 xfs_unmountfs_writesb(xfs_mount_t *mp)
1180 * skip superblock write if fs is read-only, or
1181 * if we are doing a forced umount.
1183 sbp = xfs_getsb(mp, 0);
1184 if (!(XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY ||
1185 XFS_FORCED_SHUTDOWN(mp))) {
1187 xfs_icsb_sync_counters(mp);
1190 * mark shared-readonly if desired
1192 sb = XFS_BUF_TO_SBP(sbp);
1193 if (mp->m_mk_sharedro) {
1194 if (!(sb->sb_flags & XFS_SBF_READONLY))
1195 sb->sb_flags |= XFS_SBF_READONLY;
1196 if (!XFS_SB_VERSION_HASSHARED(sb))
1197 XFS_SB_VERSION_ADDSHARED(sb);
1198 xfs_fs_cmn_err(CE_NOTE, mp,
1199 "Unmounting, marking shared read-only");
1201 XFS_BUF_UNDONE(sbp);
1202 XFS_BUF_UNREAD(sbp);
1203 XFS_BUF_UNDELAYWRITE(sbp);
1205 XFS_BUF_UNASYNC(sbp);
1206 ASSERT(XFS_BUF_TARGET(sbp) == mp->m_ddev_targp);
1207 xfsbdstrat(mp, sbp);
1208 /* Nevermind errors we might get here. */
1209 error = xfs_iowait(sbp);
1211 xfs_ioerror_alert("xfs_unmountfs_writesb",
1212 mp, sbp, XFS_BUF_ADDR(sbp));
1213 if (error && mp->m_mk_sharedro)
1214 xfs_fs_cmn_err(CE_ALERT, mp, "Superblock write error detected while unmounting. Filesystem may not be marked shared readonly");
1221 * xfs_mod_sb() can be used to copy arbitrary changes to the
1222 * in-core superblock into the superblock buffer to be logged.
1223 * It does not provide the higher level of locking that is
1224 * needed to protect the in-core superblock from concurrent
1228 xfs_mod_sb(xfs_trans_t *tp, __int64_t fields)
1241 bp = xfs_trans_getsb(tp, mp, 0);
1242 sbp = XFS_BUF_TO_SBP(bp);
1243 first = sizeof(xfs_sb_t);
1246 /* translate/copy */
1248 xfs_xlatesb(XFS_BUF_PTR(bp), &(mp->m_sb), -1, fields);
1250 /* find modified range */
1252 f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
1253 ASSERT((1LL << f) & XFS_SB_MOD_BITS);
1254 first = xfs_sb_info[f].offset;
1256 f = (xfs_sb_field_t)xfs_highbit64((__uint64_t)fields);
1257 ASSERT((1LL << f) & XFS_SB_MOD_BITS);
1258 last = xfs_sb_info[f + 1].offset - 1;
1260 xfs_trans_log_buf(tp, bp, first, last);
1263 * xfs_mod_incore_sb_unlocked() is a utility routine common used to apply
1264 * a delta to a specified field in the in-core superblock. Simply
1265 * switch on the field indicated and apply the delta to that field.
1266 * Fields are not allowed to dip below zero, so if the delta would
1267 * do this do not apply it and return EINVAL.
1269 * The SB_LOCK must be held when this routine is called.
1272 xfs_mod_incore_sb_unlocked(xfs_mount_t *mp, xfs_sb_field_t field,
1273 int delta, int rsvd)
1275 int scounter; /* short counter for 32 bit fields */
1276 long long lcounter; /* long counter for 64 bit fields */
1277 long long res_used, rem;
1280 * With the in-core superblock spin lock held, switch
1281 * on the indicated field. Apply the delta to the
1282 * proper field. If the fields value would dip below
1283 * 0, then do not apply the delta and return EINVAL.
1286 case XFS_SBS_ICOUNT:
1287 lcounter = (long long)mp->m_sb.sb_icount;
1291 return XFS_ERROR(EINVAL);
1293 mp->m_sb.sb_icount = lcounter;
1296 lcounter = (long long)mp->m_sb.sb_ifree;
1300 return XFS_ERROR(EINVAL);
1302 mp->m_sb.sb_ifree = lcounter;
1304 case XFS_SBS_FDBLOCKS:
1306 lcounter = (long long)mp->m_sb.sb_fdblocks;
1307 res_used = (long long)(mp->m_resblks - mp->m_resblks_avail);
1309 if (delta > 0) { /* Putting blocks back */
1310 if (res_used > delta) {
1311 mp->m_resblks_avail += delta;
1313 rem = delta - res_used;
1314 mp->m_resblks_avail = mp->m_resblks;
1317 } else { /* Taking blocks away */
1322 * If were out of blocks, use any available reserved blocks if
1328 lcounter = (long long)mp->m_resblks_avail + delta;
1330 return XFS_ERROR(ENOSPC);
1332 mp->m_resblks_avail = lcounter;
1334 } else { /* not reserved */
1335 return XFS_ERROR(ENOSPC);
1340 mp->m_sb.sb_fdblocks = lcounter;
1342 case XFS_SBS_FREXTENTS:
1343 lcounter = (long long)mp->m_sb.sb_frextents;
1346 return XFS_ERROR(ENOSPC);
1348 mp->m_sb.sb_frextents = lcounter;
1350 case XFS_SBS_DBLOCKS:
1351 lcounter = (long long)mp->m_sb.sb_dblocks;
1355 return XFS_ERROR(EINVAL);
1357 mp->m_sb.sb_dblocks = lcounter;
1359 case XFS_SBS_AGCOUNT:
1360 scounter = mp->m_sb.sb_agcount;
1364 return XFS_ERROR(EINVAL);
1366 mp->m_sb.sb_agcount = scounter;
1368 case XFS_SBS_IMAX_PCT:
1369 scounter = mp->m_sb.sb_imax_pct;
1373 return XFS_ERROR(EINVAL);
1375 mp->m_sb.sb_imax_pct = scounter;
1377 case XFS_SBS_REXTSIZE:
1378 scounter = mp->m_sb.sb_rextsize;
1382 return XFS_ERROR(EINVAL);
1384 mp->m_sb.sb_rextsize = scounter;
1386 case XFS_SBS_RBMBLOCKS:
1387 scounter = mp->m_sb.sb_rbmblocks;
1391 return XFS_ERROR(EINVAL);
1393 mp->m_sb.sb_rbmblocks = scounter;
1395 case XFS_SBS_RBLOCKS:
1396 lcounter = (long long)mp->m_sb.sb_rblocks;
1400 return XFS_ERROR(EINVAL);
1402 mp->m_sb.sb_rblocks = lcounter;
1404 case XFS_SBS_REXTENTS:
1405 lcounter = (long long)mp->m_sb.sb_rextents;
1409 return XFS_ERROR(EINVAL);
1411 mp->m_sb.sb_rextents = lcounter;
1413 case XFS_SBS_REXTSLOG:
1414 scounter = mp->m_sb.sb_rextslog;
1418 return XFS_ERROR(EINVAL);
1420 mp->m_sb.sb_rextslog = scounter;
1424 return XFS_ERROR(EINVAL);
1429 * xfs_mod_incore_sb() is used to change a field in the in-core
1430 * superblock structure by the specified delta. This modification
1431 * is protected by the SB_LOCK. Just use the xfs_mod_incore_sb_unlocked()
1432 * routine to do the work.
1435 xfs_mod_incore_sb(xfs_mount_t *mp, xfs_sb_field_t field, int delta, int rsvd)
1440 /* check for per-cpu counters */
1442 #ifdef HAVE_PERCPU_SB
1443 case XFS_SBS_ICOUNT:
1445 case XFS_SBS_FDBLOCKS:
1446 if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
1447 status = xfs_icsb_modify_counters(mp, field,
1454 s = XFS_SB_LOCK(mp);
1455 status = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
1456 XFS_SB_UNLOCK(mp, s);
1464 * xfs_mod_incore_sb_batch() is used to change more than one field
1465 * in the in-core superblock structure at a time. This modification
1466 * is protected by a lock internal to this module. The fields and
1467 * changes to those fields are specified in the array of xfs_mod_sb
1468 * structures passed in.
1470 * Either all of the specified deltas will be applied or none of
1471 * them will. If any modified field dips below 0, then all modifications
1472 * will be backed out and EINVAL will be returned.
1475 xfs_mod_incore_sb_batch(xfs_mount_t *mp, xfs_mod_sb_t *msb, uint nmsb, int rsvd)
1482 * Loop through the array of mod structures and apply each
1483 * individually. If any fail, then back out all those
1484 * which have already been applied. Do all of this within
1485 * the scope of the SB_LOCK so that all of the changes will
1488 s = XFS_SB_LOCK(mp);
1490 for (msbp = &msbp[0]; msbp < (msb + nmsb); msbp++) {
1492 * Apply the delta at index n. If it fails, break
1493 * from the loop so we'll fall into the undo loop
1496 switch (msbp->msb_field) {
1497 #ifdef HAVE_PERCPU_SB
1498 case XFS_SBS_ICOUNT:
1500 case XFS_SBS_FDBLOCKS:
1501 if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
1502 status = xfs_icsb_modify_counters_locked(mp,
1504 msbp->msb_delta, rsvd);
1510 status = xfs_mod_incore_sb_unlocked(mp,
1512 msbp->msb_delta, rsvd);
1522 * If we didn't complete the loop above, then back out
1523 * any changes made to the superblock. If you add code
1524 * between the loop above and here, make sure that you
1525 * preserve the value of status. Loop back until
1526 * we step below the beginning of the array. Make sure
1527 * we don't touch anything back there.
1531 while (msbp >= msb) {
1532 switch (msbp->msb_field) {
1533 #ifdef HAVE_PERCPU_SB
1534 case XFS_SBS_ICOUNT:
1536 case XFS_SBS_FDBLOCKS:
1537 if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
1539 xfs_icsb_modify_counters_locked(mp,
1548 status = xfs_mod_incore_sb_unlocked(mp,
1554 ASSERT(status == 0);
1558 XFS_SB_UNLOCK(mp, s);
1563 * xfs_getsb() is called to obtain the buffer for the superblock.
1564 * The buffer is returned locked and read in from disk.
1565 * The buffer should be released with a call to xfs_brelse().
1567 * If the flags parameter is BUF_TRYLOCK, then we'll only return
1568 * the superblock buffer if it can be locked without sleeping.
1569 * If it can't then we'll return NULL.
1578 ASSERT(mp->m_sb_bp != NULL);
1580 if (flags & XFS_BUF_TRYLOCK) {
1581 if (!XFS_BUF_CPSEMA(bp)) {
1585 XFS_BUF_PSEMA(bp, PRIBIO);
1588 ASSERT(XFS_BUF_ISDONE(bp));
1593 * Used to free the superblock along various error paths.
1602 * Use xfs_getsb() so that the buffer will be locked
1603 * when we call xfs_buf_relse().
1605 bp = xfs_getsb(mp, 0);
1606 XFS_BUF_UNMANAGE(bp);
1612 * See if the UUID is unique among mounted XFS filesystems.
1613 * Mount fails if UUID is nil or a FS with the same UUID is already mounted.
1619 if (uuid_is_nil(&mp->m_sb.sb_uuid)) {
1621 "XFS: Filesystem %s has nil UUID - can't mount",
1625 if (!uuid_table_insert(&mp->m_sb.sb_uuid)) {
1627 "XFS: Filesystem %s has duplicate UUID - can't mount",
1635 * Remove filesystem from the UUID table.
1641 uuid_table_remove(&mp->m_sb.sb_uuid);
1645 * Used to log changes to the superblock unit and width fields which could
1646 * be altered by the mount options. Only the first superblock is updated.
1649 xfs_mount_log_sbunit(
1655 ASSERT(fields & (XFS_SB_UNIT|XFS_SB_WIDTH|XFS_SB_UUID));
1657 tp = xfs_trans_alloc(mp, XFS_TRANS_SB_UNIT);
1658 if (xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
1659 XFS_DEFAULT_LOG_COUNT)) {
1660 xfs_trans_cancel(tp, 0);
1663 xfs_mod_sb(tp, fields);
1664 xfs_trans_commit(tp, 0, NULL);
1668 #ifdef HAVE_PERCPU_SB
1670 * Per-cpu incore superblock counters
1672 * Simple concept, difficult implementation
1674 * Basically, replace the incore superblock counters with a distributed per cpu
1675 * counter for contended fields (e.g. free block count).
1677 * Difficulties arise in that the incore sb is used for ENOSPC checking, and
1678 * hence needs to be accurately read when we are running low on space. Hence
1679 * there is a method to enable and disable the per-cpu counters based on how
1680 * much "stuff" is available in them.
1682 * Basically, a counter is enabled if there is enough free resource to justify
1683 * running a per-cpu fast-path. If the per-cpu counter runs out (i.e. a local
1684 * ENOSPC), then we disable the counters to synchronise all callers and
1685 * re-distribute the available resources.
1687 * If, once we redistributed the available resources, we still get a failure,
1688 * we disable the per-cpu counter and go through the slow path.
1690 * The slow path is the current xfs_mod_incore_sb() function. This means that
1691 * when we disable a per-cpu counter, we need to drain it's resources back to
1692 * the global superblock. We do this after disabling the counter to prevent
1693 * more threads from queueing up on the counter.
1695 * Essentially, this means that we still need a lock in the fast path to enable
1696 * synchronisation between the global counters and the per-cpu counters. This
1697 * is not a problem because the lock will be local to a CPU almost all the time
1698 * and have little contention except when we get to ENOSPC conditions.
1700 * Basically, this lock becomes a barrier that enables us to lock out the fast
1701 * path while we do things like enabling and disabling counters and
1702 * synchronising the counters.
1706 * 1. XFS_SB_LOCK() before picking up per-cpu locks
1707 * 2. per-cpu locks always picked up via for_each_online_cpu() order
1708 * 3. accurate counter sync requires XFS_SB_LOCK + per cpu locks
1709 * 4. modifying per-cpu counters requires holding per-cpu lock
1710 * 5. modifying global counters requires holding XFS_SB_LOCK
1711 * 6. enabling or disabling a counter requires holding the XFS_SB_LOCK
1712 * and _none_ of the per-cpu locks.
1714 * Disabled counters are only ever re-enabled by a balance operation
1715 * that results in more free resources per CPU than a given threshold.
1716 * To ensure counters don't remain disabled, they are rebalanced when
1717 * the global resource goes above a higher threshold (i.e. some hysteresis
1718 * is present to prevent thrashing).
1720 * Note: hotplug CPUs not yet supported
1723 xfs_icsb_init_counters(
1726 xfs_icsb_cnts_t *cntp;
1729 mp->m_sb_cnts = alloc_percpu(xfs_icsb_cnts_t);
1730 if (mp->m_sb_cnts == NULL)
1733 for_each_online_cpu(i) {
1734 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
1735 spin_lock_init(&cntp->icsb_lock);
1738 * start with all counters disabled so that the
1739 * initial balance kicks us off correctly
1741 mp->m_icsb_counters = -1;
1746 xfs_icsb_destroy_counters(
1750 free_percpu(mp->m_sb_cnts);
1755 xfs_icsb_lock_all_counters(
1758 xfs_icsb_cnts_t *cntp;
1761 for_each_online_cpu(i) {
1762 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
1763 spin_lock(&cntp->icsb_lock);
1768 xfs_icsb_unlock_all_counters(
1771 xfs_icsb_cnts_t *cntp;
1774 for_each_online_cpu(i) {
1775 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
1776 spin_unlock(&cntp->icsb_lock);
1783 xfs_icsb_cnts_t *cnt,
1786 xfs_icsb_cnts_t *cntp;
1789 memset(cnt, 0, sizeof(xfs_icsb_cnts_t));
1791 if (!(flags & XFS_ICSB_LAZY_COUNT))
1792 xfs_icsb_lock_all_counters(mp);
1794 for_each_online_cpu(i) {
1795 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
1796 cnt->icsb_icount += cntp->icsb_icount;
1797 cnt->icsb_ifree += cntp->icsb_ifree;
1798 cnt->icsb_fdblocks += cntp->icsb_fdblocks;
1801 if (!(flags & XFS_ICSB_LAZY_COUNT))
1802 xfs_icsb_unlock_all_counters(mp);
1806 xfs_icsb_counter_disabled(
1808 xfs_sb_field_t field)
1810 ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
1811 return test_bit(field, &mp->m_icsb_counters);
1815 xfs_icsb_disable_counter(
1817 xfs_sb_field_t field)
1819 xfs_icsb_cnts_t cnt;
1821 ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
1823 xfs_icsb_lock_all_counters(mp);
1824 if (!test_and_set_bit(field, &mp->m_icsb_counters)) {
1825 /* drain back to superblock */
1827 xfs_icsb_count(mp, &cnt, XFS_ICSB_SB_LOCKED|XFS_ICSB_LAZY_COUNT);
1829 case XFS_SBS_ICOUNT:
1830 mp->m_sb.sb_icount = cnt.icsb_icount;
1833 mp->m_sb.sb_ifree = cnt.icsb_ifree;
1835 case XFS_SBS_FDBLOCKS:
1836 mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
1843 xfs_icsb_unlock_all_counters(mp);
1849 xfs_icsb_enable_counter(
1851 xfs_sb_field_t field,
1855 xfs_icsb_cnts_t *cntp;
1858 ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
1860 xfs_icsb_lock_all_counters(mp);
1861 for_each_online_cpu(i) {
1862 cntp = per_cpu_ptr(mp->m_sb_cnts, i);
1864 case XFS_SBS_ICOUNT:
1865 cntp->icsb_icount = count + resid;
1868 cntp->icsb_ifree = count + resid;
1870 case XFS_SBS_FDBLOCKS:
1871 cntp->icsb_fdblocks = count + resid;
1879 clear_bit(field, &mp->m_icsb_counters);
1880 xfs_icsb_unlock_all_counters(mp);
1884 xfs_icsb_sync_counters_int(
1888 xfs_icsb_cnts_t cnt;
1891 /* Pass 1: lock all counters */
1892 if ((flags & XFS_ICSB_SB_LOCKED) == 0)
1893 s = XFS_SB_LOCK(mp);
1895 xfs_icsb_count(mp, &cnt, flags);
1897 /* Step 3: update mp->m_sb fields */
1898 if (!xfs_icsb_counter_disabled(mp, XFS_SBS_ICOUNT))
1899 mp->m_sb.sb_icount = cnt.icsb_icount;
1900 if (!xfs_icsb_counter_disabled(mp, XFS_SBS_IFREE))
1901 mp->m_sb.sb_ifree = cnt.icsb_ifree;
1902 if (!xfs_icsb_counter_disabled(mp, XFS_SBS_FDBLOCKS))
1903 mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
1905 if ((flags & XFS_ICSB_SB_LOCKED) == 0)
1906 XFS_SB_UNLOCK(mp, s);
1910 * Accurate update of per-cpu counters to incore superblock
1913 xfs_icsb_sync_counters(
1916 xfs_icsb_sync_counters_int(mp, 0);
1920 * lazy addition used for things like df, background sb syncs, etc
1923 xfs_icsb_sync_counters_lazy(
1926 xfs_icsb_sync_counters_int(mp, XFS_ICSB_LAZY_COUNT);
1930 * Balance and enable/disable counters as necessary.
1932 * Thresholds for re-enabling counters are somewhat magic.
1933 * inode counts are chosen to be the same number as single
1934 * on disk allocation chunk per CPU, and free blocks is
1935 * something far enough zero that we aren't going thrash
1936 * when we get near ENOSPC.
1938 #define XFS_ICSB_INO_CNTR_REENABLE 64
1939 #define XFS_ICSB_FDBLK_CNTR_REENABLE 512
1941 xfs_icsb_balance_counter(
1943 xfs_sb_field_t field,
1946 uint64_t count, resid = 0;
1947 int weight = num_online_cpus();
1950 if (!(flags & XFS_ICSB_SB_LOCKED))
1951 s = XFS_SB_LOCK(mp);
1953 /* disable counter and sync counter */
1954 xfs_icsb_disable_counter(mp, field);
1956 /* update counters - first CPU gets residual*/
1958 case XFS_SBS_ICOUNT:
1959 count = mp->m_sb.sb_icount;
1960 resid = do_div(count, weight);
1961 if (count < XFS_ICSB_INO_CNTR_REENABLE)
1965 count = mp->m_sb.sb_ifree;
1966 resid = do_div(count, weight);
1967 if (count < XFS_ICSB_INO_CNTR_REENABLE)
1970 case XFS_SBS_FDBLOCKS:
1971 count = mp->m_sb.sb_fdblocks;
1972 resid = do_div(count, weight);
1973 if (count < XFS_ICSB_FDBLK_CNTR_REENABLE)
1981 xfs_icsb_enable_counter(mp, field, count, resid);
1983 if (!(flags & XFS_ICSB_SB_LOCKED))
1984 XFS_SB_UNLOCK(mp, s);
1988 xfs_icsb_modify_counters_int(
1990 xfs_sb_field_t field,
1995 xfs_icsb_cnts_t *icsbp;
1996 long long lcounter; /* long counter for 64 bit fields */
1997 int cpu, s, locked = 0;
1998 int ret = 0, balance_done = 0;
2002 icsbp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, cpu),
2003 spin_lock(&icsbp->icsb_lock);
2004 if (unlikely(xfs_icsb_counter_disabled(mp, field)))
2008 case XFS_SBS_ICOUNT:
2009 lcounter = icsbp->icsb_icount;
2011 if (unlikely(lcounter < 0))
2013 icsbp->icsb_icount = lcounter;
2017 lcounter = icsbp->icsb_ifree;
2019 if (unlikely(lcounter < 0))
2021 icsbp->icsb_ifree = lcounter;
2024 case XFS_SBS_FDBLOCKS:
2025 BUG_ON((mp->m_resblks - mp->m_resblks_avail) != 0);
2027 lcounter = icsbp->icsb_fdblocks;
2029 if (unlikely(lcounter < 0))
2031 icsbp->icsb_fdblocks = lcounter;
2037 spin_unlock(&icsbp->icsb_lock);
2040 XFS_SB_UNLOCK(mp, s);
2044 * The slow path needs to be run with the SBLOCK
2045 * held so that we prevent other threads from
2046 * attempting to run this path at the same time.
2047 * this provides exclusion for the balancing code,
2048 * and exclusive fallback if the balance does not
2049 * provide enough resources to continue in an unlocked
2053 spin_unlock(&icsbp->icsb_lock);
2056 /* need to hold superblock incase we need
2057 * to disable a counter */
2058 if (!(flags & XFS_ICSB_SB_LOCKED)) {
2059 s = XFS_SB_LOCK(mp);
2061 flags |= XFS_ICSB_SB_LOCKED;
2063 if (!balance_done) {
2064 xfs_icsb_balance_counter(mp, field, flags);
2069 * we might not have enough on this local
2070 * cpu to allocate for a bulk request.
2071 * We need to drain this field from all CPUs
2072 * and disable the counter fastpath
2074 xfs_icsb_disable_counter(mp, field);
2077 ret = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
2080 XFS_SB_UNLOCK(mp, s);
2085 xfs_icsb_modify_counters(
2087 xfs_sb_field_t field,
2091 return xfs_icsb_modify_counters_int(mp, field, delta, rsvd, 0);
2095 * Called when superblock is already locked
2098 xfs_icsb_modify_counters_locked(
2100 xfs_sb_field_t field,
2104 return xfs_icsb_modify_counters_int(mp, field, delta,
2105 rsvd, XFS_ICSB_SB_LOCKED);