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,
63 STATIC int xfs_icsb_disable_counter(xfs_mount_t *, xfs_sb_field_t);
67 #define xfs_icsb_destroy_counters(mp) do { } while (0)
68 #define xfs_icsb_balance_counter(mp, a, b) do { } while (0)
69 #define xfs_icsb_sync_counters(mp) do { } while (0)
70 #define xfs_icsb_modify_counters(mp, a, b, c) do { } while (0)
71 #define xfs_icsb_modify_counters_locked(mp, a, b, c) do { } while (0)
77 short type; /* 0 = integer
78 * 1 = binary / string (no translation)
81 { offsetof(xfs_sb_t, sb_magicnum), 0 },
82 { offsetof(xfs_sb_t, sb_blocksize), 0 },
83 { offsetof(xfs_sb_t, sb_dblocks), 0 },
84 { offsetof(xfs_sb_t, sb_rblocks), 0 },
85 { offsetof(xfs_sb_t, sb_rextents), 0 },
86 { offsetof(xfs_sb_t, sb_uuid), 1 },
87 { offsetof(xfs_sb_t, sb_logstart), 0 },
88 { offsetof(xfs_sb_t, sb_rootino), 0 },
89 { offsetof(xfs_sb_t, sb_rbmino), 0 },
90 { offsetof(xfs_sb_t, sb_rsumino), 0 },
91 { offsetof(xfs_sb_t, sb_rextsize), 0 },
92 { offsetof(xfs_sb_t, sb_agblocks), 0 },
93 { offsetof(xfs_sb_t, sb_agcount), 0 },
94 { offsetof(xfs_sb_t, sb_rbmblocks), 0 },
95 { offsetof(xfs_sb_t, sb_logblocks), 0 },
96 { offsetof(xfs_sb_t, sb_versionnum), 0 },
97 { offsetof(xfs_sb_t, sb_sectsize), 0 },
98 { offsetof(xfs_sb_t, sb_inodesize), 0 },
99 { offsetof(xfs_sb_t, sb_inopblock), 0 },
100 { offsetof(xfs_sb_t, sb_fname[0]), 1 },
101 { offsetof(xfs_sb_t, sb_blocklog), 0 },
102 { offsetof(xfs_sb_t, sb_sectlog), 0 },
103 { offsetof(xfs_sb_t, sb_inodelog), 0 },
104 { offsetof(xfs_sb_t, sb_inopblog), 0 },
105 { offsetof(xfs_sb_t, sb_agblklog), 0 },
106 { offsetof(xfs_sb_t, sb_rextslog), 0 },
107 { offsetof(xfs_sb_t, sb_inprogress), 0 },
108 { offsetof(xfs_sb_t, sb_imax_pct), 0 },
109 { offsetof(xfs_sb_t, sb_icount), 0 },
110 { offsetof(xfs_sb_t, sb_ifree), 0 },
111 { offsetof(xfs_sb_t, sb_fdblocks), 0 },
112 { offsetof(xfs_sb_t, sb_frextents), 0 },
113 { offsetof(xfs_sb_t, sb_uquotino), 0 },
114 { offsetof(xfs_sb_t, sb_gquotino), 0 },
115 { offsetof(xfs_sb_t, sb_qflags), 0 },
116 { offsetof(xfs_sb_t, sb_flags), 0 },
117 { offsetof(xfs_sb_t, sb_shared_vn), 0 },
118 { offsetof(xfs_sb_t, sb_inoalignmt), 0 },
119 { offsetof(xfs_sb_t, sb_unit), 0 },
120 { offsetof(xfs_sb_t, sb_width), 0 },
121 { offsetof(xfs_sb_t, sb_dirblklog), 0 },
122 { offsetof(xfs_sb_t, sb_logsectlog), 0 },
123 { offsetof(xfs_sb_t, sb_logsectsize),0 },
124 { offsetof(xfs_sb_t, sb_logsunit), 0 },
125 { offsetof(xfs_sb_t, sb_features2), 0 },
126 { sizeof(xfs_sb_t), 0 }
130 * Return a pointer to an initialized xfs_mount structure.
137 mp = kmem_zalloc(sizeof(xfs_mount_t), KM_SLEEP);
139 if (xfs_icsb_init_counters(mp)) {
140 mp->m_flags |= XFS_MOUNT_NO_PERCPU_SB;
143 AIL_LOCKINIT(&mp->m_ail_lock, "xfs_ail");
144 spinlock_init(&mp->m_sb_lock, "xfs_sb");
145 mutex_init(&mp->m_ilock);
146 initnsema(&mp->m_growlock, 1, "xfs_grow");
148 * Initialize the AIL.
150 xfs_trans_ail_init(mp);
152 atomic_set(&mp->m_active_trans, 0);
158 * Free up the resources associated with a mount structure. Assume that
159 * the structure was initially zeroed, so we can tell which fields got
175 for (agno = 0; agno < mp->m_maxagi; agno++)
176 if (mp->m_perag[agno].pagb_list)
177 kmem_free(mp->m_perag[agno].pagb_list,
178 sizeof(xfs_perag_busy_t) *
180 kmem_free(mp->m_perag,
181 sizeof(xfs_perag_t) * mp->m_sb.sb_agcount);
184 AIL_LOCK_DESTROY(&mp->m_ail_lock);
185 spinlock_destroy(&mp->m_sb_lock);
186 mutex_destroy(&mp->m_ilock);
187 freesema(&mp->m_growlock);
191 if (mp->m_fsname != NULL)
192 kmem_free(mp->m_fsname, mp->m_fsname_len);
193 if (mp->m_rtname != NULL)
194 kmem_free(mp->m_rtname, strlen(mp->m_rtname) + 1);
195 if (mp->m_logname != NULL)
196 kmem_free(mp->m_logname, strlen(mp->m_logname) + 1);
199 struct vfs *vfsp = XFS_MTOVFS(mp);
201 bhv_remove_all_vfsops(vfsp, 0);
202 VFS_REMOVEBHV(vfsp, &mp->m_bhv);
205 xfs_icsb_destroy_counters(mp);
206 kmem_free(mp, sizeof(xfs_mount_t));
211 * Check the validity of the SB found.
214 xfs_mount_validate_sb(
219 * If the log device and data device have the
220 * same device number, the log is internal.
221 * Consequently, the sb_logstart should be non-zero. If
222 * we have a zero sb_logstart in this case, we may be trying to mount
223 * a volume filesystem in a non-volume manner.
225 if (sbp->sb_magicnum != XFS_SB_MAGIC) {
226 cmn_err(CE_WARN, "XFS: bad magic number");
227 return XFS_ERROR(EWRONGFS);
230 if (!XFS_SB_GOOD_VERSION(sbp)) {
231 cmn_err(CE_WARN, "XFS: bad version");
232 return XFS_ERROR(EWRONGFS);
236 sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
238 "XFS: filesystem is marked as having an external log; "
239 "specify logdev on the\nmount command line.");
240 XFS_CORRUPTION_ERROR("xfs_mount_validate_sb(1)",
241 XFS_ERRLEVEL_HIGH, mp, sbp);
242 return XFS_ERROR(EFSCORRUPTED);
246 sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
248 "XFS: filesystem is marked as having an internal log; "
249 "don't specify logdev on\nthe mount command line.");
250 XFS_CORRUPTION_ERROR("xfs_mount_validate_sb(2)",
251 XFS_ERRLEVEL_HIGH, mp, sbp);
252 return XFS_ERROR(EFSCORRUPTED);
256 * More sanity checking. These were stolen directly from
260 sbp->sb_agcount <= 0 ||
261 sbp->sb_sectsize < XFS_MIN_SECTORSIZE ||
262 sbp->sb_sectsize > XFS_MAX_SECTORSIZE ||
263 sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG ||
264 sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG ||
265 sbp->sb_blocksize < XFS_MIN_BLOCKSIZE ||
266 sbp->sb_blocksize > XFS_MAX_BLOCKSIZE ||
267 sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG ||
268 sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
269 sbp->sb_inodesize < XFS_DINODE_MIN_SIZE ||
270 sbp->sb_inodesize > XFS_DINODE_MAX_SIZE ||
271 sbp->sb_inodelog < XFS_DINODE_MIN_LOG ||
272 sbp->sb_inodelog > XFS_DINODE_MAX_LOG ||
273 (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog) ||
274 (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE) ||
275 (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) ||
276 (sbp->sb_imax_pct > 100 || sbp->sb_imax_pct < 1))) {
277 cmn_err(CE_WARN, "XFS: SB sanity check 1 failed");
278 XFS_CORRUPTION_ERROR("xfs_mount_validate_sb(3)",
279 XFS_ERRLEVEL_LOW, mp, sbp);
280 return XFS_ERROR(EFSCORRUPTED);
284 * Sanity check AG count, size fields against data size field
287 sbp->sb_dblocks == 0 ||
289 (xfs_drfsbno_t)sbp->sb_agcount * sbp->sb_agblocks ||
290 sbp->sb_dblocks < (xfs_drfsbno_t)(sbp->sb_agcount - 1) *
291 sbp->sb_agblocks + XFS_MIN_AG_BLOCKS)) {
292 cmn_err(CE_WARN, "XFS: SB sanity check 2 failed");
293 XFS_ERROR_REPORT("xfs_mount_validate_sb(4)",
294 XFS_ERRLEVEL_LOW, mp);
295 return XFS_ERROR(EFSCORRUPTED);
298 ASSERT(PAGE_SHIFT >= sbp->sb_blocklog);
299 ASSERT(sbp->sb_blocklog >= BBSHIFT);
301 #if XFS_BIG_BLKNOS /* Limited by ULONG_MAX of page cache index */
303 (sbp->sb_dblocks >> (PAGE_SHIFT - sbp->sb_blocklog)) > ULONG_MAX ||
304 (sbp->sb_rblocks >> (PAGE_SHIFT - sbp->sb_blocklog)) > ULONG_MAX)) {
305 #else /* Limited by UINT_MAX of sectors */
307 (sbp->sb_dblocks << (sbp->sb_blocklog - BBSHIFT)) > UINT_MAX ||
308 (sbp->sb_rblocks << (sbp->sb_blocklog - BBSHIFT)) > UINT_MAX)) {
311 "XFS: File system is too large to be mounted on this system.");
312 return XFS_ERROR(E2BIG);
315 if (unlikely(sbp->sb_inprogress)) {
316 cmn_err(CE_WARN, "XFS: file system busy");
317 XFS_ERROR_REPORT("xfs_mount_validate_sb(5)",
318 XFS_ERRLEVEL_LOW, mp);
319 return XFS_ERROR(EFSCORRUPTED);
323 * Version 1 directory format has never worked on Linux.
325 if (unlikely(!XFS_SB_VERSION_HASDIRV2(sbp))) {
327 "XFS: Attempted to mount file system using version 1 directory format");
328 return XFS_ERROR(ENOSYS);
332 * Until this is fixed only page-sized or smaller data blocks work.
334 if (unlikely(sbp->sb_blocksize > PAGE_SIZE)) {
336 "XFS: Attempted to mount file system with blocksize %d bytes",
339 "XFS: Only page-sized (%ld) or less blocksizes currently work.",
341 return XFS_ERROR(ENOSYS);
348 xfs_initialize_perag(
351 xfs_agnumber_t agcount)
353 xfs_agnumber_t index, max_metadata;
357 xfs_sb_t *sbp = &mp->m_sb;
358 xfs_ino_t max_inum = XFS_MAXINUMBER_32;
360 /* Check to see if the filesystem can overflow 32 bit inodes */
361 agino = XFS_OFFBNO_TO_AGINO(mp, sbp->sb_agblocks - 1, 0);
362 ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino);
364 /* Clear the mount flag if no inode can overflow 32 bits
365 * on this filesystem, or if specifically requested..
367 if ((vfs->vfs_flag & VFS_32BITINODES) && ino > max_inum) {
368 mp->m_flags |= XFS_MOUNT_32BITINODES;
370 mp->m_flags &= ~XFS_MOUNT_32BITINODES;
373 /* If we can overflow then setup the ag headers accordingly */
374 if (mp->m_flags & XFS_MOUNT_32BITINODES) {
375 /* Calculate how much should be reserved for inodes to
376 * meet the max inode percentage.
378 if (mp->m_maxicount) {
381 icount = sbp->sb_dblocks * sbp->sb_imax_pct;
383 icount += sbp->sb_agblocks - 1;
384 do_div(icount, sbp->sb_agblocks);
385 max_metadata = icount;
387 max_metadata = agcount;
389 for (index = 0; index < agcount; index++) {
390 ino = XFS_AGINO_TO_INO(mp, index, agino);
391 if (ino > max_inum) {
396 /* This ag is preferred for inodes */
397 pag = &mp->m_perag[index];
398 pag->pagi_inodeok = 1;
399 if (index < max_metadata)
400 pag->pagf_metadata = 1;
403 /* Setup default behavior for smaller filesystems */
404 for (index = 0; index < agcount; index++) {
405 pag = &mp->m_perag[index];
406 pag->pagi_inodeok = 1;
415 * data - on disk version of sb
417 * dir - conversion direction: <0 - convert sb to buf
418 * >0 - convert buf to sb
419 * fields - which fields to copy (bitmask)
440 buf_ptr = (xfs_caddr_t)data;
441 mem_ptr = (xfs_caddr_t)sb;
444 f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
445 first = xfs_sb_info[f].offset;
446 size = xfs_sb_info[f + 1].offset - first;
448 ASSERT(xfs_sb_info[f].type == 0 || xfs_sb_info[f].type == 1);
450 if (size == 1 || xfs_sb_info[f].type == 1) {
452 memcpy(mem_ptr + first, buf_ptr + first, size);
454 memcpy(buf_ptr + first, mem_ptr + first, size);
459 INT_XLATE(*(__uint16_t*)(buf_ptr+first),
460 *(__uint16_t*)(mem_ptr+first),
464 INT_XLATE(*(__uint32_t*)(buf_ptr+first),
465 *(__uint32_t*)(mem_ptr+first),
469 INT_XLATE(*(__uint64_t*)(buf_ptr+first),
470 *(__uint64_t*)(mem_ptr+first), dir, ARCH_CONVERT);
477 fields &= ~(1LL << f);
484 * Does the initial read of the superblock.
487 xfs_readsb(xfs_mount_t *mp)
489 unsigned int sector_size;
490 unsigned int extra_flags;
495 ASSERT(mp->m_sb_bp == NULL);
496 ASSERT(mp->m_ddev_targp != NULL);
499 * Allocate a (locked) buffer to hold the superblock.
500 * This will be kept around at all times to optimize
501 * access to the superblock.
503 sector_size = xfs_getsize_buftarg(mp->m_ddev_targp);
504 extra_flags = XFS_BUF_LOCK | XFS_BUF_MANAGE | XFS_BUF_MAPPED;
506 bp = xfs_buf_read_flags(mp->m_ddev_targp, XFS_SB_DADDR,
507 BTOBB(sector_size), extra_flags);
508 if (!bp || XFS_BUF_ISERROR(bp)) {
509 cmn_err(CE_WARN, "XFS: SB read failed");
510 error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM;
513 ASSERT(XFS_BUF_ISBUSY(bp));
514 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
517 * Initialize the mount structure from the superblock.
518 * But first do some basic consistency checking.
520 sbp = XFS_BUF_TO_SBP(bp);
521 xfs_xlatesb(XFS_BUF_PTR(bp), &(mp->m_sb), 1, XFS_SB_ALL_BITS);
523 error = xfs_mount_validate_sb(mp, &(mp->m_sb));
525 cmn_err(CE_WARN, "XFS: SB validate failed");
530 * We must be able to do sector-sized and sector-aligned IO.
532 if (sector_size > mp->m_sb.sb_sectsize) {
534 "XFS: device supports only %u byte sectors (not %u)",
535 sector_size, mp->m_sb.sb_sectsize);
541 * If device sector size is smaller than the superblock size,
542 * re-read the superblock so the buffer is correctly sized.
544 if (sector_size < mp->m_sb.sb_sectsize) {
545 XFS_BUF_UNMANAGE(bp);
547 sector_size = mp->m_sb.sb_sectsize;
548 bp = xfs_buf_read_flags(mp->m_ddev_targp, XFS_SB_DADDR,
549 BTOBB(sector_size), extra_flags);
550 if (!bp || XFS_BUF_ISERROR(bp)) {
551 cmn_err(CE_WARN, "XFS: SB re-read failed");
552 error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM;
555 ASSERT(XFS_BUF_ISBUSY(bp));
556 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
559 xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
560 xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
561 xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
565 ASSERT(XFS_BUF_VALUSEMA(bp) > 0);
570 XFS_BUF_UNMANAGE(bp);
580 * Mount initialization code establishing various mount
581 * fields from the superblock associated with the given
585 xfs_mount_common(xfs_mount_t *mp, xfs_sb_t *sbp)
589 mp->m_agfrotor = mp->m_agirotor = 0;
590 spinlock_init(&mp->m_agirotor_lock, "m_agirotor_lock");
591 mp->m_maxagi = mp->m_sb.sb_agcount;
592 mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
593 mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
594 mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
595 mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
596 mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
597 mp->m_litino = sbp->sb_inodesize -
598 ((uint)sizeof(xfs_dinode_core_t) + (uint)sizeof(xfs_agino_t));
599 mp->m_blockmask = sbp->sb_blocksize - 1;
600 mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
601 mp->m_blockwmask = mp->m_blockwsize - 1;
602 INIT_LIST_HEAD(&mp->m_del_inodes);
605 * Setup for attributes, in case they get created.
606 * This value is for inodes getting attributes for the first time,
607 * the per-inode value is for old attribute values.
609 ASSERT(sbp->sb_inodesize >= 256 && sbp->sb_inodesize <= 2048);
610 switch (sbp->sb_inodesize) {
612 mp->m_attroffset = XFS_LITINO(mp) -
613 XFS_BMDR_SPACE_CALC(MINABTPTRS);
618 mp->m_attroffset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
623 ASSERT(mp->m_attroffset < XFS_LITINO(mp));
625 for (i = 0; i < 2; i++) {
626 mp->m_alloc_mxr[i] = XFS_BTREE_BLOCK_MAXRECS(sbp->sb_blocksize,
628 mp->m_alloc_mnr[i] = XFS_BTREE_BLOCK_MINRECS(sbp->sb_blocksize,
631 for (i = 0; i < 2; i++) {
632 mp->m_bmap_dmxr[i] = XFS_BTREE_BLOCK_MAXRECS(sbp->sb_blocksize,
634 mp->m_bmap_dmnr[i] = XFS_BTREE_BLOCK_MINRECS(sbp->sb_blocksize,
637 for (i = 0; i < 2; i++) {
638 mp->m_inobt_mxr[i] = XFS_BTREE_BLOCK_MAXRECS(sbp->sb_blocksize,
640 mp->m_inobt_mnr[i] = XFS_BTREE_BLOCK_MINRECS(sbp->sb_blocksize,
644 mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
645 mp->m_ialloc_inos = (int)MAX((__uint16_t)XFS_INODES_PER_CHUNK,
647 mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog;
652 * This function does the following on an initial mount of a file system:
653 * - reads the superblock from disk and init the mount struct
654 * - if we're a 32-bit kernel, do a size check on the superblock
655 * so we don't mount terabyte filesystems
656 * - init mount struct realtime fields
657 * - allocate inode hash table for fs
658 * - init directory manager
659 * - perform recovery and init the log manager
668 xfs_sb_t *sbp = &(mp->m_sb);
671 int readio_log, writeio_log;
674 __int64_t update_flags;
675 uint quotamount, quotaflags;
677 int uuid_mounted = 0;
680 if (mp->m_sb_bp == NULL) {
681 if ((error = xfs_readsb(mp))) {
685 xfs_mount_common(mp, sbp);
688 * Check if sb_agblocks is aligned at stripe boundary
689 * If sb_agblocks is NOT aligned turn off m_dalign since
690 * allocator alignment is within an ag, therefore ag has
691 * to be aligned at stripe boundary.
694 if (mp->m_dalign && !(mfsi_flags & XFS_MFSI_SECOND)) {
696 * If stripe unit and stripe width are not multiples
697 * of the fs blocksize turn off alignment.
699 if ((BBTOB(mp->m_dalign) & mp->m_blockmask) ||
700 (BBTOB(mp->m_swidth) & mp->m_blockmask)) {
701 if (mp->m_flags & XFS_MOUNT_RETERR) {
703 "XFS: alignment check 1 failed");
704 error = XFS_ERROR(EINVAL);
707 mp->m_dalign = mp->m_swidth = 0;
710 * Convert the stripe unit and width to FSBs.
712 mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign);
713 if (mp->m_dalign && (sbp->sb_agblocks % mp->m_dalign)) {
714 if (mp->m_flags & XFS_MOUNT_RETERR) {
715 error = XFS_ERROR(EINVAL);
718 xfs_fs_cmn_err(CE_WARN, mp,
719 "stripe alignment turned off: sunit(%d)/swidth(%d) incompatible with agsize(%d)",
720 mp->m_dalign, mp->m_swidth,
725 } else if (mp->m_dalign) {
726 mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth);
728 if (mp->m_flags & XFS_MOUNT_RETERR) {
729 xfs_fs_cmn_err(CE_WARN, mp,
730 "stripe alignment turned off: sunit(%d) less than bsize(%d)",
733 error = XFS_ERROR(EINVAL);
741 * Update superblock with new values
744 if (XFS_SB_VERSION_HASDALIGN(sbp)) {
745 if (sbp->sb_unit != mp->m_dalign) {
746 sbp->sb_unit = mp->m_dalign;
747 update_flags |= XFS_SB_UNIT;
749 if (sbp->sb_width != mp->m_swidth) {
750 sbp->sb_width = mp->m_swidth;
751 update_flags |= XFS_SB_WIDTH;
754 } else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN &&
755 XFS_SB_VERSION_HASDALIGN(&mp->m_sb)) {
756 mp->m_dalign = sbp->sb_unit;
757 mp->m_swidth = sbp->sb_width;
760 xfs_alloc_compute_maxlevels(mp);
761 xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
762 xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
763 xfs_ialloc_compute_maxlevels(mp);
765 if (sbp->sb_imax_pct) {
768 /* Make sure the maximum inode count is a multiple of the
769 * units we allocate inodes in.
772 icount = sbp->sb_dblocks * sbp->sb_imax_pct;
774 do_div(icount, mp->m_ialloc_blks);
775 mp->m_maxicount = (icount * mp->m_ialloc_blks) <<
780 mp->m_maxioffset = xfs_max_file_offset(sbp->sb_blocklog);
783 * XFS uses the uuid from the superblock as the unique
784 * identifier for fsid. We can not use the uuid from the volume
785 * since a single partition filesystem is identical to a single
786 * partition volume/filesystem.
788 if ((mfsi_flags & XFS_MFSI_SECOND) == 0 &&
789 (mp->m_flags & XFS_MOUNT_NOUUID) == 0) {
790 if (xfs_uuid_mount(mp)) {
791 error = XFS_ERROR(EINVAL);
795 ret64 = uuid_hash64(&sbp->sb_uuid);
796 memcpy(&vfsp->vfs_fsid, &ret64, sizeof(ret64));
800 * Set the default minimum read and write sizes unless
801 * already specified in a mount option.
802 * We use smaller I/O sizes when the file system
803 * is being used for NFS service (wsync mount option).
805 if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)) {
806 if (mp->m_flags & XFS_MOUNT_WSYNC) {
807 readio_log = XFS_WSYNC_READIO_LOG;
808 writeio_log = XFS_WSYNC_WRITEIO_LOG;
810 readio_log = XFS_READIO_LOG_LARGE;
811 writeio_log = XFS_WRITEIO_LOG_LARGE;
814 readio_log = mp->m_readio_log;
815 writeio_log = mp->m_writeio_log;
819 * Set the number of readahead buffers to use based on
820 * physical memory size.
822 if (xfs_physmem <= 4096) /* <= 16MB */
823 mp->m_nreadaheads = XFS_RW_NREADAHEAD_16MB;
824 else if (xfs_physmem <= 8192) /* <= 32MB */
825 mp->m_nreadaheads = XFS_RW_NREADAHEAD_32MB;
827 mp->m_nreadaheads = XFS_RW_NREADAHEAD_K32;
828 if (sbp->sb_blocklog > readio_log) {
829 mp->m_readio_log = sbp->sb_blocklog;
831 mp->m_readio_log = readio_log;
833 mp->m_readio_blocks = 1 << (mp->m_readio_log - sbp->sb_blocklog);
834 if (sbp->sb_blocklog > writeio_log) {
835 mp->m_writeio_log = sbp->sb_blocklog;
837 mp->m_writeio_log = writeio_log;
839 mp->m_writeio_blocks = 1 << (mp->m_writeio_log - sbp->sb_blocklog);
842 * Set the inode cluster size based on the physical memory
843 * size. This may still be overridden by the file system
844 * block size if it is larger than the chosen cluster size.
846 if (xfs_physmem <= btoc(32 * 1024 * 1024)) { /* <= 32 MB */
847 mp->m_inode_cluster_size = XFS_INODE_SMALL_CLUSTER_SIZE;
849 mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE;
852 * Set whether we're using inode alignment.
854 if (XFS_SB_VERSION_HASALIGN(&mp->m_sb) &&
855 mp->m_sb.sb_inoalignmt >=
856 XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size))
857 mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1;
859 mp->m_inoalign_mask = 0;
861 * If we are using stripe alignment, check whether
862 * the stripe unit is a multiple of the inode alignment
864 if (mp->m_dalign && mp->m_inoalign_mask &&
865 !(mp->m_dalign & mp->m_inoalign_mask))
866 mp->m_sinoalign = mp->m_dalign;
870 * Check that the data (and log if separate) are an ok size.
872 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
873 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
874 cmn_err(CE_WARN, "XFS: size check 1 failed");
875 error = XFS_ERROR(E2BIG);
878 error = xfs_read_buf(mp, mp->m_ddev_targp,
879 d - XFS_FSS_TO_BB(mp, 1),
880 XFS_FSS_TO_BB(mp, 1), 0, &bp);
884 cmn_err(CE_WARN, "XFS: size check 2 failed");
885 if (error == ENOSPC) {
886 error = XFS_ERROR(E2BIG);
891 if (((mfsi_flags & XFS_MFSI_CLIENT) == 0) &&
892 mp->m_logdev_targp != mp->m_ddev_targp) {
893 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
894 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) {
895 cmn_err(CE_WARN, "XFS: size check 3 failed");
896 error = XFS_ERROR(E2BIG);
899 error = xfs_read_buf(mp, mp->m_logdev_targp,
900 d - XFS_FSB_TO_BB(mp, 1),
901 XFS_FSB_TO_BB(mp, 1), 0, &bp);
905 cmn_err(CE_WARN, "XFS: size check 3 failed");
906 if (error == ENOSPC) {
907 error = XFS_ERROR(E2BIG);
914 * Initialize realtime fields in the mount structure
916 if ((error = xfs_rtmount_init(mp))) {
917 cmn_err(CE_WARN, "XFS: RT mount failed");
922 * For client case we are done now
924 if (mfsi_flags & XFS_MFSI_CLIENT) {
929 * Copies the low order bits of the timestamp and the randomly
930 * set "sequence" number out of a UUID.
932 uuid_getnodeuniq(&sbp->sb_uuid, mp->m_fixedfsid);
935 * The vfs structure needs to have a file system independent
936 * way of checking for the invariant file system ID. Since it
937 * can't look at mount structures it has a pointer to the data
938 * in the mount structure.
940 * File systems that don't support user level file handles (i.e.
941 * all of them except for XFS) will leave vfs_altfsid as NULL.
943 vfsp->vfs_altfsid = (xfs_fsid_t *)mp->m_fixedfsid;
944 mp->m_dmevmask = 0; /* not persistent; set after each mount */
947 * Select the right directory manager.
950 XFS_SB_VERSION_HASDIRV2(&mp->m_sb) ?
955 * Initialize directory manager's entries.
960 * Initialize the attribute manager's entries.
962 mp->m_attr_magicpct = (mp->m_sb.sb_blocksize * 37) / 100;
965 * Initialize the precomputed transaction reservations values.
970 * Allocate and initialize the inode hash table for this
977 * Allocate and initialize the per-ag data.
979 init_rwsem(&mp->m_peraglock);
981 kmem_zalloc(sbp->sb_agcount * sizeof(xfs_perag_t), KM_SLEEP);
983 mp->m_maxagi = xfs_initialize_perag(vfsp, mp, sbp->sb_agcount);
986 * log's mount-time initialization. Perform 1st part recovery if needed
988 if (likely(sbp->sb_logblocks > 0)) { /* check for volume case */
989 error = xfs_log_mount(mp, mp->m_logdev_targp,
990 XFS_FSB_TO_DADDR(mp, sbp->sb_logstart),
991 XFS_FSB_TO_BB(mp, sbp->sb_logblocks));
993 cmn_err(CE_WARN, "XFS: log mount failed");
996 } else { /* No log has been defined */
997 cmn_err(CE_WARN, "XFS: no log defined");
998 XFS_ERROR_REPORT("xfs_mountfs_int(1)", XFS_ERRLEVEL_LOW, mp);
999 error = XFS_ERROR(EFSCORRUPTED);
1004 * Get and sanity-check the root inode.
1005 * Save the pointer to it in the mount structure.
1007 error = xfs_iget(mp, NULL, sbp->sb_rootino, 0, XFS_ILOCK_EXCL, &rip, 0);
1009 cmn_err(CE_WARN, "XFS: failed to read root inode");
1013 ASSERT(rip != NULL);
1014 rvp = XFS_ITOV(rip);
1016 if (unlikely((rip->i_d.di_mode & S_IFMT) != S_IFDIR)) {
1017 cmn_err(CE_WARN, "XFS: corrupted root inode");
1018 prdev("Root inode %llu is not a directory",
1019 mp->m_ddev_targp, (unsigned long long)rip->i_ino);
1020 xfs_iunlock(rip, XFS_ILOCK_EXCL);
1021 XFS_ERROR_REPORT("xfs_mountfs_int(2)", XFS_ERRLEVEL_LOW,
1023 error = XFS_ERROR(EFSCORRUPTED);
1026 mp->m_rootip = rip; /* save it */
1028 xfs_iunlock(rip, XFS_ILOCK_EXCL);
1031 * Initialize realtime inode pointers in the mount structure
1033 if ((error = xfs_rtmount_inodes(mp))) {
1035 * Free up the root inode.
1037 cmn_err(CE_WARN, "XFS: failed to read RT inodes");
1042 * If fs is not mounted readonly, then update the superblock
1043 * unit and width changes.
1045 if (update_flags && !(vfsp->vfs_flag & VFS_RDONLY))
1046 xfs_mount_log_sbunit(mp, update_flags);
1049 * Initialise the XFS quota management subsystem for this mount
1051 if ((error = XFS_QM_INIT(mp, "amount, "aflags)))
1055 * Finish recovering the file system. This part needed to be
1056 * delayed until after the root and real-time bitmap inodes
1057 * were consistently read in.
1059 error = xfs_log_mount_finish(mp, mfsi_flags);
1061 cmn_err(CE_WARN, "XFS: log mount finish failed");
1066 * Complete the quota initialisation, post-log-replay component.
1068 if ((error = XFS_QM_MOUNT(mp, quotamount, quotaflags, mfsi_flags)))
1075 * Free up the root inode.
1079 xfs_log_unmount_dealloc(mp);
1083 for (agno = 0; agno < sbp->sb_agcount; agno++)
1084 if (mp->m_perag[agno].pagb_list)
1085 kmem_free(mp->m_perag[agno].pagb_list,
1086 sizeof(xfs_perag_busy_t) * XFS_PAGB_NUM_SLOTS);
1087 kmem_free(mp->m_perag, sbp->sb_agcount * sizeof(xfs_perag_t));
1092 xfs_uuid_unmount(mp);
1100 * This flushes out the inodes,dquots and the superblock, unmounts the
1101 * log and makes sure that incore structures are freed.
1104 xfs_unmountfs(xfs_mount_t *mp, struct cred *cr)
1106 struct vfs *vfsp = XFS_MTOVFS(mp);
1107 #if defined(DEBUG) || defined(INDUCE_IO_ERROR)
1113 XFS_QM_DQPURGEALL(mp, XFS_QMOPT_QUOTALL | XFS_QMOPT_UMOUNTING);
1116 * Flush out the log synchronously so that we know for sure
1117 * that nothing is pinned. This is important because bflush()
1118 * will skip pinned buffers.
1120 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
1122 xfs_binval(mp->m_ddev_targp);
1123 if (mp->m_rtdev_targp) {
1124 xfs_binval(mp->m_rtdev_targp);
1127 xfs_unmountfs_writesb(mp);
1129 xfs_unmountfs_wait(mp); /* wait for async bufs */
1131 xfs_log_unmount(mp); /* Done! No more fs ops. */
1136 * All inodes from this mount point should be freed.
1138 ASSERT(mp->m_inodes == NULL);
1140 xfs_unmountfs_close(mp, cr);
1141 if ((mp->m_flags & XFS_MOUNT_NOUUID) == 0)
1142 xfs_uuid_unmount(mp);
1144 #if defined(DEBUG) || defined(INDUCE_IO_ERROR)
1146 * clear all error tags on this filesystem
1148 memcpy(&fsid, &vfsp->vfs_fsid, sizeof(int64_t));
1149 xfs_errortag_clearall_umount(fsid, mp->m_fsname, 0);
1152 xfs_mount_free(mp, 1);
1157 xfs_unmountfs_close(xfs_mount_t *mp, struct cred *cr)
1159 if (mp->m_logdev_targp != mp->m_ddev_targp)
1160 xfs_free_buftarg(mp->m_logdev_targp, 1);
1161 if (mp->m_rtdev_targp)
1162 xfs_free_buftarg(mp->m_rtdev_targp, 1);
1163 xfs_free_buftarg(mp->m_ddev_targp, 0);
1167 xfs_unmountfs_wait(xfs_mount_t *mp)
1169 if (mp->m_logdev_targp != mp->m_ddev_targp)
1170 xfs_wait_buftarg(mp->m_logdev_targp);
1171 if (mp->m_rtdev_targp)
1172 xfs_wait_buftarg(mp->m_rtdev_targp);
1173 xfs_wait_buftarg(mp->m_ddev_targp);
1177 xfs_unmountfs_writesb(xfs_mount_t *mp)
1184 * skip superblock write if fs is read-only, or
1185 * if we are doing a forced umount.
1187 sbp = xfs_getsb(mp, 0);
1188 if (!(XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY ||
1189 XFS_FORCED_SHUTDOWN(mp))) {
1191 xfs_icsb_sync_counters(mp);
1194 * mark shared-readonly if desired
1196 sb = XFS_BUF_TO_SBP(sbp);
1197 if (mp->m_mk_sharedro) {
1198 if (!(sb->sb_flags & XFS_SBF_READONLY))
1199 sb->sb_flags |= XFS_SBF_READONLY;
1200 if (!XFS_SB_VERSION_HASSHARED(sb))
1201 XFS_SB_VERSION_ADDSHARED(sb);
1202 xfs_fs_cmn_err(CE_NOTE, mp,
1203 "Unmounting, marking shared read-only");
1205 XFS_BUF_UNDONE(sbp);
1206 XFS_BUF_UNREAD(sbp);
1207 XFS_BUF_UNDELAYWRITE(sbp);
1209 XFS_BUF_UNASYNC(sbp);
1210 ASSERT(XFS_BUF_TARGET(sbp) == mp->m_ddev_targp);
1211 xfsbdstrat(mp, sbp);
1212 /* Nevermind errors we might get here. */
1213 error = xfs_iowait(sbp);
1215 xfs_ioerror_alert("xfs_unmountfs_writesb",
1216 mp, sbp, XFS_BUF_ADDR(sbp));
1217 if (error && mp->m_mk_sharedro)
1218 xfs_fs_cmn_err(CE_ALERT, mp, "Superblock write error detected while unmounting. Filesystem may not be marked shared readonly");
1225 * xfs_mod_sb() can be used to copy arbitrary changes to the
1226 * in-core superblock into the superblock buffer to be logged.
1227 * It does not provide the higher level of locking that is
1228 * needed to protect the in-core superblock from concurrent
1232 xfs_mod_sb(xfs_trans_t *tp, __int64_t fields)
1245 bp = xfs_trans_getsb(tp, mp, 0);
1246 sbp = XFS_BUF_TO_SBP(bp);
1247 first = sizeof(xfs_sb_t);
1250 /* translate/copy */
1252 xfs_xlatesb(XFS_BUF_PTR(bp), &(mp->m_sb), -1, fields);
1254 /* find modified range */
1256 f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
1257 ASSERT((1LL << f) & XFS_SB_MOD_BITS);
1258 first = xfs_sb_info[f].offset;
1260 f = (xfs_sb_field_t)xfs_highbit64((__uint64_t)fields);
1261 ASSERT((1LL << f) & XFS_SB_MOD_BITS);
1262 last = xfs_sb_info[f + 1].offset - 1;
1264 xfs_trans_log_buf(tp, bp, first, last);
1267 * xfs_mod_incore_sb_unlocked() is a utility routine common used to apply
1268 * a delta to a specified field in the in-core superblock. Simply
1269 * switch on the field indicated and apply the delta to that field.
1270 * Fields are not allowed to dip below zero, so if the delta would
1271 * do this do not apply it and return EINVAL.
1273 * The SB_LOCK must be held when this routine is called.
1276 xfs_mod_incore_sb_unlocked(xfs_mount_t *mp, xfs_sb_field_t field,
1277 int delta, int rsvd)
1279 int scounter; /* short counter for 32 bit fields */
1280 long long lcounter; /* long counter for 64 bit fields */
1281 long long res_used, rem;
1284 * With the in-core superblock spin lock held, switch
1285 * on the indicated field. Apply the delta to the
1286 * proper field. If the fields value would dip below
1287 * 0, then do not apply the delta and return EINVAL.
1290 case XFS_SBS_ICOUNT:
1291 lcounter = (long long)mp->m_sb.sb_icount;
1295 return XFS_ERROR(EINVAL);
1297 mp->m_sb.sb_icount = lcounter;
1300 lcounter = (long long)mp->m_sb.sb_ifree;
1304 return XFS_ERROR(EINVAL);
1306 mp->m_sb.sb_ifree = lcounter;
1308 case XFS_SBS_FDBLOCKS:
1310 lcounter = (long long)mp->m_sb.sb_fdblocks;
1311 res_used = (long long)(mp->m_resblks - mp->m_resblks_avail);
1313 if (delta > 0) { /* Putting blocks back */
1314 if (res_used > delta) {
1315 mp->m_resblks_avail += delta;
1317 rem = delta - res_used;
1318 mp->m_resblks_avail = mp->m_resblks;
1321 } else { /* Taking blocks away */
1326 * If were out of blocks, use any available reserved blocks if
1332 lcounter = (long long)mp->m_resblks_avail + delta;
1334 return XFS_ERROR(ENOSPC);
1336 mp->m_resblks_avail = lcounter;
1338 } else { /* not reserved */
1339 return XFS_ERROR(ENOSPC);
1344 mp->m_sb.sb_fdblocks = lcounter;
1346 case XFS_SBS_FREXTENTS:
1347 lcounter = (long long)mp->m_sb.sb_frextents;
1350 return XFS_ERROR(ENOSPC);
1352 mp->m_sb.sb_frextents = lcounter;
1354 case XFS_SBS_DBLOCKS:
1355 lcounter = (long long)mp->m_sb.sb_dblocks;
1359 return XFS_ERROR(EINVAL);
1361 mp->m_sb.sb_dblocks = lcounter;
1363 case XFS_SBS_AGCOUNT:
1364 scounter = mp->m_sb.sb_agcount;
1368 return XFS_ERROR(EINVAL);
1370 mp->m_sb.sb_agcount = scounter;
1372 case XFS_SBS_IMAX_PCT:
1373 scounter = mp->m_sb.sb_imax_pct;
1377 return XFS_ERROR(EINVAL);
1379 mp->m_sb.sb_imax_pct = scounter;
1381 case XFS_SBS_REXTSIZE:
1382 scounter = mp->m_sb.sb_rextsize;
1386 return XFS_ERROR(EINVAL);
1388 mp->m_sb.sb_rextsize = scounter;
1390 case XFS_SBS_RBMBLOCKS:
1391 scounter = mp->m_sb.sb_rbmblocks;
1395 return XFS_ERROR(EINVAL);
1397 mp->m_sb.sb_rbmblocks = scounter;
1399 case XFS_SBS_RBLOCKS:
1400 lcounter = (long long)mp->m_sb.sb_rblocks;
1404 return XFS_ERROR(EINVAL);
1406 mp->m_sb.sb_rblocks = lcounter;
1408 case XFS_SBS_REXTENTS:
1409 lcounter = (long long)mp->m_sb.sb_rextents;
1413 return XFS_ERROR(EINVAL);
1415 mp->m_sb.sb_rextents = lcounter;
1417 case XFS_SBS_REXTSLOG:
1418 scounter = mp->m_sb.sb_rextslog;
1422 return XFS_ERROR(EINVAL);
1424 mp->m_sb.sb_rextslog = scounter;
1428 return XFS_ERROR(EINVAL);
1433 * xfs_mod_incore_sb() is used to change a field in the in-core
1434 * superblock structure by the specified delta. This modification
1435 * is protected by the SB_LOCK. Just use the xfs_mod_incore_sb_unlocked()
1436 * routine to do the work.
1439 xfs_mod_incore_sb(xfs_mount_t *mp, xfs_sb_field_t field, int delta, int rsvd)
1444 /* check for per-cpu counters */
1446 #ifdef HAVE_PERCPU_SB
1447 case XFS_SBS_ICOUNT:
1449 case XFS_SBS_FDBLOCKS:
1450 if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
1451 status = xfs_icsb_modify_counters(mp, field,
1458 s = XFS_SB_LOCK(mp);
1459 status = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
1460 XFS_SB_UNLOCK(mp, s);
1468 * xfs_mod_incore_sb_batch() is used to change more than one field
1469 * in the in-core superblock structure at a time. This modification
1470 * is protected by a lock internal to this module. The fields and
1471 * changes to those fields are specified in the array of xfs_mod_sb
1472 * structures passed in.
1474 * Either all of the specified deltas will be applied or none of
1475 * them will. If any modified field dips below 0, then all modifications
1476 * will be backed out and EINVAL will be returned.
1479 xfs_mod_incore_sb_batch(xfs_mount_t *mp, xfs_mod_sb_t *msb, uint nmsb, int rsvd)
1486 * Loop through the array of mod structures and apply each
1487 * individually. If any fail, then back out all those
1488 * which have already been applied. Do all of this within
1489 * the scope of the SB_LOCK so that all of the changes will
1492 s = XFS_SB_LOCK(mp);
1494 for (msbp = &msbp[0]; msbp < (msb + nmsb); msbp++) {
1496 * Apply the delta at index n. If it fails, break
1497 * from the loop so we'll fall into the undo loop
1500 switch (msbp->msb_field) {
1501 #ifdef HAVE_PERCPU_SB
1502 case XFS_SBS_ICOUNT:
1504 case XFS_SBS_FDBLOCKS:
1505 if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
1506 status = xfs_icsb_modify_counters_locked(mp,
1508 msbp->msb_delta, rsvd);
1514 status = xfs_mod_incore_sb_unlocked(mp,
1516 msbp->msb_delta, rsvd);
1526 * If we didn't complete the loop above, then back out
1527 * any changes made to the superblock. If you add code
1528 * between the loop above and here, make sure that you
1529 * preserve the value of status. Loop back until
1530 * we step below the beginning of the array. Make sure
1531 * we don't touch anything back there.
1535 while (msbp >= msb) {
1536 switch (msbp->msb_field) {
1537 #ifdef HAVE_PERCPU_SB
1538 case XFS_SBS_ICOUNT:
1540 case XFS_SBS_FDBLOCKS:
1541 if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
1543 xfs_icsb_modify_counters_locked(mp,
1552 status = xfs_mod_incore_sb_unlocked(mp,
1558 ASSERT(status == 0);
1562 XFS_SB_UNLOCK(mp, s);
1567 * xfs_getsb() is called to obtain the buffer for the superblock.
1568 * The buffer is returned locked and read in from disk.
1569 * The buffer should be released with a call to xfs_brelse().
1571 * If the flags parameter is BUF_TRYLOCK, then we'll only return
1572 * the superblock buffer if it can be locked without sleeping.
1573 * If it can't then we'll return NULL.
1582 ASSERT(mp->m_sb_bp != NULL);
1584 if (flags & XFS_BUF_TRYLOCK) {
1585 if (!XFS_BUF_CPSEMA(bp)) {
1589 XFS_BUF_PSEMA(bp, PRIBIO);
1592 ASSERT(XFS_BUF_ISDONE(bp));
1597 * Used to free the superblock along various error paths.
1606 * Use xfs_getsb() so that the buffer will be locked
1607 * when we call xfs_buf_relse().
1609 bp = xfs_getsb(mp, 0);
1610 XFS_BUF_UNMANAGE(bp);
1616 * See if the UUID is unique among mounted XFS filesystems.
1617 * Mount fails if UUID is nil or a FS with the same UUID is already mounted.
1623 if (uuid_is_nil(&mp->m_sb.sb_uuid)) {
1625 "XFS: Filesystem %s has nil UUID - can't mount",
1629 if (!uuid_table_insert(&mp->m_sb.sb_uuid)) {
1631 "XFS: Filesystem %s has duplicate UUID - can't mount",
1639 * Remove filesystem from the UUID table.
1645 uuid_table_remove(&mp->m_sb.sb_uuid);
1649 * Used to log changes to the superblock unit and width fields which could
1650 * be altered by the mount options. Only the first superblock is updated.
1653 xfs_mount_log_sbunit(
1659 ASSERT(fields & (XFS_SB_UNIT|XFS_SB_WIDTH|XFS_SB_UUID));
1661 tp = xfs_trans_alloc(mp, XFS_TRANS_SB_UNIT);
1662 if (xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
1663 XFS_DEFAULT_LOG_COUNT)) {
1664 xfs_trans_cancel(tp, 0);
1667 xfs_mod_sb(tp, fields);
1668 xfs_trans_commit(tp, 0, NULL);
1672 #ifdef HAVE_PERCPU_SB
1674 * Per-cpu incore superblock counters
1676 * Simple concept, difficult implementation
1678 * Basically, replace the incore superblock counters with a distributed per cpu
1679 * counter for contended fields (e.g. free block count).
1681 * Difficulties arise in that the incore sb is used for ENOSPC checking, and
1682 * hence needs to be accurately read when we are running low on space. Hence
1683 * there is a method to enable and disable the per-cpu counters based on how
1684 * much "stuff" is available in them.
1686 * Basically, a counter is enabled if there is enough free resource to justify
1687 * running a per-cpu fast-path. If the per-cpu counter runs out (i.e. a local
1688 * ENOSPC), then we disable the counters to synchronise all callers and
1689 * re-distribute the available resources.
1691 * If, once we redistributed the available resources, we still get a failure,
1692 * we disable the per-cpu counter and go through the slow path.
1694 * The slow path is the current xfs_mod_incore_sb() function. This means that
1695 * when we disable a per-cpu counter, we need to drain it's resources back to
1696 * the global superblock. We do this after disabling the counter to prevent
1697 * more threads from queueing up on the counter.
1699 * Essentially, this means that we still need a lock in the fast path to enable
1700 * synchronisation between the global counters and the per-cpu counters. This
1701 * is not a problem because the lock will be local to a CPU almost all the time
1702 * and have little contention except when we get to ENOSPC conditions.
1704 * Basically, this lock becomes a barrier that enables us to lock out the fast
1705 * path while we do things like enabling and disabling counters and
1706 * synchronising the counters.
1710 * 1. XFS_SB_LOCK() before picking up per-cpu locks
1711 * 2. per-cpu locks always picked up via for_each_online_cpu() order
1712 * 3. accurate counter sync requires XFS_SB_LOCK + per cpu locks
1713 * 4. modifying per-cpu counters requires holding per-cpu lock
1714 * 5. modifying global counters requires holding XFS_SB_LOCK
1715 * 6. enabling or disabling a counter requires holding the XFS_SB_LOCK
1716 * and _none_ of the per-cpu locks.
1718 * Disabled counters are only ever re-enabled by a balance operation
1719 * that results in more free resources per CPU than a given threshold.
1720 * To ensure counters don't remain disabled, they are rebalanced when
1721 * the global resource goes above a higher threshold (i.e. some hysteresis
1722 * is present to prevent thrashing).
1726 * hot-plug CPU notifier support.
1728 * We cannot use the hotcpu_register() function because it does
1729 * not allow notifier instances. We need a notifier per filesystem
1730 * as we need to be able to identify the filesystem to balance
1731 * the counters out. This is achieved by having a notifier block
1732 * embedded in the xfs_mount_t and doing pointer magic to get the
1733 * mount pointer from the notifier block address.
1736 xfs_icsb_cpu_notify(
1737 struct notifier_block *nfb,
1738 unsigned long action,
1741 xfs_icsb_cnts_t *cntp;
1745 mp = (xfs_mount_t *)container_of(nfb, xfs_mount_t, m_icsb_notifier);
1746 cntp = (xfs_icsb_cnts_t *)
1747 per_cpu_ptr(mp->m_sb_cnts, (unsigned long)hcpu);
1749 case CPU_UP_PREPARE:
1750 /* Easy Case - initialize the area and locks, and
1751 * then rebalance when online does everything else for us. */
1752 memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
1755 xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
1756 xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
1757 xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
1760 /* Disable all the counters, then fold the dead cpu's
1761 * count into the total on the global superblock and
1762 * re-enable the counters. */
1763 s = XFS_SB_LOCK(mp);
1764 xfs_icsb_disable_counter(mp, XFS_SBS_ICOUNT);
1765 xfs_icsb_disable_counter(mp, XFS_SBS_IFREE);
1766 xfs_icsb_disable_counter(mp, XFS_SBS_FDBLOCKS);
1768 mp->m_sb.sb_icount += cntp->icsb_icount;
1769 mp->m_sb.sb_ifree += cntp->icsb_ifree;
1770 mp->m_sb.sb_fdblocks += cntp->icsb_fdblocks;
1772 memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
1774 xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, XFS_ICSB_SB_LOCKED);
1775 xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, XFS_ICSB_SB_LOCKED);
1776 xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, XFS_ICSB_SB_LOCKED);
1777 XFS_SB_UNLOCK(mp, s);
1785 xfs_icsb_init_counters(
1788 xfs_icsb_cnts_t *cntp;
1791 mp->m_sb_cnts = alloc_percpu(xfs_icsb_cnts_t);
1792 if (mp->m_sb_cnts == NULL)
1795 mp->m_icsb_notifier.notifier_call = xfs_icsb_cpu_notify;
1796 mp->m_icsb_notifier.priority = 0;
1797 register_cpu_notifier(&mp->m_icsb_notifier);
1799 for_each_online_cpu(i) {
1800 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
1801 memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
1804 * start with all counters disabled so that the
1805 * initial balance kicks us off correctly
1807 mp->m_icsb_counters = -1;
1812 xfs_icsb_destroy_counters(
1815 if (mp->m_sb_cnts) {
1816 unregister_cpu_notifier(&mp->m_icsb_notifier);
1817 free_percpu(mp->m_sb_cnts);
1823 xfs_icsb_cnts_t *icsbp)
1825 while (test_and_set_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags)) {
1831 xfs_icsb_unlock_cntr(
1832 xfs_icsb_cnts_t *icsbp)
1834 clear_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags);
1839 xfs_icsb_lock_all_counters(
1842 xfs_icsb_cnts_t *cntp;
1845 for_each_online_cpu(i) {
1846 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
1847 xfs_icsb_lock_cntr(cntp);
1852 xfs_icsb_unlock_all_counters(
1855 xfs_icsb_cnts_t *cntp;
1858 for_each_online_cpu(i) {
1859 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
1860 xfs_icsb_unlock_cntr(cntp);
1867 xfs_icsb_cnts_t *cnt,
1870 xfs_icsb_cnts_t *cntp;
1873 memset(cnt, 0, sizeof(xfs_icsb_cnts_t));
1875 if (!(flags & XFS_ICSB_LAZY_COUNT))
1876 xfs_icsb_lock_all_counters(mp);
1878 for_each_online_cpu(i) {
1879 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
1880 cnt->icsb_icount += cntp->icsb_icount;
1881 cnt->icsb_ifree += cntp->icsb_ifree;
1882 cnt->icsb_fdblocks += cntp->icsb_fdblocks;
1885 if (!(flags & XFS_ICSB_LAZY_COUNT))
1886 xfs_icsb_unlock_all_counters(mp);
1890 xfs_icsb_counter_disabled(
1892 xfs_sb_field_t field)
1894 ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
1895 return test_bit(field, &mp->m_icsb_counters);
1899 xfs_icsb_disable_counter(
1901 xfs_sb_field_t field)
1903 xfs_icsb_cnts_t cnt;
1905 ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
1907 xfs_icsb_lock_all_counters(mp);
1908 if (!test_and_set_bit(field, &mp->m_icsb_counters)) {
1909 /* drain back to superblock */
1911 xfs_icsb_count(mp, &cnt, XFS_ICSB_SB_LOCKED|XFS_ICSB_LAZY_COUNT);
1913 case XFS_SBS_ICOUNT:
1914 mp->m_sb.sb_icount = cnt.icsb_icount;
1917 mp->m_sb.sb_ifree = cnt.icsb_ifree;
1919 case XFS_SBS_FDBLOCKS:
1920 mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
1927 xfs_icsb_unlock_all_counters(mp);
1933 xfs_icsb_enable_counter(
1935 xfs_sb_field_t field,
1939 xfs_icsb_cnts_t *cntp;
1942 ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
1944 xfs_icsb_lock_all_counters(mp);
1945 for_each_online_cpu(i) {
1946 cntp = per_cpu_ptr(mp->m_sb_cnts, i);
1948 case XFS_SBS_ICOUNT:
1949 cntp->icsb_icount = count + resid;
1952 cntp->icsb_ifree = count + resid;
1954 case XFS_SBS_FDBLOCKS:
1955 cntp->icsb_fdblocks = count + resid;
1963 clear_bit(field, &mp->m_icsb_counters);
1964 xfs_icsb_unlock_all_counters(mp);
1968 xfs_icsb_sync_counters_int(
1972 xfs_icsb_cnts_t cnt;
1975 /* Pass 1: lock all counters */
1976 if ((flags & XFS_ICSB_SB_LOCKED) == 0)
1977 s = XFS_SB_LOCK(mp);
1979 xfs_icsb_count(mp, &cnt, flags);
1981 /* Step 3: update mp->m_sb fields */
1982 if (!xfs_icsb_counter_disabled(mp, XFS_SBS_ICOUNT))
1983 mp->m_sb.sb_icount = cnt.icsb_icount;
1984 if (!xfs_icsb_counter_disabled(mp, XFS_SBS_IFREE))
1985 mp->m_sb.sb_ifree = cnt.icsb_ifree;
1986 if (!xfs_icsb_counter_disabled(mp, XFS_SBS_FDBLOCKS))
1987 mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
1989 if ((flags & XFS_ICSB_SB_LOCKED) == 0)
1990 XFS_SB_UNLOCK(mp, s);
1994 * Accurate update of per-cpu counters to incore superblock
1997 xfs_icsb_sync_counters(
2000 xfs_icsb_sync_counters_int(mp, 0);
2004 * lazy addition used for things like df, background sb syncs, etc
2007 xfs_icsb_sync_counters_lazy(
2010 xfs_icsb_sync_counters_int(mp, XFS_ICSB_LAZY_COUNT);
2014 * Balance and enable/disable counters as necessary.
2016 * Thresholds for re-enabling counters are somewhat magic.
2017 * inode counts are chosen to be the same number as single
2018 * on disk allocation chunk per CPU, and free blocks is
2019 * something far enough zero that we aren't going thrash
2020 * when we get near ENOSPC.
2022 #define XFS_ICSB_INO_CNTR_REENABLE 64
2023 #define XFS_ICSB_FDBLK_CNTR_REENABLE 512
2025 xfs_icsb_balance_counter(
2027 xfs_sb_field_t field,
2030 uint64_t count, resid = 0;
2031 int weight = num_online_cpus();
2034 if (!(flags & XFS_ICSB_SB_LOCKED))
2035 s = XFS_SB_LOCK(mp);
2037 /* disable counter and sync counter */
2038 xfs_icsb_disable_counter(mp, field);
2040 /* update counters - first CPU gets residual*/
2042 case XFS_SBS_ICOUNT:
2043 count = mp->m_sb.sb_icount;
2044 resid = do_div(count, weight);
2045 if (count < XFS_ICSB_INO_CNTR_REENABLE)
2049 count = mp->m_sb.sb_ifree;
2050 resid = do_div(count, weight);
2051 if (count < XFS_ICSB_INO_CNTR_REENABLE)
2054 case XFS_SBS_FDBLOCKS:
2055 count = mp->m_sb.sb_fdblocks;
2056 resid = do_div(count, weight);
2057 if (count < XFS_ICSB_FDBLK_CNTR_REENABLE)
2065 xfs_icsb_enable_counter(mp, field, count, resid);
2067 if (!(flags & XFS_ICSB_SB_LOCKED))
2068 XFS_SB_UNLOCK(mp, s);
2072 xfs_icsb_modify_counters_int(
2074 xfs_sb_field_t field,
2079 xfs_icsb_cnts_t *icsbp;
2080 long long lcounter; /* long counter for 64 bit fields */
2081 int cpu, s, locked = 0;
2082 int ret = 0, balance_done = 0;
2086 icsbp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, cpu),
2087 xfs_icsb_lock_cntr(icsbp);
2088 if (unlikely(xfs_icsb_counter_disabled(mp, field)))
2092 case XFS_SBS_ICOUNT:
2093 lcounter = icsbp->icsb_icount;
2095 if (unlikely(lcounter < 0))
2097 icsbp->icsb_icount = lcounter;
2101 lcounter = icsbp->icsb_ifree;
2103 if (unlikely(lcounter < 0))
2105 icsbp->icsb_ifree = lcounter;
2108 case XFS_SBS_FDBLOCKS:
2109 BUG_ON((mp->m_resblks - mp->m_resblks_avail) != 0);
2111 lcounter = icsbp->icsb_fdblocks;
2113 if (unlikely(lcounter < 0))
2115 icsbp->icsb_fdblocks = lcounter;
2121 xfs_icsb_unlock_cntr(icsbp);
2124 XFS_SB_UNLOCK(mp, s);
2128 * The slow path needs to be run with the SBLOCK
2129 * held so that we prevent other threads from
2130 * attempting to run this path at the same time.
2131 * this provides exclusion for the balancing code,
2132 * and exclusive fallback if the balance does not
2133 * provide enough resources to continue in an unlocked
2137 xfs_icsb_unlock_cntr(icsbp);
2140 /* need to hold superblock incase we need
2141 * to disable a counter */
2142 if (!(flags & XFS_ICSB_SB_LOCKED)) {
2143 s = XFS_SB_LOCK(mp);
2145 flags |= XFS_ICSB_SB_LOCKED;
2147 if (!balance_done) {
2148 xfs_icsb_balance_counter(mp, field, flags);
2153 * we might not have enough on this local
2154 * cpu to allocate for a bulk request.
2155 * We need to drain this field from all CPUs
2156 * and disable the counter fastpath
2158 xfs_icsb_disable_counter(mp, field);
2161 ret = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
2164 XFS_SB_UNLOCK(mp, s);
2169 xfs_icsb_modify_counters(
2171 xfs_sb_field_t field,
2175 return xfs_icsb_modify_counters_int(mp, field, delta, rsvd, 0);
2179 * Called when superblock is already locked
2182 xfs_icsb_modify_counters_locked(
2184 xfs_sb_field_t field,
2188 return xfs_icsb_modify_counters_int(mp, field, delta,
2189 rsvd, XFS_ICSB_SB_LOCKED);