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