xfs_mount.c 33 KB

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