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. 2598 * The inode should already be truncated to 0 length and have 2599 * no pages associated with it. This routine also assumes that 2600 * the inode is already a part of the transaction. 2601 * 2602 * The on-disk copy of the inode will have been added to the list 2603 * of unlinked inodes in the AGI. We need to remove the inode from 2604 * that list atomically with respect to freeing it here. 2605 */ 2606 int 2607 xfs_ifree( 2608 struct xfs_trans *tp, 2609 struct xfs_inode *ip) 2610 { 2611 struct xfs_mount *mp = ip->i_mount; 2612 struct xfs_perag *pag; 2613 struct xfs_icluster xic = { 0 }; 2614 struct xfs_inode_log_item *iip = ip->i_itemp; 2615 int error; 2616 2617 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); 2618 ASSERT(VFS_I(ip)->i_nlink == 0); 2619 ASSERT(ip->i_df.if_nextents == 0); 2620 ASSERT(ip->i_disk_size == 0 || !S_ISREG(VFS_I(ip)->i_mode)); 2621 ASSERT(ip->i_nblocks == 0); 2622 2623 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino)); 2624 2625 /* 2626 * Pull the on-disk inode from the AGI unlinked list. 2627 */ 2628 error = xfs_iunlink_remove(tp, pag, ip); 2629 if (error) 2630 goto out; 2631 2632 error = xfs_difree(tp, pag, ip->i_ino, &xic); 2633 if (error) 2634 goto out; 2635 2636 /* 2637 * Free any local-format data sitting around before we reset the 2638 * data fork to extents format. Note that the attr fork data has 2639 * already been freed by xfs_attr_inactive. 2640 */ 2641 if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL) { 2642 kmem_free(ip->i_df.if_u1.if_data); 2643 ip->i_df.if_u1.if_data = NULL; 2644 ip->i_df.if_bytes = 0; 2645 } 2646 2647 VFS_I(ip)->i_mode = 0; /* mark incore inode as free */ 2648 ip->i_diflags = 0; 2649 ip->i_diflags2 = mp->m_ino_geo.new_diflags2; 2650 ip->i_forkoff = 0; /* mark the attr fork not in use */ 2651 ip->i_df.if_format = XFS_DINODE_FMT_EXTENTS; 2652 if (xfs_iflags_test(ip, XFS_IPRESERVE_DM_FIELDS)) 2653 xfs_iflags_clear(ip, XFS_IPRESERVE_DM_FIELDS); 2654 2655 /* Don't attempt to replay owner changes for a deleted inode */ 2656 spin_lock(&iip->ili_lock); 2657 iip->ili_fields &= ~(XFS_ILOG_AOWNER | XFS_ILOG_DOWNER); 2658 spin_unlock(&iip->ili_lock); 2659 2660 /* 2661 * Bump the generation count so no one will be confused 2662 * by reincarnations of this inode. 2663 */ 2664 VFS_I(ip)->i_generation++; 2665 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); 2666 2667 if (xic.deleted) 2668 error = xfs_ifree_cluster(tp, pag, ip, &xic); 2669 out: 2670 xfs_perag_put(pag); 2671 return error; 2672 } 2673 2674 /* 2675 * This is called to unpin an inode. The caller must have the inode locked 2676 * in at least shared mode so that the buffer cannot be subsequently pinned 2677 * once someone is waiting for it to be unpinned. 2678 */ 2679 static void 2680 xfs_iunpin( 2681 struct xfs_inode *ip) 2682 { 2683 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)); 2684 2685 trace_xfs_inode_unpin_nowait(ip, _RET_IP_); 2686 2687 /* Give the log a push to start the unpinning I/O */ 2688 xfs_log_force_seq(ip->i_mount, ip->i_itemp->ili_commit_seq, 0, NULL); 2689 2690 } 2691 2692 static void 2693 __xfs_iunpin_wait( 2694 struct xfs_inode *ip) 2695 { 2696 wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT); 2697 DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT); 2698 2699 xfs_iunpin(ip); 2700 2701 do { 2702 prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE); 2703 if (xfs_ipincount(ip)) 2704 io_schedule(); 2705 } while (xfs_ipincount(ip)); 2706 finish_wait(wq, &wait.wq_entry); 2707 } 2708 2709 void 2710 xfs_iunpin_wait( 2711 struct xfs_inode *ip) 2712 { 2713 if (xfs_ipincount(ip)) 2714 __xfs_iunpin_wait(ip); 2715 } 2716 2717 /* 2718 * Removing an inode from the namespace involves removing the directory entry 2719 * and dropping the link count on the inode. Removing the directory entry can 2720 * result in locking an AGF (directory blocks were freed) and removing a link 2721 * count can result in placing the inode on an unlinked list which results in 2722 * locking an AGI. 2723 * 2724 * The big problem here is that we have an ordering constraint on AGF and AGI 2725 * locking - inode allocation locks the AGI, then can allocate a new extent for 2726 * new inodes, locking the AGF after the AGI. Similarly, freeing the inode 2727 * removes the inode from the unlinked list, requiring that we lock the AGI 2728 * first, and then freeing the inode can result in an inode chunk being freed 2729 * and hence freeing disk space requiring that we lock an AGF. 2730 * 2731 * Hence the ordering that is imposed by other parts of the code is AGI before 2732 * AGF. This means we cannot remove the directory entry before we drop the inode 2733 * reference count and put it on the unlinked list as this results in a lock 2734 * order of AGF then AGI, and this can deadlock against inode allocation and 2735 * freeing. Therefore we must drop the link counts before we remove the 2736 * directory entry. 2737 * 2738 * This is still safe from a transactional point of view - it is not until we 2739 * get to xfs_defer_finish() that we have the possibility of multiple 2740 * transactions in this operation. Hence as long as we remove the directory 2741 * entry and drop the link count in the first transaction of the remove 2742 * operation, there are no transactional constraints on the ordering here. 2743 */ 2744 int 2745 xfs_remove( 2746 xfs_inode_t *dp, 2747 struct xfs_name *name, 2748 xfs_inode_t *ip) 2749 { 2750 xfs_mount_t *mp = dp->i_mount; 2751 xfs_trans_t *tp = NULL; 2752 int is_dir = S_ISDIR(VFS_I(ip)->i_mode); 2753 int dontcare; 2754 int error = 0; 2755 uint resblks; 2756 2757 trace_xfs_remove(dp, name); 2758 2759 if (xfs_is_shutdown(mp)) 2760 return -EIO; 2761 2762 error = xfs_qm_dqattach(dp); 2763 if (error) 2764 goto std_return; 2765 2766 error = xfs_qm_dqattach(ip); 2767 if (error) 2768 goto std_return; 2769 2770 /* 2771 * We try to get the real space reservation first, allowing for 2772 * directory btree deletion(s) implying possible bmap insert(s). If we 2773 * can't get the space reservation then we use 0 instead, and avoid the 2774 * bmap btree insert(s) in the directory code by, if the bmap insert 2775 * tries to happen, instead trimming the LAST block from the directory. 2776 * 2777 * Ignore EDQUOT and ENOSPC being returned via nospace_error because 2778 * the directory code can handle a reservationless update and we don't 2779 * want to prevent a user from trying to free space by deleting things. 2780 */ 2781 resblks = XFS_REMOVE_SPACE_RES(mp); 2782 error = xfs_trans_alloc_dir(dp, &M_RES(mp)->tr_remove, ip, &resblks, 2783 &tp, &dontcare); 2784 if (error) { 2785 ASSERT(error != -ENOSPC); 2786 goto std_return; 2787 } 2788 2789 /* 2790 * If we're removing a directory perform some additional validation. 2791 */ 2792 if (is_dir) { 2793 ASSERT(VFS_I(ip)->i_nlink >= 2); 2794 if (VFS_I(ip)->i_nlink != 2) { 2795 error = -ENOTEMPTY; 2796 goto out_trans_cancel; 2797 } 2798 if (!xfs_dir_isempty(ip)) { 2799 error = -ENOTEMPTY; 2800 goto out_trans_cancel; 2801 } 2802 2803 /* Drop the link from ip's "..". */ 2804 error = xfs_droplink(tp, dp); 2805 if (error) 2806 goto out_trans_cancel; 2807 2808 /* Drop the "." link from ip to self. */ 2809 error = xfs_droplink(tp, ip); 2810 if (error) 2811 goto out_trans_cancel; 2812 2813 /* 2814 * Point the unlinked child directory's ".." entry to the root 2815 * directory to eliminate back-references to inodes that may 2816 * get freed before the child directory is closed. If the fs 2817 * gets shrunk, this can lead to dirent inode validation errors. 2818 */ 2819 if (dp->i_ino != tp->t_mountp->m_sb.sb_rootino) { 2820 error = xfs_dir_replace(tp, ip, &xfs_name_dotdot, 2821 tp->t_mountp->m_sb.sb_rootino, 0); 2822 if (error) 2823 return error; 2824 } 2825 } else { 2826 /* 2827 * When removing a non-directory we need to log the parent 2828 * inode here. For a directory this is done implicitly 2829 * by the xfs_droplink call for the ".." entry. 2830 */ 2831 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); 2832 } 2833 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); 2834 2835 /* Drop the link from dp to ip. */ 2836 error = xfs_droplink(tp, ip); 2837 if (error) 2838 goto out_trans_cancel; 2839 2840 error = xfs_dir_removename(tp, dp, name, ip->i_ino, resblks); 2841 if (error) { 2842 ASSERT(error != -ENOENT); 2843 goto out_trans_cancel; 2844 } 2845 2846 /* 2847 * If this is a synchronous mount, make sure that the 2848 * remove transaction goes to disk before returning to 2849 * the user. 2850 */ 2851 if (xfs_has_wsync(mp) || xfs_has_dirsync(mp)) 2852 xfs_trans_set_sync(tp); 2853 2854 error = xfs_trans_commit(tp); 2855 if (error) 2856 goto std_return; 2857 2858 if (is_dir && xfs_inode_is_filestream(ip)) 2859 xfs_filestream_deassociate(ip); 2860 2861 return 0; 2862 2863 out_trans_cancel: 2864 xfs_trans_cancel(tp); 2865 std_return: 2866 return error; 2867 } 2868 2869 /* 2870 * Enter all inodes for a rename transaction into a sorted array. 2871 */ 2872 #define __XFS_SORT_INODES 5 2873 STATIC void 2874 xfs_sort_for_rename( 2875 struct xfs_inode *dp1, /* in: old (source) directory inode */ 2876 struct xfs_inode *dp2, /* in: new (target) directory inode */ 2877 struct xfs_inode *ip1, /* in: inode of old entry */ 2878 struct xfs_inode *ip2, /* in: inode of new entry */ 2879 struct xfs_inode *wip, /* in: whiteout inode */ 2880 struct xfs_inode **i_tab,/* out: sorted array of inodes */ 2881 int *num_inodes) /* in/out: inodes in array */ 2882 { 2883 int i, j; 2884 2885 ASSERT(*num_inodes == __XFS_SORT_INODES); 2886 memset(i_tab, 0, *num_inodes * sizeof(struct xfs_inode *)); 2887 2888 /* 2889 * i_tab contains a list of pointers to inodes. We initialize 2890 * the table here & we'll sort it. We will then use it to 2891 * order the acquisition of the inode locks. 2892 * 2893 * Note that the table may contain duplicates. e.g., dp1 == dp2. 2894 */ 2895 i = 0; 2896 i_tab[i++] = dp1; 2897 i_tab[i++] = dp2; 2898 i_tab[i++] = ip1; 2899 if (ip2) 2900 i_tab[i++] = ip2; 2901 if (wip) 2902 i_tab[i++] = wip; 2903 *num_inodes = i; 2904 2905 /* 2906 * Sort the elements via bubble sort. (Remember, there are at 2907 * most 5 elements to sort, so this is adequate.) 2908 */ 2909 for (i = 0; i < *num_inodes; i++) { 2910 for (j = 1; j < *num_inodes; j++) { 2911 if (i_tab[j]->i_ino < i_tab[j-1]->i_ino) { 2912 struct xfs_inode *temp = i_tab[j]; 2913 i_tab[j] = i_tab[j-1]; 2914 i_tab[j-1] = temp; 2915 } 2916 } 2917 } 2918 } 2919 2920 static int 2921 xfs_finish_rename( 2922 struct xfs_trans *tp) 2923 { 2924 /* 2925 * If this is a synchronous mount, make sure that the rename transaction 2926 * goes to disk before returning to the user. 2927 */ 2928 if (xfs_has_wsync(tp->t_mountp) || xfs_has_dirsync(tp->t_mountp)) 2929 xfs_trans_set_sync(tp); 2930 2931 return xfs_trans_commit(tp); 2932 } 2933 2934 /* 2935 * xfs_cross_rename() 2936 * 2937 * responsible for handling RENAME_EXCHANGE flag in renameat2() syscall 2938 */ 2939 STATIC int 2940 xfs_cross_rename( 2941 struct xfs_trans *tp, 2942 struct xfs_inode *dp1, 2943 struct xfs_name *name1, 2944 struct xfs_inode *ip1, 2945 struct xfs_inode *dp2, 2946 struct xfs_name *name2, 2947 struct xfs_inode *ip2, 2948 int spaceres) 2949 { 2950 int error = 0; 2951 int ip1_flags = 0; 2952 int ip2_flags = 0; 2953 int dp2_flags = 0; 2954 2955 /* Swap inode number for dirent in first parent */ 2956 error = xfs_dir_replace(tp, dp1, name1, ip2->i_ino, spaceres); 2957 if (error) 2958 goto out_trans_abort; 2959 2960 /* Swap inode number for dirent in second parent */ 2961 error = xfs_dir_replace(tp, dp2, name2, ip1->i_ino, spaceres); 2962 if (error) 2963 goto out_trans_abort; 2964 2965 /* 2966 * If we're renaming one or more directories across different parents, 2967 * update the respective ".." entries (and link counts) to match the new 2968 * parents. 2969 */ 2970 if (dp1 != dp2) { 2971 dp2_flags = XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG; 2972 2973 if (S_ISDIR(VFS_I(ip2)->i_mode)) { 2974 error = xfs_dir_replace(tp, ip2, &xfs_name_dotdot, 2975 dp1->i_ino, spaceres); 2976 if (error) 2977 goto out_trans_abort; 2978 2979 /* transfer ip2 ".." reference to dp1 */ 2980 if (!S_ISDIR(VFS_I(ip1)->i_mode)) { 2981 error = xfs_droplink(tp, dp2); 2982 if (error) 2983 goto out_trans_abort; 2984 xfs_bumplink(tp, dp1); 2985 } 2986 2987 /* 2988 * Although ip1 isn't changed here, userspace needs 2989 * to be warned about the change, so that applications 2990 * relying on it (like backup ones), will properly 2991 * notify the change 2992 */ 2993 ip1_flags |= XFS_ICHGTIME_CHG; 2994 ip2_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG; 2995 } 2996 2997 if (S_ISDIR(VFS_I(ip1)->i_mode)) { 2998 error = xfs_dir_replace(tp, ip1, &xfs_name_dotdot, 2999 dp2->i_ino, spaceres); 3000 if (error) 3001 goto out_trans_abort; 3002 3003 /* transfer ip1 ".." reference to dp2 */ 3004 if (!S_ISDIR(VFS_I(ip2)->i_mode)) { 3005 error = xfs_droplink(tp, dp1); 3006 if (error) 3007 goto out_trans_abort; 3008 xfs_bumplink(tp, dp2); 3009 } 3010 3011 /* 3012 * Although ip2 isn't changed here, userspace needs 3013 * to be warned about the change, so that applications 3014 * relying on it (like backup ones), will properly 3015 * notify the change 3016 */ 3017 ip1_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG; 3018 ip2_flags |= XFS_ICHGTIME_CHG; 3019 } 3020 } 3021 3022 if (ip1_flags) { 3023 xfs_trans_ichgtime(tp, ip1, ip1_flags); 3024 xfs_trans_log_inode(tp, ip1, XFS_ILOG_CORE); 3025 } 3026 if (ip2_flags) { 3027 xfs_trans_ichgtime(tp, ip2, ip2_flags); 3028 xfs_trans_log_inode(tp, ip2, XFS_ILOG_CORE); 3029 } 3030 if (dp2_flags) { 3031 xfs_trans_ichgtime(tp, dp2, dp2_flags); 3032 xfs_trans_log_inode(tp, dp2, XFS_ILOG_CORE); 3033 } 3034 xfs_trans_ichgtime(tp, dp1, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); 3035 xfs_trans_log_inode(tp, dp1, XFS_ILOG_CORE); 3036 return xfs_finish_rename(tp); 3037 3038 out_trans_abort: 3039 xfs_trans_cancel(tp); 3040 return error; 3041 } 3042 3043 /* 3044 * xfs_rename_alloc_whiteout() 3045 * 3046 * Return a referenced, unlinked, unlocked inode that can be used as a 3047 * whiteout in a rename transaction. We use a tmpfile inode here so that if we 3048 * crash between allocating the inode and linking it into the rename transaction 3049 * recovery will free the inode and we won't leak it. 3050 */ 3051 static int 3052 xfs_rename_alloc_whiteout( 3053 struct user_namespace *mnt_userns, 3054 struct xfs_inode *dp, 3055 struct xfs_inode **wip) 3056 { 3057 struct xfs_inode *tmpfile; 3058 int error; 3059 3060 error = xfs_create_tmpfile(mnt_userns, dp, S_IFCHR | WHITEOUT_MODE, 3061 &tmpfile); 3062 if (error) 3063 return error; 3064 3065 /* 3066 * Prepare the tmpfile inode as if it were created through the VFS. 3067 * Complete the inode setup and flag it as linkable. nlink is already 3068 * zero, so we can skip the drop_nlink. 3069 */ 3070 xfs_setup_iops(tmpfile); 3071 xfs_finish_inode_setup(tmpfile); 3072 VFS_I(tmpfile)->i_state |= I_LINKABLE; 3073 3074 *wip = tmpfile; 3075 return 0; 3076 } 3077 3078 /* 3079 * xfs_rename 3080 */ 3081 int 3082 xfs_rename( 3083 struct user_namespace *mnt_userns, 3084 struct xfs_inode *src_dp, 3085 struct xfs_name *src_name, 3086 struct xfs_inode *src_ip, 3087 struct xfs_inode *target_dp, 3088 struct xfs_name *target_name, 3089 struct xfs_inode *target_ip, 3090 unsigned int flags) 3091 { 3092 struct xfs_mount *mp = src_dp->i_mount; 3093 struct xfs_trans *tp; 3094 struct xfs_inode *wip = NULL; /* whiteout inode */ 3095 struct xfs_inode *inodes[__XFS_SORT_INODES]; 3096 int i; 3097 int num_inodes = __XFS_SORT_INODES; 3098 bool new_parent = (src_dp != target_dp); 3099 bool src_is_directory = S_ISDIR(VFS_I(src_ip)->i_mode); 3100 int spaceres; 3101 bool retried = false; 3102 int error, nospace_error = 0; 3103 3104 trace_xfs_rename(src_dp, target_dp, src_name, target_name); 3105 3106 if ((flags & RENAME_EXCHANGE) && !target_ip) 3107 return -EINVAL; 3108 3109 /* 3110 * If we are doing a whiteout operation, allocate the whiteout inode 3111 * we will be placing at the target and ensure the type is set 3112 * appropriately. 3113 */ 3114 if (flags & RENAME_WHITEOUT) { 3115 error = xfs_rename_alloc_whiteout(mnt_userns, target_dp, &wip); 3116 if (error) 3117 return error; 3118 3119 /* setup target dirent info as whiteout */ 3120 src_name->type = XFS_DIR3_FT_CHRDEV; 3121 } 3122 3123 xfs_sort_for_rename(src_dp, target_dp, src_ip, target_ip, wip, 3124 inodes, &num_inodes); 3125 3126 retry: 3127 nospace_error = 0; 3128 spaceres = XFS_RENAME_SPACE_RES(mp, target_name->len); 3129 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_rename, spaceres, 0, 0, &tp); 3130 if (error == -ENOSPC) { 3131 nospace_error = error; 3132 spaceres = 0; 3133 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_rename, 0, 0, 0, 3134 &tp); 3135 } 3136 if (error) 3137 goto out_release_wip; 3138 3139 /* 3140 * Attach the dquots to the inodes 3141 */ 3142 error = xfs_qm_vop_rename_dqattach(inodes); 3143 if (error) 3144 goto out_trans_cancel; 3145 3146 /* 3147 * Lock all the participating inodes. Depending upon whether 3148 * the target_name exists in the target directory, and 3149 * whether the target directory is the same as the source 3150 * directory, we can lock from 2 to 4 inodes. 3151 */ 3152 xfs_lock_inodes(inodes, num_inodes, XFS_ILOCK_EXCL); 3153 3154 /* 3155 * Join all the inodes to the transaction. From this point on, 3156 * we can rely on either trans_commit or trans_cancel to unlock 3157 * them. 3158 */ 3159 xfs_trans_ijoin(tp, src_dp, XFS_ILOCK_EXCL); 3160 if (new_parent) 3161 xfs_trans_ijoin(tp, target_dp, XFS_ILOCK_EXCL); 3162 xfs_trans_ijoin(tp, src_ip, XFS_ILOCK_EXCL); 3163 if (target_ip) 3164 xfs_trans_ijoin(tp, target_ip, XFS_ILOCK_EXCL); 3165 if (wip) 3166 xfs_trans_ijoin(tp, wip, XFS_ILOCK_EXCL); 3167 3168 /* 3169 * If we are using project inheritance, we only allow renames 3170 * into our tree when the project IDs are the same; else the 3171 * tree quota mechanism would be circumvented. 3172 */ 3173 if (unlikely((target_dp->i_diflags & XFS_DIFLAG_PROJINHERIT) && 3174 target_dp->i_projid != src_ip->i_projid)) { 3175 error = -EXDEV; 3176 goto out_trans_cancel; 3177 } 3178 3179 /* RENAME_EXCHANGE is unique from here on. */ 3180 if (flags & RENAME_EXCHANGE) 3181 return xfs_cross_rename(tp, src_dp, src_name, src_ip, 3182 target_dp, target_name, target_ip, 3183 spaceres); 3184 3185 /* 3186 * Try to reserve quota to handle an expansion of the target directory. 3187 * We'll allow the rename to continue in reservationless mode if we hit 3188 * a space usage constraint. If we trigger reservationless mode, save 3189 * the errno if there isn't any free space in the target directory. 3190 */ 3191 if (spaceres != 0) { 3192 error = xfs_trans_reserve_quota_nblks(tp, target_dp, spaceres, 3193 0, false); 3194 if (error == -EDQUOT || error == -ENOSPC) { 3195 if (!retried) { 3196 xfs_trans_cancel(tp); 3197 xfs_blockgc_free_quota(target_dp, 0); 3198 retried = true; 3199 goto retry; 3200 } 3201 3202 nospace_error = error; 3203 spaceres = 0; 3204 error = 0; 3205 } 3206 if (error) 3207 goto out_trans_cancel; 3208 } 3209 3210 /* 3211 * Check for expected errors before we dirty the transaction 3212 * so we can return an error without a transaction abort. 3213 * 3214 * Extent count overflow check: 3215 * 3216 * From the perspective of src_dp, a rename operation is essentially a 3217 * directory entry remove operation. Hence the only place where we check 3218 * for extent count overflow for src_dp is in 3219 * xfs_bmap_del_extent_real(). xfs_bmap_del_extent_real() returns 3220 * -ENOSPC when it detects a possible extent count overflow and in 3221 * response, the higher layers of directory handling code do the 3222 * following: 3223 * 1. Data/Free blocks: XFS lets these blocks linger until a 3224 * future remove operation removes them. 3225 * 2. Dabtree blocks: XFS swaps the blocks with the last block in the 3226 * Leaf space and unmaps the last block. 3227 * 3228 * For target_dp, there are two cases depending on whether the 3229 * destination directory entry exists or not. 3230 * 3231 * When destination directory entry does not exist (i.e. target_ip == 3232 * NULL), extent count overflow check is performed only when transaction 3233 * has a non-zero sized space reservation associated with it. With a 3234 * zero-sized space reservation, XFS allows a rename operation to 3235 * continue only when the directory has sufficient free space in its 3236 * data/leaf/free space blocks to hold the new entry. 3237 * 3238 * When destination directory entry exists (i.e. target_ip != NULL), all 3239 * we need to do is change the inode number associated with the already 3240 * existing entry. Hence there is no need to perform an extent count 3241 * overflow check. 3242 */ 3243 if (target_ip == NULL) { 3244 /* 3245 * If there's no space reservation, check the entry will 3246 * fit before actually inserting it. 3247 */ 3248 if (!spaceres) { 3249 error = xfs_dir_canenter(tp, target_dp, target_name); 3250 if (error) 3251 goto out_trans_cancel; 3252 } else { 3253 error = xfs_iext_count_may_overflow(target_dp, 3254 XFS_DATA_FORK, 3255 XFS_IEXT_DIR_MANIP_CNT(mp)); 3256 if (error) 3257 goto out_trans_cancel; 3258 } 3259 } else { 3260 /* 3261 * If target exists and it's a directory, check that whether 3262 * it can be destroyed. 3263 */ 3264 if (S_ISDIR(VFS_I(target_ip)->i_mode) && 3265 (!xfs_dir_isempty(target_ip) || 3266 (VFS_I(target_ip)->i_nlink > 2))) { 3267 error = -EEXIST; 3268 goto out_trans_cancel; 3269 } 3270 } 3271 3272 /* 3273 * Lock the AGI buffers we need to handle bumping the nlink of the 3274 * whiteout inode off the unlinked list and to handle dropping the 3275 * nlink of the target inode. Per locking order rules, do this in 3276 * increasing AG order and before directory block allocation tries to 3277 * grab AGFs because we grab AGIs before AGFs. 3278 * 3279 * The (vfs) caller must ensure that if src is a directory then 3280 * target_ip is either null or an empty directory. 3281 */ 3282 for (i = 0; i < num_inodes && inodes[i] != NULL; i++) { 3283 if (inodes[i] == wip || 3284 (inodes[i] == target_ip && 3285 (VFS_I(target_ip)->i_nlink == 1 || src_is_directory))) { 3286 struct xfs_buf *bp; 3287 xfs_agnumber_t agno; 3288 3289 agno = XFS_INO_TO_AGNO(mp, inodes[i]->i_ino); 3290 error = xfs_read_agi(mp, tp, agno, &bp); 3291 if (error) 3292 goto out_trans_cancel; 3293 } 3294 } 3295 3296 /* 3297 * Directory entry creation below may acquire the AGF. Remove 3298 * the whiteout from the unlinked list first to preserve correct 3299 * AGI/AGF locking order. This dirties the transaction so failures 3300 * after this point will abort and log recovery will clean up the 3301 * mess. 3302 * 3303 * For whiteouts, we need to bump the link count on the whiteout 3304 * inode. After this point, we have a real link, clear the tmpfile 3305 * state flag from the inode so it doesn't accidentally get misused 3306 * in future. 3307 */ 3308 if (wip) { 3309 struct xfs_perag *pag; 3310 3311 ASSERT(VFS_I(wip)->i_nlink == 0); 3312 3313 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, wip->i_ino)); 3314 error = xfs_iunlink_remove(tp, pag, wip); 3315 xfs_perag_put(pag); 3316 if (error) 3317 goto out_trans_cancel; 3318 3319 xfs_bumplink(tp, wip); 3320 VFS_I(wip)->i_state &= ~I_LINKABLE; 3321 } 3322 3323 /* 3324 * Set up the target. 3325 */ 3326 if (target_ip == NULL) { 3327 /* 3328 * If target does not exist and the rename crosses 3329 * directories, adjust the target directory link count 3330 * to account for the ".." reference from the new entry. 3331 */ 3332 error = xfs_dir_createname(tp, target_dp, target_name, 3333 src_ip->i_ino, spaceres); 3334 if (error) 3335 goto out_trans_cancel; 3336 3337 xfs_trans_ichgtime(tp, target_dp, 3338 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); 3339 3340 if (new_parent && src_is_directory) { 3341 xfs_bumplink(tp, target_dp); 3342 } 3343 } else { /* target_ip != NULL */ 3344 /* 3345 * Link the source inode under the target name. 3346 * If the source inode is a directory and we are moving 3347 * it across directories, its ".." entry will be 3348 * inconsistent until we replace that down below. 3349 * 3350 * In case there is already an entry with the same 3351 * name at the destination directory, remove it first. 3352 */ 3353 error = xfs_dir_replace(tp, target_dp, target_name, 3354 src_ip->i_ino, spaceres); 3355 if (error) 3356 goto out_trans_cancel; 3357 3358 xfs_trans_ichgtime(tp, target_dp, 3359 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); 3360 3361 /* 3362 * Decrement the link count on the target since the target 3363 * dir no longer points to it. 3364 */ 3365 error = xfs_droplink(tp, target_ip); 3366 if (error) 3367 goto out_trans_cancel; 3368 3369 if (src_is_directory) { 3370 /* 3371 * Drop the link from the old "." entry. 3372 */ 3373 error = xfs_droplink(tp, target_ip); 3374 if (error) 3375 goto out_trans_cancel; 3376 } 3377 } /* target_ip != NULL */ 3378 3379 /* 3380 * Remove the source. 3381 */ 3382 if (new_parent && src_is_directory) { 3383 /* 3384 * Rewrite the ".." entry to point to the new 3385 * directory. 3386 */ 3387 error = xfs_dir_replace(tp, src_ip, &xfs_name_dotdot, 3388 target_dp->i_ino, spaceres); 3389 ASSERT(error != -EEXIST); 3390 if (error) 3391 goto out_trans_cancel; 3392 } 3393 3394 /* 3395 * We always want to hit the ctime on the source inode. 3396 * 3397 * This isn't strictly required by the standards since the source 3398 * inode isn't really being changed, but old unix file systems did 3399 * it and some incremental backup programs won't work without it. 3400 */ 3401 xfs_trans_ichgtime(tp, src_ip, XFS_ICHGTIME_CHG); 3402 xfs_trans_log_inode(tp, src_ip, XFS_ILOG_CORE); 3403 3404 /* 3405 * Adjust the link count on src_dp. This is necessary when 3406 * renaming a directory, either within one parent when 3407 * the target existed, or across two parent directories. 3408 */ 3409 if (src_is_directory && (new_parent || target_ip != NULL)) { 3410 3411 /* 3412 * Decrement link count on src_directory since the 3413 * entry that's moved no longer points to it. 3414 */ 3415 error = xfs_droplink(tp, src_dp); 3416 if (error) 3417 goto out_trans_cancel; 3418 } 3419 3420 /* 3421 * For whiteouts, we only need to update the source dirent with the 3422 * inode number of the whiteout inode rather than removing it 3423 * altogether. 3424 */ 3425 if (wip) { 3426 error = xfs_dir_replace(tp, src_dp, src_name, wip->i_ino, 3427 spaceres); 3428 } else { 3429 /* 3430 * NOTE: We don't need to check for extent count overflow here 3431 * because the dir remove name code will leave the dir block in 3432 * place if the extent count would overflow. 3433 */ 3434 error = xfs_dir_removename(tp, src_dp, src_name, src_ip->i_ino, 3435 spaceres); 3436 } 3437 3438 if (error) 3439 goto out_trans_cancel; 3440 3441 xfs_trans_ichgtime(tp, src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); 3442 xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE); 3443 if (new_parent) 3444 xfs_trans_log_inode(tp, target_dp, XFS_ILOG_CORE); 3445 3446 error = xfs_finish_rename(tp); 3447 if (wip) 3448 xfs_irele(wip); 3449 return error; 3450 3451 out_trans_cancel: 3452 xfs_trans_cancel(tp); 3453 out_release_wip: 3454 if (wip) 3455 xfs_irele(wip); 3456 if (error == -ENOSPC && nospace_error) 3457 error = nospace_error; 3458 return error; 3459 } 3460 3461 static int 3462 xfs_iflush( 3463 struct xfs_inode *ip, 3464 struct xfs_buf *bp) 3465 { 3466 struct xfs_inode_log_item *iip = ip->i_itemp; 3467 struct xfs_dinode *dip; 3468 struct xfs_mount *mp = ip->i_mount; 3469 int error; 3470 3471 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)); 3472 ASSERT(xfs_iflags_test(ip, XFS_IFLUSHING)); 3473 ASSERT(ip->i_df.if_format != XFS_DINODE_FMT_BTREE || 3474 ip->i_df.if_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK)); 3475 ASSERT(iip->ili_item.li_buf == bp); 3476 3477 dip = xfs_buf_offset(bp, ip->i_imap.im_boffset); 3478 3479 /* 3480 * We don't flush the inode if any of the following checks fail, but we 3481 * do still update the log item and attach to the backing buffer as if 3482 * the flush happened. This is a formality to facilitate predictable 3483 * error handling as the caller will shutdown and fail the buffer. 3484 */ 3485 error = -EFSCORRUPTED; 3486 if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC), 3487 mp, XFS_ERRTAG_IFLUSH_1)) { 3488 xfs_alert_tag(mp, XFS_PTAG_IFLUSH, 3489 "%s: Bad inode %Lu magic number 0x%x, ptr "PTR_FMT, 3490 __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip); 3491 goto flush_out; 3492 } 3493 if (S_ISREG(VFS_I(ip)->i_mode)) { 3494 if (XFS_TEST_ERROR( 3495 ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS && 3496 ip->i_df.if_format != XFS_DINODE_FMT_BTREE, 3497 mp, XFS_ERRTAG_IFLUSH_3)) { 3498 xfs_alert_tag(mp, XFS_PTAG_IFLUSH, 3499 "%s: Bad regular inode %Lu, ptr "PTR_FMT, 3500 __func__, ip->i_ino, ip); 3501 goto flush_out; 3502 } 3503 } else if (S_ISDIR(VFS_I(ip)->i_mode)) { 3504 if (XFS_TEST_ERROR( 3505 ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS && 3506 ip->i_df.if_format != XFS_DINODE_FMT_BTREE && 3507 ip->i_df.if_format != XFS_DINODE_FMT_LOCAL, 3508 mp, XFS_ERRTAG_IFLUSH_4)) { 3509 xfs_alert_tag(mp, XFS_PTAG_IFLUSH, 3510 "%s: Bad directory inode %Lu, ptr "PTR_FMT, 3511 __func__, ip->i_ino, ip); 3512 goto flush_out; 3513 } 3514 } 3515 if (XFS_TEST_ERROR(ip->i_df.if_nextents + xfs_ifork_nextents(ip->i_afp) > 3516 ip->i_nblocks, mp, XFS_ERRTAG_IFLUSH_5)) { 3517 xfs_alert_tag(mp, XFS_PTAG_IFLUSH, 3518 "%s: detected corrupt incore inode %Lu, " 3519 "total extents = %d, nblocks = %Ld, ptr "PTR_FMT, 3520 __func__, ip->i_ino, 3521 ip->i_df.if_nextents + xfs_ifork_nextents(ip->i_afp), 3522 ip->i_nblocks, ip); 3523 goto flush_out; 3524 } 3525 if (XFS_TEST_ERROR(ip->i_forkoff > mp->m_sb.sb_inodesize, 3526 mp, XFS_ERRTAG_IFLUSH_6)) { 3527 xfs_alert_tag(mp, XFS_PTAG_IFLUSH, 3528 "%s: bad inode %Lu, forkoff 0x%x, ptr "PTR_FMT, 3529 __func__, ip->i_ino, ip->i_forkoff, ip); 3530 goto flush_out; 3531 } 3532 3533 /* 3534 * Inode item log recovery for v2 inodes are dependent on the flushiter 3535 * count for correct sequencing. We bump the flush iteration count so 3536 * we can detect flushes which postdate a log record during recovery. 3537 * This is redundant as we now log every change and hence this can't 3538 * happen but we need to still do it to ensure backwards compatibility 3539 * with old kernels that predate logging all inode changes. 3540 */ 3541 if (!xfs_has_v3inodes(mp)) 3542 ip->i_flushiter++; 3543 3544 /* 3545 * If there are inline format data / attr forks attached to this inode, 3546 * make sure they are not corrupt. 3547 */ 3548 if (ip->i_df.if_format == XFS_DINODE_FMT_LOCAL && 3549 xfs_ifork_verify_local_data(ip)) 3550 goto flush_out; 3551 if (ip->i_afp && ip->i_afp->if_format == XFS_DINODE_FMT_LOCAL && 3552 xfs_ifork_verify_local_attr(ip)) 3553 goto flush_out; 3554 3555 /* 3556 * Copy the dirty parts of the inode into the on-disk inode. We always 3557 * copy out the core of the inode, because if the inode is dirty at all 3558 * the core must be. 3559 */ 3560 xfs_inode_to_disk(ip, dip, iip->ili_item.li_lsn); 3561 3562 /* Wrap, we never let the log put out DI_MAX_FLUSH */ 3563 if (!xfs_has_v3inodes(mp)) { 3564 if (ip->i_flushiter == DI_MAX_FLUSH) 3565 ip->i_flushiter = 0; 3566 } 3567 3568 xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK); 3569 if (XFS_IFORK_Q(ip)) 3570 xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK); 3571 3572 /* 3573 * We've recorded everything logged in the inode, so we'd like to clear 3574 * the ili_fields bits so we don't log and flush things unnecessarily. 3575 * However, we can't stop logging all this information until the data 3576 * we've copied into the disk buffer is written to disk. If we did we 3577 * might overwrite the copy of the inode in the log with all the data 3578 * after re-logging only part of it, and in the face of a crash we 3579 * wouldn't have all the data we need to recover. 3580 * 3581 * What we do is move the bits to the ili_last_fields field. When 3582 * logging the inode, these bits are moved back to the ili_fields field. 3583 * In the xfs_buf_inode_iodone() routine we clear ili_last_fields, since 3584 * we know that the information those bits represent is permanently on 3585 * disk. As long as the flush completes before the inode is logged 3586 * again, then both ili_fields and ili_last_fields will be cleared. 3587 */ 3588 error = 0; 3589 flush_out: 3590 spin_lock(&iip->ili_lock); 3591 iip->ili_last_fields = iip->ili_fields; 3592 iip->ili_fields = 0; 3593 iip->ili_fsync_fields = 0; 3594 spin_unlock(&iip->ili_lock); 3595 3596 /* 3597 * Store the current LSN of the inode so that we can tell whether the 3598 * item has moved in the AIL from xfs_buf_inode_iodone(). 3599 */ 3600 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn, 3601 &iip->ili_item.li_lsn); 3602 3603 /* generate the checksum. */ 3604 xfs_dinode_calc_crc(mp, dip); 3605 return error; 3606 } 3607 3608 /* 3609 * Non-blocking flush of dirty inode metadata into the backing buffer. 3610 * 3611 * The caller must have a reference to the inode and hold the cluster buffer 3612 * locked. The function will walk across all the inodes on the cluster buffer it 3613 * can find and lock without blocking, and flush them to the cluster buffer. 3614 * 3615 * On successful flushing of at least one inode, the caller must write out the 3616 * buffer and release it. If no inodes are flushed, -EAGAIN will be returned and 3617 * the caller needs to release the buffer. On failure, the filesystem will be 3618 * shut down, the buffer will have been unlocked and released, and EFSCORRUPTED 3619 * will be returned. 3620 */ 3621 int 3622 xfs_iflush_cluster( 3623 struct xfs_buf *bp) 3624 { 3625 struct xfs_mount *mp = bp->b_mount; 3626 struct xfs_log_item *lip, *n; 3627 struct xfs_inode *ip; 3628 struct xfs_inode_log_item *iip; 3629 int clcount = 0; 3630 int error = 0; 3631 3632 /* 3633 * We must use the safe variant here as on shutdown xfs_iflush_abort() 3634 * will remove itself from the list. 3635 */ 3636 list_for_each_entry_safe(lip, n, &bp->b_li_list, li_bio_list) { 3637 iip = (struct xfs_inode_log_item *)lip; 3638 ip = iip->ili_inode; 3639 3640 /* 3641 * Quick and dirty check to avoid locks if possible. 3642 */ 3643 if (__xfs_iflags_test(ip, XFS_IRECLAIM | XFS_IFLUSHING)) 3644 continue; 3645 if (xfs_ipincount(ip)) 3646 continue; 3647 3648 /* 3649 * The inode is still attached to the buffer, which means it is 3650 * dirty but reclaim might try to grab it. Check carefully for 3651 * that, and grab the ilock while still holding the i_flags_lock 3652 * to guarantee reclaim will not be able to reclaim this inode 3653 * once we drop the i_flags_lock. 3654 */ 3655 spin_lock(&ip->i_flags_lock); 3656 ASSERT(!__xfs_iflags_test(ip, XFS_ISTALE)); 3657 if (__xfs_iflags_test(ip, XFS_IRECLAIM | XFS_IFLUSHING)) { 3658 spin_unlock(&ip->i_flags_lock); 3659 continue; 3660 } 3661 3662 /* 3663 * ILOCK will pin the inode against reclaim and prevent 3664 * concurrent transactions modifying the inode while we are 3665 * flushing the inode. If we get the lock, set the flushing 3666 * state before we drop the i_flags_lock. 3667 */ 3668 if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) { 3669 spin_unlock(&ip->i_flags_lock); 3670 continue; 3671 } 3672 __xfs_iflags_set(ip, XFS_IFLUSHING); 3673 spin_unlock(&ip->i_flags_lock); 3674 3675 /* 3676 * Abort flushing this inode if we are shut down because the 3677 * inode may not currently be in the AIL. This can occur when 3678 * log I/O failure unpins the inode without inserting into the 3679 * AIL, leaving a dirty/unpinned inode attached to the buffer 3680 * that otherwise looks like it should be flushed. 3681 */ 3682 if (xlog_is_shutdown(mp->m_log)) { 3683 xfs_iunpin_wait(ip); 3684 xfs_iflush_abort(ip); 3685 xfs_iunlock(ip, XFS_ILOCK_SHARED); 3686 error = -EIO; 3687 continue; 3688 } 3689 3690 /* don't block waiting on a log force to unpin dirty inodes */ 3691 if (xfs_ipincount(ip)) { 3692 xfs_iflags_clear(ip, XFS_IFLUSHING); 3693 xfs_iunlock(ip, XFS_ILOCK_SHARED); 3694 continue; 3695 } 3696 3697 if (!xfs_inode_clean(ip)) 3698 error = xfs_iflush(ip, bp); 3699 else 3700 xfs_iflags_clear(ip, XFS_IFLUSHING); 3701 xfs_iunlock(ip, XFS_ILOCK_SHARED); 3702 if (error) 3703 break; 3704 clcount++; 3705 } 3706 3707 if (error) { 3708 /* 3709 * Shutdown first so we kill the log before we release this 3710 * buffer. If it is an INODE_ALLOC buffer and pins the tail 3711 * of the log, failing it before the _log_ is shut down can 3712 * result in the log tail being moved forward in the journal 3713 * on disk because log writes can still be taking place. Hence 3714 * unpinning the tail will allow the ICREATE intent to be 3715 * removed from the log an recovery will fail with uninitialised 3716 * inode cluster buffers. 3717 */ 3718 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); 3719 bp->b_flags |= XBF_ASYNC; 3720 xfs_buf_ioend_fail(bp); 3721 return error; 3722 } 3723 3724 if (!clcount) 3725 return -EAGAIN; 3726 3727 XFS_STATS_INC(mp, xs_icluster_flushcnt); 3728 XFS_STATS_ADD(mp, xs_icluster_flushinode, clcount); 3729 return 0; 3730 3731 } 3732 3733 /* Release an inode. */ 3734 void 3735 xfs_irele( 3736 struct xfs_inode *ip) 3737 { 3738 trace_xfs_irele(ip, _RET_IP_); 3739 iput(VFS_I(ip)); 3740 } 3741 3742 /* 3743 * Ensure all commited transactions touching the inode are written to the log. 3744 */ 3745 int 3746 xfs_log_force_inode( 3747 struct xfs_inode *ip) 3748 { 3749 xfs_csn_t seq = 0; 3750 3751 xfs_ilock(ip, XFS_ILOCK_SHARED); 3752 if (xfs_ipincount(ip)) 3753 seq = ip->i_itemp->ili_commit_seq; 3754 xfs_iunlock(ip, XFS_ILOCK_SHARED); 3755 3756 if (!seq) 3757 return 0; 3758 return xfs_log_force_seq(ip->i_mount, seq, XFS_LOG_SYNC, NULL); 3759 } 3760 3761 /* 3762 * Grab the exclusive iolock for a data copy from src to dest, making sure to 3763 * abide vfs locking order (lowest pointer value goes first) and breaking the 3764 * layout leases before proceeding. The loop is needed because we cannot call 3765 * the blocking break_layout() with the iolocks held, and therefore have to 3766 * back out both locks. 3767 */ 3768 static int 3769 xfs_iolock_two_inodes_and_break_layout( 3770 struct inode *src, 3771 struct inode *dest) 3772 { 3773 int error; 3774 3775 if (src > dest) 3776 swap(src, dest); 3777 3778 retry: 3779 /* Wait to break both inodes' layouts before we start locking. */ 3780 error = break_layout(src, true); 3781 if (error) 3782 return error; 3783 if (src != dest) { 3784 error = break_layout(dest, true); 3785 if (error) 3786 return error; 3787 } 3788 3789 /* Lock one inode and make sure nobody got in and leased it. */ 3790 inode_lock(src); 3791 error = break_layout(src, false); 3792 if (error) { 3793 inode_unlock(src); 3794 if (error == -EWOULDBLOCK) 3795 goto retry; 3796 return error; 3797 } 3798 3799 if (src == dest) 3800 return 0; 3801 3802 /* Lock the other inode and make sure nobody got in and leased it. */ 3803 inode_lock_nested(dest, I_MUTEX_NONDIR2); 3804 error = break_layout(dest, false); 3805 if (error) { 3806 inode_unlock(src); 3807 inode_unlock(dest); 3808 if (error == -EWOULDBLOCK) 3809 goto retry; 3810 return error; 3811 } 3812 3813 return 0; 3814 } 3815 3816 /* 3817 * Lock two inodes so that userspace cannot initiate I/O via file syscalls or 3818 * mmap activity. 3819 */ 3820 int 3821 xfs_ilock2_io_mmap( 3822 struct xfs_inode *ip1, 3823 struct xfs_inode *ip2) 3824 { 3825 int ret; 3826 3827 ret = xfs_iolock_two_inodes_and_break_layout(VFS_I(ip1), VFS_I(ip2)); 3828 if (ret) 3829 return ret; 3830 filemap_invalidate_lock_two(VFS_I(ip1)->i_mapping, 3831 VFS_I(ip2)->i_mapping); 3832 return 0; 3833 } 3834 3835 /* Unlock both inodes to allow IO and mmap activity. */ 3836 void 3837 xfs_iunlock2_io_mmap( 3838 struct xfs_inode *ip1, 3839 struct xfs_inode *ip2) 3840 { 3841 filemap_invalidate_unlock_two(VFS_I(ip1)->i_mapping, 3842 VFS_I(ip2)->i_mapping); 3843 inode_unlock(VFS_I(ip2)); 3844 if (ip1 != ip2) 3845 inode_unlock(VFS_I(ip1)); 3846 } 3847