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" 2070a9883cSDave Chinner #include "xfs_shared.h" 21239880efSDave Chinner #include "xfs_format.h" 22239880efSDave Chinner #include "xfs_log_format.h" 23239880efSDave Chinner #include "xfs_trans_resv.h" 24a844f451SNathan Scott #include "xfs_bit.h" 251da177e4SLinus Torvalds #include "xfs_sb.h" 261da177e4SLinus Torvalds #include "xfs_mount.h" 273ab78df2SDarrick J. Wong #include "xfs_defer.h" 2857062787SDave Chinner #include "xfs_da_format.h" 299a2cc41cSDave Chinner #include "xfs_da_btree.h" 301da177e4SLinus Torvalds #include "xfs_inode.h" 31a4fbe6abSDave Chinner #include "xfs_dir2.h" 32a844f451SNathan Scott #include "xfs_ialloc.h" 331da177e4SLinus Torvalds #include "xfs_alloc.h" 341da177e4SLinus Torvalds #include "xfs_rtalloc.h" 351da177e4SLinus Torvalds #include "xfs_bmap.h" 36a4fbe6abSDave Chinner #include "xfs_trans.h" 37a4fbe6abSDave Chinner #include "xfs_trans_priv.h" 38a4fbe6abSDave Chinner #include "xfs_log.h" 391da177e4SLinus Torvalds #include "xfs_error.h" 401da177e4SLinus Torvalds #include "xfs_quota.h" 411da177e4SLinus Torvalds #include "xfs_fsops.h" 420b1b213fSChristoph Hellwig #include "xfs_trace.h" 436d8b79cfSDave Chinner #include "xfs_icache.h" 44a31b1d3dSBrian Foster #include "xfs_sysfs.h" 45*035e00acSDarrick J. Wong #include "xfs_rmap_btree.h" 460b1b213fSChristoph Hellwig 471da177e4SLinus Torvalds 4827174203SChristoph Hellwig static DEFINE_MUTEX(xfs_uuid_table_mutex); 4927174203SChristoph Hellwig static int xfs_uuid_table_size; 5027174203SChristoph Hellwig static uuid_t *xfs_uuid_table; 5127174203SChristoph Hellwig 52af3b6382SDarrick J. Wong void 53af3b6382SDarrick J. Wong xfs_uuid_table_free(void) 54af3b6382SDarrick J. Wong { 55af3b6382SDarrick J. Wong if (xfs_uuid_table_size == 0) 56af3b6382SDarrick J. Wong return; 57af3b6382SDarrick J. Wong kmem_free(xfs_uuid_table); 58af3b6382SDarrick J. Wong xfs_uuid_table = NULL; 59af3b6382SDarrick J. Wong xfs_uuid_table_size = 0; 60af3b6382SDarrick J. Wong } 61af3b6382SDarrick J. Wong 6227174203SChristoph Hellwig /* 6327174203SChristoph Hellwig * See if the UUID is unique among mounted XFS filesystems. 6427174203SChristoph Hellwig * Mount fails if UUID is nil or a FS with the same UUID is already mounted. 6527174203SChristoph Hellwig */ 6627174203SChristoph Hellwig STATIC int 6727174203SChristoph Hellwig xfs_uuid_mount( 6827174203SChristoph Hellwig struct xfs_mount *mp) 6927174203SChristoph Hellwig { 7027174203SChristoph Hellwig uuid_t *uuid = &mp->m_sb.sb_uuid; 7127174203SChristoph Hellwig int hole, i; 7227174203SChristoph Hellwig 7327174203SChristoph Hellwig if (mp->m_flags & XFS_MOUNT_NOUUID) 7427174203SChristoph Hellwig return 0; 7527174203SChristoph Hellwig 7627174203SChristoph Hellwig if (uuid_is_nil(uuid)) { 770b932cccSDave Chinner xfs_warn(mp, "Filesystem has nil UUID - can't mount"); 782451337dSDave Chinner return -EINVAL; 7927174203SChristoph Hellwig } 8027174203SChristoph Hellwig 8127174203SChristoph Hellwig mutex_lock(&xfs_uuid_table_mutex); 8227174203SChristoph Hellwig for (i = 0, hole = -1; i < xfs_uuid_table_size; i++) { 8327174203SChristoph Hellwig if (uuid_is_nil(&xfs_uuid_table[i])) { 8427174203SChristoph Hellwig hole = i; 8527174203SChristoph Hellwig continue; 8627174203SChristoph Hellwig } 8727174203SChristoph Hellwig if (uuid_equal(uuid, &xfs_uuid_table[i])) 8827174203SChristoph Hellwig goto out_duplicate; 8927174203SChristoph Hellwig } 9027174203SChristoph Hellwig 9127174203SChristoph Hellwig if (hole < 0) { 9227174203SChristoph Hellwig xfs_uuid_table = kmem_realloc(xfs_uuid_table, 9327174203SChristoph Hellwig (xfs_uuid_table_size + 1) * sizeof(*xfs_uuid_table), 9427174203SChristoph Hellwig KM_SLEEP); 9527174203SChristoph Hellwig hole = xfs_uuid_table_size++; 9627174203SChristoph Hellwig } 9727174203SChristoph Hellwig xfs_uuid_table[hole] = *uuid; 9827174203SChristoph Hellwig mutex_unlock(&xfs_uuid_table_mutex); 9927174203SChristoph Hellwig 10027174203SChristoph Hellwig return 0; 10127174203SChristoph Hellwig 10227174203SChristoph Hellwig out_duplicate: 10327174203SChristoph Hellwig mutex_unlock(&xfs_uuid_table_mutex); 104021000e5SMitsuo Hayasaka xfs_warn(mp, "Filesystem has duplicate UUID %pU - can't mount", uuid); 1052451337dSDave Chinner return -EINVAL; 10627174203SChristoph Hellwig } 10727174203SChristoph Hellwig 10827174203SChristoph Hellwig STATIC void 10927174203SChristoph Hellwig xfs_uuid_unmount( 11027174203SChristoph Hellwig struct xfs_mount *mp) 11127174203SChristoph Hellwig { 11227174203SChristoph Hellwig uuid_t *uuid = &mp->m_sb.sb_uuid; 11327174203SChristoph Hellwig int i; 11427174203SChristoph Hellwig 11527174203SChristoph Hellwig if (mp->m_flags & XFS_MOUNT_NOUUID) 11627174203SChristoph Hellwig return; 11727174203SChristoph Hellwig 11827174203SChristoph Hellwig mutex_lock(&xfs_uuid_table_mutex); 11927174203SChristoph Hellwig for (i = 0; i < xfs_uuid_table_size; i++) { 12027174203SChristoph Hellwig if (uuid_is_nil(&xfs_uuid_table[i])) 12127174203SChristoph Hellwig continue; 12227174203SChristoph Hellwig if (!uuid_equal(uuid, &xfs_uuid_table[i])) 12327174203SChristoph Hellwig continue; 12427174203SChristoph Hellwig memset(&xfs_uuid_table[i], 0, sizeof(uuid_t)); 12527174203SChristoph Hellwig break; 12627174203SChristoph Hellwig } 12727174203SChristoph Hellwig ASSERT(i < xfs_uuid_table_size); 12827174203SChristoph Hellwig mutex_unlock(&xfs_uuid_table_mutex); 12927174203SChristoph Hellwig } 13027174203SChristoph Hellwig 13127174203SChristoph Hellwig 132e176579eSDave Chinner STATIC void 133e176579eSDave Chinner __xfs_free_perag( 134e176579eSDave Chinner struct rcu_head *head) 135e176579eSDave Chinner { 136e176579eSDave Chinner struct xfs_perag *pag = container_of(head, struct xfs_perag, rcu_head); 137e176579eSDave Chinner 138e176579eSDave Chinner ASSERT(atomic_read(&pag->pag_ref) == 0); 139e176579eSDave Chinner kmem_free(pag); 140e176579eSDave Chinner } 141e176579eSDave Chinner 1420fa800fbSDave Chinner /* 143e176579eSDave Chinner * Free up the per-ag resources associated with the mount structure. 1441da177e4SLinus Torvalds */ 145c962fb79SChristoph Hellwig STATIC void 146ff4f038cSChristoph Hellwig xfs_free_perag( 147745f6919SChristoph Hellwig xfs_mount_t *mp) 1481da177e4SLinus Torvalds { 1491c1c6ebcSDave Chinner xfs_agnumber_t agno; 1501c1c6ebcSDave Chinner struct xfs_perag *pag; 1511da177e4SLinus Torvalds 1521c1c6ebcSDave Chinner for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) { 1531c1c6ebcSDave Chinner spin_lock(&mp->m_perag_lock); 1541c1c6ebcSDave Chinner pag = radix_tree_delete(&mp->m_perag_tree, agno); 1551c1c6ebcSDave Chinner spin_unlock(&mp->m_perag_lock); 156e176579eSDave Chinner ASSERT(pag); 157f83282a8SDave Chinner ASSERT(atomic_read(&pag->pag_ref) == 0); 158e176579eSDave Chinner call_rcu(&pag->rcu_head, __xfs_free_perag); 1591da177e4SLinus Torvalds } 1601da177e4SLinus Torvalds } 1611da177e4SLinus Torvalds 1624cc929eeSNathan Scott /* 1634cc929eeSNathan Scott * Check size of device based on the (data/realtime) block count. 1644cc929eeSNathan Scott * Note: this check is used by the growfs code as well as mount. 1654cc929eeSNathan Scott */ 1664cc929eeSNathan Scott int 1674cc929eeSNathan Scott xfs_sb_validate_fsb_count( 1684cc929eeSNathan Scott xfs_sb_t *sbp, 1694cc929eeSNathan Scott __uint64_t nblocks) 1704cc929eeSNathan Scott { 1714cc929eeSNathan Scott ASSERT(PAGE_SHIFT >= sbp->sb_blocklog); 1724cc929eeSNathan Scott ASSERT(sbp->sb_blocklog >= BBSHIFT); 1734cc929eeSNathan Scott 174d5cf09baSChristoph Hellwig /* Limited by ULONG_MAX of page cache index */ 17509cbfeafSKirill A. Shutemov if (nblocks >> (PAGE_SHIFT - sbp->sb_blocklog) > ULONG_MAX) 1762451337dSDave Chinner return -EFBIG; 1774cc929eeSNathan Scott return 0; 1784cc929eeSNathan Scott } 1791da177e4SLinus Torvalds 1801c1c6ebcSDave Chinner int 181c11e2c36SNathan Scott xfs_initialize_perag( 182c11e2c36SNathan Scott xfs_mount_t *mp, 1831c1c6ebcSDave Chinner xfs_agnumber_t agcount, 1841c1c6ebcSDave Chinner xfs_agnumber_t *maxagi) 1851da177e4SLinus Torvalds { 1862d2194f6SCarlos Maiolino xfs_agnumber_t index; 1878b26c582SDave Chinner xfs_agnumber_t first_initialised = 0; 1881da177e4SLinus Torvalds xfs_perag_t *pag; 1898b26c582SDave Chinner int error = -ENOMEM; 1901da177e4SLinus Torvalds 1911c1c6ebcSDave Chinner /* 1921c1c6ebcSDave Chinner * Walk the current per-ag tree so we don't try to initialise AGs 1931c1c6ebcSDave Chinner * that already exist (growfs case). Allocate and insert all the 1941c1c6ebcSDave Chinner * AGs we don't find ready for initialisation. 1951c1c6ebcSDave Chinner */ 1961c1c6ebcSDave Chinner for (index = 0; index < agcount; index++) { 1971c1c6ebcSDave Chinner pag = xfs_perag_get(mp, index); 1981c1c6ebcSDave Chinner if (pag) { 1991c1c6ebcSDave Chinner xfs_perag_put(pag); 2001c1c6ebcSDave Chinner continue; 2011c1c6ebcSDave Chinner } 2028b26c582SDave Chinner if (!first_initialised) 2038b26c582SDave Chinner first_initialised = index; 204fb3b504aSChristoph Hellwig 2051c1c6ebcSDave Chinner pag = kmem_zalloc(sizeof(*pag), KM_MAYFAIL); 2061c1c6ebcSDave Chinner if (!pag) 2078b26c582SDave Chinner goto out_unwind; 208fb3b504aSChristoph Hellwig pag->pag_agno = index; 209fb3b504aSChristoph Hellwig pag->pag_mount = mp; 2101a427ab0SDave Chinner spin_lock_init(&pag->pag_ici_lock); 21169b491c2SDave Chinner mutex_init(&pag->pag_ici_reclaim_lock); 212fb3b504aSChristoph Hellwig INIT_RADIX_TREE(&pag->pag_ici_root, GFP_ATOMIC); 21374f75a0cSDave Chinner spin_lock_init(&pag->pag_buf_lock); 21474f75a0cSDave Chinner pag->pag_buf_tree = RB_ROOT; 215fb3b504aSChristoph Hellwig 2161c1c6ebcSDave Chinner if (radix_tree_preload(GFP_NOFS)) 2178b26c582SDave Chinner goto out_unwind; 218fb3b504aSChristoph Hellwig 2191c1c6ebcSDave Chinner spin_lock(&mp->m_perag_lock); 2201c1c6ebcSDave Chinner if (radix_tree_insert(&mp->m_perag_tree, index, pag)) { 2211c1c6ebcSDave Chinner BUG(); 2221c1c6ebcSDave Chinner spin_unlock(&mp->m_perag_lock); 2238b26c582SDave Chinner radix_tree_preload_end(); 2248b26c582SDave Chinner error = -EEXIST; 2258b26c582SDave Chinner goto out_unwind; 2261c1c6ebcSDave Chinner } 2271c1c6ebcSDave Chinner spin_unlock(&mp->m_perag_lock); 2281c1c6ebcSDave Chinner radix_tree_preload_end(); 2291c1c6ebcSDave Chinner } 2301c1c6ebcSDave Chinner 23112c3f05cSEric Sandeen index = xfs_set_inode_alloc(mp, agcount); 232fb3b504aSChristoph Hellwig 2331c1c6ebcSDave Chinner if (maxagi) 2341c1c6ebcSDave Chinner *maxagi = index; 2358018026eSDarrick J. Wong 2368018026eSDarrick J. Wong mp->m_ag_prealloc_blocks = xfs_prealloc_blocks(mp); 2371c1c6ebcSDave Chinner return 0; 2388b26c582SDave Chinner 2398b26c582SDave Chinner out_unwind: 2408b26c582SDave Chinner kmem_free(pag); 2418b26c582SDave Chinner for (; index > first_initialised; index--) { 2428b26c582SDave Chinner pag = radix_tree_delete(&mp->m_perag_tree, index); 2438b26c582SDave Chinner kmem_free(pag); 2448b26c582SDave Chinner } 2458b26c582SDave Chinner return error; 2461da177e4SLinus Torvalds } 2471da177e4SLinus Torvalds 2481da177e4SLinus Torvalds /* 2491da177e4SLinus Torvalds * xfs_readsb 2501da177e4SLinus Torvalds * 2511da177e4SLinus Torvalds * Does the initial read of the superblock. 2521da177e4SLinus Torvalds */ 2531da177e4SLinus Torvalds int 254ff55068cSDave Chinner xfs_readsb( 255ff55068cSDave Chinner struct xfs_mount *mp, 256ff55068cSDave Chinner int flags) 2571da177e4SLinus Torvalds { 2581da177e4SLinus Torvalds unsigned int sector_size; 25904a1e6c5SDave Chinner struct xfs_buf *bp; 26004a1e6c5SDave Chinner struct xfs_sb *sbp = &mp->m_sb; 2611da177e4SLinus Torvalds int error; 262af34e09dSDave Chinner int loud = !(flags & XFS_MFSI_QUIET); 263daba5427SEric Sandeen const struct xfs_buf_ops *buf_ops; 2641da177e4SLinus Torvalds 2651da177e4SLinus Torvalds ASSERT(mp->m_sb_bp == NULL); 2661da177e4SLinus Torvalds ASSERT(mp->m_ddev_targp != NULL); 2671da177e4SLinus Torvalds 2681da177e4SLinus Torvalds /* 269daba5427SEric Sandeen * For the initial read, we must guess at the sector 270daba5427SEric Sandeen * size based on the block device. It's enough to 271daba5427SEric Sandeen * get the sb_sectsize out of the superblock and 272daba5427SEric Sandeen * then reread with the proper length. 273daba5427SEric Sandeen * We don't verify it yet, because it may not be complete. 274daba5427SEric Sandeen */ 275daba5427SEric Sandeen sector_size = xfs_getsize_buftarg(mp->m_ddev_targp); 276daba5427SEric Sandeen buf_ops = NULL; 277daba5427SEric Sandeen 278daba5427SEric Sandeen /* 279c891c30aSBrian Foster * Allocate a (locked) buffer to hold the superblock. This will be kept 280c891c30aSBrian Foster * around at all times to optimize access to the superblock. Therefore, 281c891c30aSBrian Foster * set XBF_NO_IOACCT to make sure it doesn't hold the buftarg count 282c891c30aSBrian Foster * elevated. 2831da177e4SLinus Torvalds */ 28426af6552SDave Chinner reread: 285ba372674SDave Chinner error = xfs_buf_read_uncached(mp->m_ddev_targp, XFS_SB_DADDR, 286c891c30aSBrian Foster BTOBB(sector_size), XBF_NO_IOACCT, &bp, 287c891c30aSBrian Foster buf_ops); 288ba372674SDave Chinner if (error) { 289eab4e633SDave Chinner if (loud) 290e721f504SDave Chinner xfs_warn(mp, "SB validate failed with error %d.", error); 291ac75a1f7SDave Chinner /* bad CRC means corrupted metadata */ 2922451337dSDave Chinner if (error == -EFSBADCRC) 2932451337dSDave Chinner error = -EFSCORRUPTED; 294ba372674SDave Chinner return error; 295eab4e633SDave Chinner } 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds /* 2981da177e4SLinus Torvalds * Initialize the mount structure from the superblock. 2991da177e4SLinus Torvalds */ 300556b8883SDave Chinner xfs_sb_from_disk(sbp, XFS_BUF_TO_SBP(bp)); 301556b8883SDave Chinner 302556b8883SDave Chinner /* 303556b8883SDave Chinner * If we haven't validated the superblock, do so now before we try 304556b8883SDave Chinner * to check the sector size and reread the superblock appropriately. 305556b8883SDave Chinner */ 306556b8883SDave Chinner if (sbp->sb_magicnum != XFS_SB_MAGIC) { 307556b8883SDave Chinner if (loud) 308556b8883SDave Chinner xfs_warn(mp, "Invalid superblock magic number"); 3092451337dSDave Chinner error = -EINVAL; 310556b8883SDave Chinner goto release_buf; 311556b8883SDave Chinner } 312ff55068cSDave Chinner 3131da177e4SLinus Torvalds /* 3141da177e4SLinus Torvalds * We must be able to do sector-sized and sector-aligned IO. 3151da177e4SLinus Torvalds */ 31604a1e6c5SDave Chinner if (sector_size > sbp->sb_sectsize) { 317af34e09dSDave Chinner if (loud) 318af34e09dSDave Chinner xfs_warn(mp, "device supports %u byte sectors (not %u)", 31904a1e6c5SDave Chinner sector_size, sbp->sb_sectsize); 3202451337dSDave Chinner error = -ENOSYS; 32126af6552SDave Chinner goto release_buf; 3221da177e4SLinus Torvalds } 3231da177e4SLinus Torvalds 324556b8883SDave Chinner if (buf_ops == NULL) { 3251da177e4SLinus Torvalds /* 326daba5427SEric Sandeen * Re-read the superblock so the buffer is correctly sized, 327daba5427SEric Sandeen * and properly verified. 3281da177e4SLinus Torvalds */ 3291da177e4SLinus Torvalds xfs_buf_relse(bp); 33004a1e6c5SDave Chinner sector_size = sbp->sb_sectsize; 331daba5427SEric Sandeen buf_ops = loud ? &xfs_sb_buf_ops : &xfs_sb_quiet_buf_ops; 33226af6552SDave Chinner goto reread; 3331da177e4SLinus Torvalds } 3341da177e4SLinus Torvalds 3355681ca40SDave Chinner xfs_reinit_percpu_counters(mp); 3368d280b98SDavid Chinner 33704a1e6c5SDave Chinner /* no need to be quiet anymore, so reset the buf ops */ 33804a1e6c5SDave Chinner bp->b_ops = &xfs_sb_buf_ops; 33904a1e6c5SDave Chinner 3401da177e4SLinus Torvalds mp->m_sb_bp = bp; 34126af6552SDave Chinner xfs_buf_unlock(bp); 3421da177e4SLinus Torvalds return 0; 3431da177e4SLinus Torvalds 34426af6552SDave Chinner release_buf: 3451da177e4SLinus Torvalds xfs_buf_relse(bp); 3461da177e4SLinus Torvalds return error; 3471da177e4SLinus Torvalds } 3481da177e4SLinus Torvalds 3491da177e4SLinus Torvalds /* 3500771fb45SEric Sandeen * Update alignment values based on mount options and sb values 3511da177e4SLinus Torvalds */ 3520771fb45SEric Sandeen STATIC int 3537884bc86SChristoph Hellwig xfs_update_alignment(xfs_mount_t *mp) 3541da177e4SLinus Torvalds { 3551da177e4SLinus Torvalds xfs_sb_t *sbp = &(mp->m_sb); 3561da177e4SLinus Torvalds 3574249023aSChristoph Hellwig if (mp->m_dalign) { 3581da177e4SLinus Torvalds /* 3591da177e4SLinus Torvalds * If stripe unit and stripe width are not multiples 3601da177e4SLinus Torvalds * of the fs blocksize turn off alignment. 3611da177e4SLinus Torvalds */ 3621da177e4SLinus Torvalds if ((BBTOB(mp->m_dalign) & mp->m_blockmask) || 3631da177e4SLinus Torvalds (BBTOB(mp->m_swidth) & mp->m_blockmask)) { 36439a45d84SJie Liu xfs_warn(mp, 36539a45d84SJie Liu "alignment check failed: sunit/swidth vs. blocksize(%d)", 36639a45d84SJie Liu sbp->sb_blocksize); 3672451337dSDave Chinner return -EINVAL; 3681da177e4SLinus Torvalds } else { 3691da177e4SLinus Torvalds /* 3701da177e4SLinus Torvalds * Convert the stripe unit and width to FSBs. 3711da177e4SLinus Torvalds */ 3721da177e4SLinus Torvalds mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign); 3731da177e4SLinus Torvalds if (mp->m_dalign && (sbp->sb_agblocks % mp->m_dalign)) { 37453487786SDave Chinner xfs_warn(mp, 37539a45d84SJie Liu "alignment check failed: sunit/swidth vs. agsize(%d)", 3761da177e4SLinus Torvalds sbp->sb_agblocks); 3772451337dSDave Chinner return -EINVAL; 3781da177e4SLinus Torvalds } else if (mp->m_dalign) { 3791da177e4SLinus Torvalds mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth); 3801da177e4SLinus Torvalds } else { 38139a45d84SJie Liu xfs_warn(mp, 38239a45d84SJie Liu "alignment check failed: sunit(%d) less than bsize(%d)", 38339a45d84SJie Liu mp->m_dalign, sbp->sb_blocksize); 3842451337dSDave Chinner return -EINVAL; 3851da177e4SLinus Torvalds } 3861da177e4SLinus Torvalds } 3871da177e4SLinus Torvalds 3881da177e4SLinus Torvalds /* 3891da177e4SLinus Torvalds * Update superblock with new values 3901da177e4SLinus Torvalds * and log changes 3911da177e4SLinus Torvalds */ 39262118709SEric Sandeen if (xfs_sb_version_hasdalign(sbp)) { 3931da177e4SLinus Torvalds if (sbp->sb_unit != mp->m_dalign) { 3941da177e4SLinus Torvalds sbp->sb_unit = mp->m_dalign; 39561e63ecbSDave Chinner mp->m_update_sb = true; 3961da177e4SLinus Torvalds } 3971da177e4SLinus Torvalds if (sbp->sb_width != mp->m_swidth) { 3981da177e4SLinus Torvalds sbp->sb_width = mp->m_swidth; 39961e63ecbSDave Chinner mp->m_update_sb = true; 4001da177e4SLinus Torvalds } 40134d7f603SJie Liu } else { 40234d7f603SJie Liu xfs_warn(mp, 40334d7f603SJie Liu "cannot change alignment: superblock does not support data alignment"); 4042451337dSDave Chinner return -EINVAL; 4051da177e4SLinus Torvalds } 4061da177e4SLinus Torvalds } else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN && 40762118709SEric Sandeen xfs_sb_version_hasdalign(&mp->m_sb)) { 4081da177e4SLinus Torvalds mp->m_dalign = sbp->sb_unit; 4091da177e4SLinus Torvalds mp->m_swidth = sbp->sb_width; 4101da177e4SLinus Torvalds } 4111da177e4SLinus Torvalds 4120771fb45SEric Sandeen return 0; 4130771fb45SEric Sandeen } 4141da177e4SLinus Torvalds 4150771fb45SEric Sandeen /* 4160771fb45SEric Sandeen * Set the maximum inode count for this filesystem 4170771fb45SEric Sandeen */ 4180771fb45SEric Sandeen STATIC void 4190771fb45SEric Sandeen xfs_set_maxicount(xfs_mount_t *mp) 4200771fb45SEric Sandeen { 4210771fb45SEric Sandeen xfs_sb_t *sbp = &(mp->m_sb); 4221da177e4SLinus Torvalds __uint64_t icount; 4231da177e4SLinus Torvalds 4240771fb45SEric Sandeen if (sbp->sb_imax_pct) { 4250771fb45SEric Sandeen /* 4260771fb45SEric Sandeen * Make sure the maximum inode count is a multiple 4270771fb45SEric Sandeen * of the units we allocate inodes in. 4281da177e4SLinus Torvalds */ 4291da177e4SLinus Torvalds icount = sbp->sb_dblocks * sbp->sb_imax_pct; 4301da177e4SLinus Torvalds do_div(icount, 100); 4311da177e4SLinus Torvalds do_div(icount, mp->m_ialloc_blks); 4321da177e4SLinus Torvalds mp->m_maxicount = (icount * mp->m_ialloc_blks) << 4331da177e4SLinus Torvalds sbp->sb_inopblog; 4340771fb45SEric Sandeen } else { 4351da177e4SLinus Torvalds mp->m_maxicount = 0; 4361da177e4SLinus Torvalds } 4371da177e4SLinus Torvalds } 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds /* 4401da177e4SLinus Torvalds * Set the default minimum read and write sizes unless 4411da177e4SLinus Torvalds * already specified in a mount option. 4421da177e4SLinus Torvalds * We use smaller I/O sizes when the file system 4431da177e4SLinus Torvalds * is being used for NFS service (wsync mount option). 4441da177e4SLinus Torvalds */ 4450771fb45SEric Sandeen STATIC void 4460771fb45SEric Sandeen xfs_set_rw_sizes(xfs_mount_t *mp) 4470771fb45SEric Sandeen { 4480771fb45SEric Sandeen xfs_sb_t *sbp = &(mp->m_sb); 4490771fb45SEric Sandeen int readio_log, writeio_log; 4500771fb45SEric Sandeen 4511da177e4SLinus Torvalds if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)) { 4521da177e4SLinus Torvalds if (mp->m_flags & XFS_MOUNT_WSYNC) { 4531da177e4SLinus Torvalds readio_log = XFS_WSYNC_READIO_LOG; 4541da177e4SLinus Torvalds writeio_log = XFS_WSYNC_WRITEIO_LOG; 4551da177e4SLinus Torvalds } else { 4561da177e4SLinus Torvalds readio_log = XFS_READIO_LOG_LARGE; 4571da177e4SLinus Torvalds writeio_log = XFS_WRITEIO_LOG_LARGE; 4581da177e4SLinus Torvalds } 4591da177e4SLinus Torvalds } else { 4601da177e4SLinus Torvalds readio_log = mp->m_readio_log; 4611da177e4SLinus Torvalds writeio_log = mp->m_writeio_log; 4621da177e4SLinus Torvalds } 4631da177e4SLinus Torvalds 4641da177e4SLinus Torvalds if (sbp->sb_blocklog > readio_log) { 4651da177e4SLinus Torvalds mp->m_readio_log = sbp->sb_blocklog; 4661da177e4SLinus Torvalds } else { 4671da177e4SLinus Torvalds mp->m_readio_log = readio_log; 4681da177e4SLinus Torvalds } 4691da177e4SLinus Torvalds mp->m_readio_blocks = 1 << (mp->m_readio_log - sbp->sb_blocklog); 4701da177e4SLinus Torvalds if (sbp->sb_blocklog > writeio_log) { 4711da177e4SLinus Torvalds mp->m_writeio_log = sbp->sb_blocklog; 4721da177e4SLinus Torvalds } else { 4731da177e4SLinus Torvalds mp->m_writeio_log = writeio_log; 4741da177e4SLinus Torvalds } 4751da177e4SLinus Torvalds mp->m_writeio_blocks = 1 << (mp->m_writeio_log - sbp->sb_blocklog); 4760771fb45SEric Sandeen } 477425f9dddSEric Sandeen 4781da177e4SLinus Torvalds /* 479055388a3SDave Chinner * precalculate the low space thresholds for dynamic speculative preallocation. 480055388a3SDave Chinner */ 481055388a3SDave Chinner void 482055388a3SDave Chinner xfs_set_low_space_thresholds( 483055388a3SDave Chinner struct xfs_mount *mp) 484055388a3SDave Chinner { 485055388a3SDave Chinner int i; 486055388a3SDave Chinner 487055388a3SDave Chinner for (i = 0; i < XFS_LOWSP_MAX; i++) { 488055388a3SDave Chinner __uint64_t space = mp->m_sb.sb_dblocks; 489055388a3SDave Chinner 490055388a3SDave Chinner do_div(space, 100); 491055388a3SDave Chinner mp->m_low_space[i] = space * (i + 1); 492055388a3SDave Chinner } 493055388a3SDave Chinner } 494055388a3SDave Chinner 495055388a3SDave Chinner 496055388a3SDave Chinner /* 4971da177e4SLinus Torvalds * Set whether we're using inode alignment. 4981da177e4SLinus Torvalds */ 4990771fb45SEric Sandeen STATIC void 5000771fb45SEric Sandeen xfs_set_inoalignment(xfs_mount_t *mp) 5010771fb45SEric Sandeen { 50262118709SEric Sandeen if (xfs_sb_version_hasalign(&mp->m_sb) && 5031da177e4SLinus Torvalds mp->m_sb.sb_inoalignmt >= 5041da177e4SLinus Torvalds XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size)) 5051da177e4SLinus Torvalds mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1; 5061da177e4SLinus Torvalds else 5071da177e4SLinus Torvalds mp->m_inoalign_mask = 0; 5081da177e4SLinus Torvalds /* 5091da177e4SLinus Torvalds * If we are using stripe alignment, check whether 5101da177e4SLinus Torvalds * the stripe unit is a multiple of the inode alignment 5111da177e4SLinus Torvalds */ 5121da177e4SLinus Torvalds if (mp->m_dalign && mp->m_inoalign_mask && 5131da177e4SLinus Torvalds !(mp->m_dalign & mp->m_inoalign_mask)) 5141da177e4SLinus Torvalds mp->m_sinoalign = mp->m_dalign; 5151da177e4SLinus Torvalds else 5161da177e4SLinus Torvalds mp->m_sinoalign = 0; 5170771fb45SEric Sandeen } 5180771fb45SEric Sandeen 5191da177e4SLinus Torvalds /* 5200471f62eSZhi Yong Wu * Check that the data (and log if separate) is an ok size. 5211da177e4SLinus Torvalds */ 5220771fb45SEric Sandeen STATIC int 523ba372674SDave Chinner xfs_check_sizes( 524ba372674SDave Chinner struct xfs_mount *mp) 5250771fb45SEric Sandeen { 526ba372674SDave Chinner struct xfs_buf *bp; 5270771fb45SEric Sandeen xfs_daddr_t d; 528ba372674SDave Chinner int error; 5290771fb45SEric Sandeen 5301da177e4SLinus Torvalds d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks); 5311da177e4SLinus Torvalds if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) { 5320b932cccSDave Chinner xfs_warn(mp, "filesystem size mismatch detected"); 5332451337dSDave Chinner return -EFBIG; 5341da177e4SLinus Torvalds } 535ba372674SDave Chinner error = xfs_buf_read_uncached(mp->m_ddev_targp, 5361da177e4SLinus Torvalds d - XFS_FSS_TO_BB(mp, 1), 537ba372674SDave Chinner XFS_FSS_TO_BB(mp, 1), 0, &bp, NULL); 538ba372674SDave Chinner if (error) { 5390b932cccSDave Chinner xfs_warn(mp, "last sector read failed"); 540ba372674SDave Chinner return error; 5411da177e4SLinus Torvalds } 5421922c949SDave Chinner xfs_buf_relse(bp); 5431da177e4SLinus Torvalds 544ba372674SDave Chinner if (mp->m_logdev_targp == mp->m_ddev_targp) 545ba372674SDave Chinner return 0; 546ba372674SDave Chinner 5471da177e4SLinus Torvalds d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks); 5481da177e4SLinus Torvalds if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) { 5490b932cccSDave Chinner xfs_warn(mp, "log size mismatch detected"); 5502451337dSDave Chinner return -EFBIG; 5511da177e4SLinus Torvalds } 552ba372674SDave Chinner error = xfs_buf_read_uncached(mp->m_logdev_targp, 5531da177e4SLinus Torvalds d - XFS_FSB_TO_BB(mp, 1), 554ba372674SDave Chinner XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL); 555ba372674SDave Chinner if (error) { 5560b932cccSDave Chinner xfs_warn(mp, "log device read failed"); 557ba372674SDave Chinner return error; 5581da177e4SLinus Torvalds } 5591922c949SDave Chinner xfs_buf_relse(bp); 5600771fb45SEric Sandeen return 0; 5610771fb45SEric Sandeen } 5620771fb45SEric Sandeen 5630771fb45SEric Sandeen /* 5647d095257SChristoph Hellwig * Clear the quotaflags in memory and in the superblock. 5657d095257SChristoph Hellwig */ 5667d095257SChristoph Hellwig int 5677d095257SChristoph Hellwig xfs_mount_reset_sbqflags( 5687d095257SChristoph Hellwig struct xfs_mount *mp) 5697d095257SChristoph Hellwig { 5707d095257SChristoph Hellwig mp->m_qflags = 0; 5717d095257SChristoph Hellwig 57261e63ecbSDave Chinner /* It is OK to look at sb_qflags in the mount path without m_sb_lock. */ 5737d095257SChristoph Hellwig if (mp->m_sb.sb_qflags == 0) 5747d095257SChristoph Hellwig return 0; 5757d095257SChristoph Hellwig spin_lock(&mp->m_sb_lock); 5767d095257SChristoph Hellwig mp->m_sb.sb_qflags = 0; 5777d095257SChristoph Hellwig spin_unlock(&mp->m_sb_lock); 5787d095257SChristoph Hellwig 57961e63ecbSDave Chinner if (!xfs_fs_writable(mp, SB_FREEZE_WRITE)) 5807d095257SChristoph Hellwig return 0; 5817d095257SChristoph Hellwig 58261e63ecbSDave Chinner return xfs_sync_sb(mp, false); 5837d095257SChristoph Hellwig } 5847d095257SChristoph Hellwig 585d5db0f97SEric Sandeen __uint64_t 586d5db0f97SEric Sandeen xfs_default_resblks(xfs_mount_t *mp) 587d5db0f97SEric Sandeen { 588d5db0f97SEric Sandeen __uint64_t resblks; 589d5db0f97SEric Sandeen 590d5db0f97SEric Sandeen /* 5918babd8a2SDave Chinner * We default to 5% or 8192 fsbs of space reserved, whichever is 5928babd8a2SDave Chinner * smaller. This is intended to cover concurrent allocation 5938babd8a2SDave Chinner * transactions when we initially hit enospc. These each require a 4 5948babd8a2SDave Chinner * block reservation. Hence by default we cover roughly 2000 concurrent 5958babd8a2SDave Chinner * allocation reservations. 596d5db0f97SEric Sandeen */ 597d5db0f97SEric Sandeen resblks = mp->m_sb.sb_dblocks; 598d5db0f97SEric Sandeen do_div(resblks, 20); 5998babd8a2SDave Chinner resblks = min_t(__uint64_t, resblks, 8192); 600d5db0f97SEric Sandeen return resblks; 601d5db0f97SEric Sandeen } 602d5db0f97SEric Sandeen 6037d095257SChristoph Hellwig /* 6040771fb45SEric Sandeen * This function does the following on an initial mount of a file system: 6050771fb45SEric Sandeen * - reads the superblock from disk and init the mount struct 6060771fb45SEric Sandeen * - if we're a 32-bit kernel, do a size check on the superblock 6070771fb45SEric Sandeen * so we don't mount terabyte filesystems 6080771fb45SEric Sandeen * - init mount struct realtime fields 6090771fb45SEric Sandeen * - allocate inode hash table for fs 6100771fb45SEric Sandeen * - init directory manager 6110771fb45SEric Sandeen * - perform recovery and init the log manager 6120771fb45SEric Sandeen */ 6130771fb45SEric Sandeen int 6140771fb45SEric Sandeen xfs_mountfs( 615f0b2efadSBrian Foster struct xfs_mount *mp) 6160771fb45SEric Sandeen { 617f0b2efadSBrian Foster struct xfs_sb *sbp = &(mp->m_sb); 618f0b2efadSBrian Foster struct xfs_inode *rip; 6190771fb45SEric Sandeen __uint64_t resblks; 6207d095257SChristoph Hellwig uint quotamount = 0; 6217d095257SChristoph Hellwig uint quotaflags = 0; 6220771fb45SEric Sandeen int error = 0; 6230771fb45SEric Sandeen 624ff55068cSDave Chinner xfs_sb_mount_common(mp, sbp); 6250771fb45SEric Sandeen 6260771fb45SEric Sandeen /* 627074e427bSDave Chinner * Check for a mismatched features2 values. Older kernels read & wrote 628074e427bSDave Chinner * into the wrong sb offset for sb_features2 on some platforms due to 629074e427bSDave Chinner * xfs_sb_t not being 64bit size aligned when sb_features2 was added, 630074e427bSDave Chinner * which made older superblock reading/writing routines swap it as a 631074e427bSDave Chinner * 64-bit value. 632ee1c0908SDavid Chinner * 633e6957ea4SEric Sandeen * For backwards compatibility, we make both slots equal. 634e6957ea4SEric Sandeen * 635074e427bSDave Chinner * If we detect a mismatched field, we OR the set bits into the existing 636074e427bSDave Chinner * features2 field in case it has already been modified; we don't want 637074e427bSDave Chinner * to lose any features. We then update the bad location with the ORed 638074e427bSDave Chinner * value so that older kernels will see any features2 flags. The 639074e427bSDave Chinner * superblock writeback code ensures the new sb_features2 is copied to 640074e427bSDave Chinner * sb_bad_features2 before it is logged or written to disk. 641ee1c0908SDavid Chinner */ 642e6957ea4SEric Sandeen if (xfs_sb_has_mismatched_features2(sbp)) { 6430b932cccSDave Chinner xfs_warn(mp, "correcting sb_features alignment problem"); 644ee1c0908SDavid Chinner sbp->sb_features2 |= sbp->sb_bad_features2; 64561e63ecbSDave Chinner mp->m_update_sb = true; 646e6957ea4SEric Sandeen 647e6957ea4SEric Sandeen /* 648e6957ea4SEric Sandeen * Re-check for ATTR2 in case it was found in bad_features2 649e6957ea4SEric Sandeen * slot. 650e6957ea4SEric Sandeen */ 6517c12f296STim Shimmin if (xfs_sb_version_hasattr2(&mp->m_sb) && 6527c12f296STim Shimmin !(mp->m_flags & XFS_MOUNT_NOATTR2)) 653e6957ea4SEric Sandeen mp->m_flags |= XFS_MOUNT_ATTR2; 6547c12f296STim Shimmin } 655e6957ea4SEric Sandeen 6567c12f296STim Shimmin if (xfs_sb_version_hasattr2(&mp->m_sb) && 6577c12f296STim Shimmin (mp->m_flags & XFS_MOUNT_NOATTR2)) { 6587c12f296STim Shimmin xfs_sb_version_removeattr2(&mp->m_sb); 65961e63ecbSDave Chinner mp->m_update_sb = true; 6607c12f296STim Shimmin 6617c12f296STim Shimmin /* update sb_versionnum for the clearing of the morebits */ 6627c12f296STim Shimmin if (!sbp->sb_features2) 66361e63ecbSDave Chinner mp->m_update_sb = true; 664ee1c0908SDavid Chinner } 665ee1c0908SDavid Chinner 666263997a6SDave Chinner /* always use v2 inodes by default now */ 667263997a6SDave Chinner if (!(mp->m_sb.sb_versionnum & XFS_SB_VERSION_NLINKBIT)) { 668263997a6SDave Chinner mp->m_sb.sb_versionnum |= XFS_SB_VERSION_NLINKBIT; 66961e63ecbSDave Chinner mp->m_update_sb = true; 670263997a6SDave Chinner } 671263997a6SDave Chinner 672ee1c0908SDavid Chinner /* 6730771fb45SEric Sandeen * Check if sb_agblocks is aligned at stripe boundary 6740771fb45SEric Sandeen * If sb_agblocks is NOT aligned turn off m_dalign since 6750771fb45SEric Sandeen * allocator alignment is within an ag, therefore ag has 6760771fb45SEric Sandeen * to be aligned at stripe boundary. 6770771fb45SEric Sandeen */ 6787884bc86SChristoph Hellwig error = xfs_update_alignment(mp); 6790771fb45SEric Sandeen if (error) 680f9057e3dSChristoph Hellwig goto out; 6810771fb45SEric Sandeen 6820771fb45SEric Sandeen xfs_alloc_compute_maxlevels(mp); 6830771fb45SEric Sandeen xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK); 6840771fb45SEric Sandeen xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK); 6850771fb45SEric Sandeen xfs_ialloc_compute_maxlevels(mp); 686*035e00acSDarrick J. Wong xfs_rmapbt_compute_maxlevels(mp); 6870771fb45SEric Sandeen 6880771fb45SEric Sandeen xfs_set_maxicount(mp); 6890771fb45SEric Sandeen 690e6b3bb78SCarlos Maiolino /* enable fail_at_unmount as default */ 691e6b3bb78SCarlos Maiolino mp->m_fail_unmount = 1; 692e6b3bb78SCarlos Maiolino 693a31b1d3dSBrian Foster error = xfs_sysfs_init(&mp->m_kobj, &xfs_mp_ktype, NULL, mp->m_fsname); 69427174203SChristoph Hellwig if (error) 695f9057e3dSChristoph Hellwig goto out; 6961da177e4SLinus Torvalds 697225e4635SBill O'Donnell error = xfs_sysfs_init(&mp->m_stats.xs_kobj, &xfs_stats_ktype, 698225e4635SBill O'Donnell &mp->m_kobj, "stats"); 699a31b1d3dSBrian Foster if (error) 700a31b1d3dSBrian Foster goto out_remove_sysfs; 701a31b1d3dSBrian Foster 702192852beSCarlos Maiolino error = xfs_error_sysfs_init(mp); 703225e4635SBill O'Donnell if (error) 704225e4635SBill O'Donnell goto out_del_stats; 705225e4635SBill O'Donnell 706192852beSCarlos Maiolino 707192852beSCarlos Maiolino error = xfs_uuid_mount(mp); 708192852beSCarlos Maiolino if (error) 709192852beSCarlos Maiolino goto out_remove_error_sysfs; 710192852beSCarlos Maiolino 7111da177e4SLinus Torvalds /* 7120771fb45SEric Sandeen * Set the minimum read and write sizes 7130771fb45SEric Sandeen */ 7140771fb45SEric Sandeen xfs_set_rw_sizes(mp); 7150771fb45SEric Sandeen 716055388a3SDave Chinner /* set the low space thresholds for dynamic preallocation */ 717055388a3SDave Chinner xfs_set_low_space_thresholds(mp); 718055388a3SDave Chinner 7190771fb45SEric Sandeen /* 7200771fb45SEric Sandeen * Set the inode cluster size. 7210771fb45SEric Sandeen * This may still be overridden by the file system 7220771fb45SEric Sandeen * block size if it is larger than the chosen cluster size. 7238f80587bSDave Chinner * 7248f80587bSDave Chinner * For v5 filesystems, scale the cluster size with the inode size to 7258f80587bSDave Chinner * keep a constant ratio of inode per cluster buffer, but only if mkfs 7268f80587bSDave Chinner * has set the inode alignment value appropriately for larger cluster 7278f80587bSDave Chinner * sizes. 7280771fb45SEric Sandeen */ 7290771fb45SEric Sandeen mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE; 7308f80587bSDave Chinner if (xfs_sb_version_hascrc(&mp->m_sb)) { 7318f80587bSDave Chinner int new_size = mp->m_inode_cluster_size; 7328f80587bSDave Chinner 7338f80587bSDave Chinner new_size *= mp->m_sb.sb_inodesize / XFS_DINODE_MIN_SIZE; 7348f80587bSDave Chinner if (mp->m_sb.sb_inoalignmt >= XFS_B_TO_FSBT(mp, new_size)) 7358f80587bSDave Chinner mp->m_inode_cluster_size = new_size; 7368f80587bSDave Chinner } 7370771fb45SEric Sandeen 7380771fb45SEric Sandeen /* 739e5376fc1SBrian Foster * If enabled, sparse inode chunk alignment is expected to match the 740e5376fc1SBrian Foster * cluster size. Full inode chunk alignment must match the chunk size, 741e5376fc1SBrian Foster * but that is checked on sb read verification... 742e5376fc1SBrian Foster */ 743e5376fc1SBrian Foster if (xfs_sb_version_hassparseinodes(&mp->m_sb) && 744e5376fc1SBrian Foster mp->m_sb.sb_spino_align != 745e5376fc1SBrian Foster XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size)) { 746e5376fc1SBrian Foster xfs_warn(mp, 747e5376fc1SBrian Foster "Sparse inode block alignment (%u) must match cluster size (%llu).", 748e5376fc1SBrian Foster mp->m_sb.sb_spino_align, 749e5376fc1SBrian Foster XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size)); 750e5376fc1SBrian Foster error = -EINVAL; 751e5376fc1SBrian Foster goto out_remove_uuid; 752e5376fc1SBrian Foster } 753e5376fc1SBrian Foster 754e5376fc1SBrian Foster /* 7550771fb45SEric Sandeen * Set inode alignment fields 7560771fb45SEric Sandeen */ 7570771fb45SEric Sandeen xfs_set_inoalignment(mp); 7580771fb45SEric Sandeen 7590771fb45SEric Sandeen /* 760c2bfbc9bSZhi Yong Wu * Check that the data (and log if separate) is an ok size. 7610771fb45SEric Sandeen */ 7624249023aSChristoph Hellwig error = xfs_check_sizes(mp); 7630771fb45SEric Sandeen if (error) 764f9057e3dSChristoph Hellwig goto out_remove_uuid; 7650771fb45SEric Sandeen 7660771fb45SEric Sandeen /* 7671da177e4SLinus Torvalds * Initialize realtime fields in the mount structure 7681da177e4SLinus Torvalds */ 7690771fb45SEric Sandeen error = xfs_rtmount_init(mp); 7700771fb45SEric Sandeen if (error) { 7710b932cccSDave Chinner xfs_warn(mp, "RT mount failed"); 772f9057e3dSChristoph Hellwig goto out_remove_uuid; 7731da177e4SLinus Torvalds } 7741da177e4SLinus Torvalds 7751da177e4SLinus Torvalds /* 7761da177e4SLinus Torvalds * Copies the low order bits of the timestamp and the randomly 7771da177e4SLinus Torvalds * set "sequence" number out of a UUID. 7781da177e4SLinus Torvalds */ 7791da177e4SLinus Torvalds uuid_getnodeuniq(&sbp->sb_uuid, mp->m_fixedfsid); 7801da177e4SLinus Torvalds 7811da177e4SLinus Torvalds mp->m_dmevmask = 0; /* not persistent; set after each mount */ 7821da177e4SLinus Torvalds 7830650b554SDave Chinner error = xfs_da_mount(mp); 7840650b554SDave Chinner if (error) { 7850650b554SDave Chinner xfs_warn(mp, "Failed dir/attr init: %d", error); 7860650b554SDave Chinner goto out_remove_uuid; 7870650b554SDave Chinner } 7881da177e4SLinus Torvalds 7891da177e4SLinus Torvalds /* 7901da177e4SLinus Torvalds * Initialize the precomputed transaction reservations values. 7911da177e4SLinus Torvalds */ 7921da177e4SLinus Torvalds xfs_trans_init(mp); 7931da177e4SLinus Torvalds 7941da177e4SLinus Torvalds /* 7951da177e4SLinus Torvalds * Allocate and initialize the per-ag data. 7961da177e4SLinus Torvalds */ 7971c1c6ebcSDave Chinner spin_lock_init(&mp->m_perag_lock); 7989b98b6f3SDave Chinner INIT_RADIX_TREE(&mp->m_perag_tree, GFP_ATOMIC); 7991c1c6ebcSDave Chinner error = xfs_initialize_perag(mp, sbp->sb_agcount, &mp->m_maxagi); 8001c1c6ebcSDave Chinner if (error) { 8010b932cccSDave Chinner xfs_warn(mp, "Failed per-ag init: %d", error); 8020650b554SDave Chinner goto out_free_dir; 8031c1c6ebcSDave Chinner } 8041da177e4SLinus Torvalds 805f9057e3dSChristoph Hellwig if (!sbp->sb_logblocks) { 8060b932cccSDave Chinner xfs_warn(mp, "no log defined"); 807f9057e3dSChristoph Hellwig XFS_ERROR_REPORT("xfs_mountfs", XFS_ERRLEVEL_LOW, mp); 8082451337dSDave Chinner error = -EFSCORRUPTED; 809f9057e3dSChristoph Hellwig goto out_free_perag; 810f9057e3dSChristoph Hellwig } 811f9057e3dSChristoph Hellwig 8121da177e4SLinus Torvalds /* 813f0b2efadSBrian Foster * Log's mount-time initialization. The first part of recovery can place 814f0b2efadSBrian Foster * some items on the AIL, to be handled when recovery is finished or 815f0b2efadSBrian Foster * cancelled. 8161da177e4SLinus Torvalds */ 8171da177e4SLinus Torvalds error = xfs_log_mount(mp, mp->m_logdev_targp, 8181da177e4SLinus Torvalds XFS_FSB_TO_DADDR(mp, sbp->sb_logstart), 8191da177e4SLinus Torvalds XFS_FSB_TO_BB(mp, sbp->sb_logblocks)); 8201da177e4SLinus Torvalds if (error) { 8210b932cccSDave Chinner xfs_warn(mp, "log mount failed"); 822d4f3512bSDave Chinner goto out_fail_wait; 8231da177e4SLinus Torvalds } 8241da177e4SLinus Torvalds 8251da177e4SLinus Torvalds /* 82692821e2bSDavid Chinner * Now the log is mounted, we know if it was an unclean shutdown or 82792821e2bSDavid Chinner * not. If it was, with the first phase of recovery has completed, we 82892821e2bSDavid Chinner * have consistent AG blocks on disk. We have not recovered EFIs yet, 82992821e2bSDavid Chinner * but they are recovered transactionally in the second recovery phase 83092821e2bSDavid Chinner * later. 83192821e2bSDavid Chinner * 83292821e2bSDavid Chinner * Hence we can safely re-initialise incore superblock counters from 83392821e2bSDavid Chinner * the per-ag data. These may not be correct if the filesystem was not 83492821e2bSDavid Chinner * cleanly unmounted, so we need to wait for recovery to finish before 83592821e2bSDavid Chinner * doing this. 83692821e2bSDavid Chinner * 83792821e2bSDavid Chinner * If the filesystem was cleanly unmounted, then we can trust the 83892821e2bSDavid Chinner * values in the superblock to be correct and we don't need to do 83992821e2bSDavid Chinner * anything here. 84092821e2bSDavid Chinner * 84192821e2bSDavid Chinner * If we are currently making the filesystem, the initialisation will 84292821e2bSDavid Chinner * fail as the perag data is in an undefined state. 84392821e2bSDavid Chinner */ 84492821e2bSDavid Chinner if (xfs_sb_version_haslazysbcount(&mp->m_sb) && 84592821e2bSDavid Chinner !XFS_LAST_UNMOUNT_WAS_CLEAN(mp) && 84692821e2bSDavid Chinner !mp->m_sb.sb_inprogress) { 84792821e2bSDavid Chinner error = xfs_initialize_perag_data(mp, sbp->sb_agcount); 848f9057e3dSChristoph Hellwig if (error) 8496eee8972Skbuild test robot goto out_log_dealloc; 85092821e2bSDavid Chinner } 851f9057e3dSChristoph Hellwig 85292821e2bSDavid Chinner /* 8531da177e4SLinus Torvalds * Get and sanity-check the root inode. 8541da177e4SLinus Torvalds * Save the pointer to it in the mount structure. 8551da177e4SLinus Torvalds */ 8567b6259e7SDave Chinner error = xfs_iget(mp, NULL, sbp->sb_rootino, 0, XFS_ILOCK_EXCL, &rip); 8571da177e4SLinus Torvalds if (error) { 8580b932cccSDave Chinner xfs_warn(mp, "failed to read root inode"); 859f9057e3dSChristoph Hellwig goto out_log_dealloc; 8601da177e4SLinus Torvalds } 8611da177e4SLinus Torvalds 8621da177e4SLinus Torvalds ASSERT(rip != NULL); 8631da177e4SLinus Torvalds 864c19b3b05SDave Chinner if (unlikely(!S_ISDIR(VFS_I(rip)->i_mode))) { 8650b932cccSDave Chinner xfs_warn(mp, "corrupted root inode %llu: not a directory", 866b6574520SNathan Scott (unsigned long long)rip->i_ino); 8671da177e4SLinus Torvalds xfs_iunlock(rip, XFS_ILOCK_EXCL); 8681da177e4SLinus Torvalds XFS_ERROR_REPORT("xfs_mountfs_int(2)", XFS_ERRLEVEL_LOW, 8691da177e4SLinus Torvalds mp); 8702451337dSDave Chinner error = -EFSCORRUPTED; 871f9057e3dSChristoph Hellwig goto out_rele_rip; 8721da177e4SLinus Torvalds } 8731da177e4SLinus Torvalds mp->m_rootip = rip; /* save it */ 8741da177e4SLinus Torvalds 8751da177e4SLinus Torvalds xfs_iunlock(rip, XFS_ILOCK_EXCL); 8761da177e4SLinus Torvalds 8771da177e4SLinus Torvalds /* 8781da177e4SLinus Torvalds * Initialize realtime inode pointers in the mount structure 8791da177e4SLinus Torvalds */ 8800771fb45SEric Sandeen error = xfs_rtmount_inodes(mp); 8810771fb45SEric Sandeen if (error) { 8821da177e4SLinus Torvalds /* 8831da177e4SLinus Torvalds * Free up the root inode. 8841da177e4SLinus Torvalds */ 8850b932cccSDave Chinner xfs_warn(mp, "failed to read RT inodes"); 886f9057e3dSChristoph Hellwig goto out_rele_rip; 8871da177e4SLinus Torvalds } 8881da177e4SLinus Torvalds 8891da177e4SLinus Torvalds /* 8907884bc86SChristoph Hellwig * If this is a read-only mount defer the superblock updates until 8917884bc86SChristoph Hellwig * the next remount into writeable mode. Otherwise we would never 8927884bc86SChristoph Hellwig * perform the update e.g. for the root filesystem. 8931da177e4SLinus Torvalds */ 89461e63ecbSDave Chinner if (mp->m_update_sb && !(mp->m_flags & XFS_MOUNT_RDONLY)) { 89561e63ecbSDave Chinner error = xfs_sync_sb(mp, false); 896e5720eecSDavid Chinner if (error) { 8970b932cccSDave Chinner xfs_warn(mp, "failed to write sb changes"); 898b93b6e43SChristoph Hellwig goto out_rtunmount; 899e5720eecSDavid Chinner } 900e5720eecSDavid Chinner } 9011da177e4SLinus Torvalds 9021da177e4SLinus Torvalds /* 9031da177e4SLinus Torvalds * Initialise the XFS quota management subsystem for this mount 9041da177e4SLinus Torvalds */ 9057d095257SChristoph Hellwig if (XFS_IS_QUOTA_RUNNING(mp)) { 9067d095257SChristoph Hellwig error = xfs_qm_newmount(mp, "amount, "aflags); 9070771fb45SEric Sandeen if (error) 908b93b6e43SChristoph Hellwig goto out_rtunmount; 9097d095257SChristoph Hellwig } else { 9107d095257SChristoph Hellwig ASSERT(!XFS_IS_QUOTA_ON(mp)); 9117d095257SChristoph Hellwig 9127d095257SChristoph Hellwig /* 9137d095257SChristoph Hellwig * If a file system had quotas running earlier, but decided to 9147d095257SChristoph Hellwig * mount without -o uquota/pquota/gquota options, revoke the 9157d095257SChristoph Hellwig * quotachecked license. 9167d095257SChristoph Hellwig */ 9177d095257SChristoph Hellwig if (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_ACCT) { 9180b932cccSDave Chinner xfs_notice(mp, "resetting quota flags"); 9197d095257SChristoph Hellwig error = xfs_mount_reset_sbqflags(mp); 9207d095257SChristoph Hellwig if (error) 921a70a4fa5SBrian Foster goto out_rtunmount; 9227d095257SChristoph Hellwig } 9237d095257SChristoph Hellwig } 9241da177e4SLinus Torvalds 9251da177e4SLinus Torvalds /* 926f0b2efadSBrian Foster * Finish recovering the file system. This part needed to be delayed 927f0b2efadSBrian Foster * until after the root and real-time bitmap inodes were consistently 928f0b2efadSBrian Foster * read in. 9291da177e4SLinus Torvalds */ 9304249023aSChristoph Hellwig error = xfs_log_mount_finish(mp); 9311da177e4SLinus Torvalds if (error) { 9320b932cccSDave Chinner xfs_warn(mp, "log mount finish failed"); 933b93b6e43SChristoph Hellwig goto out_rtunmount; 9341da177e4SLinus Torvalds } 9351da177e4SLinus Torvalds 9361da177e4SLinus Torvalds /* 9371da177e4SLinus Torvalds * Complete the quota initialisation, post-log-replay component. 9381da177e4SLinus Torvalds */ 9397d095257SChristoph Hellwig if (quotamount) { 9407d095257SChristoph Hellwig ASSERT(mp->m_qflags == 0); 9417d095257SChristoph Hellwig mp->m_qflags = quotaflags; 9427d095257SChristoph Hellwig 9437d095257SChristoph Hellwig xfs_qm_mount_quotas(mp); 9447d095257SChristoph Hellwig } 9457d095257SChristoph Hellwig 94684e1e99fSDavid Chinner /* 94784e1e99fSDavid Chinner * Now we are mounted, reserve a small amount of unused space for 94884e1e99fSDavid Chinner * privileged transactions. This is needed so that transaction 94984e1e99fSDavid Chinner * space required for critical operations can dip into this pool 95084e1e99fSDavid Chinner * when at ENOSPC. This is needed for operations like create with 95184e1e99fSDavid Chinner * attr, unwritten extent conversion at ENOSPC, etc. Data allocations 95284e1e99fSDavid Chinner * are not allowed to use this reserved space. 9538babd8a2SDave Chinner * 9548babd8a2SDave Chinner * This may drive us straight to ENOSPC on mount, but that implies 9558babd8a2SDave Chinner * we were already there on the last unmount. Warn if this occurs. 95684e1e99fSDavid Chinner */ 957d5db0f97SEric Sandeen if (!(mp->m_flags & XFS_MOUNT_RDONLY)) { 958d5db0f97SEric Sandeen resblks = xfs_default_resblks(mp); 959714082bcSDavid Chinner error = xfs_reserve_blocks(mp, &resblks, NULL); 960714082bcSDavid Chinner if (error) 9610b932cccSDave Chinner xfs_warn(mp, 9620b932cccSDave Chinner "Unable to allocate reserve blocks. Continuing without reserve pool."); 963d5db0f97SEric Sandeen } 96484e1e99fSDavid Chinner 9651da177e4SLinus Torvalds return 0; 9661da177e4SLinus Torvalds 967b93b6e43SChristoph Hellwig out_rtunmount: 968b93b6e43SChristoph Hellwig xfs_rtunmount_inodes(mp); 969f9057e3dSChristoph Hellwig out_rele_rip: 97043355099SChristoph Hellwig IRELE(rip); 9710ae120f8SBrian Foster cancel_delayed_work_sync(&mp->m_reclaim_work); 9720ae120f8SBrian Foster xfs_reclaim_inodes(mp, SYNC_WAIT); 973f9057e3dSChristoph Hellwig out_log_dealloc: 974e6b3bb78SCarlos Maiolino mp->m_flags |= XFS_MOUNT_UNMOUNTING; 975f0b2efadSBrian Foster xfs_log_mount_cancel(mp); 976d4f3512bSDave Chinner out_fail_wait: 977d4f3512bSDave Chinner if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp) 978d4f3512bSDave Chinner xfs_wait_buftarg(mp->m_logdev_targp); 979d4f3512bSDave Chinner xfs_wait_buftarg(mp->m_ddev_targp); 980f9057e3dSChristoph Hellwig out_free_perag: 981ff4f038cSChristoph Hellwig xfs_free_perag(mp); 9820650b554SDave Chinner out_free_dir: 9830650b554SDave Chinner xfs_da_unmount(mp); 984f9057e3dSChristoph Hellwig out_remove_uuid: 98527174203SChristoph Hellwig xfs_uuid_unmount(mp); 986192852beSCarlos Maiolino out_remove_error_sysfs: 987192852beSCarlos Maiolino xfs_error_sysfs_del(mp); 988225e4635SBill O'Donnell out_del_stats: 989225e4635SBill O'Donnell xfs_sysfs_del(&mp->m_stats.xs_kobj); 990a31b1d3dSBrian Foster out_remove_sysfs: 991a31b1d3dSBrian Foster xfs_sysfs_del(&mp->m_kobj); 992f9057e3dSChristoph Hellwig out: 9931da177e4SLinus Torvalds return error; 9941da177e4SLinus Torvalds } 9951da177e4SLinus Torvalds 9961da177e4SLinus Torvalds /* 9971da177e4SLinus Torvalds * This flushes out the inodes,dquots and the superblock, unmounts the 9981da177e4SLinus Torvalds * log and makes sure that incore structures are freed. 9991da177e4SLinus Torvalds */ 100041b5c2e7SChristoph Hellwig void 100141b5c2e7SChristoph Hellwig xfs_unmountfs( 100241b5c2e7SChristoph Hellwig struct xfs_mount *mp) 10031da177e4SLinus Torvalds { 100484e1e99fSDavid Chinner __uint64_t resblks; 100541b5c2e7SChristoph Hellwig int error; 10061da177e4SLinus Torvalds 1007579b62faSBrian Foster cancel_delayed_work_sync(&mp->m_eofblocks_work); 1008579b62faSBrian Foster 10097d095257SChristoph Hellwig xfs_qm_unmount_quotas(mp); 1010b93b6e43SChristoph Hellwig xfs_rtunmount_inodes(mp); 101177508ec8SChristoph Hellwig IRELE(mp->m_rootip); 101277508ec8SChristoph Hellwig 1013641c56fbSDavid Chinner /* 1014641c56fbSDavid Chinner * We can potentially deadlock here if we have an inode cluster 10159da096fdSMalcolm Parsons * that has been freed has its buffer still pinned in memory because 1016641c56fbSDavid Chinner * the transaction is still sitting in a iclog. The stale inodes 1017641c56fbSDavid Chinner * on that buffer will have their flush locks held until the 1018641c56fbSDavid Chinner * transaction hits the disk and the callbacks run. the inode 1019641c56fbSDavid Chinner * flush takes the flush lock unconditionally and with nothing to 1020641c56fbSDavid Chinner * push out the iclog we will never get that unlocked. hence we 1021641c56fbSDavid Chinner * need to force the log first. 1022641c56fbSDavid Chinner */ 1023a14a348bSChristoph Hellwig xfs_log_force(mp, XFS_LOG_SYNC); 1024c854363eSDave Chinner 1025c854363eSDave Chinner /* 1026e6b3bb78SCarlos Maiolino * We now need to tell the world we are unmounting. This will allow 1027e6b3bb78SCarlos Maiolino * us to detect that the filesystem is going away and we should error 1028e6b3bb78SCarlos Maiolino * out anything that we have been retrying in the background. This will 1029e6b3bb78SCarlos Maiolino * prevent neverending retries in AIL pushing from hanging the unmount. 1030e6b3bb78SCarlos Maiolino */ 1031e6b3bb78SCarlos Maiolino mp->m_flags |= XFS_MOUNT_UNMOUNTING; 1032e6b3bb78SCarlos Maiolino 1033e6b3bb78SCarlos Maiolino /* 1034211e4d43SChristoph Hellwig * Flush all pending changes from the AIL. 1035c854363eSDave Chinner */ 1036211e4d43SChristoph Hellwig xfs_ail_push_all_sync(mp->m_ail); 1037211e4d43SChristoph Hellwig 1038211e4d43SChristoph Hellwig /* 1039211e4d43SChristoph Hellwig * And reclaim all inodes. At this point there should be no dirty 10407e18530bSDave Chinner * inodes and none should be pinned or locked, but use synchronous 10417e18530bSDave Chinner * reclaim just to be sure. We can stop background inode reclaim 10427e18530bSDave Chinner * here as well if it is still running. 1043211e4d43SChristoph Hellwig */ 10447e18530bSDave Chinner cancel_delayed_work_sync(&mp->m_reclaim_work); 1045c854363eSDave Chinner xfs_reclaim_inodes(mp, SYNC_WAIT); 10461da177e4SLinus Torvalds 10477d095257SChristoph Hellwig xfs_qm_unmount(mp); 1048a357a121SLachlan McIlroy 10491da177e4SLinus Torvalds /* 105084e1e99fSDavid Chinner * Unreserve any blocks we have so that when we unmount we don't account 105184e1e99fSDavid Chinner * the reserved free space as used. This is really only necessary for 105284e1e99fSDavid Chinner * lazy superblock counting because it trusts the incore superblock 10539da096fdSMalcolm Parsons * counters to be absolutely correct on clean unmount. 105484e1e99fSDavid Chinner * 105584e1e99fSDavid Chinner * We don't bother correcting this elsewhere for lazy superblock 105684e1e99fSDavid Chinner * counting because on mount of an unclean filesystem we reconstruct the 105784e1e99fSDavid Chinner * correct counter value and this is irrelevant. 105884e1e99fSDavid Chinner * 105984e1e99fSDavid Chinner * For non-lazy counter filesystems, this doesn't matter at all because 106084e1e99fSDavid Chinner * we only every apply deltas to the superblock and hence the incore 106184e1e99fSDavid Chinner * value does not matter.... 106284e1e99fSDavid Chinner */ 106384e1e99fSDavid Chinner resblks = 0; 1064714082bcSDavid Chinner error = xfs_reserve_blocks(mp, &resblks, NULL); 1065714082bcSDavid Chinner if (error) 10660b932cccSDave Chinner xfs_warn(mp, "Unable to free reserved block pool. " 1067714082bcSDavid Chinner "Freespace may not be correct on next mount."); 1068714082bcSDavid Chinner 1069adab0f67SChandra Seetharaman error = xfs_log_sbcount(mp); 1070e5720eecSDavid Chinner if (error) 10710b932cccSDave Chinner xfs_warn(mp, "Unable to update superblock counters. " 1072e5720eecSDavid Chinner "Freespace may not be correct on next mount."); 107387c7bec7SChristoph Hellwig 1074225e4635SBill O'Donnell 107521b699c8SChristoph Hellwig xfs_log_unmount(mp); 10760650b554SDave Chinner xfs_da_unmount(mp); 107727174203SChristoph Hellwig xfs_uuid_unmount(mp); 10781da177e4SLinus Torvalds 10791550d0b0SChristoph Hellwig #if defined(DEBUG) 10800ce4cfd4SChristoph Hellwig xfs_errortag_clearall(mp, 0); 10811da177e4SLinus Torvalds #endif 1082ff4f038cSChristoph Hellwig xfs_free_perag(mp); 1083a31b1d3dSBrian Foster 1084192852beSCarlos Maiolino xfs_error_sysfs_del(mp); 1085225e4635SBill O'Donnell xfs_sysfs_del(&mp->m_stats.xs_kobj); 1086a31b1d3dSBrian Foster xfs_sysfs_del(&mp->m_kobj); 10871da177e4SLinus Torvalds } 10881da177e4SLinus Torvalds 108991ee575fSBrian Foster /* 109091ee575fSBrian Foster * Determine whether modifications can proceed. The caller specifies the minimum 109191ee575fSBrian Foster * freeze level for which modifications should not be allowed. This allows 109291ee575fSBrian Foster * certain operations to proceed while the freeze sequence is in progress, if 109391ee575fSBrian Foster * necessary. 109491ee575fSBrian Foster */ 109591ee575fSBrian Foster bool 109691ee575fSBrian Foster xfs_fs_writable( 109791ee575fSBrian Foster struct xfs_mount *mp, 109891ee575fSBrian Foster int level) 109992821e2bSDavid Chinner { 110091ee575fSBrian Foster ASSERT(level > SB_UNFROZEN); 110191ee575fSBrian Foster if ((mp->m_super->s_writers.frozen >= level) || 110291ee575fSBrian Foster XFS_FORCED_SHUTDOWN(mp) || (mp->m_flags & XFS_MOUNT_RDONLY)) 110391ee575fSBrian Foster return false; 110491ee575fSBrian Foster 110591ee575fSBrian Foster return true; 110692821e2bSDavid Chinner } 110792821e2bSDavid Chinner 110892821e2bSDavid Chinner /* 1109b2ce3974SAlex Elder * xfs_log_sbcount 1110b2ce3974SAlex Elder * 1111adab0f67SChandra Seetharaman * Sync the superblock counters to disk. 1112b2ce3974SAlex Elder * 111391ee575fSBrian Foster * Note this code can be called during the process of freezing, so we use the 111491ee575fSBrian Foster * transaction allocator that does not block when the transaction subsystem is 111591ee575fSBrian Foster * in its frozen state. 111692821e2bSDavid Chinner */ 111792821e2bSDavid Chinner int 1118adab0f67SChandra Seetharaman xfs_log_sbcount(xfs_mount_t *mp) 111992821e2bSDavid Chinner { 112091ee575fSBrian Foster /* allow this to proceed during the freeze sequence... */ 112191ee575fSBrian Foster if (!xfs_fs_writable(mp, SB_FREEZE_COMPLETE)) 112292821e2bSDavid Chinner return 0; 112392821e2bSDavid Chinner 112492821e2bSDavid Chinner /* 112592821e2bSDavid Chinner * we don't need to do this if we are updating the superblock 112692821e2bSDavid Chinner * counters on every modification. 112792821e2bSDavid Chinner */ 112892821e2bSDavid Chinner if (!xfs_sb_version_haslazysbcount(&mp->m_sb)) 112992821e2bSDavid Chinner return 0; 113092821e2bSDavid Chinner 113161e63ecbSDave Chinner return xfs_sync_sb(mp, true); 113292821e2bSDavid Chinner } 113392821e2bSDavid Chinner 11348c1903d3SDave Chinner /* 11358c1903d3SDave Chinner * Deltas for the inode count are +/-64, hence we use a large batch size 11368c1903d3SDave Chinner * of 128 so we don't need to take the counter lock on every update. 11378c1903d3SDave Chinner */ 11388c1903d3SDave Chinner #define XFS_ICOUNT_BATCH 128 1139501ab323SDave Chinner int 1140501ab323SDave Chinner xfs_mod_icount( 1141501ab323SDave Chinner struct xfs_mount *mp, 1142501ab323SDave Chinner int64_t delta) 1143501ab323SDave Chinner { 11448c1903d3SDave Chinner __percpu_counter_add(&mp->m_icount, delta, XFS_ICOUNT_BATCH); 11458c1903d3SDave Chinner if (__percpu_counter_compare(&mp->m_icount, 0, XFS_ICOUNT_BATCH) < 0) { 1146501ab323SDave Chinner ASSERT(0); 1147501ab323SDave Chinner percpu_counter_add(&mp->m_icount, -delta); 1148501ab323SDave Chinner return -EINVAL; 1149501ab323SDave Chinner } 1150501ab323SDave Chinner return 0; 1151501ab323SDave Chinner } 1152501ab323SDave Chinner 1153e88b64eaSDave Chinner int 1154e88b64eaSDave Chinner xfs_mod_ifree( 1155e88b64eaSDave Chinner struct xfs_mount *mp, 1156e88b64eaSDave Chinner int64_t delta) 1157e88b64eaSDave Chinner { 1158e88b64eaSDave Chinner percpu_counter_add(&mp->m_ifree, delta); 1159e88b64eaSDave Chinner if (percpu_counter_compare(&mp->m_ifree, 0) < 0) { 1160e88b64eaSDave Chinner ASSERT(0); 1161e88b64eaSDave Chinner percpu_counter_add(&mp->m_ifree, -delta); 1162e88b64eaSDave Chinner return -EINVAL; 1163e88b64eaSDave Chinner } 1164e88b64eaSDave Chinner return 0; 1165e88b64eaSDave Chinner } 11660d485adaSDave Chinner 11678c1903d3SDave Chinner /* 11688c1903d3SDave Chinner * Deltas for the block count can vary from 1 to very large, but lock contention 11698c1903d3SDave Chinner * only occurs on frequent small block count updates such as in the delayed 11708c1903d3SDave Chinner * allocation path for buffered writes (page a time updates). Hence we set 11718c1903d3SDave Chinner * a large batch count (1024) to minimise global counter updates except when 11728c1903d3SDave Chinner * we get near to ENOSPC and we have to be very accurate with our updates. 11738c1903d3SDave Chinner */ 11748c1903d3SDave Chinner #define XFS_FDBLOCKS_BATCH 1024 11750d485adaSDave Chinner int 11760d485adaSDave Chinner xfs_mod_fdblocks( 11770d485adaSDave Chinner struct xfs_mount *mp, 11780d485adaSDave Chinner int64_t delta, 11790d485adaSDave Chinner bool rsvd) 11800d485adaSDave Chinner { 11810d485adaSDave Chinner int64_t lcounter; 11820d485adaSDave Chinner long long res_used; 11830d485adaSDave Chinner s32 batch; 11840d485adaSDave Chinner 11850d485adaSDave Chinner if (delta > 0) { 11860d485adaSDave Chinner /* 11870d485adaSDave Chinner * If the reserve pool is depleted, put blocks back into it 11880d485adaSDave Chinner * first. Most of the time the pool is full. 11890d485adaSDave Chinner */ 11900d485adaSDave Chinner if (likely(mp->m_resblks == mp->m_resblks_avail)) { 11910d485adaSDave Chinner percpu_counter_add(&mp->m_fdblocks, delta); 11920d485adaSDave Chinner return 0; 11930d485adaSDave Chinner } 11940d485adaSDave Chinner 11950d485adaSDave Chinner spin_lock(&mp->m_sb_lock); 11960d485adaSDave Chinner res_used = (long long)(mp->m_resblks - mp->m_resblks_avail); 11970d485adaSDave Chinner 11980d485adaSDave Chinner if (res_used > delta) { 11990d485adaSDave Chinner mp->m_resblks_avail += delta; 12000d485adaSDave Chinner } else { 12010d485adaSDave Chinner delta -= res_used; 12020d485adaSDave Chinner mp->m_resblks_avail = mp->m_resblks; 12030d485adaSDave Chinner percpu_counter_add(&mp->m_fdblocks, delta); 12040d485adaSDave Chinner } 12050d485adaSDave Chinner spin_unlock(&mp->m_sb_lock); 12060d485adaSDave Chinner return 0; 12070d485adaSDave Chinner } 12080d485adaSDave Chinner 12090d485adaSDave Chinner /* 12100d485adaSDave Chinner * Taking blocks away, need to be more accurate the closer we 12110d485adaSDave Chinner * are to zero. 12120d485adaSDave Chinner * 12130d485adaSDave Chinner * If the counter has a value of less than 2 * max batch size, 12140d485adaSDave Chinner * then make everything serialise as we are real close to 12150d485adaSDave Chinner * ENOSPC. 12160d485adaSDave Chinner */ 12178c1903d3SDave Chinner if (__percpu_counter_compare(&mp->m_fdblocks, 2 * XFS_FDBLOCKS_BATCH, 12188c1903d3SDave Chinner XFS_FDBLOCKS_BATCH) < 0) 12190d485adaSDave Chinner batch = 1; 12200d485adaSDave Chinner else 12218c1903d3SDave Chinner batch = XFS_FDBLOCKS_BATCH; 12220d485adaSDave Chinner 12230d485adaSDave Chinner __percpu_counter_add(&mp->m_fdblocks, delta, batch); 12248c1903d3SDave Chinner if (__percpu_counter_compare(&mp->m_fdblocks, XFS_ALLOC_SET_ASIDE(mp), 12258c1903d3SDave Chinner XFS_FDBLOCKS_BATCH) >= 0) { 12260d485adaSDave Chinner /* we had space! */ 12270d485adaSDave Chinner return 0; 12280d485adaSDave Chinner } 12290d485adaSDave Chinner 12300d485adaSDave Chinner /* 12310d485adaSDave Chinner * lock up the sb for dipping into reserves before releasing the space 12320d485adaSDave Chinner * that took us to ENOSPC. 12330d485adaSDave Chinner */ 12340d485adaSDave Chinner spin_lock(&mp->m_sb_lock); 12350d485adaSDave Chinner percpu_counter_add(&mp->m_fdblocks, -delta); 12360d485adaSDave Chinner if (!rsvd) 12370d485adaSDave Chinner goto fdblocks_enospc; 12380d485adaSDave Chinner 12390d485adaSDave Chinner lcounter = (long long)mp->m_resblks_avail + delta; 12400d485adaSDave Chinner if (lcounter >= 0) { 12410d485adaSDave Chinner mp->m_resblks_avail = lcounter; 12420d485adaSDave Chinner spin_unlock(&mp->m_sb_lock); 12430d485adaSDave Chinner return 0; 12440d485adaSDave Chinner } 12450d485adaSDave Chinner printk_once(KERN_WARNING 12460d485adaSDave Chinner "Filesystem \"%s\": reserve blocks depleted! " 12470d485adaSDave Chinner "Consider increasing reserve pool size.", 12480d485adaSDave Chinner mp->m_fsname); 12490d485adaSDave Chinner fdblocks_enospc: 12500d485adaSDave Chinner spin_unlock(&mp->m_sb_lock); 12510d485adaSDave Chinner return -ENOSPC; 12520d485adaSDave Chinner } 12530d485adaSDave Chinner 1254bab98bbeSDave Chinner int 1255bab98bbeSDave Chinner xfs_mod_frextents( 1256bab98bbeSDave Chinner struct xfs_mount *mp, 1257bab98bbeSDave Chinner int64_t delta) 1258bab98bbeSDave Chinner { 1259bab98bbeSDave Chinner int64_t lcounter; 1260bab98bbeSDave Chinner int ret = 0; 1261bab98bbeSDave Chinner 1262bab98bbeSDave Chinner spin_lock(&mp->m_sb_lock); 1263bab98bbeSDave Chinner lcounter = mp->m_sb.sb_frextents + delta; 1264bab98bbeSDave Chinner if (lcounter < 0) 1265bab98bbeSDave Chinner ret = -ENOSPC; 1266bab98bbeSDave Chinner else 1267bab98bbeSDave Chinner mp->m_sb.sb_frextents = lcounter; 1268bab98bbeSDave Chinner spin_unlock(&mp->m_sb_lock); 1269bab98bbeSDave Chinner return ret; 1270bab98bbeSDave Chinner } 1271bab98bbeSDave Chinner 12721da177e4SLinus Torvalds /* 12731da177e4SLinus Torvalds * xfs_getsb() is called to obtain the buffer for the superblock. 12741da177e4SLinus Torvalds * The buffer is returned locked and read in from disk. 12751da177e4SLinus Torvalds * The buffer should be released with a call to xfs_brelse(). 12761da177e4SLinus Torvalds * 12771da177e4SLinus Torvalds * If the flags parameter is BUF_TRYLOCK, then we'll only return 12781da177e4SLinus Torvalds * the superblock buffer if it can be locked without sleeping. 12791da177e4SLinus Torvalds * If it can't then we'll return NULL. 12801da177e4SLinus Torvalds */ 12810c842ad4SChristoph Hellwig struct xfs_buf * 12821da177e4SLinus Torvalds xfs_getsb( 12830c842ad4SChristoph Hellwig struct xfs_mount *mp, 12841da177e4SLinus Torvalds int flags) 12851da177e4SLinus Torvalds { 12860c842ad4SChristoph Hellwig struct xfs_buf *bp = mp->m_sb_bp; 12871da177e4SLinus Torvalds 12880c842ad4SChristoph Hellwig if (!xfs_buf_trylock(bp)) { 12890c842ad4SChristoph Hellwig if (flags & XBF_TRYLOCK) 12901da177e4SLinus Torvalds return NULL; 12910c842ad4SChristoph Hellwig xfs_buf_lock(bp); 12921da177e4SLinus Torvalds } 12930c842ad4SChristoph Hellwig 129472790aa1SChandra Seetharaman xfs_buf_hold(bp); 1295b0388bf1SDave Chinner ASSERT(bp->b_flags & XBF_DONE); 1296014c2544SJesper Juhl return bp; 12971da177e4SLinus Torvalds } 12981da177e4SLinus Torvalds 12991da177e4SLinus Torvalds /* 13001da177e4SLinus Torvalds * Used to free the superblock along various error paths. 13011da177e4SLinus Torvalds */ 13021da177e4SLinus Torvalds void 13031da177e4SLinus Torvalds xfs_freesb( 130426af6552SDave Chinner struct xfs_mount *mp) 13051da177e4SLinus Torvalds { 130626af6552SDave Chinner struct xfs_buf *bp = mp->m_sb_bp; 13071da177e4SLinus Torvalds 130826af6552SDave Chinner xfs_buf_lock(bp); 13091da177e4SLinus Torvalds mp->m_sb_bp = NULL; 131026af6552SDave Chinner xfs_buf_relse(bp); 13111da177e4SLinus Torvalds } 13121da177e4SLinus Torvalds 13131da177e4SLinus Torvalds /* 1314dda35b8fSChristoph Hellwig * If the underlying (data/log/rt) device is readonly, there are some 1315dda35b8fSChristoph Hellwig * operations that cannot proceed. 1316dda35b8fSChristoph Hellwig */ 1317dda35b8fSChristoph Hellwig int 1318dda35b8fSChristoph Hellwig xfs_dev_is_read_only( 1319dda35b8fSChristoph Hellwig struct xfs_mount *mp, 1320dda35b8fSChristoph Hellwig char *message) 1321dda35b8fSChristoph Hellwig { 1322dda35b8fSChristoph Hellwig if (xfs_readonly_buftarg(mp->m_ddev_targp) || 1323dda35b8fSChristoph Hellwig xfs_readonly_buftarg(mp->m_logdev_targp) || 1324dda35b8fSChristoph Hellwig (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) { 13250b932cccSDave Chinner xfs_notice(mp, "%s required on read-only device.", message); 13260b932cccSDave Chinner xfs_notice(mp, "write access unavailable, cannot proceed."); 13272451337dSDave Chinner return -EROFS; 1328dda35b8fSChristoph Hellwig } 1329dda35b8fSChristoph Hellwig return 0; 1330dda35b8fSChristoph Hellwig } 1331