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