10b61f8a4SDave Chinner // SPDX-License-Identifier: GPL-2.0 21da177e4SLinus Torvalds /* 37b718769SNathan Scott * Copyright (c) 2000-2005 Silicon Graphics, Inc. 47b718769SNathan Scott * All Rights Reserved. 51da177e4SLinus Torvalds */ 61da177e4SLinus Torvalds #include "xfs.h" 7a844f451SNathan Scott #include "xfs_fs.h" 870a9883cSDave Chinner #include "xfs_shared.h" 9239880efSDave Chinner #include "xfs_format.h" 10239880efSDave Chinner #include "xfs_log_format.h" 11239880efSDave Chinner #include "xfs_trans_resv.h" 12a844f451SNathan Scott #include "xfs_bit.h" 131da177e4SLinus Torvalds #include "xfs_sb.h" 141da177e4SLinus Torvalds #include "xfs_mount.h" 151da177e4SLinus Torvalds #include "xfs_inode.h" 16a4fbe6abSDave Chinner #include "xfs_dir2.h" 17a844f451SNathan Scott #include "xfs_ialloc.h" 181da177e4SLinus Torvalds #include "xfs_alloc.h" 191da177e4SLinus Torvalds #include "xfs_rtalloc.h" 201da177e4SLinus Torvalds #include "xfs_bmap.h" 21a4fbe6abSDave Chinner #include "xfs_trans.h" 22a4fbe6abSDave Chinner #include "xfs_trans_priv.h" 23a4fbe6abSDave Chinner #include "xfs_log.h" 241da177e4SLinus Torvalds #include "xfs_error.h" 251da177e4SLinus Torvalds #include "xfs_quota.h" 261da177e4SLinus Torvalds #include "xfs_fsops.h" 276d8b79cfSDave Chinner #include "xfs_icache.h" 28a31b1d3dSBrian Foster #include "xfs_sysfs.h" 29035e00acSDarrick J. Wong #include "xfs_rmap_btree.h" 301946b91cSDarrick J. Wong #include "xfs_refcount_btree.h" 31174edb0eSDarrick J. Wong #include "xfs_reflink.h" 32ebf55872SChristoph Hellwig #include "xfs_extent_busy.h" 3339353ff6SDarrick J. Wong #include "xfs_health.h" 3413eaec4bSDarrick J. Wong #include "xfs_trace.h" 359bbafc71SDave Chinner #include "xfs_ag.h" 361da177e4SLinus Torvalds 3727174203SChristoph Hellwig static DEFINE_MUTEX(xfs_uuid_table_mutex); 3827174203SChristoph Hellwig static int xfs_uuid_table_size; 3927174203SChristoph Hellwig static uuid_t *xfs_uuid_table; 4027174203SChristoph Hellwig 41af3b6382SDarrick J. Wong void 42af3b6382SDarrick J. Wong xfs_uuid_table_free(void) 43af3b6382SDarrick J. Wong { 44af3b6382SDarrick J. Wong if (xfs_uuid_table_size == 0) 45af3b6382SDarrick J. Wong return; 46af3b6382SDarrick J. Wong kmem_free(xfs_uuid_table); 47af3b6382SDarrick J. Wong xfs_uuid_table = NULL; 48af3b6382SDarrick J. Wong xfs_uuid_table_size = 0; 49af3b6382SDarrick J. Wong } 50af3b6382SDarrick J. Wong 5127174203SChristoph Hellwig /* 5227174203SChristoph Hellwig * See if the UUID is unique among mounted XFS filesystems. 5327174203SChristoph Hellwig * Mount fails if UUID is nil or a FS with the same UUID is already mounted. 5427174203SChristoph Hellwig */ 5527174203SChristoph Hellwig STATIC int 5627174203SChristoph Hellwig xfs_uuid_mount( 5727174203SChristoph Hellwig struct xfs_mount *mp) 5827174203SChristoph Hellwig { 5927174203SChristoph Hellwig uuid_t *uuid = &mp->m_sb.sb_uuid; 6027174203SChristoph Hellwig int hole, i; 6127174203SChristoph Hellwig 628f720d9fSAmir Goldstein /* Publish UUID in struct super_block */ 6385787090SChristoph Hellwig uuid_copy(&mp->m_super->s_uuid, uuid); 648f720d9fSAmir Goldstein 6527174203SChristoph Hellwig if (mp->m_flags & XFS_MOUNT_NOUUID) 6627174203SChristoph Hellwig return 0; 6727174203SChristoph Hellwig 68d905fdaaSAmir Goldstein if (uuid_is_null(uuid)) { 69d905fdaaSAmir Goldstein xfs_warn(mp, "Filesystem has null UUID - can't mount"); 702451337dSDave Chinner return -EINVAL; 7127174203SChristoph Hellwig } 7227174203SChristoph Hellwig 7327174203SChristoph Hellwig mutex_lock(&xfs_uuid_table_mutex); 7427174203SChristoph Hellwig for (i = 0, hole = -1; i < xfs_uuid_table_size; i++) { 75d905fdaaSAmir Goldstein if (uuid_is_null(&xfs_uuid_table[i])) { 7627174203SChristoph Hellwig hole = i; 7727174203SChristoph Hellwig continue; 7827174203SChristoph Hellwig } 7927174203SChristoph Hellwig if (uuid_equal(uuid, &xfs_uuid_table[i])) 8027174203SChristoph Hellwig goto out_duplicate; 8127174203SChristoph Hellwig } 8227174203SChristoph Hellwig 8327174203SChristoph Hellwig if (hole < 0) { 84771915c4SCarlos Maiolino xfs_uuid_table = krealloc(xfs_uuid_table, 8527174203SChristoph Hellwig (xfs_uuid_table_size + 1) * sizeof(*xfs_uuid_table), 86771915c4SCarlos Maiolino GFP_KERNEL | __GFP_NOFAIL); 8727174203SChristoph Hellwig hole = xfs_uuid_table_size++; 8827174203SChristoph Hellwig } 8927174203SChristoph Hellwig xfs_uuid_table[hole] = *uuid; 9027174203SChristoph Hellwig mutex_unlock(&xfs_uuid_table_mutex); 9127174203SChristoph Hellwig 9227174203SChristoph Hellwig return 0; 9327174203SChristoph Hellwig 9427174203SChristoph Hellwig out_duplicate: 9527174203SChristoph Hellwig mutex_unlock(&xfs_uuid_table_mutex); 96021000e5SMitsuo Hayasaka xfs_warn(mp, "Filesystem has duplicate UUID %pU - can't mount", uuid); 972451337dSDave Chinner return -EINVAL; 9827174203SChristoph Hellwig } 9927174203SChristoph Hellwig 10027174203SChristoph Hellwig STATIC void 10127174203SChristoph Hellwig xfs_uuid_unmount( 10227174203SChristoph Hellwig struct xfs_mount *mp) 10327174203SChristoph Hellwig { 10427174203SChristoph Hellwig uuid_t *uuid = &mp->m_sb.sb_uuid; 10527174203SChristoph Hellwig int i; 10627174203SChristoph Hellwig 10727174203SChristoph Hellwig if (mp->m_flags & XFS_MOUNT_NOUUID) 10827174203SChristoph Hellwig return; 10927174203SChristoph Hellwig 11027174203SChristoph Hellwig mutex_lock(&xfs_uuid_table_mutex); 11127174203SChristoph Hellwig for (i = 0; i < xfs_uuid_table_size; i++) { 112d905fdaaSAmir Goldstein if (uuid_is_null(&xfs_uuid_table[i])) 11327174203SChristoph Hellwig continue; 11427174203SChristoph Hellwig if (!uuid_equal(uuid, &xfs_uuid_table[i])) 11527174203SChristoph Hellwig continue; 11627174203SChristoph Hellwig memset(&xfs_uuid_table[i], 0, sizeof(uuid_t)); 11727174203SChristoph Hellwig break; 11827174203SChristoph Hellwig } 11927174203SChristoph Hellwig ASSERT(i < xfs_uuid_table_size); 12027174203SChristoph Hellwig mutex_unlock(&xfs_uuid_table_mutex); 12127174203SChristoph Hellwig } 12227174203SChristoph Hellwig 1234cc929eeSNathan Scott /* 1244cc929eeSNathan Scott * Check size of device based on the (data/realtime) block count. 1254cc929eeSNathan Scott * Note: this check is used by the growfs code as well as mount. 1264cc929eeSNathan Scott */ 1274cc929eeSNathan Scott int 1284cc929eeSNathan Scott xfs_sb_validate_fsb_count( 1294cc929eeSNathan Scott xfs_sb_t *sbp, 130c8ce540dSDarrick J. Wong uint64_t nblocks) 1314cc929eeSNathan Scott { 1324cc929eeSNathan Scott ASSERT(PAGE_SHIFT >= sbp->sb_blocklog); 1334cc929eeSNathan Scott ASSERT(sbp->sb_blocklog >= BBSHIFT); 1344cc929eeSNathan Scott 135d5cf09baSChristoph Hellwig /* Limited by ULONG_MAX of page cache index */ 13609cbfeafSKirill A. Shutemov if (nblocks >> (PAGE_SHIFT - sbp->sb_blocklog) > ULONG_MAX) 1372451337dSDave Chinner return -EFBIG; 1384cc929eeSNathan Scott return 0; 1394cc929eeSNathan Scott } 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds /* 1421da177e4SLinus Torvalds * xfs_readsb 1431da177e4SLinus Torvalds * 1441da177e4SLinus Torvalds * Does the initial read of the superblock. 1451da177e4SLinus Torvalds */ 1461da177e4SLinus Torvalds int 147ff55068cSDave Chinner xfs_readsb( 148ff55068cSDave Chinner struct xfs_mount *mp, 149ff55068cSDave Chinner int flags) 1501da177e4SLinus Torvalds { 1511da177e4SLinus Torvalds unsigned int sector_size; 15204a1e6c5SDave Chinner struct xfs_buf *bp; 15304a1e6c5SDave Chinner struct xfs_sb *sbp = &mp->m_sb; 1541da177e4SLinus Torvalds int error; 155af34e09dSDave Chinner int loud = !(flags & XFS_MFSI_QUIET); 156daba5427SEric Sandeen const struct xfs_buf_ops *buf_ops; 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds ASSERT(mp->m_sb_bp == NULL); 1591da177e4SLinus Torvalds ASSERT(mp->m_ddev_targp != NULL); 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds /* 162daba5427SEric Sandeen * For the initial read, we must guess at the sector 163daba5427SEric Sandeen * size based on the block device. It's enough to 164daba5427SEric Sandeen * get the sb_sectsize out of the superblock and 165daba5427SEric Sandeen * then reread with the proper length. 166daba5427SEric Sandeen * We don't verify it yet, because it may not be complete. 167daba5427SEric Sandeen */ 168daba5427SEric Sandeen sector_size = xfs_getsize_buftarg(mp->m_ddev_targp); 169daba5427SEric Sandeen buf_ops = NULL; 170daba5427SEric Sandeen 171daba5427SEric Sandeen /* 172c891c30aSBrian Foster * Allocate a (locked) buffer to hold the superblock. This will be kept 173c891c30aSBrian Foster * around at all times to optimize access to the superblock. Therefore, 174c891c30aSBrian Foster * set XBF_NO_IOACCT to make sure it doesn't hold the buftarg count 175c891c30aSBrian Foster * elevated. 1761da177e4SLinus Torvalds */ 17726af6552SDave Chinner reread: 178ba372674SDave Chinner error = xfs_buf_read_uncached(mp->m_ddev_targp, XFS_SB_DADDR, 179c891c30aSBrian Foster BTOBB(sector_size), XBF_NO_IOACCT, &bp, 180c891c30aSBrian Foster buf_ops); 181ba372674SDave Chinner if (error) { 182eab4e633SDave Chinner if (loud) 183e721f504SDave Chinner xfs_warn(mp, "SB validate failed with error %d.", error); 184ac75a1f7SDave Chinner /* bad CRC means corrupted metadata */ 1852451337dSDave Chinner if (error == -EFSBADCRC) 1862451337dSDave Chinner error = -EFSCORRUPTED; 187ba372674SDave Chinner return error; 188eab4e633SDave Chinner } 1891da177e4SLinus Torvalds 1901da177e4SLinus Torvalds /* 1911da177e4SLinus Torvalds * Initialize the mount structure from the superblock. 1921da177e4SLinus Torvalds */ 1933e6e8afdSChristoph Hellwig xfs_sb_from_disk(sbp, bp->b_addr); 194556b8883SDave Chinner 195556b8883SDave Chinner /* 196556b8883SDave Chinner * If we haven't validated the superblock, do so now before we try 197556b8883SDave Chinner * to check the sector size and reread the superblock appropriately. 198556b8883SDave Chinner */ 199556b8883SDave Chinner if (sbp->sb_magicnum != XFS_SB_MAGIC) { 200556b8883SDave Chinner if (loud) 201556b8883SDave Chinner xfs_warn(mp, "Invalid superblock magic number"); 2022451337dSDave Chinner error = -EINVAL; 203556b8883SDave Chinner goto release_buf; 204556b8883SDave Chinner } 205ff55068cSDave Chinner 2061da177e4SLinus Torvalds /* 2071da177e4SLinus Torvalds * We must be able to do sector-sized and sector-aligned IO. 2081da177e4SLinus Torvalds */ 20904a1e6c5SDave Chinner if (sector_size > sbp->sb_sectsize) { 210af34e09dSDave Chinner if (loud) 211af34e09dSDave Chinner xfs_warn(mp, "device supports %u byte sectors (not %u)", 21204a1e6c5SDave Chinner sector_size, sbp->sb_sectsize); 2132451337dSDave Chinner error = -ENOSYS; 21426af6552SDave Chinner goto release_buf; 2151da177e4SLinus Torvalds } 2161da177e4SLinus Torvalds 217556b8883SDave Chinner if (buf_ops == NULL) { 2181da177e4SLinus Torvalds /* 219daba5427SEric Sandeen * Re-read the superblock so the buffer is correctly sized, 220daba5427SEric Sandeen * and properly verified. 2211da177e4SLinus Torvalds */ 2221da177e4SLinus Torvalds xfs_buf_relse(bp); 22304a1e6c5SDave Chinner sector_size = sbp->sb_sectsize; 224daba5427SEric Sandeen buf_ops = loud ? &xfs_sb_buf_ops : &xfs_sb_quiet_buf_ops; 22526af6552SDave Chinner goto reread; 2261da177e4SLinus Torvalds } 2271da177e4SLinus Torvalds 2285681ca40SDave Chinner xfs_reinit_percpu_counters(mp); 2298d280b98SDavid Chinner 23004a1e6c5SDave Chinner /* no need to be quiet anymore, so reset the buf ops */ 23104a1e6c5SDave Chinner bp->b_ops = &xfs_sb_buf_ops; 23204a1e6c5SDave Chinner 2331da177e4SLinus Torvalds mp->m_sb_bp = bp; 23426af6552SDave Chinner xfs_buf_unlock(bp); 2351da177e4SLinus Torvalds return 0; 2361da177e4SLinus Torvalds 23726af6552SDave Chinner release_buf: 2381da177e4SLinus Torvalds xfs_buf_relse(bp); 2391da177e4SLinus Torvalds return error; 2401da177e4SLinus Torvalds } 2411da177e4SLinus Torvalds 2421da177e4SLinus Torvalds /* 24313eaec4bSDarrick J. Wong * If the sunit/swidth change would move the precomputed root inode value, we 24413eaec4bSDarrick J. Wong * must reject the ondisk change because repair will stumble over that. 24513eaec4bSDarrick J. Wong * However, we allow the mount to proceed because we never rejected this 24613eaec4bSDarrick J. Wong * combination before. Returns true to update the sb, false otherwise. 24713eaec4bSDarrick J. Wong */ 24813eaec4bSDarrick J. Wong static inline int 24913eaec4bSDarrick J. Wong xfs_check_new_dalign( 25013eaec4bSDarrick J. Wong struct xfs_mount *mp, 25113eaec4bSDarrick J. Wong int new_dalign, 25213eaec4bSDarrick J. Wong bool *update_sb) 25313eaec4bSDarrick J. Wong { 25413eaec4bSDarrick J. Wong struct xfs_sb *sbp = &mp->m_sb; 25513eaec4bSDarrick J. Wong xfs_ino_t calc_ino; 25613eaec4bSDarrick J. Wong 25713eaec4bSDarrick J. Wong calc_ino = xfs_ialloc_calc_rootino(mp, new_dalign); 25813eaec4bSDarrick J. Wong trace_xfs_check_new_dalign(mp, new_dalign, calc_ino); 25913eaec4bSDarrick J. Wong 26013eaec4bSDarrick J. Wong if (sbp->sb_rootino == calc_ino) { 26113eaec4bSDarrick J. Wong *update_sb = true; 26213eaec4bSDarrick J. Wong return 0; 26313eaec4bSDarrick J. Wong } 26413eaec4bSDarrick J. Wong 26513eaec4bSDarrick J. Wong xfs_warn(mp, 26613eaec4bSDarrick J. Wong "Cannot change stripe alignment; would require moving root inode."); 26713eaec4bSDarrick J. Wong 26813eaec4bSDarrick J. Wong /* 26913eaec4bSDarrick J. Wong * XXX: Next time we add a new incompat feature, this should start 27013eaec4bSDarrick J. Wong * returning -EINVAL to fail the mount. Until then, spit out a warning 27113eaec4bSDarrick J. Wong * that we're ignoring the administrator's instructions. 27213eaec4bSDarrick J. Wong */ 27313eaec4bSDarrick J. Wong xfs_warn(mp, "Skipping superblock stripe alignment update."); 27413eaec4bSDarrick J. Wong *update_sb = false; 27513eaec4bSDarrick J. Wong return 0; 27613eaec4bSDarrick J. Wong } 27713eaec4bSDarrick J. Wong 27813eaec4bSDarrick J. Wong /* 2794f5b1b3aSDarrick J. Wong * If we were provided with new sunit/swidth values as mount options, make sure 2804f5b1b3aSDarrick J. Wong * that they pass basic alignment and superblock feature checks, and convert 2814f5b1b3aSDarrick J. Wong * them into the same units (FSB) that everything else expects. This step 2824f5b1b3aSDarrick J. Wong * /must/ be done before computing the inode geometry. 2831da177e4SLinus Torvalds */ 2840771fb45SEric Sandeen STATIC int 2854f5b1b3aSDarrick J. Wong xfs_validate_new_dalign( 2864f5b1b3aSDarrick J. Wong struct xfs_mount *mp) 2871da177e4SLinus Torvalds { 2884f5b1b3aSDarrick J. Wong if (mp->m_dalign == 0) 2894f5b1b3aSDarrick J. Wong return 0; 2901da177e4SLinus Torvalds 2911da177e4SLinus Torvalds /* 2921da177e4SLinus Torvalds * If stripe unit and stripe width are not multiples 2931da177e4SLinus Torvalds * of the fs blocksize turn off alignment. 2941da177e4SLinus Torvalds */ 2951da177e4SLinus Torvalds if ((BBTOB(mp->m_dalign) & mp->m_blockmask) || 2961da177e4SLinus Torvalds (BBTOB(mp->m_swidth) & mp->m_blockmask)) { 29739a45d84SJie Liu xfs_warn(mp, 29839a45d84SJie Liu "alignment check failed: sunit/swidth vs. blocksize(%d)", 2994f5b1b3aSDarrick J. Wong mp->m_sb.sb_blocksize); 3002451337dSDave Chinner return -EINVAL; 3011da177e4SLinus Torvalds } else { 3021da177e4SLinus Torvalds /* 3031da177e4SLinus Torvalds * Convert the stripe unit and width to FSBs. 3041da177e4SLinus Torvalds */ 3051da177e4SLinus Torvalds mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign); 3064f5b1b3aSDarrick J. Wong if (mp->m_dalign && (mp->m_sb.sb_agblocks % mp->m_dalign)) { 30753487786SDave Chinner xfs_warn(mp, 30839a45d84SJie Liu "alignment check failed: sunit/swidth vs. agsize(%d)", 3094f5b1b3aSDarrick J. Wong mp->m_sb.sb_agblocks); 3102451337dSDave Chinner return -EINVAL; 3111da177e4SLinus Torvalds } else if (mp->m_dalign) { 3121da177e4SLinus Torvalds mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth); 3131da177e4SLinus Torvalds } else { 31439a45d84SJie Liu xfs_warn(mp, 31539a45d84SJie Liu "alignment check failed: sunit(%d) less than bsize(%d)", 3164f5b1b3aSDarrick J. Wong mp->m_dalign, mp->m_sb.sb_blocksize); 3172451337dSDave Chinner return -EINVAL; 3181da177e4SLinus Torvalds } 3191da177e4SLinus Torvalds } 3201da177e4SLinus Torvalds 3214f5b1b3aSDarrick J. Wong if (!xfs_sb_version_hasdalign(&mp->m_sb)) { 32234d7f603SJie Liu xfs_warn(mp, 32334d7f603SJie Liu "cannot change alignment: superblock does not support data alignment"); 3242451337dSDave Chinner return -EINVAL; 3251da177e4SLinus Torvalds } 3264f5b1b3aSDarrick J. Wong 3274f5b1b3aSDarrick J. Wong return 0; 3284f5b1b3aSDarrick J. Wong } 3294f5b1b3aSDarrick J. Wong 3304f5b1b3aSDarrick J. Wong /* Update alignment values based on mount options and sb values. */ 3314f5b1b3aSDarrick J. Wong STATIC int 3324f5b1b3aSDarrick J. Wong xfs_update_alignment( 3334f5b1b3aSDarrick J. Wong struct xfs_mount *mp) 3344f5b1b3aSDarrick J. Wong { 3354f5b1b3aSDarrick J. Wong struct xfs_sb *sbp = &mp->m_sb; 3364f5b1b3aSDarrick J. Wong 3374f5b1b3aSDarrick J. Wong if (mp->m_dalign) { 33813eaec4bSDarrick J. Wong bool update_sb; 33913eaec4bSDarrick J. Wong int error; 34013eaec4bSDarrick J. Wong 3414f5b1b3aSDarrick J. Wong if (sbp->sb_unit == mp->m_dalign && 3424f5b1b3aSDarrick J. Wong sbp->sb_width == mp->m_swidth) 3434f5b1b3aSDarrick J. Wong return 0; 3444f5b1b3aSDarrick J. Wong 34513eaec4bSDarrick J. Wong error = xfs_check_new_dalign(mp, mp->m_dalign, &update_sb); 34613eaec4bSDarrick J. Wong if (error || !update_sb) 34713eaec4bSDarrick J. Wong return error; 34813eaec4bSDarrick J. Wong 3494f5b1b3aSDarrick J. Wong sbp->sb_unit = mp->m_dalign; 3504f5b1b3aSDarrick J. Wong sbp->sb_width = mp->m_swidth; 3514f5b1b3aSDarrick J. Wong mp->m_update_sb = true; 3521da177e4SLinus Torvalds } else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN && 35362118709SEric Sandeen xfs_sb_version_hasdalign(&mp->m_sb)) { 3541da177e4SLinus Torvalds mp->m_dalign = sbp->sb_unit; 3551da177e4SLinus Torvalds mp->m_swidth = sbp->sb_width; 3561da177e4SLinus Torvalds } 3571da177e4SLinus Torvalds 3580771fb45SEric Sandeen return 0; 3590771fb45SEric Sandeen } 3601da177e4SLinus Torvalds 3610771fb45SEric Sandeen /* 362055388a3SDave Chinner * precalculate the low space thresholds for dynamic speculative preallocation. 363055388a3SDave Chinner */ 364055388a3SDave Chinner void 365055388a3SDave Chinner xfs_set_low_space_thresholds( 366055388a3SDave Chinner struct xfs_mount *mp) 367055388a3SDave Chinner { 36865f03d86SDarrick J. Wong uint64_t dblocks = mp->m_sb.sb_dblocks; 36965f03d86SDarrick J. Wong uint64_t rtexts = mp->m_sb.sb_rextents; 370055388a3SDave Chinner int i; 371055388a3SDave Chinner 37265f03d86SDarrick J. Wong do_div(dblocks, 100); 37365f03d86SDarrick J. Wong do_div(rtexts, 100); 374055388a3SDave Chinner 37565f03d86SDarrick J. Wong for (i = 0; i < XFS_LOWSP_MAX; i++) { 37665f03d86SDarrick J. Wong mp->m_low_space[i] = dblocks * (i + 1); 37765f03d86SDarrick J. Wong mp->m_low_rtexts[i] = rtexts * (i + 1); 378055388a3SDave Chinner } 379055388a3SDave Chinner } 380055388a3SDave Chinner 3811da177e4SLinus Torvalds /* 3820471f62eSZhi Yong Wu * Check that the data (and log if separate) is an ok size. 3831da177e4SLinus Torvalds */ 3840771fb45SEric Sandeen STATIC int 385ba372674SDave Chinner xfs_check_sizes( 386ba372674SDave Chinner struct xfs_mount *mp) 3870771fb45SEric Sandeen { 388ba372674SDave Chinner struct xfs_buf *bp; 3890771fb45SEric Sandeen xfs_daddr_t d; 390ba372674SDave Chinner int error; 3910771fb45SEric Sandeen 3921da177e4SLinus Torvalds d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks); 3931da177e4SLinus Torvalds if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) { 3940b932cccSDave Chinner xfs_warn(mp, "filesystem size mismatch detected"); 3952451337dSDave Chinner return -EFBIG; 3961da177e4SLinus Torvalds } 397ba372674SDave Chinner error = xfs_buf_read_uncached(mp->m_ddev_targp, 3981da177e4SLinus Torvalds d - XFS_FSS_TO_BB(mp, 1), 399ba372674SDave Chinner XFS_FSS_TO_BB(mp, 1), 0, &bp, NULL); 400ba372674SDave Chinner if (error) { 4010b932cccSDave Chinner xfs_warn(mp, "last sector read failed"); 402ba372674SDave Chinner return error; 4031da177e4SLinus Torvalds } 4041922c949SDave Chinner xfs_buf_relse(bp); 4051da177e4SLinus Torvalds 406ba372674SDave Chinner if (mp->m_logdev_targp == mp->m_ddev_targp) 407ba372674SDave Chinner return 0; 408ba372674SDave Chinner 4091da177e4SLinus Torvalds d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks); 4101da177e4SLinus Torvalds if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) { 4110b932cccSDave Chinner xfs_warn(mp, "log size mismatch detected"); 4122451337dSDave Chinner return -EFBIG; 4131da177e4SLinus Torvalds } 414ba372674SDave Chinner error = xfs_buf_read_uncached(mp->m_logdev_targp, 4151da177e4SLinus Torvalds d - XFS_FSB_TO_BB(mp, 1), 416ba372674SDave Chinner XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL); 417ba372674SDave Chinner if (error) { 4180b932cccSDave Chinner xfs_warn(mp, "log device read failed"); 419ba372674SDave Chinner return error; 4201da177e4SLinus Torvalds } 4211922c949SDave Chinner xfs_buf_relse(bp); 4220771fb45SEric Sandeen return 0; 4230771fb45SEric Sandeen } 4240771fb45SEric Sandeen 4250771fb45SEric Sandeen /* 4267d095257SChristoph Hellwig * Clear the quotaflags in memory and in the superblock. 4277d095257SChristoph Hellwig */ 4287d095257SChristoph Hellwig int 4297d095257SChristoph Hellwig xfs_mount_reset_sbqflags( 4307d095257SChristoph Hellwig struct xfs_mount *mp) 4317d095257SChristoph Hellwig { 4327d095257SChristoph Hellwig mp->m_qflags = 0; 4337d095257SChristoph Hellwig 43461e63ecbSDave Chinner /* It is OK to look at sb_qflags in the mount path without m_sb_lock. */ 4357d095257SChristoph Hellwig if (mp->m_sb.sb_qflags == 0) 4367d095257SChristoph Hellwig return 0; 4377d095257SChristoph Hellwig spin_lock(&mp->m_sb_lock); 4387d095257SChristoph Hellwig mp->m_sb.sb_qflags = 0; 4397d095257SChristoph Hellwig spin_unlock(&mp->m_sb_lock); 4407d095257SChristoph Hellwig 44161e63ecbSDave Chinner if (!xfs_fs_writable(mp, SB_FREEZE_WRITE)) 4427d095257SChristoph Hellwig return 0; 4437d095257SChristoph Hellwig 44461e63ecbSDave Chinner return xfs_sync_sb(mp, false); 4457d095257SChristoph Hellwig } 4467d095257SChristoph Hellwig 447c8ce540dSDarrick J. Wong uint64_t 448d5db0f97SEric Sandeen xfs_default_resblks(xfs_mount_t *mp) 449d5db0f97SEric Sandeen { 450c8ce540dSDarrick J. Wong uint64_t resblks; 451d5db0f97SEric Sandeen 452d5db0f97SEric Sandeen /* 4538babd8a2SDave Chinner * We default to 5% or 8192 fsbs of space reserved, whichever is 4548babd8a2SDave Chinner * smaller. This is intended to cover concurrent allocation 4558babd8a2SDave Chinner * transactions when we initially hit enospc. These each require a 4 4568babd8a2SDave Chinner * block reservation. Hence by default we cover roughly 2000 concurrent 4578babd8a2SDave Chinner * allocation reservations. 458d5db0f97SEric Sandeen */ 459d5db0f97SEric Sandeen resblks = mp->m_sb.sb_dblocks; 460d5db0f97SEric Sandeen do_div(resblks, 20); 461c8ce540dSDarrick J. Wong resblks = min_t(uint64_t, resblks, 8192); 462d5db0f97SEric Sandeen return resblks; 463d5db0f97SEric Sandeen } 464d5db0f97SEric Sandeen 4652e9e6481SDarrick J. Wong /* Ensure the summary counts are correct. */ 4662e9e6481SDarrick J. Wong STATIC int 4672e9e6481SDarrick J. Wong xfs_check_summary_counts( 4682e9e6481SDarrick J. Wong struct xfs_mount *mp) 4692e9e6481SDarrick J. Wong { 4702e9e6481SDarrick J. Wong /* 4712e9e6481SDarrick J. Wong * The AG0 superblock verifier rejects in-progress filesystems, 4722e9e6481SDarrick J. Wong * so we should never see the flag set this far into mounting. 4732e9e6481SDarrick J. Wong */ 4742e9e6481SDarrick J. Wong if (mp->m_sb.sb_inprogress) { 4752e9e6481SDarrick J. Wong xfs_err(mp, "sb_inprogress set after log recovery??"); 4762e9e6481SDarrick J. Wong WARN_ON(1); 4772e9e6481SDarrick J. Wong return -EFSCORRUPTED; 4782e9e6481SDarrick J. Wong } 4792e9e6481SDarrick J. Wong 4802e9e6481SDarrick J. Wong /* 4812e9e6481SDarrick J. Wong * Now the log is mounted, we know if it was an unclean shutdown or 4822e9e6481SDarrick J. Wong * not. If it was, with the first phase of recovery has completed, we 4832e9e6481SDarrick J. Wong * have consistent AG blocks on disk. We have not recovered EFIs yet, 4842e9e6481SDarrick J. Wong * but they are recovered transactionally in the second recovery phase 4852e9e6481SDarrick J. Wong * later. 4862e9e6481SDarrick J. Wong * 4872e9e6481SDarrick J. Wong * If the log was clean when we mounted, we can check the summary 4882e9e6481SDarrick J. Wong * counters. If any of them are obviously incorrect, we can recompute 4892e9e6481SDarrick J. Wong * them from the AGF headers in the next step. 4902e9e6481SDarrick J. Wong */ 4912e9e6481SDarrick J. Wong if (XFS_LAST_UNMOUNT_WAS_CLEAN(mp) && 4922e9e6481SDarrick J. Wong (mp->m_sb.sb_fdblocks > mp->m_sb.sb_dblocks || 49300d22a1cSDarrick J. Wong !xfs_verify_icount(mp, mp->m_sb.sb_icount) || 4942e9e6481SDarrick J. Wong mp->m_sb.sb_ifree > mp->m_sb.sb_icount)) 49539353ff6SDarrick J. Wong xfs_fs_mark_sick(mp, XFS_SICK_FS_COUNTERS); 4962e9e6481SDarrick J. Wong 4972e9e6481SDarrick J. Wong /* 4982e9e6481SDarrick J. Wong * We can safely re-initialise incore superblock counters from the 4992e9e6481SDarrick J. Wong * per-ag data. These may not be correct if the filesystem was not 5002e9e6481SDarrick J. Wong * cleanly unmounted, so we waited for recovery to finish before doing 5012e9e6481SDarrick J. Wong * this. 5022e9e6481SDarrick J. Wong * 5032e9e6481SDarrick J. Wong * If the filesystem was cleanly unmounted or the previous check did 5042e9e6481SDarrick J. Wong * not flag anything weird, then we can trust the values in the 5052e9e6481SDarrick J. Wong * superblock to be correct and we don't need to do anything here. 5062e9e6481SDarrick J. Wong * Otherwise, recalculate the summary counters. 5072e9e6481SDarrick J. Wong */ 5082e9e6481SDarrick J. Wong if ((!xfs_sb_version_haslazysbcount(&mp->m_sb) || 5092e9e6481SDarrick J. Wong XFS_LAST_UNMOUNT_WAS_CLEAN(mp)) && 51039353ff6SDarrick J. Wong !xfs_fs_has_sickness(mp, XFS_SICK_FS_COUNTERS)) 5112e9e6481SDarrick J. Wong return 0; 5122e9e6481SDarrick J. Wong 5132e9e6481SDarrick J. Wong return xfs_initialize_perag_data(mp, mp->m_sb.sb_agcount); 5142e9e6481SDarrick J. Wong } 5152e9e6481SDarrick J. Wong 5167d095257SChristoph Hellwig /* 517d336f7ebSDarrick J. Wong * Flush and reclaim dirty inodes in preparation for unmount. Inodes and 518d336f7ebSDarrick J. Wong * internal inode structures can be sitting in the CIL and AIL at this point, 519d336f7ebSDarrick J. Wong * so we need to unpin them, write them back and/or reclaim them before unmount 520ab23a776SDave Chinner * can proceed. In other words, callers are required to have inactivated all 521ab23a776SDave Chinner * inodes. 522d336f7ebSDarrick J. Wong * 523d336f7ebSDarrick J. Wong * An inode cluster that has been freed can have its buffer still pinned in 524d336f7ebSDarrick J. Wong * memory because the transaction is still sitting in a iclog. The stale inodes 525d336f7ebSDarrick J. Wong * on that buffer will be pinned to the buffer until the transaction hits the 526d336f7ebSDarrick J. Wong * disk and the callbacks run. Pushing the AIL will skip the stale inodes and 527d336f7ebSDarrick J. Wong * may never see the pinned buffer, so nothing will push out the iclog and 528d336f7ebSDarrick J. Wong * unpin the buffer. 529d336f7ebSDarrick J. Wong * 530d336f7ebSDarrick J. Wong * Hence we need to force the log to unpin everything first. However, log 531d336f7ebSDarrick J. Wong * forces don't wait for the discards they issue to complete, so we have to 532d336f7ebSDarrick J. Wong * explicitly wait for them to complete here as well. 533d336f7ebSDarrick J. Wong * 534d336f7ebSDarrick J. Wong * Then we can tell the world we are unmounting so that error handling knows 535d336f7ebSDarrick J. Wong * that the filesystem is going away and we should error out anything that we 536d336f7ebSDarrick J. Wong * have been retrying in the background. This will prevent never-ending 537d336f7ebSDarrick J. Wong * retries in AIL pushing from hanging the unmount. 538d336f7ebSDarrick J. Wong * 539d336f7ebSDarrick J. Wong * Finally, we can push the AIL to clean all the remaining dirty objects, then 540d336f7ebSDarrick J. Wong * reclaim the remaining inodes that are still in memory at this point in time. 541d336f7ebSDarrick J. Wong */ 542d336f7ebSDarrick J. Wong static void 543d336f7ebSDarrick J. Wong xfs_unmount_flush_inodes( 544d336f7ebSDarrick J. Wong struct xfs_mount *mp) 545d336f7ebSDarrick J. Wong { 546d336f7ebSDarrick J. Wong xfs_log_force(mp, XFS_LOG_SYNC); 547d336f7ebSDarrick J. Wong xfs_extent_busy_wait_all(mp); 548d336f7ebSDarrick J. Wong flush_workqueue(xfs_discard_wq); 549d336f7ebSDarrick J. Wong 550d336f7ebSDarrick J. Wong mp->m_flags |= XFS_MOUNT_UNMOUNTING; 551d336f7ebSDarrick J. Wong 552d336f7ebSDarrick J. Wong xfs_ail_push_all_sync(mp->m_ail); 553ab23a776SDave Chinner xfs_inodegc_stop(mp); 554d336f7ebSDarrick J. Wong cancel_delayed_work_sync(&mp->m_reclaim_work); 555d336f7ebSDarrick J. Wong xfs_reclaim_inodes(mp); 556d336f7ebSDarrick J. Wong xfs_health_unmount(mp); 557d336f7ebSDarrick J. Wong } 558d336f7ebSDarrick J. Wong 559b2941046SDave Chinner static void 560b2941046SDave Chinner xfs_mount_setup_inode_geom( 561b2941046SDave Chinner struct xfs_mount *mp) 562b2941046SDave Chinner { 563b2941046SDave Chinner struct xfs_ino_geometry *igeo = M_IGEO(mp); 564b2941046SDave Chinner 565b2941046SDave Chinner igeo->attr_fork_offset = xfs_bmap_compute_attr_offset(mp); 566b2941046SDave Chinner ASSERT(igeo->attr_fork_offset < XFS_LITINO(mp)); 567b2941046SDave Chinner 568b2941046SDave Chinner xfs_ialloc_setup_geometry(mp); 569b2941046SDave Chinner } 570b2941046SDave Chinner 571d336f7ebSDarrick J. Wong /* 5720771fb45SEric Sandeen * This function does the following on an initial mount of a file system: 5730771fb45SEric Sandeen * - reads the superblock from disk and init the mount struct 5740771fb45SEric Sandeen * - if we're a 32-bit kernel, do a size check on the superblock 5750771fb45SEric Sandeen * so we don't mount terabyte filesystems 5760771fb45SEric Sandeen * - init mount struct realtime fields 5770771fb45SEric Sandeen * - allocate inode hash table for fs 5780771fb45SEric Sandeen * - init directory manager 5790771fb45SEric Sandeen * - perform recovery and init the log manager 5800771fb45SEric Sandeen */ 5810771fb45SEric Sandeen int 5820771fb45SEric Sandeen xfs_mountfs( 583f0b2efadSBrian Foster struct xfs_mount *mp) 5840771fb45SEric Sandeen { 585f0b2efadSBrian Foster struct xfs_sb *sbp = &(mp->m_sb); 586f0b2efadSBrian Foster struct xfs_inode *rip; 587ef325959SDarrick J. Wong struct xfs_ino_geometry *igeo = M_IGEO(mp); 588c8ce540dSDarrick J. Wong uint64_t resblks; 5897d095257SChristoph Hellwig uint quotamount = 0; 5907d095257SChristoph Hellwig uint quotaflags = 0; 5910771fb45SEric Sandeen int error = 0; 5920771fb45SEric Sandeen 593ff55068cSDave Chinner xfs_sb_mount_common(mp, sbp); 5940771fb45SEric Sandeen 5950771fb45SEric Sandeen /* 596074e427bSDave Chinner * Check for a mismatched features2 values. Older kernels read & wrote 597074e427bSDave Chinner * into the wrong sb offset for sb_features2 on some platforms due to 598074e427bSDave Chinner * xfs_sb_t not being 64bit size aligned when sb_features2 was added, 599074e427bSDave Chinner * which made older superblock reading/writing routines swap it as a 600074e427bSDave Chinner * 64-bit value. 601ee1c0908SDavid Chinner * 602e6957ea4SEric Sandeen * For backwards compatibility, we make both slots equal. 603e6957ea4SEric Sandeen * 604074e427bSDave Chinner * If we detect a mismatched field, we OR the set bits into the existing 605074e427bSDave Chinner * features2 field in case it has already been modified; we don't want 606074e427bSDave Chinner * to lose any features. We then update the bad location with the ORed 607074e427bSDave Chinner * value so that older kernels will see any features2 flags. The 608074e427bSDave Chinner * superblock writeback code ensures the new sb_features2 is copied to 609074e427bSDave Chinner * sb_bad_features2 before it is logged or written to disk. 610ee1c0908SDavid Chinner */ 611e6957ea4SEric Sandeen if (xfs_sb_has_mismatched_features2(sbp)) { 6120b932cccSDave Chinner xfs_warn(mp, "correcting sb_features alignment problem"); 613ee1c0908SDavid Chinner sbp->sb_features2 |= sbp->sb_bad_features2; 61461e63ecbSDave Chinner mp->m_update_sb = true; 615e6957ea4SEric Sandeen 616e6957ea4SEric Sandeen /* 617e6957ea4SEric Sandeen * Re-check for ATTR2 in case it was found in bad_features2 618e6957ea4SEric Sandeen * slot. 619e6957ea4SEric Sandeen */ 6207c12f296STim Shimmin if (xfs_sb_version_hasattr2(&mp->m_sb) && 6217c12f296STim Shimmin !(mp->m_flags & XFS_MOUNT_NOATTR2)) 622e6957ea4SEric Sandeen mp->m_flags |= XFS_MOUNT_ATTR2; 6237c12f296STim Shimmin } 624e6957ea4SEric Sandeen 6257c12f296STim Shimmin if (xfs_sb_version_hasattr2(&mp->m_sb) && 6267c12f296STim Shimmin (mp->m_flags & XFS_MOUNT_NOATTR2)) { 6277c12f296STim Shimmin xfs_sb_version_removeattr2(&mp->m_sb); 62861e63ecbSDave Chinner mp->m_update_sb = true; 6297c12f296STim Shimmin 6307c12f296STim Shimmin /* update sb_versionnum for the clearing of the morebits */ 6317c12f296STim Shimmin if (!sbp->sb_features2) 63261e63ecbSDave Chinner mp->m_update_sb = true; 633ee1c0908SDavid Chinner } 634ee1c0908SDavid Chinner 635263997a6SDave Chinner /* always use v2 inodes by default now */ 636263997a6SDave Chinner if (!(mp->m_sb.sb_versionnum & XFS_SB_VERSION_NLINKBIT)) { 637263997a6SDave Chinner mp->m_sb.sb_versionnum |= XFS_SB_VERSION_NLINKBIT; 63861e63ecbSDave Chinner mp->m_update_sb = true; 639263997a6SDave Chinner } 640263997a6SDave Chinner 641ee1c0908SDavid Chinner /* 6424f5b1b3aSDarrick J. Wong * If we were given new sunit/swidth options, do some basic validation 6434f5b1b3aSDarrick J. Wong * checks and convert the incore dalign and swidth values to the 6444f5b1b3aSDarrick J. Wong * same units (FSB) that everything else uses. This /must/ happen 6454f5b1b3aSDarrick J. Wong * before computing the inode geometry. 6460771fb45SEric Sandeen */ 6474f5b1b3aSDarrick J. Wong error = xfs_validate_new_dalign(mp); 6480771fb45SEric Sandeen if (error) 649f9057e3dSChristoph Hellwig goto out; 6500771fb45SEric Sandeen 6510771fb45SEric Sandeen xfs_alloc_compute_maxlevels(mp); 6520771fb45SEric Sandeen xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK); 6530771fb45SEric Sandeen xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK); 654b2941046SDave Chinner xfs_mount_setup_inode_geom(mp); 655035e00acSDarrick J. Wong xfs_rmapbt_compute_maxlevels(mp); 6561946b91cSDarrick J. Wong xfs_refcountbt_compute_maxlevels(mp); 6570771fb45SEric Sandeen 6584f5b1b3aSDarrick J. Wong /* 6594f5b1b3aSDarrick J. Wong * Check if sb_agblocks is aligned at stripe boundary. If sb_agblocks 6604f5b1b3aSDarrick J. Wong * is NOT aligned turn off m_dalign since allocator alignment is within 6614f5b1b3aSDarrick J. Wong * an ag, therefore ag has to be aligned at stripe boundary. Note that 6624f5b1b3aSDarrick J. Wong * we must compute the free space and rmap btree geometry before doing 6634f5b1b3aSDarrick J. Wong * this. 6644f5b1b3aSDarrick J. Wong */ 6654f5b1b3aSDarrick J. Wong error = xfs_update_alignment(mp); 6664f5b1b3aSDarrick J. Wong if (error) 6674f5b1b3aSDarrick J. Wong goto out; 6684f5b1b3aSDarrick J. Wong 669e6b3bb78SCarlos Maiolino /* enable fail_at_unmount as default */ 670749f24f3SThomas Meyer mp->m_fail_unmount = true; 671e6b3bb78SCarlos Maiolino 672e1d3d218SIan Kent error = xfs_sysfs_init(&mp->m_kobj, &xfs_mp_ktype, 673e1d3d218SIan Kent NULL, mp->m_super->s_id); 67427174203SChristoph Hellwig if (error) 675f9057e3dSChristoph Hellwig goto out; 6761da177e4SLinus Torvalds 677225e4635SBill O'Donnell error = xfs_sysfs_init(&mp->m_stats.xs_kobj, &xfs_stats_ktype, 678225e4635SBill O'Donnell &mp->m_kobj, "stats"); 679a31b1d3dSBrian Foster if (error) 680a31b1d3dSBrian Foster goto out_remove_sysfs; 681a31b1d3dSBrian Foster 682192852beSCarlos Maiolino error = xfs_error_sysfs_init(mp); 683225e4635SBill O'Donnell if (error) 684225e4635SBill O'Donnell goto out_del_stats; 685225e4635SBill O'Donnell 68631965ef3SDarrick J. Wong error = xfs_errortag_init(mp); 68731965ef3SDarrick J. Wong if (error) 68831965ef3SDarrick J. Wong goto out_remove_error_sysfs; 689192852beSCarlos Maiolino 690192852beSCarlos Maiolino error = xfs_uuid_mount(mp); 691192852beSCarlos Maiolino if (error) 69231965ef3SDarrick J. Wong goto out_remove_errortag; 693192852beSCarlos Maiolino 6941da177e4SLinus Torvalds /* 6952fcddee8SChristoph Hellwig * Update the preferred write size based on the information from the 6962fcddee8SChristoph Hellwig * on-disk superblock. 6970771fb45SEric Sandeen */ 6982fcddee8SChristoph Hellwig mp->m_allocsize_log = 6992fcddee8SChristoph Hellwig max_t(uint32_t, sbp->sb_blocklog, mp->m_allocsize_log); 7002fcddee8SChristoph Hellwig mp->m_allocsize_blocks = 1U << (mp->m_allocsize_log - sbp->sb_blocklog); 7010771fb45SEric Sandeen 702055388a3SDave Chinner /* set the low space thresholds for dynamic preallocation */ 703055388a3SDave Chinner xfs_set_low_space_thresholds(mp); 704055388a3SDave Chinner 7050771fb45SEric Sandeen /* 706e5376fc1SBrian Foster * If enabled, sparse inode chunk alignment is expected to match the 707e5376fc1SBrian Foster * cluster size. Full inode chunk alignment must match the chunk size, 708e5376fc1SBrian Foster * but that is checked on sb read verification... 709e5376fc1SBrian Foster */ 710e5376fc1SBrian Foster if (xfs_sb_version_hassparseinodes(&mp->m_sb) && 711e5376fc1SBrian Foster mp->m_sb.sb_spino_align != 712490d451fSDarrick J. Wong XFS_B_TO_FSBT(mp, igeo->inode_cluster_size_raw)) { 713e5376fc1SBrian Foster xfs_warn(mp, 714e5376fc1SBrian Foster "Sparse inode block alignment (%u) must match cluster size (%llu).", 715e5376fc1SBrian Foster mp->m_sb.sb_spino_align, 716490d451fSDarrick J. Wong XFS_B_TO_FSBT(mp, igeo->inode_cluster_size_raw)); 717e5376fc1SBrian Foster error = -EINVAL; 718e5376fc1SBrian Foster goto out_remove_uuid; 719e5376fc1SBrian Foster } 720e5376fc1SBrian Foster 721e5376fc1SBrian Foster /* 722c2bfbc9bSZhi Yong Wu * Check that the data (and log if separate) is an ok size. 7230771fb45SEric Sandeen */ 7244249023aSChristoph Hellwig error = xfs_check_sizes(mp); 7250771fb45SEric Sandeen if (error) 726f9057e3dSChristoph Hellwig goto out_remove_uuid; 7270771fb45SEric Sandeen 7280771fb45SEric Sandeen /* 7291da177e4SLinus Torvalds * Initialize realtime fields in the mount structure 7301da177e4SLinus Torvalds */ 7310771fb45SEric Sandeen error = xfs_rtmount_init(mp); 7320771fb45SEric Sandeen if (error) { 7330b932cccSDave Chinner xfs_warn(mp, "RT mount failed"); 734f9057e3dSChristoph Hellwig goto out_remove_uuid; 7351da177e4SLinus Torvalds } 7361da177e4SLinus Torvalds 7371da177e4SLinus Torvalds /* 7381da177e4SLinus Torvalds * Copies the low order bits of the timestamp and the randomly 7391da177e4SLinus Torvalds * set "sequence" number out of a UUID. 7401da177e4SLinus Torvalds */ 741cb0ba6ccSChristoph Hellwig mp->m_fixedfsid[0] = 742cb0ba6ccSChristoph Hellwig (get_unaligned_be16(&sbp->sb_uuid.b[8]) << 16) | 743cb0ba6ccSChristoph Hellwig get_unaligned_be16(&sbp->sb_uuid.b[4]); 744cb0ba6ccSChristoph Hellwig mp->m_fixedfsid[1] = get_unaligned_be32(&sbp->sb_uuid.b[0]); 7451da177e4SLinus Torvalds 7460650b554SDave Chinner error = xfs_da_mount(mp); 7470650b554SDave Chinner if (error) { 7480650b554SDave Chinner xfs_warn(mp, "Failed dir/attr init: %d", error); 7490650b554SDave Chinner goto out_remove_uuid; 7500650b554SDave Chinner } 7511da177e4SLinus Torvalds 7521da177e4SLinus Torvalds /* 7531da177e4SLinus Torvalds * Initialize the precomputed transaction reservations values. 7541da177e4SLinus Torvalds */ 7551da177e4SLinus Torvalds xfs_trans_init(mp); 7561da177e4SLinus Torvalds 7571da177e4SLinus Torvalds /* 7581da177e4SLinus Torvalds * Allocate and initialize the per-ag data. 7591da177e4SLinus Torvalds */ 7601c1c6ebcSDave Chinner error = xfs_initialize_perag(mp, sbp->sb_agcount, &mp->m_maxagi); 7611c1c6ebcSDave Chinner if (error) { 7620b932cccSDave Chinner xfs_warn(mp, "Failed per-ag init: %d", error); 7630650b554SDave Chinner goto out_free_dir; 7641c1c6ebcSDave Chinner } 7651da177e4SLinus Torvalds 766a71895c5SDarrick J. Wong if (XFS_IS_CORRUPT(mp, !sbp->sb_logblocks)) { 7670b932cccSDave Chinner xfs_warn(mp, "no log defined"); 7682451337dSDave Chinner error = -EFSCORRUPTED; 769f9057e3dSChristoph Hellwig goto out_free_perag; 770f9057e3dSChristoph Hellwig } 771f9057e3dSChristoph Hellwig 77240b1de00SDarrick J. Wong error = xfs_inodegc_register_shrinker(mp); 77340b1de00SDarrick J. Wong if (error) 77440b1de00SDarrick J. Wong goto out_fail_wait; 77540b1de00SDarrick J. Wong 7761da177e4SLinus Torvalds /* 777f0b2efadSBrian Foster * Log's mount-time initialization. The first part of recovery can place 778f0b2efadSBrian Foster * some items on the AIL, to be handled when recovery is finished or 779f0b2efadSBrian Foster * cancelled. 7801da177e4SLinus Torvalds */ 7811da177e4SLinus Torvalds error = xfs_log_mount(mp, mp->m_logdev_targp, 7821da177e4SLinus Torvalds XFS_FSB_TO_DADDR(mp, sbp->sb_logstart), 7831da177e4SLinus Torvalds XFS_FSB_TO_BB(mp, sbp->sb_logblocks)); 7841da177e4SLinus Torvalds if (error) { 7850b932cccSDave Chinner xfs_warn(mp, "log mount failed"); 78640b1de00SDarrick J. Wong goto out_inodegc_shrinker; 7871da177e4SLinus Torvalds } 7881da177e4SLinus Torvalds 7892e9e6481SDarrick J. Wong /* Make sure the summary counts are ok. */ 7902e9e6481SDarrick J. Wong error = xfs_check_summary_counts(mp); 791f9057e3dSChristoph Hellwig if (error) 7926eee8972Skbuild test robot goto out_log_dealloc; 793f9057e3dSChristoph Hellwig 794ab23a776SDave Chinner /* Enable background inode inactivation workers. */ 795ab23a776SDave Chinner xfs_inodegc_start(mp); 7966f649091SDarrick J. Wong xfs_blockgc_start(mp); 797ab23a776SDave Chinner 79892821e2bSDavid Chinner /* 7991da177e4SLinus Torvalds * Get and sanity-check the root inode. 8001da177e4SLinus Torvalds * Save the pointer to it in the mount structure. 8011da177e4SLinus Torvalds */ 802541b5accSDave Chinner error = xfs_iget(mp, NULL, sbp->sb_rootino, XFS_IGET_UNTRUSTED, 803541b5accSDave Chinner XFS_ILOCK_EXCL, &rip); 8041da177e4SLinus Torvalds if (error) { 805541b5accSDave Chinner xfs_warn(mp, 806541b5accSDave Chinner "Failed to read root inode 0x%llx, error %d", 807541b5accSDave Chinner sbp->sb_rootino, -error); 808f9057e3dSChristoph Hellwig goto out_log_dealloc; 8091da177e4SLinus Torvalds } 8101da177e4SLinus Torvalds 8111da177e4SLinus Torvalds ASSERT(rip != NULL); 8121da177e4SLinus Torvalds 813a71895c5SDarrick J. Wong if (XFS_IS_CORRUPT(mp, !S_ISDIR(VFS_I(rip)->i_mode))) { 8140b932cccSDave Chinner xfs_warn(mp, "corrupted root inode %llu: not a directory", 815b6574520SNathan Scott (unsigned long long)rip->i_ino); 8161da177e4SLinus Torvalds xfs_iunlock(rip, XFS_ILOCK_EXCL); 8172451337dSDave Chinner error = -EFSCORRUPTED; 818f9057e3dSChristoph Hellwig goto out_rele_rip; 8191da177e4SLinus Torvalds } 8201da177e4SLinus Torvalds mp->m_rootip = rip; /* save it */ 8211da177e4SLinus Torvalds 8221da177e4SLinus Torvalds xfs_iunlock(rip, XFS_ILOCK_EXCL); 8231da177e4SLinus Torvalds 8241da177e4SLinus Torvalds /* 8251da177e4SLinus Torvalds * Initialize realtime inode pointers in the mount structure 8261da177e4SLinus Torvalds */ 8270771fb45SEric Sandeen error = xfs_rtmount_inodes(mp); 8280771fb45SEric Sandeen if (error) { 8291da177e4SLinus Torvalds /* 8301da177e4SLinus Torvalds * Free up the root inode. 8311da177e4SLinus Torvalds */ 8320b932cccSDave Chinner xfs_warn(mp, "failed to read RT inodes"); 833f9057e3dSChristoph Hellwig goto out_rele_rip; 8341da177e4SLinus Torvalds } 8351da177e4SLinus Torvalds 8361da177e4SLinus Torvalds /* 8377884bc86SChristoph Hellwig * If this is a read-only mount defer the superblock updates until 8387884bc86SChristoph Hellwig * the next remount into writeable mode. Otherwise we would never 8397884bc86SChristoph Hellwig * perform the update e.g. for the root filesystem. 8401da177e4SLinus Torvalds */ 84161e63ecbSDave Chinner if (mp->m_update_sb && !(mp->m_flags & XFS_MOUNT_RDONLY)) { 84261e63ecbSDave Chinner error = xfs_sync_sb(mp, false); 843e5720eecSDavid Chinner if (error) { 8440b932cccSDave Chinner xfs_warn(mp, "failed to write sb changes"); 845b93b6e43SChristoph Hellwig goto out_rtunmount; 846e5720eecSDavid Chinner } 847e5720eecSDavid Chinner } 8481da177e4SLinus Torvalds 8491da177e4SLinus Torvalds /* 8501da177e4SLinus Torvalds * Initialise the XFS quota management subsystem for this mount 8511da177e4SLinus Torvalds */ 852149e53afSChristoph Hellwig if (XFS_IS_QUOTA_ON(mp)) { 8537d095257SChristoph Hellwig error = xfs_qm_newmount(mp, "amount, "aflags); 8540771fb45SEric Sandeen if (error) 855b93b6e43SChristoph Hellwig goto out_rtunmount; 8567d095257SChristoph Hellwig } else { 8577d095257SChristoph Hellwig /* 8587d095257SChristoph Hellwig * If a file system had quotas running earlier, but decided to 8597d095257SChristoph Hellwig * mount without -o uquota/pquota/gquota options, revoke the 8607d095257SChristoph Hellwig * quotachecked license. 8617d095257SChristoph Hellwig */ 8627d095257SChristoph Hellwig if (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_ACCT) { 8630b932cccSDave Chinner xfs_notice(mp, "resetting quota flags"); 8647d095257SChristoph Hellwig error = xfs_mount_reset_sbqflags(mp); 8657d095257SChristoph Hellwig if (error) 866a70a4fa5SBrian Foster goto out_rtunmount; 8677d095257SChristoph Hellwig } 8687d095257SChristoph Hellwig } 8691da177e4SLinus Torvalds 8701da177e4SLinus Torvalds /* 871f0b2efadSBrian Foster * Finish recovering the file system. This part needed to be delayed 872f0b2efadSBrian Foster * until after the root and real-time bitmap inodes were consistently 87381ed9475SDarrick J. Wong * read in. Temporarily create per-AG space reservations for metadata 87481ed9475SDarrick J. Wong * btree shape changes because space freeing transactions (for inode 87581ed9475SDarrick J. Wong * inactivation) require the per-AG reservation in lieu of reserving 87681ed9475SDarrick J. Wong * blocks. 8771da177e4SLinus Torvalds */ 87881ed9475SDarrick J. Wong error = xfs_fs_reserve_ag_blocks(mp); 87981ed9475SDarrick J. Wong if (error && error == -ENOSPC) 88081ed9475SDarrick J. Wong xfs_warn(mp, 88181ed9475SDarrick J. Wong "ENOSPC reserving per-AG metadata pool, log recovery may fail."); 8824249023aSChristoph Hellwig error = xfs_log_mount_finish(mp); 88381ed9475SDarrick J. Wong xfs_fs_unreserve_ag_blocks(mp); 8841da177e4SLinus Torvalds if (error) { 8850b932cccSDave Chinner xfs_warn(mp, "log mount finish failed"); 886b93b6e43SChristoph Hellwig goto out_rtunmount; 8871da177e4SLinus Torvalds } 8881da177e4SLinus Torvalds 8891da177e4SLinus Torvalds /* 890ddeb14f4SDave Chinner * Now the log is fully replayed, we can transition to full read-only 891ddeb14f4SDave Chinner * mode for read-only mounts. This will sync all the metadata and clean 892ddeb14f4SDave Chinner * the log so that the recovery we just performed does not have to be 893ddeb14f4SDave Chinner * replayed again on the next mount. 894ddeb14f4SDave Chinner * 895ddeb14f4SDave Chinner * We use the same quiesce mechanism as the rw->ro remount, as they are 896ddeb14f4SDave Chinner * semantically identical operations. 897ddeb14f4SDave Chinner */ 898ddeb14f4SDave Chinner if ((mp->m_flags & (XFS_MOUNT_RDONLY|XFS_MOUNT_NORECOVERY)) == 899ddeb14f4SDave Chinner XFS_MOUNT_RDONLY) { 900ea2064daSBrian Foster xfs_log_clean(mp); 901ddeb14f4SDave Chinner } 902ddeb14f4SDave Chinner 903ddeb14f4SDave Chinner /* 9041da177e4SLinus Torvalds * Complete the quota initialisation, post-log-replay component. 9051da177e4SLinus Torvalds */ 9067d095257SChristoph Hellwig if (quotamount) { 9077d095257SChristoph Hellwig ASSERT(mp->m_qflags == 0); 9087d095257SChristoph Hellwig mp->m_qflags = quotaflags; 9097d095257SChristoph Hellwig 9107d095257SChristoph Hellwig xfs_qm_mount_quotas(mp); 9117d095257SChristoph Hellwig } 9127d095257SChristoph Hellwig 91384e1e99fSDavid Chinner /* 91484e1e99fSDavid Chinner * Now we are mounted, reserve a small amount of unused space for 91584e1e99fSDavid Chinner * privileged transactions. This is needed so that transaction 91684e1e99fSDavid Chinner * space required for critical operations can dip into this pool 91784e1e99fSDavid Chinner * when at ENOSPC. This is needed for operations like create with 91884e1e99fSDavid Chinner * attr, unwritten extent conversion at ENOSPC, etc. Data allocations 91984e1e99fSDavid Chinner * are not allowed to use this reserved space. 9208babd8a2SDave Chinner * 9218babd8a2SDave Chinner * This may drive us straight to ENOSPC on mount, but that implies 9228babd8a2SDave Chinner * we were already there on the last unmount. Warn if this occurs. 92384e1e99fSDavid Chinner */ 924d5db0f97SEric Sandeen if (!(mp->m_flags & XFS_MOUNT_RDONLY)) { 925d5db0f97SEric Sandeen resblks = xfs_default_resblks(mp); 926714082bcSDavid Chinner error = xfs_reserve_blocks(mp, &resblks, NULL); 927714082bcSDavid Chinner if (error) 9280b932cccSDave Chinner xfs_warn(mp, 9290b932cccSDave Chinner "Unable to allocate reserve blocks. Continuing without reserve pool."); 930174edb0eSDarrick J. Wong 931174edb0eSDarrick J. Wong /* Recover any CoW blocks that never got remapped. */ 932174edb0eSDarrick J. Wong error = xfs_reflink_recover_cow(mp); 933174edb0eSDarrick J. Wong if (error) { 934174edb0eSDarrick J. Wong xfs_err(mp, 935174edb0eSDarrick J. Wong "Error %d recovering leftover CoW allocations.", error); 936174edb0eSDarrick J. Wong xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); 937174edb0eSDarrick J. Wong goto out_quota; 938174edb0eSDarrick J. Wong } 93984d69619SDarrick J. Wong 94084d69619SDarrick J. Wong /* Reserve AG blocks for future btree expansion. */ 94184d69619SDarrick J. Wong error = xfs_fs_reserve_ag_blocks(mp); 94284d69619SDarrick J. Wong if (error && error != -ENOSPC) 94384d69619SDarrick J. Wong goto out_agresv; 944d5db0f97SEric Sandeen } 94584e1e99fSDavid Chinner 9461da177e4SLinus Torvalds return 0; 9471da177e4SLinus Torvalds 94884d69619SDarrick J. Wong out_agresv: 94984d69619SDarrick J. Wong xfs_fs_unreserve_ag_blocks(mp); 950174edb0eSDarrick J. Wong out_quota: 951174edb0eSDarrick J. Wong xfs_qm_unmount_quotas(mp); 952b93b6e43SChristoph Hellwig out_rtunmount: 953b93b6e43SChristoph Hellwig xfs_rtunmount_inodes(mp); 954f9057e3dSChristoph Hellwig out_rele_rip: 95544a8736bSDarrick J. Wong xfs_irele(rip); 95677aff8c7SDarrick J. Wong /* Clean out dquots that might be in memory after quotacheck. */ 95777aff8c7SDarrick J. Wong xfs_qm_unmount(mp); 958ab23a776SDave Chinner 959ab23a776SDave Chinner /* 960ab23a776SDave Chinner * Inactivate all inodes that might still be in memory after a log 961ab23a776SDave Chinner * intent recovery failure so that reclaim can free them. Metadata 962ab23a776SDave Chinner * inodes and the root directory shouldn't need inactivation, but the 963ab23a776SDave Chinner * mount failed for some reason, so pull down all the state and flee. 964ab23a776SDave Chinner */ 965ab23a776SDave Chinner xfs_inodegc_flush(mp); 966ab23a776SDave Chinner 9672d1d1da3SDarrick J. Wong /* 968d336f7ebSDarrick J. Wong * Flush all inode reclamation work and flush the log. 9692d1d1da3SDarrick J. Wong * We have to do this /after/ rtunmount and qm_unmount because those 9702d1d1da3SDarrick J. Wong * two will have scheduled delayed reclaim for the rt/quota inodes. 9712d1d1da3SDarrick J. Wong * 9722d1d1da3SDarrick J. Wong * This is slightly different from the unmountfs call sequence 9732d1d1da3SDarrick J. Wong * because we could be tearing down a partially set up mount. In 9742d1d1da3SDarrick J. Wong * particular, if log_mount_finish fails we bail out without calling 9752d1d1da3SDarrick J. Wong * qm_unmount_quotas and therefore rely on qm_unmount to release the 9762d1d1da3SDarrick J. Wong * quota inodes. 9772d1d1da3SDarrick J. Wong */ 978d336f7ebSDarrick J. Wong xfs_unmount_flush_inodes(mp); 979f9057e3dSChristoph Hellwig out_log_dealloc: 980f0b2efadSBrian Foster xfs_log_mount_cancel(mp); 98140b1de00SDarrick J. Wong out_inodegc_shrinker: 98240b1de00SDarrick J. Wong unregister_shrinker(&mp->m_inodegc_shrinker); 983d4f3512bSDave Chinner out_fail_wait: 984d4f3512bSDave Chinner if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp) 98510fb9ac1SBrian Foster xfs_buftarg_drain(mp->m_logdev_targp); 98610fb9ac1SBrian Foster xfs_buftarg_drain(mp->m_ddev_targp); 987f9057e3dSChristoph Hellwig out_free_perag: 988ff4f038cSChristoph Hellwig xfs_free_perag(mp); 9890650b554SDave Chinner out_free_dir: 9900650b554SDave Chinner xfs_da_unmount(mp); 991f9057e3dSChristoph Hellwig out_remove_uuid: 99227174203SChristoph Hellwig xfs_uuid_unmount(mp); 99331965ef3SDarrick J. Wong out_remove_errortag: 99431965ef3SDarrick J. Wong xfs_errortag_del(mp); 995192852beSCarlos Maiolino out_remove_error_sysfs: 996192852beSCarlos Maiolino xfs_error_sysfs_del(mp); 997225e4635SBill O'Donnell out_del_stats: 998225e4635SBill O'Donnell xfs_sysfs_del(&mp->m_stats.xs_kobj); 999a31b1d3dSBrian Foster out_remove_sysfs: 1000a31b1d3dSBrian Foster xfs_sysfs_del(&mp->m_kobj); 1001f9057e3dSChristoph Hellwig out: 10021da177e4SLinus Torvalds return error; 10031da177e4SLinus Torvalds } 10041da177e4SLinus Torvalds 10051da177e4SLinus Torvalds /* 10061da177e4SLinus Torvalds * This flushes out the inodes,dquots and the superblock, unmounts the 10071da177e4SLinus Torvalds * log and makes sure that incore structures are freed. 10081da177e4SLinus Torvalds */ 100941b5c2e7SChristoph Hellwig void 101041b5c2e7SChristoph Hellwig xfs_unmountfs( 101141b5c2e7SChristoph Hellwig struct xfs_mount *mp) 10121da177e4SLinus Torvalds { 1013c8ce540dSDarrick J. Wong uint64_t resblks; 101441b5c2e7SChristoph Hellwig int error; 10151da177e4SLinus Torvalds 1016ab23a776SDave Chinner /* 1017ab23a776SDave Chinner * Perform all on-disk metadata updates required to inactivate inodes 1018ab23a776SDave Chinner * that the VFS evicted earlier in the unmount process. Freeing inodes 1019ab23a776SDave Chinner * and discarding CoW fork preallocations can cause shape changes to 1020ab23a776SDave Chinner * the free inode and refcount btrees, respectively, so we must finish 1021ab23a776SDave Chinner * this before we discard the metadata space reservations. Metadata 1022ab23a776SDave Chinner * inodes and the root directory do not require inactivation. 1023ab23a776SDave Chinner */ 1024ab23a776SDave Chinner xfs_inodegc_flush(mp); 1025ab23a776SDave Chinner 1026c9a6526fSDarrick J. Wong xfs_blockgc_stop(mp); 102784d69619SDarrick J. Wong xfs_fs_unreserve_ag_blocks(mp); 10287d095257SChristoph Hellwig xfs_qm_unmount_quotas(mp); 1029b93b6e43SChristoph Hellwig xfs_rtunmount_inodes(mp); 103044a8736bSDarrick J. Wong xfs_irele(mp->m_rootip); 103177508ec8SChristoph Hellwig 1032d336f7ebSDarrick J. Wong xfs_unmount_flush_inodes(mp); 10331da177e4SLinus Torvalds 10347d095257SChristoph Hellwig xfs_qm_unmount(mp); 1035a357a121SLachlan McIlroy 10361da177e4SLinus Torvalds /* 103784e1e99fSDavid Chinner * Unreserve any blocks we have so that when we unmount we don't account 103884e1e99fSDavid Chinner * the reserved free space as used. This is really only necessary for 103984e1e99fSDavid Chinner * lazy superblock counting because it trusts the incore superblock 10409da096fdSMalcolm Parsons * counters to be absolutely correct on clean unmount. 104184e1e99fSDavid Chinner * 104284e1e99fSDavid Chinner * We don't bother correcting this elsewhere for lazy superblock 104384e1e99fSDavid Chinner * counting because on mount of an unclean filesystem we reconstruct the 104484e1e99fSDavid Chinner * correct counter value and this is irrelevant. 104584e1e99fSDavid Chinner * 104684e1e99fSDavid Chinner * For non-lazy counter filesystems, this doesn't matter at all because 104784e1e99fSDavid Chinner * we only every apply deltas to the superblock and hence the incore 104884e1e99fSDavid Chinner * value does not matter.... 104984e1e99fSDavid Chinner */ 105084e1e99fSDavid Chinner resblks = 0; 1051714082bcSDavid Chinner error = xfs_reserve_blocks(mp, &resblks, NULL); 1052714082bcSDavid Chinner if (error) 10530b932cccSDave Chinner xfs_warn(mp, "Unable to free reserved block pool. " 1054714082bcSDavid Chinner "Freespace may not be correct on next mount."); 1055714082bcSDavid Chinner 105621b699c8SChristoph Hellwig xfs_log_unmount(mp); 10570650b554SDave Chinner xfs_da_unmount(mp); 105827174203SChristoph Hellwig xfs_uuid_unmount(mp); 10591da177e4SLinus Torvalds 10601550d0b0SChristoph Hellwig #if defined(DEBUG) 106131965ef3SDarrick J. Wong xfs_errortag_clearall(mp); 10621da177e4SLinus Torvalds #endif 106340b1de00SDarrick J. Wong unregister_shrinker(&mp->m_inodegc_shrinker); 1064ff4f038cSChristoph Hellwig xfs_free_perag(mp); 1065a31b1d3dSBrian Foster 106631965ef3SDarrick J. Wong xfs_errortag_del(mp); 1067192852beSCarlos Maiolino xfs_error_sysfs_del(mp); 1068225e4635SBill O'Donnell xfs_sysfs_del(&mp->m_stats.xs_kobj); 1069a31b1d3dSBrian Foster xfs_sysfs_del(&mp->m_kobj); 10701da177e4SLinus Torvalds } 10711da177e4SLinus Torvalds 107291ee575fSBrian Foster /* 107391ee575fSBrian Foster * Determine whether modifications can proceed. The caller specifies the minimum 107491ee575fSBrian Foster * freeze level for which modifications should not be allowed. This allows 107591ee575fSBrian Foster * certain operations to proceed while the freeze sequence is in progress, if 107691ee575fSBrian Foster * necessary. 107791ee575fSBrian Foster */ 107891ee575fSBrian Foster bool 107991ee575fSBrian Foster xfs_fs_writable( 108091ee575fSBrian Foster struct xfs_mount *mp, 108191ee575fSBrian Foster int level) 108292821e2bSDavid Chinner { 108391ee575fSBrian Foster ASSERT(level > SB_UNFROZEN); 108491ee575fSBrian Foster if ((mp->m_super->s_writers.frozen >= level) || 108591ee575fSBrian Foster XFS_FORCED_SHUTDOWN(mp) || (mp->m_flags & XFS_MOUNT_RDONLY)) 108691ee575fSBrian Foster return false; 108791ee575fSBrian Foster 108891ee575fSBrian Foster return true; 108992821e2bSDavid Chinner } 109092821e2bSDavid Chinner 10910d485adaSDave Chinner int 10920d485adaSDave Chinner xfs_mod_fdblocks( 10930d485adaSDave Chinner struct xfs_mount *mp, 10940d485adaSDave Chinner int64_t delta, 10950d485adaSDave Chinner bool rsvd) 10960d485adaSDave Chinner { 10970d485adaSDave Chinner int64_t lcounter; 10980d485adaSDave Chinner long long res_used; 10990d485adaSDave Chinner s32 batch; 1100fd43cf60SBrian Foster uint64_t set_aside; 11010d485adaSDave Chinner 11020d485adaSDave Chinner if (delta > 0) { 11030d485adaSDave Chinner /* 11040d485adaSDave Chinner * If the reserve pool is depleted, put blocks back into it 11050d485adaSDave Chinner * first. Most of the time the pool is full. 11060d485adaSDave Chinner */ 11070d485adaSDave Chinner if (likely(mp->m_resblks == mp->m_resblks_avail)) { 11080d485adaSDave Chinner percpu_counter_add(&mp->m_fdblocks, delta); 11090d485adaSDave Chinner return 0; 11100d485adaSDave Chinner } 11110d485adaSDave Chinner 11120d485adaSDave Chinner spin_lock(&mp->m_sb_lock); 11130d485adaSDave Chinner res_used = (long long)(mp->m_resblks - mp->m_resblks_avail); 11140d485adaSDave Chinner 11150d485adaSDave Chinner if (res_used > delta) { 11160d485adaSDave Chinner mp->m_resblks_avail += delta; 11170d485adaSDave Chinner } else { 11180d485adaSDave Chinner delta -= res_used; 11190d485adaSDave Chinner mp->m_resblks_avail = mp->m_resblks; 11200d485adaSDave Chinner percpu_counter_add(&mp->m_fdblocks, delta); 11210d485adaSDave Chinner } 11220d485adaSDave Chinner spin_unlock(&mp->m_sb_lock); 11230d485adaSDave Chinner return 0; 11240d485adaSDave Chinner } 11250d485adaSDave Chinner 11260d485adaSDave Chinner /* 11270d485adaSDave Chinner * Taking blocks away, need to be more accurate the closer we 11280d485adaSDave Chinner * are to zero. 11290d485adaSDave Chinner * 11300d485adaSDave Chinner * If the counter has a value of less than 2 * max batch size, 11310d485adaSDave Chinner * then make everything serialise as we are real close to 11320d485adaSDave Chinner * ENOSPC. 11330d485adaSDave Chinner */ 11348c1903d3SDave Chinner if (__percpu_counter_compare(&mp->m_fdblocks, 2 * XFS_FDBLOCKS_BATCH, 11358c1903d3SDave Chinner XFS_FDBLOCKS_BATCH) < 0) 11360d485adaSDave Chinner batch = 1; 11370d485adaSDave Chinner else 11388c1903d3SDave Chinner batch = XFS_FDBLOCKS_BATCH; 11390d485adaSDave Chinner 1140fd43cf60SBrian Foster /* 1141fd43cf60SBrian Foster * Set aside allocbt blocks because these blocks are tracked as free 1142fd43cf60SBrian Foster * space but not available for allocation. Technically this means that a 1143fd43cf60SBrian Foster * single reservation cannot consume all remaining free space, but the 1144fd43cf60SBrian Foster * ratio of allocbt blocks to usable free blocks should be rather small. 1145fd43cf60SBrian Foster * The tradeoff without this is that filesystems that maintain high 1146fd43cf60SBrian Foster * perag block reservations can over reserve physical block availability 1147fd43cf60SBrian Foster * and fail physical allocation, which leads to much more serious 1148fd43cf60SBrian Foster * problems (i.e. transaction abort, pagecache discards, etc.) than 1149fd43cf60SBrian Foster * slightly premature -ENOSPC. 1150fd43cf60SBrian Foster */ 1151fd43cf60SBrian Foster set_aside = mp->m_alloc_set_aside + atomic64_read(&mp->m_allocbt_blks); 1152104b4e51SNikolay Borisov percpu_counter_add_batch(&mp->m_fdblocks, delta, batch); 1153fd43cf60SBrian Foster if (__percpu_counter_compare(&mp->m_fdblocks, set_aside, 11548c1903d3SDave Chinner XFS_FDBLOCKS_BATCH) >= 0) { 11550d485adaSDave Chinner /* we had space! */ 11560d485adaSDave Chinner return 0; 11570d485adaSDave Chinner } 11580d485adaSDave Chinner 11590d485adaSDave Chinner /* 11600d485adaSDave Chinner * lock up the sb for dipping into reserves before releasing the space 11610d485adaSDave Chinner * that took us to ENOSPC. 11620d485adaSDave Chinner */ 11630d485adaSDave Chinner spin_lock(&mp->m_sb_lock); 11640d485adaSDave Chinner percpu_counter_add(&mp->m_fdblocks, -delta); 11650d485adaSDave Chinner if (!rsvd) 11660d485adaSDave Chinner goto fdblocks_enospc; 11670d485adaSDave Chinner 11680d485adaSDave Chinner lcounter = (long long)mp->m_resblks_avail + delta; 11690d485adaSDave Chinner if (lcounter >= 0) { 11700d485adaSDave Chinner mp->m_resblks_avail = lcounter; 11710d485adaSDave Chinner spin_unlock(&mp->m_sb_lock); 11720d485adaSDave Chinner return 0; 11730d485adaSDave Chinner } 1174ec43f6daSEric Sandeen xfs_warn_once(mp, 1175ec43f6daSEric Sandeen "Reserve blocks depleted! Consider increasing reserve pool size."); 1176ec43f6daSEric Sandeen 11770d485adaSDave Chinner fdblocks_enospc: 11780d485adaSDave Chinner spin_unlock(&mp->m_sb_lock); 11790d485adaSDave Chinner return -ENOSPC; 11800d485adaSDave Chinner } 11810d485adaSDave Chinner 1182bab98bbeSDave Chinner int 1183bab98bbeSDave Chinner xfs_mod_frextents( 1184bab98bbeSDave Chinner struct xfs_mount *mp, 1185bab98bbeSDave Chinner int64_t delta) 1186bab98bbeSDave Chinner { 1187bab98bbeSDave Chinner int64_t lcounter; 1188bab98bbeSDave Chinner int ret = 0; 1189bab98bbeSDave Chinner 1190bab98bbeSDave Chinner spin_lock(&mp->m_sb_lock); 1191bab98bbeSDave Chinner lcounter = mp->m_sb.sb_frextents + delta; 1192bab98bbeSDave Chinner if (lcounter < 0) 1193bab98bbeSDave Chinner ret = -ENOSPC; 1194bab98bbeSDave Chinner else 1195bab98bbeSDave Chinner mp->m_sb.sb_frextents = lcounter; 1196bab98bbeSDave Chinner spin_unlock(&mp->m_sb_lock); 1197bab98bbeSDave Chinner return ret; 1198bab98bbeSDave Chinner } 1199bab98bbeSDave Chinner 12001da177e4SLinus Torvalds /* 12011da177e4SLinus Torvalds * Used to free the superblock along various error paths. 12021da177e4SLinus Torvalds */ 12031da177e4SLinus Torvalds void 12041da177e4SLinus Torvalds xfs_freesb( 120526af6552SDave Chinner struct xfs_mount *mp) 12061da177e4SLinus Torvalds { 120726af6552SDave Chinner struct xfs_buf *bp = mp->m_sb_bp; 12081da177e4SLinus Torvalds 120926af6552SDave Chinner xfs_buf_lock(bp); 12101da177e4SLinus Torvalds mp->m_sb_bp = NULL; 121126af6552SDave Chinner xfs_buf_relse(bp); 12121da177e4SLinus Torvalds } 12131da177e4SLinus Torvalds 12141da177e4SLinus Torvalds /* 1215dda35b8fSChristoph Hellwig * If the underlying (data/log/rt) device is readonly, there are some 1216dda35b8fSChristoph Hellwig * operations that cannot proceed. 1217dda35b8fSChristoph Hellwig */ 1218dda35b8fSChristoph Hellwig int 1219dda35b8fSChristoph Hellwig xfs_dev_is_read_only( 1220dda35b8fSChristoph Hellwig struct xfs_mount *mp, 1221dda35b8fSChristoph Hellwig char *message) 1222dda35b8fSChristoph Hellwig { 1223dda35b8fSChristoph Hellwig if (xfs_readonly_buftarg(mp->m_ddev_targp) || 1224dda35b8fSChristoph Hellwig xfs_readonly_buftarg(mp->m_logdev_targp) || 1225dda35b8fSChristoph Hellwig (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) { 12260b932cccSDave Chinner xfs_notice(mp, "%s required on read-only device.", message); 12270b932cccSDave Chinner xfs_notice(mp, "write access unavailable, cannot proceed."); 12282451337dSDave Chinner return -EROFS; 1229dda35b8fSChristoph Hellwig } 1230dda35b8fSChristoph Hellwig return 0; 1231dda35b8fSChristoph Hellwig } 1232f467cad9SDarrick J. Wong 1233f467cad9SDarrick J. Wong /* Force the summary counters to be recalculated at next mount. */ 1234f467cad9SDarrick J. Wong void 1235f467cad9SDarrick J. Wong xfs_force_summary_recalc( 1236f467cad9SDarrick J. Wong struct xfs_mount *mp) 1237f467cad9SDarrick J. Wong { 1238f467cad9SDarrick J. Wong if (!xfs_sb_version_haslazysbcount(&mp->m_sb)) 1239f467cad9SDarrick J. Wong return; 1240f467cad9SDarrick J. Wong 124139353ff6SDarrick J. Wong xfs_fs_mark_sick(mp, XFS_SICK_FS_COUNTERS); 1242f467cad9SDarrick J. Wong } 12439fe82b8cSDarrick J. Wong 12449fe82b8cSDarrick J. Wong /* 1245*908ce71eSDarrick J. Wong * Enable a log incompat feature flag in the primary superblock. The caller 1246*908ce71eSDarrick J. Wong * cannot have any other transactions in progress. 1247*908ce71eSDarrick J. Wong */ 1248*908ce71eSDarrick J. Wong int 1249*908ce71eSDarrick J. Wong xfs_add_incompat_log_feature( 1250*908ce71eSDarrick J. Wong struct xfs_mount *mp, 1251*908ce71eSDarrick J. Wong uint32_t feature) 1252*908ce71eSDarrick J. Wong { 1253*908ce71eSDarrick J. Wong struct xfs_dsb *dsb; 1254*908ce71eSDarrick J. Wong int error; 1255*908ce71eSDarrick J. Wong 1256*908ce71eSDarrick J. Wong ASSERT(hweight32(feature) == 1); 1257*908ce71eSDarrick J. Wong ASSERT(!(feature & XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN)); 1258*908ce71eSDarrick J. Wong 1259*908ce71eSDarrick J. Wong /* 1260*908ce71eSDarrick J. Wong * Force the log to disk and kick the background AIL thread to reduce 1261*908ce71eSDarrick J. Wong * the chances that the bwrite will stall waiting for the AIL to unpin 1262*908ce71eSDarrick J. Wong * the primary superblock buffer. This isn't a data integrity 1263*908ce71eSDarrick J. Wong * operation, so we don't need a synchronous push. 1264*908ce71eSDarrick J. Wong */ 1265*908ce71eSDarrick J. Wong error = xfs_log_force(mp, XFS_LOG_SYNC); 1266*908ce71eSDarrick J. Wong if (error) 1267*908ce71eSDarrick J. Wong return error; 1268*908ce71eSDarrick J. Wong xfs_ail_push_all(mp->m_ail); 1269*908ce71eSDarrick J. Wong 1270*908ce71eSDarrick J. Wong /* 1271*908ce71eSDarrick J. Wong * Lock the primary superblock buffer to serialize all callers that 1272*908ce71eSDarrick J. Wong * are trying to set feature bits. 1273*908ce71eSDarrick J. Wong */ 1274*908ce71eSDarrick J. Wong xfs_buf_lock(mp->m_sb_bp); 1275*908ce71eSDarrick J. Wong xfs_buf_hold(mp->m_sb_bp); 1276*908ce71eSDarrick J. Wong 1277*908ce71eSDarrick J. Wong if (XFS_FORCED_SHUTDOWN(mp)) { 1278*908ce71eSDarrick J. Wong error = -EIO; 1279*908ce71eSDarrick J. Wong goto rele; 1280*908ce71eSDarrick J. Wong } 1281*908ce71eSDarrick J. Wong 1282*908ce71eSDarrick J. Wong if (xfs_sb_has_incompat_log_feature(&mp->m_sb, feature)) 1283*908ce71eSDarrick J. Wong goto rele; 1284*908ce71eSDarrick J. Wong 1285*908ce71eSDarrick J. Wong /* 1286*908ce71eSDarrick J. Wong * Write the primary superblock to disk immediately, because we need 1287*908ce71eSDarrick J. Wong * the log_incompat bit to be set in the primary super now to protect 1288*908ce71eSDarrick J. Wong * the log items that we're going to commit later. 1289*908ce71eSDarrick J. Wong */ 1290*908ce71eSDarrick J. Wong dsb = mp->m_sb_bp->b_addr; 1291*908ce71eSDarrick J. Wong xfs_sb_to_disk(dsb, &mp->m_sb); 1292*908ce71eSDarrick J. Wong dsb->sb_features_log_incompat |= cpu_to_be32(feature); 1293*908ce71eSDarrick J. Wong error = xfs_bwrite(mp->m_sb_bp); 1294*908ce71eSDarrick J. Wong if (error) 1295*908ce71eSDarrick J. Wong goto shutdown; 1296*908ce71eSDarrick J. Wong 1297*908ce71eSDarrick J. Wong /* 1298*908ce71eSDarrick J. Wong * Add the feature bits to the incore superblock before we unlock the 1299*908ce71eSDarrick J. Wong * buffer. 1300*908ce71eSDarrick J. Wong */ 1301*908ce71eSDarrick J. Wong xfs_sb_add_incompat_log_features(&mp->m_sb, feature); 1302*908ce71eSDarrick J. Wong xfs_buf_relse(mp->m_sb_bp); 1303*908ce71eSDarrick J. Wong 1304*908ce71eSDarrick J. Wong /* Log the superblock to disk. */ 1305*908ce71eSDarrick J. Wong return xfs_sync_sb(mp, false); 1306*908ce71eSDarrick J. Wong shutdown: 1307*908ce71eSDarrick J. Wong xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR); 1308*908ce71eSDarrick J. Wong rele: 1309*908ce71eSDarrick J. Wong xfs_buf_relse(mp->m_sb_bp); 1310*908ce71eSDarrick J. Wong return error; 1311*908ce71eSDarrick J. Wong } 1312*908ce71eSDarrick J. Wong 1313*908ce71eSDarrick J. Wong /* 1314*908ce71eSDarrick J. Wong * Clear all the log incompat flags from the superblock. 1315*908ce71eSDarrick J. Wong * 1316*908ce71eSDarrick J. Wong * The caller cannot be in a transaction, must ensure that the log does not 1317*908ce71eSDarrick J. Wong * contain any log items protected by any log incompat bit, and must ensure 1318*908ce71eSDarrick J. Wong * that there are no other threads that depend on the state of the log incompat 1319*908ce71eSDarrick J. Wong * feature flags in the primary super. 1320*908ce71eSDarrick J. Wong * 1321*908ce71eSDarrick J. Wong * Returns true if the superblock is dirty. 1322*908ce71eSDarrick J. Wong */ 1323*908ce71eSDarrick J. Wong bool 1324*908ce71eSDarrick J. Wong xfs_clear_incompat_log_features( 1325*908ce71eSDarrick J. Wong struct xfs_mount *mp) 1326*908ce71eSDarrick J. Wong { 1327*908ce71eSDarrick J. Wong bool ret = false; 1328*908ce71eSDarrick J. Wong 1329*908ce71eSDarrick J. Wong if (!xfs_sb_version_hascrc(&mp->m_sb) || 1330*908ce71eSDarrick J. Wong !xfs_sb_has_incompat_log_feature(&mp->m_sb, 1331*908ce71eSDarrick J. Wong XFS_SB_FEAT_INCOMPAT_LOG_ALL) || 1332*908ce71eSDarrick J. Wong XFS_FORCED_SHUTDOWN(mp)) 1333*908ce71eSDarrick J. Wong return false; 1334*908ce71eSDarrick J. Wong 1335*908ce71eSDarrick J. Wong /* 1336*908ce71eSDarrick J. Wong * Update the incore superblock. We synchronize on the primary super 1337*908ce71eSDarrick J. Wong * buffer lock to be consistent with the add function, though at least 1338*908ce71eSDarrick J. Wong * in theory this shouldn't be necessary. 1339*908ce71eSDarrick J. Wong */ 1340*908ce71eSDarrick J. Wong xfs_buf_lock(mp->m_sb_bp); 1341*908ce71eSDarrick J. Wong xfs_buf_hold(mp->m_sb_bp); 1342*908ce71eSDarrick J. Wong 1343*908ce71eSDarrick J. Wong if (xfs_sb_has_incompat_log_feature(&mp->m_sb, 1344*908ce71eSDarrick J. Wong XFS_SB_FEAT_INCOMPAT_LOG_ALL)) { 1345*908ce71eSDarrick J. Wong xfs_info(mp, "Clearing log incompat feature flags."); 1346*908ce71eSDarrick J. Wong xfs_sb_remove_incompat_log_features(&mp->m_sb); 1347*908ce71eSDarrick J. Wong ret = true; 1348*908ce71eSDarrick J. Wong } 1349*908ce71eSDarrick J. Wong 1350*908ce71eSDarrick J. Wong xfs_buf_relse(mp->m_sb_bp); 1351*908ce71eSDarrick J. Wong return ret; 1352*908ce71eSDarrick J. Wong } 1353*908ce71eSDarrick J. Wong 1354*908ce71eSDarrick J. Wong /* 13559fe82b8cSDarrick J. Wong * Update the in-core delayed block counter. 13569fe82b8cSDarrick J. Wong * 13579fe82b8cSDarrick J. Wong * We prefer to update the counter without having to take a spinlock for every 13589fe82b8cSDarrick J. Wong * counter update (i.e. batching). Each change to delayed allocation 13599fe82b8cSDarrick J. Wong * reservations can change can easily exceed the default percpu counter 13609fe82b8cSDarrick J. Wong * batching, so we use a larger batch factor here. 13619fe82b8cSDarrick J. Wong * 13629fe82b8cSDarrick J. Wong * Note that we don't currently have any callers requiring fast summation 13639fe82b8cSDarrick J. Wong * (e.g. percpu_counter_read) so we can use a big batch value here. 13649fe82b8cSDarrick J. Wong */ 13659fe82b8cSDarrick J. Wong #define XFS_DELALLOC_BATCH (4096) 13669fe82b8cSDarrick J. Wong void 13679fe82b8cSDarrick J. Wong xfs_mod_delalloc( 13689fe82b8cSDarrick J. Wong struct xfs_mount *mp, 13699fe82b8cSDarrick J. Wong int64_t delta) 13709fe82b8cSDarrick J. Wong { 13719fe82b8cSDarrick J. Wong percpu_counter_add_batch(&mp->m_delalloc_blks, delta, 13729fe82b8cSDarrick J. Wong XFS_DELALLOC_BATCH); 13739fe82b8cSDarrick J. Wong } 1374