blob: 967f90625eae8d5ecfecfd2b10c544bbc764611e [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 Chinner70a98832013-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"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110045#include "xfs_dinode.h"
David Chinner2a82b8b2007-07-11 11:09:12 +100046#include "xfs_filestream.h"
Christoph Hellwig93848a92013-04-03 16:11:17 +110047#include "xfs_cksum.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000048#include "xfs_trace.h"
Dave Chinner33479e02012-10-08 21:56:11 +110049#include "xfs_icache.h"
Dave Chinnerc24b5df2013-08-12 20:49:45 +100050#include "xfs_symlink.h"
Dave Chinner239880e2013-10-23 10:50:10 +110051#include "xfs_trans_priv.h"
52#include "xfs_log.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110053#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055kmem_zone_t *xfs_inode_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57/*
Christoph Hellwig8f04c472011-07-08 14:34:34 +020058 * Used in xfs_itruncate_extents(). This is the maximum number of extents
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 * freed from a file in a single transaction.
60 */
61#define XFS_ITRUNC_MAX_EXTENTS 2
62
63STATIC int xfs_iflush_int(xfs_inode_t *, xfs_buf_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Dave Chinner2a0ec1d2012-04-23 15:59:02 +100065/*
66 * helper function to extract extent size hint from inode
67 */
68xfs_extlen_t
69xfs_get_extsz_hint(
70 struct xfs_inode *ip)
71{
72 if ((ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE) && ip->i_d.di_extsize)
73 return ip->i_d.di_extsize;
74 if (XFS_IS_REALTIME_INODE(ip))
75 return ip->i_mount->m_sb.sb_rextsize;
76 return 0;
77}
78
Dave Chinnerfa96aca2012-10-08 21:56:10 +110079/*
80 * This is a wrapper routine around the xfs_ilock() routine used to centralize
81 * some grungy code. It is used in places that wish to lock the inode solely
82 * for reading the extents. The reason these places can't just call
83 * xfs_ilock(SHARED) is that the inode lock also guards to bringing in of the
84 * extents from disk for a file in b-tree format. If the inode is in b-tree
85 * format, then we need to lock the inode exclusively until the extents are read
86 * in. Locking it exclusively all the time would limit our parallelism
87 * unnecessarily, though. What we do instead is check to see if the extents
88 * have been read in yet, and only lock the inode exclusively if they have not.
89 *
90 * The function returns a value which should be given to the corresponding
Christoph Hellwig01f4f322013-12-06 12:30:08 -080091 * xfs_iunlock() call.
Dave Chinnerfa96aca2012-10-08 21:56:10 +110092 */
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/*
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100112 * The xfs inode contains 2 locks: a multi-reader lock called the
113 * i_iolock and a multi-reader lock called the i_lock. This routine
114 * allows either or both of the locks to be obtained.
115 *
116 * The 2 locks should always be ordered so that the IO lock is
117 * obtained first in order to prevent deadlock.
118 *
119 * ip -- the inode being locked
120 * lock_flags -- this parameter indicates the inode's locks
121 * to be locked. It can be:
122 * XFS_IOLOCK_SHARED,
123 * XFS_IOLOCK_EXCL,
124 * XFS_ILOCK_SHARED,
125 * XFS_ILOCK_EXCL,
126 * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
127 * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
128 * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
129 * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
130 */
131void
132xfs_ilock(
133 xfs_inode_t *ip,
134 uint lock_flags)
135{
136 trace_xfs_ilock(ip, lock_flags, _RET_IP_);
137
138 /*
139 * You can't set both SHARED and EXCL for the same lock,
140 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
141 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
142 */
143 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
144 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
145 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
146 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
147 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
148
149 if (lock_flags & XFS_IOLOCK_EXCL)
150 mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
151 else if (lock_flags & XFS_IOLOCK_SHARED)
152 mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
153
154 if (lock_flags & XFS_ILOCK_EXCL)
155 mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
156 else if (lock_flags & XFS_ILOCK_SHARED)
157 mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
158}
159
160/*
161 * This is just like xfs_ilock(), except that the caller
162 * is guaranteed not to sleep. It returns 1 if it gets
163 * the requested locks and 0 otherwise. If the IO lock is
164 * obtained but the inode lock cannot be, then the IO lock
165 * is dropped before returning.
166 *
167 * ip -- the inode being locked
168 * lock_flags -- this parameter indicates the inode's locks to be
169 * to be locked. See the comment for xfs_ilock() for a list
170 * of valid values.
171 */
172int
173xfs_ilock_nowait(
174 xfs_inode_t *ip,
175 uint lock_flags)
176{
177 trace_xfs_ilock_nowait(ip, lock_flags, _RET_IP_);
178
179 /*
180 * You can't set both SHARED and EXCL for the same lock,
181 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
182 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
183 */
184 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
185 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
186 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
187 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
188 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
189
190 if (lock_flags & XFS_IOLOCK_EXCL) {
191 if (!mrtryupdate(&ip->i_iolock))
192 goto out;
193 } else if (lock_flags & XFS_IOLOCK_SHARED) {
194 if (!mrtryaccess(&ip->i_iolock))
195 goto out;
196 }
197 if (lock_flags & XFS_ILOCK_EXCL) {
198 if (!mrtryupdate(&ip->i_lock))
199 goto out_undo_iolock;
200 } else if (lock_flags & XFS_ILOCK_SHARED) {
201 if (!mrtryaccess(&ip->i_lock))
202 goto out_undo_iolock;
203 }
204 return 1;
205
206 out_undo_iolock:
207 if (lock_flags & XFS_IOLOCK_EXCL)
208 mrunlock_excl(&ip->i_iolock);
209 else if (lock_flags & XFS_IOLOCK_SHARED)
210 mrunlock_shared(&ip->i_iolock);
211 out:
212 return 0;
213}
214
215/*
216 * xfs_iunlock() is used to drop the inode locks acquired with
217 * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
218 * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
219 * that we know which locks to drop.
220 *
221 * ip -- the inode being unlocked
222 * lock_flags -- this parameter indicates the inode's locks to be
223 * to be unlocked. See the comment for xfs_ilock() for a list
224 * of valid values for this parameter.
225 *
226 */
227void
228xfs_iunlock(
229 xfs_inode_t *ip,
230 uint lock_flags)
231{
232 /*
233 * You can't set both SHARED and EXCL for the same lock,
234 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
235 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
236 */
237 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
238 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
239 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
240 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
241 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
242 ASSERT(lock_flags != 0);
243
244 if (lock_flags & XFS_IOLOCK_EXCL)
245 mrunlock_excl(&ip->i_iolock);
246 else if (lock_flags & XFS_IOLOCK_SHARED)
247 mrunlock_shared(&ip->i_iolock);
248
249 if (lock_flags & XFS_ILOCK_EXCL)
250 mrunlock_excl(&ip->i_lock);
251 else if (lock_flags & XFS_ILOCK_SHARED)
252 mrunlock_shared(&ip->i_lock);
253
254 trace_xfs_iunlock(ip, lock_flags, _RET_IP_);
255}
256
257/*
258 * give up write locks. the i/o lock cannot be held nested
259 * if it is being demoted.
260 */
261void
262xfs_ilock_demote(
263 xfs_inode_t *ip,
264 uint lock_flags)
265{
266 ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
267 ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
268
269 if (lock_flags & XFS_ILOCK_EXCL)
270 mrdemote(&ip->i_lock);
271 if (lock_flags & XFS_IOLOCK_EXCL)
272 mrdemote(&ip->i_iolock);
273
274 trace_xfs_ilock_demote(ip, lock_flags, _RET_IP_);
275}
276
Dave Chinner742ae1e2013-04-30 21:39:34 +1000277#if defined(DEBUG) || defined(XFS_WARN)
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100278int
279xfs_isilocked(
280 xfs_inode_t *ip,
281 uint lock_flags)
282{
283 if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) {
284 if (!(lock_flags & XFS_ILOCK_SHARED))
285 return !!ip->i_lock.mr_writer;
286 return rwsem_is_locked(&ip->i_lock.mr_lock);
287 }
288
289 if (lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) {
290 if (!(lock_flags & XFS_IOLOCK_SHARED))
291 return !!ip->i_iolock.mr_writer;
292 return rwsem_is_locked(&ip->i_iolock.mr_lock);
293 }
294
295 ASSERT(0);
296 return 0;
297}
298#endif
299
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000300#ifdef DEBUG
301int xfs_locked_n;
302int xfs_small_retries;
303int xfs_middle_retries;
304int xfs_lots_retries;
305int xfs_lock_delays;
306#endif
307
308/*
309 * Bump the subclass so xfs_lock_inodes() acquires each lock with
310 * a different value
311 */
312static inline int
313xfs_lock_inumorder(int lock_mode, int subclass)
314{
315 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
316 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
317 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
318 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
319
320 return lock_mode;
321}
322
323/*
324 * The following routine will lock n inodes in exclusive mode.
325 * We assume the caller calls us with the inodes in i_ino order.
326 *
327 * We need to detect deadlock where an inode that we lock
328 * is in the AIL and we start waiting for another inode that is locked
329 * by a thread in a long running transaction (such as truncate). This can
330 * result in deadlock since the long running trans might need to wait
331 * for the inode we just locked in order to push the tail and free space
332 * in the log.
333 */
334void
335xfs_lock_inodes(
336 xfs_inode_t **ips,
337 int inodes,
338 uint lock_mode)
339{
340 int attempts = 0, i, j, try_lock;
341 xfs_log_item_t *lp;
342
343 ASSERT(ips && (inodes >= 2)); /* we need at least two */
344
345 try_lock = 0;
346 i = 0;
347
348again:
349 for (; i < inodes; i++) {
350 ASSERT(ips[i]);
351
352 if (i && (ips[i] == ips[i-1])) /* Already locked */
353 continue;
354
355 /*
356 * If try_lock is not set yet, make sure all locked inodes
357 * are not in the AIL.
358 * If any are, set try_lock to be used later.
359 */
360
361 if (!try_lock) {
362 for (j = (i - 1); j >= 0 && !try_lock; j--) {
363 lp = (xfs_log_item_t *)ips[j]->i_itemp;
364 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
365 try_lock++;
366 }
367 }
368 }
369
370 /*
371 * If any of the previous locks we have locked is in the AIL,
372 * we must TRY to get the second and subsequent locks. If
373 * we can't get any, we must release all we have
374 * and try again.
375 */
376
377 if (try_lock) {
378 /* try_lock must be 0 if i is 0. */
379 /*
380 * try_lock means we have an inode locked
381 * that is in the AIL.
382 */
383 ASSERT(i != 0);
384 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
385 attempts++;
386
387 /*
388 * Unlock all previous guys and try again.
389 * xfs_iunlock will try to push the tail
390 * if the inode is in the AIL.
391 */
392
393 for(j = i - 1; j >= 0; j--) {
394
395 /*
396 * Check to see if we've already
397 * unlocked this one.
398 * Not the first one going back,
399 * and the inode ptr is the same.
400 */
401 if ((j != (i - 1)) && ips[j] ==
402 ips[j+1])
403 continue;
404
405 xfs_iunlock(ips[j], lock_mode);
406 }
407
408 if ((attempts % 5) == 0) {
409 delay(1); /* Don't just spin the CPU */
410#ifdef DEBUG
411 xfs_lock_delays++;
412#endif
413 }
414 i = 0;
415 try_lock = 0;
416 goto again;
417 }
418 } else {
419 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
420 }
421 }
422
423#ifdef DEBUG
424 if (attempts) {
425 if (attempts < 5) xfs_small_retries++;
426 else if (attempts < 100) xfs_middle_retries++;
427 else xfs_lots_retries++;
428 } else {
429 xfs_locked_n++;
430 }
431#endif
432}
433
434/*
435 * xfs_lock_two_inodes() can only be used to lock one type of lock
436 * at a time - the iolock or the ilock, but not both at once. If
437 * we lock both at once, lockdep will report false positives saying
438 * we have violated locking orders.
439 */
440void
441xfs_lock_two_inodes(
442 xfs_inode_t *ip0,
443 xfs_inode_t *ip1,
444 uint lock_mode)
445{
446 xfs_inode_t *temp;
447 int attempts = 0;
448 xfs_log_item_t *lp;
449
450 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
451 ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0);
452 ASSERT(ip0->i_ino != ip1->i_ino);
453
454 if (ip0->i_ino > ip1->i_ino) {
455 temp = ip0;
456 ip0 = ip1;
457 ip1 = temp;
458 }
459
460 again:
461 xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
462
463 /*
464 * If the first lock we have locked is in the AIL, we must TRY to get
465 * the second lock. If we can't get it, we must release the first one
466 * and try again.
467 */
468 lp = (xfs_log_item_t *)ip0->i_itemp;
469 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
470 if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
471 xfs_iunlock(ip0, lock_mode);
472 if ((++attempts % 5) == 0)
473 delay(1); /* Don't just spin the CPU */
474 goto again;
475 }
476 } else {
477 xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
478 }
479}
480
481
Dave Chinnerfa96aca2012-10-08 21:56:10 +1100482void
483__xfs_iflock(
484 struct xfs_inode *ip)
485{
486 wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IFLOCK_BIT);
487 DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IFLOCK_BIT);
488
489 do {
490 prepare_to_wait_exclusive(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
491 if (xfs_isiflocked(ip))
492 io_schedule();
493 } while (!xfs_iflock_nowait(ip));
494
495 finish_wait(wq, &wait.wait);
496}
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498STATIC uint
499_xfs_dic2xflags(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 __uint16_t di_flags)
501{
502 uint flags = 0;
503
504 if (di_flags & XFS_DIFLAG_ANY) {
505 if (di_flags & XFS_DIFLAG_REALTIME)
506 flags |= XFS_XFLAG_REALTIME;
507 if (di_flags & XFS_DIFLAG_PREALLOC)
508 flags |= XFS_XFLAG_PREALLOC;
509 if (di_flags & XFS_DIFLAG_IMMUTABLE)
510 flags |= XFS_XFLAG_IMMUTABLE;
511 if (di_flags & XFS_DIFLAG_APPEND)
512 flags |= XFS_XFLAG_APPEND;
513 if (di_flags & XFS_DIFLAG_SYNC)
514 flags |= XFS_XFLAG_SYNC;
515 if (di_flags & XFS_DIFLAG_NOATIME)
516 flags |= XFS_XFLAG_NOATIME;
517 if (di_flags & XFS_DIFLAG_NODUMP)
518 flags |= XFS_XFLAG_NODUMP;
519 if (di_flags & XFS_DIFLAG_RTINHERIT)
520 flags |= XFS_XFLAG_RTINHERIT;
521 if (di_flags & XFS_DIFLAG_PROJINHERIT)
522 flags |= XFS_XFLAG_PROJINHERIT;
523 if (di_flags & XFS_DIFLAG_NOSYMLINKS)
524 flags |= XFS_XFLAG_NOSYMLINKS;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100525 if (di_flags & XFS_DIFLAG_EXTSIZE)
526 flags |= XFS_XFLAG_EXTSIZE;
527 if (di_flags & XFS_DIFLAG_EXTSZINHERIT)
528 flags |= XFS_XFLAG_EXTSZINHERIT;
Barry Naujokd3446ea2006-06-09 14:54:19 +1000529 if (di_flags & XFS_DIFLAG_NODEFRAG)
530 flags |= XFS_XFLAG_NODEFRAG;
David Chinner2a82b8b2007-07-11 11:09:12 +1000531 if (di_flags & XFS_DIFLAG_FILESTREAM)
532 flags |= XFS_XFLAG_FILESTREAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
534
535 return flags;
536}
537
538uint
539xfs_ip2xflags(
540 xfs_inode_t *ip)
541{
Christoph Hellwig347d1c02007-08-28 13:57:51 +1000542 xfs_icdinode_t *dic = &ip->i_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Nathan Scotta916e2b2006-06-09 17:12:17 +1000544 return _xfs_dic2xflags(dic->di_flags) |
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100545 (XFS_IFORK_Q(ip) ? XFS_XFLAG_HASATTR : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
548uint
549xfs_dic2xflags(
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100550 xfs_dinode_t *dip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
Christoph Hellwig81591fe2008-11-28 14:23:39 +1100552 return _xfs_dic2xflags(be16_to_cpu(dip->di_flags)) |
Christoph Hellwig45ba5982007-12-07 14:07:20 +1100553 (XFS_DFORK_Q(dip) ? XFS_XFLAG_HASATTR : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556/*
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000557 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
558 * is allowed, otherwise it has to be an exact match. If a CI match is found,
559 * ci_name->name will point to a the actual name (caller must free) or
560 * will be set to NULL if an exact match is found.
561 */
562int
563xfs_lookup(
564 xfs_inode_t *dp,
565 struct xfs_name *name,
566 xfs_inode_t **ipp,
567 struct xfs_name *ci_name)
568{
569 xfs_ino_t inum;
570 int error;
571 uint lock_mode;
572
573 trace_xfs_lookup(dp, name);
574
575 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
576 return XFS_ERROR(EIO);
577
578 lock_mode = xfs_ilock_map_shared(dp);
579 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
Christoph Hellwig01f4f322013-12-06 12:30:08 -0800580 xfs_iunlock(dp, lock_mode);
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000581
582 if (error)
583 goto out;
584
585 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp);
586 if (error)
587 goto out_free_name;
588
589 return 0;
590
591out_free_name:
592 if (ci_name)
593 kmem_free(ci_name->name);
594out:
595 *ipp = NULL;
596 return error;
597}
598
599/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 * Allocate an inode on disk and return a copy of its in-core version.
601 * The in-core inode is locked exclusively. Set mode, nlink, and rdev
602 * appropriately within the inode. The uid and gid for the inode are
603 * set according to the contents of the given cred structure.
604 *
605 * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc()
Carlos Maiolinocd856db2012-10-20 11:08:19 -0300606 * has a free inode available, call xfs_iget() to obtain the in-core
607 * version of the allocated inode. Finally, fill in the inode and
608 * log its initial contents. In this case, ialloc_context would be
609 * set to NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 *
Carlos Maiolinocd856db2012-10-20 11:08:19 -0300611 * If xfs_dialloc() does not have an available inode, it will replenish
612 * its supply by doing an allocation. Since we can only do one
613 * allocation within a transaction without deadlocks, we must commit
614 * the current transaction before returning the inode itself.
615 * In this case, therefore, we will set ialloc_context and return.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 * The caller should then commit the current transaction, start a new
617 * transaction, and call xfs_ialloc() again to actually get the inode.
618 *
619 * To ensure that some other process does not grab the inode that
620 * was allocated during the first call to xfs_ialloc(), this routine
621 * also returns the [locked] bp pointing to the head of the freelist
622 * as ialloc_context. The caller should hold this buffer across
623 * the commit and pass it back into this routine on the second call.
David Chinnerb11f94d2007-07-11 11:09:33 +1000624 *
625 * If we are allocating quota inodes, we do not have a parent inode
626 * to attach to or associate with (i.e. pip == NULL) because they
627 * are not linked into the directory structure - they are attached
628 * directly to the superblock - and so have no parent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 */
630int
631xfs_ialloc(
632 xfs_trans_t *tp,
633 xfs_inode_t *pip,
Al Viro576b1d62011-07-26 02:50:15 -0400634 umode_t mode,
Nathan Scott31b084a2005-05-05 13:25:00 -0700635 xfs_nlink_t nlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 xfs_dev_t rdev,
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000637 prid_t prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 int okalloc,
639 xfs_buf_t **ialloc_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 xfs_inode_t **ipp)
641{
Christoph Hellwig93848a92013-04-03 16:11:17 +1100642 struct xfs_mount *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 xfs_ino_t ino;
644 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 uint flags;
646 int error;
Christoph Hellwigdff35fd2008-08-13 16:44:15 +1000647 timespec_t tv;
David Chinnerbf904242008-10-30 17:36:14 +1100648 int filestreams = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 /*
651 * Call the space management code to pick
652 * the on-disk inode to be allocated.
653 */
David Chinnerb11f94d2007-07-11 11:09:33 +1000654 error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc,
Christoph Hellwig08358902012-07-04 10:54:47 -0400655 ialloc_context, &ino);
David Chinnerbf904242008-10-30 17:36:14 +1100656 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return error;
Christoph Hellwig08358902012-07-04 10:54:47 -0400658 if (*ialloc_context || ino == NULLFSINO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 *ipp = NULL;
660 return 0;
661 }
662 ASSERT(*ialloc_context == NULL);
663
664 /*
665 * Get the in-core inode with the lock held exclusively.
666 * This is because we're setting fields here we need
667 * to prevent others from looking at until we're done.
668 */
Christoph Hellwig93848a92013-04-03 16:11:17 +1100669 error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE,
Christoph Hellwigec3ba852011-02-13 13:26:42 +0000670 XFS_ILOCK_EXCL, &ip);
David Chinnerbf904242008-10-30 17:36:14 +1100671 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 ASSERT(ip != NULL);
674
Al Viro576b1d62011-07-26 02:50:15 -0400675 ip->i_d.di_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 ip->i_d.di_onlink = 0;
677 ip->i_d.di_nlink = nlink;
678 ASSERT(ip->i_d.di_nlink == nlink);
Dwight Engen7aab1b22013-08-15 14:08:01 -0400679 ip->i_d.di_uid = xfs_kuid_to_uid(current_fsuid());
680 ip->i_d.di_gid = xfs_kgid_to_gid(current_fsgid());
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +0000681 xfs_set_projid(ip, prid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
683
684 /*
685 * If the superblock version is up to where we support new format
686 * inodes and this is currently an old format inode, then change
687 * the inode version number now. This way we only do the conversion
688 * here rather than here and in the flush/logging code.
689 */
Christoph Hellwig93848a92013-04-03 16:11:17 +1100690 if (xfs_sb_version_hasnlink(&mp->m_sb) &&
Christoph Hellwig51ce16d2008-11-28 14:23:39 +1100691 ip->i_d.di_version == 1) {
692 ip->i_d.di_version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 /*
694 * We've already zeroed the old link count, the projid field,
695 * and the pad field.
696 */
697 }
698
699 /*
700 * Project ids won't be stored on disk if we are using a version 1 inode.
701 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +1100702 if ((prid != 0) && (ip->i_d.di_version == 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 xfs_bump_ino_vers2(tp, ip);
704
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000705 if (pip && XFS_INHERIT_GID(pip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 ip->i_d.di_gid = pip->i_d.di_gid;
Al Viroabbede12011-07-26 02:31:30 -0400707 if ((pip->i_d.di_mode & S_ISGID) && S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 ip->i_d.di_mode |= S_ISGID;
709 }
710 }
711
712 /*
713 * If the group ID of the new file does not match the effective group
714 * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
715 * (and only if the irix_sgid_inherit compatibility variable is set).
716 */
717 if ((irix_sgid_inherit) &&
718 (ip->i_d.di_mode & S_ISGID) &&
Dwight Engen7aab1b22013-08-15 14:08:01 -0400719 (!in_group_p(xfs_gid_to_kgid(ip->i_d.di_gid)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 ip->i_d.di_mode &= ~S_ISGID;
721 }
722
723 ip->i_d.di_size = 0;
724 ip->i_d.di_nextents = 0;
725 ASSERT(ip->i_d.di_nblocks == 0);
Christoph Hellwigdff35fd2008-08-13 16:44:15 +1000726
727 nanotime(&tv);
728 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
729 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
730 ip->i_d.di_atime = ip->i_d.di_mtime;
731 ip->i_d.di_ctime = ip->i_d.di_mtime;
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 /*
734 * di_gen will have been taken care of in xfs_iread.
735 */
736 ip->i_d.di_extsize = 0;
737 ip->i_d.di_dmevmask = 0;
738 ip->i_d.di_dmstate = 0;
739 ip->i_d.di_flags = 0;
Christoph Hellwig93848a92013-04-03 16:11:17 +1100740
741 if (ip->i_d.di_version == 3) {
742 ASSERT(ip->i_d.di_ino == ino);
743 ASSERT(uuid_equal(&ip->i_d.di_uuid, &mp->m_sb.sb_uuid));
744 ip->i_d.di_crc = 0;
745 ip->i_d.di_changecount = 1;
746 ip->i_d.di_lsn = 0;
747 ip->i_d.di_flags2 = 0;
748 memset(&(ip->i_d.di_pad2[0]), 0, sizeof(ip->i_d.di_pad2));
749 ip->i_d.di_crtime = ip->i_d.di_mtime;
750 }
751
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 flags = XFS_ILOG_CORE;
754 switch (mode & S_IFMT) {
755 case S_IFIFO:
756 case S_IFCHR:
757 case S_IFBLK:
758 case S_IFSOCK:
759 ip->i_d.di_format = XFS_DINODE_FMT_DEV;
760 ip->i_df.if_u2.if_rdev = rdev;
761 ip->i_df.if_flags = 0;
762 flags |= XFS_ILOG_DEV;
763 break;
764 case S_IFREG:
David Chinnerbf904242008-10-30 17:36:14 +1100765 /*
766 * we can't set up filestreams until after the VFS inode
767 * is set up properly.
768 */
769 if (pip && xfs_inode_is_filestream(pip))
770 filestreams = 1;
David Chinner2a82b8b2007-07-11 11:09:12 +1000771 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 case S_IFDIR:
David Chinnerb11f94d2007-07-11 11:09:33 +1000773 if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) {
Nathan Scott365ca832005-06-21 15:39:12 +1000774 uint di_flags = 0;
775
Al Viroabbede12011-07-26 02:31:30 -0400776 if (S_ISDIR(mode)) {
Nathan Scott365ca832005-06-21 15:39:12 +1000777 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
778 di_flags |= XFS_DIFLAG_RTINHERIT;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100779 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
780 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
781 ip->i_d.di_extsize = pip->i_d.di_extsize;
782 }
Al Viroabbede12011-07-26 02:31:30 -0400783 } else if (S_ISREG(mode)) {
Christoph Hellwig613d7042007-10-11 17:44:08 +1000784 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
Nathan Scott365ca832005-06-21 15:39:12 +1000785 di_flags |= XFS_DIFLAG_REALTIME;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100786 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
787 di_flags |= XFS_DIFLAG_EXTSIZE;
788 ip->i_d.di_extsize = pip->i_d.di_extsize;
789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
791 if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
792 xfs_inherit_noatime)
Nathan Scott365ca832005-06-21 15:39:12 +1000793 di_flags |= XFS_DIFLAG_NOATIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
795 xfs_inherit_nodump)
Nathan Scott365ca832005-06-21 15:39:12 +1000796 di_flags |= XFS_DIFLAG_NODUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
798 xfs_inherit_sync)
Nathan Scott365ca832005-06-21 15:39:12 +1000799 di_flags |= XFS_DIFLAG_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
801 xfs_inherit_nosymlinks)
Nathan Scott365ca832005-06-21 15:39:12 +1000802 di_flags |= XFS_DIFLAG_NOSYMLINKS;
803 if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
804 di_flags |= XFS_DIFLAG_PROJINHERIT;
Barry Naujokd3446ea2006-06-09 14:54:19 +1000805 if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
806 xfs_inherit_nodefrag)
807 di_flags |= XFS_DIFLAG_NODEFRAG;
David Chinner2a82b8b2007-07-11 11:09:12 +1000808 if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
809 di_flags |= XFS_DIFLAG_FILESTREAM;
Nathan Scott365ca832005-06-21 15:39:12 +1000810 ip->i_d.di_flags |= di_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
812 /* FALLTHROUGH */
813 case S_IFLNK:
814 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
815 ip->i_df.if_flags = XFS_IFEXTENTS;
816 ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0;
817 ip->i_df.if_u1.if_extents = NULL;
818 break;
819 default:
820 ASSERT(0);
821 }
822 /*
823 * Attribute fork settings for new inode.
824 */
825 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
826 ip->i_d.di_anextents = 0;
827
828 /*
829 * Log the new values stuffed into the inode.
830 */
Christoph Hellwigddc34152011-09-19 15:00:54 +0000831 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 xfs_trans_log_inode(tp, ip, flags);
833
Nathan Scottb83bd132006-06-09 16:48:30 +1000834 /* now that we have an i_mode we can setup inode ops and unlock */
Christoph Hellwig41be8be2008-08-13 16:23:13 +1000835 xfs_setup_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
David Chinnerbf904242008-10-30 17:36:14 +1100837 /* now we have set up the vfs inode we can associate the filestream */
838 if (filestreams) {
839 error = xfs_filestream_associate(pip, ip);
840 if (error < 0)
841 return -error;
842 if (!error)
843 xfs_iflags_set(ip, XFS_IFILESTREAM);
844 }
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 *ipp = ip;
847 return 0;
848}
849
Dave Chinnere546cb72013-08-12 20:49:47 +1000850/*
851 * Allocates a new inode from disk and return a pointer to the
852 * incore copy. This routine will internally commit the current
853 * transaction and allocate a new one if the Space Manager needed
854 * to do an allocation to replenish the inode free-list.
855 *
856 * This routine is designed to be called from xfs_create and
857 * xfs_create_dir.
858 *
859 */
860int
861xfs_dir_ialloc(
862 xfs_trans_t **tpp, /* input: current transaction;
863 output: may be a new transaction. */
864 xfs_inode_t *dp, /* directory within whose allocate
865 the inode. */
866 umode_t mode,
867 xfs_nlink_t nlink,
868 xfs_dev_t rdev,
869 prid_t prid, /* project id */
870 int okalloc, /* ok to allocate new space */
871 xfs_inode_t **ipp, /* pointer to inode; it will be
872 locked. */
873 int *committed)
874
875{
876 xfs_trans_t *tp;
877 xfs_trans_t *ntp;
878 xfs_inode_t *ip;
879 xfs_buf_t *ialloc_context = NULL;
880 int code;
Dave Chinnere546cb72013-08-12 20:49:47 +1000881 void *dqinfo;
882 uint tflags;
883
884 tp = *tpp;
885 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
886
887 /*
888 * xfs_ialloc will return a pointer to an incore inode if
889 * the Space Manager has an available inode on the free
890 * list. Otherwise, it will do an allocation and replenish
891 * the freelist. Since we can only do one allocation per
892 * transaction without deadlocks, we will need to commit the
893 * current transaction and start a new one. We will then
894 * need to call xfs_ialloc again to get the inode.
895 *
896 * If xfs_ialloc did an allocation to replenish the freelist,
897 * it returns the bp containing the head of the freelist as
898 * ialloc_context. We will hold a lock on it across the
899 * transaction commit so that no other process can steal
900 * the inode(s) that we've just allocated.
901 */
902 code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid, okalloc,
903 &ialloc_context, &ip);
904
905 /*
906 * Return an error if we were unable to allocate a new inode.
907 * This should only happen if we run out of space on disk or
908 * encounter a disk error.
909 */
910 if (code) {
911 *ipp = NULL;
912 return code;
913 }
914 if (!ialloc_context && !ip) {
915 *ipp = NULL;
916 return XFS_ERROR(ENOSPC);
917 }
918
919 /*
920 * If the AGI buffer is non-NULL, then we were unable to get an
921 * inode in one operation. We need to commit the current
922 * transaction and call xfs_ialloc() again. It is guaranteed
923 * to succeed the second time.
924 */
925 if (ialloc_context) {
Jie Liu3d3c8b52013-08-12 20:49:59 +1000926 struct xfs_trans_res tres;
927
Dave Chinnere546cb72013-08-12 20:49:47 +1000928 /*
929 * Normally, xfs_trans_commit releases all the locks.
930 * We call bhold to hang on to the ialloc_context across
931 * the commit. Holding this buffer prevents any other
932 * processes from doing any allocations in this
933 * allocation group.
934 */
935 xfs_trans_bhold(tp, ialloc_context);
936 /*
937 * Save the log reservation so we can use
938 * them in the next transaction.
939 */
Jie Liu3d3c8b52013-08-12 20:49:59 +1000940 tres.tr_logres = xfs_trans_get_log_res(tp);
941 tres.tr_logcount = xfs_trans_get_log_count(tp);
Dave Chinnere546cb72013-08-12 20:49:47 +1000942
943 /*
944 * We want the quota changes to be associated with the next
945 * transaction, NOT this one. So, detach the dqinfo from this
946 * and attach it to the next transaction.
947 */
948 dqinfo = NULL;
949 tflags = 0;
950 if (tp->t_dqinfo) {
951 dqinfo = (void *)tp->t_dqinfo;
952 tp->t_dqinfo = NULL;
953 tflags = tp->t_flags & XFS_TRANS_DQ_DIRTY;
954 tp->t_flags &= ~(XFS_TRANS_DQ_DIRTY);
955 }
956
957 ntp = xfs_trans_dup(tp);
958 code = xfs_trans_commit(tp, 0);
959 tp = ntp;
960 if (committed != NULL) {
961 *committed = 1;
962 }
963 /*
964 * If we get an error during the commit processing,
965 * release the buffer that is still held and return
966 * to the caller.
967 */
968 if (code) {
969 xfs_buf_relse(ialloc_context);
970 if (dqinfo) {
971 tp->t_dqinfo = dqinfo;
972 xfs_trans_free_dqinfo(tp);
973 }
974 *tpp = ntp;
975 *ipp = NULL;
976 return code;
977 }
978
979 /*
980 * transaction commit worked ok so we can drop the extra ticket
981 * reference that we gained in xfs_trans_dup()
982 */
983 xfs_log_ticket_put(tp->t_ticket);
Jie Liu3d3c8b52013-08-12 20:49:59 +1000984 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
985 code = xfs_trans_reserve(tp, &tres, 0, 0);
986
Dave Chinnere546cb72013-08-12 20:49:47 +1000987 /*
988 * Re-attach the quota info that we detached from prev trx.
989 */
990 if (dqinfo) {
991 tp->t_dqinfo = dqinfo;
992 tp->t_flags |= tflags;
993 }
994
995 if (code) {
996 xfs_buf_relse(ialloc_context);
997 *tpp = ntp;
998 *ipp = NULL;
999 return code;
1000 }
1001 xfs_trans_bjoin(tp, ialloc_context);
1002
1003 /*
1004 * Call ialloc again. Since we've locked out all
1005 * other allocations in this allocation group,
1006 * this call should always succeed.
1007 */
1008 code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid,
1009 okalloc, &ialloc_context, &ip);
1010
1011 /*
1012 * If we get an error at this point, return to the caller
1013 * so that the current transaction can be aborted.
1014 */
1015 if (code) {
1016 *tpp = tp;
1017 *ipp = NULL;
1018 return code;
1019 }
1020 ASSERT(!ialloc_context && ip);
1021
1022 } else {
1023 if (committed != NULL)
1024 *committed = 0;
1025 }
1026
1027 *ipp = ip;
1028 *tpp = tp;
1029
1030 return 0;
1031}
1032
1033/*
1034 * Decrement the link count on an inode & log the change.
1035 * If this causes the link count to go to zero, initiate the
1036 * logging activity required to truncate a file.
1037 */
1038int /* error */
1039xfs_droplink(
1040 xfs_trans_t *tp,
1041 xfs_inode_t *ip)
1042{
1043 int error;
1044
1045 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1046
1047 ASSERT (ip->i_d.di_nlink > 0);
1048 ip->i_d.di_nlink--;
1049 drop_nlink(VFS_I(ip));
1050 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1051
1052 error = 0;
1053 if (ip->i_d.di_nlink == 0) {
1054 /*
1055 * We're dropping the last link to this file.
1056 * Move the on-disk inode to the AGI unlinked list.
1057 * From xfs_inactive() we will pull the inode from
1058 * the list and free it.
1059 */
1060 error = xfs_iunlink(tp, ip);
1061 }
1062 return error;
1063}
1064
1065/*
1066 * This gets called when the inode's version needs to be changed from 1 to 2.
1067 * Currently this happens when the nlink field overflows the old 16-bit value
1068 * or when chproj is called to change the project for the first time.
1069 * As a side effect the superblock version will also get rev'd
1070 * to contain the NLINK bit.
1071 */
1072void
1073xfs_bump_ino_vers2(
1074 xfs_trans_t *tp,
1075 xfs_inode_t *ip)
1076{
1077 xfs_mount_t *mp;
1078
1079 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1080 ASSERT(ip->i_d.di_version == 1);
1081
1082 ip->i_d.di_version = 2;
1083 ip->i_d.di_onlink = 0;
1084 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
1085 mp = tp->t_mountp;
1086 if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
1087 spin_lock(&mp->m_sb_lock);
1088 if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
1089 xfs_sb_version_addnlink(&mp->m_sb);
1090 spin_unlock(&mp->m_sb_lock);
1091 xfs_mod_sb(tp, XFS_SB_VERSIONNUM);
1092 } else {
1093 spin_unlock(&mp->m_sb_lock);
1094 }
1095 }
1096 /* Caller must log the inode */
1097}
1098
1099/*
1100 * Increment the link count on an inode & log the change.
1101 */
1102int
1103xfs_bumplink(
1104 xfs_trans_t *tp,
1105 xfs_inode_t *ip)
1106{
1107 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1108
1109 ASSERT(ip->i_d.di_nlink > 0);
1110 ip->i_d.di_nlink++;
1111 inc_nlink(VFS_I(ip));
1112 if ((ip->i_d.di_version == 1) &&
1113 (ip->i_d.di_nlink > XFS_MAXLINK_1)) {
1114 /*
1115 * The inode has increased its number of links beyond
1116 * what can fit in an old format inode. It now needs
1117 * to be converted to a version 2 inode with a 32 bit
1118 * link count. If this is the first inode in the file
1119 * system to do this, then we need to bump the superblock
1120 * version number as well.
1121 */
1122 xfs_bump_ino_vers2(tp, ip);
1123 }
1124
1125 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1126 return 0;
1127}
1128
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001129int
1130xfs_create(
1131 xfs_inode_t *dp,
1132 struct xfs_name *name,
1133 umode_t mode,
1134 xfs_dev_t rdev,
1135 xfs_inode_t **ipp)
1136{
1137 int is_dir = S_ISDIR(mode);
1138 struct xfs_mount *mp = dp->i_mount;
1139 struct xfs_inode *ip = NULL;
1140 struct xfs_trans *tp = NULL;
1141 int error;
1142 xfs_bmap_free_t free_list;
1143 xfs_fsblock_t first_block;
1144 bool unlock_dp_on_error = false;
1145 uint cancel_flags;
1146 int committed;
1147 prid_t prid;
1148 struct xfs_dquot *udqp = NULL;
1149 struct xfs_dquot *gdqp = NULL;
1150 struct xfs_dquot *pdqp = NULL;
Jie Liu3d3c8b52013-08-12 20:49:59 +10001151 struct xfs_trans_res tres;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001152 uint resblks;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001153
1154 trace_xfs_create(dp, name);
1155
1156 if (XFS_FORCED_SHUTDOWN(mp))
1157 return XFS_ERROR(EIO);
1158
1159 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1160 prid = xfs_get_projid(dp);
1161 else
1162 prid = XFS_PROJID_DEFAULT;
1163
1164 /*
1165 * Make sure that we have allocated dquot(s) on disk.
1166 */
Dwight Engen7aab1b22013-08-15 14:08:01 -04001167 error = xfs_qm_vop_dqalloc(dp, xfs_kuid_to_uid(current_fsuid()),
1168 xfs_kgid_to_gid(current_fsgid()), prid,
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001169 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
1170 &udqp, &gdqp, &pdqp);
1171 if (error)
1172 return error;
1173
1174 if (is_dir) {
1175 rdev = 0;
1176 resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
Jie Liu3d3c8b52013-08-12 20:49:59 +10001177 tres.tr_logres = M_RES(mp)->tr_mkdir.tr_logres;
1178 tres.tr_logcount = XFS_MKDIR_LOG_COUNT;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001179 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
1180 } else {
1181 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
Jie Liu3d3c8b52013-08-12 20:49:59 +10001182 tres.tr_logres = M_RES(mp)->tr_create.tr_logres;
1183 tres.tr_logcount = XFS_CREATE_LOG_COUNT;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001184 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1185 }
1186
1187 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1188
1189 /*
1190 * Initially assume that the file does not exist and
1191 * reserve the resources for that case. If that is not
1192 * the case we'll drop the one we have and get a more
1193 * appropriate transaction later.
1194 */
Jie Liu3d3c8b52013-08-12 20:49:59 +10001195 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
1196 error = xfs_trans_reserve(tp, &tres, resblks, 0);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001197 if (error == ENOSPC) {
1198 /* flush outstanding delalloc blocks and retry */
1199 xfs_flush_inodes(mp);
Jie Liu3d3c8b52013-08-12 20:49:59 +10001200 error = xfs_trans_reserve(tp, &tres, resblks, 0);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001201 }
1202 if (error == ENOSPC) {
1203 /* No space at all so try a "no-allocation" reservation */
1204 resblks = 0;
Jie Liu3d3c8b52013-08-12 20:49:59 +10001205 error = xfs_trans_reserve(tp, &tres, 0, 0);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001206 }
1207 if (error) {
1208 cancel_flags = 0;
1209 goto out_trans_cancel;
1210 }
1211
1212 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
1213 unlock_dp_on_error = true;
1214
1215 xfs_bmap_init(&free_list, &first_block);
1216
1217 /*
1218 * Reserve disk quota and the inode.
1219 */
1220 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
1221 pdqp, resblks, 1, 0);
1222 if (error)
1223 goto out_trans_cancel;
1224
1225 error = xfs_dir_canenter(tp, dp, name, resblks);
1226 if (error)
1227 goto out_trans_cancel;
1228
1229 /*
1230 * A newly created regular or special file just has one directory
1231 * entry pointing to them, but a directory also the "." entry
1232 * pointing to itself.
1233 */
1234 error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev,
1235 prid, resblks > 0, &ip, &committed);
1236 if (error) {
1237 if (error == ENOSPC)
1238 goto out_trans_cancel;
1239 goto out_trans_abort;
1240 }
1241
1242 /*
1243 * Now we join the directory inode to the transaction. We do not do it
1244 * earlier because xfs_dir_ialloc might commit the previous transaction
1245 * (and release all the locks). An error from here on will result in
1246 * the transaction cancel unlocking dp so don't do it explicitly in the
1247 * error path.
1248 */
1249 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1250 unlock_dp_on_error = false;
1251
1252 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
1253 &first_block, &free_list, resblks ?
1254 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
1255 if (error) {
1256 ASSERT(error != ENOSPC);
1257 goto out_trans_abort;
1258 }
1259 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1260 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1261
1262 if (is_dir) {
1263 error = xfs_dir_init(tp, ip, dp);
1264 if (error)
1265 goto out_bmap_cancel;
1266
1267 error = xfs_bumplink(tp, dp);
1268 if (error)
1269 goto out_bmap_cancel;
1270 }
1271
1272 /*
1273 * If this is a synchronous mount, make sure that the
1274 * create transaction goes to disk before returning to
1275 * the user.
1276 */
1277 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
1278 xfs_trans_set_sync(tp);
1279
1280 /*
1281 * Attach the dquot(s) to the inodes and modify them incore.
1282 * These ids of the inode couldn't have changed since the new
1283 * inode has been locked ever since it was created.
1284 */
1285 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
1286
1287 error = xfs_bmap_finish(&tp, &free_list, &committed);
1288 if (error)
1289 goto out_bmap_cancel;
1290
1291 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1292 if (error)
1293 goto out_release_inode;
1294
1295 xfs_qm_dqrele(udqp);
1296 xfs_qm_dqrele(gdqp);
1297 xfs_qm_dqrele(pdqp);
1298
1299 *ipp = ip;
1300 return 0;
1301
1302 out_bmap_cancel:
1303 xfs_bmap_cancel(&free_list);
1304 out_trans_abort:
1305 cancel_flags |= XFS_TRANS_ABORT;
1306 out_trans_cancel:
1307 xfs_trans_cancel(tp, cancel_flags);
1308 out_release_inode:
1309 /*
1310 * Wait until after the current transaction is aborted to
1311 * release the inode. This prevents recursive transactions
1312 * and deadlocks from xfs_inactive.
1313 */
1314 if (ip)
1315 IRELE(ip);
1316
1317 xfs_qm_dqrele(udqp);
1318 xfs_qm_dqrele(gdqp);
1319 xfs_qm_dqrele(pdqp);
1320
1321 if (unlock_dp_on_error)
1322 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1323 return error;
1324}
1325
1326int
1327xfs_link(
1328 xfs_inode_t *tdp,
1329 xfs_inode_t *sip,
1330 struct xfs_name *target_name)
1331{
1332 xfs_mount_t *mp = tdp->i_mount;
1333 xfs_trans_t *tp;
1334 int error;
1335 xfs_bmap_free_t free_list;
1336 xfs_fsblock_t first_block;
1337 int cancel_flags;
1338 int committed;
1339 int resblks;
1340
1341 trace_xfs_link(tdp, target_name);
1342
1343 ASSERT(!S_ISDIR(sip->i_d.di_mode));
1344
1345 if (XFS_FORCED_SHUTDOWN(mp))
1346 return XFS_ERROR(EIO);
1347
1348 error = xfs_qm_dqattach(sip, 0);
1349 if (error)
1350 goto std_return;
1351
1352 error = xfs_qm_dqattach(tdp, 0);
1353 if (error)
1354 goto std_return;
1355
1356 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
1357 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1358 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
Jie Liu3d3c8b52013-08-12 20:49:59 +10001359 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_link, resblks, 0);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001360 if (error == ENOSPC) {
1361 resblks = 0;
Jie Liu3d3c8b52013-08-12 20:49:59 +10001362 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_link, 0, 0);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001363 }
1364 if (error) {
1365 cancel_flags = 0;
1366 goto error_return;
1367 }
1368
1369 xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
1370
1371 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
1372 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
1373
1374 /*
1375 * If we are using project inheritance, we only allow hard link
1376 * creation in our tree when the project IDs are the same; else
1377 * the tree quota mechanism could be circumvented.
1378 */
1379 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
1380 (xfs_get_projid(tdp) != xfs_get_projid(sip)))) {
1381 error = XFS_ERROR(EXDEV);
1382 goto error_return;
1383 }
1384
1385 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
1386 if (error)
1387 goto error_return;
1388
1389 xfs_bmap_init(&free_list, &first_block);
1390
1391 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
1392 &first_block, &free_list, resblks);
1393 if (error)
1394 goto abort_return;
1395 xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1396 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
1397
1398 error = xfs_bumplink(tp, sip);
1399 if (error)
1400 goto abort_return;
1401
1402 /*
1403 * If this is a synchronous mount, make sure that the
1404 * link transaction goes to disk before returning to
1405 * the user.
1406 */
1407 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1408 xfs_trans_set_sync(tp);
1409 }
1410
1411 error = xfs_bmap_finish (&tp, &free_list, &committed);
1412 if (error) {
1413 xfs_bmap_cancel(&free_list);
1414 goto abort_return;
1415 }
1416
1417 return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1418
1419 abort_return:
1420 cancel_flags |= XFS_TRANS_ABORT;
1421 error_return:
1422 xfs_trans_cancel(tp, cancel_flags);
1423 std_return:
1424 return error;
1425}
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427/*
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001428 * Free up the underlying blocks past new_size. The new size must be smaller
1429 * than the current size. This routine can be used both for the attribute and
1430 * data fork, and does not modify the inode size, which is left to the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 *
David Chinnerf6485052008-04-17 16:50:04 +10001432 * The transaction passed to this routine must have made a permanent log
1433 * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the
1434 * given transaction and start new ones, so make sure everything involved in
1435 * the transaction is tidy before calling here. Some transaction will be
1436 * returned to the caller to be committed. The incoming transaction must
1437 * already include the inode, and both inode locks must be held exclusively.
1438 * The inode must also be "held" within the transaction. On return the inode
1439 * will be "held" within the returned transaction. This routine does NOT
1440 * require any disk space to be reserved for it within the transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 *
David Chinnerf6485052008-04-17 16:50:04 +10001442 * If we get an error, we must return with the inode locked and linked into the
1443 * current transaction. This keeps things simple for the higher level code,
1444 * because it always knows that the inode is locked and held in the transaction
1445 * that returns to it whether errors occur or not. We don't mark the inode
1446 * dirty on error so that transactions can be easily aborted if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 */
1448int
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001449xfs_itruncate_extents(
1450 struct xfs_trans **tpp,
1451 struct xfs_inode *ip,
1452 int whichfork,
1453 xfs_fsize_t new_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454{
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001455 struct xfs_mount *mp = ip->i_mount;
1456 struct xfs_trans *tp = *tpp;
1457 struct xfs_trans *ntp;
1458 xfs_bmap_free_t free_list;
1459 xfs_fsblock_t first_block;
1460 xfs_fileoff_t first_unmap_block;
1461 xfs_fileoff_t last_block;
1462 xfs_filblks_t unmap_len;
1463 int committed;
1464 int error = 0;
1465 int done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Christoph Hellwig0b561852012-07-04 11:13:31 -04001467 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1468 ASSERT(!atomic_read(&VFS_I(ip)->i_count) ||
1469 xfs_isilocked(ip, XFS_IOLOCK_EXCL));
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00001470 ASSERT(new_size <= XFS_ISIZE(ip));
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001471 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 ASSERT(ip->i_itemp != NULL);
Christoph Hellwig898621d2010-06-24 11:36:58 +10001473 ASSERT(ip->i_itemp->ili_lock_flags == 0);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001474 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Christoph Hellwig673e8e52011-12-18 20:00:04 +00001476 trace_xfs_itruncate_extents_start(ip, new_size);
1477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 /*
1479 * Since it is possible for space to become allocated beyond
1480 * the end of the file (in a crash where the space is allocated
1481 * but the inode size is not yet updated), simply remove any
1482 * blocks which show up between the new EOF and the maximum
1483 * possible file size. If the first block to be removed is
1484 * beyond the maximum file size (ie it is the same as last_block),
1485 * then there is nothing to do.
1486 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001487 first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
Dave Chinner32972382012-06-08 15:44:54 +10001488 last_block = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001489 if (first_unmap_block == last_block)
1490 return 0;
1491
1492 ASSERT(first_unmap_block < last_block);
1493 unmap_len = last_block - first_unmap_block + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 while (!done) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001495 xfs_bmap_init(&free_list, &first_block);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001496 error = xfs_bunmapi(tp, ip,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001497 first_unmap_block, unmap_len,
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001498 xfs_bmapi_aflag(whichfork),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 XFS_ITRUNC_MAX_EXTENTS,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001500 &first_block, &free_list,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10001501 &done);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001502 if (error)
1503 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
1505 /*
1506 * Duplicate the transaction that has the permanent
1507 * reservation and commit the old transaction.
1508 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001509 error = xfs_bmap_finish(&tp, &free_list, &committed);
Christoph Hellwig898621d2010-06-24 11:36:58 +10001510 if (committed)
Christoph Hellwigddc34152011-09-19 15:00:54 +00001511 xfs_trans_ijoin(tp, ip, 0);
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001512 if (error)
1513 goto out_bmap_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
1515 if (committed) {
1516 /*
David Chinnerf6485052008-04-17 16:50:04 +10001517 * Mark the inode dirty so it will be logged and
David Chinnere5720ee2008-04-10 12:21:18 +10001518 * moved forward in the log as part of every commit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001520 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 }
David Chinnerf6485052008-04-17 16:50:04 +10001522
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001523 ntp = xfs_trans_dup(tp);
1524 error = xfs_trans_commit(tp, 0);
1525 tp = ntp;
David Chinnere5720ee2008-04-10 12:21:18 +10001526
Christoph Hellwigddc34152011-09-19 15:00:54 +00001527 xfs_trans_ijoin(tp, ip, 0);
David Chinnerf6485052008-04-17 16:50:04 +10001528
Dave Chinnercc09c0d2008-11-17 17:37:10 +11001529 if (error)
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001530 goto out;
1531
Dave Chinnercc09c0d2008-11-17 17:37:10 +11001532 /*
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001533 * Transaction commit worked ok so we can drop the extra ticket
Dave Chinnercc09c0d2008-11-17 17:37:10 +11001534 * reference that we gained in xfs_trans_dup()
1535 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001536 xfs_log_ticket_put(tp->t_ticket);
Jie Liu3d3c8b52013-08-12 20:49:59 +10001537 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0);
David Chinnerf6485052008-04-17 16:50:04 +10001538 if (error)
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001539 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 }
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001541
Christoph Hellwig673e8e52011-12-18 20:00:04 +00001542 /*
1543 * Always re-log the inode so that our permanent transaction can keep
1544 * on rolling it forward in the log.
1545 */
1546 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1547
1548 trace_xfs_itruncate_extents_end(ip, new_size);
1549
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001550out:
1551 *tpp = tp;
1552 return error;
1553out_bmap_cancel:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 /*
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001555 * If the bunmapi call encounters an error, return to the caller where
1556 * the transaction can be properly aborted. We just need to make sure
1557 * we're not holding any resources that we were not when we came in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 */
Christoph Hellwig8f04c472011-07-08 14:34:34 +02001559 xfs_bmap_cancel(&free_list);
1560 goto out;
1561}
1562
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001563int
1564xfs_release(
1565 xfs_inode_t *ip)
1566{
1567 xfs_mount_t *mp = ip->i_mount;
1568 int error;
1569
1570 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
1571 return 0;
1572
1573 /* If this is a read-only mount, don't do this (would generate I/O) */
1574 if (mp->m_flags & XFS_MOUNT_RDONLY)
1575 return 0;
1576
1577 if (!XFS_FORCED_SHUTDOWN(mp)) {
1578 int truncated;
1579
1580 /*
1581 * If we are using filestreams, and we have an unlinked
1582 * file that we are processing the last close on, then nothing
1583 * will be able to reopen and write to this file. Purge this
1584 * inode from the filestreams cache so that it doesn't delay
1585 * teardown of the inode.
1586 */
1587 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
1588 xfs_filestream_deassociate(ip);
1589
1590 /*
1591 * If we previously truncated this file and removed old data
1592 * in the process, we want to initiate "early" writeout on
1593 * the last close. This is an attempt to combat the notorious
1594 * NULL files problem which is particularly noticeable from a
1595 * truncate down, buffered (re-)write (delalloc), followed by
1596 * a crash. What we are effectively doing here is
1597 * significantly reducing the time window where we'd otherwise
1598 * be exposed to that problem.
1599 */
1600 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
1601 if (truncated) {
1602 xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE);
1603 if (VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0) {
1604 error = -filemap_flush(VFS_I(ip)->i_mapping);
1605 if (error)
1606 return error;
1607 }
1608 }
1609 }
1610
1611 if (ip->i_d.di_nlink == 0)
1612 return 0;
1613
1614 if (xfs_can_free_eofblocks(ip, false)) {
1615
1616 /*
1617 * If we can't get the iolock just skip truncating the blocks
1618 * past EOF because we could deadlock with the mmap_sem
1619 * otherwise. We'll get another chance to drop them once the
1620 * last reference to the inode is dropped, so we'll never leak
1621 * blocks permanently.
1622 *
1623 * Further, check if the inode is being opened, written and
1624 * closed frequently and we have delayed allocation blocks
1625 * outstanding (e.g. streaming writes from the NFS server),
1626 * truncating the blocks past EOF will cause fragmentation to
1627 * occur.
1628 *
1629 * In this case don't do the truncation, either, but we have to
1630 * be careful how we detect this case. Blocks beyond EOF show
1631 * up as i_delayed_blks even when the inode is clean, so we
1632 * need to truncate them away first before checking for a dirty
1633 * release. Hence on the first dirty close we will still remove
1634 * the speculative allocation, but after that we will leave it
1635 * in place.
1636 */
1637 if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE))
1638 return 0;
1639
1640 error = xfs_free_eofblocks(mp, ip, true);
1641 if (error && error != EAGAIN)
1642 return error;
1643
1644 /* delalloc blocks after truncation means it really is dirty */
1645 if (ip->i_delayed_blks)
1646 xfs_iflags_set(ip, XFS_IDIRTY_RELEASE);
1647 }
1648 return 0;
1649}
1650
1651/*
Brian Fosterf7be2d72013-09-20 11:06:10 -04001652 * xfs_inactive_truncate
1653 *
1654 * Called to perform a truncate when an inode becomes unlinked.
1655 */
1656STATIC int
1657xfs_inactive_truncate(
1658 struct xfs_inode *ip)
1659{
1660 struct xfs_mount *mp = ip->i_mount;
1661 struct xfs_trans *tp;
1662 int error;
1663
1664 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1665 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0);
1666 if (error) {
1667 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1668 xfs_trans_cancel(tp, 0);
1669 return error;
1670 }
1671
1672 xfs_ilock(ip, XFS_ILOCK_EXCL);
1673 xfs_trans_ijoin(tp, ip, 0);
1674
1675 /*
1676 * Log the inode size first to prevent stale data exposure in the event
1677 * of a system crash before the truncate completes. See the related
1678 * comment in xfs_setattr_size() for details.
1679 */
1680 ip->i_d.di_size = 0;
1681 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1682
1683 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
1684 if (error)
1685 goto error_trans_cancel;
1686
1687 ASSERT(ip->i_d.di_nextents == 0);
1688
1689 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1690 if (error)
1691 goto error_unlock;
1692
1693 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1694 return 0;
1695
1696error_trans_cancel:
1697 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1698error_unlock:
1699 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1700 return error;
1701}
1702
1703/*
Brian Foster88877d22013-09-20 11:06:11 -04001704 * xfs_inactive_ifree()
1705 *
1706 * Perform the inode free when an inode is unlinked.
1707 */
1708STATIC int
1709xfs_inactive_ifree(
1710 struct xfs_inode *ip)
1711{
1712 xfs_bmap_free_t free_list;
1713 xfs_fsblock_t first_block;
1714 int committed;
1715 struct xfs_mount *mp = ip->i_mount;
1716 struct xfs_trans *tp;
1717 int error;
1718
1719 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1720 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_ifree, 0, 0);
1721 if (error) {
1722 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1723 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
1724 return error;
1725 }
1726
1727 xfs_ilock(ip, XFS_ILOCK_EXCL);
1728 xfs_trans_ijoin(tp, ip, 0);
1729
1730 xfs_bmap_init(&free_list, &first_block);
1731 error = xfs_ifree(tp, ip, &free_list);
1732 if (error) {
1733 /*
1734 * If we fail to free the inode, shut down. The cancel
1735 * might do that, we need to make sure. Otherwise the
1736 * inode might be lost for a long time or forever.
1737 */
1738 if (!XFS_FORCED_SHUTDOWN(mp)) {
1739 xfs_notice(mp, "%s: xfs_ifree returned error %d",
1740 __func__, error);
1741 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1742 }
1743 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1744 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1745 return error;
1746 }
1747
1748 /*
1749 * Credit the quota account(s). The inode is gone.
1750 */
1751 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1752
1753 /*
1754 * Just ignore errors at this point. There is nothing we can
1755 * do except to try to keep going. Make sure it's not a silent
1756 * error.
1757 */
1758 error = xfs_bmap_finish(&tp, &free_list, &committed);
1759 if (error)
1760 xfs_notice(mp, "%s: xfs_bmap_finish returned error %d",
1761 __func__, error);
1762 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1763 if (error)
1764 xfs_notice(mp, "%s: xfs_trans_commit returned error %d",
1765 __func__, error);
1766
1767 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1768 return 0;
1769}
1770
1771/*
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001772 * xfs_inactive
1773 *
1774 * This is called when the vnode reference count for the vnode
1775 * goes to zero. If the file has been unlinked, then it must
1776 * now be truncated. Also, we clear all of the read-ahead state
1777 * kept for the inode here since the file is now closed.
1778 */
Brian Foster74564fb2013-09-20 11:06:12 -04001779void
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001780xfs_inactive(
1781 xfs_inode_t *ip)
1782{
Jie Liu3d3c8b52013-08-12 20:49:59 +10001783 struct xfs_mount *mp;
Jie Liu3d3c8b52013-08-12 20:49:59 +10001784 int error;
1785 int truncate = 0;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001786
1787 /*
1788 * If the inode is already free, then there can be nothing
1789 * to clean up here.
1790 */
Ben Myersd9487092013-09-10 18:11:22 -05001791 if (ip->i_d.di_mode == 0) {
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001792 ASSERT(ip->i_df.if_real_bytes == 0);
1793 ASSERT(ip->i_df.if_broot_bytes == 0);
Brian Foster74564fb2013-09-20 11:06:12 -04001794 return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001795 }
1796
1797 mp = ip->i_mount;
1798
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001799 /* If this is a read-only mount, don't do this (would generate I/O) */
1800 if (mp->m_flags & XFS_MOUNT_RDONLY)
Brian Foster74564fb2013-09-20 11:06:12 -04001801 return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001802
1803 if (ip->i_d.di_nlink != 0) {
1804 /*
1805 * force is true because we are evicting an inode from the
1806 * cache. Post-eof blocks must be freed, lest we end up with
1807 * broken free space accounting.
1808 */
Brian Foster74564fb2013-09-20 11:06:12 -04001809 if (xfs_can_free_eofblocks(ip, true))
1810 xfs_free_eofblocks(mp, ip, false);
1811
1812 return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001813 }
1814
1815 if (S_ISREG(ip->i_d.di_mode) &&
1816 (ip->i_d.di_size != 0 || XFS_ISIZE(ip) != 0 ||
1817 ip->i_d.di_nextents > 0 || ip->i_delayed_blks > 0))
1818 truncate = 1;
1819
1820 error = xfs_qm_dqattach(ip, 0);
1821 if (error)
Brian Foster74564fb2013-09-20 11:06:12 -04001822 return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001823
Brian Fosterf7be2d72013-09-20 11:06:10 -04001824 if (S_ISLNK(ip->i_d.di_mode))
Brian Foster36b21dd2013-09-20 11:06:09 -04001825 error = xfs_inactive_symlink(ip);
Brian Fosterf7be2d72013-09-20 11:06:10 -04001826 else if (truncate)
1827 error = xfs_inactive_truncate(ip);
1828 if (error)
Brian Foster74564fb2013-09-20 11:06:12 -04001829 return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001830
1831 /*
1832 * If there are attributes associated with the file then blow them away
1833 * now. The code calls a routine that recursively deconstructs the
1834 * attribute fork. We need to just commit the current transaction
1835 * because we can't use it for xfs_attr_inactive().
1836 */
1837 if (ip->i_d.di_anextents > 0) {
1838 ASSERT(ip->i_d.di_forkoff != 0);
1839
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001840 error = xfs_attr_inactive(ip);
1841 if (error)
Brian Foster74564fb2013-09-20 11:06:12 -04001842 return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001843 }
1844
1845 if (ip->i_afp)
1846 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1847
1848 ASSERT(ip->i_d.di_anextents == 0);
1849
1850 /*
1851 * Free the inode.
1852 */
Brian Foster88877d22013-09-20 11:06:11 -04001853 error = xfs_inactive_ifree(ip);
1854 if (error)
Brian Foster74564fb2013-09-20 11:06:12 -04001855 return;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001856
1857 /*
1858 * Release the dquots held by inode, if any.
1859 */
1860 xfs_qm_dqdetach(ip);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001861}
1862
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863/*
1864 * This is called when the inode's link count goes to 0.
1865 * We place the on-disk inode on a list in the AGI. It
1866 * will be pulled from this list when the inode is freed.
1867 */
1868int
1869xfs_iunlink(
1870 xfs_trans_t *tp,
1871 xfs_inode_t *ip)
1872{
1873 xfs_mount_t *mp;
1874 xfs_agi_t *agi;
1875 xfs_dinode_t *dip;
1876 xfs_buf_t *agibp;
1877 xfs_buf_t *ibp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 xfs_agino_t agino;
1879 short bucket_index;
1880 int offset;
1881 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
1883 ASSERT(ip->i_d.di_nlink == 0);
1884 ASSERT(ip->i_d.di_mode != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
1886 mp = tp->t_mountp;
1887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 /*
1889 * Get the agi buffer first. It ensures lock ordering
1890 * on the list.
1891 */
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001892 error = xfs_read_agi(mp, tp, XFS_INO_TO_AGNO(mp, ip->i_ino), &agibp);
Vlad Apostolov859d7182007-10-11 17:44:18 +10001893 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 agi = XFS_BUF_TO_AGI(agibp);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001896
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 /*
1898 * Get the index into the agi hash table for the
1899 * list this inode will go on.
1900 */
1901 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
1902 ASSERT(agino != 0);
1903 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
1904 ASSERT(agi->agi_unlinked[bucket_index]);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001905 ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001907 if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 /*
1909 * There is already another inode in the bucket we need
1910 * to add ourselves to. Add us at the front of the list.
1911 * Here we put the head pointer into our next pointer,
1912 * and then we fall through to point the head at us.
1913 */
Christoph Hellwig475ee412012-07-03 12:21:22 -04001914 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
1915 0, 0);
Vlad Apostolovc319b582007-11-23 16:27:51 +11001916 if (error)
1917 return error;
1918
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001919 ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 dip->di_next_unlinked = agi->agi_unlinked[bucket_index];
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11001921 offset = ip->i_imap.im_boffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 offsetof(xfs_dinode_t, di_next_unlinked);
Dave Chinner0a32c262013-06-05 12:09:08 +10001923
1924 /* need to recalc the inode CRC if appropriate */
1925 xfs_dinode_calc_crc(mp, dip);
1926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 xfs_trans_inode_buf(tp, ibp);
1928 xfs_trans_log_buf(tp, ibp, offset,
1929 (offset + sizeof(xfs_agino_t) - 1));
1930 xfs_inobp_check(mp, ibp);
1931 }
1932
1933 /*
1934 * Point the bucket head pointer at the inode being inserted.
1935 */
1936 ASSERT(agino != 0);
Christoph Hellwig16259e72005-11-02 15:11:25 +11001937 agi->agi_unlinked[bucket_index] = cpu_to_be32(agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 offset = offsetof(xfs_agi_t, agi_unlinked) +
1939 (sizeof(xfs_agino_t) * bucket_index);
1940 xfs_trans_log_buf(tp, agibp, offset,
1941 (offset + sizeof(xfs_agino_t) - 1));
1942 return 0;
1943}
1944
1945/*
1946 * Pull the on-disk inode from the AGI unlinked list.
1947 */
1948STATIC int
1949xfs_iunlink_remove(
1950 xfs_trans_t *tp,
1951 xfs_inode_t *ip)
1952{
1953 xfs_ino_t next_ino;
1954 xfs_mount_t *mp;
1955 xfs_agi_t *agi;
1956 xfs_dinode_t *dip;
1957 xfs_buf_t *agibp;
1958 xfs_buf_t *ibp;
1959 xfs_agnumber_t agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 xfs_agino_t agino;
1961 xfs_agino_t next_agino;
1962 xfs_buf_t *last_ibp;
Nathan Scott6fdf8cc2006-06-28 10:13:52 +10001963 xfs_dinode_t *last_dip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 short bucket_index;
Nathan Scott6fdf8cc2006-06-28 10:13:52 +10001965 int offset, last_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
1971 /*
1972 * Get the agi buffer first. It ensures lock ordering
1973 * on the list.
1974 */
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001975 error = xfs_read_agi(mp, tp, agno, &agibp);
1976 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 return error;
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001978
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 agi = XFS_BUF_TO_AGI(agibp);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11001980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 /*
1982 * Get the index into the agi hash table for the
1983 * list this inode will go on.
1984 */
1985 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
1986 ASSERT(agino != 0);
1987 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001988 ASSERT(agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 ASSERT(agi->agi_unlinked[bucket_index]);
1990
Christoph Hellwig16259e72005-11-02 15:11:25 +11001991 if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 /*
Christoph Hellwig475ee412012-07-03 12:21:22 -04001993 * We're at the head of the list. Get the inode's on-disk
1994 * buffer to see if there is anyone after us on the list.
1995 * Only modify our next pointer if it is not already NULLAGINO.
1996 * This saves us the overhead of dealing with the buffer when
1997 * there is no need to change it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 */
Christoph Hellwig475ee412012-07-03 12:21:22 -04001999 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
2000 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 if (error) {
Christoph Hellwig475ee412012-07-03 12:21:22 -04002002 xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.",
Dave Chinner0b932cc2011-03-07 10:08:35 +11002003 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 return error;
2005 }
Christoph Hellwig347d1c02007-08-28 13:57:51 +10002006 next_agino = be32_to_cpu(dip->di_next_unlinked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 ASSERT(next_agino != 0);
2008 if (next_agino != NULLAGINO) {
Christoph Hellwig347d1c02007-08-28 13:57:51 +10002009 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11002010 offset = ip->i_imap.im_boffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 offsetof(xfs_dinode_t, di_next_unlinked);
Dave Chinner0a32c262013-06-05 12:09:08 +10002012
2013 /* need to recalc the inode CRC if appropriate */
2014 xfs_dinode_calc_crc(mp, dip);
2015
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 xfs_trans_inode_buf(tp, ibp);
2017 xfs_trans_log_buf(tp, ibp, offset,
2018 (offset + sizeof(xfs_agino_t) - 1));
2019 xfs_inobp_check(mp, ibp);
2020 } else {
2021 xfs_trans_brelse(tp, ibp);
2022 }
2023 /*
2024 * Point the bucket head pointer at the next inode.
2025 */
2026 ASSERT(next_agino != 0);
2027 ASSERT(next_agino != agino);
Christoph Hellwig16259e72005-11-02 15:11:25 +11002028 agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 offset = offsetof(xfs_agi_t, agi_unlinked) +
2030 (sizeof(xfs_agino_t) * bucket_index);
2031 xfs_trans_log_buf(tp, agibp, offset,
2032 (offset + sizeof(xfs_agino_t) - 1));
2033 } else {
2034 /*
2035 * We need to search the list for the inode being freed.
2036 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11002037 next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 last_ibp = NULL;
2039 while (next_agino != agino) {
Christoph Hellwig129dbc92012-07-03 12:21:51 -04002040 struct xfs_imap imap;
2041
2042 if (last_ibp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 xfs_trans_brelse(tp, last_ibp);
Christoph Hellwig129dbc92012-07-03 12:21:51 -04002044
2045 imap.im_blkno = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino);
Christoph Hellwig129dbc92012-07-03 12:21:51 -04002047
2048 error = xfs_imap(mp, tp, next_ino, &imap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 if (error) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11002050 xfs_warn(mp,
Christoph Hellwig129dbc92012-07-03 12:21:51 -04002051 "%s: xfs_imap returned error %d.",
2052 __func__, error);
2053 return error;
2054 }
2055
2056 error = xfs_imap_to_bp(mp, tp, &imap, &last_dip,
2057 &last_ibp, 0, 0);
2058 if (error) {
2059 xfs_warn(mp,
2060 "%s: xfs_imap_to_bp returned error %d.",
Dave Chinner0b932cc2011-03-07 10:08:35 +11002061 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 return error;
2063 }
Christoph Hellwig129dbc92012-07-03 12:21:51 -04002064
2065 last_offset = imap.im_boffset;
Christoph Hellwig347d1c02007-08-28 13:57:51 +10002066 next_agino = be32_to_cpu(last_dip->di_next_unlinked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 ASSERT(next_agino != NULLAGINO);
2068 ASSERT(next_agino != 0);
2069 }
Christoph Hellwig475ee412012-07-03 12:21:22 -04002070
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 /*
Christoph Hellwig475ee412012-07-03 12:21:22 -04002072 * Now last_ibp points to the buffer previous to us on the
2073 * unlinked list. Pull us from the list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 */
Christoph Hellwig475ee412012-07-03 12:21:22 -04002075 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
2076 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 if (error) {
Christoph Hellwig475ee412012-07-03 12:21:22 -04002078 xfs_warn(mp, "%s: xfs_imap_to_bp(2) returned error %d.",
Dave Chinner0b932cc2011-03-07 10:08:35 +11002079 __func__, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 return error;
2081 }
Christoph Hellwig347d1c02007-08-28 13:57:51 +10002082 next_agino = be32_to_cpu(dip->di_next_unlinked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 ASSERT(next_agino != 0);
2084 ASSERT(next_agino != agino);
2085 if (next_agino != NULLAGINO) {
Christoph Hellwig347d1c02007-08-28 13:57:51 +10002086 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11002087 offset = ip->i_imap.im_boffset +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 offsetof(xfs_dinode_t, di_next_unlinked);
Dave Chinner0a32c262013-06-05 12:09:08 +10002089
2090 /* need to recalc the inode CRC if appropriate */
2091 xfs_dinode_calc_crc(mp, dip);
2092
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 xfs_trans_inode_buf(tp, ibp);
2094 xfs_trans_log_buf(tp, ibp, offset,
2095 (offset + sizeof(xfs_agino_t) - 1));
2096 xfs_inobp_check(mp, ibp);
2097 } else {
2098 xfs_trans_brelse(tp, ibp);
2099 }
2100 /*
2101 * Point the previous inode on the list to the next inode.
2102 */
Christoph Hellwig347d1c02007-08-28 13:57:51 +10002103 last_dip->di_next_unlinked = cpu_to_be32(next_agino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 ASSERT(next_agino != 0);
2105 offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked);
Dave Chinner0a32c262013-06-05 12:09:08 +10002106
2107 /* need to recalc the inode CRC if appropriate */
2108 xfs_dinode_calc_crc(mp, last_dip);
2109
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 xfs_trans_inode_buf(tp, last_ibp);
2111 xfs_trans_log_buf(tp, last_ibp, offset,
2112 (offset + sizeof(xfs_agino_t) - 1));
2113 xfs_inobp_check(mp, last_ibp);
2114 }
2115 return 0;
2116}
2117
Dave Chinner5b3eed72010-08-24 11:42:41 +10002118/*
Zhi Yong Wu0b8182d2013-08-12 03:14:59 +00002119 * A big issue when freeing the inode cluster is that we _cannot_ skip any
Dave Chinner5b3eed72010-08-24 11:42:41 +10002120 * inodes that are in memory - they all must be marked stale and attached to
2121 * the cluster buffer.
2122 */
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00002123STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124xfs_ifree_cluster(
2125 xfs_inode_t *free_ip,
2126 xfs_trans_t *tp,
2127 xfs_ino_t inum)
2128{
2129 xfs_mount_t *mp = free_ip->i_mount;
2130 int blks_per_cluster;
2131 int nbufs;
2132 int ninodes;
Dave Chinner5b257b42010-06-03 16:22:29 +10002133 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 xfs_daddr_t blkno;
2135 xfs_buf_t *bp;
Dave Chinner5b257b42010-06-03 16:22:29 +10002136 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 xfs_inode_log_item_t *iip;
2138 xfs_log_item_t *lip;
Dave Chinner5017e972010-01-11 11:47:40 +00002139 struct xfs_perag *pag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
Dave Chinner5017e972010-01-11 11:47:40 +00002141 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
2143 blks_per_cluster = 1;
2144 ninodes = mp->m_sb.sb_inopblock;
2145 nbufs = XFS_IALLOC_BLOCKS(mp);
2146 } else {
2147 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
2148 mp->m_sb.sb_blocksize;
2149 ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
2150 nbufs = XFS_IALLOC_BLOCKS(mp) / blks_per_cluster;
2151 }
2152
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 for (j = 0; j < nbufs; j++, inum += ninodes) {
2154 blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
2155 XFS_INO_TO_AGBNO(mp, inum));
2156
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 /*
Dave Chinner5b257b42010-06-03 16:22:29 +10002158 * We obtain and lock the backing buffer first in the process
2159 * here, as we have to ensure that any dirty inode that we
2160 * can't get the flush lock on is attached to the buffer.
2161 * If we scan the in-memory inodes first, then buffer IO can
2162 * complete before we get a lock on it, and hence we may fail
2163 * to mark all the active inodes on the buffer stale.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 */
Dave Chinner5b257b42010-06-03 16:22:29 +10002165 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
Dave Chinnerb6aff292012-11-02 11:38:42 +11002166 mp->m_bsize * blks_per_cluster,
2167 XBF_UNMAPPED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00002169 if (!bp)
2170 return ENOMEM;
Dave Chinnerb0f539d2012-11-14 17:53:49 +11002171
2172 /*
2173 * This buffer may not have been correctly initialised as we
2174 * didn't read it from disk. That's not important because we are
2175 * only using to mark the buffer as stale in the log, and to
2176 * attach stale cached inodes on it. That means it will never be
2177 * dispatched for IO. If it is, we want to know about it, and we
2178 * want it to fail. We can acheive this by adding a write
2179 * verifier to the buffer.
2180 */
Dave Chinner1813dd62012-11-14 17:54:40 +11002181 bp->b_ops = &xfs_inode_buf_ops;
Dave Chinnerb0f539d2012-11-14 17:53:49 +11002182
Dave Chinner5b257b42010-06-03 16:22:29 +10002183 /*
2184 * Walk the inodes already attached to the buffer and mark them
2185 * stale. These will all have the flush locks held, so an
Dave Chinner5b3eed72010-08-24 11:42:41 +10002186 * in-memory inode walk can't lock them. By marking them all
2187 * stale first, we will not attempt to lock them in the loop
2188 * below as the XFS_ISTALE flag will be set.
Dave Chinner5b257b42010-06-03 16:22:29 +10002189 */
Christoph Hellwigadadbee2011-07-13 13:43:49 +02002190 lip = bp->b_fspriv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 while (lip) {
2192 if (lip->li_type == XFS_LI_INODE) {
2193 iip = (xfs_inode_log_item_t *)lip;
2194 ASSERT(iip->ili_logged == 1);
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10002195 lip->li_cb = xfs_istale_done;
David Chinner7b2e2a32008-10-30 17:39:12 +11002196 xfs_trans_ail_copy_lsn(mp->m_ail,
2197 &iip->ili_flush_lsn,
2198 &iip->ili_item.li_lsn);
David Chinnere5ffd2b2006-11-21 18:55:33 +11002199 xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 }
2201 lip = lip->li_bio_list;
2202 }
2203
Dave Chinner5b3eed72010-08-24 11:42:41 +10002204
Dave Chinner5b257b42010-06-03 16:22:29 +10002205 /*
2206 * For each inode in memory attempt to add it to the inode
2207 * buffer and set it up for being staled on buffer IO
2208 * completion. This is safe as we've locked out tail pushing
2209 * and flushing by locking the buffer.
2210 *
2211 * We have already marked every inode that was part of a
2212 * transaction stale above, which means there is no point in
2213 * even trying to lock them.
2214 */
2215 for (i = 0; i < ninodes; i++) {
Dave Chinner5b3eed72010-08-24 11:42:41 +10002216retry:
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002217 rcu_read_lock();
Dave Chinner5b257b42010-06-03 16:22:29 +10002218 ip = radix_tree_lookup(&pag->pag_ici_root,
2219 XFS_INO_TO_AGINO(mp, (inum + i)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002221 /* Inode not in memory, nothing to do */
2222 if (!ip) {
2223 rcu_read_unlock();
Dave Chinner5b257b42010-06-03 16:22:29 +10002224 continue;
2225 }
2226
Dave Chinner5b3eed72010-08-24 11:42:41 +10002227 /*
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002228 * because this is an RCU protected lookup, we could
2229 * find a recently freed or even reallocated inode
2230 * during the lookup. We need to check under the
2231 * i_flags_lock for a valid inode here. Skip it if it
2232 * is not valid, the wrong inode or stale.
2233 */
2234 spin_lock(&ip->i_flags_lock);
2235 if (ip->i_ino != inum + i ||
2236 __xfs_iflags_test(ip, XFS_ISTALE)) {
2237 spin_unlock(&ip->i_flags_lock);
2238 rcu_read_unlock();
2239 continue;
2240 }
2241 spin_unlock(&ip->i_flags_lock);
2242
2243 /*
Dave Chinner5b3eed72010-08-24 11:42:41 +10002244 * Don't try to lock/unlock the current inode, but we
2245 * _cannot_ skip the other inodes that we did not find
2246 * in the list attached to the buffer and are not
2247 * already marked stale. If we can't lock it, back off
2248 * and retry.
2249 */
Dave Chinner5b257b42010-06-03 16:22:29 +10002250 if (ip != free_ip &&
2251 !xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002252 rcu_read_unlock();
Dave Chinner5b3eed72010-08-24 11:42:41 +10002253 delay(1);
2254 goto retry;
Dave Chinner5b257b42010-06-03 16:22:29 +10002255 }
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002256 rcu_read_unlock();
Dave Chinner5b257b42010-06-03 16:22:29 +10002257
Dave Chinner5b3eed72010-08-24 11:42:41 +10002258 xfs_iflock(ip);
Dave Chinner5b257b42010-06-03 16:22:29 +10002259 xfs_iflags_set(ip, XFS_ISTALE);
Dave Chinner5b257b42010-06-03 16:22:29 +10002260
Dave Chinner5b3eed72010-08-24 11:42:41 +10002261 /*
2262 * we don't need to attach clean inodes or those only
2263 * with unlogged changes (which we throw away, anyway).
2264 */
Dave Chinner5b257b42010-06-03 16:22:29 +10002265 iip = ip->i_itemp;
Dave Chinner5b3eed72010-08-24 11:42:41 +10002266 if (!iip || xfs_inode_clean(ip)) {
Dave Chinner5b257b42010-06-03 16:22:29 +10002267 ASSERT(ip != free_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 xfs_ifunlock(ip);
2269 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2270 continue;
2271 }
2272
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00002273 iip->ili_last_fields = iip->ili_fields;
2274 iip->ili_fields = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 iip->ili_logged = 1;
David Chinner7b2e2a32008-10-30 17:39:12 +11002276 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
2277 &iip->ili_item.li_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
Christoph Hellwigca30b2a2010-06-23 18:11:15 +10002279 xfs_buf_attach_iodone(bp, xfs_istale_done,
2280 &iip->ili_item);
Dave Chinner5b257b42010-06-03 16:22:29 +10002281
2282 if (ip != free_ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 }
2285
Dave Chinner5b3eed72010-08-24 11:42:41 +10002286 xfs_trans_stale_inode_buf(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 xfs_trans_binval(tp, bp);
2288 }
2289
Dave Chinner5017e972010-01-11 11:47:40 +00002290 xfs_perag_put(pag);
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00002291 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292}
2293
2294/*
2295 * This is called to return an inode to the inode free list.
2296 * The inode should already be truncated to 0 length and have
2297 * no pages associated with it. This routine also assumes that
2298 * the inode is already a part of the transaction.
2299 *
2300 * The on-disk copy of the inode will have been added to the list
2301 * of unlinked inodes in the AGI. We need to remove the inode from
2302 * that list atomically with respect to freeing it here.
2303 */
2304int
2305xfs_ifree(
2306 xfs_trans_t *tp,
2307 xfs_inode_t *ip,
2308 xfs_bmap_free_t *flist)
2309{
2310 int error;
2311 int delete;
2312 xfs_ino_t first_ino;
2313
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002314 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 ASSERT(ip->i_d.di_nlink == 0);
2316 ASSERT(ip->i_d.di_nextents == 0);
2317 ASSERT(ip->i_d.di_anextents == 0);
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00002318 ASSERT(ip->i_d.di_size == 0 || !S_ISREG(ip->i_d.di_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 ASSERT(ip->i_d.di_nblocks == 0);
2320
2321 /*
2322 * Pull the on-disk inode from the AGI unlinked list.
2323 */
2324 error = xfs_iunlink_remove(tp, ip);
Dave Chinner1baaed82013-06-27 16:04:50 +10002325 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
2328 error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino);
Dave Chinner1baaed82013-06-27 16:04:50 +10002329 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 return error;
Dave Chinner1baaed82013-06-27 16:04:50 +10002331
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 ip->i_d.di_mode = 0; /* mark incore inode as free */
2333 ip->i_d.di_flags = 0;
2334 ip->i_d.di_dmevmask = 0;
2335 ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
2337 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
2338 /*
2339 * Bump the generation count so no one will be confused
2340 * by reincarnations of this inode.
2341 */
2342 ip->i_d.di_gen++;
2343 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2344
Dave Chinner1baaed82013-06-27 16:04:50 +10002345 if (delete)
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00002346 error = xfs_ifree_cluster(ip, tp, first_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00002348 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349}
2350
2351/*
Christoph Hellwig60ec6782010-02-17 19:43:56 +00002352 * This is called to unpin an inode. The caller must have the inode locked
2353 * in at least shared mode so that the buffer cannot be subsequently pinned
2354 * once someone is waiting for it to be unpinned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 */
Christoph Hellwig60ec6782010-02-17 19:43:56 +00002356static void
Christoph Hellwigf392e632011-12-18 20:00:10 +00002357xfs_iunpin(
Christoph Hellwig60ec6782010-02-17 19:43:56 +00002358 struct xfs_inode *ip)
David Chinnera3f74ff2008-03-06 13:43:42 +11002359{
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10002360 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
David Chinnera3f74ff2008-03-06 13:43:42 +11002361
Dave Chinner4aaf15d2010-03-08 11:24:07 +11002362 trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
2363
David Chinnera3f74ff2008-03-06 13:43:42 +11002364 /* Give the log a push to start the unpinning I/O */
Christoph Hellwig60ec6782010-02-17 19:43:56 +00002365 xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0);
Christoph Hellwiga14a3482010-01-19 09:56:46 +00002366
David Chinnera3f74ff2008-03-06 13:43:42 +11002367}
2368
Christoph Hellwigf392e632011-12-18 20:00:10 +00002369static void
2370__xfs_iunpin_wait(
2371 struct xfs_inode *ip)
2372{
2373 wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
2374 DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
2375
2376 xfs_iunpin(ip);
2377
2378 do {
2379 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2380 if (xfs_ipincount(ip))
2381 io_schedule();
2382 } while (xfs_ipincount(ip));
2383 finish_wait(wq, &wait.wait);
2384}
2385
Dave Chinner777df5a2010-02-06 12:37:26 +11002386void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387xfs_iunpin_wait(
Christoph Hellwig60ec6782010-02-17 19:43:56 +00002388 struct xfs_inode *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389{
Christoph Hellwigf392e632011-12-18 20:00:10 +00002390 if (xfs_ipincount(ip))
2391 __xfs_iunpin_wait(ip);
David Chinnera3f74ff2008-03-06 13:43:42 +11002392}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
Dave Chinner27320362013-10-29 22:11:44 +11002394/*
2395 * Removing an inode from the namespace involves removing the directory entry
2396 * and dropping the link count on the inode. Removing the directory entry can
2397 * result in locking an AGF (directory blocks were freed) and removing a link
2398 * count can result in placing the inode on an unlinked list which results in
2399 * locking an AGI.
2400 *
2401 * The big problem here is that we have an ordering constraint on AGF and AGI
2402 * locking - inode allocation locks the AGI, then can allocate a new extent for
2403 * new inodes, locking the AGF after the AGI. Similarly, freeing the inode
2404 * removes the inode from the unlinked list, requiring that we lock the AGI
2405 * first, and then freeing the inode can result in an inode chunk being freed
2406 * and hence freeing disk space requiring that we lock an AGF.
2407 *
2408 * Hence the ordering that is imposed by other parts of the code is AGI before
2409 * AGF. This means we cannot remove the directory entry before we drop the inode
2410 * reference count and put it on the unlinked list as this results in a lock
2411 * order of AGF then AGI, and this can deadlock against inode allocation and
2412 * freeing. Therefore we must drop the link counts before we remove the
2413 * directory entry.
2414 *
2415 * This is still safe from a transactional point of view - it is not until we
2416 * get to xfs_bmap_finish() that we have the possibility of multiple
2417 * transactions in this operation. Hence as long as we remove the directory
2418 * entry and drop the link count in the first transaction of the remove
2419 * operation, there are no transactional constraints on the ordering here.
2420 */
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002421int
2422xfs_remove(
2423 xfs_inode_t *dp,
2424 struct xfs_name *name,
2425 xfs_inode_t *ip)
2426{
2427 xfs_mount_t *mp = dp->i_mount;
2428 xfs_trans_t *tp = NULL;
2429 int is_dir = S_ISDIR(ip->i_d.di_mode);
2430 int error = 0;
2431 xfs_bmap_free_t free_list;
2432 xfs_fsblock_t first_block;
2433 int cancel_flags;
2434 int committed;
2435 int link_zero;
2436 uint resblks;
2437 uint log_count;
2438
2439 trace_xfs_remove(dp, name);
2440
2441 if (XFS_FORCED_SHUTDOWN(mp))
2442 return XFS_ERROR(EIO);
2443
2444 error = xfs_qm_dqattach(dp, 0);
2445 if (error)
2446 goto std_return;
2447
2448 error = xfs_qm_dqattach(ip, 0);
2449 if (error)
2450 goto std_return;
2451
2452 if (is_dir) {
2453 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
2454 log_count = XFS_DEFAULT_LOG_COUNT;
2455 } else {
2456 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
2457 log_count = XFS_REMOVE_LOG_COUNT;
2458 }
2459 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2460
2461 /*
2462 * We try to get the real space reservation first,
2463 * allowing for directory btree deletion(s) implying
2464 * possible bmap insert(s). If we can't get the space
2465 * reservation then we use 0 instead, and avoid the bmap
2466 * btree insert(s) in the directory code by, if the bmap
2467 * insert tries to happen, instead trimming the LAST
2468 * block from the directory.
2469 */
2470 resblks = XFS_REMOVE_SPACE_RES(mp);
Jie Liu3d3c8b52013-08-12 20:49:59 +10002471 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_remove, resblks, 0);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002472 if (error == ENOSPC) {
2473 resblks = 0;
Jie Liu3d3c8b52013-08-12 20:49:59 +10002474 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_remove, 0, 0);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002475 }
2476 if (error) {
2477 ASSERT(error != ENOSPC);
2478 cancel_flags = 0;
2479 goto out_trans_cancel;
2480 }
2481
2482 xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
2483
2484 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2485 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2486
2487 /*
2488 * If we're removing a directory perform some additional validation.
2489 */
Dave Chinner27320362013-10-29 22:11:44 +11002490 cancel_flags |= XFS_TRANS_ABORT;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002491 if (is_dir) {
2492 ASSERT(ip->i_d.di_nlink >= 2);
2493 if (ip->i_d.di_nlink != 2) {
2494 error = XFS_ERROR(ENOTEMPTY);
2495 goto out_trans_cancel;
2496 }
2497 if (!xfs_dir_isempty(ip)) {
2498 error = XFS_ERROR(ENOTEMPTY);
2499 goto out_trans_cancel;
2500 }
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002501
Dave Chinner27320362013-10-29 22:11:44 +11002502 /* Drop the link from ip's "..". */
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002503 error = xfs_droplink(tp, dp);
2504 if (error)
Dave Chinner27320362013-10-29 22:11:44 +11002505 goto out_trans_cancel;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002506
Dave Chinner27320362013-10-29 22:11:44 +11002507 /* Drop the "." link from ip to self. */
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002508 error = xfs_droplink(tp, ip);
2509 if (error)
Dave Chinner27320362013-10-29 22:11:44 +11002510 goto out_trans_cancel;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002511 } else {
2512 /*
2513 * When removing a non-directory we need to log the parent
2514 * inode here. For a directory this is done implicitly
2515 * by the xfs_droplink call for the ".." entry.
2516 */
2517 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2518 }
Dave Chinner27320362013-10-29 22:11:44 +11002519 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002520
Dave Chinner27320362013-10-29 22:11:44 +11002521 /* Drop the link from dp to ip. */
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002522 error = xfs_droplink(tp, ip);
2523 if (error)
Dave Chinner27320362013-10-29 22:11:44 +11002524 goto out_trans_cancel;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002525
Dave Chinner27320362013-10-29 22:11:44 +11002526 /* Determine if this is the last link while the inode is locked */
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002527 link_zero = (ip->i_d.di_nlink == 0);
2528
Dave Chinner27320362013-10-29 22:11:44 +11002529 xfs_bmap_init(&free_list, &first_block);
2530 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
2531 &first_block, &free_list, resblks);
2532 if (error) {
2533 ASSERT(error != ENOENT);
2534 goto out_bmap_cancel;
2535 }
2536
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002537 /*
2538 * If this is a synchronous mount, make sure that the
2539 * remove transaction goes to disk before returning to
2540 * the user.
2541 */
2542 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
2543 xfs_trans_set_sync(tp);
2544
2545 error = xfs_bmap_finish(&tp, &free_list, &committed);
2546 if (error)
2547 goto out_bmap_cancel;
2548
2549 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2550 if (error)
2551 goto std_return;
2552
2553 /*
2554 * If we are using filestreams, kill the stream association.
2555 * If the file is still open it may get a new one but that
2556 * will get killed on last close in xfs_close() so we don't
2557 * have to worry about that.
2558 */
2559 if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
2560 xfs_filestream_deassociate(ip);
2561
2562 return 0;
2563
2564 out_bmap_cancel:
2565 xfs_bmap_cancel(&free_list);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10002566 out_trans_cancel:
2567 xfs_trans_cancel(tp, cancel_flags);
2568 std_return:
2569 return error;
2570}
2571
Dave Chinnerf6bba202013-08-12 20:49:46 +10002572/*
2573 * Enter all inodes for a rename transaction into a sorted array.
2574 */
2575STATIC void
2576xfs_sort_for_rename(
2577 xfs_inode_t *dp1, /* in: old (source) directory inode */
2578 xfs_inode_t *dp2, /* in: new (target) directory inode */
2579 xfs_inode_t *ip1, /* in: inode of old entry */
2580 xfs_inode_t *ip2, /* in: inode of new entry, if it
2581 already exists, NULL otherwise. */
2582 xfs_inode_t **i_tab,/* out: array of inode returned, sorted */
2583 int *num_inodes) /* out: number of inodes in array */
2584{
2585 xfs_inode_t *temp;
2586 int i, j;
2587
2588 /*
2589 * i_tab contains a list of pointers to inodes. We initialize
2590 * the table here & we'll sort it. We will then use it to
2591 * order the acquisition of the inode locks.
2592 *
2593 * Note that the table may contain duplicates. e.g., dp1 == dp2.
2594 */
2595 i_tab[0] = dp1;
2596 i_tab[1] = dp2;
2597 i_tab[2] = ip1;
2598 if (ip2) {
2599 *num_inodes = 4;
2600 i_tab[3] = ip2;
2601 } else {
2602 *num_inodes = 3;
2603 i_tab[3] = NULL;
2604 }
2605
2606 /*
2607 * Sort the elements via bubble sort. (Remember, there are at
2608 * most 4 elements to sort, so this is adequate.)
2609 */
2610 for (i = 0; i < *num_inodes; i++) {
2611 for (j = 1; j < *num_inodes; j++) {
2612 if (i_tab[j]->i_ino < i_tab[j-1]->i_ino) {
2613 temp = i_tab[j];
2614 i_tab[j] = i_tab[j-1];
2615 i_tab[j-1] = temp;
2616 }
2617 }
2618 }
2619}
2620
2621/*
2622 * xfs_rename
2623 */
2624int
2625xfs_rename(
2626 xfs_inode_t *src_dp,
2627 struct xfs_name *src_name,
2628 xfs_inode_t *src_ip,
2629 xfs_inode_t *target_dp,
2630 struct xfs_name *target_name,
2631 xfs_inode_t *target_ip)
2632{
2633 xfs_trans_t *tp = NULL;
2634 xfs_mount_t *mp = src_dp->i_mount;
2635 int new_parent; /* moving to a new dir */
2636 int src_is_directory; /* src_name is a directory */
2637 int error;
2638 xfs_bmap_free_t free_list;
2639 xfs_fsblock_t first_block;
2640 int cancel_flags;
2641 int committed;
2642 xfs_inode_t *inodes[4];
2643 int spaceres;
2644 int num_inodes;
2645
2646 trace_xfs_rename(src_dp, target_dp, src_name, target_name);
2647
2648 new_parent = (src_dp != target_dp);
2649 src_is_directory = S_ISDIR(src_ip->i_d.di_mode);
2650
2651 xfs_sort_for_rename(src_dp, target_dp, src_ip, target_ip,
2652 inodes, &num_inodes);
2653
2654 xfs_bmap_init(&free_list, &first_block);
2655 tp = xfs_trans_alloc(mp, XFS_TRANS_RENAME);
2656 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2657 spaceres = XFS_RENAME_SPACE_RES(mp, target_name->len);
Jie Liu3d3c8b52013-08-12 20:49:59 +10002658 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_rename, spaceres, 0);
Dave Chinnerf6bba202013-08-12 20:49:46 +10002659 if (error == ENOSPC) {
2660 spaceres = 0;
Jie Liu3d3c8b52013-08-12 20:49:59 +10002661 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_rename, 0, 0);
Dave Chinnerf6bba202013-08-12 20:49:46 +10002662 }
2663 if (error) {
2664 xfs_trans_cancel(tp, 0);
2665 goto std_return;
2666 }
2667
2668 /*
2669 * Attach the dquots to the inodes
2670 */
2671 error = xfs_qm_vop_rename_dqattach(inodes);
2672 if (error) {
2673 xfs_trans_cancel(tp, cancel_flags);
2674 goto std_return;
2675 }
2676
2677 /*
2678 * Lock all the participating inodes. Depending upon whether
2679 * the target_name exists in the target directory, and
2680 * whether the target directory is the same as the source
2681 * directory, we can lock from 2 to 4 inodes.
2682 */
2683 xfs_lock_inodes(inodes, num_inodes, XFS_ILOCK_EXCL);
2684
2685 /*
2686 * Join all the inodes to the transaction. From this point on,
2687 * we can rely on either trans_commit or trans_cancel to unlock
2688 * them.
2689 */
2690 xfs_trans_ijoin(tp, src_dp, XFS_ILOCK_EXCL);
2691 if (new_parent)
2692 xfs_trans_ijoin(tp, target_dp, XFS_ILOCK_EXCL);
2693 xfs_trans_ijoin(tp, src_ip, XFS_ILOCK_EXCL);
2694 if (target_ip)
2695 xfs_trans_ijoin(tp, target_ip, XFS_ILOCK_EXCL);
2696
2697 /*
2698 * If we are using project inheritance, we only allow renames
2699 * into our tree when the project IDs are the same; else the
2700 * tree quota mechanism would be circumvented.
2701 */
2702 if (unlikely((target_dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2703 (xfs_get_projid(target_dp) != xfs_get_projid(src_ip)))) {
2704 error = XFS_ERROR(EXDEV);
2705 goto error_return;
2706 }
2707
2708 /*
2709 * Set up the target.
2710 */
2711 if (target_ip == NULL) {
2712 /*
2713 * If there's no space reservation, check the entry will
2714 * fit before actually inserting it.
2715 */
2716 error = xfs_dir_canenter(tp, target_dp, target_name, spaceres);
2717 if (error)
2718 goto error_return;
2719 /*
2720 * If target does not exist and the rename crosses
2721 * directories, adjust the target directory link count
2722 * to account for the ".." reference from the new entry.
2723 */
2724 error = xfs_dir_createname(tp, target_dp, target_name,
2725 src_ip->i_ino, &first_block,
2726 &free_list, spaceres);
2727 if (error == ENOSPC)
2728 goto error_return;
2729 if (error)
2730 goto abort_return;
2731
2732 xfs_trans_ichgtime(tp, target_dp,
2733 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2734
2735 if (new_parent && src_is_directory) {
2736 error = xfs_bumplink(tp, target_dp);
2737 if (error)
2738 goto abort_return;
2739 }
2740 } else { /* target_ip != NULL */
2741 /*
2742 * If target exists and it's a directory, check that both
2743 * target and source are directories and that target can be
2744 * destroyed, or that neither is a directory.
2745 */
2746 if (S_ISDIR(target_ip->i_d.di_mode)) {
2747 /*
2748 * Make sure target dir is empty.
2749 */
2750 if (!(xfs_dir_isempty(target_ip)) ||
2751 (target_ip->i_d.di_nlink > 2)) {
2752 error = XFS_ERROR(EEXIST);
2753 goto error_return;
2754 }
2755 }
2756
2757 /*
2758 * Link the source inode under the target name.
2759 * If the source inode is a directory and we are moving
2760 * it across directories, its ".." entry will be
2761 * inconsistent until we replace that down below.
2762 *
2763 * In case there is already an entry with the same
2764 * name at the destination directory, remove it first.
2765 */
2766 error = xfs_dir_replace(tp, target_dp, target_name,
2767 src_ip->i_ino,
2768 &first_block, &free_list, spaceres);
2769 if (error)
2770 goto abort_return;
2771
2772 xfs_trans_ichgtime(tp, target_dp,
2773 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2774
2775 /*
2776 * Decrement the link count on the target since the target
2777 * dir no longer points to it.
2778 */
2779 error = xfs_droplink(tp, target_ip);
2780 if (error)
2781 goto abort_return;
2782
2783 if (src_is_directory) {
2784 /*
2785 * Drop the link from the old "." entry.
2786 */
2787 error = xfs_droplink(tp, target_ip);
2788 if (error)
2789 goto abort_return;
2790 }
2791 } /* target_ip != NULL */
2792
2793 /*
2794 * Remove the source.
2795 */
2796 if (new_parent && src_is_directory) {
2797 /*
2798 * Rewrite the ".." entry to point to the new
2799 * directory.
2800 */
2801 error = xfs_dir_replace(tp, src_ip, &xfs_name_dotdot,
2802 target_dp->i_ino,
2803 &first_block, &free_list, spaceres);
2804 ASSERT(error != EEXIST);
2805 if (error)
2806 goto abort_return;
2807 }
2808
2809 /*
2810 * We always want to hit the ctime on the source inode.
2811 *
2812 * This isn't strictly required by the standards since the source
2813 * inode isn't really being changed, but old unix file systems did
2814 * it and some incremental backup programs won't work without it.
2815 */
2816 xfs_trans_ichgtime(tp, src_ip, XFS_ICHGTIME_CHG);
2817 xfs_trans_log_inode(tp, src_ip, XFS_ILOG_CORE);
2818
2819 /*
2820 * Adjust the link count on src_dp. This is necessary when
2821 * renaming a directory, either within one parent when
2822 * the target existed, or across two parent directories.
2823 */
2824 if (src_is_directory && (new_parent || target_ip != NULL)) {
2825
2826 /*
2827 * Decrement link count on src_directory since the
2828 * entry that's moved no longer points to it.
2829 */
2830 error = xfs_droplink(tp, src_dp);
2831 if (error)
2832 goto abort_return;
2833 }
2834
2835 error = xfs_dir_removename(tp, src_dp, src_name, src_ip->i_ino,
2836 &first_block, &free_list, spaceres);
2837 if (error)
2838 goto abort_return;
2839
2840 xfs_trans_ichgtime(tp, src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2841 xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE);
2842 if (new_parent)
2843 xfs_trans_log_inode(tp, target_dp, XFS_ILOG_CORE);
2844
2845 /*
2846 * If this is a synchronous mount, make sure that the
2847 * rename transaction goes to disk before returning to
2848 * the user.
2849 */
2850 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2851 xfs_trans_set_sync(tp);
2852 }
2853
2854 error = xfs_bmap_finish(&tp, &free_list, &committed);
2855 if (error) {
2856 xfs_bmap_cancel(&free_list);
2857 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
2858 XFS_TRANS_ABORT));
2859 goto std_return;
2860 }
2861
2862 /*
2863 * trans_commit will unlock src_ip, target_ip & decrement
2864 * the vnode references.
2865 */
2866 return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2867
2868 abort_return:
2869 cancel_flags |= XFS_TRANS_ABORT;
2870 error_return:
2871 xfs_bmap_cancel(&free_list);
2872 xfs_trans_cancel(tp, cancel_flags);
2873 std_return:
2874 return error;
2875}
2876
David Chinnerbad55842008-03-06 13:43:49 +11002877STATIC int
2878xfs_iflush_cluster(
2879 xfs_inode_t *ip,
2880 xfs_buf_t *bp)
2881{
2882 xfs_mount_t *mp = ip->i_mount;
Dave Chinner5017e972010-01-11 11:47:40 +00002883 struct xfs_perag *pag;
David Chinnerbad55842008-03-06 13:43:49 +11002884 unsigned long first_index, mask;
David Chinnerc8f5f122008-05-20 11:30:15 +10002885 unsigned long inodes_per_cluster;
David Chinnerbad55842008-03-06 13:43:49 +11002886 int ilist_size;
2887 xfs_inode_t **ilist;
2888 xfs_inode_t *iq;
David Chinnerbad55842008-03-06 13:43:49 +11002889 int nr_found;
2890 int clcount = 0;
2891 int bufwasdelwri;
2892 int i;
2893
Dave Chinner5017e972010-01-11 11:47:40 +00002894 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
David Chinnerbad55842008-03-06 13:43:49 +11002895
David Chinnerc8f5f122008-05-20 11:30:15 +10002896 inodes_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog;
2897 ilist_size = inodes_per_cluster * sizeof(xfs_inode_t *);
David Chinner49383b02008-05-19 16:29:34 +10002898 ilist = kmem_alloc(ilist_size, KM_MAYFAIL|KM_NOFS);
David Chinnerbad55842008-03-06 13:43:49 +11002899 if (!ilist)
Dave Chinner44b56e02010-01-11 11:47:43 +00002900 goto out_put;
David Chinnerbad55842008-03-06 13:43:49 +11002901
2902 mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
2903 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask;
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002904 rcu_read_lock();
David Chinnerbad55842008-03-06 13:43:49 +11002905 /* really need a gang lookup range call here */
2906 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)ilist,
David Chinnerc8f5f122008-05-20 11:30:15 +10002907 first_index, inodes_per_cluster);
David Chinnerbad55842008-03-06 13:43:49 +11002908 if (nr_found == 0)
2909 goto out_free;
2910
2911 for (i = 0; i < nr_found; i++) {
2912 iq = ilist[i];
2913 if (iq == ip)
2914 continue;
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002915
2916 /*
2917 * because this is an RCU protected lookup, we could find a
2918 * recently freed or even reallocated inode during the lookup.
2919 * We need to check under the i_flags_lock for a valid inode
2920 * here. Skip it if it is not valid or the wrong inode.
2921 */
2922 spin_lock(&ip->i_flags_lock);
2923 if (!ip->i_ino ||
2924 (XFS_INO_TO_AGINO(mp, iq->i_ino) & mask) != first_index) {
2925 spin_unlock(&ip->i_flags_lock);
2926 continue;
2927 }
2928 spin_unlock(&ip->i_flags_lock);
2929
David Chinnerbad55842008-03-06 13:43:49 +11002930 /*
2931 * Do an un-protected check to see if the inode is dirty and
2932 * is a candidate for flushing. These checks will be repeated
2933 * later after the appropriate locks are acquired.
2934 */
David Chinner33540402008-03-06 13:43:59 +11002935 if (xfs_inode_clean(iq) && xfs_ipincount(iq) == 0)
David Chinnerbad55842008-03-06 13:43:49 +11002936 continue;
David Chinnerbad55842008-03-06 13:43:49 +11002937
2938 /*
2939 * Try to get locks. If any are unavailable or it is pinned,
2940 * then this inode cannot be flushed and is skipped.
2941 */
2942
2943 if (!xfs_ilock_nowait(iq, XFS_ILOCK_SHARED))
2944 continue;
2945 if (!xfs_iflock_nowait(iq)) {
2946 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2947 continue;
2948 }
2949 if (xfs_ipincount(iq)) {
2950 xfs_ifunlock(iq);
2951 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2952 continue;
2953 }
2954
2955 /*
2956 * arriving here means that this inode can be flushed. First
2957 * re-check that it's dirty before flushing.
2958 */
David Chinner33540402008-03-06 13:43:59 +11002959 if (!xfs_inode_clean(iq)) {
2960 int error;
David Chinnerbad55842008-03-06 13:43:49 +11002961 error = xfs_iflush_int(iq, bp);
2962 if (error) {
2963 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2964 goto cluster_corrupt_out;
2965 }
2966 clcount++;
2967 } else {
2968 xfs_ifunlock(iq);
2969 }
2970 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2971 }
2972
2973 if (clcount) {
2974 XFS_STATS_INC(xs_icluster_flushcnt);
2975 XFS_STATS_ADD(xs_icluster_flushinode, clcount);
2976 }
2977
2978out_free:
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002979 rcu_read_unlock();
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002980 kmem_free(ilist);
Dave Chinner44b56e02010-01-11 11:47:43 +00002981out_put:
2982 xfs_perag_put(pag);
David Chinnerbad55842008-03-06 13:43:49 +11002983 return 0;
2984
2985
2986cluster_corrupt_out:
2987 /*
2988 * Corruption detected in the clustering loop. Invalidate the
2989 * inode buffer and shut down the filesystem.
2990 */
Dave Chinner1a3e8f32010-12-17 17:29:43 +11002991 rcu_read_unlock();
David Chinnerbad55842008-03-06 13:43:49 +11002992 /*
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002993 * Clean up the buffer. If it was delwri, just release it --
David Chinnerbad55842008-03-06 13:43:49 +11002994 * brelse can handle it with no problems. If not, shut down the
2995 * filesystem before releasing the buffer.
2996 */
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002997 bufwasdelwri = (bp->b_flags & _XBF_DELWRI_Q);
David Chinnerbad55842008-03-06 13:43:49 +11002998 if (bufwasdelwri)
2999 xfs_buf_relse(bp);
3000
3001 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
3002
3003 if (!bufwasdelwri) {
3004 /*
3005 * Just like incore_relse: if we have b_iodone functions,
3006 * mark the buffer as an error and call them. Otherwise
3007 * mark it as stale and brelse.
3008 */
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02003009 if (bp->b_iodone) {
David Chinnerbad55842008-03-06 13:43:49 +11003010 XFS_BUF_UNDONE(bp);
Christoph Hellwigc867cb62011-10-10 16:52:46 +00003011 xfs_buf_stale(bp);
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00003012 xfs_buf_ioerror(bp, EIO);
Christoph Hellwig1a1a3e92010-10-06 18:41:18 +00003013 xfs_buf_ioend(bp, 0);
David Chinnerbad55842008-03-06 13:43:49 +11003014 } else {
Christoph Hellwigc867cb62011-10-10 16:52:46 +00003015 xfs_buf_stale(bp);
David Chinnerbad55842008-03-06 13:43:49 +11003016 xfs_buf_relse(bp);
3017 }
3018 }
3019
3020 /*
3021 * Unlocks the flush lock
3022 */
Dave Chinner04913fd2012-04-23 15:58:41 +10003023 xfs_iflush_abort(iq, false);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003024 kmem_free(ilist);
Dave Chinner44b56e02010-01-11 11:47:43 +00003025 xfs_perag_put(pag);
David Chinnerbad55842008-03-06 13:43:49 +11003026 return XFS_ERROR(EFSCORRUPTED);
3027}
3028
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029/*
Christoph Hellwig4c468192012-04-23 15:58:36 +10003030 * Flush dirty inode metadata into the backing buffer.
3031 *
3032 * The caller must have the inode lock and the inode flush lock held. The
3033 * inode lock will still be held upon return to the caller, and the inode
3034 * flush lock will be released after the inode has reached the disk.
3035 *
3036 * The caller must write out the buffer returned in *bpp and release it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 */
3038int
3039xfs_iflush(
Christoph Hellwig4c468192012-04-23 15:58:36 +10003040 struct xfs_inode *ip,
3041 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042{
Christoph Hellwig4c468192012-04-23 15:58:36 +10003043 struct xfs_mount *mp = ip->i_mount;
3044 struct xfs_buf *bp;
3045 struct xfs_dinode *dip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
3048 XFS_STATS_INC(xs_iflush_count);
3049
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10003050 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
Christoph Hellwig474fce02011-12-18 20:00:09 +00003051 ASSERT(xfs_isiflocked(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00003053 ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054
Christoph Hellwig4c468192012-04-23 15:58:36 +10003055 *bpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 xfs_iunpin_wait(ip);
3058
3059 /*
Dave Chinner4b6a4682010-01-11 11:45:21 +00003060 * For stale inodes we cannot rely on the backing buffer remaining
3061 * stale in cache for the remaining life of the stale inode and so
Christoph Hellwig475ee412012-07-03 12:21:22 -04003062 * xfs_imap_to_bp() below may give us a buffer that no longer contains
Dave Chinner4b6a4682010-01-11 11:45:21 +00003063 * inodes below. We have to check this after ensuring the inode is
3064 * unpinned so that it is safe to reclaim the stale inode after the
3065 * flush call.
3066 */
3067 if (xfs_iflags_test(ip, XFS_ISTALE)) {
3068 xfs_ifunlock(ip);
3069 return 0;
3070 }
3071
3072 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 * This may have been unpinned because the filesystem is shutting
3074 * down forcibly. If that's the case we must not write this inode
Christoph Hellwig32ce90a2012-04-23 15:58:32 +10003075 * to disk, because the log record didn't make it to disk.
3076 *
3077 * We also have to remove the log item from the AIL in this case,
3078 * as we wait for an empty AIL as part of the unmount process.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 */
3080 if (XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwig32ce90a2012-04-23 15:58:32 +10003081 error = XFS_ERROR(EIO);
3082 goto abort_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 }
3084
3085 /*
David Chinnera3f74ff2008-03-06 13:43:42 +11003086 * Get the buffer containing the on-disk inode.
3087 */
Christoph Hellwig475ee412012-07-03 12:21:22 -04003088 error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &bp, XBF_TRYLOCK,
3089 0);
David Chinnera3f74ff2008-03-06 13:43:42 +11003090 if (error || !bp) {
3091 xfs_ifunlock(ip);
3092 return error;
3093 }
3094
3095 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 * First flush out the inode that xfs_iflush was called with.
3097 */
3098 error = xfs_iflush_int(ip, bp);
David Chinnerbad55842008-03-06 13:43:49 +11003099 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 goto corrupt_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
3102 /*
David Chinnera3f74ff2008-03-06 13:43:42 +11003103 * If the buffer is pinned then push on the log now so we won't
3104 * get stuck waiting in the write for too long.
3105 */
Chandra Seetharaman811e64c2011-07-22 23:40:27 +00003106 if (xfs_buf_ispinned(bp))
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003107 xfs_log_force(mp, 0);
David Chinnera3f74ff2008-03-06 13:43:42 +11003108
3109 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 * inode clustering:
3111 * see if other inodes can be gathered into this write
3112 */
David Chinnerbad55842008-03-06 13:43:49 +11003113 error = xfs_iflush_cluster(ip, bp);
3114 if (error)
3115 goto cluster_corrupt_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
Christoph Hellwig4c468192012-04-23 15:58:36 +10003117 *bpp = bp;
3118 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119
3120corrupt_out:
3121 xfs_buf_relse(bp);
Nathan Scott7d04a332006-06-09 14:58:38 +10003122 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123cluster_corrupt_out:
Christoph Hellwig32ce90a2012-04-23 15:58:32 +10003124 error = XFS_ERROR(EFSCORRUPTED);
3125abort_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 /*
3127 * Unlocks the flush lock
3128 */
Dave Chinner04913fd2012-04-23 15:58:41 +10003129 xfs_iflush_abort(ip, false);
Christoph Hellwig32ce90a2012-04-23 15:58:32 +10003130 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131}
3132
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133STATIC int
3134xfs_iflush_int(
Christoph Hellwig93848a92013-04-03 16:11:17 +11003135 struct xfs_inode *ip,
3136 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137{
Christoph Hellwig93848a92013-04-03 16:11:17 +11003138 struct xfs_inode_log_item *iip = ip->i_itemp;
3139 struct xfs_dinode *dip;
3140 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10003142 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
Christoph Hellwig474fce02011-12-18 20:00:09 +00003143 ASSERT(xfs_isiflocked(ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00003145 ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
Christoph Hellwig93848a92013-04-03 16:11:17 +11003146 ASSERT(iip != NULL && iip->ili_fields != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 /* set *dip = inode's place in the buffer */
Christoph Hellwig92bfc6e2008-11-28 14:23:41 +11003149 dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150
Christoph Hellwig69ef9212011-07-08 14:36:05 +02003151 if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11003153 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3154 "%s: Bad inode %Lu magic number 0x%x, ptr 0x%p",
3155 __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 goto corrupt_out;
3157 }
3158 if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC,
3159 mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11003160 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3161 "%s: Bad inode %Lu, ptr 0x%p, magic number 0x%x",
3162 __func__, ip->i_ino, ip, ip->i_d.di_magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 goto corrupt_out;
3164 }
Al Viroabbede12011-07-26 02:31:30 -04003165 if (S_ISREG(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 if (XFS_TEST_ERROR(
3167 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
3168 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE),
3169 mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11003170 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3171 "%s: Bad regular inode %Lu, ptr 0x%p",
3172 __func__, ip->i_ino, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 goto corrupt_out;
3174 }
Al Viroabbede12011-07-26 02:31:30 -04003175 } else if (S_ISDIR(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 if (XFS_TEST_ERROR(
3177 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
3178 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
3179 (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL),
3180 mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11003181 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3182 "%s: Bad directory inode %Lu, ptr 0x%p",
3183 __func__, ip->i_ino, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 goto corrupt_out;
3185 }
3186 }
3187 if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents >
3188 ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5,
3189 XFS_RANDOM_IFLUSH_5)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11003190 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3191 "%s: detected corrupt incore inode %Lu, "
3192 "total extents = %d, nblocks = %Ld, ptr 0x%p",
3193 __func__, ip->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 ip->i_d.di_nextents + ip->i_d.di_anextents,
Dave Chinner6a19d932011-03-07 10:02:35 +11003195 ip->i_d.di_nblocks, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 goto corrupt_out;
3197 }
3198 if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
3199 mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) {
Dave Chinner6a19d932011-03-07 10:02:35 +11003200 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3201 "%s: bad inode %Lu, forkoff 0x%x, ptr 0x%p",
3202 __func__, ip->i_ino, ip->i_d.di_forkoff, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 goto corrupt_out;
3204 }
Dave Chinnere60896d2013-07-24 15:47:30 +10003205
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 /*
Dave Chinnere60896d2013-07-24 15:47:30 +10003207 * Inode item log recovery for v1/v2 inodes are dependent on the
3208 * di_flushiter count for correct sequencing. We bump the flush
3209 * iteration count so we can detect flushes which postdate a log record
3210 * during recovery. This is redundant as we now log every change and
3211 * hence this can't happen but we need to still do it to ensure
3212 * backwards compatibility with old kernels that predate logging all
3213 * inode changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 */
Dave Chinnere60896d2013-07-24 15:47:30 +10003215 if (ip->i_d.di_version < 3)
3216 ip->i_d.di_flushiter++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
3218 /*
3219 * Copy the dirty parts of the inode into the on-disk
3220 * inode. We always copy out the core of the inode,
3221 * because if the inode is dirty at all the core must
3222 * be.
3223 */
Christoph Hellwig81591fe2008-11-28 14:23:39 +11003224 xfs_dinode_to_disk(dip, &ip->i_d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225
3226 /* Wrap, we never let the log put out DI_MAX_FLUSH */
3227 if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
3228 ip->i_d.di_flushiter = 0;
3229
3230 /*
3231 * If this is really an old format inode and the superblock version
3232 * has not been updated to support only new format inodes, then
3233 * convert back to the old inode format. If the superblock version
3234 * has been updated, then make the conversion permanent.
3235 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11003236 ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb));
3237 if (ip->i_d.di_version == 1) {
Eric Sandeen62118702008-03-06 13:44:28 +11003238 if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 /*
3240 * Convert it back.
3241 */
3242 ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
Christoph Hellwig81591fe2008-11-28 14:23:39 +11003243 dip->di_onlink = cpu_to_be16(ip->i_d.di_nlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 } else {
3245 /*
3246 * The superblock version has already been bumped,
3247 * so just make the conversion to the new inode
3248 * format permanent.
3249 */
Christoph Hellwig51ce16d2008-11-28 14:23:39 +11003250 ip->i_d.di_version = 2;
3251 dip->di_version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 ip->i_d.di_onlink = 0;
Christoph Hellwig81591fe2008-11-28 14:23:39 +11003253 dip->di_onlink = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
Christoph Hellwig81591fe2008-11-28 14:23:39 +11003255 memset(&(dip->di_pad[0]), 0,
3256 sizeof(dip->di_pad));
Arkadiusz Mi?kiewicz67430992010-09-26 06:10:18 +00003257 ASSERT(xfs_get_projid(ip) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 }
3259 }
3260
David Chinnere4ac9672008-04-10 12:23:58 +10003261 xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK, bp);
3262 if (XFS_IFORK_Q(ip))
3263 xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 xfs_inobp_check(mp, bp);
3265
3266 /*
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00003267 * We've recorded everything logged in the inode, so we'd like to clear
3268 * the ili_fields bits so we don't log and flush things unnecessarily.
3269 * However, we can't stop logging all this information until the data
3270 * we've copied into the disk buffer is written to disk. If we did we
3271 * might overwrite the copy of the inode in the log with all the data
3272 * after re-logging only part of it, and in the face of a crash we
3273 * wouldn't have all the data we need to recover.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 *
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00003275 * What we do is move the bits to the ili_last_fields field. When
3276 * logging the inode, these bits are moved back to the ili_fields field.
3277 * In the xfs_iflush_done() routine we clear ili_last_fields, since we
3278 * know that the information those bits represent is permanently on
3279 * disk. As long as the flush completes before the inode is logged
3280 * again, then both ili_fields and ili_last_fields will be cleared.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 *
Christoph Hellwigf5d8d5c2012-02-29 09:53:54 +00003282 * We can play with the ili_fields bits here, because the inode lock
3283 * must be held exclusively in order to set bits there and the flush
3284 * lock protects the ili_last_fields bits. Set ili_logged so the flush
3285 * done routine can tell whether or not to look in the AIL. Also, store
3286 * the current LSN of the inode so that we can tell whether the item has
3287 * moved in the AIL from xfs_iflush_done(). In order to read the lsn we
3288 * need the AIL lock, because it is a 64 bit value that cannot be read
3289 * atomically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 */
Christoph Hellwig93848a92013-04-03 16:11:17 +11003291 iip->ili_last_fields = iip->ili_fields;
3292 iip->ili_fields = 0;
3293 iip->ili_logged = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294
Christoph Hellwig93848a92013-04-03 16:11:17 +11003295 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
3296 &iip->ili_item.li_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297
Christoph Hellwig93848a92013-04-03 16:11:17 +11003298 /*
3299 * Attach the function xfs_iflush_done to the inode's
3300 * buffer. This will remove the inode from the AIL
3301 * and unlock the inode's flush lock when the inode is
3302 * completely written to disk.
3303 */
3304 xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305
Christoph Hellwig93848a92013-04-03 16:11:17 +11003306 /* update the lsn in the on disk inode if required */
3307 if (ip->i_d.di_version == 3)
3308 dip->di_lsn = cpu_to_be64(iip->ili_item.li_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
Christoph Hellwig93848a92013-04-03 16:11:17 +11003310 /* generate the checksum. */
3311 xfs_dinode_calc_crc(mp, dip);
3312
3313 ASSERT(bp->b_fspriv != NULL);
3314 ASSERT(bp->b_iodone != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 return 0;
3316
3317corrupt_out:
3318 return XFS_ERROR(EFSCORRUPTED);
3319}