xfs_mount.c 33 KB

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