1 /* 2 * Copyright (c) 2000-2003,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_types.h" 21 #include "xfs_bit.h" 22 #include "xfs_log.h" 23 #include "xfs_inum.h" 24 #include "xfs_trans.h" 25 #include "xfs_sb.h" 26 #include "xfs_ag.h" 27 #include "xfs_dir.h" 28 #include "xfs_dir2.h" 29 #include "xfs_dmapi.h" 30 #include "xfs_mount.h" 31 #include "xfs_error.h" 32 #include "xfs_da_btree.h" 33 #include "xfs_bmap_btree.h" 34 #include "xfs_alloc_btree.h" 35 #include "xfs_ialloc_btree.h" 36 #include "xfs_dir_sf.h" 37 #include "xfs_dir2_sf.h" 38 #include "xfs_attr_sf.h" 39 #include "xfs_dinode.h" 40 #include "xfs_inode.h" 41 #include "xfs_btree.h" 42 #include "xfs_ialloc.h" 43 #include "xfs_alloc.h" 44 #include "xfs_bmap.h" 45 #include "xfs_quota.h" 46 #include "xfs_trans_priv.h" 47 #include "xfs_trans_space.h" 48 49 50 STATIC void xfs_trans_apply_sb_deltas(xfs_trans_t *); 51 STATIC uint xfs_trans_count_vecs(xfs_trans_t *); 52 STATIC void xfs_trans_fill_vecs(xfs_trans_t *, xfs_log_iovec_t *); 53 STATIC void xfs_trans_uncommit(xfs_trans_t *, uint); 54 STATIC void xfs_trans_committed(xfs_trans_t *, int); 55 STATIC void xfs_trans_chunk_committed(xfs_log_item_chunk_t *, xfs_lsn_t, int); 56 STATIC void xfs_trans_free(xfs_trans_t *); 57 58 kmem_zone_t *xfs_trans_zone; 59 60 61 /* 62 * Initialize the precomputed transaction reservation values 63 * in the mount structure. 64 */ 65 void 66 xfs_trans_init( 67 xfs_mount_t *mp) 68 { 69 xfs_trans_reservations_t *resp; 70 71 resp = &(mp->m_reservations); 72 resp->tr_write = 73 (uint)(XFS_CALC_WRITE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp)); 74 resp->tr_itruncate = 75 (uint)(XFS_CALC_ITRUNCATE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp)); 76 resp->tr_rename = 77 (uint)(XFS_CALC_RENAME_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp)); 78 resp->tr_link = (uint)XFS_CALC_LINK_LOG_RES(mp); 79 resp->tr_remove = 80 (uint)(XFS_CALC_REMOVE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp)); 81 resp->tr_symlink = 82 (uint)(XFS_CALC_SYMLINK_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp)); 83 resp->tr_create = 84 (uint)(XFS_CALC_CREATE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp)); 85 resp->tr_mkdir = 86 (uint)(XFS_CALC_MKDIR_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp)); 87 resp->tr_ifree = 88 (uint)(XFS_CALC_IFREE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp)); 89 resp->tr_ichange = 90 (uint)(XFS_CALC_ICHANGE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp)); 91 resp->tr_growdata = (uint)XFS_CALC_GROWDATA_LOG_RES(mp); 92 resp->tr_swrite = (uint)XFS_CALC_SWRITE_LOG_RES(mp); 93 resp->tr_writeid = (uint)XFS_CALC_WRITEID_LOG_RES(mp); 94 resp->tr_addafork = 95 (uint)(XFS_CALC_ADDAFORK_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp)); 96 resp->tr_attrinval = (uint)XFS_CALC_ATTRINVAL_LOG_RES(mp); 97 resp->tr_attrset = 98 (uint)(XFS_CALC_ATTRSET_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp)); 99 resp->tr_attrrm = 100 (uint)(XFS_CALC_ATTRRM_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp)); 101 resp->tr_clearagi = (uint)XFS_CALC_CLEAR_AGI_BUCKET_LOG_RES(mp); 102 resp->tr_growrtalloc = (uint)XFS_CALC_GROWRTALLOC_LOG_RES(mp); 103 resp->tr_growrtzero = (uint)XFS_CALC_GROWRTZERO_LOG_RES(mp); 104 resp->tr_growrtfree = (uint)XFS_CALC_GROWRTFREE_LOG_RES(mp); 105 } 106 107 /* 108 * This routine is called to allocate a transaction structure. 109 * The type parameter indicates the type of the transaction. These 110 * are enumerated in xfs_trans.h. 111 * 112 * Dynamically allocate the transaction structure from the transaction 113 * zone, initialize it, and return it to the caller. 114 */ 115 xfs_trans_t * 116 xfs_trans_alloc( 117 xfs_mount_t *mp, 118 uint type) 119 { 120 fs_check_frozen(XFS_MTOVFS(mp), SB_FREEZE_TRANS); 121 atomic_inc(&mp->m_active_trans); 122 123 return (_xfs_trans_alloc(mp, type)); 124 125 } 126 127 xfs_trans_t * 128 _xfs_trans_alloc( 129 xfs_mount_t *mp, 130 uint type) 131 { 132 xfs_trans_t *tp; 133 134 ASSERT(xfs_trans_zone != NULL); 135 tp = kmem_zone_zalloc(xfs_trans_zone, KM_SLEEP); 136 137 /* 138 * Initialize the transaction structure. 139 */ 140 tp->t_magic = XFS_TRANS_MAGIC; 141 tp->t_type = type; 142 tp->t_mountp = mp; 143 tp->t_items_free = XFS_LIC_NUM_SLOTS; 144 tp->t_busy_free = XFS_LBC_NUM_SLOTS; 145 XFS_LIC_INIT(&(tp->t_items)); 146 XFS_LBC_INIT(&(tp->t_busy)); 147 148 return (tp); 149 } 150 151 /* 152 * This is called to create a new transaction which will share the 153 * permanent log reservation of the given transaction. The remaining 154 * unused block and rt extent reservations are also inherited. This 155 * implies that the original transaction is no longer allowed to allocate 156 * blocks. Locks and log items, however, are no inherited. They must 157 * be added to the new transaction explicitly. 158 */ 159 xfs_trans_t * 160 xfs_trans_dup( 161 xfs_trans_t *tp) 162 { 163 xfs_trans_t *ntp; 164 165 ntp = kmem_zone_zalloc(xfs_trans_zone, KM_SLEEP); 166 167 /* 168 * Initialize the new transaction structure. 169 */ 170 ntp->t_magic = XFS_TRANS_MAGIC; 171 ntp->t_type = tp->t_type; 172 ntp->t_mountp = tp->t_mountp; 173 ntp->t_items_free = XFS_LIC_NUM_SLOTS; 174 ntp->t_busy_free = XFS_LBC_NUM_SLOTS; 175 XFS_LIC_INIT(&(ntp->t_items)); 176 XFS_LBC_INIT(&(ntp->t_busy)); 177 178 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES); 179 ASSERT(tp->t_ticket != NULL); 180 181 ntp->t_flags = XFS_TRANS_PERM_LOG_RES | (tp->t_flags & XFS_TRANS_RESERVE); 182 ntp->t_ticket = tp->t_ticket; 183 ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used; 184 tp->t_blk_res = tp->t_blk_res_used; 185 ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used; 186 tp->t_rtx_res = tp->t_rtx_res_used; 187 PFLAGS_DUP(&tp->t_pflags, &ntp->t_pflags); 188 189 XFS_TRANS_DUP_DQINFO(tp->t_mountp, tp, ntp); 190 191 atomic_inc(&tp->t_mountp->m_active_trans); 192 return ntp; 193 } 194 195 /* 196 * This is called to reserve free disk blocks and log space for the 197 * given transaction. This must be done before allocating any resources 198 * within the transaction. 199 * 200 * This will return ENOSPC if there are not enough blocks available. 201 * It will sleep waiting for available log space. 202 * The only valid value for the flags parameter is XFS_RES_LOG_PERM, which 203 * is used by long running transactions. If any one of the reservations 204 * fails then they will all be backed out. 205 * 206 * This does not do quota reservations. That typically is done by the 207 * caller afterwards. 208 */ 209 int 210 xfs_trans_reserve( 211 xfs_trans_t *tp, 212 uint blocks, 213 uint logspace, 214 uint rtextents, 215 uint flags, 216 uint logcount) 217 { 218 int log_flags; 219 int error; 220 int rsvd; 221 222 error = 0; 223 rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0; 224 225 /* Mark this thread as being in a transaction */ 226 PFLAGS_SET_FSTRANS(&tp->t_pflags); 227 228 /* 229 * Attempt to reserve the needed disk blocks by decrementing 230 * the number needed from the number available. This will 231 * fail if the count would go below zero. 232 */ 233 if (blocks > 0) { 234 error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FDBLOCKS, 235 -blocks, rsvd); 236 if (error != 0) { 237 PFLAGS_RESTORE_FSTRANS(&tp->t_pflags); 238 return (XFS_ERROR(ENOSPC)); 239 } 240 tp->t_blk_res += blocks; 241 } 242 243 /* 244 * Reserve the log space needed for this transaction. 245 */ 246 if (logspace > 0) { 247 ASSERT((tp->t_log_res == 0) || (tp->t_log_res == logspace)); 248 ASSERT((tp->t_log_count == 0) || 249 (tp->t_log_count == logcount)); 250 if (flags & XFS_TRANS_PERM_LOG_RES) { 251 log_flags = XFS_LOG_PERM_RESERV; 252 tp->t_flags |= XFS_TRANS_PERM_LOG_RES; 253 } else { 254 ASSERT(tp->t_ticket == NULL); 255 ASSERT(!(tp->t_flags & XFS_TRANS_PERM_LOG_RES)); 256 log_flags = 0; 257 } 258 259 error = xfs_log_reserve(tp->t_mountp, logspace, logcount, 260 &tp->t_ticket, 261 XFS_TRANSACTION, log_flags, tp->t_type); 262 if (error) { 263 goto undo_blocks; 264 } 265 tp->t_log_res = logspace; 266 tp->t_log_count = logcount; 267 } 268 269 /* 270 * Attempt to reserve the needed realtime extents by decrementing 271 * the number needed from the number available. This will 272 * fail if the count would go below zero. 273 */ 274 if (rtextents > 0) { 275 error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FREXTENTS, 276 -rtextents, rsvd); 277 if (error) { 278 error = XFS_ERROR(ENOSPC); 279 goto undo_log; 280 } 281 tp->t_rtx_res += rtextents; 282 } 283 284 return 0; 285 286 /* 287 * Error cases jump to one of these labels to undo any 288 * reservations which have already been performed. 289 */ 290 undo_log: 291 if (logspace > 0) { 292 if (flags & XFS_TRANS_PERM_LOG_RES) { 293 log_flags = XFS_LOG_REL_PERM_RESERV; 294 } else { 295 log_flags = 0; 296 } 297 xfs_log_done(tp->t_mountp, tp->t_ticket, NULL, log_flags); 298 tp->t_ticket = NULL; 299 tp->t_log_res = 0; 300 tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES; 301 } 302 303 undo_blocks: 304 if (blocks > 0) { 305 (void) xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FDBLOCKS, 306 blocks, rsvd); 307 tp->t_blk_res = 0; 308 } 309 310 PFLAGS_RESTORE_FSTRANS(&tp->t_pflags); 311 312 return (error); 313 } 314 315 316 /* 317 * Record the indicated change to the given field for application 318 * to the file system's superblock when the transaction commits. 319 * For now, just store the change in the transaction structure. 320 * 321 * Mark the transaction structure to indicate that the superblock 322 * needs to be updated before committing. 323 */ 324 void 325 xfs_trans_mod_sb( 326 xfs_trans_t *tp, 327 uint field, 328 long delta) 329 { 330 331 switch (field) { 332 case XFS_TRANS_SB_ICOUNT: 333 tp->t_icount_delta += delta; 334 break; 335 case XFS_TRANS_SB_IFREE: 336 tp->t_ifree_delta += delta; 337 break; 338 case XFS_TRANS_SB_FDBLOCKS: 339 /* 340 * Track the number of blocks allocated in the 341 * transaction. Make sure it does not exceed the 342 * number reserved. 343 */ 344 if (delta < 0) { 345 tp->t_blk_res_used += (uint)-delta; 346 ASSERT(tp->t_blk_res_used <= tp->t_blk_res); 347 } 348 tp->t_fdblocks_delta += delta; 349 break; 350 case XFS_TRANS_SB_RES_FDBLOCKS: 351 /* 352 * The allocation has already been applied to the 353 * in-core superblock's counter. This should only 354 * be applied to the on-disk superblock. 355 */ 356 ASSERT(delta < 0); 357 tp->t_res_fdblocks_delta += delta; 358 break; 359 case XFS_TRANS_SB_FREXTENTS: 360 /* 361 * Track the number of blocks allocated in the 362 * transaction. Make sure it does not exceed the 363 * number reserved. 364 */ 365 if (delta < 0) { 366 tp->t_rtx_res_used += (uint)-delta; 367 ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res); 368 } 369 tp->t_frextents_delta += delta; 370 break; 371 case XFS_TRANS_SB_RES_FREXTENTS: 372 /* 373 * The allocation has already been applied to the 374 * in-core superblocks's counter. This should only 375 * be applied to the on-disk superblock. 376 */ 377 ASSERT(delta < 0); 378 tp->t_res_frextents_delta += delta; 379 break; 380 case XFS_TRANS_SB_DBLOCKS: 381 ASSERT(delta > 0); 382 tp->t_dblocks_delta += delta; 383 break; 384 case XFS_TRANS_SB_AGCOUNT: 385 ASSERT(delta > 0); 386 tp->t_agcount_delta += delta; 387 break; 388 case XFS_TRANS_SB_IMAXPCT: 389 tp->t_imaxpct_delta += delta; 390 break; 391 case XFS_TRANS_SB_REXTSIZE: 392 tp->t_rextsize_delta += delta; 393 break; 394 case XFS_TRANS_SB_RBMBLOCKS: 395 tp->t_rbmblocks_delta += delta; 396 break; 397 case XFS_TRANS_SB_RBLOCKS: 398 tp->t_rblocks_delta += delta; 399 break; 400 case XFS_TRANS_SB_REXTENTS: 401 tp->t_rextents_delta += delta; 402 break; 403 case XFS_TRANS_SB_REXTSLOG: 404 tp->t_rextslog_delta += delta; 405 break; 406 default: 407 ASSERT(0); 408 return; 409 } 410 411 tp->t_flags |= (XFS_TRANS_SB_DIRTY | XFS_TRANS_DIRTY); 412 } 413 414 /* 415 * xfs_trans_apply_sb_deltas() is called from the commit code 416 * to bring the superblock buffer into the current transaction 417 * and modify it as requested by earlier calls to xfs_trans_mod_sb(). 418 * 419 * For now we just look at each field allowed to change and change 420 * it if necessary. 421 */ 422 STATIC void 423 xfs_trans_apply_sb_deltas( 424 xfs_trans_t *tp) 425 { 426 xfs_sb_t *sbp; 427 xfs_buf_t *bp; 428 int whole = 0; 429 430 bp = xfs_trans_getsb(tp, tp->t_mountp, 0); 431 sbp = XFS_BUF_TO_SBP(bp); 432 433 /* 434 * Check that superblock mods match the mods made to AGF counters. 435 */ 436 ASSERT((tp->t_fdblocks_delta + tp->t_res_fdblocks_delta) == 437 (tp->t_ag_freeblks_delta + tp->t_ag_flist_delta + 438 tp->t_ag_btree_delta)); 439 440 if (tp->t_icount_delta != 0) { 441 INT_MOD(sbp->sb_icount, ARCH_CONVERT, tp->t_icount_delta); 442 } 443 if (tp->t_ifree_delta != 0) { 444 INT_MOD(sbp->sb_ifree, ARCH_CONVERT, tp->t_ifree_delta); 445 } 446 447 if (tp->t_fdblocks_delta != 0) { 448 INT_MOD(sbp->sb_fdblocks, ARCH_CONVERT, tp->t_fdblocks_delta); 449 } 450 if (tp->t_res_fdblocks_delta != 0) { 451 INT_MOD(sbp->sb_fdblocks, ARCH_CONVERT, tp->t_res_fdblocks_delta); 452 } 453 454 if (tp->t_frextents_delta != 0) { 455 INT_MOD(sbp->sb_frextents, ARCH_CONVERT, tp->t_frextents_delta); 456 } 457 if (tp->t_res_frextents_delta != 0) { 458 INT_MOD(sbp->sb_frextents, ARCH_CONVERT, tp->t_res_frextents_delta); 459 } 460 if (tp->t_dblocks_delta != 0) { 461 INT_MOD(sbp->sb_dblocks, ARCH_CONVERT, tp->t_dblocks_delta); 462 whole = 1; 463 } 464 if (tp->t_agcount_delta != 0) { 465 INT_MOD(sbp->sb_agcount, ARCH_CONVERT, tp->t_agcount_delta); 466 whole = 1; 467 } 468 if (tp->t_imaxpct_delta != 0) { 469 INT_MOD(sbp->sb_imax_pct, ARCH_CONVERT, tp->t_imaxpct_delta); 470 whole = 1; 471 } 472 if (tp->t_rextsize_delta != 0) { 473 INT_MOD(sbp->sb_rextsize, ARCH_CONVERT, tp->t_rextsize_delta); 474 whole = 1; 475 } 476 if (tp->t_rbmblocks_delta != 0) { 477 INT_MOD(sbp->sb_rbmblocks, ARCH_CONVERT, tp->t_rbmblocks_delta); 478 whole = 1; 479 } 480 if (tp->t_rblocks_delta != 0) { 481 INT_MOD(sbp->sb_rblocks, ARCH_CONVERT, tp->t_rblocks_delta); 482 whole = 1; 483 } 484 if (tp->t_rextents_delta != 0) { 485 INT_MOD(sbp->sb_rextents, ARCH_CONVERT, tp->t_rextents_delta); 486 whole = 1; 487 } 488 if (tp->t_rextslog_delta != 0) { 489 INT_MOD(sbp->sb_rextslog, ARCH_CONVERT, tp->t_rextslog_delta); 490 whole = 1; 491 } 492 493 if (whole) 494 /* 495 * Log the whole thing, the fields are discontiguous. 496 */ 497 xfs_trans_log_buf(tp, bp, 0, sizeof(xfs_sb_t) - 1); 498 else 499 /* 500 * Since all the modifiable fields are contiguous, we 501 * can get away with this. 502 */ 503 xfs_trans_log_buf(tp, bp, offsetof(xfs_sb_t, sb_icount), 504 offsetof(xfs_sb_t, sb_frextents) + 505 sizeof(sbp->sb_frextents) - 1); 506 507 XFS_MTOVFS(tp->t_mountp)->vfs_super->s_dirt = 1; 508 } 509 510 /* 511 * xfs_trans_unreserve_and_mod_sb() is called to release unused 512 * reservations and apply superblock counter changes to the in-core 513 * superblock. 514 * 515 * This is done efficiently with a single call to xfs_mod_incore_sb_batch(). 516 */ 517 STATIC void 518 xfs_trans_unreserve_and_mod_sb( 519 xfs_trans_t *tp) 520 { 521 xfs_mod_sb_t msb[14]; /* If you add cases, add entries */ 522 xfs_mod_sb_t *msbp; 523 /* REFERENCED */ 524 int error; 525 int rsvd; 526 527 msbp = msb; 528 rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0; 529 530 /* 531 * Release any reserved blocks. Any that were allocated 532 * will be taken back again by fdblocks_delta below. 533 */ 534 if (tp->t_blk_res > 0) { 535 msbp->msb_field = XFS_SBS_FDBLOCKS; 536 msbp->msb_delta = tp->t_blk_res; 537 msbp++; 538 } 539 540 /* 541 * Release any reserved real time extents . Any that were 542 * allocated will be taken back again by frextents_delta below. 543 */ 544 if (tp->t_rtx_res > 0) { 545 msbp->msb_field = XFS_SBS_FREXTENTS; 546 msbp->msb_delta = tp->t_rtx_res; 547 msbp++; 548 } 549 550 /* 551 * Apply any superblock modifications to the in-core version. 552 * The t_res_fdblocks_delta and t_res_frextents_delta fields are 553 * explicity NOT applied to the in-core superblock. 554 * The idea is that that has already been done. 555 */ 556 if (tp->t_flags & XFS_TRANS_SB_DIRTY) { 557 if (tp->t_icount_delta != 0) { 558 msbp->msb_field = XFS_SBS_ICOUNT; 559 msbp->msb_delta = (int)tp->t_icount_delta; 560 msbp++; 561 } 562 if (tp->t_ifree_delta != 0) { 563 msbp->msb_field = XFS_SBS_IFREE; 564 msbp->msb_delta = (int)tp->t_ifree_delta; 565 msbp++; 566 } 567 if (tp->t_fdblocks_delta != 0) { 568 msbp->msb_field = XFS_SBS_FDBLOCKS; 569 msbp->msb_delta = (int)tp->t_fdblocks_delta; 570 msbp++; 571 } 572 if (tp->t_frextents_delta != 0) { 573 msbp->msb_field = XFS_SBS_FREXTENTS; 574 msbp->msb_delta = (int)tp->t_frextents_delta; 575 msbp++; 576 } 577 if (tp->t_dblocks_delta != 0) { 578 msbp->msb_field = XFS_SBS_DBLOCKS; 579 msbp->msb_delta = (int)tp->t_dblocks_delta; 580 msbp++; 581 } 582 if (tp->t_agcount_delta != 0) { 583 msbp->msb_field = XFS_SBS_AGCOUNT; 584 msbp->msb_delta = (int)tp->t_agcount_delta; 585 msbp++; 586 } 587 if (tp->t_imaxpct_delta != 0) { 588 msbp->msb_field = XFS_SBS_IMAX_PCT; 589 msbp->msb_delta = (int)tp->t_imaxpct_delta; 590 msbp++; 591 } 592 if (tp->t_rextsize_delta != 0) { 593 msbp->msb_field = XFS_SBS_REXTSIZE; 594 msbp->msb_delta = (int)tp->t_rextsize_delta; 595 msbp++; 596 } 597 if (tp->t_rbmblocks_delta != 0) { 598 msbp->msb_field = XFS_SBS_RBMBLOCKS; 599 msbp->msb_delta = (int)tp->t_rbmblocks_delta; 600 msbp++; 601 } 602 if (tp->t_rblocks_delta != 0) { 603 msbp->msb_field = XFS_SBS_RBLOCKS; 604 msbp->msb_delta = (int)tp->t_rblocks_delta; 605 msbp++; 606 } 607 if (tp->t_rextents_delta != 0) { 608 msbp->msb_field = XFS_SBS_REXTENTS; 609 msbp->msb_delta = (int)tp->t_rextents_delta; 610 msbp++; 611 } 612 if (tp->t_rextslog_delta != 0) { 613 msbp->msb_field = XFS_SBS_REXTSLOG; 614 msbp->msb_delta = (int)tp->t_rextslog_delta; 615 msbp++; 616 } 617 } 618 619 /* 620 * If we need to change anything, do it. 621 */ 622 if (msbp > msb) { 623 error = xfs_mod_incore_sb_batch(tp->t_mountp, msb, 624 (uint)(msbp - msb), rsvd); 625 ASSERT(error == 0); 626 } 627 } 628 629 630 /* 631 * xfs_trans_commit 632 * 633 * Commit the given transaction to the log a/synchronously. 634 * 635 * XFS disk error handling mechanism is not based on a typical 636 * transaction abort mechanism. Logically after the filesystem 637 * gets marked 'SHUTDOWN', we can't let any new transactions 638 * be durable - ie. committed to disk - because some metadata might 639 * be inconsistent. In such cases, this returns an error, and the 640 * caller may assume that all locked objects joined to the transaction 641 * have already been unlocked as if the commit had succeeded. 642 * Do not reference the transaction structure after this call. 643 */ 644 /*ARGSUSED*/ 645 int 646 _xfs_trans_commit( 647 xfs_trans_t *tp, 648 uint flags, 649 xfs_lsn_t *commit_lsn_p, 650 int *log_flushed) 651 { 652 xfs_log_iovec_t *log_vector; 653 int nvec; 654 xfs_mount_t *mp; 655 xfs_lsn_t commit_lsn; 656 /* REFERENCED */ 657 int error; 658 int log_flags; 659 int sync; 660 #define XFS_TRANS_LOGVEC_COUNT 16 661 xfs_log_iovec_t log_vector_fast[XFS_TRANS_LOGVEC_COUNT]; 662 void *commit_iclog; 663 int shutdown; 664 665 commit_lsn = -1; 666 667 /* 668 * Determine whether this commit is releasing a permanent 669 * log reservation or not. 670 */ 671 if (flags & XFS_TRANS_RELEASE_LOG_RES) { 672 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES); 673 log_flags = XFS_LOG_REL_PERM_RESERV; 674 } else { 675 log_flags = 0; 676 } 677 mp = tp->t_mountp; 678 679 /* 680 * If there is nothing to be logged by the transaction, 681 * then unlock all of the items associated with the 682 * transaction and free the transaction structure. 683 * Also make sure to return any reserved blocks to 684 * the free pool. 685 */ 686 shut_us_down: 687 shutdown = XFS_FORCED_SHUTDOWN(mp) ? EIO : 0; 688 if (!(tp->t_flags & XFS_TRANS_DIRTY) || shutdown) { 689 xfs_trans_unreserve_and_mod_sb(tp); 690 /* 691 * It is indeed possible for the transaction to be 692 * not dirty but the dqinfo portion to be. All that 693 * means is that we have some (non-persistent) quota 694 * reservations that need to be unreserved. 695 */ 696 XFS_TRANS_UNRESERVE_AND_MOD_DQUOTS(mp, tp); 697 if (tp->t_ticket) { 698 commit_lsn = xfs_log_done(mp, tp->t_ticket, 699 NULL, log_flags); 700 if (commit_lsn == -1 && !shutdown) 701 shutdown = XFS_ERROR(EIO); 702 } 703 PFLAGS_RESTORE_FSTRANS(&tp->t_pflags); 704 xfs_trans_free_items(tp, shutdown? XFS_TRANS_ABORT : 0); 705 xfs_trans_free_busy(tp); 706 xfs_trans_free(tp); 707 XFS_STATS_INC(xs_trans_empty); 708 if (commit_lsn_p) 709 *commit_lsn_p = commit_lsn; 710 return (shutdown); 711 } 712 ASSERT(tp->t_ticket != NULL); 713 714 /* 715 * If we need to update the superblock, then do it now. 716 */ 717 if (tp->t_flags & XFS_TRANS_SB_DIRTY) { 718 xfs_trans_apply_sb_deltas(tp); 719 } 720 XFS_TRANS_APPLY_DQUOT_DELTAS(mp, tp); 721 722 /* 723 * Ask each log item how many log_vector entries it will 724 * need so we can figure out how many to allocate. 725 * Try to avoid the kmem_alloc() call in the common case 726 * by using a vector from the stack when it fits. 727 */ 728 nvec = xfs_trans_count_vecs(tp); 729 if (nvec == 0) { 730 xfs_force_shutdown(mp, XFS_LOG_IO_ERROR); 731 goto shut_us_down; 732 } else if (nvec <= XFS_TRANS_LOGVEC_COUNT) { 733 log_vector = log_vector_fast; 734 } else { 735 log_vector = (xfs_log_iovec_t *)kmem_alloc(nvec * 736 sizeof(xfs_log_iovec_t), 737 KM_SLEEP); 738 } 739 740 /* 741 * Fill in the log_vector and pin the logged items, and 742 * then write the transaction to the log. 743 */ 744 xfs_trans_fill_vecs(tp, log_vector); 745 746 error = xfs_log_write(mp, log_vector, nvec, tp->t_ticket, &(tp->t_lsn)); 747 748 /* 749 * The transaction is committed incore here, and can go out to disk 750 * at any time after this call. However, all the items associated 751 * with the transaction are still locked and pinned in memory. 752 */ 753 commit_lsn = xfs_log_done(mp, tp->t_ticket, &commit_iclog, log_flags); 754 755 tp->t_commit_lsn = commit_lsn; 756 if (nvec > XFS_TRANS_LOGVEC_COUNT) { 757 kmem_free(log_vector, nvec * sizeof(xfs_log_iovec_t)); 758 } 759 760 if (commit_lsn_p) 761 *commit_lsn_p = commit_lsn; 762 763 /* 764 * If we got a log write error. Unpin the logitems that we 765 * had pinned, clean up, free trans structure, and return error. 766 */ 767 if (error || commit_lsn == -1) { 768 PFLAGS_RESTORE_FSTRANS(&tp->t_pflags); 769 xfs_trans_uncommit(tp, flags|XFS_TRANS_ABORT); 770 return XFS_ERROR(EIO); 771 } 772 773 /* 774 * Once the transaction has committed, unused 775 * reservations need to be released and changes to 776 * the superblock need to be reflected in the in-core 777 * version. Do that now. 778 */ 779 xfs_trans_unreserve_and_mod_sb(tp); 780 781 sync = tp->t_flags & XFS_TRANS_SYNC; 782 783 /* 784 * Tell the LM to call the transaction completion routine 785 * when the log write with LSN commit_lsn completes (e.g. 786 * when the transaction commit really hits the on-disk log). 787 * After this call we cannot reference tp, because the call 788 * can happen at any time and the call will free the transaction 789 * structure pointed to by tp. The only case where we call 790 * the completion routine (xfs_trans_committed) directly is 791 * if the log is turned off on a debug kernel or we're 792 * running in simulation mode (the log is explicitly turned 793 * off). 794 */ 795 tp->t_logcb.cb_func = (void(*)(void*, int))xfs_trans_committed; 796 tp->t_logcb.cb_arg = tp; 797 798 /* 799 * We need to pass the iclog buffer which was used for the 800 * transaction commit record into this function, and attach 801 * the callback to it. The callback must be attached before 802 * the items are unlocked to avoid racing with other threads 803 * waiting for an item to unlock. 804 */ 805 shutdown = xfs_log_notify(mp, commit_iclog, &(tp->t_logcb)); 806 807 /* 808 * Mark this thread as no longer being in a transaction 809 */ 810 PFLAGS_RESTORE_FSTRANS(&tp->t_pflags); 811 812 /* 813 * Once all the items of the transaction have been copied 814 * to the in core log and the callback is attached, the 815 * items can be unlocked. 816 * 817 * This will free descriptors pointing to items which were 818 * not logged since there is nothing more to do with them. 819 * For items which were logged, we will keep pointers to them 820 * so they can be unpinned after the transaction commits to disk. 821 * This will also stamp each modified meta-data item with 822 * the commit lsn of this transaction for dependency tracking 823 * purposes. 824 */ 825 xfs_trans_unlock_items(tp, commit_lsn); 826 827 /* 828 * If we detected a log error earlier, finish committing 829 * the transaction now (unpin log items, etc). 830 * 831 * Order is critical here, to avoid using the transaction 832 * pointer after its been freed (by xfs_trans_committed 833 * either here now, or as a callback). We cannot do this 834 * step inside xfs_log_notify as was done earlier because 835 * of this issue. 836 */ 837 if (shutdown) 838 xfs_trans_committed(tp, XFS_LI_ABORTED); 839 840 /* 841 * Now that the xfs_trans_committed callback has been attached, 842 * and the items are released we can finally allow the iclog to 843 * go to disk. 844 */ 845 error = xfs_log_release_iclog(mp, commit_iclog); 846 847 /* 848 * If the transaction needs to be synchronous, then force the 849 * log out now and wait for it. 850 */ 851 if (sync) { 852 if (!error) { 853 error = _xfs_log_force(mp, commit_lsn, 854 XFS_LOG_FORCE | XFS_LOG_SYNC, 855 log_flushed); 856 } 857 XFS_STATS_INC(xs_trans_sync); 858 } else { 859 XFS_STATS_INC(xs_trans_async); 860 } 861 862 return (error); 863 } 864 865 866 /* 867 * Total up the number of log iovecs needed to commit this 868 * transaction. The transaction itself needs one for the 869 * transaction header. Ask each dirty item in turn how many 870 * it needs to get the total. 871 */ 872 STATIC uint 873 xfs_trans_count_vecs( 874 xfs_trans_t *tp) 875 { 876 int nvecs; 877 xfs_log_item_desc_t *lidp; 878 879 nvecs = 1; 880 lidp = xfs_trans_first_item(tp); 881 ASSERT(lidp != NULL); 882 883 /* In the non-debug case we need to start bailing out if we 884 * didn't find a log_item here, return zero and let trans_commit 885 * deal with it. 886 */ 887 if (lidp == NULL) 888 return 0; 889 890 while (lidp != NULL) { 891 /* 892 * Skip items which aren't dirty in this transaction. 893 */ 894 if (!(lidp->lid_flags & XFS_LID_DIRTY)) { 895 lidp = xfs_trans_next_item(tp, lidp); 896 continue; 897 } 898 lidp->lid_size = IOP_SIZE(lidp->lid_item); 899 nvecs += lidp->lid_size; 900 lidp = xfs_trans_next_item(tp, lidp); 901 } 902 903 return nvecs; 904 } 905 906 /* 907 * Called from the trans_commit code when we notice that 908 * the filesystem is in the middle of a forced shutdown. 909 */ 910 STATIC void 911 xfs_trans_uncommit( 912 xfs_trans_t *tp, 913 uint flags) 914 { 915 xfs_log_item_desc_t *lidp; 916 917 for (lidp = xfs_trans_first_item(tp); 918 lidp != NULL; 919 lidp = xfs_trans_next_item(tp, lidp)) { 920 /* 921 * Unpin all but those that aren't dirty. 922 */ 923 if (lidp->lid_flags & XFS_LID_DIRTY) 924 IOP_UNPIN_REMOVE(lidp->lid_item, tp); 925 } 926 927 xfs_trans_unreserve_and_mod_sb(tp); 928 XFS_TRANS_UNRESERVE_AND_MOD_DQUOTS(tp->t_mountp, tp); 929 930 xfs_trans_free_items(tp, flags); 931 xfs_trans_free_busy(tp); 932 xfs_trans_free(tp); 933 } 934 935 /* 936 * Fill in the vector with pointers to data to be logged 937 * by this transaction. The transaction header takes 938 * the first vector, and then each dirty item takes the 939 * number of vectors it indicated it needed in xfs_trans_count_vecs(). 940 * 941 * As each item fills in the entries it needs, also pin the item 942 * so that it cannot be flushed out until the log write completes. 943 */ 944 STATIC void 945 xfs_trans_fill_vecs( 946 xfs_trans_t *tp, 947 xfs_log_iovec_t *log_vector) 948 { 949 xfs_log_item_desc_t *lidp; 950 xfs_log_iovec_t *vecp; 951 uint nitems; 952 953 /* 954 * Skip over the entry for the transaction header, we'll 955 * fill that in at the end. 956 */ 957 vecp = log_vector + 1; /* pointer arithmetic */ 958 959 nitems = 0; 960 lidp = xfs_trans_first_item(tp); 961 ASSERT(lidp != NULL); 962 while (lidp != NULL) { 963 /* 964 * Skip items which aren't dirty in this transaction. 965 */ 966 if (!(lidp->lid_flags & XFS_LID_DIRTY)) { 967 lidp = xfs_trans_next_item(tp, lidp); 968 continue; 969 } 970 /* 971 * The item may be marked dirty but not log anything. 972 * This can be used to get called when a transaction 973 * is committed. 974 */ 975 if (lidp->lid_size) { 976 nitems++; 977 } 978 IOP_FORMAT(lidp->lid_item, vecp); 979 vecp += lidp->lid_size; /* pointer arithmetic */ 980 IOP_PIN(lidp->lid_item); 981 lidp = xfs_trans_next_item(tp, lidp); 982 } 983 984 /* 985 * Now that we've counted the number of items in this 986 * transaction, fill in the transaction header. 987 */ 988 tp->t_header.th_magic = XFS_TRANS_HEADER_MAGIC; 989 tp->t_header.th_type = tp->t_type; 990 tp->t_header.th_num_items = nitems; 991 log_vector->i_addr = (xfs_caddr_t)&tp->t_header; 992 log_vector->i_len = sizeof(xfs_trans_header_t); 993 XLOG_VEC_SET_TYPE(log_vector, XLOG_REG_TYPE_TRANSHDR); 994 } 995 996 997 /* 998 * Unlock all of the transaction's items and free the transaction. 999 * The transaction must not have modified any of its items, because 1000 * there is no way to restore them to their previous state. 1001 * 1002 * If the transaction has made a log reservation, make sure to release 1003 * it as well. 1004 */ 1005 void 1006 xfs_trans_cancel( 1007 xfs_trans_t *tp, 1008 int flags) 1009 { 1010 int log_flags; 1011 #ifdef DEBUG 1012 xfs_log_item_chunk_t *licp; 1013 xfs_log_item_desc_t *lidp; 1014 xfs_log_item_t *lip; 1015 int i; 1016 #endif 1017 1018 /* 1019 * See if the caller is being too lazy to figure out if 1020 * the transaction really needs an abort. 1021 */ 1022 if ((flags & XFS_TRANS_ABORT) && !(tp->t_flags & XFS_TRANS_DIRTY)) 1023 flags &= ~XFS_TRANS_ABORT; 1024 /* 1025 * See if the caller is relying on us to shut down the 1026 * filesystem. This happens in paths where we detect 1027 * corruption and decide to give up. 1028 */ 1029 if ((tp->t_flags & XFS_TRANS_DIRTY) && 1030 !XFS_FORCED_SHUTDOWN(tp->t_mountp)) 1031 xfs_force_shutdown(tp->t_mountp, XFS_CORRUPT_INCORE); 1032 #ifdef DEBUG 1033 if (!(flags & XFS_TRANS_ABORT)) { 1034 licp = &(tp->t_items); 1035 while (licp != NULL) { 1036 lidp = licp->lic_descs; 1037 for (i = 0; i < licp->lic_unused; i++, lidp++) { 1038 if (XFS_LIC_ISFREE(licp, i)) { 1039 continue; 1040 } 1041 1042 lip = lidp->lid_item; 1043 if (!XFS_FORCED_SHUTDOWN(tp->t_mountp)) 1044 ASSERT(!(lip->li_type == XFS_LI_EFD)); 1045 } 1046 licp = licp->lic_next; 1047 } 1048 } 1049 #endif 1050 xfs_trans_unreserve_and_mod_sb(tp); 1051 XFS_TRANS_UNRESERVE_AND_MOD_DQUOTS(tp->t_mountp, tp); 1052 1053 if (tp->t_ticket) { 1054 if (flags & XFS_TRANS_RELEASE_LOG_RES) { 1055 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES); 1056 log_flags = XFS_LOG_REL_PERM_RESERV; 1057 } else { 1058 log_flags = 0; 1059 } 1060 xfs_log_done(tp->t_mountp, tp->t_ticket, NULL, log_flags); 1061 } 1062 1063 /* mark this thread as no longer being in a transaction */ 1064 PFLAGS_RESTORE_FSTRANS(&tp->t_pflags); 1065 1066 xfs_trans_free_items(tp, flags); 1067 xfs_trans_free_busy(tp); 1068 xfs_trans_free(tp); 1069 } 1070 1071 1072 /* 1073 * Free the transaction structure. If there is more clean up 1074 * to do when the structure is freed, add it here. 1075 */ 1076 STATIC void 1077 xfs_trans_free( 1078 xfs_trans_t *tp) 1079 { 1080 atomic_dec(&tp->t_mountp->m_active_trans); 1081 XFS_TRANS_FREE_DQINFO(tp->t_mountp, tp); 1082 kmem_zone_free(xfs_trans_zone, tp); 1083 } 1084 1085 1086 /* 1087 * THIS SHOULD BE REWRITTEN TO USE xfs_trans_next_item(). 1088 * 1089 * This is typically called by the LM when a transaction has been fully 1090 * committed to disk. It needs to unpin the items which have 1091 * been logged by the transaction and update their positions 1092 * in the AIL if necessary. 1093 * This also gets called when the transactions didn't get written out 1094 * because of an I/O error. Abortflag & XFS_LI_ABORTED is set then. 1095 * 1096 * Call xfs_trans_chunk_committed() to process the items in 1097 * each chunk. 1098 */ 1099 STATIC void 1100 xfs_trans_committed( 1101 xfs_trans_t *tp, 1102 int abortflag) 1103 { 1104 xfs_log_item_chunk_t *licp; 1105 xfs_log_item_chunk_t *next_licp; 1106 xfs_log_busy_chunk_t *lbcp; 1107 xfs_log_busy_slot_t *lbsp; 1108 int i; 1109 1110 /* 1111 * Call the transaction's completion callback if there 1112 * is one. 1113 */ 1114 if (tp->t_callback != NULL) { 1115 tp->t_callback(tp, tp->t_callarg); 1116 } 1117 1118 /* 1119 * Special case the chunk embedded in the transaction. 1120 */ 1121 licp = &(tp->t_items); 1122 if (!(XFS_LIC_ARE_ALL_FREE(licp))) { 1123 xfs_trans_chunk_committed(licp, tp->t_lsn, abortflag); 1124 } 1125 1126 /* 1127 * Process the items in each chunk in turn. 1128 */ 1129 licp = licp->lic_next; 1130 while (licp != NULL) { 1131 ASSERT(!XFS_LIC_ARE_ALL_FREE(licp)); 1132 xfs_trans_chunk_committed(licp, tp->t_lsn, abortflag); 1133 next_licp = licp->lic_next; 1134 kmem_free(licp, sizeof(xfs_log_item_chunk_t)); 1135 licp = next_licp; 1136 } 1137 1138 /* 1139 * Clear all the per-AG busy list items listed in this transaction 1140 */ 1141 lbcp = &tp->t_busy; 1142 while (lbcp != NULL) { 1143 for (i = 0, lbsp = lbcp->lbc_busy; i < lbcp->lbc_unused; i++, lbsp++) { 1144 if (!XFS_LBC_ISFREE(lbcp, i)) { 1145 xfs_alloc_clear_busy(tp, lbsp->lbc_ag, 1146 lbsp->lbc_idx); 1147 } 1148 } 1149 lbcp = lbcp->lbc_next; 1150 } 1151 xfs_trans_free_busy(tp); 1152 1153 /* 1154 * That's it for the transaction structure. Free it. 1155 */ 1156 xfs_trans_free(tp); 1157 } 1158 1159 /* 1160 * This is called to perform the commit processing for each 1161 * item described by the given chunk. 1162 * 1163 * The commit processing consists of unlocking items which were 1164 * held locked with the SYNC_UNLOCK attribute, calling the committed 1165 * routine of each logged item, updating the item's position in the AIL 1166 * if necessary, and unpinning each item. If the committed routine 1167 * returns -1, then do nothing further with the item because it 1168 * may have been freed. 1169 * 1170 * Since items are unlocked when they are copied to the incore 1171 * log, it is possible for two transactions to be completing 1172 * and manipulating the same item simultaneously. The AIL lock 1173 * will protect the lsn field of each item. The value of this 1174 * field can never go backwards. 1175 * 1176 * We unpin the items after repositioning them in the AIL, because 1177 * otherwise they could be immediately flushed and we'd have to race 1178 * with the flusher trying to pull the item from the AIL as we add it. 1179 */ 1180 STATIC void 1181 xfs_trans_chunk_committed( 1182 xfs_log_item_chunk_t *licp, 1183 xfs_lsn_t lsn, 1184 int aborted) 1185 { 1186 xfs_log_item_desc_t *lidp; 1187 xfs_log_item_t *lip; 1188 xfs_lsn_t item_lsn; 1189 struct xfs_mount *mp; 1190 int i; 1191 SPLDECL(s); 1192 1193 lidp = licp->lic_descs; 1194 for (i = 0; i < licp->lic_unused; i++, lidp++) { 1195 if (XFS_LIC_ISFREE(licp, i)) { 1196 continue; 1197 } 1198 1199 lip = lidp->lid_item; 1200 if (aborted) 1201 lip->li_flags |= XFS_LI_ABORTED; 1202 1203 /* 1204 * Send in the ABORTED flag to the COMMITTED routine 1205 * so that it knows whether the transaction was aborted 1206 * or not. 1207 */ 1208 item_lsn = IOP_COMMITTED(lip, lsn); 1209 1210 /* 1211 * If the committed routine returns -1, make 1212 * no more references to the item. 1213 */ 1214 if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0) { 1215 continue; 1216 } 1217 1218 /* 1219 * If the returned lsn is greater than what it 1220 * contained before, update the location of the 1221 * item in the AIL. If it is not, then do nothing. 1222 * Items can never move backwards in the AIL. 1223 * 1224 * While the new lsn should usually be greater, it 1225 * is possible that a later transaction completing 1226 * simultaneously with an earlier one using the 1227 * same item could complete first with a higher lsn. 1228 * This would cause the earlier transaction to fail 1229 * the test below. 1230 */ 1231 mp = lip->li_mountp; 1232 AIL_LOCK(mp,s); 1233 if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0) { 1234 /* 1235 * This will set the item's lsn to item_lsn 1236 * and update the position of the item in 1237 * the AIL. 1238 * 1239 * xfs_trans_update_ail() drops the AIL lock. 1240 */ 1241 xfs_trans_update_ail(mp, lip, item_lsn, s); 1242 } else { 1243 AIL_UNLOCK(mp, s); 1244 } 1245 1246 /* 1247 * Now that we've repositioned the item in the AIL, 1248 * unpin it so it can be flushed. Pass information 1249 * about buffer stale state down from the log item 1250 * flags, if anyone else stales the buffer we do not 1251 * want to pay any attention to it. 1252 */ 1253 IOP_UNPIN(lip, lidp->lid_flags & XFS_LID_BUF_STALE); 1254 } 1255 } 1256