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