xfs_mount.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_bit.h"
  13. #include "xfs_sb.h"
  14. #include "xfs_mount.h"
  15. #include "xfs_inode.h"
  16. #include "xfs_dir2.h"
  17. #include "xfs_ialloc.h"
  18. #include "xfs_alloc.h"
  19. #include "xfs_rtalloc.h"
  20. #include "xfs_bmap.h"
  21. #include "xfs_trans.h"
  22. #include "xfs_trans_priv.h"
  23. #include "xfs_log.h"
  24. #include "xfs_error.h"
  25. #include "xfs_quota.h"
  26. #include "xfs_fsops.h"
  27. #include "xfs_icache.h"
  28. #include "xfs_sysfs.h"
  29. #include "xfs_rmap_btree.h"
  30. #include "xfs_refcount_btree.h"
  31. #include "xfs_reflink.h"
  32. #include "xfs_extent_busy.h"
  33. #include "xfs_health.h"
  34. #include "xfs_trace.h"
  35. static DEFINE_MUTEX(xfs_uuid_table_mutex);
  36. static int xfs_uuid_table_size;
  37. static uuid_t *xfs_uuid_table;
  38. void
  39. xfs_uuid_table_free(void)
  40. {
  41. if (xfs_uuid_table_size == 0)
  42. return;
  43. kmem_free(xfs_uuid_table);
  44. xfs_uuid_table = NULL;
  45. xfs_uuid_table_size = 0;
  46. }
  47. /*
  48. * See if the UUID is unique among mounted XFS filesystems.
  49. * Mount fails if UUID is nil or a FS with the same UUID is already mounted.
  50. */
  51. STATIC int
  52. xfs_uuid_mount(
  53. struct xfs_mount *mp)
  54. {
  55. uuid_t *uuid = &mp->m_sb.sb_uuid;
  56. int hole, i;
  57. /* Publish UUID in struct super_block */
  58. uuid_copy(&mp->m_super->s_uuid, uuid);
  59. if (mp->m_flags & XFS_MOUNT_NOUUID)
  60. return 0;
  61. if (uuid_is_null(uuid)) {
  62. xfs_warn(mp, "Filesystem has null UUID - can't mount");
  63. return -EINVAL;
  64. }
  65. mutex_lock(&xfs_uuid_table_mutex);
  66. for (i = 0, hole = -1; i < xfs_uuid_table_size; i++) {
  67. if (uuid_is_null(&xfs_uuid_table[i])) {
  68. hole = i;
  69. continue;
  70. }
  71. if (uuid_equal(uuid, &xfs_uuid_table[i]))
  72. goto out_duplicate;
  73. }
  74. if (hole < 0) {
  75. xfs_uuid_table = krealloc(xfs_uuid_table,
  76. (xfs_uuid_table_size + 1) * sizeof(*xfs_uuid_table),
  77. GFP_KERNEL | __GFP_NOFAIL);
  78. hole = xfs_uuid_table_size++;
  79. }
  80. xfs_uuid_table[hole] = *uuid;
  81. mutex_unlock(&xfs_uuid_table_mutex);
  82. return 0;
  83. out_duplicate:
  84. mutex_unlock(&xfs_uuid_table_mutex);
  85. xfs_warn(mp, "Filesystem has duplicate UUID %pU - can't mount", uuid);
  86. return -EINVAL;
  87. }
  88. STATIC void
  89. xfs_uuid_unmount(
  90. struct xfs_mount *mp)
  91. {
  92. uuid_t *uuid = &mp->m_sb.sb_uuid;
  93. int i;
  94. if (mp->m_flags & XFS_MOUNT_NOUUID)
  95. return;
  96. mutex_lock(&xfs_uuid_table_mutex);
  97. for (i = 0; i < xfs_uuid_table_size; i++) {
  98. if (uuid_is_null(&xfs_uuid_table[i]))
  99. continue;
  100. if (!uuid_equal(uuid, &xfs_uuid_table[i]))
  101. continue;
  102. memset(&xfs_uuid_table[i], 0, sizeof(uuid_t));
  103. break;
  104. }
  105. ASSERT(i < xfs_uuid_table_size);
  106. mutex_unlock(&xfs_uuid_table_mutex);
  107. }
  108. STATIC void
  109. __xfs_free_perag(
  110. struct rcu_head *head)
  111. {
  112. struct xfs_perag *pag = container_of(head, struct xfs_perag, rcu_head);
  113. ASSERT(atomic_read(&pag->pag_ref) == 0);
  114. kmem_free(pag);
  115. }
  116. /*
  117. * Free up the per-ag resources associated with the mount structure.
  118. */
  119. STATIC void
  120. xfs_free_perag(
  121. xfs_mount_t *mp)
  122. {
  123. xfs_agnumber_t agno;
  124. struct xfs_perag *pag;
  125. for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
  126. spin_lock(&mp->m_perag_lock);
  127. pag = radix_tree_delete(&mp->m_perag_tree, agno);
  128. spin_unlock(&mp->m_perag_lock);
  129. ASSERT(pag);
  130. ASSERT(atomic_read(&pag->pag_ref) == 0);
  131. xfs_iunlink_destroy(pag);
  132. xfs_buf_hash_destroy(pag);
  133. call_rcu(&pag->rcu_head, __xfs_free_perag);
  134. }
  135. }
  136. /*
  137. * Check size of device based on the (data/realtime) block count.
  138. * Note: this check is used by the growfs code as well as mount.
  139. */
  140. int
  141. xfs_sb_validate_fsb_count(
  142. xfs_sb_t *sbp,
  143. uint64_t nblocks)
  144. {
  145. ASSERT(PAGE_SHIFT >= sbp->sb_blocklog);
  146. ASSERT(sbp->sb_blocklog >= BBSHIFT);
  147. /* Limited by ULONG_MAX of page cache index */
  148. if (nblocks >> (PAGE_SHIFT - sbp->sb_blocklog) > ULONG_MAX)
  149. return -EFBIG;
  150. return 0;
  151. }
  152. int
  153. xfs_initialize_perag(
  154. xfs_mount_t *mp,
  155. xfs_agnumber_t agcount,
  156. xfs_agnumber_t *maxagi)
  157. {
  158. xfs_agnumber_t index;
  159. xfs_agnumber_t first_initialised = NULLAGNUMBER;
  160. xfs_perag_t *pag;
  161. int error = -ENOMEM;
  162. /*
  163. * Walk the current per-ag tree so we don't try to initialise AGs
  164. * that already exist (growfs case). Allocate and insert all the
  165. * AGs we don't find ready for initialisation.
  166. */
  167. for (index = 0; index < agcount; index++) {
  168. pag = xfs_perag_get(mp, index);
  169. if (pag) {
  170. xfs_perag_put(pag);
  171. continue;
  172. }
  173. pag = kmem_zalloc(sizeof(*pag), KM_MAYFAIL);
  174. if (!pag) {
  175. error = -ENOMEM;
  176. goto out_unwind_new_pags;
  177. }
  178. pag->pag_agno = index;
  179. pag->pag_mount = mp;
  180. spin_lock_init(&pag->pag_ici_lock);
  181. INIT_RADIX_TREE(&pag->pag_ici_root, GFP_ATOMIC);
  182. error = xfs_buf_hash_init(pag);
  183. if (error)
  184. goto out_free_pag;
  185. init_waitqueue_head(&pag->pagb_wait);
  186. spin_lock_init(&pag->pagb_lock);
  187. pag->pagb_count = 0;
  188. pag->pagb_tree = RB_ROOT;
  189. error = radix_tree_preload(GFP_NOFS);
  190. if (error)
  191. goto out_hash_destroy;
  192. spin_lock(&mp->m_perag_lock);
  193. if (radix_tree_insert(&mp->m_perag_tree, index, pag)) {
  194. WARN_ON_ONCE(1);
  195. spin_unlock(&mp->m_perag_lock);
  196. radix_tree_preload_end();
  197. error = -EEXIST;
  198. goto out_hash_destroy;
  199. }
  200. spin_unlock(&mp->m_perag_lock);
  201. radix_tree_preload_end();
  202. /* first new pag is fully initialized */
  203. if (first_initialised == NULLAGNUMBER)
  204. first_initialised = index;
  205. error = xfs_iunlink_init(pag);
  206. if (error)
  207. goto out_hash_destroy;
  208. spin_lock_init(&pag->pag_state_lock);
  209. }
  210. index = xfs_set_inode_alloc(mp, agcount);
  211. if (maxagi)
  212. *maxagi = index;
  213. mp->m_ag_prealloc_blocks = xfs_prealloc_blocks(mp);
  214. return 0;
  215. out_hash_destroy:
  216. xfs_buf_hash_destroy(pag);
  217. out_free_pag:
  218. kmem_free(pag);
  219. out_unwind_new_pags:
  220. /* unwind any prior newly initialized pags */
  221. for (index = first_initialised; index < agcount; index++) {
  222. pag = radix_tree_delete(&mp->m_perag_tree, index);
  223. if (!pag)
  224. break;
  225. xfs_buf_hash_destroy(pag);
  226. xfs_iunlink_destroy(pag);
  227. kmem_free(pag);
  228. }
  229. return error;
  230. }
  231. /*
  232. * xfs_readsb
  233. *
  234. * Does the initial read of the superblock.
  235. */
  236. int
  237. xfs_readsb(
  238. struct xfs_mount *mp,
  239. int flags)
  240. {
  241. unsigned int sector_size;
  242. struct xfs_buf *bp;
  243. struct xfs_sb *sbp = &mp->m_sb;
  244. int error;
  245. int loud = !(flags & XFS_MFSI_QUIET);
  246. const struct xfs_buf_ops *buf_ops;
  247. ASSERT(mp->m_sb_bp == NULL);
  248. ASSERT(mp->m_ddev_targp != NULL);
  249. /*
  250. * For the initial read, we must guess at the sector
  251. * size based on the block device. It's enough to
  252. * get the sb_sectsize out of the superblock and
  253. * then reread with the proper length.
  254. * We don't verify it yet, because it may not be complete.
  255. */
  256. sector_size = xfs_getsize_buftarg(mp->m_ddev_targp);
  257. buf_ops = NULL;
  258. /*
  259. * Allocate a (locked) buffer to hold the superblock. This will be kept
  260. * around at all times to optimize access to the superblock. Therefore,
  261. * set XBF_NO_IOACCT to make sure it doesn't hold the buftarg count
  262. * elevated.
  263. */
  264. reread:
  265. error = xfs_buf_read_uncached(mp->m_ddev_targp, XFS_SB_DADDR,
  266. BTOBB(sector_size), XBF_NO_IOACCT, &bp,
  267. buf_ops);
  268. if (error) {
  269. if (loud)
  270. xfs_warn(mp, "SB validate failed with error %d.", error);
  271. /* bad CRC means corrupted metadata */
  272. if (error == -EFSBADCRC)
  273. error = -EFSCORRUPTED;
  274. return error;
  275. }
  276. /*
  277. * Initialize the mount structure from the superblock.
  278. */
  279. xfs_sb_from_disk(sbp, bp->b_addr);
  280. /*
  281. * If we haven't validated the superblock, do so now before we try
  282. * to check the sector size and reread the superblock appropriately.
  283. */
  284. if (sbp->sb_magicnum != XFS_SB_MAGIC) {
  285. if (loud)
  286. xfs_warn(mp, "Invalid superblock magic number");
  287. error = -EINVAL;
  288. goto release_buf;
  289. }
  290. /*
  291. * We must be able to do sector-sized and sector-aligned IO.
  292. */
  293. if (sector_size > sbp->sb_sectsize) {
  294. if (loud)
  295. xfs_warn(mp, "device supports %u byte sectors (not %u)",
  296. sector_size, sbp->sb_sectsize);
  297. error = -ENOSYS;
  298. goto release_buf;
  299. }
  300. if (buf_ops == NULL) {
  301. /*
  302. * Re-read the superblock so the buffer is correctly sized,
  303. * and properly verified.
  304. */
  305. xfs_buf_relse(bp);
  306. sector_size = sbp->sb_sectsize;
  307. buf_ops = loud ? &xfs_sb_buf_ops : &xfs_sb_quiet_buf_ops;
  308. goto reread;
  309. }
  310. xfs_reinit_percpu_counters(mp);
  311. /* no need to be quiet anymore, so reset the buf ops */
  312. bp->b_ops = &xfs_sb_buf_ops;
  313. mp->m_sb_bp = bp;
  314. xfs_buf_unlock(bp);
  315. return 0;
  316. release_buf:
  317. xfs_buf_relse(bp);
  318. return error;
  319. }
  320. /*
  321. * If the sunit/swidth change would move the precomputed root inode value, we
  322. * must reject the ondisk change because repair will stumble over that.
  323. * However, we allow the mount to proceed because we never rejected this
  324. * combination before. Returns true to update the sb, false otherwise.
  325. */
  326. static inline int
  327. xfs_check_new_dalign(
  328. struct xfs_mount *mp,
  329. int new_dalign,
  330. bool *update_sb)
  331. {
  332. struct xfs_sb *sbp = &mp->m_sb;
  333. xfs_ino_t calc_ino;
  334. calc_ino = xfs_ialloc_calc_rootino(mp, new_dalign);
  335. trace_xfs_check_new_dalign(mp, new_dalign, calc_ino);
  336. if (sbp->sb_rootino == calc_ino) {
  337. *update_sb = true;
  338. return 0;
  339. }
  340. xfs_warn(mp,
  341. "Cannot change stripe alignment; would require moving root inode.");
  342. /*
  343. * XXX: Next time we add a new incompat feature, this should start
  344. * returning -EINVAL to fail the mount. Until then, spit out a warning
  345. * that we're ignoring the administrator's instructions.
  346. */
  347. xfs_warn(mp, "Skipping superblock stripe alignment update.");
  348. *update_sb = false;
  349. return 0;
  350. }
  351. /*
  352. * If we were provided with new sunit/swidth values as mount options, make sure
  353. * that they pass basic alignment and superblock feature checks, and convert
  354. * them into the same units (FSB) that everything else expects. This step
  355. * /must/ be done before computing the inode geometry.
  356. */
  357. STATIC int
  358. xfs_validate_new_dalign(
  359. struct xfs_mount *mp)
  360. {
  361. if (mp->m_dalign == 0)
  362. return 0;
  363. /*
  364. * If stripe unit and stripe width are not multiples
  365. * of the fs blocksize turn off alignment.
  366. */
  367. if ((BBTOB(mp->m_dalign) & mp->m_blockmask) ||
  368. (BBTOB(mp->m_swidth) & mp->m_blockmask)) {
  369. xfs_warn(mp,
  370. "alignment check failed: sunit/swidth vs. blocksize(%d)",
  371. mp->m_sb.sb_blocksize);
  372. return -EINVAL;
  373. } else {
  374. /*
  375. * Convert the stripe unit and width to FSBs.
  376. */
  377. mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign);
  378. if (mp->m_dalign && (mp->m_sb.sb_agblocks % mp->m_dalign)) {
  379. xfs_warn(mp,
  380. "alignment check failed: sunit/swidth vs. agsize(%d)",
  381. mp->m_sb.sb_agblocks);
  382. return -EINVAL;
  383. } else if (mp->m_dalign) {
  384. mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth);
  385. } else {
  386. xfs_warn(mp,
  387. "alignment check failed: sunit(%d) less than bsize(%d)",
  388. mp->m_dalign, mp->m_sb.sb_blocksize);
  389. return -EINVAL;
  390. }
  391. }
  392. if (!xfs_sb_version_hasdalign(&mp->m_sb)) {
  393. xfs_warn(mp,
  394. "cannot change alignment: superblock does not support data alignment");
  395. return -EINVAL;
  396. }
  397. return 0;
  398. }
  399. /* Update alignment values based on mount options and sb values. */
  400. STATIC int
  401. xfs_update_alignment(
  402. struct xfs_mount *mp)
  403. {
  404. struct xfs_sb *sbp = &mp->m_sb;
  405. if (mp->m_dalign) {
  406. bool update_sb;
  407. int error;
  408. if (sbp->sb_unit == mp->m_dalign &&
  409. sbp->sb_width == mp->m_swidth)
  410. return 0;
  411. error = xfs_check_new_dalign(mp, mp->m_dalign, &update_sb);
  412. if (error || !update_sb)
  413. return error;
  414. sbp->sb_unit = mp->m_dalign;
  415. sbp->sb_width = mp->m_swidth;
  416. mp->m_update_sb = true;
  417. } else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN &&
  418. xfs_sb_version_hasdalign(&mp->m_sb)) {
  419. mp->m_dalign = sbp->sb_unit;
  420. mp->m_swidth = sbp->sb_width;
  421. }
  422. return 0;
  423. }
  424. /*
  425. * precalculate the low space thresholds for dynamic speculative preallocation.
  426. */
  427. void
  428. xfs_set_low_space_thresholds(
  429. struct xfs_mount *mp)
  430. {
  431. int i;
  432. for (i = 0; i < XFS_LOWSP_MAX; i++) {
  433. uint64_t space = mp->m_sb.sb_dblocks;
  434. do_div(space, 100);
  435. mp->m_low_space[i] = space * (i + 1);
  436. }
  437. }
  438. /*
  439. * Check that the data (and log if separate) is an ok size.
  440. */
  441. STATIC int
  442. xfs_check_sizes(
  443. struct xfs_mount *mp)
  444. {
  445. struct xfs_buf *bp;
  446. xfs_daddr_t d;
  447. int error;
  448. d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
  449. if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
  450. xfs_warn(mp, "filesystem size mismatch detected");
  451. return -EFBIG;
  452. }
  453. error = xfs_buf_read_uncached(mp->m_ddev_targp,
  454. d - XFS_FSS_TO_BB(mp, 1),
  455. XFS_FSS_TO_BB(mp, 1), 0, &bp, NULL);
  456. if (error) {
  457. xfs_warn(mp, "last sector read failed");
  458. return error;
  459. }
  460. xfs_buf_relse(bp);
  461. if (mp->m_logdev_targp == mp->m_ddev_targp)
  462. return 0;
  463. d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
  464. if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) {
  465. xfs_warn(mp, "log size mismatch detected");
  466. return -EFBIG;
  467. }
  468. error = xfs_buf_read_uncached(mp->m_logdev_targp,
  469. d - XFS_FSB_TO_BB(mp, 1),
  470. XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL);
  471. if (error) {
  472. xfs_warn(mp, "log device read failed");
  473. return error;
  474. }
  475. xfs_buf_relse(bp);
  476. return 0;
  477. }
  478. /*
  479. * Clear the quotaflags in memory and in the superblock.
  480. */
  481. int
  482. xfs_mount_reset_sbqflags(
  483. struct xfs_mount *mp)
  484. {
  485. mp->m_qflags = 0;
  486. /* It is OK to look at sb_qflags in the mount path without m_sb_lock. */
  487. if (mp->m_sb.sb_qflags == 0)
  488. return 0;
  489. spin_lock(&mp->m_sb_lock);
  490. mp->m_sb.sb_qflags = 0;
  491. spin_unlock(&mp->m_sb_lock);
  492. if (!xfs_fs_writable(mp, SB_FREEZE_WRITE))
  493. return 0;
  494. return xfs_sync_sb(mp, false);
  495. }
  496. uint64_t
  497. xfs_default_resblks(xfs_mount_t *mp)
  498. {
  499. uint64_t resblks;
  500. /*
  501. * We default to 5% or 8192 fsbs of space reserved, whichever is
  502. * smaller. This is intended to cover concurrent allocation
  503. * transactions when we initially hit enospc. These each require a 4
  504. * block reservation. Hence by default we cover roughly 2000 concurrent
  505. * allocation reservations.
  506. */
  507. resblks = mp->m_sb.sb_dblocks;
  508. do_div(resblks, 20);
  509. resblks = min_t(uint64_t, resblks, 8192);
  510. return resblks;
  511. }
  512. /* Ensure the summary counts are correct. */
  513. STATIC int
  514. xfs_check_summary_counts(
  515. struct xfs_mount *mp)
  516. {
  517. /*
  518. * The AG0 superblock verifier rejects in-progress filesystems,
  519. * so we should never see the flag set this far into mounting.
  520. */
  521. if (mp->m_sb.sb_inprogress) {
  522. xfs_err(mp, "sb_inprogress set after log recovery??");
  523. WARN_ON(1);
  524. return -EFSCORRUPTED;
  525. }
  526. /*
  527. * Now the log is mounted, we know if it was an unclean shutdown or
  528. * not. If it was, with the first phase of recovery has completed, we
  529. * have consistent AG blocks on disk. We have not recovered EFIs yet,
  530. * but they are recovered transactionally in the second recovery phase
  531. * later.
  532. *
  533. * If the log was clean when we mounted, we can check the summary
  534. * counters. If any of them are obviously incorrect, we can recompute
  535. * them from the AGF headers in the next step.
  536. */
  537. if (XFS_LAST_UNMOUNT_WAS_CLEAN(mp) &&
  538. (mp->m_sb.sb_fdblocks > mp->m_sb.sb_dblocks ||
  539. !xfs_verify_icount(mp, mp->m_sb.sb_icount) ||
  540. mp->m_sb.sb_ifree > mp->m_sb.sb_icount))
  541. xfs_fs_mark_sick(mp, XFS_SICK_FS_COUNTERS);
  542. /*
  543. * We can safely re-initialise incore superblock counters from the
  544. * per-ag data. These may not be correct if the filesystem was not
  545. * cleanly unmounted, so we waited for recovery to finish before doing
  546. * this.
  547. *
  548. * If the filesystem was cleanly unmounted or the previous check did
  549. * not flag anything weird, then we can trust the values in the
  550. * superblock to be correct and we don't need to do anything here.
  551. * Otherwise, recalculate the summary counters.
  552. */
  553. if ((!xfs_sb_version_haslazysbcount(&mp->m_sb) ||
  554. XFS_LAST_UNMOUNT_WAS_CLEAN(mp)) &&
  555. !xfs_fs_has_sickness(mp, XFS_SICK_FS_COUNTERS))
  556. return 0;
  557. return xfs_initialize_perag_data(mp, mp->m_sb.sb_agcount);
  558. }
  559. /*
  560. * This function does the following on an initial mount of a file system:
  561. * - reads the superblock from disk and init the mount struct
  562. * - if we're a 32-bit kernel, do a size check on the superblock
  563. * so we don't mount terabyte filesystems
  564. * - init mount struct realtime fields
  565. * - allocate inode hash table for fs
  566. * - init directory manager
  567. * - perform recovery and init the log manager
  568. */
  569. int
  570. xfs_mountfs(
  571. struct xfs_mount *mp)
  572. {
  573. struct xfs_sb *sbp = &(mp->m_sb);
  574. struct xfs_inode *rip;
  575. struct xfs_ino_geometry *igeo = M_IGEO(mp);
  576. uint64_t resblks;
  577. uint quotamount = 0;
  578. uint quotaflags = 0;
  579. int error = 0;
  580. xfs_sb_mount_common(mp, sbp);
  581. /*
  582. * Check for a mismatched features2 values. Older kernels read & wrote
  583. * into the wrong sb offset for sb_features2 on some platforms due to
  584. * xfs_sb_t not being 64bit size aligned when sb_features2 was added,
  585. * which made older superblock reading/writing routines swap it as a
  586. * 64-bit value.
  587. *
  588. * For backwards compatibility, we make both slots equal.
  589. *
  590. * If we detect a mismatched field, we OR the set bits into the existing
  591. * features2 field in case it has already been modified; we don't want
  592. * to lose any features. We then update the bad location with the ORed
  593. * value so that older kernels will see any features2 flags. The
  594. * superblock writeback code ensures the new sb_features2 is copied to
  595. * sb_bad_features2 before it is logged or written to disk.
  596. */
  597. if (xfs_sb_has_mismatched_features2(sbp)) {
  598. xfs_warn(mp, "correcting sb_features alignment problem");
  599. sbp->sb_features2 |= sbp->sb_bad_features2;
  600. mp->m_update_sb = true;
  601. /*
  602. * Re-check for ATTR2 in case it was found in bad_features2
  603. * slot.
  604. */
  605. if (xfs_sb_version_hasattr2(&mp->m_sb) &&
  606. !(mp->m_flags & XFS_MOUNT_NOATTR2))
  607. mp->m_flags |= XFS_MOUNT_ATTR2;
  608. }
  609. if (xfs_sb_version_hasattr2(&mp->m_sb) &&
  610. (mp->m_flags & XFS_MOUNT_NOATTR2)) {
  611. xfs_sb_version_removeattr2(&mp->m_sb);
  612. mp->m_update_sb = true;
  613. /* update sb_versionnum for the clearing of the morebits */
  614. if (!sbp->sb_features2)
  615. mp->m_update_sb = true;
  616. }
  617. /* always use v2 inodes by default now */
  618. if (!(mp->m_sb.sb_versionnum & XFS_SB_VERSION_NLINKBIT)) {
  619. mp->m_sb.sb_versionnum |= XFS_SB_VERSION_NLINKBIT;
  620. mp->m_update_sb = true;
  621. }
  622. /*
  623. * If we were given new sunit/swidth options, do some basic validation
  624. * checks and convert the incore dalign and swidth values to the
  625. * same units (FSB) that everything else uses. This /must/ happen
  626. * before computing the inode geometry.
  627. */
  628. error = xfs_validate_new_dalign(mp);
  629. if (error)
  630. goto out;
  631. xfs_alloc_compute_maxlevels(mp);
  632. xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
  633. xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
  634. xfs_ialloc_setup_geometry(mp);
  635. xfs_rmapbt_compute_maxlevels(mp);
  636. xfs_refcountbt_compute_maxlevels(mp);
  637. /*
  638. * Check if sb_agblocks is aligned at stripe boundary. If sb_agblocks
  639. * is NOT aligned turn off m_dalign since allocator alignment is within
  640. * an ag, therefore ag has to be aligned at stripe boundary. Note that
  641. * we must compute the free space and rmap btree geometry before doing
  642. * this.
  643. */
  644. error = xfs_update_alignment(mp);
  645. if (error)
  646. goto out;
  647. /* enable fail_at_unmount as default */
  648. mp->m_fail_unmount = true;
  649. error = xfs_sysfs_init(&mp->m_kobj, &xfs_mp_ktype,
  650. NULL, mp->m_super->s_id);
  651. if (error)
  652. goto out;
  653. error = xfs_sysfs_init(&mp->m_stats.xs_kobj, &xfs_stats_ktype,
  654. &mp->m_kobj, "stats");
  655. if (error)
  656. goto out_remove_sysfs;
  657. error = xfs_error_sysfs_init(mp);
  658. if (error)
  659. goto out_del_stats;
  660. error = xfs_errortag_init(mp);
  661. if (error)
  662. goto out_remove_error_sysfs;
  663. error = xfs_uuid_mount(mp);
  664. if (error)
  665. goto out_remove_errortag;
  666. /*
  667. * Update the preferred write size based on the information from the
  668. * on-disk superblock.
  669. */
  670. mp->m_allocsize_log =
  671. max_t(uint32_t, sbp->sb_blocklog, mp->m_allocsize_log);
  672. mp->m_allocsize_blocks = 1U << (mp->m_allocsize_log - sbp->sb_blocklog);
  673. /* set the low space thresholds for dynamic preallocation */
  674. xfs_set_low_space_thresholds(mp);
  675. /*
  676. * If enabled, sparse inode chunk alignment is expected to match the
  677. * cluster size. Full inode chunk alignment must match the chunk size,
  678. * but that is checked on sb read verification...
  679. */
  680. if (xfs_sb_version_hassparseinodes(&mp->m_sb) &&
  681. mp->m_sb.sb_spino_align !=
  682. XFS_B_TO_FSBT(mp, igeo->inode_cluster_size_raw)) {
  683. xfs_warn(mp,
  684. "Sparse inode block alignment (%u) must match cluster size (%llu).",
  685. mp->m_sb.sb_spino_align,
  686. XFS_B_TO_FSBT(mp, igeo->inode_cluster_size_raw));
  687. error = -EINVAL;
  688. goto out_remove_uuid;
  689. }
  690. /*
  691. * Check that the data (and log if separate) is an ok size.
  692. */
  693. error = xfs_check_sizes(mp);
  694. if (error)
  695. goto out_remove_uuid;
  696. /*
  697. * Initialize realtime fields in the mount structure
  698. */
  699. error = xfs_rtmount_init(mp);
  700. if (error) {
  701. xfs_warn(mp, "RT mount failed");
  702. goto out_remove_uuid;
  703. }
  704. /*
  705. * Copies the low order bits of the timestamp and the randomly
  706. * set "sequence" number out of a UUID.
  707. */
  708. mp->m_fixedfsid[0] =
  709. (get_unaligned_be16(&sbp->sb_uuid.b[8]) << 16) |
  710. get_unaligned_be16(&sbp->sb_uuid.b[4]);
  711. mp->m_fixedfsid[1] = get_unaligned_be32(&sbp->sb_uuid.b[0]);
  712. error = xfs_da_mount(mp);
  713. if (error) {
  714. xfs_warn(mp, "Failed dir/attr init: %d", error);
  715. goto out_remove_uuid;
  716. }
  717. /*
  718. * Initialize the precomputed transaction reservations values.
  719. */
  720. xfs_trans_init(mp);
  721. /*
  722. * Allocate and initialize the per-ag data.
  723. */
  724. error = xfs_initialize_perag(mp, sbp->sb_agcount, &mp->m_maxagi);
  725. if (error) {
  726. xfs_warn(mp, "Failed per-ag init: %d", error);
  727. goto out_free_dir;
  728. }
  729. if (XFS_IS_CORRUPT(mp, !sbp->sb_logblocks)) {
  730. xfs_warn(mp, "no log defined");
  731. error = -EFSCORRUPTED;
  732. goto out_free_perag;
  733. }
  734. /*
  735. * Log's mount-time initialization. The first part of recovery can place
  736. * some items on the AIL, to be handled when recovery is finished or
  737. * cancelled.
  738. */
  739. error = xfs_log_mount(mp, mp->m_logdev_targp,
  740. XFS_FSB_TO_DADDR(mp, sbp->sb_logstart),
  741. XFS_FSB_TO_BB(mp, sbp->sb_logblocks));
  742. if (error) {
  743. xfs_warn(mp, "log mount failed");
  744. goto out_fail_wait;
  745. }
  746. /* Make sure the summary counts are ok. */
  747. error = xfs_check_summary_counts(mp);
  748. if (error)
  749. goto out_log_dealloc;
  750. /*
  751. * Get and sanity-check the root inode.
  752. * Save the pointer to it in the mount structure.
  753. */
  754. error = xfs_iget(mp, NULL, sbp->sb_rootino, XFS_IGET_UNTRUSTED,
  755. XFS_ILOCK_EXCL, &rip);
  756. if (error) {
  757. xfs_warn(mp,
  758. "Failed to read root inode 0x%llx, error %d",
  759. sbp->sb_rootino, -error);
  760. goto out_log_dealloc;
  761. }
  762. ASSERT(rip != NULL);
  763. if (XFS_IS_CORRUPT(mp, !S_ISDIR(VFS_I(rip)->i_mode))) {
  764. xfs_warn(mp, "corrupted root inode %llu: not a directory",
  765. (unsigned long long)rip->i_ino);
  766. xfs_iunlock(rip, XFS_ILOCK_EXCL);
  767. error = -EFSCORRUPTED;
  768. goto out_rele_rip;
  769. }
  770. mp->m_rootip = rip; /* save it */
  771. xfs_iunlock(rip, XFS_ILOCK_EXCL);
  772. /*
  773. * Initialize realtime inode pointers in the mount structure
  774. */
  775. error = xfs_rtmount_inodes(mp);
  776. if (error) {
  777. /*
  778. * Free up the root inode.
  779. */
  780. xfs_warn(mp, "failed to read RT inodes");
  781. goto out_rele_rip;
  782. }
  783. /*
  784. * If this is a read-only mount defer the superblock updates until
  785. * the next remount into writeable mode. Otherwise we would never
  786. * perform the update e.g. for the root filesystem.
  787. */
  788. if (mp->m_update_sb && !(mp->m_flags & XFS_MOUNT_RDONLY)) {
  789. error = xfs_sync_sb(mp, false);
  790. if (error) {
  791. xfs_warn(mp, "failed to write sb changes");
  792. goto out_rtunmount;
  793. }
  794. }
  795. /*
  796. * Initialise the XFS quota management subsystem for this mount
  797. */
  798. if (XFS_IS_QUOTA_RUNNING(mp)) {
  799. error = xfs_qm_newmount(mp, &quotamount, &quotaflags);
  800. if (error)
  801. goto out_rtunmount;
  802. } else {
  803. ASSERT(!XFS_IS_QUOTA_ON(mp));
  804. /*
  805. * If a file system had quotas running earlier, but decided to
  806. * mount without -o uquota/pquota/gquota options, revoke the
  807. * quotachecked license.
  808. */
  809. if (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_ACCT) {
  810. xfs_notice(mp, "resetting quota flags");
  811. error = xfs_mount_reset_sbqflags(mp);
  812. if (error)
  813. goto out_rtunmount;
  814. }
  815. }
  816. /*
  817. * Finish recovering the file system. This part needed to be delayed
  818. * until after the root and real-time bitmap inodes were consistently
  819. * read in.
  820. */
  821. error = xfs_log_mount_finish(mp);
  822. if (error) {
  823. xfs_warn(mp, "log mount finish failed");
  824. goto out_rtunmount;
  825. }
  826. /*
  827. * Now the log is fully replayed, we can transition to full read-only
  828. * mode for read-only mounts. This will sync all the metadata and clean
  829. * the log so that the recovery we just performed does not have to be
  830. * replayed again on the next mount.
  831. *
  832. * We use the same quiesce mechanism as the rw->ro remount, as they are
  833. * semantically identical operations.
  834. */
  835. if ((mp->m_flags & (XFS_MOUNT_RDONLY|XFS_MOUNT_NORECOVERY)) ==
  836. XFS_MOUNT_RDONLY) {
  837. xfs_quiesce_attr(mp);
  838. }
  839. /*
  840. * Complete the quota initialisation, post-log-replay component.
  841. */
  842. if (quotamount) {
  843. ASSERT(mp->m_qflags == 0);
  844. mp->m_qflags = quotaflags;
  845. xfs_qm_mount_quotas(mp);
  846. }
  847. /*
  848. * Now we are mounted, reserve a small amount of unused space for
  849. * privileged transactions. This is needed so that transaction
  850. * space required for critical operations can dip into this pool
  851. * when at ENOSPC. This is needed for operations like create with
  852. * attr, unwritten extent conversion at ENOSPC, etc. Data allocations
  853. * are not allowed to use this reserved space.
  854. *
  855. * This may drive us straight to ENOSPC on mount, but that implies
  856. * we were already there on the last unmount. Warn if this occurs.
  857. */
  858. if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
  859. resblks = xfs_default_resblks(mp);
  860. error = xfs_reserve_blocks(mp, &resblks, NULL);
  861. if (error)
  862. xfs_warn(mp,
  863. "Unable to allocate reserve blocks. Continuing without reserve pool.");
  864. /* Recover any CoW blocks that never got remapped. */
  865. error = xfs_reflink_recover_cow(mp);
  866. if (error) {
  867. xfs_err(mp,
  868. "Error %d recovering leftover CoW allocations.", error);
  869. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  870. goto out_quota;
  871. }
  872. /* Reserve AG blocks for future btree expansion. */
  873. error = xfs_fs_reserve_ag_blocks(mp);
  874. if (error && error != -ENOSPC)
  875. goto out_agresv;
  876. }
  877. return 0;
  878. out_agresv:
  879. xfs_fs_unreserve_ag_blocks(mp);
  880. out_quota:
  881. xfs_qm_unmount_quotas(mp);
  882. out_rtunmount:
  883. xfs_rtunmount_inodes(mp);
  884. out_rele_rip:
  885. xfs_irele(rip);
  886. /* Clean out dquots that might be in memory after quotacheck. */
  887. xfs_qm_unmount(mp);
  888. /*
  889. * Cancel all delayed reclaim work and reclaim the inodes directly.
  890. * We have to do this /after/ rtunmount and qm_unmount because those
  891. * two will have scheduled delayed reclaim for the rt/quota inodes.
  892. *
  893. * This is slightly different from the unmountfs call sequence
  894. * because we could be tearing down a partially set up mount. In
  895. * particular, if log_mount_finish fails we bail out without calling
  896. * qm_unmount_quotas and therefore rely on qm_unmount to release the
  897. * quota inodes.
  898. */
  899. cancel_delayed_work_sync(&mp->m_reclaim_work);
  900. xfs_reclaim_inodes(mp);
  901. xfs_health_unmount(mp);
  902. out_log_dealloc:
  903. mp->m_flags |= XFS_MOUNT_UNMOUNTING;
  904. xfs_log_mount_cancel(mp);
  905. out_fail_wait:
  906. if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp)
  907. xfs_wait_buftarg(mp->m_logdev_targp);
  908. xfs_wait_buftarg(mp->m_ddev_targp);
  909. out_free_perag:
  910. xfs_free_perag(mp);
  911. out_free_dir:
  912. xfs_da_unmount(mp);
  913. out_remove_uuid:
  914. xfs_uuid_unmount(mp);
  915. out_remove_errortag:
  916. xfs_errortag_del(mp);
  917. out_remove_error_sysfs:
  918. xfs_error_sysfs_del(mp);
  919. out_del_stats:
  920. xfs_sysfs_del(&mp->m_stats.xs_kobj);
  921. out_remove_sysfs:
  922. xfs_sysfs_del(&mp->m_kobj);
  923. out:
  924. return error;
  925. }
  926. /*
  927. * This flushes out the inodes,dquots and the superblock, unmounts the
  928. * log and makes sure that incore structures are freed.
  929. */
  930. void
  931. xfs_unmountfs(
  932. struct xfs_mount *mp)
  933. {
  934. uint64_t resblks;
  935. int error;
  936. xfs_stop_block_reaping(mp);
  937. xfs_fs_unreserve_ag_blocks(mp);
  938. xfs_qm_unmount_quotas(mp);
  939. xfs_rtunmount_inodes(mp);
  940. xfs_irele(mp->m_rootip);
  941. /*
  942. * We can potentially deadlock here if we have an inode cluster
  943. * that has been freed has its buffer still pinned in memory because
  944. * the transaction is still sitting in a iclog. The stale inodes
  945. * on that buffer will be pinned to the buffer until the
  946. * transaction hits the disk and the callbacks run. Pushing the AIL will
  947. * skip the stale inodes and may never see the pinned buffer, so
  948. * nothing will push out the iclog and unpin the buffer. Hence we
  949. * need to force the log here to ensure all items are flushed into the
  950. * AIL before we go any further.
  951. */
  952. xfs_log_force(mp, XFS_LOG_SYNC);
  953. /*
  954. * Wait for all busy extents to be freed, including completion of
  955. * any discard operation.
  956. */
  957. xfs_extent_busy_wait_all(mp);
  958. flush_workqueue(xfs_discard_wq);
  959. /*
  960. * We now need to tell the world we are unmounting. This will allow
  961. * us to detect that the filesystem is going away and we should error
  962. * out anything that we have been retrying in the background. This will
  963. * prevent neverending retries in AIL pushing from hanging the unmount.
  964. */
  965. mp->m_flags |= XFS_MOUNT_UNMOUNTING;
  966. /*
  967. * Flush all pending changes from the AIL.
  968. */
  969. xfs_ail_push_all_sync(mp->m_ail);
  970. /*
  971. * Reclaim all inodes. At this point there should be no dirty inodes and
  972. * none should be pinned or locked. Stop background inode reclaim here
  973. * if it is still running.
  974. */
  975. cancel_delayed_work_sync(&mp->m_reclaim_work);
  976. xfs_reclaim_inodes(mp);
  977. xfs_health_unmount(mp);
  978. xfs_qm_unmount(mp);
  979. /*
  980. * Unreserve any blocks we have so that when we unmount we don't account
  981. * the reserved free space as used. This is really only necessary for
  982. * lazy superblock counting because it trusts the incore superblock
  983. * counters to be absolutely correct on clean unmount.
  984. *
  985. * We don't bother correcting this elsewhere for lazy superblock
  986. * counting because on mount of an unclean filesystem we reconstruct the
  987. * correct counter value and this is irrelevant.
  988. *
  989. * For non-lazy counter filesystems, this doesn't matter at all because
  990. * we only every apply deltas to the superblock and hence the incore
  991. * value does not matter....
  992. */
  993. resblks = 0;
  994. error = xfs_reserve_blocks(mp, &resblks, NULL);
  995. if (error)
  996. xfs_warn(mp, "Unable to free reserved block pool. "
  997. "Freespace may not be correct on next mount.");
  998. error = xfs_log_sbcount(mp);
  999. if (error)
  1000. xfs_warn(mp, "Unable to update superblock counters. "
  1001. "Freespace may not be correct on next mount.");
  1002. xfs_log_unmount(mp);
  1003. xfs_da_unmount(mp);
  1004. xfs_uuid_unmount(mp);
  1005. #if defined(DEBUG)
  1006. xfs_errortag_clearall(mp);
  1007. #endif
  1008. xfs_free_perag(mp);
  1009. xfs_errortag_del(mp);
  1010. xfs_error_sysfs_del(mp);
  1011. xfs_sysfs_del(&mp->m_stats.xs_kobj);
  1012. xfs_sysfs_del(&mp->m_kobj);
  1013. }
  1014. /*
  1015. * Determine whether modifications can proceed. The caller specifies the minimum
  1016. * freeze level for which modifications should not be allowed. This allows
  1017. * certain operations to proceed while the freeze sequence is in progress, if
  1018. * necessary.
  1019. */
  1020. bool
  1021. xfs_fs_writable(
  1022. struct xfs_mount *mp,
  1023. int level)
  1024. {
  1025. ASSERT(level > SB_UNFROZEN);
  1026. if ((mp->m_super->s_writers.frozen >= level) ||
  1027. XFS_FORCED_SHUTDOWN(mp) || (mp->m_flags & XFS_MOUNT_RDONLY))
  1028. return false;
  1029. return true;
  1030. }
  1031. /*
  1032. * xfs_log_sbcount
  1033. *
  1034. * Sync the superblock counters to disk.
  1035. *
  1036. * Note this code can be called during the process of freezing, so we use the
  1037. * transaction allocator that does not block when the transaction subsystem is
  1038. * in its frozen state.
  1039. */
  1040. int
  1041. xfs_log_sbcount(xfs_mount_t *mp)
  1042. {
  1043. /* allow this to proceed during the freeze sequence... */
  1044. if (!xfs_fs_writable(mp, SB_FREEZE_COMPLETE))
  1045. return 0;
  1046. /*
  1047. * we don't need to do this if we are updating the superblock
  1048. * counters on every modification.
  1049. */
  1050. if (!xfs_sb_version_haslazysbcount(&mp->m_sb))
  1051. return 0;
  1052. return xfs_sync_sb(mp, true);
  1053. }
  1054. /*
  1055. * Deltas for the block count can vary from 1 to very large, but lock contention
  1056. * only occurs on frequent small block count updates such as in the delayed
  1057. * allocation path for buffered writes (page a time updates). Hence we set
  1058. * a large batch count (1024) to minimise global counter updates except when
  1059. * we get near to ENOSPC and we have to be very accurate with our updates.
  1060. */
  1061. #define XFS_FDBLOCKS_BATCH 1024
  1062. int
  1063. xfs_mod_fdblocks(
  1064. struct xfs_mount *mp,
  1065. int64_t delta,
  1066. bool rsvd)
  1067. {
  1068. int64_t lcounter;
  1069. long long res_used;
  1070. s32 batch;
  1071. if (delta > 0) {
  1072. /*
  1073. * If the reserve pool is depleted, put blocks back into it
  1074. * first. Most of the time the pool is full.
  1075. */
  1076. if (likely(mp->m_resblks == mp->m_resblks_avail)) {
  1077. percpu_counter_add(&mp->m_fdblocks, delta);
  1078. return 0;
  1079. }
  1080. spin_lock(&mp->m_sb_lock);
  1081. res_used = (long long)(mp->m_resblks - mp->m_resblks_avail);
  1082. if (res_used > delta) {
  1083. mp->m_resblks_avail += delta;
  1084. } else {
  1085. delta -= res_used;
  1086. mp->m_resblks_avail = mp->m_resblks;
  1087. percpu_counter_add(&mp->m_fdblocks, delta);
  1088. }
  1089. spin_unlock(&mp->m_sb_lock);
  1090. return 0;
  1091. }
  1092. /*
  1093. * Taking blocks away, need to be more accurate the closer we
  1094. * are to zero.
  1095. *
  1096. * If the counter has a value of less than 2 * max batch size,
  1097. * then make everything serialise as we are real close to
  1098. * ENOSPC.
  1099. */
  1100. if (__percpu_counter_compare(&mp->m_fdblocks, 2 * XFS_FDBLOCKS_BATCH,
  1101. XFS_FDBLOCKS_BATCH) < 0)
  1102. batch = 1;
  1103. else
  1104. batch = XFS_FDBLOCKS_BATCH;
  1105. percpu_counter_add_batch(&mp->m_fdblocks, delta, batch);
  1106. if (__percpu_counter_compare(&mp->m_fdblocks, mp->m_alloc_set_aside,
  1107. XFS_FDBLOCKS_BATCH) >= 0) {
  1108. /* we had space! */
  1109. return 0;
  1110. }
  1111. /*
  1112. * lock up the sb for dipping into reserves before releasing the space
  1113. * that took us to ENOSPC.
  1114. */
  1115. spin_lock(&mp->m_sb_lock);
  1116. percpu_counter_add(&mp->m_fdblocks, -delta);
  1117. if (!rsvd)
  1118. goto fdblocks_enospc;
  1119. lcounter = (long long)mp->m_resblks_avail + delta;
  1120. if (lcounter >= 0) {
  1121. mp->m_resblks_avail = lcounter;
  1122. spin_unlock(&mp->m_sb_lock);
  1123. return 0;
  1124. }
  1125. xfs_warn_once(mp,
  1126. "Reserve blocks depleted! Consider increasing reserve pool size.");
  1127. fdblocks_enospc:
  1128. spin_unlock(&mp->m_sb_lock);
  1129. return -ENOSPC;
  1130. }
  1131. int
  1132. xfs_mod_frextents(
  1133. struct xfs_mount *mp,
  1134. int64_t delta)
  1135. {
  1136. int64_t lcounter;
  1137. int ret = 0;
  1138. spin_lock(&mp->m_sb_lock);
  1139. lcounter = mp->m_sb.sb_frextents + delta;
  1140. if (lcounter < 0)
  1141. ret = -ENOSPC;
  1142. else
  1143. mp->m_sb.sb_frextents = lcounter;
  1144. spin_unlock(&mp->m_sb_lock);
  1145. return ret;
  1146. }
  1147. /*
  1148. * Used to free the superblock along various error paths.
  1149. */
  1150. void
  1151. xfs_freesb(
  1152. struct xfs_mount *mp)
  1153. {
  1154. struct xfs_buf *bp = mp->m_sb_bp;
  1155. xfs_buf_lock(bp);
  1156. mp->m_sb_bp = NULL;
  1157. xfs_buf_relse(bp);
  1158. }
  1159. /*
  1160. * If the underlying (data/log/rt) device is readonly, there are some
  1161. * operations that cannot proceed.
  1162. */
  1163. int
  1164. xfs_dev_is_read_only(
  1165. struct xfs_mount *mp,
  1166. char *message)
  1167. {
  1168. if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
  1169. xfs_readonly_buftarg(mp->m_logdev_targp) ||
  1170. (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
  1171. xfs_notice(mp, "%s required on read-only device.", message);
  1172. xfs_notice(mp, "write access unavailable, cannot proceed.");
  1173. return -EROFS;
  1174. }
  1175. return 0;
  1176. }
  1177. /* Force the summary counters to be recalculated at next mount. */
  1178. void
  1179. xfs_force_summary_recalc(
  1180. struct xfs_mount *mp)
  1181. {
  1182. if (!xfs_sb_version_haslazysbcount(&mp->m_sb))
  1183. return;
  1184. xfs_fs_mark_sick(mp, XFS_SICK_FS_COUNTERS);
  1185. }
  1186. /*
  1187. * Update the in-core delayed block counter.
  1188. *
  1189. * We prefer to update the counter without having to take a spinlock for every
  1190. * counter update (i.e. batching). Each change to delayed allocation
  1191. * reservations can change can easily exceed the default percpu counter
  1192. * batching, so we use a larger batch factor here.
  1193. *
  1194. * Note that we don't currently have any callers requiring fast summation
  1195. * (e.g. percpu_counter_read) so we can use a big batch value here.
  1196. */
  1197. #define XFS_DELALLOC_BATCH (4096)
  1198. void
  1199. xfs_mod_delalloc(
  1200. struct xfs_mount *mp,
  1201. int64_t delta)
  1202. {
  1203. percpu_counter_add_batch(&mp->m_delalloc_blks, delta,
  1204. XFS_DELALLOC_BATCH);
  1205. }