xfs_fsops.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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_defer.h"
  15. #include "xfs_trans.h"
  16. #include "xfs_error.h"
  17. #include "xfs_btree.h"
  18. #include "xfs_alloc.h"
  19. #include "xfs_fsops.h"
  20. #include "xfs_trans_space.h"
  21. #include "xfs_rtalloc.h"
  22. #include "xfs_trace.h"
  23. #include "xfs_log.h"
  24. #include "xfs_ag.h"
  25. #include "xfs_ag_resv.h"
  26. /*
  27. * growfs operations
  28. */
  29. static int
  30. xfs_growfs_data_private(
  31. xfs_mount_t *mp, /* mount point for filesystem */
  32. xfs_growfs_data_t *in) /* growfs data input struct */
  33. {
  34. xfs_buf_t *bp;
  35. int error;
  36. xfs_agnumber_t nagcount;
  37. xfs_agnumber_t nagimax = 0;
  38. xfs_rfsblock_t nb, nb_mod;
  39. xfs_rfsblock_t new;
  40. xfs_agnumber_t oagcount;
  41. xfs_trans_t *tp;
  42. LIST_HEAD (buffer_list);
  43. struct aghdr_init_data id = {};
  44. nb = in->newblocks;
  45. if (nb < mp->m_sb.sb_dblocks)
  46. return -EINVAL;
  47. if ((error = xfs_sb_validate_fsb_count(&mp->m_sb, nb)))
  48. return error;
  49. error = xfs_buf_read_uncached(mp->m_ddev_targp,
  50. XFS_FSB_TO_BB(mp, nb) - XFS_FSS_TO_BB(mp, 1),
  51. XFS_FSS_TO_BB(mp, 1), 0, &bp, NULL);
  52. if (error)
  53. return error;
  54. xfs_buf_relse(bp);
  55. new = nb; /* use new as a temporary here */
  56. nb_mod = do_div(new, mp->m_sb.sb_agblocks);
  57. nagcount = new + (nb_mod != 0);
  58. if (nb_mod && nb_mod < XFS_MIN_AG_BLOCKS) {
  59. nagcount--;
  60. nb = (xfs_rfsblock_t)nagcount * mp->m_sb.sb_agblocks;
  61. if (nb < mp->m_sb.sb_dblocks)
  62. return -EINVAL;
  63. }
  64. new = nb - mp->m_sb.sb_dblocks;
  65. oagcount = mp->m_sb.sb_agcount;
  66. /* allocate the new per-ag structures */
  67. if (nagcount > oagcount) {
  68. error = xfs_initialize_perag(mp, nagcount, &nagimax);
  69. if (error)
  70. return error;
  71. }
  72. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growdata,
  73. XFS_GROWFS_SPACE_RES(mp), 0, XFS_TRANS_RESERVE, &tp);
  74. if (error)
  75. return error;
  76. /*
  77. * Write new AG headers to disk. Non-transactional, but need to be
  78. * written and completed prior to the growfs transaction being logged.
  79. * To do this, we use a delayed write buffer list and wait for
  80. * submission and IO completion of the list as a whole. This allows the
  81. * IO subsystem to merge all the AG headers in a single AG into a single
  82. * IO and hide most of the latency of the IO from us.
  83. *
  84. * This also means that if we get an error whilst building the buffer
  85. * list to write, we can cancel the entire list without having written
  86. * anything.
  87. */
  88. INIT_LIST_HEAD(&id.buffer_list);
  89. for (id.agno = nagcount - 1;
  90. id.agno >= oagcount;
  91. id.agno--, new -= id.agsize) {
  92. if (id.agno == nagcount - 1)
  93. id.agsize = nb -
  94. (id.agno * (xfs_rfsblock_t)mp->m_sb.sb_agblocks);
  95. else
  96. id.agsize = mp->m_sb.sb_agblocks;
  97. error = xfs_ag_init_headers(mp, &id);
  98. if (error) {
  99. xfs_buf_delwri_cancel(&id.buffer_list);
  100. goto out_trans_cancel;
  101. }
  102. }
  103. error = xfs_buf_delwri_submit(&id.buffer_list);
  104. if (error)
  105. goto out_trans_cancel;
  106. xfs_trans_agblocks_delta(tp, id.nfree);
  107. /* If there are new blocks in the old last AG, extend it. */
  108. if (new) {
  109. error = xfs_ag_extend_space(mp, tp, &id, new);
  110. if (error)
  111. goto out_trans_cancel;
  112. }
  113. /*
  114. * Update changed superblock fields transactionally. These are not
  115. * seen by the rest of the world until the transaction commit applies
  116. * them atomically to the superblock.
  117. */
  118. if (nagcount > oagcount)
  119. xfs_trans_mod_sb(tp, XFS_TRANS_SB_AGCOUNT, nagcount - oagcount);
  120. if (nb > mp->m_sb.sb_dblocks)
  121. xfs_trans_mod_sb(tp, XFS_TRANS_SB_DBLOCKS,
  122. nb - mp->m_sb.sb_dblocks);
  123. if (id.nfree)
  124. xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, id.nfree);
  125. xfs_trans_set_sync(tp);
  126. error = xfs_trans_commit(tp);
  127. if (error)
  128. return error;
  129. /* New allocation groups fully initialized, so update mount struct */
  130. if (nagimax)
  131. mp->m_maxagi = nagimax;
  132. xfs_set_low_space_thresholds(mp);
  133. mp->m_alloc_set_aside = xfs_alloc_set_aside(mp);
  134. /*
  135. * If we expanded the last AG, free the per-AG reservation
  136. * so we can reinitialize it with the new size.
  137. */
  138. if (new) {
  139. struct xfs_perag *pag;
  140. pag = xfs_perag_get(mp, id.agno);
  141. error = xfs_ag_resv_free(pag);
  142. xfs_perag_put(pag);
  143. if (error)
  144. return error;
  145. }
  146. /*
  147. * Reserve AG metadata blocks. ENOSPC here does not mean there was a
  148. * growfs failure, just that there still isn't space for new user data
  149. * after the grow has been run.
  150. */
  151. error = xfs_fs_reserve_ag_blocks(mp);
  152. if (error == -ENOSPC)
  153. error = 0;
  154. return error;
  155. out_trans_cancel:
  156. xfs_trans_cancel(tp);
  157. return error;
  158. }
  159. static int
  160. xfs_growfs_log_private(
  161. xfs_mount_t *mp, /* mount point for filesystem */
  162. xfs_growfs_log_t *in) /* growfs log input struct */
  163. {
  164. xfs_extlen_t nb;
  165. nb = in->newblocks;
  166. if (nb < XFS_MIN_LOG_BLOCKS || nb < XFS_B_TO_FSB(mp, XFS_MIN_LOG_BYTES))
  167. return -EINVAL;
  168. if (nb == mp->m_sb.sb_logblocks &&
  169. in->isint == (mp->m_sb.sb_logstart != 0))
  170. return -EINVAL;
  171. /*
  172. * Moving the log is hard, need new interfaces to sync
  173. * the log first, hold off all activity while moving it.
  174. * Can have shorter or longer log in the same space,
  175. * or transform internal to external log or vice versa.
  176. */
  177. return -ENOSYS;
  178. }
  179. static int
  180. xfs_growfs_imaxpct(
  181. struct xfs_mount *mp,
  182. __u32 imaxpct)
  183. {
  184. struct xfs_trans *tp;
  185. int dpct;
  186. int error;
  187. if (imaxpct > 100)
  188. return -EINVAL;
  189. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growdata,
  190. XFS_GROWFS_SPACE_RES(mp), 0, XFS_TRANS_RESERVE, &tp);
  191. if (error)
  192. return error;
  193. dpct = imaxpct - mp->m_sb.sb_imax_pct;
  194. xfs_trans_mod_sb(tp, XFS_TRANS_SB_IMAXPCT, dpct);
  195. xfs_trans_set_sync(tp);
  196. return xfs_trans_commit(tp);
  197. }
  198. /*
  199. * protected versions of growfs function acquire and release locks on the mount
  200. * point - exported through ioctls: XFS_IOC_FSGROWFSDATA, XFS_IOC_FSGROWFSLOG,
  201. * XFS_IOC_FSGROWFSRT
  202. */
  203. int
  204. xfs_growfs_data(
  205. struct xfs_mount *mp,
  206. struct xfs_growfs_data *in)
  207. {
  208. int error = 0;
  209. if (!capable(CAP_SYS_ADMIN))
  210. return -EPERM;
  211. if (!mutex_trylock(&mp->m_growlock))
  212. return -EWOULDBLOCK;
  213. /* update imaxpct separately to the physical grow of the filesystem */
  214. if (in->imaxpct != mp->m_sb.sb_imax_pct) {
  215. error = xfs_growfs_imaxpct(mp, in->imaxpct);
  216. if (error)
  217. goto out_error;
  218. }
  219. if (in->newblocks != mp->m_sb.sb_dblocks) {
  220. error = xfs_growfs_data_private(mp, in);
  221. if (error)
  222. goto out_error;
  223. }
  224. /* Post growfs calculations needed to reflect new state in operations */
  225. if (mp->m_sb.sb_imax_pct) {
  226. uint64_t icount = mp->m_sb.sb_dblocks * mp->m_sb.sb_imax_pct;
  227. do_div(icount, 100);
  228. mp->m_maxicount = icount << mp->m_sb.sb_inopblog;
  229. } else
  230. mp->m_maxicount = 0;
  231. /* Update secondary superblocks now the physical grow has completed */
  232. error = xfs_update_secondary_sbs(mp);
  233. out_error:
  234. /*
  235. * Increment the generation unconditionally, the error could be from
  236. * updating the secondary superblocks, in which case the new size
  237. * is live already.
  238. */
  239. mp->m_generation++;
  240. mutex_unlock(&mp->m_growlock);
  241. return error;
  242. }
  243. int
  244. xfs_growfs_log(
  245. xfs_mount_t *mp,
  246. xfs_growfs_log_t *in)
  247. {
  248. int error;
  249. if (!capable(CAP_SYS_ADMIN))
  250. return -EPERM;
  251. if (!mutex_trylock(&mp->m_growlock))
  252. return -EWOULDBLOCK;
  253. error = xfs_growfs_log_private(mp, in);
  254. mutex_unlock(&mp->m_growlock);
  255. return error;
  256. }
  257. /*
  258. * exported through ioctl XFS_IOC_FSCOUNTS
  259. */
  260. int
  261. xfs_fs_counts(
  262. xfs_mount_t *mp,
  263. xfs_fsop_counts_t *cnt)
  264. {
  265. cnt->allocino = percpu_counter_read_positive(&mp->m_icount);
  266. cnt->freeino = percpu_counter_read_positive(&mp->m_ifree);
  267. cnt->freedata = percpu_counter_read_positive(&mp->m_fdblocks) -
  268. mp->m_alloc_set_aside;
  269. spin_lock(&mp->m_sb_lock);
  270. cnt->freertx = mp->m_sb.sb_frextents;
  271. spin_unlock(&mp->m_sb_lock);
  272. return 0;
  273. }
  274. /*
  275. * exported through ioctl XFS_IOC_SET_RESBLKS & XFS_IOC_GET_RESBLKS
  276. *
  277. * xfs_reserve_blocks is called to set m_resblks
  278. * in the in-core mount table. The number of unused reserved blocks
  279. * is kept in m_resblks_avail.
  280. *
  281. * Reserve the requested number of blocks if available. Otherwise return
  282. * as many as possible to satisfy the request. The actual number
  283. * reserved are returned in outval
  284. *
  285. * A null inval pointer indicates that only the current reserved blocks
  286. * available should be returned no settings are changed.
  287. */
  288. int
  289. xfs_reserve_blocks(
  290. xfs_mount_t *mp,
  291. uint64_t *inval,
  292. xfs_fsop_resblks_t *outval)
  293. {
  294. int64_t lcounter, delta;
  295. int64_t fdblks_delta = 0;
  296. uint64_t request;
  297. int64_t free;
  298. int error = 0;
  299. /* If inval is null, report current values and return */
  300. if (inval == (uint64_t *)NULL) {
  301. if (!outval)
  302. return -EINVAL;
  303. outval->resblks = mp->m_resblks;
  304. outval->resblks_avail = mp->m_resblks_avail;
  305. return 0;
  306. }
  307. request = *inval;
  308. /*
  309. * With per-cpu counters, this becomes an interesting problem. we need
  310. * to work out if we are freeing or allocation blocks first, then we can
  311. * do the modification as necessary.
  312. *
  313. * We do this under the m_sb_lock so that if we are near ENOSPC, we will
  314. * hold out any changes while we work out what to do. This means that
  315. * the amount of free space can change while we do this, so we need to
  316. * retry if we end up trying to reserve more space than is available.
  317. */
  318. spin_lock(&mp->m_sb_lock);
  319. /*
  320. * If our previous reservation was larger than the current value,
  321. * then move any unused blocks back to the free pool. Modify the resblks
  322. * counters directly since we shouldn't have any problems unreserving
  323. * space.
  324. */
  325. if (mp->m_resblks > request) {
  326. lcounter = mp->m_resblks_avail - request;
  327. if (lcounter > 0) { /* release unused blocks */
  328. fdblks_delta = lcounter;
  329. mp->m_resblks_avail -= lcounter;
  330. }
  331. mp->m_resblks = request;
  332. if (fdblks_delta) {
  333. spin_unlock(&mp->m_sb_lock);
  334. error = xfs_mod_fdblocks(mp, fdblks_delta, 0);
  335. spin_lock(&mp->m_sb_lock);
  336. }
  337. goto out;
  338. }
  339. /*
  340. * If the request is larger than the current reservation, reserve the
  341. * blocks before we update the reserve counters. Sample m_fdblocks and
  342. * perform a partial reservation if the request exceeds free space.
  343. */
  344. error = -ENOSPC;
  345. do {
  346. free = percpu_counter_sum(&mp->m_fdblocks) -
  347. mp->m_alloc_set_aside;
  348. if (free <= 0)
  349. break;
  350. delta = request - mp->m_resblks;
  351. lcounter = free - delta;
  352. if (lcounter < 0)
  353. /* We can't satisfy the request, just get what we can */
  354. fdblks_delta = free;
  355. else
  356. fdblks_delta = delta;
  357. /*
  358. * We'll either succeed in getting space from the free block
  359. * count or we'll get an ENOSPC. If we get a ENOSPC, it means
  360. * things changed while we were calculating fdblks_delta and so
  361. * we should try again to see if there is anything left to
  362. * reserve.
  363. *
  364. * Don't set the reserved flag here - we don't want to reserve
  365. * the extra reserve blocks from the reserve.....
  366. */
  367. spin_unlock(&mp->m_sb_lock);
  368. error = xfs_mod_fdblocks(mp, -fdblks_delta, 0);
  369. spin_lock(&mp->m_sb_lock);
  370. } while (error == -ENOSPC);
  371. /*
  372. * Update the reserve counters if blocks have been successfully
  373. * allocated.
  374. */
  375. if (!error && fdblks_delta) {
  376. mp->m_resblks += fdblks_delta;
  377. mp->m_resblks_avail += fdblks_delta;
  378. }
  379. out:
  380. if (outval) {
  381. outval->resblks = mp->m_resblks;
  382. outval->resblks_avail = mp->m_resblks_avail;
  383. }
  384. spin_unlock(&mp->m_sb_lock);
  385. return error;
  386. }
  387. int
  388. xfs_fs_goingdown(
  389. xfs_mount_t *mp,
  390. uint32_t inflags)
  391. {
  392. switch (inflags) {
  393. case XFS_FSOP_GOING_FLAGS_DEFAULT: {
  394. struct super_block *sb = freeze_bdev(mp->m_super->s_bdev);
  395. if (sb && !IS_ERR(sb)) {
  396. xfs_force_shutdown(mp, SHUTDOWN_FORCE_UMOUNT);
  397. thaw_bdev(sb->s_bdev, sb);
  398. }
  399. break;
  400. }
  401. case XFS_FSOP_GOING_FLAGS_LOGFLUSH:
  402. xfs_force_shutdown(mp, SHUTDOWN_FORCE_UMOUNT);
  403. break;
  404. case XFS_FSOP_GOING_FLAGS_NOLOGFLUSH:
  405. xfs_force_shutdown(mp,
  406. SHUTDOWN_FORCE_UMOUNT | SHUTDOWN_LOG_IO_ERROR);
  407. break;
  408. default:
  409. return -EINVAL;
  410. }
  411. return 0;
  412. }
  413. /*
  414. * Force a shutdown of the filesystem instantly while keeping the filesystem
  415. * consistent. We don't do an unmount here; just shutdown the shop, make sure
  416. * that absolutely nothing persistent happens to this filesystem after this
  417. * point.
  418. */
  419. void
  420. xfs_do_force_shutdown(
  421. xfs_mount_t *mp,
  422. int flags,
  423. char *fname,
  424. int lnnum)
  425. {
  426. int logerror;
  427. logerror = flags & SHUTDOWN_LOG_IO_ERROR;
  428. if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
  429. xfs_notice(mp,
  430. "%s(0x%x) called from line %d of file %s. Return address = "PTR_FMT,
  431. __func__, flags, lnnum, fname, __return_address);
  432. }
  433. /*
  434. * No need to duplicate efforts.
  435. */
  436. if (XFS_FORCED_SHUTDOWN(mp) && !logerror)
  437. return;
  438. /*
  439. * This flags XFS_MOUNT_FS_SHUTDOWN, makes sure that we don't
  440. * queue up anybody new on the log reservations, and wakes up
  441. * everybody who's sleeping on log reservations to tell them
  442. * the bad news.
  443. */
  444. if (xfs_log_force_umount(mp, logerror))
  445. return;
  446. if (flags & SHUTDOWN_CORRUPT_INCORE) {
  447. xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_CORRUPT,
  448. "Corruption of in-memory data detected. Shutting down filesystem");
  449. if (XFS_ERRLEVEL_HIGH <= xfs_error_level)
  450. xfs_stack_trace();
  451. } else if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
  452. if (logerror) {
  453. xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_LOGERROR,
  454. "Log I/O Error Detected. Shutting down filesystem");
  455. } else if (flags & SHUTDOWN_DEVICE_REQ) {
  456. xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_IOERROR,
  457. "All device paths lost. Shutting down filesystem");
  458. } else if (!(flags & SHUTDOWN_REMOTE_REQ)) {
  459. xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_IOERROR,
  460. "I/O Error Detected. Shutting down filesystem");
  461. }
  462. }
  463. if (!(flags & SHUTDOWN_FORCE_UMOUNT)) {
  464. xfs_alert(mp,
  465. "Please umount the filesystem and rectify the problem(s)");
  466. }
  467. }
  468. /*
  469. * Reserve free space for per-AG metadata.
  470. */
  471. int
  472. xfs_fs_reserve_ag_blocks(
  473. struct xfs_mount *mp)
  474. {
  475. xfs_agnumber_t agno;
  476. struct xfs_perag *pag;
  477. int error = 0;
  478. int err2;
  479. mp->m_finobt_nores = false;
  480. for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
  481. pag = xfs_perag_get(mp, agno);
  482. err2 = xfs_ag_resv_init(pag, NULL);
  483. xfs_perag_put(pag);
  484. if (err2 && !error)
  485. error = err2;
  486. }
  487. if (error && error != -ENOSPC) {
  488. xfs_warn(mp,
  489. "Error %d reserving per-AG metadata reserve pool.", error);
  490. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  491. }
  492. return error;
  493. }
  494. /*
  495. * Free space reserved for per-AG metadata.
  496. */
  497. int
  498. xfs_fs_unreserve_ag_blocks(
  499. struct xfs_mount *mp)
  500. {
  501. xfs_agnumber_t agno;
  502. struct xfs_perag *pag;
  503. int error = 0;
  504. int err2;
  505. for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
  506. pag = xfs_perag_get(mp, agno);
  507. err2 = xfs_ag_resv_free(pag);
  508. xfs_perag_put(pag);
  509. if (err2 && !error)
  510. error = err2;
  511. }
  512. if (error)
  513. xfs_warn(mp,
  514. "Error %d freeing per-AG metadata reserve pool.", error);
  515. return error;
  516. }