xfs: remove xfs_iunlock_map_shared
[deliverable/linux.git] / fs / xfs / xfs_inode.c
CommitLineData
1da177e4 1/*
3e57ecf6 2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
7b718769 3 * All Rights Reserved.
1da177e4 4 *
7b718769
NS
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
7b718769
NS
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
1da177e4 13 *
7b718769
NS
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1da177e4 17 */
40ebd81d
RD
18#include <linux/log2.h>
19
1da177e4 20#include "xfs.h"
a844f451 21#include "xfs_fs.h"
70a9883c 22#include "xfs_shared.h"
239880ef
DC
23#include "xfs_format.h"
24#include "xfs_log_format.h"
25#include "xfs_trans_resv.h"
a844f451 26#include "xfs_inum.h"
1da177e4
LT
27#include "xfs_sb.h"
28#include "xfs_ag.h"
1da177e4 29#include "xfs_mount.h"
a4fbe6ab 30#include "xfs_inode.h"
57062787 31#include "xfs_da_format.h"
c24b5dfa 32#include "xfs_da_btree.h"
c24b5dfa 33#include "xfs_dir2.h"
a844f451 34#include "xfs_attr_sf.h"
c24b5dfa 35#include "xfs_attr.h"
239880ef
DC
36#include "xfs_trans_space.h"
37#include "xfs_trans.h"
1da177e4 38#include "xfs_buf_item.h"
a844f451 39#include "xfs_inode_item.h"
a844f451
NS
40#include "xfs_ialloc.h"
41#include "xfs_bmap.h"
68988114 42#include "xfs_bmap_util.h"
1da177e4 43#include "xfs_error.h"
1da177e4 44#include "xfs_quota.h"
a4fbe6ab 45#include "xfs_dinode.h"
2a82b8be 46#include "xfs_filestream.h"
93848a99 47#include "xfs_cksum.h"
0b1b213f 48#include "xfs_trace.h"
33479e05 49#include "xfs_icache.h"
c24b5dfa 50#include "xfs_symlink.h"
239880ef
DC
51#include "xfs_trans_priv.h"
52#include "xfs_log.h"
a4fbe6ab 53#include "xfs_bmap_btree.h"
1da177e4 54
1da177e4 55kmem_zone_t *xfs_inode_zone;
1da177e4
LT
56
57/*
8f04c47a 58 * Used in xfs_itruncate_extents(). This is the maximum number of extents
1da177e4
LT
59 * 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 *);
1da177e4 64
2a0ec1d9
DC
65/*
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
fa96acad
DC
79/*
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
01f4f327 91 * xfs_iunlock() call.
fa96acad
DC
92 */
93uint
94xfs_ilock_map_shared(
95 xfs_inode_t *ip)
96{
97 uint lock_mode;
98
99 if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
100 ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
101 lock_mode = XFS_ILOCK_EXCL;
102 } else {
103 lock_mode = XFS_ILOCK_SHARED;
104 }
105
106 xfs_ilock(ip, lock_mode);
107
108 return lock_mode;
109}
110
fa96acad
DC
111/*
112 * 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
742ae1e3 277#if defined(DEBUG) || defined(XFS_WARN)
fa96acad
DC
278int
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
c24b5dfa
DC
300#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
fa96acad
DC
482void
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
1da177e4
LT
498STATIC uint
499_xfs_dic2xflags(
1da177e4
LT
500 __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;
dd9f438e
NS
525 if (di_flags & XFS_DIFLAG_EXTSIZE)
526 flags |= XFS_XFLAG_EXTSIZE;
527 if (di_flags & XFS_DIFLAG_EXTSZINHERIT)
528 flags |= XFS_XFLAG_EXTSZINHERIT;
d3446eac
BN
529 if (di_flags & XFS_DIFLAG_NODEFRAG)
530 flags |= XFS_XFLAG_NODEFRAG;
2a82b8be
DC
531 if (di_flags & XFS_DIFLAG_FILESTREAM)
532 flags |= XFS_XFLAG_FILESTREAM;
1da177e4
LT
533 }
534
535 return flags;
536}
537
538uint
539xfs_ip2xflags(
540 xfs_inode_t *ip)
541{
347d1c01 542 xfs_icdinode_t *dic = &ip->i_d;
1da177e4 543
a916e2bd 544 return _xfs_dic2xflags(dic->di_flags) |
45ba598e 545 (XFS_IFORK_Q(ip) ? XFS_XFLAG_HASATTR : 0);
1da177e4
LT
546}
547
548uint
549xfs_dic2xflags(
45ba598e 550 xfs_dinode_t *dip)
1da177e4 551{
81591fe2 552 return _xfs_dic2xflags(be16_to_cpu(dip->di_flags)) |
45ba598e 553 (XFS_DFORK_Q(dip) ? XFS_XFLAG_HASATTR : 0);
1da177e4
LT
554}
555
c24b5dfa
DC
556/*
557 * 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);
01f4f327 580 xfs_iunlock(dp, lock_mode);
c24b5dfa
DC
581
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
1da177e4
LT
599/*
600 * 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()
cd856db6
CM
606 * 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.
1da177e4 610 *
cd856db6
CM
611 * 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.
1da177e4
LT
616 * 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.
b11f94d5
DC
624 *
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.
1da177e4
LT
629 */
630int
631xfs_ialloc(
632 xfs_trans_t *tp,
633 xfs_inode_t *pip,
576b1d67 634 umode_t mode,
31b084ae 635 xfs_nlink_t nlink,
1da177e4 636 xfs_dev_t rdev,
6743099c 637 prid_t prid,
1da177e4
LT
638 int okalloc,
639 xfs_buf_t **ialloc_context,
1da177e4
LT
640 xfs_inode_t **ipp)
641{
93848a99 642 struct xfs_mount *mp = tp->t_mountp;
1da177e4
LT
643 xfs_ino_t ino;
644 xfs_inode_t *ip;
1da177e4
LT
645 uint flags;
646 int error;
dff35fd4 647 timespec_t tv;
bf904248 648 int filestreams = 0;
1da177e4
LT
649
650 /*
651 * Call the space management code to pick
652 * the on-disk inode to be allocated.
653 */
b11f94d5 654 error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc,
08358906 655 ialloc_context, &ino);
bf904248 656 if (error)
1da177e4 657 return error;
08358906 658 if (*ialloc_context || ino == NULLFSINO) {
1da177e4
LT
659 *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 */
93848a99 669 error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE,
ec3ba85f 670 XFS_ILOCK_EXCL, &ip);
bf904248 671 if (error)
1da177e4 672 return error;
1da177e4
LT
673 ASSERT(ip != NULL);
674
576b1d67 675 ip->i_d.di_mode = mode;
1da177e4
LT
676 ip->i_d.di_onlink = 0;
677 ip->i_d.di_nlink = nlink;
678 ASSERT(ip->i_d.di_nlink == nlink);
7aab1b28
DE
679 ip->i_d.di_uid = xfs_kuid_to_uid(current_fsuid());
680 ip->i_d.di_gid = xfs_kgid_to_gid(current_fsgid());
6743099c 681 xfs_set_projid(ip, prid);
1da177e4
LT
682 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 */
93848a99 690 if (xfs_sb_version_hasnlink(&mp->m_sb) &&
51ce16d5
CH
691 ip->i_d.di_version == 1) {
692 ip->i_d.di_version = 2;
1da177e4
LT
693 /*
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 */
51ce16d5 702 if ((prid != 0) && (ip->i_d.di_version == 1))
1da177e4
LT
703 xfs_bump_ino_vers2(tp, ip);
704
bd186aa9 705 if (pip && XFS_INHERIT_GID(pip)) {
1da177e4 706 ip->i_d.di_gid = pip->i_d.di_gid;
abbede1b 707 if ((pip->i_d.di_mode & S_ISGID) && S_ISDIR(mode)) {
1da177e4
LT
708 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) &&
7aab1b28 719 (!in_group_p(xfs_gid_to_kgid(ip->i_d.di_gid)))) {
1da177e4
LT
720 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);
dff35fd4
CH
726
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
1da177e4
LT
733 /*
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;
93848a99
CH
740
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
1da177e4
LT
753 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:
bf904248
DC
765 /*
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;
2a82b8be 771 /* fall through */
1da177e4 772 case S_IFDIR:
b11f94d5 773 if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) {
365ca83d
NS
774 uint di_flags = 0;
775
abbede1b 776 if (S_ISDIR(mode)) {
365ca83d
NS
777 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
778 di_flags |= XFS_DIFLAG_RTINHERIT;
dd9f438e
NS
779 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 }
abbede1b 783 } else if (S_ISREG(mode)) {
613d7043 784 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
365ca83d 785 di_flags |= XFS_DIFLAG_REALTIME;
dd9f438e
NS
786 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 }
1da177e4
LT
790 }
791 if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
792 xfs_inherit_noatime)
365ca83d 793 di_flags |= XFS_DIFLAG_NOATIME;
1da177e4
LT
794 if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
795 xfs_inherit_nodump)
365ca83d 796 di_flags |= XFS_DIFLAG_NODUMP;
1da177e4
LT
797 if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
798 xfs_inherit_sync)
365ca83d 799 di_flags |= XFS_DIFLAG_SYNC;
1da177e4
LT
800 if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
801 xfs_inherit_nosymlinks)
365ca83d
NS
802 di_flags |= XFS_DIFLAG_NOSYMLINKS;
803 if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
804 di_flags |= XFS_DIFLAG_PROJINHERIT;
d3446eac
BN
805 if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
806 xfs_inherit_nodefrag)
807 di_flags |= XFS_DIFLAG_NODEFRAG;
2a82b8be
DC
808 if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
809 di_flags |= XFS_DIFLAG_FILESTREAM;
365ca83d 810 ip->i_d.di_flags |= di_flags;
1da177e4
LT
811 }
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 */
ddc3415a 831 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1da177e4
LT
832 xfs_trans_log_inode(tp, ip, flags);
833
b83bd138 834 /* now that we have an i_mode we can setup inode ops and unlock */
41be8bed 835 xfs_setup_inode(ip);
1da177e4 836
bf904248
DC
837 /* 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
1da177e4
LT
846 *ipp = ip;
847 return 0;
848}
849
e546cb79
DC
850/*
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;
e546cb79
DC
881 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) {
3d3c8b52
JL
926 struct xfs_trans_res tres;
927
e546cb79
DC
928 /*
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 */
3d3c8b52
JL
940 tres.tr_logres = xfs_trans_get_log_res(tp);
941 tres.tr_logcount = xfs_trans_get_log_count(tp);
e546cb79
DC
942
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);
3d3c8b52
JL
984 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
985 code = xfs_trans_reserve(tp, &tres, 0, 0);
986
e546cb79
DC
987 /*
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
c24b5dfa
DC
1129int
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;
3d3c8b52 1151 struct xfs_trans_res tres;
c24b5dfa 1152 uint resblks;
c24b5dfa
DC
1153
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 */
7aab1b28
DE
1167 error = xfs_qm_vop_dqalloc(dp, xfs_kuid_to_uid(current_fsuid()),
1168 xfs_kgid_to_gid(current_fsgid()), prid,
c24b5dfa
DC
1169 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);
3d3c8b52
JL
1177 tres.tr_logres = M_RES(mp)->tr_mkdir.tr_logres;
1178 tres.tr_logcount = XFS_MKDIR_LOG_COUNT;
c24b5dfa
DC
1179 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
1180 } else {
1181 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
3d3c8b52
JL
1182 tres.tr_logres = M_RES(mp)->tr_create.tr_logres;
1183 tres.tr_logcount = XFS_CREATE_LOG_COUNT;
c24b5dfa
DC
1184 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 */
3d3c8b52
JL
1195 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
1196 error = xfs_trans_reserve(tp, &tres, resblks, 0);
c24b5dfa
DC
1197 if (error == ENOSPC) {
1198 /* flush outstanding delalloc blocks and retry */
1199 xfs_flush_inodes(mp);
3d3c8b52 1200 error = xfs_trans_reserve(tp, &tres, resblks, 0);
c24b5dfa
DC
1201 }
1202 if (error == ENOSPC) {
1203 /* No space at all so try a "no-allocation" reservation */
1204 resblks = 0;
3d3c8b52 1205 error = xfs_trans_reserve(tp, &tres, 0, 0);
c24b5dfa
DC
1206 }
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);
3d3c8b52 1359 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_link, resblks, 0);
c24b5dfa
DC
1360 if (error == ENOSPC) {
1361 resblks = 0;
3d3c8b52 1362 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_link, 0, 0);
c24b5dfa
DC
1363 }
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
1da177e4 1427/*
8f04c47a
CH
1428 * 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.
1da177e4 1431 *
f6485057
DC
1432 * 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.
1da177e4 1441 *
f6485057
DC
1442 * 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.
1da177e4
LT
1447 */
1448int
8f04c47a
CH
1449xfs_itruncate_extents(
1450 struct xfs_trans **tpp,
1451 struct xfs_inode *ip,
1452 int whichfork,
1453 xfs_fsize_t new_size)
1da177e4 1454{
8f04c47a
CH
1455 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;
1da177e4 1466
0b56185b
CH
1467 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1468 ASSERT(!atomic_read(&VFS_I(ip)->i_count) ||
1469 xfs_isilocked(ip, XFS_IOLOCK_EXCL));
ce7ae151 1470 ASSERT(new_size <= XFS_ISIZE(ip));
8f04c47a 1471 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1da177e4 1472 ASSERT(ip->i_itemp != NULL);
898621d5 1473 ASSERT(ip->i_itemp->ili_lock_flags == 0);
8f04c47a 1474 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1da177e4 1475
673e8e59
CH
1476 trace_xfs_itruncate_extents_start(ip, new_size);
1477
1da177e4
LT
1478 /*
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 */
8f04c47a 1487 first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
32972383 1488 last_block = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
8f04c47a
CH
1489 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;
1da177e4 1494 while (!done) {
9d87c319 1495 xfs_bmap_init(&free_list, &first_block);
8f04c47a 1496 error = xfs_bunmapi(tp, ip,
3e57ecf6 1497 first_unmap_block, unmap_len,
8f04c47a 1498 xfs_bmapi_aflag(whichfork),
1da177e4 1499 XFS_ITRUNC_MAX_EXTENTS,
3e57ecf6 1500 &first_block, &free_list,
b4e9181e 1501 &done);
8f04c47a
CH
1502 if (error)
1503 goto out_bmap_cancel;
1da177e4
LT
1504
1505 /*
1506 * Duplicate the transaction that has the permanent
1507 * reservation and commit the old transaction.
1508 */
8f04c47a 1509 error = xfs_bmap_finish(&tp, &free_list, &committed);
898621d5 1510 if (committed)
ddc3415a 1511 xfs_trans_ijoin(tp, ip, 0);
8f04c47a
CH
1512 if (error)
1513 goto out_bmap_cancel;
1da177e4
LT
1514
1515 if (committed) {
1516 /*
f6485057 1517 * Mark the inode dirty so it will be logged and
e5720eec 1518 * moved forward in the log as part of every commit.
1da177e4 1519 */
8f04c47a 1520 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1da177e4 1521 }
f6485057 1522
8f04c47a
CH
1523 ntp = xfs_trans_dup(tp);
1524 error = xfs_trans_commit(tp, 0);
1525 tp = ntp;
e5720eec 1526
ddc3415a 1527 xfs_trans_ijoin(tp, ip, 0);
f6485057 1528
cc09c0dc 1529 if (error)
8f04c47a
CH
1530 goto out;
1531
cc09c0dc 1532 /*
8f04c47a 1533 * Transaction commit worked ok so we can drop the extra ticket
cc09c0dc
DC
1534 * reference that we gained in xfs_trans_dup()
1535 */
8f04c47a 1536 xfs_log_ticket_put(tp->t_ticket);
3d3c8b52 1537 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0);
f6485057 1538 if (error)
8f04c47a 1539 goto out;
1da177e4 1540 }
8f04c47a 1541
673e8e59
CH
1542 /*
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
8f04c47a
CH
1550out:
1551 *tpp = tp;
1552 return error;
1553out_bmap_cancel:
1da177e4 1554 /*
8f04c47a
CH
1555 * 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.
1da177e4 1558 */
8f04c47a
CH
1559 xfs_bmap_cancel(&free_list);
1560 goto out;
1561}
1562
c24b5dfa
DC
1563int
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
f7be2d7f
BF
1651/*
1652 * 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
88877d2b
BF
1703/*
1704 * 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
c24b5dfa
DC
1771/*
1772 * 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 */
74564fb4 1779void
c24b5dfa
DC
1780xfs_inactive(
1781 xfs_inode_t *ip)
1782{
3d3c8b52 1783 struct xfs_mount *mp;
3d3c8b52
JL
1784 int error;
1785 int truncate = 0;
c24b5dfa
DC
1786
1787 /*
1788 * If the inode is already free, then there can be nothing
1789 * to clean up here.
1790 */
d948709b 1791 if (ip->i_d.di_mode == 0) {
c24b5dfa
DC
1792 ASSERT(ip->i_df.if_real_bytes == 0);
1793 ASSERT(ip->i_df.if_broot_bytes == 0);
74564fb4 1794 return;
c24b5dfa
DC
1795 }
1796
1797 mp = ip->i_mount;
1798
c24b5dfa
DC
1799 /* If this is a read-only mount, don't do this (would generate I/O) */
1800 if (mp->m_flags & XFS_MOUNT_RDONLY)
74564fb4 1801 return;
c24b5dfa
DC
1802
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 */
74564fb4
BF
1809 if (xfs_can_free_eofblocks(ip, true))
1810 xfs_free_eofblocks(mp, ip, false);
1811
1812 return;
c24b5dfa
DC
1813 }
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)
74564fb4 1822 return;
c24b5dfa 1823
f7be2d7f 1824 if (S_ISLNK(ip->i_d.di_mode))
36b21dde 1825 error = xfs_inactive_symlink(ip);
f7be2d7f
BF
1826 else if (truncate)
1827 error = xfs_inactive_truncate(ip);
1828 if (error)
74564fb4 1829 return;
c24b5dfa
DC
1830
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
c24b5dfa
DC
1840 error = xfs_attr_inactive(ip);
1841 if (error)
74564fb4 1842 return;
c24b5dfa
DC
1843 }
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 */
88877d2b
BF
1853 error = xfs_inactive_ifree(ip);
1854 if (error)
74564fb4 1855 return;
c24b5dfa
DC
1856
1857 /*
1858 * Release the dquots held by inode, if any.
1859 */
1860 xfs_qm_dqdetach(ip);
c24b5dfa
DC
1861}
1862
1da177e4
LT
1863/*
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;
1da177e4
LT
1878 xfs_agino_t agino;
1879 short bucket_index;
1880 int offset;
1881 int error;
1da177e4
LT
1882
1883 ASSERT(ip->i_d.di_nlink == 0);
1884 ASSERT(ip->i_d.di_mode != 0);
1da177e4
LT
1885
1886 mp = tp->t_mountp;
1887
1da177e4
LT
1888 /*
1889 * Get the agi buffer first. It ensures lock ordering
1890 * on the list.
1891 */
5e1be0fb 1892 error = xfs_read_agi(mp, tp, XFS_INO_TO_AGNO(mp, ip->i_ino), &agibp);
859d7182 1893 if (error)
1da177e4 1894 return error;
1da177e4 1895 agi = XFS_BUF_TO_AGI(agibp);
5e1be0fb 1896
1da177e4
LT
1897 /*
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]);
16259e7d 1905 ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino);
1da177e4 1906
69ef921b 1907 if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) {
1da177e4
LT
1908 /*
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 */
475ee413
CH
1914 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
1915 0, 0);
c319b58b
VA
1916 if (error)
1917 return error;
1918
69ef921b 1919 ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO));
1da177e4 1920 dip->di_next_unlinked = agi->agi_unlinked[bucket_index];
92bfc6e7 1921 offset = ip->i_imap.im_boffset +
1da177e4 1922 offsetof(xfs_dinode_t, di_next_unlinked);
0a32c26e
DC
1923
1924 /* need to recalc the inode CRC if appropriate */
1925 xfs_dinode_calc_crc(mp, dip);
1926
1da177e4
LT
1927 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);
16259e7d 1937 agi->agi_unlinked[bucket_index] = cpu_to_be32(agino);
1da177e4
LT
1938 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;
1da177e4
LT
1960 xfs_agino_t agino;
1961 xfs_agino_t next_agino;
1962 xfs_buf_t *last_ibp;
6fdf8ccc 1963 xfs_dinode_t *last_dip = NULL;
1da177e4 1964 short bucket_index;
6fdf8ccc 1965 int offset, last_offset = 0;
1da177e4 1966 int error;
1da177e4 1967
1da177e4 1968 mp = tp->t_mountp;
1da177e4 1969 agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
1da177e4
LT
1970
1971 /*
1972 * Get the agi buffer first. It ensures lock ordering
1973 * on the list.
1974 */
5e1be0fb
CH
1975 error = xfs_read_agi(mp, tp, agno, &agibp);
1976 if (error)
1da177e4 1977 return error;
5e1be0fb 1978
1da177e4 1979 agi = XFS_BUF_TO_AGI(agibp);
5e1be0fb 1980
1da177e4
LT
1981 /*
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;
69ef921b 1988 ASSERT(agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO));
1da177e4
LT
1989 ASSERT(agi->agi_unlinked[bucket_index]);
1990
16259e7d 1991 if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) {
1da177e4 1992 /*
475ee413
CH
1993 * 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.
1da177e4 1998 */
475ee413
CH
1999 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
2000 0, 0);
1da177e4 2001 if (error) {
475ee413 2002 xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.",
0b932ccc 2003 __func__, error);
1da177e4
LT
2004 return error;
2005 }
347d1c01 2006 next_agino = be32_to_cpu(dip->di_next_unlinked);
1da177e4
LT
2007 ASSERT(next_agino != 0);
2008 if (next_agino != NULLAGINO) {
347d1c01 2009 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
92bfc6e7 2010 offset = ip->i_imap.im_boffset +
1da177e4 2011 offsetof(xfs_dinode_t, di_next_unlinked);
0a32c26e
DC
2012
2013 /* need to recalc the inode CRC if appropriate */
2014 xfs_dinode_calc_crc(mp, dip);
2015
1da177e4
LT
2016 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);
16259e7d 2028 agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino);
1da177e4
LT
2029 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 */
16259e7d 2037 next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
1da177e4
LT
2038 last_ibp = NULL;
2039 while (next_agino != agino) {
129dbc9a
CH
2040 struct xfs_imap imap;
2041
2042 if (last_ibp)
1da177e4 2043 xfs_trans_brelse(tp, last_ibp);
129dbc9a
CH
2044
2045 imap.im_blkno = 0;
1da177e4 2046 next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino);
129dbc9a
CH
2047
2048 error = xfs_imap(mp, tp, next_ino, &imap, 0);
2049 if (error) {
2050 xfs_warn(mp,
2051 "%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);
1da177e4 2058 if (error) {
0b932ccc 2059 xfs_warn(mp,
129dbc9a 2060 "%s: xfs_imap_to_bp returned error %d.",
0b932ccc 2061 __func__, error);
1da177e4
LT
2062 return error;
2063 }
129dbc9a
CH
2064
2065 last_offset = imap.im_boffset;
347d1c01 2066 next_agino = be32_to_cpu(last_dip->di_next_unlinked);
1da177e4
LT
2067 ASSERT(next_agino != NULLAGINO);
2068 ASSERT(next_agino != 0);
2069 }
475ee413 2070
1da177e4 2071 /*
475ee413
CH
2072 * Now last_ibp points to the buffer previous to us on the
2073 * unlinked list. Pull us from the list.
1da177e4 2074 */
475ee413
CH
2075 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
2076 0, 0);
1da177e4 2077 if (error) {
475ee413 2078 xfs_warn(mp, "%s: xfs_imap_to_bp(2) returned error %d.",
0b932ccc 2079 __func__, error);
1da177e4
LT
2080 return error;
2081 }
347d1c01 2082 next_agino = be32_to_cpu(dip->di_next_unlinked);
1da177e4
LT
2083 ASSERT(next_agino != 0);
2084 ASSERT(next_agino != agino);
2085 if (next_agino != NULLAGINO) {
347d1c01 2086 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
92bfc6e7 2087 offset = ip->i_imap.im_boffset +
1da177e4 2088 offsetof(xfs_dinode_t, di_next_unlinked);
0a32c26e
DC
2089
2090 /* need to recalc the inode CRC if appropriate */
2091 xfs_dinode_calc_crc(mp, dip);
2092
1da177e4
LT
2093 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 */
347d1c01 2103 last_dip->di_next_unlinked = cpu_to_be32(next_agino);
1da177e4
LT
2104 ASSERT(next_agino != 0);
2105 offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked);
0a32c26e
DC
2106
2107 /* need to recalc the inode CRC if appropriate */
2108 xfs_dinode_calc_crc(mp, last_dip);
2109
1da177e4
LT
2110 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
5b3eed75 2118/*
0b8182db 2119 * A big issue when freeing the inode cluster is that we _cannot_ skip any
5b3eed75
DC
2120 * inodes that are in memory - they all must be marked stale and attached to
2121 * the cluster buffer.
2122 */
2a30f36d 2123STATIC int
1da177e4
LT
2124xfs_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;
5b257b4a 2133 int i, j;
1da177e4
LT
2134 xfs_daddr_t blkno;
2135 xfs_buf_t *bp;
5b257b4a 2136 xfs_inode_t *ip;
1da177e4
LT
2137 xfs_inode_log_item_t *iip;
2138 xfs_log_item_t *lip;
5017e97d 2139 struct xfs_perag *pag;
1da177e4 2140
5017e97d 2141 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum));
1da177e4
LT
2142 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
1da177e4
LT
2153 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
5b257b4a
DC
2157 /*
2158 * 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.
2164 */
2165 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
b6aff29f
DC
2166 mp->m_bsize * blks_per_cluster,
2167 XBF_UNMAPPED);
5b257b4a 2168
2a30f36d
CS
2169 if (!bp)
2170 return ENOMEM;
b0f539de
DC
2171
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 */
1813dd64 2181 bp->b_ops = &xfs_inode_buf_ops;
b0f539de 2182
5b257b4a
DC
2183 /*
2184 * Walk the inodes already attached to the buffer and mark them
2185 * stale. These will all have the flush locks held, so an
5b3eed75
DC
2186 * 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.
5b257b4a 2189 */
adadbeef 2190 lip = bp->b_fspriv;
5b257b4a
DC
2191 while (lip) {
2192 if (lip->li_type == XFS_LI_INODE) {
2193 iip = (xfs_inode_log_item_t *)lip;
2194 ASSERT(iip->ili_logged == 1);
ca30b2a7 2195 lip->li_cb = xfs_istale_done;
5b257b4a
DC
2196 xfs_trans_ail_copy_lsn(mp->m_ail,
2197 &iip->ili_flush_lsn,
2198 &iip->ili_item.li_lsn);
2199 xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
5b257b4a
DC
2200 }
2201 lip = lip->li_bio_list;
2202 }
1da177e4 2203
5b3eed75 2204
1da177e4 2205 /*
5b257b4a
DC
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.
1da177e4 2210 *
5b257b4a
DC
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.
1da177e4 2214 */
1da177e4 2215 for (i = 0; i < ninodes; i++) {
5b3eed75 2216retry:
1a3e8f3d 2217 rcu_read_lock();
da353b0d
DC
2218 ip = radix_tree_lookup(&pag->pag_ici_root,
2219 XFS_INO_TO_AGINO(mp, (inum + i)));
1da177e4 2220
1a3e8f3d
DC
2221 /* Inode not in memory, nothing to do */
2222 if (!ip) {
2223 rcu_read_unlock();
1da177e4
LT
2224 continue;
2225 }
2226
1a3e8f3d
DC
2227 /*
2228 * 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
5b3eed75
DC
2243 /*
2244 * 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 */
5b257b4a
DC
2250 if (ip != free_ip &&
2251 !xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
1a3e8f3d 2252 rcu_read_unlock();
5b3eed75
DC
2253 delay(1);
2254 goto retry;
1da177e4 2255 }
1a3e8f3d 2256 rcu_read_unlock();
1da177e4 2257
5b3eed75 2258 xfs_iflock(ip);
5b257b4a 2259 xfs_iflags_set(ip, XFS_ISTALE);
1da177e4 2260
5b3eed75
DC
2261 /*
2262 * we don't need to attach clean inodes or those only
2263 * with unlogged changes (which we throw away, anyway).
2264 */
1da177e4 2265 iip = ip->i_itemp;
5b3eed75 2266 if (!iip || xfs_inode_clean(ip)) {
5b257b4a 2267 ASSERT(ip != free_ip);
1da177e4
LT
2268 xfs_ifunlock(ip);
2269 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2270 continue;
2271 }
2272
f5d8d5c4
CH
2273 iip->ili_last_fields = iip->ili_fields;
2274 iip->ili_fields = 0;
1da177e4 2275 iip->ili_logged = 1;
7b2e2a31
DC
2276 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
2277 &iip->ili_item.li_lsn);
1da177e4 2278
ca30b2a7
CH
2279 xfs_buf_attach_iodone(bp, xfs_istale_done,
2280 &iip->ili_item);
5b257b4a
DC
2281
2282 if (ip != free_ip)
1da177e4 2283 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1da177e4
LT
2284 }
2285
5b3eed75 2286 xfs_trans_stale_inode_buf(tp, bp);
1da177e4
LT
2287 xfs_trans_binval(tp, bp);
2288 }
2289
5017e97d 2290 xfs_perag_put(pag);
2a30f36d 2291 return 0;
1da177e4
LT
2292}
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
579aa9ca 2314 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1da177e4
LT
2315 ASSERT(ip->i_d.di_nlink == 0);
2316 ASSERT(ip->i_d.di_nextents == 0);
2317 ASSERT(ip->i_d.di_anextents == 0);
ce7ae151 2318 ASSERT(ip->i_d.di_size == 0 || !S_ISREG(ip->i_d.di_mode));
1da177e4
LT
2319 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);
1baaed8f 2325 if (error)
1da177e4 2326 return error;
1da177e4
LT
2327
2328 error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino);
1baaed8f 2329 if (error)
1da177e4 2330 return error;
1baaed8f 2331
1da177e4
LT
2332 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 */
1da177e4
LT
2336 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
1baaed8f 2345 if (delete)
2a30f36d 2346 error = xfs_ifree_cluster(ip, tp, first_ino);
1da177e4 2347
2a30f36d 2348 return error;
1da177e4
LT
2349}
2350
1da177e4 2351/*
60ec6783
CH
2352 * 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.
1da177e4 2355 */
60ec6783 2356static void
f392e631 2357xfs_iunpin(
60ec6783 2358 struct xfs_inode *ip)
1da177e4 2359{
579aa9ca 2360 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
1da177e4 2361
4aaf15d1
DC
2362 trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
2363
a3f74ffb 2364 /* Give the log a push to start the unpinning I/O */
60ec6783 2365 xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0);
a14a348b 2366
a3f74ffb 2367}
1da177e4 2368
f392e631
CH
2369static 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
777df5af 2386void
a3f74ffb 2387xfs_iunpin_wait(
60ec6783 2388 struct xfs_inode *ip)
a3f74ffb 2389{
f392e631
CH
2390 if (xfs_ipincount(ip))
2391 __xfs_iunpin_wait(ip);
1da177e4
LT
2392}
2393
27320369
DC
2394/*
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 */
c24b5dfa
DC
2421int
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);
3d3c8b52 2471 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_remove, resblks, 0);
c24b5dfa
DC
2472 if (error == ENOSPC) {
2473 resblks = 0;
3d3c8b52 2474 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_remove, 0, 0);
c24b5dfa
DC
2475 }
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 */
27320369 2490 cancel_flags |= XFS_TRANS_ABORT;
c24b5dfa
DC
2491 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 }
c24b5dfa 2501
27320369 2502 /* Drop the link from ip's "..". */
c24b5dfa
DC
2503 error = xfs_droplink(tp, dp);
2504 if (error)
27320369 2505 goto out_trans_cancel;
c24b5dfa 2506
27320369 2507 /* Drop the "." link from ip to self. */
c24b5dfa
DC
2508 error = xfs_droplink(tp, ip);
2509 if (error)
27320369 2510 goto out_trans_cancel;
c24b5dfa
DC
2511 } 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 }
27320369 2519 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
c24b5dfa 2520
27320369 2521 /* Drop the link from dp to ip. */
c24b5dfa
DC
2522 error = xfs_droplink(tp, ip);
2523 if (error)
27320369 2524 goto out_trans_cancel;
c24b5dfa 2525
27320369 2526 /* Determine if this is the last link while the inode is locked */
c24b5dfa
DC
2527 link_zero = (ip->i_d.di_nlink == 0);
2528
27320369
DC
2529 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
c24b5dfa
DC
2537 /*
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);
c24b5dfa
DC
2566 out_trans_cancel:
2567 xfs_trans_cancel(tp, cancel_flags);
2568 std_return:
2569 return error;
2570}
2571
f6bba201
DC
2572/*
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);
3d3c8b52 2658 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_rename, spaceres, 0);
f6bba201
DC
2659 if (error == ENOSPC) {
2660 spaceres = 0;
3d3c8b52 2661 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_rename, 0, 0);
f6bba201
DC
2662 }
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
5c4d97d0
DC
2877STATIC int
2878xfs_iflush_cluster(
2879 xfs_inode_t *ip,
2880 xfs_buf_t *bp)
1da177e4 2881{
5c4d97d0
DC
2882 xfs_mount_t *mp = ip->i_mount;
2883 struct xfs_perag *pag;
2884 unsigned long first_index, mask;
2885 unsigned long inodes_per_cluster;
2886 int ilist_size;
2887 xfs_inode_t **ilist;
2888 xfs_inode_t *iq;
2889 int nr_found;
2890 int clcount = 0;
2891 int bufwasdelwri;
1da177e4 2892 int i;
1da177e4 2893
5c4d97d0 2894 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
1da177e4 2895
5c4d97d0
DC
2896 inodes_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog;
2897 ilist_size = inodes_per_cluster * sizeof(xfs_inode_t *);
2898 ilist = kmem_alloc(ilist_size, KM_MAYFAIL|KM_NOFS);
2899 if (!ilist)
2900 goto out_put;
1da177e4 2901
5c4d97d0
DC
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;
2904 rcu_read_lock();
2905 /* really need a gang lookup range call here */
2906 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)ilist,
2907 first_index, inodes_per_cluster);
2908 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)
bad55843 2914 continue;
1a3e8f3d
DC
2915
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
bad55843
DC
2930 /*
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 */
33540408 2935 if (xfs_inode_clean(iq) && xfs_ipincount(iq) == 0)
bad55843 2936 continue;
bad55843
DC
2937
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 */
33540408
DC
2959 if (!xfs_inode_clean(iq)) {
2960 int error;
bad55843
DC
2961 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:
1a3e8f3d 2979 rcu_read_unlock();
f0e2d93c 2980 kmem_free(ilist);
44b56e0a
DC
2981out_put:
2982 xfs_perag_put(pag);
bad55843
DC
2983 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 */
1a3e8f3d 2991 rcu_read_unlock();
bad55843 2992 /*
43ff2122 2993 * Clean up the buffer. If it was delwri, just release it --
bad55843
DC
2994 * brelse can handle it with no problems. If not, shut down the
2995 * filesystem before releasing the buffer.
2996 */
43ff2122 2997 bufwasdelwri = (bp->b_flags & _XBF_DELWRI_Q);
bad55843
DC
2998 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 */
cb669ca5 3009 if (bp->b_iodone) {
bad55843 3010 XFS_BUF_UNDONE(bp);
c867cb61 3011 xfs_buf_stale(bp);
5a52c2a5 3012 xfs_buf_ioerror(bp, EIO);
1a1a3e97 3013 xfs_buf_ioend(bp, 0);
bad55843 3014 } else {
c867cb61 3015 xfs_buf_stale(bp);
bad55843
DC
3016 xfs_buf_relse(bp);
3017 }
3018 }
3019
3020 /*
3021 * Unlocks the flush lock
3022 */
04913fdd 3023 xfs_iflush_abort(iq, false);
f0e2d93c 3024 kmem_free(ilist);
44b56e0a 3025 xfs_perag_put(pag);
bad55843
DC
3026 return XFS_ERROR(EFSCORRUPTED);
3027}
3028
1da177e4 3029/*
4c46819a
CH
3030 * 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.
1da177e4
LT
3037 */
3038int
3039xfs_iflush(
4c46819a
CH
3040 struct xfs_inode *ip,
3041 struct xfs_buf **bpp)
1da177e4 3042{
4c46819a
CH
3043 struct xfs_mount *mp = ip->i_mount;
3044 struct xfs_buf *bp;
3045 struct xfs_dinode *dip;
1da177e4 3046 int error;
1da177e4
LT
3047
3048 XFS_STATS_INC(xs_iflush_count);
3049
579aa9ca 3050 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
474fce06 3051 ASSERT(xfs_isiflocked(ip));
1da177e4 3052 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
8096b1eb 3053 ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
1da177e4 3054
4c46819a 3055 *bpp = NULL;
1da177e4 3056
1da177e4
LT
3057 xfs_iunpin_wait(ip);
3058
4b6a4688
DC
3059 /*
3060 * 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
475ee413 3062 * xfs_imap_to_bp() below may give us a buffer that no longer contains
4b6a4688
DC
3063 * 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
1da177e4
LT
3072 /*
3073 * This may have been unpinned because the filesystem is shutting
3074 * down forcibly. If that's the case we must not write this inode
32ce90a4
CH
3075 * 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.
1da177e4
LT
3079 */
3080 if (XFS_FORCED_SHUTDOWN(mp)) {
32ce90a4
CH
3081 error = XFS_ERROR(EIO);
3082 goto abort_out;
1da177e4
LT
3083 }
3084
a3f74ffb
DC
3085 /*
3086 * Get the buffer containing the on-disk inode.
3087 */
475ee413
CH
3088 error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &bp, XBF_TRYLOCK,
3089 0);
a3f74ffb
DC
3090 if (error || !bp) {
3091 xfs_ifunlock(ip);
3092 return error;
3093 }
3094
1da177e4
LT
3095 /*
3096 * First flush out the inode that xfs_iflush was called with.
3097 */
3098 error = xfs_iflush_int(ip, bp);
bad55843 3099 if (error)
1da177e4 3100 goto corrupt_out;
1da177e4 3101
a3f74ffb
DC
3102 /*
3103 * 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 */
811e64c7 3106 if (xfs_buf_ispinned(bp))
a14a348b 3107 xfs_log_force(mp, 0);
a3f74ffb 3108
1da177e4
LT
3109 /*
3110 * inode clustering:
3111 * see if other inodes can be gathered into this write
3112 */
bad55843
DC
3113 error = xfs_iflush_cluster(ip, bp);
3114 if (error)
3115 goto cluster_corrupt_out;
1da177e4 3116
4c46819a
CH
3117 *bpp = bp;
3118 return 0;
1da177e4
LT
3119
3120corrupt_out:
3121 xfs_buf_relse(bp);
7d04a335 3122 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1da177e4 3123cluster_corrupt_out:
32ce90a4
CH
3124 error = XFS_ERROR(EFSCORRUPTED);
3125abort_out:
1da177e4
LT
3126 /*
3127 * Unlocks the flush lock
3128 */
04913fdd 3129 xfs_iflush_abort(ip, false);
32ce90a4 3130 return error;
1da177e4
LT
3131}
3132
1da177e4
LT
3133STATIC int
3134xfs_iflush_int(
93848a99
CH
3135 struct xfs_inode *ip,
3136 struct xfs_buf *bp)
1da177e4 3137{
93848a99
CH
3138 struct xfs_inode_log_item *iip = ip->i_itemp;
3139 struct xfs_dinode *dip;
3140 struct xfs_mount *mp = ip->i_mount;
1da177e4 3141
579aa9ca 3142 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
474fce06 3143 ASSERT(xfs_isiflocked(ip));
1da177e4 3144 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
8096b1eb 3145 ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
93848a99 3146 ASSERT(iip != NULL && iip->ili_fields != 0);
1da177e4 3147
1da177e4 3148 /* set *dip = inode's place in the buffer */
92bfc6e7 3149 dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
1da177e4 3150
69ef921b 3151 if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
1da177e4 3152 mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) {
6a19d939
DC
3153 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);
1da177e4
LT
3156 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)) {
6a19d939
DC
3160 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);
1da177e4
LT
3163 goto corrupt_out;
3164 }
abbede1b 3165 if (S_ISREG(ip->i_d.di_mode)) {
1da177e4
LT
3166 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)) {
6a19d939
DC
3170 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3171 "%s: Bad regular inode %Lu, ptr 0x%p",
3172 __func__, ip->i_ino, ip);
1da177e4
LT
3173 goto corrupt_out;
3174 }
abbede1b 3175 } else if (S_ISDIR(ip->i_d.di_mode)) {
1da177e4
LT
3176 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)) {
6a19d939
DC
3181 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3182 "%s: Bad directory inode %Lu, ptr 0x%p",
3183 __func__, ip->i_ino, ip);
1da177e4
LT
3184 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)) {
6a19d939
DC
3190 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,
1da177e4 3194 ip->i_d.di_nextents + ip->i_d.di_anextents,
6a19d939 3195 ip->i_d.di_nblocks, ip);
1da177e4
LT
3196 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)) {
6a19d939
DC
3200 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);
1da177e4
LT
3203 goto corrupt_out;
3204 }
e60896d8 3205
1da177e4 3206 /*
e60896d8
DC
3207 * 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.
1da177e4 3214 */
e60896d8
DC
3215 if (ip->i_d.di_version < 3)
3216 ip->i_d.di_flushiter++;
1da177e4
LT
3217
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 */
81591fe2 3224 xfs_dinode_to_disk(dip, &ip->i_d);
1da177e4
LT
3225
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 */
51ce16d5
CH
3236 ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb));
3237 if (ip->i_d.di_version == 1) {
62118709 3238 if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
1da177e4
LT
3239 /*
3240 * Convert it back.
3241 */
3242 ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
81591fe2 3243 dip->di_onlink = cpu_to_be16(ip->i_d.di_nlink);
1da177e4
LT
3244 } else {
3245 /*
3246 * The superblock version has already been bumped,
3247 * so just make the conversion to the new inode
3248 * format permanent.
3249 */
51ce16d5
CH
3250 ip->i_d.di_version = 2;
3251 dip->di_version = 2;
1da177e4 3252 ip->i_d.di_onlink = 0;
81591fe2 3253 dip->di_onlink = 0;
1da177e4 3254 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
81591fe2
CH
3255 memset(&(dip->di_pad[0]), 0,
3256 sizeof(dip->di_pad));
6743099c 3257 ASSERT(xfs_get_projid(ip) == 0);
1da177e4
LT
3258 }
3259 }
3260
e4ac967b
DC
3261 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);
1da177e4
LT
3264 xfs_inobp_check(mp, bp);
3265
3266 /*
f5d8d5c4
CH
3267 * 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.
1da177e4 3274 *
f5d8d5c4
CH
3275 * 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.
1da177e4 3281 *
f5d8d5c4
CH
3282 * 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.
1da177e4 3290 */
93848a99
CH
3291 iip->ili_last_fields = iip->ili_fields;
3292 iip->ili_fields = 0;
3293 iip->ili_logged = 1;
1da177e4 3294
93848a99
CH
3295 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
3296 &iip->ili_item.li_lsn);
1da177e4 3297
93848a99
CH
3298 /*
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);
1da177e4 3305
93848a99
CH
3306 /* 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);
3309
3310 /* generate the checksum. */
3311 xfs_dinode_calc_crc(mp, dip);
1da177e4 3312
93848a99
CH
3313 ASSERT(bp->b_fspriv != NULL);
3314 ASSERT(bp->b_iodone != NULL);
1da177e4
LT
3315 return 0;
3316
3317corrupt_out:
3318 return XFS_ERROR(EFSCORRUPTED);
3319}
This page took 0.818 seconds and 5 git commands to generate.