xfs_mount.c 33 KB

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