xfs_mount.c 35 KB

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