1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc. 4 * All Rights Reserved. 5 */ 6 #include "xfs.h" 7 #include "xfs_fs.h" 8 #include "xfs_shared.h" 9 #include "xfs_format.h" 10 #include "xfs_log_format.h" 11 #include "xfs_trans_resv.h" 12 #include "xfs_mount.h" 13 #include "xfs_inode.h" 14 #include "xfs_trans.h" 15 #include "xfs_inode_item.h" 16 #include "xfs_trace.h" 17 #include "xfs_trans_priv.h" 18 #include "xfs_buf_item.h" 19 #include "xfs_log.h" 20 #include "xfs_log_priv.h" 21 #include "xfs_error.h" 22 #include "xfs_rtbitmap.h" 23 24 #include <linux/iversion.h> 25 26 struct kmem_cache *xfs_ili_cache; /* inode log item */ 27 28 static inline struct xfs_inode_log_item *INODE_ITEM(struct xfs_log_item *lip) 29 { 30 return container_of(lip, struct xfs_inode_log_item, ili_item); 31 } 32 33 static uint64_t 34 xfs_inode_item_sort( 35 struct xfs_log_item *lip) 36 { 37 return INODE_ITEM(lip)->ili_inode->i_ino; 38 } 39 40 #ifdef DEBUG_EXPENSIVE 41 static void 42 xfs_inode_item_precommit_check( 43 struct xfs_inode *ip) 44 { 45 struct xfs_mount *mp = ip->i_mount; 46 struct xfs_dinode *dip; 47 xfs_failaddr_t fa; 48 49 dip = kzalloc(mp->m_sb.sb_inodesize, GFP_KERNEL | GFP_NOFS); 50 if (!dip) { 51 ASSERT(dip != NULL); 52 return; 53 } 54 55 xfs_inode_to_disk(ip, dip, 0); 56 xfs_dinode_calc_crc(mp, dip); 57 fa = xfs_dinode_verify(mp, ip->i_ino, dip); 58 if (fa) { 59 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, dip, 60 sizeof(*dip), fa); 61 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); 62 ASSERT(fa == NULL); 63 } 64 kfree(dip); 65 } 66 #else 67 # define xfs_inode_item_precommit_check(ip) ((void)0) 68 #endif 69 70 /* 71 * Prior to finally logging the inode, we have to ensure that all the 72 * per-modification inode state changes are applied. This includes VFS inode 73 * state updates, format conversions, verifier state synchronisation and 74 * ensuring the inode buffer remains in memory whilst the inode is dirty. 75 * 76 * We have to be careful when we grab the inode cluster buffer due to lock 77 * ordering constraints. The unlinked inode modifications (xfs_iunlink_item) 78 * require AGI -> inode cluster buffer lock order. The inode cluster buffer is 79 * not locked until ->precommit, so it happens after everything else has been 80 * modified. 81 * 82 * Further, we have AGI -> AGF lock ordering, and with O_TMPFILE handling we 83 * have AGI -> AGF -> iunlink item -> inode cluster buffer lock order. Hence we 84 * cannot safely lock the inode cluster buffer in xfs_trans_log_inode() because 85 * it can be called on a inode (e.g. via bumplink/droplink) before we take the 86 * AGF lock modifying directory blocks. 87 * 88 * Rather than force a complete rework of all the transactions to call 89 * xfs_trans_log_inode() once and once only at the end of every transaction, we 90 * move the pinning of the inode cluster buffer to a ->precommit operation. This 91 * matches how the xfs_iunlink_item locks the inode cluster buffer, and it 92 * ensures that the inode cluster buffer locking is always done last in a 93 * transaction. i.e. we ensure the lock order is always AGI -> AGF -> inode 94 * cluster buffer. 95 * 96 * If we return the inode number as the precommit sort key then we'll also 97 * guarantee that the order all inode cluster buffer locking is the same all the 98 * inodes and unlink items in the transaction. 99 */ 100 static int 101 xfs_inode_item_precommit( 102 struct xfs_trans *tp, 103 struct xfs_log_item *lip) 104 { 105 struct xfs_inode_log_item *iip = INODE_ITEM(lip); 106 struct xfs_inode *ip = iip->ili_inode; 107 struct inode *inode = VFS_I(ip); 108 unsigned int flags = iip->ili_dirty_flags; 109 110 /* 111 * Don't bother with i_lock for the I_DIRTY_TIME check here, as races 112 * don't matter - we either will need an extra transaction in 24 hours 113 * to log the timestamps, or will clear already cleared fields in the 114 * worst case. 115 */ 116 if (inode->i_state & I_DIRTY_TIME) { 117 spin_lock(&inode->i_lock); 118 inode->i_state &= ~I_DIRTY_TIME; 119 spin_unlock(&inode->i_lock); 120 } 121 122 /* 123 * If we're updating the inode core or the timestamps and it's possible 124 * to upgrade this inode to bigtime format, do so now. 125 */ 126 if ((flags & (XFS_ILOG_CORE | XFS_ILOG_TIMESTAMP)) && 127 xfs_has_bigtime(ip->i_mount) && 128 !xfs_inode_has_bigtime(ip)) { 129 ip->i_diflags2 |= XFS_DIFLAG2_BIGTIME; 130 flags |= XFS_ILOG_CORE; 131 } 132 133 /* 134 * Inode verifiers do not check that the extent size hint is an integer 135 * multiple of the rt extent size on a directory with both rtinherit 136 * and extszinherit flags set. If we're logging a directory that is 137 * misconfigured in this way, clear the hint. 138 */ 139 if ((ip->i_diflags & XFS_DIFLAG_RTINHERIT) && 140 (ip->i_diflags & XFS_DIFLAG_EXTSZINHERIT) && 141 xfs_extlen_to_rtxmod(ip->i_mount, ip->i_extsize) > 0) { 142 ip->i_diflags &= ~(XFS_DIFLAG_EXTSIZE | 143 XFS_DIFLAG_EXTSZINHERIT); 144 ip->i_extsize = 0; 145 flags |= XFS_ILOG_CORE; 146 } 147 148 /* 149 * Record the specific change for fdatasync optimisation. This allows 150 * fdatasync to skip log forces for inodes that are only timestamp 151 * dirty. Once we've processed the XFS_ILOG_IVERSION flag, convert it 152 * to XFS_ILOG_CORE so that the actual on-disk dirty tracking 153 * (ili_fields) correctly tracks that the version has changed. 154 */ 155 spin_lock(&iip->ili_lock); 156 iip->ili_fsync_fields |= (flags & ~XFS_ILOG_IVERSION); 157 if (flags & XFS_ILOG_IVERSION) 158 flags = ((flags & ~XFS_ILOG_IVERSION) | XFS_ILOG_CORE); 159 160 /* 161 * Inode verifiers do not check that the CoW extent size hint is an 162 * integer multiple of the rt extent size on a directory with both 163 * rtinherit and cowextsize flags set. If we're logging a directory 164 * that is misconfigured in this way, clear the hint. 165 */ 166 if ((ip->i_diflags & XFS_DIFLAG_RTINHERIT) && 167 (ip->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE) && 168 xfs_extlen_to_rtxmod(ip->i_mount, ip->i_cowextsize) > 0) { 169 ip->i_diflags2 &= ~XFS_DIFLAG2_COWEXTSIZE; 170 ip->i_cowextsize = 0; 171 flags |= XFS_ILOG_CORE; 172 } 173 174 if (!iip->ili_item.li_buf) { 175 struct xfs_buf *bp; 176 int error; 177 178 /* 179 * We hold the ILOCK here, so this inode is not going to be 180 * flushed while we are here. Further, because there is no 181 * buffer attached to the item, we know that there is no IO in 182 * progress, so nothing will clear the ili_fields while we read 183 * in the buffer. Hence we can safely drop the spin lock and 184 * read the buffer knowing that the state will not change from 185 * here. 186 */ 187 spin_unlock(&iip->ili_lock); 188 error = xfs_imap_to_bp(ip->i_mount, tp, &ip->i_imap, &bp); 189 if (error) 190 return error; 191 192 /* 193 * We need an explicit buffer reference for the log item but 194 * don't want the buffer to remain attached to the transaction. 195 * Hold the buffer but release the transaction reference once 196 * we've attached the inode log item to the buffer log item 197 * list. 198 */ 199 xfs_buf_hold(bp); 200 spin_lock(&iip->ili_lock); 201 iip->ili_item.li_buf = bp; 202 bp->b_iodone = xfs_buf_inode_iodone; 203 list_add_tail(&iip->ili_item.li_bio_list, &bp->b_li_list); 204 xfs_trans_brelse(tp, bp); 205 } 206 207 /* 208 * Always OR in the bits from the ili_last_fields field. This is to 209 * coordinate with the xfs_iflush() and xfs_buf_inode_iodone() routines 210 * in the eventual clearing of the ili_fields bits. See the big comment 211 * in xfs_iflush() for an explanation of this coordination mechanism. 212 */ 213 iip->ili_fields |= (flags | iip->ili_last_fields); 214 spin_unlock(&iip->ili_lock); 215 216 xfs_inode_item_precommit_check(ip); 217 218 /* 219 * We are done with the log item transaction dirty state, so clear it so 220 * that it doesn't pollute future transactions. 221 */ 222 iip->ili_dirty_flags = 0; 223 return 0; 224 } 225 226 /* 227 * The logged size of an inode fork is always the current size of the inode 228 * fork. This means that when an inode fork is relogged, the size of the logged 229 * region is determined by the current state, not the combination of the 230 * previously logged state + the current state. This is different relogging 231 * behaviour to most other log items which will retain the size of the 232 * previously logged changes when smaller regions are relogged. 233 * 234 * Hence operations that remove data from the inode fork (e.g. shortform 235 * dir/attr remove, extent form extent removal, etc), the size of the relogged 236 * inode gets -smaller- rather than stays the same size as the previously logged 237 * size and this can result in the committing transaction reducing the amount of 238 * space being consumed by the CIL. 239 */ 240 STATIC void 241 xfs_inode_item_data_fork_size( 242 struct xfs_inode_log_item *iip, 243 int *nvecs, 244 int *nbytes) 245 { 246 struct xfs_inode *ip = iip->ili_inode; 247 248 switch (ip->i_df.if_format) { 249 case XFS_DINODE_FMT_EXTENTS: 250 if ((iip->ili_fields & XFS_ILOG_DEXT) && 251 ip->i_df.if_nextents > 0 && 252 ip->i_df.if_bytes > 0) { 253 /* worst case, doesn't subtract delalloc extents */ 254 *nbytes += xfs_inode_data_fork_size(ip); 255 *nvecs += 1; 256 } 257 break; 258 case XFS_DINODE_FMT_BTREE: 259 case XFS_DINODE_FMT_META_BTREE: 260 if ((iip->ili_fields & XFS_ILOG_DBROOT) && 261 ip->i_df.if_broot_bytes > 0) { 262 *nbytes += ip->i_df.if_broot_bytes; 263 *nvecs += 1; 264 } 265 break; 266 case XFS_DINODE_FMT_LOCAL: 267 if ((iip->ili_fields & XFS_ILOG_DDATA) && 268 ip->i_df.if_bytes > 0) { 269 *nbytes += xlog_calc_iovec_len(ip->i_df.if_bytes); 270 *nvecs += 1; 271 } 272 break; 273 274 case XFS_DINODE_FMT_DEV: 275 break; 276 default: 277 ASSERT(0); 278 break; 279 } 280 } 281 282 STATIC void 283 xfs_inode_item_attr_fork_size( 284 struct xfs_inode_log_item *iip, 285 int *nvecs, 286 int *nbytes) 287 { 288 struct xfs_inode *ip = iip->ili_inode; 289 290 switch (ip->i_af.if_format) { 291 case XFS_DINODE_FMT_EXTENTS: 292 if ((iip->ili_fields & XFS_ILOG_AEXT) && 293 ip->i_af.if_nextents > 0 && 294 ip->i_af.if_bytes > 0) { 295 /* worst case, doesn't subtract unused space */ 296 *nbytes += xfs_inode_attr_fork_size(ip); 297 *nvecs += 1; 298 } 299 break; 300 case XFS_DINODE_FMT_BTREE: 301 if ((iip->ili_fields & XFS_ILOG_ABROOT) && 302 ip->i_af.if_broot_bytes > 0) { 303 *nbytes += ip->i_af.if_broot_bytes; 304 *nvecs += 1; 305 } 306 break; 307 case XFS_DINODE_FMT_LOCAL: 308 if ((iip->ili_fields & XFS_ILOG_ADATA) && 309 ip->i_af.if_bytes > 0) { 310 *nbytes += xlog_calc_iovec_len(ip->i_af.if_bytes); 311 *nvecs += 1; 312 } 313 break; 314 default: 315 ASSERT(0); 316 break; 317 } 318 } 319 320 /* 321 * This returns the number of iovecs needed to log the given inode item. 322 * 323 * We need one iovec for the inode log format structure, one for the 324 * inode core, and possibly one for the inode data/extents/b-tree root 325 * and one for the inode attribute data/extents/b-tree root. 326 */ 327 STATIC void 328 xfs_inode_item_size( 329 struct xfs_log_item *lip, 330 int *nvecs, 331 int *nbytes) 332 { 333 struct xfs_inode_log_item *iip = INODE_ITEM(lip); 334 struct xfs_inode *ip = iip->ili_inode; 335 336 *nvecs += 2; 337 *nbytes += sizeof(struct xfs_inode_log_format) + 338 xfs_log_dinode_size(ip->i_mount); 339 340 xfs_inode_item_data_fork_size(iip, nvecs, nbytes); 341 if (xfs_inode_has_attr_fork(ip)) 342 xfs_inode_item_attr_fork_size(iip, nvecs, nbytes); 343 } 344 345 STATIC void 346 xfs_inode_item_format_data_fork( 347 struct xfs_inode_log_item *iip, 348 struct xfs_inode_log_format *ilf, 349 struct xfs_log_vec *lv, 350 struct xfs_log_iovec **vecp) 351 { 352 struct xfs_inode *ip = iip->ili_inode; 353 size_t data_bytes; 354 355 switch (ip->i_df.if_format) { 356 case XFS_DINODE_FMT_EXTENTS: 357 iip->ili_fields &= 358 ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT | XFS_ILOG_DEV); 359 360 if ((iip->ili_fields & XFS_ILOG_DEXT) && 361 ip->i_df.if_nextents > 0 && 362 ip->i_df.if_bytes > 0) { 363 struct xfs_bmbt_rec *p; 364 365 ASSERT(xfs_iext_count(&ip->i_df) > 0); 366 367 p = xlog_prepare_iovec(lv, vecp, XLOG_REG_TYPE_IEXT); 368 data_bytes = xfs_iextents_copy(ip, p, XFS_DATA_FORK); 369 xlog_finish_iovec(lv, *vecp, data_bytes); 370 371 ASSERT(data_bytes <= ip->i_df.if_bytes); 372 373 ilf->ilf_dsize = data_bytes; 374 ilf->ilf_size++; 375 } else { 376 iip->ili_fields &= ~XFS_ILOG_DEXT; 377 } 378 break; 379 case XFS_DINODE_FMT_BTREE: 380 case XFS_DINODE_FMT_META_BTREE: 381 iip->ili_fields &= 382 ~(XFS_ILOG_DDATA | XFS_ILOG_DEXT | XFS_ILOG_DEV); 383 384 if ((iip->ili_fields & XFS_ILOG_DBROOT) && 385 ip->i_df.if_broot_bytes > 0) { 386 ASSERT(ip->i_df.if_broot != NULL); 387 xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_IBROOT, 388 ip->i_df.if_broot, 389 ip->i_df.if_broot_bytes); 390 ilf->ilf_dsize = ip->i_df.if_broot_bytes; 391 ilf->ilf_size++; 392 } else { 393 ASSERT(!(iip->ili_fields & 394 XFS_ILOG_DBROOT)); 395 iip->ili_fields &= ~XFS_ILOG_DBROOT; 396 } 397 break; 398 case XFS_DINODE_FMT_LOCAL: 399 iip->ili_fields &= 400 ~(XFS_ILOG_DEXT | XFS_ILOG_DBROOT | XFS_ILOG_DEV); 401 if ((iip->ili_fields & XFS_ILOG_DDATA) && 402 ip->i_df.if_bytes > 0) { 403 ASSERT(ip->i_df.if_data != NULL); 404 ASSERT(ip->i_disk_size > 0); 405 xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_ILOCAL, 406 ip->i_df.if_data, ip->i_df.if_bytes); 407 ilf->ilf_dsize = (unsigned)ip->i_df.if_bytes; 408 ilf->ilf_size++; 409 } else { 410 iip->ili_fields &= ~XFS_ILOG_DDATA; 411 } 412 break; 413 case XFS_DINODE_FMT_DEV: 414 iip->ili_fields &= 415 ~(XFS_ILOG_DDATA | XFS_ILOG_DBROOT | XFS_ILOG_DEXT); 416 if (iip->ili_fields & XFS_ILOG_DEV) 417 ilf->ilf_u.ilfu_rdev = sysv_encode_dev(VFS_I(ip)->i_rdev); 418 break; 419 default: 420 ASSERT(0); 421 break; 422 } 423 } 424 425 STATIC void 426 xfs_inode_item_format_attr_fork( 427 struct xfs_inode_log_item *iip, 428 struct xfs_inode_log_format *ilf, 429 struct xfs_log_vec *lv, 430 struct xfs_log_iovec **vecp) 431 { 432 struct xfs_inode *ip = iip->ili_inode; 433 size_t data_bytes; 434 435 switch (ip->i_af.if_format) { 436 case XFS_DINODE_FMT_EXTENTS: 437 iip->ili_fields &= 438 ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT); 439 440 if ((iip->ili_fields & XFS_ILOG_AEXT) && 441 ip->i_af.if_nextents > 0 && 442 ip->i_af.if_bytes > 0) { 443 struct xfs_bmbt_rec *p; 444 445 ASSERT(xfs_iext_count(&ip->i_af) == 446 ip->i_af.if_nextents); 447 448 p = xlog_prepare_iovec(lv, vecp, XLOG_REG_TYPE_IATTR_EXT); 449 data_bytes = xfs_iextents_copy(ip, p, XFS_ATTR_FORK); 450 xlog_finish_iovec(lv, *vecp, data_bytes); 451 452 ilf->ilf_asize = data_bytes; 453 ilf->ilf_size++; 454 } else { 455 iip->ili_fields &= ~XFS_ILOG_AEXT; 456 } 457 break; 458 case XFS_DINODE_FMT_BTREE: 459 iip->ili_fields &= 460 ~(XFS_ILOG_ADATA | XFS_ILOG_AEXT); 461 462 if ((iip->ili_fields & XFS_ILOG_ABROOT) && 463 ip->i_af.if_broot_bytes > 0) { 464 ASSERT(ip->i_af.if_broot != NULL); 465 466 xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_IATTR_BROOT, 467 ip->i_af.if_broot, 468 ip->i_af.if_broot_bytes); 469 ilf->ilf_asize = ip->i_af.if_broot_bytes; 470 ilf->ilf_size++; 471 } else { 472 iip->ili_fields &= ~XFS_ILOG_ABROOT; 473 } 474 break; 475 case XFS_DINODE_FMT_LOCAL: 476 iip->ili_fields &= 477 ~(XFS_ILOG_AEXT | XFS_ILOG_ABROOT); 478 479 if ((iip->ili_fields & XFS_ILOG_ADATA) && 480 ip->i_af.if_bytes > 0) { 481 ASSERT(ip->i_af.if_data != NULL); 482 xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_IATTR_LOCAL, 483 ip->i_af.if_data, ip->i_af.if_bytes); 484 ilf->ilf_asize = (unsigned)ip->i_af.if_bytes; 485 ilf->ilf_size++; 486 } else { 487 iip->ili_fields &= ~XFS_ILOG_ADATA; 488 } 489 break; 490 default: 491 ASSERT(0); 492 break; 493 } 494 } 495 496 /* 497 * Convert an incore timestamp to a log timestamp. Note that the log format 498 * specifies host endian format! 499 */ 500 static inline xfs_log_timestamp_t 501 xfs_inode_to_log_dinode_ts( 502 struct xfs_inode *ip, 503 const struct timespec64 tv) 504 { 505 struct xfs_log_legacy_timestamp *lits; 506 xfs_log_timestamp_t its; 507 508 if (xfs_inode_has_bigtime(ip)) 509 return xfs_inode_encode_bigtime(tv); 510 511 lits = (struct xfs_log_legacy_timestamp *)&its; 512 lits->t_sec = tv.tv_sec; 513 lits->t_nsec = tv.tv_nsec; 514 515 return its; 516 } 517 518 /* 519 * The legacy DMAPI fields are only present in the on-disk and in-log inodes, 520 * but not in the in-memory one. But we are guaranteed to have an inode buffer 521 * in memory when logging an inode, so we can just copy it from the on-disk 522 * inode to the in-log inode here so that recovery of file system with these 523 * fields set to non-zero values doesn't lose them. For all other cases we zero 524 * the fields. 525 */ 526 static void 527 xfs_copy_dm_fields_to_log_dinode( 528 struct xfs_inode *ip, 529 struct xfs_log_dinode *to) 530 { 531 struct xfs_dinode *dip; 532 533 dip = xfs_buf_offset(ip->i_itemp->ili_item.li_buf, 534 ip->i_imap.im_boffset); 535 536 if (xfs_iflags_test(ip, XFS_IPRESERVE_DM_FIELDS)) { 537 to->di_dmevmask = be32_to_cpu(dip->di_dmevmask); 538 to->di_dmstate = be16_to_cpu(dip->di_dmstate); 539 } else { 540 to->di_dmevmask = 0; 541 to->di_dmstate = 0; 542 } 543 } 544 545 static inline void 546 xfs_inode_to_log_dinode_iext_counters( 547 struct xfs_inode *ip, 548 struct xfs_log_dinode *to) 549 { 550 if (xfs_inode_has_large_extent_counts(ip)) { 551 to->di_big_nextents = xfs_ifork_nextents(&ip->i_df); 552 to->di_big_anextents = xfs_ifork_nextents(&ip->i_af); 553 to->di_nrext64_pad = 0; 554 } else { 555 to->di_nextents = xfs_ifork_nextents(&ip->i_df); 556 to->di_anextents = xfs_ifork_nextents(&ip->i_af); 557 } 558 } 559 560 static void 561 xfs_inode_to_log_dinode( 562 struct xfs_inode *ip, 563 struct xfs_log_dinode *to, 564 xfs_lsn_t lsn) 565 { 566 struct inode *inode = VFS_I(ip); 567 568 to->di_magic = XFS_DINODE_MAGIC; 569 to->di_format = xfs_ifork_format(&ip->i_df); 570 to->di_uid = i_uid_read(inode); 571 to->di_gid = i_gid_read(inode); 572 to->di_projid_lo = ip->i_projid & 0xffff; 573 to->di_projid_hi = ip->i_projid >> 16; 574 575 to->di_atime = xfs_inode_to_log_dinode_ts(ip, inode_get_atime(inode)); 576 to->di_mtime = xfs_inode_to_log_dinode_ts(ip, inode_get_mtime(inode)); 577 to->di_ctime = xfs_inode_to_log_dinode_ts(ip, inode_get_ctime(inode)); 578 to->di_nlink = inode->i_nlink; 579 to->di_gen = inode->i_generation; 580 to->di_mode = inode->i_mode; 581 582 to->di_size = ip->i_disk_size; 583 to->di_nblocks = ip->i_nblocks; 584 to->di_extsize = ip->i_extsize; 585 to->di_forkoff = ip->i_forkoff; 586 to->di_aformat = xfs_ifork_format(&ip->i_af); 587 to->di_flags = ip->i_diflags; 588 589 xfs_copy_dm_fields_to_log_dinode(ip, to); 590 591 /* log a dummy value to ensure log structure is fully initialised */ 592 to->di_next_unlinked = NULLAGINO; 593 594 if (xfs_has_v3inodes(ip->i_mount)) { 595 to->di_version = 3; 596 to->di_changecount = inode_peek_iversion(inode); 597 to->di_crtime = xfs_inode_to_log_dinode_ts(ip, ip->i_crtime); 598 to->di_flags2 = ip->i_diflags2; 599 /* also covers the di_used_blocks union arm: */ 600 to->di_cowextsize = ip->i_cowextsize; 601 to->di_ino = ip->i_ino; 602 to->di_lsn = lsn; 603 memset(to->di_pad2, 0, sizeof(to->di_pad2)); 604 uuid_copy(&to->di_uuid, &ip->i_mount->m_sb.sb_meta_uuid); 605 to->di_v3_pad = 0; 606 607 /* dummy value for initialisation */ 608 to->di_crc = 0; 609 610 if (xfs_is_metadir_inode(ip)) 611 to->di_metatype = ip->i_metatype; 612 else 613 to->di_metatype = 0; 614 } else { 615 to->di_version = 2; 616 to->di_flushiter = ip->i_flushiter; 617 memset(to->di_v2_pad, 0, sizeof(to->di_v2_pad)); 618 to->di_metatype = 0; 619 } 620 621 xfs_inode_to_log_dinode_iext_counters(ip, to); 622 } 623 624 /* 625 * Format the inode core. Current timestamp data is only in the VFS inode 626 * fields, so we need to grab them from there. Hence rather than just copying 627 * the XFS inode core structure, format the fields directly into the iovec. 628 */ 629 static void 630 xfs_inode_item_format_core( 631 struct xfs_inode *ip, 632 struct xfs_log_vec *lv, 633 struct xfs_log_iovec **vecp) 634 { 635 struct xfs_log_dinode *dic; 636 637 dic = xlog_prepare_iovec(lv, vecp, XLOG_REG_TYPE_ICORE); 638 xfs_inode_to_log_dinode(ip, dic, ip->i_itemp->ili_item.li_lsn); 639 xlog_finish_iovec(lv, *vecp, xfs_log_dinode_size(ip->i_mount)); 640 } 641 642 /* 643 * This is called to fill in the vector of log iovecs for the given inode 644 * log item. It fills the first item with an inode log format structure, 645 * the second with the on-disk inode structure, and a possible third and/or 646 * fourth with the inode data/extents/b-tree root and inode attributes 647 * data/extents/b-tree root. 648 * 649 * Note: Always use the 64 bit inode log format structure so we don't 650 * leave an uninitialised hole in the format item on 64 bit systems. Log 651 * recovery on 32 bit systems handles this just fine, so there's no reason 652 * for not using an initialising the properly padded structure all the time. 653 */ 654 STATIC void 655 xfs_inode_item_format( 656 struct xfs_log_item *lip, 657 struct xfs_log_vec *lv) 658 { 659 struct xfs_inode_log_item *iip = INODE_ITEM(lip); 660 struct xfs_inode *ip = iip->ili_inode; 661 struct xfs_log_iovec *vecp = NULL; 662 struct xfs_inode_log_format *ilf; 663 664 ilf = xlog_prepare_iovec(lv, &vecp, XLOG_REG_TYPE_IFORMAT); 665 ilf->ilf_type = XFS_LI_INODE; 666 ilf->ilf_ino = ip->i_ino; 667 ilf->ilf_blkno = ip->i_imap.im_blkno; 668 ilf->ilf_len = ip->i_imap.im_len; 669 ilf->ilf_boffset = ip->i_imap.im_boffset; 670 ilf->ilf_fields = XFS_ILOG_CORE; 671 ilf->ilf_size = 2; /* format + core */ 672 673 /* 674 * make sure we don't leak uninitialised data into the log in the case 675 * when we don't log every field in the inode. 676 */ 677 ilf->ilf_dsize = 0; 678 ilf->ilf_asize = 0; 679 ilf->ilf_pad = 0; 680 memset(&ilf->ilf_u, 0, sizeof(ilf->ilf_u)); 681 682 xlog_finish_iovec(lv, vecp, sizeof(*ilf)); 683 684 xfs_inode_item_format_core(ip, lv, &vecp); 685 xfs_inode_item_format_data_fork(iip, ilf, lv, &vecp); 686 if (xfs_inode_has_attr_fork(ip)) { 687 xfs_inode_item_format_attr_fork(iip, ilf, lv, &vecp); 688 } else { 689 iip->ili_fields &= 690 ~(XFS_ILOG_ADATA | XFS_ILOG_ABROOT | XFS_ILOG_AEXT); 691 } 692 693 /* update the format with the exact fields we actually logged */ 694 ilf->ilf_fields |= (iip->ili_fields & ~XFS_ILOG_TIMESTAMP); 695 } 696 697 /* 698 * This is called to pin the inode associated with the inode log 699 * item in memory so it cannot be written out. 700 */ 701 STATIC void 702 xfs_inode_item_pin( 703 struct xfs_log_item *lip) 704 { 705 struct xfs_inode *ip = INODE_ITEM(lip)->ili_inode; 706 707 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL); 708 ASSERT(lip->li_buf); 709 710 trace_xfs_inode_pin(ip, _RET_IP_); 711 atomic_inc(&ip->i_pincount); 712 } 713 714 715 /* 716 * This is called to unpin the inode associated with the inode log 717 * item which was previously pinned with a call to xfs_inode_item_pin(). 718 * 719 * Also wake up anyone in xfs_iunpin_wait() if the count goes to 0. 720 * 721 * Note that unpin can race with inode cluster buffer freeing marking the buffer 722 * stale. In that case, flush completions are run from the buffer unpin call, 723 * which may happen before the inode is unpinned. If we lose the race, there 724 * will be no buffer attached to the log item, but the inode will be marked 725 * XFS_ISTALE. 726 */ 727 STATIC void 728 xfs_inode_item_unpin( 729 struct xfs_log_item *lip, 730 int remove) 731 { 732 struct xfs_inode *ip = INODE_ITEM(lip)->ili_inode; 733 734 trace_xfs_inode_unpin(ip, _RET_IP_); 735 ASSERT(lip->li_buf || xfs_iflags_test(ip, XFS_ISTALE)); 736 ASSERT(atomic_read(&ip->i_pincount) > 0); 737 if (atomic_dec_and_test(&ip->i_pincount)) 738 wake_up_bit(&ip->i_flags, __XFS_IPINNED_BIT); 739 } 740 741 STATIC uint 742 xfs_inode_item_push( 743 struct xfs_log_item *lip, 744 struct list_head *buffer_list) 745 __releases(&lip->li_ailp->ail_lock) 746 __acquires(&lip->li_ailp->ail_lock) 747 { 748 struct xfs_inode_log_item *iip = INODE_ITEM(lip); 749 struct xfs_inode *ip = iip->ili_inode; 750 struct xfs_buf *bp = lip->li_buf; 751 uint rval = XFS_ITEM_SUCCESS; 752 int error; 753 754 if (!bp || (ip->i_flags & XFS_ISTALE)) { 755 /* 756 * Inode item/buffer is being aborted due to cluster 757 * buffer deletion. Trigger a log force to have that operation 758 * completed and items removed from the AIL before the next push 759 * attempt. 760 */ 761 return XFS_ITEM_PINNED; 762 } 763 764 if (xfs_ipincount(ip) > 0 || xfs_buf_ispinned(bp)) 765 return XFS_ITEM_PINNED; 766 767 if (xfs_iflags_test(ip, XFS_IFLUSHING)) 768 return XFS_ITEM_FLUSHING; 769 770 if (!xfs_buf_trylock(bp)) 771 return XFS_ITEM_LOCKED; 772 773 spin_unlock(&lip->li_ailp->ail_lock); 774 775 /* 776 * We need to hold a reference for flushing the cluster buffer as it may 777 * fail the buffer without IO submission. In which case, we better get a 778 * reference for that completion because otherwise we don't get a 779 * reference for IO until we queue the buffer for delwri submission. 780 */ 781 xfs_buf_hold(bp); 782 error = xfs_iflush_cluster(bp); 783 if (!error) { 784 if (!xfs_buf_delwri_queue(bp, buffer_list)) 785 rval = XFS_ITEM_FLUSHING; 786 xfs_buf_relse(bp); 787 } else { 788 /* 789 * Release the buffer if we were unable to flush anything. On 790 * any other error, the buffer has already been released. 791 */ 792 if (error == -EAGAIN) 793 xfs_buf_relse(bp); 794 rval = XFS_ITEM_LOCKED; 795 } 796 797 spin_lock(&lip->li_ailp->ail_lock); 798 return rval; 799 } 800 801 /* 802 * Unlock the inode associated with the inode log item. 803 */ 804 STATIC void 805 xfs_inode_item_release( 806 struct xfs_log_item *lip) 807 { 808 struct xfs_inode_log_item *iip = INODE_ITEM(lip); 809 struct xfs_inode *ip = iip->ili_inode; 810 unsigned short lock_flags; 811 812 ASSERT(ip->i_itemp != NULL); 813 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL); 814 815 lock_flags = iip->ili_lock_flags; 816 iip->ili_lock_flags = 0; 817 if (lock_flags) 818 xfs_iunlock(ip, lock_flags); 819 } 820 821 /* 822 * This is called to find out where the oldest active copy of the inode log 823 * item in the on disk log resides now that the last log write of it completed 824 * at the given lsn. Since we always re-log all dirty data in an inode, the 825 * latest copy in the on disk log is the only one that matters. Therefore, 826 * simply return the given lsn. 827 * 828 * If the inode has been marked stale because the cluster is being freed, we 829 * don't want to (re-)insert this inode into the AIL. There is a race condition 830 * where the cluster buffer may be unpinned before the inode is inserted into 831 * the AIL during transaction committed processing. If the buffer is unpinned 832 * before the inode item has been committed and inserted, then it is possible 833 * for the buffer to be written and IO completes before the inode is inserted 834 * into the AIL. In that case, we'd be inserting a clean, stale inode into the 835 * AIL which will never get removed. It will, however, get reclaimed which 836 * triggers an assert in xfs_inode_free() complaining about freein an inode 837 * still in the AIL. 838 * 839 * To avoid this, just unpin the inode directly and return a LSN of -1 so the 840 * transaction committed code knows that it does not need to do any further 841 * processing on the item. 842 */ 843 STATIC xfs_lsn_t 844 xfs_inode_item_committed( 845 struct xfs_log_item *lip, 846 xfs_lsn_t lsn) 847 { 848 struct xfs_inode_log_item *iip = INODE_ITEM(lip); 849 struct xfs_inode *ip = iip->ili_inode; 850 851 if (xfs_iflags_test(ip, XFS_ISTALE)) { 852 xfs_inode_item_unpin(lip, 0); 853 return -1; 854 } 855 return lsn; 856 } 857 858 STATIC void 859 xfs_inode_item_committing( 860 struct xfs_log_item *lip, 861 xfs_csn_t seq) 862 { 863 INODE_ITEM(lip)->ili_commit_seq = seq; 864 return xfs_inode_item_release(lip); 865 } 866 867 static const struct xfs_item_ops xfs_inode_item_ops = { 868 .iop_sort = xfs_inode_item_sort, 869 .iop_precommit = xfs_inode_item_precommit, 870 .iop_size = xfs_inode_item_size, 871 .iop_format = xfs_inode_item_format, 872 .iop_pin = xfs_inode_item_pin, 873 .iop_unpin = xfs_inode_item_unpin, 874 .iop_release = xfs_inode_item_release, 875 .iop_committed = xfs_inode_item_committed, 876 .iop_push = xfs_inode_item_push, 877 .iop_committing = xfs_inode_item_committing, 878 }; 879 880 881 /* 882 * Initialize the inode log item for a newly allocated (in-core) inode. 883 */ 884 void 885 xfs_inode_item_init( 886 struct xfs_inode *ip, 887 struct xfs_mount *mp) 888 { 889 struct xfs_inode_log_item *iip; 890 891 ASSERT(ip->i_itemp == NULL); 892 iip = ip->i_itemp = kmem_cache_zalloc(xfs_ili_cache, 893 GFP_KERNEL | __GFP_NOFAIL); 894 895 iip->ili_inode = ip; 896 spin_lock_init(&iip->ili_lock); 897 xfs_log_item_init(mp, &iip->ili_item, XFS_LI_INODE, 898 &xfs_inode_item_ops); 899 } 900 901 /* 902 * Free the inode log item and any memory hanging off of it. 903 */ 904 void 905 xfs_inode_item_destroy( 906 struct xfs_inode *ip) 907 { 908 struct xfs_inode_log_item *iip = ip->i_itemp; 909 910 ASSERT(iip->ili_item.li_buf == NULL); 911 912 ip->i_itemp = NULL; 913 kvfree(iip->ili_item.li_lv_shadow); 914 kmem_cache_free(xfs_ili_cache, iip); 915 } 916 917 918 /* 919 * We only want to pull the item from the AIL if it is actually there 920 * and its location in the log has not changed since we started the 921 * flush. Thus, we only bother if the inode's lsn has not changed. 922 */ 923 static void 924 xfs_iflush_ail_updates( 925 struct xfs_ail *ailp, 926 struct list_head *list) 927 { 928 struct xfs_log_item *lip; 929 xfs_lsn_t tail_lsn = 0; 930 931 /* this is an opencoded batch version of xfs_trans_ail_delete */ 932 spin_lock(&ailp->ail_lock); 933 list_for_each_entry(lip, list, li_bio_list) { 934 xfs_lsn_t lsn; 935 936 clear_bit(XFS_LI_FAILED, &lip->li_flags); 937 if (INODE_ITEM(lip)->ili_flush_lsn != lip->li_lsn) 938 continue; 939 940 /* 941 * dgc: Not sure how this happens, but it happens very 942 * occassionaly via generic/388. xfs_iflush_abort() also 943 * silently handles this same "under writeback but not in AIL at 944 * shutdown" condition via xfs_trans_ail_delete(). 945 */ 946 if (!test_bit(XFS_LI_IN_AIL, &lip->li_flags)) { 947 ASSERT(xlog_is_shutdown(lip->li_log)); 948 continue; 949 } 950 951 lsn = xfs_ail_delete_one(ailp, lip); 952 if (!tail_lsn && lsn) 953 tail_lsn = lsn; 954 } 955 xfs_ail_update_finish(ailp, tail_lsn); 956 } 957 958 /* 959 * Walk the list of inodes that have completed their IOs. If they are clean 960 * remove them from the list and dissociate them from the buffer. Buffers that 961 * are still dirty remain linked to the buffer and on the list. Caller must 962 * handle them appropriately. 963 */ 964 static void 965 xfs_iflush_finish( 966 struct xfs_buf *bp, 967 struct list_head *list) 968 { 969 struct xfs_log_item *lip, *n; 970 971 list_for_each_entry_safe(lip, n, list, li_bio_list) { 972 struct xfs_inode_log_item *iip = INODE_ITEM(lip); 973 bool drop_buffer = false; 974 975 spin_lock(&iip->ili_lock); 976 977 /* 978 * Remove the reference to the cluster buffer if the inode is 979 * clean in memory and drop the buffer reference once we've 980 * dropped the locks we hold. 981 */ 982 ASSERT(iip->ili_item.li_buf == bp); 983 if (!iip->ili_fields) { 984 iip->ili_item.li_buf = NULL; 985 list_del_init(&lip->li_bio_list); 986 drop_buffer = true; 987 } 988 iip->ili_last_fields = 0; 989 iip->ili_flush_lsn = 0; 990 clear_bit(XFS_LI_FLUSHING, &lip->li_flags); 991 spin_unlock(&iip->ili_lock); 992 xfs_iflags_clear(iip->ili_inode, XFS_IFLUSHING); 993 if (drop_buffer) 994 xfs_buf_rele(bp); 995 } 996 } 997 998 /* 999 * Inode buffer IO completion routine. It is responsible for removing inodes 1000 * attached to the buffer from the AIL if they have not been re-logged and 1001 * completing the inode flush. 1002 */ 1003 void 1004 xfs_buf_inode_iodone( 1005 struct xfs_buf *bp) 1006 { 1007 struct xfs_log_item *lip, *n; 1008 LIST_HEAD(flushed_inodes); 1009 LIST_HEAD(ail_updates); 1010 1011 /* 1012 * Pull the attached inodes from the buffer one at a time and take the 1013 * appropriate action on them. 1014 */ 1015 list_for_each_entry_safe(lip, n, &bp->b_li_list, li_bio_list) { 1016 struct xfs_inode_log_item *iip = INODE_ITEM(lip); 1017 1018 if (xfs_iflags_test(iip->ili_inode, XFS_ISTALE)) { 1019 xfs_iflush_abort(iip->ili_inode); 1020 continue; 1021 } 1022 if (!iip->ili_last_fields) 1023 continue; 1024 1025 /* Do an unlocked check for needing the AIL lock. */ 1026 if (iip->ili_flush_lsn == lip->li_lsn || 1027 test_bit(XFS_LI_FAILED, &lip->li_flags)) 1028 list_move_tail(&lip->li_bio_list, &ail_updates); 1029 else 1030 list_move_tail(&lip->li_bio_list, &flushed_inodes); 1031 } 1032 1033 if (!list_empty(&ail_updates)) { 1034 xfs_iflush_ail_updates(bp->b_mount->m_ail, &ail_updates); 1035 list_splice_tail(&ail_updates, &flushed_inodes); 1036 } 1037 1038 xfs_iflush_finish(bp, &flushed_inodes); 1039 if (!list_empty(&flushed_inodes)) 1040 list_splice_tail(&flushed_inodes, &bp->b_li_list); 1041 } 1042 1043 /* 1044 * Clear the inode logging fields so no more flushes are attempted. If we are 1045 * on a buffer list, it is now safe to remove it because the buffer is 1046 * guaranteed to be locked. The caller will drop the reference to the buffer 1047 * the log item held. 1048 */ 1049 static void 1050 xfs_iflush_abort_clean( 1051 struct xfs_inode_log_item *iip) 1052 { 1053 iip->ili_last_fields = 0; 1054 iip->ili_fields = 0; 1055 iip->ili_fsync_fields = 0; 1056 iip->ili_flush_lsn = 0; 1057 iip->ili_item.li_buf = NULL; 1058 list_del_init(&iip->ili_item.li_bio_list); 1059 clear_bit(XFS_LI_FLUSHING, &iip->ili_item.li_flags); 1060 } 1061 1062 /* 1063 * Abort flushing the inode from a context holding the cluster buffer locked. 1064 * 1065 * This is the normal runtime method of aborting writeback of an inode that is 1066 * attached to a cluster buffer. It occurs when the inode and the backing 1067 * cluster buffer have been freed (i.e. inode is XFS_ISTALE), or when cluster 1068 * flushing or buffer IO completion encounters a log shutdown situation. 1069 * 1070 * If we need to abort inode writeback and we don't already hold the buffer 1071 * locked, call xfs_iflush_shutdown_abort() instead as this should only ever be 1072 * necessary in a shutdown situation. 1073 */ 1074 void 1075 xfs_iflush_abort( 1076 struct xfs_inode *ip) 1077 { 1078 struct xfs_inode_log_item *iip = ip->i_itemp; 1079 struct xfs_buf *bp; 1080 1081 if (!iip) { 1082 /* clean inode, nothing to do */ 1083 xfs_iflags_clear(ip, XFS_IFLUSHING); 1084 return; 1085 } 1086 1087 /* 1088 * Remove the inode item from the AIL before we clear its internal 1089 * state. Whilst the inode is in the AIL, it should have a valid buffer 1090 * pointer for push operations to access - it is only safe to remove the 1091 * inode from the buffer once it has been removed from the AIL. 1092 */ 1093 xfs_trans_ail_delete(&iip->ili_item, 0); 1094 1095 /* 1096 * Grab the inode buffer so can we release the reference the inode log 1097 * item holds on it. 1098 */ 1099 spin_lock(&iip->ili_lock); 1100 bp = iip->ili_item.li_buf; 1101 xfs_iflush_abort_clean(iip); 1102 spin_unlock(&iip->ili_lock); 1103 1104 xfs_iflags_clear(ip, XFS_IFLUSHING); 1105 if (bp) 1106 xfs_buf_rele(bp); 1107 } 1108 1109 /* 1110 * Abort an inode flush in the case of a shutdown filesystem. This can be called 1111 * from anywhere with just an inode reference and does not require holding the 1112 * inode cluster buffer locked. If the inode is attached to a cluster buffer, 1113 * it will grab and lock it safely, then abort the inode flush. 1114 */ 1115 void 1116 xfs_iflush_shutdown_abort( 1117 struct xfs_inode *ip) 1118 { 1119 struct xfs_inode_log_item *iip = ip->i_itemp; 1120 struct xfs_buf *bp; 1121 1122 if (!iip) { 1123 /* clean inode, nothing to do */ 1124 xfs_iflags_clear(ip, XFS_IFLUSHING); 1125 return; 1126 } 1127 1128 spin_lock(&iip->ili_lock); 1129 bp = iip->ili_item.li_buf; 1130 if (!bp) { 1131 spin_unlock(&iip->ili_lock); 1132 xfs_iflush_abort(ip); 1133 return; 1134 } 1135 1136 /* 1137 * We have to take a reference to the buffer so that it doesn't get 1138 * freed when we drop the ili_lock and then wait to lock the buffer. 1139 * We'll clean up the extra reference after we pick up the ili_lock 1140 * again. 1141 */ 1142 xfs_buf_hold(bp); 1143 spin_unlock(&iip->ili_lock); 1144 xfs_buf_lock(bp); 1145 1146 spin_lock(&iip->ili_lock); 1147 if (!iip->ili_item.li_buf) { 1148 /* 1149 * Raced with another removal, hold the only reference 1150 * to bp now. Inode should not be in the AIL now, so just clean 1151 * up and return; 1152 */ 1153 ASSERT(list_empty(&iip->ili_item.li_bio_list)); 1154 ASSERT(!test_bit(XFS_LI_IN_AIL, &iip->ili_item.li_flags)); 1155 xfs_iflush_abort_clean(iip); 1156 spin_unlock(&iip->ili_lock); 1157 xfs_iflags_clear(ip, XFS_IFLUSHING); 1158 xfs_buf_relse(bp); 1159 return; 1160 } 1161 1162 /* 1163 * Got two references to bp. The first will get dropped by 1164 * xfs_iflush_abort() when the item is removed from the buffer list, but 1165 * we can't drop our reference until _abort() returns because we have to 1166 * unlock the buffer as well. Hence we abort and then unlock and release 1167 * our reference to the buffer. 1168 */ 1169 ASSERT(iip->ili_item.li_buf == bp); 1170 spin_unlock(&iip->ili_lock); 1171 xfs_iflush_abort(ip); 1172 xfs_buf_relse(bp); 1173 } 1174 1175 1176 /* 1177 * convert an xfs_inode_log_format struct from the old 32 bit version 1178 * (which can have different field alignments) to the native 64 bit version 1179 */ 1180 int 1181 xfs_inode_item_format_convert( 1182 struct xfs_log_iovec *buf, 1183 struct xfs_inode_log_format *in_f) 1184 { 1185 struct xfs_inode_log_format_32 *in_f32 = buf->i_addr; 1186 1187 if (buf->i_len != sizeof(*in_f32)) { 1188 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, NULL); 1189 return -EFSCORRUPTED; 1190 } 1191 1192 in_f->ilf_type = in_f32->ilf_type; 1193 in_f->ilf_size = in_f32->ilf_size; 1194 in_f->ilf_fields = in_f32->ilf_fields; 1195 in_f->ilf_asize = in_f32->ilf_asize; 1196 in_f->ilf_dsize = in_f32->ilf_dsize; 1197 in_f->ilf_ino = in_f32->ilf_ino; 1198 memcpy(&in_f->ilf_u, &in_f32->ilf_u, sizeof(in_f->ilf_u)); 1199 in_f->ilf_blkno = in_f32->ilf_blkno; 1200 in_f->ilf_len = in_f32->ilf_len; 1201 in_f->ilf_boffset = in_f32->ilf_boffset; 1202 return 0; 1203 } 1204