xfs_fsops.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_sb.h"
  13. #include "xfs_mount.h"
  14. #include "xfs_trans.h"
  15. #include "xfs_error.h"
  16. #include "xfs_alloc.h"
  17. #include "xfs_fsops.h"
  18. #include "xfs_trans_space.h"
  19. #include "xfs_log.h"
  20. #include "xfs_log_priv.h"
  21. #include "xfs_ag.h"
  22. #include "xfs_ag_resv.h"
  23. #include "xfs_trace.h"
  24. /*
  25. * Write new AG headers to disk. Non-transactional, but need to be
  26. * written and completed prior to the growfs transaction being logged.
  27. * To do this, we use a delayed write buffer list and wait for
  28. * submission and IO completion of the list as a whole. This allows the
  29. * IO subsystem to merge all the AG headers in a single AG into a single
  30. * IO and hide most of the latency of the IO from us.
  31. *
  32. * This also means that if we get an error whilst building the buffer
  33. * list to write, we can cancel the entire list without having written
  34. * anything.
  35. */
  36. static int
  37. xfs_resizefs_init_new_ags(
  38. struct xfs_trans *tp,
  39. struct aghdr_init_data *id,
  40. xfs_agnumber_t oagcount,
  41. xfs_agnumber_t nagcount,
  42. xfs_rfsblock_t delta,
  43. struct xfs_perag *last_pag,
  44. bool *lastag_extended)
  45. {
  46. struct xfs_mount *mp = tp->t_mountp;
  47. xfs_rfsblock_t nb = mp->m_sb.sb_dblocks + delta;
  48. int error;
  49. *lastag_extended = false;
  50. INIT_LIST_HEAD(&id->buffer_list);
  51. for (id->agno = nagcount - 1;
  52. id->agno >= oagcount;
  53. id->agno--, delta -= id->agsize) {
  54. if (id->agno == nagcount - 1)
  55. id->agsize = nb - (id->agno *
  56. (xfs_rfsblock_t)mp->m_sb.sb_agblocks);
  57. else
  58. id->agsize = mp->m_sb.sb_agblocks;
  59. error = xfs_ag_init_headers(mp, id);
  60. if (error) {
  61. xfs_buf_delwri_cancel(&id->buffer_list);
  62. return error;
  63. }
  64. }
  65. error = xfs_buf_delwri_submit(&id->buffer_list);
  66. if (error)
  67. return error;
  68. if (delta) {
  69. *lastag_extended = true;
  70. error = xfs_ag_extend_space(last_pag, tp, delta);
  71. }
  72. return error;
  73. }
  74. /*
  75. * growfs operations
  76. */
  77. static int
  78. xfs_growfs_data_private(
  79. struct xfs_mount *mp, /* mount point for filesystem */
  80. struct xfs_growfs_data *in) /* growfs data input struct */
  81. {
  82. xfs_agnumber_t oagcount = mp->m_sb.sb_agcount;
  83. struct xfs_buf *bp;
  84. int error;
  85. xfs_agnumber_t nagcount;
  86. xfs_agnumber_t nagimax = 0;
  87. xfs_rfsblock_t nb, nb_div, nb_mod;
  88. int64_t delta;
  89. bool lastag_extended = false;
  90. struct xfs_trans *tp;
  91. struct aghdr_init_data id = {};
  92. struct xfs_perag *last_pag;
  93. nb = in->newblocks;
  94. error = xfs_sb_validate_fsb_count(&mp->m_sb, nb);
  95. if (error)
  96. return error;
  97. if (nb > mp->m_sb.sb_dblocks) {
  98. error = xfs_buf_read_uncached(mp->m_ddev_targp,
  99. XFS_FSB_TO_BB(mp, nb) - XFS_FSS_TO_BB(mp, 1),
  100. XFS_FSS_TO_BB(mp, 1), 0, &bp, NULL);
  101. if (error)
  102. return error;
  103. xfs_buf_relse(bp);
  104. }
  105. nb_div = nb;
  106. nb_mod = do_div(nb_div, mp->m_sb.sb_agblocks);
  107. if (nb_mod && nb_mod >= XFS_MIN_AG_BLOCKS)
  108. nb_div++;
  109. else if (nb_mod)
  110. nb = nb_div * mp->m_sb.sb_agblocks;
  111. if (nb_div > XFS_MAX_AGNUMBER + 1) {
  112. nb_div = XFS_MAX_AGNUMBER + 1;
  113. nb = nb_div * mp->m_sb.sb_agblocks;
  114. }
  115. nagcount = nb_div;
  116. delta = nb - mp->m_sb.sb_dblocks;
  117. /*
  118. * Reject filesystems with a single AG because they are not
  119. * supported, and reject a shrink operation that would cause a
  120. * filesystem to become unsupported.
  121. */
  122. if (delta < 0 && nagcount < 2)
  123. return -EINVAL;
  124. /* No work to do */
  125. if (delta == 0)
  126. return 0;
  127. /* TODO: shrinking the entire AGs hasn't yet completed */
  128. if (nagcount < oagcount)
  129. return -EINVAL;
  130. /* allocate the new per-ag structures */
  131. error = xfs_initialize_perag(mp, oagcount, nagcount, nb, &nagimax);
  132. if (error)
  133. return error;
  134. if (delta > 0)
  135. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growdata,
  136. XFS_GROWFS_SPACE_RES(mp), 0, XFS_TRANS_RESERVE,
  137. &tp);
  138. else
  139. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growdata, -delta, 0,
  140. 0, &tp);
  141. if (error)
  142. goto out_free_unused_perag;
  143. last_pag = xfs_perag_get(mp, oagcount - 1);
  144. if (delta > 0) {
  145. error = xfs_resizefs_init_new_ags(tp, &id, oagcount, nagcount,
  146. delta, last_pag, &lastag_extended);
  147. } else {
  148. xfs_warn_mount(mp, XFS_OPSTATE_WARNED_SHRINK,
  149. "EXPERIMENTAL online shrink feature in use. Use at your own risk!");
  150. error = xfs_ag_shrink_space(last_pag, &tp, -delta);
  151. }
  152. xfs_perag_put(last_pag);
  153. if (error)
  154. goto out_trans_cancel;
  155. /*
  156. * Update changed superblock fields transactionally. These are not
  157. * seen by the rest of the world until the transaction commit applies
  158. * them atomically to the superblock.
  159. */
  160. if (nagcount > oagcount)
  161. xfs_trans_mod_sb(tp, XFS_TRANS_SB_AGCOUNT, nagcount - oagcount);
  162. if (delta)
  163. xfs_trans_mod_sb(tp, XFS_TRANS_SB_DBLOCKS, delta);
  164. if (id.nfree)
  165. xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, id.nfree);
  166. /*
  167. * Sync sb counters now to reflect the updated values. This is
  168. * particularly important for shrink because the write verifier
  169. * will fail if sb_fdblocks is ever larger than sb_dblocks.
  170. */
  171. if (xfs_has_lazysbcount(mp))
  172. xfs_log_sb(tp);
  173. xfs_trans_set_sync(tp);
  174. error = xfs_trans_commit(tp);
  175. if (error)
  176. return error;
  177. /* New allocation groups fully initialized, so update mount struct */
  178. if (nagimax)
  179. mp->m_maxagi = nagimax;
  180. xfs_set_low_space_thresholds(mp);
  181. mp->m_alloc_set_aside = xfs_alloc_set_aside(mp);
  182. if (delta > 0) {
  183. /*
  184. * If we expanded the last AG, free the per-AG reservation
  185. * so we can reinitialize it with the new size.
  186. */
  187. if (lastag_extended) {
  188. struct xfs_perag *pag;
  189. pag = xfs_perag_get(mp, id.agno);
  190. xfs_ag_resv_free(pag);
  191. xfs_perag_put(pag);
  192. }
  193. /*
  194. * Reserve AG metadata blocks. ENOSPC here does not mean there
  195. * was a growfs failure, just that there still isn't space for
  196. * new user data after the grow has been run.
  197. */
  198. error = xfs_fs_reserve_ag_blocks(mp);
  199. if (error == -ENOSPC)
  200. error = 0;
  201. }
  202. return error;
  203. out_trans_cancel:
  204. xfs_trans_cancel(tp);
  205. out_free_unused_perag:
  206. if (nagcount > oagcount)
  207. xfs_free_perag_range(mp, oagcount, nagcount);
  208. return error;
  209. }
  210. static int
  211. xfs_growfs_log_private(
  212. struct xfs_mount *mp, /* mount point for filesystem */
  213. struct xfs_growfs_log *in) /* growfs log input struct */
  214. {
  215. xfs_extlen_t nb;
  216. nb = in->newblocks;
  217. if (nb < XFS_MIN_LOG_BLOCKS || nb < XFS_B_TO_FSB(mp, XFS_MIN_LOG_BYTES))
  218. return -EINVAL;
  219. if (nb == mp->m_sb.sb_logblocks &&
  220. in->isint == (mp->m_sb.sb_logstart != 0))
  221. return -EINVAL;
  222. /*
  223. * Moving the log is hard, need new interfaces to sync
  224. * the log first, hold off all activity while moving it.
  225. * Can have shorter or longer log in the same space,
  226. * or transform internal to external log or vice versa.
  227. */
  228. return -ENOSYS;
  229. }
  230. static int
  231. xfs_growfs_imaxpct(
  232. struct xfs_mount *mp,
  233. __u32 imaxpct)
  234. {
  235. struct xfs_trans *tp;
  236. int dpct;
  237. int error;
  238. if (imaxpct > 100)
  239. return -EINVAL;
  240. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growdata,
  241. XFS_GROWFS_SPACE_RES(mp), 0, XFS_TRANS_RESERVE, &tp);
  242. if (error)
  243. return error;
  244. dpct = imaxpct - mp->m_sb.sb_imax_pct;
  245. xfs_trans_mod_sb(tp, XFS_TRANS_SB_IMAXPCT, dpct);
  246. xfs_trans_set_sync(tp);
  247. return xfs_trans_commit(tp);
  248. }
  249. /*
  250. * protected versions of growfs function acquire and release locks on the mount
  251. * point - exported through ioctls: XFS_IOC_FSGROWFSDATA, XFS_IOC_FSGROWFSLOG,
  252. * XFS_IOC_FSGROWFSRT
  253. */
  254. int
  255. xfs_growfs_data(
  256. struct xfs_mount *mp,
  257. struct xfs_growfs_data *in)
  258. {
  259. int error = 0;
  260. if (!capable(CAP_SYS_ADMIN))
  261. return -EPERM;
  262. if (!mutex_trylock(&mp->m_growlock))
  263. return -EWOULDBLOCK;
  264. /* update imaxpct separately to the physical grow of the filesystem */
  265. if (in->imaxpct != mp->m_sb.sb_imax_pct) {
  266. error = xfs_growfs_imaxpct(mp, in->imaxpct);
  267. if (error)
  268. goto out_error;
  269. }
  270. if (in->newblocks != mp->m_sb.sb_dblocks) {
  271. error = xfs_growfs_data_private(mp, in);
  272. if (error)
  273. goto out_error;
  274. }
  275. /* Post growfs calculations needed to reflect new state in operations */
  276. if (mp->m_sb.sb_imax_pct) {
  277. uint64_t icount = mp->m_sb.sb_dblocks * mp->m_sb.sb_imax_pct;
  278. do_div(icount, 100);
  279. M_IGEO(mp)->maxicount = XFS_FSB_TO_INO(mp, icount);
  280. } else
  281. M_IGEO(mp)->maxicount = 0;
  282. /* Update secondary superblocks now the physical grow has completed */
  283. error = xfs_update_secondary_sbs(mp);
  284. out_error:
  285. /*
  286. * Increment the generation unconditionally, the error could be from
  287. * updating the secondary superblocks, in which case the new size
  288. * is live already.
  289. */
  290. mp->m_generation++;
  291. mutex_unlock(&mp->m_growlock);
  292. return error;
  293. }
  294. int
  295. xfs_growfs_log(
  296. xfs_mount_t *mp,
  297. struct xfs_growfs_log *in)
  298. {
  299. int error;
  300. if (!capable(CAP_SYS_ADMIN))
  301. return -EPERM;
  302. if (!mutex_trylock(&mp->m_growlock))
  303. return -EWOULDBLOCK;
  304. error = xfs_growfs_log_private(mp, in);
  305. mutex_unlock(&mp->m_growlock);
  306. return error;
  307. }
  308. /*
  309. * Reserve the requested number of blocks if available. Otherwise return
  310. * as many as possible to satisfy the request. The actual number
  311. * reserved are returned in outval.
  312. */
  313. int
  314. xfs_reserve_blocks(
  315. struct xfs_mount *mp,
  316. uint64_t request)
  317. {
  318. int64_t lcounter, delta;
  319. int64_t fdblks_delta = 0;
  320. int64_t free;
  321. int error = 0;
  322. /*
  323. * With per-cpu counters, this becomes an interesting problem. we need
  324. * to work out if we are freeing or allocation blocks first, then we can
  325. * do the modification as necessary.
  326. *
  327. * We do this under the m_sb_lock so that if we are near ENOSPC, we will
  328. * hold out any changes while we work out what to do. This means that
  329. * the amount of free space can change while we do this, so we need to
  330. * retry if we end up trying to reserve more space than is available.
  331. */
  332. spin_lock(&mp->m_sb_lock);
  333. /*
  334. * If our previous reservation was larger than the current value,
  335. * then move any unused blocks back to the free pool. Modify the resblks
  336. * counters directly since we shouldn't have any problems unreserving
  337. * space.
  338. */
  339. if (mp->m_resblks > request) {
  340. lcounter = mp->m_resblks_avail - request;
  341. if (lcounter > 0) { /* release unused blocks */
  342. fdblks_delta = lcounter;
  343. mp->m_resblks_avail -= lcounter;
  344. }
  345. mp->m_resblks = request;
  346. if (fdblks_delta) {
  347. spin_unlock(&mp->m_sb_lock);
  348. xfs_add_fdblocks(mp, fdblks_delta);
  349. spin_lock(&mp->m_sb_lock);
  350. }
  351. goto out;
  352. }
  353. /*
  354. * If the request is larger than the current reservation, reserve the
  355. * blocks before we update the reserve counters. Sample m_fdblocks and
  356. * perform a partial reservation if the request exceeds free space.
  357. *
  358. * The code below estimates how many blocks it can request from
  359. * fdblocks to stash in the reserve pool. This is a classic TOCTOU
  360. * race since fdblocks updates are not always coordinated via
  361. * m_sb_lock. Set the reserve size even if there's not enough free
  362. * space to fill it because mod_fdblocks will refill an undersized
  363. * reserve when it can.
  364. */
  365. free = percpu_counter_sum(&mp->m_fdblocks) -
  366. xfs_fdblocks_unavailable(mp);
  367. delta = request - mp->m_resblks;
  368. mp->m_resblks = request;
  369. if (delta > 0 && free > 0) {
  370. /*
  371. * We'll either succeed in getting space from the free block
  372. * count or we'll get an ENOSPC. Don't set the reserved flag
  373. * here - we don't want to reserve the extra reserve blocks
  374. * from the reserve.
  375. *
  376. * The desired reserve size can change after we drop the lock.
  377. * Use mod_fdblocks to put the space into the reserve or into
  378. * fdblocks as appropriate.
  379. */
  380. fdblks_delta = min(free, delta);
  381. spin_unlock(&mp->m_sb_lock);
  382. error = xfs_dec_fdblocks(mp, fdblks_delta, 0);
  383. if (!error)
  384. xfs_add_fdblocks(mp, fdblks_delta);
  385. spin_lock(&mp->m_sb_lock);
  386. }
  387. out:
  388. spin_unlock(&mp->m_sb_lock);
  389. return error;
  390. }
  391. int
  392. xfs_fs_goingdown(
  393. xfs_mount_t *mp,
  394. uint32_t inflags)
  395. {
  396. switch (inflags) {
  397. case XFS_FSOP_GOING_FLAGS_DEFAULT: {
  398. if (!bdev_freeze(mp->m_super->s_bdev)) {
  399. xfs_force_shutdown(mp, SHUTDOWN_FORCE_UMOUNT);
  400. bdev_thaw(mp->m_super->s_bdev);
  401. }
  402. break;
  403. }
  404. case XFS_FSOP_GOING_FLAGS_LOGFLUSH:
  405. xfs_force_shutdown(mp, SHUTDOWN_FORCE_UMOUNT);
  406. break;
  407. case XFS_FSOP_GOING_FLAGS_NOLOGFLUSH:
  408. xfs_force_shutdown(mp,
  409. SHUTDOWN_FORCE_UMOUNT | SHUTDOWN_LOG_IO_ERROR);
  410. break;
  411. default:
  412. return -EINVAL;
  413. }
  414. return 0;
  415. }
  416. /*
  417. * Force a shutdown of the filesystem instantly while keeping the filesystem
  418. * consistent. We don't do an unmount here; just shutdown the shop, make sure
  419. * that absolutely nothing persistent happens to this filesystem after this
  420. * point.
  421. *
  422. * The shutdown state change is atomic, resulting in the first and only the
  423. * first shutdown call processing the shutdown. This means we only shutdown the
  424. * log once as it requires, and we don't spam the logs when multiple concurrent
  425. * shutdowns race to set the shutdown flags.
  426. */
  427. void
  428. xfs_do_force_shutdown(
  429. struct xfs_mount *mp,
  430. uint32_t flags,
  431. char *fname,
  432. int lnnum)
  433. {
  434. int tag;
  435. const char *why;
  436. if (xfs_set_shutdown(mp)) {
  437. xlog_shutdown_wait(mp->m_log);
  438. return;
  439. }
  440. if (mp->m_sb_bp)
  441. mp->m_sb_bp->b_flags |= XBF_DONE;
  442. if (flags & SHUTDOWN_FORCE_UMOUNT)
  443. xfs_alert(mp, "User initiated shutdown received.");
  444. if (xlog_force_shutdown(mp->m_log, flags)) {
  445. tag = XFS_PTAG_SHUTDOWN_LOGERROR;
  446. why = "Log I/O Error";
  447. } else if (flags & SHUTDOWN_CORRUPT_INCORE) {
  448. tag = XFS_PTAG_SHUTDOWN_CORRUPT;
  449. why = "Corruption of in-memory data";
  450. } else if (flags & SHUTDOWN_CORRUPT_ONDISK) {
  451. tag = XFS_PTAG_SHUTDOWN_CORRUPT;
  452. why = "Corruption of on-disk metadata";
  453. } else if (flags & SHUTDOWN_DEVICE_REMOVED) {
  454. tag = XFS_PTAG_SHUTDOWN_IOERROR;
  455. why = "Block device removal";
  456. } else {
  457. tag = XFS_PTAG_SHUTDOWN_IOERROR;
  458. why = "Metadata I/O Error";
  459. }
  460. trace_xfs_force_shutdown(mp, tag, flags, fname, lnnum);
  461. xfs_alert_tag(mp, tag,
  462. "%s (0x%x) detected at %pS (%s:%d). Shutting down filesystem.",
  463. why, flags, __return_address, fname, lnnum);
  464. xfs_alert(mp,
  465. "Please unmount the filesystem and rectify the problem(s)");
  466. if (xfs_error_level >= XFS_ERRLEVEL_HIGH)
  467. xfs_stack_trace();
  468. }
  469. /*
  470. * Reserve free space for per-AG metadata.
  471. */
  472. int
  473. xfs_fs_reserve_ag_blocks(
  474. struct xfs_mount *mp)
  475. {
  476. xfs_agnumber_t agno;
  477. struct xfs_perag *pag;
  478. int error = 0;
  479. int err2;
  480. mp->m_finobt_nores = false;
  481. for_each_perag(mp, agno, pag) {
  482. err2 = xfs_ag_resv_init(pag, NULL);
  483. if (err2 && !error)
  484. error = err2;
  485. }
  486. if (error && error != -ENOSPC) {
  487. xfs_warn(mp,
  488. "Error %d reserving per-AG metadata reserve pool.", error);
  489. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  490. }
  491. return error;
  492. }
  493. /*
  494. * Free space reserved for per-AG metadata.
  495. */
  496. void
  497. xfs_fs_unreserve_ag_blocks(
  498. struct xfs_mount *mp)
  499. {
  500. xfs_agnumber_t agno;
  501. struct xfs_perag *pag;
  502. for_each_perag(mp, agno, pag)
  503. xfs_ag_resv_free(pag);
  504. }