1 /* 2 * Copyright (c) 2000-2002,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_buf_item.h" 26 #include "xfs_sb.h" 27 #include "xfs_ag.h" 28 #include "xfs_dir2.h" 29 #include "xfs_dmapi.h" 30 #include "xfs_mount.h" 31 #include "xfs_trans_priv.h" 32 #include "xfs_bmap_btree.h" 33 #include "xfs_alloc_btree.h" 34 #include "xfs_ialloc_btree.h" 35 #include "xfs_dir2_sf.h" 36 #include "xfs_attr_sf.h" 37 #include "xfs_dinode.h" 38 #include "xfs_inode.h" 39 #include "xfs_inode_item.h" 40 #include "xfs_btree.h" 41 #include "xfs_ialloc.h" 42 #include "xfs_rw.h" 43 #include "xfs_error.h" 44 45 46 kmem_zone_t *xfs_ili_zone; /* inode log item zone */ 47 48 /* 49 * This returns the number of iovecs needed to log the given inode item. 50 * 51 * We need one iovec for the inode log format structure, one for the 52 * inode core, and possibly one for the inode data/extents/b-tree root 53 * and one for the inode attribute data/extents/b-tree root. 54 */ 55 STATIC uint 56 xfs_inode_item_size( 57 xfs_inode_log_item_t *iip) 58 { 59 uint nvecs; 60 xfs_inode_t *ip; 61 62 ip = iip->ili_inode; 63 nvecs = 2; 64 65 /* 66 * Only log the data/extents/b-tree root if there is something 67 * left to log. 68 */ 69 iip->ili_format.ilf_fields |= XFS_ILOG_CORE; 70 71 switch (ip->i_d.di_format) { 72 case XFS_DINODE_FMT_EXTENTS: 73 iip->ili_format.ilf_fields &= 74 ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT | 75 XFS_ILOG_DEV | XFS_ILOG_UUID); 76 if ((iip->ili_format.ilf_fields & XFS_ILOG_DEXT) && 77 (ip->i_d.di_nextents > 0) && 78 (ip->i_df.if_bytes > 0)) { 79 ASSERT(ip->i_df.if_u1.if_extents != NULL); 80 nvecs++; 81 } else { 82 iip->ili_format.ilf_fields &= ~XFS_ILOG_DEXT; 83 } 84 break; 85 86 case XFS_DINODE_FMT_BTREE: 87 ASSERT(ip->i_df.if_ext_max == 88 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t)); 89 iip->ili_format.ilf_fields &= 90 ~(XFS_ILOG_DDATA | XFS_ILOG_DEXT | 91 XFS_ILOG_DEV | XFS_ILOG_UUID); 92 if ((iip->ili_format.ilf_fields & XFS_ILOG_DBROOT) && 93 (ip->i_df.if_broot_bytes > 0)) { 94 ASSERT(ip->i_df.if_broot != NULL); 95 nvecs++; 96 } else { 97 ASSERT(!(iip->ili_format.ilf_fields & 98 XFS_ILOG_DBROOT)); 99 #ifdef XFS_TRANS_DEBUG 100 if (iip->ili_root_size > 0) { 101 ASSERT(iip->ili_root_size == 102 ip->i_df.if_broot_bytes); 103 ASSERT(memcmp(iip->ili_orig_root, 104 ip->i_df.if_broot, 105 iip->ili_root_size) == 0); 106 } else { 107 ASSERT(ip->i_df.if_broot_bytes == 0); 108 } 109 #endif 110 iip->ili_format.ilf_fields &= ~XFS_ILOG_DBROOT; 111 } 112 break; 113 114 case XFS_DINODE_FMT_LOCAL: 115 iip->ili_format.ilf_fields &= 116 ~(XFS_ILOG_DEXT | XFS_ILOG_DBROOT | 117 XFS_ILOG_DEV | XFS_ILOG_UUID); 118 if ((iip->ili_format.ilf_fields & XFS_ILOG_DDATA) && 119 (ip->i_df.if_bytes > 0)) { 120 ASSERT(ip->i_df.if_u1.if_data != NULL); 121 ASSERT(ip->i_d.di_size > 0); 122 nvecs++; 123 } else { 124 iip->ili_format.ilf_fields &= ~XFS_ILOG_DDATA; 125 } 126 break; 127 128 case XFS_DINODE_FMT_DEV: 129 iip->ili_format.ilf_fields &= 130 ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT | 131 XFS_ILOG_DEXT | XFS_ILOG_UUID); 132 break; 133 134 case XFS_DINODE_FMT_UUID: 135 iip->ili_format.ilf_fields &= 136 ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT | 137 XFS_ILOG_DEXT | XFS_ILOG_DEV); 138 break; 139 140 default: 141 ASSERT(0); 142 break; 143 } 144 145 /* 146 * If there are no attributes associated with this file, 147 * then there cannot be anything more to log. 148 * Clear all attribute-related log flags. 149 */ 150 if (!XFS_IFORK_Q(ip)) { 151 iip->ili_format.ilf_fields &= 152 ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT | XFS_ILOG_AEXT); 153 return nvecs; 154 } 155 156 /* 157 * Log any necessary attribute data. 158 */ 159 switch (ip->i_d.di_aformat) { 160 case XFS_DINODE_FMT_EXTENTS: 161 iip->ili_format.ilf_fields &= 162 ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT); 163 if ((iip->ili_format.ilf_fields & XFS_ILOG_AEXT) && 164 (ip->i_d.di_anextents > 0) && 165 (ip->i_afp->if_bytes > 0)) { 166 ASSERT(ip->i_afp->if_u1.if_extents != NULL); 167 nvecs++; 168 } else { 169 iip->ili_format.ilf_fields &= ~XFS_ILOG_AEXT; 170 } 171 break; 172 173 case XFS_DINODE_FMT_BTREE: 174 iip->ili_format.ilf_fields &= 175 ~(XFS_ILOG_ADATA | XFS_ILOG_AEXT); 176 if ((iip->ili_format.ilf_fields & XFS_ILOG_ABROOT) && 177 (ip->i_afp->if_broot_bytes > 0)) { 178 ASSERT(ip->i_afp->if_broot != NULL); 179 nvecs++; 180 } else { 181 iip->ili_format.ilf_fields &= ~XFS_ILOG_ABROOT; 182 } 183 break; 184 185 case XFS_DINODE_FMT_LOCAL: 186 iip->ili_format.ilf_fields &= 187 ~(XFS_ILOG_AEXT | XFS_ILOG_ABROOT); 188 if ((iip->ili_format.ilf_fields & XFS_ILOG_ADATA) && 189 (ip->i_afp->if_bytes > 0)) { 190 ASSERT(ip->i_afp->if_u1.if_data != NULL); 191 nvecs++; 192 } else { 193 iip->ili_format.ilf_fields &= ~XFS_ILOG_ADATA; 194 } 195 break; 196 197 default: 198 ASSERT(0); 199 break; 200 } 201 202 return nvecs; 203 } 204 205 /* 206 * This is called to fill in the vector of log iovecs for the 207 * given inode log item. It fills the first item with an inode 208 * log format structure, the second with the on-disk inode structure, 209 * and a possible third and/or fourth with the inode data/extents/b-tree 210 * root and inode attributes data/extents/b-tree root. 211 */ 212 STATIC void 213 xfs_inode_item_format( 214 xfs_inode_log_item_t *iip, 215 xfs_log_iovec_t *log_vector) 216 { 217 uint nvecs; 218 xfs_log_iovec_t *vecp; 219 xfs_inode_t *ip; 220 size_t data_bytes; 221 xfs_bmbt_rec_t *ext_buffer; 222 int nrecs; 223 xfs_mount_t *mp; 224 225 ip = iip->ili_inode; 226 vecp = log_vector; 227 228 vecp->i_addr = (xfs_caddr_t)&iip->ili_format; 229 vecp->i_len = sizeof(xfs_inode_log_format_t); 230 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_IFORMAT); 231 vecp++; 232 nvecs = 1; 233 234 /* 235 * Clear i_update_core if the timestamps (or any other 236 * non-transactional modification) need flushing/logging 237 * and we're about to log them with the rest of the core. 238 * 239 * This is the same logic as xfs_iflush() but this code can't 240 * run at the same time as xfs_iflush because we're in commit 241 * processing here and so we have the inode lock held in 242 * exclusive mode. Although it doesn't really matter 243 * for the timestamps if both routines were to grab the 244 * timestamps or not. That would be ok. 245 * 246 * We clear i_update_core before copying out the data. 247 * This is for coordination with our timestamp updates 248 * that don't hold the inode lock. They will always 249 * update the timestamps BEFORE setting i_update_core, 250 * so if we clear i_update_core after they set it we 251 * are guaranteed to see their updates to the timestamps 252 * either here. Likewise, if they set it after we clear it 253 * here, we'll see it either on the next commit of this 254 * inode or the next time the inode gets flushed via 255 * xfs_iflush(). This depends on strongly ordered memory 256 * semantics, but we have that. We use the SYNCHRONIZE 257 * macro to make sure that the compiler does not reorder 258 * the i_update_core access below the data copy below. 259 */ 260 if (ip->i_update_core) { 261 ip->i_update_core = 0; 262 SYNCHRONIZE(); 263 } 264 265 /* 266 * Make sure to get the latest atime from the Linux inode. 267 */ 268 xfs_synchronize_atime(ip); 269 270 /* 271 * make sure the linux inode is dirty 272 */ 273 xfs_mark_inode_dirty_sync(ip); 274 275 vecp->i_addr = (xfs_caddr_t)&ip->i_d; 276 vecp->i_len = sizeof(struct xfs_icdinode); 277 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_ICORE); 278 vecp++; 279 nvecs++; 280 iip->ili_format.ilf_fields |= XFS_ILOG_CORE; 281 282 /* 283 * If this is really an old format inode, then we need to 284 * log it as such. This means that we have to copy the link 285 * count from the new field to the old. We don't have to worry 286 * about the new fields, because nothing trusts them as long as 287 * the old inode version number is there. If the superblock already 288 * has a new version number, then we don't bother converting back. 289 */ 290 mp = ip->i_mount; 291 ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb)); 292 if (ip->i_d.di_version == 1) { 293 if (!xfs_sb_version_hasnlink(&mp->m_sb)) { 294 /* 295 * Convert it back. 296 */ 297 ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1); 298 ip->i_d.di_onlink = ip->i_d.di_nlink; 299 } else { 300 /* 301 * The superblock version has already been bumped, 302 * so just make the conversion to the new inode 303 * format permanent. 304 */ 305 ip->i_d.di_version = 2; 306 ip->i_d.di_onlink = 0; 307 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad)); 308 } 309 } 310 311 switch (ip->i_d.di_format) { 312 case XFS_DINODE_FMT_EXTENTS: 313 ASSERT(!(iip->ili_format.ilf_fields & 314 (XFS_ILOG_DDATA | XFS_ILOG_DBROOT | 315 XFS_ILOG_DEV | XFS_ILOG_UUID))); 316 if (iip->ili_format.ilf_fields & XFS_ILOG_DEXT) { 317 ASSERT(ip->i_df.if_bytes > 0); 318 ASSERT(ip->i_df.if_u1.if_extents != NULL); 319 ASSERT(ip->i_d.di_nextents > 0); 320 ASSERT(iip->ili_extents_buf == NULL); 321 nrecs = ip->i_df.if_bytes / 322 (uint)sizeof(xfs_bmbt_rec_t); 323 ASSERT(nrecs > 0); 324 #ifdef XFS_NATIVE_HOST 325 if (nrecs == ip->i_d.di_nextents) { 326 /* 327 * There are no delayed allocation 328 * extents, so just point to the 329 * real extents array. 330 */ 331 vecp->i_addr = 332 (char *)(ip->i_df.if_u1.if_extents); 333 vecp->i_len = ip->i_df.if_bytes; 334 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_IEXT); 335 } else 336 #endif 337 { 338 /* 339 * There are delayed allocation extents 340 * in the inode, or we need to convert 341 * the extents to on disk format. 342 * Use xfs_iextents_copy() 343 * to copy only the real extents into 344 * a separate buffer. We'll free the 345 * buffer in the unlock routine. 346 */ 347 ext_buffer = kmem_alloc(ip->i_df.if_bytes, 348 KM_SLEEP); 349 iip->ili_extents_buf = ext_buffer; 350 vecp->i_addr = (xfs_caddr_t)ext_buffer; 351 vecp->i_len = xfs_iextents_copy(ip, ext_buffer, 352 XFS_DATA_FORK); 353 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_IEXT); 354 } 355 ASSERT(vecp->i_len <= ip->i_df.if_bytes); 356 iip->ili_format.ilf_dsize = vecp->i_len; 357 vecp++; 358 nvecs++; 359 } 360 break; 361 362 case XFS_DINODE_FMT_BTREE: 363 ASSERT(!(iip->ili_format.ilf_fields & 364 (XFS_ILOG_DDATA | XFS_ILOG_DEXT | 365 XFS_ILOG_DEV | XFS_ILOG_UUID))); 366 if (iip->ili_format.ilf_fields & XFS_ILOG_DBROOT) { 367 ASSERT(ip->i_df.if_broot_bytes > 0); 368 ASSERT(ip->i_df.if_broot != NULL); 369 vecp->i_addr = (xfs_caddr_t)ip->i_df.if_broot; 370 vecp->i_len = ip->i_df.if_broot_bytes; 371 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_IBROOT); 372 vecp++; 373 nvecs++; 374 iip->ili_format.ilf_dsize = ip->i_df.if_broot_bytes; 375 } 376 break; 377 378 case XFS_DINODE_FMT_LOCAL: 379 ASSERT(!(iip->ili_format.ilf_fields & 380 (XFS_ILOG_DBROOT | XFS_ILOG_DEXT | 381 XFS_ILOG_DEV | XFS_ILOG_UUID))); 382 if (iip->ili_format.ilf_fields & XFS_ILOG_DDATA) { 383 ASSERT(ip->i_df.if_bytes > 0); 384 ASSERT(ip->i_df.if_u1.if_data != NULL); 385 ASSERT(ip->i_d.di_size > 0); 386 387 vecp->i_addr = (xfs_caddr_t)ip->i_df.if_u1.if_data; 388 /* 389 * Round i_bytes up to a word boundary. 390 * The underlying memory is guaranteed to 391 * to be there by xfs_idata_realloc(). 392 */ 393 data_bytes = roundup(ip->i_df.if_bytes, 4); 394 ASSERT((ip->i_df.if_real_bytes == 0) || 395 (ip->i_df.if_real_bytes == data_bytes)); 396 vecp->i_len = (int)data_bytes; 397 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_ILOCAL); 398 vecp++; 399 nvecs++; 400 iip->ili_format.ilf_dsize = (unsigned)data_bytes; 401 } 402 break; 403 404 case XFS_DINODE_FMT_DEV: 405 ASSERT(!(iip->ili_format.ilf_fields & 406 (XFS_ILOG_DBROOT | XFS_ILOG_DEXT | 407 XFS_ILOG_DDATA | XFS_ILOG_UUID))); 408 if (iip->ili_format.ilf_fields & XFS_ILOG_DEV) { 409 iip->ili_format.ilf_u.ilfu_rdev = 410 ip->i_df.if_u2.if_rdev; 411 } 412 break; 413 414 case XFS_DINODE_FMT_UUID: 415 ASSERT(!(iip->ili_format.ilf_fields & 416 (XFS_ILOG_DBROOT | XFS_ILOG_DEXT | 417 XFS_ILOG_DDATA | XFS_ILOG_DEV))); 418 if (iip->ili_format.ilf_fields & XFS_ILOG_UUID) { 419 iip->ili_format.ilf_u.ilfu_uuid = 420 ip->i_df.if_u2.if_uuid; 421 } 422 break; 423 424 default: 425 ASSERT(0); 426 break; 427 } 428 429 /* 430 * If there are no attributes associated with the file, 431 * then we're done. 432 * Assert that no attribute-related log flags are set. 433 */ 434 if (!XFS_IFORK_Q(ip)) { 435 ASSERT(nvecs == iip->ili_item.li_desc->lid_size); 436 iip->ili_format.ilf_size = nvecs; 437 ASSERT(!(iip->ili_format.ilf_fields & 438 (XFS_ILOG_ADATA | XFS_ILOG_ABROOT | XFS_ILOG_AEXT))); 439 return; 440 } 441 442 switch (ip->i_d.di_aformat) { 443 case XFS_DINODE_FMT_EXTENTS: 444 ASSERT(!(iip->ili_format.ilf_fields & 445 (XFS_ILOG_ADATA | XFS_ILOG_ABROOT))); 446 if (iip->ili_format.ilf_fields & XFS_ILOG_AEXT) { 447 ASSERT(ip->i_afp->if_bytes > 0); 448 ASSERT(ip->i_afp->if_u1.if_extents != NULL); 449 ASSERT(ip->i_d.di_anextents > 0); 450 #ifdef DEBUG 451 nrecs = ip->i_afp->if_bytes / 452 (uint)sizeof(xfs_bmbt_rec_t); 453 #endif 454 ASSERT(nrecs > 0); 455 ASSERT(nrecs == ip->i_d.di_anextents); 456 #ifdef XFS_NATIVE_HOST 457 /* 458 * There are not delayed allocation extents 459 * for attributes, so just point at the array. 460 */ 461 vecp->i_addr = (char *)(ip->i_afp->if_u1.if_extents); 462 vecp->i_len = ip->i_afp->if_bytes; 463 #else 464 ASSERT(iip->ili_aextents_buf == NULL); 465 /* 466 * Need to endian flip before logging 467 */ 468 ext_buffer = kmem_alloc(ip->i_afp->if_bytes, 469 KM_SLEEP); 470 iip->ili_aextents_buf = ext_buffer; 471 vecp->i_addr = (xfs_caddr_t)ext_buffer; 472 vecp->i_len = xfs_iextents_copy(ip, ext_buffer, 473 XFS_ATTR_FORK); 474 #endif 475 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_IATTR_EXT); 476 iip->ili_format.ilf_asize = vecp->i_len; 477 vecp++; 478 nvecs++; 479 } 480 break; 481 482 case XFS_DINODE_FMT_BTREE: 483 ASSERT(!(iip->ili_format.ilf_fields & 484 (XFS_ILOG_ADATA | XFS_ILOG_AEXT))); 485 if (iip->ili_format.ilf_fields & XFS_ILOG_ABROOT) { 486 ASSERT(ip->i_afp->if_broot_bytes > 0); 487 ASSERT(ip->i_afp->if_broot != NULL); 488 vecp->i_addr = (xfs_caddr_t)ip->i_afp->if_broot; 489 vecp->i_len = ip->i_afp->if_broot_bytes; 490 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_IATTR_BROOT); 491 vecp++; 492 nvecs++; 493 iip->ili_format.ilf_asize = ip->i_afp->if_broot_bytes; 494 } 495 break; 496 497 case XFS_DINODE_FMT_LOCAL: 498 ASSERT(!(iip->ili_format.ilf_fields & 499 (XFS_ILOG_ABROOT | XFS_ILOG_AEXT))); 500 if (iip->ili_format.ilf_fields & XFS_ILOG_ADATA) { 501 ASSERT(ip->i_afp->if_bytes > 0); 502 ASSERT(ip->i_afp->if_u1.if_data != NULL); 503 504 vecp->i_addr = (xfs_caddr_t)ip->i_afp->if_u1.if_data; 505 /* 506 * Round i_bytes up to a word boundary. 507 * The underlying memory is guaranteed to 508 * to be there by xfs_idata_realloc(). 509 */ 510 data_bytes = roundup(ip->i_afp->if_bytes, 4); 511 ASSERT((ip->i_afp->if_real_bytes == 0) || 512 (ip->i_afp->if_real_bytes == data_bytes)); 513 vecp->i_len = (int)data_bytes; 514 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_IATTR_LOCAL); 515 vecp++; 516 nvecs++; 517 iip->ili_format.ilf_asize = (unsigned)data_bytes; 518 } 519 break; 520 521 default: 522 ASSERT(0); 523 break; 524 } 525 526 ASSERT(nvecs == iip->ili_item.li_desc->lid_size); 527 iip->ili_format.ilf_size = nvecs; 528 } 529 530 531 /* 532 * This is called to pin the inode associated with the inode log 533 * item in memory so it cannot be written out. Do this by calling 534 * xfs_ipin() to bump the pin count in the inode while holding the 535 * inode pin lock. 536 */ 537 STATIC void 538 xfs_inode_item_pin( 539 xfs_inode_log_item_t *iip) 540 { 541 ASSERT(xfs_isilocked(iip->ili_inode, XFS_ILOCK_EXCL)); 542 xfs_ipin(iip->ili_inode); 543 } 544 545 546 /* 547 * This is called to unpin the inode associated with the inode log 548 * item which was previously pinned with a call to xfs_inode_item_pin(). 549 * Just call xfs_iunpin() on the inode to do this. 550 */ 551 /* ARGSUSED */ 552 STATIC void 553 xfs_inode_item_unpin( 554 xfs_inode_log_item_t *iip, 555 int stale) 556 { 557 xfs_iunpin(iip->ili_inode); 558 } 559 560 /* ARGSUSED */ 561 STATIC void 562 xfs_inode_item_unpin_remove( 563 xfs_inode_log_item_t *iip, 564 xfs_trans_t *tp) 565 { 566 xfs_iunpin(iip->ili_inode); 567 } 568 569 /* 570 * This is called to attempt to lock the inode associated with this 571 * inode log item, in preparation for the push routine which does the actual 572 * iflush. Don't sleep on the inode lock or the flush lock. 573 * 574 * If the flush lock is already held, indicating that the inode has 575 * been or is in the process of being flushed, then (ideally) we'd like to 576 * see if the inode's buffer is still incore, and if so give it a nudge. 577 * We delay doing so until the pushbuf routine, though, to avoid holding 578 * the AIL lock across a call to the blackhole which is the buffer cache. 579 * Also we don't want to sleep in any device strategy routines, which can happen 580 * if we do the subsequent bawrite in here. 581 */ 582 STATIC uint 583 xfs_inode_item_trylock( 584 xfs_inode_log_item_t *iip) 585 { 586 register xfs_inode_t *ip; 587 588 ip = iip->ili_inode; 589 590 if (xfs_ipincount(ip) > 0) { 591 return XFS_ITEM_PINNED; 592 } 593 594 if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) { 595 return XFS_ITEM_LOCKED; 596 } 597 598 if (!xfs_iflock_nowait(ip)) { 599 /* 600 * If someone else isn't already trying to push the inode 601 * buffer, we get to do it. 602 */ 603 if (iip->ili_pushbuf_flag == 0) { 604 iip->ili_pushbuf_flag = 1; 605 #ifdef DEBUG 606 iip->ili_push_owner = current_pid(); 607 #endif 608 /* 609 * Inode is left locked in shared mode. 610 * Pushbuf routine gets to unlock it. 611 */ 612 return XFS_ITEM_PUSHBUF; 613 } else { 614 /* 615 * We hold the AIL lock, so we must specify the 616 * NONOTIFY flag so that we won't double trip. 617 */ 618 xfs_iunlock(ip, XFS_ILOCK_SHARED|XFS_IUNLOCK_NONOTIFY); 619 return XFS_ITEM_FLUSHING; 620 } 621 /* NOTREACHED */ 622 } 623 624 /* Stale items should force out the iclog */ 625 if (ip->i_flags & XFS_ISTALE) { 626 xfs_ifunlock(ip); 627 xfs_iunlock(ip, XFS_ILOCK_SHARED|XFS_IUNLOCK_NONOTIFY); 628 return XFS_ITEM_PINNED; 629 } 630 631 #ifdef DEBUG 632 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) { 633 ASSERT(iip->ili_format.ilf_fields != 0); 634 ASSERT(iip->ili_logged == 0); 635 ASSERT(iip->ili_item.li_flags & XFS_LI_IN_AIL); 636 } 637 #endif 638 return XFS_ITEM_SUCCESS; 639 } 640 641 /* 642 * Unlock the inode associated with the inode log item. 643 * Clear the fields of the inode and inode log item that 644 * are specific to the current transaction. If the 645 * hold flags is set, do not unlock the inode. 646 */ 647 STATIC void 648 xfs_inode_item_unlock( 649 xfs_inode_log_item_t *iip) 650 { 651 uint hold; 652 uint iolocked; 653 uint lock_flags; 654 xfs_inode_t *ip; 655 656 ASSERT(iip != NULL); 657 ASSERT(iip->ili_inode->i_itemp != NULL); 658 ASSERT(xfs_isilocked(iip->ili_inode, XFS_ILOCK_EXCL)); 659 ASSERT((!(iip->ili_inode->i_itemp->ili_flags & 660 XFS_ILI_IOLOCKED_EXCL)) || 661 xfs_isilocked(iip->ili_inode, XFS_IOLOCK_EXCL)); 662 ASSERT((!(iip->ili_inode->i_itemp->ili_flags & 663 XFS_ILI_IOLOCKED_SHARED)) || 664 xfs_isilocked(iip->ili_inode, XFS_IOLOCK_SHARED)); 665 /* 666 * Clear the transaction pointer in the inode. 667 */ 668 ip = iip->ili_inode; 669 ip->i_transp = NULL; 670 671 /* 672 * If the inode needed a separate buffer with which to log 673 * its extents, then free it now. 674 */ 675 if (iip->ili_extents_buf != NULL) { 676 ASSERT(ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS); 677 ASSERT(ip->i_d.di_nextents > 0); 678 ASSERT(iip->ili_format.ilf_fields & XFS_ILOG_DEXT); 679 ASSERT(ip->i_df.if_bytes > 0); 680 kmem_free(iip->ili_extents_buf); 681 iip->ili_extents_buf = NULL; 682 } 683 if (iip->ili_aextents_buf != NULL) { 684 ASSERT(ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS); 685 ASSERT(ip->i_d.di_anextents > 0); 686 ASSERT(iip->ili_format.ilf_fields & XFS_ILOG_AEXT); 687 ASSERT(ip->i_afp->if_bytes > 0); 688 kmem_free(iip->ili_aextents_buf); 689 iip->ili_aextents_buf = NULL; 690 } 691 692 /* 693 * Figure out if we should unlock the inode or not. 694 */ 695 hold = iip->ili_flags & XFS_ILI_HOLD; 696 697 /* 698 * Before clearing out the flags, remember whether we 699 * are holding the inode's IO lock. 700 */ 701 iolocked = iip->ili_flags & XFS_ILI_IOLOCKED_ANY; 702 703 /* 704 * Clear out the fields of the inode log item particular 705 * to the current transaction. 706 */ 707 iip->ili_flags = 0; 708 709 /* 710 * Unlock the inode if XFS_ILI_HOLD was not set. 711 */ 712 if (!hold) { 713 lock_flags = XFS_ILOCK_EXCL; 714 if (iolocked & XFS_ILI_IOLOCKED_EXCL) { 715 lock_flags |= XFS_IOLOCK_EXCL; 716 } else if (iolocked & XFS_ILI_IOLOCKED_SHARED) { 717 lock_flags |= XFS_IOLOCK_SHARED; 718 } 719 xfs_iput(iip->ili_inode, lock_flags); 720 } 721 } 722 723 /* 724 * This is called to find out where the oldest active copy of the 725 * inode log item in the on disk log resides now that the last log 726 * write of it completed at the given lsn. Since we always re-log 727 * all dirty data in an inode, the latest copy in the on disk log 728 * is the only one that matters. Therefore, simply return the 729 * given lsn. 730 */ 731 /*ARGSUSED*/ 732 STATIC xfs_lsn_t 733 xfs_inode_item_committed( 734 xfs_inode_log_item_t *iip, 735 xfs_lsn_t lsn) 736 { 737 return (lsn); 738 } 739 740 /* 741 * This gets called by xfs_trans_push_ail(), when IOP_TRYLOCK 742 * failed to get the inode flush lock but did get the inode locked SHARED. 743 * Here we're trying to see if the inode buffer is incore, and if so whether it's 744 * marked delayed write. If that's the case, we'll initiate a bawrite on that 745 * buffer to expedite the process. 746 * 747 * We aren't holding the AIL lock (or the flush lock) when this gets called, 748 * so it is inherently race-y. 749 */ 750 STATIC void 751 xfs_inode_item_pushbuf( 752 xfs_inode_log_item_t *iip) 753 { 754 xfs_inode_t *ip; 755 xfs_mount_t *mp; 756 xfs_buf_t *bp; 757 uint dopush; 758 759 ip = iip->ili_inode; 760 761 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED)); 762 763 /* 764 * The ili_pushbuf_flag keeps others from 765 * trying to duplicate our effort. 766 */ 767 ASSERT(iip->ili_pushbuf_flag != 0); 768 ASSERT(iip->ili_push_owner == current_pid()); 769 770 /* 771 * If a flush is not in progress anymore, chances are that the 772 * inode was taken off the AIL. So, just get out. 773 */ 774 if (completion_done(&ip->i_flush) || 775 ((iip->ili_item.li_flags & XFS_LI_IN_AIL) == 0)) { 776 iip->ili_pushbuf_flag = 0; 777 xfs_iunlock(ip, XFS_ILOCK_SHARED); 778 return; 779 } 780 781 mp = ip->i_mount; 782 bp = xfs_incore(mp->m_ddev_targp, iip->ili_format.ilf_blkno, 783 iip->ili_format.ilf_len, XFS_INCORE_TRYLOCK); 784 785 if (bp != NULL) { 786 if (XFS_BUF_ISDELAYWRITE(bp)) { 787 /* 788 * We were racing with iflush because we don't hold 789 * the AIL lock or the flush lock. However, at this point, 790 * we have the buffer, and we know that it's dirty. 791 * So, it's possible that iflush raced with us, and 792 * this item is already taken off the AIL. 793 * If not, we can flush it async. 794 */ 795 dopush = ((iip->ili_item.li_flags & XFS_LI_IN_AIL) && 796 !completion_done(&ip->i_flush)); 797 iip->ili_pushbuf_flag = 0; 798 xfs_iunlock(ip, XFS_ILOCK_SHARED); 799 xfs_buftrace("INODE ITEM PUSH", bp); 800 if (XFS_BUF_ISPINNED(bp)) { 801 xfs_log_force(mp, (xfs_lsn_t)0, 802 XFS_LOG_FORCE); 803 } 804 if (dopush) { 805 int error; 806 error = xfs_bawrite(mp, bp); 807 if (error) 808 xfs_fs_cmn_err(CE_WARN, mp, 809 "xfs_inode_item_pushbuf: pushbuf error %d on iip %p, bp %p", 810 error, iip, bp); 811 } else { 812 xfs_buf_relse(bp); 813 } 814 } else { 815 iip->ili_pushbuf_flag = 0; 816 xfs_iunlock(ip, XFS_ILOCK_SHARED); 817 xfs_buf_relse(bp); 818 } 819 return; 820 } 821 /* 822 * We have to be careful about resetting pushbuf flag too early (above). 823 * Even though in theory we can do it as soon as we have the buflock, 824 * we don't want others to be doing work needlessly. They'll come to 825 * this function thinking that pushing the buffer is their 826 * responsibility only to find that the buffer is still locked by 827 * another doing the same thing 828 */ 829 iip->ili_pushbuf_flag = 0; 830 xfs_iunlock(ip, XFS_ILOCK_SHARED); 831 return; 832 } 833 834 835 /* 836 * This is called to asynchronously write the inode associated with this 837 * inode log item out to disk. The inode will already have been locked by 838 * a successful call to xfs_inode_item_trylock(). 839 */ 840 STATIC void 841 xfs_inode_item_push( 842 xfs_inode_log_item_t *iip) 843 { 844 xfs_inode_t *ip; 845 846 ip = iip->ili_inode; 847 848 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED)); 849 ASSERT(!completion_done(&ip->i_flush)); 850 /* 851 * Since we were able to lock the inode's flush lock and 852 * we found it on the AIL, the inode must be dirty. This 853 * is because the inode is removed from the AIL while still 854 * holding the flush lock in xfs_iflush_done(). Thus, if 855 * we found it in the AIL and were able to obtain the flush 856 * lock without sleeping, then there must not have been 857 * anyone in the process of flushing the inode. 858 */ 859 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || 860 iip->ili_format.ilf_fields != 0); 861 862 /* 863 * Write out the inode. The completion routine ('iflush_done') will 864 * pull it from the AIL, mark it clean, unlock the flush lock. 865 */ 866 (void) xfs_iflush(ip, XFS_IFLUSH_ASYNC); 867 xfs_iunlock(ip, XFS_ILOCK_SHARED); 868 869 return; 870 } 871 872 /* 873 * XXX rcc - this one really has to do something. Probably needs 874 * to stamp in a new field in the incore inode. 875 */ 876 /* ARGSUSED */ 877 STATIC void 878 xfs_inode_item_committing( 879 xfs_inode_log_item_t *iip, 880 xfs_lsn_t lsn) 881 { 882 iip->ili_last_lsn = lsn; 883 return; 884 } 885 886 /* 887 * This is the ops vector shared by all buf log items. 888 */ 889 static struct xfs_item_ops xfs_inode_item_ops = { 890 .iop_size = (uint(*)(xfs_log_item_t*))xfs_inode_item_size, 891 .iop_format = (void(*)(xfs_log_item_t*, xfs_log_iovec_t*)) 892 xfs_inode_item_format, 893 .iop_pin = (void(*)(xfs_log_item_t*))xfs_inode_item_pin, 894 .iop_unpin = (void(*)(xfs_log_item_t*, int))xfs_inode_item_unpin, 895 .iop_unpin_remove = (void(*)(xfs_log_item_t*, xfs_trans_t*)) 896 xfs_inode_item_unpin_remove, 897 .iop_trylock = (uint(*)(xfs_log_item_t*))xfs_inode_item_trylock, 898 .iop_unlock = (void(*)(xfs_log_item_t*))xfs_inode_item_unlock, 899 .iop_committed = (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t)) 900 xfs_inode_item_committed, 901 .iop_push = (void(*)(xfs_log_item_t*))xfs_inode_item_push, 902 .iop_pushbuf = (void(*)(xfs_log_item_t*))xfs_inode_item_pushbuf, 903 .iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t)) 904 xfs_inode_item_committing 905 }; 906 907 908 /* 909 * Initialize the inode log item for a newly allocated (in-core) inode. 910 */ 911 void 912 xfs_inode_item_init( 913 xfs_inode_t *ip, 914 xfs_mount_t *mp) 915 { 916 xfs_inode_log_item_t *iip; 917 918 ASSERT(ip->i_itemp == NULL); 919 iip = ip->i_itemp = kmem_zone_zalloc(xfs_ili_zone, KM_SLEEP); 920 921 iip->ili_item.li_type = XFS_LI_INODE; 922 iip->ili_item.li_ops = &xfs_inode_item_ops; 923 iip->ili_item.li_mountp = mp; 924 iip->ili_item.li_ailp = mp->m_ail; 925 iip->ili_inode = ip; 926 927 /* 928 We have zeroed memory. No need ... 929 iip->ili_extents_buf = NULL; 930 iip->ili_pushbuf_flag = 0; 931 */ 932 933 iip->ili_format.ilf_type = XFS_LI_INODE; 934 iip->ili_format.ilf_ino = ip->i_ino; 935 iip->ili_format.ilf_blkno = ip->i_imap.im_blkno; 936 iip->ili_format.ilf_len = ip->i_imap.im_len; 937 iip->ili_format.ilf_boffset = ip->i_imap.im_boffset; 938 } 939 940 /* 941 * Free the inode log item and any memory hanging off of it. 942 */ 943 void 944 xfs_inode_item_destroy( 945 xfs_inode_t *ip) 946 { 947 #ifdef XFS_TRANS_DEBUG 948 if (ip->i_itemp->ili_root_size != 0) { 949 kmem_free(ip->i_itemp->ili_orig_root); 950 } 951 #endif 952 kmem_zone_free(xfs_ili_zone, ip->i_itemp); 953 } 954 955 956 /* 957 * This is the inode flushing I/O completion routine. It is called 958 * from interrupt level when the buffer containing the inode is 959 * flushed to disk. It is responsible for removing the inode item 960 * from the AIL if it has not been re-logged, and unlocking the inode's 961 * flush lock. 962 */ 963 /*ARGSUSED*/ 964 void 965 xfs_iflush_done( 966 xfs_buf_t *bp, 967 xfs_inode_log_item_t *iip) 968 { 969 xfs_inode_t *ip = iip->ili_inode; 970 struct xfs_ail *ailp = iip->ili_item.li_ailp; 971 972 /* 973 * We only want to pull the item from the AIL if it is 974 * actually there and its location in the log has not 975 * changed since we started the flush. Thus, we only bother 976 * if the ili_logged flag is set and the inode's lsn has not 977 * changed. First we check the lsn outside 978 * the lock since it's cheaper, and then we recheck while 979 * holding the lock before removing the inode from the AIL. 980 */ 981 if (iip->ili_logged && 982 (iip->ili_item.li_lsn == iip->ili_flush_lsn)) { 983 spin_lock(&ailp->xa_lock); 984 if (iip->ili_item.li_lsn == iip->ili_flush_lsn) { 985 /* xfs_trans_ail_delete() drops the AIL lock. */ 986 xfs_trans_ail_delete(ailp, (xfs_log_item_t*)iip); 987 } else { 988 spin_unlock(&ailp->xa_lock); 989 } 990 } 991 992 iip->ili_logged = 0; 993 994 /* 995 * Clear the ili_last_fields bits now that we know that the 996 * data corresponding to them is safely on disk. 997 */ 998 iip->ili_last_fields = 0; 999 1000 /* 1001 * Release the inode's flush lock since we're done with it. 1002 */ 1003 xfs_ifunlock(ip); 1004 1005 return; 1006 } 1007 1008 /* 1009 * This is the inode flushing abort routine. It is called 1010 * from xfs_iflush when the filesystem is shutting down to clean 1011 * up the inode state. 1012 * It is responsible for removing the inode item 1013 * from the AIL if it has not been re-logged, and unlocking the inode's 1014 * flush lock. 1015 */ 1016 void 1017 xfs_iflush_abort( 1018 xfs_inode_t *ip) 1019 { 1020 xfs_inode_log_item_t *iip = ip->i_itemp; 1021 xfs_mount_t *mp; 1022 1023 iip = ip->i_itemp; 1024 mp = ip->i_mount; 1025 if (iip) { 1026 struct xfs_ail *ailp = iip->ili_item.li_ailp; 1027 if (iip->ili_item.li_flags & XFS_LI_IN_AIL) { 1028 spin_lock(&ailp->xa_lock); 1029 if (iip->ili_item.li_flags & XFS_LI_IN_AIL) { 1030 /* xfs_trans_ail_delete() drops the AIL lock. */ 1031 xfs_trans_ail_delete(ailp, (xfs_log_item_t *)iip); 1032 } else 1033 spin_unlock(&ailp->xa_lock); 1034 } 1035 iip->ili_logged = 0; 1036 /* 1037 * Clear the ili_last_fields bits now that we know that the 1038 * data corresponding to them is safely on disk. 1039 */ 1040 iip->ili_last_fields = 0; 1041 /* 1042 * Clear the inode logging fields so no more flushes are 1043 * attempted. 1044 */ 1045 iip->ili_format.ilf_fields = 0; 1046 } 1047 /* 1048 * Release the inode's flush lock since we're done with it. 1049 */ 1050 xfs_ifunlock(ip); 1051 } 1052 1053 void 1054 xfs_istale_done( 1055 xfs_buf_t *bp, 1056 xfs_inode_log_item_t *iip) 1057 { 1058 xfs_iflush_abort(iip->ili_inode); 1059 } 1060 1061 /* 1062 * convert an xfs_inode_log_format struct from either 32 or 64 bit versions 1063 * (which can have different field alignments) to the native version 1064 */ 1065 int 1066 xfs_inode_item_format_convert( 1067 xfs_log_iovec_t *buf, 1068 xfs_inode_log_format_t *in_f) 1069 { 1070 if (buf->i_len == sizeof(xfs_inode_log_format_32_t)) { 1071 xfs_inode_log_format_32_t *in_f32; 1072 1073 in_f32 = (xfs_inode_log_format_32_t *)buf->i_addr; 1074 in_f->ilf_type = in_f32->ilf_type; 1075 in_f->ilf_size = in_f32->ilf_size; 1076 in_f->ilf_fields = in_f32->ilf_fields; 1077 in_f->ilf_asize = in_f32->ilf_asize; 1078 in_f->ilf_dsize = in_f32->ilf_dsize; 1079 in_f->ilf_ino = in_f32->ilf_ino; 1080 /* copy biggest field of ilf_u */ 1081 memcpy(in_f->ilf_u.ilfu_uuid.__u_bits, 1082 in_f32->ilf_u.ilfu_uuid.__u_bits, 1083 sizeof(uuid_t)); 1084 in_f->ilf_blkno = in_f32->ilf_blkno; 1085 in_f->ilf_len = in_f32->ilf_len; 1086 in_f->ilf_boffset = in_f32->ilf_boffset; 1087 return 0; 1088 } else if (buf->i_len == sizeof(xfs_inode_log_format_64_t)){ 1089 xfs_inode_log_format_64_t *in_f64; 1090 1091 in_f64 = (xfs_inode_log_format_64_t *)buf->i_addr; 1092 in_f->ilf_type = in_f64->ilf_type; 1093 in_f->ilf_size = in_f64->ilf_size; 1094 in_f->ilf_fields = in_f64->ilf_fields; 1095 in_f->ilf_asize = in_f64->ilf_asize; 1096 in_f->ilf_dsize = in_f64->ilf_dsize; 1097 in_f->ilf_ino = in_f64->ilf_ino; 1098 /* copy biggest field of ilf_u */ 1099 memcpy(in_f->ilf_u.ilfu_uuid.__u_bits, 1100 in_f64->ilf_u.ilfu_uuid.__u_bits, 1101 sizeof(uuid_t)); 1102 in_f->ilf_blkno = in_f64->ilf_blkno; 1103 in_f->ilf_len = in_f64->ilf_len; 1104 in_f->ilf_boffset = in_f64->ilf_boffset; 1105 return 0; 1106 } 1107 return EFSCORRUPTED; 1108 } 1109