1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2000-2006 Silicon Graphics, Inc. 4 * All Rights Reserved. 5 */ 6 #include <linux/iversion.h> 7 8 #include "xfs.h" 9 #include "xfs_fs.h" 10 #include "xfs_shared.h" 11 #include "xfs_format.h" 12 #include "xfs_log_format.h" 13 #include "xfs_trans_resv.h" 14 #include "xfs_mount.h" 15 #include "xfs_defer.h" 16 #include "xfs_inode.h" 17 #include "xfs_dir2.h" 18 #include "xfs_attr.h" 19 #include "xfs_trans_space.h" 20 #include "xfs_trans.h" 21 #include "xfs_buf_item.h" 22 #include "xfs_inode_item.h" 23 #include "xfs_iunlink_item.h" 24 #include "xfs_ialloc.h" 25 #include "xfs_bmap.h" 26 #include "xfs_bmap_util.h" 27 #include "xfs_errortag.h" 28 #include "xfs_error.h" 29 #include "xfs_quota.h" 30 #include "xfs_filestream.h" 31 #include "xfs_trace.h" 32 #include "xfs_icache.h" 33 #include "xfs_symlink.h" 34 #include "xfs_trans_priv.h" 35 #include "xfs_log.h" 36 #include "xfs_bmap_btree.h" 37 #include "xfs_reflink.h" 38 #include "xfs_ag.h" 39 #include "xfs_log_priv.h" 40 41 struct kmem_cache *xfs_inode_cache; 42 43 /* 44 * Used in xfs_itruncate_extents(). This is the maximum number of extents 45 * freed from a file in a single transaction. 46 */ 47 #define XFS_ITRUNC_MAX_EXTENTS 2 48 49 STATIC int xfs_iunlink(struct xfs_trans *, struct xfs_inode *); 50 STATIC int xfs_iunlink_remove(struct xfs_trans *tp, struct xfs_perag *pag, 51 struct xfs_inode *); 52 53 /* 54 * helper function to extract extent size hint from inode 55 */ 56 xfs_extlen_t 57 xfs_get_extsz_hint( 58 struct xfs_inode *ip) 59 { 60 /* 61 * No point in aligning allocations if we need to COW to actually 62 * write to them. 63 */ 64 if (xfs_is_always_cow_inode(ip)) 65 return 0; 66 if ((ip->i_diflags & XFS_DIFLAG_EXTSIZE) && ip->i_extsize) 67 return ip->i_extsize; 68 if (XFS_IS_REALTIME_INODE(ip)) 69 return ip->i_mount->m_sb.sb_rextsize; 70 return 0; 71 } 72 73 /* 74 * Helper function to extract CoW extent size hint from inode. 75 * Between the extent size hint and the CoW extent size hint, we 76 * return the greater of the two. If the value is zero (automatic), 77 * use the default size. 78 */ 79 xfs_extlen_t 80 xfs_get_cowextsz_hint( 81 struct xfs_inode *ip) 82 { 83 xfs_extlen_t a, b; 84 85 a = 0; 86 if (ip->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE) 87 a = ip->i_cowextsize; 88 b = xfs_get_extsz_hint(ip); 89 90 a = max(a, b); 91 if (a == 0) 92 return XFS_DEFAULT_COWEXTSZ_HINT; 93 return a; 94 } 95 96 /* 97 * These two are wrapper routines around the xfs_ilock() routine used to 98 * centralize some grungy code. They are used in places that wish to lock the 99 * inode solely for reading the extents. The reason these places can't just 100 * call xfs_ilock(ip, XFS_ILOCK_SHARED) is that the inode lock also guards to 101 * bringing in of the extents from disk for a file in b-tree format. If the 102 * inode is in b-tree format, then we need to lock the inode exclusively until 103 * the extents are read in. Locking it exclusively all the time would limit 104 * our parallelism unnecessarily, though. What we do instead is check to see 105 * if the extents have been read in yet, and only lock the inode exclusively 106 * if they have not. 107 * 108 * The functions return a value which should be given to the corresponding 109 * xfs_iunlock() call. 110 */ 111 uint 112 xfs_ilock_data_map_shared( 113 struct xfs_inode *ip) 114 { 115 uint lock_mode = XFS_ILOCK_SHARED; 116 117 if (xfs_need_iread_extents(&ip->i_df)) 118 lock_mode = XFS_ILOCK_EXCL; 119 xfs_ilock(ip, lock_mode); 120 return lock_mode; 121 } 122 123 uint 124 xfs_ilock_attr_map_shared( 125 struct xfs_inode *ip) 126 { 127 uint lock_mode = XFS_ILOCK_SHARED; 128 129 if (xfs_inode_has_attr_fork(ip) && xfs_need_iread_extents(&ip->i_af)) 130 lock_mode = XFS_ILOCK_EXCL; 131 xfs_ilock(ip, lock_mode); 132 return lock_mode; 133 } 134 135 /* 136 * You can't set both SHARED and EXCL for the same lock, 137 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_MMAPLOCK_SHARED, 138 * XFS_MMAPLOCK_EXCL, XFS_ILOCK_SHARED, XFS_ILOCK_EXCL are valid values 139 * to set in lock_flags. 140 */ 141 static inline void 142 xfs_lock_flags_assert( 143 uint lock_flags) 144 { 145 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) != 146 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)); 147 ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) != 148 (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)); 149 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) != 150 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)); 151 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0); 152 ASSERT(lock_flags != 0); 153 } 154 155 /* 156 * In addition to i_rwsem in the VFS inode, the xfs inode contains 2 157 * multi-reader locks: invalidate_lock and the i_lock. This routine allows 158 * various combinations of the locks to be obtained. 159 * 160 * The 3 locks should always be ordered so that the IO lock is obtained first, 161 * the mmap lock second and the ilock last in order to prevent deadlock. 162 * 163 * Basic locking order: 164 * 165 * i_rwsem -> invalidate_lock -> page_lock -> i_ilock 166 * 167 * mmap_lock locking order: 168 * 169 * i_rwsem -> page lock -> mmap_lock 170 * mmap_lock -> invalidate_lock -> page_lock 171 * 172 * The difference in mmap_lock locking order mean that we cannot hold the 173 * invalidate_lock over syscall based read(2)/write(2) based IO. These IO paths 174 * can fault in pages during copy in/out (for buffered IO) or require the 175 * mmap_lock in get_user_pages() to map the user pages into the kernel address 176 * space for direct IO. Similarly the i_rwsem cannot be taken inside a page 177 * fault because page faults already hold the mmap_lock. 178 * 179 * Hence to serialise fully against both syscall and mmap based IO, we need to 180 * take both the i_rwsem and the invalidate_lock. These locks should *only* be 181 * both taken in places where we need to invalidate the page cache in a race 182 * free manner (e.g. truncate, hole punch and other extent manipulation 183 * functions). 184 */ 185 void 186 xfs_ilock( 187 xfs_inode_t *ip, 188 uint lock_flags) 189 { 190 trace_xfs_ilock(ip, lock_flags, _RET_IP_); 191 192 xfs_lock_flags_assert(lock_flags); 193 194 if (lock_flags & XFS_IOLOCK_EXCL) { 195 down_write_nested(&VFS_I(ip)->i_rwsem, 196 XFS_IOLOCK_DEP(lock_flags)); 197 } else if (lock_flags & XFS_IOLOCK_SHARED) { 198 down_read_nested(&VFS_I(ip)->i_rwsem, 199 XFS_IOLOCK_DEP(lock_flags)); 200 } 201 202 if (lock_flags & XFS_MMAPLOCK_EXCL) { 203 down_write_nested(&VFS_I(ip)->i_mapping->invalidate_lock, 204 XFS_MMAPLOCK_DEP(lock_flags)); 205 } else if (lock_flags & XFS_MMAPLOCK_SHARED) { 206 down_read_nested(&VFS_I(ip)->i_mapping->invalidate_lock, 207 XFS_MMAPLOCK_DEP(lock_flags)); 208 } 209 210 if (lock_flags & XFS_ILOCK_EXCL) 211 mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags)); 212 else if (lock_flags & XFS_ILOCK_SHARED) 213 mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags)); 214 } 215 216 /* 217 * This is just like xfs_ilock(), except that the caller 218 * is guaranteed not to sleep. It returns 1 if it gets 219 * the requested locks and 0 otherwise. If the IO lock is 220 * obtained but the inode lock cannot be, then the IO lock 221 * is dropped before returning. 222 * 223 * ip -- the inode being locked 224 * lock_flags -- this parameter indicates the inode's locks to be 225 * to be locked. See the comment for xfs_ilock() for a list 226 * of valid values. 227 */ 228 int 229 xfs_ilock_nowait( 230 xfs_inode_t *ip, 231 uint lock_flags) 232 { 233 trace_xfs_ilock_nowait(ip, lock_flags, _RET_IP_); 234 235 xfs_lock_flags_assert(lock_flags); 236 237 if (lock_flags & XFS_IOLOCK_EXCL) { 238 if (!down_write_trylock(&VFS_I(ip)->i_rwsem)) 239 goto out; 240 } else if (lock_flags & XFS_IOLOCK_SHARED) { 241 if (!down_read_trylock(&VFS_I(ip)->i_rwsem)) 242 goto out; 243 } 244 245 if (lock_flags & XFS_MMAPLOCK_EXCL) { 246 if (!down_write_trylock(&VFS_I(ip)->i_mapping->invalidate_lock)) 247 goto out_undo_iolock; 248 } else if (lock_flags & XFS_MMAPLOCK_SHARED) { 249 if (!down_read_trylock(&VFS_I(ip)->i_mapping->invalidate_lock)) 250 goto out_undo_iolock; 251 } 252 253 if (lock_flags & XFS_ILOCK_EXCL) { 254 if (!mrtryupdate(&ip->i_lock)) 255 goto out_undo_mmaplock; 256 } else if (lock_flags & XFS_ILOCK_SHARED) { 257 if (!mrtryaccess(&ip->i_lock)) 258 goto out_undo_mmaplock; 259 } 260 return 1; 261 262 out_undo_mmaplock: 263 if (lock_flags & XFS_MMAPLOCK_EXCL) 264 up_write(&VFS_I(ip)->i_mapping->invalidate_lock); 265 else if (lock_flags & XFS_MMAPLOCK_SHARED) 266 up_read(&VFS_I(ip)->i_mapping->invalidate_lock); 267 out_undo_iolock: 268 if (lock_flags & XFS_IOLOCK_EXCL) 269 up_write(&VFS_I(ip)->i_rwsem); 270 else if (lock_flags & XFS_IOLOCK_SHARED) 271 up_read(&VFS_I(ip)->i_rwsem); 272 out: 273 return 0; 274 } 275 276 /* 277 * xfs_iunlock() is used to drop the inode locks acquired with 278 * xfs_ilock() and xfs_ilock_nowait(). The caller must pass 279 * in the flags given to xfs_ilock() or xfs_ilock_nowait() so 280 * that we know which locks to drop. 281 * 282 * ip -- the inode being unlocked 283 * lock_flags -- this parameter indicates the inode's locks to be 284 * to be unlocked. See the comment for xfs_ilock() for a list 285 * of valid values for this parameter. 286 * 287 */ 288 void 289 xfs_iunlock( 290 xfs_inode_t *ip, 291 uint lock_flags) 292 { 293 xfs_lock_flags_assert(lock_flags); 294 295 if (lock_flags & XFS_IOLOCK_EXCL) 296 up_write(&VFS_I(ip)->i_rwsem); 297 else if (lock_flags & XFS_IOLOCK_SHARED) 298 up_read(&VFS_I(ip)->i_rwsem); 299 300 if (lock_flags & XFS_MMAPLOCK_EXCL) 301 up_write(&VFS_I(ip)->i_mapping->invalidate_lock); 302 else if (lock_flags & XFS_MMAPLOCK_SHARED) 303 up_read(&VFS_I(ip)->i_mapping->invalidate_lock); 304 305 if (lock_flags & XFS_ILOCK_EXCL) 306 mrunlock_excl(&ip->i_lock); 307 else if (lock_flags & XFS_ILOCK_SHARED) 308 mrunlock_shared(&ip->i_lock); 309 310 trace_xfs_iunlock(ip, lock_flags, _RET_IP_); 311 } 312 313 /* 314 * give up write locks. the i/o lock cannot be held nested 315 * if it is being demoted. 316 */ 317 void 318 xfs_ilock_demote( 319 xfs_inode_t *ip, 320 uint lock_flags) 321 { 322 ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_MMAPLOCK_EXCL|XFS_ILOCK_EXCL)); 323 ASSERT((lock_flags & 324 ~(XFS_IOLOCK_EXCL|XFS_MMAPLOCK_EXCL|XFS_ILOCK_EXCL)) == 0); 325 326 if (lock_flags & XFS_ILOCK_EXCL) 327 mrdemote(&ip->i_lock); 328 if (lock_flags & XFS_MMAPLOCK_EXCL) 329 downgrade_write(&VFS_I(ip)->i_mapping->invalidate_lock); 330 if (lock_flags & XFS_IOLOCK_EXCL) 331 downgrade_write(&VFS_I(ip)->i_rwsem); 332 333 trace_xfs_ilock_demote(ip, lock_flags, _RET_IP_); 334 } 335 336 #if defined(DEBUG) || defined(XFS_WARN) 337 static inline bool 338 __xfs_rwsem_islocked( 339 struct rw_semaphore *rwsem, 340 bool shared) 341 { 342 if (!debug_locks) 343 return rwsem_is_locked(rwsem); 344 345 if (!shared) 346 return lockdep_is_held_type(rwsem, 0); 347 348 /* 349 * We are checking that the lock is held at least in shared 350 * mode but don't care that it might be held exclusively 351 * (i.e. shared | excl). Hence we check if the lock is held 352 * in any mode rather than an explicit shared mode. 353 */ 354 return lockdep_is_held_type(rwsem, -1); 355 } 356 357 bool 358 xfs_isilocked( 359 struct xfs_inode *ip, 360 uint lock_flags) 361 { 362 if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) { 363 if (!(lock_flags & XFS_ILOCK_SHARED)) 364 return !!ip->i_lock.mr_writer; 365 return rwsem_is_locked(&ip->i_lock.mr_lock); 366 } 367 368 if (lock_flags & (XFS_MMAPLOCK_EXCL|XFS_MMAPLOCK_SHARED)) { 369 return __xfs_rwsem_islocked(&VFS_I(ip)->i_mapping->invalidate_lock, 370 (lock_flags & XFS_MMAPLOCK_SHARED)); 371 } 372 373 if (lock_flags & (XFS_IOLOCK_EXCL | XFS_IOLOCK_SHARED)) { 374 return __xfs_rwsem_islocked(&VFS_I(ip)->i_rwsem, 375 (lock_flags & XFS_IOLOCK_SHARED)); 376 } 377 378 ASSERT(0); 379 return false; 380 } 381 #endif 382 383 /* 384 * xfs_lockdep_subclass_ok() is only used in an ASSERT, so is only called when 385 * DEBUG or XFS_WARN is set. And MAX_LOCKDEP_SUBCLASSES is then only defined 386 * when CONFIG_LOCKDEP is set. Hence the complex define below to avoid build 387 * errors and warnings. 388 */ 389 #if (defined(DEBUG) || defined(XFS_WARN)) && defined(CONFIG_LOCKDEP) 390 static bool 391 xfs_lockdep_subclass_ok( 392 int subclass) 393 { 394 return subclass < MAX_LOCKDEP_SUBCLASSES; 395 } 396 #else 397 #define xfs_lockdep_subclass_ok(subclass) (true) 398 #endif 399 400 /* 401 * Bump the subclass so xfs_lock_inodes() acquires each lock with a different 402 * value. This can be called for any type of inode lock combination, including 403 * parent locking. Care must be taken to ensure we don't overrun the subclass 404 * storage fields in the class mask we build. 405 */ 406 static inline uint 407 xfs_lock_inumorder( 408 uint lock_mode, 409 uint subclass) 410 { 411 uint class = 0; 412 413 ASSERT(!(lock_mode & (XFS_ILOCK_PARENT | XFS_ILOCK_RTBITMAP | 414 XFS_ILOCK_RTSUM))); 415 ASSERT(xfs_lockdep_subclass_ok(subclass)); 416 417 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) { 418 ASSERT(subclass <= XFS_IOLOCK_MAX_SUBCLASS); 419 class += subclass << XFS_IOLOCK_SHIFT; 420 } 421 422 if (lock_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) { 423 ASSERT(subclass <= XFS_MMAPLOCK_MAX_SUBCLASS); 424 class += subclass << XFS_MMAPLOCK_SHIFT; 425 } 426 427 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) { 428 ASSERT(subclass <= XFS_ILOCK_MAX_SUBCLASS); 429 class += subclass << XFS_ILOCK_SHIFT; 430 } 431 432 return (lock_mode & ~XFS_LOCK_SUBCLASS_MASK) | class; 433 } 434 435 /* 436 * The following routine will lock n inodes in exclusive mode. We assume the 437 * caller calls us with the inodes in i_ino order. 438 * 439 * We need to detect deadlock where an inode that we lock is in the AIL and we 440 * start waiting for another inode that is locked by a thread in a long running 441 * transaction (such as truncate). This can result in deadlock since the long 442 * running trans might need to wait for the inode we just locked in order to 443 * push the tail and free space in the log. 444 * 445 * xfs_lock_inodes() can only be used to lock one type of lock at a time - 446 * the iolock, the mmaplock or the ilock, but not more than one at a time. If we 447 * lock more than one at a time, lockdep will report false positives saying we 448 * have violated locking orders. 449 */ 450 static void 451 xfs_lock_inodes( 452 struct xfs_inode **ips, 453 int inodes, 454 uint lock_mode) 455 { 456 int attempts = 0; 457 uint i; 458 int j; 459 bool try_lock; 460 struct xfs_log_item *lp; 461 462 /* 463 * Currently supports between 2 and 5 inodes with exclusive locking. We 464 * support an arbitrary depth of locking here, but absolute limits on 465 * inodes depend on the type of locking and the limits placed by 466 * lockdep annotations in xfs_lock_inumorder. These are all checked by 467 * the asserts. 468 */ 469 ASSERT(ips && inodes >= 2 && inodes <= 5); 470 ASSERT(lock_mode & (XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL | 471 XFS_ILOCK_EXCL)); 472 ASSERT(!(lock_mode & (XFS_IOLOCK_SHARED | XFS_MMAPLOCK_SHARED | 473 XFS_ILOCK_SHARED))); 474 ASSERT(!(lock_mode & XFS_MMAPLOCK_EXCL) || 475 inodes <= XFS_MMAPLOCK_MAX_SUBCLASS + 1); 476 ASSERT(!(lock_mode & XFS_ILOCK_EXCL) || 477 inodes <= XFS_ILOCK_MAX_SUBCLASS + 1); 478 479 if (lock_mode & XFS_IOLOCK_EXCL) { 480 ASSERT(!(lock_mode & (XFS_MMAPLOCK_EXCL | XFS_ILOCK_EXCL))); 481 } else if (lock_mode & XFS_MMAPLOCK_EXCL) 482 ASSERT(!(lock_mode & XFS_ILOCK_EXCL)); 483 484 again: 485 try_lock = false; 486 i = 0; 487 for (; i < inodes; i++) { 488 ASSERT(ips[i]); 489 490 if (i && (ips[i] == ips[i - 1])) /* Already locked */ 491 continue; 492 493 /* 494 * If try_lock is not set yet, make sure all locked inodes are 495 * not in the AIL. If any are, set try_lock to be used later. 496 */ 497 if (!try_lock) { 498 for (j = (i - 1); j >= 0 && !try_lock; j--) { 499 lp = &ips[j]->i_itemp->ili_item; 500 if (lp && test_bit(XFS_LI_IN_AIL, &lp->li_flags)) 501 try_lock = true; 502 } 503 } 504 505 /* 506 * If any of the previous locks we have locked is in the AIL, 507 * we must TRY to get the second and subsequent locks. If 508 * we can't get any, we must release all we have 509 * and try again. 510 */ 511 if (!try_lock) { 512 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i)); 513 continue; 514 } 515 516 /* try_lock means we have an inode locked that is in the AIL. */ 517 ASSERT(i != 0); 518 if (xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) 519 continue; 520 521 /* 522 * Unlock all previous guys and try again. xfs_iunlock will try 523 * to push the tail if the inode is in the AIL. 524 */ 525 attempts++; 526 for (j = i - 1; j >= 0; j--) { 527 /* 528 * Check to see if we've already unlocked this one. Not 529 * the first one going back, and the inode ptr is the 530 * same. 531 */ 532 if (j != (i - 1) && ips[j] == ips[j + 1]) 533 continue; 534 535 xfs_iunlock(ips[j], lock_mode); 536 } 537 538 if ((attempts % 5) == 0) { 539 delay(1); /* Don't just spin the CPU */ 540 } 541 goto again; 542 } 543 } 544 545 /* 546 * xfs_lock_two_inodes() can only be used to lock ilock. The iolock and 547 * mmaplock must be double-locked separately since we use i_rwsem and 548 * invalidate_lock for that. We now support taking one lock EXCL and the 549 * other SHARED. 550 */ 551 void 552 xfs_lock_two_inodes( 553 struct xfs_inode *ip0, 554 uint ip0_mode, 555 struct xfs_inode *ip1, 556 uint ip1_mode) 557 { 558 int attempts = 0; 559 struct xfs_log_item *lp; 560 561 ASSERT(hweight32(ip0_mode) == 1); 562 ASSERT(hweight32(ip1_mode) == 1); 563 ASSERT(!(ip0_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))); 564 ASSERT(!(ip1_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))); 565 ASSERT(!(ip0_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL))); 566 ASSERT(!(ip1_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL))); 567 ASSERT(ip0->i_ino != ip1->i_ino); 568 569 if (ip0->i_ino > ip1->i_ino) { 570 swap(ip0, ip1); 571 swap(ip0_mode, ip1_mode); 572 } 573 574 again: 575 xfs_ilock(ip0, xfs_lock_inumorder(ip0_mode, 0)); 576 577 /* 578 * If the first lock we have locked is in the AIL, we must TRY to get 579 * the second lock. If we can't get it, we must release the first one 580 * and try again. 581 */ 582 lp = &ip0->i_itemp->ili_item; 583 if (lp && test_bit(XFS_LI_IN_AIL, &lp->li_flags)) { 584 if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(ip1_mode, 1))) { 585 xfs_iunlock(ip0, ip0_mode); 586 if ((++attempts % 5) == 0) 587 delay(1); /* Don't just spin the CPU */ 588 goto again; 589 } 590 } else { 591 xfs_ilock(ip1, xfs_lock_inumorder(ip1_mode, 1)); 592 } 593 } 594 595 uint 596 xfs_ip2xflags( 597 struct xfs_inode *ip) 598 { 599 uint flags = 0; 600 601 if (ip->i_diflags & XFS_DIFLAG_ANY) { 602 if (ip->i_diflags & XFS_DIFLAG_REALTIME) 603 flags |= FS_XFLAG_REALTIME; 604 if (ip->i_diflags & XFS_DIFLAG_PREALLOC) 605 flags |= FS_XFLAG_PREALLOC; 606 if (ip->i_diflags & XFS_DIFLAG_IMMUTABLE) 607 flags |= FS_XFLAG_IMMUTABLE; 608 if (ip->i_diflags & XFS_DIFLAG_APPEND) 609 flags |= FS_XFLAG_APPEND; 610 if (ip->i_diflags & XFS_DIFLAG_SYNC) 611 flags |= FS_XFLAG_SYNC; 612 if (ip->i_diflags & XFS_DIFLAG_NOATIME) 613 flags |= FS_XFLAG_NOATIME; 614 if (ip->i_diflags & XFS_DIFLAG_NODUMP) 615 flags |= FS_XFLAG_NODUMP; 616 if (ip->i_diflags & XFS_DIFLAG_RTINHERIT) 617 flags |= FS_XFLAG_RTINHERIT; 618 if (ip->i_diflags & XFS_DIFLAG_PROJINHERIT) 619 flags |= FS_XFLAG_PROJINHERIT; 620 if (ip->i_diflags & XFS_DIFLAG_NOSYMLINKS) 621 flags |= FS_XFLAG_NOSYMLINKS; 622 if (ip->i_diflags & XFS_DIFLAG_EXTSIZE) 623 flags |= FS_XFLAG_EXTSIZE; 624 if (ip->i_diflags & XFS_DIFLAG_EXTSZINHERIT) 625 flags |= FS_XFLAG_EXTSZINHERIT; 626 if (ip->i_diflags & XFS_DIFLAG_NODEFRAG) 627 flags |= FS_XFLAG_NODEFRAG; 628 if (ip->i_diflags & XFS_DIFLAG_FILESTREAM) 629 flags |= FS_XFLAG_FILESTREAM; 630 } 631 632 if (ip->i_diflags2 & XFS_DIFLAG2_ANY) { 633 if (ip->i_diflags2 & XFS_DIFLAG2_DAX) 634 flags |= FS_XFLAG_DAX; 635 if (ip->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE) 636 flags |= FS_XFLAG_COWEXTSIZE; 637 } 638 639 if (xfs_inode_has_attr_fork(ip)) 640 flags |= FS_XFLAG_HASATTR; 641 return flags; 642 } 643 644 /* 645 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match 646 * is allowed, otherwise it has to be an exact match. If a CI match is found, 647 * ci_name->name will point to a the actual name (caller must free) or 648 * will be set to NULL if an exact match is found. 649 */ 650 int 651 xfs_lookup( 652 struct xfs_inode *dp, 653 const struct xfs_name *name, 654 struct xfs_inode **ipp, 655 struct xfs_name *ci_name) 656 { 657 xfs_ino_t inum; 658 int error; 659 660 trace_xfs_lookup(dp, name); 661 662 if (xfs_is_shutdown(dp->i_mount)) 663 return -EIO; 664 665 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name); 666 if (error) 667 goto out_unlock; 668 669 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp); 670 if (error) 671 goto out_free_name; 672 673 return 0; 674 675 out_free_name: 676 if (ci_name) 677 kmem_free(ci_name->name); 678 out_unlock: 679 *ipp = NULL; 680 return error; 681 } 682 683 /* Propagate di_flags from a parent inode to a child inode. */ 684 static void 685 xfs_inode_inherit_flags( 686 struct xfs_inode *ip, 687 const struct xfs_inode *pip) 688 { 689 unsigned int di_flags = 0; 690 xfs_failaddr_t failaddr; 691 umode_t mode = VFS_I(ip)->i_mode; 692 693 if (S_ISDIR(mode)) { 694 if (pip->i_diflags & XFS_DIFLAG_RTINHERIT) 695 di_flags |= XFS_DIFLAG_RTINHERIT; 696 if (pip->i_diflags & XFS_DIFLAG_EXTSZINHERIT) { 697 di_flags |= XFS_DIFLAG_EXTSZINHERIT; 698 ip->i_extsize = pip->i_extsize; 699 } 700 if (pip->i_diflags & XFS_DIFLAG_PROJINHERIT) 701 di_flags |= XFS_DIFLAG_PROJINHERIT; 702 } else if (S_ISREG(mode)) { 703 if ((pip->i_diflags & XFS_DIFLAG_RTINHERIT) && 704 xfs_has_realtime(ip->i_mount)) 705 di_flags |= XFS_DIFLAG_REALTIME; 706 if (pip->i_diflags & XFS_DIFLAG_EXTSZINHERIT) { 707 di_flags |= XFS_DIFLAG_EXTSIZE; 708 ip->i_extsize = pip->i_extsize; 709 } 710 } 711 if ((pip->i_diflags & XFS_DIFLAG_NOATIME) && 712 xfs_inherit_noatime) 713 di_flags |= XFS_DIFLAG_NOATIME; 714 if ((pip->i_diflags & XFS_DIFLAG_NODUMP) && 715 xfs_inherit_nodump) 716 di_flags |= XFS_DIFLAG_NODUMP; 717 if ((pip->i_diflags & XFS_DIFLAG_SYNC) && 718 xfs_inherit_sync) 719 di_flags |= XFS_DIFLAG_SYNC; 720 if ((pip->i_diflags & XFS_DIFLAG_NOSYMLINKS) && 721 xfs_inherit_nosymlinks) 722 di_flags |= XFS_DIFLAG_NOSYMLINKS; 723 if ((pip->i_diflags & XFS_DIFLAG_NODEFRAG) && 724 xfs_inherit_nodefrag) 725 di_flags |= XFS_DIFLAG_NODEFRAG; 726 if (pip->i_diflags & XFS_DIFLAG_FILESTREAM) 727 di_flags |= XFS_DIFLAG_FILESTREAM; 728 729 ip->i_diflags |= di_flags; 730 731 /* 732 * Inode verifiers on older kernels only check that the extent size 733 * hint is an integer multiple of the rt extent size on realtime files. 734 * They did not check the hint alignment on a directory with both 735 * rtinherit and extszinherit flags set. If the misaligned hint is 736 * propagated from a directory into a new realtime file, new file 737 * allocations will fail due to math errors in the rt allocator and/or 738 * trip the verifiers. Validate the hint settings in the new file so 739 * that we don't let broken hints propagate. 740 */ 741 failaddr = xfs_inode_validate_extsize(ip->i_mount, ip->i_extsize, 742 VFS_I(ip)->i_mode, ip->i_diflags); 743 if (failaddr) { 744 ip->i_diflags &= ~(XFS_DIFLAG_EXTSIZE | 745 XFS_DIFLAG_EXTSZINHERIT); 746 ip->i_extsize = 0; 747 } 748 } 749 750 /* Propagate di_flags2 from a parent inode to a child inode. */ 751 static void 752 xfs_inode_inherit_flags2( 753 struct xfs_inode *ip, 754 const struct xfs_inode *pip) 755 { 756 xfs_failaddr_t failaddr; 757 758 if (pip->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE) { 759 ip->i_diflags2 |= XFS_DIFLAG2_COWEXTSIZE; 760 ip->i_cowextsize = pip->i_cowextsize; 761 } 762 if (pip->i_diflags2 & XFS_DIFLAG2_DAX) 763 ip->i_diflags2 |= XFS_DIFLAG2_DAX; 764 765 /* Don't let invalid cowextsize hints propagate. */ 766 failaddr = xfs_inode_validate_cowextsize(ip->i_mount, ip->i_cowextsize, 767 VFS_I(ip)->i_mode, ip->i_diflags, ip->i_diflags2); 768 if (failaddr) { 769 ip->i_diflags2 &= ~XFS_DIFLAG2_COWEXTSIZE; 770 ip->i_cowextsize = 0; 771 } 772 } 773 774 /* 775 * Initialise a newly allocated inode and return the in-core inode to the 776 * caller locked exclusively. 777 */ 778 int 779 xfs_init_new_inode( 780 struct mnt_idmap *idmap, 781 struct xfs_trans *tp, 782 struct xfs_inode *pip, 783 xfs_ino_t ino, 784 umode_t mode, 785 xfs_nlink_t nlink, 786 dev_t rdev, 787 prid_t prid, 788 bool init_xattrs, 789 struct xfs_inode **ipp) 790 { 791 struct inode *dir = pip ? VFS_I(pip) : NULL; 792 struct xfs_mount *mp = tp->t_mountp; 793 struct xfs_inode *ip; 794 unsigned int flags; 795 int error; 796 struct timespec64 tv; 797 struct inode *inode; 798 799 /* 800 * Protect against obviously corrupt allocation btree records. Later 801 * xfs_iget checks will catch re-allocation of other active in-memory 802 * and on-disk inodes. If we don't catch reallocating the parent inode 803 * here we will deadlock in xfs_iget() so we have to do these checks 804 * first. 805 */ 806 if ((pip && ino == pip->i_ino) || !xfs_verify_dir_ino(mp, ino)) { 807 xfs_alert(mp, "Allocated a known in-use inode 0x%llx!", ino); 808 return -EFSCORRUPTED; 809 } 810 811 /* 812 * Get the in-core inode with the lock held exclusively to prevent 813 * others from looking at until we're done. 814 */ 815 error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE, XFS_ILOCK_EXCL, &ip); 816 if (error) 817 return error; 818 819 ASSERT(ip != NULL); 820 inode = VFS_I(ip); 821 set_nlink(inode, nlink); 822 inode->i_rdev = rdev; 823 ip->i_projid = prid; 824 825 if (dir && !(dir->i_mode & S_ISGID) && xfs_has_grpid(mp)) { 826 inode_fsuid_set(inode, idmap); 827 inode->i_gid = dir->i_gid; 828 inode->i_mode = mode; 829 } else { 830 inode_init_owner(idmap, inode, dir, mode); 831 } 832 833 /* 834 * If the group ID of the new file does not match the effective group 835 * ID or one of the supplementary group IDs, the S_ISGID bit is cleared 836 * (and only if the irix_sgid_inherit compatibility variable is set). 837 */ 838 if (irix_sgid_inherit && (inode->i_mode & S_ISGID) && 839 !vfsgid_in_group_p(i_gid_into_vfsgid(idmap, inode))) 840 inode->i_mode &= ~S_ISGID; 841 842 ip->i_disk_size = 0; 843 ip->i_df.if_nextents = 0; 844 ASSERT(ip->i_nblocks == 0); 845 846 tv = inode_set_ctime_current(inode); 847 inode->i_mtime = tv; 848 inode->i_atime = tv; 849 850 ip->i_extsize = 0; 851 ip->i_diflags = 0; 852 853 if (xfs_has_v3inodes(mp)) { 854 inode_set_iversion(inode, 1); 855 ip->i_cowextsize = 0; 856 ip->i_crtime = tv; 857 } 858 859 flags = XFS_ILOG_CORE; 860 switch (mode & S_IFMT) { 861 case S_IFIFO: 862 case S_IFCHR: 863 case S_IFBLK: 864 case S_IFSOCK: 865 ip->i_df.if_format = XFS_DINODE_FMT_DEV; 866 flags |= XFS_ILOG_DEV; 867 break; 868 case S_IFREG: 869 case S_IFDIR: 870 if (pip && (pip->i_diflags & XFS_DIFLAG_ANY)) 871 xfs_inode_inherit_flags(ip, pip); 872 if (pip && (pip->i_diflags2 & XFS_DIFLAG2_ANY)) 873 xfs_inode_inherit_flags2(ip, pip); 874 fallthrough; 875 case S_IFLNK: 876 ip->i_df.if_format = XFS_DINODE_FMT_EXTENTS; 877 ip->i_df.if_bytes = 0; 878 ip->i_df.if_u1.if_root = NULL; 879 break; 880 default: 881 ASSERT(0); 882 } 883 884 /* 885 * If we need to create attributes immediately after allocating the 886 * inode, initialise an empty attribute fork right now. We use the 887 * default fork offset for attributes here as we don't know exactly what 888 * size or how many attributes we might be adding. We can do this 889 * safely here because we know the data fork is completely empty and 890 * this saves us from needing to run a separate transaction to set the 891 * fork offset in the immediate future. 892 */ 893 if (init_xattrs && xfs_has_attr(mp)) { 894 ip->i_forkoff = xfs_default_attroffset(ip) >> 3; 895 xfs_ifork_init_attr(ip, XFS_DINODE_FMT_EXTENTS, 0); 896 } 897 898 /* 899 * Log the new values stuffed into the inode. 900 */ 901 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); 902 xfs_trans_log_inode(tp, ip, flags); 903 904 /* now that we have an i_mode we can setup the inode structure */ 905 xfs_setup_inode(ip); 906 907 *ipp = ip; 908 return 0; 909 } 910 911 /* 912 * Decrement the link count on an inode & log the change. If this causes the 913 * link count to go to zero, move the inode to AGI unlinked list so that it can 914 * be freed when the last active reference goes away via xfs_inactive(). 915 */ 916 static int /* error */ 917 xfs_droplink( 918 xfs_trans_t *tp, 919 xfs_inode_t *ip) 920 { 921 if (VFS_I(ip)->i_nlink == 0) { 922 xfs_alert(ip->i_mount, 923 "%s: Attempt to drop inode (%llu) with nlink zero.", 924 __func__, ip->i_ino); 925 return -EFSCORRUPTED; 926 } 927 928 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG); 929 930 drop_nlink(VFS_I(ip)); 931 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); 932 933 if (VFS_I(ip)->i_nlink) 934 return 0; 935 936 return xfs_iunlink(tp, ip); 937 } 938 939 /* 940 * Increment the link count on an inode & log the change. 941 */ 942 static void 943 xfs_bumplink( 944 xfs_trans_t *tp, 945 xfs_inode_t *ip) 946 { 947 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG); 948 949 inc_nlink(VFS_I(ip)); 950 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); 951 } 952 953 int 954 xfs_create( 955 struct mnt_idmap *idmap, 956 xfs_inode_t *dp, 957 struct xfs_name *name, 958 umode_t mode, 959 dev_t rdev, 960 bool init_xattrs, 961 xfs_inode_t **ipp) 962 { 963 int is_dir = S_ISDIR(mode); 964 struct xfs_mount *mp = dp->i_mount; 965 struct xfs_inode *ip = NULL; 966 struct xfs_trans *tp = NULL; 967 int error; 968 bool unlock_dp_on_error = false; 969 prid_t prid; 970 struct xfs_dquot *udqp = NULL; 971 struct xfs_dquot *gdqp = NULL; 972 struct xfs_dquot *pdqp = NULL; 973 struct xfs_trans_res *tres; 974 uint resblks; 975 xfs_ino_t ino; 976 977 trace_xfs_create(dp, name); 978 979 if (xfs_is_shutdown(mp)) 980 return -EIO; 981 982 prid = xfs_get_initial_prid(dp); 983 984 /* 985 * Make sure that we have allocated dquot(s) on disk. The uid/gid 986 * computation code must match what the VFS uses to assign i_[ug]id. 987 * INHERIT adjusts the gid computation for setgid/grpid systems. 988 */ 989 error = xfs_qm_vop_dqalloc(dp, mapped_fsuid(idmap, i_user_ns(VFS_I(dp))), 990 mapped_fsgid(idmap, i_user_ns(VFS_I(dp))), prid, 991 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, 992 &udqp, &gdqp, &pdqp); 993 if (error) 994 return error; 995 996 if (is_dir) { 997 resblks = XFS_MKDIR_SPACE_RES(mp, name->len); 998 tres = &M_RES(mp)->tr_mkdir; 999 } else { 1000 resblks = XFS_CREATE_SPACE_RES(mp, name->len); 1001 tres = &M_RES(mp)->tr_create; 1002 } 1003 1004 /* 1005 * Initially assume that the file does not exist and 1006 * reserve the resources for that case. If that is not 1007 * the case we'll drop the one we have and get a more 1008 * appropriate transaction later. 1009 */ 1010 error = xfs_trans_alloc_icreate(mp, tres, udqp, gdqp, pdqp, resblks, 1011 &tp); 1012 if (error == -ENOSPC) { 1013 /* flush outstanding delalloc blocks and retry */ 1014 xfs_flush_inodes(mp); 1015 error = xfs_trans_alloc_icreate(mp, tres, udqp, gdqp, pdqp, 1016 resblks, &tp); 1017 } 1018 if (error) 1019 goto out_release_dquots; 1020 1021 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT); 1022 unlock_dp_on_error = true; 1023 1024 /* 1025 * A newly created regular or special file just has one directory 1026 * entry pointing to them, but a directory also the "." entry 1027 * pointing to itself. 1028 */ 1029 error = xfs_dialloc(&tp, dp->i_ino, mode, &ino); 1030 if (!error) 1031 error = xfs_init_new_inode(idmap, tp, dp, ino, mode, 1032 is_dir ? 2 : 1, rdev, prid, init_xattrs, &ip); 1033 if (error) 1034 goto out_trans_cancel; 1035 1036 /* 1037 * Now we join the directory inode to the transaction. We do not do it 1038 * earlier because xfs_dialloc might commit the previous transaction 1039 * (and release all the locks). An error from here on will result in 1040 * the transaction cancel unlocking dp so don't do it explicitly in the 1041 * error path. 1042 */ 1043 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL); 1044 unlock_dp_on_error = false; 1045 1046 error = xfs_dir_createname(tp, dp, name, ip->i_ino, 1047 resblks - XFS_IALLOC_SPACE_RES(mp)); 1048 if (error) { 1049 ASSERT(error != -ENOSPC); 1050 goto out_trans_cancel; 1051 } 1052 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); 1053 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); 1054 1055 if (is_dir) { 1056 error = xfs_dir_init(tp, ip, dp); 1057 if (error) 1058 goto out_trans_cancel; 1059 1060 xfs_bumplink(tp, dp); 1061 } 1062 1063 /* 1064 * If this is a synchronous mount, make sure that the 1065 * create transaction goes to disk before returning to 1066 * the user. 1067 */ 1068 if (xfs_has_wsync(mp) || xfs_has_dirsync(mp)) 1069 xfs_trans_set_sync(tp); 1070 1071 /* 1072 * Attach the dquot(s) to the inodes and modify them incore. 1073 * These ids of the inode couldn't have changed since the new 1074 * inode has been locked ever since it was created. 1075 */ 1076 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp); 1077 1078 error = xfs_trans_commit(tp); 1079 if (error) 1080 goto out_release_inode; 1081 1082 xfs_qm_dqrele(udqp); 1083 xfs_qm_dqrele(gdqp); 1084 xfs_qm_dqrele(pdqp); 1085 1086 *ipp = ip; 1087 return 0; 1088 1089 out_trans_cancel: 1090 xfs_trans_cancel(tp); 1091 out_release_inode: 1092 /* 1093 * Wait until after the current transaction is aborted to finish the 1094 * setup of the inode and release the inode. This prevents recursive 1095 * transactions and deadlocks from xfs_inactive. 1096 */ 1097 if (ip) { 1098 xfs_finish_inode_setup(ip); 1099 xfs_irele(ip); 1100 } 1101 out_release_dquots: 1102 xfs_qm_dqrele(udqp); 1103 xfs_qm_dqrele(gdqp); 1104 xfs_qm_dqrele(pdqp); 1105 1106 if (unlock_dp_on_error) 1107 xfs_iunlock(dp, XFS_ILOCK_EXCL); 1108 return error; 1109 } 1110 1111 int 1112 xfs_create_tmpfile( 1113 struct mnt_idmap *idmap, 1114 struct xfs_inode *dp, 1115 umode_t mode, 1116 struct xfs_inode **ipp) 1117 { 1118 struct xfs_mount *mp = dp->i_mount; 1119 struct xfs_inode *ip = NULL; 1120 struct xfs_trans *tp = NULL; 1121 int error; 1122 prid_t prid; 1123 struct xfs_dquot *udqp = NULL; 1124 struct xfs_dquot *gdqp = NULL; 1125 struct xfs_dquot *pdqp = NULL; 1126 struct xfs_trans_res *tres; 1127 uint resblks; 1128 xfs_ino_t ino; 1129 1130 if (xfs_is_shutdown(mp)) 1131 return -EIO; 1132 1133 prid = xfs_get_initial_prid(dp); 1134 1135 /* 1136 * Make sure that we have allocated dquot(s) on disk. The uid/gid 1137 * computation code must match what the VFS uses to assign i_[ug]id. 1138 * INHERIT adjusts the gid computation for setgid/grpid systems. 1139 */ 1140 error = xfs_qm_vop_dqalloc(dp, mapped_fsuid(idmap, i_user_ns(VFS_I(dp))), 1141 mapped_fsgid(idmap, i_user_ns(VFS_I(dp))), prid, 1142 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, 1143 &udqp, &gdqp, &pdqp); 1144 if (error) 1145 return error; 1146 1147 resblks = XFS_IALLOC_SPACE_RES(mp); 1148 tres = &M_RES(mp)->tr_create_tmpfile; 1149 1150 error = xfs_trans_alloc_icreate(mp, tres, udqp, gdqp, pdqp, resblks, 1151 &tp); 1152 if (error) 1153 goto out_release_dquots; 1154 1155 error = xfs_dialloc(&tp, dp->i_ino, mode, &ino); 1156 if (!error) 1157 error = xfs_init_new_inode(idmap, tp, dp, ino, mode, 1158 0, 0, prid, false, &ip); 1159 if (error) 1160 goto out_trans_cancel; 1161 1162 if (xfs_has_wsync(mp)) 1163 xfs_trans_set_sync(tp); 1164 1165 /* 1166 * Attach the dquot(s) to the inodes and modify them incore. 1167 * These ids of the inode couldn't have changed since the new 1168 * inode has been locked ever since it was created. 1169 */ 1170 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp); 1171 1172 error = xfs_iunlink(tp, ip); 1173 if (error) 1174 goto out_trans_cancel; 1175 1176 error = xfs_trans_commit(tp); 1177 if (error) 1178 goto out_release_inode; 1179 1180 xfs_qm_dqrele(udqp); 1181 xfs_qm_dqrele(gdqp); 1182 xfs_qm_dqrele(pdqp); 1183 1184 *ipp = ip; 1185 return 0; 1186 1187 out_trans_cancel: 1188 xfs_trans_cancel(tp); 1189 out_release_inode: 1190 /* 1191 * Wait until after the current transaction is aborted to finish the 1192 * setup of the inode and release the inode. This prevents recursive 1193 * transactions and deadlocks from xfs_inactive. 1194 */ 1195 if (ip) { 1196 xfs_finish_inode_setup(ip); 1197 xfs_irele(ip); 1198 } 1199 out_release_dquots: 1200 xfs_qm_dqrele(udqp); 1201 xfs_qm_dqrele(gdqp); 1202 xfs_qm_dqrele(pdqp); 1203 1204 return error; 1205 } 1206 1207 int 1208 xfs_link( 1209 xfs_inode_t *tdp, 1210 xfs_inode_t *sip, 1211 struct xfs_name *target_name) 1212 { 1213 xfs_mount_t *mp = tdp->i_mount; 1214 xfs_trans_t *tp; 1215 int error, nospace_error = 0; 1216 int resblks; 1217 1218 trace_xfs_link(tdp, target_name); 1219 1220 ASSERT(!S_ISDIR(VFS_I(sip)->i_mode)); 1221 1222 if (xfs_is_shutdown(mp)) 1223 return -EIO; 1224 1225 error = xfs_qm_dqattach(sip); 1226 if (error) 1227 goto std_return; 1228 1229 error = xfs_qm_dqattach(tdp); 1230 if (error) 1231 goto std_return; 1232 1233 resblks = XFS_LINK_SPACE_RES(mp, target_name->len); 1234 error = xfs_trans_alloc_dir(tdp, &M_RES(mp)->tr_link, sip, &resblks, 1235 &tp, &nospace_error); 1236 if (error) 1237 goto std_return; 1238 1239 /* 1240 * If we are using project inheritance, we only allow hard link 1241 * creation in our tree when the project IDs are the same; else 1242 * the tree quota mechanism could be circumvented. 1243 */ 1244 if (unlikely((tdp->i_diflags & XFS_DIFLAG_PROJINHERIT) && 1245 tdp->i_projid != sip->i_projid)) { 1246 /* 1247 * Project quota setup skips special files which can 1248 * leave inodes in a PROJINHERIT directory without a 1249 * project ID set. We need to allow links to be made 1250 * to these "project-less" inodes because userspace 1251 * expects them to succeed after project ID setup, 1252 * but everything else should be rejected. 1253 */ 1254 if (!special_file(VFS_I(sip)->i_mode) || 1255 sip->i_projid != 0) { 1256 error = -EXDEV; 1257 goto error_return; 1258 } 1259 } 1260 1261 if (!resblks) { 1262 error = xfs_dir_canenter(tp, tdp, target_name); 1263 if (error) 1264 goto error_return; 1265 } 1266 1267 /* 1268 * Handle initial link state of O_TMPFILE inode 1269 */ 1270 if (VFS_I(sip)->i_nlink == 0) { 1271 struct xfs_perag *pag; 1272 1273 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, sip->i_ino)); 1274 error = xfs_iunlink_remove(tp, pag, sip); 1275 xfs_perag_put(pag); 1276 if (error) 1277 goto error_return; 1278 } 1279 1280 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino, 1281 resblks); 1282 if (error) 1283 goto error_return; 1284 xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); 1285 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE); 1286 1287 xfs_bumplink(tp, sip); 1288 1289 /* 1290 * If this is a synchronous mount, make sure that the 1291 * link transaction goes to disk before returning to 1292 * the user. 1293 */ 1294 if (xfs_has_wsync(mp) || xfs_has_dirsync(mp)) 1295 xfs_trans_set_sync(tp); 1296 1297 return xfs_trans_commit(tp); 1298 1299 error_return: 1300 xfs_trans_cancel(tp); 1301 std_return: 1302 if (error == -ENOSPC && nospace_error) 1303 error = nospace_error; 1304 return error; 1305 } 1306 1307 /* Clear the reflink flag and the cowblocks tag if possible. */ 1308 static void 1309 xfs_itruncate_clear_reflink_flags( 1310 struct xfs_inode *ip) 1311 { 1312 struct xfs_ifork *dfork; 1313 struct xfs_ifork *cfork; 1314 1315 if (!xfs_is_reflink_inode(ip)) 1316 return; 1317 dfork = xfs_ifork_ptr(ip, XFS_DATA_FORK); 1318 cfork = xfs_ifork_ptr(ip, XFS_COW_FORK); 1319 if (dfork->if_bytes == 0 && cfork->if_bytes == 0) 1320 ip->i_diflags2 &= ~XFS_DIFLAG2_REFLINK; 1321 if (cfork->if_bytes == 0) 1322 xfs_inode_clear_cowblocks_tag(ip); 1323 } 1324 1325 /* 1326 * Free up the underlying blocks past new_size. The new size must be smaller 1327 * than the current size. This routine can be used both for the attribute and 1328 * data fork, and does not modify the inode size, which is left to the caller. 1329 * 1330 * The transaction passed to this routine must have made a permanent log 1331 * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the 1332 * given transaction and start new ones, so make sure everything involved in 1333 * the transaction is tidy before calling here. Some transaction will be 1334 * returned to the caller to be committed. The incoming transaction must 1335 * already include the inode, and both inode locks must be held exclusively. 1336 * The inode must also be "held" within the transaction. On return the inode 1337 * will be "held" within the returned transaction. This routine does NOT 1338 * require any disk space to be reserved for it within the transaction. 1339 * 1340 * If we get an error, we must return with the inode locked and linked into the 1341 * current transaction. This keeps things simple for the higher level code, 1342 * because it always knows that the inode is locked and held in the transaction 1343 * that returns to it whether errors occur or not. We don't mark the inode 1344 * dirty on error so that transactions can be easily aborted if possible. 1345 */ 1346 int 1347 xfs_itruncate_extents_flags( 1348 struct xfs_trans **tpp, 1349 struct xfs_inode *ip, 1350 int whichfork, 1351 xfs_fsize_t new_size, 1352 int flags) 1353 { 1354 struct xfs_mount *mp = ip->i_mount; 1355 struct xfs_trans *tp = *tpp; 1356 xfs_fileoff_t first_unmap_block; 1357 xfs_filblks_t unmap_len; 1358 int error = 0; 1359 1360 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); 1361 ASSERT(!atomic_read(&VFS_I(ip)->i_count) || 1362 xfs_isilocked(ip, XFS_IOLOCK_EXCL)); 1363 ASSERT(new_size <= XFS_ISIZE(ip)); 1364 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES); 1365 ASSERT(ip->i_itemp != NULL); 1366 ASSERT(ip->i_itemp->ili_lock_flags == 0); 1367 ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); 1368 1369 trace_xfs_itruncate_extents_start(ip, new_size); 1370 1371 flags |= xfs_bmapi_aflag(whichfork); 1372 1373 /* 1374 * Since it is possible for space to become allocated beyond 1375 * the end of the file (in a crash where the space is allocated 1376 * but the inode size is not yet updated), simply remove any 1377 * blocks which show up between the new EOF and the maximum 1378 * possible file size. 1379 * 1380 * We have to free all the blocks to the bmbt maximum offset, even if 1381 * the page cache can't scale that far. 1382 */ 1383 first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size); 1384 if (!xfs_verify_fileoff(mp, first_unmap_block)) { 1385 WARN_ON_ONCE(first_unmap_block > XFS_MAX_FILEOFF); 1386 return 0; 1387 } 1388 1389 unmap_len = XFS_MAX_FILEOFF - first_unmap_block + 1; 1390 while (unmap_len > 0) { 1391 ASSERT(tp->t_highest_agno == NULLAGNUMBER); 1392 error = __xfs_bunmapi(tp, ip, first_unmap_block, &unmap_len, 1393 flags, XFS_ITRUNC_MAX_EXTENTS); 1394 if (error) 1395 goto out; 1396 1397 /* free the just unmapped extents */ 1398 error = xfs_defer_finish(&tp); 1399 if (error) 1400 goto out; 1401 } 1402 1403 if (whichfork == XFS_DATA_FORK) { 1404 /* Remove all pending CoW reservations. */ 1405 error = xfs_reflink_cancel_cow_blocks(ip, &tp, 1406 first_unmap_block, XFS_MAX_FILEOFF, true); 1407 if (error) 1408 goto out; 1409 1410 xfs_itruncate_clear_reflink_flags(ip); 1411 } 1412 1413 /* 1414 * Always re-log the inode so that our permanent transaction can keep 1415 * on rolling it forward in the log. 1416 */ 1417 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); 1418 1419 trace_xfs_itruncate_extents_end(ip, new_size); 1420 1421 out: 1422 *tpp = tp; 1423 return error; 1424 } 1425 1426 int 1427 xfs_release( 1428 xfs_inode_t *ip) 1429 { 1430 xfs_mount_t *mp = ip->i_mount; 1431 int error = 0; 1432 1433 if (!S_ISREG(VFS_I(ip)->i_mode) || (VFS_I(ip)->i_mode == 0)) 1434 return 0; 1435 1436 /* If this is a read-only mount, don't do this (would generate I/O) */ 1437 if (xfs_is_readonly(mp)) 1438 return 0; 1439 1440 if (!xfs_is_shutdown(mp)) { 1441 int truncated; 1442 1443 /* 1444 * If we previously truncated this file and removed old data 1445 * in the process, we want to initiate "early" writeout on 1446 * the last close. This is an attempt to combat the notorious 1447 * NULL files problem which is particularly noticeable from a 1448 * truncate down, buffered (re-)write (delalloc), followed by 1449 * a crash. What we are effectively doing here is 1450 * significantly reducing the time window where we'd otherwise 1451 * be exposed to that problem. 1452 */ 1453 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED); 1454 if (truncated) { 1455 xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE); 1456 if (ip->i_delayed_blks > 0) { 1457 error = filemap_flush(VFS_I(ip)->i_mapping); 1458 if (error) 1459 return error; 1460 } 1461 } 1462 } 1463 1464 if (VFS_I(ip)->i_nlink == 0) 1465 return 0; 1466 1467 /* 1468 * If we can't get the iolock just skip truncating the blocks past EOF 1469 * because we could deadlock with the mmap_lock otherwise. We'll get 1470 * another chance to drop them once the last reference to the inode is 1471 * dropped, so we'll never leak blocks permanently. 1472 */ 1473 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) 1474 return 0; 1475 1476 if (xfs_can_free_eofblocks(ip)) { 1477 /* 1478 * Check if the inode is being opened, written and closed 1479 * frequently and we have delayed allocation blocks outstanding 1480 * (e.g. streaming writes from the NFS server), truncating the 1481 * blocks past EOF will cause fragmentation to occur. 1482 * 1483 * In this case don't do the truncation, but we have to be 1484 * careful how we detect this case. Blocks beyond EOF show up as 1485 * i_delayed_blks even when the inode is clean, so we need to 1486 * truncate them away first before checking for a dirty release. 1487 * Hence on the first dirty close we will still remove the 1488 * speculative allocation, but after that we will leave it in 1489 * place. 1490 */ 1491 if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE)) 1492 goto out_unlock; 1493 1494 error = xfs_free_eofblocks(ip); 1495 if (error) 1496 goto out_unlock; 1497 1498 /* delalloc blocks after truncation means it really is dirty */ 1499 if (ip->i_delayed_blks) 1500 xfs_iflags_set(ip, XFS_IDIRTY_RELEASE); 1501 } 1502 1503 out_unlock: 1504 xfs_iunlock(ip, XFS_IOLOCK_EXCL); 1505 return error; 1506 } 1507 1508 /* 1509 * xfs_inactive_truncate 1510 * 1511 * Called to perform a truncate when an inode becomes unlinked. 1512 */ 1513 STATIC int 1514 xfs_inactive_truncate( 1515 struct xfs_inode *ip) 1516 { 1517 struct xfs_mount *mp = ip->i_mount; 1518 struct xfs_trans *tp; 1519 int error; 1520 1521 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp); 1522 if (error) { 1523 ASSERT(xfs_is_shutdown(mp)); 1524 return error; 1525 } 1526 xfs_ilock(ip, XFS_ILOCK_EXCL); 1527 xfs_trans_ijoin(tp, ip, 0); 1528 1529 /* 1530 * Log the inode size first to prevent stale data exposure in the event 1531 * of a system crash before the truncate completes. See the related 1532 * comment in xfs_vn_setattr_size() for details. 1533 */ 1534 ip->i_disk_size = 0; 1535 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); 1536 1537 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0); 1538 if (error) 1539 goto error_trans_cancel; 1540 1541 ASSERT(ip->i_df.if_nextents == 0); 1542 1543 error = xfs_trans_commit(tp); 1544 if (error) 1545 goto error_unlock; 1546 1547 xfs_iunlock(ip, XFS_ILOCK_EXCL); 1548 return 0; 1549 1550 error_trans_cancel: 1551 xfs_trans_cancel(tp); 1552 error_unlock: 1553 xfs_iunlock(ip, XFS_ILOCK_EXCL); 1554 return error; 1555 } 1556 1557 /* 1558 * xfs_inactive_ifree() 1559 * 1560 * Perform the inode free when an inode is unlinked. 1561 */ 1562 STATIC int 1563 xfs_inactive_ifree( 1564 struct xfs_inode *ip) 1565 { 1566 struct xfs_mount *mp = ip->i_mount; 1567 struct xfs_trans *tp; 1568 int error; 1569 1570 /* 1571 * We try to use a per-AG reservation for any block needed by the finobt 1572 * tree, but as the finobt feature predates the per-AG reservation 1573 * support a degraded file system might not have enough space for the 1574 * reservation at mount time. In that case try to dip into the reserved 1575 * pool and pray. 1576 * 1577 * Send a warning if the reservation does happen to fail, as the inode 1578 * now remains allocated and sits on the unlinked list until the fs is 1579 * repaired. 1580 */ 1581 if (unlikely(mp->m_finobt_nores)) { 1582 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ifree, 1583 XFS_IFREE_SPACE_RES(mp), 0, XFS_TRANS_RESERVE, 1584 &tp); 1585 } else { 1586 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ifree, 0, 0, 0, &tp); 1587 } 1588 if (error) { 1589 if (error == -ENOSPC) { 1590 xfs_warn_ratelimited(mp, 1591 "Failed to remove inode(s) from unlinked list. " 1592 "Please free space, unmount and run xfs_repair."); 1593 } else { 1594 ASSERT(xfs_is_shutdown(mp)); 1595 } 1596 return error; 1597 } 1598 1599 /* 1600 * We do not hold the inode locked across the entire rolling transaction 1601 * here. We only need to hold it for the first transaction that 1602 * xfs_ifree() builds, which may mark the inode XFS_ISTALE if the 1603 * underlying cluster buffer is freed. Relogging an XFS_ISTALE inode 1604 * here breaks the relationship between cluster buffer invalidation and 1605 * stale inode invalidation on cluster buffer item journal commit 1606 * completion, and can result in leaving dirty stale inodes hanging 1607 * around in memory. 1608 * 1609 * We have no need for serialising this inode operation against other 1610 * operations - we freed the inode and hence reallocation is required 1611 * and that will serialise on reallocating the space the deferops need 1612 * to free. Hence we can unlock the inode on the first commit of 1613 * the transaction rather than roll it right through the deferops. This 1614 * avoids relogging the XFS_ISTALE inode. 1615 * 1616 * We check that xfs_ifree() hasn't grown an internal transaction roll 1617 * by asserting that the inode is still locked when it returns. 1618 */ 1619 xfs_ilock(ip, XFS_ILOCK_EXCL); 1620 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); 1621 1622 error = xfs_ifree(tp, ip); 1623 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); 1624 if (error) { 1625 /* 1626 * If we fail to free the inode, shut down. The cancel 1627 * might do that, we need to make sure. Otherwise the 1628 * inode might be lost for a long time or forever. 1629 */ 1630 if (!xfs_is_shutdown(mp)) { 1631 xfs_notice(mp, "%s: xfs_ifree returned error %d", 1632 __func__, error); 1633 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR); 1634 } 1635 xfs_trans_cancel(tp); 1636 return error; 1637 } 1638 1639 /* 1640 * Credit the quota account(s). The inode is gone. 1641 */ 1642 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1); 1643 1644 return xfs_trans_commit(tp); 1645 } 1646 1647 /* 1648 * Returns true if we need to update the on-disk metadata before we can free 1649 * the memory used by this inode. Updates include freeing post-eof 1650 * preallocations; freeing COW staging extents; and marking the inode free in 1651 * the inobt if it is on the unlinked list. 1652 */ 1653 bool 1654 xfs_inode_needs_inactive( 1655 struct xfs_inode *ip) 1656 { 1657 struct xfs_mount *mp = ip->i_mount; 1658 struct xfs_ifork *cow_ifp = xfs_ifork_ptr(ip, XFS_COW_FORK); 1659 1660 /* 1661 * If the inode is already free, then there can be nothing 1662 * to clean up here. 1663 */ 1664 if (VFS_I(ip)->i_mode == 0) 1665 return false; 1666 1667 /* 1668 * If this is a read-only mount, don't do this (would generate I/O) 1669 * unless we're in log recovery and cleaning the iunlinked list. 1670 */ 1671 if (xfs_is_readonly(mp) && !xlog_recovery_needed(mp->m_log)) 1672 return false; 1673 1674 /* If the log isn't running, push inodes straight to reclaim. */ 1675 if (xfs_is_shutdown(mp) || xfs_has_norecovery(mp)) 1676 return false; 1677 1678 /* Metadata inodes require explicit resource cleanup. */ 1679 if (xfs_is_metadata_inode(ip)) 1680 return false; 1681 1682 /* Want to clean out the cow blocks if there are any. */ 1683 if (cow_ifp && cow_ifp->if_bytes > 0) 1684 return true; 1685 1686 /* Unlinked files must be freed. */ 1687 if (VFS_I(ip)->i_nlink == 0) 1688 return true; 1689 1690 /* 1691 * This file isn't being freed, so check if there are post-eof blocks 1692 * to free. 1693 * 1694 * Note: don't bother with iolock here since lockdep complains about 1695 * acquiring it in reclaim context. We have the only reference to the 1696 * inode at this point anyways. 1697 */ 1698 return xfs_can_free_eofblocks(ip); 1699 } 1700 1701 /* 1702 * xfs_inactive 1703 * 1704 * This is called when the vnode reference count for the vnode 1705 * goes to zero. If the file has been unlinked, then it must 1706 * now be truncated. Also, we clear all of the read-ahead state 1707 * kept for the inode here since the file is now closed. 1708 */ 1709 int 1710 xfs_inactive( 1711 xfs_inode_t *ip) 1712 { 1713 struct xfs_mount *mp; 1714 int error = 0; 1715 int truncate = 0; 1716 1717 /* 1718 * If the inode is already free, then there can be nothing 1719 * to clean up here. 1720 */ 1721 if (VFS_I(ip)->i_mode == 0) { 1722 ASSERT(ip->i_df.if_broot_bytes == 0); 1723 goto out; 1724 } 1725 1726 mp = ip->i_mount; 1727 ASSERT(!xfs_iflags_test(ip, XFS_IRECOVERY)); 1728 1729 /* 1730 * If this is a read-only mount, don't do this (would generate I/O) 1731 * unless we're in log recovery and cleaning the iunlinked list. 1732 */ 1733 if (xfs_is_readonly(mp) && !xlog_recovery_needed(mp->m_log)) 1734 goto out; 1735 1736 /* Metadata inodes require explicit resource cleanup. */ 1737 if (xfs_is_metadata_inode(ip)) 1738 goto out; 1739 1740 /* Try to clean out the cow blocks if there are any. */ 1741 if (xfs_inode_has_cow_data(ip)) 1742 xfs_reflink_cancel_cow_range(ip, 0, NULLFILEOFF, true); 1743 1744 if (VFS_I(ip)->i_nlink != 0) { 1745 /* 1746 * Note: don't bother with iolock here since lockdep complains 1747 * about acquiring it in reclaim context. We have the only 1748 * reference to the inode at this point anyways. 1749 */ 1750 if (xfs_can_free_eofblocks(ip)) 1751 error = xfs_free_eofblocks(ip); 1752 1753 goto out; 1754 } 1755 1756 if (S_ISREG(VFS_I(ip)->i_mode) && 1757 (ip->i_disk_size != 0 || XFS_ISIZE(ip) != 0 || 1758 ip->i_df.if_nextents > 0 || ip->i_delayed_blks > 0)) 1759 truncate = 1; 1760 1761 if (xfs_iflags_test(ip, XFS_IQUOTAUNCHECKED)) { 1762 /* 1763 * If this inode is being inactivated during a quotacheck and 1764 * has not yet been scanned by quotacheck, we /must/ remove 1765 * the dquots from the inode before inactivation changes the 1766 * block and inode counts. Most probably this is a result of 1767 * reloading the incore iunlinked list to purge unrecovered 1768 * unlinked inodes. 1769 */ 1770 xfs_qm_dqdetach(ip); 1771 } else { 1772 error = xfs_qm_dqattach(ip); 1773 if (error) 1774 goto out; 1775 } 1776 1777 if (S_ISLNK(VFS_I(ip)->i_mode)) 1778 error = xfs_inactive_symlink(ip); 1779 else if (truncate) 1780 error = xfs_inactive_truncate(ip); 1781 if (error) 1782 goto out; 1783 1784 /* 1785 * If there are attributes associated with the file then blow them away 1786 * now. The code calls a routine that recursively deconstructs the 1787 * attribute fork. If also blows away the in-core attribute fork. 1788 */ 1789 if (xfs_inode_has_attr_fork(ip)) { 1790 error = xfs_attr_inactive(ip); 1791 if (error) 1792 goto out; 1793 } 1794 1795 ASSERT(ip->i_forkoff == 0); 1796 1797 /* 1798 * Free the inode. 1799 */ 1800 error = xfs_inactive_ifree(ip); 1801 1802 out: 1803 /* 1804 * We're done making metadata updates for this inode, so we can release 1805 * the attached dquots. 1806 */ 1807 xfs_qm_dqdetach(ip); 1808 return error; 1809 } 1810 1811 /* 1812 * In-Core Unlinked List Lookups 1813 * ============================= 1814 * 1815 * Every inode is supposed to be reachable from some other piece of metadata 1816 * with the exception of the root directory. Inodes with a connection to a 1817 * file descriptor but not linked from anywhere in the on-disk directory tree 1818 * are collectively known as unlinked inodes, though the filesystem itself 1819 * maintains links to these inodes so that on-disk metadata are consistent. 1820 * 1821 * XFS implements a per-AG on-disk hash table of unlinked inodes. The AGI 1822 * header contains a number of buckets that point to an inode, and each inode 1823 * record has a pointer to the next inode in the hash chain. This 1824 * singly-linked list causes scaling problems in the iunlink remove function 1825 * because we must walk that list to find the inode that points to the inode 1826 * being removed from the unlinked hash bucket list. 1827 * 1828 * Hence we keep an in-memory double linked list to link each inode on an 1829 * unlinked list. Because there are 64 unlinked lists per AGI, keeping pointer 1830 * based lists would require having 64 list heads in the perag, one for each 1831 * list. This is expensive in terms of memory (think millions of AGs) and cache 1832 * misses on lookups. Instead, use the fact that inodes on the unlinked list 1833 * must be referenced at the VFS level to keep them on the list and hence we 1834 * have an existence guarantee for inodes on the unlinked list. 1835 * 1836 * Given we have an existence guarantee, we can use lockless inode cache lookups 1837 * to resolve aginos to xfs inodes. This means we only need 8 bytes per inode 1838 * for the double linked unlinked list, and we don't need any extra locking to 1839 * keep the list safe as all manipulations are done under the AGI buffer lock. 1840 * Keeping the list up to date does not require memory allocation, just finding 1841 * the XFS inode and updating the next/prev unlinked list aginos. 1842 */ 1843 1844 /* 1845 * Find an inode on the unlinked list. This does not take references to the 1846 * inode as we have existence guarantees by holding the AGI buffer lock and that 1847 * only unlinked, referenced inodes can be on the unlinked inode list. If we 1848 * don't find the inode in cache, then let the caller handle the situation. 1849 */ 1850 static struct xfs_inode * 1851 xfs_iunlink_lookup( 1852 struct xfs_perag *pag, 1853 xfs_agino_t agino) 1854 { 1855 struct xfs_inode *ip; 1856 1857 rcu_read_lock(); 1858 ip = radix_tree_lookup(&pag->pag_ici_root, agino); 1859 if (!ip) { 1860 /* Caller can handle inode not being in memory. */ 1861 rcu_read_unlock(); 1862 return NULL; 1863 } 1864 1865 /* 1866 * Inode in RCU freeing limbo should not happen. Warn about this and 1867 * let the caller handle the failure. 1868 */ 1869 if (WARN_ON_ONCE(!ip->i_ino)) { 1870 rcu_read_unlock(); 1871 return NULL; 1872 } 1873 ASSERT(!xfs_iflags_test(ip, XFS_IRECLAIMABLE | XFS_IRECLAIM)); 1874 rcu_read_unlock(); 1875 return ip; 1876 } 1877 1878 /* 1879 * Update the prev pointer of the next agino. Returns -ENOLINK if the inode 1880 * is not in cache. 1881 */ 1882 static int 1883 xfs_iunlink_update_backref( 1884 struct xfs_perag *pag, 1885 xfs_agino_t prev_agino, 1886 xfs_agino_t next_agino) 1887 { 1888 struct xfs_inode *ip; 1889 1890 /* No update necessary if we are at the end of the list. */ 1891 if (next_agino == NULLAGINO) 1892 return 0; 1893 1894 ip = xfs_iunlink_lookup(pag, next_agino); 1895 if (!ip) 1896 return -ENOLINK; 1897 1898 ip->i_prev_unlinked = prev_agino; 1899 return 0; 1900 } 1901 1902 /* 1903 * Point the AGI unlinked bucket at an inode and log the results. The caller 1904 * is responsible for validating the old value. 1905 */ 1906 STATIC int 1907 xfs_iunlink_update_bucket( 1908 struct xfs_trans *tp, 1909 struct xfs_perag *pag, 1910 struct xfs_buf *agibp, 1911 unsigned int bucket_index, 1912 xfs_agino_t new_agino) 1913 { 1914 struct xfs_agi *agi = agibp->b_addr; 1915 xfs_agino_t old_value; 1916 int offset; 1917 1918 ASSERT(xfs_verify_agino_or_null(pag, new_agino)); 1919 1920 old_value = be32_to_cpu(agi->agi_unlinked[bucket_index]); 1921 trace_xfs_iunlink_update_bucket(tp->t_mountp, pag->pag_agno, bucket_index, 1922 old_value, new_agino); 1923 1924 /* 1925 * We should never find the head of the list already set to the value 1926 * passed in because either we're adding or removing ourselves from the 1927 * head of the list. 1928 */ 1929 if (old_value == new_agino) { 1930 xfs_buf_mark_corrupt(agibp); 1931 return -EFSCORRUPTED; 1932 } 1933 1934 agi->agi_unlinked[bucket_index] = cpu_to_be32(new_agino); 1935 offset = offsetof(struct xfs_agi, agi_unlinked) + 1936 (sizeof(xfs_agino_t) * bucket_index); 1937 xfs_trans_log_buf(tp, agibp, offset, offset + sizeof(xfs_agino_t) - 1); 1938 return 0; 1939 } 1940 1941 /* 1942 * Load the inode @next_agino into the cache and set its prev_unlinked pointer 1943 * to @prev_agino. Caller must hold the AGI to synchronize with other changes 1944 * to the unlinked list. 1945 */ 1946 STATIC int 1947 xfs_iunlink_reload_next( 1948 struct xfs_trans *tp, 1949 struct xfs_buf *agibp, 1950 xfs_agino_t prev_agino, 1951 xfs_agino_t next_agino) 1952 { 1953 struct xfs_perag *pag = agibp->b_pag; 1954 struct xfs_mount *mp = pag->pag_mount; 1955 struct xfs_inode *next_ip = NULL; 1956 xfs_ino_t ino; 1957 int error; 1958 1959 ASSERT(next_agino != NULLAGINO); 1960 1961 #ifdef DEBUG 1962 rcu_read_lock(); 1963 next_ip = radix_tree_lookup(&pag->pag_ici_root, next_agino); 1964 ASSERT(next_ip == NULL); 1965 rcu_read_unlock(); 1966 #endif 1967 1968 xfs_info_ratelimited(mp, 1969 "Found unrecovered unlinked inode 0x%x in AG 0x%x. Initiating recovery.", 1970 next_agino, pag->pag_agno); 1971 1972 /* 1973 * Use an untrusted lookup just to be cautious in case the AGI has been 1974 * corrupted and now points at a free inode. That shouldn't happen, 1975 * but we'd rather shut down now since we're already running in a weird 1976 * situation. 1977 */ 1978 ino = XFS_AGINO_TO_INO(mp, pag->pag_agno, next_agino); 1979 error = xfs_iget(mp, tp, ino, XFS_IGET_UNTRUSTED, 0, &next_ip); 1980 if (error) 1981 return error; 1982 1983 /* If this is not an unlinked inode, something is very wrong. */ 1984 if (VFS_I(next_ip)->i_nlink != 0) { 1985 error = -EFSCORRUPTED; 1986 goto rele; 1987 } 1988 1989 next_ip->i_prev_unlinked = prev_agino; 1990 trace_xfs_iunlink_reload_next(next_ip); 1991 rele: 1992 ASSERT(!(VFS_I(next_ip)->i_state & I_DONTCACHE)); 1993 if (xfs_is_quotacheck_running(mp) && next_ip) 1994 xfs_iflags_set(next_ip, XFS_IQUOTAUNCHECKED); 1995 xfs_irele(next_ip); 1996 return error; 1997 } 1998 1999 static int 2000 xfs_iunlink_insert_inode( 2001 struct xfs_trans *tp, 2002 struct xfs_perag *pag, 2003 struct xfs_buf *agibp, 2004 struct xfs_inode *ip) 2005 { 2006 struct xfs_mount *mp = tp->t_mountp; 2007 struct xfs_agi *agi = agibp->b_addr; 2008 xfs_agino_t next_agino; 2009 xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ip->i_ino); 2010 short bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS; 2011 int error; 2012 2013 /* 2014 * Get the index into the agi hash table for the list this inode will 2015 * go on. Make sure the pointer isn't garbage and that this inode 2016 * isn't already on the list. 2017 */ 2018 next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]); 2019 if (next_agino == agino || 2020 !xfs_verify_agino_or_null(pag, next_agino)) { 2021 xfs_buf_mark_corrupt(agibp); 2022 return -EFSCORRUPTED; 2023 } 2024 2025 /* 2026 * Update the prev pointer in the next inode to point back to this 2027 * inode. 2028 */ 2029 error = xfs_iunlink_update_backref(pag, agino, next_agino); 2030 if (error == -ENOLINK) 2031 error = xfs_iunlink_reload_next(tp, agibp, agino, next_agino); 2032 if (error) 2033 return error; 2034 2035 if (next_agino != NULLAGINO) { 2036 /* 2037 * There is already another inode in the bucket, so point this 2038 * inode to the current head of the list. 2039 */ 2040 error = xfs_iunlink_log_inode(tp, ip, pag, next_agino); 2041 if (error) 2042 return error; 2043 ip->i_next_unlinked = next_agino; 2044 } 2045 2046 /* Point the head of the list to point to this inode. */ 2047 ip->i_prev_unlinked = NULLAGINO; 2048 return xfs_iunlink_update_bucket(tp, pag, agibp, bucket_index, agino); 2049 } 2050 2051 /* 2052 * This is called when the inode's link count has gone to 0 or we are creating 2053 * a tmpfile via O_TMPFILE. The inode @ip must have nlink == 0. 2054 * 2055 * We place the on-disk inode on a list in the AGI. It will be pulled from this 2056 * list when the inode is freed. 2057 */ 2058 STATIC int 2059 xfs_iunlink( 2060 struct xfs_trans *tp, 2061 struct xfs_inode *ip) 2062 { 2063 struct xfs_mount *mp = tp->t_mountp; 2064 struct xfs_perag *pag; 2065 struct xfs_buf *agibp; 2066 int error; 2067 2068 ASSERT(VFS_I(ip)->i_nlink == 0); 2069 ASSERT(VFS_I(ip)->i_mode != 0); 2070 trace_xfs_iunlink(ip); 2071 2072 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino)); 2073 2074 /* Get the agi buffer first. It ensures lock ordering on the list. */ 2075 error = xfs_read_agi(pag, tp, &agibp); 2076 if (error) 2077 goto out; 2078 2079 error = xfs_iunlink_insert_inode(tp, pag, agibp, ip); 2080 out: 2081 xfs_perag_put(pag); 2082 return error; 2083 } 2084 2085 static int 2086 xfs_iunlink_remove_inode( 2087 struct xfs_trans *tp, 2088 struct xfs_perag *pag, 2089 struct xfs_buf *agibp, 2090 struct xfs_inode *ip) 2091 { 2092 struct xfs_mount *mp = tp->t_mountp; 2093 struct xfs_agi *agi = agibp->b_addr; 2094 xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ip->i_ino); 2095 xfs_agino_t head_agino; 2096 short bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS; 2097 int error; 2098 2099 trace_xfs_iunlink_remove(ip); 2100 2101 /* 2102 * Get the index into the agi hash table for the list this inode will 2103 * go on. Make sure the head pointer isn't garbage. 2104 */ 2105 head_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]); 2106 if (!xfs_verify_agino(pag, head_agino)) { 2107 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, 2108 agi, sizeof(*agi)); 2109 return -EFSCORRUPTED; 2110 } 2111 2112 /* 2113 * Set our inode's next_unlinked pointer to NULL and then return 2114 * the old pointer value so that we can update whatever was previous 2115 * to us in the list to point to whatever was next in the list. 2116 */ 2117 error = xfs_iunlink_log_inode(tp, ip, pag, NULLAGINO); 2118 if (error) 2119 return error; 2120 2121 /* 2122 * Update the prev pointer in the next inode to point back to previous 2123 * inode in the chain. 2124 */ 2125 error = xfs_iunlink_update_backref(pag, ip->i_prev_unlinked, 2126 ip->i_next_unlinked); 2127 if (error == -ENOLINK) 2128 error = xfs_iunlink_reload_next(tp, agibp, ip->i_prev_unlinked, 2129 ip->i_next_unlinked); 2130 if (error) 2131 return error; 2132 2133 if (head_agino != agino) { 2134 struct xfs_inode *prev_ip; 2135 2136 prev_ip = xfs_iunlink_lookup(pag, ip->i_prev_unlinked); 2137 if (!prev_ip) 2138 return -EFSCORRUPTED; 2139 2140 error = xfs_iunlink_log_inode(tp, prev_ip, pag, 2141 ip->i_next_unlinked); 2142 prev_ip->i_next_unlinked = ip->i_next_unlinked; 2143 } else { 2144 /* Point the head of the list to the next unlinked inode. */ 2145 error = xfs_iunlink_update_bucket(tp, pag, agibp, bucket_index, 2146 ip->i_next_unlinked); 2147 } 2148 2149 ip->i_next_unlinked = NULLAGINO; 2150 ip->i_prev_unlinked = 0; 2151 return error; 2152 } 2153 2154 /* 2155 * Pull the on-disk inode from the AGI unlinked list. 2156 */ 2157 STATIC int 2158 xfs_iunlink_remove( 2159 struct xfs_trans *tp, 2160 struct xfs_perag *pag, 2161 struct xfs_inode *ip) 2162 { 2163 struct xfs_buf *agibp; 2164 int error; 2165 2166 trace_xfs_iunlink_remove(ip); 2167 2168 /* Get the agi buffer first. It ensures lock ordering on the list. */ 2169 error = xfs_read_agi(pag, tp, &agibp); 2170 if (error) 2171 return error; 2172 2173 return xfs_iunlink_remove_inode(tp, pag, agibp, ip); 2174 } 2175 2176 /* 2177 * Look up the inode number specified and if it is not already marked XFS_ISTALE 2178 * mark it stale. We should only find clean inodes in this lookup that aren't 2179 * already stale. 2180 */ 2181 static void 2182 xfs_ifree_mark_inode_stale( 2183 struct xfs_perag *pag, 2184 struct xfs_inode *free_ip, 2185 xfs_ino_t inum) 2186 { 2187 struct xfs_mount *mp = pag->pag_mount; 2188 struct xfs_inode_log_item *iip; 2189 struct xfs_inode *ip; 2190 2191 retry: 2192 rcu_read_lock(); 2193 ip = radix_tree_lookup(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, inum)); 2194 2195 /* Inode not in memory, nothing to do */ 2196 if (!ip) { 2197 rcu_read_unlock(); 2198 return; 2199 } 2200 2201 /* 2202 * because this is an RCU protected lookup, we could find a recently 2203 * freed or even reallocated inode during the lookup. We need to check 2204 * under the i_flags_lock for a valid inode here. Skip it if it is not 2205 * valid, the wrong inode or stale. 2206 */ 2207 spin_lock(&ip->i_flags_lock); 2208 if (ip->i_ino != inum || __xfs_iflags_test(ip, XFS_ISTALE)) 2209 goto out_iflags_unlock; 2210 2211 /* 2212 * Don't try to lock/unlock the current inode, but we _cannot_ skip the 2213 * other inodes that we did not find in the list attached to the buffer 2214 * and are not already marked stale. If we can't lock it, back off and 2215 * retry. 2216 */ 2217 if (ip != free_ip) { 2218 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) { 2219 spin_unlock(&ip->i_flags_lock); 2220 rcu_read_unlock(); 2221 delay(1); 2222 goto retry; 2223 } 2224 } 2225 ip->i_flags |= XFS_ISTALE; 2226 2227 /* 2228 * If the inode is flushing, it is already attached to the buffer. All 2229 * we needed to do here is mark the inode stale so buffer IO completion 2230 * will remove it from the AIL. 2231 */ 2232 iip = ip->i_itemp; 2233 if (__xfs_iflags_test(ip, XFS_IFLUSHING)) { 2234 ASSERT(!list_empty(&iip->ili_item.li_bio_list)); 2235 ASSERT(iip->ili_last_fields); 2236 goto out_iunlock; 2237 } 2238 2239 /* 2240 * Inodes not attached to the buffer can be released immediately. 2241 * Everything else has to go through xfs_iflush_abort() on journal 2242 * commit as the flock synchronises removal of the inode from the 2243 * cluster buffer against inode reclaim. 2244 */ 2245 if (!iip || list_empty(&iip->ili_item.li_bio_list)) 2246 goto out_iunlock; 2247 2248 __xfs_iflags_set(ip, XFS_IFLUSHING); 2249 spin_unlock(&ip->i_flags_lock); 2250 rcu_read_unlock(); 2251 2252 /* we have a dirty inode in memory that has not yet been flushed. */ 2253 spin_lock(&iip->ili_lock); 2254 iip->ili_last_fields = iip->ili_fields; 2255 iip->ili_fields = 0; 2256 iip->ili_fsync_fields = 0; 2257 spin_unlock(&iip->ili_lock); 2258 ASSERT(iip->ili_last_fields); 2259 2260 if (ip != free_ip) 2261 xfs_iunlock(ip, XFS_ILOCK_EXCL); 2262 return; 2263 2264 out_iunlock: 2265 if (ip != free_ip) 2266 xfs_iunlock(ip, XFS_ILOCK_EXCL); 2267 out_iflags_unlock: 2268 spin_unlock(&ip->i_flags_lock); 2269 rcu_read_unlock(); 2270 } 2271 2272 /* 2273 * A big issue when freeing the inode cluster is that we _cannot_ skip any 2274 * inodes that are in memory - they all must be marked stale and attached to 2275 * the cluster buffer. 2276 */ 2277 static int 2278 xfs_ifree_cluster( 2279 struct xfs_trans *tp, 2280 struct xfs_perag *pag, 2281 struct xfs_inode *free_ip, 2282 struct xfs_icluster *xic) 2283 { 2284 struct xfs_mount *mp = free_ip->i_mount; 2285 struct xfs_ino_geometry *igeo = M_IGEO(mp); 2286 struct xfs_buf *bp; 2287 xfs_daddr_t blkno; 2288 xfs_ino_t inum = xic->first_ino; 2289 int nbufs; 2290 int i, j; 2291 int ioffset; 2292 int error; 2293 2294 nbufs = igeo->ialloc_blks / igeo->blocks_per_cluster; 2295 2296 for (j = 0; j < nbufs; j++, inum += igeo->inodes_per_cluster) { 2297 /* 2298 * The allocation bitmap tells us which inodes of the chunk were 2299 * physically allocated. Skip the cluster if an inode falls into 2300 * a sparse region. 2301 */ 2302 ioffset = inum - xic->first_ino; 2303 if ((xic->alloc & XFS_INOBT_MASK(ioffset)) == 0) { 2304 ASSERT(ioffset % igeo->inodes_per_cluster == 0); 2305 continue; 2306 } 2307 2308 blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum), 2309 XFS_INO_TO_AGBNO(mp, inum)); 2310 2311 /* 2312 * We obtain and lock the backing buffer first in the process 2313 * here to ensure dirty inodes attached to the buffer remain in 2314 * the flushing state while we mark them stale. 2315 * 2316 * If we scan the in-memory inodes first, then buffer IO can 2317 * complete before we get a lock on it, and hence we may fail 2318 * to mark all the active inodes on the buffer stale. 2319 */ 2320 error = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno, 2321 mp->m_bsize * igeo->blocks_per_cluster, 2322 XBF_UNMAPPED, &bp); 2323 if (error) 2324 return error; 2325 2326 /* 2327 * This buffer may not have been correctly initialised as we 2328 * didn't read it from disk. That's not important because we are 2329 * only using to mark the buffer as stale in the log, and to 2330 * attach stale cached inodes on it. 2331 * 2332 * For the inode that triggered the cluster freeing, this 2333 * attachment may occur in xfs_inode_item_precommit() after we 2334 * have marked this buffer stale. If this buffer was not in 2335 * memory before xfs_ifree_cluster() started, it will not be 2336 * marked XBF_DONE and this will cause problems later in 2337 * xfs_inode_item_precommit() when we trip over a (stale, !done) 2338 * buffer to attached to the transaction. 2339 * 2340 * Hence we have to mark the buffer as XFS_DONE here. This is 2341 * safe because we are also marking the buffer as XBF_STALE and 2342 * XFS_BLI_STALE. That means it will never be dispatched for 2343 * IO and it won't be unlocked until the cluster freeing has 2344 * been committed to the journal and the buffer unpinned. If it 2345 * is written, we want to know about it, and we want it to 2346 * fail. We can acheive this by adding a write verifier to the 2347 * buffer. 2348 */ 2349 bp->b_flags |= XBF_DONE; 2350 bp->b_ops = &xfs_inode_buf_ops; 2351 2352 /* 2353 * Now we need to set all the cached clean inodes as XFS_ISTALE, 2354 * too. This requires lookups, and will skip inodes that we've 2355 * already marked XFS_ISTALE. 2356 */ 2357 for (i = 0; i < igeo->inodes_per_cluster; i++) 2358 xfs_ifree_mark_inode_stale(pag, free_ip, inum + i); 2359 2360 xfs_trans_stale_inode_buf(tp, bp); 2361 xfs_trans_binval(tp, bp); 2362 } 2363 return 0; 2364 } 2365 2366 /* 2367 * This is called to return an inode to the inode free list. The inode should 2368 * already be truncated to 0 length and have no pages associated with it. This 2369 * routine also assumes that the inode is already a part of the transaction. 2370 * 2371 * The on-disk copy of the inode will have been added to the list of unlinked 2372 * inodes in the AGI. We need to remove the inode from that list atomically with 2373 * respect to freeing it here. 2374 */ 2375 int 2376 xfs_ifree( 2377 struct xfs_trans *tp, 2378 struct xfs_inode *ip) 2379 { 2380 struct xfs_mount *mp = ip->i_mount; 2381 struct xfs_perag *pag; 2382 struct xfs_icluster xic = { 0 }; 2383 struct xfs_inode_log_item *iip = ip->i_itemp; 2384 int error; 2385 2386 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); 2387 ASSERT(VFS_I(ip)->i_nlink == 0); 2388 ASSERT(ip->i_df.if_nextents == 0); 2389 ASSERT(ip->i_disk_size == 0 || !S_ISREG(VFS_I(ip)->i_mode)); 2390 ASSERT(ip->i_nblocks == 0); 2391 2392 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino)); 2393 2394 /* 2395 * Free the inode first so that we guarantee that the AGI lock is going 2396 * to be taken before we remove the inode from the unlinked list. This 2397 * makes the AGI lock -> unlinked list modification order the same as 2398 * used in O_TMPFILE creation. 2399 */ 2400 error = xfs_difree(tp, pag, ip->i_ino, &xic); 2401 if (error) 2402 goto out; 2403 2404 error = xfs_iunlink_remove(tp, pag, ip); 2405 if (error) 2406 goto out; 2407 2408 /* 2409 * Free any local-format data sitting around before we reset the 2410 * data fork to extents format. Note that the attr fork data has 2411 * already been freed by xfs_attr_inactive. 2412 */ 2413 if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL) { 2414 kmem_free(ip->i_df.if_u1.if_data); 2415 ip->i_df.if_u1.if_data = NULL; 2416 ip->i_df.if_bytes = 0; 2417 } 2418 2419 VFS_I(ip)->i_mode = 0; /* mark incore inode as free */ 2420 ip->i_diflags = 0; 2421 ip->i_diflags2 = mp->m_ino_geo.new_diflags2; 2422 ip->i_forkoff = 0; /* mark the attr fork not in use */ 2423 ip->i_df.if_format = XFS_DINODE_FMT_EXTENTS; 2424 if (xfs_iflags_test(ip, XFS_IPRESERVE_DM_FIELDS)) 2425 xfs_iflags_clear(ip, XFS_IPRESERVE_DM_FIELDS); 2426 2427 /* Don't attempt to replay owner changes for a deleted inode */ 2428 spin_lock(&iip->ili_lock); 2429 iip->ili_fields &= ~(XFS_ILOG_AOWNER | XFS_ILOG_DOWNER); 2430 spin_unlock(&iip->ili_lock); 2431 2432 /* 2433 * Bump the generation count so no one will be confused 2434 * by reincarnations of this inode. 2435 */ 2436 VFS_I(ip)->i_generation++; 2437 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); 2438 2439 if (xic.deleted) 2440 error = xfs_ifree_cluster(tp, pag, ip, &xic); 2441 out: 2442 xfs_perag_put(pag); 2443 return error; 2444 } 2445 2446 /* 2447 * This is called to unpin an inode. The caller must have the inode locked 2448 * in at least shared mode so that the buffer cannot be subsequently pinned 2449 * once someone is waiting for it to be unpinned. 2450 */ 2451 static void 2452 xfs_iunpin( 2453 struct xfs_inode *ip) 2454 { 2455 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)); 2456 2457 trace_xfs_inode_unpin_nowait(ip, _RET_IP_); 2458 2459 /* Give the log a push to start the unpinning I/O */ 2460 xfs_log_force_seq(ip->i_mount, ip->i_itemp->ili_commit_seq, 0, NULL); 2461 2462 } 2463 2464 static void 2465 __xfs_iunpin_wait( 2466 struct xfs_inode *ip) 2467 { 2468 wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT); 2469 DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT); 2470 2471 xfs_iunpin(ip); 2472 2473 do { 2474 prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE); 2475 if (xfs_ipincount(ip)) 2476 io_schedule(); 2477 } while (xfs_ipincount(ip)); 2478 finish_wait(wq, &wait.wq_entry); 2479 } 2480 2481 void 2482 xfs_iunpin_wait( 2483 struct xfs_inode *ip) 2484 { 2485 if (xfs_ipincount(ip)) 2486 __xfs_iunpin_wait(ip); 2487 } 2488 2489 /* 2490 * Removing an inode from the namespace involves removing the directory entry 2491 * and dropping the link count on the inode. Removing the directory entry can 2492 * result in locking an AGF (directory blocks were freed) and removing a link 2493 * count can result in placing the inode on an unlinked list which results in 2494 * locking an AGI. 2495 * 2496 * The big problem here is that we have an ordering constraint on AGF and AGI 2497 * locking - inode allocation locks the AGI, then can allocate a new extent for 2498 * new inodes, locking the AGF after the AGI. Similarly, freeing the inode 2499 * removes the inode from the unlinked list, requiring that we lock the AGI 2500 * first, and then freeing the inode can result in an inode chunk being freed 2501 * and hence freeing disk space requiring that we lock an AGF. 2502 * 2503 * Hence the ordering that is imposed by other parts of the code is AGI before 2504 * AGF. This means we cannot remove the directory entry before we drop the inode 2505 * reference count and put it on the unlinked list as this results in a lock 2506 * order of AGF then AGI, and this can deadlock against inode allocation and 2507 * freeing. Therefore we must drop the link counts before we remove the 2508 * directory entry. 2509 * 2510 * This is still safe from a transactional point of view - it is not until we 2511 * get to xfs_defer_finish() that we have the possibility of multiple 2512 * transactions in this operation. Hence as long as we remove the directory 2513 * entry and drop the link count in the first transaction of the remove 2514 * operation, there are no transactional constraints on the ordering here. 2515 */ 2516 int 2517 xfs_remove( 2518 xfs_inode_t *dp, 2519 struct xfs_name *name, 2520 xfs_inode_t *ip) 2521 { 2522 xfs_mount_t *mp = dp->i_mount; 2523 xfs_trans_t *tp = NULL; 2524 int is_dir = S_ISDIR(VFS_I(ip)->i_mode); 2525 int dontcare; 2526 int error = 0; 2527 uint resblks; 2528 2529 trace_xfs_remove(dp, name); 2530 2531 if (xfs_is_shutdown(mp)) 2532 return -EIO; 2533 2534 error = xfs_qm_dqattach(dp); 2535 if (error) 2536 goto std_return; 2537 2538 error = xfs_qm_dqattach(ip); 2539 if (error) 2540 goto std_return; 2541 2542 /* 2543 * We try to get the real space reservation first, allowing for 2544 * directory btree deletion(s) implying possible bmap insert(s). If we 2545 * can't get the space reservation then we use 0 instead, and avoid the 2546 * bmap btree insert(s) in the directory code by, if the bmap insert 2547 * tries to happen, instead trimming the LAST block from the directory. 2548 * 2549 * Ignore EDQUOT and ENOSPC being returned via nospace_error because 2550 * the directory code can handle a reservationless update and we don't 2551 * want to prevent a user from trying to free space by deleting things. 2552 */ 2553 resblks = XFS_REMOVE_SPACE_RES(mp); 2554 error = xfs_trans_alloc_dir(dp, &M_RES(mp)->tr_remove, ip, &resblks, 2555 &tp, &dontcare); 2556 if (error) { 2557 ASSERT(error != -ENOSPC); 2558 goto std_return; 2559 } 2560 2561 /* 2562 * If we're removing a directory perform some additional validation. 2563 */ 2564 if (is_dir) { 2565 ASSERT(VFS_I(ip)->i_nlink >= 2); 2566 if (VFS_I(ip)->i_nlink != 2) { 2567 error = -ENOTEMPTY; 2568 goto out_trans_cancel; 2569 } 2570 if (!xfs_dir_isempty(ip)) { 2571 error = -ENOTEMPTY; 2572 goto out_trans_cancel; 2573 } 2574 2575 /* Drop the link from ip's "..". */ 2576 error = xfs_droplink(tp, dp); 2577 if (error) 2578 goto out_trans_cancel; 2579 2580 /* Drop the "." link from ip to self. */ 2581 error = xfs_droplink(tp, ip); 2582 if (error) 2583 goto out_trans_cancel; 2584 2585 /* 2586 * Point the unlinked child directory's ".." entry to the root 2587 * directory to eliminate back-references to inodes that may 2588 * get freed before the child directory is closed. If the fs 2589 * gets shrunk, this can lead to dirent inode validation errors. 2590 */ 2591 if (dp->i_ino != tp->t_mountp->m_sb.sb_rootino) { 2592 error = xfs_dir_replace(tp, ip, &xfs_name_dotdot, 2593 tp->t_mountp->m_sb.sb_rootino, 0); 2594 if (error) 2595 goto out_trans_cancel; 2596 } 2597 } else { 2598 /* 2599 * When removing a non-directory we need to log the parent 2600 * inode here. For a directory this is done implicitly 2601 * by the xfs_droplink call for the ".." entry. 2602 */ 2603 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); 2604 } 2605 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); 2606 2607 /* Drop the link from dp to ip. */ 2608 error = xfs_droplink(tp, ip); 2609 if (error) 2610 goto out_trans_cancel; 2611 2612 error = xfs_dir_removename(tp, dp, name, ip->i_ino, resblks); 2613 if (error) { 2614 ASSERT(error != -ENOENT); 2615 goto out_trans_cancel; 2616 } 2617 2618 /* 2619 * If this is a synchronous mount, make sure that the 2620 * remove transaction goes to disk before returning to 2621 * the user. 2622 */ 2623 if (xfs_has_wsync(mp) || xfs_has_dirsync(mp)) 2624 xfs_trans_set_sync(tp); 2625 2626 error = xfs_trans_commit(tp); 2627 if (error) 2628 goto std_return; 2629 2630 if (is_dir && xfs_inode_is_filestream(ip)) 2631 xfs_filestream_deassociate(ip); 2632 2633 return 0; 2634 2635 out_trans_cancel: 2636 xfs_trans_cancel(tp); 2637 std_return: 2638 return error; 2639 } 2640 2641 /* 2642 * Enter all inodes for a rename transaction into a sorted array. 2643 */ 2644 #define __XFS_SORT_INODES 5 2645 STATIC void 2646 xfs_sort_for_rename( 2647 struct xfs_inode *dp1, /* in: old (source) directory inode */ 2648 struct xfs_inode *dp2, /* in: new (target) directory inode */ 2649 struct xfs_inode *ip1, /* in: inode of old entry */ 2650 struct xfs_inode *ip2, /* in: inode of new entry */ 2651 struct xfs_inode *wip, /* in: whiteout inode */ 2652 struct xfs_inode **i_tab,/* out: sorted array of inodes */ 2653 int *num_inodes) /* in/out: inodes in array */ 2654 { 2655 int i, j; 2656 2657 ASSERT(*num_inodes == __XFS_SORT_INODES); 2658 memset(i_tab, 0, *num_inodes * sizeof(struct xfs_inode *)); 2659 2660 /* 2661 * i_tab contains a list of pointers to inodes. We initialize 2662 * the table here & we'll sort it. We will then use it to 2663 * order the acquisition of the inode locks. 2664 * 2665 * Note that the table may contain duplicates. e.g., dp1 == dp2. 2666 */ 2667 i = 0; 2668 i_tab[i++] = dp1; 2669 i_tab[i++] = dp2; 2670 i_tab[i++] = ip1; 2671 if (ip2) 2672 i_tab[i++] = ip2; 2673 if (wip) 2674 i_tab[i++] = wip; 2675 *num_inodes = i; 2676 2677 /* 2678 * Sort the elements via bubble sort. (Remember, there are at 2679 * most 5 elements to sort, so this is adequate.) 2680 */ 2681 for (i = 0; i < *num_inodes; i++) { 2682 for (j = 1; j < *num_inodes; j++) { 2683 if (i_tab[j]->i_ino < i_tab[j-1]->i_ino) { 2684 struct xfs_inode *temp = i_tab[j]; 2685 i_tab[j] = i_tab[j-1]; 2686 i_tab[j-1] = temp; 2687 } 2688 } 2689 } 2690 } 2691 2692 static int 2693 xfs_finish_rename( 2694 struct xfs_trans *tp) 2695 { 2696 /* 2697 * If this is a synchronous mount, make sure that the rename transaction 2698 * goes to disk before returning to the user. 2699 */ 2700 if (xfs_has_wsync(tp->t_mountp) || xfs_has_dirsync(tp->t_mountp)) 2701 xfs_trans_set_sync(tp); 2702 2703 return xfs_trans_commit(tp); 2704 } 2705 2706 /* 2707 * xfs_cross_rename() 2708 * 2709 * responsible for handling RENAME_EXCHANGE flag in renameat2() syscall 2710 */ 2711 STATIC int 2712 xfs_cross_rename( 2713 struct xfs_trans *tp, 2714 struct xfs_inode *dp1, 2715 struct xfs_name *name1, 2716 struct xfs_inode *ip1, 2717 struct xfs_inode *dp2, 2718 struct xfs_name *name2, 2719 struct xfs_inode *ip2, 2720 int spaceres) 2721 { 2722 int error = 0; 2723 int ip1_flags = 0; 2724 int ip2_flags = 0; 2725 int dp2_flags = 0; 2726 2727 /* Swap inode number for dirent in first parent */ 2728 error = xfs_dir_replace(tp, dp1, name1, ip2->i_ino, spaceres); 2729 if (error) 2730 goto out_trans_abort; 2731 2732 /* Swap inode number for dirent in second parent */ 2733 error = xfs_dir_replace(tp, dp2, name2, ip1->i_ino, spaceres); 2734 if (error) 2735 goto out_trans_abort; 2736 2737 /* 2738 * If we're renaming one or more directories across different parents, 2739 * update the respective ".." entries (and link counts) to match the new 2740 * parents. 2741 */ 2742 if (dp1 != dp2) { 2743 dp2_flags = XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG; 2744 2745 if (S_ISDIR(VFS_I(ip2)->i_mode)) { 2746 error = xfs_dir_replace(tp, ip2, &xfs_name_dotdot, 2747 dp1->i_ino, spaceres); 2748 if (error) 2749 goto out_trans_abort; 2750 2751 /* transfer ip2 ".." reference to dp1 */ 2752 if (!S_ISDIR(VFS_I(ip1)->i_mode)) { 2753 error = xfs_droplink(tp, dp2); 2754 if (error) 2755 goto out_trans_abort; 2756 xfs_bumplink(tp, dp1); 2757 } 2758 2759 /* 2760 * Although ip1 isn't changed here, userspace needs 2761 * to be warned about the change, so that applications 2762 * relying on it (like backup ones), will properly 2763 * notify the change 2764 */ 2765 ip1_flags |= XFS_ICHGTIME_CHG; 2766 ip2_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG; 2767 } 2768 2769 if (S_ISDIR(VFS_I(ip1)->i_mode)) { 2770 error = xfs_dir_replace(tp, ip1, &xfs_name_dotdot, 2771 dp2->i_ino, spaceres); 2772 if (error) 2773 goto out_trans_abort; 2774 2775 /* transfer ip1 ".." reference to dp2 */ 2776 if (!S_ISDIR(VFS_I(ip2)->i_mode)) { 2777 error = xfs_droplink(tp, dp1); 2778 if (error) 2779 goto out_trans_abort; 2780 xfs_bumplink(tp, dp2); 2781 } 2782 2783 /* 2784 * Although ip2 isn't changed here, userspace needs 2785 * to be warned about the change, so that applications 2786 * relying on it (like backup ones), will properly 2787 * notify the change 2788 */ 2789 ip1_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG; 2790 ip2_flags |= XFS_ICHGTIME_CHG; 2791 } 2792 } 2793 2794 if (ip1_flags) { 2795 xfs_trans_ichgtime(tp, ip1, ip1_flags); 2796 xfs_trans_log_inode(tp, ip1, XFS_ILOG_CORE); 2797 } 2798 if (ip2_flags) { 2799 xfs_trans_ichgtime(tp, ip2, ip2_flags); 2800 xfs_trans_log_inode(tp, ip2, XFS_ILOG_CORE); 2801 } 2802 if (dp2_flags) { 2803 xfs_trans_ichgtime(tp, dp2, dp2_flags); 2804 xfs_trans_log_inode(tp, dp2, XFS_ILOG_CORE); 2805 } 2806 xfs_trans_ichgtime(tp, dp1, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); 2807 xfs_trans_log_inode(tp, dp1, XFS_ILOG_CORE); 2808 return xfs_finish_rename(tp); 2809 2810 out_trans_abort: 2811 xfs_trans_cancel(tp); 2812 return error; 2813 } 2814 2815 /* 2816 * xfs_rename_alloc_whiteout() 2817 * 2818 * Return a referenced, unlinked, unlocked inode that can be used as a 2819 * whiteout in a rename transaction. We use a tmpfile inode here so that if we 2820 * crash between allocating the inode and linking it into the rename transaction 2821 * recovery will free the inode and we won't leak it. 2822 */ 2823 static int 2824 xfs_rename_alloc_whiteout( 2825 struct mnt_idmap *idmap, 2826 struct xfs_name *src_name, 2827 struct xfs_inode *dp, 2828 struct xfs_inode **wip) 2829 { 2830 struct xfs_inode *tmpfile; 2831 struct qstr name; 2832 int error; 2833 2834 error = xfs_create_tmpfile(idmap, dp, S_IFCHR | WHITEOUT_MODE, 2835 &tmpfile); 2836 if (error) 2837 return error; 2838 2839 name.name = src_name->name; 2840 name.len = src_name->len; 2841 error = xfs_inode_init_security(VFS_I(tmpfile), VFS_I(dp), &name); 2842 if (error) { 2843 xfs_finish_inode_setup(tmpfile); 2844 xfs_irele(tmpfile); 2845 return error; 2846 } 2847 2848 /* 2849 * Prepare the tmpfile inode as if it were created through the VFS. 2850 * Complete the inode setup and flag it as linkable. nlink is already 2851 * zero, so we can skip the drop_nlink. 2852 */ 2853 xfs_setup_iops(tmpfile); 2854 xfs_finish_inode_setup(tmpfile); 2855 VFS_I(tmpfile)->i_state |= I_LINKABLE; 2856 2857 *wip = tmpfile; 2858 return 0; 2859 } 2860 2861 /* 2862 * xfs_rename 2863 */ 2864 int 2865 xfs_rename( 2866 struct mnt_idmap *idmap, 2867 struct xfs_inode *src_dp, 2868 struct xfs_name *src_name, 2869 struct xfs_inode *src_ip, 2870 struct xfs_inode *target_dp, 2871 struct xfs_name *target_name, 2872 struct xfs_inode *target_ip, 2873 unsigned int flags) 2874 { 2875 struct xfs_mount *mp = src_dp->i_mount; 2876 struct xfs_trans *tp; 2877 struct xfs_inode *wip = NULL; /* whiteout inode */ 2878 struct xfs_inode *inodes[__XFS_SORT_INODES]; 2879 int i; 2880 int num_inodes = __XFS_SORT_INODES; 2881 bool new_parent = (src_dp != target_dp); 2882 bool src_is_directory = S_ISDIR(VFS_I(src_ip)->i_mode); 2883 int spaceres; 2884 bool retried = false; 2885 int error, nospace_error = 0; 2886 2887 trace_xfs_rename(src_dp, target_dp, src_name, target_name); 2888 2889 if ((flags & RENAME_EXCHANGE) && !target_ip) 2890 return -EINVAL; 2891 2892 /* 2893 * If we are doing a whiteout operation, allocate the whiteout inode 2894 * we will be placing at the target and ensure the type is set 2895 * appropriately. 2896 */ 2897 if (flags & RENAME_WHITEOUT) { 2898 error = xfs_rename_alloc_whiteout(idmap, src_name, 2899 target_dp, &wip); 2900 if (error) 2901 return error; 2902 2903 /* setup target dirent info as whiteout */ 2904 src_name->type = XFS_DIR3_FT_CHRDEV; 2905 } 2906 2907 xfs_sort_for_rename(src_dp, target_dp, src_ip, target_ip, wip, 2908 inodes, &num_inodes); 2909 2910 retry: 2911 nospace_error = 0; 2912 spaceres = XFS_RENAME_SPACE_RES(mp, target_name->len); 2913 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_rename, spaceres, 0, 0, &tp); 2914 if (error == -ENOSPC) { 2915 nospace_error = error; 2916 spaceres = 0; 2917 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_rename, 0, 0, 0, 2918 &tp); 2919 } 2920 if (error) 2921 goto out_release_wip; 2922 2923 /* 2924 * Attach the dquots to the inodes 2925 */ 2926 error = xfs_qm_vop_rename_dqattach(inodes); 2927 if (error) 2928 goto out_trans_cancel; 2929 2930 /* 2931 * Lock all the participating inodes. Depending upon whether 2932 * the target_name exists in the target directory, and 2933 * whether the target directory is the same as the source 2934 * directory, we can lock from 2 to 5 inodes. 2935 */ 2936 xfs_lock_inodes(inodes, num_inodes, XFS_ILOCK_EXCL); 2937 2938 /* 2939 * Join all the inodes to the transaction. From this point on, 2940 * we can rely on either trans_commit or trans_cancel to unlock 2941 * them. 2942 */ 2943 xfs_trans_ijoin(tp, src_dp, XFS_ILOCK_EXCL); 2944 if (new_parent) 2945 xfs_trans_ijoin(tp, target_dp, XFS_ILOCK_EXCL); 2946 xfs_trans_ijoin(tp, src_ip, XFS_ILOCK_EXCL); 2947 if (target_ip) 2948 xfs_trans_ijoin(tp, target_ip, XFS_ILOCK_EXCL); 2949 if (wip) 2950 xfs_trans_ijoin(tp, wip, XFS_ILOCK_EXCL); 2951 2952 /* 2953 * If we are using project inheritance, we only allow renames 2954 * into our tree when the project IDs are the same; else the 2955 * tree quota mechanism would be circumvented. 2956 */ 2957 if (unlikely((target_dp->i_diflags & XFS_DIFLAG_PROJINHERIT) && 2958 target_dp->i_projid != src_ip->i_projid)) { 2959 error = -EXDEV; 2960 goto out_trans_cancel; 2961 } 2962 2963 /* RENAME_EXCHANGE is unique from here on. */ 2964 if (flags & RENAME_EXCHANGE) 2965 return xfs_cross_rename(tp, src_dp, src_name, src_ip, 2966 target_dp, target_name, target_ip, 2967 spaceres); 2968 2969 /* 2970 * Try to reserve quota to handle an expansion of the target directory. 2971 * We'll allow the rename to continue in reservationless mode if we hit 2972 * a space usage constraint. If we trigger reservationless mode, save 2973 * the errno if there isn't any free space in the target directory. 2974 */ 2975 if (spaceres != 0) { 2976 error = xfs_trans_reserve_quota_nblks(tp, target_dp, spaceres, 2977 0, false); 2978 if (error == -EDQUOT || error == -ENOSPC) { 2979 if (!retried) { 2980 xfs_trans_cancel(tp); 2981 xfs_blockgc_free_quota(target_dp, 0); 2982 retried = true; 2983 goto retry; 2984 } 2985 2986 nospace_error = error; 2987 spaceres = 0; 2988 error = 0; 2989 } 2990 if (error) 2991 goto out_trans_cancel; 2992 } 2993 2994 /* 2995 * Check for expected errors before we dirty the transaction 2996 * so we can return an error without a transaction abort. 2997 */ 2998 if (target_ip == NULL) { 2999 /* 3000 * If there's no space reservation, check the entry will 3001 * fit before actually inserting it. 3002 */ 3003 if (!spaceres) { 3004 error = xfs_dir_canenter(tp, target_dp, target_name); 3005 if (error) 3006 goto out_trans_cancel; 3007 } 3008 } else { 3009 /* 3010 * If target exists and it's a directory, check that whether 3011 * it can be destroyed. 3012 */ 3013 if (S_ISDIR(VFS_I(target_ip)->i_mode) && 3014 (!xfs_dir_isempty(target_ip) || 3015 (VFS_I(target_ip)->i_nlink > 2))) { 3016 error = -EEXIST; 3017 goto out_trans_cancel; 3018 } 3019 } 3020 3021 /* 3022 * Lock the AGI buffers we need to handle bumping the nlink of the 3023 * whiteout inode off the unlinked list and to handle dropping the 3024 * nlink of the target inode. Per locking order rules, do this in 3025 * increasing AG order and before directory block allocation tries to 3026 * grab AGFs because we grab AGIs before AGFs. 3027 * 3028 * The (vfs) caller must ensure that if src is a directory then 3029 * target_ip is either null or an empty directory. 3030 */ 3031 for (i = 0; i < num_inodes && inodes[i] != NULL; i++) { 3032 if (inodes[i] == wip || 3033 (inodes[i] == target_ip && 3034 (VFS_I(target_ip)->i_nlink == 1 || src_is_directory))) { 3035 struct xfs_perag *pag; 3036 struct xfs_buf *bp; 3037 3038 pag = xfs_perag_get(mp, 3039 XFS_INO_TO_AGNO(mp, inodes[i]->i_ino)); 3040 error = xfs_read_agi(pag, tp, &bp); 3041 xfs_perag_put(pag); 3042 if (error) 3043 goto out_trans_cancel; 3044 } 3045 } 3046 3047 /* 3048 * Directory entry creation below may acquire the AGF. Remove 3049 * the whiteout from the unlinked list first to preserve correct 3050 * AGI/AGF locking order. This dirties the transaction so failures 3051 * after this point will abort and log recovery will clean up the 3052 * mess. 3053 * 3054 * For whiteouts, we need to bump the link count on the whiteout 3055 * inode. After this point, we have a real link, clear the tmpfile 3056 * state flag from the inode so it doesn't accidentally get misused 3057 * in future. 3058 */ 3059 if (wip) { 3060 struct xfs_perag *pag; 3061 3062 ASSERT(VFS_I(wip)->i_nlink == 0); 3063 3064 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, wip->i_ino)); 3065 error = xfs_iunlink_remove(tp, pag, wip); 3066 xfs_perag_put(pag); 3067 if (error) 3068 goto out_trans_cancel; 3069 3070 xfs_bumplink(tp, wip); 3071 VFS_I(wip)->i_state &= ~I_LINKABLE; 3072 } 3073 3074 /* 3075 * Set up the target. 3076 */ 3077 if (target_ip == NULL) { 3078 /* 3079 * If target does not exist and the rename crosses 3080 * directories, adjust the target directory link count 3081 * to account for the ".." reference from the new entry. 3082 */ 3083 error = xfs_dir_createname(tp, target_dp, target_name, 3084 src_ip->i_ino, spaceres); 3085 if (error) 3086 goto out_trans_cancel; 3087 3088 xfs_trans_ichgtime(tp, target_dp, 3089 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); 3090 3091 if (new_parent && src_is_directory) { 3092 xfs_bumplink(tp, target_dp); 3093 } 3094 } else { /* target_ip != NULL */ 3095 /* 3096 * Link the source inode under the target name. 3097 * If the source inode is a directory and we are moving 3098 * it across directories, its ".." entry will be 3099 * inconsistent until we replace that down below. 3100 * 3101 * In case there is already an entry with the same 3102 * name at the destination directory, remove it first. 3103 */ 3104 error = xfs_dir_replace(tp, target_dp, target_name, 3105 src_ip->i_ino, spaceres); 3106 if (error) 3107 goto out_trans_cancel; 3108 3109 xfs_trans_ichgtime(tp, target_dp, 3110 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); 3111 3112 /* 3113 * Decrement the link count on the target since the target 3114 * dir no longer points to it. 3115 */ 3116 error = xfs_droplink(tp, target_ip); 3117 if (error) 3118 goto out_trans_cancel; 3119 3120 if (src_is_directory) { 3121 /* 3122 * Drop the link from the old "." entry. 3123 */ 3124 error = xfs_droplink(tp, target_ip); 3125 if (error) 3126 goto out_trans_cancel; 3127 } 3128 } /* target_ip != NULL */ 3129 3130 /* 3131 * Remove the source. 3132 */ 3133 if (new_parent && src_is_directory) { 3134 /* 3135 * Rewrite the ".." entry to point to the new 3136 * directory. 3137 */ 3138 error = xfs_dir_replace(tp, src_ip, &xfs_name_dotdot, 3139 target_dp->i_ino, spaceres); 3140 ASSERT(error != -EEXIST); 3141 if (error) 3142 goto out_trans_cancel; 3143 } 3144 3145 /* 3146 * We always want to hit the ctime on the source inode. 3147 * 3148 * This isn't strictly required by the standards since the source 3149 * inode isn't really being changed, but old unix file systems did 3150 * it and some incremental backup programs won't work without it. 3151 */ 3152 xfs_trans_ichgtime(tp, src_ip, XFS_ICHGTIME_CHG); 3153 xfs_trans_log_inode(tp, src_ip, XFS_ILOG_CORE); 3154 3155 /* 3156 * Adjust the link count on src_dp. This is necessary when 3157 * renaming a directory, either within one parent when 3158 * the target existed, or across two parent directories. 3159 */ 3160 if (src_is_directory && (new_parent || target_ip != NULL)) { 3161 3162 /* 3163 * Decrement link count on src_directory since the 3164 * entry that's moved no longer points to it. 3165 */ 3166 error = xfs_droplink(tp, src_dp); 3167 if (error) 3168 goto out_trans_cancel; 3169 } 3170 3171 /* 3172 * For whiteouts, we only need to update the source dirent with the 3173 * inode number of the whiteout inode rather than removing it 3174 * altogether. 3175 */ 3176 if (wip) 3177 error = xfs_dir_replace(tp, src_dp, src_name, wip->i_ino, 3178 spaceres); 3179 else 3180 error = xfs_dir_removename(tp, src_dp, src_name, src_ip->i_ino, 3181 spaceres); 3182 3183 if (error) 3184 goto out_trans_cancel; 3185 3186 xfs_trans_ichgtime(tp, src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); 3187 xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE); 3188 if (new_parent) 3189 xfs_trans_log_inode(tp, target_dp, XFS_ILOG_CORE); 3190 3191 error = xfs_finish_rename(tp); 3192 if (wip) 3193 xfs_irele(wip); 3194 return error; 3195 3196 out_trans_cancel: 3197 xfs_trans_cancel(tp); 3198 out_release_wip: 3199 if (wip) 3200 xfs_irele(wip); 3201 if (error == -ENOSPC && nospace_error) 3202 error = nospace_error; 3203 return error; 3204 } 3205 3206 static int 3207 xfs_iflush( 3208 struct xfs_inode *ip, 3209 struct xfs_buf *bp) 3210 { 3211 struct xfs_inode_log_item *iip = ip->i_itemp; 3212 struct xfs_dinode *dip; 3213 struct xfs_mount *mp = ip->i_mount; 3214 int error; 3215 3216 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)); 3217 ASSERT(xfs_iflags_test(ip, XFS_IFLUSHING)); 3218 ASSERT(ip->i_df.if_format != XFS_DINODE_FMT_BTREE || 3219 ip->i_df.if_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK)); 3220 ASSERT(iip->ili_item.li_buf == bp); 3221 3222 dip = xfs_buf_offset(bp, ip->i_imap.im_boffset); 3223 3224 /* 3225 * We don't flush the inode if any of the following checks fail, but we 3226 * do still update the log item and attach to the backing buffer as if 3227 * the flush happened. This is a formality to facilitate predictable 3228 * error handling as the caller will shutdown and fail the buffer. 3229 */ 3230 error = -EFSCORRUPTED; 3231 if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC), 3232 mp, XFS_ERRTAG_IFLUSH_1)) { 3233 xfs_alert_tag(mp, XFS_PTAG_IFLUSH, 3234 "%s: Bad inode %llu magic number 0x%x, ptr "PTR_FMT, 3235 __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip); 3236 goto flush_out; 3237 } 3238 if (S_ISREG(VFS_I(ip)->i_mode)) { 3239 if (XFS_TEST_ERROR( 3240 ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS && 3241 ip->i_df.if_format != XFS_DINODE_FMT_BTREE, 3242 mp, XFS_ERRTAG_IFLUSH_3)) { 3243 xfs_alert_tag(mp, XFS_PTAG_IFLUSH, 3244 "%s: Bad regular inode %llu, ptr "PTR_FMT, 3245 __func__, ip->i_ino, ip); 3246 goto flush_out; 3247 } 3248 } else if (S_ISDIR(VFS_I(ip)->i_mode)) { 3249 if (XFS_TEST_ERROR( 3250 ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS && 3251 ip->i_df.if_format != XFS_DINODE_FMT_BTREE && 3252 ip->i_df.if_format != XFS_DINODE_FMT_LOCAL, 3253 mp, XFS_ERRTAG_IFLUSH_4)) { 3254 xfs_alert_tag(mp, XFS_PTAG_IFLUSH, 3255 "%s: Bad directory inode %llu, ptr "PTR_FMT, 3256 __func__, ip->i_ino, ip); 3257 goto flush_out; 3258 } 3259 } 3260 if (XFS_TEST_ERROR(ip->i_df.if_nextents + xfs_ifork_nextents(&ip->i_af) > 3261 ip->i_nblocks, mp, XFS_ERRTAG_IFLUSH_5)) { 3262 xfs_alert_tag(mp, XFS_PTAG_IFLUSH, 3263 "%s: detected corrupt incore inode %llu, " 3264 "total extents = %llu nblocks = %lld, ptr "PTR_FMT, 3265 __func__, ip->i_ino, 3266 ip->i_df.if_nextents + xfs_ifork_nextents(&ip->i_af), 3267 ip->i_nblocks, ip); 3268 goto flush_out; 3269 } 3270 if (XFS_TEST_ERROR(ip->i_forkoff > mp->m_sb.sb_inodesize, 3271 mp, XFS_ERRTAG_IFLUSH_6)) { 3272 xfs_alert_tag(mp, XFS_PTAG_IFLUSH, 3273 "%s: bad inode %llu, forkoff 0x%x, ptr "PTR_FMT, 3274 __func__, ip->i_ino, ip->i_forkoff, ip); 3275 goto flush_out; 3276 } 3277 3278 /* 3279 * Inode item log recovery for v2 inodes are dependent on the flushiter 3280 * count for correct sequencing. We bump the flush iteration count so 3281 * we can detect flushes which postdate a log record during recovery. 3282 * This is redundant as we now log every change and hence this can't 3283 * happen but we need to still do it to ensure backwards compatibility 3284 * with old kernels that predate logging all inode changes. 3285 */ 3286 if (!xfs_has_v3inodes(mp)) 3287 ip->i_flushiter++; 3288 3289 /* 3290 * If there are inline format data / attr forks attached to this inode, 3291 * make sure they are not corrupt. 3292 */ 3293 if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL && 3294 xfs_ifork_verify_local_data(ip)) 3295 goto flush_out; 3296 if (xfs_inode_has_attr_fork(ip) && 3297 ip->i_af.if_format == XFS_DINODE_FMT_LOCAL && 3298 xfs_ifork_verify_local_attr(ip)) 3299 goto flush_out; 3300 3301 /* 3302 * Copy the dirty parts of the inode into the on-disk inode. We always 3303 * copy out the core of the inode, because if the inode is dirty at all 3304 * the core must be. 3305 */ 3306 xfs_inode_to_disk(ip, dip, iip->ili_item.li_lsn); 3307 3308 /* Wrap, we never let the log put out DI_MAX_FLUSH */ 3309 if (!xfs_has_v3inodes(mp)) { 3310 if (ip->i_flushiter == DI_MAX_FLUSH) 3311 ip->i_flushiter = 0; 3312 } 3313 3314 xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK); 3315 if (xfs_inode_has_attr_fork(ip)) 3316 xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK); 3317 3318 /* 3319 * We've recorded everything logged in the inode, so we'd like to clear 3320 * the ili_fields bits so we don't log and flush things unnecessarily. 3321 * However, we can't stop logging all this information until the data 3322 * we've copied into the disk buffer is written to disk. If we did we 3323 * might overwrite the copy of the inode in the log with all the data 3324 * after re-logging only part of it, and in the face of a crash we 3325 * wouldn't have all the data we need to recover. 3326 * 3327 * What we do is move the bits to the ili_last_fields field. When 3328 * logging the inode, these bits are moved back to the ili_fields field. 3329 * In the xfs_buf_inode_iodone() routine we clear ili_last_fields, since 3330 * we know that the information those bits represent is permanently on 3331 * disk. As long as the flush completes before the inode is logged 3332 * again, then both ili_fields and ili_last_fields will be cleared. 3333 */ 3334 error = 0; 3335 flush_out: 3336 spin_lock(&iip->ili_lock); 3337 iip->ili_last_fields = iip->ili_fields; 3338 iip->ili_fields = 0; 3339 iip->ili_fsync_fields = 0; 3340 spin_unlock(&iip->ili_lock); 3341 3342 /* 3343 * Store the current LSN of the inode so that we can tell whether the 3344 * item has moved in the AIL from xfs_buf_inode_iodone(). 3345 */ 3346 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn, 3347 &iip->ili_item.li_lsn); 3348 3349 /* generate the checksum. */ 3350 xfs_dinode_calc_crc(mp, dip); 3351 return error; 3352 } 3353 3354 /* 3355 * Non-blocking flush of dirty inode metadata into the backing buffer. 3356 * 3357 * The caller must have a reference to the inode and hold the cluster buffer 3358 * locked. The function will walk across all the inodes on the cluster buffer it 3359 * can find and lock without blocking, and flush them to the cluster buffer. 3360 * 3361 * On successful flushing of at least one inode, the caller must write out the 3362 * buffer and release it. If no inodes are flushed, -EAGAIN will be returned and 3363 * the caller needs to release the buffer. On failure, the filesystem will be 3364 * shut down, the buffer will have been unlocked and released, and EFSCORRUPTED 3365 * will be returned. 3366 */ 3367 int 3368 xfs_iflush_cluster( 3369 struct xfs_buf *bp) 3370 { 3371 struct xfs_mount *mp = bp->b_mount; 3372 struct xfs_log_item *lip, *n; 3373 struct xfs_inode *ip; 3374 struct xfs_inode_log_item *iip; 3375 int clcount = 0; 3376 int error = 0; 3377 3378 /* 3379 * We must use the safe variant here as on shutdown xfs_iflush_abort() 3380 * will remove itself from the list. 3381 */ 3382 list_for_each_entry_safe(lip, n, &bp->b_li_list, li_bio_list) { 3383 iip = (struct xfs_inode_log_item *)lip; 3384 ip = iip->ili_inode; 3385 3386 /* 3387 * Quick and dirty check to avoid locks if possible. 3388 */ 3389 if (__xfs_iflags_test(ip, XFS_IRECLAIM | XFS_IFLUSHING)) 3390 continue; 3391 if (xfs_ipincount(ip)) 3392 continue; 3393 3394 /* 3395 * The inode is still attached to the buffer, which means it is 3396 * dirty but reclaim might try to grab it. Check carefully for 3397 * that, and grab the ilock while still holding the i_flags_lock 3398 * to guarantee reclaim will not be able to reclaim this inode 3399 * once we drop the i_flags_lock. 3400 */ 3401 spin_lock(&ip->i_flags_lock); 3402 ASSERT(!__xfs_iflags_test(ip, XFS_ISTALE)); 3403 if (__xfs_iflags_test(ip, XFS_IRECLAIM | XFS_IFLUSHING)) { 3404 spin_unlock(&ip->i_flags_lock); 3405 continue; 3406 } 3407 3408 /* 3409 * ILOCK will pin the inode against reclaim and prevent 3410 * concurrent transactions modifying the inode while we are 3411 * flushing the inode. If we get the lock, set the flushing 3412 * state before we drop the i_flags_lock. 3413 */ 3414 if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) { 3415 spin_unlock(&ip->i_flags_lock); 3416 continue; 3417 } 3418 __xfs_iflags_set(ip, XFS_IFLUSHING); 3419 spin_unlock(&ip->i_flags_lock); 3420 3421 /* 3422 * Abort flushing this inode if we are shut down because the 3423 * inode may not currently be in the AIL. This can occur when 3424 * log I/O failure unpins the inode without inserting into the 3425 * AIL, leaving a dirty/unpinned inode attached to the buffer 3426 * that otherwise looks like it should be flushed. 3427 */ 3428 if (xlog_is_shutdown(mp->m_log)) { 3429 xfs_iunpin_wait(ip); 3430 xfs_iflush_abort(ip); 3431 xfs_iunlock(ip, XFS_ILOCK_SHARED); 3432 error = -EIO; 3433 continue; 3434 } 3435 3436 /* don't block waiting on a log force to unpin dirty inodes */ 3437 if (xfs_ipincount(ip)) { 3438 xfs_iflags_clear(ip, XFS_IFLUSHING); 3439 xfs_iunlock(ip, XFS_ILOCK_SHARED); 3440 continue; 3441 } 3442 3443 if (!xfs_inode_clean(ip)) 3444 error = xfs_iflush(ip, bp); 3445 else 3446 xfs_iflags_clear(ip, XFS_IFLUSHING); 3447 xfs_iunlock(ip, XFS_ILOCK_SHARED); 3448 if (error) 3449 break; 3450 clcount++; 3451 } 3452 3453 if (error) { 3454 /* 3455 * Shutdown first so we kill the log before we release this 3456 * buffer. If it is an INODE_ALLOC buffer and pins the tail 3457 * of the log, failing it before the _log_ is shut down can 3458 * result in the log tail being moved forward in the journal 3459 * on disk because log writes can still be taking place. Hence 3460 * unpinning the tail will allow the ICREATE intent to be 3461 * removed from the log an recovery will fail with uninitialised 3462 * inode cluster buffers. 3463 */ 3464 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); 3465 bp->b_flags |= XBF_ASYNC; 3466 xfs_buf_ioend_fail(bp); 3467 return error; 3468 } 3469 3470 if (!clcount) 3471 return -EAGAIN; 3472 3473 XFS_STATS_INC(mp, xs_icluster_flushcnt); 3474 XFS_STATS_ADD(mp, xs_icluster_flushinode, clcount); 3475 return 0; 3476 3477 } 3478 3479 /* Release an inode. */ 3480 void 3481 xfs_irele( 3482 struct xfs_inode *ip) 3483 { 3484 trace_xfs_irele(ip, _RET_IP_); 3485 iput(VFS_I(ip)); 3486 } 3487 3488 /* 3489 * Ensure all commited transactions touching the inode are written to the log. 3490 */ 3491 int 3492 xfs_log_force_inode( 3493 struct xfs_inode *ip) 3494 { 3495 xfs_csn_t seq = 0; 3496 3497 xfs_ilock(ip, XFS_ILOCK_SHARED); 3498 if (xfs_ipincount(ip)) 3499 seq = ip->i_itemp->ili_commit_seq; 3500 xfs_iunlock(ip, XFS_ILOCK_SHARED); 3501 3502 if (!seq) 3503 return 0; 3504 return xfs_log_force_seq(ip->i_mount, seq, XFS_LOG_SYNC, NULL); 3505 } 3506 3507 /* 3508 * Grab the exclusive iolock for a data copy from src to dest, making sure to 3509 * abide vfs locking order (lowest pointer value goes first) and breaking the 3510 * layout leases before proceeding. The loop is needed because we cannot call 3511 * the blocking break_layout() with the iolocks held, and therefore have to 3512 * back out both locks. 3513 */ 3514 static int 3515 xfs_iolock_two_inodes_and_break_layout( 3516 struct inode *src, 3517 struct inode *dest) 3518 { 3519 int error; 3520 3521 if (src > dest) 3522 swap(src, dest); 3523 3524 retry: 3525 /* Wait to break both inodes' layouts before we start locking. */ 3526 error = break_layout(src, true); 3527 if (error) 3528 return error; 3529 if (src != dest) { 3530 error = break_layout(dest, true); 3531 if (error) 3532 return error; 3533 } 3534 3535 /* Lock one inode and make sure nobody got in and leased it. */ 3536 inode_lock(src); 3537 error = break_layout(src, false); 3538 if (error) { 3539 inode_unlock(src); 3540 if (error == -EWOULDBLOCK) 3541 goto retry; 3542 return error; 3543 } 3544 3545 if (src == dest) 3546 return 0; 3547 3548 /* Lock the other inode and make sure nobody got in and leased it. */ 3549 inode_lock_nested(dest, I_MUTEX_NONDIR2); 3550 error = break_layout(dest, false); 3551 if (error) { 3552 inode_unlock(src); 3553 inode_unlock(dest); 3554 if (error == -EWOULDBLOCK) 3555 goto retry; 3556 return error; 3557 } 3558 3559 return 0; 3560 } 3561 3562 static int 3563 xfs_mmaplock_two_inodes_and_break_dax_layout( 3564 struct xfs_inode *ip1, 3565 struct xfs_inode *ip2) 3566 { 3567 int error; 3568 bool retry; 3569 struct page *page; 3570 3571 if (ip1->i_ino > ip2->i_ino) 3572 swap(ip1, ip2); 3573 3574 again: 3575 retry = false; 3576 /* Lock the first inode */ 3577 xfs_ilock(ip1, XFS_MMAPLOCK_EXCL); 3578 error = xfs_break_dax_layouts(VFS_I(ip1), &retry); 3579 if (error || retry) { 3580 xfs_iunlock(ip1, XFS_MMAPLOCK_EXCL); 3581 if (error == 0 && retry) 3582 goto again; 3583 return error; 3584 } 3585 3586 if (ip1 == ip2) 3587 return 0; 3588 3589 /* Nested lock the second inode */ 3590 xfs_ilock(ip2, xfs_lock_inumorder(XFS_MMAPLOCK_EXCL, 1)); 3591 /* 3592 * We cannot use xfs_break_dax_layouts() directly here because it may 3593 * need to unlock & lock the XFS_MMAPLOCK_EXCL which is not suitable 3594 * for this nested lock case. 3595 */ 3596 page = dax_layout_busy_page(VFS_I(ip2)->i_mapping); 3597 if (page && page_ref_count(page) != 1) { 3598 xfs_iunlock(ip2, XFS_MMAPLOCK_EXCL); 3599 xfs_iunlock(ip1, XFS_MMAPLOCK_EXCL); 3600 goto again; 3601 } 3602 3603 return 0; 3604 } 3605 3606 /* 3607 * Lock two inodes so that userspace cannot initiate I/O via file syscalls or 3608 * mmap activity. 3609 */ 3610 int 3611 xfs_ilock2_io_mmap( 3612 struct xfs_inode *ip1, 3613 struct xfs_inode *ip2) 3614 { 3615 int ret; 3616 3617 ret = xfs_iolock_two_inodes_and_break_layout(VFS_I(ip1), VFS_I(ip2)); 3618 if (ret) 3619 return ret; 3620 3621 if (IS_DAX(VFS_I(ip1)) && IS_DAX(VFS_I(ip2))) { 3622 ret = xfs_mmaplock_two_inodes_and_break_dax_layout(ip1, ip2); 3623 if (ret) { 3624 inode_unlock(VFS_I(ip2)); 3625 if (ip1 != ip2) 3626 inode_unlock(VFS_I(ip1)); 3627 return ret; 3628 } 3629 } else 3630 filemap_invalidate_lock_two(VFS_I(ip1)->i_mapping, 3631 VFS_I(ip2)->i_mapping); 3632 3633 return 0; 3634 } 3635 3636 /* Unlock both inodes to allow IO and mmap activity. */ 3637 void 3638 xfs_iunlock2_io_mmap( 3639 struct xfs_inode *ip1, 3640 struct xfs_inode *ip2) 3641 { 3642 if (IS_DAX(VFS_I(ip1)) && IS_DAX(VFS_I(ip2))) { 3643 xfs_iunlock(ip2, XFS_MMAPLOCK_EXCL); 3644 if (ip1 != ip2) 3645 xfs_iunlock(ip1, XFS_MMAPLOCK_EXCL); 3646 } else 3647 filemap_invalidate_unlock_two(VFS_I(ip1)->i_mapping, 3648 VFS_I(ip2)->i_mapping); 3649 3650 inode_unlock(VFS_I(ip2)); 3651 if (ip1 != ip2) 3652 inode_unlock(VFS_I(ip1)); 3653 } 3654 3655 /* Drop the MMAPLOCK and the IOLOCK after a remap completes. */ 3656 void 3657 xfs_iunlock2_remapping( 3658 struct xfs_inode *ip1, 3659 struct xfs_inode *ip2) 3660 { 3661 xfs_iflags_clear(ip1, XFS_IREMAPPING); 3662 3663 if (ip1 != ip2) 3664 xfs_iunlock(ip1, XFS_MMAPLOCK_SHARED); 3665 xfs_iunlock(ip2, XFS_MMAPLOCK_EXCL); 3666 3667 if (ip1 != ip2) 3668 inode_unlock_shared(VFS_I(ip1)); 3669 inode_unlock(VFS_I(ip2)); 3670 } 3671 3672 /* 3673 * Reload the incore inode list for this inode. Caller should ensure that 3674 * the link count cannot change, either by taking ILOCK_SHARED or otherwise 3675 * preventing other threads from executing. 3676 */ 3677 int 3678 xfs_inode_reload_unlinked_bucket( 3679 struct xfs_trans *tp, 3680 struct xfs_inode *ip) 3681 { 3682 struct xfs_mount *mp = tp->t_mountp; 3683 struct xfs_buf *agibp; 3684 struct xfs_agi *agi; 3685 struct xfs_perag *pag; 3686 xfs_agnumber_t agno = XFS_INO_TO_AGNO(mp, ip->i_ino); 3687 xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ip->i_ino); 3688 xfs_agino_t prev_agino, next_agino; 3689 unsigned int bucket; 3690 bool foundit = false; 3691 int error; 3692 3693 /* Grab the first inode in the list */ 3694 pag = xfs_perag_get(mp, agno); 3695 error = xfs_ialloc_read_agi(pag, tp, &agibp); 3696 xfs_perag_put(pag); 3697 if (error) 3698 return error; 3699 3700 /* 3701 * We've taken ILOCK_SHARED and the AGI buffer lock to stabilize the 3702 * incore unlinked list pointers for this inode. Check once more to 3703 * see if we raced with anyone else to reload the unlinked list. 3704 */ 3705 if (!xfs_inode_unlinked_incomplete(ip)) { 3706 foundit = true; 3707 goto out_agibp; 3708 } 3709 3710 bucket = agino % XFS_AGI_UNLINKED_BUCKETS; 3711 agi = agibp->b_addr; 3712 3713 trace_xfs_inode_reload_unlinked_bucket(ip); 3714 3715 xfs_info_ratelimited(mp, 3716 "Found unrecovered unlinked inode 0x%x in AG 0x%x. Initiating list recovery.", 3717 agino, agno); 3718 3719 prev_agino = NULLAGINO; 3720 next_agino = be32_to_cpu(agi->agi_unlinked[bucket]); 3721 while (next_agino != NULLAGINO) { 3722 struct xfs_inode *next_ip = NULL; 3723 3724 /* Found this caller's inode, set its backlink. */ 3725 if (next_agino == agino) { 3726 next_ip = ip; 3727 next_ip->i_prev_unlinked = prev_agino; 3728 foundit = true; 3729 goto next_inode; 3730 } 3731 3732 /* Try in-memory lookup first. */ 3733 next_ip = xfs_iunlink_lookup(pag, next_agino); 3734 if (next_ip) 3735 goto next_inode; 3736 3737 /* Inode not in memory, try reloading it. */ 3738 error = xfs_iunlink_reload_next(tp, agibp, prev_agino, 3739 next_agino); 3740 if (error) 3741 break; 3742 3743 /* Grab the reloaded inode. */ 3744 next_ip = xfs_iunlink_lookup(pag, next_agino); 3745 if (!next_ip) { 3746 /* No incore inode at all? We reloaded it... */ 3747 ASSERT(next_ip != NULL); 3748 error = -EFSCORRUPTED; 3749 break; 3750 } 3751 3752 next_inode: 3753 prev_agino = next_agino; 3754 next_agino = next_ip->i_next_unlinked; 3755 } 3756 3757 out_agibp: 3758 xfs_trans_brelse(tp, agibp); 3759 /* Should have found this inode somewhere in the iunlinked bucket. */ 3760 if (!error && !foundit) 3761 error = -EFSCORRUPTED; 3762 return error; 3763 } 3764 3765 /* Decide if this inode is missing its unlinked list and reload it. */ 3766 int 3767 xfs_inode_reload_unlinked( 3768 struct xfs_inode *ip) 3769 { 3770 struct xfs_trans *tp; 3771 int error; 3772 3773 error = xfs_trans_alloc_empty(ip->i_mount, &tp); 3774 if (error) 3775 return error; 3776 3777 xfs_ilock(ip, XFS_ILOCK_SHARED); 3778 if (xfs_inode_unlinked_incomplete(ip)) 3779 error = xfs_inode_reload_unlinked_bucket(tp, ip); 3780 xfs_iunlock(ip, XFS_ILOCK_SHARED); 3781 xfs_trans_cancel(tp); 3782 3783 return error; 3784 } 3785 3786 /* Returns the size of fundamental allocation unit for a file, in bytes. */ 3787 unsigned int 3788 xfs_inode_alloc_unitsize( 3789 struct xfs_inode *ip) 3790 { 3791 unsigned int blocks = 1; 3792 3793 if (XFS_IS_REALTIME_INODE(ip)) 3794 blocks = ip->i_mount->m_sb.sb_rextsize; 3795 3796 return XFS_FSB_TO_B(ip->i_mount, blocks); 3797 } 3798