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