11da177e4SLinus Torvalds /* 27b718769SNathan Scott * Copyright (c) 2000-2005 Silicon Graphics, Inc. 37b718769SNathan Scott * All Rights Reserved. 41da177e4SLinus Torvalds * 57b718769SNathan Scott * This program is free software; you can redistribute it and/or 67b718769SNathan Scott * modify it under the terms of the GNU General Public License as 71da177e4SLinus Torvalds * published by the Free Software Foundation. 81da177e4SLinus Torvalds * 97b718769SNathan Scott * This program is distributed in the hope that it would be useful, 107b718769SNathan Scott * but WITHOUT ANY WARRANTY; without even the implied warranty of 117b718769SNathan Scott * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 127b718769SNathan Scott * GNU General Public License for more details. 131da177e4SLinus Torvalds * 147b718769SNathan Scott * You should have received a copy of the GNU General Public License 157b718769SNathan Scott * along with this program; if not, write the Free Software Foundation, 167b718769SNathan Scott * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 171da177e4SLinus Torvalds */ 181da177e4SLinus Torvalds #include "xfs.h" 19a844f451SNathan Scott #include "xfs_fs.h" 201da177e4SLinus Torvalds #include "xfs_types.h" 21a844f451SNathan Scott #include "xfs_bit.h" 221da177e4SLinus Torvalds #include "xfs_log.h" 23a844f451SNathan Scott #include "xfs_inum.h" 241da177e4SLinus Torvalds #include "xfs_trans.h" 251da177e4SLinus Torvalds #include "xfs_sb.h" 261da177e4SLinus Torvalds #include "xfs_ag.h" 271da177e4SLinus Torvalds #include "xfs_dir2.h" 281da177e4SLinus Torvalds #include "xfs_mount.h" 291da177e4SLinus Torvalds #include "xfs_bmap_btree.h" 30a844f451SNathan Scott #include "xfs_alloc_btree.h" 311da177e4SLinus Torvalds #include "xfs_ialloc_btree.h" 321da177e4SLinus Torvalds #include "xfs_dinode.h" 331da177e4SLinus Torvalds #include "xfs_inode.h" 34a844f451SNathan Scott #include "xfs_btree.h" 35a844f451SNathan Scott #include "xfs_ialloc.h" 361da177e4SLinus Torvalds #include "xfs_alloc.h" 371da177e4SLinus Torvalds #include "xfs_rtalloc.h" 381da177e4SLinus Torvalds #include "xfs_bmap.h" 391da177e4SLinus Torvalds #include "xfs_error.h" 401da177e4SLinus Torvalds #include "xfs_rw.h" 411da177e4SLinus Torvalds #include "xfs_quota.h" 421da177e4SLinus Torvalds #include "xfs_fsops.h" 4343355099SChristoph Hellwig #include "xfs_utils.h" 440b1b213fSChristoph Hellwig #include "xfs_trace.h" 450b1b213fSChristoph Hellwig 461da177e4SLinus Torvalds 47ba0f32d4SChristoph Hellwig STATIC void xfs_unmountfs_wait(xfs_mount_t *); 481da177e4SLinus Torvalds 498d280b98SDavid Chinner 508d280b98SDavid Chinner #ifdef HAVE_PERCPU_SB 5120f4ebf2SDavid Chinner STATIC void xfs_icsb_balance_counter(xfs_mount_t *, xfs_sb_field_t, 5245af6c6dSChristoph Hellwig int); 5345af6c6dSChristoph Hellwig STATIC void xfs_icsb_balance_counter_locked(xfs_mount_t *, xfs_sb_field_t, 5445af6c6dSChristoph Hellwig int); 5536fbe6e6SDavid Chinner STATIC void xfs_icsb_disable_counter(xfs_mount_t *, xfs_sb_field_t); 568d280b98SDavid Chinner #else 578d280b98SDavid Chinner 5845af6c6dSChristoph Hellwig #define xfs_icsb_balance_counter(mp, a, b) do { } while (0) 5945af6c6dSChristoph Hellwig #define xfs_icsb_balance_counter_locked(mp, a, b) do { } while (0) 608d280b98SDavid Chinner #endif 618d280b98SDavid Chinner 621df84c93SChristoph Hellwig static const struct { 631da177e4SLinus Torvalds short offset; 641da177e4SLinus Torvalds short type; /* 0 = integer 651da177e4SLinus Torvalds * 1 = binary / string (no translation) 661da177e4SLinus Torvalds */ 671da177e4SLinus Torvalds } xfs_sb_info[] = { 681da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_magicnum), 0 }, 691da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_blocksize), 0 }, 701da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_dblocks), 0 }, 711da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_rblocks), 0 }, 721da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_rextents), 0 }, 731da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_uuid), 1 }, 741da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_logstart), 0 }, 751da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_rootino), 0 }, 761da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_rbmino), 0 }, 771da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_rsumino), 0 }, 781da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_rextsize), 0 }, 791da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_agblocks), 0 }, 801da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_agcount), 0 }, 811da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_rbmblocks), 0 }, 821da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_logblocks), 0 }, 831da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_versionnum), 0 }, 841da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_sectsize), 0 }, 851da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_inodesize), 0 }, 861da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_inopblock), 0 }, 871da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_fname[0]), 1 }, 881da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_blocklog), 0 }, 891da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_sectlog), 0 }, 901da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_inodelog), 0 }, 911da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_inopblog), 0 }, 921da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_agblklog), 0 }, 931da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_rextslog), 0 }, 941da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_inprogress), 0 }, 951da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_imax_pct), 0 }, 961da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_icount), 0 }, 971da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_ifree), 0 }, 981da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_fdblocks), 0 }, 991da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_frextents), 0 }, 1001da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_uquotino), 0 }, 1011da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_gquotino), 0 }, 1021da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_qflags), 0 }, 1031da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_flags), 0 }, 1041da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_shared_vn), 0 }, 1051da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_inoalignmt), 0 }, 1061da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_unit), 0 }, 1071da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_width), 0 }, 1081da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_dirblklog), 0 }, 1091da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_logsectlog), 0 }, 1101da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_logsectsize),0 }, 1111da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_logsunit), 0 }, 1121da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_features2), 0 }, 113ee1c0908SDavid Chinner { offsetof(xfs_sb_t, sb_bad_features2), 0 }, 1141da177e4SLinus Torvalds { sizeof(xfs_sb_t), 0 } 1151da177e4SLinus Torvalds }; 1161da177e4SLinus Torvalds 11727174203SChristoph Hellwig static DEFINE_MUTEX(xfs_uuid_table_mutex); 11827174203SChristoph Hellwig static int xfs_uuid_table_size; 11927174203SChristoph Hellwig static uuid_t *xfs_uuid_table; 12027174203SChristoph Hellwig 12127174203SChristoph Hellwig /* 12227174203SChristoph Hellwig * See if the UUID is unique among mounted XFS filesystems. 12327174203SChristoph Hellwig * Mount fails if UUID is nil or a FS with the same UUID is already mounted. 12427174203SChristoph Hellwig */ 12527174203SChristoph Hellwig STATIC int 12627174203SChristoph Hellwig xfs_uuid_mount( 12727174203SChristoph Hellwig struct xfs_mount *mp) 12827174203SChristoph Hellwig { 12927174203SChristoph Hellwig uuid_t *uuid = &mp->m_sb.sb_uuid; 13027174203SChristoph Hellwig int hole, i; 13127174203SChristoph Hellwig 13227174203SChristoph Hellwig if (mp->m_flags & XFS_MOUNT_NOUUID) 13327174203SChristoph Hellwig return 0; 13427174203SChristoph Hellwig 13527174203SChristoph Hellwig if (uuid_is_nil(uuid)) { 13627174203SChristoph Hellwig cmn_err(CE_WARN, 13727174203SChristoph Hellwig "XFS: Filesystem %s has nil UUID - can't mount", 13827174203SChristoph Hellwig mp->m_fsname); 13927174203SChristoph Hellwig return XFS_ERROR(EINVAL); 14027174203SChristoph Hellwig } 14127174203SChristoph Hellwig 14227174203SChristoph Hellwig mutex_lock(&xfs_uuid_table_mutex); 14327174203SChristoph Hellwig for (i = 0, hole = -1; i < xfs_uuid_table_size; i++) { 14427174203SChristoph Hellwig if (uuid_is_nil(&xfs_uuid_table[i])) { 14527174203SChristoph Hellwig hole = i; 14627174203SChristoph Hellwig continue; 14727174203SChristoph Hellwig } 14827174203SChristoph Hellwig if (uuid_equal(uuid, &xfs_uuid_table[i])) 14927174203SChristoph Hellwig goto out_duplicate; 15027174203SChristoph Hellwig } 15127174203SChristoph Hellwig 15227174203SChristoph Hellwig if (hole < 0) { 15327174203SChristoph Hellwig xfs_uuid_table = kmem_realloc(xfs_uuid_table, 15427174203SChristoph Hellwig (xfs_uuid_table_size + 1) * sizeof(*xfs_uuid_table), 15527174203SChristoph Hellwig xfs_uuid_table_size * sizeof(*xfs_uuid_table), 15627174203SChristoph Hellwig KM_SLEEP); 15727174203SChristoph Hellwig hole = xfs_uuid_table_size++; 15827174203SChristoph Hellwig } 15927174203SChristoph Hellwig xfs_uuid_table[hole] = *uuid; 16027174203SChristoph Hellwig mutex_unlock(&xfs_uuid_table_mutex); 16127174203SChristoph Hellwig 16227174203SChristoph Hellwig return 0; 16327174203SChristoph Hellwig 16427174203SChristoph Hellwig out_duplicate: 16527174203SChristoph Hellwig mutex_unlock(&xfs_uuid_table_mutex); 16627174203SChristoph Hellwig cmn_err(CE_WARN, "XFS: Filesystem %s has duplicate UUID - can't mount", 16727174203SChristoph Hellwig mp->m_fsname); 16827174203SChristoph Hellwig return XFS_ERROR(EINVAL); 16927174203SChristoph Hellwig } 17027174203SChristoph Hellwig 17127174203SChristoph Hellwig STATIC void 17227174203SChristoph Hellwig xfs_uuid_unmount( 17327174203SChristoph Hellwig struct xfs_mount *mp) 17427174203SChristoph Hellwig { 17527174203SChristoph Hellwig uuid_t *uuid = &mp->m_sb.sb_uuid; 17627174203SChristoph Hellwig int i; 17727174203SChristoph Hellwig 17827174203SChristoph Hellwig if (mp->m_flags & XFS_MOUNT_NOUUID) 17927174203SChristoph Hellwig return; 18027174203SChristoph Hellwig 18127174203SChristoph Hellwig mutex_lock(&xfs_uuid_table_mutex); 18227174203SChristoph Hellwig for (i = 0; i < xfs_uuid_table_size; i++) { 18327174203SChristoph Hellwig if (uuid_is_nil(&xfs_uuid_table[i])) 18427174203SChristoph Hellwig continue; 18527174203SChristoph Hellwig if (!uuid_equal(uuid, &xfs_uuid_table[i])) 18627174203SChristoph Hellwig continue; 18727174203SChristoph Hellwig memset(&xfs_uuid_table[i], 0, sizeof(uuid_t)); 18827174203SChristoph Hellwig break; 18927174203SChristoph Hellwig } 19027174203SChristoph Hellwig ASSERT(i < xfs_uuid_table_size); 19127174203SChristoph Hellwig mutex_unlock(&xfs_uuid_table_mutex); 19227174203SChristoph Hellwig } 19327174203SChristoph Hellwig 19427174203SChristoph Hellwig 1951da177e4SLinus Torvalds /* 1960fa800fbSDave Chinner * Reference counting access wrappers to the perag structures. 197e176579eSDave Chinner * Because we never free per-ag structures, the only thing we 198e176579eSDave Chinner * have to protect against changes is the tree structure itself. 1990fa800fbSDave Chinner */ 2000fa800fbSDave Chinner struct xfs_perag * 2010fa800fbSDave Chinner xfs_perag_get(struct xfs_mount *mp, xfs_agnumber_t agno) 2020fa800fbSDave Chinner { 2030fa800fbSDave Chinner struct xfs_perag *pag; 2040fa800fbSDave Chinner int ref = 0; 2050fa800fbSDave Chinner 206e176579eSDave Chinner rcu_read_lock(); 2070fa800fbSDave Chinner pag = radix_tree_lookup(&mp->m_perag_tree, agno); 2080fa800fbSDave Chinner if (pag) { 2090fa800fbSDave Chinner ASSERT(atomic_read(&pag->pag_ref) >= 0); 2100fa800fbSDave Chinner ref = atomic_inc_return(&pag->pag_ref); 2110fa800fbSDave Chinner } 212e176579eSDave Chinner rcu_read_unlock(); 2130fa800fbSDave Chinner trace_xfs_perag_get(mp, agno, ref, _RET_IP_); 2140fa800fbSDave Chinner return pag; 2150fa800fbSDave Chinner } 2160fa800fbSDave Chinner 21765d0f205SDave Chinner /* 21865d0f205SDave Chinner * search from @first to find the next perag with the given tag set. 21965d0f205SDave Chinner */ 22065d0f205SDave Chinner struct xfs_perag * 22165d0f205SDave Chinner xfs_perag_get_tag( 22265d0f205SDave Chinner struct xfs_mount *mp, 22365d0f205SDave Chinner xfs_agnumber_t first, 22465d0f205SDave Chinner int tag) 22565d0f205SDave Chinner { 22665d0f205SDave Chinner struct xfs_perag *pag; 22765d0f205SDave Chinner int found; 22865d0f205SDave Chinner int ref; 22965d0f205SDave Chinner 23065d0f205SDave Chinner rcu_read_lock(); 23165d0f205SDave Chinner found = radix_tree_gang_lookup_tag(&mp->m_perag_tree, 23265d0f205SDave Chinner (void **)&pag, first, 1, tag); 23365d0f205SDave Chinner if (found <= 0) { 23465d0f205SDave Chinner rcu_read_unlock(); 23565d0f205SDave Chinner return NULL; 23665d0f205SDave Chinner } 23765d0f205SDave Chinner ref = atomic_inc_return(&pag->pag_ref); 23865d0f205SDave Chinner rcu_read_unlock(); 23965d0f205SDave Chinner trace_xfs_perag_get_tag(mp, pag->pag_agno, ref, _RET_IP_); 24065d0f205SDave Chinner return pag; 24165d0f205SDave Chinner } 24265d0f205SDave Chinner 2430fa800fbSDave Chinner void 2440fa800fbSDave Chinner xfs_perag_put(struct xfs_perag *pag) 2450fa800fbSDave Chinner { 2460fa800fbSDave Chinner int ref; 2470fa800fbSDave Chinner 2480fa800fbSDave Chinner ASSERT(atomic_read(&pag->pag_ref) > 0); 2490fa800fbSDave Chinner ref = atomic_dec_return(&pag->pag_ref); 2500fa800fbSDave Chinner trace_xfs_perag_put(pag->pag_mount, pag->pag_agno, ref, _RET_IP_); 2510fa800fbSDave Chinner } 2520fa800fbSDave Chinner 253e176579eSDave Chinner STATIC void 254e176579eSDave Chinner __xfs_free_perag( 255e176579eSDave Chinner struct rcu_head *head) 256e176579eSDave Chinner { 257e176579eSDave Chinner struct xfs_perag *pag = container_of(head, struct xfs_perag, rcu_head); 258e176579eSDave Chinner 259e176579eSDave Chinner ASSERT(atomic_read(&pag->pag_ref) == 0); 260e176579eSDave Chinner kmem_free(pag); 261e176579eSDave Chinner } 262e176579eSDave Chinner 2630fa800fbSDave Chinner /* 264e176579eSDave Chinner * Free up the per-ag resources associated with the mount structure. 2651da177e4SLinus Torvalds */ 266c962fb79SChristoph Hellwig STATIC void 267ff4f038cSChristoph Hellwig xfs_free_perag( 268745f6919SChristoph Hellwig xfs_mount_t *mp) 2691da177e4SLinus Torvalds { 2701c1c6ebcSDave Chinner xfs_agnumber_t agno; 2711c1c6ebcSDave Chinner struct xfs_perag *pag; 2721da177e4SLinus Torvalds 2731c1c6ebcSDave Chinner for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) { 2741c1c6ebcSDave Chinner spin_lock(&mp->m_perag_lock); 2751c1c6ebcSDave Chinner pag = radix_tree_delete(&mp->m_perag_tree, agno); 2761c1c6ebcSDave Chinner spin_unlock(&mp->m_perag_lock); 277e176579eSDave Chinner ASSERT(pag); 278f83282a8SDave Chinner ASSERT(atomic_read(&pag->pag_ref) == 0); 279e176579eSDave Chinner call_rcu(&pag->rcu_head, __xfs_free_perag); 2801da177e4SLinus Torvalds } 2811da177e4SLinus Torvalds } 2821da177e4SLinus Torvalds 2834cc929eeSNathan Scott /* 2844cc929eeSNathan Scott * Check size of device based on the (data/realtime) block count. 2854cc929eeSNathan Scott * Note: this check is used by the growfs code as well as mount. 2864cc929eeSNathan Scott */ 2874cc929eeSNathan Scott int 2884cc929eeSNathan Scott xfs_sb_validate_fsb_count( 2894cc929eeSNathan Scott xfs_sb_t *sbp, 2904cc929eeSNathan Scott __uint64_t nblocks) 2914cc929eeSNathan Scott { 2924cc929eeSNathan Scott ASSERT(PAGE_SHIFT >= sbp->sb_blocklog); 2934cc929eeSNathan Scott ASSERT(sbp->sb_blocklog >= BBSHIFT); 2944cc929eeSNathan Scott 2954cc929eeSNathan Scott #if XFS_BIG_BLKNOS /* Limited by ULONG_MAX of page cache index */ 2964cc929eeSNathan Scott if (nblocks >> (PAGE_CACHE_SHIFT - sbp->sb_blocklog) > ULONG_MAX) 297657a4cffSEric Sandeen return EFBIG; 2984cc929eeSNathan Scott #else /* Limited by UINT_MAX of sectors */ 2994cc929eeSNathan Scott if (nblocks << (sbp->sb_blocklog - BBSHIFT) > UINT_MAX) 300657a4cffSEric Sandeen return EFBIG; 3014cc929eeSNathan Scott #endif 3024cc929eeSNathan Scott return 0; 3034cc929eeSNathan Scott } 3041da177e4SLinus Torvalds 3051da177e4SLinus Torvalds /* 3061da177e4SLinus Torvalds * Check the validity of the SB found. 3071da177e4SLinus Torvalds */ 3081da177e4SLinus Torvalds STATIC int 3091da177e4SLinus Torvalds xfs_mount_validate_sb( 3101da177e4SLinus Torvalds xfs_mount_t *mp, 311764d1f89SNathan Scott xfs_sb_t *sbp, 312764d1f89SNathan Scott int flags) 3131da177e4SLinus Torvalds { 314af34e09dSDave Chinner int loud = !(flags & XFS_MFSI_QUIET); 315af34e09dSDave Chinner 3161da177e4SLinus Torvalds /* 3171da177e4SLinus Torvalds * If the log device and data device have the 3181da177e4SLinus Torvalds * same device number, the log is internal. 3191da177e4SLinus Torvalds * Consequently, the sb_logstart should be non-zero. If 3201da177e4SLinus Torvalds * we have a zero sb_logstart in this case, we may be trying to mount 3211da177e4SLinus Torvalds * a volume filesystem in a non-volume manner. 3221da177e4SLinus Torvalds */ 3231da177e4SLinus Torvalds if (sbp->sb_magicnum != XFS_SB_MAGIC) { 324af34e09dSDave Chinner if (loud) 325af34e09dSDave Chinner xfs_warn(mp, "bad magic number"); 3261da177e4SLinus Torvalds return XFS_ERROR(EWRONGFS); 3271da177e4SLinus Torvalds } 3281da177e4SLinus Torvalds 32962118709SEric Sandeen if (!xfs_sb_good_version(sbp)) { 330af34e09dSDave Chinner if (loud) 331af34e09dSDave Chinner xfs_warn(mp, "bad version"); 3321da177e4SLinus Torvalds return XFS_ERROR(EWRONGFS); 3331da177e4SLinus Torvalds } 3341da177e4SLinus Torvalds 3351da177e4SLinus Torvalds if (unlikely( 3361da177e4SLinus Torvalds sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) { 337af34e09dSDave Chinner if (loud) 338af34e09dSDave Chinner xfs_warn(mp, 339764d1f89SNathan Scott "filesystem is marked as having an external log; " 340af34e09dSDave Chinner "specify logdev on the mount command line."); 341764d1f89SNathan Scott return XFS_ERROR(EINVAL); 3421da177e4SLinus Torvalds } 3431da177e4SLinus Torvalds 3441da177e4SLinus Torvalds if (unlikely( 3451da177e4SLinus Torvalds sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) { 346af34e09dSDave Chinner if (loud) 347af34e09dSDave Chinner xfs_warn(mp, 348764d1f89SNathan Scott "filesystem is marked as having an internal log; " 349af34e09dSDave Chinner "do not specify logdev on the mount command line."); 350764d1f89SNathan Scott return XFS_ERROR(EINVAL); 3511da177e4SLinus Torvalds } 3521da177e4SLinus Torvalds 3531da177e4SLinus Torvalds /* 3541da177e4SLinus Torvalds * More sanity checking. These were stolen directly from 3551da177e4SLinus Torvalds * xfs_repair. 3561da177e4SLinus Torvalds */ 3571da177e4SLinus Torvalds if (unlikely( 3581da177e4SLinus Torvalds sbp->sb_agcount <= 0 || 3591da177e4SLinus Torvalds sbp->sb_sectsize < XFS_MIN_SECTORSIZE || 3601da177e4SLinus Torvalds sbp->sb_sectsize > XFS_MAX_SECTORSIZE || 3611da177e4SLinus Torvalds sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG || 3621da177e4SLinus Torvalds sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG || 3632ac00af7SOlaf Weber sbp->sb_sectsize != (1 << sbp->sb_sectlog) || 3641da177e4SLinus Torvalds sbp->sb_blocksize < XFS_MIN_BLOCKSIZE || 3651da177e4SLinus Torvalds sbp->sb_blocksize > XFS_MAX_BLOCKSIZE || 3661da177e4SLinus Torvalds sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG || 3671da177e4SLinus Torvalds sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG || 3682ac00af7SOlaf Weber sbp->sb_blocksize != (1 << sbp->sb_blocklog) || 3691da177e4SLinus Torvalds sbp->sb_inodesize < XFS_DINODE_MIN_SIZE || 3701da177e4SLinus Torvalds sbp->sb_inodesize > XFS_DINODE_MAX_SIZE || 3719f989c94SNathan Scott sbp->sb_inodelog < XFS_DINODE_MIN_LOG || 3729f989c94SNathan Scott sbp->sb_inodelog > XFS_DINODE_MAX_LOG || 3732ac00af7SOlaf Weber sbp->sb_inodesize != (1 << sbp->sb_inodelog) || 3749f989c94SNathan Scott (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog) || 3751da177e4SLinus Torvalds (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE) || 3761da177e4SLinus Torvalds (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) || 377e50bd16fSNathan Scott (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */))) { 378af34e09dSDave Chinner if (loud) 379af34e09dSDave Chinner xfs_warn(mp, "SB sanity check 1 failed"); 3801da177e4SLinus Torvalds return XFS_ERROR(EFSCORRUPTED); 3811da177e4SLinus Torvalds } 3821da177e4SLinus Torvalds 3831da177e4SLinus Torvalds /* 3841da177e4SLinus Torvalds * Sanity check AG count, size fields against data size field 3851da177e4SLinus Torvalds */ 3861da177e4SLinus Torvalds if (unlikely( 3871da177e4SLinus Torvalds sbp->sb_dblocks == 0 || 3881da177e4SLinus Torvalds sbp->sb_dblocks > 3891da177e4SLinus Torvalds (xfs_drfsbno_t)sbp->sb_agcount * sbp->sb_agblocks || 3901da177e4SLinus Torvalds sbp->sb_dblocks < (xfs_drfsbno_t)(sbp->sb_agcount - 1) * 3911da177e4SLinus Torvalds sbp->sb_agblocks + XFS_MIN_AG_BLOCKS)) { 392af34e09dSDave Chinner if (loud) 393af34e09dSDave Chinner xfs_warn(mp, "SB sanity check 2 failed"); 3941da177e4SLinus Torvalds return XFS_ERROR(EFSCORRUPTED); 3951da177e4SLinus Torvalds } 3961da177e4SLinus Torvalds 3972edbddd5SLachlan McIlroy /* 3982edbddd5SLachlan McIlroy * Until this is fixed only page-sized or smaller data blocks work. 3992edbddd5SLachlan McIlroy */ 4002edbddd5SLachlan McIlroy if (unlikely(sbp->sb_blocksize > PAGE_SIZE)) { 401af34e09dSDave Chinner if (loud) { 402af34e09dSDave Chinner xfs_warn(mp, 403af34e09dSDave Chinner "File system with blocksize %d bytes. " 404af34e09dSDave Chinner "Only pagesize (%ld) or less will currently work.", 405af34e09dSDave Chinner sbp->sb_blocksize, PAGE_SIZE); 406af34e09dSDave Chinner } 4072edbddd5SLachlan McIlroy return XFS_ERROR(ENOSYS); 4082edbddd5SLachlan McIlroy } 4092edbddd5SLachlan McIlroy 4101a5902c5SChristoph Hellwig /* 4111a5902c5SChristoph Hellwig * Currently only very few inode sizes are supported. 4121a5902c5SChristoph Hellwig */ 4131a5902c5SChristoph Hellwig switch (sbp->sb_inodesize) { 4141a5902c5SChristoph Hellwig case 256: 4151a5902c5SChristoph Hellwig case 512: 4161a5902c5SChristoph Hellwig case 1024: 4171a5902c5SChristoph Hellwig case 2048: 4181a5902c5SChristoph Hellwig break; 4191a5902c5SChristoph Hellwig default: 420af34e09dSDave Chinner if (loud) 421af34e09dSDave Chinner xfs_warn(mp, "inode size of %d bytes not supported", 4221a5902c5SChristoph Hellwig sbp->sb_inodesize); 4231a5902c5SChristoph Hellwig return XFS_ERROR(ENOSYS); 4241a5902c5SChristoph Hellwig } 4251a5902c5SChristoph Hellwig 4264cc929eeSNathan Scott if (xfs_sb_validate_fsb_count(sbp, sbp->sb_dblocks) || 4274cc929eeSNathan Scott xfs_sb_validate_fsb_count(sbp, sbp->sb_rblocks)) { 428af34e09dSDave Chinner if (loud) 429af34e09dSDave Chinner xfs_warn(mp, 430764d1f89SNathan Scott "file system too large to be mounted on this system."); 431657a4cffSEric Sandeen return XFS_ERROR(EFBIG); 4321da177e4SLinus Torvalds } 4331da177e4SLinus Torvalds 4341da177e4SLinus Torvalds if (unlikely(sbp->sb_inprogress)) { 435af34e09dSDave Chinner if (loud) 436af34e09dSDave Chinner xfs_warn(mp, "file system busy"); 4371da177e4SLinus Torvalds return XFS_ERROR(EFSCORRUPTED); 4381da177e4SLinus Torvalds } 4391da177e4SLinus Torvalds 4401da177e4SLinus Torvalds /* 441de20614bSNathan Scott * Version 1 directory format has never worked on Linux. 442de20614bSNathan Scott */ 44362118709SEric Sandeen if (unlikely(!xfs_sb_version_hasdirv2(sbp))) { 444af34e09dSDave Chinner if (loud) 445af34e09dSDave Chinner xfs_warn(mp, 446764d1f89SNathan Scott "file system using version 1 directory format"); 447de20614bSNathan Scott return XFS_ERROR(ENOSYS); 448de20614bSNathan Scott } 449de20614bSNathan Scott 4501da177e4SLinus Torvalds return 0; 4511da177e4SLinus Torvalds } 4521da177e4SLinus Torvalds 4531c1c6ebcSDave Chinner int 454c11e2c36SNathan Scott xfs_initialize_perag( 455c11e2c36SNathan Scott xfs_mount_t *mp, 4561c1c6ebcSDave Chinner xfs_agnumber_t agcount, 4571c1c6ebcSDave Chinner xfs_agnumber_t *maxagi) 4581da177e4SLinus Torvalds { 4591da177e4SLinus Torvalds xfs_agnumber_t index, max_metadata; 4608b26c582SDave Chinner xfs_agnumber_t first_initialised = 0; 4611da177e4SLinus Torvalds xfs_perag_t *pag; 4621da177e4SLinus Torvalds xfs_agino_t agino; 4631da177e4SLinus Torvalds xfs_ino_t ino; 4641da177e4SLinus Torvalds xfs_sb_t *sbp = &mp->m_sb; 4658b26c582SDave Chinner int error = -ENOMEM; 4661da177e4SLinus Torvalds 4671c1c6ebcSDave Chinner /* 4681c1c6ebcSDave Chinner * Walk the current per-ag tree so we don't try to initialise AGs 4691c1c6ebcSDave Chinner * that already exist (growfs case). Allocate and insert all the 4701c1c6ebcSDave Chinner * AGs we don't find ready for initialisation. 4711c1c6ebcSDave Chinner */ 4721c1c6ebcSDave Chinner for (index = 0; index < agcount; index++) { 4731c1c6ebcSDave Chinner pag = xfs_perag_get(mp, index); 4741c1c6ebcSDave Chinner if (pag) { 4751c1c6ebcSDave Chinner xfs_perag_put(pag); 4761c1c6ebcSDave Chinner continue; 4771c1c6ebcSDave Chinner } 4788b26c582SDave Chinner if (!first_initialised) 4798b26c582SDave Chinner first_initialised = index; 480fb3b504aSChristoph Hellwig 4811c1c6ebcSDave Chinner pag = kmem_zalloc(sizeof(*pag), KM_MAYFAIL); 4821c1c6ebcSDave Chinner if (!pag) 4838b26c582SDave Chinner goto out_unwind; 484fb3b504aSChristoph Hellwig pag->pag_agno = index; 485fb3b504aSChristoph Hellwig pag->pag_mount = mp; 4861a427ab0SDave Chinner spin_lock_init(&pag->pag_ici_lock); 48769b491c2SDave Chinner mutex_init(&pag->pag_ici_reclaim_lock); 488fb3b504aSChristoph Hellwig INIT_RADIX_TREE(&pag->pag_ici_root, GFP_ATOMIC); 48974f75a0cSDave Chinner spin_lock_init(&pag->pag_buf_lock); 49074f75a0cSDave Chinner pag->pag_buf_tree = RB_ROOT; 491fb3b504aSChristoph Hellwig 4921c1c6ebcSDave Chinner if (radix_tree_preload(GFP_NOFS)) 4938b26c582SDave Chinner goto out_unwind; 494fb3b504aSChristoph Hellwig 4951c1c6ebcSDave Chinner spin_lock(&mp->m_perag_lock); 4961c1c6ebcSDave Chinner if (radix_tree_insert(&mp->m_perag_tree, index, pag)) { 4971c1c6ebcSDave Chinner BUG(); 4981c1c6ebcSDave Chinner spin_unlock(&mp->m_perag_lock); 4998b26c582SDave Chinner radix_tree_preload_end(); 5008b26c582SDave Chinner error = -EEXIST; 5018b26c582SDave Chinner goto out_unwind; 5021c1c6ebcSDave Chinner } 5031c1c6ebcSDave Chinner spin_unlock(&mp->m_perag_lock); 5041c1c6ebcSDave Chinner radix_tree_preload_end(); 5051c1c6ebcSDave Chinner } 5061c1c6ebcSDave Chinner 507fb3b504aSChristoph Hellwig /* 508fb3b504aSChristoph Hellwig * If we mount with the inode64 option, or no inode overflows 509fb3b504aSChristoph Hellwig * the legacy 32-bit address space clear the inode32 option. 5101da177e4SLinus Torvalds */ 511fb3b504aSChristoph Hellwig agino = XFS_OFFBNO_TO_AGINO(mp, sbp->sb_agblocks - 1, 0); 512fb3b504aSChristoph Hellwig ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino); 5131da177e4SLinus Torvalds 514fb3b504aSChristoph Hellwig if ((mp->m_flags & XFS_MOUNT_SMALL_INUMS) && ino > XFS_MAXINUMBER_32) 515fb3b504aSChristoph Hellwig mp->m_flags |= XFS_MOUNT_32BITINODES; 516fb3b504aSChristoph Hellwig else 517fb3b504aSChristoph Hellwig mp->m_flags &= ~XFS_MOUNT_32BITINODES; 518fb3b504aSChristoph Hellwig 5191da177e4SLinus Torvalds if (mp->m_flags & XFS_MOUNT_32BITINODES) { 520fb3b504aSChristoph Hellwig /* 521fb3b504aSChristoph Hellwig * Calculate how much should be reserved for inodes to meet 522fb3b504aSChristoph Hellwig * the max inode percentage. 5231da177e4SLinus Torvalds */ 5241da177e4SLinus Torvalds if (mp->m_maxicount) { 5251da177e4SLinus Torvalds __uint64_t icount; 5261da177e4SLinus Torvalds 5271da177e4SLinus Torvalds icount = sbp->sb_dblocks * sbp->sb_imax_pct; 5281da177e4SLinus Torvalds do_div(icount, 100); 5291da177e4SLinus Torvalds icount += sbp->sb_agblocks - 1; 530a749ee86SEric Sandeen do_div(icount, sbp->sb_agblocks); 5311da177e4SLinus Torvalds max_metadata = icount; 5321da177e4SLinus Torvalds } else { 5331da177e4SLinus Torvalds max_metadata = agcount; 5341da177e4SLinus Torvalds } 535fb3b504aSChristoph Hellwig 5361da177e4SLinus Torvalds for (index = 0; index < agcount; index++) { 5371da177e4SLinus Torvalds ino = XFS_AGINO_TO_INO(mp, index, agino); 538fb3b504aSChristoph Hellwig if (ino > XFS_MAXINUMBER_32) { 5391da177e4SLinus Torvalds index++; 5401da177e4SLinus Torvalds break; 5411da177e4SLinus Torvalds } 5421da177e4SLinus Torvalds 54344b56e0aSDave Chinner pag = xfs_perag_get(mp, index); 5441da177e4SLinus Torvalds pag->pagi_inodeok = 1; 5451da177e4SLinus Torvalds if (index < max_metadata) 5461da177e4SLinus Torvalds pag->pagf_metadata = 1; 54744b56e0aSDave Chinner xfs_perag_put(pag); 5481da177e4SLinus Torvalds } 5491da177e4SLinus Torvalds } else { 5501da177e4SLinus Torvalds for (index = 0; index < agcount; index++) { 55144b56e0aSDave Chinner pag = xfs_perag_get(mp, index); 5521da177e4SLinus Torvalds pag->pagi_inodeok = 1; 55344b56e0aSDave Chinner xfs_perag_put(pag); 5541da177e4SLinus Torvalds } 5551da177e4SLinus Torvalds } 556fb3b504aSChristoph Hellwig 5571c1c6ebcSDave Chinner if (maxagi) 5581c1c6ebcSDave Chinner *maxagi = index; 5591c1c6ebcSDave Chinner return 0; 5608b26c582SDave Chinner 5618b26c582SDave Chinner out_unwind: 5628b26c582SDave Chinner kmem_free(pag); 5638b26c582SDave Chinner for (; index > first_initialised; index--) { 5648b26c582SDave Chinner pag = radix_tree_delete(&mp->m_perag_tree, index); 5658b26c582SDave Chinner kmem_free(pag); 5668b26c582SDave Chinner } 5678b26c582SDave Chinner return error; 5681da177e4SLinus Torvalds } 5691da177e4SLinus Torvalds 5702bdf7cd0SChristoph Hellwig void 5712bdf7cd0SChristoph Hellwig xfs_sb_from_disk( 5722bdf7cd0SChristoph Hellwig xfs_sb_t *to, 5732bdf7cd0SChristoph Hellwig xfs_dsb_t *from) 5742bdf7cd0SChristoph Hellwig { 5752bdf7cd0SChristoph Hellwig to->sb_magicnum = be32_to_cpu(from->sb_magicnum); 5762bdf7cd0SChristoph Hellwig to->sb_blocksize = be32_to_cpu(from->sb_blocksize); 5772bdf7cd0SChristoph Hellwig to->sb_dblocks = be64_to_cpu(from->sb_dblocks); 5782bdf7cd0SChristoph Hellwig to->sb_rblocks = be64_to_cpu(from->sb_rblocks); 5792bdf7cd0SChristoph Hellwig to->sb_rextents = be64_to_cpu(from->sb_rextents); 5802bdf7cd0SChristoph Hellwig memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid)); 5812bdf7cd0SChristoph Hellwig to->sb_logstart = be64_to_cpu(from->sb_logstart); 5822bdf7cd0SChristoph Hellwig to->sb_rootino = be64_to_cpu(from->sb_rootino); 5832bdf7cd0SChristoph Hellwig to->sb_rbmino = be64_to_cpu(from->sb_rbmino); 5842bdf7cd0SChristoph Hellwig to->sb_rsumino = be64_to_cpu(from->sb_rsumino); 5852bdf7cd0SChristoph Hellwig to->sb_rextsize = be32_to_cpu(from->sb_rextsize); 5862bdf7cd0SChristoph Hellwig to->sb_agblocks = be32_to_cpu(from->sb_agblocks); 5872bdf7cd0SChristoph Hellwig to->sb_agcount = be32_to_cpu(from->sb_agcount); 5882bdf7cd0SChristoph Hellwig to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks); 5892bdf7cd0SChristoph Hellwig to->sb_logblocks = be32_to_cpu(from->sb_logblocks); 5902bdf7cd0SChristoph Hellwig to->sb_versionnum = be16_to_cpu(from->sb_versionnum); 5912bdf7cd0SChristoph Hellwig to->sb_sectsize = be16_to_cpu(from->sb_sectsize); 5922bdf7cd0SChristoph Hellwig to->sb_inodesize = be16_to_cpu(from->sb_inodesize); 5932bdf7cd0SChristoph Hellwig to->sb_inopblock = be16_to_cpu(from->sb_inopblock); 5942bdf7cd0SChristoph Hellwig memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname)); 5952bdf7cd0SChristoph Hellwig to->sb_blocklog = from->sb_blocklog; 5962bdf7cd0SChristoph Hellwig to->sb_sectlog = from->sb_sectlog; 5972bdf7cd0SChristoph Hellwig to->sb_inodelog = from->sb_inodelog; 5982bdf7cd0SChristoph Hellwig to->sb_inopblog = from->sb_inopblog; 5992bdf7cd0SChristoph Hellwig to->sb_agblklog = from->sb_agblklog; 6002bdf7cd0SChristoph Hellwig to->sb_rextslog = from->sb_rextslog; 6012bdf7cd0SChristoph Hellwig to->sb_inprogress = from->sb_inprogress; 6022bdf7cd0SChristoph Hellwig to->sb_imax_pct = from->sb_imax_pct; 6032bdf7cd0SChristoph Hellwig to->sb_icount = be64_to_cpu(from->sb_icount); 6042bdf7cd0SChristoph Hellwig to->sb_ifree = be64_to_cpu(from->sb_ifree); 6052bdf7cd0SChristoph Hellwig to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks); 6062bdf7cd0SChristoph Hellwig to->sb_frextents = be64_to_cpu(from->sb_frextents); 6072bdf7cd0SChristoph Hellwig to->sb_uquotino = be64_to_cpu(from->sb_uquotino); 6082bdf7cd0SChristoph Hellwig to->sb_gquotino = be64_to_cpu(from->sb_gquotino); 6092bdf7cd0SChristoph Hellwig to->sb_qflags = be16_to_cpu(from->sb_qflags); 6102bdf7cd0SChristoph Hellwig to->sb_flags = from->sb_flags; 6112bdf7cd0SChristoph Hellwig to->sb_shared_vn = from->sb_shared_vn; 6122bdf7cd0SChristoph Hellwig to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt); 6132bdf7cd0SChristoph Hellwig to->sb_unit = be32_to_cpu(from->sb_unit); 6142bdf7cd0SChristoph Hellwig to->sb_width = be32_to_cpu(from->sb_width); 6152bdf7cd0SChristoph Hellwig to->sb_dirblklog = from->sb_dirblklog; 6162bdf7cd0SChristoph Hellwig to->sb_logsectlog = from->sb_logsectlog; 6172bdf7cd0SChristoph Hellwig to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize); 6182bdf7cd0SChristoph Hellwig to->sb_logsunit = be32_to_cpu(from->sb_logsunit); 6192bdf7cd0SChristoph Hellwig to->sb_features2 = be32_to_cpu(from->sb_features2); 620ee1c0908SDavid Chinner to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2); 6212bdf7cd0SChristoph Hellwig } 6222bdf7cd0SChristoph Hellwig 6231da177e4SLinus Torvalds /* 6242bdf7cd0SChristoph Hellwig * Copy in core superblock to ondisk one. 6251da177e4SLinus Torvalds * 6262bdf7cd0SChristoph Hellwig * The fields argument is mask of superblock fields to copy. 6271da177e4SLinus Torvalds */ 6281da177e4SLinus Torvalds void 6292bdf7cd0SChristoph Hellwig xfs_sb_to_disk( 6302bdf7cd0SChristoph Hellwig xfs_dsb_t *to, 6312bdf7cd0SChristoph Hellwig xfs_sb_t *from, 6321da177e4SLinus Torvalds __int64_t fields) 6331da177e4SLinus Torvalds { 6342bdf7cd0SChristoph Hellwig xfs_caddr_t to_ptr = (xfs_caddr_t)to; 6352bdf7cd0SChristoph Hellwig xfs_caddr_t from_ptr = (xfs_caddr_t)from; 6361da177e4SLinus Torvalds xfs_sb_field_t f; 6371da177e4SLinus Torvalds int first; 6381da177e4SLinus Torvalds int size; 6391da177e4SLinus Torvalds 6401da177e4SLinus Torvalds ASSERT(fields); 6411da177e4SLinus Torvalds if (!fields) 6421da177e4SLinus Torvalds return; 6431da177e4SLinus Torvalds 6441da177e4SLinus Torvalds while (fields) { 6451da177e4SLinus Torvalds f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields); 6461da177e4SLinus Torvalds first = xfs_sb_info[f].offset; 6471da177e4SLinus Torvalds size = xfs_sb_info[f + 1].offset - first; 6481da177e4SLinus Torvalds 6491da177e4SLinus Torvalds ASSERT(xfs_sb_info[f].type == 0 || xfs_sb_info[f].type == 1); 6501da177e4SLinus Torvalds 6511da177e4SLinus Torvalds if (size == 1 || xfs_sb_info[f].type == 1) { 6522bdf7cd0SChristoph Hellwig memcpy(to_ptr + first, from_ptr + first, size); 6531da177e4SLinus Torvalds } else { 6541da177e4SLinus Torvalds switch (size) { 6551da177e4SLinus Torvalds case 2: 6562bdf7cd0SChristoph Hellwig *(__be16 *)(to_ptr + first) = 6572bdf7cd0SChristoph Hellwig cpu_to_be16(*(__u16 *)(from_ptr + first)); 6581da177e4SLinus Torvalds break; 6591da177e4SLinus Torvalds case 4: 6602bdf7cd0SChristoph Hellwig *(__be32 *)(to_ptr + first) = 6612bdf7cd0SChristoph Hellwig cpu_to_be32(*(__u32 *)(from_ptr + first)); 6621da177e4SLinus Torvalds break; 6631da177e4SLinus Torvalds case 8: 6642bdf7cd0SChristoph Hellwig *(__be64 *)(to_ptr + first) = 6652bdf7cd0SChristoph Hellwig cpu_to_be64(*(__u64 *)(from_ptr + first)); 6661da177e4SLinus Torvalds break; 6671da177e4SLinus Torvalds default: 6681da177e4SLinus Torvalds ASSERT(0); 6691da177e4SLinus Torvalds } 6701da177e4SLinus Torvalds } 6711da177e4SLinus Torvalds 6721da177e4SLinus Torvalds fields &= ~(1LL << f); 6731da177e4SLinus Torvalds } 6741da177e4SLinus Torvalds } 6751da177e4SLinus Torvalds 6761da177e4SLinus Torvalds /* 6771da177e4SLinus Torvalds * xfs_readsb 6781da177e4SLinus Torvalds * 6791da177e4SLinus Torvalds * Does the initial read of the superblock. 6801da177e4SLinus Torvalds */ 6811da177e4SLinus Torvalds int 682764d1f89SNathan Scott xfs_readsb(xfs_mount_t *mp, int flags) 6831da177e4SLinus Torvalds { 6841da177e4SLinus Torvalds unsigned int sector_size; 6851da177e4SLinus Torvalds xfs_buf_t *bp; 6861da177e4SLinus Torvalds int error; 687af34e09dSDave Chinner int loud = !(flags & XFS_MFSI_QUIET); 6881da177e4SLinus Torvalds 6891da177e4SLinus Torvalds ASSERT(mp->m_sb_bp == NULL); 6901da177e4SLinus Torvalds ASSERT(mp->m_ddev_targp != NULL); 6911da177e4SLinus Torvalds 6921da177e4SLinus Torvalds /* 6931da177e4SLinus Torvalds * Allocate a (locked) buffer to hold the superblock. 6941da177e4SLinus Torvalds * This will be kept around at all times to optimize 6951da177e4SLinus Torvalds * access to the superblock. 6961da177e4SLinus Torvalds */ 6971da177e4SLinus Torvalds sector_size = xfs_getsize_buftarg(mp->m_ddev_targp); 6981da177e4SLinus Torvalds 69926af6552SDave Chinner reread: 70026af6552SDave Chinner bp = xfs_buf_read_uncached(mp, mp->m_ddev_targp, 70126af6552SDave Chinner XFS_SB_DADDR, sector_size, 0); 70226af6552SDave Chinner if (!bp) { 703af34e09dSDave Chinner if (loud) 704af34e09dSDave Chinner xfs_warn(mp, "SB buffer read failed"); 70526af6552SDave Chinner return EIO; 7061da177e4SLinus Torvalds } 7071da177e4SLinus Torvalds 7081da177e4SLinus Torvalds /* 7091da177e4SLinus Torvalds * Initialize the mount structure from the superblock. 7101da177e4SLinus Torvalds * But first do some basic consistency checking. 7111da177e4SLinus Torvalds */ 7122bdf7cd0SChristoph Hellwig xfs_sb_from_disk(&mp->m_sb, XFS_BUF_TO_SBP(bp)); 713764d1f89SNathan Scott error = xfs_mount_validate_sb(mp, &(mp->m_sb), flags); 7141da177e4SLinus Torvalds if (error) { 715af34e09dSDave Chinner if (loud) 716af34e09dSDave Chinner xfs_warn(mp, "SB validate failed"); 71726af6552SDave Chinner goto release_buf; 7181da177e4SLinus Torvalds } 7191da177e4SLinus Torvalds 7201da177e4SLinus Torvalds /* 7211da177e4SLinus Torvalds * We must be able to do sector-sized and sector-aligned IO. 7221da177e4SLinus Torvalds */ 7231da177e4SLinus Torvalds if (sector_size > mp->m_sb.sb_sectsize) { 724af34e09dSDave Chinner if (loud) 725af34e09dSDave Chinner xfs_warn(mp, "device supports %u byte sectors (not %u)", 7261da177e4SLinus Torvalds sector_size, mp->m_sb.sb_sectsize); 7271da177e4SLinus Torvalds error = ENOSYS; 72826af6552SDave Chinner goto release_buf; 7291da177e4SLinus Torvalds } 7301da177e4SLinus Torvalds 7311da177e4SLinus Torvalds /* 7321da177e4SLinus Torvalds * If device sector size is smaller than the superblock size, 7331da177e4SLinus Torvalds * re-read the superblock so the buffer is correctly sized. 7341da177e4SLinus Torvalds */ 7351da177e4SLinus Torvalds if (sector_size < mp->m_sb.sb_sectsize) { 7361da177e4SLinus Torvalds xfs_buf_relse(bp); 7371da177e4SLinus Torvalds sector_size = mp->m_sb.sb_sectsize; 73826af6552SDave Chinner goto reread; 7391da177e4SLinus Torvalds } 7401da177e4SLinus Torvalds 7415478eeadSLachlan McIlroy /* Initialize per-cpu counters */ 7425478eeadSLachlan McIlroy xfs_icsb_reinit_counters(mp); 7438d280b98SDavid Chinner 7441da177e4SLinus Torvalds mp->m_sb_bp = bp; 74526af6552SDave Chinner xfs_buf_unlock(bp); 7461da177e4SLinus Torvalds return 0; 7471da177e4SLinus Torvalds 74826af6552SDave Chinner release_buf: 7491da177e4SLinus Torvalds xfs_buf_relse(bp); 7501da177e4SLinus Torvalds return error; 7511da177e4SLinus Torvalds } 7521da177e4SLinus Torvalds 7531da177e4SLinus Torvalds 7541da177e4SLinus Torvalds /* 7551da177e4SLinus Torvalds * xfs_mount_common 7561da177e4SLinus Torvalds * 7571da177e4SLinus Torvalds * Mount initialization code establishing various mount 7581da177e4SLinus Torvalds * fields from the superblock associated with the given 7591da177e4SLinus Torvalds * mount structure 7601da177e4SLinus Torvalds */ 761ba0f32d4SChristoph Hellwig STATIC void 7621da177e4SLinus Torvalds xfs_mount_common(xfs_mount_t *mp, xfs_sb_t *sbp) 7631da177e4SLinus Torvalds { 7641da177e4SLinus Torvalds mp->m_agfrotor = mp->m_agirotor = 0; 765007c61c6SEric Sandeen spin_lock_init(&mp->m_agirotor_lock); 7661da177e4SLinus Torvalds mp->m_maxagi = mp->m_sb.sb_agcount; 7671da177e4SLinus Torvalds mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG; 7681da177e4SLinus Torvalds mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT; 7691da177e4SLinus Torvalds mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT; 7701da177e4SLinus Torvalds mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1; 7711da177e4SLinus Torvalds mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog; 7721da177e4SLinus Torvalds mp->m_blockmask = sbp->sb_blocksize - 1; 7731da177e4SLinus Torvalds mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG; 7741da177e4SLinus Torvalds mp->m_blockwmask = mp->m_blockwsize - 1; 7751da177e4SLinus Torvalds 77660197e8dSChristoph Hellwig mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 1); 77760197e8dSChristoph Hellwig mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 0); 77860197e8dSChristoph Hellwig mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2; 77960197e8dSChristoph Hellwig mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2; 78060197e8dSChristoph Hellwig 78160197e8dSChristoph Hellwig mp->m_inobt_mxr[0] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 1); 78260197e8dSChristoph Hellwig mp->m_inobt_mxr[1] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 0); 78360197e8dSChristoph Hellwig mp->m_inobt_mnr[0] = mp->m_inobt_mxr[0] / 2; 78460197e8dSChristoph Hellwig mp->m_inobt_mnr[1] = mp->m_inobt_mxr[1] / 2; 78560197e8dSChristoph Hellwig 78660197e8dSChristoph Hellwig mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 1); 78760197e8dSChristoph Hellwig mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 0); 78860197e8dSChristoph Hellwig mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2; 78960197e8dSChristoph Hellwig mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2; 7901da177e4SLinus Torvalds 7911da177e4SLinus Torvalds mp->m_bsize = XFS_FSB_TO_BB(mp, 1); 7921da177e4SLinus Torvalds mp->m_ialloc_inos = (int)MAX((__uint16_t)XFS_INODES_PER_CHUNK, 7931da177e4SLinus Torvalds sbp->sb_inopblock); 7941da177e4SLinus Torvalds mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog; 7951da177e4SLinus Torvalds } 79692821e2bSDavid Chinner 79792821e2bSDavid Chinner /* 79892821e2bSDavid Chinner * xfs_initialize_perag_data 79992821e2bSDavid Chinner * 80092821e2bSDavid Chinner * Read in each per-ag structure so we can count up the number of 80192821e2bSDavid Chinner * allocated inodes, free inodes and used filesystem blocks as this 80292821e2bSDavid Chinner * information is no longer persistent in the superblock. Once we have 80392821e2bSDavid Chinner * this information, write it into the in-core superblock structure. 80492821e2bSDavid Chinner */ 80592821e2bSDavid Chinner STATIC int 80692821e2bSDavid Chinner xfs_initialize_perag_data(xfs_mount_t *mp, xfs_agnumber_t agcount) 80792821e2bSDavid Chinner { 80892821e2bSDavid Chinner xfs_agnumber_t index; 80992821e2bSDavid Chinner xfs_perag_t *pag; 81092821e2bSDavid Chinner xfs_sb_t *sbp = &mp->m_sb; 81192821e2bSDavid Chinner uint64_t ifree = 0; 81292821e2bSDavid Chinner uint64_t ialloc = 0; 81392821e2bSDavid Chinner uint64_t bfree = 0; 81492821e2bSDavid Chinner uint64_t bfreelst = 0; 81592821e2bSDavid Chinner uint64_t btree = 0; 81692821e2bSDavid Chinner int error; 81792821e2bSDavid Chinner 81892821e2bSDavid Chinner for (index = 0; index < agcount; index++) { 81992821e2bSDavid Chinner /* 82092821e2bSDavid Chinner * read the agf, then the agi. This gets us 8219da096fdSMalcolm Parsons * all the information we need and populates the 82292821e2bSDavid Chinner * per-ag structures for us. 82392821e2bSDavid Chinner */ 82492821e2bSDavid Chinner error = xfs_alloc_pagf_init(mp, NULL, index, 0); 82592821e2bSDavid Chinner if (error) 82692821e2bSDavid Chinner return error; 82792821e2bSDavid Chinner 82892821e2bSDavid Chinner error = xfs_ialloc_pagi_init(mp, NULL, index); 82992821e2bSDavid Chinner if (error) 83092821e2bSDavid Chinner return error; 83144b56e0aSDave Chinner pag = xfs_perag_get(mp, index); 83292821e2bSDavid Chinner ifree += pag->pagi_freecount; 83392821e2bSDavid Chinner ialloc += pag->pagi_count; 83492821e2bSDavid Chinner bfree += pag->pagf_freeblks; 83592821e2bSDavid Chinner bfreelst += pag->pagf_flcount; 83692821e2bSDavid Chinner btree += pag->pagf_btreeblks; 83744b56e0aSDave Chinner xfs_perag_put(pag); 83892821e2bSDavid Chinner } 83992821e2bSDavid Chinner /* 84092821e2bSDavid Chinner * Overwrite incore superblock counters with just-read data 84192821e2bSDavid Chinner */ 8423685c2a1SEric Sandeen spin_lock(&mp->m_sb_lock); 84392821e2bSDavid Chinner sbp->sb_ifree = ifree; 84492821e2bSDavid Chinner sbp->sb_icount = ialloc; 84592821e2bSDavid Chinner sbp->sb_fdblocks = bfree + bfreelst + btree; 8463685c2a1SEric Sandeen spin_unlock(&mp->m_sb_lock); 84792821e2bSDavid Chinner 84892821e2bSDavid Chinner /* Fixup the per-cpu counters as well. */ 84992821e2bSDavid Chinner xfs_icsb_reinit_counters(mp); 85092821e2bSDavid Chinner 85192821e2bSDavid Chinner return 0; 85292821e2bSDavid Chinner } 85392821e2bSDavid Chinner 8541da177e4SLinus Torvalds /* 8550771fb45SEric Sandeen * Update alignment values based on mount options and sb values 8561da177e4SLinus Torvalds */ 8570771fb45SEric Sandeen STATIC int 8587884bc86SChristoph Hellwig xfs_update_alignment(xfs_mount_t *mp) 8591da177e4SLinus Torvalds { 8601da177e4SLinus Torvalds xfs_sb_t *sbp = &(mp->m_sb); 8611da177e4SLinus Torvalds 8624249023aSChristoph Hellwig if (mp->m_dalign) { 8631da177e4SLinus Torvalds /* 8641da177e4SLinus Torvalds * If stripe unit and stripe width are not multiples 8651da177e4SLinus Torvalds * of the fs blocksize turn off alignment. 8661da177e4SLinus Torvalds */ 8671da177e4SLinus Torvalds if ((BBTOB(mp->m_dalign) & mp->m_blockmask) || 8681da177e4SLinus Torvalds (BBTOB(mp->m_swidth) & mp->m_blockmask)) { 8691da177e4SLinus Torvalds if (mp->m_flags & XFS_MOUNT_RETERR) { 8701da177e4SLinus Torvalds cmn_err(CE_WARN, 8711da177e4SLinus Torvalds "XFS: alignment check 1 failed"); 8720771fb45SEric Sandeen return XFS_ERROR(EINVAL); 8731da177e4SLinus Torvalds } 8741da177e4SLinus Torvalds mp->m_dalign = mp->m_swidth = 0; 8751da177e4SLinus Torvalds } else { 8761da177e4SLinus Torvalds /* 8771da177e4SLinus Torvalds * Convert the stripe unit and width to FSBs. 8781da177e4SLinus Torvalds */ 8791da177e4SLinus Torvalds mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign); 8801da177e4SLinus Torvalds if (mp->m_dalign && (sbp->sb_agblocks % mp->m_dalign)) { 8811da177e4SLinus Torvalds if (mp->m_flags & XFS_MOUNT_RETERR) { 8820771fb45SEric Sandeen return XFS_ERROR(EINVAL); 8831da177e4SLinus Torvalds } 884*53487786SDave Chinner xfs_warn(mp, 885*53487786SDave Chinner "stripe alignment turned off: sunit(%d)/swidth(%d) " 886*53487786SDave Chinner "incompatible with agsize(%d)", 8871da177e4SLinus Torvalds mp->m_dalign, mp->m_swidth, 8881da177e4SLinus Torvalds sbp->sb_agblocks); 8891da177e4SLinus Torvalds 8901da177e4SLinus Torvalds mp->m_dalign = 0; 8911da177e4SLinus Torvalds mp->m_swidth = 0; 8921da177e4SLinus Torvalds } else if (mp->m_dalign) { 8931da177e4SLinus Torvalds mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth); 8941da177e4SLinus Torvalds } else { 8951da177e4SLinus Torvalds if (mp->m_flags & XFS_MOUNT_RETERR) { 896*53487786SDave Chinner xfs_warn(mp, 8971da177e4SLinus Torvalds "stripe alignment turned off: sunit(%d) less than bsize(%d)", 8981da177e4SLinus Torvalds mp->m_dalign, 8991da177e4SLinus Torvalds mp->m_blockmask +1); 9000771fb45SEric Sandeen return XFS_ERROR(EINVAL); 9011da177e4SLinus Torvalds } 9021da177e4SLinus Torvalds mp->m_swidth = 0; 9031da177e4SLinus Torvalds } 9041da177e4SLinus Torvalds } 9051da177e4SLinus Torvalds 9061da177e4SLinus Torvalds /* 9071da177e4SLinus Torvalds * Update superblock with new values 9081da177e4SLinus Torvalds * and log changes 9091da177e4SLinus Torvalds */ 91062118709SEric Sandeen if (xfs_sb_version_hasdalign(sbp)) { 9111da177e4SLinus Torvalds if (sbp->sb_unit != mp->m_dalign) { 9121da177e4SLinus Torvalds sbp->sb_unit = mp->m_dalign; 9137884bc86SChristoph Hellwig mp->m_update_flags |= XFS_SB_UNIT; 9141da177e4SLinus Torvalds } 9151da177e4SLinus Torvalds if (sbp->sb_width != mp->m_swidth) { 9161da177e4SLinus Torvalds sbp->sb_width = mp->m_swidth; 9177884bc86SChristoph Hellwig mp->m_update_flags |= XFS_SB_WIDTH; 9181da177e4SLinus Torvalds } 9191da177e4SLinus Torvalds } 9201da177e4SLinus Torvalds } else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN && 92162118709SEric Sandeen xfs_sb_version_hasdalign(&mp->m_sb)) { 9221da177e4SLinus Torvalds mp->m_dalign = sbp->sb_unit; 9231da177e4SLinus Torvalds mp->m_swidth = sbp->sb_width; 9241da177e4SLinus Torvalds } 9251da177e4SLinus Torvalds 9260771fb45SEric Sandeen return 0; 9270771fb45SEric Sandeen } 9281da177e4SLinus Torvalds 9290771fb45SEric Sandeen /* 9300771fb45SEric Sandeen * Set the maximum inode count for this filesystem 9310771fb45SEric Sandeen */ 9320771fb45SEric Sandeen STATIC void 9330771fb45SEric Sandeen xfs_set_maxicount(xfs_mount_t *mp) 9340771fb45SEric Sandeen { 9350771fb45SEric Sandeen xfs_sb_t *sbp = &(mp->m_sb); 9361da177e4SLinus Torvalds __uint64_t icount; 9371da177e4SLinus Torvalds 9380771fb45SEric Sandeen if (sbp->sb_imax_pct) { 9390771fb45SEric Sandeen /* 9400771fb45SEric Sandeen * Make sure the maximum inode count is a multiple 9410771fb45SEric Sandeen * of the units we allocate inodes in. 9421da177e4SLinus Torvalds */ 9431da177e4SLinus Torvalds icount = sbp->sb_dblocks * sbp->sb_imax_pct; 9441da177e4SLinus Torvalds do_div(icount, 100); 9451da177e4SLinus Torvalds do_div(icount, mp->m_ialloc_blks); 9461da177e4SLinus Torvalds mp->m_maxicount = (icount * mp->m_ialloc_blks) << 9471da177e4SLinus Torvalds sbp->sb_inopblog; 9480771fb45SEric Sandeen } else { 9491da177e4SLinus Torvalds mp->m_maxicount = 0; 9501da177e4SLinus Torvalds } 9511da177e4SLinus Torvalds } 9521da177e4SLinus Torvalds 9531da177e4SLinus Torvalds /* 9541da177e4SLinus Torvalds * Set the default minimum read and write sizes unless 9551da177e4SLinus Torvalds * already specified in a mount option. 9561da177e4SLinus Torvalds * We use smaller I/O sizes when the file system 9571da177e4SLinus Torvalds * is being used for NFS service (wsync mount option). 9581da177e4SLinus Torvalds */ 9590771fb45SEric Sandeen STATIC void 9600771fb45SEric Sandeen xfs_set_rw_sizes(xfs_mount_t *mp) 9610771fb45SEric Sandeen { 9620771fb45SEric Sandeen xfs_sb_t *sbp = &(mp->m_sb); 9630771fb45SEric Sandeen int readio_log, writeio_log; 9640771fb45SEric Sandeen 9651da177e4SLinus Torvalds if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)) { 9661da177e4SLinus Torvalds if (mp->m_flags & XFS_MOUNT_WSYNC) { 9671da177e4SLinus Torvalds readio_log = XFS_WSYNC_READIO_LOG; 9681da177e4SLinus Torvalds writeio_log = XFS_WSYNC_WRITEIO_LOG; 9691da177e4SLinus Torvalds } else { 9701da177e4SLinus Torvalds readio_log = XFS_READIO_LOG_LARGE; 9711da177e4SLinus Torvalds writeio_log = XFS_WRITEIO_LOG_LARGE; 9721da177e4SLinus Torvalds } 9731da177e4SLinus Torvalds } else { 9741da177e4SLinus Torvalds readio_log = mp->m_readio_log; 9751da177e4SLinus Torvalds writeio_log = mp->m_writeio_log; 9761da177e4SLinus Torvalds } 9771da177e4SLinus Torvalds 9781da177e4SLinus Torvalds if (sbp->sb_blocklog > readio_log) { 9791da177e4SLinus Torvalds mp->m_readio_log = sbp->sb_blocklog; 9801da177e4SLinus Torvalds } else { 9811da177e4SLinus Torvalds mp->m_readio_log = readio_log; 9821da177e4SLinus Torvalds } 9831da177e4SLinus Torvalds mp->m_readio_blocks = 1 << (mp->m_readio_log - sbp->sb_blocklog); 9841da177e4SLinus Torvalds if (sbp->sb_blocklog > writeio_log) { 9851da177e4SLinus Torvalds mp->m_writeio_log = sbp->sb_blocklog; 9861da177e4SLinus Torvalds } else { 9871da177e4SLinus Torvalds mp->m_writeio_log = writeio_log; 9881da177e4SLinus Torvalds } 9891da177e4SLinus Torvalds mp->m_writeio_blocks = 1 << (mp->m_writeio_log - sbp->sb_blocklog); 9900771fb45SEric Sandeen } 991425f9dddSEric Sandeen 9921da177e4SLinus Torvalds /* 993055388a3SDave Chinner * precalculate the low space thresholds for dynamic speculative preallocation. 994055388a3SDave Chinner */ 995055388a3SDave Chinner void 996055388a3SDave Chinner xfs_set_low_space_thresholds( 997055388a3SDave Chinner struct xfs_mount *mp) 998055388a3SDave Chinner { 999055388a3SDave Chinner int i; 1000055388a3SDave Chinner 1001055388a3SDave Chinner for (i = 0; i < XFS_LOWSP_MAX; i++) { 1002055388a3SDave Chinner __uint64_t space = mp->m_sb.sb_dblocks; 1003055388a3SDave Chinner 1004055388a3SDave Chinner do_div(space, 100); 1005055388a3SDave Chinner mp->m_low_space[i] = space * (i + 1); 1006055388a3SDave Chinner } 1007055388a3SDave Chinner } 1008055388a3SDave Chinner 1009055388a3SDave Chinner 1010055388a3SDave Chinner /* 10111da177e4SLinus Torvalds * Set whether we're using inode alignment. 10121da177e4SLinus Torvalds */ 10130771fb45SEric Sandeen STATIC void 10140771fb45SEric Sandeen xfs_set_inoalignment(xfs_mount_t *mp) 10150771fb45SEric Sandeen { 101662118709SEric Sandeen if (xfs_sb_version_hasalign(&mp->m_sb) && 10171da177e4SLinus Torvalds mp->m_sb.sb_inoalignmt >= 10181da177e4SLinus Torvalds XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size)) 10191da177e4SLinus Torvalds mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1; 10201da177e4SLinus Torvalds else 10211da177e4SLinus Torvalds mp->m_inoalign_mask = 0; 10221da177e4SLinus Torvalds /* 10231da177e4SLinus Torvalds * If we are using stripe alignment, check whether 10241da177e4SLinus Torvalds * the stripe unit is a multiple of the inode alignment 10251da177e4SLinus Torvalds */ 10261da177e4SLinus Torvalds if (mp->m_dalign && mp->m_inoalign_mask && 10271da177e4SLinus Torvalds !(mp->m_dalign & mp->m_inoalign_mask)) 10281da177e4SLinus Torvalds mp->m_sinoalign = mp->m_dalign; 10291da177e4SLinus Torvalds else 10301da177e4SLinus Torvalds mp->m_sinoalign = 0; 10310771fb45SEric Sandeen } 10320771fb45SEric Sandeen 10331da177e4SLinus Torvalds /* 10341da177e4SLinus Torvalds * Check that the data (and log if separate) are an ok size. 10351da177e4SLinus Torvalds */ 10360771fb45SEric Sandeen STATIC int 10374249023aSChristoph Hellwig xfs_check_sizes(xfs_mount_t *mp) 10380771fb45SEric Sandeen { 10390771fb45SEric Sandeen xfs_buf_t *bp; 10400771fb45SEric Sandeen xfs_daddr_t d; 10410771fb45SEric Sandeen 10421da177e4SLinus Torvalds d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks); 10431da177e4SLinus Torvalds if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) { 10441922c949SDave Chinner cmn_err(CE_WARN, "XFS: filesystem size mismatch detected"); 1045657a4cffSEric Sandeen return XFS_ERROR(EFBIG); 10461da177e4SLinus Torvalds } 10471922c949SDave Chinner bp = xfs_buf_read_uncached(mp, mp->m_ddev_targp, 10481da177e4SLinus Torvalds d - XFS_FSS_TO_BB(mp, 1), 10491922c949SDave Chinner BBTOB(XFS_FSS_TO_BB(mp, 1)), 0); 10501922c949SDave Chinner if (!bp) { 10511922c949SDave Chinner cmn_err(CE_WARN, "XFS: last sector read failed"); 10521922c949SDave Chinner return EIO; 10531da177e4SLinus Torvalds } 10541922c949SDave Chinner xfs_buf_relse(bp); 10551da177e4SLinus Torvalds 10564249023aSChristoph Hellwig if (mp->m_logdev_targp != mp->m_ddev_targp) { 10571da177e4SLinus Torvalds d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks); 10581da177e4SLinus Torvalds if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) { 10591922c949SDave Chinner cmn_err(CE_WARN, "XFS: log size mismatch detected"); 1060657a4cffSEric Sandeen return XFS_ERROR(EFBIG); 10611da177e4SLinus Torvalds } 10621922c949SDave Chinner bp = xfs_buf_read_uncached(mp, mp->m_logdev_targp, 10631da177e4SLinus Torvalds d - XFS_FSB_TO_BB(mp, 1), 10641922c949SDave Chinner XFS_FSB_TO_B(mp, 1), 0); 10651922c949SDave Chinner if (!bp) { 10661922c949SDave Chinner cmn_err(CE_WARN, "XFS: log device read failed"); 10671922c949SDave Chinner return EIO; 10681da177e4SLinus Torvalds } 10691922c949SDave Chinner xfs_buf_relse(bp); 10700771fb45SEric Sandeen } 10710771fb45SEric Sandeen return 0; 10720771fb45SEric Sandeen } 10730771fb45SEric Sandeen 10740771fb45SEric Sandeen /* 10757d095257SChristoph Hellwig * Clear the quotaflags in memory and in the superblock. 10767d095257SChristoph Hellwig */ 10777d095257SChristoph Hellwig int 10787d095257SChristoph Hellwig xfs_mount_reset_sbqflags( 10797d095257SChristoph Hellwig struct xfs_mount *mp) 10807d095257SChristoph Hellwig { 10817d095257SChristoph Hellwig int error; 10827d095257SChristoph Hellwig struct xfs_trans *tp; 10837d095257SChristoph Hellwig 10847d095257SChristoph Hellwig mp->m_qflags = 0; 10857d095257SChristoph Hellwig 10867d095257SChristoph Hellwig /* 10877d095257SChristoph Hellwig * It is OK to look at sb_qflags here in mount path, 10887d095257SChristoph Hellwig * without m_sb_lock. 10897d095257SChristoph Hellwig */ 10907d095257SChristoph Hellwig if (mp->m_sb.sb_qflags == 0) 10917d095257SChristoph Hellwig return 0; 10927d095257SChristoph Hellwig spin_lock(&mp->m_sb_lock); 10937d095257SChristoph Hellwig mp->m_sb.sb_qflags = 0; 10947d095257SChristoph Hellwig spin_unlock(&mp->m_sb_lock); 10957d095257SChristoph Hellwig 10967d095257SChristoph Hellwig /* 10977d095257SChristoph Hellwig * If the fs is readonly, let the incore superblock run 10987d095257SChristoph Hellwig * with quotas off but don't flush the update out to disk 10997d095257SChristoph Hellwig */ 11007d095257SChristoph Hellwig if (mp->m_flags & XFS_MOUNT_RDONLY) 11017d095257SChristoph Hellwig return 0; 11027d095257SChristoph Hellwig 11037d095257SChristoph Hellwig #ifdef QUOTADEBUG 1104*53487786SDave Chinner xfs_notice(mp, "Writing superblock quota changes"); 11057d095257SChristoph Hellwig #endif 11067d095257SChristoph Hellwig 11077d095257SChristoph Hellwig tp = xfs_trans_alloc(mp, XFS_TRANS_QM_SBCHANGE); 11087d095257SChristoph Hellwig error = xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0, 11097d095257SChristoph Hellwig XFS_DEFAULT_LOG_COUNT); 11107d095257SChristoph Hellwig if (error) { 11117d095257SChristoph Hellwig xfs_trans_cancel(tp, 0); 1112*53487786SDave Chinner xfs_alert(mp, "%s: Superblock update failed!", __func__); 11137d095257SChristoph Hellwig return error; 11147d095257SChristoph Hellwig } 11157d095257SChristoph Hellwig 11167d095257SChristoph Hellwig xfs_mod_sb(tp, XFS_SB_QFLAGS); 11177d095257SChristoph Hellwig return xfs_trans_commit(tp, 0); 11187d095257SChristoph Hellwig } 11197d095257SChristoph Hellwig 1120d5db0f97SEric Sandeen __uint64_t 1121d5db0f97SEric Sandeen xfs_default_resblks(xfs_mount_t *mp) 1122d5db0f97SEric Sandeen { 1123d5db0f97SEric Sandeen __uint64_t resblks; 1124d5db0f97SEric Sandeen 1125d5db0f97SEric Sandeen /* 11268babd8a2SDave Chinner * We default to 5% or 8192 fsbs of space reserved, whichever is 11278babd8a2SDave Chinner * smaller. This is intended to cover concurrent allocation 11288babd8a2SDave Chinner * transactions when we initially hit enospc. These each require a 4 11298babd8a2SDave Chinner * block reservation. Hence by default we cover roughly 2000 concurrent 11308babd8a2SDave Chinner * allocation reservations. 1131d5db0f97SEric Sandeen */ 1132d5db0f97SEric Sandeen resblks = mp->m_sb.sb_dblocks; 1133d5db0f97SEric Sandeen do_div(resblks, 20); 11348babd8a2SDave Chinner resblks = min_t(__uint64_t, resblks, 8192); 1135d5db0f97SEric Sandeen return resblks; 1136d5db0f97SEric Sandeen } 1137d5db0f97SEric Sandeen 11387d095257SChristoph Hellwig /* 11390771fb45SEric Sandeen * This function does the following on an initial mount of a file system: 11400771fb45SEric Sandeen * - reads the superblock from disk and init the mount struct 11410771fb45SEric Sandeen * - if we're a 32-bit kernel, do a size check on the superblock 11420771fb45SEric Sandeen * so we don't mount terabyte filesystems 11430771fb45SEric Sandeen * - init mount struct realtime fields 11440771fb45SEric Sandeen * - allocate inode hash table for fs 11450771fb45SEric Sandeen * - init directory manager 11460771fb45SEric Sandeen * - perform recovery and init the log manager 11470771fb45SEric Sandeen */ 11480771fb45SEric Sandeen int 11490771fb45SEric Sandeen xfs_mountfs( 11504249023aSChristoph Hellwig xfs_mount_t *mp) 11510771fb45SEric Sandeen { 11520771fb45SEric Sandeen xfs_sb_t *sbp = &(mp->m_sb); 11530771fb45SEric Sandeen xfs_inode_t *rip; 11540771fb45SEric Sandeen __uint64_t resblks; 11557d095257SChristoph Hellwig uint quotamount = 0; 11567d095257SChristoph Hellwig uint quotaflags = 0; 11570771fb45SEric Sandeen int error = 0; 11580771fb45SEric Sandeen 11590771fb45SEric Sandeen xfs_mount_common(mp, sbp); 11600771fb45SEric Sandeen 11610771fb45SEric Sandeen /* 1162e6957ea4SEric Sandeen * Check for a mismatched features2 values. Older kernels 1163e6957ea4SEric Sandeen * read & wrote into the wrong sb offset for sb_features2 1164e6957ea4SEric Sandeen * on some platforms due to xfs_sb_t not being 64bit size aligned 1165e6957ea4SEric Sandeen * when sb_features2 was added, which made older superblock 1166e6957ea4SEric Sandeen * reading/writing routines swap it as a 64-bit value. 1167ee1c0908SDavid Chinner * 1168e6957ea4SEric Sandeen * For backwards compatibility, we make both slots equal. 1169e6957ea4SEric Sandeen * 1170e6957ea4SEric Sandeen * If we detect a mismatched field, we OR the set bits into the 1171e6957ea4SEric Sandeen * existing features2 field in case it has already been modified; we 1172e6957ea4SEric Sandeen * don't want to lose any features. We then update the bad location 1173e6957ea4SEric Sandeen * with the ORed value so that older kernels will see any features2 1174e6957ea4SEric Sandeen * flags, and mark the two fields as needing updates once the 1175e6957ea4SEric Sandeen * transaction subsystem is online. 1176ee1c0908SDavid Chinner */ 1177e6957ea4SEric Sandeen if (xfs_sb_has_mismatched_features2(sbp)) { 1178ee1c0908SDavid Chinner cmn_err(CE_WARN, 1179ee1c0908SDavid Chinner "XFS: correcting sb_features alignment problem"); 1180ee1c0908SDavid Chinner sbp->sb_features2 |= sbp->sb_bad_features2; 1181e6957ea4SEric Sandeen sbp->sb_bad_features2 = sbp->sb_features2; 11827884bc86SChristoph Hellwig mp->m_update_flags |= XFS_SB_FEATURES2 | XFS_SB_BAD_FEATURES2; 1183e6957ea4SEric Sandeen 1184e6957ea4SEric Sandeen /* 1185e6957ea4SEric Sandeen * Re-check for ATTR2 in case it was found in bad_features2 1186e6957ea4SEric Sandeen * slot. 1187e6957ea4SEric Sandeen */ 11887c12f296STim Shimmin if (xfs_sb_version_hasattr2(&mp->m_sb) && 11897c12f296STim Shimmin !(mp->m_flags & XFS_MOUNT_NOATTR2)) 1190e6957ea4SEric Sandeen mp->m_flags |= XFS_MOUNT_ATTR2; 11917c12f296STim Shimmin } 1192e6957ea4SEric Sandeen 11937c12f296STim Shimmin if (xfs_sb_version_hasattr2(&mp->m_sb) && 11947c12f296STim Shimmin (mp->m_flags & XFS_MOUNT_NOATTR2)) { 11957c12f296STim Shimmin xfs_sb_version_removeattr2(&mp->m_sb); 11967884bc86SChristoph Hellwig mp->m_update_flags |= XFS_SB_FEATURES2; 11977c12f296STim Shimmin 11987c12f296STim Shimmin /* update sb_versionnum for the clearing of the morebits */ 11997c12f296STim Shimmin if (!sbp->sb_features2) 12007884bc86SChristoph Hellwig mp->m_update_flags |= XFS_SB_VERSIONNUM; 1201ee1c0908SDavid Chinner } 1202ee1c0908SDavid Chinner 1203ee1c0908SDavid Chinner /* 12040771fb45SEric Sandeen * Check if sb_agblocks is aligned at stripe boundary 12050771fb45SEric Sandeen * If sb_agblocks is NOT aligned turn off m_dalign since 12060771fb45SEric Sandeen * allocator alignment is within an ag, therefore ag has 12070771fb45SEric Sandeen * to be aligned at stripe boundary. 12080771fb45SEric Sandeen */ 12097884bc86SChristoph Hellwig error = xfs_update_alignment(mp); 12100771fb45SEric Sandeen if (error) 1211f9057e3dSChristoph Hellwig goto out; 12120771fb45SEric Sandeen 12130771fb45SEric Sandeen xfs_alloc_compute_maxlevels(mp); 12140771fb45SEric Sandeen xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK); 12150771fb45SEric Sandeen xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK); 12160771fb45SEric Sandeen xfs_ialloc_compute_maxlevels(mp); 12170771fb45SEric Sandeen 12180771fb45SEric Sandeen xfs_set_maxicount(mp); 12190771fb45SEric Sandeen 12200771fb45SEric Sandeen mp->m_maxioffset = xfs_max_file_offset(sbp->sb_blocklog); 12210771fb45SEric Sandeen 122227174203SChristoph Hellwig error = xfs_uuid_mount(mp); 122327174203SChristoph Hellwig if (error) 1224f9057e3dSChristoph Hellwig goto out; 12251da177e4SLinus Torvalds 12261da177e4SLinus Torvalds /* 12270771fb45SEric Sandeen * Set the minimum read and write sizes 12280771fb45SEric Sandeen */ 12290771fb45SEric Sandeen xfs_set_rw_sizes(mp); 12300771fb45SEric Sandeen 1231055388a3SDave Chinner /* set the low space thresholds for dynamic preallocation */ 1232055388a3SDave Chinner xfs_set_low_space_thresholds(mp); 1233055388a3SDave Chinner 12340771fb45SEric Sandeen /* 12350771fb45SEric Sandeen * Set the inode cluster size. 12360771fb45SEric Sandeen * This may still be overridden by the file system 12370771fb45SEric Sandeen * block size if it is larger than the chosen cluster size. 12380771fb45SEric Sandeen */ 12390771fb45SEric Sandeen mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE; 12400771fb45SEric Sandeen 12410771fb45SEric Sandeen /* 12420771fb45SEric Sandeen * Set inode alignment fields 12430771fb45SEric Sandeen */ 12440771fb45SEric Sandeen xfs_set_inoalignment(mp); 12450771fb45SEric Sandeen 12460771fb45SEric Sandeen /* 12470771fb45SEric Sandeen * Check that the data (and log if separate) are an ok size. 12480771fb45SEric Sandeen */ 12494249023aSChristoph Hellwig error = xfs_check_sizes(mp); 12500771fb45SEric Sandeen if (error) 1251f9057e3dSChristoph Hellwig goto out_remove_uuid; 12520771fb45SEric Sandeen 12530771fb45SEric Sandeen /* 12541da177e4SLinus Torvalds * Initialize realtime fields in the mount structure 12551da177e4SLinus Torvalds */ 12560771fb45SEric Sandeen error = xfs_rtmount_init(mp); 12570771fb45SEric Sandeen if (error) { 12581da177e4SLinus Torvalds cmn_err(CE_WARN, "XFS: RT mount failed"); 1259f9057e3dSChristoph Hellwig goto out_remove_uuid; 12601da177e4SLinus Torvalds } 12611da177e4SLinus Torvalds 12621da177e4SLinus Torvalds /* 12631da177e4SLinus Torvalds * Copies the low order bits of the timestamp and the randomly 12641da177e4SLinus Torvalds * set "sequence" number out of a UUID. 12651da177e4SLinus Torvalds */ 12661da177e4SLinus Torvalds uuid_getnodeuniq(&sbp->sb_uuid, mp->m_fixedfsid); 12671da177e4SLinus Torvalds 12681da177e4SLinus Torvalds mp->m_dmevmask = 0; /* not persistent; set after each mount */ 12691da177e4SLinus Torvalds 1270f6c2d1faSNathan Scott xfs_dir_mount(mp); 12711da177e4SLinus Torvalds 12721da177e4SLinus Torvalds /* 12731da177e4SLinus Torvalds * Initialize the attribute manager's entries. 12741da177e4SLinus Torvalds */ 12751da177e4SLinus Torvalds mp->m_attr_magicpct = (mp->m_sb.sb_blocksize * 37) / 100; 12761da177e4SLinus Torvalds 12771da177e4SLinus Torvalds /* 12781da177e4SLinus Torvalds * Initialize the precomputed transaction reservations values. 12791da177e4SLinus Torvalds */ 12801da177e4SLinus Torvalds xfs_trans_init(mp); 12811da177e4SLinus Torvalds 12821da177e4SLinus Torvalds /* 12831da177e4SLinus Torvalds * Allocate and initialize the per-ag data. 12841da177e4SLinus Torvalds */ 12851c1c6ebcSDave Chinner spin_lock_init(&mp->m_perag_lock); 12869b98b6f3SDave Chinner INIT_RADIX_TREE(&mp->m_perag_tree, GFP_ATOMIC); 12871c1c6ebcSDave Chinner error = xfs_initialize_perag(mp, sbp->sb_agcount, &mp->m_maxagi); 12881c1c6ebcSDave Chinner if (error) { 12891c1c6ebcSDave Chinner cmn_err(CE_WARN, "XFS: Failed per-ag init: %d", error); 1290f9057e3dSChristoph Hellwig goto out_remove_uuid; 12911c1c6ebcSDave Chinner } 12921da177e4SLinus Torvalds 1293f9057e3dSChristoph Hellwig if (!sbp->sb_logblocks) { 1294f9057e3dSChristoph Hellwig cmn_err(CE_WARN, "XFS: no log defined"); 1295f9057e3dSChristoph Hellwig XFS_ERROR_REPORT("xfs_mountfs", XFS_ERRLEVEL_LOW, mp); 1296f9057e3dSChristoph Hellwig error = XFS_ERROR(EFSCORRUPTED); 1297f9057e3dSChristoph Hellwig goto out_free_perag; 1298f9057e3dSChristoph Hellwig } 1299f9057e3dSChristoph Hellwig 13001da177e4SLinus Torvalds /* 13011da177e4SLinus Torvalds * log's mount-time initialization. Perform 1st part recovery if needed 13021da177e4SLinus Torvalds */ 13031da177e4SLinus Torvalds error = xfs_log_mount(mp, mp->m_logdev_targp, 13041da177e4SLinus Torvalds XFS_FSB_TO_DADDR(mp, sbp->sb_logstart), 13051da177e4SLinus Torvalds XFS_FSB_TO_BB(mp, sbp->sb_logblocks)); 13061da177e4SLinus Torvalds if (error) { 13071da177e4SLinus Torvalds cmn_err(CE_WARN, "XFS: log mount failed"); 1308f9057e3dSChristoph Hellwig goto out_free_perag; 13091da177e4SLinus Torvalds } 13101da177e4SLinus Torvalds 13111da177e4SLinus Torvalds /* 131292821e2bSDavid Chinner * Now the log is mounted, we know if it was an unclean shutdown or 131392821e2bSDavid Chinner * not. If it was, with the first phase of recovery has completed, we 131492821e2bSDavid Chinner * have consistent AG blocks on disk. We have not recovered EFIs yet, 131592821e2bSDavid Chinner * but they are recovered transactionally in the second recovery phase 131692821e2bSDavid Chinner * later. 131792821e2bSDavid Chinner * 131892821e2bSDavid Chinner * Hence we can safely re-initialise incore superblock counters from 131992821e2bSDavid Chinner * the per-ag data. These may not be correct if the filesystem was not 132092821e2bSDavid Chinner * cleanly unmounted, so we need to wait for recovery to finish before 132192821e2bSDavid Chinner * doing this. 132292821e2bSDavid Chinner * 132392821e2bSDavid Chinner * If the filesystem was cleanly unmounted, then we can trust the 132492821e2bSDavid Chinner * values in the superblock to be correct and we don't need to do 132592821e2bSDavid Chinner * anything here. 132692821e2bSDavid Chinner * 132792821e2bSDavid Chinner * If we are currently making the filesystem, the initialisation will 132892821e2bSDavid Chinner * fail as the perag data is in an undefined state. 132992821e2bSDavid Chinner */ 133092821e2bSDavid Chinner if (xfs_sb_version_haslazysbcount(&mp->m_sb) && 133192821e2bSDavid Chinner !XFS_LAST_UNMOUNT_WAS_CLEAN(mp) && 133292821e2bSDavid Chinner !mp->m_sb.sb_inprogress) { 133392821e2bSDavid Chinner error = xfs_initialize_perag_data(mp, sbp->sb_agcount); 1334f9057e3dSChristoph Hellwig if (error) 1335f9057e3dSChristoph Hellwig goto out_free_perag; 133692821e2bSDavid Chinner } 1337f9057e3dSChristoph Hellwig 133892821e2bSDavid Chinner /* 13391da177e4SLinus Torvalds * Get and sanity-check the root inode. 13401da177e4SLinus Torvalds * Save the pointer to it in the mount structure. 13411da177e4SLinus Torvalds */ 13427b6259e7SDave Chinner error = xfs_iget(mp, NULL, sbp->sb_rootino, 0, XFS_ILOCK_EXCL, &rip); 13431da177e4SLinus Torvalds if (error) { 13441da177e4SLinus Torvalds cmn_err(CE_WARN, "XFS: failed to read root inode"); 1345f9057e3dSChristoph Hellwig goto out_log_dealloc; 13461da177e4SLinus Torvalds } 13471da177e4SLinus Torvalds 13481da177e4SLinus Torvalds ASSERT(rip != NULL); 13491da177e4SLinus Torvalds 13501da177e4SLinus Torvalds if (unlikely((rip->i_d.di_mode & S_IFMT) != S_IFDIR)) { 13511da177e4SLinus Torvalds cmn_err(CE_WARN, "XFS: corrupted root inode"); 1352b6574520SNathan Scott cmn_err(CE_WARN, "Device %s - root %llu is not a directory", 1353b6574520SNathan Scott XFS_BUFTARG_NAME(mp->m_ddev_targp), 1354b6574520SNathan Scott (unsigned long long)rip->i_ino); 13551da177e4SLinus Torvalds xfs_iunlock(rip, XFS_ILOCK_EXCL); 13561da177e4SLinus Torvalds XFS_ERROR_REPORT("xfs_mountfs_int(2)", XFS_ERRLEVEL_LOW, 13571da177e4SLinus Torvalds mp); 13581da177e4SLinus Torvalds error = XFS_ERROR(EFSCORRUPTED); 1359f9057e3dSChristoph Hellwig goto out_rele_rip; 13601da177e4SLinus Torvalds } 13611da177e4SLinus Torvalds mp->m_rootip = rip; /* save it */ 13621da177e4SLinus Torvalds 13631da177e4SLinus Torvalds xfs_iunlock(rip, XFS_ILOCK_EXCL); 13641da177e4SLinus Torvalds 13651da177e4SLinus Torvalds /* 13661da177e4SLinus Torvalds * Initialize realtime inode pointers in the mount structure 13671da177e4SLinus Torvalds */ 13680771fb45SEric Sandeen error = xfs_rtmount_inodes(mp); 13690771fb45SEric Sandeen if (error) { 13701da177e4SLinus Torvalds /* 13711da177e4SLinus Torvalds * Free up the root inode. 13721da177e4SLinus Torvalds */ 13731da177e4SLinus Torvalds cmn_err(CE_WARN, "XFS: failed to read RT inodes"); 1374f9057e3dSChristoph Hellwig goto out_rele_rip; 13751da177e4SLinus Torvalds } 13761da177e4SLinus Torvalds 13771da177e4SLinus Torvalds /* 13787884bc86SChristoph Hellwig * If this is a read-only mount defer the superblock updates until 13797884bc86SChristoph Hellwig * the next remount into writeable mode. Otherwise we would never 13807884bc86SChristoph Hellwig * perform the update e.g. for the root filesystem. 13811da177e4SLinus Torvalds */ 13827884bc86SChristoph Hellwig if (mp->m_update_flags && !(mp->m_flags & XFS_MOUNT_RDONLY)) { 13837884bc86SChristoph Hellwig error = xfs_mount_log_sb(mp, mp->m_update_flags); 1384e5720eecSDavid Chinner if (error) { 1385e5720eecSDavid Chinner cmn_err(CE_WARN, "XFS: failed to write sb changes"); 1386b93b6e43SChristoph Hellwig goto out_rtunmount; 1387e5720eecSDavid Chinner } 1388e5720eecSDavid Chinner } 13891da177e4SLinus Torvalds 13901da177e4SLinus Torvalds /* 13911da177e4SLinus Torvalds * Initialise the XFS quota management subsystem for this mount 13921da177e4SLinus Torvalds */ 13937d095257SChristoph Hellwig if (XFS_IS_QUOTA_RUNNING(mp)) { 13947d095257SChristoph Hellwig error = xfs_qm_newmount(mp, "amount, "aflags); 13950771fb45SEric Sandeen if (error) 1396b93b6e43SChristoph Hellwig goto out_rtunmount; 13977d095257SChristoph Hellwig } else { 13987d095257SChristoph Hellwig ASSERT(!XFS_IS_QUOTA_ON(mp)); 13997d095257SChristoph Hellwig 14007d095257SChristoph Hellwig /* 14017d095257SChristoph Hellwig * If a file system had quotas running earlier, but decided to 14027d095257SChristoph Hellwig * mount without -o uquota/pquota/gquota options, revoke the 14037d095257SChristoph Hellwig * quotachecked license. 14047d095257SChristoph Hellwig */ 14057d095257SChristoph Hellwig if (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_ACCT) { 14067d095257SChristoph Hellwig cmn_err(CE_NOTE, 14077d095257SChristoph Hellwig "XFS: resetting qflags for filesystem %s", 14087d095257SChristoph Hellwig mp->m_fsname); 14097d095257SChristoph Hellwig 14107d095257SChristoph Hellwig error = xfs_mount_reset_sbqflags(mp); 14117d095257SChristoph Hellwig if (error) 14127d095257SChristoph Hellwig return error; 14137d095257SChristoph Hellwig } 14147d095257SChristoph Hellwig } 14151da177e4SLinus Torvalds 14161da177e4SLinus Torvalds /* 14171da177e4SLinus Torvalds * Finish recovering the file system. This part needed to be 14181da177e4SLinus Torvalds * delayed until after the root and real-time bitmap inodes 14191da177e4SLinus Torvalds * were consistently read in. 14201da177e4SLinus Torvalds */ 14214249023aSChristoph Hellwig error = xfs_log_mount_finish(mp); 14221da177e4SLinus Torvalds if (error) { 14231da177e4SLinus Torvalds cmn_err(CE_WARN, "XFS: log mount finish failed"); 1424b93b6e43SChristoph Hellwig goto out_rtunmount; 14251da177e4SLinus Torvalds } 14261da177e4SLinus Torvalds 14271da177e4SLinus Torvalds /* 14281da177e4SLinus Torvalds * Complete the quota initialisation, post-log-replay component. 14291da177e4SLinus Torvalds */ 14307d095257SChristoph Hellwig if (quotamount) { 14317d095257SChristoph Hellwig ASSERT(mp->m_qflags == 0); 14327d095257SChristoph Hellwig mp->m_qflags = quotaflags; 14337d095257SChristoph Hellwig 14347d095257SChristoph Hellwig xfs_qm_mount_quotas(mp); 14357d095257SChristoph Hellwig } 14367d095257SChristoph Hellwig 143784e1e99fSDavid Chinner /* 143884e1e99fSDavid Chinner * Now we are mounted, reserve a small amount of unused space for 143984e1e99fSDavid Chinner * privileged transactions. This is needed so that transaction 144084e1e99fSDavid Chinner * space required for critical operations can dip into this pool 144184e1e99fSDavid Chinner * when at ENOSPC. This is needed for operations like create with 144284e1e99fSDavid Chinner * attr, unwritten extent conversion at ENOSPC, etc. Data allocations 144384e1e99fSDavid Chinner * are not allowed to use this reserved space. 14448babd8a2SDave Chinner * 14458babd8a2SDave Chinner * This may drive us straight to ENOSPC on mount, but that implies 14468babd8a2SDave Chinner * we were already there on the last unmount. Warn if this occurs. 144784e1e99fSDavid Chinner */ 1448d5db0f97SEric Sandeen if (!(mp->m_flags & XFS_MOUNT_RDONLY)) { 1449d5db0f97SEric Sandeen resblks = xfs_default_resblks(mp); 1450714082bcSDavid Chinner error = xfs_reserve_blocks(mp, &resblks, NULL); 1451714082bcSDavid Chinner if (error) 1452d5db0f97SEric Sandeen cmn_err(CE_WARN, "XFS: Unable to allocate reserve " 1453d5db0f97SEric Sandeen "blocks. Continuing without a reserve pool."); 1454d5db0f97SEric Sandeen } 145584e1e99fSDavid Chinner 14561da177e4SLinus Torvalds return 0; 14571da177e4SLinus Torvalds 1458b93b6e43SChristoph Hellwig out_rtunmount: 1459b93b6e43SChristoph Hellwig xfs_rtunmount_inodes(mp); 1460f9057e3dSChristoph Hellwig out_rele_rip: 146143355099SChristoph Hellwig IRELE(rip); 1462f9057e3dSChristoph Hellwig out_log_dealloc: 146321b699c8SChristoph Hellwig xfs_log_unmount(mp); 1464f9057e3dSChristoph Hellwig out_free_perag: 1465ff4f038cSChristoph Hellwig xfs_free_perag(mp); 1466f9057e3dSChristoph Hellwig out_remove_uuid: 146727174203SChristoph Hellwig xfs_uuid_unmount(mp); 1468f9057e3dSChristoph Hellwig out: 14691da177e4SLinus Torvalds return error; 14701da177e4SLinus Torvalds } 14711da177e4SLinus Torvalds 14721da177e4SLinus Torvalds /* 14731da177e4SLinus Torvalds * This flushes out the inodes,dquots and the superblock, unmounts the 14741da177e4SLinus Torvalds * log and makes sure that incore structures are freed. 14751da177e4SLinus Torvalds */ 147641b5c2e7SChristoph Hellwig void 147741b5c2e7SChristoph Hellwig xfs_unmountfs( 147841b5c2e7SChristoph Hellwig struct xfs_mount *mp) 14791da177e4SLinus Torvalds { 148084e1e99fSDavid Chinner __uint64_t resblks; 148141b5c2e7SChristoph Hellwig int error; 14821da177e4SLinus Torvalds 14837d095257SChristoph Hellwig xfs_qm_unmount_quotas(mp); 1484b93b6e43SChristoph Hellwig xfs_rtunmount_inodes(mp); 148577508ec8SChristoph Hellwig IRELE(mp->m_rootip); 148677508ec8SChristoph Hellwig 1487641c56fbSDavid Chinner /* 1488641c56fbSDavid Chinner * We can potentially deadlock here if we have an inode cluster 14899da096fdSMalcolm Parsons * that has been freed has its buffer still pinned in memory because 1490641c56fbSDavid Chinner * the transaction is still sitting in a iclog. The stale inodes 1491641c56fbSDavid Chinner * on that buffer will have their flush locks held until the 1492641c56fbSDavid Chinner * transaction hits the disk and the callbacks run. the inode 1493641c56fbSDavid Chinner * flush takes the flush lock unconditionally and with nothing to 1494641c56fbSDavid Chinner * push out the iclog we will never get that unlocked. hence we 1495641c56fbSDavid Chinner * need to force the log first. 1496641c56fbSDavid Chinner */ 1497a14a348bSChristoph Hellwig xfs_log_force(mp, XFS_LOG_SYNC); 1498c854363eSDave Chinner 1499c854363eSDave Chinner /* 1500c854363eSDave Chinner * Do a delwri reclaim pass first so that as many dirty inodes are 1501c854363eSDave Chinner * queued up for IO as possible. Then flush the buffers before making 1502c854363eSDave Chinner * a synchronous path to catch all the remaining inodes are reclaimed. 1503c854363eSDave Chinner * This makes the reclaim process as quick as possible by avoiding 1504c854363eSDave Chinner * synchronous writeout and blocking on inodes already in the delwri 1505c854363eSDave Chinner * state as much as possible. 1506c854363eSDave Chinner */ 1507c854363eSDave Chinner xfs_reclaim_inodes(mp, 0); 1508c854363eSDave Chinner XFS_bflush(mp->m_ddev_targp); 1509c854363eSDave Chinner xfs_reclaim_inodes(mp, SYNC_WAIT); 15101da177e4SLinus Torvalds 15117d095257SChristoph Hellwig xfs_qm_unmount(mp); 1512a357a121SLachlan McIlroy 15131da177e4SLinus Torvalds /* 15141da177e4SLinus Torvalds * Flush out the log synchronously so that we know for sure 15151da177e4SLinus Torvalds * that nothing is pinned. This is important because bflush() 15161da177e4SLinus Torvalds * will skip pinned buffers. 15171da177e4SLinus Torvalds */ 1518a14a348bSChristoph Hellwig xfs_log_force(mp, XFS_LOG_SYNC); 15191da177e4SLinus Torvalds 15201da177e4SLinus Torvalds xfs_binval(mp->m_ddev_targp); 15211da177e4SLinus Torvalds if (mp->m_rtdev_targp) { 15221da177e4SLinus Torvalds xfs_binval(mp->m_rtdev_targp); 15231da177e4SLinus Torvalds } 15241da177e4SLinus Torvalds 152584e1e99fSDavid Chinner /* 152684e1e99fSDavid Chinner * Unreserve any blocks we have so that when we unmount we don't account 152784e1e99fSDavid Chinner * the reserved free space as used. This is really only necessary for 152884e1e99fSDavid Chinner * lazy superblock counting because it trusts the incore superblock 15299da096fdSMalcolm Parsons * counters to be absolutely correct on clean unmount. 153084e1e99fSDavid Chinner * 153184e1e99fSDavid Chinner * We don't bother correcting this elsewhere for lazy superblock 153284e1e99fSDavid Chinner * counting because on mount of an unclean filesystem we reconstruct the 153384e1e99fSDavid Chinner * correct counter value and this is irrelevant. 153484e1e99fSDavid Chinner * 153584e1e99fSDavid Chinner * For non-lazy counter filesystems, this doesn't matter at all because 153684e1e99fSDavid Chinner * we only every apply deltas to the superblock and hence the incore 153784e1e99fSDavid Chinner * value does not matter.... 153884e1e99fSDavid Chinner */ 153984e1e99fSDavid Chinner resblks = 0; 1540714082bcSDavid Chinner error = xfs_reserve_blocks(mp, &resblks, NULL); 1541714082bcSDavid Chinner if (error) 1542714082bcSDavid Chinner cmn_err(CE_WARN, "XFS: Unable to free reserved block pool. " 1543714082bcSDavid Chinner "Freespace may not be correct on next mount."); 1544714082bcSDavid Chinner 1545e5720eecSDavid Chinner error = xfs_log_sbcount(mp, 1); 1546e5720eecSDavid Chinner if (error) 1547e5720eecSDavid Chinner cmn_err(CE_WARN, "XFS: Unable to update superblock counters. " 1548e5720eecSDavid Chinner "Freespace may not be correct on next mount."); 15491da177e4SLinus Torvalds xfs_unmountfs_writesb(mp); 15501da177e4SLinus Torvalds xfs_unmountfs_wait(mp); /* wait for async bufs */ 155121b699c8SChristoph Hellwig xfs_log_unmount_write(mp); 155221b699c8SChristoph Hellwig xfs_log_unmount(mp); 155327174203SChristoph Hellwig xfs_uuid_unmount(mp); 15541da177e4SLinus Torvalds 15551550d0b0SChristoph Hellwig #if defined(DEBUG) 15560ce4cfd4SChristoph Hellwig xfs_errortag_clearall(mp, 0); 15571da177e4SLinus Torvalds #endif 1558ff4f038cSChristoph Hellwig xfs_free_perag(mp); 15591da177e4SLinus Torvalds } 15601da177e4SLinus Torvalds 1561ba0f32d4SChristoph Hellwig STATIC void 15621da177e4SLinus Torvalds xfs_unmountfs_wait(xfs_mount_t *mp) 15631da177e4SLinus Torvalds { 15641da177e4SLinus Torvalds if (mp->m_logdev_targp != mp->m_ddev_targp) 15651da177e4SLinus Torvalds xfs_wait_buftarg(mp->m_logdev_targp); 15661da177e4SLinus Torvalds if (mp->m_rtdev_targp) 15671da177e4SLinus Torvalds xfs_wait_buftarg(mp->m_rtdev_targp); 15681da177e4SLinus Torvalds xfs_wait_buftarg(mp->m_ddev_targp); 15691da177e4SLinus Torvalds } 15701da177e4SLinus Torvalds 15711da177e4SLinus Torvalds int 157292821e2bSDavid Chinner xfs_fs_writable(xfs_mount_t *mp) 157392821e2bSDavid Chinner { 1574b267ce99SChristoph Hellwig return !(xfs_test_for_freeze(mp) || XFS_FORCED_SHUTDOWN(mp) || 1575bd186aa9SChristoph Hellwig (mp->m_flags & XFS_MOUNT_RDONLY)); 157692821e2bSDavid Chinner } 157792821e2bSDavid Chinner 157892821e2bSDavid Chinner /* 157992821e2bSDavid Chinner * xfs_log_sbcount 158092821e2bSDavid Chinner * 158192821e2bSDavid Chinner * Called either periodically to keep the on disk superblock values 158292821e2bSDavid Chinner * roughly up to date or from unmount to make sure the values are 158392821e2bSDavid Chinner * correct on a clean unmount. 158492821e2bSDavid Chinner * 158592821e2bSDavid Chinner * Note this code can be called during the process of freezing, so 158692821e2bSDavid Chinner * we may need to use the transaction allocator which does not not 158792821e2bSDavid Chinner * block when the transaction subsystem is in its frozen state. 158892821e2bSDavid Chinner */ 158992821e2bSDavid Chinner int 159092821e2bSDavid Chinner xfs_log_sbcount( 159192821e2bSDavid Chinner xfs_mount_t *mp, 159292821e2bSDavid Chinner uint sync) 159392821e2bSDavid Chinner { 159492821e2bSDavid Chinner xfs_trans_t *tp; 159592821e2bSDavid Chinner int error; 159692821e2bSDavid Chinner 159792821e2bSDavid Chinner if (!xfs_fs_writable(mp)) 159892821e2bSDavid Chinner return 0; 159992821e2bSDavid Chinner 1600d4d90b57SChristoph Hellwig xfs_icsb_sync_counters(mp, 0); 160192821e2bSDavid Chinner 160292821e2bSDavid Chinner /* 160392821e2bSDavid Chinner * we don't need to do this if we are updating the superblock 160492821e2bSDavid Chinner * counters on every modification. 160592821e2bSDavid Chinner */ 160692821e2bSDavid Chinner if (!xfs_sb_version_haslazysbcount(&mp->m_sb)) 160792821e2bSDavid Chinner return 0; 160892821e2bSDavid Chinner 160980641dc6SChristoph Hellwig tp = _xfs_trans_alloc(mp, XFS_TRANS_SB_COUNT, KM_SLEEP); 161092821e2bSDavid Chinner error = xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0, 161192821e2bSDavid Chinner XFS_DEFAULT_LOG_COUNT); 161292821e2bSDavid Chinner if (error) { 161392821e2bSDavid Chinner xfs_trans_cancel(tp, 0); 161492821e2bSDavid Chinner return error; 161592821e2bSDavid Chinner } 161692821e2bSDavid Chinner 161792821e2bSDavid Chinner xfs_mod_sb(tp, XFS_SB_IFREE | XFS_SB_ICOUNT | XFS_SB_FDBLOCKS); 161892821e2bSDavid Chinner if (sync) 161992821e2bSDavid Chinner xfs_trans_set_sync(tp); 1620e5720eecSDavid Chinner error = xfs_trans_commit(tp, 0); 1621e5720eecSDavid Chinner return error; 162292821e2bSDavid Chinner } 162392821e2bSDavid Chinner 162492821e2bSDavid Chinner int 16251da177e4SLinus Torvalds xfs_unmountfs_writesb(xfs_mount_t *mp) 16261da177e4SLinus Torvalds { 16271da177e4SLinus Torvalds xfs_buf_t *sbp; 16281da177e4SLinus Torvalds int error = 0; 16291da177e4SLinus Torvalds 16301da177e4SLinus Torvalds /* 16311da177e4SLinus Torvalds * skip superblock write if fs is read-only, or 16321da177e4SLinus Torvalds * if we are doing a forced umount. 16331da177e4SLinus Torvalds */ 1634bd186aa9SChristoph Hellwig if (!((mp->m_flags & XFS_MOUNT_RDONLY) || 16351da177e4SLinus Torvalds XFS_FORCED_SHUTDOWN(mp))) { 16368d280b98SDavid Chinner 163792821e2bSDavid Chinner sbp = xfs_getsb(mp, 0); 16388d280b98SDavid Chinner 16391da177e4SLinus Torvalds XFS_BUF_UNDONE(sbp); 16401da177e4SLinus Torvalds XFS_BUF_UNREAD(sbp); 16411da177e4SLinus Torvalds XFS_BUF_UNDELAYWRITE(sbp); 16421da177e4SLinus Torvalds XFS_BUF_WRITE(sbp); 16431da177e4SLinus Torvalds XFS_BUF_UNASYNC(sbp); 16441da177e4SLinus Torvalds ASSERT(XFS_BUF_TARGET(sbp) == mp->m_ddev_targp); 16451da177e4SLinus Torvalds xfsbdstrat(mp, sbp); 16461a1a3e97SChristoph Hellwig error = xfs_buf_iowait(sbp); 16471da177e4SLinus Torvalds if (error) 16481da177e4SLinus Torvalds xfs_ioerror_alert("xfs_unmountfs_writesb", 16491da177e4SLinus Torvalds mp, sbp, XFS_BUF_ADDR(sbp)); 16501da177e4SLinus Torvalds xfs_buf_relse(sbp); 165192821e2bSDavid Chinner } 1652014c2544SJesper Juhl return error; 16531da177e4SLinus Torvalds } 16541da177e4SLinus Torvalds 16551da177e4SLinus Torvalds /* 16561da177e4SLinus Torvalds * xfs_mod_sb() can be used to copy arbitrary changes to the 16571da177e4SLinus Torvalds * in-core superblock into the superblock buffer to be logged. 16581da177e4SLinus Torvalds * It does not provide the higher level of locking that is 16591da177e4SLinus Torvalds * needed to protect the in-core superblock from concurrent 16601da177e4SLinus Torvalds * access. 16611da177e4SLinus Torvalds */ 16621da177e4SLinus Torvalds void 16631da177e4SLinus Torvalds xfs_mod_sb(xfs_trans_t *tp, __int64_t fields) 16641da177e4SLinus Torvalds { 16651da177e4SLinus Torvalds xfs_buf_t *bp; 16661da177e4SLinus Torvalds int first; 16671da177e4SLinus Torvalds int last; 16681da177e4SLinus Torvalds xfs_mount_t *mp; 16691da177e4SLinus Torvalds xfs_sb_field_t f; 16701da177e4SLinus Torvalds 16711da177e4SLinus Torvalds ASSERT(fields); 16721da177e4SLinus Torvalds if (!fields) 16731da177e4SLinus Torvalds return; 16741da177e4SLinus Torvalds mp = tp->t_mountp; 16751da177e4SLinus Torvalds bp = xfs_trans_getsb(tp, mp, 0); 16761da177e4SLinus Torvalds first = sizeof(xfs_sb_t); 16771da177e4SLinus Torvalds last = 0; 16781da177e4SLinus Torvalds 16791da177e4SLinus Torvalds /* translate/copy */ 16801da177e4SLinus Torvalds 16812bdf7cd0SChristoph Hellwig xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb, fields); 16821da177e4SLinus Torvalds 16831da177e4SLinus Torvalds /* find modified range */ 1684587aa0feSDave Chinner f = (xfs_sb_field_t)xfs_highbit64((__uint64_t)fields); 1685587aa0feSDave Chinner ASSERT((1LL << f) & XFS_SB_MOD_BITS); 1686587aa0feSDave Chinner last = xfs_sb_info[f + 1].offset - 1; 16871da177e4SLinus Torvalds 16881da177e4SLinus Torvalds f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields); 16891da177e4SLinus Torvalds ASSERT((1LL << f) & XFS_SB_MOD_BITS); 16901da177e4SLinus Torvalds first = xfs_sb_info[f].offset; 16911da177e4SLinus Torvalds 16921da177e4SLinus Torvalds xfs_trans_log_buf(tp, bp, first, last); 16931da177e4SLinus Torvalds } 1694d210a28cSYingping Lu 1695d210a28cSYingping Lu 16961da177e4SLinus Torvalds /* 16971da177e4SLinus Torvalds * xfs_mod_incore_sb_unlocked() is a utility routine common used to apply 16981da177e4SLinus Torvalds * a delta to a specified field in the in-core superblock. Simply 16991da177e4SLinus Torvalds * switch on the field indicated and apply the delta to that field. 17001da177e4SLinus Torvalds * Fields are not allowed to dip below zero, so if the delta would 17011da177e4SLinus Torvalds * do this do not apply it and return EINVAL. 17021da177e4SLinus Torvalds * 17033685c2a1SEric Sandeen * The m_sb_lock must be held when this routine is called. 17041da177e4SLinus Torvalds */ 1705d96f8f89SEric Sandeen STATIC int 170620f4ebf2SDavid Chinner xfs_mod_incore_sb_unlocked( 170720f4ebf2SDavid Chinner xfs_mount_t *mp, 170820f4ebf2SDavid Chinner xfs_sb_field_t field, 170920f4ebf2SDavid Chinner int64_t delta, 171020f4ebf2SDavid Chinner int rsvd) 17111da177e4SLinus Torvalds { 17121da177e4SLinus Torvalds int scounter; /* short counter for 32 bit fields */ 17131da177e4SLinus Torvalds long long lcounter; /* long counter for 64 bit fields */ 17141da177e4SLinus Torvalds long long res_used, rem; 17151da177e4SLinus Torvalds 17161da177e4SLinus Torvalds /* 17171da177e4SLinus Torvalds * With the in-core superblock spin lock held, switch 17181da177e4SLinus Torvalds * on the indicated field. Apply the delta to the 17191da177e4SLinus Torvalds * proper field. If the fields value would dip below 17201da177e4SLinus Torvalds * 0, then do not apply the delta and return EINVAL. 17211da177e4SLinus Torvalds */ 17221da177e4SLinus Torvalds switch (field) { 17231da177e4SLinus Torvalds case XFS_SBS_ICOUNT: 17241da177e4SLinus Torvalds lcounter = (long long)mp->m_sb.sb_icount; 17251da177e4SLinus Torvalds lcounter += delta; 17261da177e4SLinus Torvalds if (lcounter < 0) { 17271da177e4SLinus Torvalds ASSERT(0); 1728014c2544SJesper Juhl return XFS_ERROR(EINVAL); 17291da177e4SLinus Torvalds } 17301da177e4SLinus Torvalds mp->m_sb.sb_icount = lcounter; 1731014c2544SJesper Juhl return 0; 17321da177e4SLinus Torvalds case XFS_SBS_IFREE: 17331da177e4SLinus Torvalds lcounter = (long long)mp->m_sb.sb_ifree; 17341da177e4SLinus Torvalds lcounter += delta; 17351da177e4SLinus Torvalds if (lcounter < 0) { 17361da177e4SLinus Torvalds ASSERT(0); 1737014c2544SJesper Juhl return XFS_ERROR(EINVAL); 17381da177e4SLinus Torvalds } 17391da177e4SLinus Torvalds mp->m_sb.sb_ifree = lcounter; 1740014c2544SJesper Juhl return 0; 17411da177e4SLinus Torvalds case XFS_SBS_FDBLOCKS: 17424be536deSDavid Chinner lcounter = (long long) 17434be536deSDavid Chinner mp->m_sb.sb_fdblocks - XFS_ALLOC_SET_ASIDE(mp); 17441da177e4SLinus Torvalds res_used = (long long)(mp->m_resblks - mp->m_resblks_avail); 17451da177e4SLinus Torvalds 17461da177e4SLinus Torvalds if (delta > 0) { /* Putting blocks back */ 17471da177e4SLinus Torvalds if (res_used > delta) { 17481da177e4SLinus Torvalds mp->m_resblks_avail += delta; 17491da177e4SLinus Torvalds } else { 17501da177e4SLinus Torvalds rem = delta - res_used; 17511da177e4SLinus Torvalds mp->m_resblks_avail = mp->m_resblks; 17521da177e4SLinus Torvalds lcounter += rem; 17531da177e4SLinus Torvalds } 17541da177e4SLinus Torvalds } else { /* Taking blocks away */ 17551da177e4SLinus Torvalds lcounter += delta; 17568babd8a2SDave Chinner if (lcounter >= 0) { 17578babd8a2SDave Chinner mp->m_sb.sb_fdblocks = lcounter + 17588babd8a2SDave Chinner XFS_ALLOC_SET_ASIDE(mp); 17598babd8a2SDave Chinner return 0; 17608babd8a2SDave Chinner } 17611da177e4SLinus Torvalds 17621da177e4SLinus Torvalds /* 17638babd8a2SDave Chinner * We are out of blocks, use any available reserved 17648babd8a2SDave Chinner * blocks if were allowed to. 17651da177e4SLinus Torvalds */ 17668babd8a2SDave Chinner if (!rsvd) 1767014c2544SJesper Juhl return XFS_ERROR(ENOSPC); 17688babd8a2SDave Chinner 17698babd8a2SDave Chinner lcounter = (long long)mp->m_resblks_avail + delta; 17708babd8a2SDave Chinner if (lcounter >= 0) { 17711da177e4SLinus Torvalds mp->m_resblks_avail = lcounter; 1772014c2544SJesper Juhl return 0; 17738babd8a2SDave Chinner } 17748babd8a2SDave Chinner printk_once(KERN_WARNING 17758babd8a2SDave Chinner "Filesystem \"%s\": reserve blocks depleted! " 17768babd8a2SDave Chinner "Consider increasing reserve pool size.", 17778babd8a2SDave Chinner mp->m_fsname); 1778014c2544SJesper Juhl return XFS_ERROR(ENOSPC); 17791da177e4SLinus Torvalds } 17801da177e4SLinus Torvalds 17814be536deSDavid Chinner mp->m_sb.sb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp); 1782014c2544SJesper Juhl return 0; 17831da177e4SLinus Torvalds case XFS_SBS_FREXTENTS: 17841da177e4SLinus Torvalds lcounter = (long long)mp->m_sb.sb_frextents; 17851da177e4SLinus Torvalds lcounter += delta; 17861da177e4SLinus Torvalds if (lcounter < 0) { 1787014c2544SJesper Juhl return XFS_ERROR(ENOSPC); 17881da177e4SLinus Torvalds } 17891da177e4SLinus Torvalds mp->m_sb.sb_frextents = lcounter; 1790014c2544SJesper Juhl return 0; 17911da177e4SLinus Torvalds case XFS_SBS_DBLOCKS: 17921da177e4SLinus Torvalds lcounter = (long long)mp->m_sb.sb_dblocks; 17931da177e4SLinus Torvalds lcounter += delta; 17941da177e4SLinus Torvalds if (lcounter < 0) { 17951da177e4SLinus Torvalds ASSERT(0); 1796014c2544SJesper Juhl return XFS_ERROR(EINVAL); 17971da177e4SLinus Torvalds } 17981da177e4SLinus Torvalds mp->m_sb.sb_dblocks = lcounter; 1799014c2544SJesper Juhl return 0; 18001da177e4SLinus Torvalds case XFS_SBS_AGCOUNT: 18011da177e4SLinus Torvalds scounter = mp->m_sb.sb_agcount; 18021da177e4SLinus Torvalds scounter += delta; 18031da177e4SLinus Torvalds if (scounter < 0) { 18041da177e4SLinus Torvalds ASSERT(0); 1805014c2544SJesper Juhl return XFS_ERROR(EINVAL); 18061da177e4SLinus Torvalds } 18071da177e4SLinus Torvalds mp->m_sb.sb_agcount = scounter; 1808014c2544SJesper Juhl return 0; 18091da177e4SLinus Torvalds case XFS_SBS_IMAX_PCT: 18101da177e4SLinus Torvalds scounter = mp->m_sb.sb_imax_pct; 18111da177e4SLinus Torvalds scounter += delta; 18121da177e4SLinus Torvalds if (scounter < 0) { 18131da177e4SLinus Torvalds ASSERT(0); 1814014c2544SJesper Juhl return XFS_ERROR(EINVAL); 18151da177e4SLinus Torvalds } 18161da177e4SLinus Torvalds mp->m_sb.sb_imax_pct = scounter; 1817014c2544SJesper Juhl return 0; 18181da177e4SLinus Torvalds case XFS_SBS_REXTSIZE: 18191da177e4SLinus Torvalds scounter = mp->m_sb.sb_rextsize; 18201da177e4SLinus Torvalds scounter += delta; 18211da177e4SLinus Torvalds if (scounter < 0) { 18221da177e4SLinus Torvalds ASSERT(0); 1823014c2544SJesper Juhl return XFS_ERROR(EINVAL); 18241da177e4SLinus Torvalds } 18251da177e4SLinus Torvalds mp->m_sb.sb_rextsize = scounter; 1826014c2544SJesper Juhl return 0; 18271da177e4SLinus Torvalds case XFS_SBS_RBMBLOCKS: 18281da177e4SLinus Torvalds scounter = mp->m_sb.sb_rbmblocks; 18291da177e4SLinus Torvalds scounter += delta; 18301da177e4SLinus Torvalds if (scounter < 0) { 18311da177e4SLinus Torvalds ASSERT(0); 1832014c2544SJesper Juhl return XFS_ERROR(EINVAL); 18331da177e4SLinus Torvalds } 18341da177e4SLinus Torvalds mp->m_sb.sb_rbmblocks = scounter; 1835014c2544SJesper Juhl return 0; 18361da177e4SLinus Torvalds case XFS_SBS_RBLOCKS: 18371da177e4SLinus Torvalds lcounter = (long long)mp->m_sb.sb_rblocks; 18381da177e4SLinus Torvalds lcounter += delta; 18391da177e4SLinus Torvalds if (lcounter < 0) { 18401da177e4SLinus Torvalds ASSERT(0); 1841014c2544SJesper Juhl return XFS_ERROR(EINVAL); 18421da177e4SLinus Torvalds } 18431da177e4SLinus Torvalds mp->m_sb.sb_rblocks = lcounter; 1844014c2544SJesper Juhl return 0; 18451da177e4SLinus Torvalds case XFS_SBS_REXTENTS: 18461da177e4SLinus Torvalds lcounter = (long long)mp->m_sb.sb_rextents; 18471da177e4SLinus Torvalds lcounter += delta; 18481da177e4SLinus Torvalds if (lcounter < 0) { 18491da177e4SLinus Torvalds ASSERT(0); 1850014c2544SJesper Juhl return XFS_ERROR(EINVAL); 18511da177e4SLinus Torvalds } 18521da177e4SLinus Torvalds mp->m_sb.sb_rextents = lcounter; 1853014c2544SJesper Juhl return 0; 18541da177e4SLinus Torvalds case XFS_SBS_REXTSLOG: 18551da177e4SLinus Torvalds scounter = mp->m_sb.sb_rextslog; 18561da177e4SLinus Torvalds scounter += delta; 18571da177e4SLinus Torvalds if (scounter < 0) { 18581da177e4SLinus Torvalds ASSERT(0); 1859014c2544SJesper Juhl return XFS_ERROR(EINVAL); 18601da177e4SLinus Torvalds } 18611da177e4SLinus Torvalds mp->m_sb.sb_rextslog = scounter; 1862014c2544SJesper Juhl return 0; 18631da177e4SLinus Torvalds default: 18641da177e4SLinus Torvalds ASSERT(0); 1865014c2544SJesper Juhl return XFS_ERROR(EINVAL); 18661da177e4SLinus Torvalds } 18671da177e4SLinus Torvalds } 18681da177e4SLinus Torvalds 18691da177e4SLinus Torvalds /* 18701da177e4SLinus Torvalds * xfs_mod_incore_sb() is used to change a field in the in-core 18711da177e4SLinus Torvalds * superblock structure by the specified delta. This modification 18723685c2a1SEric Sandeen * is protected by the m_sb_lock. Just use the xfs_mod_incore_sb_unlocked() 18731da177e4SLinus Torvalds * routine to do the work. 18741da177e4SLinus Torvalds */ 18751da177e4SLinus Torvalds int 187620f4ebf2SDavid Chinner xfs_mod_incore_sb( 187796540c78SChristoph Hellwig struct xfs_mount *mp, 187820f4ebf2SDavid Chinner xfs_sb_field_t field, 187920f4ebf2SDavid Chinner int64_t delta, 188020f4ebf2SDavid Chinner int rsvd) 18811da177e4SLinus Torvalds { 18821da177e4SLinus Torvalds int status; 18831da177e4SLinus Torvalds 18848d280b98SDavid Chinner #ifdef HAVE_PERCPU_SB 188596540c78SChristoph Hellwig ASSERT(field < XFS_SBS_ICOUNT || field > XFS_SBS_FDBLOCKS); 18868d280b98SDavid Chinner #endif 18873685c2a1SEric Sandeen spin_lock(&mp->m_sb_lock); 18881da177e4SLinus Torvalds status = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd); 18893685c2a1SEric Sandeen spin_unlock(&mp->m_sb_lock); 18908d280b98SDavid Chinner 1891014c2544SJesper Juhl return status; 18921da177e4SLinus Torvalds } 18931da177e4SLinus Torvalds 18941da177e4SLinus Torvalds /* 18951b040712SChristoph Hellwig * Change more than one field in the in-core superblock structure at a time. 18961da177e4SLinus Torvalds * 18971b040712SChristoph Hellwig * The fields and changes to those fields are specified in the array of 18981b040712SChristoph Hellwig * xfs_mod_sb structures passed in. Either all of the specified deltas 18991b040712SChristoph Hellwig * will be applied or none of them will. If any modified field dips below 0, 19001b040712SChristoph Hellwig * then all modifications will be backed out and EINVAL will be returned. 19011b040712SChristoph Hellwig * 19021b040712SChristoph Hellwig * Note that this function may not be used for the superblock values that 19031b040712SChristoph Hellwig * are tracked with the in-memory per-cpu counters - a direct call to 19041b040712SChristoph Hellwig * xfs_icsb_modify_counters is required for these. 19051da177e4SLinus Torvalds */ 19061da177e4SLinus Torvalds int 19071b040712SChristoph Hellwig xfs_mod_incore_sb_batch( 19081b040712SChristoph Hellwig struct xfs_mount *mp, 19091b040712SChristoph Hellwig xfs_mod_sb_t *msb, 19101b040712SChristoph Hellwig uint nmsb, 19111b040712SChristoph Hellwig int rsvd) 19121da177e4SLinus Torvalds { 19131b040712SChristoph Hellwig xfs_mod_sb_t *msbp = &msb[0]; 19141b040712SChristoph Hellwig int error = 0; 19151da177e4SLinus Torvalds 19161da177e4SLinus Torvalds /* 19171b040712SChristoph Hellwig * Loop through the array of mod structures and apply each individually. 19181b040712SChristoph Hellwig * If any fail, then back out all those which have already been applied. 19191b040712SChristoph Hellwig * Do all of this within the scope of the m_sb_lock so that all of the 19201b040712SChristoph Hellwig * changes will be atomic. 19211da177e4SLinus Torvalds */ 19223685c2a1SEric Sandeen spin_lock(&mp->m_sb_lock); 19231da177e4SLinus Torvalds for (msbp = &msbp[0]; msbp < (msb + nmsb); msbp++) { 19241b040712SChristoph Hellwig ASSERT(msbp->msb_field < XFS_SBS_ICOUNT || 19251b040712SChristoph Hellwig msbp->msb_field > XFS_SBS_FDBLOCKS); 19268d280b98SDavid Chinner 19271b040712SChristoph Hellwig error = xfs_mod_incore_sb_unlocked(mp, msbp->msb_field, 19281b040712SChristoph Hellwig msbp->msb_delta, rsvd); 19291b040712SChristoph Hellwig if (error) 19301b040712SChristoph Hellwig goto unwind; 19311da177e4SLinus Torvalds } 19321b040712SChristoph Hellwig spin_unlock(&mp->m_sb_lock); 19331b040712SChristoph Hellwig return 0; 19341da177e4SLinus Torvalds 19351b040712SChristoph Hellwig unwind: 19361b040712SChristoph Hellwig while (--msbp >= msb) { 19371b040712SChristoph Hellwig error = xfs_mod_incore_sb_unlocked(mp, msbp->msb_field, 19381b040712SChristoph Hellwig -msbp->msb_delta, rsvd); 19391b040712SChristoph Hellwig ASSERT(error == 0); 19401da177e4SLinus Torvalds } 19413685c2a1SEric Sandeen spin_unlock(&mp->m_sb_lock); 19421b040712SChristoph Hellwig return error; 19431da177e4SLinus Torvalds } 19441da177e4SLinus Torvalds 19451da177e4SLinus Torvalds /* 19461da177e4SLinus Torvalds * xfs_getsb() is called to obtain the buffer for the superblock. 19471da177e4SLinus Torvalds * The buffer is returned locked and read in from disk. 19481da177e4SLinus Torvalds * The buffer should be released with a call to xfs_brelse(). 19491da177e4SLinus Torvalds * 19501da177e4SLinus Torvalds * If the flags parameter is BUF_TRYLOCK, then we'll only return 19511da177e4SLinus Torvalds * the superblock buffer if it can be locked without sleeping. 19521da177e4SLinus Torvalds * If it can't then we'll return NULL. 19531da177e4SLinus Torvalds */ 19541da177e4SLinus Torvalds xfs_buf_t * 19551da177e4SLinus Torvalds xfs_getsb( 19561da177e4SLinus Torvalds xfs_mount_t *mp, 19571da177e4SLinus Torvalds int flags) 19581da177e4SLinus Torvalds { 19591da177e4SLinus Torvalds xfs_buf_t *bp; 19601da177e4SLinus Torvalds 19611da177e4SLinus Torvalds ASSERT(mp->m_sb_bp != NULL); 19621da177e4SLinus Torvalds bp = mp->m_sb_bp; 19630cadda1cSChristoph Hellwig if (flags & XBF_TRYLOCK) { 19641da177e4SLinus Torvalds if (!XFS_BUF_CPSEMA(bp)) { 19651da177e4SLinus Torvalds return NULL; 19661da177e4SLinus Torvalds } 19671da177e4SLinus Torvalds } else { 19681da177e4SLinus Torvalds XFS_BUF_PSEMA(bp, PRIBIO); 19691da177e4SLinus Torvalds } 19701da177e4SLinus Torvalds XFS_BUF_HOLD(bp); 19711da177e4SLinus Torvalds ASSERT(XFS_BUF_ISDONE(bp)); 1972014c2544SJesper Juhl return bp; 19731da177e4SLinus Torvalds } 19741da177e4SLinus Torvalds 19751da177e4SLinus Torvalds /* 19761da177e4SLinus Torvalds * Used to free the superblock along various error paths. 19771da177e4SLinus Torvalds */ 19781da177e4SLinus Torvalds void 19791da177e4SLinus Torvalds xfs_freesb( 198026af6552SDave Chinner struct xfs_mount *mp) 19811da177e4SLinus Torvalds { 198226af6552SDave Chinner struct xfs_buf *bp = mp->m_sb_bp; 19831da177e4SLinus Torvalds 198426af6552SDave Chinner xfs_buf_lock(bp); 19851da177e4SLinus Torvalds mp->m_sb_bp = NULL; 198626af6552SDave Chinner xfs_buf_relse(bp); 19871da177e4SLinus Torvalds } 19881da177e4SLinus Torvalds 19891da177e4SLinus Torvalds /* 19901da177e4SLinus Torvalds * Used to log changes to the superblock unit and width fields which could 1991e6957ea4SEric Sandeen * be altered by the mount options, as well as any potential sb_features2 1992e6957ea4SEric Sandeen * fixup. Only the first superblock is updated. 19931da177e4SLinus Torvalds */ 19947884bc86SChristoph Hellwig int 1995ee1c0908SDavid Chinner xfs_mount_log_sb( 19961da177e4SLinus Torvalds xfs_mount_t *mp, 19971da177e4SLinus Torvalds __int64_t fields) 19981da177e4SLinus Torvalds { 19991da177e4SLinus Torvalds xfs_trans_t *tp; 2000e5720eecSDavid Chinner int error; 20011da177e4SLinus Torvalds 2002ee1c0908SDavid Chinner ASSERT(fields & (XFS_SB_UNIT | XFS_SB_WIDTH | XFS_SB_UUID | 20034b166de0SDavid Chinner XFS_SB_FEATURES2 | XFS_SB_BAD_FEATURES2 | 20044b166de0SDavid Chinner XFS_SB_VERSIONNUM)); 20051da177e4SLinus Torvalds 20061da177e4SLinus Torvalds tp = xfs_trans_alloc(mp, XFS_TRANS_SB_UNIT); 2007e5720eecSDavid Chinner error = xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0, 2008e5720eecSDavid Chinner XFS_DEFAULT_LOG_COUNT); 2009e5720eecSDavid Chinner if (error) { 20101da177e4SLinus Torvalds xfs_trans_cancel(tp, 0); 2011e5720eecSDavid Chinner return error; 20121da177e4SLinus Torvalds } 20131da177e4SLinus Torvalds xfs_mod_sb(tp, fields); 2014e5720eecSDavid Chinner error = xfs_trans_commit(tp, 0); 2015e5720eecSDavid Chinner return error; 20161da177e4SLinus Torvalds } 20178d280b98SDavid Chinner 2018dda35b8fSChristoph Hellwig /* 2019dda35b8fSChristoph Hellwig * If the underlying (data/log/rt) device is readonly, there are some 2020dda35b8fSChristoph Hellwig * operations that cannot proceed. 2021dda35b8fSChristoph Hellwig */ 2022dda35b8fSChristoph Hellwig int 2023dda35b8fSChristoph Hellwig xfs_dev_is_read_only( 2024dda35b8fSChristoph Hellwig struct xfs_mount *mp, 2025dda35b8fSChristoph Hellwig char *message) 2026dda35b8fSChristoph Hellwig { 2027dda35b8fSChristoph Hellwig if (xfs_readonly_buftarg(mp->m_ddev_targp) || 2028dda35b8fSChristoph Hellwig xfs_readonly_buftarg(mp->m_logdev_targp) || 2029dda35b8fSChristoph Hellwig (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) { 2030dda35b8fSChristoph Hellwig cmn_err(CE_NOTE, 2031dda35b8fSChristoph Hellwig "XFS: %s required on read-only device.", message); 2032dda35b8fSChristoph Hellwig cmn_err(CE_NOTE, 2033dda35b8fSChristoph Hellwig "XFS: write access unavailable, cannot proceed."); 2034dda35b8fSChristoph Hellwig return EROFS; 2035dda35b8fSChristoph Hellwig } 2036dda35b8fSChristoph Hellwig return 0; 2037dda35b8fSChristoph Hellwig } 20388d280b98SDavid Chinner 20398d280b98SDavid Chinner #ifdef HAVE_PERCPU_SB 20408d280b98SDavid Chinner /* 20418d280b98SDavid Chinner * Per-cpu incore superblock counters 20428d280b98SDavid Chinner * 20438d280b98SDavid Chinner * Simple concept, difficult implementation 20448d280b98SDavid Chinner * 20458d280b98SDavid Chinner * Basically, replace the incore superblock counters with a distributed per cpu 20468d280b98SDavid Chinner * counter for contended fields (e.g. free block count). 20478d280b98SDavid Chinner * 20488d280b98SDavid Chinner * Difficulties arise in that the incore sb is used for ENOSPC checking, and 20498d280b98SDavid Chinner * hence needs to be accurately read when we are running low on space. Hence 20508d280b98SDavid Chinner * there is a method to enable and disable the per-cpu counters based on how 20518d280b98SDavid Chinner * much "stuff" is available in them. 20528d280b98SDavid Chinner * 20538d280b98SDavid Chinner * Basically, a counter is enabled if there is enough free resource to justify 20548d280b98SDavid Chinner * running a per-cpu fast-path. If the per-cpu counter runs out (i.e. a local 20558d280b98SDavid Chinner * ENOSPC), then we disable the counters to synchronise all callers and 20568d280b98SDavid Chinner * re-distribute the available resources. 20578d280b98SDavid Chinner * 20588d280b98SDavid Chinner * If, once we redistributed the available resources, we still get a failure, 20598d280b98SDavid Chinner * we disable the per-cpu counter and go through the slow path. 20608d280b98SDavid Chinner * 20618d280b98SDavid Chinner * The slow path is the current xfs_mod_incore_sb() function. This means that 20629da096fdSMalcolm Parsons * when we disable a per-cpu counter, we need to drain its resources back to 20638d280b98SDavid Chinner * the global superblock. We do this after disabling the counter to prevent 20648d280b98SDavid Chinner * more threads from queueing up on the counter. 20658d280b98SDavid Chinner * 20668d280b98SDavid Chinner * Essentially, this means that we still need a lock in the fast path to enable 20678d280b98SDavid Chinner * synchronisation between the global counters and the per-cpu counters. This 20688d280b98SDavid Chinner * is not a problem because the lock will be local to a CPU almost all the time 20698d280b98SDavid Chinner * and have little contention except when we get to ENOSPC conditions. 20708d280b98SDavid Chinner * 20718d280b98SDavid Chinner * Basically, this lock becomes a barrier that enables us to lock out the fast 20728d280b98SDavid Chinner * path while we do things like enabling and disabling counters and 20738d280b98SDavid Chinner * synchronising the counters. 20748d280b98SDavid Chinner * 20758d280b98SDavid Chinner * Locking rules: 20768d280b98SDavid Chinner * 20773685c2a1SEric Sandeen * 1. m_sb_lock before picking up per-cpu locks 20788d280b98SDavid Chinner * 2. per-cpu locks always picked up via for_each_online_cpu() order 20793685c2a1SEric Sandeen * 3. accurate counter sync requires m_sb_lock + per cpu locks 20808d280b98SDavid Chinner * 4. modifying per-cpu counters requires holding per-cpu lock 20813685c2a1SEric Sandeen * 5. modifying global counters requires holding m_sb_lock 20823685c2a1SEric Sandeen * 6. enabling or disabling a counter requires holding the m_sb_lock 20838d280b98SDavid Chinner * and _none_ of the per-cpu locks. 20848d280b98SDavid Chinner * 20858d280b98SDavid Chinner * Disabled counters are only ever re-enabled by a balance operation 20868d280b98SDavid Chinner * that results in more free resources per CPU than a given threshold. 20878d280b98SDavid Chinner * To ensure counters don't remain disabled, they are rebalanced when 20888d280b98SDavid Chinner * the global resource goes above a higher threshold (i.e. some hysteresis 20898d280b98SDavid Chinner * is present to prevent thrashing). 20908d280b98SDavid Chinner */ 2091e8234a68SDavid Chinner 20925a67e4c5SChandra Seetharaman #ifdef CONFIG_HOTPLUG_CPU 2093e8234a68SDavid Chinner /* 2094e8234a68SDavid Chinner * hot-plug CPU notifier support. 2095e8234a68SDavid Chinner * 20965a67e4c5SChandra Seetharaman * We need a notifier per filesystem as we need to be able to identify 20975a67e4c5SChandra Seetharaman * the filesystem to balance the counters out. This is achieved by 20985a67e4c5SChandra Seetharaman * having a notifier block embedded in the xfs_mount_t and doing pointer 20995a67e4c5SChandra Seetharaman * magic to get the mount pointer from the notifier block address. 2100e8234a68SDavid Chinner */ 2101e8234a68SDavid Chinner STATIC int 2102e8234a68SDavid Chinner xfs_icsb_cpu_notify( 2103e8234a68SDavid Chinner struct notifier_block *nfb, 2104e8234a68SDavid Chinner unsigned long action, 2105e8234a68SDavid Chinner void *hcpu) 2106e8234a68SDavid Chinner { 2107e8234a68SDavid Chinner xfs_icsb_cnts_t *cntp; 2108e8234a68SDavid Chinner xfs_mount_t *mp; 2109e8234a68SDavid Chinner 2110e8234a68SDavid Chinner mp = (xfs_mount_t *)container_of(nfb, xfs_mount_t, m_icsb_notifier); 2111e8234a68SDavid Chinner cntp = (xfs_icsb_cnts_t *) 2112e8234a68SDavid Chinner per_cpu_ptr(mp->m_sb_cnts, (unsigned long)hcpu); 2113e8234a68SDavid Chinner switch (action) { 2114e8234a68SDavid Chinner case CPU_UP_PREPARE: 21158bb78442SRafael J. Wysocki case CPU_UP_PREPARE_FROZEN: 2116e8234a68SDavid Chinner /* Easy Case - initialize the area and locks, and 2117e8234a68SDavid Chinner * then rebalance when online does everything else for us. */ 211801e1b69cSDavid Chinner memset(cntp, 0, sizeof(xfs_icsb_cnts_t)); 2119e8234a68SDavid Chinner break; 2120e8234a68SDavid Chinner case CPU_ONLINE: 21218bb78442SRafael J. Wysocki case CPU_ONLINE_FROZEN: 212203135cf7SDavid Chinner xfs_icsb_lock(mp); 212345af6c6dSChristoph Hellwig xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0); 212445af6c6dSChristoph Hellwig xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0); 212545af6c6dSChristoph Hellwig xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0); 212603135cf7SDavid Chinner xfs_icsb_unlock(mp); 2127e8234a68SDavid Chinner break; 2128e8234a68SDavid Chinner case CPU_DEAD: 21298bb78442SRafael J. Wysocki case CPU_DEAD_FROZEN: 2130e8234a68SDavid Chinner /* Disable all the counters, then fold the dead cpu's 2131e8234a68SDavid Chinner * count into the total on the global superblock and 2132e8234a68SDavid Chinner * re-enable the counters. */ 213303135cf7SDavid Chinner xfs_icsb_lock(mp); 21343685c2a1SEric Sandeen spin_lock(&mp->m_sb_lock); 2135e8234a68SDavid Chinner xfs_icsb_disable_counter(mp, XFS_SBS_ICOUNT); 2136e8234a68SDavid Chinner xfs_icsb_disable_counter(mp, XFS_SBS_IFREE); 2137e8234a68SDavid Chinner xfs_icsb_disable_counter(mp, XFS_SBS_FDBLOCKS); 2138e8234a68SDavid Chinner 2139e8234a68SDavid Chinner mp->m_sb.sb_icount += cntp->icsb_icount; 2140e8234a68SDavid Chinner mp->m_sb.sb_ifree += cntp->icsb_ifree; 2141e8234a68SDavid Chinner mp->m_sb.sb_fdblocks += cntp->icsb_fdblocks; 2142e8234a68SDavid Chinner 214301e1b69cSDavid Chinner memset(cntp, 0, sizeof(xfs_icsb_cnts_t)); 2144e8234a68SDavid Chinner 214545af6c6dSChristoph Hellwig xfs_icsb_balance_counter_locked(mp, XFS_SBS_ICOUNT, 0); 214645af6c6dSChristoph Hellwig xfs_icsb_balance_counter_locked(mp, XFS_SBS_IFREE, 0); 214745af6c6dSChristoph Hellwig xfs_icsb_balance_counter_locked(mp, XFS_SBS_FDBLOCKS, 0); 21483685c2a1SEric Sandeen spin_unlock(&mp->m_sb_lock); 214903135cf7SDavid Chinner xfs_icsb_unlock(mp); 2150e8234a68SDavid Chinner break; 2151e8234a68SDavid Chinner } 2152e8234a68SDavid Chinner 2153e8234a68SDavid Chinner return NOTIFY_OK; 2154e8234a68SDavid Chinner } 21555a67e4c5SChandra Seetharaman #endif /* CONFIG_HOTPLUG_CPU */ 2156e8234a68SDavid Chinner 21578d280b98SDavid Chinner int 21588d280b98SDavid Chinner xfs_icsb_init_counters( 21598d280b98SDavid Chinner xfs_mount_t *mp) 21608d280b98SDavid Chinner { 21618d280b98SDavid Chinner xfs_icsb_cnts_t *cntp; 21628d280b98SDavid Chinner int i; 21638d280b98SDavid Chinner 21648d280b98SDavid Chinner mp->m_sb_cnts = alloc_percpu(xfs_icsb_cnts_t); 21658d280b98SDavid Chinner if (mp->m_sb_cnts == NULL) 21668d280b98SDavid Chinner return -ENOMEM; 21678d280b98SDavid Chinner 21685a67e4c5SChandra Seetharaman #ifdef CONFIG_HOTPLUG_CPU 2169e8234a68SDavid Chinner mp->m_icsb_notifier.notifier_call = xfs_icsb_cpu_notify; 2170e8234a68SDavid Chinner mp->m_icsb_notifier.priority = 0; 21715a67e4c5SChandra Seetharaman register_hotcpu_notifier(&mp->m_icsb_notifier); 21725a67e4c5SChandra Seetharaman #endif /* CONFIG_HOTPLUG_CPU */ 2173e8234a68SDavid Chinner 21748d280b98SDavid Chinner for_each_online_cpu(i) { 21758d280b98SDavid Chinner cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i); 217601e1b69cSDavid Chinner memset(cntp, 0, sizeof(xfs_icsb_cnts_t)); 21778d280b98SDavid Chinner } 217820b64285SDavid Chinner 217920b64285SDavid Chinner mutex_init(&mp->m_icsb_mutex); 218020b64285SDavid Chinner 21818d280b98SDavid Chinner /* 21828d280b98SDavid Chinner * start with all counters disabled so that the 21838d280b98SDavid Chinner * initial balance kicks us off correctly 21848d280b98SDavid Chinner */ 21858d280b98SDavid Chinner mp->m_icsb_counters = -1; 21868d280b98SDavid Chinner return 0; 21878d280b98SDavid Chinner } 21888d280b98SDavid Chinner 21895478eeadSLachlan McIlroy void 21905478eeadSLachlan McIlroy xfs_icsb_reinit_counters( 21915478eeadSLachlan McIlroy xfs_mount_t *mp) 21925478eeadSLachlan McIlroy { 21935478eeadSLachlan McIlroy xfs_icsb_lock(mp); 21945478eeadSLachlan McIlroy /* 21955478eeadSLachlan McIlroy * start with all counters disabled so that the 21965478eeadSLachlan McIlroy * initial balance kicks us off correctly 21975478eeadSLachlan McIlroy */ 21985478eeadSLachlan McIlroy mp->m_icsb_counters = -1; 219945af6c6dSChristoph Hellwig xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0); 220045af6c6dSChristoph Hellwig xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0); 220145af6c6dSChristoph Hellwig xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0); 22025478eeadSLachlan McIlroy xfs_icsb_unlock(mp); 22035478eeadSLachlan McIlroy } 22045478eeadSLachlan McIlroy 2205c962fb79SChristoph Hellwig void 22068d280b98SDavid Chinner xfs_icsb_destroy_counters( 22078d280b98SDavid Chinner xfs_mount_t *mp) 22088d280b98SDavid Chinner { 2209e8234a68SDavid Chinner if (mp->m_sb_cnts) { 22105a67e4c5SChandra Seetharaman unregister_hotcpu_notifier(&mp->m_icsb_notifier); 22118d280b98SDavid Chinner free_percpu(mp->m_sb_cnts); 22128d280b98SDavid Chinner } 221303135cf7SDavid Chinner mutex_destroy(&mp->m_icsb_mutex); 2214e8234a68SDavid Chinner } 22158d280b98SDavid Chinner 2216b8f82a4aSChristoph Hellwig STATIC void 221701e1b69cSDavid Chinner xfs_icsb_lock_cntr( 221801e1b69cSDavid Chinner xfs_icsb_cnts_t *icsbp) 221901e1b69cSDavid Chinner { 222001e1b69cSDavid Chinner while (test_and_set_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags)) { 222101e1b69cSDavid Chinner ndelay(1000); 222201e1b69cSDavid Chinner } 222301e1b69cSDavid Chinner } 222401e1b69cSDavid Chinner 2225b8f82a4aSChristoph Hellwig STATIC void 222601e1b69cSDavid Chinner xfs_icsb_unlock_cntr( 222701e1b69cSDavid Chinner xfs_icsb_cnts_t *icsbp) 222801e1b69cSDavid Chinner { 222901e1b69cSDavid Chinner clear_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags); 223001e1b69cSDavid Chinner } 223101e1b69cSDavid Chinner 22328d280b98SDavid Chinner 2233b8f82a4aSChristoph Hellwig STATIC void 22348d280b98SDavid Chinner xfs_icsb_lock_all_counters( 22358d280b98SDavid Chinner xfs_mount_t *mp) 22368d280b98SDavid Chinner { 22378d280b98SDavid Chinner xfs_icsb_cnts_t *cntp; 22388d280b98SDavid Chinner int i; 22398d280b98SDavid Chinner 22408d280b98SDavid Chinner for_each_online_cpu(i) { 22418d280b98SDavid Chinner cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i); 224201e1b69cSDavid Chinner xfs_icsb_lock_cntr(cntp); 22438d280b98SDavid Chinner } 22448d280b98SDavid Chinner } 22458d280b98SDavid Chinner 2246b8f82a4aSChristoph Hellwig STATIC void 22478d280b98SDavid Chinner xfs_icsb_unlock_all_counters( 22488d280b98SDavid Chinner xfs_mount_t *mp) 22498d280b98SDavid Chinner { 22508d280b98SDavid Chinner xfs_icsb_cnts_t *cntp; 22518d280b98SDavid Chinner int i; 22528d280b98SDavid Chinner 22538d280b98SDavid Chinner for_each_online_cpu(i) { 22548d280b98SDavid Chinner cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i); 225501e1b69cSDavid Chinner xfs_icsb_unlock_cntr(cntp); 22568d280b98SDavid Chinner } 22578d280b98SDavid Chinner } 22588d280b98SDavid Chinner 22598d280b98SDavid Chinner STATIC void 22608d280b98SDavid Chinner xfs_icsb_count( 22618d280b98SDavid Chinner xfs_mount_t *mp, 22628d280b98SDavid Chinner xfs_icsb_cnts_t *cnt, 22638d280b98SDavid Chinner int flags) 22648d280b98SDavid Chinner { 22658d280b98SDavid Chinner xfs_icsb_cnts_t *cntp; 22668d280b98SDavid Chinner int i; 22678d280b98SDavid Chinner 22688d280b98SDavid Chinner memset(cnt, 0, sizeof(xfs_icsb_cnts_t)); 22698d280b98SDavid Chinner 22708d280b98SDavid Chinner if (!(flags & XFS_ICSB_LAZY_COUNT)) 22718d280b98SDavid Chinner xfs_icsb_lock_all_counters(mp); 22728d280b98SDavid Chinner 22738d280b98SDavid Chinner for_each_online_cpu(i) { 22748d280b98SDavid Chinner cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i); 22758d280b98SDavid Chinner cnt->icsb_icount += cntp->icsb_icount; 22768d280b98SDavid Chinner cnt->icsb_ifree += cntp->icsb_ifree; 22778d280b98SDavid Chinner cnt->icsb_fdblocks += cntp->icsb_fdblocks; 22788d280b98SDavid Chinner } 22798d280b98SDavid Chinner 22808d280b98SDavid Chinner if (!(flags & XFS_ICSB_LAZY_COUNT)) 22818d280b98SDavid Chinner xfs_icsb_unlock_all_counters(mp); 22828d280b98SDavid Chinner } 22838d280b98SDavid Chinner 22848d280b98SDavid Chinner STATIC int 22858d280b98SDavid Chinner xfs_icsb_counter_disabled( 22868d280b98SDavid Chinner xfs_mount_t *mp, 22878d280b98SDavid Chinner xfs_sb_field_t field) 22888d280b98SDavid Chinner { 22898d280b98SDavid Chinner ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS)); 22908d280b98SDavid Chinner return test_bit(field, &mp->m_icsb_counters); 22918d280b98SDavid Chinner } 22928d280b98SDavid Chinner 229336fbe6e6SDavid Chinner STATIC void 22948d280b98SDavid Chinner xfs_icsb_disable_counter( 22958d280b98SDavid Chinner xfs_mount_t *mp, 22968d280b98SDavid Chinner xfs_sb_field_t field) 22978d280b98SDavid Chinner { 22988d280b98SDavid Chinner xfs_icsb_cnts_t cnt; 22998d280b98SDavid Chinner 23008d280b98SDavid Chinner ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS)); 23018d280b98SDavid Chinner 230220b64285SDavid Chinner /* 230320b64285SDavid Chinner * If we are already disabled, then there is nothing to do 230420b64285SDavid Chinner * here. We check before locking all the counters to avoid 230520b64285SDavid Chinner * the expensive lock operation when being called in the 230620b64285SDavid Chinner * slow path and the counter is already disabled. This is 230720b64285SDavid Chinner * safe because the only time we set or clear this state is under 230820b64285SDavid Chinner * the m_icsb_mutex. 230920b64285SDavid Chinner */ 231020b64285SDavid Chinner if (xfs_icsb_counter_disabled(mp, field)) 231136fbe6e6SDavid Chinner return; 231220b64285SDavid Chinner 23138d280b98SDavid Chinner xfs_icsb_lock_all_counters(mp); 23148d280b98SDavid Chinner if (!test_and_set_bit(field, &mp->m_icsb_counters)) { 23158d280b98SDavid Chinner /* drain back to superblock */ 23168d280b98SDavid Chinner 2317ce46193bSChristoph Hellwig xfs_icsb_count(mp, &cnt, XFS_ICSB_LAZY_COUNT); 23188d280b98SDavid Chinner switch(field) { 23198d280b98SDavid Chinner case XFS_SBS_ICOUNT: 23208d280b98SDavid Chinner mp->m_sb.sb_icount = cnt.icsb_icount; 23218d280b98SDavid Chinner break; 23228d280b98SDavid Chinner case XFS_SBS_IFREE: 23238d280b98SDavid Chinner mp->m_sb.sb_ifree = cnt.icsb_ifree; 23248d280b98SDavid Chinner break; 23258d280b98SDavid Chinner case XFS_SBS_FDBLOCKS: 23268d280b98SDavid Chinner mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks; 23278d280b98SDavid Chinner break; 23288d280b98SDavid Chinner default: 23298d280b98SDavid Chinner BUG(); 23308d280b98SDavid Chinner } 23318d280b98SDavid Chinner } 23328d280b98SDavid Chinner 23338d280b98SDavid Chinner xfs_icsb_unlock_all_counters(mp); 23348d280b98SDavid Chinner } 23358d280b98SDavid Chinner 23368d280b98SDavid Chinner STATIC void 23378d280b98SDavid Chinner xfs_icsb_enable_counter( 23388d280b98SDavid Chinner xfs_mount_t *mp, 23398d280b98SDavid Chinner xfs_sb_field_t field, 23408d280b98SDavid Chinner uint64_t count, 23418d280b98SDavid Chinner uint64_t resid) 23428d280b98SDavid Chinner { 23438d280b98SDavid Chinner xfs_icsb_cnts_t *cntp; 23448d280b98SDavid Chinner int i; 23458d280b98SDavid Chinner 23468d280b98SDavid Chinner ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS)); 23478d280b98SDavid Chinner 23488d280b98SDavid Chinner xfs_icsb_lock_all_counters(mp); 23498d280b98SDavid Chinner for_each_online_cpu(i) { 23508d280b98SDavid Chinner cntp = per_cpu_ptr(mp->m_sb_cnts, i); 23518d280b98SDavid Chinner switch (field) { 23528d280b98SDavid Chinner case XFS_SBS_ICOUNT: 23538d280b98SDavid Chinner cntp->icsb_icount = count + resid; 23548d280b98SDavid Chinner break; 23558d280b98SDavid Chinner case XFS_SBS_IFREE: 23568d280b98SDavid Chinner cntp->icsb_ifree = count + resid; 23578d280b98SDavid Chinner break; 23588d280b98SDavid Chinner case XFS_SBS_FDBLOCKS: 23598d280b98SDavid Chinner cntp->icsb_fdblocks = count + resid; 23608d280b98SDavid Chinner break; 23618d280b98SDavid Chinner default: 23628d280b98SDavid Chinner BUG(); 23638d280b98SDavid Chinner break; 23648d280b98SDavid Chinner } 23658d280b98SDavid Chinner resid = 0; 23668d280b98SDavid Chinner } 23678d280b98SDavid Chinner clear_bit(field, &mp->m_icsb_counters); 23688d280b98SDavid Chinner xfs_icsb_unlock_all_counters(mp); 23698d280b98SDavid Chinner } 23708d280b98SDavid Chinner 2371dbcabad1SDavid Chinner void 2372d4d90b57SChristoph Hellwig xfs_icsb_sync_counters_locked( 23738d280b98SDavid Chinner xfs_mount_t *mp, 23748d280b98SDavid Chinner int flags) 23758d280b98SDavid Chinner { 23768d280b98SDavid Chinner xfs_icsb_cnts_t cnt; 23778d280b98SDavid Chinner 23788d280b98SDavid Chinner xfs_icsb_count(mp, &cnt, flags); 23798d280b98SDavid Chinner 23808d280b98SDavid Chinner if (!xfs_icsb_counter_disabled(mp, XFS_SBS_ICOUNT)) 23818d280b98SDavid Chinner mp->m_sb.sb_icount = cnt.icsb_icount; 23828d280b98SDavid Chinner if (!xfs_icsb_counter_disabled(mp, XFS_SBS_IFREE)) 23838d280b98SDavid Chinner mp->m_sb.sb_ifree = cnt.icsb_ifree; 23848d280b98SDavid Chinner if (!xfs_icsb_counter_disabled(mp, XFS_SBS_FDBLOCKS)) 23858d280b98SDavid Chinner mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks; 23868d280b98SDavid Chinner } 23878d280b98SDavid Chinner 23888d280b98SDavid Chinner /* 23898d280b98SDavid Chinner * Accurate update of per-cpu counters to incore superblock 23908d280b98SDavid Chinner */ 2391d4d90b57SChristoph Hellwig void 23928d280b98SDavid Chinner xfs_icsb_sync_counters( 2393d4d90b57SChristoph Hellwig xfs_mount_t *mp, 2394d4d90b57SChristoph Hellwig int flags) 23958d280b98SDavid Chinner { 2396d4d90b57SChristoph Hellwig spin_lock(&mp->m_sb_lock); 2397d4d90b57SChristoph Hellwig xfs_icsb_sync_counters_locked(mp, flags); 2398d4d90b57SChristoph Hellwig spin_unlock(&mp->m_sb_lock); 23998d280b98SDavid Chinner } 24008d280b98SDavid Chinner 24018d280b98SDavid Chinner /* 24028d280b98SDavid Chinner * Balance and enable/disable counters as necessary. 24038d280b98SDavid Chinner * 240420b64285SDavid Chinner * Thresholds for re-enabling counters are somewhat magic. inode counts are 240520b64285SDavid Chinner * chosen to be the same number as single on disk allocation chunk per CPU, and 240620b64285SDavid Chinner * free blocks is something far enough zero that we aren't going thrash when we 240720b64285SDavid Chinner * get near ENOSPC. We also need to supply a minimum we require per cpu to 240820b64285SDavid Chinner * prevent looping endlessly when xfs_alloc_space asks for more than will 240920b64285SDavid Chinner * be distributed to a single CPU but each CPU has enough blocks to be 241020b64285SDavid Chinner * reenabled. 241120b64285SDavid Chinner * 241220b64285SDavid Chinner * Note that we can be called when counters are already disabled. 241320b64285SDavid Chinner * xfs_icsb_disable_counter() optimises the counter locking in this case to 241420b64285SDavid Chinner * prevent locking every per-cpu counter needlessly. 24158d280b98SDavid Chinner */ 241620b64285SDavid Chinner 241720b64285SDavid Chinner #define XFS_ICSB_INO_CNTR_REENABLE (uint64_t)64 24184be536deSDavid Chinner #define XFS_ICSB_FDBLK_CNTR_REENABLE(mp) \ 241920b64285SDavid Chinner (uint64_t)(512 + XFS_ALLOC_SET_ASIDE(mp)) 24208d280b98SDavid Chinner STATIC void 242145af6c6dSChristoph Hellwig xfs_icsb_balance_counter_locked( 24228d280b98SDavid Chinner xfs_mount_t *mp, 24238d280b98SDavid Chinner xfs_sb_field_t field, 242420b64285SDavid Chinner int min_per_cpu) 24258d280b98SDavid Chinner { 24266fdf8cccSNathan Scott uint64_t count, resid; 24278d280b98SDavid Chinner int weight = num_online_cpus(); 242820b64285SDavid Chinner uint64_t min = (uint64_t)min_per_cpu; 24298d280b98SDavid Chinner 24308d280b98SDavid Chinner /* disable counter and sync counter */ 24318d280b98SDavid Chinner xfs_icsb_disable_counter(mp, field); 24328d280b98SDavid Chinner 24338d280b98SDavid Chinner /* update counters - first CPU gets residual*/ 24348d280b98SDavid Chinner switch (field) { 24358d280b98SDavid Chinner case XFS_SBS_ICOUNT: 24368d280b98SDavid Chinner count = mp->m_sb.sb_icount; 24378d280b98SDavid Chinner resid = do_div(count, weight); 243820b64285SDavid Chinner if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE)) 243945af6c6dSChristoph Hellwig return; 24408d280b98SDavid Chinner break; 24418d280b98SDavid Chinner case XFS_SBS_IFREE: 24428d280b98SDavid Chinner count = mp->m_sb.sb_ifree; 24438d280b98SDavid Chinner resid = do_div(count, weight); 244420b64285SDavid Chinner if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE)) 244545af6c6dSChristoph Hellwig return; 24468d280b98SDavid Chinner break; 24478d280b98SDavid Chinner case XFS_SBS_FDBLOCKS: 24488d280b98SDavid Chinner count = mp->m_sb.sb_fdblocks; 24498d280b98SDavid Chinner resid = do_div(count, weight); 245020b64285SDavid Chinner if (count < max(min, XFS_ICSB_FDBLK_CNTR_REENABLE(mp))) 245145af6c6dSChristoph Hellwig return; 24528d280b98SDavid Chinner break; 24538d280b98SDavid Chinner default: 24548d280b98SDavid Chinner BUG(); 24556fdf8cccSNathan Scott count = resid = 0; /* quiet, gcc */ 24568d280b98SDavid Chinner break; 24578d280b98SDavid Chinner } 24588d280b98SDavid Chinner 24598d280b98SDavid Chinner xfs_icsb_enable_counter(mp, field, count, resid); 246045af6c6dSChristoph Hellwig } 246145af6c6dSChristoph Hellwig 246245af6c6dSChristoph Hellwig STATIC void 246345af6c6dSChristoph Hellwig xfs_icsb_balance_counter( 246445af6c6dSChristoph Hellwig xfs_mount_t *mp, 246545af6c6dSChristoph Hellwig xfs_sb_field_t fields, 246645af6c6dSChristoph Hellwig int min_per_cpu) 246745af6c6dSChristoph Hellwig { 246845af6c6dSChristoph Hellwig spin_lock(&mp->m_sb_lock); 246945af6c6dSChristoph Hellwig xfs_icsb_balance_counter_locked(mp, fields, min_per_cpu); 24703685c2a1SEric Sandeen spin_unlock(&mp->m_sb_lock); 24718d280b98SDavid Chinner } 24728d280b98SDavid Chinner 24731b040712SChristoph Hellwig int 247420b64285SDavid Chinner xfs_icsb_modify_counters( 24758d280b98SDavid Chinner xfs_mount_t *mp, 24768d280b98SDavid Chinner xfs_sb_field_t field, 247720f4ebf2SDavid Chinner int64_t delta, 247820b64285SDavid Chinner int rsvd) 24798d280b98SDavid Chinner { 24808d280b98SDavid Chinner xfs_icsb_cnts_t *icsbp; 24818d280b98SDavid Chinner long long lcounter; /* long counter for 64 bit fields */ 24827a9e02d6SChristoph Lameter int ret = 0; 24838d280b98SDavid Chinner 248420b64285SDavid Chinner might_sleep(); 24858d280b98SDavid Chinner again: 24867a9e02d6SChristoph Lameter preempt_disable(); 24877a9e02d6SChristoph Lameter icsbp = this_cpu_ptr(mp->m_sb_cnts); 248820b64285SDavid Chinner 248920b64285SDavid Chinner /* 249020b64285SDavid Chinner * if the counter is disabled, go to slow path 249120b64285SDavid Chinner */ 24928d280b98SDavid Chinner if (unlikely(xfs_icsb_counter_disabled(mp, field))) 24938d280b98SDavid Chinner goto slow_path; 249420b64285SDavid Chinner xfs_icsb_lock_cntr(icsbp); 249520b64285SDavid Chinner if (unlikely(xfs_icsb_counter_disabled(mp, field))) { 249620b64285SDavid Chinner xfs_icsb_unlock_cntr(icsbp); 249720b64285SDavid Chinner goto slow_path; 249820b64285SDavid Chinner } 24998d280b98SDavid Chinner 25008d280b98SDavid Chinner switch (field) { 25018d280b98SDavid Chinner case XFS_SBS_ICOUNT: 25028d280b98SDavid Chinner lcounter = icsbp->icsb_icount; 25038d280b98SDavid Chinner lcounter += delta; 25048d280b98SDavid Chinner if (unlikely(lcounter < 0)) 250520b64285SDavid Chinner goto balance_counter; 25068d280b98SDavid Chinner icsbp->icsb_icount = lcounter; 25078d280b98SDavid Chinner break; 25088d280b98SDavid Chinner 25098d280b98SDavid Chinner case XFS_SBS_IFREE: 25108d280b98SDavid Chinner lcounter = icsbp->icsb_ifree; 25118d280b98SDavid Chinner lcounter += delta; 25128d280b98SDavid Chinner if (unlikely(lcounter < 0)) 251320b64285SDavid Chinner goto balance_counter; 25148d280b98SDavid Chinner icsbp->icsb_ifree = lcounter; 25158d280b98SDavid Chinner break; 25168d280b98SDavid Chinner 25178d280b98SDavid Chinner case XFS_SBS_FDBLOCKS: 25188d280b98SDavid Chinner BUG_ON((mp->m_resblks - mp->m_resblks_avail) != 0); 25198d280b98SDavid Chinner 25204be536deSDavid Chinner lcounter = icsbp->icsb_fdblocks - XFS_ALLOC_SET_ASIDE(mp); 25218d280b98SDavid Chinner lcounter += delta; 25228d280b98SDavid Chinner if (unlikely(lcounter < 0)) 252320b64285SDavid Chinner goto balance_counter; 25244be536deSDavid Chinner icsbp->icsb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp); 25258d280b98SDavid Chinner break; 25268d280b98SDavid Chinner default: 25278d280b98SDavid Chinner BUG(); 25288d280b98SDavid Chinner break; 25298d280b98SDavid Chinner } 253001e1b69cSDavid Chinner xfs_icsb_unlock_cntr(icsbp); 25317a9e02d6SChristoph Lameter preempt_enable(); 25328d280b98SDavid Chinner return 0; 25338d280b98SDavid Chinner 25348d280b98SDavid Chinner slow_path: 25357a9e02d6SChristoph Lameter preempt_enable(); 253620b64285SDavid Chinner 253720b64285SDavid Chinner /* 253820b64285SDavid Chinner * serialise with a mutex so we don't burn lots of cpu on 253920b64285SDavid Chinner * the superblock lock. We still need to hold the superblock 254020b64285SDavid Chinner * lock, however, when we modify the global structures. 254120b64285SDavid Chinner */ 254203135cf7SDavid Chinner xfs_icsb_lock(mp); 254320b64285SDavid Chinner 254420b64285SDavid Chinner /* 254520b64285SDavid Chinner * Now running atomically. 254620b64285SDavid Chinner * 254720b64285SDavid Chinner * If the counter is enabled, someone has beaten us to rebalancing. 254820b64285SDavid Chinner * Drop the lock and try again in the fast path.... 254920b64285SDavid Chinner */ 255020b64285SDavid Chinner if (!(xfs_icsb_counter_disabled(mp, field))) { 255103135cf7SDavid Chinner xfs_icsb_unlock(mp); 255220b64285SDavid Chinner goto again; 255320b64285SDavid Chinner } 255420b64285SDavid Chinner 255520b64285SDavid Chinner /* 255620b64285SDavid Chinner * The counter is currently disabled. Because we are 255720b64285SDavid Chinner * running atomically here, we know a rebalance cannot 255820b64285SDavid Chinner * be in progress. Hence we can go straight to operating 255920b64285SDavid Chinner * on the global superblock. We do not call xfs_mod_incore_sb() 25603685c2a1SEric Sandeen * here even though we need to get the m_sb_lock. Doing so 256120b64285SDavid Chinner * will cause us to re-enter this function and deadlock. 25623685c2a1SEric Sandeen * Hence we get the m_sb_lock ourselves and then call 256320b64285SDavid Chinner * xfs_mod_incore_sb_unlocked() as the unlocked path operates 256420b64285SDavid Chinner * directly on the global counters. 256520b64285SDavid Chinner */ 25663685c2a1SEric Sandeen spin_lock(&mp->m_sb_lock); 256720b64285SDavid Chinner ret = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd); 25683685c2a1SEric Sandeen spin_unlock(&mp->m_sb_lock); 256920b64285SDavid Chinner 257020b64285SDavid Chinner /* 257120b64285SDavid Chinner * Now that we've modified the global superblock, we 257220b64285SDavid Chinner * may be able to re-enable the distributed counters 257320b64285SDavid Chinner * (e.g. lots of space just got freed). After that 257420b64285SDavid Chinner * we are done. 257520b64285SDavid Chinner */ 257620b64285SDavid Chinner if (ret != ENOSPC) 257745af6c6dSChristoph Hellwig xfs_icsb_balance_counter(mp, field, 0); 257803135cf7SDavid Chinner xfs_icsb_unlock(mp); 257920b64285SDavid Chinner return ret; 258020b64285SDavid Chinner 258120b64285SDavid Chinner balance_counter: 258201e1b69cSDavid Chinner xfs_icsb_unlock_cntr(icsbp); 25837a9e02d6SChristoph Lameter preempt_enable(); 25848d280b98SDavid Chinner 258520b64285SDavid Chinner /* 258620b64285SDavid Chinner * We may have multiple threads here if multiple per-cpu 258720b64285SDavid Chinner * counters run dry at the same time. This will mean we can 258820b64285SDavid Chinner * do more balances than strictly necessary but it is not 258920b64285SDavid Chinner * the common slowpath case. 259020b64285SDavid Chinner */ 259103135cf7SDavid Chinner xfs_icsb_lock(mp); 259220b64285SDavid Chinner 259320b64285SDavid Chinner /* 259420b64285SDavid Chinner * running atomically. 259520b64285SDavid Chinner * 259620b64285SDavid Chinner * This will leave the counter in the correct state for future 259720b64285SDavid Chinner * accesses. After the rebalance, we simply try again and our retry 259820b64285SDavid Chinner * will either succeed through the fast path or slow path without 259920b64285SDavid Chinner * another balance operation being required. 260020b64285SDavid Chinner */ 260145af6c6dSChristoph Hellwig xfs_icsb_balance_counter(mp, field, delta); 260203135cf7SDavid Chinner xfs_icsb_unlock(mp); 26038d280b98SDavid Chinner goto again; 26048d280b98SDavid Chinner } 26058d280b98SDavid Chinner 26068d280b98SDavid Chinner #endif 2607