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. 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. * During the second phase of log recovery, we need iget and
  842. * iput to behave like they do for an active filesystem.
  843. * xfs_fs_drop_inode needs to be able to prevent the deletion
  844. * of inodes before we're done replaying log items on those
  845. * inodes.
  846. */
  847. mp->m_super->s_flags |= MS_ACTIVE;
  848. /*
  849. * Finish recovering the file system. This part needed to be delayed
  850. * until after the root and real-time bitmap inodes were consistently
  851. * read in.
  852. */
  853. error = xfs_log_mount_finish(mp);
  854. if (error) {
  855. xfs_warn(mp, "log mount finish failed");
  856. goto out_rtunmount;
  857. }
  858. /*
  859. * Now the log is fully replayed, we can transition to full read-only
  860. * mode for read-only mounts. This will sync all the metadata and clean
  861. * the log so that the recovery we just performed does not have to be
  862. * replayed again on the next mount.
  863. *
  864. * We use the same quiesce mechanism as the rw->ro remount, as they are
  865. * semantically identical operations.
  866. */
  867. if ((mp->m_flags & (XFS_MOUNT_RDONLY|XFS_MOUNT_NORECOVERY)) ==
  868. XFS_MOUNT_RDONLY) {
  869. xfs_quiesce_attr(mp);
  870. }
  871. /*
  872. * Complete the quota initialisation, post-log-replay component.
  873. */
  874. if (quotamount) {
  875. ASSERT(mp->m_qflags == 0);
  876. mp->m_qflags = quotaflags;
  877. xfs_qm_mount_quotas(mp);
  878. }
  879. /*
  880. * Now we are mounted, reserve a small amount of unused space for
  881. * privileged transactions. This is needed so that transaction
  882. * space required for critical operations can dip into this pool
  883. * when at ENOSPC. This is needed for operations like create with
  884. * attr, unwritten extent conversion at ENOSPC, etc. Data allocations
  885. * are not allowed to use this reserved space.
  886. *
  887. * This may drive us straight to ENOSPC on mount, but that implies
  888. * we were already there on the last unmount. Warn if this occurs.
  889. */
  890. if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
  891. resblks = xfs_default_resblks(mp);
  892. error = xfs_reserve_blocks(mp, &resblks, NULL);
  893. if (error)
  894. xfs_warn(mp,
  895. "Unable to allocate reserve blocks. Continuing without reserve pool.");
  896. /* Recover any CoW blocks that never got remapped. */
  897. error = xfs_reflink_recover_cow(mp);
  898. if (error) {
  899. xfs_err(mp,
  900. "Error %d recovering leftover CoW allocations.", error);
  901. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  902. goto out_quota;
  903. }
  904. /* Reserve AG blocks for future btree expansion. */
  905. error = xfs_fs_reserve_ag_blocks(mp);
  906. if (error && error != -ENOSPC)
  907. goto out_agresv;
  908. }
  909. return 0;
  910. out_agresv:
  911. xfs_fs_unreserve_ag_blocks(mp);
  912. out_quota:
  913. xfs_qm_unmount_quotas(mp);
  914. out_rtunmount:
  915. mp->m_super->s_flags &= ~MS_ACTIVE;
  916. xfs_rtunmount_inodes(mp);
  917. out_rele_rip:
  918. IRELE(rip);
  919. cancel_delayed_work_sync(&mp->m_reclaim_work);
  920. xfs_reclaim_inodes(mp, SYNC_WAIT);
  921. out_log_dealloc:
  922. mp->m_flags |= XFS_MOUNT_UNMOUNTING;
  923. xfs_log_mount_cancel(mp);
  924. out_fail_wait:
  925. if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp)
  926. xfs_wait_buftarg(mp->m_logdev_targp);
  927. xfs_wait_buftarg(mp->m_ddev_targp);
  928. out_free_perag:
  929. xfs_free_perag(mp);
  930. out_free_dir:
  931. xfs_da_unmount(mp);
  932. out_remove_uuid:
  933. xfs_uuid_unmount(mp);
  934. out_remove_errortag:
  935. xfs_errortag_del(mp);
  936. out_remove_error_sysfs:
  937. xfs_error_sysfs_del(mp);
  938. out_del_stats:
  939. xfs_sysfs_del(&mp->m_stats.xs_kobj);
  940. out_remove_sysfs:
  941. xfs_sysfs_del(&mp->m_kobj);
  942. out:
  943. return error;
  944. }
  945. /*
  946. * This flushes out the inodes,dquots and the superblock, unmounts the
  947. * log and makes sure that incore structures are freed.
  948. */
  949. void
  950. xfs_unmountfs(
  951. struct xfs_mount *mp)
  952. {
  953. uint64_t resblks;
  954. int error;
  955. cancel_delayed_work_sync(&mp->m_eofblocks_work);
  956. cancel_delayed_work_sync(&mp->m_cowblocks_work);
  957. xfs_fs_unreserve_ag_blocks(mp);
  958. xfs_qm_unmount_quotas(mp);
  959. xfs_rtunmount_inodes(mp);
  960. IRELE(mp->m_rootip);
  961. /*
  962. * We can potentially deadlock here if we have an inode cluster
  963. * that has been freed has its buffer still pinned in memory because
  964. * the transaction is still sitting in a iclog. The stale inodes
  965. * on that buffer will have their flush locks held until the
  966. * transaction hits the disk and the callbacks run. the inode
  967. * flush takes the flush lock unconditionally and with nothing to
  968. * push out the iclog we will never get that unlocked. hence we
  969. * need to force the log first.
  970. */
  971. xfs_log_force(mp, XFS_LOG_SYNC);
  972. /*
  973. * Wait for all busy extents to be freed, including completion of
  974. * any discard operation.
  975. */
  976. xfs_extent_busy_wait_all(mp);
  977. flush_workqueue(xfs_discard_wq);
  978. /*
  979. * We now need to tell the world we are unmounting. This will allow
  980. * us to detect that the filesystem is going away and we should error
  981. * out anything that we have been retrying in the background. This will
  982. * prevent neverending retries in AIL pushing from hanging the unmount.
  983. */
  984. mp->m_flags |= XFS_MOUNT_UNMOUNTING;
  985. /*
  986. * Flush all pending changes from the AIL.
  987. */
  988. xfs_ail_push_all_sync(mp->m_ail);
  989. /*
  990. * And reclaim all inodes. At this point there should be no dirty
  991. * inodes and none should be pinned or locked, but use synchronous
  992. * reclaim just to be sure. We can stop background inode reclaim
  993. * here as well if it is still running.
  994. */
  995. cancel_delayed_work_sync(&mp->m_reclaim_work);
  996. xfs_reclaim_inodes(mp, SYNC_WAIT);
  997. xfs_qm_unmount(mp);
  998. /*
  999. * Unreserve any blocks we have so that when we unmount we don't account
  1000. * the reserved free space as used. This is really only necessary for
  1001. * lazy superblock counting because it trusts the incore superblock
  1002. * counters to be absolutely correct on clean unmount.
  1003. *
  1004. * We don't bother correcting this elsewhere for lazy superblock
  1005. * counting because on mount of an unclean filesystem we reconstruct the
  1006. * correct counter value and this is irrelevant.
  1007. *
  1008. * For non-lazy counter filesystems, this doesn't matter at all because
  1009. * we only every apply deltas to the superblock and hence the incore
  1010. * value does not matter....
  1011. */
  1012. resblks = 0;
  1013. error = xfs_reserve_blocks(mp, &resblks, NULL);
  1014. if (error)
  1015. xfs_warn(mp, "Unable to free reserved block pool. "
  1016. "Freespace may not be correct on next mount.");
  1017. error = xfs_log_sbcount(mp);
  1018. if (error)
  1019. xfs_warn(mp, "Unable to update superblock counters. "
  1020. "Freespace may not be correct on next mount.");
  1021. xfs_log_unmount(mp);
  1022. xfs_da_unmount(mp);
  1023. xfs_uuid_unmount(mp);
  1024. #if defined(DEBUG)
  1025. xfs_errortag_clearall(mp);
  1026. #endif
  1027. xfs_free_perag(mp);
  1028. xfs_errortag_del(mp);
  1029. xfs_error_sysfs_del(mp);
  1030. xfs_sysfs_del(&mp->m_stats.xs_kobj);
  1031. xfs_sysfs_del(&mp->m_kobj);
  1032. }
  1033. /*
  1034. * Determine whether modifications can proceed. The caller specifies the minimum
  1035. * freeze level for which modifications should not be allowed. This allows
  1036. * certain operations to proceed while the freeze sequence is in progress, if
  1037. * necessary.
  1038. */
  1039. bool
  1040. xfs_fs_writable(
  1041. struct xfs_mount *mp,
  1042. int level)
  1043. {
  1044. ASSERT(level > SB_UNFROZEN);
  1045. if ((mp->m_super->s_writers.frozen >= level) ||
  1046. XFS_FORCED_SHUTDOWN(mp) || (mp->m_flags & XFS_MOUNT_RDONLY))
  1047. return false;
  1048. return true;
  1049. }
  1050. /*
  1051. * xfs_log_sbcount
  1052. *
  1053. * Sync the superblock counters to disk.
  1054. *
  1055. * Note this code can be called during the process of freezing, so we use the
  1056. * transaction allocator that does not block when the transaction subsystem is
  1057. * in its frozen state.
  1058. */
  1059. int
  1060. xfs_log_sbcount(xfs_mount_t *mp)
  1061. {
  1062. /* allow this to proceed during the freeze sequence... */
  1063. if (!xfs_fs_writable(mp, SB_FREEZE_COMPLETE))
  1064. return 0;
  1065. /*
  1066. * we don't need to do this if we are updating the superblock
  1067. * counters on every modification.
  1068. */
  1069. if (!xfs_sb_version_haslazysbcount(&mp->m_sb))
  1070. return 0;
  1071. return xfs_sync_sb(mp, true);
  1072. }
  1073. /*
  1074. * Deltas for the inode count are +/-64, hence we use a large batch size
  1075. * of 128 so we don't need to take the counter lock on every update.
  1076. */
  1077. #define XFS_ICOUNT_BATCH 128
  1078. int
  1079. xfs_mod_icount(
  1080. struct xfs_mount *mp,
  1081. int64_t delta)
  1082. {
  1083. percpu_counter_add_batch(&mp->m_icount, delta, XFS_ICOUNT_BATCH);
  1084. if (__percpu_counter_compare(&mp->m_icount, 0, XFS_ICOUNT_BATCH) < 0) {
  1085. ASSERT(0);
  1086. percpu_counter_add(&mp->m_icount, -delta);
  1087. return -EINVAL;
  1088. }
  1089. return 0;
  1090. }
  1091. int
  1092. xfs_mod_ifree(
  1093. struct xfs_mount *mp,
  1094. int64_t delta)
  1095. {
  1096. percpu_counter_add(&mp->m_ifree, delta);
  1097. if (percpu_counter_compare(&mp->m_ifree, 0) < 0) {
  1098. ASSERT(0);
  1099. percpu_counter_add(&mp->m_ifree, -delta);
  1100. return -EINVAL;
  1101. }
  1102. return 0;
  1103. }
  1104. /*
  1105. * Deltas for the block count can vary from 1 to very large, but lock contention
  1106. * only occurs on frequent small block count updates such as in the delayed
  1107. * allocation path for buffered writes (page a time updates). Hence we set
  1108. * a large batch count (1024) to minimise global counter updates except when
  1109. * we get near to ENOSPC and we have to be very accurate with our updates.
  1110. */
  1111. #define XFS_FDBLOCKS_BATCH 1024
  1112. int
  1113. xfs_mod_fdblocks(
  1114. struct xfs_mount *mp,
  1115. int64_t delta,
  1116. bool rsvd)
  1117. {
  1118. int64_t lcounter;
  1119. long long res_used;
  1120. s32 batch;
  1121. if (delta > 0) {
  1122. /*
  1123. * If the reserve pool is depleted, put blocks back into it
  1124. * first. Most of the time the pool is full.
  1125. */
  1126. if (likely(mp->m_resblks == mp->m_resblks_avail)) {
  1127. percpu_counter_add(&mp->m_fdblocks, delta);
  1128. return 0;
  1129. }
  1130. spin_lock(&mp->m_sb_lock);
  1131. res_used = (long long)(mp->m_resblks - mp->m_resblks_avail);
  1132. if (res_used > delta) {
  1133. mp->m_resblks_avail += delta;
  1134. } else {
  1135. delta -= res_used;
  1136. mp->m_resblks_avail = mp->m_resblks;
  1137. percpu_counter_add(&mp->m_fdblocks, delta);
  1138. }
  1139. spin_unlock(&mp->m_sb_lock);
  1140. return 0;
  1141. }
  1142. /*
  1143. * Taking blocks away, need to be more accurate the closer we
  1144. * are to zero.
  1145. *
  1146. * If the counter has a value of less than 2 * max batch size,
  1147. * then make everything serialise as we are real close to
  1148. * ENOSPC.
  1149. */
  1150. if (__percpu_counter_compare(&mp->m_fdblocks, 2 * XFS_FDBLOCKS_BATCH,
  1151. XFS_FDBLOCKS_BATCH) < 0)
  1152. batch = 1;
  1153. else
  1154. batch = XFS_FDBLOCKS_BATCH;
  1155. percpu_counter_add_batch(&mp->m_fdblocks, delta, batch);
  1156. if (__percpu_counter_compare(&mp->m_fdblocks, mp->m_alloc_set_aside,
  1157. XFS_FDBLOCKS_BATCH) >= 0) {
  1158. /* we had space! */
  1159. return 0;
  1160. }
  1161. /*
  1162. * lock up the sb for dipping into reserves before releasing the space
  1163. * that took us to ENOSPC.
  1164. */
  1165. spin_lock(&mp->m_sb_lock);
  1166. percpu_counter_add(&mp->m_fdblocks, -delta);
  1167. if (!rsvd)
  1168. goto fdblocks_enospc;
  1169. lcounter = (long long)mp->m_resblks_avail + delta;
  1170. if (lcounter >= 0) {
  1171. mp->m_resblks_avail = lcounter;
  1172. spin_unlock(&mp->m_sb_lock);
  1173. return 0;
  1174. }
  1175. printk_once(KERN_WARNING
  1176. "Filesystem \"%s\": reserve blocks depleted! "
  1177. "Consider increasing reserve pool size.",
  1178. mp->m_fsname);
  1179. fdblocks_enospc:
  1180. spin_unlock(&mp->m_sb_lock);
  1181. return -ENOSPC;
  1182. }
  1183. int
  1184. xfs_mod_frextents(
  1185. struct xfs_mount *mp,
  1186. int64_t delta)
  1187. {
  1188. int64_t lcounter;
  1189. int ret = 0;
  1190. spin_lock(&mp->m_sb_lock);
  1191. lcounter = mp->m_sb.sb_frextents + delta;
  1192. if (lcounter < 0)
  1193. ret = -ENOSPC;
  1194. else
  1195. mp->m_sb.sb_frextents = lcounter;
  1196. spin_unlock(&mp->m_sb_lock);
  1197. return ret;
  1198. }
  1199. /*
  1200. * xfs_getsb() is called to obtain the buffer for the superblock.
  1201. * The buffer is returned locked and read in from disk.
  1202. * The buffer should be released with a call to xfs_brelse().
  1203. *
  1204. * If the flags parameter is BUF_TRYLOCK, then we'll only return
  1205. * the superblock buffer if it can be locked without sleeping.
  1206. * If it can't then we'll return NULL.
  1207. */
  1208. struct xfs_buf *
  1209. xfs_getsb(
  1210. struct xfs_mount *mp,
  1211. int flags)
  1212. {
  1213. struct xfs_buf *bp = mp->m_sb_bp;
  1214. if (!xfs_buf_trylock(bp)) {
  1215. if (flags & XBF_TRYLOCK)
  1216. return NULL;
  1217. xfs_buf_lock(bp);
  1218. }
  1219. xfs_buf_hold(bp);
  1220. ASSERT(bp->b_flags & XBF_DONE);
  1221. return bp;
  1222. }
  1223. /*
  1224. * Used to free the superblock along various error paths.
  1225. */
  1226. void
  1227. xfs_freesb(
  1228. struct xfs_mount *mp)
  1229. {
  1230. struct xfs_buf *bp = mp->m_sb_bp;
  1231. xfs_buf_lock(bp);
  1232. mp->m_sb_bp = NULL;
  1233. xfs_buf_relse(bp);
  1234. }
  1235. /*
  1236. * If the underlying (data/log/rt) device is readonly, there are some
  1237. * operations that cannot proceed.
  1238. */
  1239. int
  1240. xfs_dev_is_read_only(
  1241. struct xfs_mount *mp,
  1242. char *message)
  1243. {
  1244. if (xfs_readonly_buftarg(mp->m_ddev_targp) ||
  1245. xfs_readonly_buftarg(mp->m_logdev_targp) ||
  1246. (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) {
  1247. xfs_notice(mp, "%s required on read-only device.", message);
  1248. xfs_notice(mp, "write access unavailable, cannot proceed.");
  1249. return -EROFS;
  1250. }
  1251. return 0;
  1252. }