blob: c79b875f0354059d1eff80415e6f0b9e96503858 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Robert P. J. Day40ebd812007-11-23 16:30:51 +110018#include <linux/log2.h>
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_fs.h"
Dave Chinner70a9883c2013-10-23 10:36:05 +110022#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110023#include "xfs_format.h"
24#include "xfs_log_format.h"
25#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110026#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_sb.h"
28#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_mount.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110030#include "xfs_inode.h"
Dave Chinner57062782013-10-15 09:17:51 +110031#include "xfs_da_format.h"
Dave Chinnerc24b5df2013-08-12 20:49:45 +100032#include "xfs_da_btree.h"
Dave Chinnerc24b5df2013-08-12 20:49:45 +100033#include "xfs_dir2.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_attr_sf.h"
Dave Chinnerc24b5df2013-08-12 20:49:45 +100035#include "xfs_attr.h"
Dave Chinner239880e2013-10-23 10:50:10 +110036#include "xfs_trans_space.h"
37#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "xfs_buf_item.h"
Nathan Scotta844f452005-11-02 14:38:42 +110039#include "xfs_inode_item.h"
Nathan Scotta844f452005-11-02 14:38:42 +110040#include "xfs_ialloc.h"
41#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100042#include "xfs_bmap_util.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include "xfs_quota.h"
David Chinner2a82b8b2007-07-11 11:09:12 +100045#include "xfs_filestream.h"
Christoph Hellwig93848a92013-04-03 16:11:17 +110046#include "xfs_cksum.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000047#include "xfs_trace.h"
Dave Chinner33479e02012-10-08 21:56:11 +110048#include "xfs_icache.h"
Dave Chinnerc24b5df2013-08-12 20:49:45 +100049#include "xfs_symlink.h"
Dave Chinner239880e2013-10-23 10:50:10 +110050#include "xfs_trans_priv.h"
51#include "xfs_log.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110052#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054kmem_zone_t *xfs_inode_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/*
Christoph Hellwig8f04c472011-07-08 14:34:34 +020057 * Used in xfs_itruncate_extents(). This is the maximum number of extents
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 * 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 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Dave Chinner2a0ec1d2012-04-23 15:59:02 +100064/*
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
Dave Chinnerfa96aca2012-10-08 21:56:10 +110078/*
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
Dave Chinner742ae1e2013-04-30 21:39:34 +1000289#if defined(DEBUG) || defined(XFS_WARN)
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100290int
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
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000312#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
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100494void
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510STATIC uint
511_xfs_dic2xflags(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 __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;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100537 if (di_flags & XFS_DIFLAG_EXTSIZE)
538 flags |= XFS_XFLAG_EXTSIZE;
539 if (di_flags & XFS_DIFLAG_EXTSZINHERIT)
540 flags |= XFS_XFLAG_EXTSZINHERIT;
Barry Naujokd3446ea2006-06-09 14:54:19 +1000541 if (di_flags & XFS_DIFLAG_NODEFRAG)
542 flags |= XFS_XFLAG_NODEFRAG;
David Chinner2a82b8b2007-07-11 11:09:12 +1000543 if (di_flags & XFS_DIFLAG_FILESTREAM)
544 flags |= XFS_XFLAG_FILESTREAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
546
547 return flags;
548}
549
550uint
551xfs_ip2xflags(
552 xfs_inode_t *ip)
553{
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000554 xfs_icdinode_t *dic = &ip->i_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Nathan Scotta916e2b2006-06-09 17:12:17 +1000556 return _xfs_dic2xflags(dic->di_flags) |
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100557 (XFS_IFORK_Q(ip) ? XFS_XFLAG_HASATTR : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
560uint
561xfs_dic2xflags(
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100562 xfs_dinode_t *dip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100564 return _xfs_dic2xflags(be16_to_cpu(dip->di_flags)) |
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100565 (XFS_DFORK_Q(dip) ? XFS_XFLAG_HASATTR : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568/*
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000569 * 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
611/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 * 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()
Carlos Maiolinocd856db2012-10-20 11:08:19 -0300618 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 *
Carlos Maiolinocd856db2012-10-20 11:08:19 -0300623 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 * 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.
David Chinnerb11f94d2007-07-11 11:09:33 +1000636 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 */
642int
643xfs_ialloc(
644 xfs_trans_t *tp,
645 xfs_inode_t *pip,
Al Viro576b1d62011-07-26 02:50:15 -0400646 umode_t mode,
Nathan Scott31b084a2005-05-05 13:25:00 -0700647 xfs_nlink_t nlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 xfs_dev_t rdev,
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000649 prid_t prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 int okalloc,
651 xfs_buf_t **ialloc_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 xfs_inode_t **ipp)
653{
Christoph Hellwig93848a92013-04-03 16:11:17 +1100654 struct xfs_mount *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 xfs_ino_t ino;
656 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 uint flags;
658 int error;
Christoph Hellwigdff35fd2008-08-13 16:44:15 +1000659 timespec_t tv;
David Chinnerbf904242008-10-30 17:36:14 +1100660 int filestreams = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 /*
663 * Call the space management code to pick
664 * the on-disk inode to be allocated.
665 */
David Chinnerb11f94d2007-07-11 11:09:33 +1000666 error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc,
Christoph Hellwig08358902012-07-04 10:54:47 -0400667 ialloc_context, &ino);
David Chinnerbf904242008-10-30 17:36:14 +1100668 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return error;
Christoph Hellwig08358902012-07-04 10:54:47 -0400670 if (*ialloc_context || ino == NULLFSINO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 *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 */
Christoph Hellwig93848a92013-04-03 16:11:17 +1100681 error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE,
Christoph Hellwigec3ba852011-02-13 13:26:42 +0000682 XFS_ILOCK_EXCL, &ip);
David Chinnerbf904242008-10-30 17:36:14 +1100683 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 ASSERT(ip != NULL);
686
Al Viro576b1d62011-07-26 02:50:15 -0400687 ip->i_d.di_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 ip->i_d.di_onlink = 0;
689 ip->i_d.di_nlink = nlink;
690 ASSERT(ip->i_d.di_nlink == nlink);
Dwight Engen7aab1b22013-08-15 14:08:01 -0400691 ip->i_d.di_uid = xfs_kuid_to_uid(current_fsuid());
692 ip->i_d.di_gid = xfs_kgid_to_gid(current_fsgid());
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000693 xfs_set_projid(ip, prid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 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 */
Christoph Hellwig93848a92013-04-03 16:11:17 +1100702 if (xfs_sb_version_hasnlink(&mp->m_sb) &&
Christoph Hellwig51ce16d2008-11-28 14:23:39 +1100703 ip->i_d.di_version == 1) {
704 ip->i_d.di_version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 /*
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 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +1100714 if ((prid != 0) && (ip->i_d.di_version == 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 xfs_bump_ino_vers2(tp, ip);
716
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000717 if (pip && XFS_INHERIT_GID(pip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 ip->i_d.di_gid = pip->i_d.di_gid;
Al Viroabbede12011-07-26 02:31:30 -0400719 if ((pip->i_d.di_mode & S_ISGID) && S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 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) &&
Dwight Engen7aab1b22013-08-15 14:08:01 -0400731 (!in_group_p(xfs_gid_to_kgid(ip->i_d.di_gid)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 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);
Christoph Hellwigdff35fd2008-08-13 16:44:15 +1000738
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 /*
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;
Christoph Hellwig93848a92013-04-03 16:11:17 +1100752
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 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:
David Chinnerbf904242008-10-30 17:36:14 +1100777 /*
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;
David Chinner2a82b8b2007-07-11 11:09:12 +1000783 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 case S_IFDIR:
David Chinnerb11f94d2007-07-11 11:09:33 +1000785 if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) {
Nathan Scott365ca832005-06-21 15:39:12 +1000786 uint di_flags = 0;
787
Al Viroabbede12011-07-26 02:31:30 -0400788 if (S_ISDIR(mode)) {
Nathan Scott365ca832005-06-21 15:39:12 +1000789 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
790 di_flags |= XFS_DIFLAG_RTINHERIT;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100791 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 }
Al Viroabbede12011-07-26 02:31:30 -0400795 } else if (S_ISREG(mode)) {
Christoph Hellwig613d7042007-10-11 17:44:08 +1000796 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
Nathan Scott365ca832005-06-21 15:39:12 +1000797 di_flags |= XFS_DIFLAG_REALTIME;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100798 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
803 if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
804 xfs_inherit_noatime)
Nathan Scott365ca832005-06-21 15:39:12 +1000805 di_flags |= XFS_DIFLAG_NOATIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
807 xfs_inherit_nodump)
Nathan Scott365ca832005-06-21 15:39:12 +1000808 di_flags |= XFS_DIFLAG_NODUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
810 xfs_inherit_sync)
Nathan Scott365ca832005-06-21 15:39:12 +1000811 di_flags |= XFS_DIFLAG_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
813 xfs_inherit_nosymlinks)
Nathan Scott365ca832005-06-21 15:39:12 +1000814 di_flags |= XFS_DIFLAG_NOSYMLINKS;
815 if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
816 di_flags |= XFS_DIFLAG_PROJINHERIT;
Barry Naujokd3446ea2006-06-09 14:54:19 +1000817 if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
818 xfs_inherit_nodefrag)
819 di_flags |= XFS_DIFLAG_NODEFRAG;
David Chinner2a82b8b2007-07-11 11:09:12 +1000820 if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
821 di_flags |= XFS_DIFLAG_FILESTREAM;
Nathan Scott365ca832005-06-21 15:39:12 +1000822 ip->i_d.di_flags |= di_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
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 */
Christoph Hellwigddc34152011-09-19 15:00:54 +0000843 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 xfs_trans_log_inode(tp, ip, flags);
845
Nathan Scottb83bd132006-06-09 16:48:30 +1000846 /* now that we have an i_mode we can setup inode ops and unlock */
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000847 xfs_setup_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
David Chinnerbf904242008-10-30 17:36:14 +1100849 /* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 *ipp = ip;
859 return 0;
860}
861
Dave Chinnere546cb72013-08-12 20:49:47 +1000862/*
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;
Dave Chinnere546cb72013-08-12 20:49:47 +1000893 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) {
Jie Liu3d3c8b52013-08-12 20:49:59 +1000938 struct xfs_trans_res tres;
939
Dave Chinnere546cb72013-08-12 20:49:47 +1000940 /*
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 */
Jie Liu3d3c8b52013-08-12 20:49:59 +1000952 tres.tr_logres = xfs_trans_get_log_res(tp);
953 tres.tr_logcount = xfs_trans_get_log_count(tp);
Dave Chinnere546cb72013-08-12 20:49:47 +1000954
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);
Jie Liu3d3c8b52013-08-12 20:49:59 +1000996 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
997 code = xfs_trans_reserve(tp, &tres, 0, 0);
998
Dave Chinnere546cb72013-08-12 20:49:47 +1000999 /*
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
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001141int
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;
Jie Liu3d3c8b52013-08-12 20:49:59 +10001163 struct xfs_trans_res tres;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001164 uint resblks;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001165
1166 trace_xfs_create(dp, name);
1167
1168 if (XFS_FORCED_SHUTDOWN(mp))
1169 return XFS_ERROR(EIO);
1170
Zhi Yong Wu163467d2013-12-18 08:22:39 +08001171 prid = xfs_get_initial_prid(dp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001172
1173 /*
1174 * Make sure that we have allocated dquot(s) on disk.
1175 */
Dwight Engen7aab1b22013-08-15 14:08:01 -04001176 error = xfs_qm_vop_dqalloc(dp, xfs_kuid_to_uid(current_fsuid()),
1177 xfs_kgid_to_gid(current_fsgid()), prid,
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001178 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);
Jie Liu3d3c8b52013-08-12 20:49:59 +10001186 tres.tr_logres = M_RES(mp)->tr_mkdir.tr_logres;
1187 tres.tr_logcount = XFS_MKDIR_LOG_COUNT;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001188 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
1189 } else {
1190 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
Jie Liu3d3c8b52013-08-12 20:49:59 +10001191 tres.tr_logres = M_RES(mp)->tr_create.tr_logres;
1192 tres.tr_logcount = XFS_CREATE_LOG_COUNT;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001193 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 */
Jie Liu3d3c8b52013-08-12 20:49:59 +10001204 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
1205 error = xfs_trans_reserve(tp, &tres, resblks, 0);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001206 if (error == ENOSPC) {
1207 /* flush outstanding delalloc blocks and retry */
1208 xfs_flush_inodes(mp);
Jie Liu3d3c8b52013-08-12 20:49:59 +10001209 error = xfs_trans_reserve(tp, &tres, resblks, 0);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001210 }
1211 if (error == ENOSPC) {
1212 /* No space at all so try a "no-allocation" reservation */
1213 resblks = 0;
Jie Liu3d3c8b52013-08-12 20:49:59 +10001214 error = xfs_trans_reserve(tp, &tres, 0, 0);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001215 }
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
1335int
1336xfs_link(
1337 xfs_inode_t *tdp,
1338 xfs_inode_t *sip,
1339 struct xfs_name *target_name)
1340{
1341 xfs_mount_t *mp = tdp->i_mount;
1342 xfs_trans_t *tp;
1343 int error;
1344 xfs_bmap_free_t free_list;
1345 xfs_fsblock_t first_block;
1346 int cancel_flags;
1347 int committed;
1348 int resblks;
1349
1350 trace_xfs_link(tdp, target_name);
1351
1352 ASSERT(!S_ISDIR(sip->i_d.di_mode));
1353
1354 if (XFS_FORCED_SHUTDOWN(mp))
1355 return XFS_ERROR(EIO);
1356
1357 error = xfs_qm_dqattach(sip, 0);
1358 if (error)
1359 goto std_return;
1360
1361 error = xfs_qm_dqattach(tdp, 0);
1362 if (error)
1363 goto std_return;
1364
1365 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
1366 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1367 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
Jie Liu3d3c8b52013-08-12 20:49:59 +10001368 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_link, resblks, 0);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001369 if (error == ENOSPC) {
1370 resblks = 0;
Jie Liu3d3c8b52013-08-12 20:49:59 +10001371 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_link, 0, 0);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001372 }
1373 if (error) {
1374 cancel_flags = 0;
1375 goto error_return;
1376 }
1377
1378 xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
1379
1380 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
1381 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
1382
1383 /*
1384 * If we are using project inheritance, we only allow hard link
1385 * creation in our tree when the project IDs are the same; else
1386 * the tree quota mechanism could be circumvented.
1387 */
1388 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
1389 (xfs_get_projid(tdp) != xfs_get_projid(sip)))) {
1390 error = XFS_ERROR(EXDEV);
1391 goto error_return;
1392 }
1393
1394 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
1395 if (error)
1396 goto error_return;
1397
1398 xfs_bmap_init(&free_list, &first_block);
1399
1400 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
1401 &first_block, &free_list, resblks);
1402 if (error)
1403 goto abort_return;
1404 xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1405 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
1406
1407 error = xfs_bumplink(tp, sip);
1408 if (error)
1409 goto abort_return;
1410
1411 /*
1412 * If this is a synchronous mount, make sure that the
1413 * link transaction goes to disk before returning to
1414 * the user.
1415 */
1416 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1417 xfs_trans_set_sync(tp);
1418 }
1419
1420 error = xfs_bmap_finish (&tp, &free_list, &committed);
1421 if (error) {
1422 xfs_bmap_cancel(&free_list);
1423 goto abort_return;
1424 }
1425
1426 return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1427
1428 abort_return:
1429 cancel_flags |= XFS_TRANS_ABORT;
1430 error_return:
1431 xfs_trans_cancel(tp, cancel_flags);
1432 std_return:
1433 return error;
1434}
1435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436/*
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001437 * Free up the underlying blocks past new_size. The new size must be smaller
1438 * than the current size. This routine can be used both for the attribute and
1439 * data fork, and does not modify the inode size, which is left to the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 *
David Chinnerf6485052008-04-17 16:50:04 +10001441 * The transaction passed to this routine must have made a permanent log
1442 * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the
1443 * given transaction and start new ones, so make sure everything involved in
1444 * the transaction is tidy before calling here. Some transaction will be
1445 * returned to the caller to be committed. The incoming transaction must
1446 * already include the inode, and both inode locks must be held exclusively.
1447 * The inode must also be "held" within the transaction. On return the inode
1448 * will be "held" within the returned transaction. This routine does NOT
1449 * require any disk space to be reserved for it within the transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 *
David Chinnerf6485052008-04-17 16:50:04 +10001451 * If we get an error, we must return with the inode locked and linked into the
1452 * current transaction. This keeps things simple for the higher level code,
1453 * because it always knows that the inode is locked and held in the transaction
1454 * that returns to it whether errors occur or not. We don't mark the inode
1455 * dirty on error so that transactions can be easily aborted if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 */
1457int
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001458xfs_itruncate_extents(
1459 struct xfs_trans **tpp,
1460 struct xfs_inode *ip,
1461 int whichfork,
1462 xfs_fsize_t new_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001464 struct xfs_mount *mp = ip->i_mount;
1465 struct xfs_trans *tp = *tpp;
1466 struct xfs_trans *ntp;
1467 xfs_bmap_free_t free_list;
1468 xfs_fsblock_t first_block;
1469 xfs_fileoff_t first_unmap_block;
1470 xfs_fileoff_t last_block;
1471 xfs_filblks_t unmap_len;
1472 int committed;
1473 int error = 0;
1474 int done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Christoph Hellwig0b561852012-07-04 11:13:31 -04001476 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1477 ASSERT(!atomic_read(&VFS_I(ip)->i_count) ||
1478 xfs_isilocked(ip, XFS_IOLOCK_EXCL));
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00001479 ASSERT(new_size <= XFS_ISIZE(ip));
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001480 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 ASSERT(ip->i_itemp != NULL);
Christoph Hellwig898621d2010-06-24 11:36:58 +10001482 ASSERT(ip->i_itemp->ili_lock_flags == 0);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001483 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Christoph Hellwig673e8e52011-12-18 20:00:04 +00001485 trace_xfs_itruncate_extents_start(ip, new_size);
1486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 /*
1488 * Since it is possible for space to become allocated beyond
1489 * the end of the file (in a crash where the space is allocated
1490 * but the inode size is not yet updated), simply remove any
1491 * blocks which show up between the new EOF and the maximum
1492 * possible file size. If the first block to be removed is
1493 * beyond the maximum file size (ie it is the same as last_block),
1494 * then there is nothing to do.
1495 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001496 first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
Dave Chinner32972382012-06-08 15:44:54 +10001497 last_block = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001498 if (first_unmap_block == last_block)
1499 return 0;
1500
1501 ASSERT(first_unmap_block < last_block);
1502 unmap_len = last_block - first_unmap_block + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 while (!done) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001504 xfs_bmap_init(&free_list, &first_block);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001505 error = xfs_bunmapi(tp, ip,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001506 first_unmap_block, unmap_len,
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001507 xfs_bmapi_aflag(whichfork),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 XFS_ITRUNC_MAX_EXTENTS,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001509 &first_block, &free_list,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10001510 &done);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001511 if (error)
1512 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
1514 /*
1515 * Duplicate the transaction that has the permanent
1516 * reservation and commit the old transaction.
1517 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001518 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig898621d2010-06-24 11:36:58 +10001519 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001520 xfs_trans_ijoin(tp, ip, 0);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001521 if (error)
1522 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
1524 if (committed) {
1525 /*
David Chinnerf6485052008-04-17 16:50:04 +10001526 * Mark the inode dirty so it will be logged and
David Chinnere5720ee2008-04-10 12:21:18 +10001527 * moved forward in the log as part of every commit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001529 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 }
David Chinnerf6485052008-04-17 16:50:04 +10001531
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001532 ntp = xfs_trans_dup(tp);
1533 error = xfs_trans_commit(tp, 0);
1534 tp = ntp;
David Chinnere5720ee2008-04-10 12:21:18 +10001535
Christoph Hellwigddc34152011-09-19 15:00:54 +00001536 xfs_trans_ijoin(tp, ip, 0);
David Chinnerf6485052008-04-17 16:50:04 +10001537
Dave Chinnercc09c0d2008-11-17 17:37:10 +11001538 if (error)
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001539 goto out;
1540
Dave Chinnercc09c0d2008-11-17 17:37:10 +11001541 /*
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001542 * Transaction commit worked ok so we can drop the extra ticket
Dave Chinnercc09c0d2008-11-17 17:37:10 +11001543 * reference that we gained in xfs_trans_dup()
1544 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001545 xfs_log_ticket_put(tp->t_ticket);
Jie Liu3d3c8b52013-08-12 20:49:59 +10001546 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0);
David Chinnerf6485052008-04-17 16:50:04 +10001547 if (error)
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001548 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 }
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001550
Christoph Hellwig673e8e52011-12-18 20:00:04 +00001551 /*
1552 * Always re-log the inode so that our permanent transaction can keep
1553 * on rolling it forward in the log.
1554 */
1555 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1556
1557 trace_xfs_itruncate_extents_end(ip, new_size);
1558
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001559out:
1560 *tpp = tp;
1561 return error;
1562out_bmap_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 /*
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001564 * If the bunmapi call encounters an error, return to the caller where
1565 * the transaction can be properly aborted. We just need to make sure
1566 * we're not holding any resources that we were not when we came in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001568 xfs_bmap_cancel(&free_list);
1569 goto out;
1570}
1571
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001572int
1573xfs_release(
1574 xfs_inode_t *ip)
1575{
1576 xfs_mount_t *mp = ip->i_mount;
1577 int error;
1578
1579 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
1580 return 0;
1581
1582 /* If this is a read-only mount, don't do this (would generate I/O) */
1583 if (mp->m_flags & XFS_MOUNT_RDONLY)
1584 return 0;
1585
1586 if (!XFS_FORCED_SHUTDOWN(mp)) {
1587 int truncated;
1588
1589 /*
1590 * If we are using filestreams, and we have an unlinked
1591 * file that we are processing the last close on, then nothing
1592 * will be able to reopen and write to this file. Purge this
1593 * inode from the filestreams cache so that it doesn't delay
1594 * teardown of the inode.
1595 */
1596 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
1597 xfs_filestream_deassociate(ip);
1598
1599 /*
1600 * If we previously truncated this file and removed old data
1601 * in the process, we want to initiate "early" writeout on
1602 * the last close. This is an attempt to combat the notorious
1603 * NULL files problem which is particularly noticeable from a
1604 * truncate down, buffered (re-)write (delalloc), followed by
1605 * a crash. What we are effectively doing here is
1606 * significantly reducing the time window where we'd otherwise
1607 * be exposed to that problem.
1608 */
1609 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
1610 if (truncated) {
1611 xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE);
1612 if (VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0) {
1613 error = -filemap_flush(VFS_I(ip)->i_mapping);
1614 if (error)
1615 return error;
1616 }
1617 }
1618 }
1619
1620 if (ip->i_d.di_nlink == 0)
1621 return 0;
1622
1623 if (xfs_can_free_eofblocks(ip, false)) {
1624
1625 /*
1626 * If we can't get the iolock just skip truncating the blocks
1627 * past EOF because we could deadlock with the mmap_sem
1628 * otherwise. We'll get another chance to drop them once the
1629 * last reference to the inode is dropped, so we'll never leak
1630 * blocks permanently.
1631 *
1632 * Further, check if the inode is being opened, written and
1633 * closed frequently and we have delayed allocation blocks
1634 * outstanding (e.g. streaming writes from the NFS server),
1635 * truncating the blocks past EOF will cause fragmentation to
1636 * occur.
1637 *
1638 * In this case don't do the truncation, either, but we have to
1639 * be careful how we detect this case. Blocks beyond EOF show
1640 * up as i_delayed_blks even when the inode is clean, so we
1641 * need to truncate them away first before checking for a dirty
1642 * release. Hence on the first dirty close we will still remove
1643 * the speculative allocation, but after that we will leave it
1644 * in place.
1645 */
1646 if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE))
1647 return 0;
1648
1649 error = xfs_free_eofblocks(mp, ip, true);
1650 if (error && error != EAGAIN)
1651 return error;
1652
1653 /* delalloc blocks after truncation means it really is dirty */
1654 if (ip->i_delayed_blks)
1655 xfs_iflags_set(ip, XFS_IDIRTY_RELEASE);
1656 }
1657 return 0;
1658}
1659
1660/*
Brian Fosterf7be2d72013-09-20 11:06:10 -04001661 * xfs_inactive_truncate
1662 *
1663 * Called to perform a truncate when an inode becomes unlinked.
1664 */
1665STATIC int
1666xfs_inactive_truncate(
1667 struct xfs_inode *ip)
1668{
1669 struct xfs_mount *mp = ip->i_mount;
1670 struct xfs_trans *tp;
1671 int error;
1672
1673 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1674 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0);
1675 if (error) {
1676 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1677 xfs_trans_cancel(tp, 0);
1678 return error;
1679 }
1680
1681 xfs_ilock(ip, XFS_ILOCK_EXCL);
1682 xfs_trans_ijoin(tp, ip, 0);
1683
1684 /*
1685 * Log the inode size first to prevent stale data exposure in the event
1686 * of a system crash before the truncate completes. See the related
1687 * comment in xfs_setattr_size() for details.
1688 */
1689 ip->i_d.di_size = 0;
1690 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1691
1692 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
1693 if (error)
1694 goto error_trans_cancel;
1695
1696 ASSERT(ip->i_d.di_nextents == 0);
1697
1698 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1699 if (error)
1700 goto error_unlock;
1701
1702 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1703 return 0;
1704
1705error_trans_cancel:
1706 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1707error_unlock:
1708 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1709 return error;
1710}
1711
1712/*
Brian Foster88877d22013-09-20 11:06:11 -04001713 * xfs_inactive_ifree()
1714 *
1715 * Perform the inode free when an inode is unlinked.
1716 */
1717STATIC int
1718xfs_inactive_ifree(
1719 struct xfs_inode *ip)
1720{
1721 xfs_bmap_free_t free_list;
1722 xfs_fsblock_t first_block;
1723 int committed;
1724 struct xfs_mount *mp = ip->i_mount;
1725 struct xfs_trans *tp;
1726 int error;
1727
1728 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1729 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_ifree, 0, 0);
1730 if (error) {
1731 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1732 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
1733 return error;
1734 }
1735
1736 xfs_ilock(ip, XFS_ILOCK_EXCL);
1737 xfs_trans_ijoin(tp, ip, 0);
1738
1739 xfs_bmap_init(&free_list, &first_block);
1740 error = xfs_ifree(tp, ip, &free_list);
1741 if (error) {
1742 /*
1743 * If we fail to free the inode, shut down. The cancel
1744 * might do that, we need to make sure. Otherwise the
1745 * inode might be lost for a long time or forever.
1746 */
1747 if (!XFS_FORCED_SHUTDOWN(mp)) {
1748 xfs_notice(mp, "%s: xfs_ifree returned error %d",
1749 __func__, error);
1750 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1751 }
1752 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1753 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1754 return error;
1755 }
1756
1757 /*
1758 * Credit the quota account(s). The inode is gone.
1759 */
1760 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1761
1762 /*
1763 * Just ignore errors at this point. There is nothing we can
1764 * do except to try to keep going. Make sure it's not a silent
1765 * error.
1766 */
1767 error = xfs_bmap_finish(&tp, &free_list, &committed);
1768 if (error)
1769 xfs_notice(mp, "%s: xfs_bmap_finish returned error %d",
1770 __func__, error);
1771 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1772 if (error)
1773 xfs_notice(mp, "%s: xfs_trans_commit returned error %d",
1774 __func__, error);
1775
1776 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1777 return 0;
1778}
1779
1780/*
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001781 * xfs_inactive
1782 *
1783 * This is called when the vnode reference count for the vnode
1784 * goes to zero. If the file has been unlinked, then it must
1785 * now be truncated. Also, we clear all of the read-ahead state
1786 * kept for the inode here since the file is now closed.
1787 */
Brian Foster74564fb2013-09-20 11:06:12 -04001788void
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001789xfs_inactive(
1790 xfs_inode_t *ip)
1791{
Jie Liu3d3c8b52013-08-12 20:49:59 +10001792 struct xfs_mount *mp;
Jie Liu3d3c8b52013-08-12 20:49:59 +10001793 int error;
1794 int truncate = 0;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001795
1796 /*
1797 * If the inode is already free, then there can be nothing
1798 * to clean up here.
1799 */
Ben Myersd9487092013-09-10 18:11:22 -05001800 if (ip->i_d.di_mode == 0) {
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001801 ASSERT(ip->i_df.if_real_bytes == 0);
1802 ASSERT(ip->i_df.if_broot_bytes == 0);
Brian Foster74564fb2013-09-20 11:06:12 -04001803 return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001804 }
1805
1806 mp = ip->i_mount;
1807
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001808 /* If this is a read-only mount, don't do this (would generate I/O) */
1809 if (mp->m_flags & XFS_MOUNT_RDONLY)
Brian Foster74564fb2013-09-20 11:06:12 -04001810 return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001811
1812 if (ip->i_d.di_nlink != 0) {
1813 /*
1814 * force is true because we are evicting an inode from the
1815 * cache. Post-eof blocks must be freed, lest we end up with
1816 * broken free space accounting.
1817 */
Brian Foster74564fb2013-09-20 11:06:12 -04001818 if (xfs_can_free_eofblocks(ip, true))
1819 xfs_free_eofblocks(mp, ip, false);
1820
1821 return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001822 }
1823
1824 if (S_ISREG(ip->i_d.di_mode) &&
1825 (ip->i_d.di_size != 0 || XFS_ISIZE(ip) != 0 ||
1826 ip->i_d.di_nextents > 0 || ip->i_delayed_blks > 0))
1827 truncate = 1;
1828
1829 error = xfs_qm_dqattach(ip, 0);
1830 if (error)
Brian Foster74564fb2013-09-20 11:06:12 -04001831 return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001832
Brian Fosterf7be2d72013-09-20 11:06:10 -04001833 if (S_ISLNK(ip->i_d.di_mode))
Brian Foster36b21dd2013-09-20 11:06:09 -04001834 error = xfs_inactive_symlink(ip);
Brian Fosterf7be2d72013-09-20 11:06:10 -04001835 else if (truncate)
1836 error = xfs_inactive_truncate(ip);
1837 if (error)
Brian Foster74564fb2013-09-20 11:06:12 -04001838 return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001839
1840 /*
1841 * If there are attributes associated with the file then blow them away
1842 * now. The code calls a routine that recursively deconstructs the
1843 * attribute fork. We need to just commit the current transaction
1844 * because we can't use it for xfs_attr_inactive().
1845 */
1846 if (ip->i_d.di_anextents > 0) {
1847 ASSERT(ip->i_d.di_forkoff != 0);
1848
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001849 error = xfs_attr_inactive(ip);
1850 if (error)
Brian Foster74564fb2013-09-20 11:06:12 -04001851 return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001852 }
1853
1854 if (ip->i_afp)
1855 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1856
1857 ASSERT(ip->i_d.di_anextents == 0);
1858
1859 /*
1860 * Free the inode.
1861 */
Brian Foster88877d22013-09-20 11:06:11 -04001862 error = xfs_inactive_ifree(ip);
1863 if (error)
Brian Foster74564fb2013-09-20 11:06:12 -04001864 return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001865
1866 /*
1867 * Release the dquots held by inode, if any.
1868 */
1869 xfs_qm_dqdetach(ip);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001870}
1871
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872/*
1873 * This is called when the inode's link count goes to 0.
1874 * We place the on-disk inode on a list in the AGI. It
1875 * will be pulled from this list when the inode is freed.
1876 */
1877int
1878xfs_iunlink(
1879 xfs_trans_t *tp,
1880 xfs_inode_t *ip)
1881{
1882 xfs_mount_t *mp;
1883 xfs_agi_t *agi;
1884 xfs_dinode_t *dip;
1885 xfs_buf_t *agibp;
1886 xfs_buf_t *ibp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 xfs_agino_t agino;
1888 short bucket_index;
1889 int offset;
1890 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
1892 ASSERT(ip->i_d.di_nlink == 0);
1893 ASSERT(ip->i_d.di_mode != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
1895 mp = tp->t_mountp;
1896
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 /*
1898 * Get the agi buffer first. It ensures lock ordering
1899 * on the list.
1900 */
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001901 error = xfs_read_agi(mp, tp, XFS_INO_TO_AGNO(mp, ip->i_ino), &agibp);
Vlad Apostolov859d7182007-10-11 17:44:18 +10001902 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 agi = XFS_BUF_TO_AGI(agibp);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001905
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 /*
1907 * Get the index into the agi hash table for the
1908 * list this inode will go on.
1909 */
1910 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
1911 ASSERT(agino != 0);
1912 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
1913 ASSERT(agi->agi_unlinked[bucket_index]);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001914 ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001916 if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 /*
1918 * There is already another inode in the bucket we need
1919 * to add ourselves to. Add us at the front of the list.
1920 * Here we put the head pointer into our next pointer,
1921 * and then we fall through to point the head at us.
1922 */
Christoph Hellwig475ee412012-07-03 12:21:22 -04001923 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
1924 0, 0);
Vlad Apostolovc319b582007-11-23 16:27:51 +11001925 if (error)
1926 return error;
1927
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001928 ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 dip->di_next_unlinked = agi->agi_unlinked[bucket_index];
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11001930 offset = ip->i_imap.im_boffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 offsetof(xfs_dinode_t, di_next_unlinked);
Dave Chinner0a32c262013-06-05 12:09:08 +10001932
1933 /* need to recalc the inode CRC if appropriate */
1934 xfs_dinode_calc_crc(mp, dip);
1935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 xfs_trans_inode_buf(tp, ibp);
1937 xfs_trans_log_buf(tp, ibp, offset,
1938 (offset + sizeof(xfs_agino_t) - 1));
1939 xfs_inobp_check(mp, ibp);
1940 }
1941
1942 /*
1943 * Point the bucket head pointer at the inode being inserted.
1944 */
1945 ASSERT(agino != 0);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001946 agi->agi_unlinked[bucket_index] = cpu_to_be32(agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 offset = offsetof(xfs_agi_t, agi_unlinked) +
1948 (sizeof(xfs_agino_t) * bucket_index);
1949 xfs_trans_log_buf(tp, agibp, offset,
1950 (offset + sizeof(xfs_agino_t) - 1));
1951 return 0;
1952}
1953
1954/*
1955 * Pull the on-disk inode from the AGI unlinked list.
1956 */
1957STATIC int
1958xfs_iunlink_remove(
1959 xfs_trans_t *tp,
1960 xfs_inode_t *ip)
1961{
1962 xfs_ino_t next_ino;
1963 xfs_mount_t *mp;
1964 xfs_agi_t *agi;
1965 xfs_dinode_t *dip;
1966 xfs_buf_t *agibp;
1967 xfs_buf_t *ibp;
1968 xfs_agnumber_t agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 xfs_agino_t agino;
1970 xfs_agino_t next_agino;
1971 xfs_buf_t *last_ibp;
Nathan Scott6fdf8cc2006-06-28 10:13:52 +10001972 xfs_dinode_t *last_dip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 short bucket_index;
Nathan Scott6fdf8cc2006-06-28 10:13:52 +10001974 int offset, last_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
1980 /*
1981 * Get the agi buffer first. It ensures lock ordering
1982 * on the list.
1983 */
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001984 error = xfs_read_agi(mp, tp, agno, &agibp);
1985 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 return error;
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001987
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 agi = XFS_BUF_TO_AGI(agibp);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 /*
1991 * Get the index into the agi hash table for the
1992 * list this inode will go on.
1993 */
1994 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
1995 ASSERT(agino != 0);
1996 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001997 ASSERT(agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 ASSERT(agi->agi_unlinked[bucket_index]);
1999
Christoph Hellwig16259e72005-11-02 15:11:25 +11002000 if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 /*
Christoph Hellwig475ee412012-07-03 12:21:22 -04002002 * We're at the head of the list. Get the inode's on-disk
2003 * buffer to see if there is anyone after us on the list.
2004 * Only modify our next pointer if it is not already NULLAGINO.
2005 * This saves us the overhead of dealing with the buffer when
2006 * there is no need to change it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 */
Christoph Hellwig475ee412012-07-03 12:21:22 -04002008 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
2009 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 if (error) {
Christoph Hellwig475ee412012-07-03 12:21:22 -04002011 xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.",
Dave Chinner0b932cc2011-03-07 10:08:35 +11002012 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 return error;
2014 }
Christoph Hellwig347d1c02007-08-28 13:57:51 +10002015 next_agino = be32_to_cpu(dip->di_next_unlinked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 ASSERT(next_agino != 0);
2017 if (next_agino != NULLAGINO) {
Christoph Hellwig347d1c02007-08-28 13:57:51 +10002018 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11002019 offset = ip->i_imap.im_boffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 offsetof(xfs_dinode_t, di_next_unlinked);
Dave Chinner0a32c262013-06-05 12:09:08 +10002021
2022 /* need to recalc the inode CRC if appropriate */
2023 xfs_dinode_calc_crc(mp, dip);
2024
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 xfs_trans_inode_buf(tp, ibp);
2026 xfs_trans_log_buf(tp, ibp, offset,
2027 (offset + sizeof(xfs_agino_t) - 1));
2028 xfs_inobp_check(mp, ibp);
2029 } else {
2030 xfs_trans_brelse(tp, ibp);
2031 }
2032 /*
2033 * Point the bucket head pointer at the next inode.
2034 */
2035 ASSERT(next_agino != 0);
2036 ASSERT(next_agino != agino);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002037 agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 offset = offsetof(xfs_agi_t, agi_unlinked) +
2039 (sizeof(xfs_agino_t) * bucket_index);
2040 xfs_trans_log_buf(tp, agibp, offset,
2041 (offset + sizeof(xfs_agino_t) - 1));
2042 } else {
2043 /*
2044 * We need to search the list for the inode being freed.
2045 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11002046 next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 last_ibp = NULL;
2048 while (next_agino != agino) {
Christoph Hellwig129dbc92012-07-03 12:21:51 -04002049 struct xfs_imap imap;
2050
2051 if (last_ibp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 xfs_trans_brelse(tp, last_ibp);
Christoph Hellwig129dbc92012-07-03 12:21:51 -04002053
2054 imap.im_blkno = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino);
Christoph Hellwig129dbc92012-07-03 12:21:51 -04002056
2057 error = xfs_imap(mp, tp, next_ino, &imap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11002059 xfs_warn(mp,
Christoph Hellwig129dbc92012-07-03 12:21:51 -04002060 "%s: xfs_imap returned error %d.",
2061 __func__, error);
2062 return error;
2063 }
2064
2065 error = xfs_imap_to_bp(mp, tp, &imap, &last_dip,
2066 &last_ibp, 0, 0);
2067 if (error) {
2068 xfs_warn(mp,
2069 "%s: xfs_imap_to_bp returned error %d.",
Dave Chinner0b932cc2011-03-07 10:08:35 +11002070 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 return error;
2072 }
Christoph Hellwig129dbc92012-07-03 12:21:51 -04002073
2074 last_offset = imap.im_boffset;
Christoph Hellwig347d1c02007-08-28 13:57:51 +10002075 next_agino = be32_to_cpu(last_dip->di_next_unlinked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 ASSERT(next_agino != NULLAGINO);
2077 ASSERT(next_agino != 0);
2078 }
Christoph Hellwig475ee412012-07-03 12:21:22 -04002079
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 /*
Christoph Hellwig475ee412012-07-03 12:21:22 -04002081 * Now last_ibp points to the buffer previous to us on the
2082 * unlinked list. Pull us from the list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 */
Christoph Hellwig475ee412012-07-03 12:21:22 -04002084 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
2085 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 if (error) {
Christoph Hellwig475ee412012-07-03 12:21:22 -04002087 xfs_warn(mp, "%s: xfs_imap_to_bp(2) returned error %d.",
Dave Chinner0b932cc2011-03-07 10:08:35 +11002088 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 return error;
2090 }
Christoph Hellwig347d1c02007-08-28 13:57:51 +10002091 next_agino = be32_to_cpu(dip->di_next_unlinked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 ASSERT(next_agino != 0);
2093 ASSERT(next_agino != agino);
2094 if (next_agino != NULLAGINO) {
Christoph Hellwig347d1c02007-08-28 13:57:51 +10002095 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11002096 offset = ip->i_imap.im_boffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 offsetof(xfs_dinode_t, di_next_unlinked);
Dave Chinner0a32c262013-06-05 12:09:08 +10002098
2099 /* need to recalc the inode CRC if appropriate */
2100 xfs_dinode_calc_crc(mp, dip);
2101
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 xfs_trans_inode_buf(tp, ibp);
2103 xfs_trans_log_buf(tp, ibp, offset,
2104 (offset + sizeof(xfs_agino_t) - 1));
2105 xfs_inobp_check(mp, ibp);
2106 } else {
2107 xfs_trans_brelse(tp, ibp);
2108 }
2109 /*
2110 * Point the previous inode on the list to the next inode.
2111 */
Christoph Hellwig347d1c02007-08-28 13:57:51 +10002112 last_dip->di_next_unlinked = cpu_to_be32(next_agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 ASSERT(next_agino != 0);
2114 offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked);
Dave Chinner0a32c262013-06-05 12:09:08 +10002115
2116 /* need to recalc the inode CRC if appropriate */
2117 xfs_dinode_calc_crc(mp, last_dip);
2118
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 xfs_trans_inode_buf(tp, last_ibp);
2120 xfs_trans_log_buf(tp, last_ibp, offset,
2121 (offset + sizeof(xfs_agino_t) - 1));
2122 xfs_inobp_check(mp, last_ibp);
2123 }
2124 return 0;
2125}
2126
Dave Chinner5b3eed72010-08-24 11:42:41 +10002127/*
Zhi Yong Wu0b8182d2013-08-12 03:14:59 +00002128 * A big issue when freeing the inode cluster is that we _cannot_ skip any
Dave Chinner5b3eed72010-08-24 11:42:41 +10002129 * inodes that are in memory - they all must be marked stale and attached to
2130 * the cluster buffer.
2131 */
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00002132STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133xfs_ifree_cluster(
2134 xfs_inode_t *free_ip,
2135 xfs_trans_t *tp,
2136 xfs_ino_t inum)
2137{
2138 xfs_mount_t *mp = free_ip->i_mount;
2139 int blks_per_cluster;
2140 int nbufs;
2141 int ninodes;
Dave Chinner5b257b42010-06-03 16:22:29 +10002142 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 xfs_daddr_t blkno;
2144 xfs_buf_t *bp;
Dave Chinner5b257b42010-06-03 16:22:29 +10002145 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 xfs_inode_log_item_t *iip;
2147 xfs_log_item_t *lip;
Dave Chinner5017e972010-01-11 11:47:40 +00002148 struct xfs_perag *pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
Dave Chinner5017e972010-01-11 11:47:40 +00002150 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
2152 blks_per_cluster = 1;
2153 ninodes = mp->m_sb.sb_inopblock;
2154 nbufs = XFS_IALLOC_BLOCKS(mp);
2155 } else {
2156 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
2157 mp->m_sb.sb_blocksize;
2158 ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
2159 nbufs = XFS_IALLOC_BLOCKS(mp) / blks_per_cluster;
2160 }
2161
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 for (j = 0; j < nbufs; j++, inum += ninodes) {
2163 blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
2164 XFS_INO_TO_AGBNO(mp, inum));
2165
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 /*
Dave Chinner5b257b42010-06-03 16:22:29 +10002167 * We obtain and lock the backing buffer first in the process
2168 * here, as we have to ensure that any dirty inode that we
2169 * can't get the flush lock on is attached to the buffer.
2170 * If we scan the in-memory inodes first, then buffer IO can
2171 * complete before we get a lock on it, and hence we may fail
2172 * to mark all the active inodes on the buffer stale.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 */
Dave Chinner5b257b42010-06-03 16:22:29 +10002174 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
Dave Chinnerb6aff292012-11-02 11:38:42 +11002175 mp->m_bsize * blks_per_cluster,
2176 XBF_UNMAPPED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00002178 if (!bp)
2179 return ENOMEM;
Dave Chinnerb0f539d2012-11-14 17:53:49 +11002180
2181 /*
2182 * This buffer may not have been correctly initialised as we
2183 * didn't read it from disk. That's not important because we are
2184 * only using to mark the buffer as stale in the log, and to
2185 * attach stale cached inodes on it. That means it will never be
2186 * dispatched for IO. If it is, we want to know about it, and we
2187 * want it to fail. We can acheive this by adding a write
2188 * verifier to the buffer.
2189 */
Dave Chinner1813dd62012-11-14 17:54:40 +11002190 bp->b_ops = &xfs_inode_buf_ops;
Dave Chinnerb0f539d2012-11-14 17:53:49 +11002191
Dave Chinner5b257b42010-06-03 16:22:29 +10002192 /*
2193 * Walk the inodes already attached to the buffer and mark them
2194 * stale. These will all have the flush locks held, so an
Dave Chinner5b3eed72010-08-24 11:42:41 +10002195 * in-memory inode walk can't lock them. By marking them all
2196 * stale first, we will not attempt to lock them in the loop
2197 * below as the XFS_ISTALE flag will be set.
Dave Chinner5b257b42010-06-03 16:22:29 +10002198 */
Christoph Hellwigadadbee2011-07-13 13:43:49 +02002199 lip = bp->b_fspriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 while (lip) {
2201 if (lip->li_type == XFS_LI_INODE) {
2202 iip = (xfs_inode_log_item_t *)lip;
2203 ASSERT(iip->ili_logged == 1);
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10002204 lip->li_cb = xfs_istale_done;
David Chinner7b2e2a32008-10-30 17:39:12 +11002205 xfs_trans_ail_copy_lsn(mp->m_ail,
2206 &iip->ili_flush_lsn,
2207 &iip->ili_item.li_lsn);
David Chinnere5ffd2b2006-11-21 18:55:33 +11002208 xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 }
2210 lip = lip->li_bio_list;
2211 }
2212
Dave Chinner5b3eed72010-08-24 11:42:41 +10002213
Dave Chinner5b257b42010-06-03 16:22:29 +10002214 /*
2215 * For each inode in memory attempt to add it to the inode
2216 * buffer and set it up for being staled on buffer IO
2217 * completion. This is safe as we've locked out tail pushing
2218 * and flushing by locking the buffer.
2219 *
2220 * We have already marked every inode that was part of a
2221 * transaction stale above, which means there is no point in
2222 * even trying to lock them.
2223 */
2224 for (i = 0; i < ninodes; i++) {
Dave Chinner5b3eed72010-08-24 11:42:41 +10002225retry:
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002226 rcu_read_lock();
Dave Chinner5b257b42010-06-03 16:22:29 +10002227 ip = radix_tree_lookup(&pag->pag_ici_root,
2228 XFS_INO_TO_AGINO(mp, (inum + i)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002230 /* Inode not in memory, nothing to do */
2231 if (!ip) {
2232 rcu_read_unlock();
Dave Chinner5b257b42010-06-03 16:22:29 +10002233 continue;
2234 }
2235
Dave Chinner5b3eed72010-08-24 11:42:41 +10002236 /*
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002237 * because this is an RCU protected lookup, we could
2238 * find a recently freed or even reallocated inode
2239 * during the lookup. We need to check under the
2240 * i_flags_lock for a valid inode here. Skip it if it
2241 * is not valid, the wrong inode or stale.
2242 */
2243 spin_lock(&ip->i_flags_lock);
2244 if (ip->i_ino != inum + i ||
2245 __xfs_iflags_test(ip, XFS_ISTALE)) {
2246 spin_unlock(&ip->i_flags_lock);
2247 rcu_read_unlock();
2248 continue;
2249 }
2250 spin_unlock(&ip->i_flags_lock);
2251
2252 /*
Dave Chinner5b3eed72010-08-24 11:42:41 +10002253 * Don't try to lock/unlock the current inode, but we
2254 * _cannot_ skip the other inodes that we did not find
2255 * in the list attached to the buffer and are not
2256 * already marked stale. If we can't lock it, back off
2257 * and retry.
2258 */
Dave Chinner5b257b42010-06-03 16:22:29 +10002259 if (ip != free_ip &&
2260 !xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002261 rcu_read_unlock();
Dave Chinner5b3eed72010-08-24 11:42:41 +10002262 delay(1);
2263 goto retry;
Dave Chinner5b257b42010-06-03 16:22:29 +10002264 }
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002265 rcu_read_unlock();
Dave Chinner5b257b42010-06-03 16:22:29 +10002266
Dave Chinner5b3eed72010-08-24 11:42:41 +10002267 xfs_iflock(ip);
Dave Chinner5b257b42010-06-03 16:22:29 +10002268 xfs_iflags_set(ip, XFS_ISTALE);
Dave Chinner5b257b42010-06-03 16:22:29 +10002269
Dave Chinner5b3eed72010-08-24 11:42:41 +10002270 /*
2271 * we don't need to attach clean inodes or those only
2272 * with unlogged changes (which we throw away, anyway).
2273 */
Dave Chinner5b257b42010-06-03 16:22:29 +10002274 iip = ip->i_itemp;
Dave Chinner5b3eed72010-08-24 11:42:41 +10002275 if (!iip || xfs_inode_clean(ip)) {
Dave Chinner5b257b42010-06-03 16:22:29 +10002276 ASSERT(ip != free_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 xfs_ifunlock(ip);
2278 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2279 continue;
2280 }
2281
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002282 iip->ili_last_fields = iip->ili_fields;
2283 iip->ili_fields = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 iip->ili_logged = 1;
David Chinner7b2e2a32008-10-30 17:39:12 +11002285 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
2286 &iip->ili_item.li_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10002288 xfs_buf_attach_iodone(bp, xfs_istale_done,
2289 &iip->ili_item);
Dave Chinner5b257b42010-06-03 16:22:29 +10002290
2291 if (ip != free_ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 }
2294
Dave Chinner5b3eed72010-08-24 11:42:41 +10002295 xfs_trans_stale_inode_buf(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 xfs_trans_binval(tp, bp);
2297 }
2298
Dave Chinner5017e972010-01-11 11:47:40 +00002299 xfs_perag_put(pag);
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00002300 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301}
2302
2303/*
2304 * This is called to return an inode to the inode free list.
2305 * The inode should already be truncated to 0 length and have
2306 * no pages associated with it. This routine also assumes that
2307 * the inode is already a part of the transaction.
2308 *
2309 * The on-disk copy of the inode will have been added to the list
2310 * of unlinked inodes in the AGI. We need to remove the inode from
2311 * that list atomically with respect to freeing it here.
2312 */
2313int
2314xfs_ifree(
2315 xfs_trans_t *tp,
2316 xfs_inode_t *ip,
2317 xfs_bmap_free_t *flist)
2318{
2319 int error;
2320 int delete;
2321 xfs_ino_t first_ino;
2322
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002323 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 ASSERT(ip->i_d.di_nlink == 0);
2325 ASSERT(ip->i_d.di_nextents == 0);
2326 ASSERT(ip->i_d.di_anextents == 0);
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00002327 ASSERT(ip->i_d.di_size == 0 || !S_ISREG(ip->i_d.di_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 ASSERT(ip->i_d.di_nblocks == 0);
2329
2330 /*
2331 * Pull the on-disk inode from the AGI unlinked list.
2332 */
2333 error = xfs_iunlink_remove(tp, ip);
Dave Chinner1baaed82013-06-27 16:04:50 +10002334 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
2337 error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino);
Dave Chinner1baaed82013-06-27 16:04:50 +10002338 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 return error;
Dave Chinner1baaed82013-06-27 16:04:50 +10002340
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 ip->i_d.di_mode = 0; /* mark incore inode as free */
2342 ip->i_d.di_flags = 0;
2343 ip->i_d.di_dmevmask = 0;
2344 ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
2346 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
2347 /*
2348 * Bump the generation count so no one will be confused
2349 * by reincarnations of this inode.
2350 */
2351 ip->i_d.di_gen++;
2352 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2353
Dave Chinner1baaed82013-06-27 16:04:50 +10002354 if (delete)
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00002355 error = xfs_ifree_cluster(ip, tp, first_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00002357 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358}
2359
2360/*
Christoph Hellwig60ec67832010-02-17 19:43:56 +00002361 * This is called to unpin an inode. The caller must have the inode locked
2362 * in at least shared mode so that the buffer cannot be subsequently pinned
2363 * once someone is waiting for it to be unpinned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 */
Christoph Hellwig60ec67832010-02-17 19:43:56 +00002365static void
Christoph Hellwigf392e632011-12-18 20:00:10 +00002366xfs_iunpin(
Christoph Hellwig60ec67832010-02-17 19:43:56 +00002367 struct xfs_inode *ip)
David Chinnera3f74ff2008-03-06 13:43:42 +11002368{
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002369 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
David Chinnera3f74ff2008-03-06 13:43:42 +11002370
Dave Chinner4aaf15d2010-03-08 11:24:07 +11002371 trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
2372
David Chinnera3f74ff2008-03-06 13:43:42 +11002373 /* Give the log a push to start the unpinning I/O */
Christoph Hellwig60ec67832010-02-17 19:43:56 +00002374 xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002375
David Chinnera3f74ff2008-03-06 13:43:42 +11002376}
2377
Christoph Hellwigf392e632011-12-18 20:00:10 +00002378static void
2379__xfs_iunpin_wait(
2380 struct xfs_inode *ip)
2381{
2382 wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
2383 DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
2384
2385 xfs_iunpin(ip);
2386
2387 do {
2388 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2389 if (xfs_ipincount(ip))
2390 io_schedule();
2391 } while (xfs_ipincount(ip));
2392 finish_wait(wq, &wait.wait);
2393}
2394
Dave Chinner777df5a2010-02-06 12:37:26 +11002395void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396xfs_iunpin_wait(
Christoph Hellwig60ec67832010-02-17 19:43:56 +00002397 struct xfs_inode *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398{
Christoph Hellwigf392e632011-12-18 20:00:10 +00002399 if (xfs_ipincount(ip))
2400 __xfs_iunpin_wait(ip);
David Chinnera3f74ff2008-03-06 13:43:42 +11002401}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
Dave Chinner27320362013-10-29 22:11:44 +11002403/*
2404 * Removing an inode from the namespace involves removing the directory entry
2405 * and dropping the link count on the inode. Removing the directory entry can
2406 * result in locking an AGF (directory blocks were freed) and removing a link
2407 * count can result in placing the inode on an unlinked list which results in
2408 * locking an AGI.
2409 *
2410 * The big problem here is that we have an ordering constraint on AGF and AGI
2411 * locking - inode allocation locks the AGI, then can allocate a new extent for
2412 * new inodes, locking the AGF after the AGI. Similarly, freeing the inode
2413 * removes the inode from the unlinked list, requiring that we lock the AGI
2414 * first, and then freeing the inode can result in an inode chunk being freed
2415 * and hence freeing disk space requiring that we lock an AGF.
2416 *
2417 * Hence the ordering that is imposed by other parts of the code is AGI before
2418 * AGF. This means we cannot remove the directory entry before we drop the inode
2419 * reference count and put it on the unlinked list as this results in a lock
2420 * order of AGF then AGI, and this can deadlock against inode allocation and
2421 * freeing. Therefore we must drop the link counts before we remove the
2422 * directory entry.
2423 *
2424 * This is still safe from a transactional point of view - it is not until we
2425 * get to xfs_bmap_finish() that we have the possibility of multiple
2426 * transactions in this operation. Hence as long as we remove the directory
2427 * entry and drop the link count in the first transaction of the remove
2428 * operation, there are no transactional constraints on the ordering here.
2429 */
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002430int
2431xfs_remove(
2432 xfs_inode_t *dp,
2433 struct xfs_name *name,
2434 xfs_inode_t *ip)
2435{
2436 xfs_mount_t *mp = dp->i_mount;
2437 xfs_trans_t *tp = NULL;
2438 int is_dir = S_ISDIR(ip->i_d.di_mode);
2439 int error = 0;
2440 xfs_bmap_free_t free_list;
2441 xfs_fsblock_t first_block;
2442 int cancel_flags;
2443 int committed;
2444 int link_zero;
2445 uint resblks;
2446 uint log_count;
2447
2448 trace_xfs_remove(dp, name);
2449
2450 if (XFS_FORCED_SHUTDOWN(mp))
2451 return XFS_ERROR(EIO);
2452
2453 error = xfs_qm_dqattach(dp, 0);
2454 if (error)
2455 goto std_return;
2456
2457 error = xfs_qm_dqattach(ip, 0);
2458 if (error)
2459 goto std_return;
2460
2461 if (is_dir) {
2462 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
2463 log_count = XFS_DEFAULT_LOG_COUNT;
2464 } else {
2465 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
2466 log_count = XFS_REMOVE_LOG_COUNT;
2467 }
2468 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2469
2470 /*
2471 * We try to get the real space reservation first,
2472 * allowing for directory btree deletion(s) implying
2473 * possible bmap insert(s). If we can't get the space
2474 * reservation then we use 0 instead, and avoid the bmap
2475 * btree insert(s) in the directory code by, if the bmap
2476 * insert tries to happen, instead trimming the LAST
2477 * block from the directory.
2478 */
2479 resblks = XFS_REMOVE_SPACE_RES(mp);
Jie Liu3d3c8b52013-08-12 20:49:59 +10002480 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_remove, resblks, 0);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002481 if (error == ENOSPC) {
2482 resblks = 0;
Jie Liu3d3c8b52013-08-12 20:49:59 +10002483 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_remove, 0, 0);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002484 }
2485 if (error) {
2486 ASSERT(error != ENOSPC);
2487 cancel_flags = 0;
2488 goto out_trans_cancel;
2489 }
2490
2491 xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
2492
2493 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2494 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2495
2496 /*
2497 * If we're removing a directory perform some additional validation.
2498 */
Dave Chinner27320362013-10-29 22:11:44 +11002499 cancel_flags |= XFS_TRANS_ABORT;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002500 if (is_dir) {
2501 ASSERT(ip->i_d.di_nlink >= 2);
2502 if (ip->i_d.di_nlink != 2) {
2503 error = XFS_ERROR(ENOTEMPTY);
2504 goto out_trans_cancel;
2505 }
2506 if (!xfs_dir_isempty(ip)) {
2507 error = XFS_ERROR(ENOTEMPTY);
2508 goto out_trans_cancel;
2509 }
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002510
Dave Chinner27320362013-10-29 22:11:44 +11002511 /* Drop the link from ip's "..". */
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002512 error = xfs_droplink(tp, dp);
2513 if (error)
Dave Chinner27320362013-10-29 22:11:44 +11002514 goto out_trans_cancel;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002515
Dave Chinner27320362013-10-29 22:11:44 +11002516 /* Drop the "." link from ip to self. */
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002517 error = xfs_droplink(tp, ip);
2518 if (error)
Dave Chinner27320362013-10-29 22:11:44 +11002519 goto out_trans_cancel;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002520 } else {
2521 /*
2522 * When removing a non-directory we need to log the parent
2523 * inode here. For a directory this is done implicitly
2524 * by the xfs_droplink call for the ".." entry.
2525 */
2526 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2527 }
Dave Chinner27320362013-10-29 22:11:44 +11002528 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002529
Dave Chinner27320362013-10-29 22:11:44 +11002530 /* Drop the link from dp to ip. */
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002531 error = xfs_droplink(tp, ip);
2532 if (error)
Dave Chinner27320362013-10-29 22:11:44 +11002533 goto out_trans_cancel;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002534
Dave Chinner27320362013-10-29 22:11:44 +11002535 /* Determine if this is the last link while the inode is locked */
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002536 link_zero = (ip->i_d.di_nlink == 0);
2537
Dave Chinner27320362013-10-29 22:11:44 +11002538 xfs_bmap_init(&free_list, &first_block);
2539 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
2540 &first_block, &free_list, resblks);
2541 if (error) {
2542 ASSERT(error != ENOENT);
2543 goto out_bmap_cancel;
2544 }
2545
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002546 /*
2547 * If this is a synchronous mount, make sure that the
2548 * remove transaction goes to disk before returning to
2549 * the user.
2550 */
2551 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
2552 xfs_trans_set_sync(tp);
2553
2554 error = xfs_bmap_finish(&tp, &free_list, &committed);
2555 if (error)
2556 goto out_bmap_cancel;
2557
2558 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2559 if (error)
2560 goto std_return;
2561
2562 /*
2563 * If we are using filestreams, kill the stream association.
2564 * If the file is still open it may get a new one but that
2565 * will get killed on last close in xfs_close() so we don't
2566 * have to worry about that.
2567 */
2568 if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
2569 xfs_filestream_deassociate(ip);
2570
2571 return 0;
2572
2573 out_bmap_cancel:
2574 xfs_bmap_cancel(&free_list);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002575 out_trans_cancel:
2576 xfs_trans_cancel(tp, cancel_flags);
2577 std_return:
2578 return error;
2579}
2580
Dave Chinnerf6bba202013-08-12 20:49:46 +10002581/*
2582 * Enter all inodes for a rename transaction into a sorted array.
2583 */
2584STATIC void
2585xfs_sort_for_rename(
2586 xfs_inode_t *dp1, /* in: old (source) directory inode */
2587 xfs_inode_t *dp2, /* in: new (target) directory inode */
2588 xfs_inode_t *ip1, /* in: inode of old entry */
2589 xfs_inode_t *ip2, /* in: inode of new entry, if it
2590 already exists, NULL otherwise. */
2591 xfs_inode_t **i_tab,/* out: array of inode returned, sorted */
2592 int *num_inodes) /* out: number of inodes in array */
2593{
2594 xfs_inode_t *temp;
2595 int i, j;
2596
2597 /*
2598 * i_tab contains a list of pointers to inodes. We initialize
2599 * the table here & we'll sort it. We will then use it to
2600 * order the acquisition of the inode locks.
2601 *
2602 * Note that the table may contain duplicates. e.g., dp1 == dp2.
2603 */
2604 i_tab[0] = dp1;
2605 i_tab[1] = dp2;
2606 i_tab[2] = ip1;
2607 if (ip2) {
2608 *num_inodes = 4;
2609 i_tab[3] = ip2;
2610 } else {
2611 *num_inodes = 3;
2612 i_tab[3] = NULL;
2613 }
2614
2615 /*
2616 * Sort the elements via bubble sort. (Remember, there are at
2617 * most 4 elements to sort, so this is adequate.)
2618 */
2619 for (i = 0; i < *num_inodes; i++) {
2620 for (j = 1; j < *num_inodes; j++) {
2621 if (i_tab[j]->i_ino < i_tab[j-1]->i_ino) {
2622 temp = i_tab[j];
2623 i_tab[j] = i_tab[j-1];
2624 i_tab[j-1] = temp;
2625 }
2626 }
2627 }
2628}
2629
2630/*
2631 * xfs_rename
2632 */
2633int
2634xfs_rename(
2635 xfs_inode_t *src_dp,
2636 struct xfs_name *src_name,
2637 xfs_inode_t *src_ip,
2638 xfs_inode_t *target_dp,
2639 struct xfs_name *target_name,
2640 xfs_inode_t *target_ip)
2641{
2642 xfs_trans_t *tp = NULL;
2643 xfs_mount_t *mp = src_dp->i_mount;
2644 int new_parent; /* moving to a new dir */
2645 int src_is_directory; /* src_name is a directory */
2646 int error;
2647 xfs_bmap_free_t free_list;
2648 xfs_fsblock_t first_block;
2649 int cancel_flags;
2650 int committed;
2651 xfs_inode_t *inodes[4];
2652 int spaceres;
2653 int num_inodes;
2654
2655 trace_xfs_rename(src_dp, target_dp, src_name, target_name);
2656
2657 new_parent = (src_dp != target_dp);
2658 src_is_directory = S_ISDIR(src_ip->i_d.di_mode);
2659
2660 xfs_sort_for_rename(src_dp, target_dp, src_ip, target_ip,
2661 inodes, &num_inodes);
2662
2663 xfs_bmap_init(&free_list, &first_block);
2664 tp = xfs_trans_alloc(mp, XFS_TRANS_RENAME);
2665 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2666 spaceres = XFS_RENAME_SPACE_RES(mp, target_name->len);
Jie Liu3d3c8b52013-08-12 20:49:59 +10002667 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_rename, spaceres, 0);
Dave Chinnerf6bba202013-08-12 20:49:46 +10002668 if (error == ENOSPC) {
2669 spaceres = 0;
Jie Liu3d3c8b52013-08-12 20:49:59 +10002670 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_rename, 0, 0);
Dave Chinnerf6bba202013-08-12 20:49:46 +10002671 }
2672 if (error) {
2673 xfs_trans_cancel(tp, 0);
2674 goto std_return;
2675 }
2676
2677 /*
2678 * Attach the dquots to the inodes
2679 */
2680 error = xfs_qm_vop_rename_dqattach(inodes);
2681 if (error) {
2682 xfs_trans_cancel(tp, cancel_flags);
2683 goto std_return;
2684 }
2685
2686 /*
2687 * Lock all the participating inodes. Depending upon whether
2688 * the target_name exists in the target directory, and
2689 * whether the target directory is the same as the source
2690 * directory, we can lock from 2 to 4 inodes.
2691 */
2692 xfs_lock_inodes(inodes, num_inodes, XFS_ILOCK_EXCL);
2693
2694 /*
2695 * Join all the inodes to the transaction. From this point on,
2696 * we can rely on either trans_commit or trans_cancel to unlock
2697 * them.
2698 */
2699 xfs_trans_ijoin(tp, src_dp, XFS_ILOCK_EXCL);
2700 if (new_parent)
2701 xfs_trans_ijoin(tp, target_dp, XFS_ILOCK_EXCL);
2702 xfs_trans_ijoin(tp, src_ip, XFS_ILOCK_EXCL);
2703 if (target_ip)
2704 xfs_trans_ijoin(tp, target_ip, XFS_ILOCK_EXCL);
2705
2706 /*
2707 * If we are using project inheritance, we only allow renames
2708 * into our tree when the project IDs are the same; else the
2709 * tree quota mechanism would be circumvented.
2710 */
2711 if (unlikely((target_dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2712 (xfs_get_projid(target_dp) != xfs_get_projid(src_ip)))) {
2713 error = XFS_ERROR(EXDEV);
2714 goto error_return;
2715 }
2716
2717 /*
2718 * Set up the target.
2719 */
2720 if (target_ip == NULL) {
2721 /*
2722 * If there's no space reservation, check the entry will
2723 * fit before actually inserting it.
2724 */
2725 error = xfs_dir_canenter(tp, target_dp, target_name, spaceres);
2726 if (error)
2727 goto error_return;
2728 /*
2729 * If target does not exist and the rename crosses
2730 * directories, adjust the target directory link count
2731 * to account for the ".." reference from the new entry.
2732 */
2733 error = xfs_dir_createname(tp, target_dp, target_name,
2734 src_ip->i_ino, &first_block,
2735 &free_list, spaceres);
2736 if (error == ENOSPC)
2737 goto error_return;
2738 if (error)
2739 goto abort_return;
2740
2741 xfs_trans_ichgtime(tp, target_dp,
2742 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2743
2744 if (new_parent && src_is_directory) {
2745 error = xfs_bumplink(tp, target_dp);
2746 if (error)
2747 goto abort_return;
2748 }
2749 } else { /* target_ip != NULL */
2750 /*
2751 * If target exists and it's a directory, check that both
2752 * target and source are directories and that target can be
2753 * destroyed, or that neither is a directory.
2754 */
2755 if (S_ISDIR(target_ip->i_d.di_mode)) {
2756 /*
2757 * Make sure target dir is empty.
2758 */
2759 if (!(xfs_dir_isempty(target_ip)) ||
2760 (target_ip->i_d.di_nlink > 2)) {
2761 error = XFS_ERROR(EEXIST);
2762 goto error_return;
2763 }
2764 }
2765
2766 /*
2767 * Link the source inode under the target name.
2768 * If the source inode is a directory and we are moving
2769 * it across directories, its ".." entry will be
2770 * inconsistent until we replace that down below.
2771 *
2772 * In case there is already an entry with the same
2773 * name at the destination directory, remove it first.
2774 */
2775 error = xfs_dir_replace(tp, target_dp, target_name,
2776 src_ip->i_ino,
2777 &first_block, &free_list, spaceres);
2778 if (error)
2779 goto abort_return;
2780
2781 xfs_trans_ichgtime(tp, target_dp,
2782 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2783
2784 /*
2785 * Decrement the link count on the target since the target
2786 * dir no longer points to it.
2787 */
2788 error = xfs_droplink(tp, target_ip);
2789 if (error)
2790 goto abort_return;
2791
2792 if (src_is_directory) {
2793 /*
2794 * Drop the link from the old "." entry.
2795 */
2796 error = xfs_droplink(tp, target_ip);
2797 if (error)
2798 goto abort_return;
2799 }
2800 } /* target_ip != NULL */
2801
2802 /*
2803 * Remove the source.
2804 */
2805 if (new_parent && src_is_directory) {
2806 /*
2807 * Rewrite the ".." entry to point to the new
2808 * directory.
2809 */
2810 error = xfs_dir_replace(tp, src_ip, &xfs_name_dotdot,
2811 target_dp->i_ino,
2812 &first_block, &free_list, spaceres);
2813 ASSERT(error != EEXIST);
2814 if (error)
2815 goto abort_return;
2816 }
2817
2818 /*
2819 * We always want to hit the ctime on the source inode.
2820 *
2821 * This isn't strictly required by the standards since the source
2822 * inode isn't really being changed, but old unix file systems did
2823 * it and some incremental backup programs won't work without it.
2824 */
2825 xfs_trans_ichgtime(tp, src_ip, XFS_ICHGTIME_CHG);
2826 xfs_trans_log_inode(tp, src_ip, XFS_ILOG_CORE);
2827
2828 /*
2829 * Adjust the link count on src_dp. This is necessary when
2830 * renaming a directory, either within one parent when
2831 * the target existed, or across two parent directories.
2832 */
2833 if (src_is_directory && (new_parent || target_ip != NULL)) {
2834
2835 /*
2836 * Decrement link count on src_directory since the
2837 * entry that's moved no longer points to it.
2838 */
2839 error = xfs_droplink(tp, src_dp);
2840 if (error)
2841 goto abort_return;
2842 }
2843
2844 error = xfs_dir_removename(tp, src_dp, src_name, src_ip->i_ino,
2845 &first_block, &free_list, spaceres);
2846 if (error)
2847 goto abort_return;
2848
2849 xfs_trans_ichgtime(tp, src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2850 xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE);
2851 if (new_parent)
2852 xfs_trans_log_inode(tp, target_dp, XFS_ILOG_CORE);
2853
2854 /*
2855 * If this is a synchronous mount, make sure that the
2856 * rename transaction goes to disk before returning to
2857 * the user.
2858 */
2859 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2860 xfs_trans_set_sync(tp);
2861 }
2862
2863 error = xfs_bmap_finish(&tp, &free_list, &committed);
2864 if (error) {
2865 xfs_bmap_cancel(&free_list);
2866 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
2867 XFS_TRANS_ABORT));
2868 goto std_return;
2869 }
2870
2871 /*
2872 * trans_commit will unlock src_ip, target_ip & decrement
2873 * the vnode references.
2874 */
2875 return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2876
2877 abort_return:
2878 cancel_flags |= XFS_TRANS_ABORT;
2879 error_return:
2880 xfs_bmap_cancel(&free_list);
2881 xfs_trans_cancel(tp, cancel_flags);
2882 std_return:
2883 return error;
2884}
2885
David Chinnerbad55842008-03-06 13:43:49 +11002886STATIC int
2887xfs_iflush_cluster(
2888 xfs_inode_t *ip,
2889 xfs_buf_t *bp)
2890{
2891 xfs_mount_t *mp = ip->i_mount;
Dave Chinner5017e972010-01-11 11:47:40 +00002892 struct xfs_perag *pag;
David Chinnerbad55842008-03-06 13:43:49 +11002893 unsigned long first_index, mask;
David Chinnerc8f5f122008-05-20 11:30:15 +10002894 unsigned long inodes_per_cluster;
David Chinnerbad55842008-03-06 13:43:49 +11002895 int ilist_size;
2896 xfs_inode_t **ilist;
2897 xfs_inode_t *iq;
David Chinnerbad55842008-03-06 13:43:49 +11002898 int nr_found;
2899 int clcount = 0;
2900 int bufwasdelwri;
2901 int i;
2902
Dave Chinner5017e972010-01-11 11:47:40 +00002903 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
David Chinnerbad55842008-03-06 13:43:49 +11002904
David Chinnerc8f5f122008-05-20 11:30:15 +10002905 inodes_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog;
2906 ilist_size = inodes_per_cluster * sizeof(xfs_inode_t *);
David Chinner49383b02008-05-19 16:29:34 +10002907 ilist = kmem_alloc(ilist_size, KM_MAYFAIL|KM_NOFS);
David Chinnerbad55842008-03-06 13:43:49 +11002908 if (!ilist)
Dave Chinner44b56e02010-01-11 11:47:43 +00002909 goto out_put;
David Chinnerbad55842008-03-06 13:43:49 +11002910
2911 mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
2912 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask;
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002913 rcu_read_lock();
David Chinnerbad55842008-03-06 13:43:49 +11002914 /* really need a gang lookup range call here */
2915 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)ilist,
David Chinnerc8f5f122008-05-20 11:30:15 +10002916 first_index, inodes_per_cluster);
David Chinnerbad55842008-03-06 13:43:49 +11002917 if (nr_found == 0)
2918 goto out_free;
2919
2920 for (i = 0; i < nr_found; i++) {
2921 iq = ilist[i];
2922 if (iq == ip)
2923 continue;
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002924
2925 /*
2926 * because this is an RCU protected lookup, we could find a
2927 * recently freed or even reallocated inode during the lookup.
2928 * We need to check under the i_flags_lock for a valid inode
2929 * here. Skip it if it is not valid or the wrong inode.
2930 */
2931 spin_lock(&ip->i_flags_lock);
2932 if (!ip->i_ino ||
2933 (XFS_INO_TO_AGINO(mp, iq->i_ino) & mask) != first_index) {
2934 spin_unlock(&ip->i_flags_lock);
2935 continue;
2936 }
2937 spin_unlock(&ip->i_flags_lock);
2938
David Chinnerbad55842008-03-06 13:43:49 +11002939 /*
2940 * Do an un-protected check to see if the inode is dirty and
2941 * is a candidate for flushing. These checks will be repeated
2942 * later after the appropriate locks are acquired.
2943 */
David Chinner33540402008-03-06 13:43:59 +11002944 if (xfs_inode_clean(iq) && xfs_ipincount(iq) == 0)
David Chinnerbad55842008-03-06 13:43:49 +11002945 continue;
David Chinnerbad55842008-03-06 13:43:49 +11002946
2947 /*
2948 * Try to get locks. If any are unavailable or it is pinned,
2949 * then this inode cannot be flushed and is skipped.
2950 */
2951
2952 if (!xfs_ilock_nowait(iq, XFS_ILOCK_SHARED))
2953 continue;
2954 if (!xfs_iflock_nowait(iq)) {
2955 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2956 continue;
2957 }
2958 if (xfs_ipincount(iq)) {
2959 xfs_ifunlock(iq);
2960 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2961 continue;
2962 }
2963
2964 /*
2965 * arriving here means that this inode can be flushed. First
2966 * re-check that it's dirty before flushing.
2967 */
David Chinner33540402008-03-06 13:43:59 +11002968 if (!xfs_inode_clean(iq)) {
2969 int error;
David Chinnerbad55842008-03-06 13:43:49 +11002970 error = xfs_iflush_int(iq, bp);
2971 if (error) {
2972 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2973 goto cluster_corrupt_out;
2974 }
2975 clcount++;
2976 } else {
2977 xfs_ifunlock(iq);
2978 }
2979 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2980 }
2981
2982 if (clcount) {
2983 XFS_STATS_INC(xs_icluster_flushcnt);
2984 XFS_STATS_ADD(xs_icluster_flushinode, clcount);
2985 }
2986
2987out_free:
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002988 rcu_read_unlock();
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002989 kmem_free(ilist);
Dave Chinner44b56e02010-01-11 11:47:43 +00002990out_put:
2991 xfs_perag_put(pag);
David Chinnerbad55842008-03-06 13:43:49 +11002992 return 0;
2993
2994
2995cluster_corrupt_out:
2996 /*
2997 * Corruption detected in the clustering loop. Invalidate the
2998 * inode buffer and shut down the filesystem.
2999 */
Dave Chinner1a3e8f32010-12-17 17:29:43 +11003000 rcu_read_unlock();
David Chinnerbad55842008-03-06 13:43:49 +11003001 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10003002 * Clean up the buffer. If it was delwri, just release it --
David Chinnerbad55842008-03-06 13:43:49 +11003003 * brelse can handle it with no problems. If not, shut down the
3004 * filesystem before releasing the buffer.
3005 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10003006 bufwasdelwri = (bp->b_flags & _XBF_DELWRI_Q);
David Chinnerbad55842008-03-06 13:43:49 +11003007 if (bufwasdelwri)
3008 xfs_buf_relse(bp);
3009
3010 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
3011
3012 if (!bufwasdelwri) {
3013 /*
3014 * Just like incore_relse: if we have b_iodone functions,
3015 * mark the buffer as an error and call them. Otherwise
3016 * mark it as stale and brelse.
3017 */
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02003018 if (bp->b_iodone) {
David Chinnerbad55842008-03-06 13:43:49 +11003019 XFS_BUF_UNDONE(bp);
Christoph Hellwigc867cb62011-10-10 16:52:46 +00003020 xfs_buf_stale(bp);
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00003021 xfs_buf_ioerror(bp, EIO);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00003022 xfs_buf_ioend(bp, 0);
David Chinnerbad55842008-03-06 13:43:49 +11003023 } else {
Christoph Hellwigc867cb62011-10-10 16:52:46 +00003024 xfs_buf_stale(bp);
David Chinnerbad55842008-03-06 13:43:49 +11003025 xfs_buf_relse(bp);
3026 }
3027 }
3028
3029 /*
3030 * Unlocks the flush lock
3031 */
Dave Chinner04913fd2012-04-23 15:58:41 +10003032 xfs_iflush_abort(iq, false);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003033 kmem_free(ilist);
Dave Chinner44b56e02010-01-11 11:47:43 +00003034 xfs_perag_put(pag);
David Chinnerbad55842008-03-06 13:43:49 +11003035 return XFS_ERROR(EFSCORRUPTED);
3036}
3037
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038/*
Christoph Hellwig4c468192012-04-23 15:58:36 +10003039 * Flush dirty inode metadata into the backing buffer.
3040 *
3041 * The caller must have the inode lock and the inode flush lock held. The
3042 * inode lock will still be held upon return to the caller, and the inode
3043 * flush lock will be released after the inode has reached the disk.
3044 *
3045 * The caller must write out the buffer returned in *bpp and release it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 */
3047int
3048xfs_iflush(
Christoph Hellwig4c468192012-04-23 15:58:36 +10003049 struct xfs_inode *ip,
3050 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051{
Christoph Hellwig4c468192012-04-23 15:58:36 +10003052 struct xfs_mount *mp = ip->i_mount;
3053 struct xfs_buf *bp;
3054 struct xfs_dinode *dip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056
3057 XFS_STATS_INC(xs_iflush_count);
3058
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10003059 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
Christoph Hellwig474fce02011-12-18 20:00:09 +00003060 ASSERT(xfs_isiflocked(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00003062 ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063
Christoph Hellwig4c468192012-04-23 15:58:36 +10003064 *bpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 xfs_iunpin_wait(ip);
3067
3068 /*
Dave Chinner4b6a4682010-01-11 11:45:21 +00003069 * For stale inodes we cannot rely on the backing buffer remaining
3070 * stale in cache for the remaining life of the stale inode and so
Christoph Hellwig475ee412012-07-03 12:21:22 -04003071 * xfs_imap_to_bp() below may give us a buffer that no longer contains
Dave Chinner4b6a4682010-01-11 11:45:21 +00003072 * inodes below. We have to check this after ensuring the inode is
3073 * unpinned so that it is safe to reclaim the stale inode after the
3074 * flush call.
3075 */
3076 if (xfs_iflags_test(ip, XFS_ISTALE)) {
3077 xfs_ifunlock(ip);
3078 return 0;
3079 }
3080
3081 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 * This may have been unpinned because the filesystem is shutting
3083 * down forcibly. If that's the case we must not write this inode
Christoph Hellwig32ce90a2012-04-23 15:58:32 +10003084 * to disk, because the log record didn't make it to disk.
3085 *
3086 * We also have to remove the log item from the AIL in this case,
3087 * as we wait for an empty AIL as part of the unmount process.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 */
3089 if (XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwig32ce90a2012-04-23 15:58:32 +10003090 error = XFS_ERROR(EIO);
3091 goto abort_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 }
3093
3094 /*
David Chinnera3f74ff2008-03-06 13:43:42 +11003095 * Get the buffer containing the on-disk inode.
3096 */
Christoph Hellwig475ee412012-07-03 12:21:22 -04003097 error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &bp, XBF_TRYLOCK,
3098 0);
David Chinnera3f74ff2008-03-06 13:43:42 +11003099 if (error || !bp) {
3100 xfs_ifunlock(ip);
3101 return error;
3102 }
3103
3104 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 * First flush out the inode that xfs_iflush was called with.
3106 */
3107 error = xfs_iflush_int(ip, bp);
David Chinnerbad55842008-03-06 13:43:49 +11003108 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 goto corrupt_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110
3111 /*
David Chinnera3f74ff2008-03-06 13:43:42 +11003112 * If the buffer is pinned then push on the log now so we won't
3113 * get stuck waiting in the write for too long.
3114 */
Chandra Seetharaman811e64c2011-07-22 23:40:27 +00003115 if (xfs_buf_ispinned(bp))
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003116 xfs_log_force(mp, 0);
David Chinnera3f74ff2008-03-06 13:43:42 +11003117
3118 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 * inode clustering:
3120 * see if other inodes can be gathered into this write
3121 */
David Chinnerbad55842008-03-06 13:43:49 +11003122 error = xfs_iflush_cluster(ip, bp);
3123 if (error)
3124 goto cluster_corrupt_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125
Christoph Hellwig4c468192012-04-23 15:58:36 +10003126 *bpp = bp;
3127 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
3129corrupt_out:
3130 xfs_buf_relse(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +10003131 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132cluster_corrupt_out:
Christoph Hellwig32ce90a2012-04-23 15:58:32 +10003133 error = XFS_ERROR(EFSCORRUPTED);
3134abort_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 /*
3136 * Unlocks the flush lock
3137 */
Dave Chinner04913fd2012-04-23 15:58:41 +10003138 xfs_iflush_abort(ip, false);
Christoph Hellwig32ce90a2012-04-23 15:58:32 +10003139 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140}
3141
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142STATIC int
3143xfs_iflush_int(
Christoph Hellwig93848a92013-04-03 16:11:17 +11003144 struct xfs_inode *ip,
3145 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146{
Christoph Hellwig93848a92013-04-03 16:11:17 +11003147 struct xfs_inode_log_item *iip = ip->i_itemp;
3148 struct xfs_dinode *dip;
3149 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10003151 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
Christoph Hellwig474fce02011-12-18 20:00:09 +00003152 ASSERT(xfs_isiflocked(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00003154 ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
Christoph Hellwig93848a92013-04-03 16:11:17 +11003155 ASSERT(iip != NULL && iip->ili_fields != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 /* set *dip = inode's place in the buffer */
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11003158 dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159
Christoph Hellwig69ef9212011-07-08 14:36:05 +02003160 if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11003162 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3163 "%s: Bad inode %Lu magic number 0x%x, ptr 0x%p",
3164 __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 goto corrupt_out;
3166 }
3167 if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC,
3168 mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11003169 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3170 "%s: Bad inode %Lu, ptr 0x%p, magic number 0x%x",
3171 __func__, ip->i_ino, ip, ip->i_d.di_magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 goto corrupt_out;
3173 }
Al Viroabbede12011-07-26 02:31:30 -04003174 if (S_ISREG(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 if (XFS_TEST_ERROR(
3176 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
3177 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE),
3178 mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11003179 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3180 "%s: Bad regular inode %Lu, ptr 0x%p",
3181 __func__, ip->i_ino, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 goto corrupt_out;
3183 }
Al Viroabbede12011-07-26 02:31:30 -04003184 } else if (S_ISDIR(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 if (XFS_TEST_ERROR(
3186 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
3187 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
3188 (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL),
3189 mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11003190 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3191 "%s: Bad directory inode %Lu, ptr 0x%p",
3192 __func__, ip->i_ino, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 goto corrupt_out;
3194 }
3195 }
3196 if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents >
3197 ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5,
3198 XFS_RANDOM_IFLUSH_5)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11003199 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3200 "%s: detected corrupt incore inode %Lu, "
3201 "total extents = %d, nblocks = %Ld, ptr 0x%p",
3202 __func__, ip->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 ip->i_d.di_nextents + ip->i_d.di_anextents,
Dave Chinner6a19d932011-03-07 10:02:35 +11003204 ip->i_d.di_nblocks, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 goto corrupt_out;
3206 }
3207 if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
3208 mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11003209 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3210 "%s: bad inode %Lu, forkoff 0x%x, ptr 0x%p",
3211 __func__, ip->i_ino, ip->i_d.di_forkoff, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 goto corrupt_out;
3213 }
Dave Chinnere60896d2013-07-24 15:47:30 +10003214
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 /*
Dave Chinnere60896d2013-07-24 15:47:30 +10003216 * Inode item log recovery for v1/v2 inodes are dependent on the
3217 * di_flushiter count for correct sequencing. We bump the flush
3218 * iteration count so we can detect flushes which postdate a log record
3219 * during recovery. This is redundant as we now log every change and
3220 * hence this can't happen but we need to still do it to ensure
3221 * backwards compatibility with old kernels that predate logging all
3222 * inode changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 */
Dave Chinnere60896d2013-07-24 15:47:30 +10003224 if (ip->i_d.di_version < 3)
3225 ip->i_d.di_flushiter++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226
3227 /*
3228 * Copy the dirty parts of the inode into the on-disk
3229 * inode. We always copy out the core of the inode,
3230 * because if the inode is dirty at all the core must
3231 * be.
3232 */
Christoph Hellwig81591fe2008-11-28 14:23:39 +11003233 xfs_dinode_to_disk(dip, &ip->i_d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234
3235 /* Wrap, we never let the log put out DI_MAX_FLUSH */
3236 if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
3237 ip->i_d.di_flushiter = 0;
3238
3239 /*
3240 * If this is really an old format inode and the superblock version
3241 * has not been updated to support only new format inodes, then
3242 * convert back to the old inode format. If the superblock version
3243 * has been updated, then make the conversion permanent.
3244 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11003245 ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb));
3246 if (ip->i_d.di_version == 1) {
Eric Sandeen62118702008-03-06 13:44:28 +11003247 if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 /*
3249 * Convert it back.
3250 */
3251 ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
Christoph Hellwig81591fe2008-11-28 14:23:39 +11003252 dip->di_onlink = cpu_to_be16(ip->i_d.di_nlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 } else {
3254 /*
3255 * The superblock version has already been bumped,
3256 * so just make the conversion to the new inode
3257 * format permanent.
3258 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11003259 ip->i_d.di_version = 2;
3260 dip->di_version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 ip->i_d.di_onlink = 0;
Christoph Hellwig81591fe2008-11-28 14:23:39 +11003262 dip->di_onlink = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
Christoph Hellwig81591fe2008-11-28 14:23:39 +11003264 memset(&(dip->di_pad[0]), 0,
3265 sizeof(dip->di_pad));
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00003266 ASSERT(xfs_get_projid(ip) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 }
3268 }
3269
David Chinnere4ac9672008-04-10 12:23:58 +10003270 xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK, bp);
3271 if (XFS_IFORK_Q(ip))
3272 xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 xfs_inobp_check(mp, bp);
3274
3275 /*
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00003276 * We've recorded everything logged in the inode, so we'd like to clear
3277 * the ili_fields bits so we don't log and flush things unnecessarily.
3278 * However, we can't stop logging all this information until the data
3279 * we've copied into the disk buffer is written to disk. If we did we
3280 * might overwrite the copy of the inode in the log with all the data
3281 * after re-logging only part of it, and in the face of a crash we
3282 * wouldn't have all the data we need to recover.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 *
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00003284 * What we do is move the bits to the ili_last_fields field. When
3285 * logging the inode, these bits are moved back to the ili_fields field.
3286 * In the xfs_iflush_done() routine we clear ili_last_fields, since we
3287 * know that the information those bits represent is permanently on
3288 * disk. As long as the flush completes before the inode is logged
3289 * again, then both ili_fields and ili_last_fields will be cleared.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 *
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00003291 * We can play with the ili_fields bits here, because the inode lock
3292 * must be held exclusively in order to set bits there and the flush
3293 * lock protects the ili_last_fields bits. Set ili_logged so the flush
3294 * done routine can tell whether or not to look in the AIL. Also, store
3295 * the current LSN of the inode so that we can tell whether the item has
3296 * moved in the AIL from xfs_iflush_done(). In order to read the lsn we
3297 * need the AIL lock, because it is a 64 bit value that cannot be read
3298 * atomically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 */
Christoph Hellwig93848a92013-04-03 16:11:17 +11003300 iip->ili_last_fields = iip->ili_fields;
3301 iip->ili_fields = 0;
3302 iip->ili_logged = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303
Christoph Hellwig93848a92013-04-03 16:11:17 +11003304 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
3305 &iip->ili_item.li_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306
Christoph Hellwig93848a92013-04-03 16:11:17 +11003307 /*
3308 * Attach the function xfs_iflush_done to the inode's
3309 * buffer. This will remove the inode from the AIL
3310 * and unlock the inode's flush lock when the inode is
3311 * completely written to disk.
3312 */
3313 xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314
Christoph Hellwig93848a92013-04-03 16:11:17 +11003315 /* update the lsn in the on disk inode if required */
3316 if (ip->i_d.di_version == 3)
3317 dip->di_lsn = cpu_to_be64(iip->ili_item.li_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318
Christoph Hellwig93848a92013-04-03 16:11:17 +11003319 /* generate the checksum. */
3320 xfs_dinode_calc_crc(mp, dip);
3321
3322 ASSERT(bp->b_fspriv != NULL);
3323 ASSERT(bp->b_iodone != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 return 0;
3325
3326corrupt_out:
3327 return XFS_ERROR(EFSCORRUPTED);
3328}