xfs_mount.c 33 KB

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