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