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