xfs_mount.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406
  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. mutex_destroy(&pag->pag_ici_reclaim_lock);
  147. call_rcu(&pag->rcu_head, __xfs_free_perag);
  148. }
  149. }
  150. /*
  151. * Check size of device based on the (data/realtime) block count.
  152. * Note: this check is used by the growfs code as well as mount.
  153. */
  154. int
  155. xfs_sb_validate_fsb_count(
  156. xfs_sb_t *sbp,
  157. uint64_t nblocks)
  158. {
  159. ASSERT(PAGE_SHIFT >= sbp->sb_blocklog);
  160. ASSERT(sbp->sb_blocklog >= BBSHIFT);
  161. /* Limited by ULONG_MAX of page cache index */
  162. if (nblocks >> (PAGE_SHIFT - sbp->sb_blocklog) > ULONG_MAX)
  163. return -EFBIG;
  164. return 0;
  165. }
  166. int
  167. xfs_initialize_perag(
  168. xfs_mount_t *mp,
  169. xfs_agnumber_t agcount,
  170. xfs_agnumber_t *maxagi)
  171. {
  172. xfs_agnumber_t index;
  173. xfs_agnumber_t first_initialised = NULLAGNUMBER;
  174. xfs_perag_t *pag;
  175. int error = -ENOMEM;
  176. /*
  177. * Walk the current per-ag tree so we don't try to initialise AGs
  178. * that already exist (growfs case). Allocate and insert all the
  179. * AGs we don't find ready for initialisation.
  180. */
  181. for (index = 0; index < agcount; index++) {
  182. pag = xfs_perag_get(mp, index);
  183. if (pag) {
  184. xfs_perag_put(pag);
  185. continue;
  186. }
  187. pag = kmem_zalloc(sizeof(*pag), KM_MAYFAIL);
  188. if (!pag)
  189. goto out_unwind_new_pags;
  190. pag->pag_agno = index;
  191. pag->pag_mount = mp;
  192. spin_lock_init(&pag->pag_ici_lock);
  193. mutex_init(&pag->pag_ici_reclaim_lock);
  194. INIT_RADIX_TREE(&pag->pag_ici_root, GFP_ATOMIC);
  195. if (xfs_buf_hash_init(pag))
  196. goto out_free_pag;
  197. init_waitqueue_head(&pag->pagb_wait);
  198. if (radix_tree_preload(GFP_NOFS))
  199. goto out_hash_destroy;
  200. spin_lock(&mp->m_perag_lock);
  201. if (radix_tree_insert(&mp->m_perag_tree, index, pag)) {
  202. BUG();
  203. spin_unlock(&mp->m_perag_lock);
  204. radix_tree_preload_end();
  205. error = -EEXIST;
  206. goto out_hash_destroy;
  207. }
  208. spin_unlock(&mp->m_perag_lock);
  209. radix_tree_preload_end();
  210. /* first new pag is fully initialized */
  211. if (first_initialised == NULLAGNUMBER)
  212. first_initialised = index;
  213. }
  214. index = xfs_set_inode_alloc(mp, agcount);
  215. if (maxagi)
  216. *maxagi = index;
  217. mp->m_ag_prealloc_blocks = xfs_prealloc_blocks(mp);
  218. return 0;
  219. out_hash_destroy:
  220. xfs_buf_hash_destroy(pag);
  221. out_free_pag:
  222. mutex_destroy(&pag->pag_ici_reclaim_lock);
  223. kmem_free(pag);
  224. out_unwind_new_pags:
  225. /* unwind any prior newly initialized pags */
  226. for (index = first_initialised; index < agcount; index++) {
  227. pag = radix_tree_delete(&mp->m_perag_tree, index);
  228. if (!pag)
  229. break;
  230. xfs_buf_hash_destroy(pag);
  231. mutex_destroy(&pag->pag_ici_reclaim_lock);
  232. kmem_free(pag);
  233. }
  234. return error;
  235. }
  236. /*
  237. * xfs_readsb
  238. *
  239. * Does the initial read of the superblock.
  240. */
  241. int
  242. xfs_readsb(
  243. struct xfs_mount *mp,
  244. int flags)
  245. {
  246. unsigned int sector_size;
  247. struct xfs_buf *bp;
  248. struct xfs_sb *sbp = &mp->m_sb;
  249. int error;
  250. int loud = !(flags & XFS_MFSI_QUIET);
  251. const struct xfs_buf_ops *buf_ops;
  252. ASSERT(mp->m_sb_bp == NULL);
  253. ASSERT(mp->m_ddev_targp != NULL);
  254. /*
  255. * For the initial read, we must guess at the sector
  256. * size based on the block device. It's enough to
  257. * get the sb_sectsize out of the superblock and
  258. * then reread with the proper length.
  259. * We don't verify it yet, because it may not be complete.
  260. */
  261. sector_size = xfs_getsize_buftarg(mp->m_ddev_targp);
  262. buf_ops = NULL;
  263. /*
  264. * Allocate a (locked) buffer to hold the superblock. This will be kept
  265. * around at all times to optimize access to the superblock. Therefore,
  266. * set XBF_NO_IOACCT to make sure it doesn't hold the buftarg count
  267. * elevated.
  268. */
  269. reread:
  270. error = xfs_buf_read_uncached(mp->m_ddev_targp, XFS_SB_DADDR,
  271. BTOBB(sector_size), XBF_NO_IOACCT, &bp,
  272. buf_ops);
  273. if (error) {
  274. if (loud)
  275. xfs_warn(mp, "SB validate failed with error %d.", error);
  276. /* bad CRC means corrupted metadata */
  277. if (error == -EFSBADCRC)
  278. error = -EFSCORRUPTED;
  279. return error;
  280. }
  281. /*
  282. * Initialize the mount structure from the superblock.
  283. */
  284. xfs_sb_from_disk(sbp, XFS_BUF_TO_SBP(bp));
  285. /*
  286. * If we haven't validated the superblock, do so now before we try
  287. * to check the sector size and reread the superblock appropriately.
  288. */
  289. if (sbp->sb_magicnum != XFS_SB_MAGIC) {
  290. if (loud)
  291. xfs_warn(mp, "Invalid superblock magic number");
  292. error = -EINVAL;
  293. goto release_buf;
  294. }
  295. /*
  296. * We must be able to do sector-sized and sector-aligned IO.
  297. */
  298. if (sector_size > sbp->sb_sectsize) {
  299. if (loud)
  300. xfs_warn(mp, "device supports %u byte sectors (not %u)",
  301. sector_size, sbp->sb_sectsize);
  302. error = -ENOSYS;
  303. goto release_buf;
  304. }
  305. if (buf_ops == NULL) {
  306. /*
  307. * Re-read the superblock so the buffer is correctly sized,
  308. * and properly verified.
  309. */
  310. xfs_buf_relse(bp);
  311. sector_size = sbp->sb_sectsize;
  312. buf_ops = loud ? &xfs_sb_buf_ops : &xfs_sb_quiet_buf_ops;
  313. goto reread;
  314. }
  315. xfs_reinit_percpu_counters(mp);
  316. /* no need to be quiet anymore, so reset the buf ops */
  317. bp->b_ops = &xfs_sb_buf_ops;
  318. mp->m_sb_bp = bp;
  319. xfs_buf_unlock(bp);
  320. return 0;
  321. release_buf:
  322. xfs_buf_relse(bp);
  323. return error;
  324. }
  325. /*
  326. * Update alignment values based on mount options and sb values
  327. */
  328. STATIC int
  329. xfs_update_alignment(xfs_mount_t *mp)
  330. {
  331. xfs_sb_t *sbp = &(mp->m_sb);
  332. if (mp->m_dalign) {
  333. /*
  334. * If stripe unit and stripe width are not multiples
  335. * of the fs blocksize turn off alignment.
  336. */
  337. if ((BBTOB(mp->m_dalign) & mp->m_blockmask) ||
  338. (BBTOB(mp->m_swidth) & mp->m_blockmask)) {
  339. xfs_warn(mp,
  340. "alignment check failed: sunit/swidth vs. blocksize(%d)",
  341. sbp->sb_blocksize);
  342. return -EINVAL;
  343. } else {
  344. /*
  345. * Convert the stripe unit and width to FSBs.
  346. */
  347. mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign);
  348. if (mp->m_dalign && (sbp->sb_agblocks % mp->m_dalign)) {
  349. xfs_warn(mp,
  350. "alignment check failed: sunit/swidth vs. agsize(%d)",
  351. sbp->sb_agblocks);
  352. return -EINVAL;
  353. } else if (mp->m_dalign) {
  354. mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth);
  355. } else {
  356. xfs_warn(mp,
  357. "alignment check failed: sunit(%d) less than bsize(%d)",
  358. mp->m_dalign, sbp->sb_blocksize);
  359. return -EINVAL;
  360. }
  361. }
  362. /*
  363. * Update superblock with new values
  364. * and log changes
  365. */
  366. if (xfs_sb_version_hasdalign(sbp)) {
  367. if (sbp->sb_unit != mp->m_dalign) {
  368. sbp->sb_unit = mp->m_dalign;
  369. mp->m_update_sb = true;
  370. }
  371. if (sbp->sb_width != mp->m_swidth) {
  372. sbp->sb_width = mp->m_swidth;
  373. mp->m_update_sb = true;
  374. }
  375. } else {
  376. xfs_warn(mp,
  377. "cannot change alignment: superblock does not support data alignment");
  378. return -EINVAL;
  379. }
  380. } else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN &&
  381. xfs_sb_version_hasdalign(&mp->m_sb)) {
  382. mp->m_dalign = sbp->sb_unit;
  383. mp->m_swidth = sbp->sb_width;
  384. }
  385. return 0;
  386. }
  387. /*
  388. * Set the maximum inode count for this filesystem
  389. */
  390. STATIC void
  391. xfs_set_maxicount(xfs_mount_t *mp)
  392. {
  393. xfs_sb_t *sbp = &(mp->m_sb);
  394. uint64_t icount;
  395. if (sbp->sb_imax_pct) {
  396. /*
  397. * Make sure the maximum inode count is a multiple
  398. * of the units we allocate inodes in.
  399. */
  400. icount = sbp->sb_dblocks * sbp->sb_imax_pct;
  401. do_div(icount, 100);
  402. do_div(icount, mp->m_ialloc_blks);
  403. mp->m_maxicount = (icount * mp->m_ialloc_blks) <<
  404. sbp->sb_inopblog;
  405. } else {
  406. mp->m_maxicount = 0;
  407. }
  408. }
  409. /*
  410. * Set the default minimum read and write sizes unless
  411. * already specified in a mount option.
  412. * We use smaller I/O sizes when the file system
  413. * is being used for NFS service (wsync mount option).
  414. */
  415. STATIC void
  416. xfs_set_rw_sizes(xfs_mount_t *mp)
  417. {
  418. xfs_sb_t *sbp = &(mp->m_sb);
  419. int readio_log, writeio_log;
  420. if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)) {
  421. if (mp->m_flags & XFS_MOUNT_WSYNC) {
  422. readio_log = XFS_WSYNC_READIO_LOG;
  423. writeio_log = XFS_WSYNC_WRITEIO_LOG;
  424. } else {
  425. readio_log = XFS_READIO_LOG_LARGE;
  426. writeio_log = XFS_WRITEIO_LOG_LARGE;
  427. }
  428. } else {
  429. readio_log = mp->m_readio_log;
  430. writeio_log = mp->m_writeio_log;
  431. }
  432. if (sbp->sb_blocklog > readio_log) {
  433. mp->m_readio_log = sbp->sb_blocklog;
  434. } else {
  435. mp->m_readio_log = readio_log;
  436. }
  437. mp->m_readio_blocks = 1 << (mp->m_readio_log - sbp->sb_blocklog);
  438. if (sbp->sb_blocklog > writeio_log) {
  439. mp->m_writeio_log = sbp->sb_blocklog;
  440. } else {
  441. mp->m_writeio_log = writeio_log;
  442. }
  443. mp->m_writeio_blocks = 1 << (mp->m_writeio_log - sbp->sb_blocklog);
  444. }
  445. /*
  446. * precalculate the low space thresholds for dynamic speculative preallocation.
  447. */
  448. void
  449. xfs_set_low_space_thresholds(
  450. struct xfs_mount *mp)
  451. {
  452. int i;
  453. for (i = 0; i < XFS_LOWSP_MAX; i++) {
  454. uint64_t space = mp->m_sb.sb_dblocks;
  455. do_div(space, 100);
  456. mp->m_low_space[i] = space * (i + 1);
  457. }
  458. }
  459. /*
  460. * Set whether we're using inode alignment.
  461. */
  462. STATIC void
  463. xfs_set_inoalignment(xfs_mount_t *mp)
  464. {
  465. if (xfs_sb_version_hasalign(&mp->m_sb) &&
  466. mp->m_sb.sb_inoalignmt >= xfs_icluster_size_fsb(mp))
  467. mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1;
  468. else
  469. mp->m_inoalign_mask = 0;
  470. /*
  471. * If we are using stripe alignment, check whether
  472. * the stripe unit is a multiple of the inode alignment
  473. */
  474. if (mp->m_dalign && mp->m_inoalign_mask &&
  475. !(mp->m_dalign & mp->m_inoalign_mask))
  476. mp->m_sinoalign = mp->m_dalign;
  477. else
  478. mp->m_sinoalign = 0;
  479. }
  480. /*
  481. * Check that the data (and log if separate) is an ok size.
  482. */
  483. STATIC int
  484. xfs_check_sizes(
  485. struct xfs_mount *mp)
  486. {
  487. struct xfs_buf *bp;
  488. xfs_daddr_t d;
  489. int error;
  490. d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
  491. if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
  492. xfs_warn(mp, "filesystem size mismatch detected");
  493. return -EFBIG;
  494. }
  495. error = xfs_buf_read_uncached(mp->m_ddev_targp,
  496. d - XFS_FSS_TO_BB(mp, 1),
  497. XFS_FSS_TO_BB(mp, 1), 0, &bp, NULL);
  498. if (error) {
  499. xfs_warn(mp, "last sector read failed");
  500. return error;
  501. }
  502. xfs_buf_relse(bp);
  503. if (mp->m_logdev_targp == mp->m_ddev_targp)
  504. return 0;
  505. d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
  506. if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) {
  507. xfs_warn(mp, "log size mismatch detected");
  508. return -EFBIG;
  509. }
  510. error = xfs_buf_read_uncached(mp->m_logdev_targp,
  511. d - XFS_FSB_TO_BB(mp, 1),
  512. XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL);
  513. if (error) {
  514. xfs_warn(mp, "log device read failed");
  515. return error;
  516. }
  517. xfs_buf_relse(bp);
  518. return 0;
  519. }
  520. /*
  521. * Clear the quotaflags in memory and in the superblock.
  522. */
  523. int
  524. xfs_mount_reset_sbqflags(
  525. struct xfs_mount *mp)
  526. {
  527. mp->m_qflags = 0;
  528. /* It is OK to look at sb_qflags in the mount path without m_sb_lock. */
  529. if (mp->m_sb.sb_qflags == 0)
  530. return 0;
  531. spin_lock(&mp->m_sb_lock);
  532. mp->m_sb.sb_qflags = 0;
  533. spin_unlock(&mp->m_sb_lock);
  534. if (!xfs_fs_writable(mp, SB_FREEZE_WRITE))
  535. return 0;
  536. return xfs_sync_sb(mp, false);
  537. }
  538. uint64_t
  539. xfs_default_resblks(xfs_mount_t *mp)
  540. {
  541. uint64_t resblks;
  542. /*
  543. * We default to 5% or 8192 fsbs of space reserved, whichever is
  544. * smaller. This is intended to cover concurrent allocation
  545. * transactions when we initially hit enospc. These each require a 4
  546. * block reservation. Hence by default we cover roughly 2000 concurrent
  547. * allocation reservations.
  548. */
  549. resblks = mp->m_sb.sb_dblocks;
  550. do_div(resblks, 20);
  551. resblks = min_t(uint64_t, resblks, 8192);
  552. return resblks;
  553. }
  554. /*
  555. * This function does the following on an initial mount of a file system:
  556. * - reads the superblock from disk and init the mount struct
  557. * - if we're a 32-bit kernel, do a size check on the superblock
  558. * so we don't mount terabyte filesystems
  559. * - init mount struct realtime fields
  560. * - allocate inode hash table for fs
  561. * - init directory manager
  562. * - perform recovery and init the log manager
  563. */
  564. int
  565. xfs_mountfs(
  566. struct xfs_mount *mp)
  567. {
  568. struct xfs_sb *sbp = &(mp->m_sb);
  569. struct xfs_inode *rip;
  570. uint64_t resblks;
  571. uint quotamount = 0;
  572. uint quotaflags = 0;
  573. int error = 0;
  574. xfs_sb_mount_common(mp, sbp);
  575. /*
  576. * Check for a mismatched features2 values. Older kernels read & wrote
  577. * into the wrong sb offset for sb_features2 on some platforms due to
  578. * xfs_sb_t not being 64bit size aligned when sb_features2 was added,
  579. * which made older superblock reading/writing routines swap it as a
  580. * 64-bit value.
  581. *
  582. * For backwards compatibility, we make both slots equal.
  583. *
  584. * If we detect a mismatched field, we OR the set bits into the existing
  585. * features2 field in case it has already been modified; we don't want
  586. * to lose any features. We then update the bad location with the ORed
  587. * value so that older kernels will see any features2 flags. The
  588. * superblock writeback code ensures the new sb_features2 is copied to
  589. * sb_bad_features2 before it is logged or written to disk.
  590. */
  591. if (xfs_sb_has_mismatched_features2(sbp)) {
  592. xfs_warn(mp, "correcting sb_features alignment problem");
  593. sbp->sb_features2 |= sbp->sb_bad_features2;
  594. mp->m_update_sb = true;
  595. /*
  596. * Re-check for ATTR2 in case it was found in bad_features2
  597. * slot.
  598. */
  599. if (xfs_sb_version_hasattr2(&mp->m_sb) &&
  600. !(mp->m_flags & XFS_MOUNT_NOATTR2))
  601. mp->m_flags |= XFS_MOUNT_ATTR2;
  602. }
  603. if (xfs_sb_version_hasattr2(&mp->m_sb) &&
  604. (mp->m_flags & XFS_MOUNT_NOATTR2)) {
  605. xfs_sb_version_removeattr2(&mp->m_sb);
  606. mp->m_update_sb = true;
  607. /* update sb_versionnum for the clearing of the morebits */
  608. if (!sbp->sb_features2)
  609. mp->m_update_sb = true;
  610. }
  611. /* always use v2 inodes by default now */
  612. if (!(mp->m_sb.sb_versionnum & XFS_SB_VERSION_NLINKBIT)) {
  613. mp->m_sb.sb_versionnum |= XFS_SB_VERSION_NLINKBIT;
  614. mp->m_update_sb = true;
  615. }
  616. /*
  617. * Check if sb_agblocks is aligned at stripe boundary
  618. * If sb_agblocks is NOT aligned turn off m_dalign since
  619. * allocator alignment is within an ag, therefore ag has
  620. * to be aligned at stripe boundary.
  621. */
  622. error = xfs_update_alignment(mp);
  623. if (error)
  624. goto out;
  625. xfs_alloc_compute_maxlevels(mp);
  626. xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
  627. xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
  628. xfs_ialloc_compute_maxlevels(mp);
  629. xfs_rmapbt_compute_maxlevels(mp);
  630. xfs_refcountbt_compute_maxlevels(mp);
  631. xfs_set_maxicount(mp);
  632. /* enable fail_at_unmount as default */
  633. mp->m_fail_unmount = true;
  634. error = xfs_sysfs_init(&mp->m_kobj, &xfs_mp_ktype, NULL, mp->m_fsname);
  635. if (error)
  636. goto out;
  637. error = xfs_sysfs_init(&mp->m_stats.xs_kobj, &xfs_stats_ktype,
  638. &mp->m_kobj, "stats");
  639. if (error)
  640. goto out_remove_sysfs;
  641. error = xfs_error_sysfs_init(mp);
  642. if (error)
  643. goto out_del_stats;
  644. error = xfs_errortag_init(mp);
  645. if (error)
  646. goto out_remove_error_sysfs;
  647. error = xfs_uuid_mount(mp);
  648. if (error)
  649. goto out_remove_errortag;
  650. /*
  651. * Set the minimum read and write sizes
  652. */
  653. xfs_set_rw_sizes(mp);
  654. /* set the low space thresholds for dynamic preallocation */
  655. xfs_set_low_space_thresholds(mp);
  656. /*
  657. * Set the inode cluster size.
  658. * This may still be overridden by the file system
  659. * block size if it is larger than the chosen cluster size.
  660. *
  661. * For v5 filesystems, scale the cluster size with the inode size to
  662. * keep a constant ratio of inode per cluster buffer, but only if mkfs
  663. * has set the inode alignment value appropriately for larger cluster
  664. * sizes.
  665. */
  666. mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE;
  667. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  668. int new_size = mp->m_inode_cluster_size;
  669. new_size *= mp->m_sb.sb_inodesize / XFS_DINODE_MIN_SIZE;
  670. if (mp->m_sb.sb_inoalignmt >= XFS_B_TO_FSBT(mp, new_size))
  671. mp->m_inode_cluster_size = new_size;
  672. }
  673. /*
  674. * If enabled, sparse inode chunk alignment is expected to match the
  675. * cluster size. Full inode chunk alignment must match the chunk size,
  676. * but that is checked on sb read verification...
  677. */
  678. if (xfs_sb_version_hassparseinodes(&mp->m_sb) &&
  679. mp->m_sb.sb_spino_align !=
  680. XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size)) {
  681. xfs_warn(mp,
  682. "Sparse inode block alignment (%u) must match cluster size (%llu).",
  683. mp->m_sb.sb_spino_align,
  684. XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size));
  685. error = -EINVAL;
  686. goto out_remove_uuid;
  687. }
  688. /*
  689. * Set inode alignment fields
  690. */
  691. xfs_set_inoalignment(mp);
  692. /*
  693. * Check that the data (and log if separate) is an ok size.
  694. */
  695. error = xfs_check_sizes(mp);
  696. if (error)
  697. goto out_remove_uuid;
  698. /*
  699. * Initialize realtime fields in the mount structure
  700. */
  701. error = xfs_rtmount_init(mp);
  702. if (error) {
  703. xfs_warn(mp, "RT mount failed");
  704. goto out_remove_uuid;
  705. }
  706. /*
  707. * Copies the low order bits of the timestamp and the randomly
  708. * set "sequence" number out of a UUID.
  709. */
  710. mp->m_fixedfsid[0] =
  711. (get_unaligned_be16(&sbp->sb_uuid.b[8]) << 16) |
  712. get_unaligned_be16(&sbp->sb_uuid.b[4]);
  713. mp->m_fixedfsid[1] = get_unaligned_be32(&sbp->sb_uuid.b[0]);
  714. error = xfs_da_mount(mp);
  715. if (error) {
  716. xfs_warn(mp, "Failed dir/attr init: %d", error);
  717. goto out_remove_uuid;
  718. }
  719. /*
  720. * Initialize the precomputed transaction reservations values.
  721. */
  722. xfs_trans_init(mp);
  723. /*
  724. * Allocate and initialize the per-ag data.
  725. */
  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. /* Clean out dquots that might be in memory after quotacheck. */
  911. xfs_qm_unmount(mp);
  912. /*
  913. * Cancel all delayed reclaim work and reclaim the inodes directly.
  914. * We have to do this /after/ rtunmount and qm_unmount because those
  915. * two will have scheduled delayed reclaim for the rt/quota inodes.
  916. *
  917. * This is slightly different from the unmountfs call sequence
  918. * because we could be tearing down a partially set up mount. In
  919. * particular, if log_mount_finish fails we bail out without calling
  920. * qm_unmount_quotas and therefore rely on qm_unmount to release the
  921. * quota inodes.
  922. */
  923. cancel_delayed_work_sync(&mp->m_reclaim_work);
  924. xfs_reclaim_inodes(mp, SYNC_WAIT);
  925. out_log_dealloc:
  926. mp->m_flags |= XFS_MOUNT_UNMOUNTING;
  927. xfs_log_mount_cancel(mp);
  928. out_fail_wait:
  929. if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp)
  930. xfs_wait_buftarg(mp->m_logdev_targp);
  931. xfs_wait_buftarg(mp->m_ddev_targp);
  932. out_free_perag:
  933. xfs_free_perag(mp);
  934. out_free_dir:
  935. xfs_da_unmount(mp);
  936. out_remove_uuid:
  937. xfs_uuid_unmount(mp);
  938. out_remove_errortag:
  939. xfs_errortag_del(mp);
  940. out_remove_error_sysfs:
  941. xfs_error_sysfs_del(mp);
  942. out_del_stats:
  943. xfs_sysfs_del(&mp->m_stats.xs_kobj);
  944. out_remove_sysfs:
  945. xfs_sysfs_del(&mp->m_kobj);
  946. out:
  947. return error;
  948. }
  949. /*
  950. * This flushes out the inodes,dquots and the superblock, unmounts the
  951. * log and makes sure that incore structures are freed.
  952. */
  953. void
  954. xfs_unmountfs(
  955. struct xfs_mount *mp)
  956. {
  957. uint64_t resblks;
  958. int error;
  959. xfs_icache_disable_reclaim(mp);
  960. xfs_fs_unreserve_ag_blocks(mp);
  961. xfs_qm_unmount_quotas(mp);
  962. xfs_rtunmount_inodes(mp);
  963. IRELE(mp->m_rootip);
  964. /*
  965. * We can potentially deadlock here if we have an inode cluster
  966. * that has been freed has its buffer still pinned in memory because
  967. * the transaction is still sitting in a iclog. The stale inodes
  968. * on that buffer will have their flush locks held until the
  969. * transaction hits the disk and the callbacks run. the inode
  970. * flush takes the flush lock unconditionally and with nothing to
  971. * push out the iclog we will never get that unlocked. hence we
  972. * need to force the log first.
  973. */
  974. xfs_log_force(mp, XFS_LOG_SYNC);
  975. /*
  976. * Wait for all busy extents to be freed, including completion of
  977. * any discard operation.
  978. */
  979. xfs_extent_busy_wait_all(mp);
  980. flush_workqueue(xfs_discard_wq);
  981. /*
  982. * We now need to tell the world we are unmounting. This will allow
  983. * us to detect that the filesystem is going away and we should error
  984. * out anything that we have been retrying in the background. This will
  985. * prevent neverending retries in AIL pushing from hanging the unmount.
  986. */
  987. mp->m_flags |= XFS_MOUNT_UNMOUNTING;
  988. /*
  989. * Flush all pending changes from the AIL.
  990. */
  991. xfs_ail_push_all_sync(mp->m_ail);
  992. /*
  993. * And reclaim all inodes. At this point there should be no dirty
  994. * inodes and none should be pinned or locked, but use synchronous
  995. * reclaim just to be sure. We can stop background inode reclaim
  996. * here as well if it is still running.
  997. */
  998. cancel_delayed_work_sync(&mp->m_reclaim_work);
  999. xfs_reclaim_inodes(mp, SYNC_WAIT);
  1000. xfs_qm_unmount(mp);
  1001. /*
  1002. * Unreserve any blocks we have so that when we unmount we don't account
  1003. * the reserved free space as used. This is really only necessary for
  1004. * lazy superblock counting because it trusts the incore superblock
  1005. * counters to be absolutely correct on clean unmount.
  1006. *
  1007. * We don't bother correcting this elsewhere for lazy superblock
  1008. * counting because on mount of an unclean filesystem we reconstruct the
  1009. * correct counter value and this is irrelevant.
  1010. *
  1011. * For non-lazy counter filesystems, this doesn't matter at all because
  1012. * we only every apply deltas to the superblock and hence the incore
  1013. * value does not matter....
  1014. */
  1015. resblks = 0;
  1016. error = xfs_reserve_blocks(mp, &resblks, NULL);
  1017. if (error)
  1018. xfs_warn(mp, "Unable to free reserved block pool. "
  1019. "Freespace may not be correct on next mount.");
  1020. error = xfs_log_sbcount(mp);
  1021. if (error)
  1022. xfs_warn(mp, "Unable to update superblock counters. "
  1023. "Freespace may not be correct on next mount.");
  1024. xfs_log_unmount(mp);
  1025. xfs_da_unmount(mp);
  1026. xfs_uuid_unmount(mp);
  1027. #if defined(DEBUG)
  1028. xfs_errortag_clearall(mp);
  1029. #endif
  1030. xfs_free_perag(mp);
  1031. xfs_errortag_del(mp);
  1032. xfs_error_sysfs_del(mp);
  1033. xfs_sysfs_del(&mp->m_stats.xs_kobj);
  1034. xfs_sysfs_del(&mp->m_kobj);
  1035. }
  1036. /*
  1037. * Determine whether modifications can proceed. The caller specifies the minimum
  1038. * freeze level for which modifications should not be allowed. This allows
  1039. * certain operations to proceed while the freeze sequence is in progress, if
  1040. * necessary.
  1041. */
  1042. bool
  1043. xfs_fs_writable(
  1044. struct xfs_mount *mp,
  1045. int level)
  1046. {
  1047. ASSERT(level > SB_UNFROZEN);
  1048. if ((mp->m_super->s_writers.frozen >= level) ||
  1049. XFS_FORCED_SHUTDOWN(mp) || (mp->m_flags & XFS_MOUNT_RDONLY))
  1050. return false;
  1051. return true;
  1052. }
  1053. /*
  1054. * xfs_log_sbcount
  1055. *
  1056. * Sync the superblock counters to disk.
  1057. *
  1058. * Note this code can be called during the process of freezing, so we use the
  1059. * transaction allocator that does not block when the transaction subsystem is
  1060. * in its frozen state.
  1061. */
  1062. int
  1063. xfs_log_sbcount(xfs_mount_t *mp)
  1064. {
  1065. /* allow this to proceed during the freeze sequence... */
  1066. if (!xfs_fs_writable(mp, SB_FREEZE_COMPLETE))
  1067. return 0;
  1068. /*
  1069. * we don't need to do this if we are updating the superblock
  1070. * counters on every modification.
  1071. */
  1072. if (!xfs_sb_version_haslazysbcount(&mp->m_sb))
  1073. return 0;
  1074. return xfs_sync_sb(mp, true);
  1075. }
  1076. /*
  1077. * Deltas for the inode count are +/-64, hence we use a large batch size
  1078. * of 128 so we don't need to take the counter lock on every update.
  1079. */
  1080. #define XFS_ICOUNT_BATCH 128
  1081. int
  1082. xfs_mod_icount(
  1083. struct xfs_mount *mp,
  1084. int64_t delta)
  1085. {
  1086. percpu_counter_add_batch(&mp->m_icount, delta, XFS_ICOUNT_BATCH);
  1087. if (__percpu_counter_compare(&mp->m_icount, 0, XFS_ICOUNT_BATCH) < 0) {
  1088. ASSERT(0);
  1089. percpu_counter_add(&mp->m_icount, -delta);
  1090. return -EINVAL;
  1091. }
  1092. return 0;
  1093. }
  1094. int
  1095. xfs_mod_ifree(
  1096. struct xfs_mount *mp,
  1097. int64_t delta)
  1098. {
  1099. percpu_counter_add(&mp->m_ifree, delta);
  1100. if (percpu_counter_compare(&mp->m_ifree, 0) < 0) {
  1101. ASSERT(0);
  1102. percpu_counter_add(&mp->m_ifree, -delta);
  1103. return -EINVAL;
  1104. }
  1105. return 0;
  1106. }
  1107. /*
  1108. * Deltas for the block count can vary from 1 to very large, but lock contention
  1109. * only occurs on frequent small block count updates such as in the delayed
  1110. * allocation path for buffered writes (page a time updates). Hence we set
  1111. * a large batch count (1024) to minimise global counter updates except when
  1112. * we get near to ENOSPC and we have to be very accurate with our updates.
  1113. */
  1114. #define XFS_FDBLOCKS_BATCH 1024
  1115. int
  1116. xfs_mod_fdblocks(
  1117. struct xfs_mount *mp,
  1118. int64_t delta,
  1119. bool rsvd)
  1120. {
  1121. int64_t lcounter;
  1122. long long res_used;
  1123. s32 batch;
  1124. if (delta > 0) {
  1125. /*
  1126. * If the reserve pool is depleted, put blocks back into it
  1127. * first. Most of the time the pool is full.
  1128. */
  1129. if (likely(mp->m_resblks == mp->m_resblks_avail)) {
  1130. percpu_counter_add(&mp->m_fdblocks, delta);
  1131. return 0;
  1132. }
  1133. spin_lock(&mp->m_sb_lock);
  1134. res_used = (long long)(mp->m_resblks - mp->m_resblks_avail);
  1135. if (res_used > delta) {
  1136. mp->m_resblks_avail += delta;
  1137. } else {
  1138. delta -= res_used;
  1139. mp->m_resblks_avail = mp->m_resblks;
  1140. percpu_counter_add(&mp->m_fdblocks, delta);
  1141. }
  1142. spin_unlock(&mp->m_sb_lock);
  1143. return 0;
  1144. }
  1145. /*
  1146. * Taking blocks away, need to be more accurate the closer we
  1147. * are to zero.
  1148. *
  1149. * If the counter has a value of less than 2 * max batch size,
  1150. * then make everything serialise as we are real close to
  1151. * ENOSPC.
  1152. */
  1153. if (__percpu_counter_compare(&mp->m_fdblocks, 2 * XFS_FDBLOCKS_BATCH,
  1154. XFS_FDBLOCKS_BATCH) < 0)
  1155. batch = 1;
  1156. else
  1157. batch = XFS_FDBLOCKS_BATCH;
  1158. percpu_counter_add_batch(&mp->m_fdblocks, delta, batch);
  1159. if (__percpu_counter_compare(&mp->m_fdblocks, mp->m_alloc_set_aside,
  1160. XFS_FDBLOCKS_BATCH) >= 0) {
  1161. /* we had space! */
  1162. return 0;
  1163. }
  1164. /*
  1165. * lock up the sb for dipping into reserves before releasing the space
  1166. * that took us to ENOSPC.
  1167. */
  1168. spin_lock(&mp->m_sb_lock);
  1169. percpu_counter_add(&mp->m_fdblocks, -delta);
  1170. if (!rsvd)
  1171. goto fdblocks_enospc;
  1172. lcounter = (long long)mp->m_resblks_avail + delta;
  1173. if (lcounter >= 0) {
  1174. mp->m_resblks_avail = lcounter;
  1175. spin_unlock(&mp->m_sb_lock);
  1176. return 0;
  1177. }
  1178. printk_once(KERN_WARNING
  1179. "Filesystem \"%s\": reserve blocks depleted! "
  1180. "Consider increasing reserve pool size.",
  1181. mp->m_fsname);
  1182. fdblocks_enospc:
  1183. spin_unlock(&mp->m_sb_lock);
  1184. return -ENOSPC;
  1185. }
  1186. int
  1187. xfs_mod_frextents(
  1188. struct xfs_mount *mp,
  1189. int64_t delta)
  1190. {
  1191. int64_t lcounter;
  1192. int ret = 0;
  1193. spin_lock(&mp->m_sb_lock);
  1194. lcounter = mp->m_sb.sb_frextents + delta;
  1195. if (lcounter < 0)
  1196. ret = -ENOSPC;
  1197. else
  1198. mp->m_sb.sb_frextents = lcounter;
  1199. spin_unlock(&mp->m_sb_lock);
  1200. return ret;
  1201. }
  1202. /*
  1203. * xfs_getsb() is called to obtain the buffer for the superblock.
  1204. * The buffer is returned locked and read in from disk.
  1205. * The buffer should be released with a call to xfs_brelse().
  1206. *
  1207. * If the flags parameter is BUF_TRYLOCK, then we'll only return
  1208. * the superblock buffer if it can be locked without sleeping.
  1209. * If it can't then we'll return NULL.
  1210. */
  1211. struct xfs_buf *
  1212. xfs_getsb(
  1213. struct xfs_mount *mp,
  1214. int flags)
  1215. {
  1216. struct xfs_buf *bp = mp->m_sb_bp;
  1217. if (!xfs_buf_trylock(bp)) {
  1218. if (flags & XBF_TRYLOCK)
  1219. return NULL;
  1220. xfs_buf_lock(bp);
  1221. }
  1222. xfs_buf_hold(bp);
  1223. ASSERT(bp->b_flags & XBF_DONE);
  1224. return bp;
  1225. }
  1226. /*
  1227. * Used to free the superblock along various error paths.
  1228. */
  1229. void
  1230. xfs_freesb(
  1231. struct xfs_mount *mp)
  1232. {
  1233. struct xfs_buf *bp = mp->m_sb_bp;
  1234. xfs_buf_lock(bp);
  1235. mp->m_sb_bp = NULL;
  1236. xfs_buf_relse(bp);
  1237. }
  1238. /*
  1239. * If the underlying (data/log/rt) device is readonly, there are some
  1240. * operations that cannot proceed.
  1241. */
  1242. int
  1243. xfs_dev_is_read_only(
  1244. struct xfs_mount *mp,
  1245. char *message)
  1246. {
  1247. if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
  1248. xfs_readonly_buftarg(mp->m_logdev_targp) ||
  1249. (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
  1250. xfs_notice(mp, "%s required on read-only device.", message);
  1251. xfs_notice(mp, "write access unavailable, cannot proceed.");
  1252. return -EROFS;
  1253. }
  1254. return 0;
  1255. }