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_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
26 #include "xfs_mount.h"
27 #include "xfs_da_format.h"
28 #include "xfs_inode.h"
30 #include "xfs_ialloc.h"
31 #include "xfs_alloc.h"
32 #include "xfs_rtalloc.h"
34 #include "xfs_trans.h"
35 #include "xfs_trans_priv.h"
37 #include "xfs_error.h"
38 #include "xfs_quota.h"
39 #include "xfs_fsops.h"
40 #include "xfs_trace.h"
41 #include "xfs_icache.h"
42 #include "xfs_sysfs.h"
46 STATIC void xfs_icsb_balance_counter(xfs_mount_t *, xfs_sb_field_t,
48 STATIC void xfs_icsb_balance_counter_locked(xfs_mount_t *, xfs_sb_field_t,
50 STATIC void xfs_icsb_disable_counter(xfs_mount_t *, xfs_sb_field_t);
53 #define xfs_icsb_balance_counter(mp, a, b) do { } while (0)
54 #define xfs_icsb_balance_counter_locked(mp, a, b) do { } while (0)
57 static DEFINE_MUTEX(xfs_uuid_table_mutex);
58 static int xfs_uuid_table_size;
59 static uuid_t *xfs_uuid_table;
62 * See if the UUID is unique among mounted XFS filesystems.
63 * Mount fails if UUID is nil or a FS with the same UUID is already mounted.
69 uuid_t *uuid = &mp->m_sb.sb_uuid;
72 if (mp->m_flags & XFS_MOUNT_NOUUID)
75 if (uuid_is_nil(uuid)) {
76 xfs_warn(mp, "Filesystem has nil UUID - can't mount");
80 mutex_lock(&xfs_uuid_table_mutex);
81 for (i = 0, hole = -1; i < xfs_uuid_table_size; i++) {
82 if (uuid_is_nil(&xfs_uuid_table[i])) {
86 if (uuid_equal(uuid, &xfs_uuid_table[i]))
91 xfs_uuid_table = kmem_realloc(xfs_uuid_table,
92 (xfs_uuid_table_size + 1) * sizeof(*xfs_uuid_table),
93 xfs_uuid_table_size * sizeof(*xfs_uuid_table),
95 hole = xfs_uuid_table_size++;
97 xfs_uuid_table[hole] = *uuid;
98 mutex_unlock(&xfs_uuid_table_mutex);
103 mutex_unlock(&xfs_uuid_table_mutex);
104 xfs_warn(mp, "Filesystem has duplicate UUID %pU - can't mount", uuid);
110 struct xfs_mount *mp)
112 uuid_t *uuid = &mp->m_sb.sb_uuid;
115 if (mp->m_flags & XFS_MOUNT_NOUUID)
118 mutex_lock(&xfs_uuid_table_mutex);
119 for (i = 0; i < xfs_uuid_table_size; i++) {
120 if (uuid_is_nil(&xfs_uuid_table[i]))
122 if (!uuid_equal(uuid, &xfs_uuid_table[i]))
124 memset(&xfs_uuid_table[i], 0, sizeof(uuid_t));
127 ASSERT(i < xfs_uuid_table_size);
128 mutex_unlock(&xfs_uuid_table_mutex);
134 struct rcu_head *head)
136 struct xfs_perag *pag = container_of(head, struct xfs_perag, rcu_head);
138 ASSERT(atomic_read(&pag->pag_ref) == 0);
143 * Free up the per-ag resources associated with the mount structure.
150 struct xfs_perag *pag;
152 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
153 spin_lock(&mp->m_perag_lock);
154 pag = radix_tree_delete(&mp->m_perag_tree, agno);
155 spin_unlock(&mp->m_perag_lock);
157 ASSERT(atomic_read(&pag->pag_ref) == 0);
158 call_rcu(&pag->rcu_head, __xfs_free_perag);
163 * Check size of device based on the (data/realtime) block count.
164 * Note: this check is used by the growfs code as well as mount.
167 xfs_sb_validate_fsb_count(
171 ASSERT(PAGE_SHIFT >= sbp->sb_blocklog);
172 ASSERT(sbp->sb_blocklog >= BBSHIFT);
174 /* Limited by ULONG_MAX of page cache index */
175 if (nblocks >> (PAGE_CACHE_SHIFT - sbp->sb_blocklog) > ULONG_MAX)
181 xfs_initialize_perag(
183 xfs_agnumber_t agcount,
184 xfs_agnumber_t *maxagi)
186 xfs_agnumber_t index;
187 xfs_agnumber_t first_initialised = 0;
191 xfs_sb_t *sbp = &mp->m_sb;
195 * Walk the current per-ag tree so we don't try to initialise AGs
196 * that already exist (growfs case). Allocate and insert all the
197 * AGs we don't find ready for initialisation.
199 for (index = 0; index < agcount; index++) {
200 pag = xfs_perag_get(mp, index);
205 if (!first_initialised)
206 first_initialised = index;
208 pag = kmem_zalloc(sizeof(*pag), KM_MAYFAIL);
211 pag->pag_agno = index;
213 spin_lock_init(&pag->pag_ici_lock);
214 mutex_init(&pag->pag_ici_reclaim_lock);
215 INIT_RADIX_TREE(&pag->pag_ici_root, GFP_ATOMIC);
216 spin_lock_init(&pag->pag_buf_lock);
217 pag->pag_buf_tree = RB_ROOT;
219 if (radix_tree_preload(GFP_NOFS))
222 spin_lock(&mp->m_perag_lock);
223 if (radix_tree_insert(&mp->m_perag_tree, index, pag)) {
225 spin_unlock(&mp->m_perag_lock);
226 radix_tree_preload_end();
230 spin_unlock(&mp->m_perag_lock);
231 radix_tree_preload_end();
235 * If we mount with the inode64 option, or no inode overflows
236 * the legacy 32-bit address space clear the inode32 option.
238 agino = XFS_OFFBNO_TO_AGINO(mp, sbp->sb_agblocks - 1, 0);
239 ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino);
241 if ((mp->m_flags & XFS_MOUNT_SMALL_INUMS) && ino > XFS_MAXINUMBER_32)
242 mp->m_flags |= XFS_MOUNT_32BITINODES;
244 mp->m_flags &= ~XFS_MOUNT_32BITINODES;
246 if (mp->m_flags & XFS_MOUNT_32BITINODES)
247 index = xfs_set_inode32(mp, agcount);
249 index = xfs_set_inode64(mp, agcount);
257 for (; index > first_initialised; index--) {
258 pag = radix_tree_delete(&mp->m_perag_tree, index);
267 * Does the initial read of the superblock.
271 struct xfs_mount *mp,
274 unsigned int sector_size;
276 struct xfs_sb *sbp = &mp->m_sb;
278 int loud = !(flags & XFS_MFSI_QUIET);
279 const struct xfs_buf_ops *buf_ops;
281 ASSERT(mp->m_sb_bp == NULL);
282 ASSERT(mp->m_ddev_targp != NULL);
285 * For the initial read, we must guess at the sector
286 * size based on the block device. It's enough to
287 * get the sb_sectsize out of the superblock and
288 * then reread with the proper length.
289 * We don't verify it yet, because it may not be complete.
291 sector_size = xfs_getsize_buftarg(mp->m_ddev_targp);
295 * Allocate a (locked) buffer to hold the superblock.
296 * This will be kept around at all times to optimize
297 * access to the superblock.
300 error = xfs_buf_read_uncached(mp->m_ddev_targp, XFS_SB_DADDR,
301 BTOBB(sector_size), 0, &bp, buf_ops);
304 xfs_warn(mp, "SB validate failed with error %d.", error);
305 /* bad CRC means corrupted metadata */
306 if (error == -EFSBADCRC)
307 error = -EFSCORRUPTED;
312 * Initialize the mount structure from the superblock.
314 xfs_sb_from_disk(sbp, XFS_BUF_TO_SBP(bp));
317 * If we haven't validated the superblock, do so now before we try
318 * to check the sector size and reread the superblock appropriately.
320 if (sbp->sb_magicnum != XFS_SB_MAGIC) {
322 xfs_warn(mp, "Invalid superblock magic number");
328 * We must be able to do sector-sized and sector-aligned IO.
330 if (sector_size > sbp->sb_sectsize) {
332 xfs_warn(mp, "device supports %u byte sectors (not %u)",
333 sector_size, sbp->sb_sectsize);
338 if (buf_ops == NULL) {
340 * Re-read the superblock so the buffer is correctly sized,
341 * and properly verified.
344 sector_size = sbp->sb_sectsize;
345 buf_ops = loud ? &xfs_sb_buf_ops : &xfs_sb_quiet_buf_ops;
349 /* Initialize per-cpu counters */
350 xfs_icsb_reinit_counters(mp);
352 /* no need to be quiet anymore, so reset the buf ops */
353 bp->b_ops = &xfs_sb_buf_ops;
365 * Update alignment values based on mount options and sb values
368 xfs_update_alignment(xfs_mount_t *mp)
370 xfs_sb_t *sbp = &(mp->m_sb);
374 * If stripe unit and stripe width are not multiples
375 * of the fs blocksize turn off alignment.
377 if ((BBTOB(mp->m_dalign) & mp->m_blockmask) ||
378 (BBTOB(mp->m_swidth) & mp->m_blockmask)) {
380 "alignment check failed: sunit/swidth vs. blocksize(%d)",
385 * Convert the stripe unit and width to FSBs.
387 mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign);
388 if (mp->m_dalign && (sbp->sb_agblocks % mp->m_dalign)) {
390 "alignment check failed: sunit/swidth vs. agsize(%d)",
393 } else if (mp->m_dalign) {
394 mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth);
397 "alignment check failed: sunit(%d) less than bsize(%d)",
398 mp->m_dalign, sbp->sb_blocksize);
404 * Update superblock with new values
407 if (xfs_sb_version_hasdalign(sbp)) {
408 if (sbp->sb_unit != mp->m_dalign) {
409 sbp->sb_unit = mp->m_dalign;
410 mp->m_update_flags |= XFS_SB_UNIT;
412 if (sbp->sb_width != mp->m_swidth) {
413 sbp->sb_width = mp->m_swidth;
414 mp->m_update_flags |= XFS_SB_WIDTH;
418 "cannot change alignment: superblock does not support data alignment");
421 } else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN &&
422 xfs_sb_version_hasdalign(&mp->m_sb)) {
423 mp->m_dalign = sbp->sb_unit;
424 mp->m_swidth = sbp->sb_width;
431 * Set the maximum inode count for this filesystem
434 xfs_set_maxicount(xfs_mount_t *mp)
436 xfs_sb_t *sbp = &(mp->m_sb);
439 if (sbp->sb_imax_pct) {
441 * Make sure the maximum inode count is a multiple
442 * of the units we allocate inodes in.
444 icount = sbp->sb_dblocks * sbp->sb_imax_pct;
446 do_div(icount, mp->m_ialloc_blks);
447 mp->m_maxicount = (icount * mp->m_ialloc_blks) <<
455 * Set the default minimum read and write sizes unless
456 * already specified in a mount option.
457 * We use smaller I/O sizes when the file system
458 * is being used for NFS service (wsync mount option).
461 xfs_set_rw_sizes(xfs_mount_t *mp)
463 xfs_sb_t *sbp = &(mp->m_sb);
464 int readio_log, writeio_log;
466 if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)) {
467 if (mp->m_flags & XFS_MOUNT_WSYNC) {
468 readio_log = XFS_WSYNC_READIO_LOG;
469 writeio_log = XFS_WSYNC_WRITEIO_LOG;
471 readio_log = XFS_READIO_LOG_LARGE;
472 writeio_log = XFS_WRITEIO_LOG_LARGE;
475 readio_log = mp->m_readio_log;
476 writeio_log = mp->m_writeio_log;
479 if (sbp->sb_blocklog > readio_log) {
480 mp->m_readio_log = sbp->sb_blocklog;
482 mp->m_readio_log = readio_log;
484 mp->m_readio_blocks = 1 << (mp->m_readio_log - sbp->sb_blocklog);
485 if (sbp->sb_blocklog > writeio_log) {
486 mp->m_writeio_log = sbp->sb_blocklog;
488 mp->m_writeio_log = writeio_log;
490 mp->m_writeio_blocks = 1 << (mp->m_writeio_log - sbp->sb_blocklog);
494 * precalculate the low space thresholds for dynamic speculative preallocation.
497 xfs_set_low_space_thresholds(
498 struct xfs_mount *mp)
502 for (i = 0; i < XFS_LOWSP_MAX; i++) {
503 __uint64_t space = mp->m_sb.sb_dblocks;
506 mp->m_low_space[i] = space * (i + 1);
512 * Set whether we're using inode alignment.
515 xfs_set_inoalignment(xfs_mount_t *mp)
517 if (xfs_sb_version_hasalign(&mp->m_sb) &&
518 mp->m_sb.sb_inoalignmt >=
519 XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size))
520 mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1;
522 mp->m_inoalign_mask = 0;
524 * If we are using stripe alignment, check whether
525 * the stripe unit is a multiple of the inode alignment
527 if (mp->m_dalign && mp->m_inoalign_mask &&
528 !(mp->m_dalign & mp->m_inoalign_mask))
529 mp->m_sinoalign = mp->m_dalign;
535 * Check that the data (and log if separate) is an ok size.
539 struct xfs_mount *mp)
545 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
546 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
547 xfs_warn(mp, "filesystem size mismatch detected");
550 error = xfs_buf_read_uncached(mp->m_ddev_targp,
551 d - XFS_FSS_TO_BB(mp, 1),
552 XFS_FSS_TO_BB(mp, 1), 0, &bp, NULL);
554 xfs_warn(mp, "last sector read failed");
559 if (mp->m_logdev_targp == mp->m_ddev_targp)
562 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
563 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) {
564 xfs_warn(mp, "log size mismatch detected");
567 error = xfs_buf_read_uncached(mp->m_logdev_targp,
568 d - XFS_FSB_TO_BB(mp, 1),
569 XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL);
571 xfs_warn(mp, "log device read failed");
579 * Clear the quotaflags in memory and in the superblock.
582 xfs_mount_reset_sbqflags(
583 struct xfs_mount *mp)
586 struct xfs_trans *tp;
591 * It is OK to look at sb_qflags here in mount path,
594 if (mp->m_sb.sb_qflags == 0)
596 spin_lock(&mp->m_sb_lock);
597 mp->m_sb.sb_qflags = 0;
598 spin_unlock(&mp->m_sb_lock);
601 * If the fs is readonly, let the incore superblock run
602 * with quotas off but don't flush the update out to disk
604 if (mp->m_flags & XFS_MOUNT_RDONLY)
607 tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SBCHANGE);
608 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_qm_sbchange, 0, 0);
610 xfs_trans_cancel(tp, 0);
611 xfs_alert(mp, "%s: Superblock update failed!", __func__);
615 xfs_mod_sb(tp, XFS_SB_QFLAGS);
616 return xfs_trans_commit(tp, 0);
620 xfs_default_resblks(xfs_mount_t *mp)
625 * We default to 5% or 8192 fsbs of space reserved, whichever is
626 * smaller. This is intended to cover concurrent allocation
627 * transactions when we initially hit enospc. These each require a 4
628 * block reservation. Hence by default we cover roughly 2000 concurrent
629 * allocation reservations.
631 resblks = mp->m_sb.sb_dblocks;
633 resblks = min_t(__uint64_t, resblks, 8192);
638 * This function does the following on an initial mount of a file system:
639 * - reads the superblock from disk and init the mount struct
640 * - if we're a 32-bit kernel, do a size check on the superblock
641 * so we don't mount terabyte filesystems
642 * - init mount struct realtime fields
643 * - allocate inode hash table for fs
644 * - init directory manager
645 * - perform recovery and init the log manager
651 xfs_sb_t *sbp = &(mp->m_sb);
658 xfs_sb_mount_common(mp, sbp);
661 * Check for a mismatched features2 values. Older kernels
662 * read & wrote into the wrong sb offset for sb_features2
663 * on some platforms due to xfs_sb_t not being 64bit size aligned
664 * when sb_features2 was added, which made older superblock
665 * reading/writing routines swap it as a 64-bit value.
667 * For backwards compatibility, we make both slots equal.
669 * If we detect a mismatched field, we OR the set bits into the
670 * existing features2 field in case it has already been modified; we
671 * don't want to lose any features. We then update the bad location
672 * with the ORed value so that older kernels will see any features2
673 * flags, and mark the two fields as needing updates once the
674 * transaction subsystem is online.
676 if (xfs_sb_has_mismatched_features2(sbp)) {
677 xfs_warn(mp, "correcting sb_features alignment problem");
678 sbp->sb_features2 |= sbp->sb_bad_features2;
679 sbp->sb_bad_features2 = sbp->sb_features2;
680 mp->m_update_flags |= XFS_SB_FEATURES2 | XFS_SB_BAD_FEATURES2;
683 * Re-check for ATTR2 in case it was found in bad_features2
686 if (xfs_sb_version_hasattr2(&mp->m_sb) &&
687 !(mp->m_flags & XFS_MOUNT_NOATTR2))
688 mp->m_flags |= XFS_MOUNT_ATTR2;
691 if (xfs_sb_version_hasattr2(&mp->m_sb) &&
692 (mp->m_flags & XFS_MOUNT_NOATTR2)) {
693 xfs_sb_version_removeattr2(&mp->m_sb);
694 mp->m_update_flags |= XFS_SB_FEATURES2;
696 /* update sb_versionnum for the clearing of the morebits */
697 if (!sbp->sb_features2)
698 mp->m_update_flags |= XFS_SB_VERSIONNUM;
701 /* always use v2 inodes by default now */
702 if (!(mp->m_sb.sb_versionnum & XFS_SB_VERSION_NLINKBIT)) {
703 mp->m_sb.sb_versionnum |= XFS_SB_VERSION_NLINKBIT;
704 mp->m_update_flags |= XFS_SB_VERSIONNUM;
708 * Check if sb_agblocks is aligned at stripe boundary
709 * If sb_agblocks is NOT aligned turn off m_dalign since
710 * allocator alignment is within an ag, therefore ag has
711 * to be aligned at stripe boundary.
713 error = xfs_update_alignment(mp);
717 xfs_alloc_compute_maxlevels(mp);
718 xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
719 xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
720 xfs_ialloc_compute_maxlevels(mp);
722 xfs_set_maxicount(mp);
724 error = xfs_sysfs_init(&mp->m_kobj, &xfs_mp_ktype, NULL, mp->m_fsname);
728 error = xfs_uuid_mount(mp);
730 goto out_remove_sysfs;
733 * Set the minimum read and write sizes
735 xfs_set_rw_sizes(mp);
737 /* set the low space thresholds for dynamic preallocation */
738 xfs_set_low_space_thresholds(mp);
741 * Set the inode cluster size.
742 * This may still be overridden by the file system
743 * block size if it is larger than the chosen cluster size.
745 * For v5 filesystems, scale the cluster size with the inode size to
746 * keep a constant ratio of inode per cluster buffer, but only if mkfs
747 * has set the inode alignment value appropriately for larger cluster
750 mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE;
751 if (xfs_sb_version_hascrc(&mp->m_sb)) {
752 int new_size = mp->m_inode_cluster_size;
754 new_size *= mp->m_sb.sb_inodesize / XFS_DINODE_MIN_SIZE;
755 if (mp->m_sb.sb_inoalignmt >= XFS_B_TO_FSBT(mp, new_size))
756 mp->m_inode_cluster_size = new_size;
760 * Set inode alignment fields
762 xfs_set_inoalignment(mp);
765 * Check that the data (and log if separate) is an ok size.
767 error = xfs_check_sizes(mp);
769 goto out_remove_uuid;
772 * Initialize realtime fields in the mount structure
774 error = xfs_rtmount_init(mp);
776 xfs_warn(mp, "RT mount failed");
777 goto out_remove_uuid;
781 * Copies the low order bits of the timestamp and the randomly
782 * set "sequence" number out of a UUID.
784 uuid_getnodeuniq(&sbp->sb_uuid, mp->m_fixedfsid);
786 mp->m_dmevmask = 0; /* not persistent; set after each mount */
788 error = xfs_da_mount(mp);
790 xfs_warn(mp, "Failed dir/attr init: %d", error);
791 goto out_remove_uuid;
795 * Initialize the precomputed transaction reservations values.
800 * Allocate and initialize the per-ag data.
802 spin_lock_init(&mp->m_perag_lock);
803 INIT_RADIX_TREE(&mp->m_perag_tree, GFP_ATOMIC);
804 error = xfs_initialize_perag(mp, sbp->sb_agcount, &mp->m_maxagi);
806 xfs_warn(mp, "Failed per-ag init: %d", error);
810 if (!sbp->sb_logblocks) {
811 xfs_warn(mp, "no log defined");
812 XFS_ERROR_REPORT("xfs_mountfs", XFS_ERRLEVEL_LOW, mp);
813 error = -EFSCORRUPTED;
818 * log's mount-time initialization. Perform 1st part recovery if needed
820 error = xfs_log_mount(mp, mp->m_logdev_targp,
821 XFS_FSB_TO_DADDR(mp, sbp->sb_logstart),
822 XFS_FSB_TO_BB(mp, sbp->sb_logblocks));
824 xfs_warn(mp, "log mount failed");
829 * Now the log is mounted, we know if it was an unclean shutdown or
830 * not. If it was, with the first phase of recovery has completed, we
831 * have consistent AG blocks on disk. We have not recovered EFIs yet,
832 * but they are recovered transactionally in the second recovery phase
835 * Hence we can safely re-initialise incore superblock counters from
836 * the per-ag data. These may not be correct if the filesystem was not
837 * cleanly unmounted, so we need to wait for recovery to finish before
840 * If the filesystem was cleanly unmounted, then we can trust the
841 * values in the superblock to be correct and we don't need to do
844 * If we are currently making the filesystem, the initialisation will
845 * fail as the perag data is in an undefined state.
847 if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
848 !XFS_LAST_UNMOUNT_WAS_CLEAN(mp) &&
849 !mp->m_sb.sb_inprogress) {
850 error = xfs_initialize_perag_data(mp, sbp->sb_agcount);
852 goto out_log_dealloc;
856 * Get and sanity-check the root inode.
857 * Save the pointer to it in the mount structure.
859 error = xfs_iget(mp, NULL, sbp->sb_rootino, 0, XFS_ILOCK_EXCL, &rip);
861 xfs_warn(mp, "failed to read root inode");
862 goto out_log_dealloc;
867 if (unlikely(!S_ISDIR(rip->i_d.di_mode))) {
868 xfs_warn(mp, "corrupted root inode %llu: not a directory",
869 (unsigned long long)rip->i_ino);
870 xfs_iunlock(rip, XFS_ILOCK_EXCL);
871 XFS_ERROR_REPORT("xfs_mountfs_int(2)", XFS_ERRLEVEL_LOW,
873 error = -EFSCORRUPTED;
876 mp->m_rootip = rip; /* save it */
878 xfs_iunlock(rip, XFS_ILOCK_EXCL);
881 * Initialize realtime inode pointers in the mount structure
883 error = xfs_rtmount_inodes(mp);
886 * Free up the root inode.
888 xfs_warn(mp, "failed to read RT inodes");
893 * If this is a read-only mount defer the superblock updates until
894 * the next remount into writeable mode. Otherwise we would never
895 * perform the update e.g. for the root filesystem.
897 if (mp->m_update_flags && !(mp->m_flags & XFS_MOUNT_RDONLY)) {
898 error = xfs_mount_log_sb(mp, mp->m_update_flags);
900 xfs_warn(mp, "failed to write sb changes");
906 * Initialise the XFS quota management subsystem for this mount
908 if (XFS_IS_QUOTA_RUNNING(mp)) {
909 error = xfs_qm_newmount(mp, "amount, "aflags);
913 ASSERT(!XFS_IS_QUOTA_ON(mp));
916 * If a file system had quotas running earlier, but decided to
917 * mount without -o uquota/pquota/gquota options, revoke the
918 * quotachecked license.
920 if (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_ACCT) {
921 xfs_notice(mp, "resetting quota flags");
922 error = xfs_mount_reset_sbqflags(mp);
929 * Finish recovering the file system. This part needed to be
930 * delayed until after the root and real-time bitmap inodes
931 * were consistently read in.
933 error = xfs_log_mount_finish(mp);
935 xfs_warn(mp, "log mount finish failed");
940 * Complete the quota initialisation, post-log-replay component.
943 ASSERT(mp->m_qflags == 0);
944 mp->m_qflags = quotaflags;
946 xfs_qm_mount_quotas(mp);
950 * Now we are mounted, reserve a small amount of unused space for
951 * privileged transactions. This is needed so that transaction
952 * space required for critical operations can dip into this pool
953 * when at ENOSPC. This is needed for operations like create with
954 * attr, unwritten extent conversion at ENOSPC, etc. Data allocations
955 * are not allowed to use this reserved space.
957 * This may drive us straight to ENOSPC on mount, but that implies
958 * we were already there on the last unmount. Warn if this occurs.
960 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
961 resblks = xfs_default_resblks(mp);
962 error = xfs_reserve_blocks(mp, &resblks, NULL);
965 "Unable to allocate reserve blocks. Continuing without reserve pool.");
971 xfs_rtunmount_inodes(mp);
977 if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp)
978 xfs_wait_buftarg(mp->m_logdev_targp);
979 xfs_wait_buftarg(mp->m_ddev_targp);
985 xfs_uuid_unmount(mp);
987 xfs_sysfs_del(&mp->m_kobj);
993 * This flushes out the inodes,dquots and the superblock, unmounts the
994 * log and makes sure that incore structures are freed.
998 struct xfs_mount *mp)
1003 cancel_delayed_work_sync(&mp->m_eofblocks_work);
1005 xfs_qm_unmount_quotas(mp);
1006 xfs_rtunmount_inodes(mp);
1007 IRELE(mp->m_rootip);
1010 * We can potentially deadlock here if we have an inode cluster
1011 * that has been freed has its buffer still pinned in memory because
1012 * the transaction is still sitting in a iclog. The stale inodes
1013 * on that buffer will have their flush locks held until the
1014 * transaction hits the disk and the callbacks run. the inode
1015 * flush takes the flush lock unconditionally and with nothing to
1016 * push out the iclog we will never get that unlocked. hence we
1017 * need to force the log first.
1019 xfs_log_force(mp, XFS_LOG_SYNC);
1022 * Flush all pending changes from the AIL.
1024 xfs_ail_push_all_sync(mp->m_ail);
1027 * And reclaim all inodes. At this point there should be no dirty
1028 * inodes and none should be pinned or locked, but use synchronous
1029 * reclaim just to be sure. We can stop background inode reclaim
1030 * here as well if it is still running.
1032 cancel_delayed_work_sync(&mp->m_reclaim_work);
1033 xfs_reclaim_inodes(mp, SYNC_WAIT);
1038 * Unreserve any blocks we have so that when we unmount we don't account
1039 * the reserved free space as used. This is really only necessary for
1040 * lazy superblock counting because it trusts the incore superblock
1041 * counters to be absolutely correct on clean unmount.
1043 * We don't bother correcting this elsewhere for lazy superblock
1044 * counting because on mount of an unclean filesystem we reconstruct the
1045 * correct counter value and this is irrelevant.
1047 * For non-lazy counter filesystems, this doesn't matter at all because
1048 * we only every apply deltas to the superblock and hence the incore
1049 * value does not matter....
1052 error = xfs_reserve_blocks(mp, &resblks, NULL);
1054 xfs_warn(mp, "Unable to free reserved block pool. "
1055 "Freespace may not be correct on next mount.");
1057 error = xfs_log_sbcount(mp);
1059 xfs_warn(mp, "Unable to update superblock counters. "
1060 "Freespace may not be correct on next mount.");
1062 xfs_log_unmount(mp);
1064 xfs_uuid_unmount(mp);
1067 xfs_errortag_clearall(mp, 0);
1071 xfs_sysfs_del(&mp->m_kobj);
1075 xfs_fs_writable(xfs_mount_t *mp)
1077 return !(mp->m_super->s_writers.frozen || XFS_FORCED_SHUTDOWN(mp) ||
1078 (mp->m_flags & XFS_MOUNT_RDONLY));
1084 * Sync the superblock counters to disk.
1086 * Note this code can be called during the process of freezing, so
1087 * we may need to use the transaction allocator which does not
1088 * block when the transaction subsystem is in its frozen state.
1091 xfs_log_sbcount(xfs_mount_t *mp)
1096 if (!xfs_fs_writable(mp))
1099 xfs_icsb_sync_counters(mp, 0);
1102 * we don't need to do this if we are updating the superblock
1103 * counters on every modification.
1105 if (!xfs_sb_version_haslazysbcount(&mp->m_sb))
1108 tp = _xfs_trans_alloc(mp, XFS_TRANS_SB_COUNT, KM_SLEEP);
1109 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_sb, 0, 0);
1111 xfs_trans_cancel(tp, 0);
1115 xfs_mod_sb(tp, XFS_SB_IFREE | XFS_SB_ICOUNT | XFS_SB_FDBLOCKS);
1116 xfs_trans_set_sync(tp);
1117 error = xfs_trans_commit(tp, 0);
1122 * xfs_mod_incore_sb_unlocked() is a utility routine commonly used to apply
1123 * a delta to a specified field in the in-core superblock. Simply
1124 * switch on the field indicated and apply the delta to that field.
1125 * Fields are not allowed to dip below zero, so if the delta would
1126 * do this do not apply it and return EINVAL.
1128 * The m_sb_lock must be held when this routine is called.
1131 xfs_mod_incore_sb_unlocked(
1133 xfs_sb_field_t field,
1137 int scounter; /* short counter for 32 bit fields */
1138 long long lcounter; /* long counter for 64 bit fields */
1139 long long res_used, rem;
1142 * With the in-core superblock spin lock held, switch
1143 * on the indicated field. Apply the delta to the
1144 * proper field. If the fields value would dip below
1145 * 0, then do not apply the delta and return EINVAL.
1148 case XFS_SBS_ICOUNT:
1149 lcounter = (long long)mp->m_sb.sb_icount;
1155 mp->m_sb.sb_icount = lcounter;
1158 lcounter = (long long)mp->m_sb.sb_ifree;
1164 mp->m_sb.sb_ifree = lcounter;
1166 case XFS_SBS_FDBLOCKS:
1167 lcounter = (long long)
1168 mp->m_sb.sb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
1169 res_used = (long long)(mp->m_resblks - mp->m_resblks_avail);
1171 if (delta > 0) { /* Putting blocks back */
1172 if (res_used > delta) {
1173 mp->m_resblks_avail += delta;
1175 rem = delta - res_used;
1176 mp->m_resblks_avail = mp->m_resblks;
1179 } else { /* Taking blocks away */
1181 if (lcounter >= 0) {
1182 mp->m_sb.sb_fdblocks = lcounter +
1183 XFS_ALLOC_SET_ASIDE(mp);
1188 * We are out of blocks, use any available reserved
1189 * blocks if were allowed to.
1194 lcounter = (long long)mp->m_resblks_avail + delta;
1195 if (lcounter >= 0) {
1196 mp->m_resblks_avail = lcounter;
1199 printk_once(KERN_WARNING
1200 "Filesystem \"%s\": reserve blocks depleted! "
1201 "Consider increasing reserve pool size.",
1206 mp->m_sb.sb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp);
1208 case XFS_SBS_FREXTENTS:
1209 lcounter = (long long)mp->m_sb.sb_frextents;
1214 mp->m_sb.sb_frextents = lcounter;
1216 case XFS_SBS_DBLOCKS:
1217 lcounter = (long long)mp->m_sb.sb_dblocks;
1223 mp->m_sb.sb_dblocks = lcounter;
1225 case XFS_SBS_AGCOUNT:
1226 scounter = mp->m_sb.sb_agcount;
1232 mp->m_sb.sb_agcount = scounter;
1234 case XFS_SBS_IMAX_PCT:
1235 scounter = mp->m_sb.sb_imax_pct;
1241 mp->m_sb.sb_imax_pct = scounter;
1243 case XFS_SBS_REXTSIZE:
1244 scounter = mp->m_sb.sb_rextsize;
1250 mp->m_sb.sb_rextsize = scounter;
1252 case XFS_SBS_RBMBLOCKS:
1253 scounter = mp->m_sb.sb_rbmblocks;
1259 mp->m_sb.sb_rbmblocks = scounter;
1261 case XFS_SBS_RBLOCKS:
1262 lcounter = (long long)mp->m_sb.sb_rblocks;
1268 mp->m_sb.sb_rblocks = lcounter;
1270 case XFS_SBS_REXTENTS:
1271 lcounter = (long long)mp->m_sb.sb_rextents;
1277 mp->m_sb.sb_rextents = lcounter;
1279 case XFS_SBS_REXTSLOG:
1280 scounter = mp->m_sb.sb_rextslog;
1286 mp->m_sb.sb_rextslog = scounter;
1295 * xfs_mod_incore_sb() is used to change a field in the in-core
1296 * superblock structure by the specified delta. This modification
1297 * is protected by the m_sb_lock. Just use the xfs_mod_incore_sb_unlocked()
1298 * routine to do the work.
1302 struct xfs_mount *mp,
1303 xfs_sb_field_t field,
1309 #ifdef HAVE_PERCPU_SB
1310 ASSERT(field < XFS_SBS_ICOUNT || field > XFS_SBS_FDBLOCKS);
1312 spin_lock(&mp->m_sb_lock);
1313 status = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
1314 spin_unlock(&mp->m_sb_lock);
1320 * Change more than one field in the in-core superblock structure at a time.
1322 * The fields and changes to those fields are specified in the array of
1323 * xfs_mod_sb structures passed in. Either all of the specified deltas
1324 * will be applied or none of them will. If any modified field dips below 0,
1325 * then all modifications will be backed out and EINVAL will be returned.
1327 * Note that this function may not be used for the superblock values that
1328 * are tracked with the in-memory per-cpu counters - a direct call to
1329 * xfs_icsb_modify_counters is required for these.
1332 xfs_mod_incore_sb_batch(
1333 struct xfs_mount *mp,
1342 * Loop through the array of mod structures and apply each individually.
1343 * If any fail, then back out all those which have already been applied.
1344 * Do all of this within the scope of the m_sb_lock so that all of the
1345 * changes will be atomic.
1347 spin_lock(&mp->m_sb_lock);
1348 for (msbp = msb; msbp < (msb + nmsb); msbp++) {
1349 ASSERT(msbp->msb_field < XFS_SBS_ICOUNT ||
1350 msbp->msb_field > XFS_SBS_FDBLOCKS);
1352 error = xfs_mod_incore_sb_unlocked(mp, msbp->msb_field,
1353 msbp->msb_delta, rsvd);
1357 spin_unlock(&mp->m_sb_lock);
1361 while (--msbp >= msb) {
1362 error = xfs_mod_incore_sb_unlocked(mp, msbp->msb_field,
1363 -msbp->msb_delta, rsvd);
1366 spin_unlock(&mp->m_sb_lock);
1371 * xfs_getsb() is called to obtain the buffer for the superblock.
1372 * The buffer is returned locked and read in from disk.
1373 * The buffer should be released with a call to xfs_brelse().
1375 * If the flags parameter is BUF_TRYLOCK, then we'll only return
1376 * the superblock buffer if it can be locked without sleeping.
1377 * If it can't then we'll return NULL.
1381 struct xfs_mount *mp,
1384 struct xfs_buf *bp = mp->m_sb_bp;
1386 if (!xfs_buf_trylock(bp)) {
1387 if (flags & XBF_TRYLOCK)
1393 ASSERT(XFS_BUF_ISDONE(bp));
1398 * Used to free the superblock along various error paths.
1402 struct xfs_mount *mp)
1404 struct xfs_buf *bp = mp->m_sb_bp;
1412 * Used to log changes to the superblock unit and width fields which could
1413 * be altered by the mount options, as well as any potential sb_features2
1414 * fixup. Only the first superblock is updated.
1424 ASSERT(fields & (XFS_SB_UNIT | XFS_SB_WIDTH | XFS_SB_UUID |
1425 XFS_SB_FEATURES2 | XFS_SB_BAD_FEATURES2 |
1426 XFS_SB_VERSIONNUM));
1428 tp = xfs_trans_alloc(mp, XFS_TRANS_SB_UNIT);
1429 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_sb, 0, 0);
1431 xfs_trans_cancel(tp, 0);
1434 xfs_mod_sb(tp, fields);
1435 error = xfs_trans_commit(tp, 0);
1440 * If the underlying (data/log/rt) device is readonly, there are some
1441 * operations that cannot proceed.
1444 xfs_dev_is_read_only(
1445 struct xfs_mount *mp,
1448 if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
1449 xfs_readonly_buftarg(mp->m_logdev_targp) ||
1450 (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
1451 xfs_notice(mp, "%s required on read-only device.", message);
1452 xfs_notice(mp, "write access unavailable, cannot proceed.");
1458 #ifdef HAVE_PERCPU_SB
1460 * Per-cpu incore superblock counters
1462 * Simple concept, difficult implementation
1464 * Basically, replace the incore superblock counters with a distributed per cpu
1465 * counter for contended fields (e.g. free block count).
1467 * Difficulties arise in that the incore sb is used for ENOSPC checking, and
1468 * hence needs to be accurately read when we are running low on space. Hence
1469 * there is a method to enable and disable the per-cpu counters based on how
1470 * much "stuff" is available in them.
1472 * Basically, a counter is enabled if there is enough free resource to justify
1473 * running a per-cpu fast-path. If the per-cpu counter runs out (i.e. a local
1474 * ENOSPC), then we disable the counters to synchronise all callers and
1475 * re-distribute the available resources.
1477 * If, once we redistributed the available resources, we still get a failure,
1478 * we disable the per-cpu counter and go through the slow path.
1480 * The slow path is the current xfs_mod_incore_sb() function. This means that
1481 * when we disable a per-cpu counter, we need to drain its resources back to
1482 * the global superblock. We do this after disabling the counter to prevent
1483 * more threads from queueing up on the counter.
1485 * Essentially, this means that we still need a lock in the fast path to enable
1486 * synchronisation between the global counters and the per-cpu counters. This
1487 * is not a problem because the lock will be local to a CPU almost all the time
1488 * and have little contention except when we get to ENOSPC conditions.
1490 * Basically, this lock becomes a barrier that enables us to lock out the fast
1491 * path while we do things like enabling and disabling counters and
1492 * synchronising the counters.
1496 * 1. m_sb_lock before picking up per-cpu locks
1497 * 2. per-cpu locks always picked up via for_each_online_cpu() order
1498 * 3. accurate counter sync requires m_sb_lock + per cpu locks
1499 * 4. modifying per-cpu counters requires holding per-cpu lock
1500 * 5. modifying global counters requires holding m_sb_lock
1501 * 6. enabling or disabling a counter requires holding the m_sb_lock
1502 * and _none_ of the per-cpu locks.
1504 * Disabled counters are only ever re-enabled by a balance operation
1505 * that results in more free resources per CPU than a given threshold.
1506 * To ensure counters don't remain disabled, they are rebalanced when
1507 * the global resource goes above a higher threshold (i.e. some hysteresis
1508 * is present to prevent thrashing).
1511 #ifdef CONFIG_HOTPLUG_CPU
1513 * hot-plug CPU notifier support.
1515 * We need a notifier per filesystem as we need to be able to identify
1516 * the filesystem to balance the counters out. This is achieved by
1517 * having a notifier block embedded in the xfs_mount_t and doing pointer
1518 * magic to get the mount pointer from the notifier block address.
1521 xfs_icsb_cpu_notify(
1522 struct notifier_block *nfb,
1523 unsigned long action,
1526 xfs_icsb_cnts_t *cntp;
1529 mp = (xfs_mount_t *)container_of(nfb, xfs_mount_t, m_icsb_notifier);
1530 cntp = (xfs_icsb_cnts_t *)
1531 per_cpu_ptr(mp->m_sb_cnts, (unsigned long)hcpu);
1533 case CPU_UP_PREPARE:
1534 case CPU_UP_PREPARE_FROZEN:
1535 /* Easy Case - initialize the area and locks, and
1536 * then rebalance when online does everything else for us. */
1537 memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
1540 case CPU_ONLINE_FROZEN:
1542 xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
1543 xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
1544 xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
1545 xfs_icsb_unlock(mp);
1548 case CPU_DEAD_FROZEN:
1549 /* Disable all the counters, then fold the dead cpu's
1550 * count into the total on the global superblock and
1551 * re-enable the counters. */
1553 spin_lock(&mp->m_sb_lock);
1554 xfs_icsb_disable_counter(mp, XFS_SBS_ICOUNT);
1555 xfs_icsb_disable_counter(mp, XFS_SBS_IFREE);
1556 xfs_icsb_disable_counter(mp, XFS_SBS_FDBLOCKS);
1558 mp->m_sb.sb_icount += cntp->icsb_icount;
1559 mp->m_sb.sb_ifree += cntp->icsb_ifree;
1560 mp->m_sb.sb_fdblocks += cntp->icsb_fdblocks;
1562 memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
1564 xfs_icsb_balance_counter_locked(mp, XFS_SBS_ICOUNT, 0);
1565 xfs_icsb_balance_counter_locked(mp, XFS_SBS_IFREE, 0);
1566 xfs_icsb_balance_counter_locked(mp, XFS_SBS_FDBLOCKS, 0);
1567 spin_unlock(&mp->m_sb_lock);
1568 xfs_icsb_unlock(mp);
1574 #endif /* CONFIG_HOTPLUG_CPU */
1577 xfs_icsb_init_counters(
1580 xfs_icsb_cnts_t *cntp;
1583 mp->m_sb_cnts = alloc_percpu(xfs_icsb_cnts_t);
1584 if (mp->m_sb_cnts == NULL)
1587 for_each_online_cpu(i) {
1588 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
1589 memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
1592 mutex_init(&mp->m_icsb_mutex);
1595 * start with all counters disabled so that the
1596 * initial balance kicks us off correctly
1598 mp->m_icsb_counters = -1;
1600 #ifdef CONFIG_HOTPLUG_CPU
1601 mp->m_icsb_notifier.notifier_call = xfs_icsb_cpu_notify;
1602 mp->m_icsb_notifier.priority = 0;
1603 register_hotcpu_notifier(&mp->m_icsb_notifier);
1604 #endif /* CONFIG_HOTPLUG_CPU */
1610 xfs_icsb_reinit_counters(
1615 * start with all counters disabled so that the
1616 * initial balance kicks us off correctly
1618 mp->m_icsb_counters = -1;
1619 xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
1620 xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
1621 xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
1622 xfs_icsb_unlock(mp);
1626 xfs_icsb_destroy_counters(
1629 if (mp->m_sb_cnts) {
1630 unregister_hotcpu_notifier(&mp->m_icsb_notifier);
1631 free_percpu(mp->m_sb_cnts);
1633 mutex_destroy(&mp->m_icsb_mutex);
1638 xfs_icsb_cnts_t *icsbp)
1640 while (test_and_set_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags)) {
1646 xfs_icsb_unlock_cntr(
1647 xfs_icsb_cnts_t *icsbp)
1649 clear_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags);
1654 xfs_icsb_lock_all_counters(
1657 xfs_icsb_cnts_t *cntp;
1660 for_each_online_cpu(i) {
1661 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
1662 xfs_icsb_lock_cntr(cntp);
1667 xfs_icsb_unlock_all_counters(
1670 xfs_icsb_cnts_t *cntp;
1673 for_each_online_cpu(i) {
1674 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
1675 xfs_icsb_unlock_cntr(cntp);
1682 xfs_icsb_cnts_t *cnt,
1685 xfs_icsb_cnts_t *cntp;
1688 memset(cnt, 0, sizeof(xfs_icsb_cnts_t));
1690 if (!(flags & XFS_ICSB_LAZY_COUNT))
1691 xfs_icsb_lock_all_counters(mp);
1693 for_each_online_cpu(i) {
1694 cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
1695 cnt->icsb_icount += cntp->icsb_icount;
1696 cnt->icsb_ifree += cntp->icsb_ifree;
1697 cnt->icsb_fdblocks += cntp->icsb_fdblocks;
1700 if (!(flags & XFS_ICSB_LAZY_COUNT))
1701 xfs_icsb_unlock_all_counters(mp);
1705 xfs_icsb_counter_disabled(
1707 xfs_sb_field_t field)
1709 ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
1710 return test_bit(field, &mp->m_icsb_counters);
1714 xfs_icsb_disable_counter(
1716 xfs_sb_field_t field)
1718 xfs_icsb_cnts_t cnt;
1720 ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
1723 * If we are already disabled, then there is nothing to do
1724 * here. We check before locking all the counters to avoid
1725 * the expensive lock operation when being called in the
1726 * slow path and the counter is already disabled. This is
1727 * safe because the only time we set or clear this state is under
1730 if (xfs_icsb_counter_disabled(mp, field))
1733 xfs_icsb_lock_all_counters(mp);
1734 if (!test_and_set_bit(field, &mp->m_icsb_counters)) {
1735 /* drain back to superblock */
1737 xfs_icsb_count(mp, &cnt, XFS_ICSB_LAZY_COUNT);
1739 case XFS_SBS_ICOUNT:
1740 mp->m_sb.sb_icount = cnt.icsb_icount;
1743 mp->m_sb.sb_ifree = cnt.icsb_ifree;
1745 case XFS_SBS_FDBLOCKS:
1746 mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
1753 xfs_icsb_unlock_all_counters(mp);
1757 xfs_icsb_enable_counter(
1759 xfs_sb_field_t field,
1763 xfs_icsb_cnts_t *cntp;
1766 ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
1768 xfs_icsb_lock_all_counters(mp);
1769 for_each_online_cpu(i) {
1770 cntp = per_cpu_ptr(mp->m_sb_cnts, i);
1772 case XFS_SBS_ICOUNT:
1773 cntp->icsb_icount = count + resid;
1776 cntp->icsb_ifree = count + resid;
1778 case XFS_SBS_FDBLOCKS:
1779 cntp->icsb_fdblocks = count + resid;
1787 clear_bit(field, &mp->m_icsb_counters);
1788 xfs_icsb_unlock_all_counters(mp);
1792 xfs_icsb_sync_counters_locked(
1796 xfs_icsb_cnts_t cnt;
1798 xfs_icsb_count(mp, &cnt, flags);
1800 if (!xfs_icsb_counter_disabled(mp, XFS_SBS_ICOUNT))
1801 mp->m_sb.sb_icount = cnt.icsb_icount;
1802 if (!xfs_icsb_counter_disabled(mp, XFS_SBS_IFREE))
1803 mp->m_sb.sb_ifree = cnt.icsb_ifree;
1804 if (!xfs_icsb_counter_disabled(mp, XFS_SBS_FDBLOCKS))
1805 mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
1809 * Accurate update of per-cpu counters to incore superblock
1812 xfs_icsb_sync_counters(
1816 spin_lock(&mp->m_sb_lock);
1817 xfs_icsb_sync_counters_locked(mp, flags);
1818 spin_unlock(&mp->m_sb_lock);
1822 * Balance and enable/disable counters as necessary.
1824 * Thresholds for re-enabling counters are somewhat magic. inode counts are
1825 * chosen to be the same number as single on disk allocation chunk per CPU, and
1826 * free blocks is something far enough zero that we aren't going thrash when we
1827 * get near ENOSPC. We also need to supply a minimum we require per cpu to
1828 * prevent looping endlessly when xfs_alloc_space asks for more than will
1829 * be distributed to a single CPU but each CPU has enough blocks to be
1832 * Note that we can be called when counters are already disabled.
1833 * xfs_icsb_disable_counter() optimises the counter locking in this case to
1834 * prevent locking every per-cpu counter needlessly.
1837 #define XFS_ICSB_INO_CNTR_REENABLE (uint64_t)64
1838 #define XFS_ICSB_FDBLK_CNTR_REENABLE(mp) \
1839 (uint64_t)(512 + XFS_ALLOC_SET_ASIDE(mp))
1841 xfs_icsb_balance_counter_locked(
1843 xfs_sb_field_t field,
1846 uint64_t count, resid;
1847 int weight = num_online_cpus();
1848 uint64_t min = (uint64_t)min_per_cpu;
1850 /* disable counter and sync counter */
1851 xfs_icsb_disable_counter(mp, field);
1853 /* update counters - first CPU gets residual*/
1855 case XFS_SBS_ICOUNT:
1856 count = mp->m_sb.sb_icount;
1857 resid = do_div(count, weight);
1858 if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE))
1862 count = mp->m_sb.sb_ifree;
1863 resid = do_div(count, weight);
1864 if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE))
1867 case XFS_SBS_FDBLOCKS:
1868 count = mp->m_sb.sb_fdblocks;
1869 resid = do_div(count, weight);
1870 if (count < max(min, XFS_ICSB_FDBLK_CNTR_REENABLE(mp)))
1875 count = resid = 0; /* quiet, gcc */
1879 xfs_icsb_enable_counter(mp, field, count, resid);
1883 xfs_icsb_balance_counter(
1885 xfs_sb_field_t fields,
1888 spin_lock(&mp->m_sb_lock);
1889 xfs_icsb_balance_counter_locked(mp, fields, min_per_cpu);
1890 spin_unlock(&mp->m_sb_lock);
1894 xfs_icsb_modify_counters(
1896 xfs_sb_field_t field,
1900 xfs_icsb_cnts_t *icsbp;
1901 long long lcounter; /* long counter for 64 bit fields */
1907 icsbp = this_cpu_ptr(mp->m_sb_cnts);
1910 * if the counter is disabled, go to slow path
1912 if (unlikely(xfs_icsb_counter_disabled(mp, field)))
1914 xfs_icsb_lock_cntr(icsbp);
1915 if (unlikely(xfs_icsb_counter_disabled(mp, field))) {
1916 xfs_icsb_unlock_cntr(icsbp);
1921 case XFS_SBS_ICOUNT:
1922 lcounter = icsbp->icsb_icount;
1924 if (unlikely(lcounter < 0))
1925 goto balance_counter;
1926 icsbp->icsb_icount = lcounter;
1930 lcounter = icsbp->icsb_ifree;
1932 if (unlikely(lcounter < 0))
1933 goto balance_counter;
1934 icsbp->icsb_ifree = lcounter;
1937 case XFS_SBS_FDBLOCKS:
1938 BUG_ON((mp->m_resblks - mp->m_resblks_avail) != 0);
1940 lcounter = icsbp->icsb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
1942 if (unlikely(lcounter < 0))
1943 goto balance_counter;
1944 icsbp->icsb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp);
1950 xfs_icsb_unlock_cntr(icsbp);
1958 * serialise with a mutex so we don't burn lots of cpu on
1959 * the superblock lock. We still need to hold the superblock
1960 * lock, however, when we modify the global structures.
1965 * Now running atomically.
1967 * If the counter is enabled, someone has beaten us to rebalancing.
1968 * Drop the lock and try again in the fast path....
1970 if (!(xfs_icsb_counter_disabled(mp, field))) {
1971 xfs_icsb_unlock(mp);
1976 * The counter is currently disabled. Because we are
1977 * running atomically here, we know a rebalance cannot
1978 * be in progress. Hence we can go straight to operating
1979 * on the global superblock. We do not call xfs_mod_incore_sb()
1980 * here even though we need to get the m_sb_lock. Doing so
1981 * will cause us to re-enter this function and deadlock.
1982 * Hence we get the m_sb_lock ourselves and then call
1983 * xfs_mod_incore_sb_unlocked() as the unlocked path operates
1984 * directly on the global counters.
1986 spin_lock(&mp->m_sb_lock);
1987 ret = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
1988 spin_unlock(&mp->m_sb_lock);
1991 * Now that we've modified the global superblock, we
1992 * may be able to re-enable the distributed counters
1993 * (e.g. lots of space just got freed). After that
1997 xfs_icsb_balance_counter(mp, field, 0);
1998 xfs_icsb_unlock(mp);
2002 xfs_icsb_unlock_cntr(icsbp);
2006 * We may have multiple threads here if multiple per-cpu
2007 * counters run dry at the same time. This will mean we can
2008 * do more balances than strictly necessary but it is not
2009 * the common slowpath case.
2014 * running atomically.
2016 * This will leave the counter in the correct state for future
2017 * accesses. After the rebalance, we simply try again and our retry
2018 * will either succeed through the fast path or slow path without
2019 * another balance operation being required.
2021 xfs_icsb_balance_counter(mp, field, delta);
2022 xfs_icsb_unlock(mp);