xfs_mount.c 35 KB

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