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" 351da177e4SLinus Torvalds 3627174203SChristoph Hellwig static DEFINE_MUTEX(xfs_uuid_table_mutex); 3727174203SChristoph Hellwig static int xfs_uuid_table_size; 3827174203SChristoph Hellwig static uuid_t *xfs_uuid_table; 3927174203SChristoph Hellwig 40af3b6382SDarrick J. Wong void 41af3b6382SDarrick J. Wong xfs_uuid_table_free(void) 42af3b6382SDarrick J. Wong { 43af3b6382SDarrick J. Wong if (xfs_uuid_table_size == 0) 44af3b6382SDarrick J. Wong return; 45af3b6382SDarrick J. Wong kmem_free(xfs_uuid_table); 46af3b6382SDarrick J. Wong xfs_uuid_table = NULL; 47af3b6382SDarrick J. Wong xfs_uuid_table_size = 0; 48af3b6382SDarrick J. Wong } 49af3b6382SDarrick J. Wong 5027174203SChristoph Hellwig /* 5127174203SChristoph Hellwig * See if the UUID is unique among mounted XFS filesystems. 5227174203SChristoph Hellwig * Mount fails if UUID is nil or a FS with the same UUID is already mounted. 5327174203SChristoph Hellwig */ 5427174203SChristoph Hellwig STATIC int 5527174203SChristoph Hellwig xfs_uuid_mount( 5627174203SChristoph Hellwig struct xfs_mount *mp) 5727174203SChristoph Hellwig { 5827174203SChristoph Hellwig uuid_t *uuid = &mp->m_sb.sb_uuid; 5927174203SChristoph Hellwig int hole, i; 6027174203SChristoph Hellwig 618f720d9fSAmir Goldstein /* Publish UUID in struct super_block */ 6285787090SChristoph Hellwig uuid_copy(&mp->m_super->s_uuid, uuid); 638f720d9fSAmir Goldstein 6427174203SChristoph Hellwig if (mp->m_flags & XFS_MOUNT_NOUUID) 6527174203SChristoph Hellwig return 0; 6627174203SChristoph Hellwig 67d905fdaaSAmir Goldstein if (uuid_is_null(uuid)) { 68d905fdaaSAmir Goldstein xfs_warn(mp, "Filesystem has null UUID - can't mount"); 692451337dSDave Chinner return -EINVAL; 7027174203SChristoph Hellwig } 7127174203SChristoph Hellwig 7227174203SChristoph Hellwig mutex_lock(&xfs_uuid_table_mutex); 7327174203SChristoph Hellwig for (i = 0, hole = -1; i < xfs_uuid_table_size; i++) { 74d905fdaaSAmir Goldstein if (uuid_is_null(&xfs_uuid_table[i])) { 7527174203SChristoph Hellwig hole = i; 7627174203SChristoph Hellwig continue; 7727174203SChristoph Hellwig } 7827174203SChristoph Hellwig if (uuid_equal(uuid, &xfs_uuid_table[i])) 7927174203SChristoph Hellwig goto out_duplicate; 8027174203SChristoph Hellwig } 8127174203SChristoph Hellwig 8227174203SChristoph Hellwig if (hole < 0) { 83771915c4SCarlos Maiolino xfs_uuid_table = krealloc(xfs_uuid_table, 8427174203SChristoph Hellwig (xfs_uuid_table_size + 1) * sizeof(*xfs_uuid_table), 85771915c4SCarlos Maiolino GFP_KERNEL | __GFP_NOFAIL); 8627174203SChristoph Hellwig hole = xfs_uuid_table_size++; 8727174203SChristoph Hellwig } 8827174203SChristoph Hellwig xfs_uuid_table[hole] = *uuid; 8927174203SChristoph Hellwig mutex_unlock(&xfs_uuid_table_mutex); 9027174203SChristoph Hellwig 9127174203SChristoph Hellwig return 0; 9227174203SChristoph Hellwig 9327174203SChristoph Hellwig out_duplicate: 9427174203SChristoph Hellwig mutex_unlock(&xfs_uuid_table_mutex); 95021000e5SMitsuo Hayasaka xfs_warn(mp, "Filesystem has duplicate UUID %pU - can't mount", uuid); 962451337dSDave Chinner return -EINVAL; 9727174203SChristoph Hellwig } 9827174203SChristoph Hellwig 9927174203SChristoph Hellwig STATIC void 10027174203SChristoph Hellwig xfs_uuid_unmount( 10127174203SChristoph Hellwig struct xfs_mount *mp) 10227174203SChristoph Hellwig { 10327174203SChristoph Hellwig uuid_t *uuid = &mp->m_sb.sb_uuid; 10427174203SChristoph Hellwig int i; 10527174203SChristoph Hellwig 10627174203SChristoph Hellwig if (mp->m_flags & XFS_MOUNT_NOUUID) 10727174203SChristoph Hellwig return; 10827174203SChristoph Hellwig 10927174203SChristoph Hellwig mutex_lock(&xfs_uuid_table_mutex); 11027174203SChristoph Hellwig for (i = 0; i < xfs_uuid_table_size; i++) { 111d905fdaaSAmir Goldstein if (uuid_is_null(&xfs_uuid_table[i])) 11227174203SChristoph Hellwig continue; 11327174203SChristoph Hellwig if (!uuid_equal(uuid, &xfs_uuid_table[i])) 11427174203SChristoph Hellwig continue; 11527174203SChristoph Hellwig memset(&xfs_uuid_table[i], 0, sizeof(uuid_t)); 11627174203SChristoph Hellwig break; 11727174203SChristoph Hellwig } 11827174203SChristoph Hellwig ASSERT(i < xfs_uuid_table_size); 11927174203SChristoph Hellwig mutex_unlock(&xfs_uuid_table_mutex); 12027174203SChristoph Hellwig } 12127174203SChristoph Hellwig 12227174203SChristoph Hellwig 123e176579eSDave Chinner STATIC void 124e176579eSDave Chinner __xfs_free_perag( 125e176579eSDave Chinner struct rcu_head *head) 126e176579eSDave Chinner { 127e176579eSDave Chinner struct xfs_perag *pag = container_of(head, struct xfs_perag, rcu_head); 128e176579eSDave Chinner 129894ecacfSDarrick J. Wong ASSERT(!delayed_work_pending(&pag->pag_blockgc_work)); 130e176579eSDave Chinner ASSERT(atomic_read(&pag->pag_ref) == 0); 131e176579eSDave Chinner kmem_free(pag); 132e176579eSDave Chinner } 133e176579eSDave Chinner 1340fa800fbSDave Chinner /* 135e176579eSDave Chinner * Free up the per-ag resources associated with the mount structure. 1361da177e4SLinus Torvalds */ 137c962fb79SChristoph Hellwig STATIC void 138ff4f038cSChristoph Hellwig xfs_free_perag( 139745f6919SChristoph Hellwig xfs_mount_t *mp) 1401da177e4SLinus Torvalds { 1411c1c6ebcSDave Chinner xfs_agnumber_t agno; 1421c1c6ebcSDave Chinner struct xfs_perag *pag; 1431da177e4SLinus Torvalds 1441c1c6ebcSDave Chinner for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) { 1451c1c6ebcSDave Chinner spin_lock(&mp->m_perag_lock); 1461c1c6ebcSDave Chinner pag = radix_tree_delete(&mp->m_perag_tree, agno); 1471c1c6ebcSDave Chinner spin_unlock(&mp->m_perag_lock); 148e176579eSDave Chinner ASSERT(pag); 149f83282a8SDave Chinner ASSERT(atomic_read(&pag->pag_ref) == 0); 150894ecacfSDarrick J. Wong cancel_delayed_work_sync(&pag->pag_blockgc_work); 1519b247179SDarrick J. Wong xfs_iunlink_destroy(pag); 1526031e73aSLucas Stach xfs_buf_hash_destroy(pag); 153e176579eSDave Chinner call_rcu(&pag->rcu_head, __xfs_free_perag); 1541da177e4SLinus Torvalds } 1551da177e4SLinus Torvalds } 1561da177e4SLinus Torvalds 1574cc929eeSNathan Scott /* 1584cc929eeSNathan Scott * Check size of device based on the (data/realtime) block count. 1594cc929eeSNathan Scott * Note: this check is used by the growfs code as well as mount. 1604cc929eeSNathan Scott */ 1614cc929eeSNathan Scott int 1624cc929eeSNathan Scott xfs_sb_validate_fsb_count( 1634cc929eeSNathan Scott xfs_sb_t *sbp, 164c8ce540dSDarrick J. Wong uint64_t nblocks) 1654cc929eeSNathan Scott { 1664cc929eeSNathan Scott ASSERT(PAGE_SHIFT >= sbp->sb_blocklog); 1674cc929eeSNathan Scott ASSERT(sbp->sb_blocklog >= BBSHIFT); 1684cc929eeSNathan Scott 169d5cf09baSChristoph Hellwig /* Limited by ULONG_MAX of page cache index */ 17009cbfeafSKirill A. Shutemov if (nblocks >> (PAGE_SHIFT - sbp->sb_blocklog) > ULONG_MAX) 1712451337dSDave Chinner return -EFBIG; 1724cc929eeSNathan Scott return 0; 1734cc929eeSNathan Scott } 1741da177e4SLinus Torvalds 1751c1c6ebcSDave Chinner int 176c11e2c36SNathan Scott xfs_initialize_perag( 177c11e2c36SNathan Scott xfs_mount_t *mp, 1781c1c6ebcSDave Chinner xfs_agnumber_t agcount, 1791c1c6ebcSDave Chinner xfs_agnumber_t *maxagi) 1801da177e4SLinus Torvalds { 1812d2194f6SCarlos Maiolino xfs_agnumber_t index; 182b20fe473SBill O'Donnell xfs_agnumber_t first_initialised = NULLAGNUMBER; 1831da177e4SLinus Torvalds xfs_perag_t *pag; 1848b26c582SDave Chinner int error = -ENOMEM; 1851da177e4SLinus Torvalds 1861c1c6ebcSDave Chinner /* 1871c1c6ebcSDave Chinner * Walk the current per-ag tree so we don't try to initialise AGs 1881c1c6ebcSDave Chinner * that already exist (growfs case). Allocate and insert all the 1891c1c6ebcSDave Chinner * AGs we don't find ready for initialisation. 1901c1c6ebcSDave Chinner */ 1911c1c6ebcSDave Chinner for (index = 0; index < agcount; index++) { 1921c1c6ebcSDave Chinner pag = xfs_perag_get(mp, index); 1931c1c6ebcSDave Chinner if (pag) { 1941c1c6ebcSDave Chinner xfs_perag_put(pag); 1951c1c6ebcSDave Chinner continue; 1961c1c6ebcSDave Chinner } 197fb3b504aSChristoph Hellwig 1981c1c6ebcSDave Chinner pag = kmem_zalloc(sizeof(*pag), KM_MAYFAIL); 199595189c2SYu Kuai if (!pag) { 200595189c2SYu Kuai error = -ENOMEM; 201b20fe473SBill O'Donnell goto out_unwind_new_pags; 202595189c2SYu Kuai } 203fb3b504aSChristoph Hellwig pag->pag_agno = index; 204fb3b504aSChristoph Hellwig pag->pag_mount = mp; 2051a427ab0SDave Chinner spin_lock_init(&pag->pag_ici_lock); 206894ecacfSDarrick J. Wong INIT_DELAYED_WORK(&pag->pag_blockgc_work, xfs_blockgc_worker); 207fb3b504aSChristoph Hellwig INIT_RADIX_TREE(&pag->pag_ici_root, GFP_ATOMIC); 208595189c2SYu Kuai 209595189c2SYu Kuai error = xfs_buf_hash_init(pag); 210595189c2SYu Kuai if (error) 211b20fe473SBill O'Donnell goto out_free_pag; 212ebf55872SChristoph Hellwig init_waitqueue_head(&pag->pagb_wait); 213ff23f4afSDarrick J. Wong spin_lock_init(&pag->pagb_lock); 214ff23f4afSDarrick J. Wong pag->pagb_count = 0; 215ff23f4afSDarrick J. Wong pag->pagb_tree = RB_ROOT; 216fb3b504aSChristoph Hellwig 217595189c2SYu Kuai error = radix_tree_preload(GFP_NOFS); 218595189c2SYu Kuai if (error) 219b20fe473SBill O'Donnell goto out_hash_destroy; 220fb3b504aSChristoph Hellwig 2211c1c6ebcSDave Chinner spin_lock(&mp->m_perag_lock); 2221c1c6ebcSDave Chinner if (radix_tree_insert(&mp->m_perag_tree, index, pag)) { 223eb2e9994SAustin Kim WARN_ON_ONCE(1); 2241c1c6ebcSDave Chinner spin_unlock(&mp->m_perag_lock); 2258b26c582SDave Chinner radix_tree_preload_end(); 2268b26c582SDave Chinner error = -EEXIST; 227b20fe473SBill O'Donnell goto out_hash_destroy; 2281c1c6ebcSDave Chinner } 2291c1c6ebcSDave Chinner spin_unlock(&mp->m_perag_lock); 2301c1c6ebcSDave Chinner radix_tree_preload_end(); 231b20fe473SBill O'Donnell /* first new pag is fully initialized */ 232b20fe473SBill O'Donnell if (first_initialised == NULLAGNUMBER) 233b20fe473SBill O'Donnell first_initialised = index; 2349b247179SDarrick J. Wong error = xfs_iunlink_init(pag); 2359b247179SDarrick J. Wong if (error) 2369b247179SDarrick J. Wong goto out_hash_destroy; 2376772c1f1SDarrick J. Wong spin_lock_init(&pag->pag_state_lock); 2381c1c6ebcSDave Chinner } 2391c1c6ebcSDave Chinner 24012c3f05cSEric Sandeen index = xfs_set_inode_alloc(mp, agcount); 241fb3b504aSChristoph Hellwig 2421c1c6ebcSDave Chinner if (maxagi) 2431c1c6ebcSDave Chinner *maxagi = index; 2448018026eSDarrick J. Wong 2458018026eSDarrick J. Wong mp->m_ag_prealloc_blocks = xfs_prealloc_blocks(mp); 2461c1c6ebcSDave Chinner return 0; 2478b26c582SDave Chinner 248b20fe473SBill O'Donnell out_hash_destroy: 2496031e73aSLucas Stach xfs_buf_hash_destroy(pag); 250b20fe473SBill O'Donnell out_free_pag: 2518b26c582SDave Chinner kmem_free(pag); 252b20fe473SBill O'Donnell out_unwind_new_pags: 253b20fe473SBill O'Donnell /* unwind any prior newly initialized pags */ 254b20fe473SBill O'Donnell for (index = first_initialised; index < agcount; index++) { 2558b26c582SDave Chinner pag = radix_tree_delete(&mp->m_perag_tree, index); 256b20fe473SBill O'Donnell if (!pag) 257b20fe473SBill O'Donnell break; 2586031e73aSLucas Stach xfs_buf_hash_destroy(pag); 2599b247179SDarrick J. Wong xfs_iunlink_destroy(pag); 2608b26c582SDave Chinner kmem_free(pag); 2618b26c582SDave Chinner } 2628b26c582SDave Chinner return error; 2631da177e4SLinus Torvalds } 2641da177e4SLinus Torvalds 2651da177e4SLinus Torvalds /* 2661da177e4SLinus Torvalds * xfs_readsb 2671da177e4SLinus Torvalds * 2681da177e4SLinus Torvalds * Does the initial read of the superblock. 2691da177e4SLinus Torvalds */ 2701da177e4SLinus Torvalds int 271ff55068cSDave Chinner xfs_readsb( 272ff55068cSDave Chinner struct xfs_mount *mp, 273ff55068cSDave Chinner int flags) 2741da177e4SLinus Torvalds { 2751da177e4SLinus Torvalds unsigned int sector_size; 27604a1e6c5SDave Chinner struct xfs_buf *bp; 27704a1e6c5SDave Chinner struct xfs_sb *sbp = &mp->m_sb; 2781da177e4SLinus Torvalds int error; 279af34e09dSDave Chinner int loud = !(flags & XFS_MFSI_QUIET); 280daba5427SEric Sandeen const struct xfs_buf_ops *buf_ops; 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds ASSERT(mp->m_sb_bp == NULL); 2831da177e4SLinus Torvalds ASSERT(mp->m_ddev_targp != NULL); 2841da177e4SLinus Torvalds 2851da177e4SLinus Torvalds /* 286daba5427SEric Sandeen * For the initial read, we must guess at the sector 287daba5427SEric Sandeen * size based on the block device. It's enough to 288daba5427SEric Sandeen * get the sb_sectsize out of the superblock and 289daba5427SEric Sandeen * then reread with the proper length. 290daba5427SEric Sandeen * We don't verify it yet, because it may not be complete. 291daba5427SEric Sandeen */ 292daba5427SEric Sandeen sector_size = xfs_getsize_buftarg(mp->m_ddev_targp); 293daba5427SEric Sandeen buf_ops = NULL; 294daba5427SEric Sandeen 295daba5427SEric Sandeen /* 296c891c30aSBrian Foster * Allocate a (locked) buffer to hold the superblock. This will be kept 297c891c30aSBrian Foster * around at all times to optimize access to the superblock. Therefore, 298c891c30aSBrian Foster * set XBF_NO_IOACCT to make sure it doesn't hold the buftarg count 299c891c30aSBrian Foster * elevated. 3001da177e4SLinus Torvalds */ 30126af6552SDave Chinner reread: 302ba372674SDave Chinner error = xfs_buf_read_uncached(mp->m_ddev_targp, XFS_SB_DADDR, 303c891c30aSBrian Foster BTOBB(sector_size), XBF_NO_IOACCT, &bp, 304c891c30aSBrian Foster buf_ops); 305ba372674SDave Chinner if (error) { 306eab4e633SDave Chinner if (loud) 307e721f504SDave Chinner xfs_warn(mp, "SB validate failed with error %d.", error); 308ac75a1f7SDave Chinner /* bad CRC means corrupted metadata */ 3092451337dSDave Chinner if (error == -EFSBADCRC) 3102451337dSDave Chinner error = -EFSCORRUPTED; 311ba372674SDave Chinner return error; 312eab4e633SDave Chinner } 3131da177e4SLinus Torvalds 3141da177e4SLinus Torvalds /* 3151da177e4SLinus Torvalds * Initialize the mount structure from the superblock. 3161da177e4SLinus Torvalds */ 3173e6e8afdSChristoph Hellwig xfs_sb_from_disk(sbp, bp->b_addr); 318556b8883SDave Chinner 319556b8883SDave Chinner /* 320556b8883SDave Chinner * If we haven't validated the superblock, do so now before we try 321556b8883SDave Chinner * to check the sector size and reread the superblock appropriately. 322556b8883SDave Chinner */ 323556b8883SDave Chinner if (sbp->sb_magicnum != XFS_SB_MAGIC) { 324556b8883SDave Chinner if (loud) 325556b8883SDave Chinner xfs_warn(mp, "Invalid superblock magic number"); 3262451337dSDave Chinner error = -EINVAL; 327556b8883SDave Chinner goto release_buf; 328556b8883SDave Chinner } 329ff55068cSDave Chinner 3301da177e4SLinus Torvalds /* 3311da177e4SLinus Torvalds * We must be able to do sector-sized and sector-aligned IO. 3321da177e4SLinus Torvalds */ 33304a1e6c5SDave Chinner if (sector_size > sbp->sb_sectsize) { 334af34e09dSDave Chinner if (loud) 335af34e09dSDave Chinner xfs_warn(mp, "device supports %u byte sectors (not %u)", 33604a1e6c5SDave Chinner sector_size, sbp->sb_sectsize); 3372451337dSDave Chinner error = -ENOSYS; 33826af6552SDave Chinner goto release_buf; 3391da177e4SLinus Torvalds } 3401da177e4SLinus Torvalds 341556b8883SDave Chinner if (buf_ops == NULL) { 3421da177e4SLinus Torvalds /* 343daba5427SEric Sandeen * Re-read the superblock so the buffer is correctly sized, 344daba5427SEric Sandeen * and properly verified. 3451da177e4SLinus Torvalds */ 3461da177e4SLinus Torvalds xfs_buf_relse(bp); 34704a1e6c5SDave Chinner sector_size = sbp->sb_sectsize; 348daba5427SEric Sandeen buf_ops = loud ? &xfs_sb_buf_ops : &xfs_sb_quiet_buf_ops; 34926af6552SDave Chinner goto reread; 3501da177e4SLinus Torvalds } 3511da177e4SLinus Torvalds 3525681ca40SDave Chinner xfs_reinit_percpu_counters(mp); 3538d280b98SDavid Chinner 35404a1e6c5SDave Chinner /* no need to be quiet anymore, so reset the buf ops */ 35504a1e6c5SDave Chinner bp->b_ops = &xfs_sb_buf_ops; 35604a1e6c5SDave Chinner 3571da177e4SLinus Torvalds mp->m_sb_bp = bp; 35826af6552SDave Chinner xfs_buf_unlock(bp); 3591da177e4SLinus Torvalds return 0; 3601da177e4SLinus Torvalds 36126af6552SDave Chinner release_buf: 3621da177e4SLinus Torvalds xfs_buf_relse(bp); 3631da177e4SLinus Torvalds return error; 3641da177e4SLinus Torvalds } 3651da177e4SLinus Torvalds 3661da177e4SLinus Torvalds /* 36713eaec4bSDarrick J. Wong * If the sunit/swidth change would move the precomputed root inode value, we 36813eaec4bSDarrick J. Wong * must reject the ondisk change because repair will stumble over that. 36913eaec4bSDarrick J. Wong * However, we allow the mount to proceed because we never rejected this 37013eaec4bSDarrick J. Wong * combination before. Returns true to update the sb, false otherwise. 37113eaec4bSDarrick J. Wong */ 37213eaec4bSDarrick J. Wong static inline int 37313eaec4bSDarrick J. Wong xfs_check_new_dalign( 37413eaec4bSDarrick J. Wong struct xfs_mount *mp, 37513eaec4bSDarrick J. Wong int new_dalign, 37613eaec4bSDarrick J. Wong bool *update_sb) 37713eaec4bSDarrick J. Wong { 37813eaec4bSDarrick J. Wong struct xfs_sb *sbp = &mp->m_sb; 37913eaec4bSDarrick J. Wong xfs_ino_t calc_ino; 38013eaec4bSDarrick J. Wong 38113eaec4bSDarrick J. Wong calc_ino = xfs_ialloc_calc_rootino(mp, new_dalign); 38213eaec4bSDarrick J. Wong trace_xfs_check_new_dalign(mp, new_dalign, calc_ino); 38313eaec4bSDarrick J. Wong 38413eaec4bSDarrick J. Wong if (sbp->sb_rootino == calc_ino) { 38513eaec4bSDarrick J. Wong *update_sb = true; 38613eaec4bSDarrick J. Wong return 0; 38713eaec4bSDarrick J. Wong } 38813eaec4bSDarrick J. Wong 38913eaec4bSDarrick J. Wong xfs_warn(mp, 39013eaec4bSDarrick J. Wong "Cannot change stripe alignment; would require moving root inode."); 39113eaec4bSDarrick J. Wong 39213eaec4bSDarrick J. Wong /* 39313eaec4bSDarrick J. Wong * XXX: Next time we add a new incompat feature, this should start 39413eaec4bSDarrick J. Wong * returning -EINVAL to fail the mount. Until then, spit out a warning 39513eaec4bSDarrick J. Wong * that we're ignoring the administrator's instructions. 39613eaec4bSDarrick J. Wong */ 39713eaec4bSDarrick J. Wong xfs_warn(mp, "Skipping superblock stripe alignment update."); 39813eaec4bSDarrick J. Wong *update_sb = false; 39913eaec4bSDarrick J. Wong return 0; 40013eaec4bSDarrick J. Wong } 40113eaec4bSDarrick J. Wong 40213eaec4bSDarrick J. Wong /* 4034f5b1b3aSDarrick J. Wong * If we were provided with new sunit/swidth values as mount options, make sure 4044f5b1b3aSDarrick J. Wong * that they pass basic alignment and superblock feature checks, and convert 4054f5b1b3aSDarrick J. Wong * them into the same units (FSB) that everything else expects. This step 4064f5b1b3aSDarrick J. Wong * /must/ be done before computing the inode geometry. 4071da177e4SLinus Torvalds */ 4080771fb45SEric Sandeen STATIC int 4094f5b1b3aSDarrick J. Wong xfs_validate_new_dalign( 4104f5b1b3aSDarrick J. Wong struct xfs_mount *mp) 4111da177e4SLinus Torvalds { 4124f5b1b3aSDarrick J. Wong if (mp->m_dalign == 0) 4134f5b1b3aSDarrick J. Wong return 0; 4141da177e4SLinus Torvalds 4151da177e4SLinus Torvalds /* 4161da177e4SLinus Torvalds * If stripe unit and stripe width are not multiples 4171da177e4SLinus Torvalds * of the fs blocksize turn off alignment. 4181da177e4SLinus Torvalds */ 4191da177e4SLinus Torvalds if ((BBTOB(mp->m_dalign) & mp->m_blockmask) || 4201da177e4SLinus Torvalds (BBTOB(mp->m_swidth) & mp->m_blockmask)) { 42139a45d84SJie Liu xfs_warn(mp, 42239a45d84SJie Liu "alignment check failed: sunit/swidth vs. blocksize(%d)", 4234f5b1b3aSDarrick J. Wong mp->m_sb.sb_blocksize); 4242451337dSDave Chinner return -EINVAL; 4251da177e4SLinus Torvalds } else { 4261da177e4SLinus Torvalds /* 4271da177e4SLinus Torvalds * Convert the stripe unit and width to FSBs. 4281da177e4SLinus Torvalds */ 4291da177e4SLinus Torvalds mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign); 4304f5b1b3aSDarrick J. Wong if (mp->m_dalign && (mp->m_sb.sb_agblocks % mp->m_dalign)) { 43153487786SDave Chinner xfs_warn(mp, 43239a45d84SJie Liu "alignment check failed: sunit/swidth vs. agsize(%d)", 4334f5b1b3aSDarrick J. Wong mp->m_sb.sb_agblocks); 4342451337dSDave Chinner return -EINVAL; 4351da177e4SLinus Torvalds } else if (mp->m_dalign) { 4361da177e4SLinus Torvalds mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth); 4371da177e4SLinus Torvalds } else { 43839a45d84SJie Liu xfs_warn(mp, 43939a45d84SJie Liu "alignment check failed: sunit(%d) less than bsize(%d)", 4404f5b1b3aSDarrick J. Wong mp->m_dalign, mp->m_sb.sb_blocksize); 4412451337dSDave Chinner return -EINVAL; 4421da177e4SLinus Torvalds } 4431da177e4SLinus Torvalds } 4441da177e4SLinus Torvalds 4454f5b1b3aSDarrick J. Wong if (!xfs_sb_version_hasdalign(&mp->m_sb)) { 44634d7f603SJie Liu xfs_warn(mp, 44734d7f603SJie Liu "cannot change alignment: superblock does not support data alignment"); 4482451337dSDave Chinner return -EINVAL; 4491da177e4SLinus Torvalds } 4504f5b1b3aSDarrick J. Wong 4514f5b1b3aSDarrick J. Wong return 0; 4524f5b1b3aSDarrick J. Wong } 4534f5b1b3aSDarrick J. Wong 4544f5b1b3aSDarrick J. Wong /* Update alignment values based on mount options and sb values. */ 4554f5b1b3aSDarrick J. Wong STATIC int 4564f5b1b3aSDarrick J. Wong xfs_update_alignment( 4574f5b1b3aSDarrick J. Wong struct xfs_mount *mp) 4584f5b1b3aSDarrick J. Wong { 4594f5b1b3aSDarrick J. Wong struct xfs_sb *sbp = &mp->m_sb; 4604f5b1b3aSDarrick J. Wong 4614f5b1b3aSDarrick J. Wong if (mp->m_dalign) { 46213eaec4bSDarrick J. Wong bool update_sb; 46313eaec4bSDarrick J. Wong int error; 46413eaec4bSDarrick J. Wong 4654f5b1b3aSDarrick J. Wong if (sbp->sb_unit == mp->m_dalign && 4664f5b1b3aSDarrick J. Wong sbp->sb_width == mp->m_swidth) 4674f5b1b3aSDarrick J. Wong return 0; 4684f5b1b3aSDarrick J. Wong 46913eaec4bSDarrick J. Wong error = xfs_check_new_dalign(mp, mp->m_dalign, &update_sb); 47013eaec4bSDarrick J. Wong if (error || !update_sb) 47113eaec4bSDarrick J. Wong return error; 47213eaec4bSDarrick J. Wong 4734f5b1b3aSDarrick J. Wong sbp->sb_unit = mp->m_dalign; 4744f5b1b3aSDarrick J. Wong sbp->sb_width = mp->m_swidth; 4754f5b1b3aSDarrick J. Wong mp->m_update_sb = true; 4761da177e4SLinus Torvalds } else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN && 47762118709SEric Sandeen xfs_sb_version_hasdalign(&mp->m_sb)) { 4781da177e4SLinus Torvalds mp->m_dalign = sbp->sb_unit; 4791da177e4SLinus Torvalds mp->m_swidth = sbp->sb_width; 4801da177e4SLinus Torvalds } 4811da177e4SLinus Torvalds 4820771fb45SEric Sandeen return 0; 4830771fb45SEric Sandeen } 4841da177e4SLinus Torvalds 4850771fb45SEric Sandeen /* 486055388a3SDave Chinner * precalculate the low space thresholds for dynamic speculative preallocation. 487055388a3SDave Chinner */ 488055388a3SDave Chinner void 489055388a3SDave Chinner xfs_set_low_space_thresholds( 490055388a3SDave Chinner struct xfs_mount *mp) 491055388a3SDave Chinner { 492055388a3SDave Chinner int i; 493055388a3SDave Chinner 494055388a3SDave Chinner for (i = 0; i < XFS_LOWSP_MAX; i++) { 495c8ce540dSDarrick J. Wong uint64_t space = mp->m_sb.sb_dblocks; 496055388a3SDave Chinner 497055388a3SDave Chinner do_div(space, 100); 498055388a3SDave Chinner mp->m_low_space[i] = space * (i + 1); 499055388a3SDave Chinner } 500055388a3SDave Chinner } 501055388a3SDave Chinner 5021da177e4SLinus Torvalds /* 5030471f62eSZhi Yong Wu * Check that the data (and log if separate) is an ok size. 5041da177e4SLinus Torvalds */ 5050771fb45SEric Sandeen STATIC int 506ba372674SDave Chinner xfs_check_sizes( 507ba372674SDave Chinner struct xfs_mount *mp) 5080771fb45SEric Sandeen { 509ba372674SDave Chinner struct xfs_buf *bp; 5100771fb45SEric Sandeen xfs_daddr_t d; 511ba372674SDave Chinner int error; 5120771fb45SEric Sandeen 5131da177e4SLinus Torvalds d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks); 5141da177e4SLinus Torvalds if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) { 5150b932cccSDave Chinner xfs_warn(mp, "filesystem size mismatch detected"); 5162451337dSDave Chinner return -EFBIG; 5171da177e4SLinus Torvalds } 518ba372674SDave Chinner error = xfs_buf_read_uncached(mp->m_ddev_targp, 5191da177e4SLinus Torvalds d - XFS_FSS_TO_BB(mp, 1), 520ba372674SDave Chinner XFS_FSS_TO_BB(mp, 1), 0, &bp, NULL); 521ba372674SDave Chinner if (error) { 5220b932cccSDave Chinner xfs_warn(mp, "last sector read failed"); 523ba372674SDave Chinner return error; 5241da177e4SLinus Torvalds } 5251922c949SDave Chinner xfs_buf_relse(bp); 5261da177e4SLinus Torvalds 527ba372674SDave Chinner if (mp->m_logdev_targp == mp->m_ddev_targp) 528ba372674SDave Chinner return 0; 529ba372674SDave Chinner 5301da177e4SLinus Torvalds d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks); 5311da177e4SLinus Torvalds if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) { 5320b932cccSDave Chinner xfs_warn(mp, "log size mismatch detected"); 5332451337dSDave Chinner return -EFBIG; 5341da177e4SLinus Torvalds } 535ba372674SDave Chinner error = xfs_buf_read_uncached(mp->m_logdev_targp, 5361da177e4SLinus Torvalds d - XFS_FSB_TO_BB(mp, 1), 537ba372674SDave Chinner XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL); 538ba372674SDave Chinner if (error) { 5390b932cccSDave Chinner xfs_warn(mp, "log device read failed"); 540ba372674SDave Chinner return error; 5411da177e4SLinus Torvalds } 5421922c949SDave Chinner xfs_buf_relse(bp); 5430771fb45SEric Sandeen return 0; 5440771fb45SEric Sandeen } 5450771fb45SEric Sandeen 5460771fb45SEric Sandeen /* 5477d095257SChristoph Hellwig * Clear the quotaflags in memory and in the superblock. 5487d095257SChristoph Hellwig */ 5497d095257SChristoph Hellwig int 5507d095257SChristoph Hellwig xfs_mount_reset_sbqflags( 5517d095257SChristoph Hellwig struct xfs_mount *mp) 5527d095257SChristoph Hellwig { 5537d095257SChristoph Hellwig mp->m_qflags = 0; 5547d095257SChristoph Hellwig 55561e63ecbSDave Chinner /* It is OK to look at sb_qflags in the mount path without m_sb_lock. */ 5567d095257SChristoph Hellwig if (mp->m_sb.sb_qflags == 0) 5577d095257SChristoph Hellwig return 0; 5587d095257SChristoph Hellwig spin_lock(&mp->m_sb_lock); 5597d095257SChristoph Hellwig mp->m_sb.sb_qflags = 0; 5607d095257SChristoph Hellwig spin_unlock(&mp->m_sb_lock); 5617d095257SChristoph Hellwig 56261e63ecbSDave Chinner if (!xfs_fs_writable(mp, SB_FREEZE_WRITE)) 5637d095257SChristoph Hellwig return 0; 5647d095257SChristoph Hellwig 56561e63ecbSDave Chinner return xfs_sync_sb(mp, false); 5667d095257SChristoph Hellwig } 5677d095257SChristoph Hellwig 568c8ce540dSDarrick J. Wong uint64_t 569d5db0f97SEric Sandeen xfs_default_resblks(xfs_mount_t *mp) 570d5db0f97SEric Sandeen { 571c8ce540dSDarrick J. Wong uint64_t resblks; 572d5db0f97SEric Sandeen 573d5db0f97SEric Sandeen /* 5748babd8a2SDave Chinner * We default to 5% or 8192 fsbs of space reserved, whichever is 5758babd8a2SDave Chinner * smaller. This is intended to cover concurrent allocation 5768babd8a2SDave Chinner * transactions when we initially hit enospc. These each require a 4 5778babd8a2SDave Chinner * block reservation. Hence by default we cover roughly 2000 concurrent 5788babd8a2SDave Chinner * allocation reservations. 579d5db0f97SEric Sandeen */ 580d5db0f97SEric Sandeen resblks = mp->m_sb.sb_dblocks; 581d5db0f97SEric Sandeen do_div(resblks, 20); 582c8ce540dSDarrick J. Wong resblks = min_t(uint64_t, resblks, 8192); 583d5db0f97SEric Sandeen return resblks; 584d5db0f97SEric Sandeen } 585d5db0f97SEric Sandeen 5862e9e6481SDarrick J. Wong /* Ensure the summary counts are correct. */ 5872e9e6481SDarrick J. Wong STATIC int 5882e9e6481SDarrick J. Wong xfs_check_summary_counts( 5892e9e6481SDarrick J. Wong struct xfs_mount *mp) 5902e9e6481SDarrick J. Wong { 5912e9e6481SDarrick J. Wong /* 5922e9e6481SDarrick J. Wong * The AG0 superblock verifier rejects in-progress filesystems, 5932e9e6481SDarrick J. Wong * so we should never see the flag set this far into mounting. 5942e9e6481SDarrick J. Wong */ 5952e9e6481SDarrick J. Wong if (mp->m_sb.sb_inprogress) { 5962e9e6481SDarrick J. Wong xfs_err(mp, "sb_inprogress set after log recovery??"); 5972e9e6481SDarrick J. Wong WARN_ON(1); 5982e9e6481SDarrick J. Wong return -EFSCORRUPTED; 5992e9e6481SDarrick J. Wong } 6002e9e6481SDarrick J. Wong 6012e9e6481SDarrick J. Wong /* 6022e9e6481SDarrick J. Wong * Now the log is mounted, we know if it was an unclean shutdown or 6032e9e6481SDarrick J. Wong * not. If it was, with the first phase of recovery has completed, we 6042e9e6481SDarrick J. Wong * have consistent AG blocks on disk. We have not recovered EFIs yet, 6052e9e6481SDarrick J. Wong * but they are recovered transactionally in the second recovery phase 6062e9e6481SDarrick J. Wong * later. 6072e9e6481SDarrick J. Wong * 6082e9e6481SDarrick J. Wong * If the log was clean when we mounted, we can check the summary 6092e9e6481SDarrick J. Wong * counters. If any of them are obviously incorrect, we can recompute 6102e9e6481SDarrick J. Wong * them from the AGF headers in the next step. 6112e9e6481SDarrick J. Wong */ 6122e9e6481SDarrick J. Wong if (XFS_LAST_UNMOUNT_WAS_CLEAN(mp) && 6132e9e6481SDarrick J. Wong (mp->m_sb.sb_fdblocks > mp->m_sb.sb_dblocks || 61400d22a1cSDarrick J. Wong !xfs_verify_icount(mp, mp->m_sb.sb_icount) || 6152e9e6481SDarrick J. Wong mp->m_sb.sb_ifree > mp->m_sb.sb_icount)) 61639353ff6SDarrick J. Wong xfs_fs_mark_sick(mp, XFS_SICK_FS_COUNTERS); 6172e9e6481SDarrick J. Wong 6182e9e6481SDarrick J. Wong /* 6192e9e6481SDarrick J. Wong * We can safely re-initialise incore superblock counters from the 6202e9e6481SDarrick J. Wong * per-ag data. These may not be correct if the filesystem was not 6212e9e6481SDarrick J. Wong * cleanly unmounted, so we waited for recovery to finish before doing 6222e9e6481SDarrick J. Wong * this. 6232e9e6481SDarrick J. Wong * 6242e9e6481SDarrick J. Wong * If the filesystem was cleanly unmounted or the previous check did 6252e9e6481SDarrick J. Wong * not flag anything weird, then we can trust the values in the 6262e9e6481SDarrick J. Wong * superblock to be correct and we don't need to do anything here. 6272e9e6481SDarrick J. Wong * Otherwise, recalculate the summary counters. 6282e9e6481SDarrick J. Wong */ 6292e9e6481SDarrick J. Wong if ((!xfs_sb_version_haslazysbcount(&mp->m_sb) || 6302e9e6481SDarrick J. Wong XFS_LAST_UNMOUNT_WAS_CLEAN(mp)) && 63139353ff6SDarrick J. Wong !xfs_fs_has_sickness(mp, XFS_SICK_FS_COUNTERS)) 6322e9e6481SDarrick J. Wong return 0; 6332e9e6481SDarrick J. Wong 6342e9e6481SDarrick J. Wong return xfs_initialize_perag_data(mp, mp->m_sb.sb_agcount); 6352e9e6481SDarrick J. Wong } 6362e9e6481SDarrick J. Wong 6377d095257SChristoph Hellwig /* 638d336f7ebSDarrick J. Wong * Flush and reclaim dirty inodes in preparation for unmount. Inodes and 639d336f7ebSDarrick J. Wong * internal inode structures can be sitting in the CIL and AIL at this point, 640d336f7ebSDarrick J. Wong * so we need to unpin them, write them back and/or reclaim them before unmount 641d336f7ebSDarrick J. Wong * can proceed. 642d336f7ebSDarrick J. Wong * 643d336f7ebSDarrick J. Wong * An inode cluster that has been freed can have its buffer still pinned in 644d336f7ebSDarrick J. Wong * memory because the transaction is still sitting in a iclog. The stale inodes 645d336f7ebSDarrick J. Wong * on that buffer will be pinned to the buffer until the transaction hits the 646d336f7ebSDarrick J. Wong * disk and the callbacks run. Pushing the AIL will skip the stale inodes and 647d336f7ebSDarrick J. Wong * may never see the pinned buffer, so nothing will push out the iclog and 648d336f7ebSDarrick J. Wong * unpin the buffer. 649d336f7ebSDarrick J. Wong * 650d336f7ebSDarrick J. Wong * Hence we need to force the log to unpin everything first. However, log 651d336f7ebSDarrick J. Wong * forces don't wait for the discards they issue to complete, so we have to 652d336f7ebSDarrick J. Wong * explicitly wait for them to complete here as well. 653d336f7ebSDarrick J. Wong * 654d336f7ebSDarrick J. Wong * Then we can tell the world we are unmounting so that error handling knows 655d336f7ebSDarrick J. Wong * that the filesystem is going away and we should error out anything that we 656d336f7ebSDarrick J. Wong * have been retrying in the background. This will prevent never-ending 657d336f7ebSDarrick J. Wong * retries in AIL pushing from hanging the unmount. 658d336f7ebSDarrick J. Wong * 659d336f7ebSDarrick J. Wong * Finally, we can push the AIL to clean all the remaining dirty objects, then 660d336f7ebSDarrick J. Wong * reclaim the remaining inodes that are still in memory at this point in time. 661d336f7ebSDarrick J. Wong */ 662d336f7ebSDarrick J. Wong static void 663d336f7ebSDarrick J. Wong xfs_unmount_flush_inodes( 664d336f7ebSDarrick J. Wong struct xfs_mount *mp) 665d336f7ebSDarrick J. Wong { 666d336f7ebSDarrick J. Wong xfs_log_force(mp, XFS_LOG_SYNC); 667d336f7ebSDarrick J. Wong xfs_extent_busy_wait_all(mp); 668d336f7ebSDarrick J. Wong flush_workqueue(xfs_discard_wq); 669d336f7ebSDarrick J. Wong 670d336f7ebSDarrick J. Wong mp->m_flags |= XFS_MOUNT_UNMOUNTING; 671d336f7ebSDarrick J. Wong 672d336f7ebSDarrick J. Wong xfs_ail_push_all_sync(mp->m_ail); 673d336f7ebSDarrick J. Wong cancel_delayed_work_sync(&mp->m_reclaim_work); 674d336f7ebSDarrick J. Wong xfs_reclaim_inodes(mp); 675d336f7ebSDarrick J. Wong xfs_health_unmount(mp); 676d336f7ebSDarrick J. Wong } 677d336f7ebSDarrick J. Wong 678b2941046SDave Chinner static void 679b2941046SDave Chinner xfs_mount_setup_inode_geom( 680b2941046SDave Chinner struct xfs_mount *mp) 681b2941046SDave Chinner { 682b2941046SDave Chinner struct xfs_ino_geometry *igeo = M_IGEO(mp); 683b2941046SDave Chinner 684b2941046SDave Chinner igeo->attr_fork_offset = xfs_bmap_compute_attr_offset(mp); 685b2941046SDave Chinner ASSERT(igeo->attr_fork_offset < XFS_LITINO(mp)); 686b2941046SDave Chinner 687b2941046SDave Chinner xfs_ialloc_setup_geometry(mp); 688b2941046SDave Chinner } 689b2941046SDave Chinner 690d336f7ebSDarrick J. Wong /* 6910771fb45SEric Sandeen * This function does the following on an initial mount of a file system: 6920771fb45SEric Sandeen * - reads the superblock from disk and init the mount struct 6930771fb45SEric Sandeen * - if we're a 32-bit kernel, do a size check on the superblock 6940771fb45SEric Sandeen * so we don't mount terabyte filesystems 6950771fb45SEric Sandeen * - init mount struct realtime fields 6960771fb45SEric Sandeen * - allocate inode hash table for fs 6970771fb45SEric Sandeen * - init directory manager 6980771fb45SEric Sandeen * - perform recovery and init the log manager 6990771fb45SEric Sandeen */ 7000771fb45SEric Sandeen int 7010771fb45SEric Sandeen xfs_mountfs( 702f0b2efadSBrian Foster struct xfs_mount *mp) 7030771fb45SEric Sandeen { 704f0b2efadSBrian Foster struct xfs_sb *sbp = &(mp->m_sb); 705f0b2efadSBrian Foster struct xfs_inode *rip; 706ef325959SDarrick J. Wong struct xfs_ino_geometry *igeo = M_IGEO(mp); 707c8ce540dSDarrick J. Wong uint64_t resblks; 7087d095257SChristoph Hellwig uint quotamount = 0; 7097d095257SChristoph Hellwig uint quotaflags = 0; 7100771fb45SEric Sandeen int error = 0; 7110771fb45SEric Sandeen 712ff55068cSDave Chinner xfs_sb_mount_common(mp, sbp); 7130771fb45SEric Sandeen 7140771fb45SEric Sandeen /* 715074e427bSDave Chinner * Check for a mismatched features2 values. Older kernels read & wrote 716074e427bSDave Chinner * into the wrong sb offset for sb_features2 on some platforms due to 717074e427bSDave Chinner * xfs_sb_t not being 64bit size aligned when sb_features2 was added, 718074e427bSDave Chinner * which made older superblock reading/writing routines swap it as a 719074e427bSDave Chinner * 64-bit value. 720ee1c0908SDavid Chinner * 721e6957ea4SEric Sandeen * For backwards compatibility, we make both slots equal. 722e6957ea4SEric Sandeen * 723074e427bSDave Chinner * If we detect a mismatched field, we OR the set bits into the existing 724074e427bSDave Chinner * features2 field in case it has already been modified; we don't want 725074e427bSDave Chinner * to lose any features. We then update the bad location with the ORed 726074e427bSDave Chinner * value so that older kernels will see any features2 flags. The 727074e427bSDave Chinner * superblock writeback code ensures the new sb_features2 is copied to 728074e427bSDave Chinner * sb_bad_features2 before it is logged or written to disk. 729ee1c0908SDavid Chinner */ 730e6957ea4SEric Sandeen if (xfs_sb_has_mismatched_features2(sbp)) { 7310b932cccSDave Chinner xfs_warn(mp, "correcting sb_features alignment problem"); 732ee1c0908SDavid Chinner sbp->sb_features2 |= sbp->sb_bad_features2; 73361e63ecbSDave Chinner mp->m_update_sb = true; 734e6957ea4SEric Sandeen 735e6957ea4SEric Sandeen /* 736e6957ea4SEric Sandeen * Re-check for ATTR2 in case it was found in bad_features2 737e6957ea4SEric Sandeen * slot. 738e6957ea4SEric Sandeen */ 7397c12f296STim Shimmin if (xfs_sb_version_hasattr2(&mp->m_sb) && 7407c12f296STim Shimmin !(mp->m_flags & XFS_MOUNT_NOATTR2)) 741e6957ea4SEric Sandeen mp->m_flags |= XFS_MOUNT_ATTR2; 7427c12f296STim Shimmin } 743e6957ea4SEric Sandeen 7447c12f296STim Shimmin if (xfs_sb_version_hasattr2(&mp->m_sb) && 7457c12f296STim Shimmin (mp->m_flags & XFS_MOUNT_NOATTR2)) { 7467c12f296STim Shimmin xfs_sb_version_removeattr2(&mp->m_sb); 74761e63ecbSDave Chinner mp->m_update_sb = true; 7487c12f296STim Shimmin 7497c12f296STim Shimmin /* update sb_versionnum for the clearing of the morebits */ 7507c12f296STim Shimmin if (!sbp->sb_features2) 75161e63ecbSDave Chinner mp->m_update_sb = true; 752ee1c0908SDavid Chinner } 753ee1c0908SDavid Chinner 754263997a6SDave Chinner /* always use v2 inodes by default now */ 755263997a6SDave Chinner if (!(mp->m_sb.sb_versionnum & XFS_SB_VERSION_NLINKBIT)) { 756263997a6SDave Chinner mp->m_sb.sb_versionnum |= XFS_SB_VERSION_NLINKBIT; 75761e63ecbSDave Chinner mp->m_update_sb = true; 758263997a6SDave Chinner } 759263997a6SDave Chinner 760ee1c0908SDavid Chinner /* 7614f5b1b3aSDarrick J. Wong * If we were given new sunit/swidth options, do some basic validation 7624f5b1b3aSDarrick J. Wong * checks and convert the incore dalign and swidth values to the 7634f5b1b3aSDarrick J. Wong * same units (FSB) that everything else uses. This /must/ happen 7644f5b1b3aSDarrick J. Wong * before computing the inode geometry. 7650771fb45SEric Sandeen */ 7664f5b1b3aSDarrick J. Wong error = xfs_validate_new_dalign(mp); 7670771fb45SEric Sandeen if (error) 768f9057e3dSChristoph Hellwig goto out; 7690771fb45SEric Sandeen 7700771fb45SEric Sandeen xfs_alloc_compute_maxlevels(mp); 7710771fb45SEric Sandeen xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK); 7720771fb45SEric Sandeen xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK); 773b2941046SDave Chinner xfs_mount_setup_inode_geom(mp); 774035e00acSDarrick J. Wong xfs_rmapbt_compute_maxlevels(mp); 7751946b91cSDarrick J. Wong xfs_refcountbt_compute_maxlevels(mp); 7760771fb45SEric Sandeen 7774f5b1b3aSDarrick J. Wong /* 7784f5b1b3aSDarrick J. Wong * Check if sb_agblocks is aligned at stripe boundary. If sb_agblocks 7794f5b1b3aSDarrick J. Wong * is NOT aligned turn off m_dalign since allocator alignment is within 7804f5b1b3aSDarrick J. Wong * an ag, therefore ag has to be aligned at stripe boundary. Note that 7814f5b1b3aSDarrick J. Wong * we must compute the free space and rmap btree geometry before doing 7824f5b1b3aSDarrick J. Wong * this. 7834f5b1b3aSDarrick J. Wong */ 7844f5b1b3aSDarrick J. Wong error = xfs_update_alignment(mp); 7854f5b1b3aSDarrick J. Wong if (error) 7864f5b1b3aSDarrick J. Wong goto out; 7874f5b1b3aSDarrick J. Wong 788e6b3bb78SCarlos Maiolino /* enable fail_at_unmount as default */ 789749f24f3SThomas Meyer mp->m_fail_unmount = true; 790e6b3bb78SCarlos Maiolino 791e1d3d218SIan Kent error = xfs_sysfs_init(&mp->m_kobj, &xfs_mp_ktype, 792e1d3d218SIan Kent NULL, mp->m_super->s_id); 79327174203SChristoph Hellwig if (error) 794f9057e3dSChristoph Hellwig goto out; 7951da177e4SLinus Torvalds 796225e4635SBill O'Donnell error = xfs_sysfs_init(&mp->m_stats.xs_kobj, &xfs_stats_ktype, 797225e4635SBill O'Donnell &mp->m_kobj, "stats"); 798a31b1d3dSBrian Foster if (error) 799a31b1d3dSBrian Foster goto out_remove_sysfs; 800a31b1d3dSBrian Foster 801192852beSCarlos Maiolino error = xfs_error_sysfs_init(mp); 802225e4635SBill O'Donnell if (error) 803225e4635SBill O'Donnell goto out_del_stats; 804225e4635SBill O'Donnell 80531965ef3SDarrick J. Wong error = xfs_errortag_init(mp); 80631965ef3SDarrick J. Wong if (error) 80731965ef3SDarrick J. Wong goto out_remove_error_sysfs; 808192852beSCarlos Maiolino 809192852beSCarlos Maiolino error = xfs_uuid_mount(mp); 810192852beSCarlos Maiolino if (error) 81131965ef3SDarrick J. Wong goto out_remove_errortag; 812192852beSCarlos Maiolino 8131da177e4SLinus Torvalds /* 8142fcddee8SChristoph Hellwig * Update the preferred write size based on the information from the 8152fcddee8SChristoph Hellwig * on-disk superblock. 8160771fb45SEric Sandeen */ 8172fcddee8SChristoph Hellwig mp->m_allocsize_log = 8182fcddee8SChristoph Hellwig max_t(uint32_t, sbp->sb_blocklog, mp->m_allocsize_log); 8192fcddee8SChristoph Hellwig mp->m_allocsize_blocks = 1U << (mp->m_allocsize_log - sbp->sb_blocklog); 8200771fb45SEric Sandeen 821055388a3SDave Chinner /* set the low space thresholds for dynamic preallocation */ 822055388a3SDave Chinner xfs_set_low_space_thresholds(mp); 823055388a3SDave Chinner 8240771fb45SEric Sandeen /* 825e5376fc1SBrian Foster * If enabled, sparse inode chunk alignment is expected to match the 826e5376fc1SBrian Foster * cluster size. Full inode chunk alignment must match the chunk size, 827e5376fc1SBrian Foster * but that is checked on sb read verification... 828e5376fc1SBrian Foster */ 829e5376fc1SBrian Foster if (xfs_sb_version_hassparseinodes(&mp->m_sb) && 830e5376fc1SBrian Foster mp->m_sb.sb_spino_align != 831490d451fSDarrick J. Wong XFS_B_TO_FSBT(mp, igeo->inode_cluster_size_raw)) { 832e5376fc1SBrian Foster xfs_warn(mp, 833e5376fc1SBrian Foster "Sparse inode block alignment (%u) must match cluster size (%llu).", 834e5376fc1SBrian Foster mp->m_sb.sb_spino_align, 835490d451fSDarrick J. Wong XFS_B_TO_FSBT(mp, igeo->inode_cluster_size_raw)); 836e5376fc1SBrian Foster error = -EINVAL; 837e5376fc1SBrian Foster goto out_remove_uuid; 838e5376fc1SBrian Foster } 839e5376fc1SBrian Foster 840e5376fc1SBrian Foster /* 841c2bfbc9bSZhi Yong Wu * Check that the data (and log if separate) is an ok size. 8420771fb45SEric Sandeen */ 8434249023aSChristoph Hellwig error = xfs_check_sizes(mp); 8440771fb45SEric Sandeen if (error) 845f9057e3dSChristoph Hellwig goto out_remove_uuid; 8460771fb45SEric Sandeen 8470771fb45SEric Sandeen /* 8481da177e4SLinus Torvalds * Initialize realtime fields in the mount structure 8491da177e4SLinus Torvalds */ 8500771fb45SEric Sandeen error = xfs_rtmount_init(mp); 8510771fb45SEric Sandeen if (error) { 8520b932cccSDave Chinner xfs_warn(mp, "RT mount failed"); 853f9057e3dSChristoph Hellwig goto out_remove_uuid; 8541da177e4SLinus Torvalds } 8551da177e4SLinus Torvalds 8561da177e4SLinus Torvalds /* 8571da177e4SLinus Torvalds * Copies the low order bits of the timestamp and the randomly 8581da177e4SLinus Torvalds * set "sequence" number out of a UUID. 8591da177e4SLinus Torvalds */ 860cb0ba6ccSChristoph Hellwig mp->m_fixedfsid[0] = 861cb0ba6ccSChristoph Hellwig (get_unaligned_be16(&sbp->sb_uuid.b[8]) << 16) | 862cb0ba6ccSChristoph Hellwig get_unaligned_be16(&sbp->sb_uuid.b[4]); 863cb0ba6ccSChristoph Hellwig mp->m_fixedfsid[1] = get_unaligned_be32(&sbp->sb_uuid.b[0]); 8641da177e4SLinus Torvalds 8650650b554SDave Chinner error = xfs_da_mount(mp); 8660650b554SDave Chinner if (error) { 8670650b554SDave Chinner xfs_warn(mp, "Failed dir/attr init: %d", error); 8680650b554SDave Chinner goto out_remove_uuid; 8690650b554SDave Chinner } 8701da177e4SLinus Torvalds 8711da177e4SLinus Torvalds /* 8721da177e4SLinus Torvalds * Initialize the precomputed transaction reservations values. 8731da177e4SLinus Torvalds */ 8741da177e4SLinus Torvalds xfs_trans_init(mp); 8751da177e4SLinus Torvalds 8761da177e4SLinus Torvalds /* 8771da177e4SLinus Torvalds * Allocate and initialize the per-ag data. 8781da177e4SLinus Torvalds */ 8791c1c6ebcSDave Chinner error = xfs_initialize_perag(mp, sbp->sb_agcount, &mp->m_maxagi); 8801c1c6ebcSDave Chinner if (error) { 8810b932cccSDave Chinner xfs_warn(mp, "Failed per-ag init: %d", error); 8820650b554SDave Chinner goto out_free_dir; 8831c1c6ebcSDave Chinner } 8841da177e4SLinus Torvalds 885a71895c5SDarrick J. Wong if (XFS_IS_CORRUPT(mp, !sbp->sb_logblocks)) { 8860b932cccSDave Chinner xfs_warn(mp, "no log defined"); 8872451337dSDave Chinner error = -EFSCORRUPTED; 888f9057e3dSChristoph Hellwig goto out_free_perag; 889f9057e3dSChristoph Hellwig } 890f9057e3dSChristoph Hellwig 8911da177e4SLinus Torvalds /* 892f0b2efadSBrian Foster * Log's mount-time initialization. The first part of recovery can place 893f0b2efadSBrian Foster * some items on the AIL, to be handled when recovery is finished or 894f0b2efadSBrian Foster * cancelled. 8951da177e4SLinus Torvalds */ 8961da177e4SLinus Torvalds error = xfs_log_mount(mp, mp->m_logdev_targp, 8971da177e4SLinus Torvalds XFS_FSB_TO_DADDR(mp, sbp->sb_logstart), 8981da177e4SLinus Torvalds XFS_FSB_TO_BB(mp, sbp->sb_logblocks)); 8991da177e4SLinus Torvalds if (error) { 9000b932cccSDave Chinner xfs_warn(mp, "log mount failed"); 901d4f3512bSDave Chinner goto out_fail_wait; 9021da177e4SLinus Torvalds } 9031da177e4SLinus Torvalds 9042e9e6481SDarrick J. Wong /* Make sure the summary counts are ok. */ 9052e9e6481SDarrick J. Wong error = xfs_check_summary_counts(mp); 906f9057e3dSChristoph Hellwig if (error) 9076eee8972Skbuild test robot goto out_log_dealloc; 908f9057e3dSChristoph Hellwig 90992821e2bSDavid Chinner /* 9101da177e4SLinus Torvalds * Get and sanity-check the root inode. 9111da177e4SLinus Torvalds * Save the pointer to it in the mount structure. 9121da177e4SLinus Torvalds */ 913541b5accSDave Chinner error = xfs_iget(mp, NULL, sbp->sb_rootino, XFS_IGET_UNTRUSTED, 914541b5accSDave Chinner XFS_ILOCK_EXCL, &rip); 9151da177e4SLinus Torvalds if (error) { 916541b5accSDave Chinner xfs_warn(mp, 917541b5accSDave Chinner "Failed to read root inode 0x%llx, error %d", 918541b5accSDave Chinner sbp->sb_rootino, -error); 919f9057e3dSChristoph Hellwig goto out_log_dealloc; 9201da177e4SLinus Torvalds } 9211da177e4SLinus Torvalds 9221da177e4SLinus Torvalds ASSERT(rip != NULL); 9231da177e4SLinus Torvalds 924a71895c5SDarrick J. Wong if (XFS_IS_CORRUPT(mp, !S_ISDIR(VFS_I(rip)->i_mode))) { 9250b932cccSDave Chinner xfs_warn(mp, "corrupted root inode %llu: not a directory", 926b6574520SNathan Scott (unsigned long long)rip->i_ino); 9271da177e4SLinus Torvalds xfs_iunlock(rip, XFS_ILOCK_EXCL); 9282451337dSDave Chinner error = -EFSCORRUPTED; 929f9057e3dSChristoph Hellwig goto out_rele_rip; 9301da177e4SLinus Torvalds } 9311da177e4SLinus Torvalds mp->m_rootip = rip; /* save it */ 9321da177e4SLinus Torvalds 9331da177e4SLinus Torvalds xfs_iunlock(rip, XFS_ILOCK_EXCL); 9341da177e4SLinus Torvalds 9351da177e4SLinus Torvalds /* 9361da177e4SLinus Torvalds * Initialize realtime inode pointers in the mount structure 9371da177e4SLinus Torvalds */ 9380771fb45SEric Sandeen error = xfs_rtmount_inodes(mp); 9390771fb45SEric Sandeen if (error) { 9401da177e4SLinus Torvalds /* 9411da177e4SLinus Torvalds * Free up the root inode. 9421da177e4SLinus Torvalds */ 9430b932cccSDave Chinner xfs_warn(mp, "failed to read RT inodes"); 944f9057e3dSChristoph Hellwig goto out_rele_rip; 9451da177e4SLinus Torvalds } 9461da177e4SLinus Torvalds 9471da177e4SLinus Torvalds /* 9487884bc86SChristoph Hellwig * If this is a read-only mount defer the superblock updates until 9497884bc86SChristoph Hellwig * the next remount into writeable mode. Otherwise we would never 9507884bc86SChristoph Hellwig * perform the update e.g. for the root filesystem. 9511da177e4SLinus Torvalds */ 95261e63ecbSDave Chinner if (mp->m_update_sb && !(mp->m_flags & XFS_MOUNT_RDONLY)) { 95361e63ecbSDave Chinner error = xfs_sync_sb(mp, false); 954e5720eecSDavid Chinner if (error) { 9550b932cccSDave Chinner xfs_warn(mp, "failed to write sb changes"); 956b93b6e43SChristoph Hellwig goto out_rtunmount; 957e5720eecSDavid Chinner } 958e5720eecSDavid Chinner } 9591da177e4SLinus Torvalds 9601da177e4SLinus Torvalds /* 9611da177e4SLinus Torvalds * Initialise the XFS quota management subsystem for this mount 9621da177e4SLinus Torvalds */ 9637d095257SChristoph Hellwig if (XFS_IS_QUOTA_RUNNING(mp)) { 9647d095257SChristoph Hellwig error = xfs_qm_newmount(mp, "amount, "aflags); 9650771fb45SEric Sandeen if (error) 966b93b6e43SChristoph Hellwig goto out_rtunmount; 9677d095257SChristoph Hellwig } else { 9687d095257SChristoph Hellwig ASSERT(!XFS_IS_QUOTA_ON(mp)); 9697d095257SChristoph Hellwig 9707d095257SChristoph Hellwig /* 9717d095257SChristoph Hellwig * If a file system had quotas running earlier, but decided to 9727d095257SChristoph Hellwig * mount without -o uquota/pquota/gquota options, revoke the 9737d095257SChristoph Hellwig * quotachecked license. 9747d095257SChristoph Hellwig */ 9757d095257SChristoph Hellwig if (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_ACCT) { 9760b932cccSDave Chinner xfs_notice(mp, "resetting quota flags"); 9777d095257SChristoph Hellwig error = xfs_mount_reset_sbqflags(mp); 9787d095257SChristoph Hellwig if (error) 979a70a4fa5SBrian Foster goto out_rtunmount; 9807d095257SChristoph Hellwig } 9817d095257SChristoph Hellwig } 9821da177e4SLinus Torvalds 9831da177e4SLinus Torvalds /* 984f0b2efadSBrian Foster * Finish recovering the file system. This part needed to be delayed 985f0b2efadSBrian Foster * until after the root and real-time bitmap inodes were consistently 986f0b2efadSBrian Foster * read in. 9871da177e4SLinus Torvalds */ 9884249023aSChristoph Hellwig error = xfs_log_mount_finish(mp); 9891da177e4SLinus Torvalds if (error) { 9900b932cccSDave Chinner xfs_warn(mp, "log mount finish failed"); 991b93b6e43SChristoph Hellwig goto out_rtunmount; 9921da177e4SLinus Torvalds } 9931da177e4SLinus Torvalds 9941da177e4SLinus Torvalds /* 995ddeb14f4SDave Chinner * Now the log is fully replayed, we can transition to full read-only 996ddeb14f4SDave Chinner * mode for read-only mounts. This will sync all the metadata and clean 997ddeb14f4SDave Chinner * the log so that the recovery we just performed does not have to be 998ddeb14f4SDave Chinner * replayed again on the next mount. 999ddeb14f4SDave Chinner * 1000ddeb14f4SDave Chinner * We use the same quiesce mechanism as the rw->ro remount, as they are 1001ddeb14f4SDave Chinner * semantically identical operations. 1002ddeb14f4SDave Chinner */ 1003ddeb14f4SDave Chinner if ((mp->m_flags & (XFS_MOUNT_RDONLY|XFS_MOUNT_NORECOVERY)) == 1004ddeb14f4SDave Chinner XFS_MOUNT_RDONLY) { 1005ea2064daSBrian Foster xfs_log_clean(mp); 1006ddeb14f4SDave Chinner } 1007ddeb14f4SDave Chinner 1008ddeb14f4SDave Chinner /* 10091da177e4SLinus Torvalds * Complete the quota initialisation, post-log-replay component. 10101da177e4SLinus Torvalds */ 10117d095257SChristoph Hellwig if (quotamount) { 10127d095257SChristoph Hellwig ASSERT(mp->m_qflags == 0); 10137d095257SChristoph Hellwig mp->m_qflags = quotaflags; 10147d095257SChristoph Hellwig 10157d095257SChristoph Hellwig xfs_qm_mount_quotas(mp); 10167d095257SChristoph Hellwig } 10177d095257SChristoph Hellwig 101884e1e99fSDavid Chinner /* 101984e1e99fSDavid Chinner * Now we are mounted, reserve a small amount of unused space for 102084e1e99fSDavid Chinner * privileged transactions. This is needed so that transaction 102184e1e99fSDavid Chinner * space required for critical operations can dip into this pool 102284e1e99fSDavid Chinner * when at ENOSPC. This is needed for operations like create with 102384e1e99fSDavid Chinner * attr, unwritten extent conversion at ENOSPC, etc. Data allocations 102484e1e99fSDavid Chinner * are not allowed to use this reserved space. 10258babd8a2SDave Chinner * 10268babd8a2SDave Chinner * This may drive us straight to ENOSPC on mount, but that implies 10278babd8a2SDave Chinner * we were already there on the last unmount. Warn if this occurs. 102884e1e99fSDavid Chinner */ 1029d5db0f97SEric Sandeen if (!(mp->m_flags & XFS_MOUNT_RDONLY)) { 1030d5db0f97SEric Sandeen resblks = xfs_default_resblks(mp); 1031714082bcSDavid Chinner error = xfs_reserve_blocks(mp, &resblks, NULL); 1032714082bcSDavid Chinner if (error) 10330b932cccSDave Chinner xfs_warn(mp, 10340b932cccSDave Chinner "Unable to allocate reserve blocks. Continuing without reserve pool."); 1035174edb0eSDarrick J. Wong 1036174edb0eSDarrick J. Wong /* Recover any CoW blocks that never got remapped. */ 1037174edb0eSDarrick J. Wong error = xfs_reflink_recover_cow(mp); 1038174edb0eSDarrick J. Wong if (error) { 1039174edb0eSDarrick J. Wong xfs_err(mp, 1040174edb0eSDarrick J. Wong "Error %d recovering leftover CoW allocations.", error); 1041174edb0eSDarrick J. Wong xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); 1042174edb0eSDarrick J. Wong goto out_quota; 1043174edb0eSDarrick J. Wong } 104484d69619SDarrick J. Wong 104584d69619SDarrick J. Wong /* Reserve AG blocks for future btree expansion. */ 104684d69619SDarrick J. Wong error = xfs_fs_reserve_ag_blocks(mp); 104784d69619SDarrick J. Wong if (error && error != -ENOSPC) 104884d69619SDarrick J. Wong goto out_agresv; 1049d5db0f97SEric Sandeen } 105084e1e99fSDavid Chinner 10511da177e4SLinus Torvalds return 0; 10521da177e4SLinus Torvalds 105384d69619SDarrick J. Wong out_agresv: 105484d69619SDarrick J. Wong xfs_fs_unreserve_ag_blocks(mp); 1055174edb0eSDarrick J. Wong out_quota: 1056174edb0eSDarrick J. Wong xfs_qm_unmount_quotas(mp); 1057b93b6e43SChristoph Hellwig out_rtunmount: 1058b93b6e43SChristoph Hellwig xfs_rtunmount_inodes(mp); 1059f9057e3dSChristoph Hellwig out_rele_rip: 106044a8736bSDarrick J. Wong xfs_irele(rip); 106177aff8c7SDarrick J. Wong /* Clean out dquots that might be in memory after quotacheck. */ 106277aff8c7SDarrick J. Wong xfs_qm_unmount(mp); 10632d1d1da3SDarrick J. Wong /* 1064d336f7ebSDarrick J. Wong * Flush all inode reclamation work and flush the log. 10652d1d1da3SDarrick J. Wong * We have to do this /after/ rtunmount and qm_unmount because those 10662d1d1da3SDarrick J. Wong * two will have scheduled delayed reclaim for the rt/quota inodes. 10672d1d1da3SDarrick J. Wong * 10682d1d1da3SDarrick J. Wong * This is slightly different from the unmountfs call sequence 10692d1d1da3SDarrick J. Wong * because we could be tearing down a partially set up mount. In 10702d1d1da3SDarrick J. Wong * particular, if log_mount_finish fails we bail out without calling 10712d1d1da3SDarrick J. Wong * qm_unmount_quotas and therefore rely on qm_unmount to release the 10722d1d1da3SDarrick J. Wong * quota inodes. 10732d1d1da3SDarrick J. Wong */ 1074d336f7ebSDarrick J. Wong xfs_unmount_flush_inodes(mp); 1075f9057e3dSChristoph Hellwig out_log_dealloc: 1076f0b2efadSBrian Foster xfs_log_mount_cancel(mp); 1077d4f3512bSDave Chinner out_fail_wait: 1078d4f3512bSDave Chinner if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp) 107910fb9ac1SBrian Foster xfs_buftarg_drain(mp->m_logdev_targp); 108010fb9ac1SBrian Foster xfs_buftarg_drain(mp->m_ddev_targp); 1081f9057e3dSChristoph Hellwig out_free_perag: 1082ff4f038cSChristoph Hellwig xfs_free_perag(mp); 10830650b554SDave Chinner out_free_dir: 10840650b554SDave Chinner xfs_da_unmount(mp); 1085f9057e3dSChristoph Hellwig out_remove_uuid: 108627174203SChristoph Hellwig xfs_uuid_unmount(mp); 108731965ef3SDarrick J. Wong out_remove_errortag: 108831965ef3SDarrick J. Wong xfs_errortag_del(mp); 1089192852beSCarlos Maiolino out_remove_error_sysfs: 1090192852beSCarlos Maiolino xfs_error_sysfs_del(mp); 1091225e4635SBill O'Donnell out_del_stats: 1092225e4635SBill O'Donnell xfs_sysfs_del(&mp->m_stats.xs_kobj); 1093a31b1d3dSBrian Foster out_remove_sysfs: 1094a31b1d3dSBrian Foster xfs_sysfs_del(&mp->m_kobj); 1095f9057e3dSChristoph Hellwig out: 10961da177e4SLinus Torvalds return error; 10971da177e4SLinus Torvalds } 10981da177e4SLinus Torvalds 10991da177e4SLinus Torvalds /* 11001da177e4SLinus Torvalds * This flushes out the inodes,dquots and the superblock, unmounts the 11011da177e4SLinus Torvalds * log and makes sure that incore structures are freed. 11021da177e4SLinus Torvalds */ 110341b5c2e7SChristoph Hellwig void 110441b5c2e7SChristoph Hellwig xfs_unmountfs( 110541b5c2e7SChristoph Hellwig struct xfs_mount *mp) 11061da177e4SLinus Torvalds { 1107c8ce540dSDarrick J. Wong uint64_t resblks; 110841b5c2e7SChristoph Hellwig int error; 11091da177e4SLinus Torvalds 1110c9a6526fSDarrick J. Wong xfs_blockgc_stop(mp); 111184d69619SDarrick J. Wong xfs_fs_unreserve_ag_blocks(mp); 11127d095257SChristoph Hellwig xfs_qm_unmount_quotas(mp); 1113b93b6e43SChristoph Hellwig xfs_rtunmount_inodes(mp); 111444a8736bSDarrick J. Wong xfs_irele(mp->m_rootip); 111577508ec8SChristoph Hellwig 1116d336f7ebSDarrick J. Wong xfs_unmount_flush_inodes(mp); 11171da177e4SLinus Torvalds 11187d095257SChristoph Hellwig xfs_qm_unmount(mp); 1119a357a121SLachlan McIlroy 11201da177e4SLinus Torvalds /* 112184e1e99fSDavid Chinner * Unreserve any blocks we have so that when we unmount we don't account 112284e1e99fSDavid Chinner * the reserved free space as used. This is really only necessary for 112384e1e99fSDavid Chinner * lazy superblock counting because it trusts the incore superblock 11249da096fdSMalcolm Parsons * counters to be absolutely correct on clean unmount. 112584e1e99fSDavid Chinner * 112684e1e99fSDavid Chinner * We don't bother correcting this elsewhere for lazy superblock 112784e1e99fSDavid Chinner * counting because on mount of an unclean filesystem we reconstruct the 112884e1e99fSDavid Chinner * correct counter value and this is irrelevant. 112984e1e99fSDavid Chinner * 113084e1e99fSDavid Chinner * For non-lazy counter filesystems, this doesn't matter at all because 113184e1e99fSDavid Chinner * we only every apply deltas to the superblock and hence the incore 113284e1e99fSDavid Chinner * value does not matter.... 113384e1e99fSDavid Chinner */ 113484e1e99fSDavid Chinner resblks = 0; 1135714082bcSDavid Chinner error = xfs_reserve_blocks(mp, &resblks, NULL); 1136714082bcSDavid Chinner if (error) 11370b932cccSDave Chinner xfs_warn(mp, "Unable to free reserved block pool. " 1138714082bcSDavid Chinner "Freespace may not be correct on next mount."); 1139714082bcSDavid Chinner 114021b699c8SChristoph Hellwig xfs_log_unmount(mp); 11410650b554SDave Chinner xfs_da_unmount(mp); 114227174203SChristoph Hellwig xfs_uuid_unmount(mp); 11431da177e4SLinus Torvalds 11441550d0b0SChristoph Hellwig #if defined(DEBUG) 114531965ef3SDarrick J. Wong xfs_errortag_clearall(mp); 11461da177e4SLinus Torvalds #endif 1147ff4f038cSChristoph Hellwig xfs_free_perag(mp); 1148a31b1d3dSBrian Foster 114931965ef3SDarrick J. Wong xfs_errortag_del(mp); 1150192852beSCarlos Maiolino xfs_error_sysfs_del(mp); 1151225e4635SBill O'Donnell xfs_sysfs_del(&mp->m_stats.xs_kobj); 1152a31b1d3dSBrian Foster xfs_sysfs_del(&mp->m_kobj); 11531da177e4SLinus Torvalds } 11541da177e4SLinus Torvalds 115591ee575fSBrian Foster /* 115691ee575fSBrian Foster * Determine whether modifications can proceed. The caller specifies the minimum 115791ee575fSBrian Foster * freeze level for which modifications should not be allowed. This allows 115891ee575fSBrian Foster * certain operations to proceed while the freeze sequence is in progress, if 115991ee575fSBrian Foster * necessary. 116091ee575fSBrian Foster */ 116191ee575fSBrian Foster bool 116291ee575fSBrian Foster xfs_fs_writable( 116391ee575fSBrian Foster struct xfs_mount *mp, 116491ee575fSBrian Foster int level) 116592821e2bSDavid Chinner { 116691ee575fSBrian Foster ASSERT(level > SB_UNFROZEN); 116791ee575fSBrian Foster if ((mp->m_super->s_writers.frozen >= level) || 116891ee575fSBrian Foster XFS_FORCED_SHUTDOWN(mp) || (mp->m_flags & XFS_MOUNT_RDONLY)) 116991ee575fSBrian Foster return false; 117091ee575fSBrian Foster 117191ee575fSBrian Foster return true; 117292821e2bSDavid Chinner } 117392821e2bSDavid Chinner 117492821e2bSDavid Chinner /* 11758c1903d3SDave Chinner * Deltas for the block count can vary from 1 to very large, but lock contention 11768c1903d3SDave Chinner * only occurs on frequent small block count updates such as in the delayed 11778c1903d3SDave Chinner * allocation path for buffered writes (page a time updates). Hence we set 11788c1903d3SDave Chinner * a large batch count (1024) to minimise global counter updates except when 11798c1903d3SDave Chinner * we get near to ENOSPC and we have to be very accurate with our updates. 11808c1903d3SDave Chinner */ 11818c1903d3SDave Chinner #define XFS_FDBLOCKS_BATCH 1024 11820d485adaSDave Chinner int 11830d485adaSDave Chinner xfs_mod_fdblocks( 11840d485adaSDave Chinner struct xfs_mount *mp, 11850d485adaSDave Chinner int64_t delta, 11860d485adaSDave Chinner bool rsvd) 11870d485adaSDave Chinner { 11880d485adaSDave Chinner int64_t lcounter; 11890d485adaSDave Chinner long long res_used; 11900d485adaSDave Chinner s32 batch; 1191*fd43cf60SBrian Foster uint64_t set_aside; 11920d485adaSDave Chinner 11930d485adaSDave Chinner if (delta > 0) { 11940d485adaSDave Chinner /* 11950d485adaSDave Chinner * If the reserve pool is depleted, put blocks back into it 11960d485adaSDave Chinner * first. Most of the time the pool is full. 11970d485adaSDave Chinner */ 11980d485adaSDave Chinner if (likely(mp->m_resblks == mp->m_resblks_avail)) { 11990d485adaSDave Chinner percpu_counter_add(&mp->m_fdblocks, delta); 12000d485adaSDave Chinner return 0; 12010d485adaSDave Chinner } 12020d485adaSDave Chinner 12030d485adaSDave Chinner spin_lock(&mp->m_sb_lock); 12040d485adaSDave Chinner res_used = (long long)(mp->m_resblks - mp->m_resblks_avail); 12050d485adaSDave Chinner 12060d485adaSDave Chinner if (res_used > delta) { 12070d485adaSDave Chinner mp->m_resblks_avail += delta; 12080d485adaSDave Chinner } else { 12090d485adaSDave Chinner delta -= res_used; 12100d485adaSDave Chinner mp->m_resblks_avail = mp->m_resblks; 12110d485adaSDave Chinner percpu_counter_add(&mp->m_fdblocks, delta); 12120d485adaSDave Chinner } 12130d485adaSDave Chinner spin_unlock(&mp->m_sb_lock); 12140d485adaSDave Chinner return 0; 12150d485adaSDave Chinner } 12160d485adaSDave Chinner 12170d485adaSDave Chinner /* 12180d485adaSDave Chinner * Taking blocks away, need to be more accurate the closer we 12190d485adaSDave Chinner * are to zero. 12200d485adaSDave Chinner * 12210d485adaSDave Chinner * If the counter has a value of less than 2 * max batch size, 12220d485adaSDave Chinner * then make everything serialise as we are real close to 12230d485adaSDave Chinner * ENOSPC. 12240d485adaSDave Chinner */ 12258c1903d3SDave Chinner if (__percpu_counter_compare(&mp->m_fdblocks, 2 * XFS_FDBLOCKS_BATCH, 12268c1903d3SDave Chinner XFS_FDBLOCKS_BATCH) < 0) 12270d485adaSDave Chinner batch = 1; 12280d485adaSDave Chinner else 12298c1903d3SDave Chinner batch = XFS_FDBLOCKS_BATCH; 12300d485adaSDave Chinner 1231*fd43cf60SBrian Foster /* 1232*fd43cf60SBrian Foster * Set aside allocbt blocks because these blocks are tracked as free 1233*fd43cf60SBrian Foster * space but not available for allocation. Technically this means that a 1234*fd43cf60SBrian Foster * single reservation cannot consume all remaining free space, but the 1235*fd43cf60SBrian Foster * ratio of allocbt blocks to usable free blocks should be rather small. 1236*fd43cf60SBrian Foster * The tradeoff without this is that filesystems that maintain high 1237*fd43cf60SBrian Foster * perag block reservations can over reserve physical block availability 1238*fd43cf60SBrian Foster * and fail physical allocation, which leads to much more serious 1239*fd43cf60SBrian Foster * problems (i.e. transaction abort, pagecache discards, etc.) than 1240*fd43cf60SBrian Foster * slightly premature -ENOSPC. 1241*fd43cf60SBrian Foster */ 1242*fd43cf60SBrian Foster set_aside = mp->m_alloc_set_aside + atomic64_read(&mp->m_allocbt_blks); 1243104b4e51SNikolay Borisov percpu_counter_add_batch(&mp->m_fdblocks, delta, batch); 1244*fd43cf60SBrian Foster if (__percpu_counter_compare(&mp->m_fdblocks, set_aside, 12458c1903d3SDave Chinner XFS_FDBLOCKS_BATCH) >= 0) { 12460d485adaSDave Chinner /* we had space! */ 12470d485adaSDave Chinner return 0; 12480d485adaSDave Chinner } 12490d485adaSDave Chinner 12500d485adaSDave Chinner /* 12510d485adaSDave Chinner * lock up the sb for dipping into reserves before releasing the space 12520d485adaSDave Chinner * that took us to ENOSPC. 12530d485adaSDave Chinner */ 12540d485adaSDave Chinner spin_lock(&mp->m_sb_lock); 12550d485adaSDave Chinner percpu_counter_add(&mp->m_fdblocks, -delta); 12560d485adaSDave Chinner if (!rsvd) 12570d485adaSDave Chinner goto fdblocks_enospc; 12580d485adaSDave Chinner 12590d485adaSDave Chinner lcounter = (long long)mp->m_resblks_avail + delta; 12600d485adaSDave Chinner if (lcounter >= 0) { 12610d485adaSDave Chinner mp->m_resblks_avail = lcounter; 12620d485adaSDave Chinner spin_unlock(&mp->m_sb_lock); 12630d485adaSDave Chinner return 0; 12640d485adaSDave Chinner } 1265ec43f6daSEric Sandeen xfs_warn_once(mp, 1266ec43f6daSEric Sandeen "Reserve blocks depleted! Consider increasing reserve pool size."); 1267ec43f6daSEric Sandeen 12680d485adaSDave Chinner fdblocks_enospc: 12690d485adaSDave Chinner spin_unlock(&mp->m_sb_lock); 12700d485adaSDave Chinner return -ENOSPC; 12710d485adaSDave Chinner } 12720d485adaSDave Chinner 1273bab98bbeSDave Chinner int 1274bab98bbeSDave Chinner xfs_mod_frextents( 1275bab98bbeSDave Chinner struct xfs_mount *mp, 1276bab98bbeSDave Chinner int64_t delta) 1277bab98bbeSDave Chinner { 1278bab98bbeSDave Chinner int64_t lcounter; 1279bab98bbeSDave Chinner int ret = 0; 1280bab98bbeSDave Chinner 1281bab98bbeSDave Chinner spin_lock(&mp->m_sb_lock); 1282bab98bbeSDave Chinner lcounter = mp->m_sb.sb_frextents + delta; 1283bab98bbeSDave Chinner if (lcounter < 0) 1284bab98bbeSDave Chinner ret = -ENOSPC; 1285bab98bbeSDave Chinner else 1286bab98bbeSDave Chinner mp->m_sb.sb_frextents = lcounter; 1287bab98bbeSDave Chinner spin_unlock(&mp->m_sb_lock); 1288bab98bbeSDave Chinner return ret; 1289bab98bbeSDave Chinner } 1290bab98bbeSDave Chinner 12911da177e4SLinus Torvalds /* 12921da177e4SLinus Torvalds * Used to free the superblock along various error paths. 12931da177e4SLinus Torvalds */ 12941da177e4SLinus Torvalds void 12951da177e4SLinus Torvalds xfs_freesb( 129626af6552SDave Chinner struct xfs_mount *mp) 12971da177e4SLinus Torvalds { 129826af6552SDave Chinner struct xfs_buf *bp = mp->m_sb_bp; 12991da177e4SLinus Torvalds 130026af6552SDave Chinner xfs_buf_lock(bp); 13011da177e4SLinus Torvalds mp->m_sb_bp = NULL; 130226af6552SDave Chinner xfs_buf_relse(bp); 13031da177e4SLinus Torvalds } 13041da177e4SLinus Torvalds 13051da177e4SLinus Torvalds /* 1306dda35b8fSChristoph Hellwig * If the underlying (data/log/rt) device is readonly, there are some 1307dda35b8fSChristoph Hellwig * operations that cannot proceed. 1308dda35b8fSChristoph Hellwig */ 1309dda35b8fSChristoph Hellwig int 1310dda35b8fSChristoph Hellwig xfs_dev_is_read_only( 1311dda35b8fSChristoph Hellwig struct xfs_mount *mp, 1312dda35b8fSChristoph Hellwig char *message) 1313dda35b8fSChristoph Hellwig { 1314dda35b8fSChristoph Hellwig if (xfs_readonly_buftarg(mp->m_ddev_targp) || 1315dda35b8fSChristoph Hellwig xfs_readonly_buftarg(mp->m_logdev_targp) || 1316dda35b8fSChristoph Hellwig (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) { 13170b932cccSDave Chinner xfs_notice(mp, "%s required on read-only device.", message); 13180b932cccSDave Chinner xfs_notice(mp, "write access unavailable, cannot proceed."); 13192451337dSDave Chinner return -EROFS; 1320dda35b8fSChristoph Hellwig } 1321dda35b8fSChristoph Hellwig return 0; 1322dda35b8fSChristoph Hellwig } 1323f467cad9SDarrick J. Wong 1324f467cad9SDarrick J. Wong /* Force the summary counters to be recalculated at next mount. */ 1325f467cad9SDarrick J. Wong void 1326f467cad9SDarrick J. Wong xfs_force_summary_recalc( 1327f467cad9SDarrick J. Wong struct xfs_mount *mp) 1328f467cad9SDarrick J. Wong { 1329f467cad9SDarrick J. Wong if (!xfs_sb_version_haslazysbcount(&mp->m_sb)) 1330f467cad9SDarrick J. Wong return; 1331f467cad9SDarrick J. Wong 133239353ff6SDarrick J. Wong xfs_fs_mark_sick(mp, XFS_SICK_FS_COUNTERS); 1333f467cad9SDarrick J. Wong } 13349fe82b8cSDarrick J. Wong 13359fe82b8cSDarrick J. Wong /* 13369fe82b8cSDarrick J. Wong * Update the in-core delayed block counter. 13379fe82b8cSDarrick J. Wong * 13389fe82b8cSDarrick J. Wong * We prefer to update the counter without having to take a spinlock for every 13399fe82b8cSDarrick J. Wong * counter update (i.e. batching). Each change to delayed allocation 13409fe82b8cSDarrick J. Wong * reservations can change can easily exceed the default percpu counter 13419fe82b8cSDarrick J. Wong * batching, so we use a larger batch factor here. 13429fe82b8cSDarrick J. Wong * 13439fe82b8cSDarrick J. Wong * Note that we don't currently have any callers requiring fast summation 13449fe82b8cSDarrick J. Wong * (e.g. percpu_counter_read) so we can use a big batch value here. 13459fe82b8cSDarrick J. Wong */ 13469fe82b8cSDarrick J. Wong #define XFS_DELALLOC_BATCH (4096) 13479fe82b8cSDarrick J. Wong void 13489fe82b8cSDarrick J. Wong xfs_mod_delalloc( 13499fe82b8cSDarrick J. Wong struct xfs_mount *mp, 13509fe82b8cSDarrick J. Wong int64_t delta) 13519fe82b8cSDarrick J. Wong { 13529fe82b8cSDarrick J. Wong percpu_counter_add_batch(&mp->m_delalloc_blks, delta, 13539fe82b8cSDarrick J. Wong XFS_DELALLOC_BATCH); 13549fe82b8cSDarrick J. Wong } 1355