xfs_mount.c 35 KB

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