1 /* 2 * Copyright (c) 2000-2001 Silicon Graphics, Inc. All Rights Reserved. 3 * 4 * This program is free software; you can redistribute it and/or modify it 5 * under the terms of version 2 of the GNU General Public License as 6 * published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it would be useful, but 9 * WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 11 * 12 * Further, this software is distributed without any warranty that it is 13 * free of the rightful claim of any third person regarding infringement 14 * or the like. Any license provided herein, whether implied or 15 * otherwise, applies only to this software file. Patent licenses, if 16 * any, provided herein do not apply to combinations of this program with 17 * other software, or any other product whatsoever. 18 * 19 * You should have received a copy of the GNU General Public License along 20 * with this program; if not, write the Free Software Foundation, Inc., 59 21 * Temple Place - Suite 330, Boston MA 02111-1307, USA. 22 * 23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, 24 * Mountain View, CA 94043, or: 25 * 26 * http://www.sgi.com 27 * 28 * For further information regarding this notice, see: 29 * 30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/ 31 */ 32 33 /* 34 * This file contains the implementation of the xfs_efi_log_item 35 * and xfs_efd_log_item items. 36 */ 37 38 #include "xfs.h" 39 40 #include "xfs_macros.h" 41 #include "xfs_types.h" 42 #include "xfs_inum.h" 43 #include "xfs_log.h" 44 #include "xfs_trans.h" 45 #include "xfs_buf_item.h" 46 #include "xfs_sb.h" 47 #include "xfs_dir.h" 48 #include "xfs_dmapi.h" 49 #include "xfs_mount.h" 50 #include "xfs_trans_priv.h" 51 #include "xfs_extfree_item.h" 52 53 54 kmem_zone_t *xfs_efi_zone; 55 kmem_zone_t *xfs_efd_zone; 56 57 STATIC void xfs_efi_item_unlock(xfs_efi_log_item_t *); 58 STATIC void xfs_efi_item_abort(xfs_efi_log_item_t *); 59 STATIC void xfs_efd_item_abort(xfs_efd_log_item_t *); 60 61 62 void 63 xfs_efi_item_free(xfs_efi_log_item_t *efip) 64 { 65 int nexts = efip->efi_format.efi_nextents; 66 67 if (nexts > XFS_EFI_MAX_FAST_EXTENTS) { 68 kmem_free(efip, sizeof(xfs_efi_log_item_t) + 69 (nexts - 1) * sizeof(xfs_extent_t)); 70 } else { 71 kmem_zone_free(xfs_efi_zone, efip); 72 } 73 } 74 75 /* 76 * This returns the number of iovecs needed to log the given efi item. 77 * We only need 1 iovec for an efi item. It just logs the efi_log_format 78 * structure. 79 */ 80 /*ARGSUSED*/ 81 STATIC uint 82 xfs_efi_item_size(xfs_efi_log_item_t *efip) 83 { 84 return 1; 85 } 86 87 /* 88 * This is called to fill in the vector of log iovecs for the 89 * given efi log item. We use only 1 iovec, and we point that 90 * at the efi_log_format structure embedded in the efi item. 91 * It is at this point that we assert that all of the extent 92 * slots in the efi item have been filled. 93 */ 94 STATIC void 95 xfs_efi_item_format(xfs_efi_log_item_t *efip, 96 xfs_log_iovec_t *log_vector) 97 { 98 uint size; 99 100 ASSERT(efip->efi_next_extent == efip->efi_format.efi_nextents); 101 102 efip->efi_format.efi_type = XFS_LI_EFI; 103 104 size = sizeof(xfs_efi_log_format_t); 105 size += (efip->efi_format.efi_nextents - 1) * sizeof(xfs_extent_t); 106 efip->efi_format.efi_size = 1; 107 108 log_vector->i_addr = (xfs_caddr_t)&(efip->efi_format); 109 log_vector->i_len = size; 110 XLOG_VEC_SET_TYPE(log_vector, XLOG_REG_TYPE_EFI_FORMAT); 111 ASSERT(size >= sizeof(xfs_efi_log_format_t)); 112 } 113 114 115 /* 116 * Pinning has no meaning for an efi item, so just return. 117 */ 118 /*ARGSUSED*/ 119 STATIC void 120 xfs_efi_item_pin(xfs_efi_log_item_t *efip) 121 { 122 return; 123 } 124 125 126 /* 127 * While EFIs cannot really be pinned, the unpin operation is the 128 * last place at which the EFI is manipulated during a transaction. 129 * Here we coordinate with xfs_efi_cancel() to determine who gets to 130 * free the EFI. 131 */ 132 /*ARGSUSED*/ 133 STATIC void 134 xfs_efi_item_unpin(xfs_efi_log_item_t *efip, int stale) 135 { 136 xfs_mount_t *mp; 137 SPLDECL(s); 138 139 mp = efip->efi_item.li_mountp; 140 AIL_LOCK(mp, s); 141 if (efip->efi_flags & XFS_EFI_CANCELED) { 142 /* 143 * xfs_trans_delete_ail() drops the AIL lock. 144 */ 145 xfs_trans_delete_ail(mp, (xfs_log_item_t *)efip, s); 146 xfs_efi_item_free(efip); 147 } else { 148 efip->efi_flags |= XFS_EFI_COMMITTED; 149 AIL_UNLOCK(mp, s); 150 } 151 } 152 153 /* 154 * like unpin only we have to also clear the xaction descriptor 155 * pointing the log item if we free the item. This routine duplicates 156 * unpin because efi_flags is protected by the AIL lock. Freeing 157 * the descriptor and then calling unpin would force us to drop the AIL 158 * lock which would open up a race condition. 159 */ 160 STATIC void 161 xfs_efi_item_unpin_remove(xfs_efi_log_item_t *efip, xfs_trans_t *tp) 162 { 163 xfs_mount_t *mp; 164 xfs_log_item_desc_t *lidp; 165 SPLDECL(s); 166 167 mp = efip->efi_item.li_mountp; 168 AIL_LOCK(mp, s); 169 if (efip->efi_flags & XFS_EFI_CANCELED) { 170 /* 171 * free the xaction descriptor pointing to this item 172 */ 173 lidp = xfs_trans_find_item(tp, (xfs_log_item_t *) efip); 174 xfs_trans_free_item(tp, lidp); 175 /* 176 * pull the item off the AIL. 177 * xfs_trans_delete_ail() drops the AIL lock. 178 */ 179 xfs_trans_delete_ail(mp, (xfs_log_item_t *)efip, s); 180 xfs_efi_item_free(efip); 181 } else { 182 efip->efi_flags |= XFS_EFI_COMMITTED; 183 AIL_UNLOCK(mp, s); 184 } 185 } 186 187 /* 188 * Efi items have no locking or pushing. However, since EFIs are 189 * pulled from the AIL when their corresponding EFDs are committed 190 * to disk, their situation is very similar to being pinned. Return 191 * XFS_ITEM_PINNED so that the caller will eventually flush the log. 192 * This should help in getting the EFI out of the AIL. 193 */ 194 /*ARGSUSED*/ 195 STATIC uint 196 xfs_efi_item_trylock(xfs_efi_log_item_t *efip) 197 { 198 return XFS_ITEM_PINNED; 199 } 200 201 /* 202 * Efi items have no locking, so just return. 203 */ 204 /*ARGSUSED*/ 205 STATIC void 206 xfs_efi_item_unlock(xfs_efi_log_item_t *efip) 207 { 208 if (efip->efi_item.li_flags & XFS_LI_ABORTED) 209 xfs_efi_item_abort(efip); 210 return; 211 } 212 213 /* 214 * The EFI is logged only once and cannot be moved in the log, so 215 * simply return the lsn at which it's been logged. The canceled 216 * flag is not paid any attention here. Checking for that is delayed 217 * until the EFI is unpinned. 218 */ 219 /*ARGSUSED*/ 220 STATIC xfs_lsn_t 221 xfs_efi_item_committed(xfs_efi_log_item_t *efip, xfs_lsn_t lsn) 222 { 223 return lsn; 224 } 225 226 /* 227 * This is called when the transaction logging the EFI is aborted. 228 * Free up the EFI and return. No need to clean up the slot for 229 * the item in the transaction. That was done by the unpin code 230 * which is called prior to this routine in the abort/fs-shutdown path. 231 */ 232 STATIC void 233 xfs_efi_item_abort(xfs_efi_log_item_t *efip) 234 { 235 xfs_efi_item_free(efip); 236 } 237 238 /* 239 * There isn't much you can do to push on an efi item. It is simply 240 * stuck waiting for all of its corresponding efd items to be 241 * committed to disk. 242 */ 243 /*ARGSUSED*/ 244 STATIC void 245 xfs_efi_item_push(xfs_efi_log_item_t *efip) 246 { 247 return; 248 } 249 250 /* 251 * The EFI dependency tracking op doesn't do squat. It can't because 252 * it doesn't know where the free extent is coming from. The dependency 253 * tracking has to be handled by the "enclosing" metadata object. For 254 * example, for inodes, the inode is locked throughout the extent freeing 255 * so the dependency should be recorded there. 256 */ 257 /*ARGSUSED*/ 258 STATIC void 259 xfs_efi_item_committing(xfs_efi_log_item_t *efip, xfs_lsn_t lsn) 260 { 261 return; 262 } 263 264 /* 265 * This is the ops vector shared by all efi log items. 266 */ 267 STATIC struct xfs_item_ops xfs_efi_item_ops = { 268 .iop_size = (uint(*)(xfs_log_item_t*))xfs_efi_item_size, 269 .iop_format = (void(*)(xfs_log_item_t*, xfs_log_iovec_t*)) 270 xfs_efi_item_format, 271 .iop_pin = (void(*)(xfs_log_item_t*))xfs_efi_item_pin, 272 .iop_unpin = (void(*)(xfs_log_item_t*, int))xfs_efi_item_unpin, 273 .iop_unpin_remove = (void(*)(xfs_log_item_t*, xfs_trans_t *)) 274 xfs_efi_item_unpin_remove, 275 .iop_trylock = (uint(*)(xfs_log_item_t*))xfs_efi_item_trylock, 276 .iop_unlock = (void(*)(xfs_log_item_t*))xfs_efi_item_unlock, 277 .iop_committed = (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t)) 278 xfs_efi_item_committed, 279 .iop_push = (void(*)(xfs_log_item_t*))xfs_efi_item_push, 280 .iop_abort = (void(*)(xfs_log_item_t*))xfs_efi_item_abort, 281 .iop_pushbuf = NULL, 282 .iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t)) 283 xfs_efi_item_committing 284 }; 285 286 287 /* 288 * Allocate and initialize an efi item with the given number of extents. 289 */ 290 xfs_efi_log_item_t * 291 xfs_efi_init(xfs_mount_t *mp, 292 uint nextents) 293 294 { 295 xfs_efi_log_item_t *efip; 296 uint size; 297 298 ASSERT(nextents > 0); 299 if (nextents > XFS_EFI_MAX_FAST_EXTENTS) { 300 size = (uint)(sizeof(xfs_efi_log_item_t) + 301 ((nextents - 1) * sizeof(xfs_extent_t))); 302 efip = (xfs_efi_log_item_t*)kmem_zalloc(size, KM_SLEEP); 303 } else { 304 efip = (xfs_efi_log_item_t*)kmem_zone_zalloc(xfs_efi_zone, 305 KM_SLEEP); 306 } 307 308 efip->efi_item.li_type = XFS_LI_EFI; 309 efip->efi_item.li_ops = &xfs_efi_item_ops; 310 efip->efi_item.li_mountp = mp; 311 efip->efi_format.efi_nextents = nextents; 312 efip->efi_format.efi_id = (__psint_t)(void*)efip; 313 314 return (efip); 315 } 316 317 /* 318 * This is called by the efd item code below to release references to 319 * the given efi item. Each efd calls this with the number of 320 * extents that it has logged, and when the sum of these reaches 321 * the total number of extents logged by this efi item we can free 322 * the efi item. 323 * 324 * Freeing the efi item requires that we remove it from the AIL. 325 * We'll use the AIL lock to protect our counters as well as 326 * the removal from the AIL. 327 */ 328 void 329 xfs_efi_release(xfs_efi_log_item_t *efip, 330 uint nextents) 331 { 332 xfs_mount_t *mp; 333 int extents_left; 334 SPLDECL(s); 335 336 mp = efip->efi_item.li_mountp; 337 ASSERT(efip->efi_next_extent > 0); 338 ASSERT(efip->efi_flags & XFS_EFI_COMMITTED); 339 340 AIL_LOCK(mp, s); 341 ASSERT(efip->efi_next_extent >= nextents); 342 efip->efi_next_extent -= nextents; 343 extents_left = efip->efi_next_extent; 344 if (extents_left == 0) { 345 /* 346 * xfs_trans_delete_ail() drops the AIL lock. 347 */ 348 xfs_trans_delete_ail(mp, (xfs_log_item_t *)efip, s); 349 xfs_efi_item_free(efip); 350 } else { 351 AIL_UNLOCK(mp, s); 352 } 353 } 354 355 /* 356 * This is called when the transaction that should be committing the 357 * EFD corresponding to the given EFI is aborted. The committed and 358 * canceled flags are used to coordinate the freeing of the EFI and 359 * the references by the transaction that committed it. 360 */ 361 STATIC void 362 xfs_efi_cancel( 363 xfs_efi_log_item_t *efip) 364 { 365 xfs_mount_t *mp; 366 SPLDECL(s); 367 368 mp = efip->efi_item.li_mountp; 369 AIL_LOCK(mp, s); 370 if (efip->efi_flags & XFS_EFI_COMMITTED) { 371 /* 372 * xfs_trans_delete_ail() drops the AIL lock. 373 */ 374 xfs_trans_delete_ail(mp, (xfs_log_item_t *)efip, s); 375 xfs_efi_item_free(efip); 376 } else { 377 efip->efi_flags |= XFS_EFI_CANCELED; 378 AIL_UNLOCK(mp, s); 379 } 380 } 381 382 STATIC void 383 xfs_efd_item_free(xfs_efd_log_item_t *efdp) 384 { 385 int nexts = efdp->efd_format.efd_nextents; 386 387 if (nexts > XFS_EFD_MAX_FAST_EXTENTS) { 388 kmem_free(efdp, sizeof(xfs_efd_log_item_t) + 389 (nexts - 1) * sizeof(xfs_extent_t)); 390 } else { 391 kmem_zone_free(xfs_efd_zone, efdp); 392 } 393 } 394 395 /* 396 * This returns the number of iovecs needed to log the given efd item. 397 * We only need 1 iovec for an efd item. It just logs the efd_log_format 398 * structure. 399 */ 400 /*ARGSUSED*/ 401 STATIC uint 402 xfs_efd_item_size(xfs_efd_log_item_t *efdp) 403 { 404 return 1; 405 } 406 407 /* 408 * This is called to fill in the vector of log iovecs for the 409 * given efd log item. We use only 1 iovec, and we point that 410 * at the efd_log_format structure embedded in the efd item. 411 * It is at this point that we assert that all of the extent 412 * slots in the efd item have been filled. 413 */ 414 STATIC void 415 xfs_efd_item_format(xfs_efd_log_item_t *efdp, 416 xfs_log_iovec_t *log_vector) 417 { 418 uint size; 419 420 ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents); 421 422 efdp->efd_format.efd_type = XFS_LI_EFD; 423 424 size = sizeof(xfs_efd_log_format_t); 425 size += (efdp->efd_format.efd_nextents - 1) * sizeof(xfs_extent_t); 426 efdp->efd_format.efd_size = 1; 427 428 log_vector->i_addr = (xfs_caddr_t)&(efdp->efd_format); 429 log_vector->i_len = size; 430 XLOG_VEC_SET_TYPE(log_vector, XLOG_REG_TYPE_EFD_FORMAT); 431 ASSERT(size >= sizeof(xfs_efd_log_format_t)); 432 } 433 434 435 /* 436 * Pinning has no meaning for an efd item, so just return. 437 */ 438 /*ARGSUSED*/ 439 STATIC void 440 xfs_efd_item_pin(xfs_efd_log_item_t *efdp) 441 { 442 return; 443 } 444 445 446 /* 447 * Since pinning has no meaning for an efd item, unpinning does 448 * not either. 449 */ 450 /*ARGSUSED*/ 451 STATIC void 452 xfs_efd_item_unpin(xfs_efd_log_item_t *efdp, int stale) 453 { 454 return; 455 } 456 457 /*ARGSUSED*/ 458 STATIC void 459 xfs_efd_item_unpin_remove(xfs_efd_log_item_t *efdp, xfs_trans_t *tp) 460 { 461 return; 462 } 463 464 /* 465 * Efd items have no locking, so just return success. 466 */ 467 /*ARGSUSED*/ 468 STATIC uint 469 xfs_efd_item_trylock(xfs_efd_log_item_t *efdp) 470 { 471 return XFS_ITEM_LOCKED; 472 } 473 474 /* 475 * Efd items have no locking or pushing, so return failure 476 * so that the caller doesn't bother with us. 477 */ 478 /*ARGSUSED*/ 479 STATIC void 480 xfs_efd_item_unlock(xfs_efd_log_item_t *efdp) 481 { 482 if (efdp->efd_item.li_flags & XFS_LI_ABORTED) 483 xfs_efd_item_abort(efdp); 484 return; 485 } 486 487 /* 488 * When the efd item is committed to disk, all we need to do 489 * is delete our reference to our partner efi item and then 490 * free ourselves. Since we're freeing ourselves we must 491 * return -1 to keep the transaction code from further referencing 492 * this item. 493 */ 494 /*ARGSUSED*/ 495 STATIC xfs_lsn_t 496 xfs_efd_item_committed(xfs_efd_log_item_t *efdp, xfs_lsn_t lsn) 497 { 498 /* 499 * If we got a log I/O error, it's always the case that the LR with the 500 * EFI got unpinned and freed before the EFD got aborted. 501 */ 502 if ((efdp->efd_item.li_flags & XFS_LI_ABORTED) == 0) 503 xfs_efi_release(efdp->efd_efip, efdp->efd_format.efd_nextents); 504 505 xfs_efd_item_free(efdp); 506 return (xfs_lsn_t)-1; 507 } 508 509 /* 510 * The transaction of which this EFD is a part has been aborted. 511 * Inform its companion EFI of this fact and then clean up after 512 * ourselves. No need to clean up the slot for the item in the 513 * transaction. That was done by the unpin code which is called 514 * prior to this routine in the abort/fs-shutdown path. 515 */ 516 STATIC void 517 xfs_efd_item_abort(xfs_efd_log_item_t *efdp) 518 { 519 /* 520 * If we got a log I/O error, it's always the case that the LR with the 521 * EFI got unpinned and freed before the EFD got aborted. So don't 522 * reference the EFI at all in that case. 523 */ 524 if ((efdp->efd_item.li_flags & XFS_LI_ABORTED) == 0) 525 xfs_efi_cancel(efdp->efd_efip); 526 527 xfs_efd_item_free(efdp); 528 } 529 530 /* 531 * There isn't much you can do to push on an efd item. It is simply 532 * stuck waiting for the log to be flushed to disk. 533 */ 534 /*ARGSUSED*/ 535 STATIC void 536 xfs_efd_item_push(xfs_efd_log_item_t *efdp) 537 { 538 return; 539 } 540 541 /* 542 * The EFD dependency tracking op doesn't do squat. It can't because 543 * it doesn't know where the free extent is coming from. The dependency 544 * tracking has to be handled by the "enclosing" metadata object. For 545 * example, for inodes, the inode is locked throughout the extent freeing 546 * so the dependency should be recorded there. 547 */ 548 /*ARGSUSED*/ 549 STATIC void 550 xfs_efd_item_committing(xfs_efd_log_item_t *efip, xfs_lsn_t lsn) 551 { 552 return; 553 } 554 555 /* 556 * This is the ops vector shared by all efd log items. 557 */ 558 STATIC struct xfs_item_ops xfs_efd_item_ops = { 559 .iop_size = (uint(*)(xfs_log_item_t*))xfs_efd_item_size, 560 .iop_format = (void(*)(xfs_log_item_t*, xfs_log_iovec_t*)) 561 xfs_efd_item_format, 562 .iop_pin = (void(*)(xfs_log_item_t*))xfs_efd_item_pin, 563 .iop_unpin = (void(*)(xfs_log_item_t*, int))xfs_efd_item_unpin, 564 .iop_unpin_remove = (void(*)(xfs_log_item_t*, xfs_trans_t*)) 565 xfs_efd_item_unpin_remove, 566 .iop_trylock = (uint(*)(xfs_log_item_t*))xfs_efd_item_trylock, 567 .iop_unlock = (void(*)(xfs_log_item_t*))xfs_efd_item_unlock, 568 .iop_committed = (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t)) 569 xfs_efd_item_committed, 570 .iop_push = (void(*)(xfs_log_item_t*))xfs_efd_item_push, 571 .iop_abort = (void(*)(xfs_log_item_t*))xfs_efd_item_abort, 572 .iop_pushbuf = NULL, 573 .iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t)) 574 xfs_efd_item_committing 575 }; 576 577 578 /* 579 * Allocate and initialize an efd item with the given number of extents. 580 */ 581 xfs_efd_log_item_t * 582 xfs_efd_init(xfs_mount_t *mp, 583 xfs_efi_log_item_t *efip, 584 uint nextents) 585 586 { 587 xfs_efd_log_item_t *efdp; 588 uint size; 589 590 ASSERT(nextents > 0); 591 if (nextents > XFS_EFD_MAX_FAST_EXTENTS) { 592 size = (uint)(sizeof(xfs_efd_log_item_t) + 593 ((nextents - 1) * sizeof(xfs_extent_t))); 594 efdp = (xfs_efd_log_item_t*)kmem_zalloc(size, KM_SLEEP); 595 } else { 596 efdp = (xfs_efd_log_item_t*)kmem_zone_zalloc(xfs_efd_zone, 597 KM_SLEEP); 598 } 599 600 efdp->efd_item.li_type = XFS_LI_EFD; 601 efdp->efd_item.li_ops = &xfs_efd_item_ops; 602 efdp->efd_item.li_mountp = mp; 603 efdp->efd_efip = efip; 604 efdp->efd_format.efd_nextents = nextents; 605 efdp->efd_format.efd_efi_id = efip->efi_format.efi_id; 606 607 return (efdp); 608 } 609