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 trace_xfs_inode_push_stale(ip, _RET_IP_); 762 return XFS_ITEM_PINNED; 763 } 764 765 if (xfs_ipincount(ip) > 0 || xfs_buf_ispinned(bp)) { 766 trace_xfs_inode_push_pinned(ip, _RET_IP_); 767 return XFS_ITEM_PINNED; 768 } 769 770 if (xfs_iflags_test(ip, XFS_IFLUSHING)) 771 return XFS_ITEM_FLUSHING; 772 773 if (!xfs_buf_trylock(bp)) 774 return XFS_ITEM_LOCKED; 775 776 spin_unlock(&lip->li_ailp->ail_lock); 777 778 /* 779 * We need to hold a reference for flushing the cluster buffer as it may 780 * fail the buffer without IO submission. In which case, we better get a 781 * reference for that completion because otherwise we don't get a 782 * reference for IO until we queue the buffer for delwri submission. 783 */ 784 xfs_buf_hold(bp); 785 error = xfs_iflush_cluster(bp); 786 if (!error) { 787 if (!xfs_buf_delwri_queue(bp, buffer_list)) 788 rval = XFS_ITEM_FLUSHING; 789 xfs_buf_relse(bp); 790 } else { 791 /* 792 * Release the buffer if we were unable to flush anything. On 793 * any other error, the buffer has already been released. 794 */ 795 if (error == -EAGAIN) 796 xfs_buf_relse(bp); 797 rval = XFS_ITEM_LOCKED; 798 } 799 800 spin_lock(&lip->li_ailp->ail_lock); 801 return rval; 802 } 803 804 /* 805 * Unlock the inode associated with the inode log item. 806 */ 807 STATIC void 808 xfs_inode_item_release( 809 struct xfs_log_item *lip) 810 { 811 struct xfs_inode_log_item *iip = INODE_ITEM(lip); 812 struct xfs_inode *ip = iip->ili_inode; 813 unsigned short lock_flags; 814 815 ASSERT(ip->i_itemp != NULL); 816 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL); 817 818 lock_flags = iip->ili_lock_flags; 819 iip->ili_lock_flags = 0; 820 if (lock_flags) 821 xfs_iunlock(ip, lock_flags); 822 } 823 824 /* 825 * This is called to find out where the oldest active copy of the inode log 826 * item in the on disk log resides now that the last log write of it completed 827 * at the given lsn. Since we always re-log all dirty data in an inode, the 828 * latest copy in the on disk log is the only one that matters. Therefore, 829 * simply return the given lsn. 830 * 831 * If the inode has been marked stale because the cluster is being freed, we 832 * don't want to (re-)insert this inode into the AIL. There is a race condition 833 * where the cluster buffer may be unpinned before the inode is inserted into 834 * the AIL during transaction committed processing. If the buffer is unpinned 835 * before the inode item has been committed and inserted, then it is possible 836 * for the buffer to be written and IO completes before the inode is inserted 837 * into the AIL. In that case, we'd be inserting a clean, stale inode into the 838 * AIL which will never get removed. It will, however, get reclaimed which 839 * triggers an assert in xfs_inode_free() complaining about freein an inode 840 * still in the AIL. 841 * 842 * To avoid this, just unpin the inode directly and return a LSN of -1 so the 843 * transaction committed code knows that it does not need to do any further 844 * processing on the item. 845 */ 846 STATIC xfs_lsn_t 847 xfs_inode_item_committed( 848 struct xfs_log_item *lip, 849 xfs_lsn_t lsn) 850 { 851 struct xfs_inode_log_item *iip = INODE_ITEM(lip); 852 struct xfs_inode *ip = iip->ili_inode; 853 854 if (xfs_iflags_test(ip, XFS_ISTALE)) { 855 xfs_inode_item_unpin(lip, 0); 856 return -1; 857 } 858 return lsn; 859 } 860 861 STATIC void 862 xfs_inode_item_committing( 863 struct xfs_log_item *lip, 864 xfs_csn_t seq) 865 { 866 INODE_ITEM(lip)->ili_commit_seq = seq; 867 return xfs_inode_item_release(lip); 868 } 869 870 static const struct xfs_item_ops xfs_inode_item_ops = { 871 .iop_sort = xfs_inode_item_sort, 872 .iop_precommit = xfs_inode_item_precommit, 873 .iop_size = xfs_inode_item_size, 874 .iop_format = xfs_inode_item_format, 875 .iop_pin = xfs_inode_item_pin, 876 .iop_unpin = xfs_inode_item_unpin, 877 .iop_release = xfs_inode_item_release, 878 .iop_committed = xfs_inode_item_committed, 879 .iop_push = xfs_inode_item_push, 880 .iop_committing = xfs_inode_item_committing, 881 }; 882 883 884 /* 885 * Initialize the inode log item for a newly allocated (in-core) inode. 886 */ 887 void 888 xfs_inode_item_init( 889 struct xfs_inode *ip, 890 struct xfs_mount *mp) 891 { 892 struct xfs_inode_log_item *iip; 893 894 ASSERT(ip->i_itemp == NULL); 895 iip = ip->i_itemp = kmem_cache_zalloc(xfs_ili_cache, 896 GFP_KERNEL | __GFP_NOFAIL); 897 898 iip->ili_inode = ip; 899 spin_lock_init(&iip->ili_lock); 900 xfs_log_item_init(mp, &iip->ili_item, XFS_LI_INODE, 901 &xfs_inode_item_ops); 902 } 903 904 /* 905 * Free the inode log item and any memory hanging off of it. 906 */ 907 void 908 xfs_inode_item_destroy( 909 struct xfs_inode *ip) 910 { 911 struct xfs_inode_log_item *iip = ip->i_itemp; 912 913 ASSERT(iip->ili_item.li_buf == NULL); 914 915 ip->i_itemp = NULL; 916 kvfree(iip->ili_item.li_lv_shadow); 917 kmem_cache_free(xfs_ili_cache, iip); 918 } 919 920 921 /* 922 * We only want to pull the item from the AIL if it is actually there 923 * and its location in the log has not changed since we started the 924 * flush. Thus, we only bother if the inode's lsn has not changed. 925 */ 926 static void 927 xfs_iflush_ail_updates( 928 struct xfs_ail *ailp, 929 struct list_head *list) 930 { 931 struct xfs_log_item *lip; 932 xfs_lsn_t tail_lsn = 0; 933 934 /* this is an opencoded batch version of xfs_trans_ail_delete */ 935 spin_lock(&ailp->ail_lock); 936 list_for_each_entry(lip, list, li_bio_list) { 937 xfs_lsn_t lsn; 938 939 clear_bit(XFS_LI_FAILED, &lip->li_flags); 940 if (INODE_ITEM(lip)->ili_flush_lsn != lip->li_lsn) 941 continue; 942 943 /* 944 * dgc: Not sure how this happens, but it happens very 945 * occassionaly via generic/388. xfs_iflush_abort() also 946 * silently handles this same "under writeback but not in AIL at 947 * shutdown" condition via xfs_trans_ail_delete(). 948 */ 949 if (!test_bit(XFS_LI_IN_AIL, &lip->li_flags)) { 950 ASSERT(xlog_is_shutdown(lip->li_log)); 951 continue; 952 } 953 954 lsn = xfs_ail_delete_one(ailp, lip); 955 if (!tail_lsn && lsn) 956 tail_lsn = lsn; 957 } 958 xfs_ail_update_finish(ailp, tail_lsn); 959 } 960 961 /* 962 * Walk the list of inodes that have completed their IOs. If they are clean 963 * remove them from the list and dissociate them from the buffer. Buffers that 964 * are still dirty remain linked to the buffer and on the list. Caller must 965 * handle them appropriately. 966 */ 967 static void 968 xfs_iflush_finish( 969 struct xfs_buf *bp, 970 struct list_head *list) 971 { 972 struct xfs_log_item *lip, *n; 973 974 list_for_each_entry_safe(lip, n, list, li_bio_list) { 975 struct xfs_inode_log_item *iip = INODE_ITEM(lip); 976 bool drop_buffer = false; 977 978 spin_lock(&iip->ili_lock); 979 980 /* 981 * Remove the reference to the cluster buffer if the inode is 982 * clean in memory and drop the buffer reference once we've 983 * dropped the locks we hold. 984 */ 985 ASSERT(iip->ili_item.li_buf == bp); 986 if (!iip->ili_fields) { 987 iip->ili_item.li_buf = NULL; 988 list_del_init(&lip->li_bio_list); 989 drop_buffer = true; 990 } 991 iip->ili_last_fields = 0; 992 iip->ili_flush_lsn = 0; 993 clear_bit(XFS_LI_FLUSHING, &lip->li_flags); 994 spin_unlock(&iip->ili_lock); 995 xfs_iflags_clear(iip->ili_inode, XFS_IFLUSHING); 996 if (drop_buffer) 997 xfs_buf_rele(bp); 998 } 999 } 1000 1001 /* 1002 * Inode buffer IO completion routine. It is responsible for removing inodes 1003 * attached to the buffer from the AIL if they have not been re-logged and 1004 * completing the inode flush. 1005 */ 1006 void 1007 xfs_buf_inode_iodone( 1008 struct xfs_buf *bp) 1009 { 1010 struct xfs_log_item *lip, *n; 1011 LIST_HEAD(flushed_inodes); 1012 LIST_HEAD(ail_updates); 1013 1014 /* 1015 * Pull the attached inodes from the buffer one at a time and take the 1016 * appropriate action on them. 1017 */ 1018 list_for_each_entry_safe(lip, n, &bp->b_li_list, li_bio_list) { 1019 struct xfs_inode_log_item *iip = INODE_ITEM(lip); 1020 1021 if (xfs_iflags_test(iip->ili_inode, XFS_ISTALE)) { 1022 xfs_iflush_abort(iip->ili_inode); 1023 continue; 1024 } 1025 if (!iip->ili_last_fields) 1026 continue; 1027 1028 /* Do an unlocked check for needing the AIL lock. */ 1029 if (iip->ili_flush_lsn == lip->li_lsn || 1030 test_bit(XFS_LI_FAILED, &lip->li_flags)) 1031 list_move_tail(&lip->li_bio_list, &ail_updates); 1032 else 1033 list_move_tail(&lip->li_bio_list, &flushed_inodes); 1034 } 1035 1036 if (!list_empty(&ail_updates)) { 1037 xfs_iflush_ail_updates(bp->b_mount->m_ail, &ail_updates); 1038 list_splice_tail(&ail_updates, &flushed_inodes); 1039 } 1040 1041 xfs_iflush_finish(bp, &flushed_inodes); 1042 if (!list_empty(&flushed_inodes)) 1043 list_splice_tail(&flushed_inodes, &bp->b_li_list); 1044 } 1045 1046 /* 1047 * Clear the inode logging fields so no more flushes are attempted. If we are 1048 * on a buffer list, it is now safe to remove it because the buffer is 1049 * guaranteed to be locked. The caller will drop the reference to the buffer 1050 * the log item held. 1051 */ 1052 static void 1053 xfs_iflush_abort_clean( 1054 struct xfs_inode_log_item *iip) 1055 { 1056 iip->ili_last_fields = 0; 1057 iip->ili_fields = 0; 1058 iip->ili_fsync_fields = 0; 1059 iip->ili_flush_lsn = 0; 1060 iip->ili_item.li_buf = NULL; 1061 list_del_init(&iip->ili_item.li_bio_list); 1062 clear_bit(XFS_LI_FLUSHING, &iip->ili_item.li_flags); 1063 } 1064 1065 /* 1066 * Abort flushing the inode from a context holding the cluster buffer locked. 1067 * 1068 * This is the normal runtime method of aborting writeback of an inode that is 1069 * attached to a cluster buffer. It occurs when the inode and the backing 1070 * cluster buffer have been freed (i.e. inode is XFS_ISTALE), or when cluster 1071 * flushing or buffer IO completion encounters a log shutdown situation. 1072 * 1073 * If we need to abort inode writeback and we don't already hold the buffer 1074 * locked, call xfs_iflush_shutdown_abort() instead as this should only ever be 1075 * necessary in a shutdown situation. 1076 */ 1077 void 1078 xfs_iflush_abort( 1079 struct xfs_inode *ip) 1080 { 1081 struct xfs_inode_log_item *iip = ip->i_itemp; 1082 struct xfs_buf *bp; 1083 1084 if (!iip) { 1085 /* clean inode, nothing to do */ 1086 xfs_iflags_clear(ip, XFS_IFLUSHING); 1087 return; 1088 } 1089 1090 /* 1091 * Remove the inode item from the AIL before we clear its internal 1092 * state. Whilst the inode is in the AIL, it should have a valid buffer 1093 * pointer for push operations to access - it is only safe to remove the 1094 * inode from the buffer once it has been removed from the AIL. 1095 */ 1096 xfs_trans_ail_delete(&iip->ili_item, 0); 1097 1098 /* 1099 * Grab the inode buffer so can we release the reference the inode log 1100 * item holds on it. 1101 */ 1102 spin_lock(&iip->ili_lock); 1103 bp = iip->ili_item.li_buf; 1104 xfs_iflush_abort_clean(iip); 1105 spin_unlock(&iip->ili_lock); 1106 1107 xfs_iflags_clear(ip, XFS_IFLUSHING); 1108 if (bp) 1109 xfs_buf_rele(bp); 1110 } 1111 1112 /* 1113 * Abort an inode flush in the case of a shutdown filesystem. This can be called 1114 * from anywhere with just an inode reference and does not require holding the 1115 * inode cluster buffer locked. If the inode is attached to a cluster buffer, 1116 * it will grab and lock it safely, then abort the inode flush. 1117 */ 1118 void 1119 xfs_iflush_shutdown_abort( 1120 struct xfs_inode *ip) 1121 { 1122 struct xfs_inode_log_item *iip = ip->i_itemp; 1123 struct xfs_buf *bp; 1124 1125 if (!iip) { 1126 /* clean inode, nothing to do */ 1127 xfs_iflags_clear(ip, XFS_IFLUSHING); 1128 return; 1129 } 1130 1131 spin_lock(&iip->ili_lock); 1132 bp = iip->ili_item.li_buf; 1133 if (!bp) { 1134 spin_unlock(&iip->ili_lock); 1135 xfs_iflush_abort(ip); 1136 return; 1137 } 1138 1139 /* 1140 * We have to take a reference to the buffer so that it doesn't get 1141 * freed when we drop the ili_lock and then wait to lock the buffer. 1142 * We'll clean up the extra reference after we pick up the ili_lock 1143 * again. 1144 */ 1145 xfs_buf_hold(bp); 1146 spin_unlock(&iip->ili_lock); 1147 xfs_buf_lock(bp); 1148 1149 spin_lock(&iip->ili_lock); 1150 if (!iip->ili_item.li_buf) { 1151 /* 1152 * Raced with another removal, hold the only reference 1153 * to bp now. Inode should not be in the AIL now, so just clean 1154 * up and return; 1155 */ 1156 ASSERT(list_empty(&iip->ili_item.li_bio_list)); 1157 ASSERT(!test_bit(XFS_LI_IN_AIL, &iip->ili_item.li_flags)); 1158 xfs_iflush_abort_clean(iip); 1159 spin_unlock(&iip->ili_lock); 1160 xfs_iflags_clear(ip, XFS_IFLUSHING); 1161 xfs_buf_relse(bp); 1162 return; 1163 } 1164 1165 /* 1166 * Got two references to bp. The first will get dropped by 1167 * xfs_iflush_abort() when the item is removed from the buffer list, but 1168 * we can't drop our reference until _abort() returns because we have to 1169 * unlock the buffer as well. Hence we abort and then unlock and release 1170 * our reference to the buffer. 1171 */ 1172 ASSERT(iip->ili_item.li_buf == bp); 1173 spin_unlock(&iip->ili_lock); 1174 xfs_iflush_abort(ip); 1175 xfs_buf_relse(bp); 1176 } 1177 1178 1179 /* 1180 * convert an xfs_inode_log_format struct from the old 32 bit version 1181 * (which can have different field alignments) to the native 64 bit version 1182 */ 1183 int 1184 xfs_inode_item_format_convert( 1185 struct kvec *buf, 1186 struct xfs_inode_log_format *in_f) 1187 { 1188 struct xfs_inode_log_format_32 *in_f32 = buf->iov_base; 1189 1190 if (buf->iov_len != sizeof(*in_f32)) { 1191 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, NULL); 1192 return -EFSCORRUPTED; 1193 } 1194 1195 in_f->ilf_type = in_f32->ilf_type; 1196 in_f->ilf_size = in_f32->ilf_size; 1197 in_f->ilf_fields = in_f32->ilf_fields; 1198 in_f->ilf_asize = in_f32->ilf_asize; 1199 in_f->ilf_dsize = in_f32->ilf_dsize; 1200 in_f->ilf_ino = in_f32->ilf_ino; 1201 memcpy(&in_f->ilf_u, &in_f32->ilf_u, sizeof(in_f->ilf_u)); 1202 in_f->ilf_blkno = in_f32->ilf_blkno; 1203 in_f->ilf_len = in_f32->ilf_len; 1204 in_f->ilf_boffset = in_f32->ilf_boffset; 1205 return 0; 1206 } 1207