]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - fs/xfs/xfs_inode.c
xfs: add O_TMPFILE support
[mirror_ubuntu-zesty-kernel.git] / fs / xfs / xfs_inode.c
CommitLineData
1da177e4 1/*
3e57ecf6 2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
7b718769 3 * All Rights Reserved.
1da177e4 4 *
7b718769
NS
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
7b718769
NS
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
1da177e4 13 *
7b718769
NS
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1da177e4 17 */
40ebd81d
RD
18#include <linux/log2.h>
19
1da177e4 20#include "xfs.h"
a844f451 21#include "xfs_fs.h"
70a9883c 22#include "xfs_shared.h"
239880ef
DC
23#include "xfs_format.h"
24#include "xfs_log_format.h"
25#include "xfs_trans_resv.h"
a844f451 26#include "xfs_inum.h"
1da177e4
LT
27#include "xfs_sb.h"
28#include "xfs_ag.h"
1da177e4 29#include "xfs_mount.h"
a4fbe6ab 30#include "xfs_inode.h"
57062787 31#include "xfs_da_format.h"
c24b5dfa 32#include "xfs_da_btree.h"
c24b5dfa 33#include "xfs_dir2.h"
a844f451 34#include "xfs_attr_sf.h"
c24b5dfa 35#include "xfs_attr.h"
239880ef
DC
36#include "xfs_trans_space.h"
37#include "xfs_trans.h"
1da177e4 38#include "xfs_buf_item.h"
a844f451 39#include "xfs_inode_item.h"
a844f451
NS
40#include "xfs_ialloc.h"
41#include "xfs_bmap.h"
68988114 42#include "xfs_bmap_util.h"
1da177e4 43#include "xfs_error.h"
1da177e4 44#include "xfs_quota.h"
2a82b8be 45#include "xfs_filestream.h"
93848a99 46#include "xfs_cksum.h"
0b1b213f 47#include "xfs_trace.h"
33479e05 48#include "xfs_icache.h"
c24b5dfa 49#include "xfs_symlink.h"
239880ef
DC
50#include "xfs_trans_priv.h"
51#include "xfs_log.h"
a4fbe6ab 52#include "xfs_bmap_btree.h"
1da177e4 53
1da177e4 54kmem_zone_t *xfs_inode_zone;
1da177e4
LT
55
56/*
8f04c47a 57 * Used in xfs_itruncate_extents(). This is the maximum number of extents
1da177e4
LT
58 * freed from a file in a single transaction.
59 */
60#define XFS_ITRUNC_MAX_EXTENTS 2
61
62STATIC int xfs_iflush_int(xfs_inode_t *, xfs_buf_t *);
1da177e4 63
2a0ec1d9
DC
64/*
65 * helper function to extract extent size hint from inode
66 */
67xfs_extlen_t
68xfs_get_extsz_hint(
69 struct xfs_inode *ip)
70{
71 if ((ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE) && ip->i_d.di_extsize)
72 return ip->i_d.di_extsize;
73 if (XFS_IS_REALTIME_INODE(ip))
74 return ip->i_mount->m_sb.sb_rextsize;
75 return 0;
76}
77
fa96acad
DC
78/*
79 * This is a wrapper routine around the xfs_ilock() routine used to centralize
80 * some grungy code. It is used in places that wish to lock the inode solely
81 * for reading the extents. The reason these places can't just call
82 * xfs_ilock(SHARED) is that the inode lock also guards to bringing in of the
83 * extents from disk for a file in b-tree format. If the inode is in b-tree
84 * format, then we need to lock the inode exclusively until the extents are read
85 * in. Locking it exclusively all the time would limit our parallelism
86 * unnecessarily, though. What we do instead is check to see if the extents
87 * have been read in yet, and only lock the inode exclusively if they have not.
88 *
89 * The function returns a value which should be given to the corresponding
90 * xfs_iunlock_map_shared(). This value is the mode in which the lock was
91 * actually taken.
92 */
93uint
94xfs_ilock_map_shared(
95 xfs_inode_t *ip)
96{
97 uint lock_mode;
98
99 if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
100 ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
101 lock_mode = XFS_ILOCK_EXCL;
102 } else {
103 lock_mode = XFS_ILOCK_SHARED;
104 }
105
106 xfs_ilock(ip, lock_mode);
107
108 return lock_mode;
109}
110
111/*
112 * This is simply the unlock routine to go with xfs_ilock_map_shared().
113 * All it does is call xfs_iunlock() with the given lock_mode.
114 */
115void
116xfs_iunlock_map_shared(
117 xfs_inode_t *ip,
118 unsigned int lock_mode)
119{
120 xfs_iunlock(ip, lock_mode);
121}
122
123/*
124 * The xfs inode contains 2 locks: a multi-reader lock called the
125 * i_iolock and a multi-reader lock called the i_lock. This routine
126 * allows either or both of the locks to be obtained.
127 *
128 * The 2 locks should always be ordered so that the IO lock is
129 * obtained first in order to prevent deadlock.
130 *
131 * ip -- the inode being locked
132 * lock_flags -- this parameter indicates the inode's locks
133 * to be locked. It can be:
134 * XFS_IOLOCK_SHARED,
135 * XFS_IOLOCK_EXCL,
136 * XFS_ILOCK_SHARED,
137 * XFS_ILOCK_EXCL,
138 * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
139 * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
140 * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
141 * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
142 */
143void
144xfs_ilock(
145 xfs_inode_t *ip,
146 uint lock_flags)
147{
148 trace_xfs_ilock(ip, lock_flags, _RET_IP_);
149
150 /*
151 * You can't set both SHARED and EXCL for the same lock,
152 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
153 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
154 */
155 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
156 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
157 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
158 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
159 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
160
161 if (lock_flags & XFS_IOLOCK_EXCL)
162 mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
163 else if (lock_flags & XFS_IOLOCK_SHARED)
164 mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
165
166 if (lock_flags & XFS_ILOCK_EXCL)
167 mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
168 else if (lock_flags & XFS_ILOCK_SHARED)
169 mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
170}
171
172/*
173 * This is just like xfs_ilock(), except that the caller
174 * is guaranteed not to sleep. It returns 1 if it gets
175 * the requested locks and 0 otherwise. If the IO lock is
176 * obtained but the inode lock cannot be, then the IO lock
177 * is dropped before returning.
178 *
179 * ip -- the inode being locked
180 * lock_flags -- this parameter indicates the inode's locks to be
181 * to be locked. See the comment for xfs_ilock() for a list
182 * of valid values.
183 */
184int
185xfs_ilock_nowait(
186 xfs_inode_t *ip,
187 uint lock_flags)
188{
189 trace_xfs_ilock_nowait(ip, lock_flags, _RET_IP_);
190
191 /*
192 * You can't set both SHARED and EXCL for the same lock,
193 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
194 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
195 */
196 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
197 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
198 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
199 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
200 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
201
202 if (lock_flags & XFS_IOLOCK_EXCL) {
203 if (!mrtryupdate(&ip->i_iolock))
204 goto out;
205 } else if (lock_flags & XFS_IOLOCK_SHARED) {
206 if (!mrtryaccess(&ip->i_iolock))
207 goto out;
208 }
209 if (lock_flags & XFS_ILOCK_EXCL) {
210 if (!mrtryupdate(&ip->i_lock))
211 goto out_undo_iolock;
212 } else if (lock_flags & XFS_ILOCK_SHARED) {
213 if (!mrtryaccess(&ip->i_lock))
214 goto out_undo_iolock;
215 }
216 return 1;
217
218 out_undo_iolock:
219 if (lock_flags & XFS_IOLOCK_EXCL)
220 mrunlock_excl(&ip->i_iolock);
221 else if (lock_flags & XFS_IOLOCK_SHARED)
222 mrunlock_shared(&ip->i_iolock);
223 out:
224 return 0;
225}
226
227/*
228 * xfs_iunlock() is used to drop the inode locks acquired with
229 * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
230 * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
231 * that we know which locks to drop.
232 *
233 * ip -- the inode being unlocked
234 * lock_flags -- this parameter indicates the inode's locks to be
235 * to be unlocked. See the comment for xfs_ilock() for a list
236 * of valid values for this parameter.
237 *
238 */
239void
240xfs_iunlock(
241 xfs_inode_t *ip,
242 uint lock_flags)
243{
244 /*
245 * You can't set both SHARED and EXCL for the same lock,
246 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
247 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
248 */
249 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
250 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
251 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
252 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
253 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
254 ASSERT(lock_flags != 0);
255
256 if (lock_flags & XFS_IOLOCK_EXCL)
257 mrunlock_excl(&ip->i_iolock);
258 else if (lock_flags & XFS_IOLOCK_SHARED)
259 mrunlock_shared(&ip->i_iolock);
260
261 if (lock_flags & XFS_ILOCK_EXCL)
262 mrunlock_excl(&ip->i_lock);
263 else if (lock_flags & XFS_ILOCK_SHARED)
264 mrunlock_shared(&ip->i_lock);
265
266 trace_xfs_iunlock(ip, lock_flags, _RET_IP_);
267}
268
269/*
270 * give up write locks. the i/o lock cannot be held nested
271 * if it is being demoted.
272 */
273void
274xfs_ilock_demote(
275 xfs_inode_t *ip,
276 uint lock_flags)
277{
278 ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
279 ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
280
281 if (lock_flags & XFS_ILOCK_EXCL)
282 mrdemote(&ip->i_lock);
283 if (lock_flags & XFS_IOLOCK_EXCL)
284 mrdemote(&ip->i_iolock);
285
286 trace_xfs_ilock_demote(ip, lock_flags, _RET_IP_);
287}
288
742ae1e3 289#if defined(DEBUG) || defined(XFS_WARN)
fa96acad
DC
290int
291xfs_isilocked(
292 xfs_inode_t *ip,
293 uint lock_flags)
294{
295 if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) {
296 if (!(lock_flags & XFS_ILOCK_SHARED))
297 return !!ip->i_lock.mr_writer;
298 return rwsem_is_locked(&ip->i_lock.mr_lock);
299 }
300
301 if (lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) {
302 if (!(lock_flags & XFS_IOLOCK_SHARED))
303 return !!ip->i_iolock.mr_writer;
304 return rwsem_is_locked(&ip->i_iolock.mr_lock);
305 }
306
307 ASSERT(0);
308 return 0;
309}
310#endif
311
c24b5dfa
DC
312#ifdef DEBUG
313int xfs_locked_n;
314int xfs_small_retries;
315int xfs_middle_retries;
316int xfs_lots_retries;
317int xfs_lock_delays;
318#endif
319
320/*
321 * Bump the subclass so xfs_lock_inodes() acquires each lock with
322 * a different value
323 */
324static inline int
325xfs_lock_inumorder(int lock_mode, int subclass)
326{
327 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
328 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
329 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
330 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
331
332 return lock_mode;
333}
334
335/*
336 * The following routine will lock n inodes in exclusive mode.
337 * We assume the caller calls us with the inodes in i_ino order.
338 *
339 * We need to detect deadlock where an inode that we lock
340 * is in the AIL and we start waiting for another inode that is locked
341 * by a thread in a long running transaction (such as truncate). This can
342 * result in deadlock since the long running trans might need to wait
343 * for the inode we just locked in order to push the tail and free space
344 * in the log.
345 */
346void
347xfs_lock_inodes(
348 xfs_inode_t **ips,
349 int inodes,
350 uint lock_mode)
351{
352 int attempts = 0, i, j, try_lock;
353 xfs_log_item_t *lp;
354
355 ASSERT(ips && (inodes >= 2)); /* we need at least two */
356
357 try_lock = 0;
358 i = 0;
359
360again:
361 for (; i < inodes; i++) {
362 ASSERT(ips[i]);
363
364 if (i && (ips[i] == ips[i-1])) /* Already locked */
365 continue;
366
367 /*
368 * If try_lock is not set yet, make sure all locked inodes
369 * are not in the AIL.
370 * If any are, set try_lock to be used later.
371 */
372
373 if (!try_lock) {
374 for (j = (i - 1); j >= 0 && !try_lock; j--) {
375 lp = (xfs_log_item_t *)ips[j]->i_itemp;
376 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
377 try_lock++;
378 }
379 }
380 }
381
382 /*
383 * If any of the previous locks we have locked is in the AIL,
384 * we must TRY to get the second and subsequent locks. If
385 * we can't get any, we must release all we have
386 * and try again.
387 */
388
389 if (try_lock) {
390 /* try_lock must be 0 if i is 0. */
391 /*
392 * try_lock means we have an inode locked
393 * that is in the AIL.
394 */
395 ASSERT(i != 0);
396 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
397 attempts++;
398
399 /*
400 * Unlock all previous guys and try again.
401 * xfs_iunlock will try to push the tail
402 * if the inode is in the AIL.
403 */
404
405 for(j = i - 1; j >= 0; j--) {
406
407 /*
408 * Check to see if we've already
409 * unlocked this one.
410 * Not the first one going back,
411 * and the inode ptr is the same.
412 */
413 if ((j != (i - 1)) && ips[j] ==
414 ips[j+1])
415 continue;
416
417 xfs_iunlock(ips[j], lock_mode);
418 }
419
420 if ((attempts % 5) == 0) {
421 delay(1); /* Don't just spin the CPU */
422#ifdef DEBUG
423 xfs_lock_delays++;
424#endif
425 }
426 i = 0;
427 try_lock = 0;
428 goto again;
429 }
430 } else {
431 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
432 }
433 }
434
435#ifdef DEBUG
436 if (attempts) {
437 if (attempts < 5) xfs_small_retries++;
438 else if (attempts < 100) xfs_middle_retries++;
439 else xfs_lots_retries++;
440 } else {
441 xfs_locked_n++;
442 }
443#endif
444}
445
446/*
447 * xfs_lock_two_inodes() can only be used to lock one type of lock
448 * at a time - the iolock or the ilock, but not both at once. If
449 * we lock both at once, lockdep will report false positives saying
450 * we have violated locking orders.
451 */
452void
453xfs_lock_two_inodes(
454 xfs_inode_t *ip0,
455 xfs_inode_t *ip1,
456 uint lock_mode)
457{
458 xfs_inode_t *temp;
459 int attempts = 0;
460 xfs_log_item_t *lp;
461
462 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
463 ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0);
464 ASSERT(ip0->i_ino != ip1->i_ino);
465
466 if (ip0->i_ino > ip1->i_ino) {
467 temp = ip0;
468 ip0 = ip1;
469 ip1 = temp;
470 }
471
472 again:
473 xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
474
475 /*
476 * If the first lock we have locked is in the AIL, we must TRY to get
477 * the second lock. If we can't get it, we must release the first one
478 * and try again.
479 */
480 lp = (xfs_log_item_t *)ip0->i_itemp;
481 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
482 if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
483 xfs_iunlock(ip0, lock_mode);
484 if ((++attempts % 5) == 0)
485 delay(1); /* Don't just spin the CPU */
486 goto again;
487 }
488 } else {
489 xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
490 }
491}
492
493
fa96acad
DC
494void
495__xfs_iflock(
496 struct xfs_inode *ip)
497{
498 wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IFLOCK_BIT);
499 DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IFLOCK_BIT);
500
501 do {
502 prepare_to_wait_exclusive(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
503 if (xfs_isiflocked(ip))
504 io_schedule();
505 } while (!xfs_iflock_nowait(ip));
506
507 finish_wait(wq, &wait.wait);
508}
509
1da177e4
LT
510STATIC uint
511_xfs_dic2xflags(
1da177e4
LT
512 __uint16_t di_flags)
513{
514 uint flags = 0;
515
516 if (di_flags & XFS_DIFLAG_ANY) {
517 if (di_flags & XFS_DIFLAG_REALTIME)
518 flags |= XFS_XFLAG_REALTIME;
519 if (di_flags & XFS_DIFLAG_PREALLOC)
520 flags |= XFS_XFLAG_PREALLOC;
521 if (di_flags & XFS_DIFLAG_IMMUTABLE)
522 flags |= XFS_XFLAG_IMMUTABLE;
523 if (di_flags & XFS_DIFLAG_APPEND)
524 flags |= XFS_XFLAG_APPEND;
525 if (di_flags & XFS_DIFLAG_SYNC)
526 flags |= XFS_XFLAG_SYNC;
527 if (di_flags & XFS_DIFLAG_NOATIME)
528 flags |= XFS_XFLAG_NOATIME;
529 if (di_flags & XFS_DIFLAG_NODUMP)
530 flags |= XFS_XFLAG_NODUMP;
531 if (di_flags & XFS_DIFLAG_RTINHERIT)
532 flags |= XFS_XFLAG_RTINHERIT;
533 if (di_flags & XFS_DIFLAG_PROJINHERIT)
534 flags |= XFS_XFLAG_PROJINHERIT;
535 if (di_flags & XFS_DIFLAG_NOSYMLINKS)
536 flags |= XFS_XFLAG_NOSYMLINKS;
dd9f438e
NS
537 if (di_flags & XFS_DIFLAG_EXTSIZE)
538 flags |= XFS_XFLAG_EXTSIZE;
539 if (di_flags & XFS_DIFLAG_EXTSZINHERIT)
540 flags |= XFS_XFLAG_EXTSZINHERIT;
d3446eac
BN
541 if (di_flags & XFS_DIFLAG_NODEFRAG)
542 flags |= XFS_XFLAG_NODEFRAG;
2a82b8be
DC
543 if (di_flags & XFS_DIFLAG_FILESTREAM)
544 flags |= XFS_XFLAG_FILESTREAM;
1da177e4
LT
545 }
546
547 return flags;
548}
549
550uint
551xfs_ip2xflags(
552 xfs_inode_t *ip)
553{
347d1c01 554 xfs_icdinode_t *dic = &ip->i_d;
1da177e4 555
a916e2bd 556 return _xfs_dic2xflags(dic->di_flags) |
45ba598e 557 (XFS_IFORK_Q(ip) ? XFS_XFLAG_HASATTR : 0);
1da177e4
LT
558}
559
560uint
561xfs_dic2xflags(
45ba598e 562 xfs_dinode_t *dip)
1da177e4 563{
81591fe2 564 return _xfs_dic2xflags(be16_to_cpu(dip->di_flags)) |
45ba598e 565 (XFS_DFORK_Q(dip) ? XFS_XFLAG_HASATTR : 0);
1da177e4
LT
566}
567
c24b5dfa
DC
568/*
569 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
570 * is allowed, otherwise it has to be an exact match. If a CI match is found,
571 * ci_name->name will point to a the actual name (caller must free) or
572 * will be set to NULL if an exact match is found.
573 */
574int
575xfs_lookup(
576 xfs_inode_t *dp,
577 struct xfs_name *name,
578 xfs_inode_t **ipp,
579 struct xfs_name *ci_name)
580{
581 xfs_ino_t inum;
582 int error;
583 uint lock_mode;
584
585 trace_xfs_lookup(dp, name);
586
587 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
588 return XFS_ERROR(EIO);
589
590 lock_mode = xfs_ilock_map_shared(dp);
591 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
592 xfs_iunlock_map_shared(dp, lock_mode);
593
594 if (error)
595 goto out;
596
597 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp);
598 if (error)
599 goto out_free_name;
600
601 return 0;
602
603out_free_name:
604 if (ci_name)
605 kmem_free(ci_name->name);
606out:
607 *ipp = NULL;
608 return error;
609}
610
1da177e4
LT
611/*
612 * Allocate an inode on disk and return a copy of its in-core version.
613 * The in-core inode is locked exclusively. Set mode, nlink, and rdev
614 * appropriately within the inode. The uid and gid for the inode are
615 * set according to the contents of the given cred structure.
616 *
617 * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc()
cd856db6
CM
618 * has a free inode available, call xfs_iget() to obtain the in-core
619 * version of the allocated inode. Finally, fill in the inode and
620 * log its initial contents. In this case, ialloc_context would be
621 * set to NULL.
1da177e4 622 *
cd856db6
CM
623 * If xfs_dialloc() does not have an available inode, it will replenish
624 * its supply by doing an allocation. Since we can only do one
625 * allocation within a transaction without deadlocks, we must commit
626 * the current transaction before returning the inode itself.
627 * In this case, therefore, we will set ialloc_context and return.
1da177e4
LT
628 * The caller should then commit the current transaction, start a new
629 * transaction, and call xfs_ialloc() again to actually get the inode.
630 *
631 * To ensure that some other process does not grab the inode that
632 * was allocated during the first call to xfs_ialloc(), this routine
633 * also returns the [locked] bp pointing to the head of the freelist
634 * as ialloc_context. The caller should hold this buffer across
635 * the commit and pass it back into this routine on the second call.
b11f94d5
DC
636 *
637 * If we are allocating quota inodes, we do not have a parent inode
638 * to attach to or associate with (i.e. pip == NULL) because they
639 * are not linked into the directory structure - they are attached
640 * directly to the superblock - and so have no parent.
1da177e4
LT
641 */
642int
643xfs_ialloc(
644 xfs_trans_t *tp,
645 xfs_inode_t *pip,
576b1d67 646 umode_t mode,
31b084ae 647 xfs_nlink_t nlink,
1da177e4 648 xfs_dev_t rdev,
6743099c 649 prid_t prid,
1da177e4
LT
650 int okalloc,
651 xfs_buf_t **ialloc_context,
1da177e4
LT
652 xfs_inode_t **ipp)
653{
93848a99 654 struct xfs_mount *mp = tp->t_mountp;
1da177e4
LT
655 xfs_ino_t ino;
656 xfs_inode_t *ip;
1da177e4
LT
657 uint flags;
658 int error;
dff35fd4 659 timespec_t tv;
bf904248 660 int filestreams = 0;
1da177e4
LT
661
662 /*
663 * Call the space management code to pick
664 * the on-disk inode to be allocated.
665 */
b11f94d5 666 error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc,
08358906 667 ialloc_context, &ino);
bf904248 668 if (error)
1da177e4 669 return error;
08358906 670 if (*ialloc_context || ino == NULLFSINO) {
1da177e4
LT
671 *ipp = NULL;
672 return 0;
673 }
674 ASSERT(*ialloc_context == NULL);
675
676 /*
677 * Get the in-core inode with the lock held exclusively.
678 * This is because we're setting fields here we need
679 * to prevent others from looking at until we're done.
680 */
93848a99 681 error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE,
ec3ba85f 682 XFS_ILOCK_EXCL, &ip);
bf904248 683 if (error)
1da177e4 684 return error;
1da177e4
LT
685 ASSERT(ip != NULL);
686
576b1d67 687 ip->i_d.di_mode = mode;
1da177e4
LT
688 ip->i_d.di_onlink = 0;
689 ip->i_d.di_nlink = nlink;
690 ASSERT(ip->i_d.di_nlink == nlink);
7aab1b28
DE
691 ip->i_d.di_uid = xfs_kuid_to_uid(current_fsuid());
692 ip->i_d.di_gid = xfs_kgid_to_gid(current_fsgid());
6743099c 693 xfs_set_projid(ip, prid);
1da177e4
LT
694 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
695
696 /*
697 * If the superblock version is up to where we support new format
698 * inodes and this is currently an old format inode, then change
699 * the inode version number now. This way we only do the conversion
700 * here rather than here and in the flush/logging code.
701 */
93848a99 702 if (xfs_sb_version_hasnlink(&mp->m_sb) &&
51ce16d5
CH
703 ip->i_d.di_version == 1) {
704 ip->i_d.di_version = 2;
1da177e4
LT
705 /*
706 * We've already zeroed the old link count, the projid field,
707 * and the pad field.
708 */
709 }
710
711 /*
712 * Project ids won't be stored on disk if we are using a version 1 inode.
713 */
51ce16d5 714 if ((prid != 0) && (ip->i_d.di_version == 1))
1da177e4
LT
715 xfs_bump_ino_vers2(tp, ip);
716
bd186aa9 717 if (pip && XFS_INHERIT_GID(pip)) {
1da177e4 718 ip->i_d.di_gid = pip->i_d.di_gid;
abbede1b 719 if ((pip->i_d.di_mode & S_ISGID) && S_ISDIR(mode)) {
1da177e4
LT
720 ip->i_d.di_mode |= S_ISGID;
721 }
722 }
723
724 /*
725 * If the group ID of the new file does not match the effective group
726 * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
727 * (and only if the irix_sgid_inherit compatibility variable is set).
728 */
729 if ((irix_sgid_inherit) &&
730 (ip->i_d.di_mode & S_ISGID) &&
7aab1b28 731 (!in_group_p(xfs_gid_to_kgid(ip->i_d.di_gid)))) {
1da177e4
LT
732 ip->i_d.di_mode &= ~S_ISGID;
733 }
734
735 ip->i_d.di_size = 0;
736 ip->i_d.di_nextents = 0;
737 ASSERT(ip->i_d.di_nblocks == 0);
dff35fd4
CH
738
739 nanotime(&tv);
740 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
741 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
742 ip->i_d.di_atime = ip->i_d.di_mtime;
743 ip->i_d.di_ctime = ip->i_d.di_mtime;
744
1da177e4
LT
745 /*
746 * di_gen will have been taken care of in xfs_iread.
747 */
748 ip->i_d.di_extsize = 0;
749 ip->i_d.di_dmevmask = 0;
750 ip->i_d.di_dmstate = 0;
751 ip->i_d.di_flags = 0;
93848a99
CH
752
753 if (ip->i_d.di_version == 3) {
754 ASSERT(ip->i_d.di_ino == ino);
755 ASSERT(uuid_equal(&ip->i_d.di_uuid, &mp->m_sb.sb_uuid));
756 ip->i_d.di_crc = 0;
757 ip->i_d.di_changecount = 1;
758 ip->i_d.di_lsn = 0;
759 ip->i_d.di_flags2 = 0;
760 memset(&(ip->i_d.di_pad2[0]), 0, sizeof(ip->i_d.di_pad2));
761 ip->i_d.di_crtime = ip->i_d.di_mtime;
762 }
763
764
1da177e4
LT
765 flags = XFS_ILOG_CORE;
766 switch (mode & S_IFMT) {
767 case S_IFIFO:
768 case S_IFCHR:
769 case S_IFBLK:
770 case S_IFSOCK:
771 ip->i_d.di_format = XFS_DINODE_FMT_DEV;
772 ip->i_df.if_u2.if_rdev = rdev;
773 ip->i_df.if_flags = 0;
774 flags |= XFS_ILOG_DEV;
775 break;
776 case S_IFREG:
bf904248
DC
777 /*
778 * we can't set up filestreams until after the VFS inode
779 * is set up properly.
780 */
781 if (pip && xfs_inode_is_filestream(pip))
782 filestreams = 1;
2a82b8be 783 /* fall through */
1da177e4 784 case S_IFDIR:
b11f94d5 785 if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) {
365ca83d
NS
786 uint di_flags = 0;
787
abbede1b 788 if (S_ISDIR(mode)) {
365ca83d
NS
789 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
790 di_flags |= XFS_DIFLAG_RTINHERIT;
dd9f438e
NS
791 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
792 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
793 ip->i_d.di_extsize = pip->i_d.di_extsize;
794 }
abbede1b 795 } else if (S_ISREG(mode)) {
613d7043 796 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
365ca83d 797 di_flags |= XFS_DIFLAG_REALTIME;
dd9f438e
NS
798 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
799 di_flags |= XFS_DIFLAG_EXTSIZE;
800 ip->i_d.di_extsize = pip->i_d.di_extsize;
801 }
1da177e4
LT
802 }
803 if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
804 xfs_inherit_noatime)
365ca83d 805 di_flags |= XFS_DIFLAG_NOATIME;
1da177e4
LT
806 if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
807 xfs_inherit_nodump)
365ca83d 808 di_flags |= XFS_DIFLAG_NODUMP;
1da177e4
LT
809 if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
810 xfs_inherit_sync)
365ca83d 811 di_flags |= XFS_DIFLAG_SYNC;
1da177e4
LT
812 if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
813 xfs_inherit_nosymlinks)
365ca83d
NS
814 di_flags |= XFS_DIFLAG_NOSYMLINKS;
815 if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
816 di_flags |= XFS_DIFLAG_PROJINHERIT;
d3446eac
BN
817 if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
818 xfs_inherit_nodefrag)
819 di_flags |= XFS_DIFLAG_NODEFRAG;
2a82b8be
DC
820 if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
821 di_flags |= XFS_DIFLAG_FILESTREAM;
365ca83d 822 ip->i_d.di_flags |= di_flags;
1da177e4
LT
823 }
824 /* FALLTHROUGH */
825 case S_IFLNK:
826 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
827 ip->i_df.if_flags = XFS_IFEXTENTS;
828 ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0;
829 ip->i_df.if_u1.if_extents = NULL;
830 break;
831 default:
832 ASSERT(0);
833 }
834 /*
835 * Attribute fork settings for new inode.
836 */
837 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
838 ip->i_d.di_anextents = 0;
839
840 /*
841 * Log the new values stuffed into the inode.
842 */
ddc3415a 843 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1da177e4
LT
844 xfs_trans_log_inode(tp, ip, flags);
845
b83bd138 846 /* now that we have an i_mode we can setup inode ops and unlock */
41be8bed 847 xfs_setup_inode(ip);
1da177e4 848
bf904248
DC
849 /* now we have set up the vfs inode we can associate the filestream */
850 if (filestreams) {
851 error = xfs_filestream_associate(pip, ip);
852 if (error < 0)
853 return -error;
854 if (!error)
855 xfs_iflags_set(ip, XFS_IFILESTREAM);
856 }
857
1da177e4
LT
858 *ipp = ip;
859 return 0;
860}
861
e546cb79
DC
862/*
863 * Allocates a new inode from disk and return a pointer to the
864 * incore copy. This routine will internally commit the current
865 * transaction and allocate a new one if the Space Manager needed
866 * to do an allocation to replenish the inode free-list.
867 *
868 * This routine is designed to be called from xfs_create and
869 * xfs_create_dir.
870 *
871 */
872int
873xfs_dir_ialloc(
874 xfs_trans_t **tpp, /* input: current transaction;
875 output: may be a new transaction. */
876 xfs_inode_t *dp, /* directory within whose allocate
877 the inode. */
878 umode_t mode,
879 xfs_nlink_t nlink,
880 xfs_dev_t rdev,
881 prid_t prid, /* project id */
882 int okalloc, /* ok to allocate new space */
883 xfs_inode_t **ipp, /* pointer to inode; it will be
884 locked. */
885 int *committed)
886
887{
888 xfs_trans_t *tp;
889 xfs_trans_t *ntp;
890 xfs_inode_t *ip;
891 xfs_buf_t *ialloc_context = NULL;
892 int code;
e546cb79
DC
893 void *dqinfo;
894 uint tflags;
895
896 tp = *tpp;
897 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
898
899 /*
900 * xfs_ialloc will return a pointer to an incore inode if
901 * the Space Manager has an available inode on the free
902 * list. Otherwise, it will do an allocation and replenish
903 * the freelist. Since we can only do one allocation per
904 * transaction without deadlocks, we will need to commit the
905 * current transaction and start a new one. We will then
906 * need to call xfs_ialloc again to get the inode.
907 *
908 * If xfs_ialloc did an allocation to replenish the freelist,
909 * it returns the bp containing the head of the freelist as
910 * ialloc_context. We will hold a lock on it across the
911 * transaction commit so that no other process can steal
912 * the inode(s) that we've just allocated.
913 */
914 code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid, okalloc,
915 &ialloc_context, &ip);
916
917 /*
918 * Return an error if we were unable to allocate a new inode.
919 * This should only happen if we run out of space on disk or
920 * encounter a disk error.
921 */
922 if (code) {
923 *ipp = NULL;
924 return code;
925 }
926 if (!ialloc_context && !ip) {
927 *ipp = NULL;
928 return XFS_ERROR(ENOSPC);
929 }
930
931 /*
932 * If the AGI buffer is non-NULL, then we were unable to get an
933 * inode in one operation. We need to commit the current
934 * transaction and call xfs_ialloc() again. It is guaranteed
935 * to succeed the second time.
936 */
937 if (ialloc_context) {
3d3c8b52
JL
938 struct xfs_trans_res tres;
939
e546cb79
DC
940 /*
941 * Normally, xfs_trans_commit releases all the locks.
942 * We call bhold to hang on to the ialloc_context across
943 * the commit. Holding this buffer prevents any other
944 * processes from doing any allocations in this
945 * allocation group.
946 */
947 xfs_trans_bhold(tp, ialloc_context);
948 /*
949 * Save the log reservation so we can use
950 * them in the next transaction.
951 */
3d3c8b52
JL
952 tres.tr_logres = xfs_trans_get_log_res(tp);
953 tres.tr_logcount = xfs_trans_get_log_count(tp);
e546cb79
DC
954
955 /*
956 * We want the quota changes to be associated with the next
957 * transaction, NOT this one. So, detach the dqinfo from this
958 * and attach it to the next transaction.
959 */
960 dqinfo = NULL;
961 tflags = 0;
962 if (tp->t_dqinfo) {
963 dqinfo = (void *)tp->t_dqinfo;
964 tp->t_dqinfo = NULL;
965 tflags = tp->t_flags & XFS_TRANS_DQ_DIRTY;
966 tp->t_flags &= ~(XFS_TRANS_DQ_DIRTY);
967 }
968
969 ntp = xfs_trans_dup(tp);
970 code = xfs_trans_commit(tp, 0);
971 tp = ntp;
972 if (committed != NULL) {
973 *committed = 1;
974 }
975 /*
976 * If we get an error during the commit processing,
977 * release the buffer that is still held and return
978 * to the caller.
979 */
980 if (code) {
981 xfs_buf_relse(ialloc_context);
982 if (dqinfo) {
983 tp->t_dqinfo = dqinfo;
984 xfs_trans_free_dqinfo(tp);
985 }
986 *tpp = ntp;
987 *ipp = NULL;
988 return code;
989 }
990
991 /*
992 * transaction commit worked ok so we can drop the extra ticket
993 * reference that we gained in xfs_trans_dup()
994 */
995 xfs_log_ticket_put(tp->t_ticket);
3d3c8b52
JL
996 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
997 code = xfs_trans_reserve(tp, &tres, 0, 0);
998
e546cb79
DC
999 /*
1000 * Re-attach the quota info that we detached from prev trx.
1001 */
1002 if (dqinfo) {
1003 tp->t_dqinfo = dqinfo;
1004 tp->t_flags |= tflags;
1005 }
1006
1007 if (code) {
1008 xfs_buf_relse(ialloc_context);
1009 *tpp = ntp;
1010 *ipp = NULL;
1011 return code;
1012 }
1013 xfs_trans_bjoin(tp, ialloc_context);
1014
1015 /*
1016 * Call ialloc again. Since we've locked out all
1017 * other allocations in this allocation group,
1018 * this call should always succeed.
1019 */
1020 code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid,
1021 okalloc, &ialloc_context, &ip);
1022
1023 /*
1024 * If we get an error at this point, return to the caller
1025 * so that the current transaction can be aborted.
1026 */
1027 if (code) {
1028 *tpp = tp;
1029 *ipp = NULL;
1030 return code;
1031 }
1032 ASSERT(!ialloc_context && ip);
1033
1034 } else {
1035 if (committed != NULL)
1036 *committed = 0;
1037 }
1038
1039 *ipp = ip;
1040 *tpp = tp;
1041
1042 return 0;
1043}
1044
1045/*
1046 * Decrement the link count on an inode & log the change.
1047 * If this causes the link count to go to zero, initiate the
1048 * logging activity required to truncate a file.
1049 */
1050int /* error */
1051xfs_droplink(
1052 xfs_trans_t *tp,
1053 xfs_inode_t *ip)
1054{
1055 int error;
1056
1057 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1058
1059 ASSERT (ip->i_d.di_nlink > 0);
1060 ip->i_d.di_nlink--;
1061 drop_nlink(VFS_I(ip));
1062 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1063
1064 error = 0;
1065 if (ip->i_d.di_nlink == 0) {
1066 /*
1067 * We're dropping the last link to this file.
1068 * Move the on-disk inode to the AGI unlinked list.
1069 * From xfs_inactive() we will pull the inode from
1070 * the list and free it.
1071 */
1072 error = xfs_iunlink(tp, ip);
1073 }
1074 return error;
1075}
1076
1077/*
1078 * This gets called when the inode's version needs to be changed from 1 to 2.
1079 * Currently this happens when the nlink field overflows the old 16-bit value
1080 * or when chproj is called to change the project for the first time.
1081 * As a side effect the superblock version will also get rev'd
1082 * to contain the NLINK bit.
1083 */
1084void
1085xfs_bump_ino_vers2(
1086 xfs_trans_t *tp,
1087 xfs_inode_t *ip)
1088{
1089 xfs_mount_t *mp;
1090
1091 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1092 ASSERT(ip->i_d.di_version == 1);
1093
1094 ip->i_d.di_version = 2;
1095 ip->i_d.di_onlink = 0;
1096 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
1097 mp = tp->t_mountp;
1098 if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
1099 spin_lock(&mp->m_sb_lock);
1100 if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
1101 xfs_sb_version_addnlink(&mp->m_sb);
1102 spin_unlock(&mp->m_sb_lock);
1103 xfs_mod_sb(tp, XFS_SB_VERSIONNUM);
1104 } else {
1105 spin_unlock(&mp->m_sb_lock);
1106 }
1107 }
1108 /* Caller must log the inode */
1109}
1110
1111/*
1112 * Increment the link count on an inode & log the change.
1113 */
1114int
1115xfs_bumplink(
1116 xfs_trans_t *tp,
1117 xfs_inode_t *ip)
1118{
1119 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1120
1121 ASSERT(ip->i_d.di_nlink > 0);
1122 ip->i_d.di_nlink++;
1123 inc_nlink(VFS_I(ip));
1124 if ((ip->i_d.di_version == 1) &&
1125 (ip->i_d.di_nlink > XFS_MAXLINK_1)) {
1126 /*
1127 * The inode has increased its number of links beyond
1128 * what can fit in an old format inode. It now needs
1129 * to be converted to a version 2 inode with a 32 bit
1130 * link count. If this is the first inode in the file
1131 * system to do this, then we need to bump the superblock
1132 * version number as well.
1133 */
1134 xfs_bump_ino_vers2(tp, ip);
1135 }
1136
1137 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1138 return 0;
1139}
1140
c24b5dfa
DC
1141int
1142xfs_create(
1143 xfs_inode_t *dp,
1144 struct xfs_name *name,
1145 umode_t mode,
1146 xfs_dev_t rdev,
1147 xfs_inode_t **ipp)
1148{
1149 int is_dir = S_ISDIR(mode);
1150 struct xfs_mount *mp = dp->i_mount;
1151 struct xfs_inode *ip = NULL;
1152 struct xfs_trans *tp = NULL;
1153 int error;
1154 xfs_bmap_free_t free_list;
1155 xfs_fsblock_t first_block;
1156 bool unlock_dp_on_error = false;
1157 uint cancel_flags;
1158 int committed;
1159 prid_t prid;
1160 struct xfs_dquot *udqp = NULL;
1161 struct xfs_dquot *gdqp = NULL;
1162 struct xfs_dquot *pdqp = NULL;
3d3c8b52 1163 struct xfs_trans_res tres;
c24b5dfa 1164 uint resblks;
c24b5dfa
DC
1165
1166 trace_xfs_create(dp, name);
1167
1168 if (XFS_FORCED_SHUTDOWN(mp))
1169 return XFS_ERROR(EIO);
1170
163467d3 1171 prid = xfs_get_initial_prid(dp);
c24b5dfa
DC
1172
1173 /*
1174 * Make sure that we have allocated dquot(s) on disk.
1175 */
7aab1b28
DE
1176 error = xfs_qm_vop_dqalloc(dp, xfs_kuid_to_uid(current_fsuid()),
1177 xfs_kgid_to_gid(current_fsgid()), prid,
c24b5dfa
DC
1178 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
1179 &udqp, &gdqp, &pdqp);
1180 if (error)
1181 return error;
1182
1183 if (is_dir) {
1184 rdev = 0;
1185 resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
3d3c8b52
JL
1186 tres.tr_logres = M_RES(mp)->tr_mkdir.tr_logres;
1187 tres.tr_logcount = XFS_MKDIR_LOG_COUNT;
c24b5dfa
DC
1188 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
1189 } else {
1190 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
3d3c8b52
JL
1191 tres.tr_logres = M_RES(mp)->tr_create.tr_logres;
1192 tres.tr_logcount = XFS_CREATE_LOG_COUNT;
c24b5dfa
DC
1193 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1194 }
1195
1196 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1197
1198 /*
1199 * Initially assume that the file does not exist and
1200 * reserve the resources for that case. If that is not
1201 * the case we'll drop the one we have and get a more
1202 * appropriate transaction later.
1203 */
3d3c8b52
JL
1204 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
1205 error = xfs_trans_reserve(tp, &tres, resblks, 0);
c24b5dfa
DC
1206 if (error == ENOSPC) {
1207 /* flush outstanding delalloc blocks and retry */
1208 xfs_flush_inodes(mp);
3d3c8b52 1209 error = xfs_trans_reserve(tp, &tres, resblks, 0);
c24b5dfa
DC
1210 }
1211 if (error == ENOSPC) {
1212 /* No space at all so try a "no-allocation" reservation */
1213 resblks = 0;
3d3c8b52 1214 error = xfs_trans_reserve(tp, &tres, 0, 0);
c24b5dfa
DC
1215 }
1216 if (error) {
1217 cancel_flags = 0;
1218 goto out_trans_cancel;
1219 }
1220
1221 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
1222 unlock_dp_on_error = true;
1223
1224 xfs_bmap_init(&free_list, &first_block);
1225
1226 /*
1227 * Reserve disk quota and the inode.
1228 */
1229 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
1230 pdqp, resblks, 1, 0);
1231 if (error)
1232 goto out_trans_cancel;
1233
1234 error = xfs_dir_canenter(tp, dp, name, resblks);
1235 if (error)
1236 goto out_trans_cancel;
1237
1238 /*
1239 * A newly created regular or special file just has one directory
1240 * entry pointing to them, but a directory also the "." entry
1241 * pointing to itself.
1242 */
1243 error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev,
1244 prid, resblks > 0, &ip, &committed);
1245 if (error) {
1246 if (error == ENOSPC)
1247 goto out_trans_cancel;
1248 goto out_trans_abort;
1249 }
1250
1251 /*
1252 * Now we join the directory inode to the transaction. We do not do it
1253 * earlier because xfs_dir_ialloc might commit the previous transaction
1254 * (and release all the locks). An error from here on will result in
1255 * the transaction cancel unlocking dp so don't do it explicitly in the
1256 * error path.
1257 */
1258 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1259 unlock_dp_on_error = false;
1260
1261 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
1262 &first_block, &free_list, resblks ?
1263 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
1264 if (error) {
1265 ASSERT(error != ENOSPC);
1266 goto out_trans_abort;
1267 }
1268 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1269 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1270
1271 if (is_dir) {
1272 error = xfs_dir_init(tp, ip, dp);
1273 if (error)
1274 goto out_bmap_cancel;
1275
1276 error = xfs_bumplink(tp, dp);
1277 if (error)
1278 goto out_bmap_cancel;
1279 }
1280
1281 /*
1282 * If this is a synchronous mount, make sure that the
1283 * create transaction goes to disk before returning to
1284 * the user.
1285 */
1286 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
1287 xfs_trans_set_sync(tp);
1288
1289 /*
1290 * Attach the dquot(s) to the inodes and modify them incore.
1291 * These ids of the inode couldn't have changed since the new
1292 * inode has been locked ever since it was created.
1293 */
1294 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
1295
1296 error = xfs_bmap_finish(&tp, &free_list, &committed);
1297 if (error)
1298 goto out_bmap_cancel;
1299
1300 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1301 if (error)
1302 goto out_release_inode;
1303
1304 xfs_qm_dqrele(udqp);
1305 xfs_qm_dqrele(gdqp);
1306 xfs_qm_dqrele(pdqp);
1307
1308 *ipp = ip;
1309 return 0;
1310
1311 out_bmap_cancel:
1312 xfs_bmap_cancel(&free_list);
1313 out_trans_abort:
1314 cancel_flags |= XFS_TRANS_ABORT;
1315 out_trans_cancel:
1316 xfs_trans_cancel(tp, cancel_flags);
1317 out_release_inode:
1318 /*
1319 * Wait until after the current transaction is aborted to
1320 * release the inode. This prevents recursive transactions
1321 * and deadlocks from xfs_inactive.
1322 */
1323 if (ip)
1324 IRELE(ip);
1325
1326 xfs_qm_dqrele(udqp);
1327 xfs_qm_dqrele(gdqp);
1328 xfs_qm_dqrele(pdqp);
1329
1330 if (unlock_dp_on_error)
1331 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1332 return error;
1333}
1334
99b6436b
ZYW
1335int
1336xfs_create_tmpfile(
1337 struct xfs_inode *dp,
1338 struct dentry *dentry,
1339 umode_t mode)
1340{
1341 struct xfs_mount *mp = dp->i_mount;
1342 struct xfs_inode *ip = NULL;
1343 struct xfs_trans *tp = NULL;
1344 int error;
1345 uint cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1346 prid_t prid;
1347 struct xfs_dquot *udqp = NULL;
1348 struct xfs_dquot *gdqp = NULL;
1349 struct xfs_dquot *pdqp = NULL;
1350 struct xfs_trans_res *tres;
1351 uint resblks;
1352
1353 if (XFS_FORCED_SHUTDOWN(mp))
1354 return XFS_ERROR(EIO);
1355
1356 prid = xfs_get_initial_prid(dp);
1357
1358 /*
1359 * Make sure that we have allocated dquot(s) on disk.
1360 */
1361 error = xfs_qm_vop_dqalloc(dp, xfs_kuid_to_uid(current_fsuid()),
1362 xfs_kgid_to_gid(current_fsgid()), prid,
1363 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
1364 &udqp, &gdqp, &pdqp);
1365 if (error)
1366 return error;
1367
1368 resblks = XFS_IALLOC_SPACE_RES(mp);
1369 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE_TMPFILE);
1370
1371 tres = &M_RES(mp)->tr_create_tmpfile;
1372 error = xfs_trans_reserve(tp, tres, resblks, 0);
1373 if (error == ENOSPC) {
1374 /* No space at all so try a "no-allocation" reservation */
1375 resblks = 0;
1376 error = xfs_trans_reserve(tp, tres, 0, 0);
1377 }
1378 if (error) {
1379 cancel_flags = 0;
1380 goto out_trans_cancel;
1381 }
1382
1383 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
1384 pdqp, resblks, 1, 0);
1385 if (error)
1386 goto out_trans_cancel;
1387
1388 error = xfs_dir_ialloc(&tp, dp, mode, 1, 0,
1389 prid, resblks > 0, &ip, NULL);
1390 if (error) {
1391 if (error == ENOSPC)
1392 goto out_trans_cancel;
1393 goto out_trans_abort;
1394 }
1395
1396 if (mp->m_flags & XFS_MOUNT_WSYNC)
1397 xfs_trans_set_sync(tp);
1398
1399 /*
1400 * Attach the dquot(s) to the inodes and modify them incore.
1401 * These ids of the inode couldn't have changed since the new
1402 * inode has been locked ever since it was created.
1403 */
1404 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
1405
1406 ip->i_d.di_nlink--;
1407 d_tmpfile(dentry, VFS_I(ip));
1408 error = xfs_iunlink(tp, ip);
1409 if (error)
1410 goto out_trans_abort;
1411
1412 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1413 if (error)
1414 goto out_release_inode;
1415
1416 xfs_qm_dqrele(udqp);
1417 xfs_qm_dqrele(gdqp);
1418 xfs_qm_dqrele(pdqp);
1419
1420 return 0;
1421
1422 out_trans_abort:
1423 cancel_flags |= XFS_TRANS_ABORT;
1424 out_trans_cancel:
1425 xfs_trans_cancel(tp, cancel_flags);
1426 out_release_inode:
1427 /*
1428 * Wait until after the current transaction is aborted to
1429 * release the inode. This prevents recursive transactions
1430 * and deadlocks from xfs_inactive.
1431 */
1432 if (ip)
1433 IRELE(ip);
1434
1435 xfs_qm_dqrele(udqp);
1436 xfs_qm_dqrele(gdqp);
1437 xfs_qm_dqrele(pdqp);
1438
1439 return error;
1440}
1441
c24b5dfa
DC
1442int
1443xfs_link(
1444 xfs_inode_t *tdp,
1445 xfs_inode_t *sip,
1446 struct xfs_name *target_name)
1447{
1448 xfs_mount_t *mp = tdp->i_mount;
1449 xfs_trans_t *tp;
1450 int error;
1451 xfs_bmap_free_t free_list;
1452 xfs_fsblock_t first_block;
1453 int cancel_flags;
1454 int committed;
1455 int resblks;
1456
1457 trace_xfs_link(tdp, target_name);
1458
1459 ASSERT(!S_ISDIR(sip->i_d.di_mode));
1460
1461 if (XFS_FORCED_SHUTDOWN(mp))
1462 return XFS_ERROR(EIO);
1463
1464 error = xfs_qm_dqattach(sip, 0);
1465 if (error)
1466 goto std_return;
1467
1468 error = xfs_qm_dqattach(tdp, 0);
1469 if (error)
1470 goto std_return;
1471
1472 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
1473 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1474 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
3d3c8b52 1475 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_link, resblks, 0);
c24b5dfa
DC
1476 if (error == ENOSPC) {
1477 resblks = 0;
3d3c8b52 1478 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_link, 0, 0);
c24b5dfa
DC
1479 }
1480 if (error) {
1481 cancel_flags = 0;
1482 goto error_return;
1483 }
1484
1485 xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
1486
1487 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
1488 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
1489
1490 /*
1491 * If we are using project inheritance, we only allow hard link
1492 * creation in our tree when the project IDs are the same; else
1493 * the tree quota mechanism could be circumvented.
1494 */
1495 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
1496 (xfs_get_projid(tdp) != xfs_get_projid(sip)))) {
1497 error = XFS_ERROR(EXDEV);
1498 goto error_return;
1499 }
1500
1501 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
1502 if (error)
1503 goto error_return;
1504
1505 xfs_bmap_init(&free_list, &first_block);
1506
1507 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
1508 &first_block, &free_list, resblks);
1509 if (error)
1510 goto abort_return;
1511 xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1512 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
1513
1514 error = xfs_bumplink(tp, sip);
1515 if (error)
1516 goto abort_return;
1517
1518 /*
1519 * If this is a synchronous mount, make sure that the
1520 * link transaction goes to disk before returning to
1521 * the user.
1522 */
1523 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1524 xfs_trans_set_sync(tp);
1525 }
1526
1527 error = xfs_bmap_finish (&tp, &free_list, &committed);
1528 if (error) {
1529 xfs_bmap_cancel(&free_list);
1530 goto abort_return;
1531 }
1532
1533 return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1534
1535 abort_return:
1536 cancel_flags |= XFS_TRANS_ABORT;
1537 error_return:
1538 xfs_trans_cancel(tp, cancel_flags);
1539 std_return:
1540 return error;
1541}
1542
1da177e4 1543/*
8f04c47a
CH
1544 * Free up the underlying blocks past new_size. The new size must be smaller
1545 * than the current size. This routine can be used both for the attribute and
1546 * data fork, and does not modify the inode size, which is left to the caller.
1da177e4 1547 *
f6485057
DC
1548 * The transaction passed to this routine must have made a permanent log
1549 * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the
1550 * given transaction and start new ones, so make sure everything involved in
1551 * the transaction is tidy before calling here. Some transaction will be
1552 * returned to the caller to be committed. The incoming transaction must
1553 * already include the inode, and both inode locks must be held exclusively.
1554 * The inode must also be "held" within the transaction. On return the inode
1555 * will be "held" within the returned transaction. This routine does NOT
1556 * require any disk space to be reserved for it within the transaction.
1da177e4 1557 *
f6485057
DC
1558 * If we get an error, we must return with the inode locked and linked into the
1559 * current transaction. This keeps things simple for the higher level code,
1560 * because it always knows that the inode is locked and held in the transaction
1561 * that returns to it whether errors occur or not. We don't mark the inode
1562 * dirty on error so that transactions can be easily aborted if possible.
1da177e4
LT
1563 */
1564int
8f04c47a
CH
1565xfs_itruncate_extents(
1566 struct xfs_trans **tpp,
1567 struct xfs_inode *ip,
1568 int whichfork,
1569 xfs_fsize_t new_size)
1da177e4 1570{
8f04c47a
CH
1571 struct xfs_mount *mp = ip->i_mount;
1572 struct xfs_trans *tp = *tpp;
1573 struct xfs_trans *ntp;
1574 xfs_bmap_free_t free_list;
1575 xfs_fsblock_t first_block;
1576 xfs_fileoff_t first_unmap_block;
1577 xfs_fileoff_t last_block;
1578 xfs_filblks_t unmap_len;
1579 int committed;
1580 int error = 0;
1581 int done = 0;
1da177e4 1582
0b56185b
CH
1583 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1584 ASSERT(!atomic_read(&VFS_I(ip)->i_count) ||
1585 xfs_isilocked(ip, XFS_IOLOCK_EXCL));
ce7ae151 1586 ASSERT(new_size <= XFS_ISIZE(ip));
8f04c47a 1587 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1da177e4 1588 ASSERT(ip->i_itemp != NULL);
898621d5 1589 ASSERT(ip->i_itemp->ili_lock_flags == 0);
8f04c47a 1590 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1da177e4 1591
673e8e59
CH
1592 trace_xfs_itruncate_extents_start(ip, new_size);
1593
1da177e4
LT
1594 /*
1595 * Since it is possible for space to become allocated beyond
1596 * the end of the file (in a crash where the space is allocated
1597 * but the inode size is not yet updated), simply remove any
1598 * blocks which show up between the new EOF and the maximum
1599 * possible file size. If the first block to be removed is
1600 * beyond the maximum file size (ie it is the same as last_block),
1601 * then there is nothing to do.
1602 */
8f04c47a 1603 first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
32972383 1604 last_block = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
8f04c47a
CH
1605 if (first_unmap_block == last_block)
1606 return 0;
1607
1608 ASSERT(first_unmap_block < last_block);
1609 unmap_len = last_block - first_unmap_block + 1;
1da177e4 1610 while (!done) {
9d87c319 1611 xfs_bmap_init(&free_list, &first_block);
8f04c47a 1612 error = xfs_bunmapi(tp, ip,
3e57ecf6 1613 first_unmap_block, unmap_len,
8f04c47a 1614 xfs_bmapi_aflag(whichfork),
1da177e4 1615 XFS_ITRUNC_MAX_EXTENTS,
3e57ecf6 1616 &first_block, &free_list,
b4e9181e 1617 &done);
8f04c47a
CH
1618 if (error)
1619 goto out_bmap_cancel;
1da177e4
LT
1620
1621 /*
1622 * Duplicate the transaction that has the permanent
1623 * reservation and commit the old transaction.
1624 */
8f04c47a 1625 error = xfs_bmap_finish(&tp, &free_list, &committed);
898621d5 1626 if (committed)
ddc3415a 1627 xfs_trans_ijoin(tp, ip, 0);
8f04c47a
CH
1628 if (error)
1629 goto out_bmap_cancel;
1da177e4
LT
1630
1631 if (committed) {
1632 /*
f6485057 1633 * Mark the inode dirty so it will be logged and
e5720eec 1634 * moved forward in the log as part of every commit.
1da177e4 1635 */
8f04c47a 1636 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1da177e4 1637 }
f6485057 1638
8f04c47a
CH
1639 ntp = xfs_trans_dup(tp);
1640 error = xfs_trans_commit(tp, 0);
1641 tp = ntp;
e5720eec 1642
ddc3415a 1643 xfs_trans_ijoin(tp, ip, 0);
f6485057 1644
cc09c0dc 1645 if (error)
8f04c47a
CH
1646 goto out;
1647
cc09c0dc 1648 /*
8f04c47a 1649 * Transaction commit worked ok so we can drop the extra ticket
cc09c0dc
DC
1650 * reference that we gained in xfs_trans_dup()
1651 */
8f04c47a 1652 xfs_log_ticket_put(tp->t_ticket);
3d3c8b52 1653 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0);
f6485057 1654 if (error)
8f04c47a 1655 goto out;
1da177e4 1656 }
8f04c47a 1657
673e8e59
CH
1658 /*
1659 * Always re-log the inode so that our permanent transaction can keep
1660 * on rolling it forward in the log.
1661 */
1662 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1663
1664 trace_xfs_itruncate_extents_end(ip, new_size);
1665
8f04c47a
CH
1666out:
1667 *tpp = tp;
1668 return error;
1669out_bmap_cancel:
1da177e4 1670 /*
8f04c47a
CH
1671 * If the bunmapi call encounters an error, return to the caller where
1672 * the transaction can be properly aborted. We just need to make sure
1673 * we're not holding any resources that we were not when we came in.
1da177e4 1674 */
8f04c47a
CH
1675 xfs_bmap_cancel(&free_list);
1676 goto out;
1677}
1678
c24b5dfa
DC
1679int
1680xfs_release(
1681 xfs_inode_t *ip)
1682{
1683 xfs_mount_t *mp = ip->i_mount;
1684 int error;
1685
1686 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
1687 return 0;
1688
1689 /* If this is a read-only mount, don't do this (would generate I/O) */
1690 if (mp->m_flags & XFS_MOUNT_RDONLY)
1691 return 0;
1692
1693 if (!XFS_FORCED_SHUTDOWN(mp)) {
1694 int truncated;
1695
1696 /*
1697 * If we are using filestreams, and we have an unlinked
1698 * file that we are processing the last close on, then nothing
1699 * will be able to reopen and write to this file. Purge this
1700 * inode from the filestreams cache so that it doesn't delay
1701 * teardown of the inode.
1702 */
1703 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
1704 xfs_filestream_deassociate(ip);
1705
1706 /*
1707 * If we previously truncated this file and removed old data
1708 * in the process, we want to initiate "early" writeout on
1709 * the last close. This is an attempt to combat the notorious
1710 * NULL files problem which is particularly noticeable from a
1711 * truncate down, buffered (re-)write (delalloc), followed by
1712 * a crash. What we are effectively doing here is
1713 * significantly reducing the time window where we'd otherwise
1714 * be exposed to that problem.
1715 */
1716 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
1717 if (truncated) {
1718 xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE);
1719 if (VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0) {
1720 error = -filemap_flush(VFS_I(ip)->i_mapping);
1721 if (error)
1722 return error;
1723 }
1724 }
1725 }
1726
1727 if (ip->i_d.di_nlink == 0)
1728 return 0;
1729
1730 if (xfs_can_free_eofblocks(ip, false)) {
1731
1732 /*
1733 * If we can't get the iolock just skip truncating the blocks
1734 * past EOF because we could deadlock with the mmap_sem
1735 * otherwise. We'll get another chance to drop them once the
1736 * last reference to the inode is dropped, so we'll never leak
1737 * blocks permanently.
1738 *
1739 * Further, check if the inode is being opened, written and
1740 * closed frequently and we have delayed allocation blocks
1741 * outstanding (e.g. streaming writes from the NFS server),
1742 * truncating the blocks past EOF will cause fragmentation to
1743 * occur.
1744 *
1745 * In this case don't do the truncation, either, but we have to
1746 * be careful how we detect this case. Blocks beyond EOF show
1747 * up as i_delayed_blks even when the inode is clean, so we
1748 * need to truncate them away first before checking for a dirty
1749 * release. Hence on the first dirty close we will still remove
1750 * the speculative allocation, but after that we will leave it
1751 * in place.
1752 */
1753 if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE))
1754 return 0;
1755
1756 error = xfs_free_eofblocks(mp, ip, true);
1757 if (error && error != EAGAIN)
1758 return error;
1759
1760 /* delalloc blocks after truncation means it really is dirty */
1761 if (ip->i_delayed_blks)
1762 xfs_iflags_set(ip, XFS_IDIRTY_RELEASE);
1763 }
1764 return 0;
1765}
1766
f7be2d7f
BF
1767/*
1768 * xfs_inactive_truncate
1769 *
1770 * Called to perform a truncate when an inode becomes unlinked.
1771 */
1772STATIC int
1773xfs_inactive_truncate(
1774 struct xfs_inode *ip)
1775{
1776 struct xfs_mount *mp = ip->i_mount;
1777 struct xfs_trans *tp;
1778 int error;
1779
1780 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1781 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0);
1782 if (error) {
1783 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1784 xfs_trans_cancel(tp, 0);
1785 return error;
1786 }
1787
1788 xfs_ilock(ip, XFS_ILOCK_EXCL);
1789 xfs_trans_ijoin(tp, ip, 0);
1790
1791 /*
1792 * Log the inode size first to prevent stale data exposure in the event
1793 * of a system crash before the truncate completes. See the related
1794 * comment in xfs_setattr_size() for details.
1795 */
1796 ip->i_d.di_size = 0;
1797 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1798
1799 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
1800 if (error)
1801 goto error_trans_cancel;
1802
1803 ASSERT(ip->i_d.di_nextents == 0);
1804
1805 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1806 if (error)
1807 goto error_unlock;
1808
1809 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1810 return 0;
1811
1812error_trans_cancel:
1813 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1814error_unlock:
1815 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1816 return error;
1817}
1818
88877d2b
BF
1819/*
1820 * xfs_inactive_ifree()
1821 *
1822 * Perform the inode free when an inode is unlinked.
1823 */
1824STATIC int
1825xfs_inactive_ifree(
1826 struct xfs_inode *ip)
1827{
1828 xfs_bmap_free_t free_list;
1829 xfs_fsblock_t first_block;
1830 int committed;
1831 struct xfs_mount *mp = ip->i_mount;
1832 struct xfs_trans *tp;
1833 int error;
1834
1835 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1836 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_ifree, 0, 0);
1837 if (error) {
1838 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1839 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
1840 return error;
1841 }
1842
1843 xfs_ilock(ip, XFS_ILOCK_EXCL);
1844 xfs_trans_ijoin(tp, ip, 0);
1845
1846 xfs_bmap_init(&free_list, &first_block);
1847 error = xfs_ifree(tp, ip, &free_list);
1848 if (error) {
1849 /*
1850 * If we fail to free the inode, shut down. The cancel
1851 * might do that, we need to make sure. Otherwise the
1852 * inode might be lost for a long time or forever.
1853 */
1854 if (!XFS_FORCED_SHUTDOWN(mp)) {
1855 xfs_notice(mp, "%s: xfs_ifree returned error %d",
1856 __func__, error);
1857 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1858 }
1859 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1860 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1861 return error;
1862 }
1863
1864 /*
1865 * Credit the quota account(s). The inode is gone.
1866 */
1867 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1868
1869 /*
1870 * Just ignore errors at this point. There is nothing we can
1871 * do except to try to keep going. Make sure it's not a silent
1872 * error.
1873 */
1874 error = xfs_bmap_finish(&tp, &free_list, &committed);
1875 if (error)
1876 xfs_notice(mp, "%s: xfs_bmap_finish returned error %d",
1877 __func__, error);
1878 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1879 if (error)
1880 xfs_notice(mp, "%s: xfs_trans_commit returned error %d",
1881 __func__, error);
1882
1883 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1884 return 0;
1885}
1886
c24b5dfa
DC
1887/*
1888 * xfs_inactive
1889 *
1890 * This is called when the vnode reference count for the vnode
1891 * goes to zero. If the file has been unlinked, then it must
1892 * now be truncated. Also, we clear all of the read-ahead state
1893 * kept for the inode here since the file is now closed.
1894 */
74564fb4 1895void
c24b5dfa
DC
1896xfs_inactive(
1897 xfs_inode_t *ip)
1898{
3d3c8b52 1899 struct xfs_mount *mp;
3d3c8b52
JL
1900 int error;
1901 int truncate = 0;
c24b5dfa
DC
1902
1903 /*
1904 * If the inode is already free, then there can be nothing
1905 * to clean up here.
1906 */
d948709b 1907 if (ip->i_d.di_mode == 0) {
c24b5dfa
DC
1908 ASSERT(ip->i_df.if_real_bytes == 0);
1909 ASSERT(ip->i_df.if_broot_bytes == 0);
74564fb4 1910 return;
c24b5dfa
DC
1911 }
1912
1913 mp = ip->i_mount;
1914
c24b5dfa
DC
1915 /* If this is a read-only mount, don't do this (would generate I/O) */
1916 if (mp->m_flags & XFS_MOUNT_RDONLY)
74564fb4 1917 return;
c24b5dfa
DC
1918
1919 if (ip->i_d.di_nlink != 0) {
1920 /*
1921 * force is true because we are evicting an inode from the
1922 * cache. Post-eof blocks must be freed, lest we end up with
1923 * broken free space accounting.
1924 */
74564fb4
BF
1925 if (xfs_can_free_eofblocks(ip, true))
1926 xfs_free_eofblocks(mp, ip, false);
1927
1928 return;
c24b5dfa
DC
1929 }
1930
1931 if (S_ISREG(ip->i_d.di_mode) &&
1932 (ip->i_d.di_size != 0 || XFS_ISIZE(ip) != 0 ||
1933 ip->i_d.di_nextents > 0 || ip->i_delayed_blks > 0))
1934 truncate = 1;
1935
1936 error = xfs_qm_dqattach(ip, 0);
1937 if (error)
74564fb4 1938 return;
c24b5dfa 1939
f7be2d7f 1940 if (S_ISLNK(ip->i_d.di_mode))
36b21dde 1941 error = xfs_inactive_symlink(ip);
f7be2d7f
BF
1942 else if (truncate)
1943 error = xfs_inactive_truncate(ip);
1944 if (error)
74564fb4 1945 return;
c24b5dfa
DC
1946
1947 /*
1948 * If there are attributes associated with the file then blow them away
1949 * now. The code calls a routine that recursively deconstructs the
1950 * attribute fork. We need to just commit the current transaction
1951 * because we can't use it for xfs_attr_inactive().
1952 */
1953 if (ip->i_d.di_anextents > 0) {
1954 ASSERT(ip->i_d.di_forkoff != 0);
1955
c24b5dfa
DC
1956 error = xfs_attr_inactive(ip);
1957 if (error)
74564fb4 1958 return;
c24b5dfa
DC
1959 }
1960
1961 if (ip->i_afp)
1962 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1963
1964 ASSERT(ip->i_d.di_anextents == 0);
1965
1966 /*
1967 * Free the inode.
1968 */
88877d2b
BF
1969 error = xfs_inactive_ifree(ip);
1970 if (error)
74564fb4 1971 return;
c24b5dfa
DC
1972
1973 /*
1974 * Release the dquots held by inode, if any.
1975 */
1976 xfs_qm_dqdetach(ip);
c24b5dfa
DC
1977}
1978
1da177e4
LT
1979/*
1980 * This is called when the inode's link count goes to 0.
1981 * We place the on-disk inode on a list in the AGI. It
1982 * will be pulled from this list when the inode is freed.
1983 */
1984int
1985xfs_iunlink(
1986 xfs_trans_t *tp,
1987 xfs_inode_t *ip)
1988{
1989 xfs_mount_t *mp;
1990 xfs_agi_t *agi;
1991 xfs_dinode_t *dip;
1992 xfs_buf_t *agibp;
1993 xfs_buf_t *ibp;
1da177e4
LT
1994 xfs_agino_t agino;
1995 short bucket_index;
1996 int offset;
1997 int error;
1da177e4
LT
1998
1999 ASSERT(ip->i_d.di_nlink == 0);
2000 ASSERT(ip->i_d.di_mode != 0);
1da177e4
LT
2001
2002 mp = tp->t_mountp;
2003
1da177e4
LT
2004 /*
2005 * Get the agi buffer first. It ensures lock ordering
2006 * on the list.
2007 */
5e1be0fb 2008 error = xfs_read_agi(mp, tp, XFS_INO_TO_AGNO(mp, ip->i_ino), &agibp);
859d7182 2009 if (error)
1da177e4 2010 return error;
1da177e4 2011 agi = XFS_BUF_TO_AGI(agibp);
5e1be0fb 2012
1da177e4
LT
2013 /*
2014 * Get the index into the agi hash table for the
2015 * list this inode will go on.
2016 */
2017 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
2018 ASSERT(agino != 0);
2019 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
2020 ASSERT(agi->agi_unlinked[bucket_index]);
16259e7d 2021 ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino);
1da177e4 2022
69ef921b 2023 if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) {
1da177e4
LT
2024 /*
2025 * There is already another inode in the bucket we need
2026 * to add ourselves to. Add us at the front of the list.
2027 * Here we put the head pointer into our next pointer,
2028 * and then we fall through to point the head at us.
2029 */
475ee413
CH
2030 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
2031 0, 0);
c319b58b
VA
2032 if (error)
2033 return error;
2034
69ef921b 2035 ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO));
1da177e4 2036 dip->di_next_unlinked = agi->agi_unlinked[bucket_index];
92bfc6e7 2037 offset = ip->i_imap.im_boffset +
1da177e4 2038 offsetof(xfs_dinode_t, di_next_unlinked);
0a32c26e
DC
2039
2040 /* need to recalc the inode CRC if appropriate */
2041 xfs_dinode_calc_crc(mp, dip);
2042
1da177e4
LT
2043 xfs_trans_inode_buf(tp, ibp);
2044 xfs_trans_log_buf(tp, ibp, offset,
2045 (offset + sizeof(xfs_agino_t) - 1));
2046 xfs_inobp_check(mp, ibp);
2047 }
2048
2049 /*
2050 * Point the bucket head pointer at the inode being inserted.
2051 */
2052 ASSERT(agino != 0);
16259e7d 2053 agi->agi_unlinked[bucket_index] = cpu_to_be32(agino);
1da177e4
LT
2054 offset = offsetof(xfs_agi_t, agi_unlinked) +
2055 (sizeof(xfs_agino_t) * bucket_index);
2056 xfs_trans_log_buf(tp, agibp, offset,
2057 (offset + sizeof(xfs_agino_t) - 1));
2058 return 0;
2059}
2060
2061/*
2062 * Pull the on-disk inode from the AGI unlinked list.
2063 */
2064STATIC int
2065xfs_iunlink_remove(
2066 xfs_trans_t *tp,
2067 xfs_inode_t *ip)
2068{
2069 xfs_ino_t next_ino;
2070 xfs_mount_t *mp;
2071 xfs_agi_t *agi;
2072 xfs_dinode_t *dip;
2073 xfs_buf_t *agibp;
2074 xfs_buf_t *ibp;
2075 xfs_agnumber_t agno;
1da177e4
LT
2076 xfs_agino_t agino;
2077 xfs_agino_t next_agino;
2078 xfs_buf_t *last_ibp;
6fdf8ccc 2079 xfs_dinode_t *last_dip = NULL;
1da177e4 2080 short bucket_index;
6fdf8ccc 2081 int offset, last_offset = 0;
1da177e4 2082 int error;
1da177e4 2083
1da177e4 2084 mp = tp->t_mountp;
1da177e4 2085 agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
1da177e4
LT
2086
2087 /*
2088 * Get the agi buffer first. It ensures lock ordering
2089 * on the list.
2090 */
5e1be0fb
CH
2091 error = xfs_read_agi(mp, tp, agno, &agibp);
2092 if (error)
1da177e4 2093 return error;
5e1be0fb 2094
1da177e4 2095 agi = XFS_BUF_TO_AGI(agibp);
5e1be0fb 2096
1da177e4
LT
2097 /*
2098 * Get the index into the agi hash table for the
2099 * list this inode will go on.
2100 */
2101 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
2102 ASSERT(agino != 0);
2103 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
69ef921b 2104 ASSERT(agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO));
1da177e4
LT
2105 ASSERT(agi->agi_unlinked[bucket_index]);
2106
16259e7d 2107 if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) {
1da177e4 2108 /*
475ee413
CH
2109 * We're at the head of the list. Get the inode's on-disk
2110 * buffer to see if there is anyone after us on the list.
2111 * Only modify our next pointer if it is not already NULLAGINO.
2112 * This saves us the overhead of dealing with the buffer when
2113 * there is no need to change it.
1da177e4 2114 */
475ee413
CH
2115 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
2116 0, 0);
1da177e4 2117 if (error) {
475ee413 2118 xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.",
0b932ccc 2119 __func__, error);
1da177e4
LT
2120 return error;
2121 }
347d1c01 2122 next_agino = be32_to_cpu(dip->di_next_unlinked);
1da177e4
LT
2123 ASSERT(next_agino != 0);
2124 if (next_agino != NULLAGINO) {
347d1c01 2125 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
92bfc6e7 2126 offset = ip->i_imap.im_boffset +
1da177e4 2127 offsetof(xfs_dinode_t, di_next_unlinked);
0a32c26e
DC
2128
2129 /* need to recalc the inode CRC if appropriate */
2130 xfs_dinode_calc_crc(mp, dip);
2131
1da177e4
LT
2132 xfs_trans_inode_buf(tp, ibp);
2133 xfs_trans_log_buf(tp, ibp, offset,
2134 (offset + sizeof(xfs_agino_t) - 1));
2135 xfs_inobp_check(mp, ibp);
2136 } else {
2137 xfs_trans_brelse(tp, ibp);
2138 }
2139 /*
2140 * Point the bucket head pointer at the next inode.
2141 */
2142 ASSERT(next_agino != 0);
2143 ASSERT(next_agino != agino);
16259e7d 2144 agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino);
1da177e4
LT
2145 offset = offsetof(xfs_agi_t, agi_unlinked) +
2146 (sizeof(xfs_agino_t) * bucket_index);
2147 xfs_trans_log_buf(tp, agibp, offset,
2148 (offset + sizeof(xfs_agino_t) - 1));
2149 } else {
2150 /*
2151 * We need to search the list for the inode being freed.
2152 */
16259e7d 2153 next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
1da177e4
LT
2154 last_ibp = NULL;
2155 while (next_agino != agino) {
129dbc9a
CH
2156 struct xfs_imap imap;
2157
2158 if (last_ibp)
1da177e4 2159 xfs_trans_brelse(tp, last_ibp);
129dbc9a
CH
2160
2161 imap.im_blkno = 0;
1da177e4 2162 next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino);
129dbc9a
CH
2163
2164 error = xfs_imap(mp, tp, next_ino, &imap, 0);
2165 if (error) {
2166 xfs_warn(mp,
2167 "%s: xfs_imap returned error %d.",
2168 __func__, error);
2169 return error;
2170 }
2171
2172 error = xfs_imap_to_bp(mp, tp, &imap, &last_dip,
2173 &last_ibp, 0, 0);
1da177e4 2174 if (error) {
0b932ccc 2175 xfs_warn(mp,
129dbc9a 2176 "%s: xfs_imap_to_bp returned error %d.",
0b932ccc 2177 __func__, error);
1da177e4
LT
2178 return error;
2179 }
129dbc9a
CH
2180
2181 last_offset = imap.im_boffset;
347d1c01 2182 next_agino = be32_to_cpu(last_dip->di_next_unlinked);
1da177e4
LT
2183 ASSERT(next_agino != NULLAGINO);
2184 ASSERT(next_agino != 0);
2185 }
475ee413 2186
1da177e4 2187 /*
475ee413
CH
2188 * Now last_ibp points to the buffer previous to us on the
2189 * unlinked list. Pull us from the list.
1da177e4 2190 */
475ee413
CH
2191 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
2192 0, 0);
1da177e4 2193 if (error) {
475ee413 2194 xfs_warn(mp, "%s: xfs_imap_to_bp(2) returned error %d.",
0b932ccc 2195 __func__, error);
1da177e4
LT
2196 return error;
2197 }
347d1c01 2198 next_agino = be32_to_cpu(dip->di_next_unlinked);
1da177e4
LT
2199 ASSERT(next_agino != 0);
2200 ASSERT(next_agino != agino);
2201 if (next_agino != NULLAGINO) {
347d1c01 2202 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
92bfc6e7 2203 offset = ip->i_imap.im_boffset +
1da177e4 2204 offsetof(xfs_dinode_t, di_next_unlinked);
0a32c26e
DC
2205
2206 /* need to recalc the inode CRC if appropriate */
2207 xfs_dinode_calc_crc(mp, dip);
2208
1da177e4
LT
2209 xfs_trans_inode_buf(tp, ibp);
2210 xfs_trans_log_buf(tp, ibp, offset,
2211 (offset + sizeof(xfs_agino_t) - 1));
2212 xfs_inobp_check(mp, ibp);
2213 } else {
2214 xfs_trans_brelse(tp, ibp);
2215 }
2216 /*
2217 * Point the previous inode on the list to the next inode.
2218 */
347d1c01 2219 last_dip->di_next_unlinked = cpu_to_be32(next_agino);
1da177e4
LT
2220 ASSERT(next_agino != 0);
2221 offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked);
0a32c26e
DC
2222
2223 /* need to recalc the inode CRC if appropriate */
2224 xfs_dinode_calc_crc(mp, last_dip);
2225
1da177e4
LT
2226 xfs_trans_inode_buf(tp, last_ibp);
2227 xfs_trans_log_buf(tp, last_ibp, offset,
2228 (offset + sizeof(xfs_agino_t) - 1));
2229 xfs_inobp_check(mp, last_ibp);
2230 }
2231 return 0;
2232}
2233
5b3eed75 2234/*
0b8182db 2235 * A big issue when freeing the inode cluster is that we _cannot_ skip any
5b3eed75
DC
2236 * inodes that are in memory - they all must be marked stale and attached to
2237 * the cluster buffer.
2238 */
2a30f36d 2239STATIC int
1da177e4
LT
2240xfs_ifree_cluster(
2241 xfs_inode_t *free_ip,
2242 xfs_trans_t *tp,
2243 xfs_ino_t inum)
2244{
2245 xfs_mount_t *mp = free_ip->i_mount;
2246 int blks_per_cluster;
2247 int nbufs;
2248 int ninodes;
5b257b4a 2249 int i, j;
1da177e4
LT
2250 xfs_daddr_t blkno;
2251 xfs_buf_t *bp;
5b257b4a 2252 xfs_inode_t *ip;
1da177e4
LT
2253 xfs_inode_log_item_t *iip;
2254 xfs_log_item_t *lip;
5017e97d 2255 struct xfs_perag *pag;
1da177e4 2256
5017e97d 2257 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum));
1da177e4
LT
2258 if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
2259 blks_per_cluster = 1;
2260 ninodes = mp->m_sb.sb_inopblock;
2261 nbufs = XFS_IALLOC_BLOCKS(mp);
2262 } else {
2263 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
2264 mp->m_sb.sb_blocksize;
2265 ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
2266 nbufs = XFS_IALLOC_BLOCKS(mp) / blks_per_cluster;
2267 }
2268
1da177e4
LT
2269 for (j = 0; j < nbufs; j++, inum += ninodes) {
2270 blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
2271 XFS_INO_TO_AGBNO(mp, inum));
2272
5b257b4a
DC
2273 /*
2274 * We obtain and lock the backing buffer first in the process
2275 * here, as we have to ensure that any dirty inode that we
2276 * can't get the flush lock on is attached to the buffer.
2277 * If we scan the in-memory inodes first, then buffer IO can
2278 * complete before we get a lock on it, and hence we may fail
2279 * to mark all the active inodes on the buffer stale.
2280 */
2281 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
b6aff29f
DC
2282 mp->m_bsize * blks_per_cluster,
2283 XBF_UNMAPPED);
5b257b4a 2284
2a30f36d
CS
2285 if (!bp)
2286 return ENOMEM;
b0f539de
DC
2287
2288 /*
2289 * This buffer may not have been correctly initialised as we
2290 * didn't read it from disk. That's not important because we are
2291 * only using to mark the buffer as stale in the log, and to
2292 * attach stale cached inodes on it. That means it will never be
2293 * dispatched for IO. If it is, we want to know about it, and we
2294 * want it to fail. We can acheive this by adding a write
2295 * verifier to the buffer.
2296 */
1813dd64 2297 bp->b_ops = &xfs_inode_buf_ops;
b0f539de 2298
5b257b4a
DC
2299 /*
2300 * Walk the inodes already attached to the buffer and mark them
2301 * stale. These will all have the flush locks held, so an
5b3eed75
DC
2302 * in-memory inode walk can't lock them. By marking them all
2303 * stale first, we will not attempt to lock them in the loop
2304 * below as the XFS_ISTALE flag will be set.
5b257b4a 2305 */
adadbeef 2306 lip = bp->b_fspriv;
5b257b4a
DC
2307 while (lip) {
2308 if (lip->li_type == XFS_LI_INODE) {
2309 iip = (xfs_inode_log_item_t *)lip;
2310 ASSERT(iip->ili_logged == 1);
ca30b2a7 2311 lip->li_cb = xfs_istale_done;
5b257b4a
DC
2312 xfs_trans_ail_copy_lsn(mp->m_ail,
2313 &iip->ili_flush_lsn,
2314 &iip->ili_item.li_lsn);
2315 xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
5b257b4a
DC
2316 }
2317 lip = lip->li_bio_list;
2318 }
1da177e4 2319
5b3eed75 2320
1da177e4 2321 /*
5b257b4a
DC
2322 * For each inode in memory attempt to add it to the inode
2323 * buffer and set it up for being staled on buffer IO
2324 * completion. This is safe as we've locked out tail pushing
2325 * and flushing by locking the buffer.
1da177e4 2326 *
5b257b4a
DC
2327 * We have already marked every inode that was part of a
2328 * transaction stale above, which means there is no point in
2329 * even trying to lock them.
1da177e4 2330 */
1da177e4 2331 for (i = 0; i < ninodes; i++) {
5b3eed75 2332retry:
1a3e8f3d 2333 rcu_read_lock();
da353b0d
DC
2334 ip = radix_tree_lookup(&pag->pag_ici_root,
2335 XFS_INO_TO_AGINO(mp, (inum + i)));
1da177e4 2336
1a3e8f3d
DC
2337 /* Inode not in memory, nothing to do */
2338 if (!ip) {
2339 rcu_read_unlock();
1da177e4
LT
2340 continue;
2341 }
2342
1a3e8f3d
DC
2343 /*
2344 * because this is an RCU protected lookup, we could
2345 * find a recently freed or even reallocated inode
2346 * during the lookup. We need to check under the
2347 * i_flags_lock for a valid inode here. Skip it if it
2348 * is not valid, the wrong inode or stale.
2349 */
2350 spin_lock(&ip->i_flags_lock);
2351 if (ip->i_ino != inum + i ||
2352 __xfs_iflags_test(ip, XFS_ISTALE)) {
2353 spin_unlock(&ip->i_flags_lock);
2354 rcu_read_unlock();
2355 continue;
2356 }
2357 spin_unlock(&ip->i_flags_lock);
2358
5b3eed75
DC
2359 /*
2360 * Don't try to lock/unlock the current inode, but we
2361 * _cannot_ skip the other inodes that we did not find
2362 * in the list attached to the buffer and are not
2363 * already marked stale. If we can't lock it, back off
2364 * and retry.
2365 */
5b257b4a
DC
2366 if (ip != free_ip &&
2367 !xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
1a3e8f3d 2368 rcu_read_unlock();
5b3eed75
DC
2369 delay(1);
2370 goto retry;
1da177e4 2371 }
1a3e8f3d 2372 rcu_read_unlock();
1da177e4 2373
5b3eed75 2374 xfs_iflock(ip);
5b257b4a 2375 xfs_iflags_set(ip, XFS_ISTALE);
1da177e4 2376
5b3eed75
DC
2377 /*
2378 * we don't need to attach clean inodes or those only
2379 * with unlogged changes (which we throw away, anyway).
2380 */
1da177e4 2381 iip = ip->i_itemp;
5b3eed75 2382 if (!iip || xfs_inode_clean(ip)) {
5b257b4a 2383 ASSERT(ip != free_ip);
1da177e4
LT
2384 xfs_ifunlock(ip);
2385 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2386 continue;
2387 }
2388
f5d8d5c4
CH
2389 iip->ili_last_fields = iip->ili_fields;
2390 iip->ili_fields = 0;
1da177e4 2391 iip->ili_logged = 1;
7b2e2a31
DC
2392 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
2393 &iip->ili_item.li_lsn);
1da177e4 2394
ca30b2a7
CH
2395 xfs_buf_attach_iodone(bp, xfs_istale_done,
2396 &iip->ili_item);
5b257b4a
DC
2397
2398 if (ip != free_ip)
1da177e4 2399 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1da177e4
LT
2400 }
2401
5b3eed75 2402 xfs_trans_stale_inode_buf(tp, bp);
1da177e4
LT
2403 xfs_trans_binval(tp, bp);
2404 }
2405
5017e97d 2406 xfs_perag_put(pag);
2a30f36d 2407 return 0;
1da177e4
LT
2408}
2409
2410/*
2411 * This is called to return an inode to the inode free list.
2412 * The inode should already be truncated to 0 length and have
2413 * no pages associated with it. This routine also assumes that
2414 * the inode is already a part of the transaction.
2415 *
2416 * The on-disk copy of the inode will have been added to the list
2417 * of unlinked inodes in the AGI. We need to remove the inode from
2418 * that list atomically with respect to freeing it here.
2419 */
2420int
2421xfs_ifree(
2422 xfs_trans_t *tp,
2423 xfs_inode_t *ip,
2424 xfs_bmap_free_t *flist)
2425{
2426 int error;
2427 int delete;
2428 xfs_ino_t first_ino;
2429
579aa9ca 2430 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1da177e4
LT
2431 ASSERT(ip->i_d.di_nlink == 0);
2432 ASSERT(ip->i_d.di_nextents == 0);
2433 ASSERT(ip->i_d.di_anextents == 0);
ce7ae151 2434 ASSERT(ip->i_d.di_size == 0 || !S_ISREG(ip->i_d.di_mode));
1da177e4
LT
2435 ASSERT(ip->i_d.di_nblocks == 0);
2436
2437 /*
2438 * Pull the on-disk inode from the AGI unlinked list.
2439 */
2440 error = xfs_iunlink_remove(tp, ip);
1baaed8f 2441 if (error)
1da177e4 2442 return error;
1da177e4
LT
2443
2444 error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino);
1baaed8f 2445 if (error)
1da177e4 2446 return error;
1baaed8f 2447
1da177e4
LT
2448 ip->i_d.di_mode = 0; /* mark incore inode as free */
2449 ip->i_d.di_flags = 0;
2450 ip->i_d.di_dmevmask = 0;
2451 ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */
1da177e4
LT
2452 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
2453 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
2454 /*
2455 * Bump the generation count so no one will be confused
2456 * by reincarnations of this inode.
2457 */
2458 ip->i_d.di_gen++;
2459 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2460
1baaed8f 2461 if (delete)
2a30f36d 2462 error = xfs_ifree_cluster(ip, tp, first_ino);
1da177e4 2463
2a30f36d 2464 return error;
1da177e4
LT
2465}
2466
1da177e4 2467/*
60ec6783
CH
2468 * This is called to unpin an inode. The caller must have the inode locked
2469 * in at least shared mode so that the buffer cannot be subsequently pinned
2470 * once someone is waiting for it to be unpinned.
1da177e4 2471 */
60ec6783 2472static void
f392e631 2473xfs_iunpin(
60ec6783 2474 struct xfs_inode *ip)
1da177e4 2475{
579aa9ca 2476 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
1da177e4 2477
4aaf15d1
DC
2478 trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
2479
a3f74ffb 2480 /* Give the log a push to start the unpinning I/O */
60ec6783 2481 xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0);
a14a348b 2482
a3f74ffb 2483}
1da177e4 2484
f392e631
CH
2485static void
2486__xfs_iunpin_wait(
2487 struct xfs_inode *ip)
2488{
2489 wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
2490 DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
2491
2492 xfs_iunpin(ip);
2493
2494 do {
2495 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2496 if (xfs_ipincount(ip))
2497 io_schedule();
2498 } while (xfs_ipincount(ip));
2499 finish_wait(wq, &wait.wait);
2500}
2501
777df5af 2502void
a3f74ffb 2503xfs_iunpin_wait(
60ec6783 2504 struct xfs_inode *ip)
a3f74ffb 2505{
f392e631
CH
2506 if (xfs_ipincount(ip))
2507 __xfs_iunpin_wait(ip);
1da177e4
LT
2508}
2509
27320369
DC
2510/*
2511 * Removing an inode from the namespace involves removing the directory entry
2512 * and dropping the link count on the inode. Removing the directory entry can
2513 * result in locking an AGF (directory blocks were freed) and removing a link
2514 * count can result in placing the inode on an unlinked list which results in
2515 * locking an AGI.
2516 *
2517 * The big problem here is that we have an ordering constraint on AGF and AGI
2518 * locking - inode allocation locks the AGI, then can allocate a new extent for
2519 * new inodes, locking the AGF after the AGI. Similarly, freeing the inode
2520 * removes the inode from the unlinked list, requiring that we lock the AGI
2521 * first, and then freeing the inode can result in an inode chunk being freed
2522 * and hence freeing disk space requiring that we lock an AGF.
2523 *
2524 * Hence the ordering that is imposed by other parts of the code is AGI before
2525 * AGF. This means we cannot remove the directory entry before we drop the inode
2526 * reference count and put it on the unlinked list as this results in a lock
2527 * order of AGF then AGI, and this can deadlock against inode allocation and
2528 * freeing. Therefore we must drop the link counts before we remove the
2529 * directory entry.
2530 *
2531 * This is still safe from a transactional point of view - it is not until we
2532 * get to xfs_bmap_finish() that we have the possibility of multiple
2533 * transactions in this operation. Hence as long as we remove the directory
2534 * entry and drop the link count in the first transaction of the remove
2535 * operation, there are no transactional constraints on the ordering here.
2536 */
c24b5dfa
DC
2537int
2538xfs_remove(
2539 xfs_inode_t *dp,
2540 struct xfs_name *name,
2541 xfs_inode_t *ip)
2542{
2543 xfs_mount_t *mp = dp->i_mount;
2544 xfs_trans_t *tp = NULL;
2545 int is_dir = S_ISDIR(ip->i_d.di_mode);
2546 int error = 0;
2547 xfs_bmap_free_t free_list;
2548 xfs_fsblock_t first_block;
2549 int cancel_flags;
2550 int committed;
2551 int link_zero;
2552 uint resblks;
2553 uint log_count;
2554
2555 trace_xfs_remove(dp, name);
2556
2557 if (XFS_FORCED_SHUTDOWN(mp))
2558 return XFS_ERROR(EIO);
2559
2560 error = xfs_qm_dqattach(dp, 0);
2561 if (error)
2562 goto std_return;
2563
2564 error = xfs_qm_dqattach(ip, 0);
2565 if (error)
2566 goto std_return;
2567
2568 if (is_dir) {
2569 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
2570 log_count = XFS_DEFAULT_LOG_COUNT;
2571 } else {
2572 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
2573 log_count = XFS_REMOVE_LOG_COUNT;
2574 }
2575 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2576
2577 /*
2578 * We try to get the real space reservation first,
2579 * allowing for directory btree deletion(s) implying
2580 * possible bmap insert(s). If we can't get the space
2581 * reservation then we use 0 instead, and avoid the bmap
2582 * btree insert(s) in the directory code by, if the bmap
2583 * insert tries to happen, instead trimming the LAST
2584 * block from the directory.
2585 */
2586 resblks = XFS_REMOVE_SPACE_RES(mp);
3d3c8b52 2587 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_remove, resblks, 0);
c24b5dfa
DC
2588 if (error == ENOSPC) {
2589 resblks = 0;
3d3c8b52 2590 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_remove, 0, 0);
c24b5dfa
DC
2591 }
2592 if (error) {
2593 ASSERT(error != ENOSPC);
2594 cancel_flags = 0;
2595 goto out_trans_cancel;
2596 }
2597
2598 xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
2599
2600 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2601 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2602
2603 /*
2604 * If we're removing a directory perform some additional validation.
2605 */
27320369 2606 cancel_flags |= XFS_TRANS_ABORT;
c24b5dfa
DC
2607 if (is_dir) {
2608 ASSERT(ip->i_d.di_nlink >= 2);
2609 if (ip->i_d.di_nlink != 2) {
2610 error = XFS_ERROR(ENOTEMPTY);
2611 goto out_trans_cancel;
2612 }
2613 if (!xfs_dir_isempty(ip)) {
2614 error = XFS_ERROR(ENOTEMPTY);
2615 goto out_trans_cancel;
2616 }
c24b5dfa 2617
27320369 2618 /* Drop the link from ip's "..". */
c24b5dfa
DC
2619 error = xfs_droplink(tp, dp);
2620 if (error)
27320369 2621 goto out_trans_cancel;
c24b5dfa 2622
27320369 2623 /* Drop the "." link from ip to self. */
c24b5dfa
DC
2624 error = xfs_droplink(tp, ip);
2625 if (error)
27320369 2626 goto out_trans_cancel;
c24b5dfa
DC
2627 } else {
2628 /*
2629 * When removing a non-directory we need to log the parent
2630 * inode here. For a directory this is done implicitly
2631 * by the xfs_droplink call for the ".." entry.
2632 */
2633 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2634 }
27320369 2635 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
c24b5dfa 2636
27320369 2637 /* Drop the link from dp to ip. */
c24b5dfa
DC
2638 error = xfs_droplink(tp, ip);
2639 if (error)
27320369 2640 goto out_trans_cancel;
c24b5dfa 2641
27320369 2642 /* Determine if this is the last link while the inode is locked */
c24b5dfa
DC
2643 link_zero = (ip->i_d.di_nlink == 0);
2644
27320369
DC
2645 xfs_bmap_init(&free_list, &first_block);
2646 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
2647 &first_block, &free_list, resblks);
2648 if (error) {
2649 ASSERT(error != ENOENT);
2650 goto out_bmap_cancel;
2651 }
2652
c24b5dfa
DC
2653 /*
2654 * If this is a synchronous mount, make sure that the
2655 * remove transaction goes to disk before returning to
2656 * the user.
2657 */
2658 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
2659 xfs_trans_set_sync(tp);
2660
2661 error = xfs_bmap_finish(&tp, &free_list, &committed);
2662 if (error)
2663 goto out_bmap_cancel;
2664
2665 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2666 if (error)
2667 goto std_return;
2668
2669 /*
2670 * If we are using filestreams, kill the stream association.
2671 * If the file is still open it may get a new one but that
2672 * will get killed on last close in xfs_close() so we don't
2673 * have to worry about that.
2674 */
2675 if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
2676 xfs_filestream_deassociate(ip);
2677
2678 return 0;
2679
2680 out_bmap_cancel:
2681 xfs_bmap_cancel(&free_list);
c24b5dfa
DC
2682 out_trans_cancel:
2683 xfs_trans_cancel(tp, cancel_flags);
2684 std_return:
2685 return error;
2686}
2687
f6bba201
DC
2688/*
2689 * Enter all inodes for a rename transaction into a sorted array.
2690 */
2691STATIC void
2692xfs_sort_for_rename(
2693 xfs_inode_t *dp1, /* in: old (source) directory inode */
2694 xfs_inode_t *dp2, /* in: new (target) directory inode */
2695 xfs_inode_t *ip1, /* in: inode of old entry */
2696 xfs_inode_t *ip2, /* in: inode of new entry, if it
2697 already exists, NULL otherwise. */
2698 xfs_inode_t **i_tab,/* out: array of inode returned, sorted */
2699 int *num_inodes) /* out: number of inodes in array */
2700{
2701 xfs_inode_t *temp;
2702 int i, j;
2703
2704 /*
2705 * i_tab contains a list of pointers to inodes. We initialize
2706 * the table here & we'll sort it. We will then use it to
2707 * order the acquisition of the inode locks.
2708 *
2709 * Note that the table may contain duplicates. e.g., dp1 == dp2.
2710 */
2711 i_tab[0] = dp1;
2712 i_tab[1] = dp2;
2713 i_tab[2] = ip1;
2714 if (ip2) {
2715 *num_inodes = 4;
2716 i_tab[3] = ip2;
2717 } else {
2718 *num_inodes = 3;
2719 i_tab[3] = NULL;
2720 }
2721
2722 /*
2723 * Sort the elements via bubble sort. (Remember, there are at
2724 * most 4 elements to sort, so this is adequate.)
2725 */
2726 for (i = 0; i < *num_inodes; i++) {
2727 for (j = 1; j < *num_inodes; j++) {
2728 if (i_tab[j]->i_ino < i_tab[j-1]->i_ino) {
2729 temp = i_tab[j];
2730 i_tab[j] = i_tab[j-1];
2731 i_tab[j-1] = temp;
2732 }
2733 }
2734 }
2735}
2736
2737/*
2738 * xfs_rename
2739 */
2740int
2741xfs_rename(
2742 xfs_inode_t *src_dp,
2743 struct xfs_name *src_name,
2744 xfs_inode_t *src_ip,
2745 xfs_inode_t *target_dp,
2746 struct xfs_name *target_name,
2747 xfs_inode_t *target_ip)
2748{
2749 xfs_trans_t *tp = NULL;
2750 xfs_mount_t *mp = src_dp->i_mount;
2751 int new_parent; /* moving to a new dir */
2752 int src_is_directory; /* src_name is a directory */
2753 int error;
2754 xfs_bmap_free_t free_list;
2755 xfs_fsblock_t first_block;
2756 int cancel_flags;
2757 int committed;
2758 xfs_inode_t *inodes[4];
2759 int spaceres;
2760 int num_inodes;
2761
2762 trace_xfs_rename(src_dp, target_dp, src_name, target_name);
2763
2764 new_parent = (src_dp != target_dp);
2765 src_is_directory = S_ISDIR(src_ip->i_d.di_mode);
2766
2767 xfs_sort_for_rename(src_dp, target_dp, src_ip, target_ip,
2768 inodes, &num_inodes);
2769
2770 xfs_bmap_init(&free_list, &first_block);
2771 tp = xfs_trans_alloc(mp, XFS_TRANS_RENAME);
2772 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2773 spaceres = XFS_RENAME_SPACE_RES(mp, target_name->len);
3d3c8b52 2774 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_rename, spaceres, 0);
f6bba201
DC
2775 if (error == ENOSPC) {
2776 spaceres = 0;
3d3c8b52 2777 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_rename, 0, 0);
f6bba201
DC
2778 }
2779 if (error) {
2780 xfs_trans_cancel(tp, 0);
2781 goto std_return;
2782 }
2783
2784 /*
2785 * Attach the dquots to the inodes
2786 */
2787 error = xfs_qm_vop_rename_dqattach(inodes);
2788 if (error) {
2789 xfs_trans_cancel(tp, cancel_flags);
2790 goto std_return;
2791 }
2792
2793 /*
2794 * Lock all the participating inodes. Depending upon whether
2795 * the target_name exists in the target directory, and
2796 * whether the target directory is the same as the source
2797 * directory, we can lock from 2 to 4 inodes.
2798 */
2799 xfs_lock_inodes(inodes, num_inodes, XFS_ILOCK_EXCL);
2800
2801 /*
2802 * Join all the inodes to the transaction. From this point on,
2803 * we can rely on either trans_commit or trans_cancel to unlock
2804 * them.
2805 */
2806 xfs_trans_ijoin(tp, src_dp, XFS_ILOCK_EXCL);
2807 if (new_parent)
2808 xfs_trans_ijoin(tp, target_dp, XFS_ILOCK_EXCL);
2809 xfs_trans_ijoin(tp, src_ip, XFS_ILOCK_EXCL);
2810 if (target_ip)
2811 xfs_trans_ijoin(tp, target_ip, XFS_ILOCK_EXCL);
2812
2813 /*
2814 * If we are using project inheritance, we only allow renames
2815 * into our tree when the project IDs are the same; else the
2816 * tree quota mechanism would be circumvented.
2817 */
2818 if (unlikely((target_dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2819 (xfs_get_projid(target_dp) != xfs_get_projid(src_ip)))) {
2820 error = XFS_ERROR(EXDEV);
2821 goto error_return;
2822 }
2823
2824 /*
2825 * Set up the target.
2826 */
2827 if (target_ip == NULL) {
2828 /*
2829 * If there's no space reservation, check the entry will
2830 * fit before actually inserting it.
2831 */
2832 error = xfs_dir_canenter(tp, target_dp, target_name, spaceres);
2833 if (error)
2834 goto error_return;
2835 /*
2836 * If target does not exist and the rename crosses
2837 * directories, adjust the target directory link count
2838 * to account for the ".." reference from the new entry.
2839 */
2840 error = xfs_dir_createname(tp, target_dp, target_name,
2841 src_ip->i_ino, &first_block,
2842 &free_list, spaceres);
2843 if (error == ENOSPC)
2844 goto error_return;
2845 if (error)
2846 goto abort_return;
2847
2848 xfs_trans_ichgtime(tp, target_dp,
2849 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2850
2851 if (new_parent && src_is_directory) {
2852 error = xfs_bumplink(tp, target_dp);
2853 if (error)
2854 goto abort_return;
2855 }
2856 } else { /* target_ip != NULL */
2857 /*
2858 * If target exists and it's a directory, check that both
2859 * target and source are directories and that target can be
2860 * destroyed, or that neither is a directory.
2861 */
2862 if (S_ISDIR(target_ip->i_d.di_mode)) {
2863 /*
2864 * Make sure target dir is empty.
2865 */
2866 if (!(xfs_dir_isempty(target_ip)) ||
2867 (target_ip->i_d.di_nlink > 2)) {
2868 error = XFS_ERROR(EEXIST);
2869 goto error_return;
2870 }
2871 }
2872
2873 /*
2874 * Link the source inode under the target name.
2875 * If the source inode is a directory and we are moving
2876 * it across directories, its ".." entry will be
2877 * inconsistent until we replace that down below.
2878 *
2879 * In case there is already an entry with the same
2880 * name at the destination directory, remove it first.
2881 */
2882 error = xfs_dir_replace(tp, target_dp, target_name,
2883 src_ip->i_ino,
2884 &first_block, &free_list, spaceres);
2885 if (error)
2886 goto abort_return;
2887
2888 xfs_trans_ichgtime(tp, target_dp,
2889 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2890
2891 /*
2892 * Decrement the link count on the target since the target
2893 * dir no longer points to it.
2894 */
2895 error = xfs_droplink(tp, target_ip);
2896 if (error)
2897 goto abort_return;
2898
2899 if (src_is_directory) {
2900 /*
2901 * Drop the link from the old "." entry.
2902 */
2903 error = xfs_droplink(tp, target_ip);
2904 if (error)
2905 goto abort_return;
2906 }
2907 } /* target_ip != NULL */
2908
2909 /*
2910 * Remove the source.
2911 */
2912 if (new_parent && src_is_directory) {
2913 /*
2914 * Rewrite the ".." entry to point to the new
2915 * directory.
2916 */
2917 error = xfs_dir_replace(tp, src_ip, &xfs_name_dotdot,
2918 target_dp->i_ino,
2919 &first_block, &free_list, spaceres);
2920 ASSERT(error != EEXIST);
2921 if (error)
2922 goto abort_return;
2923 }
2924
2925 /*
2926 * We always want to hit the ctime on the source inode.
2927 *
2928 * This isn't strictly required by the standards since the source
2929 * inode isn't really being changed, but old unix file systems did
2930 * it and some incremental backup programs won't work without it.
2931 */
2932 xfs_trans_ichgtime(tp, src_ip, XFS_ICHGTIME_CHG);
2933 xfs_trans_log_inode(tp, src_ip, XFS_ILOG_CORE);
2934
2935 /*
2936 * Adjust the link count on src_dp. This is necessary when
2937 * renaming a directory, either within one parent when
2938 * the target existed, or across two parent directories.
2939 */
2940 if (src_is_directory && (new_parent || target_ip != NULL)) {
2941
2942 /*
2943 * Decrement link count on src_directory since the
2944 * entry that's moved no longer points to it.
2945 */
2946 error = xfs_droplink(tp, src_dp);
2947 if (error)
2948 goto abort_return;
2949 }
2950
2951 error = xfs_dir_removename(tp, src_dp, src_name, src_ip->i_ino,
2952 &first_block, &free_list, spaceres);
2953 if (error)
2954 goto abort_return;
2955
2956 xfs_trans_ichgtime(tp, src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2957 xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE);
2958 if (new_parent)
2959 xfs_trans_log_inode(tp, target_dp, XFS_ILOG_CORE);
2960
2961 /*
2962 * If this is a synchronous mount, make sure that the
2963 * rename 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
2970 error = xfs_bmap_finish(&tp, &free_list, &committed);
2971 if (error) {
2972 xfs_bmap_cancel(&free_list);
2973 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
2974 XFS_TRANS_ABORT));
2975 goto std_return;
2976 }
2977
2978 /*
2979 * trans_commit will unlock src_ip, target_ip & decrement
2980 * the vnode references.
2981 */
2982 return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2983
2984 abort_return:
2985 cancel_flags |= XFS_TRANS_ABORT;
2986 error_return:
2987 xfs_bmap_cancel(&free_list);
2988 xfs_trans_cancel(tp, cancel_flags);
2989 std_return:
2990 return error;
2991}
2992
5c4d97d0
DC
2993STATIC int
2994xfs_iflush_cluster(
2995 xfs_inode_t *ip,
2996 xfs_buf_t *bp)
1da177e4 2997{
5c4d97d0
DC
2998 xfs_mount_t *mp = ip->i_mount;
2999 struct xfs_perag *pag;
3000 unsigned long first_index, mask;
3001 unsigned long inodes_per_cluster;
3002 int ilist_size;
3003 xfs_inode_t **ilist;
3004 xfs_inode_t *iq;
3005 int nr_found;
3006 int clcount = 0;
3007 int bufwasdelwri;
1da177e4 3008 int i;
1da177e4 3009
5c4d97d0 3010 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
1da177e4 3011
5c4d97d0
DC
3012 inodes_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog;
3013 ilist_size = inodes_per_cluster * sizeof(xfs_inode_t *);
3014 ilist = kmem_alloc(ilist_size, KM_MAYFAIL|KM_NOFS);
3015 if (!ilist)
3016 goto out_put;
1da177e4 3017
5c4d97d0
DC
3018 mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
3019 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask;
3020 rcu_read_lock();
3021 /* really need a gang lookup range call here */
3022 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)ilist,
3023 first_index, inodes_per_cluster);
3024 if (nr_found == 0)
3025 goto out_free;
3026
3027 for (i = 0; i < nr_found; i++) {
3028 iq = ilist[i];
3029 if (iq == ip)
bad55843 3030 continue;
1a3e8f3d
DC
3031
3032 /*
3033 * because this is an RCU protected lookup, we could find a
3034 * recently freed or even reallocated inode during the lookup.
3035 * We need to check under the i_flags_lock for a valid inode
3036 * here. Skip it if it is not valid or the wrong inode.
3037 */
3038 spin_lock(&ip->i_flags_lock);
3039 if (!ip->i_ino ||
3040 (XFS_INO_TO_AGINO(mp, iq->i_ino) & mask) != first_index) {
3041 spin_unlock(&ip->i_flags_lock);
3042 continue;
3043 }
3044 spin_unlock(&ip->i_flags_lock);
3045
bad55843
DC
3046 /*
3047 * Do an un-protected check to see if the inode is dirty and
3048 * is a candidate for flushing. These checks will be repeated
3049 * later after the appropriate locks are acquired.
3050 */
33540408 3051 if (xfs_inode_clean(iq) && xfs_ipincount(iq) == 0)
bad55843 3052 continue;
bad55843
DC
3053
3054 /*
3055 * Try to get locks. If any are unavailable or it is pinned,
3056 * then this inode cannot be flushed and is skipped.
3057 */
3058
3059 if (!xfs_ilock_nowait(iq, XFS_ILOCK_SHARED))
3060 continue;
3061 if (!xfs_iflock_nowait(iq)) {
3062 xfs_iunlock(iq, XFS_ILOCK_SHARED);
3063 continue;
3064 }
3065 if (xfs_ipincount(iq)) {
3066 xfs_ifunlock(iq);
3067 xfs_iunlock(iq, XFS_ILOCK_SHARED);
3068 continue;
3069 }
3070
3071 /*
3072 * arriving here means that this inode can be flushed. First
3073 * re-check that it's dirty before flushing.
3074 */
33540408
DC
3075 if (!xfs_inode_clean(iq)) {
3076 int error;
bad55843
DC
3077 error = xfs_iflush_int(iq, bp);
3078 if (error) {
3079 xfs_iunlock(iq, XFS_ILOCK_SHARED);
3080 goto cluster_corrupt_out;
3081 }
3082 clcount++;
3083 } else {
3084 xfs_ifunlock(iq);
3085 }
3086 xfs_iunlock(iq, XFS_ILOCK_SHARED);
3087 }
3088
3089 if (clcount) {
3090 XFS_STATS_INC(xs_icluster_flushcnt);
3091 XFS_STATS_ADD(xs_icluster_flushinode, clcount);
3092 }
3093
3094out_free:
1a3e8f3d 3095 rcu_read_unlock();
f0e2d93c 3096 kmem_free(ilist);
44b56e0a
DC
3097out_put:
3098 xfs_perag_put(pag);
bad55843
DC
3099 return 0;
3100
3101
3102cluster_corrupt_out:
3103 /*
3104 * Corruption detected in the clustering loop. Invalidate the
3105 * inode buffer and shut down the filesystem.
3106 */
1a3e8f3d 3107 rcu_read_unlock();
bad55843 3108 /*
43ff2122 3109 * Clean up the buffer. If it was delwri, just release it --
bad55843
DC
3110 * brelse can handle it with no problems. If not, shut down the
3111 * filesystem before releasing the buffer.
3112 */
43ff2122 3113 bufwasdelwri = (bp->b_flags & _XBF_DELWRI_Q);
bad55843
DC
3114 if (bufwasdelwri)
3115 xfs_buf_relse(bp);
3116
3117 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
3118
3119 if (!bufwasdelwri) {
3120 /*
3121 * Just like incore_relse: if we have b_iodone functions,
3122 * mark the buffer as an error and call them. Otherwise
3123 * mark it as stale and brelse.
3124 */
cb669ca5 3125 if (bp->b_iodone) {
bad55843 3126 XFS_BUF_UNDONE(bp);
c867cb61 3127 xfs_buf_stale(bp);
5a52c2a5 3128 xfs_buf_ioerror(bp, EIO);
1a1a3e97 3129 xfs_buf_ioend(bp, 0);
bad55843 3130 } else {
c867cb61 3131 xfs_buf_stale(bp);
bad55843
DC
3132 xfs_buf_relse(bp);
3133 }
3134 }
3135
3136 /*
3137 * Unlocks the flush lock
3138 */
04913fdd 3139 xfs_iflush_abort(iq, false);
f0e2d93c 3140 kmem_free(ilist);
44b56e0a 3141 xfs_perag_put(pag);
bad55843
DC
3142 return XFS_ERROR(EFSCORRUPTED);
3143}
3144
1da177e4 3145/*
4c46819a
CH
3146 * Flush dirty inode metadata into the backing buffer.
3147 *
3148 * The caller must have the inode lock and the inode flush lock held. The
3149 * inode lock will still be held upon return to the caller, and the inode
3150 * flush lock will be released after the inode has reached the disk.
3151 *
3152 * The caller must write out the buffer returned in *bpp and release it.
1da177e4
LT
3153 */
3154int
3155xfs_iflush(
4c46819a
CH
3156 struct xfs_inode *ip,
3157 struct xfs_buf **bpp)
1da177e4 3158{
4c46819a
CH
3159 struct xfs_mount *mp = ip->i_mount;
3160 struct xfs_buf *bp;
3161 struct xfs_dinode *dip;
1da177e4 3162 int error;
1da177e4
LT
3163
3164 XFS_STATS_INC(xs_iflush_count);
3165
579aa9ca 3166 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
474fce06 3167 ASSERT(xfs_isiflocked(ip));
1da177e4 3168 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
8096b1eb 3169 ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
1da177e4 3170
4c46819a 3171 *bpp = NULL;
1da177e4 3172
1da177e4
LT
3173 xfs_iunpin_wait(ip);
3174
4b6a4688
DC
3175 /*
3176 * For stale inodes we cannot rely on the backing buffer remaining
3177 * stale in cache for the remaining life of the stale inode and so
475ee413 3178 * xfs_imap_to_bp() below may give us a buffer that no longer contains
4b6a4688
DC
3179 * inodes below. We have to check this after ensuring the inode is
3180 * unpinned so that it is safe to reclaim the stale inode after the
3181 * flush call.
3182 */
3183 if (xfs_iflags_test(ip, XFS_ISTALE)) {
3184 xfs_ifunlock(ip);
3185 return 0;
3186 }
3187
1da177e4
LT
3188 /*
3189 * This may have been unpinned because the filesystem is shutting
3190 * down forcibly. If that's the case we must not write this inode
32ce90a4
CH
3191 * to disk, because the log record didn't make it to disk.
3192 *
3193 * We also have to remove the log item from the AIL in this case,
3194 * as we wait for an empty AIL as part of the unmount process.
1da177e4
LT
3195 */
3196 if (XFS_FORCED_SHUTDOWN(mp)) {
32ce90a4
CH
3197 error = XFS_ERROR(EIO);
3198 goto abort_out;
1da177e4
LT
3199 }
3200
a3f74ffb
DC
3201 /*
3202 * Get the buffer containing the on-disk inode.
3203 */
475ee413
CH
3204 error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &bp, XBF_TRYLOCK,
3205 0);
a3f74ffb
DC
3206 if (error || !bp) {
3207 xfs_ifunlock(ip);
3208 return error;
3209 }
3210
1da177e4
LT
3211 /*
3212 * First flush out the inode that xfs_iflush was called with.
3213 */
3214 error = xfs_iflush_int(ip, bp);
bad55843 3215 if (error)
1da177e4 3216 goto corrupt_out;
1da177e4 3217
a3f74ffb
DC
3218 /*
3219 * If the buffer is pinned then push on the log now so we won't
3220 * get stuck waiting in the write for too long.
3221 */
811e64c7 3222 if (xfs_buf_ispinned(bp))
a14a348b 3223 xfs_log_force(mp, 0);
a3f74ffb 3224
1da177e4
LT
3225 /*
3226 * inode clustering:
3227 * see if other inodes can be gathered into this write
3228 */
bad55843
DC
3229 error = xfs_iflush_cluster(ip, bp);
3230 if (error)
3231 goto cluster_corrupt_out;
1da177e4 3232
4c46819a
CH
3233 *bpp = bp;
3234 return 0;
1da177e4
LT
3235
3236corrupt_out:
3237 xfs_buf_relse(bp);
7d04a335 3238 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1da177e4 3239cluster_corrupt_out:
32ce90a4
CH
3240 error = XFS_ERROR(EFSCORRUPTED);
3241abort_out:
1da177e4
LT
3242 /*
3243 * Unlocks the flush lock
3244 */
04913fdd 3245 xfs_iflush_abort(ip, false);
32ce90a4 3246 return error;
1da177e4
LT
3247}
3248
1da177e4
LT
3249STATIC int
3250xfs_iflush_int(
93848a99
CH
3251 struct xfs_inode *ip,
3252 struct xfs_buf *bp)
1da177e4 3253{
93848a99
CH
3254 struct xfs_inode_log_item *iip = ip->i_itemp;
3255 struct xfs_dinode *dip;
3256 struct xfs_mount *mp = ip->i_mount;
1da177e4 3257
579aa9ca 3258 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
474fce06 3259 ASSERT(xfs_isiflocked(ip));
1da177e4 3260 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
8096b1eb 3261 ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
93848a99 3262 ASSERT(iip != NULL && iip->ili_fields != 0);
1da177e4 3263
1da177e4 3264 /* set *dip = inode's place in the buffer */
92bfc6e7 3265 dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
1da177e4 3266
69ef921b 3267 if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
1da177e4 3268 mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) {
6a19d939
DC
3269 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3270 "%s: Bad inode %Lu magic number 0x%x, ptr 0x%p",
3271 __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
1da177e4
LT
3272 goto corrupt_out;
3273 }
3274 if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC,
3275 mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) {
6a19d939
DC
3276 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3277 "%s: Bad inode %Lu, ptr 0x%p, magic number 0x%x",
3278 __func__, ip->i_ino, ip, ip->i_d.di_magic);
1da177e4
LT
3279 goto corrupt_out;
3280 }
abbede1b 3281 if (S_ISREG(ip->i_d.di_mode)) {
1da177e4
LT
3282 if (XFS_TEST_ERROR(
3283 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
3284 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE),
3285 mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) {
6a19d939
DC
3286 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3287 "%s: Bad regular inode %Lu, ptr 0x%p",
3288 __func__, ip->i_ino, ip);
1da177e4
LT
3289 goto corrupt_out;
3290 }
abbede1b 3291 } else if (S_ISDIR(ip->i_d.di_mode)) {
1da177e4
LT
3292 if (XFS_TEST_ERROR(
3293 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
3294 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
3295 (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL),
3296 mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) {
6a19d939
DC
3297 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3298 "%s: Bad directory inode %Lu, ptr 0x%p",
3299 __func__, ip->i_ino, ip);
1da177e4
LT
3300 goto corrupt_out;
3301 }
3302 }
3303 if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents >
3304 ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5,
3305 XFS_RANDOM_IFLUSH_5)) {
6a19d939
DC
3306 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3307 "%s: detected corrupt incore inode %Lu, "
3308 "total extents = %d, nblocks = %Ld, ptr 0x%p",
3309 __func__, ip->i_ino,
1da177e4 3310 ip->i_d.di_nextents + ip->i_d.di_anextents,
6a19d939 3311 ip->i_d.di_nblocks, ip);
1da177e4
LT
3312 goto corrupt_out;
3313 }
3314 if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
3315 mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) {
6a19d939
DC
3316 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3317 "%s: bad inode %Lu, forkoff 0x%x, ptr 0x%p",
3318 __func__, ip->i_ino, ip->i_d.di_forkoff, ip);
1da177e4
LT
3319 goto corrupt_out;
3320 }
e60896d8 3321
1da177e4 3322 /*
e60896d8
DC
3323 * Inode item log recovery for v1/v2 inodes are dependent on the
3324 * di_flushiter count for correct sequencing. We bump the flush
3325 * iteration count so we can detect flushes which postdate a log record
3326 * during recovery. This is redundant as we now log every change and
3327 * hence this can't happen but we need to still do it to ensure
3328 * backwards compatibility with old kernels that predate logging all
3329 * inode changes.
1da177e4 3330 */
e60896d8
DC
3331 if (ip->i_d.di_version < 3)
3332 ip->i_d.di_flushiter++;
1da177e4
LT
3333
3334 /*
3335 * Copy the dirty parts of the inode into the on-disk
3336 * inode. We always copy out the core of the inode,
3337 * because if the inode is dirty at all the core must
3338 * be.
3339 */
81591fe2 3340 xfs_dinode_to_disk(dip, &ip->i_d);
1da177e4
LT
3341
3342 /* Wrap, we never let the log put out DI_MAX_FLUSH */
3343 if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
3344 ip->i_d.di_flushiter = 0;
3345
3346 /*
3347 * If this is really an old format inode and the superblock version
3348 * has not been updated to support only new format inodes, then
3349 * convert back to the old inode format. If the superblock version
3350 * has been updated, then make the conversion permanent.
3351 */
51ce16d5
CH
3352 ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb));
3353 if (ip->i_d.di_version == 1) {
62118709 3354 if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
1da177e4
LT
3355 /*
3356 * Convert it back.
3357 */
3358 ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
81591fe2 3359 dip->di_onlink = cpu_to_be16(ip->i_d.di_nlink);
1da177e4
LT
3360 } else {
3361 /*
3362 * The superblock version has already been bumped,
3363 * so just make the conversion to the new inode
3364 * format permanent.
3365 */
51ce16d5
CH
3366 ip->i_d.di_version = 2;
3367 dip->di_version = 2;
1da177e4 3368 ip->i_d.di_onlink = 0;
81591fe2 3369 dip->di_onlink = 0;
1da177e4 3370 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
81591fe2
CH
3371 memset(&(dip->di_pad[0]), 0,
3372 sizeof(dip->di_pad));
6743099c 3373 ASSERT(xfs_get_projid(ip) == 0);
1da177e4
LT
3374 }
3375 }
3376
e4ac967b
DC
3377 xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK, bp);
3378 if (XFS_IFORK_Q(ip))
3379 xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK, bp);
1da177e4
LT
3380 xfs_inobp_check(mp, bp);
3381
3382 /*
f5d8d5c4
CH
3383 * We've recorded everything logged in the inode, so we'd like to clear
3384 * the ili_fields bits so we don't log and flush things unnecessarily.
3385 * However, we can't stop logging all this information until the data
3386 * we've copied into the disk buffer is written to disk. If we did we
3387 * might overwrite the copy of the inode in the log with all the data
3388 * after re-logging only part of it, and in the face of a crash we
3389 * wouldn't have all the data we need to recover.
1da177e4 3390 *
f5d8d5c4
CH
3391 * What we do is move the bits to the ili_last_fields field. When
3392 * logging the inode, these bits are moved back to the ili_fields field.
3393 * In the xfs_iflush_done() routine we clear ili_last_fields, since we
3394 * know that the information those bits represent is permanently on
3395 * disk. As long as the flush completes before the inode is logged
3396 * again, then both ili_fields and ili_last_fields will be cleared.
1da177e4 3397 *
f5d8d5c4
CH
3398 * We can play with the ili_fields bits here, because the inode lock
3399 * must be held exclusively in order to set bits there and the flush
3400 * lock protects the ili_last_fields bits. Set ili_logged so the flush
3401 * done routine can tell whether or not to look in the AIL. Also, store
3402 * the current LSN of the inode so that we can tell whether the item has
3403 * moved in the AIL from xfs_iflush_done(). In order to read the lsn we
3404 * need the AIL lock, because it is a 64 bit value that cannot be read
3405 * atomically.
1da177e4 3406 */
93848a99
CH
3407 iip->ili_last_fields = iip->ili_fields;
3408 iip->ili_fields = 0;
3409 iip->ili_logged = 1;
1da177e4 3410
93848a99
CH
3411 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
3412 &iip->ili_item.li_lsn);
1da177e4 3413
93848a99
CH
3414 /*
3415 * Attach the function xfs_iflush_done to the inode's
3416 * buffer. This will remove the inode from the AIL
3417 * and unlock the inode's flush lock when the inode is
3418 * completely written to disk.
3419 */
3420 xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item);
1da177e4 3421
93848a99
CH
3422 /* update the lsn in the on disk inode if required */
3423 if (ip->i_d.di_version == 3)
3424 dip->di_lsn = cpu_to_be64(iip->ili_item.li_lsn);
3425
3426 /* generate the checksum. */
3427 xfs_dinode_calc_crc(mp, dip);
1da177e4 3428
93848a99
CH
3429 ASSERT(bp->b_fspriv != NULL);
3430 ASSERT(bp->b_iodone != NULL);
1da177e4
LT
3431 return 0;
3432
3433corrupt_out:
3434 return XFS_ERROR(EFSCORRUPTED);
3435}