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_dmapi.h" 291da177e4SLinus Torvalds #include "xfs_mount.h" 301da177e4SLinus Torvalds #include "xfs_bmap_btree.h" 31a844f451SNathan Scott #include "xfs_alloc_btree.h" 321da177e4SLinus Torvalds #include "xfs_ialloc_btree.h" 331da177e4SLinus Torvalds #include "xfs_dir2_sf.h" 34a844f451SNathan Scott #include "xfs_attr_sf.h" 351da177e4SLinus Torvalds #include "xfs_dinode.h" 361da177e4SLinus Torvalds #include "xfs_inode.h" 37a844f451SNathan Scott #include "xfs_btree.h" 38a844f451SNathan Scott #include "xfs_ialloc.h" 391da177e4SLinus Torvalds #include "xfs_alloc.h" 401da177e4SLinus Torvalds #include "xfs_rtalloc.h" 411da177e4SLinus Torvalds #include "xfs_bmap.h" 421da177e4SLinus Torvalds #include "xfs_error.h" 431da177e4SLinus Torvalds #include "xfs_rw.h" 441da177e4SLinus Torvalds #include "xfs_quota.h" 451da177e4SLinus Torvalds #include "xfs_fsops.h" 461da177e4SLinus Torvalds 471da177e4SLinus Torvalds STATIC void xfs_mount_log_sbunit(xfs_mount_t *, __int64_t); 481da177e4SLinus Torvalds STATIC int xfs_uuid_mount(xfs_mount_t *); 491da177e4SLinus Torvalds STATIC void xfs_uuid_unmount(xfs_mount_t *mp); 50ba0f32d4SChristoph Hellwig STATIC void xfs_unmountfs_wait(xfs_mount_t *); 511da177e4SLinus Torvalds 528d280b98SDavid Chinner 538d280b98SDavid Chinner #ifdef HAVE_PERCPU_SB 548d280b98SDavid Chinner STATIC void xfs_icsb_destroy_counters(xfs_mount_t *); 55*20b64285SDavid Chinner STATIC void xfs_icsb_balance_counter(xfs_mount_t *, xfs_sb_field_t, int, 56*20b64285SDavid Chinner int); 578d280b98SDavid Chinner STATIC void xfs_icsb_sync_counters(xfs_mount_t *); 588d280b98SDavid Chinner STATIC int xfs_icsb_modify_counters(xfs_mount_t *, xfs_sb_field_t, 598d280b98SDavid Chinner int, int); 60e8234a68SDavid Chinner STATIC int xfs_icsb_disable_counter(xfs_mount_t *, xfs_sb_field_t); 618d280b98SDavid Chinner 628d280b98SDavid Chinner #else 638d280b98SDavid Chinner 648d280b98SDavid Chinner #define xfs_icsb_destroy_counters(mp) do { } while (0) 65*20b64285SDavid Chinner #define xfs_icsb_balance_counter(mp, a, b, c) do { } while (0) 668d280b98SDavid Chinner #define xfs_icsb_sync_counters(mp) do { } while (0) 678d280b98SDavid Chinner #define xfs_icsb_modify_counters(mp, a, b, c) do { } while (0) 688d280b98SDavid Chinner 698d280b98SDavid Chinner #endif 708d280b98SDavid Chinner 711df84c93SChristoph Hellwig static const struct { 721da177e4SLinus Torvalds short offset; 731da177e4SLinus Torvalds short type; /* 0 = integer 741da177e4SLinus Torvalds * 1 = binary / string (no translation) 751da177e4SLinus Torvalds */ 761da177e4SLinus Torvalds } xfs_sb_info[] = { 771da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_magicnum), 0 }, 781da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_blocksize), 0 }, 791da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_dblocks), 0 }, 801da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_rblocks), 0 }, 811da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_rextents), 0 }, 821da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_uuid), 1 }, 831da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_logstart), 0 }, 841da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_rootino), 0 }, 851da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_rbmino), 0 }, 861da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_rsumino), 0 }, 871da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_rextsize), 0 }, 881da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_agblocks), 0 }, 891da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_agcount), 0 }, 901da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_rbmblocks), 0 }, 911da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_logblocks), 0 }, 921da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_versionnum), 0 }, 931da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_sectsize), 0 }, 941da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_inodesize), 0 }, 951da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_inopblock), 0 }, 961da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_fname[0]), 1 }, 971da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_blocklog), 0 }, 981da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_sectlog), 0 }, 991da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_inodelog), 0 }, 1001da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_inopblog), 0 }, 1011da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_agblklog), 0 }, 1021da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_rextslog), 0 }, 1031da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_inprogress), 0 }, 1041da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_imax_pct), 0 }, 1051da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_icount), 0 }, 1061da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_ifree), 0 }, 1071da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_fdblocks), 0 }, 1081da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_frextents), 0 }, 1091da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_uquotino), 0 }, 1101da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_gquotino), 0 }, 1111da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_qflags), 0 }, 1121da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_flags), 0 }, 1131da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_shared_vn), 0 }, 1141da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_inoalignmt), 0 }, 1151da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_unit), 0 }, 1161da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_width), 0 }, 1171da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_dirblklog), 0 }, 1181da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_logsectlog), 0 }, 1191da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_logsectsize),0 }, 1201da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_logsunit), 0 }, 1211da177e4SLinus Torvalds { offsetof(xfs_sb_t, sb_features2), 0 }, 1221da177e4SLinus Torvalds { sizeof(xfs_sb_t), 0 } 1231da177e4SLinus Torvalds }; 1241da177e4SLinus Torvalds 1251da177e4SLinus Torvalds /* 1261da177e4SLinus Torvalds * Return a pointer to an initialized xfs_mount structure. 1271da177e4SLinus Torvalds */ 1281da177e4SLinus Torvalds xfs_mount_t * 1291da177e4SLinus Torvalds xfs_mount_init(void) 1301da177e4SLinus Torvalds { 1311da177e4SLinus Torvalds xfs_mount_t *mp; 1321da177e4SLinus Torvalds 1338d280b98SDavid Chinner mp = kmem_zalloc(sizeof(xfs_mount_t), KM_SLEEP); 1348d280b98SDavid Chinner 1358d280b98SDavid Chinner if (xfs_icsb_init_counters(mp)) { 1368d280b98SDavid Chinner mp->m_flags |= XFS_MOUNT_NO_PERCPU_SB; 1378d280b98SDavid Chinner } 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds AIL_LOCKINIT(&mp->m_ail_lock, "xfs_ail"); 1401da177e4SLinus Torvalds spinlock_init(&mp->m_sb_lock, "xfs_sb"); 141794ee1baSJes Sorensen mutex_init(&mp->m_ilock); 1421da177e4SLinus Torvalds initnsema(&mp->m_growlock, 1, "xfs_grow"); 1431da177e4SLinus Torvalds /* 1441da177e4SLinus Torvalds * Initialize the AIL. 1451da177e4SLinus Torvalds */ 1461da177e4SLinus Torvalds xfs_trans_ail_init(mp); 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds atomic_set(&mp->m_active_trans, 0); 1491da177e4SLinus Torvalds 1501da177e4SLinus Torvalds return mp; 1511da177e4SLinus Torvalds } 1521da177e4SLinus Torvalds 1531da177e4SLinus Torvalds /* 1541da177e4SLinus Torvalds * Free up the resources associated with a mount structure. Assume that 1551da177e4SLinus Torvalds * the structure was initially zeroed, so we can tell which fields got 1561da177e4SLinus Torvalds * initialized. 1571da177e4SLinus Torvalds */ 1581da177e4SLinus Torvalds void 1591da177e4SLinus Torvalds xfs_mount_free( 1601da177e4SLinus Torvalds xfs_mount_t *mp, 1611da177e4SLinus Torvalds int remove_bhv) 1621da177e4SLinus Torvalds { 1631da177e4SLinus Torvalds if (mp->m_ihash) 1641da177e4SLinus Torvalds xfs_ihash_free(mp); 1651da177e4SLinus Torvalds if (mp->m_chash) 1661da177e4SLinus Torvalds xfs_chash_free(mp); 1671da177e4SLinus Torvalds 1681da177e4SLinus Torvalds if (mp->m_perag) { 1691da177e4SLinus Torvalds int agno; 1701da177e4SLinus Torvalds 1711da177e4SLinus Torvalds for (agno = 0; agno < mp->m_maxagi; agno++) 1721da177e4SLinus Torvalds if (mp->m_perag[agno].pagb_list) 1731da177e4SLinus Torvalds kmem_free(mp->m_perag[agno].pagb_list, 1741da177e4SLinus Torvalds sizeof(xfs_perag_busy_t) * 1751da177e4SLinus Torvalds XFS_PAGB_NUM_SLOTS); 1761da177e4SLinus Torvalds kmem_free(mp->m_perag, 1771da177e4SLinus Torvalds sizeof(xfs_perag_t) * mp->m_sb.sb_agcount); 1781da177e4SLinus Torvalds } 1791da177e4SLinus Torvalds 1801da177e4SLinus Torvalds AIL_LOCK_DESTROY(&mp->m_ail_lock); 1811da177e4SLinus Torvalds spinlock_destroy(&mp->m_sb_lock); 1821da177e4SLinus Torvalds mutex_destroy(&mp->m_ilock); 1831da177e4SLinus Torvalds freesema(&mp->m_growlock); 1841da177e4SLinus Torvalds if (mp->m_quotainfo) 1851da177e4SLinus Torvalds XFS_QM_DONE(mp); 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds if (mp->m_fsname != NULL) 1881da177e4SLinus Torvalds kmem_free(mp->m_fsname, mp->m_fsname_len); 189fc1f8c1cSNathan Scott if (mp->m_rtname != NULL) 190fc1f8c1cSNathan Scott kmem_free(mp->m_rtname, strlen(mp->m_rtname) + 1); 191fc1f8c1cSNathan Scott if (mp->m_logname != NULL) 192fc1f8c1cSNathan Scott kmem_free(mp->m_logname, strlen(mp->m_logname) + 1); 1931da177e4SLinus Torvalds 1941da177e4SLinus Torvalds if (remove_bhv) { 195b83bd138SNathan Scott struct bhv_vfs *vfsp = XFS_MTOVFS(mp); 1961da177e4SLinus Torvalds 1971da177e4SLinus Torvalds bhv_remove_all_vfsops(vfsp, 0); 1981da177e4SLinus Torvalds VFS_REMOVEBHV(vfsp, &mp->m_bhv); 1991da177e4SLinus Torvalds } 2001da177e4SLinus Torvalds 2018d280b98SDavid Chinner xfs_icsb_destroy_counters(mp); 2021da177e4SLinus Torvalds kmem_free(mp, sizeof(xfs_mount_t)); 2031da177e4SLinus Torvalds } 2041da177e4SLinus Torvalds 2051da177e4SLinus Torvalds 2061da177e4SLinus Torvalds /* 2071da177e4SLinus Torvalds * Check the validity of the SB found. 2081da177e4SLinus Torvalds */ 2091da177e4SLinus Torvalds STATIC int 2101da177e4SLinus Torvalds xfs_mount_validate_sb( 2111da177e4SLinus Torvalds xfs_mount_t *mp, 212764d1f89SNathan Scott xfs_sb_t *sbp, 213764d1f89SNathan Scott int flags) 2141da177e4SLinus Torvalds { 2151da177e4SLinus Torvalds /* 2161da177e4SLinus Torvalds * If the log device and data device have the 2171da177e4SLinus Torvalds * same device number, the log is internal. 2181da177e4SLinus Torvalds * Consequently, the sb_logstart should be non-zero. If 2191da177e4SLinus Torvalds * we have a zero sb_logstart in this case, we may be trying to mount 2201da177e4SLinus Torvalds * a volume filesystem in a non-volume manner. 2211da177e4SLinus Torvalds */ 2221da177e4SLinus Torvalds if (sbp->sb_magicnum != XFS_SB_MAGIC) { 223764d1f89SNathan Scott xfs_fs_mount_cmn_err(flags, "bad magic number"); 2241da177e4SLinus Torvalds return XFS_ERROR(EWRONGFS); 2251da177e4SLinus Torvalds } 2261da177e4SLinus Torvalds 2271da177e4SLinus Torvalds if (!XFS_SB_GOOD_VERSION(sbp)) { 228764d1f89SNathan Scott xfs_fs_mount_cmn_err(flags, "bad version"); 2291da177e4SLinus Torvalds return XFS_ERROR(EWRONGFS); 2301da177e4SLinus Torvalds } 2311da177e4SLinus Torvalds 2321da177e4SLinus Torvalds if (unlikely( 2331da177e4SLinus Torvalds sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) { 234764d1f89SNathan Scott xfs_fs_mount_cmn_err(flags, 235764d1f89SNathan Scott "filesystem is marked as having an external log; " 2361da177e4SLinus Torvalds "specify logdev on the\nmount command line."); 237764d1f89SNathan Scott return XFS_ERROR(EINVAL); 2381da177e4SLinus Torvalds } 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds if (unlikely( 2411da177e4SLinus Torvalds sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) { 242764d1f89SNathan Scott xfs_fs_mount_cmn_err(flags, 243764d1f89SNathan Scott "filesystem is marked as having an internal log; " 244764d1f89SNathan Scott "do not specify logdev on\nthe mount command line."); 245764d1f89SNathan Scott return XFS_ERROR(EINVAL); 2461da177e4SLinus Torvalds } 2471da177e4SLinus Torvalds 2481da177e4SLinus Torvalds /* 2491da177e4SLinus Torvalds * More sanity checking. These were stolen directly from 2501da177e4SLinus Torvalds * xfs_repair. 2511da177e4SLinus Torvalds */ 2521da177e4SLinus Torvalds if (unlikely( 2531da177e4SLinus Torvalds sbp->sb_agcount <= 0 || 2541da177e4SLinus Torvalds sbp->sb_sectsize < XFS_MIN_SECTORSIZE || 2551da177e4SLinus Torvalds sbp->sb_sectsize > XFS_MAX_SECTORSIZE || 2561da177e4SLinus Torvalds sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG || 2571da177e4SLinus Torvalds sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG || 2581da177e4SLinus Torvalds sbp->sb_blocksize < XFS_MIN_BLOCKSIZE || 2591da177e4SLinus Torvalds sbp->sb_blocksize > XFS_MAX_BLOCKSIZE || 2601da177e4SLinus Torvalds sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG || 2611da177e4SLinus Torvalds sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG || 2621da177e4SLinus Torvalds sbp->sb_inodesize < XFS_DINODE_MIN_SIZE || 2631da177e4SLinus Torvalds sbp->sb_inodesize > XFS_DINODE_MAX_SIZE || 2649f989c94SNathan Scott sbp->sb_inodelog < XFS_DINODE_MIN_LOG || 2659f989c94SNathan Scott sbp->sb_inodelog > XFS_DINODE_MAX_LOG || 2669f989c94SNathan Scott (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog) || 2671da177e4SLinus Torvalds (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE) || 2681da177e4SLinus Torvalds (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) || 269e50bd16fSNathan Scott (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */))) { 270764d1f89SNathan Scott xfs_fs_mount_cmn_err(flags, "SB sanity check 1 failed"); 2711da177e4SLinus Torvalds return XFS_ERROR(EFSCORRUPTED); 2721da177e4SLinus Torvalds } 2731da177e4SLinus Torvalds 2741da177e4SLinus Torvalds /* 2751da177e4SLinus Torvalds * Sanity check AG count, size fields against data size field 2761da177e4SLinus Torvalds */ 2771da177e4SLinus Torvalds if (unlikely( 2781da177e4SLinus Torvalds sbp->sb_dblocks == 0 || 2791da177e4SLinus Torvalds sbp->sb_dblocks > 2801da177e4SLinus Torvalds (xfs_drfsbno_t)sbp->sb_agcount * sbp->sb_agblocks || 2811da177e4SLinus Torvalds sbp->sb_dblocks < (xfs_drfsbno_t)(sbp->sb_agcount - 1) * 2821da177e4SLinus Torvalds sbp->sb_agblocks + XFS_MIN_AG_BLOCKS)) { 283764d1f89SNathan Scott xfs_fs_mount_cmn_err(flags, "SB sanity check 2 failed"); 2841da177e4SLinus Torvalds return XFS_ERROR(EFSCORRUPTED); 2851da177e4SLinus Torvalds } 2861da177e4SLinus Torvalds 2871da177e4SLinus Torvalds ASSERT(PAGE_SHIFT >= sbp->sb_blocklog); 2881da177e4SLinus Torvalds ASSERT(sbp->sb_blocklog >= BBSHIFT); 2891da177e4SLinus Torvalds 2901da177e4SLinus Torvalds #if XFS_BIG_BLKNOS /* Limited by ULONG_MAX of page cache index */ 2911da177e4SLinus Torvalds if (unlikely( 2921da177e4SLinus Torvalds (sbp->sb_dblocks >> (PAGE_SHIFT - sbp->sb_blocklog)) > ULONG_MAX || 2931da177e4SLinus Torvalds (sbp->sb_rblocks >> (PAGE_SHIFT - sbp->sb_blocklog)) > ULONG_MAX)) { 2941da177e4SLinus Torvalds #else /* Limited by UINT_MAX of sectors */ 2951da177e4SLinus Torvalds if (unlikely( 2961da177e4SLinus Torvalds (sbp->sb_dblocks << (sbp->sb_blocklog - BBSHIFT)) > UINT_MAX || 2971da177e4SLinus Torvalds (sbp->sb_rblocks << (sbp->sb_blocklog - BBSHIFT)) > UINT_MAX)) { 2981da177e4SLinus Torvalds #endif 299764d1f89SNathan Scott xfs_fs_mount_cmn_err(flags, 300764d1f89SNathan Scott "file system too large to be mounted on this system."); 3011da177e4SLinus Torvalds return XFS_ERROR(E2BIG); 3021da177e4SLinus Torvalds } 3031da177e4SLinus Torvalds 3041da177e4SLinus Torvalds if (unlikely(sbp->sb_inprogress)) { 305764d1f89SNathan Scott xfs_fs_mount_cmn_err(flags, "file system busy"); 3061da177e4SLinus Torvalds return XFS_ERROR(EFSCORRUPTED); 3071da177e4SLinus Torvalds } 3081da177e4SLinus Torvalds 3091da177e4SLinus Torvalds /* 310de20614bSNathan Scott * Version 1 directory format has never worked on Linux. 311de20614bSNathan Scott */ 312de20614bSNathan Scott if (unlikely(!XFS_SB_VERSION_HASDIRV2(sbp))) { 313764d1f89SNathan Scott xfs_fs_mount_cmn_err(flags, 314764d1f89SNathan Scott "file system using version 1 directory format"); 315de20614bSNathan Scott return XFS_ERROR(ENOSYS); 316de20614bSNathan Scott } 317de20614bSNathan Scott 318de20614bSNathan Scott /* 3191da177e4SLinus Torvalds * Until this is fixed only page-sized or smaller data blocks work. 3201da177e4SLinus Torvalds */ 3211da177e4SLinus Torvalds if (unlikely(sbp->sb_blocksize > PAGE_SIZE)) { 322764d1f89SNathan Scott xfs_fs_mount_cmn_err(flags, 323764d1f89SNathan Scott "file system with blocksize %d bytes", 3241da177e4SLinus Torvalds sbp->sb_blocksize); 325764d1f89SNathan Scott xfs_fs_mount_cmn_err(flags, 326764d1f89SNathan Scott "only pagesize (%ld) or less will currently work.", 3271da177e4SLinus Torvalds PAGE_SIZE); 3281da177e4SLinus Torvalds return XFS_ERROR(ENOSYS); 3291da177e4SLinus Torvalds } 3301da177e4SLinus Torvalds 3311da177e4SLinus Torvalds return 0; 3321da177e4SLinus Torvalds } 3331da177e4SLinus Torvalds 3341da177e4SLinus Torvalds xfs_agnumber_t 335c11e2c36SNathan Scott xfs_initialize_perag( 336b83bd138SNathan Scott bhv_vfs_t *vfs, 337c11e2c36SNathan Scott xfs_mount_t *mp, 338c11e2c36SNathan Scott xfs_agnumber_t agcount) 3391da177e4SLinus Torvalds { 3401da177e4SLinus Torvalds xfs_agnumber_t index, max_metadata; 3411da177e4SLinus Torvalds xfs_perag_t *pag; 3421da177e4SLinus Torvalds xfs_agino_t agino; 3431da177e4SLinus Torvalds xfs_ino_t ino; 3441da177e4SLinus Torvalds xfs_sb_t *sbp = &mp->m_sb; 3451da177e4SLinus Torvalds xfs_ino_t max_inum = XFS_MAXINUMBER_32; 3461da177e4SLinus Torvalds 3471da177e4SLinus Torvalds /* Check to see if the filesystem can overflow 32 bit inodes */ 3481da177e4SLinus Torvalds agino = XFS_OFFBNO_TO_AGINO(mp, sbp->sb_agblocks - 1, 0); 3491da177e4SLinus Torvalds ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino); 3501da177e4SLinus Torvalds 3511da177e4SLinus Torvalds /* Clear the mount flag if no inode can overflow 32 bits 3521da177e4SLinus Torvalds * on this filesystem, or if specifically requested.. 3531da177e4SLinus Torvalds */ 354c11e2c36SNathan Scott if ((vfs->vfs_flag & VFS_32BITINODES) && ino > max_inum) { 3551da177e4SLinus Torvalds mp->m_flags |= XFS_MOUNT_32BITINODES; 3561da177e4SLinus Torvalds } else { 3571da177e4SLinus Torvalds mp->m_flags &= ~XFS_MOUNT_32BITINODES; 3581da177e4SLinus Torvalds } 3591da177e4SLinus Torvalds 3601da177e4SLinus Torvalds /* If we can overflow then setup the ag headers accordingly */ 3611da177e4SLinus Torvalds if (mp->m_flags & XFS_MOUNT_32BITINODES) { 3621da177e4SLinus Torvalds /* Calculate how much should be reserved for inodes to 3631da177e4SLinus Torvalds * meet the max inode percentage. 3641da177e4SLinus Torvalds */ 3651da177e4SLinus Torvalds if (mp->m_maxicount) { 3661da177e4SLinus Torvalds __uint64_t icount; 3671da177e4SLinus Torvalds 3681da177e4SLinus Torvalds icount = sbp->sb_dblocks * sbp->sb_imax_pct; 3691da177e4SLinus Torvalds do_div(icount, 100); 3701da177e4SLinus Torvalds icount += sbp->sb_agblocks - 1; 371a749ee86SEric Sandeen do_div(icount, sbp->sb_agblocks); 3721da177e4SLinus Torvalds max_metadata = icount; 3731da177e4SLinus Torvalds } else { 3741da177e4SLinus Torvalds max_metadata = agcount; 3751da177e4SLinus Torvalds } 3761da177e4SLinus Torvalds for (index = 0; index < agcount; index++) { 3771da177e4SLinus Torvalds ino = XFS_AGINO_TO_INO(mp, index, agino); 3781da177e4SLinus Torvalds if (ino > max_inum) { 3791da177e4SLinus Torvalds index++; 3801da177e4SLinus Torvalds break; 3811da177e4SLinus Torvalds } 3821da177e4SLinus Torvalds 383c41564b5SNathan Scott /* This ag is preferred for inodes */ 3841da177e4SLinus Torvalds pag = &mp->m_perag[index]; 3851da177e4SLinus Torvalds pag->pagi_inodeok = 1; 3861da177e4SLinus Torvalds if (index < max_metadata) 3871da177e4SLinus Torvalds pag->pagf_metadata = 1; 3881da177e4SLinus Torvalds } 3891da177e4SLinus Torvalds } else { 3901da177e4SLinus Torvalds /* Setup default behavior for smaller filesystems */ 3911da177e4SLinus Torvalds for (index = 0; index < agcount; index++) { 3921da177e4SLinus Torvalds pag = &mp->m_perag[index]; 3931da177e4SLinus Torvalds pag->pagi_inodeok = 1; 3941da177e4SLinus Torvalds } 3951da177e4SLinus Torvalds } 3961da177e4SLinus Torvalds return index; 3971da177e4SLinus Torvalds } 3981da177e4SLinus Torvalds 3991da177e4SLinus Torvalds /* 4001da177e4SLinus Torvalds * xfs_xlatesb 4011da177e4SLinus Torvalds * 4021da177e4SLinus Torvalds * data - on disk version of sb 4031da177e4SLinus Torvalds * sb - a superblock 4041da177e4SLinus Torvalds * dir - conversion direction: <0 - convert sb to buf 4051da177e4SLinus Torvalds * >0 - convert buf to sb 4061da177e4SLinus Torvalds * fields - which fields to copy (bitmask) 4071da177e4SLinus Torvalds */ 4081da177e4SLinus Torvalds void 4091da177e4SLinus Torvalds xfs_xlatesb( 4101da177e4SLinus Torvalds void *data, 4111da177e4SLinus Torvalds xfs_sb_t *sb, 4121da177e4SLinus Torvalds int dir, 4131da177e4SLinus Torvalds __int64_t fields) 4141da177e4SLinus Torvalds { 4151da177e4SLinus Torvalds xfs_caddr_t buf_ptr; 4161da177e4SLinus Torvalds xfs_caddr_t mem_ptr; 4171da177e4SLinus Torvalds xfs_sb_field_t f; 4181da177e4SLinus Torvalds int first; 4191da177e4SLinus Torvalds int size; 4201da177e4SLinus Torvalds 4211da177e4SLinus Torvalds ASSERT(dir); 4221da177e4SLinus Torvalds ASSERT(fields); 4231da177e4SLinus Torvalds 4241da177e4SLinus Torvalds if (!fields) 4251da177e4SLinus Torvalds return; 4261da177e4SLinus Torvalds 4271da177e4SLinus Torvalds buf_ptr = (xfs_caddr_t)data; 4281da177e4SLinus Torvalds mem_ptr = (xfs_caddr_t)sb; 4291da177e4SLinus Torvalds 4301da177e4SLinus Torvalds while (fields) { 4311da177e4SLinus Torvalds f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields); 4321da177e4SLinus Torvalds first = xfs_sb_info[f].offset; 4331da177e4SLinus Torvalds size = xfs_sb_info[f + 1].offset - first; 4341da177e4SLinus Torvalds 4351da177e4SLinus Torvalds ASSERT(xfs_sb_info[f].type == 0 || xfs_sb_info[f].type == 1); 4361da177e4SLinus Torvalds 4371da177e4SLinus Torvalds if (size == 1 || xfs_sb_info[f].type == 1) { 4381da177e4SLinus Torvalds if (dir > 0) { 4391da177e4SLinus Torvalds memcpy(mem_ptr + first, buf_ptr + first, size); 4401da177e4SLinus Torvalds } else { 4411da177e4SLinus Torvalds memcpy(buf_ptr + first, mem_ptr + first, size); 4421da177e4SLinus Torvalds } 4431da177e4SLinus Torvalds } else { 4441da177e4SLinus Torvalds switch (size) { 4451da177e4SLinus Torvalds case 2: 4461da177e4SLinus Torvalds INT_XLATE(*(__uint16_t*)(buf_ptr+first), 4471da177e4SLinus Torvalds *(__uint16_t*)(mem_ptr+first), 4481da177e4SLinus Torvalds dir, ARCH_CONVERT); 4491da177e4SLinus Torvalds break; 4501da177e4SLinus Torvalds case 4: 4511da177e4SLinus Torvalds INT_XLATE(*(__uint32_t*)(buf_ptr+first), 4521da177e4SLinus Torvalds *(__uint32_t*)(mem_ptr+first), 4531da177e4SLinus Torvalds dir, ARCH_CONVERT); 4541da177e4SLinus Torvalds break; 4551da177e4SLinus Torvalds case 8: 4561da177e4SLinus Torvalds INT_XLATE(*(__uint64_t*)(buf_ptr+first), 4571da177e4SLinus Torvalds *(__uint64_t*)(mem_ptr+first), dir, ARCH_CONVERT); 4581da177e4SLinus Torvalds break; 4591da177e4SLinus Torvalds default: 4601da177e4SLinus Torvalds ASSERT(0); 4611da177e4SLinus Torvalds } 4621da177e4SLinus Torvalds } 4631da177e4SLinus Torvalds 4641da177e4SLinus Torvalds fields &= ~(1LL << f); 4651da177e4SLinus Torvalds } 4661da177e4SLinus Torvalds } 4671da177e4SLinus Torvalds 4681da177e4SLinus Torvalds /* 4691da177e4SLinus Torvalds * xfs_readsb 4701da177e4SLinus Torvalds * 4711da177e4SLinus Torvalds * Does the initial read of the superblock. 4721da177e4SLinus Torvalds */ 4731da177e4SLinus Torvalds int 474764d1f89SNathan Scott xfs_readsb(xfs_mount_t *mp, int flags) 4751da177e4SLinus Torvalds { 4761da177e4SLinus Torvalds unsigned int sector_size; 4771da177e4SLinus Torvalds unsigned int extra_flags; 4781da177e4SLinus Torvalds xfs_buf_t *bp; 4791da177e4SLinus Torvalds xfs_sb_t *sbp; 4801da177e4SLinus Torvalds int error; 4811da177e4SLinus Torvalds 4821da177e4SLinus Torvalds ASSERT(mp->m_sb_bp == NULL); 4831da177e4SLinus Torvalds ASSERT(mp->m_ddev_targp != NULL); 4841da177e4SLinus Torvalds 4851da177e4SLinus Torvalds /* 4861da177e4SLinus Torvalds * Allocate a (locked) buffer to hold the superblock. 4871da177e4SLinus Torvalds * This will be kept around at all times to optimize 4881da177e4SLinus Torvalds * access to the superblock. 4891da177e4SLinus Torvalds */ 4901da177e4SLinus Torvalds sector_size = xfs_getsize_buftarg(mp->m_ddev_targp); 4911da177e4SLinus Torvalds extra_flags = XFS_BUF_LOCK | XFS_BUF_MANAGE | XFS_BUF_MAPPED; 4921da177e4SLinus Torvalds 4931da177e4SLinus Torvalds bp = xfs_buf_read_flags(mp->m_ddev_targp, XFS_SB_DADDR, 4941da177e4SLinus Torvalds BTOBB(sector_size), extra_flags); 4951da177e4SLinus Torvalds if (!bp || XFS_BUF_ISERROR(bp)) { 496764d1f89SNathan Scott xfs_fs_mount_cmn_err(flags, "SB read failed"); 4971da177e4SLinus Torvalds error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM; 4981da177e4SLinus Torvalds goto fail; 4991da177e4SLinus Torvalds } 5001da177e4SLinus Torvalds ASSERT(XFS_BUF_ISBUSY(bp)); 5011da177e4SLinus Torvalds ASSERT(XFS_BUF_VALUSEMA(bp) <= 0); 5021da177e4SLinus Torvalds 5031da177e4SLinus Torvalds /* 5041da177e4SLinus Torvalds * Initialize the mount structure from the superblock. 5051da177e4SLinus Torvalds * But first do some basic consistency checking. 5061da177e4SLinus Torvalds */ 5071da177e4SLinus Torvalds sbp = XFS_BUF_TO_SBP(bp); 5081da177e4SLinus Torvalds xfs_xlatesb(XFS_BUF_PTR(bp), &(mp->m_sb), 1, XFS_SB_ALL_BITS); 5091da177e4SLinus Torvalds 510764d1f89SNathan Scott error = xfs_mount_validate_sb(mp, &(mp->m_sb), flags); 5111da177e4SLinus Torvalds if (error) { 512764d1f89SNathan Scott xfs_fs_mount_cmn_err(flags, "SB validate failed"); 5131da177e4SLinus Torvalds goto fail; 5141da177e4SLinus Torvalds } 5151da177e4SLinus Torvalds 5161da177e4SLinus Torvalds /* 5171da177e4SLinus Torvalds * We must be able to do sector-sized and sector-aligned IO. 5181da177e4SLinus Torvalds */ 5191da177e4SLinus Torvalds if (sector_size > mp->m_sb.sb_sectsize) { 520764d1f89SNathan Scott xfs_fs_mount_cmn_err(flags, 521764d1f89SNathan Scott "device supports only %u byte sectors (not %u)", 5221da177e4SLinus Torvalds sector_size, mp->m_sb.sb_sectsize); 5231da177e4SLinus Torvalds error = ENOSYS; 5241da177e4SLinus Torvalds goto fail; 5251da177e4SLinus Torvalds } 5261da177e4SLinus Torvalds 5271da177e4SLinus Torvalds /* 5281da177e4SLinus Torvalds * If device sector size is smaller than the superblock size, 5291da177e4SLinus Torvalds * re-read the superblock so the buffer is correctly sized. 5301da177e4SLinus Torvalds */ 5311da177e4SLinus Torvalds if (sector_size < mp->m_sb.sb_sectsize) { 5321da177e4SLinus Torvalds XFS_BUF_UNMANAGE(bp); 5331da177e4SLinus Torvalds xfs_buf_relse(bp); 5341da177e4SLinus Torvalds sector_size = mp->m_sb.sb_sectsize; 5351da177e4SLinus Torvalds bp = xfs_buf_read_flags(mp->m_ddev_targp, XFS_SB_DADDR, 5361da177e4SLinus Torvalds BTOBB(sector_size), extra_flags); 5371da177e4SLinus Torvalds if (!bp || XFS_BUF_ISERROR(bp)) { 538764d1f89SNathan Scott xfs_fs_mount_cmn_err(flags, "SB re-read failed"); 5391da177e4SLinus Torvalds error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM; 5401da177e4SLinus Torvalds goto fail; 5411da177e4SLinus Torvalds } 5421da177e4SLinus Torvalds ASSERT(XFS_BUF_ISBUSY(bp)); 5431da177e4SLinus Torvalds ASSERT(XFS_BUF_VALUSEMA(bp) <= 0); 5441da177e4SLinus Torvalds } 5451da177e4SLinus Torvalds 546*20b64285SDavid Chinner mutex_lock(&mp->m_icsb_mutex); 547*20b64285SDavid Chinner xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0, 0); 548*20b64285SDavid Chinner xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0, 0); 549*20b64285SDavid Chinner xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0, 0); 550*20b64285SDavid Chinner mutex_unlock(&mp->m_icsb_mutex); 5518d280b98SDavid Chinner 5521da177e4SLinus Torvalds mp->m_sb_bp = bp; 5531da177e4SLinus Torvalds xfs_buf_relse(bp); 5541da177e4SLinus Torvalds ASSERT(XFS_BUF_VALUSEMA(bp) > 0); 5551da177e4SLinus Torvalds return 0; 5561da177e4SLinus Torvalds 5571da177e4SLinus Torvalds fail: 5581da177e4SLinus Torvalds if (bp) { 5591da177e4SLinus Torvalds XFS_BUF_UNMANAGE(bp); 5601da177e4SLinus Torvalds xfs_buf_relse(bp); 5611da177e4SLinus Torvalds } 5621da177e4SLinus Torvalds return error; 5631da177e4SLinus Torvalds } 5641da177e4SLinus Torvalds 5651da177e4SLinus Torvalds 5661da177e4SLinus Torvalds /* 5671da177e4SLinus Torvalds * xfs_mount_common 5681da177e4SLinus Torvalds * 5691da177e4SLinus Torvalds * Mount initialization code establishing various mount 5701da177e4SLinus Torvalds * fields from the superblock associated with the given 5711da177e4SLinus Torvalds * mount structure 5721da177e4SLinus Torvalds */ 573ba0f32d4SChristoph Hellwig STATIC void 5741da177e4SLinus Torvalds xfs_mount_common(xfs_mount_t *mp, xfs_sb_t *sbp) 5751da177e4SLinus Torvalds { 5761da177e4SLinus Torvalds int i; 5771da177e4SLinus Torvalds 5781da177e4SLinus Torvalds mp->m_agfrotor = mp->m_agirotor = 0; 5791da177e4SLinus Torvalds spinlock_init(&mp->m_agirotor_lock, "m_agirotor_lock"); 5801da177e4SLinus Torvalds mp->m_maxagi = mp->m_sb.sb_agcount; 5811da177e4SLinus Torvalds mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG; 5821da177e4SLinus Torvalds mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT; 5831da177e4SLinus Torvalds mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT; 5841da177e4SLinus Torvalds mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1; 5851da177e4SLinus Torvalds mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog; 5861da177e4SLinus Torvalds mp->m_litino = sbp->sb_inodesize - 5871da177e4SLinus Torvalds ((uint)sizeof(xfs_dinode_core_t) + (uint)sizeof(xfs_agino_t)); 5881da177e4SLinus Torvalds mp->m_blockmask = sbp->sb_blocksize - 1; 5891da177e4SLinus Torvalds mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG; 5901da177e4SLinus Torvalds mp->m_blockwmask = mp->m_blockwsize - 1; 5911da177e4SLinus Torvalds INIT_LIST_HEAD(&mp->m_del_inodes); 5921da177e4SLinus Torvalds 5931da177e4SLinus Torvalds /* 5941da177e4SLinus Torvalds * Setup for attributes, in case they get created. 5951da177e4SLinus Torvalds * This value is for inodes getting attributes for the first time, 5961da177e4SLinus Torvalds * the per-inode value is for old attribute values. 5971da177e4SLinus Torvalds */ 5981da177e4SLinus Torvalds ASSERT(sbp->sb_inodesize >= 256 && sbp->sb_inodesize <= 2048); 5991da177e4SLinus Torvalds switch (sbp->sb_inodesize) { 6001da177e4SLinus Torvalds case 256: 601d8cc890dSNathan Scott mp->m_attroffset = XFS_LITINO(mp) - 602d8cc890dSNathan Scott XFS_BMDR_SPACE_CALC(MINABTPTRS); 6031da177e4SLinus Torvalds break; 6041da177e4SLinus Torvalds case 512: 6051da177e4SLinus Torvalds case 1024: 6061da177e4SLinus Torvalds case 2048: 607d8cc890dSNathan Scott mp->m_attroffset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS); 6081da177e4SLinus Torvalds break; 6091da177e4SLinus Torvalds default: 6101da177e4SLinus Torvalds ASSERT(0); 6111da177e4SLinus Torvalds } 6121da177e4SLinus Torvalds ASSERT(mp->m_attroffset < XFS_LITINO(mp)); 6131da177e4SLinus Torvalds 6141da177e4SLinus Torvalds for (i = 0; i < 2; i++) { 6151da177e4SLinus Torvalds mp->m_alloc_mxr[i] = XFS_BTREE_BLOCK_MAXRECS(sbp->sb_blocksize, 6161da177e4SLinus Torvalds xfs_alloc, i == 0); 6171da177e4SLinus Torvalds mp->m_alloc_mnr[i] = XFS_BTREE_BLOCK_MINRECS(sbp->sb_blocksize, 6181da177e4SLinus Torvalds xfs_alloc, i == 0); 6191da177e4SLinus Torvalds } 6201da177e4SLinus Torvalds for (i = 0; i < 2; i++) { 6211da177e4SLinus Torvalds mp->m_bmap_dmxr[i] = XFS_BTREE_BLOCK_MAXRECS(sbp->sb_blocksize, 6221da177e4SLinus Torvalds xfs_bmbt, i == 0); 6231da177e4SLinus Torvalds mp->m_bmap_dmnr[i] = XFS_BTREE_BLOCK_MINRECS(sbp->sb_blocksize, 6241da177e4SLinus Torvalds xfs_bmbt, i == 0); 6251da177e4SLinus Torvalds } 6261da177e4SLinus Torvalds for (i = 0; i < 2; i++) { 6271da177e4SLinus Torvalds mp->m_inobt_mxr[i] = XFS_BTREE_BLOCK_MAXRECS(sbp->sb_blocksize, 6281da177e4SLinus Torvalds xfs_inobt, i == 0); 6291da177e4SLinus Torvalds mp->m_inobt_mnr[i] = XFS_BTREE_BLOCK_MINRECS(sbp->sb_blocksize, 6301da177e4SLinus Torvalds xfs_inobt, i == 0); 6311da177e4SLinus Torvalds } 6321da177e4SLinus Torvalds 6331da177e4SLinus Torvalds mp->m_bsize = XFS_FSB_TO_BB(mp, 1); 6341da177e4SLinus Torvalds mp->m_ialloc_inos = (int)MAX((__uint16_t)XFS_INODES_PER_CHUNK, 6351da177e4SLinus Torvalds sbp->sb_inopblock); 6361da177e4SLinus Torvalds mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog; 6371da177e4SLinus Torvalds } 6381da177e4SLinus Torvalds /* 6391da177e4SLinus Torvalds * xfs_mountfs 6401da177e4SLinus Torvalds * 6411da177e4SLinus Torvalds * This function does the following on an initial mount of a file system: 6421da177e4SLinus Torvalds * - reads the superblock from disk and init the mount struct 6431da177e4SLinus Torvalds * - if we're a 32-bit kernel, do a size check on the superblock 6441da177e4SLinus Torvalds * so we don't mount terabyte filesystems 6451da177e4SLinus Torvalds * - init mount struct realtime fields 6461da177e4SLinus Torvalds * - allocate inode hash table for fs 6471da177e4SLinus Torvalds * - init directory manager 6481da177e4SLinus Torvalds * - perform recovery and init the log manager 6491da177e4SLinus Torvalds */ 6501da177e4SLinus Torvalds int 6511da177e4SLinus Torvalds xfs_mountfs( 652b83bd138SNathan Scott bhv_vfs_t *vfsp, 6531da177e4SLinus Torvalds xfs_mount_t *mp, 6541da177e4SLinus Torvalds int mfsi_flags) 6551da177e4SLinus Torvalds { 6561da177e4SLinus Torvalds xfs_buf_t *bp; 6571da177e4SLinus Torvalds xfs_sb_t *sbp = &(mp->m_sb); 6581da177e4SLinus Torvalds xfs_inode_t *rip; 65967fcaa73SNathan Scott bhv_vnode_t *rvp = NULL; 6601da177e4SLinus Torvalds int readio_log, writeio_log; 6611da177e4SLinus Torvalds xfs_daddr_t d; 6621da177e4SLinus Torvalds __uint64_t ret64; 6631da177e4SLinus Torvalds __int64_t update_flags; 6641da177e4SLinus Torvalds uint quotamount, quotaflags; 6651da177e4SLinus Torvalds int agno; 6661da177e4SLinus Torvalds int uuid_mounted = 0; 6671da177e4SLinus Torvalds int error = 0; 6681da177e4SLinus Torvalds 6691da177e4SLinus Torvalds if (mp->m_sb_bp == NULL) { 670764d1f89SNathan Scott if ((error = xfs_readsb(mp, mfsi_flags))) { 671014c2544SJesper Juhl return error; 6721da177e4SLinus Torvalds } 6731da177e4SLinus Torvalds } 6741da177e4SLinus Torvalds xfs_mount_common(mp, sbp); 6751da177e4SLinus Torvalds 6761da177e4SLinus Torvalds /* 6771da177e4SLinus Torvalds * Check if sb_agblocks is aligned at stripe boundary 6781da177e4SLinus Torvalds * If sb_agblocks is NOT aligned turn off m_dalign since 6791da177e4SLinus Torvalds * allocator alignment is within an ag, therefore ag has 6801da177e4SLinus Torvalds * to be aligned at stripe boundary. 6811da177e4SLinus Torvalds */ 6821da177e4SLinus Torvalds update_flags = 0LL; 6831da177e4SLinus Torvalds if (mp->m_dalign && !(mfsi_flags & XFS_MFSI_SECOND)) { 6841da177e4SLinus Torvalds /* 6851da177e4SLinus Torvalds * If stripe unit and stripe width are not multiples 6861da177e4SLinus Torvalds * of the fs blocksize turn off alignment. 6871da177e4SLinus Torvalds */ 6881da177e4SLinus Torvalds if ((BBTOB(mp->m_dalign) & mp->m_blockmask) || 6891da177e4SLinus Torvalds (BBTOB(mp->m_swidth) & mp->m_blockmask)) { 6901da177e4SLinus Torvalds if (mp->m_flags & XFS_MOUNT_RETERR) { 6911da177e4SLinus Torvalds cmn_err(CE_WARN, 6921da177e4SLinus Torvalds "XFS: alignment check 1 failed"); 6931da177e4SLinus Torvalds error = XFS_ERROR(EINVAL); 6941da177e4SLinus Torvalds goto error1; 6951da177e4SLinus Torvalds } 6961da177e4SLinus Torvalds mp->m_dalign = mp->m_swidth = 0; 6971da177e4SLinus Torvalds } else { 6981da177e4SLinus Torvalds /* 6991da177e4SLinus Torvalds * Convert the stripe unit and width to FSBs. 7001da177e4SLinus Torvalds */ 7011da177e4SLinus Torvalds mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign); 7021da177e4SLinus Torvalds if (mp->m_dalign && (sbp->sb_agblocks % mp->m_dalign)) { 7031da177e4SLinus Torvalds if (mp->m_flags & XFS_MOUNT_RETERR) { 7041da177e4SLinus Torvalds error = XFS_ERROR(EINVAL); 7051da177e4SLinus Torvalds goto error1; 7061da177e4SLinus Torvalds } 7071da177e4SLinus Torvalds xfs_fs_cmn_err(CE_WARN, mp, 7081da177e4SLinus Torvalds "stripe alignment turned off: sunit(%d)/swidth(%d) incompatible with agsize(%d)", 7091da177e4SLinus Torvalds mp->m_dalign, mp->m_swidth, 7101da177e4SLinus Torvalds sbp->sb_agblocks); 7111da177e4SLinus Torvalds 7121da177e4SLinus Torvalds mp->m_dalign = 0; 7131da177e4SLinus Torvalds mp->m_swidth = 0; 7141da177e4SLinus Torvalds } else if (mp->m_dalign) { 7151da177e4SLinus Torvalds mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth); 7161da177e4SLinus Torvalds } else { 7171da177e4SLinus Torvalds if (mp->m_flags & XFS_MOUNT_RETERR) { 7181da177e4SLinus Torvalds xfs_fs_cmn_err(CE_WARN, mp, 7191da177e4SLinus Torvalds "stripe alignment turned off: sunit(%d) less than bsize(%d)", 7201da177e4SLinus Torvalds mp->m_dalign, 7211da177e4SLinus Torvalds mp->m_blockmask +1); 7221da177e4SLinus Torvalds error = XFS_ERROR(EINVAL); 7231da177e4SLinus Torvalds goto error1; 7241da177e4SLinus Torvalds } 7251da177e4SLinus Torvalds mp->m_swidth = 0; 7261da177e4SLinus Torvalds } 7271da177e4SLinus Torvalds } 7281da177e4SLinus Torvalds 7291da177e4SLinus Torvalds /* 7301da177e4SLinus Torvalds * Update superblock with new values 7311da177e4SLinus Torvalds * and log changes 7321da177e4SLinus Torvalds */ 7331da177e4SLinus Torvalds if (XFS_SB_VERSION_HASDALIGN(sbp)) { 7341da177e4SLinus Torvalds if (sbp->sb_unit != mp->m_dalign) { 7351da177e4SLinus Torvalds sbp->sb_unit = mp->m_dalign; 7361da177e4SLinus Torvalds update_flags |= XFS_SB_UNIT; 7371da177e4SLinus Torvalds } 7381da177e4SLinus Torvalds if (sbp->sb_width != mp->m_swidth) { 7391da177e4SLinus Torvalds sbp->sb_width = mp->m_swidth; 7401da177e4SLinus Torvalds update_flags |= XFS_SB_WIDTH; 7411da177e4SLinus Torvalds } 7421da177e4SLinus Torvalds } 7431da177e4SLinus Torvalds } else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN && 7441da177e4SLinus Torvalds XFS_SB_VERSION_HASDALIGN(&mp->m_sb)) { 7451da177e4SLinus Torvalds mp->m_dalign = sbp->sb_unit; 7461da177e4SLinus Torvalds mp->m_swidth = sbp->sb_width; 7471da177e4SLinus Torvalds } 7481da177e4SLinus Torvalds 7491da177e4SLinus Torvalds xfs_alloc_compute_maxlevels(mp); 7501da177e4SLinus Torvalds xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK); 7511da177e4SLinus Torvalds xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK); 7521da177e4SLinus Torvalds xfs_ialloc_compute_maxlevels(mp); 7531da177e4SLinus Torvalds 7541da177e4SLinus Torvalds if (sbp->sb_imax_pct) { 7551da177e4SLinus Torvalds __uint64_t icount; 7561da177e4SLinus Torvalds 7571da177e4SLinus Torvalds /* Make sure the maximum inode count is a multiple of the 7581da177e4SLinus Torvalds * units we allocate inodes in. 7591da177e4SLinus Torvalds */ 7601da177e4SLinus Torvalds 7611da177e4SLinus Torvalds icount = sbp->sb_dblocks * sbp->sb_imax_pct; 7621da177e4SLinus Torvalds do_div(icount, 100); 7631da177e4SLinus Torvalds do_div(icount, mp->m_ialloc_blks); 7641da177e4SLinus Torvalds mp->m_maxicount = (icount * mp->m_ialloc_blks) << 7651da177e4SLinus Torvalds sbp->sb_inopblog; 7661da177e4SLinus Torvalds } else 7671da177e4SLinus Torvalds mp->m_maxicount = 0; 7681da177e4SLinus Torvalds 7691da177e4SLinus Torvalds mp->m_maxioffset = xfs_max_file_offset(sbp->sb_blocklog); 7701da177e4SLinus Torvalds 7711da177e4SLinus Torvalds /* 7721da177e4SLinus Torvalds * XFS uses the uuid from the superblock as the unique 7731da177e4SLinus Torvalds * identifier for fsid. We can not use the uuid from the volume 7741da177e4SLinus Torvalds * since a single partition filesystem is identical to a single 7751da177e4SLinus Torvalds * partition volume/filesystem. 7761da177e4SLinus Torvalds */ 7771da177e4SLinus Torvalds if ((mfsi_flags & XFS_MFSI_SECOND) == 0 && 7781da177e4SLinus Torvalds (mp->m_flags & XFS_MOUNT_NOUUID) == 0) { 7791da177e4SLinus Torvalds if (xfs_uuid_mount(mp)) { 7801da177e4SLinus Torvalds error = XFS_ERROR(EINVAL); 7811da177e4SLinus Torvalds goto error1; 7821da177e4SLinus Torvalds } 7831da177e4SLinus Torvalds uuid_mounted=1; 7841da177e4SLinus Torvalds ret64 = uuid_hash64(&sbp->sb_uuid); 7851da177e4SLinus Torvalds memcpy(&vfsp->vfs_fsid, &ret64, sizeof(ret64)); 7861da177e4SLinus Torvalds } 7871da177e4SLinus Torvalds 7881da177e4SLinus Torvalds /* 7891da177e4SLinus Torvalds * Set the default minimum read and write sizes unless 7901da177e4SLinus Torvalds * already specified in a mount option. 7911da177e4SLinus Torvalds * We use smaller I/O sizes when the file system 7921da177e4SLinus Torvalds * is being used for NFS service (wsync mount option). 7931da177e4SLinus Torvalds */ 7941da177e4SLinus Torvalds if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)) { 7951da177e4SLinus Torvalds if (mp->m_flags & XFS_MOUNT_WSYNC) { 7961da177e4SLinus Torvalds readio_log = XFS_WSYNC_READIO_LOG; 7971da177e4SLinus Torvalds writeio_log = XFS_WSYNC_WRITEIO_LOG; 7981da177e4SLinus Torvalds } else { 7991da177e4SLinus Torvalds readio_log = XFS_READIO_LOG_LARGE; 8001da177e4SLinus Torvalds writeio_log = XFS_WRITEIO_LOG_LARGE; 8011da177e4SLinus Torvalds } 8021da177e4SLinus Torvalds } else { 8031da177e4SLinus Torvalds readio_log = mp->m_readio_log; 8041da177e4SLinus Torvalds writeio_log = mp->m_writeio_log; 8051da177e4SLinus Torvalds } 8061da177e4SLinus Torvalds 8071da177e4SLinus Torvalds /* 8081da177e4SLinus Torvalds * Set the number of readahead buffers to use based on 8091da177e4SLinus Torvalds * physical memory size. 8101da177e4SLinus Torvalds */ 8111da177e4SLinus Torvalds if (xfs_physmem <= 4096) /* <= 16MB */ 8121da177e4SLinus Torvalds mp->m_nreadaheads = XFS_RW_NREADAHEAD_16MB; 8131da177e4SLinus Torvalds else if (xfs_physmem <= 8192) /* <= 32MB */ 8141da177e4SLinus Torvalds mp->m_nreadaheads = XFS_RW_NREADAHEAD_32MB; 8151da177e4SLinus Torvalds else 8161da177e4SLinus Torvalds mp->m_nreadaheads = XFS_RW_NREADAHEAD_K32; 8171da177e4SLinus Torvalds if (sbp->sb_blocklog > readio_log) { 8181da177e4SLinus Torvalds mp->m_readio_log = sbp->sb_blocklog; 8191da177e4SLinus Torvalds } else { 8201da177e4SLinus Torvalds mp->m_readio_log = readio_log; 8211da177e4SLinus Torvalds } 8221da177e4SLinus Torvalds mp->m_readio_blocks = 1 << (mp->m_readio_log - sbp->sb_blocklog); 8231da177e4SLinus Torvalds if (sbp->sb_blocklog > writeio_log) { 8241da177e4SLinus Torvalds mp->m_writeio_log = sbp->sb_blocklog; 8251da177e4SLinus Torvalds } else { 8261da177e4SLinus Torvalds mp->m_writeio_log = writeio_log; 8271da177e4SLinus Torvalds } 8281da177e4SLinus Torvalds mp->m_writeio_blocks = 1 << (mp->m_writeio_log - sbp->sb_blocklog); 8291da177e4SLinus Torvalds 8301da177e4SLinus Torvalds /* 8311da177e4SLinus Torvalds * Set the inode cluster size based on the physical memory 8321da177e4SLinus Torvalds * size. This may still be overridden by the file system 8331da177e4SLinus Torvalds * block size if it is larger than the chosen cluster size. 8341da177e4SLinus Torvalds */ 8351da177e4SLinus Torvalds if (xfs_physmem <= btoc(32 * 1024 * 1024)) { /* <= 32 MB */ 8361da177e4SLinus Torvalds mp->m_inode_cluster_size = XFS_INODE_SMALL_CLUSTER_SIZE; 8371da177e4SLinus Torvalds } else { 8381da177e4SLinus Torvalds mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE; 8391da177e4SLinus Torvalds } 8401da177e4SLinus Torvalds /* 8411da177e4SLinus Torvalds * Set whether we're using inode alignment. 8421da177e4SLinus Torvalds */ 8431da177e4SLinus Torvalds if (XFS_SB_VERSION_HASALIGN(&mp->m_sb) && 8441da177e4SLinus Torvalds mp->m_sb.sb_inoalignmt >= 8451da177e4SLinus Torvalds XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size)) 8461da177e4SLinus Torvalds mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1; 8471da177e4SLinus Torvalds else 8481da177e4SLinus Torvalds mp->m_inoalign_mask = 0; 8491da177e4SLinus Torvalds /* 8501da177e4SLinus Torvalds * If we are using stripe alignment, check whether 8511da177e4SLinus Torvalds * the stripe unit is a multiple of the inode alignment 8521da177e4SLinus Torvalds */ 8531da177e4SLinus Torvalds if (mp->m_dalign && mp->m_inoalign_mask && 8541da177e4SLinus Torvalds !(mp->m_dalign & mp->m_inoalign_mask)) 8551da177e4SLinus Torvalds mp->m_sinoalign = mp->m_dalign; 8561da177e4SLinus Torvalds else 8571da177e4SLinus Torvalds mp->m_sinoalign = 0; 8581da177e4SLinus Torvalds /* 8591da177e4SLinus Torvalds * Check that the data (and log if separate) are an ok size. 8601da177e4SLinus Torvalds */ 8611da177e4SLinus Torvalds d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks); 8621da177e4SLinus Torvalds if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) { 8631da177e4SLinus Torvalds cmn_err(CE_WARN, "XFS: size check 1 failed"); 8641da177e4SLinus Torvalds error = XFS_ERROR(E2BIG); 8651da177e4SLinus Torvalds goto error1; 8661da177e4SLinus Torvalds } 8671da177e4SLinus Torvalds error = xfs_read_buf(mp, mp->m_ddev_targp, 8681da177e4SLinus Torvalds d - XFS_FSS_TO_BB(mp, 1), 8691da177e4SLinus Torvalds XFS_FSS_TO_BB(mp, 1), 0, &bp); 8701da177e4SLinus Torvalds if (!error) { 8711da177e4SLinus Torvalds xfs_buf_relse(bp); 8721da177e4SLinus Torvalds } else { 8731da177e4SLinus Torvalds cmn_err(CE_WARN, "XFS: size check 2 failed"); 8741da177e4SLinus Torvalds if (error == ENOSPC) { 8751da177e4SLinus Torvalds error = XFS_ERROR(E2BIG); 8761da177e4SLinus Torvalds } 8771da177e4SLinus Torvalds goto error1; 8781da177e4SLinus Torvalds } 8791da177e4SLinus Torvalds 8801da177e4SLinus Torvalds if (((mfsi_flags & XFS_MFSI_CLIENT) == 0) && 8811da177e4SLinus Torvalds mp->m_logdev_targp != mp->m_ddev_targp) { 8821da177e4SLinus Torvalds d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks); 8831da177e4SLinus Torvalds if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) { 8841da177e4SLinus Torvalds cmn_err(CE_WARN, "XFS: size check 3 failed"); 8851da177e4SLinus Torvalds error = XFS_ERROR(E2BIG); 8861da177e4SLinus Torvalds goto error1; 8871da177e4SLinus Torvalds } 8881da177e4SLinus Torvalds error = xfs_read_buf(mp, mp->m_logdev_targp, 8891da177e4SLinus Torvalds d - XFS_FSB_TO_BB(mp, 1), 8901da177e4SLinus Torvalds XFS_FSB_TO_BB(mp, 1), 0, &bp); 8911da177e4SLinus Torvalds if (!error) { 8921da177e4SLinus Torvalds xfs_buf_relse(bp); 8931da177e4SLinus Torvalds } else { 8941da177e4SLinus Torvalds cmn_err(CE_WARN, "XFS: size check 3 failed"); 8951da177e4SLinus Torvalds if (error == ENOSPC) { 8961da177e4SLinus Torvalds error = XFS_ERROR(E2BIG); 8971da177e4SLinus Torvalds } 8981da177e4SLinus Torvalds goto error1; 8991da177e4SLinus Torvalds } 9001da177e4SLinus Torvalds } 9011da177e4SLinus Torvalds 9021da177e4SLinus Torvalds /* 9031da177e4SLinus Torvalds * Initialize realtime fields in the mount structure 9041da177e4SLinus Torvalds */ 9051da177e4SLinus Torvalds if ((error = xfs_rtmount_init(mp))) { 9061da177e4SLinus Torvalds cmn_err(CE_WARN, "XFS: RT mount failed"); 9071da177e4SLinus Torvalds goto error1; 9081da177e4SLinus Torvalds } 9091da177e4SLinus Torvalds 9101da177e4SLinus Torvalds /* 9111da177e4SLinus Torvalds * For client case we are done now 9121da177e4SLinus Torvalds */ 9131da177e4SLinus Torvalds if (mfsi_flags & XFS_MFSI_CLIENT) { 914014c2544SJesper Juhl return 0; 9151da177e4SLinus Torvalds } 9161da177e4SLinus Torvalds 9171da177e4SLinus Torvalds /* 9181da177e4SLinus Torvalds * Copies the low order bits of the timestamp and the randomly 9191da177e4SLinus Torvalds * set "sequence" number out of a UUID. 9201da177e4SLinus Torvalds */ 9211da177e4SLinus Torvalds uuid_getnodeuniq(&sbp->sb_uuid, mp->m_fixedfsid); 9221da177e4SLinus Torvalds 9231da177e4SLinus Torvalds /* 9241da177e4SLinus Torvalds * The vfs structure needs to have a file system independent 9251da177e4SLinus Torvalds * way of checking for the invariant file system ID. Since it 9261da177e4SLinus Torvalds * can't look at mount structures it has a pointer to the data 9271da177e4SLinus Torvalds * in the mount structure. 9281da177e4SLinus Torvalds * 9291da177e4SLinus Torvalds * File systems that don't support user level file handles (i.e. 9301da177e4SLinus Torvalds * all of them except for XFS) will leave vfs_altfsid as NULL. 9311da177e4SLinus Torvalds */ 9321da177e4SLinus Torvalds vfsp->vfs_altfsid = (xfs_fsid_t *)mp->m_fixedfsid; 9331da177e4SLinus Torvalds mp->m_dmevmask = 0; /* not persistent; set after each mount */ 9341da177e4SLinus Torvalds 935f6c2d1faSNathan Scott xfs_dir_mount(mp); 9361da177e4SLinus Torvalds 9371da177e4SLinus Torvalds /* 9381da177e4SLinus Torvalds * Initialize the attribute manager's entries. 9391da177e4SLinus Torvalds */ 9401da177e4SLinus Torvalds mp->m_attr_magicpct = (mp->m_sb.sb_blocksize * 37) / 100; 9411da177e4SLinus Torvalds 9421da177e4SLinus Torvalds /* 9431da177e4SLinus Torvalds * Initialize the precomputed transaction reservations values. 9441da177e4SLinus Torvalds */ 9451da177e4SLinus Torvalds xfs_trans_init(mp); 9461da177e4SLinus Torvalds 9471da177e4SLinus Torvalds /* 9481da177e4SLinus Torvalds * Allocate and initialize the inode hash table for this 9491da177e4SLinus Torvalds * file system. 9501da177e4SLinus Torvalds */ 9511da177e4SLinus Torvalds xfs_ihash_init(mp); 9521da177e4SLinus Torvalds xfs_chash_init(mp); 9531da177e4SLinus Torvalds 9541da177e4SLinus Torvalds /* 9551da177e4SLinus Torvalds * Allocate and initialize the per-ag data. 9561da177e4SLinus Torvalds */ 9571da177e4SLinus Torvalds init_rwsem(&mp->m_peraglock); 9581da177e4SLinus Torvalds mp->m_perag = 9591da177e4SLinus Torvalds kmem_zalloc(sbp->sb_agcount * sizeof(xfs_perag_t), KM_SLEEP); 9601da177e4SLinus Torvalds 961c11e2c36SNathan Scott mp->m_maxagi = xfs_initialize_perag(vfsp, mp, sbp->sb_agcount); 9621da177e4SLinus Torvalds 9631da177e4SLinus Torvalds /* 9641da177e4SLinus Torvalds * log's mount-time initialization. Perform 1st part recovery if needed 9651da177e4SLinus Torvalds */ 9661da177e4SLinus Torvalds if (likely(sbp->sb_logblocks > 0)) { /* check for volume case */ 9671da177e4SLinus Torvalds error = xfs_log_mount(mp, mp->m_logdev_targp, 9681da177e4SLinus Torvalds XFS_FSB_TO_DADDR(mp, sbp->sb_logstart), 9691da177e4SLinus Torvalds XFS_FSB_TO_BB(mp, sbp->sb_logblocks)); 9701da177e4SLinus Torvalds if (error) { 9711da177e4SLinus Torvalds cmn_err(CE_WARN, "XFS: log mount failed"); 9721da177e4SLinus Torvalds goto error2; 9731da177e4SLinus Torvalds } 9741da177e4SLinus Torvalds } else { /* No log has been defined */ 9751da177e4SLinus Torvalds cmn_err(CE_WARN, "XFS: no log defined"); 9761da177e4SLinus Torvalds XFS_ERROR_REPORT("xfs_mountfs_int(1)", XFS_ERRLEVEL_LOW, mp); 9771da177e4SLinus Torvalds error = XFS_ERROR(EFSCORRUPTED); 9781da177e4SLinus Torvalds goto error2; 9791da177e4SLinus Torvalds } 9801da177e4SLinus Torvalds 9811da177e4SLinus Torvalds /* 9821da177e4SLinus Torvalds * Get and sanity-check the root inode. 9831da177e4SLinus Torvalds * Save the pointer to it in the mount structure. 9841da177e4SLinus Torvalds */ 9851da177e4SLinus Torvalds error = xfs_iget(mp, NULL, sbp->sb_rootino, 0, XFS_ILOCK_EXCL, &rip, 0); 9861da177e4SLinus Torvalds if (error) { 9871da177e4SLinus Torvalds cmn_err(CE_WARN, "XFS: failed to read root inode"); 9881da177e4SLinus Torvalds goto error3; 9891da177e4SLinus Torvalds } 9901da177e4SLinus Torvalds 9911da177e4SLinus Torvalds ASSERT(rip != NULL); 9921da177e4SLinus Torvalds rvp = XFS_ITOV(rip); 9931da177e4SLinus Torvalds 9941da177e4SLinus Torvalds if (unlikely((rip->i_d.di_mode & S_IFMT) != S_IFDIR)) { 9951da177e4SLinus Torvalds cmn_err(CE_WARN, "XFS: corrupted root inode"); 996b6574520SNathan Scott cmn_err(CE_WARN, "Device %s - root %llu is not a directory", 997b6574520SNathan Scott XFS_BUFTARG_NAME(mp->m_ddev_targp), 998b6574520SNathan Scott (unsigned long long)rip->i_ino); 9991da177e4SLinus Torvalds xfs_iunlock(rip, XFS_ILOCK_EXCL); 10001da177e4SLinus Torvalds XFS_ERROR_REPORT("xfs_mountfs_int(2)", XFS_ERRLEVEL_LOW, 10011da177e4SLinus Torvalds mp); 10021da177e4SLinus Torvalds error = XFS_ERROR(EFSCORRUPTED); 10031da177e4SLinus Torvalds goto error4; 10041da177e4SLinus Torvalds } 10051da177e4SLinus Torvalds mp->m_rootip = rip; /* save it */ 10061da177e4SLinus Torvalds 10071da177e4SLinus Torvalds xfs_iunlock(rip, XFS_ILOCK_EXCL); 10081da177e4SLinus Torvalds 10091da177e4SLinus Torvalds /* 10101da177e4SLinus Torvalds * Initialize realtime inode pointers in the mount structure 10111da177e4SLinus Torvalds */ 10121da177e4SLinus Torvalds if ((error = xfs_rtmount_inodes(mp))) { 10131da177e4SLinus Torvalds /* 10141da177e4SLinus Torvalds * Free up the root inode. 10151da177e4SLinus Torvalds */ 10161da177e4SLinus Torvalds cmn_err(CE_WARN, "XFS: failed to read RT inodes"); 10171da177e4SLinus Torvalds goto error4; 10181da177e4SLinus Torvalds } 10191da177e4SLinus Torvalds 10201da177e4SLinus Torvalds /* 10211da177e4SLinus Torvalds * If fs is not mounted readonly, then update the superblock 10221da177e4SLinus Torvalds * unit and width changes. 10231da177e4SLinus Torvalds */ 10241da177e4SLinus Torvalds if (update_flags && !(vfsp->vfs_flag & VFS_RDONLY)) 10251da177e4SLinus Torvalds xfs_mount_log_sbunit(mp, update_flags); 10261da177e4SLinus Torvalds 10271da177e4SLinus Torvalds /* 10281da177e4SLinus Torvalds * Initialise the XFS quota management subsystem for this mount 10291da177e4SLinus Torvalds */ 10301da177e4SLinus Torvalds if ((error = XFS_QM_INIT(mp, "amount, "aflags))) 10311da177e4SLinus Torvalds goto error4; 10321da177e4SLinus Torvalds 10331da177e4SLinus Torvalds /* 10341da177e4SLinus Torvalds * Finish recovering the file system. This part needed to be 10351da177e4SLinus Torvalds * delayed until after the root and real-time bitmap inodes 10361da177e4SLinus Torvalds * were consistently read in. 10371da177e4SLinus Torvalds */ 10381da177e4SLinus Torvalds error = xfs_log_mount_finish(mp, mfsi_flags); 10391da177e4SLinus Torvalds if (error) { 10401da177e4SLinus Torvalds cmn_err(CE_WARN, "XFS: log mount finish failed"); 10411da177e4SLinus Torvalds goto error4; 10421da177e4SLinus Torvalds } 10431da177e4SLinus Torvalds 10441da177e4SLinus Torvalds /* 10451da177e4SLinus Torvalds * Complete the quota initialisation, post-log-replay component. 10461da177e4SLinus Torvalds */ 10471da177e4SLinus Torvalds if ((error = XFS_QM_MOUNT(mp, quotamount, quotaflags, mfsi_flags))) 10481da177e4SLinus Torvalds goto error4; 10491da177e4SLinus Torvalds 10501da177e4SLinus Torvalds return 0; 10511da177e4SLinus Torvalds 10521da177e4SLinus Torvalds error4: 10531da177e4SLinus Torvalds /* 10541da177e4SLinus Torvalds * Free up the root inode. 10551da177e4SLinus Torvalds */ 10561da177e4SLinus Torvalds VN_RELE(rvp); 10571da177e4SLinus Torvalds error3: 10581da177e4SLinus Torvalds xfs_log_unmount_dealloc(mp); 10591da177e4SLinus Torvalds error2: 10601da177e4SLinus Torvalds xfs_ihash_free(mp); 10611da177e4SLinus Torvalds xfs_chash_free(mp); 10621da177e4SLinus Torvalds for (agno = 0; agno < sbp->sb_agcount; agno++) 10631da177e4SLinus Torvalds if (mp->m_perag[agno].pagb_list) 10641da177e4SLinus Torvalds kmem_free(mp->m_perag[agno].pagb_list, 10651da177e4SLinus Torvalds sizeof(xfs_perag_busy_t) * XFS_PAGB_NUM_SLOTS); 10661da177e4SLinus Torvalds kmem_free(mp->m_perag, sbp->sb_agcount * sizeof(xfs_perag_t)); 10671da177e4SLinus Torvalds mp->m_perag = NULL; 10681da177e4SLinus Torvalds /* FALLTHROUGH */ 10691da177e4SLinus Torvalds error1: 10701da177e4SLinus Torvalds if (uuid_mounted) 10711da177e4SLinus Torvalds xfs_uuid_unmount(mp); 10721da177e4SLinus Torvalds xfs_freesb(mp); 10731da177e4SLinus Torvalds return error; 10741da177e4SLinus Torvalds } 10751da177e4SLinus Torvalds 10761da177e4SLinus Torvalds /* 10771da177e4SLinus Torvalds * xfs_unmountfs 10781da177e4SLinus Torvalds * 10791da177e4SLinus Torvalds * This flushes out the inodes,dquots and the superblock, unmounts the 10801da177e4SLinus Torvalds * log and makes sure that incore structures are freed. 10811da177e4SLinus Torvalds */ 10821da177e4SLinus Torvalds int 10831da177e4SLinus Torvalds xfs_unmountfs(xfs_mount_t *mp, struct cred *cr) 10841da177e4SLinus Torvalds { 1085b83bd138SNathan Scott struct bhv_vfs *vfsp = XFS_MTOVFS(mp); 10861da177e4SLinus Torvalds #if defined(DEBUG) || defined(INDUCE_IO_ERROR) 10871da177e4SLinus Torvalds int64_t fsid; 10881da177e4SLinus Torvalds #endif 10891da177e4SLinus Torvalds 1090efa80278SChristoph Hellwig xfs_iflush_all(mp); 10911da177e4SLinus Torvalds 1092ee2a4f7cSNathan Scott XFS_QM_DQPURGEALL(mp, XFS_QMOPT_QUOTALL | XFS_QMOPT_UMOUNTING); 10931da177e4SLinus Torvalds 10941da177e4SLinus Torvalds /* 10951da177e4SLinus Torvalds * Flush out the log synchronously so that we know for sure 10961da177e4SLinus Torvalds * that nothing is pinned. This is important because bflush() 10971da177e4SLinus Torvalds * will skip pinned buffers. 10981da177e4SLinus Torvalds */ 10991da177e4SLinus Torvalds xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC); 11001da177e4SLinus Torvalds 11011da177e4SLinus Torvalds xfs_binval(mp->m_ddev_targp); 11021da177e4SLinus Torvalds if (mp->m_rtdev_targp) { 11031da177e4SLinus Torvalds xfs_binval(mp->m_rtdev_targp); 11041da177e4SLinus Torvalds } 11051da177e4SLinus Torvalds 11061da177e4SLinus Torvalds xfs_unmountfs_writesb(mp); 11071da177e4SLinus Torvalds 11081da177e4SLinus Torvalds xfs_unmountfs_wait(mp); /* wait for async bufs */ 11091da177e4SLinus Torvalds 11101da177e4SLinus Torvalds xfs_log_unmount(mp); /* Done! No more fs ops. */ 11111da177e4SLinus Torvalds 11121da177e4SLinus Torvalds xfs_freesb(mp); 11131da177e4SLinus Torvalds 11141da177e4SLinus Torvalds /* 11151da177e4SLinus Torvalds * All inodes from this mount point should be freed. 11161da177e4SLinus Torvalds */ 11171da177e4SLinus Torvalds ASSERT(mp->m_inodes == NULL); 11181da177e4SLinus Torvalds 11191da177e4SLinus Torvalds xfs_unmountfs_close(mp, cr); 11201da177e4SLinus Torvalds if ((mp->m_flags & XFS_MOUNT_NOUUID) == 0) 11211da177e4SLinus Torvalds xfs_uuid_unmount(mp); 11221da177e4SLinus Torvalds 11231da177e4SLinus Torvalds #if defined(DEBUG) || defined(INDUCE_IO_ERROR) 11241da177e4SLinus Torvalds /* 11251da177e4SLinus Torvalds * clear all error tags on this filesystem 11261da177e4SLinus Torvalds */ 11271da177e4SLinus Torvalds memcpy(&fsid, &vfsp->vfs_fsid, sizeof(int64_t)); 11281da177e4SLinus Torvalds xfs_errortag_clearall_umount(fsid, mp->m_fsname, 0); 11291da177e4SLinus Torvalds #endif 11301da177e4SLinus Torvalds XFS_IODONE(vfsp); 11311da177e4SLinus Torvalds xfs_mount_free(mp, 1); 11321da177e4SLinus Torvalds return 0; 11331da177e4SLinus Torvalds } 11341da177e4SLinus Torvalds 11351da177e4SLinus Torvalds void 11361da177e4SLinus Torvalds xfs_unmountfs_close(xfs_mount_t *mp, struct cred *cr) 11371da177e4SLinus Torvalds { 11381da177e4SLinus Torvalds if (mp->m_logdev_targp != mp->m_ddev_targp) 11391da177e4SLinus Torvalds xfs_free_buftarg(mp->m_logdev_targp, 1); 11401da177e4SLinus Torvalds if (mp->m_rtdev_targp) 11411da177e4SLinus Torvalds xfs_free_buftarg(mp->m_rtdev_targp, 1); 11421da177e4SLinus Torvalds xfs_free_buftarg(mp->m_ddev_targp, 0); 11431da177e4SLinus Torvalds } 11441da177e4SLinus Torvalds 1145ba0f32d4SChristoph Hellwig STATIC void 11461da177e4SLinus Torvalds xfs_unmountfs_wait(xfs_mount_t *mp) 11471da177e4SLinus Torvalds { 11481da177e4SLinus Torvalds if (mp->m_logdev_targp != mp->m_ddev_targp) 11491da177e4SLinus Torvalds xfs_wait_buftarg(mp->m_logdev_targp); 11501da177e4SLinus Torvalds if (mp->m_rtdev_targp) 11511da177e4SLinus Torvalds xfs_wait_buftarg(mp->m_rtdev_targp); 11521da177e4SLinus Torvalds xfs_wait_buftarg(mp->m_ddev_targp); 11531da177e4SLinus Torvalds } 11541da177e4SLinus Torvalds 11551da177e4SLinus Torvalds int 11561da177e4SLinus Torvalds xfs_unmountfs_writesb(xfs_mount_t *mp) 11571da177e4SLinus Torvalds { 11581da177e4SLinus Torvalds xfs_buf_t *sbp; 11591da177e4SLinus Torvalds xfs_sb_t *sb; 11601da177e4SLinus Torvalds int error = 0; 11611da177e4SLinus Torvalds 11621da177e4SLinus Torvalds /* 11631da177e4SLinus Torvalds * skip superblock write if fs is read-only, or 11641da177e4SLinus Torvalds * if we are doing a forced umount. 11651da177e4SLinus Torvalds */ 11661da177e4SLinus Torvalds sbp = xfs_getsb(mp, 0); 11671da177e4SLinus Torvalds if (!(XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY || 11681da177e4SLinus Torvalds XFS_FORCED_SHUTDOWN(mp))) { 11698d280b98SDavid Chinner 11708d280b98SDavid Chinner xfs_icsb_sync_counters(mp); 11718d280b98SDavid Chinner 11721da177e4SLinus Torvalds /* 11731da177e4SLinus Torvalds * mark shared-readonly if desired 11741da177e4SLinus Torvalds */ 11751da177e4SLinus Torvalds sb = XFS_BUF_TO_SBP(sbp); 11761da177e4SLinus Torvalds if (mp->m_mk_sharedro) { 11771da177e4SLinus Torvalds if (!(sb->sb_flags & XFS_SBF_READONLY)) 11781da177e4SLinus Torvalds sb->sb_flags |= XFS_SBF_READONLY; 11791da177e4SLinus Torvalds if (!XFS_SB_VERSION_HASSHARED(sb)) 11801da177e4SLinus Torvalds XFS_SB_VERSION_ADDSHARED(sb); 11811da177e4SLinus Torvalds xfs_fs_cmn_err(CE_NOTE, mp, 11821da177e4SLinus Torvalds "Unmounting, marking shared read-only"); 11831da177e4SLinus Torvalds } 11841da177e4SLinus Torvalds XFS_BUF_UNDONE(sbp); 11851da177e4SLinus Torvalds XFS_BUF_UNREAD(sbp); 11861da177e4SLinus Torvalds XFS_BUF_UNDELAYWRITE(sbp); 11871da177e4SLinus Torvalds XFS_BUF_WRITE(sbp); 11881da177e4SLinus Torvalds XFS_BUF_UNASYNC(sbp); 11891da177e4SLinus Torvalds ASSERT(XFS_BUF_TARGET(sbp) == mp->m_ddev_targp); 11901da177e4SLinus Torvalds xfsbdstrat(mp, sbp); 11911da177e4SLinus Torvalds /* Nevermind errors we might get here. */ 11921da177e4SLinus Torvalds error = xfs_iowait(sbp); 11931da177e4SLinus Torvalds if (error) 11941da177e4SLinus Torvalds xfs_ioerror_alert("xfs_unmountfs_writesb", 11951da177e4SLinus Torvalds mp, sbp, XFS_BUF_ADDR(sbp)); 11961da177e4SLinus Torvalds if (error && mp->m_mk_sharedro) 11971da177e4SLinus Torvalds xfs_fs_cmn_err(CE_ALERT, mp, "Superblock write error detected while unmounting. Filesystem may not be marked shared readonly"); 11981da177e4SLinus Torvalds } 11991da177e4SLinus Torvalds xfs_buf_relse(sbp); 1200014c2544SJesper Juhl return error; 12011da177e4SLinus Torvalds } 12021da177e4SLinus Torvalds 12031da177e4SLinus Torvalds /* 12041da177e4SLinus Torvalds * xfs_mod_sb() can be used to copy arbitrary changes to the 12051da177e4SLinus Torvalds * in-core superblock into the superblock buffer to be logged. 12061da177e4SLinus Torvalds * It does not provide the higher level of locking that is 12071da177e4SLinus Torvalds * needed to protect the in-core superblock from concurrent 12081da177e4SLinus Torvalds * access. 12091da177e4SLinus Torvalds */ 12101da177e4SLinus Torvalds void 12111da177e4SLinus Torvalds xfs_mod_sb(xfs_trans_t *tp, __int64_t fields) 12121da177e4SLinus Torvalds { 12131da177e4SLinus Torvalds xfs_buf_t *bp; 12141da177e4SLinus Torvalds int first; 12151da177e4SLinus Torvalds int last; 12161da177e4SLinus Torvalds xfs_mount_t *mp; 12171da177e4SLinus Torvalds xfs_sb_t *sbp; 12181da177e4SLinus Torvalds xfs_sb_field_t f; 12191da177e4SLinus Torvalds 12201da177e4SLinus Torvalds ASSERT(fields); 12211da177e4SLinus Torvalds if (!fields) 12221da177e4SLinus Torvalds return; 12231da177e4SLinus Torvalds mp = tp->t_mountp; 12241da177e4SLinus Torvalds bp = xfs_trans_getsb(tp, mp, 0); 12251da177e4SLinus Torvalds sbp = XFS_BUF_TO_SBP(bp); 12261da177e4SLinus Torvalds first = sizeof(xfs_sb_t); 12271da177e4SLinus Torvalds last = 0; 12281da177e4SLinus Torvalds 12291da177e4SLinus Torvalds /* translate/copy */ 12301da177e4SLinus Torvalds 12311da177e4SLinus Torvalds xfs_xlatesb(XFS_BUF_PTR(bp), &(mp->m_sb), -1, fields); 12321da177e4SLinus Torvalds 12331da177e4SLinus Torvalds /* find modified range */ 12341da177e4SLinus Torvalds 12351da177e4SLinus Torvalds f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields); 12361da177e4SLinus Torvalds ASSERT((1LL << f) & XFS_SB_MOD_BITS); 12371da177e4SLinus Torvalds first = xfs_sb_info[f].offset; 12381da177e4SLinus Torvalds 12391da177e4SLinus Torvalds f = (xfs_sb_field_t)xfs_highbit64((__uint64_t)fields); 12401da177e4SLinus Torvalds ASSERT((1LL << f) & XFS_SB_MOD_BITS); 12411da177e4SLinus Torvalds last = xfs_sb_info[f + 1].offset - 1; 12421da177e4SLinus Torvalds 12431da177e4SLinus Torvalds xfs_trans_log_buf(tp, bp, first, last); 12441da177e4SLinus Torvalds } 1245d210a28cSYingping Lu 1246d210a28cSYingping Lu 12471da177e4SLinus Torvalds /* 12481da177e4SLinus Torvalds * xfs_mod_incore_sb_unlocked() is a utility routine common used to apply 12491da177e4SLinus Torvalds * a delta to a specified field in the in-core superblock. Simply 12501da177e4SLinus Torvalds * switch on the field indicated and apply the delta to that field. 12511da177e4SLinus Torvalds * Fields are not allowed to dip below zero, so if the delta would 12521da177e4SLinus Torvalds * do this do not apply it and return EINVAL. 12531da177e4SLinus Torvalds * 12541da177e4SLinus Torvalds * The SB_LOCK must be held when this routine is called. 12551da177e4SLinus Torvalds */ 12568d280b98SDavid Chinner int 12571da177e4SLinus Torvalds xfs_mod_incore_sb_unlocked(xfs_mount_t *mp, xfs_sb_field_t field, 12581da177e4SLinus Torvalds int delta, int rsvd) 12591da177e4SLinus Torvalds { 12601da177e4SLinus Torvalds int scounter; /* short counter for 32 bit fields */ 12611da177e4SLinus Torvalds long long lcounter; /* long counter for 64 bit fields */ 12621da177e4SLinus Torvalds long long res_used, rem; 12631da177e4SLinus Torvalds 12641da177e4SLinus Torvalds /* 12651da177e4SLinus Torvalds * With the in-core superblock spin lock held, switch 12661da177e4SLinus Torvalds * on the indicated field. Apply the delta to the 12671da177e4SLinus Torvalds * proper field. If the fields value would dip below 12681da177e4SLinus Torvalds * 0, then do not apply the delta and return EINVAL. 12691da177e4SLinus Torvalds */ 12701da177e4SLinus Torvalds switch (field) { 12711da177e4SLinus Torvalds case XFS_SBS_ICOUNT: 12721da177e4SLinus Torvalds lcounter = (long long)mp->m_sb.sb_icount; 12731da177e4SLinus Torvalds lcounter += delta; 12741da177e4SLinus Torvalds if (lcounter < 0) { 12751da177e4SLinus Torvalds ASSERT(0); 1276014c2544SJesper Juhl return XFS_ERROR(EINVAL); 12771da177e4SLinus Torvalds } 12781da177e4SLinus Torvalds mp->m_sb.sb_icount = lcounter; 1279014c2544SJesper Juhl return 0; 12801da177e4SLinus Torvalds case XFS_SBS_IFREE: 12811da177e4SLinus Torvalds lcounter = (long long)mp->m_sb.sb_ifree; 12821da177e4SLinus Torvalds lcounter += delta; 12831da177e4SLinus Torvalds if (lcounter < 0) { 12841da177e4SLinus Torvalds ASSERT(0); 1285014c2544SJesper Juhl return XFS_ERROR(EINVAL); 12861da177e4SLinus Torvalds } 12871da177e4SLinus Torvalds mp->m_sb.sb_ifree = lcounter; 1288014c2544SJesper Juhl return 0; 12891da177e4SLinus Torvalds case XFS_SBS_FDBLOCKS: 12901da177e4SLinus Torvalds 12914be536deSDavid Chinner lcounter = (long long) 12924be536deSDavid Chinner mp->m_sb.sb_fdblocks - XFS_ALLOC_SET_ASIDE(mp); 12931da177e4SLinus Torvalds res_used = (long long)(mp->m_resblks - mp->m_resblks_avail); 12941da177e4SLinus Torvalds 12951da177e4SLinus Torvalds if (delta > 0) { /* Putting blocks back */ 12961da177e4SLinus Torvalds if (res_used > delta) { 12971da177e4SLinus Torvalds mp->m_resblks_avail += delta; 12981da177e4SLinus Torvalds } else { 12991da177e4SLinus Torvalds rem = delta - res_used; 13001da177e4SLinus Torvalds mp->m_resblks_avail = mp->m_resblks; 13011da177e4SLinus Torvalds lcounter += rem; 13021da177e4SLinus Torvalds } 13031da177e4SLinus Torvalds } else { /* Taking blocks away */ 13041da177e4SLinus Torvalds 13051da177e4SLinus Torvalds lcounter += delta; 13061da177e4SLinus Torvalds 13071da177e4SLinus Torvalds /* 13081da177e4SLinus Torvalds * If were out of blocks, use any available reserved blocks if 13091da177e4SLinus Torvalds * were allowed to. 13101da177e4SLinus Torvalds */ 13111da177e4SLinus Torvalds 13121da177e4SLinus Torvalds if (lcounter < 0) { 13131da177e4SLinus Torvalds if (rsvd) { 13141da177e4SLinus Torvalds lcounter = (long long)mp->m_resblks_avail + delta; 13151da177e4SLinus Torvalds if (lcounter < 0) { 1316014c2544SJesper Juhl return XFS_ERROR(ENOSPC); 13171da177e4SLinus Torvalds } 13181da177e4SLinus Torvalds mp->m_resblks_avail = lcounter; 1319014c2544SJesper Juhl return 0; 13201da177e4SLinus Torvalds } else { /* not reserved */ 1321014c2544SJesper Juhl return XFS_ERROR(ENOSPC); 13221da177e4SLinus Torvalds } 13231da177e4SLinus Torvalds } 13241da177e4SLinus Torvalds } 13251da177e4SLinus Torvalds 13264be536deSDavid Chinner mp->m_sb.sb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp); 1327014c2544SJesper Juhl return 0; 13281da177e4SLinus Torvalds case XFS_SBS_FREXTENTS: 13291da177e4SLinus Torvalds lcounter = (long long)mp->m_sb.sb_frextents; 13301da177e4SLinus Torvalds lcounter += delta; 13311da177e4SLinus Torvalds if (lcounter < 0) { 1332014c2544SJesper Juhl return XFS_ERROR(ENOSPC); 13331da177e4SLinus Torvalds } 13341da177e4SLinus Torvalds mp->m_sb.sb_frextents = lcounter; 1335014c2544SJesper Juhl return 0; 13361da177e4SLinus Torvalds case XFS_SBS_DBLOCKS: 13371da177e4SLinus Torvalds lcounter = (long long)mp->m_sb.sb_dblocks; 13381da177e4SLinus Torvalds lcounter += delta; 13391da177e4SLinus Torvalds if (lcounter < 0) { 13401da177e4SLinus Torvalds ASSERT(0); 1341014c2544SJesper Juhl return XFS_ERROR(EINVAL); 13421da177e4SLinus Torvalds } 13431da177e4SLinus Torvalds mp->m_sb.sb_dblocks = lcounter; 1344014c2544SJesper Juhl return 0; 13451da177e4SLinus Torvalds case XFS_SBS_AGCOUNT: 13461da177e4SLinus Torvalds scounter = mp->m_sb.sb_agcount; 13471da177e4SLinus Torvalds scounter += delta; 13481da177e4SLinus Torvalds if (scounter < 0) { 13491da177e4SLinus Torvalds ASSERT(0); 1350014c2544SJesper Juhl return XFS_ERROR(EINVAL); 13511da177e4SLinus Torvalds } 13521da177e4SLinus Torvalds mp->m_sb.sb_agcount = scounter; 1353014c2544SJesper Juhl return 0; 13541da177e4SLinus Torvalds case XFS_SBS_IMAX_PCT: 13551da177e4SLinus Torvalds scounter = mp->m_sb.sb_imax_pct; 13561da177e4SLinus Torvalds scounter += delta; 13571da177e4SLinus Torvalds if (scounter < 0) { 13581da177e4SLinus Torvalds ASSERT(0); 1359014c2544SJesper Juhl return XFS_ERROR(EINVAL); 13601da177e4SLinus Torvalds } 13611da177e4SLinus Torvalds mp->m_sb.sb_imax_pct = scounter; 1362014c2544SJesper Juhl return 0; 13631da177e4SLinus Torvalds case XFS_SBS_REXTSIZE: 13641da177e4SLinus Torvalds scounter = mp->m_sb.sb_rextsize; 13651da177e4SLinus Torvalds scounter += delta; 13661da177e4SLinus Torvalds if (scounter < 0) { 13671da177e4SLinus Torvalds ASSERT(0); 1368014c2544SJesper Juhl return XFS_ERROR(EINVAL); 13691da177e4SLinus Torvalds } 13701da177e4SLinus Torvalds mp->m_sb.sb_rextsize = scounter; 1371014c2544SJesper Juhl return 0; 13721da177e4SLinus Torvalds case XFS_SBS_RBMBLOCKS: 13731da177e4SLinus Torvalds scounter = mp->m_sb.sb_rbmblocks; 13741da177e4SLinus Torvalds scounter += delta; 13751da177e4SLinus Torvalds if (scounter < 0) { 13761da177e4SLinus Torvalds ASSERT(0); 1377014c2544SJesper Juhl return XFS_ERROR(EINVAL); 13781da177e4SLinus Torvalds } 13791da177e4SLinus Torvalds mp->m_sb.sb_rbmblocks = scounter; 1380014c2544SJesper Juhl return 0; 13811da177e4SLinus Torvalds case XFS_SBS_RBLOCKS: 13821da177e4SLinus Torvalds lcounter = (long long)mp->m_sb.sb_rblocks; 13831da177e4SLinus Torvalds lcounter += delta; 13841da177e4SLinus Torvalds if (lcounter < 0) { 13851da177e4SLinus Torvalds ASSERT(0); 1386014c2544SJesper Juhl return XFS_ERROR(EINVAL); 13871da177e4SLinus Torvalds } 13881da177e4SLinus Torvalds mp->m_sb.sb_rblocks = lcounter; 1389014c2544SJesper Juhl return 0; 13901da177e4SLinus Torvalds case XFS_SBS_REXTENTS: 13911da177e4SLinus Torvalds lcounter = (long long)mp->m_sb.sb_rextents; 13921da177e4SLinus Torvalds lcounter += delta; 13931da177e4SLinus Torvalds if (lcounter < 0) { 13941da177e4SLinus Torvalds ASSERT(0); 1395014c2544SJesper Juhl return XFS_ERROR(EINVAL); 13961da177e4SLinus Torvalds } 13971da177e4SLinus Torvalds mp->m_sb.sb_rextents = lcounter; 1398014c2544SJesper Juhl return 0; 13991da177e4SLinus Torvalds case XFS_SBS_REXTSLOG: 14001da177e4SLinus Torvalds scounter = mp->m_sb.sb_rextslog; 14011da177e4SLinus Torvalds scounter += delta; 14021da177e4SLinus Torvalds if (scounter < 0) { 14031da177e4SLinus Torvalds ASSERT(0); 1404014c2544SJesper Juhl return XFS_ERROR(EINVAL); 14051da177e4SLinus Torvalds } 14061da177e4SLinus Torvalds mp->m_sb.sb_rextslog = scounter; 1407014c2544SJesper Juhl return 0; 14081da177e4SLinus Torvalds default: 14091da177e4SLinus Torvalds ASSERT(0); 1410014c2544SJesper Juhl return XFS_ERROR(EINVAL); 14111da177e4SLinus Torvalds } 14121da177e4SLinus Torvalds } 14131da177e4SLinus Torvalds 14141da177e4SLinus Torvalds /* 14151da177e4SLinus Torvalds * xfs_mod_incore_sb() is used to change a field in the in-core 14161da177e4SLinus Torvalds * superblock structure by the specified delta. This modification 14171da177e4SLinus Torvalds * is protected by the SB_LOCK. Just use the xfs_mod_incore_sb_unlocked() 14181da177e4SLinus Torvalds * routine to do the work. 14191da177e4SLinus Torvalds */ 14201da177e4SLinus Torvalds int 14211da177e4SLinus Torvalds xfs_mod_incore_sb(xfs_mount_t *mp, xfs_sb_field_t field, int delta, int rsvd) 14221da177e4SLinus Torvalds { 14231da177e4SLinus Torvalds unsigned long s; 14241da177e4SLinus Torvalds int status; 14251da177e4SLinus Torvalds 14268d280b98SDavid Chinner /* check for per-cpu counters */ 14278d280b98SDavid Chinner switch (field) { 14288d280b98SDavid Chinner #ifdef HAVE_PERCPU_SB 14298d280b98SDavid Chinner case XFS_SBS_ICOUNT: 14308d280b98SDavid Chinner case XFS_SBS_IFREE: 14318d280b98SDavid Chinner case XFS_SBS_FDBLOCKS: 14328d280b98SDavid Chinner if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) { 14338d280b98SDavid Chinner status = xfs_icsb_modify_counters(mp, field, 14348d280b98SDavid Chinner delta, rsvd); 14358d280b98SDavid Chinner break; 14368d280b98SDavid Chinner } 14378d280b98SDavid Chinner /* FALLTHROUGH */ 14388d280b98SDavid Chinner #endif 14398d280b98SDavid Chinner default: 14401da177e4SLinus Torvalds s = XFS_SB_LOCK(mp); 14411da177e4SLinus Torvalds status = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd); 14421da177e4SLinus Torvalds XFS_SB_UNLOCK(mp, s); 14438d280b98SDavid Chinner break; 14448d280b98SDavid Chinner } 14458d280b98SDavid Chinner 1446014c2544SJesper Juhl return status; 14471da177e4SLinus Torvalds } 14481da177e4SLinus Torvalds 14491da177e4SLinus Torvalds /* 14501da177e4SLinus Torvalds * xfs_mod_incore_sb_batch() is used to change more than one field 14511da177e4SLinus Torvalds * in the in-core superblock structure at a time. This modification 14521da177e4SLinus Torvalds * is protected by a lock internal to this module. The fields and 14531da177e4SLinus Torvalds * changes to those fields are specified in the array of xfs_mod_sb 14541da177e4SLinus Torvalds * structures passed in. 14551da177e4SLinus Torvalds * 14561da177e4SLinus Torvalds * Either all of the specified deltas will be applied or none of 14571da177e4SLinus Torvalds * them will. If any modified field dips below 0, then all modifications 14581da177e4SLinus Torvalds * will be backed out and EINVAL will be returned. 14591da177e4SLinus Torvalds */ 14601da177e4SLinus Torvalds int 14611da177e4SLinus Torvalds xfs_mod_incore_sb_batch(xfs_mount_t *mp, xfs_mod_sb_t *msb, uint nmsb, int rsvd) 14621da177e4SLinus Torvalds { 14631da177e4SLinus Torvalds unsigned long s; 14641da177e4SLinus Torvalds int status=0; 14651da177e4SLinus Torvalds xfs_mod_sb_t *msbp; 14661da177e4SLinus Torvalds 14671da177e4SLinus Torvalds /* 14681da177e4SLinus Torvalds * Loop through the array of mod structures and apply each 14691da177e4SLinus Torvalds * individually. If any fail, then back out all those 14701da177e4SLinus Torvalds * which have already been applied. Do all of this within 14711da177e4SLinus Torvalds * the scope of the SB_LOCK so that all of the changes will 14721da177e4SLinus Torvalds * be atomic. 14731da177e4SLinus Torvalds */ 14741da177e4SLinus Torvalds s = XFS_SB_LOCK(mp); 14751da177e4SLinus Torvalds msbp = &msb[0]; 14761da177e4SLinus Torvalds for (msbp = &msbp[0]; msbp < (msb + nmsb); msbp++) { 14771da177e4SLinus Torvalds /* 14781da177e4SLinus Torvalds * Apply the delta at index n. If it fails, break 14791da177e4SLinus Torvalds * from the loop so we'll fall into the undo loop 14801da177e4SLinus Torvalds * below. 14811da177e4SLinus Torvalds */ 14828d280b98SDavid Chinner switch (msbp->msb_field) { 14838d280b98SDavid Chinner #ifdef HAVE_PERCPU_SB 14848d280b98SDavid Chinner case XFS_SBS_ICOUNT: 14858d280b98SDavid Chinner case XFS_SBS_IFREE: 14868d280b98SDavid Chinner case XFS_SBS_FDBLOCKS: 14878d280b98SDavid Chinner if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) { 1488*20b64285SDavid Chinner XFS_SB_UNLOCK(mp, s); 1489*20b64285SDavid Chinner status = xfs_icsb_modify_counters(mp, 14908d280b98SDavid Chinner msbp->msb_field, 14911da177e4SLinus Torvalds msbp->msb_delta, rsvd); 1492*20b64285SDavid Chinner s = XFS_SB_LOCK(mp); 14938d280b98SDavid Chinner break; 14948d280b98SDavid Chinner } 14958d280b98SDavid Chinner /* FALLTHROUGH */ 14968d280b98SDavid Chinner #endif 14978d280b98SDavid Chinner default: 14988d280b98SDavid Chinner status = xfs_mod_incore_sb_unlocked(mp, 14998d280b98SDavid Chinner msbp->msb_field, 15008d280b98SDavid Chinner msbp->msb_delta, rsvd); 15018d280b98SDavid Chinner break; 15028d280b98SDavid Chinner } 15038d280b98SDavid Chinner 15041da177e4SLinus Torvalds if (status != 0) { 15051da177e4SLinus Torvalds break; 15061da177e4SLinus Torvalds } 15071da177e4SLinus Torvalds } 15081da177e4SLinus Torvalds 15091da177e4SLinus Torvalds /* 15101da177e4SLinus Torvalds * If we didn't complete the loop above, then back out 15111da177e4SLinus Torvalds * any changes made to the superblock. If you add code 15121da177e4SLinus Torvalds * between the loop above and here, make sure that you 15131da177e4SLinus Torvalds * preserve the value of status. Loop back until 15141da177e4SLinus Torvalds * we step below the beginning of the array. Make sure 15151da177e4SLinus Torvalds * we don't touch anything back there. 15161da177e4SLinus Torvalds */ 15171da177e4SLinus Torvalds if (status != 0) { 15181da177e4SLinus Torvalds msbp--; 15191da177e4SLinus Torvalds while (msbp >= msb) { 15208d280b98SDavid Chinner switch (msbp->msb_field) { 15218d280b98SDavid Chinner #ifdef HAVE_PERCPU_SB 15228d280b98SDavid Chinner case XFS_SBS_ICOUNT: 15238d280b98SDavid Chinner case XFS_SBS_IFREE: 15248d280b98SDavid Chinner case XFS_SBS_FDBLOCKS: 15258d280b98SDavid Chinner if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) { 1526*20b64285SDavid Chinner XFS_SB_UNLOCK(mp, s); 1527*20b64285SDavid Chinner status = xfs_icsb_modify_counters(mp, 15288d280b98SDavid Chinner msbp->msb_field, 15298d280b98SDavid Chinner -(msbp->msb_delta), 15308d280b98SDavid Chinner rsvd); 1531*20b64285SDavid Chinner s = XFS_SB_LOCK(mp); 15328d280b98SDavid Chinner break; 15338d280b98SDavid Chinner } 15348d280b98SDavid Chinner /* FALLTHROUGH */ 15358d280b98SDavid Chinner #endif 15368d280b98SDavid Chinner default: 15371da177e4SLinus Torvalds status = xfs_mod_incore_sb_unlocked(mp, 15388d280b98SDavid Chinner msbp->msb_field, 15398d280b98SDavid Chinner -(msbp->msb_delta), 15408d280b98SDavid Chinner rsvd); 15418d280b98SDavid Chinner break; 15428d280b98SDavid Chinner } 15431da177e4SLinus Torvalds ASSERT(status == 0); 15441da177e4SLinus Torvalds msbp--; 15451da177e4SLinus Torvalds } 15461da177e4SLinus Torvalds } 15471da177e4SLinus Torvalds XFS_SB_UNLOCK(mp, s); 1548014c2544SJesper Juhl return status; 15491da177e4SLinus Torvalds } 15501da177e4SLinus Torvalds 15511da177e4SLinus Torvalds /* 15521da177e4SLinus Torvalds * xfs_getsb() is called to obtain the buffer for the superblock. 15531da177e4SLinus Torvalds * The buffer is returned locked and read in from disk. 15541da177e4SLinus Torvalds * The buffer should be released with a call to xfs_brelse(). 15551da177e4SLinus Torvalds * 15561da177e4SLinus Torvalds * If the flags parameter is BUF_TRYLOCK, then we'll only return 15571da177e4SLinus Torvalds * the superblock buffer if it can be locked without sleeping. 15581da177e4SLinus Torvalds * If it can't then we'll return NULL. 15591da177e4SLinus Torvalds */ 15601da177e4SLinus Torvalds xfs_buf_t * 15611da177e4SLinus Torvalds xfs_getsb( 15621da177e4SLinus Torvalds xfs_mount_t *mp, 15631da177e4SLinus Torvalds int flags) 15641da177e4SLinus Torvalds { 15651da177e4SLinus Torvalds xfs_buf_t *bp; 15661da177e4SLinus Torvalds 15671da177e4SLinus Torvalds ASSERT(mp->m_sb_bp != NULL); 15681da177e4SLinus Torvalds bp = mp->m_sb_bp; 15691da177e4SLinus Torvalds if (flags & XFS_BUF_TRYLOCK) { 15701da177e4SLinus Torvalds if (!XFS_BUF_CPSEMA(bp)) { 15711da177e4SLinus Torvalds return NULL; 15721da177e4SLinus Torvalds } 15731da177e4SLinus Torvalds } else { 15741da177e4SLinus Torvalds XFS_BUF_PSEMA(bp, PRIBIO); 15751da177e4SLinus Torvalds } 15761da177e4SLinus Torvalds XFS_BUF_HOLD(bp); 15771da177e4SLinus Torvalds ASSERT(XFS_BUF_ISDONE(bp)); 1578014c2544SJesper Juhl return bp; 15791da177e4SLinus Torvalds } 15801da177e4SLinus Torvalds 15811da177e4SLinus Torvalds /* 15821da177e4SLinus Torvalds * Used to free the superblock along various error paths. 15831da177e4SLinus Torvalds */ 15841da177e4SLinus Torvalds void 15851da177e4SLinus Torvalds xfs_freesb( 15861da177e4SLinus Torvalds xfs_mount_t *mp) 15871da177e4SLinus Torvalds { 15881da177e4SLinus Torvalds xfs_buf_t *bp; 15891da177e4SLinus Torvalds 15901da177e4SLinus Torvalds /* 15911da177e4SLinus Torvalds * Use xfs_getsb() so that the buffer will be locked 15921da177e4SLinus Torvalds * when we call xfs_buf_relse(). 15931da177e4SLinus Torvalds */ 15941da177e4SLinus Torvalds bp = xfs_getsb(mp, 0); 15951da177e4SLinus Torvalds XFS_BUF_UNMANAGE(bp); 15961da177e4SLinus Torvalds xfs_buf_relse(bp); 15971da177e4SLinus Torvalds mp->m_sb_bp = NULL; 15981da177e4SLinus Torvalds } 15991da177e4SLinus Torvalds 16001da177e4SLinus Torvalds /* 16011da177e4SLinus Torvalds * See if the UUID is unique among mounted XFS filesystems. 16021da177e4SLinus Torvalds * Mount fails if UUID is nil or a FS with the same UUID is already mounted. 16031da177e4SLinus Torvalds */ 16041da177e4SLinus Torvalds STATIC int 16051da177e4SLinus Torvalds xfs_uuid_mount( 16061da177e4SLinus Torvalds xfs_mount_t *mp) 16071da177e4SLinus Torvalds { 16081da177e4SLinus Torvalds if (uuid_is_nil(&mp->m_sb.sb_uuid)) { 16091da177e4SLinus Torvalds cmn_err(CE_WARN, 16101da177e4SLinus Torvalds "XFS: Filesystem %s has nil UUID - can't mount", 16111da177e4SLinus Torvalds mp->m_fsname); 16121da177e4SLinus Torvalds return -1; 16131da177e4SLinus Torvalds } 16141da177e4SLinus Torvalds if (!uuid_table_insert(&mp->m_sb.sb_uuid)) { 16151da177e4SLinus Torvalds cmn_err(CE_WARN, 16161da177e4SLinus Torvalds "XFS: Filesystem %s has duplicate UUID - can't mount", 16171da177e4SLinus Torvalds mp->m_fsname); 16181da177e4SLinus Torvalds return -1; 16191da177e4SLinus Torvalds } 16201da177e4SLinus Torvalds return 0; 16211da177e4SLinus Torvalds } 16221da177e4SLinus Torvalds 16231da177e4SLinus Torvalds /* 16241da177e4SLinus Torvalds * Remove filesystem from the UUID table. 16251da177e4SLinus Torvalds */ 16261da177e4SLinus Torvalds STATIC void 16271da177e4SLinus Torvalds xfs_uuid_unmount( 16281da177e4SLinus Torvalds xfs_mount_t *mp) 16291da177e4SLinus Torvalds { 16301da177e4SLinus Torvalds uuid_table_remove(&mp->m_sb.sb_uuid); 16311da177e4SLinus Torvalds } 16321da177e4SLinus Torvalds 16331da177e4SLinus Torvalds /* 16341da177e4SLinus Torvalds * Used to log changes to the superblock unit and width fields which could 16351da177e4SLinus Torvalds * be altered by the mount options. Only the first superblock is updated. 16361da177e4SLinus Torvalds */ 16371da177e4SLinus Torvalds STATIC void 16381da177e4SLinus Torvalds xfs_mount_log_sbunit( 16391da177e4SLinus Torvalds xfs_mount_t *mp, 16401da177e4SLinus Torvalds __int64_t fields) 16411da177e4SLinus Torvalds { 16421da177e4SLinus Torvalds xfs_trans_t *tp; 16431da177e4SLinus Torvalds 16441da177e4SLinus Torvalds ASSERT(fields & (XFS_SB_UNIT|XFS_SB_WIDTH|XFS_SB_UUID)); 16451da177e4SLinus Torvalds 16461da177e4SLinus Torvalds tp = xfs_trans_alloc(mp, XFS_TRANS_SB_UNIT); 16471da177e4SLinus Torvalds if (xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0, 16481da177e4SLinus Torvalds XFS_DEFAULT_LOG_COUNT)) { 16491da177e4SLinus Torvalds xfs_trans_cancel(tp, 0); 16501da177e4SLinus Torvalds return; 16511da177e4SLinus Torvalds } 16521da177e4SLinus Torvalds xfs_mod_sb(tp, fields); 16531da177e4SLinus Torvalds xfs_trans_commit(tp, 0, NULL); 16541da177e4SLinus Torvalds } 16558d280b98SDavid Chinner 16568d280b98SDavid Chinner 16578d280b98SDavid Chinner #ifdef HAVE_PERCPU_SB 16588d280b98SDavid Chinner /* 16598d280b98SDavid Chinner * Per-cpu incore superblock counters 16608d280b98SDavid Chinner * 16618d280b98SDavid Chinner * Simple concept, difficult implementation 16628d280b98SDavid Chinner * 16638d280b98SDavid Chinner * Basically, replace the incore superblock counters with a distributed per cpu 16648d280b98SDavid Chinner * counter for contended fields (e.g. free block count). 16658d280b98SDavid Chinner * 16668d280b98SDavid Chinner * Difficulties arise in that the incore sb is used for ENOSPC checking, and 16678d280b98SDavid Chinner * hence needs to be accurately read when we are running low on space. Hence 16688d280b98SDavid Chinner * there is a method to enable and disable the per-cpu counters based on how 16698d280b98SDavid Chinner * much "stuff" is available in them. 16708d280b98SDavid Chinner * 16718d280b98SDavid Chinner * Basically, a counter is enabled if there is enough free resource to justify 16728d280b98SDavid Chinner * running a per-cpu fast-path. If the per-cpu counter runs out (i.e. a local 16738d280b98SDavid Chinner * ENOSPC), then we disable the counters to synchronise all callers and 16748d280b98SDavid Chinner * re-distribute the available resources. 16758d280b98SDavid Chinner * 16768d280b98SDavid Chinner * If, once we redistributed the available resources, we still get a failure, 16778d280b98SDavid Chinner * we disable the per-cpu counter and go through the slow path. 16788d280b98SDavid Chinner * 16798d280b98SDavid Chinner * The slow path is the current xfs_mod_incore_sb() function. This means that 16808d280b98SDavid Chinner * when we disable a per-cpu counter, we need to drain it's resources back to 16818d280b98SDavid Chinner * the global superblock. We do this after disabling the counter to prevent 16828d280b98SDavid Chinner * more threads from queueing up on the counter. 16838d280b98SDavid Chinner * 16848d280b98SDavid Chinner * Essentially, this means that we still need a lock in the fast path to enable 16858d280b98SDavid Chinner * synchronisation between the global counters and the per-cpu counters. This 16868d280b98SDavid Chinner * is not a problem because the lock will be local to a CPU almost all the time 16878d280b98SDavid Chinner * and have little contention except when we get to ENOSPC conditions. 16888d280b98SDavid Chinner * 16898d280b98SDavid Chinner * Basically, this lock becomes a barrier that enables us to lock out the fast 16908d280b98SDavid Chinner * path while we do things like enabling and disabling counters and 16918d280b98SDavid Chinner * synchronising the counters. 16928d280b98SDavid Chinner * 16938d280b98SDavid Chinner * Locking rules: 16948d280b98SDavid Chinner * 16958d280b98SDavid Chinner * 1. XFS_SB_LOCK() before picking up per-cpu locks 16968d280b98SDavid Chinner * 2. per-cpu locks always picked up via for_each_online_cpu() order 16978d280b98SDavid Chinner * 3. accurate counter sync requires XFS_SB_LOCK + per cpu locks 16988d280b98SDavid Chinner * 4. modifying per-cpu counters requires holding per-cpu lock 16998d280b98SDavid Chinner * 5. modifying global counters requires holding XFS_SB_LOCK 17008d280b98SDavid Chinner * 6. enabling or disabling a counter requires holding the XFS_SB_LOCK 17018d280b98SDavid Chinner * and _none_ of the per-cpu locks. 17028d280b98SDavid Chinner * 17038d280b98SDavid Chinner * Disabled counters are only ever re-enabled by a balance operation 17048d280b98SDavid Chinner * that results in more free resources per CPU than a given threshold. 17058d280b98SDavid Chinner * To ensure counters don't remain disabled, they are rebalanced when 17068d280b98SDavid Chinner * the global resource goes above a higher threshold (i.e. some hysteresis 17078d280b98SDavid Chinner * is present to prevent thrashing). 17088d280b98SDavid Chinner */ 1709e8234a68SDavid Chinner 17105a67e4c5SChandra Seetharaman #ifdef CONFIG_HOTPLUG_CPU 1711e8234a68SDavid Chinner /* 1712e8234a68SDavid Chinner * hot-plug CPU notifier support. 1713e8234a68SDavid Chinner * 17145a67e4c5SChandra Seetharaman * We need a notifier per filesystem as we need to be able to identify 17155a67e4c5SChandra Seetharaman * the filesystem to balance the counters out. This is achieved by 17165a67e4c5SChandra Seetharaman * having a notifier block embedded in the xfs_mount_t and doing pointer 17175a67e4c5SChandra Seetharaman * magic to get the mount pointer from the notifier block address. 1718e8234a68SDavid Chinner */ 1719e8234a68SDavid Chinner STATIC int 1720e8234a68SDavid Chinner xfs_icsb_cpu_notify( 1721e8234a68SDavid Chinner struct notifier_block *nfb, 1722e8234a68SDavid Chinner unsigned long action, 1723e8234a68SDavid Chinner void *hcpu) 1724e8234a68SDavid Chinner { 1725e8234a68SDavid Chinner xfs_icsb_cnts_t *cntp; 1726e8234a68SDavid Chinner xfs_mount_t *mp; 1727e8234a68SDavid Chinner int s; 1728e8234a68SDavid Chinner 1729e8234a68SDavid Chinner mp = (xfs_mount_t *)container_of(nfb, xfs_mount_t, m_icsb_notifier); 1730e8234a68SDavid Chinner cntp = (xfs_icsb_cnts_t *) 1731e8234a68SDavid Chinner per_cpu_ptr(mp->m_sb_cnts, (unsigned long)hcpu); 1732e8234a68SDavid Chinner switch (action) { 1733e8234a68SDavid Chinner case CPU_UP_PREPARE: 1734e8234a68SDavid Chinner /* Easy Case - initialize the area and locks, and 1735e8234a68SDavid Chinner * then rebalance when online does everything else for us. */ 173601e1b69cSDavid Chinner memset(cntp, 0, sizeof(xfs_icsb_cnts_t)); 1737e8234a68SDavid Chinner break; 1738e8234a68SDavid Chinner case CPU_ONLINE: 1739*20b64285SDavid Chinner mutex_lock(&mp->m_icsb_mutex); 1740*20b64285SDavid Chinner xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0, 0); 1741*20b64285SDavid Chinner xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0, 0); 1742*20b64285SDavid Chinner xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0, 0); 1743*20b64285SDavid Chinner mutex_unlock(&mp->m_icsb_mutex); 1744e8234a68SDavid Chinner break; 1745e8234a68SDavid Chinner case CPU_DEAD: 1746e8234a68SDavid Chinner /* Disable all the counters, then fold the dead cpu's 1747e8234a68SDavid Chinner * count into the total on the global superblock and 1748e8234a68SDavid Chinner * re-enable the counters. */ 1749*20b64285SDavid Chinner mutex_lock(&mp->m_icsb_mutex); 1750e8234a68SDavid Chinner s = XFS_SB_LOCK(mp); 1751e8234a68SDavid Chinner xfs_icsb_disable_counter(mp, XFS_SBS_ICOUNT); 1752e8234a68SDavid Chinner xfs_icsb_disable_counter(mp, XFS_SBS_IFREE); 1753e8234a68SDavid Chinner xfs_icsb_disable_counter(mp, XFS_SBS_FDBLOCKS); 1754e8234a68SDavid Chinner 1755e8234a68SDavid Chinner mp->m_sb.sb_icount += cntp->icsb_icount; 1756e8234a68SDavid Chinner mp->m_sb.sb_ifree += cntp->icsb_ifree; 1757e8234a68SDavid Chinner mp->m_sb.sb_fdblocks += cntp->icsb_fdblocks; 1758e8234a68SDavid Chinner 175901e1b69cSDavid Chinner memset(cntp, 0, sizeof(xfs_icsb_cnts_t)); 1760e8234a68SDavid Chinner 1761*20b64285SDavid Chinner xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 1762*20b64285SDavid Chinner XFS_ICSB_SB_LOCKED, 0); 1763*20b64285SDavid Chinner xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 1764*20b64285SDavid Chinner XFS_ICSB_SB_LOCKED, 0); 1765*20b64285SDavid Chinner xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 1766*20b64285SDavid Chinner XFS_ICSB_SB_LOCKED, 0); 1767e8234a68SDavid Chinner XFS_SB_UNLOCK(mp, s); 1768*20b64285SDavid Chinner mutex_unlock(&mp->m_icsb_mutex); 1769e8234a68SDavid Chinner break; 1770e8234a68SDavid Chinner } 1771e8234a68SDavid Chinner 1772e8234a68SDavid Chinner return NOTIFY_OK; 1773e8234a68SDavid Chinner } 17745a67e4c5SChandra Seetharaman #endif /* CONFIG_HOTPLUG_CPU */ 1775e8234a68SDavid Chinner 17768d280b98SDavid Chinner int 17778d280b98SDavid Chinner xfs_icsb_init_counters( 17788d280b98SDavid Chinner xfs_mount_t *mp) 17798d280b98SDavid Chinner { 17808d280b98SDavid Chinner xfs_icsb_cnts_t *cntp; 17818d280b98SDavid Chinner int i; 17828d280b98SDavid Chinner 17838d280b98SDavid Chinner mp->m_sb_cnts = alloc_percpu(xfs_icsb_cnts_t); 17848d280b98SDavid Chinner if (mp->m_sb_cnts == NULL) 17858d280b98SDavid Chinner return -ENOMEM; 17868d280b98SDavid Chinner 17875a67e4c5SChandra Seetharaman #ifdef CONFIG_HOTPLUG_CPU 1788e8234a68SDavid Chinner mp->m_icsb_notifier.notifier_call = xfs_icsb_cpu_notify; 1789e8234a68SDavid Chinner mp->m_icsb_notifier.priority = 0; 17905a67e4c5SChandra Seetharaman register_hotcpu_notifier(&mp->m_icsb_notifier); 17915a67e4c5SChandra Seetharaman #endif /* CONFIG_HOTPLUG_CPU */ 1792e8234a68SDavid Chinner 17938d280b98SDavid Chinner for_each_online_cpu(i) { 17948d280b98SDavid Chinner cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i); 179501e1b69cSDavid Chinner memset(cntp, 0, sizeof(xfs_icsb_cnts_t)); 17968d280b98SDavid Chinner } 1797*20b64285SDavid Chinner 1798*20b64285SDavid Chinner mutex_init(&mp->m_icsb_mutex); 1799*20b64285SDavid Chinner 18008d280b98SDavid Chinner /* 18018d280b98SDavid Chinner * start with all counters disabled so that the 18028d280b98SDavid Chinner * initial balance kicks us off correctly 18038d280b98SDavid Chinner */ 18048d280b98SDavid Chinner mp->m_icsb_counters = -1; 18058d280b98SDavid Chinner return 0; 18068d280b98SDavid Chinner } 18078d280b98SDavid Chinner 18088d280b98SDavid Chinner STATIC void 18098d280b98SDavid Chinner xfs_icsb_destroy_counters( 18108d280b98SDavid Chinner xfs_mount_t *mp) 18118d280b98SDavid Chinner { 1812e8234a68SDavid Chinner if (mp->m_sb_cnts) { 18135a67e4c5SChandra Seetharaman unregister_hotcpu_notifier(&mp->m_icsb_notifier); 18148d280b98SDavid Chinner free_percpu(mp->m_sb_cnts); 18158d280b98SDavid Chinner } 1816e8234a68SDavid Chinner } 18178d280b98SDavid Chinner 18187989cb8eSDavid Chinner STATIC_INLINE void 181901e1b69cSDavid Chinner xfs_icsb_lock_cntr( 182001e1b69cSDavid Chinner xfs_icsb_cnts_t *icsbp) 182101e1b69cSDavid Chinner { 182201e1b69cSDavid Chinner while (test_and_set_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags)) { 182301e1b69cSDavid Chinner ndelay(1000); 182401e1b69cSDavid Chinner } 182501e1b69cSDavid Chinner } 182601e1b69cSDavid Chinner 18277989cb8eSDavid Chinner STATIC_INLINE void 182801e1b69cSDavid Chinner xfs_icsb_unlock_cntr( 182901e1b69cSDavid Chinner xfs_icsb_cnts_t *icsbp) 183001e1b69cSDavid Chinner { 183101e1b69cSDavid Chinner clear_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags); 183201e1b69cSDavid Chinner } 183301e1b69cSDavid Chinner 18348d280b98SDavid Chinner 18357989cb8eSDavid Chinner STATIC_INLINE void 18368d280b98SDavid Chinner xfs_icsb_lock_all_counters( 18378d280b98SDavid Chinner xfs_mount_t *mp) 18388d280b98SDavid Chinner { 18398d280b98SDavid Chinner xfs_icsb_cnts_t *cntp; 18408d280b98SDavid Chinner int i; 18418d280b98SDavid Chinner 18428d280b98SDavid Chinner for_each_online_cpu(i) { 18438d280b98SDavid Chinner cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i); 184401e1b69cSDavid Chinner xfs_icsb_lock_cntr(cntp); 18458d280b98SDavid Chinner } 18468d280b98SDavid Chinner } 18478d280b98SDavid Chinner 18487989cb8eSDavid Chinner STATIC_INLINE void 18498d280b98SDavid Chinner xfs_icsb_unlock_all_counters( 18508d280b98SDavid Chinner xfs_mount_t *mp) 18518d280b98SDavid Chinner { 18528d280b98SDavid Chinner xfs_icsb_cnts_t *cntp; 18538d280b98SDavid Chinner int i; 18548d280b98SDavid Chinner 18558d280b98SDavid Chinner for_each_online_cpu(i) { 18568d280b98SDavid Chinner cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i); 185701e1b69cSDavid Chinner xfs_icsb_unlock_cntr(cntp); 18588d280b98SDavid Chinner } 18598d280b98SDavid Chinner } 18608d280b98SDavid Chinner 18618d280b98SDavid Chinner STATIC void 18628d280b98SDavid Chinner xfs_icsb_count( 18638d280b98SDavid Chinner xfs_mount_t *mp, 18648d280b98SDavid Chinner xfs_icsb_cnts_t *cnt, 18658d280b98SDavid Chinner int flags) 18668d280b98SDavid Chinner { 18678d280b98SDavid Chinner xfs_icsb_cnts_t *cntp; 18688d280b98SDavid Chinner int i; 18698d280b98SDavid Chinner 18708d280b98SDavid Chinner memset(cnt, 0, sizeof(xfs_icsb_cnts_t)); 18718d280b98SDavid Chinner 18728d280b98SDavid Chinner if (!(flags & XFS_ICSB_LAZY_COUNT)) 18738d280b98SDavid Chinner xfs_icsb_lock_all_counters(mp); 18748d280b98SDavid Chinner 18758d280b98SDavid Chinner for_each_online_cpu(i) { 18768d280b98SDavid Chinner cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i); 18778d280b98SDavid Chinner cnt->icsb_icount += cntp->icsb_icount; 18788d280b98SDavid Chinner cnt->icsb_ifree += cntp->icsb_ifree; 18798d280b98SDavid Chinner cnt->icsb_fdblocks += cntp->icsb_fdblocks; 18808d280b98SDavid Chinner } 18818d280b98SDavid Chinner 18828d280b98SDavid Chinner if (!(flags & XFS_ICSB_LAZY_COUNT)) 18838d280b98SDavid Chinner xfs_icsb_unlock_all_counters(mp); 18848d280b98SDavid Chinner } 18858d280b98SDavid Chinner 18868d280b98SDavid Chinner STATIC int 18878d280b98SDavid Chinner xfs_icsb_counter_disabled( 18888d280b98SDavid Chinner xfs_mount_t *mp, 18898d280b98SDavid Chinner xfs_sb_field_t field) 18908d280b98SDavid Chinner { 18918d280b98SDavid Chinner ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS)); 18928d280b98SDavid Chinner return test_bit(field, &mp->m_icsb_counters); 18938d280b98SDavid Chinner } 18948d280b98SDavid Chinner 18958d280b98SDavid Chinner STATIC int 18968d280b98SDavid Chinner xfs_icsb_disable_counter( 18978d280b98SDavid Chinner xfs_mount_t *mp, 18988d280b98SDavid Chinner xfs_sb_field_t field) 18998d280b98SDavid Chinner { 19008d280b98SDavid Chinner xfs_icsb_cnts_t cnt; 19018d280b98SDavid Chinner 19028d280b98SDavid Chinner ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS)); 19038d280b98SDavid Chinner 1904*20b64285SDavid Chinner /* 1905*20b64285SDavid Chinner * If we are already disabled, then there is nothing to do 1906*20b64285SDavid Chinner * here. We check before locking all the counters to avoid 1907*20b64285SDavid Chinner * the expensive lock operation when being called in the 1908*20b64285SDavid Chinner * slow path and the counter is already disabled. This is 1909*20b64285SDavid Chinner * safe because the only time we set or clear this state is under 1910*20b64285SDavid Chinner * the m_icsb_mutex. 1911*20b64285SDavid Chinner */ 1912*20b64285SDavid Chinner if (xfs_icsb_counter_disabled(mp, field)) 1913*20b64285SDavid Chinner return 0; 1914*20b64285SDavid Chinner 19158d280b98SDavid Chinner xfs_icsb_lock_all_counters(mp); 19168d280b98SDavid Chinner if (!test_and_set_bit(field, &mp->m_icsb_counters)) { 19178d280b98SDavid Chinner /* drain back to superblock */ 19188d280b98SDavid Chinner 19198d280b98SDavid Chinner xfs_icsb_count(mp, &cnt, XFS_ICSB_SB_LOCKED|XFS_ICSB_LAZY_COUNT); 19208d280b98SDavid Chinner switch(field) { 19218d280b98SDavid Chinner case XFS_SBS_ICOUNT: 19228d280b98SDavid Chinner mp->m_sb.sb_icount = cnt.icsb_icount; 19238d280b98SDavid Chinner break; 19248d280b98SDavid Chinner case XFS_SBS_IFREE: 19258d280b98SDavid Chinner mp->m_sb.sb_ifree = cnt.icsb_ifree; 19268d280b98SDavid Chinner break; 19278d280b98SDavid Chinner case XFS_SBS_FDBLOCKS: 19288d280b98SDavid Chinner mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks; 19298d280b98SDavid Chinner break; 19308d280b98SDavid Chinner default: 19318d280b98SDavid Chinner BUG(); 19328d280b98SDavid Chinner } 19338d280b98SDavid Chinner } 19348d280b98SDavid Chinner 19358d280b98SDavid Chinner xfs_icsb_unlock_all_counters(mp); 19368d280b98SDavid Chinner 19378d280b98SDavid Chinner return 0; 19388d280b98SDavid Chinner } 19398d280b98SDavid Chinner 19408d280b98SDavid Chinner STATIC void 19418d280b98SDavid Chinner xfs_icsb_enable_counter( 19428d280b98SDavid Chinner xfs_mount_t *mp, 19438d280b98SDavid Chinner xfs_sb_field_t field, 19448d280b98SDavid Chinner uint64_t count, 19458d280b98SDavid Chinner uint64_t resid) 19468d280b98SDavid Chinner { 19478d280b98SDavid Chinner xfs_icsb_cnts_t *cntp; 19488d280b98SDavid Chinner int i; 19498d280b98SDavid Chinner 19508d280b98SDavid Chinner ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS)); 19518d280b98SDavid Chinner 19528d280b98SDavid Chinner xfs_icsb_lock_all_counters(mp); 19538d280b98SDavid Chinner for_each_online_cpu(i) { 19548d280b98SDavid Chinner cntp = per_cpu_ptr(mp->m_sb_cnts, i); 19558d280b98SDavid Chinner switch (field) { 19568d280b98SDavid Chinner case XFS_SBS_ICOUNT: 19578d280b98SDavid Chinner cntp->icsb_icount = count + resid; 19588d280b98SDavid Chinner break; 19598d280b98SDavid Chinner case XFS_SBS_IFREE: 19608d280b98SDavid Chinner cntp->icsb_ifree = count + resid; 19618d280b98SDavid Chinner break; 19628d280b98SDavid Chinner case XFS_SBS_FDBLOCKS: 19638d280b98SDavid Chinner cntp->icsb_fdblocks = count + resid; 19648d280b98SDavid Chinner break; 19658d280b98SDavid Chinner default: 19668d280b98SDavid Chinner BUG(); 19678d280b98SDavid Chinner break; 19688d280b98SDavid Chinner } 19698d280b98SDavid Chinner resid = 0; 19708d280b98SDavid Chinner } 19718d280b98SDavid Chinner clear_bit(field, &mp->m_icsb_counters); 19728d280b98SDavid Chinner xfs_icsb_unlock_all_counters(mp); 19738d280b98SDavid Chinner } 19748d280b98SDavid Chinner 19758d280b98SDavid Chinner STATIC void 19768d280b98SDavid Chinner xfs_icsb_sync_counters_int( 19778d280b98SDavid Chinner xfs_mount_t *mp, 19788d280b98SDavid Chinner int flags) 19798d280b98SDavid Chinner { 19808d280b98SDavid Chinner xfs_icsb_cnts_t cnt; 19818d280b98SDavid Chinner int s; 19828d280b98SDavid Chinner 19838d280b98SDavid Chinner /* Pass 1: lock all counters */ 19848d280b98SDavid Chinner if ((flags & XFS_ICSB_SB_LOCKED) == 0) 19858d280b98SDavid Chinner s = XFS_SB_LOCK(mp); 19868d280b98SDavid Chinner 19878d280b98SDavid Chinner xfs_icsb_count(mp, &cnt, flags); 19888d280b98SDavid Chinner 19898d280b98SDavid Chinner /* Step 3: update mp->m_sb fields */ 19908d280b98SDavid Chinner if (!xfs_icsb_counter_disabled(mp, XFS_SBS_ICOUNT)) 19918d280b98SDavid Chinner mp->m_sb.sb_icount = cnt.icsb_icount; 19928d280b98SDavid Chinner if (!xfs_icsb_counter_disabled(mp, XFS_SBS_IFREE)) 19938d280b98SDavid Chinner mp->m_sb.sb_ifree = cnt.icsb_ifree; 19948d280b98SDavid Chinner if (!xfs_icsb_counter_disabled(mp, XFS_SBS_FDBLOCKS)) 19958d280b98SDavid Chinner mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks; 19968d280b98SDavid Chinner 19978d280b98SDavid Chinner if ((flags & XFS_ICSB_SB_LOCKED) == 0) 19988d280b98SDavid Chinner XFS_SB_UNLOCK(mp, s); 19998d280b98SDavid Chinner } 20008d280b98SDavid Chinner 20018d280b98SDavid Chinner /* 20028d280b98SDavid Chinner * Accurate update of per-cpu counters to incore superblock 20038d280b98SDavid Chinner */ 20048d280b98SDavid Chinner STATIC void 20058d280b98SDavid Chinner xfs_icsb_sync_counters( 20068d280b98SDavid Chinner xfs_mount_t *mp) 20078d280b98SDavid Chinner { 20088d280b98SDavid Chinner xfs_icsb_sync_counters_int(mp, 0); 20098d280b98SDavid Chinner } 20108d280b98SDavid Chinner 20118d280b98SDavid Chinner /* 20128d280b98SDavid Chinner * lazy addition used for things like df, background sb syncs, etc 20138d280b98SDavid Chinner */ 20148d280b98SDavid Chinner void 20158d280b98SDavid Chinner xfs_icsb_sync_counters_lazy( 20168d280b98SDavid Chinner xfs_mount_t *mp) 20178d280b98SDavid Chinner { 20188d280b98SDavid Chinner xfs_icsb_sync_counters_int(mp, XFS_ICSB_LAZY_COUNT); 20198d280b98SDavid Chinner } 20208d280b98SDavid Chinner 20218d280b98SDavid Chinner /* 20228d280b98SDavid Chinner * Balance and enable/disable counters as necessary. 20238d280b98SDavid Chinner * 2024*20b64285SDavid Chinner * Thresholds for re-enabling counters are somewhat magic. inode counts are 2025*20b64285SDavid Chinner * chosen to be the same number as single on disk allocation chunk per CPU, and 2026*20b64285SDavid Chinner * free blocks is something far enough zero that we aren't going thrash when we 2027*20b64285SDavid Chinner * get near ENOSPC. We also need to supply a minimum we require per cpu to 2028*20b64285SDavid Chinner * prevent looping endlessly when xfs_alloc_space asks for more than will 2029*20b64285SDavid Chinner * be distributed to a single CPU but each CPU has enough blocks to be 2030*20b64285SDavid Chinner * reenabled. 2031*20b64285SDavid Chinner * 2032*20b64285SDavid Chinner * Note that we can be called when counters are already disabled. 2033*20b64285SDavid Chinner * xfs_icsb_disable_counter() optimises the counter locking in this case to 2034*20b64285SDavid Chinner * prevent locking every per-cpu counter needlessly. 20358d280b98SDavid Chinner */ 2036*20b64285SDavid Chinner 2037*20b64285SDavid Chinner #define XFS_ICSB_INO_CNTR_REENABLE (uint64_t)64 20384be536deSDavid Chinner #define XFS_ICSB_FDBLK_CNTR_REENABLE(mp) \ 2039*20b64285SDavid Chinner (uint64_t)(512 + XFS_ALLOC_SET_ASIDE(mp)) 20408d280b98SDavid Chinner STATIC void 20418d280b98SDavid Chinner xfs_icsb_balance_counter( 20428d280b98SDavid Chinner xfs_mount_t *mp, 20438d280b98SDavid Chinner xfs_sb_field_t field, 2044*20b64285SDavid Chinner int flags, 2045*20b64285SDavid Chinner int min_per_cpu) 20468d280b98SDavid Chinner { 20476fdf8cccSNathan Scott uint64_t count, resid; 20488d280b98SDavid Chinner int weight = num_online_cpus(); 20498d280b98SDavid Chinner int s; 2050*20b64285SDavid Chinner uint64_t min = (uint64_t)min_per_cpu; 20518d280b98SDavid Chinner 20528d280b98SDavid Chinner if (!(flags & XFS_ICSB_SB_LOCKED)) 20538d280b98SDavid Chinner s = XFS_SB_LOCK(mp); 20548d280b98SDavid Chinner 20558d280b98SDavid Chinner /* disable counter and sync counter */ 20568d280b98SDavid Chinner xfs_icsb_disable_counter(mp, field); 20578d280b98SDavid Chinner 20588d280b98SDavid Chinner /* update counters - first CPU gets residual*/ 20598d280b98SDavid Chinner switch (field) { 20608d280b98SDavid Chinner case XFS_SBS_ICOUNT: 20618d280b98SDavid Chinner count = mp->m_sb.sb_icount; 20628d280b98SDavid Chinner resid = do_div(count, weight); 2063*20b64285SDavid Chinner if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE)) 20648d280b98SDavid Chinner goto out; 20658d280b98SDavid Chinner break; 20668d280b98SDavid Chinner case XFS_SBS_IFREE: 20678d280b98SDavid Chinner count = mp->m_sb.sb_ifree; 20688d280b98SDavid Chinner resid = do_div(count, weight); 2069*20b64285SDavid Chinner if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE)) 20708d280b98SDavid Chinner goto out; 20718d280b98SDavid Chinner break; 20728d280b98SDavid Chinner case XFS_SBS_FDBLOCKS: 20738d280b98SDavid Chinner count = mp->m_sb.sb_fdblocks; 20748d280b98SDavid Chinner resid = do_div(count, weight); 2075*20b64285SDavid Chinner if (count < max(min, XFS_ICSB_FDBLK_CNTR_REENABLE(mp))) 20768d280b98SDavid Chinner goto out; 20778d280b98SDavid Chinner break; 20788d280b98SDavid Chinner default: 20798d280b98SDavid Chinner BUG(); 20806fdf8cccSNathan Scott count = resid = 0; /* quiet, gcc */ 20818d280b98SDavid Chinner break; 20828d280b98SDavid Chinner } 20838d280b98SDavid Chinner 20848d280b98SDavid Chinner xfs_icsb_enable_counter(mp, field, count, resid); 20858d280b98SDavid Chinner out: 20868d280b98SDavid Chinner if (!(flags & XFS_ICSB_SB_LOCKED)) 20878d280b98SDavid Chinner XFS_SB_UNLOCK(mp, s); 20888d280b98SDavid Chinner } 20898d280b98SDavid Chinner 2090*20b64285SDavid Chinner int 2091*20b64285SDavid Chinner xfs_icsb_modify_counters( 20928d280b98SDavid Chinner xfs_mount_t *mp, 20938d280b98SDavid Chinner xfs_sb_field_t field, 20948d280b98SDavid Chinner int delta, 2095*20b64285SDavid Chinner int rsvd) 20968d280b98SDavid Chinner { 20978d280b98SDavid Chinner xfs_icsb_cnts_t *icsbp; 20988d280b98SDavid Chinner long long lcounter; /* long counter for 64 bit fields */ 2099*20b64285SDavid Chinner int cpu, ret = 0, s; 21008d280b98SDavid Chinner 2101*20b64285SDavid Chinner might_sleep(); 21028d280b98SDavid Chinner again: 21038d280b98SDavid Chinner cpu = get_cpu(); 2104*20b64285SDavid Chinner icsbp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, cpu); 2105*20b64285SDavid Chinner 2106*20b64285SDavid Chinner /* 2107*20b64285SDavid Chinner * if the counter is disabled, go to slow path 2108*20b64285SDavid Chinner */ 21098d280b98SDavid Chinner if (unlikely(xfs_icsb_counter_disabled(mp, field))) 21108d280b98SDavid Chinner goto slow_path; 2111*20b64285SDavid Chinner xfs_icsb_lock_cntr(icsbp); 2112*20b64285SDavid Chinner if (unlikely(xfs_icsb_counter_disabled(mp, field))) { 2113*20b64285SDavid Chinner xfs_icsb_unlock_cntr(icsbp); 2114*20b64285SDavid Chinner goto slow_path; 2115*20b64285SDavid Chinner } 21168d280b98SDavid Chinner 21178d280b98SDavid Chinner switch (field) { 21188d280b98SDavid Chinner case XFS_SBS_ICOUNT: 21198d280b98SDavid Chinner lcounter = icsbp->icsb_icount; 21208d280b98SDavid Chinner lcounter += delta; 21218d280b98SDavid Chinner if (unlikely(lcounter < 0)) 2122*20b64285SDavid Chinner goto balance_counter; 21238d280b98SDavid Chinner icsbp->icsb_icount = lcounter; 21248d280b98SDavid Chinner break; 21258d280b98SDavid Chinner 21268d280b98SDavid Chinner case XFS_SBS_IFREE: 21278d280b98SDavid Chinner lcounter = icsbp->icsb_ifree; 21288d280b98SDavid Chinner lcounter += delta; 21298d280b98SDavid Chinner if (unlikely(lcounter < 0)) 2130*20b64285SDavid Chinner goto balance_counter; 21318d280b98SDavid Chinner icsbp->icsb_ifree = lcounter; 21328d280b98SDavid Chinner break; 21338d280b98SDavid Chinner 21348d280b98SDavid Chinner case XFS_SBS_FDBLOCKS: 21358d280b98SDavid Chinner BUG_ON((mp->m_resblks - mp->m_resblks_avail) != 0); 21368d280b98SDavid Chinner 21374be536deSDavid Chinner lcounter = icsbp->icsb_fdblocks - XFS_ALLOC_SET_ASIDE(mp); 21388d280b98SDavid Chinner lcounter += delta; 21398d280b98SDavid Chinner if (unlikely(lcounter < 0)) 2140*20b64285SDavid Chinner goto balance_counter; 21414be536deSDavid Chinner icsbp->icsb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp); 21428d280b98SDavid Chinner break; 21438d280b98SDavid Chinner default: 21448d280b98SDavid Chinner BUG(); 21458d280b98SDavid Chinner break; 21468d280b98SDavid Chinner } 214701e1b69cSDavid Chinner xfs_icsb_unlock_cntr(icsbp); 21488d280b98SDavid Chinner put_cpu(); 21498d280b98SDavid Chinner return 0; 21508d280b98SDavid Chinner 21518d280b98SDavid Chinner slow_path: 2152*20b64285SDavid Chinner put_cpu(); 2153*20b64285SDavid Chinner 2154*20b64285SDavid Chinner /* 2155*20b64285SDavid Chinner * serialise with a mutex so we don't burn lots of cpu on 2156*20b64285SDavid Chinner * the superblock lock. We still need to hold the superblock 2157*20b64285SDavid Chinner * lock, however, when we modify the global structures. 2158*20b64285SDavid Chinner */ 2159*20b64285SDavid Chinner mutex_lock(&mp->m_icsb_mutex); 2160*20b64285SDavid Chinner 2161*20b64285SDavid Chinner /* 2162*20b64285SDavid Chinner * Now running atomically. 2163*20b64285SDavid Chinner * 2164*20b64285SDavid Chinner * If the counter is enabled, someone has beaten us to rebalancing. 2165*20b64285SDavid Chinner * Drop the lock and try again in the fast path.... 2166*20b64285SDavid Chinner */ 2167*20b64285SDavid Chinner if (!(xfs_icsb_counter_disabled(mp, field))) { 2168*20b64285SDavid Chinner mutex_unlock(&mp->m_icsb_mutex); 2169*20b64285SDavid Chinner goto again; 2170*20b64285SDavid Chinner } 2171*20b64285SDavid Chinner 2172*20b64285SDavid Chinner /* 2173*20b64285SDavid Chinner * The counter is currently disabled. Because we are 2174*20b64285SDavid Chinner * running atomically here, we know a rebalance cannot 2175*20b64285SDavid Chinner * be in progress. Hence we can go straight to operating 2176*20b64285SDavid Chinner * on the global superblock. We do not call xfs_mod_incore_sb() 2177*20b64285SDavid Chinner * here even though we need to get the SB_LOCK. Doing so 2178*20b64285SDavid Chinner * will cause us to re-enter this function and deadlock. 2179*20b64285SDavid Chinner * Hence we get the SB_LOCK ourselves and then call 2180*20b64285SDavid Chinner * xfs_mod_incore_sb_unlocked() as the unlocked path operates 2181*20b64285SDavid Chinner * directly on the global counters. 2182*20b64285SDavid Chinner */ 2183*20b64285SDavid Chinner s = XFS_SB_LOCK(mp); 2184*20b64285SDavid Chinner ret = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd); 2185*20b64285SDavid Chinner XFS_SB_UNLOCK(mp, s); 2186*20b64285SDavid Chinner 2187*20b64285SDavid Chinner /* 2188*20b64285SDavid Chinner * Now that we've modified the global superblock, we 2189*20b64285SDavid Chinner * may be able to re-enable the distributed counters 2190*20b64285SDavid Chinner * (e.g. lots of space just got freed). After that 2191*20b64285SDavid Chinner * we are done. 2192*20b64285SDavid Chinner */ 2193*20b64285SDavid Chinner if (ret != ENOSPC) 2194*20b64285SDavid Chinner xfs_icsb_balance_counter(mp, field, 0, 0); 2195*20b64285SDavid Chinner mutex_unlock(&mp->m_icsb_mutex); 2196*20b64285SDavid Chinner return ret; 2197*20b64285SDavid Chinner 2198*20b64285SDavid Chinner balance_counter: 219901e1b69cSDavid Chinner xfs_icsb_unlock_cntr(icsbp); 22008d280b98SDavid Chinner put_cpu(); 22018d280b98SDavid Chinner 2202*20b64285SDavid Chinner /* 2203*20b64285SDavid Chinner * We may have multiple threads here if multiple per-cpu 2204*20b64285SDavid Chinner * counters run dry at the same time. This will mean we can 2205*20b64285SDavid Chinner * do more balances than strictly necessary but it is not 2206*20b64285SDavid Chinner * the common slowpath case. 2207*20b64285SDavid Chinner */ 2208*20b64285SDavid Chinner mutex_lock(&mp->m_icsb_mutex); 2209*20b64285SDavid Chinner 2210*20b64285SDavid Chinner /* 2211*20b64285SDavid Chinner * running atomically. 2212*20b64285SDavid Chinner * 2213*20b64285SDavid Chinner * This will leave the counter in the correct state for future 2214*20b64285SDavid Chinner * accesses. After the rebalance, we simply try again and our retry 2215*20b64285SDavid Chinner * will either succeed through the fast path or slow path without 2216*20b64285SDavid Chinner * another balance operation being required. 2217*20b64285SDavid Chinner */ 2218*20b64285SDavid Chinner xfs_icsb_balance_counter(mp, field, 0, delta); 2219*20b64285SDavid Chinner mutex_unlock(&mp->m_icsb_mutex); 22208d280b98SDavid Chinner goto again; 22218d280b98SDavid Chinner } 22228d280b98SDavid Chinner 22238d280b98SDavid Chinner #endif 2224