Merge tag 'staging-3.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[linux-2.6.git] / fs / xfs / xfs_inode.c
blobb78481f99d9d6a683f0d18815432fce69d8d2838
1 /*
2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
3 * All Rights Reserved.
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
7 * published by the Free Software Foundation.
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.
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
18 #include <linux/log2.h>
20 #include "xfs.h"
21 #include "xfs_fs.h"
22 #include "xfs_types.h"
23 #include "xfs_log.h"
24 #include "xfs_inum.h"
25 #include "xfs_trans.h"
26 #include "xfs_trans_priv.h"
27 #include "xfs_sb.h"
28 #include "xfs_ag.h"
29 #include "xfs_mount.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_attr_sf.h"
34 #include "xfs_dinode.h"
35 #include "xfs_inode.h"
36 #include "xfs_buf_item.h"
37 #include "xfs_inode_item.h"
38 #include "xfs_btree.h"
39 #include "xfs_alloc.h"
40 #include "xfs_ialloc.h"
41 #include "xfs_bmap.h"
42 #include "xfs_error.h"
43 #include "xfs_utils.h"
44 #include "xfs_quota.h"
45 #include "xfs_filestream.h"
46 #include "xfs_vnodeops.h"
47 #include "xfs_cksum.h"
48 #include "xfs_trace.h"
49 #include "xfs_icache.h"
51 kmem_zone_t *xfs_ifork_zone;
52 kmem_zone_t *xfs_inode_zone;
55 * Used in xfs_itruncate_extents(). This is the maximum number of extents
56 * freed from a file in a single transaction.
58 #define XFS_ITRUNC_MAX_EXTENTS 2
60 STATIC int xfs_iflush_int(xfs_inode_t *, xfs_buf_t *);
61 STATIC int xfs_iformat_local(xfs_inode_t *, xfs_dinode_t *, int, int);
62 STATIC int xfs_iformat_extents(xfs_inode_t *, xfs_dinode_t *, int);
63 STATIC int xfs_iformat_btree(xfs_inode_t *, xfs_dinode_t *, int);
66 * helper function to extract extent size hint from inode
68 xfs_extlen_t
69 xfs_get_extsz_hint(
70 struct xfs_inode *ip)
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;
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.
90 * The function returns a value which should be given to the corresponding
91 * xfs_iunlock_map_shared(). This value is the mode in which the lock was
92 * actually taken.
94 uint
95 xfs_ilock_map_shared(
96 xfs_inode_t *ip)
98 uint lock_mode;
100 if ((ip->i_d.di_format == XFS_DINODE_FMT_BTREE) &&
101 ((ip->i_df.if_flags & XFS_IFEXTENTS) == 0)) {
102 lock_mode = XFS_ILOCK_EXCL;
103 } else {
104 lock_mode = XFS_ILOCK_SHARED;
107 xfs_ilock(ip, lock_mode);
109 return lock_mode;
113 * This is simply the unlock routine to go with xfs_ilock_map_shared().
114 * All it does is call xfs_iunlock() with the given lock_mode.
116 void
117 xfs_iunlock_map_shared(
118 xfs_inode_t *ip,
119 unsigned int lock_mode)
121 xfs_iunlock(ip, lock_mode);
125 * The xfs inode contains 2 locks: a multi-reader lock called the
126 * i_iolock and a multi-reader lock called the i_lock. This routine
127 * allows either or both of the locks to be obtained.
129 * The 2 locks should always be ordered so that the IO lock is
130 * obtained first in order to prevent deadlock.
132 * ip -- the inode being locked
133 * lock_flags -- this parameter indicates the inode's locks
134 * to be locked. It can be:
135 * XFS_IOLOCK_SHARED,
136 * XFS_IOLOCK_EXCL,
137 * XFS_ILOCK_SHARED,
138 * XFS_ILOCK_EXCL,
139 * XFS_IOLOCK_SHARED | XFS_ILOCK_SHARED,
140 * XFS_IOLOCK_SHARED | XFS_ILOCK_EXCL,
141 * XFS_IOLOCK_EXCL | XFS_ILOCK_SHARED,
142 * XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL
144 void
145 xfs_ilock(
146 xfs_inode_t *ip,
147 uint lock_flags)
149 trace_xfs_ilock(ip, lock_flags, _RET_IP_);
152 * You can't set both SHARED and EXCL for the same lock,
153 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
154 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
156 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
157 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
158 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
159 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
160 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
162 if (lock_flags & XFS_IOLOCK_EXCL)
163 mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
164 else if (lock_flags & XFS_IOLOCK_SHARED)
165 mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
167 if (lock_flags & XFS_ILOCK_EXCL)
168 mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
169 else if (lock_flags & XFS_ILOCK_SHARED)
170 mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
174 * This is just like xfs_ilock(), except that the caller
175 * is guaranteed not to sleep. It returns 1 if it gets
176 * the requested locks and 0 otherwise. If the IO lock is
177 * obtained but the inode lock cannot be, then the IO lock
178 * is dropped before returning.
180 * ip -- the inode being locked
181 * lock_flags -- this parameter indicates the inode's locks to be
182 * to be locked. See the comment for xfs_ilock() for a list
183 * of valid values.
186 xfs_ilock_nowait(
187 xfs_inode_t *ip,
188 uint lock_flags)
190 trace_xfs_ilock_nowait(ip, lock_flags, _RET_IP_);
193 * You can't set both SHARED and EXCL for the same lock,
194 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
195 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
197 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
198 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
199 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
200 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
201 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
203 if (lock_flags & XFS_IOLOCK_EXCL) {
204 if (!mrtryupdate(&ip->i_iolock))
205 goto out;
206 } else if (lock_flags & XFS_IOLOCK_SHARED) {
207 if (!mrtryaccess(&ip->i_iolock))
208 goto out;
210 if (lock_flags & XFS_ILOCK_EXCL) {
211 if (!mrtryupdate(&ip->i_lock))
212 goto out_undo_iolock;
213 } else if (lock_flags & XFS_ILOCK_SHARED) {
214 if (!mrtryaccess(&ip->i_lock))
215 goto out_undo_iolock;
217 return 1;
219 out_undo_iolock:
220 if (lock_flags & XFS_IOLOCK_EXCL)
221 mrunlock_excl(&ip->i_iolock);
222 else if (lock_flags & XFS_IOLOCK_SHARED)
223 mrunlock_shared(&ip->i_iolock);
224 out:
225 return 0;
229 * xfs_iunlock() is used to drop the inode locks acquired with
230 * xfs_ilock() and xfs_ilock_nowait(). The caller must pass
231 * in the flags given to xfs_ilock() or xfs_ilock_nowait() so
232 * that we know which locks to drop.
234 * ip -- the inode being unlocked
235 * lock_flags -- this parameter indicates the inode's locks to be
236 * to be unlocked. See the comment for xfs_ilock() for a list
237 * of valid values for this parameter.
240 void
241 xfs_iunlock(
242 xfs_inode_t *ip,
243 uint lock_flags)
246 * You can't set both SHARED and EXCL for the same lock,
247 * and only XFS_IOLOCK_SHARED, XFS_IOLOCK_EXCL, XFS_ILOCK_SHARED,
248 * and XFS_ILOCK_EXCL are valid values to set in lock_flags.
250 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
251 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
252 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
253 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
254 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_DEP_MASK)) == 0);
255 ASSERT(lock_flags != 0);
257 if (lock_flags & XFS_IOLOCK_EXCL)
258 mrunlock_excl(&ip->i_iolock);
259 else if (lock_flags & XFS_IOLOCK_SHARED)
260 mrunlock_shared(&ip->i_iolock);
262 if (lock_flags & XFS_ILOCK_EXCL)
263 mrunlock_excl(&ip->i_lock);
264 else if (lock_flags & XFS_ILOCK_SHARED)
265 mrunlock_shared(&ip->i_lock);
267 trace_xfs_iunlock(ip, lock_flags, _RET_IP_);
271 * give up write locks. the i/o lock cannot be held nested
272 * if it is being demoted.
274 void
275 xfs_ilock_demote(
276 xfs_inode_t *ip,
277 uint lock_flags)
279 ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL));
280 ASSERT((lock_flags & ~(XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
282 if (lock_flags & XFS_ILOCK_EXCL)
283 mrdemote(&ip->i_lock);
284 if (lock_flags & XFS_IOLOCK_EXCL)
285 mrdemote(&ip->i_iolock);
287 trace_xfs_ilock_demote(ip, lock_flags, _RET_IP_);
290 #if defined(DEBUG) || defined(XFS_WARN)
292 xfs_isilocked(
293 xfs_inode_t *ip,
294 uint lock_flags)
296 if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) {
297 if (!(lock_flags & XFS_ILOCK_SHARED))
298 return !!ip->i_lock.mr_writer;
299 return rwsem_is_locked(&ip->i_lock.mr_lock);
302 if (lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) {
303 if (!(lock_flags & XFS_IOLOCK_SHARED))
304 return !!ip->i_iolock.mr_writer;
305 return rwsem_is_locked(&ip->i_iolock.mr_lock);
308 ASSERT(0);
309 return 0;
311 #endif
313 void
314 __xfs_iflock(
315 struct xfs_inode *ip)
317 wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IFLOCK_BIT);
318 DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IFLOCK_BIT);
320 do {
321 prepare_to_wait_exclusive(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
322 if (xfs_isiflocked(ip))
323 io_schedule();
324 } while (!xfs_iflock_nowait(ip));
326 finish_wait(wq, &wait.wait);
329 #ifdef DEBUG
331 * Make sure that the extents in the given memory buffer
332 * are valid.
334 STATIC void
335 xfs_validate_extents(
336 xfs_ifork_t *ifp,
337 int nrecs,
338 xfs_exntfmt_t fmt)
340 xfs_bmbt_irec_t irec;
341 xfs_bmbt_rec_host_t rec;
342 int i;
344 for (i = 0; i < nrecs; i++) {
345 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
346 rec.l0 = get_unaligned(&ep->l0);
347 rec.l1 = get_unaligned(&ep->l1);
348 xfs_bmbt_get_all(&rec, &irec);
349 if (fmt == XFS_EXTFMT_NOSTATE)
350 ASSERT(irec.br_state == XFS_EXT_NORM);
353 #else /* DEBUG */
354 #define xfs_validate_extents(ifp, nrecs, fmt)
355 #endif /* DEBUG */
358 * Check that none of the inode's in the buffer have a next
359 * unlinked field of 0.
361 #if defined(DEBUG)
362 void
363 xfs_inobp_check(
364 xfs_mount_t *mp,
365 xfs_buf_t *bp)
367 int i;
368 int j;
369 xfs_dinode_t *dip;
371 j = mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog;
373 for (i = 0; i < j; i++) {
374 dip = (xfs_dinode_t *)xfs_buf_offset(bp,
375 i * mp->m_sb.sb_inodesize);
376 if (!dip->di_next_unlinked) {
377 xfs_alert(mp,
378 "Detected bogus zero next_unlinked field in incore inode buffer 0x%p.",
379 bp);
380 ASSERT(dip->di_next_unlinked);
384 #endif
386 static void
387 xfs_inode_buf_verify(
388 struct xfs_buf *bp)
390 struct xfs_mount *mp = bp->b_target->bt_mount;
391 int i;
392 int ni;
395 * Validate the magic number and version of every inode in the buffer
397 ni = XFS_BB_TO_FSB(mp, bp->b_length) * mp->m_sb.sb_inopblock;
398 for (i = 0; i < ni; i++) {
399 int di_ok;
400 xfs_dinode_t *dip;
402 dip = (struct xfs_dinode *)xfs_buf_offset(bp,
403 (i << mp->m_sb.sb_inodelog));
404 di_ok = dip->di_magic == cpu_to_be16(XFS_DINODE_MAGIC) &&
405 XFS_DINODE_GOOD_VERSION(dip->di_version);
406 if (unlikely(XFS_TEST_ERROR(!di_ok, mp,
407 XFS_ERRTAG_ITOBP_INOTOBP,
408 XFS_RANDOM_ITOBP_INOTOBP))) {
409 xfs_buf_ioerror(bp, EFSCORRUPTED);
410 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_HIGH,
411 mp, dip);
412 #ifdef DEBUG
413 xfs_emerg(mp,
414 "bad inode magic/vsn daddr %lld #%d (magic=%x)",
415 (unsigned long long)bp->b_bn, i,
416 be16_to_cpu(dip->di_magic));
417 ASSERT(0);
418 #endif
421 xfs_inobp_check(mp, bp);
425 static void
426 xfs_inode_buf_read_verify(
427 struct xfs_buf *bp)
429 xfs_inode_buf_verify(bp);
432 static void
433 xfs_inode_buf_write_verify(
434 struct xfs_buf *bp)
436 xfs_inode_buf_verify(bp);
439 const struct xfs_buf_ops xfs_inode_buf_ops = {
440 .verify_read = xfs_inode_buf_read_verify,
441 .verify_write = xfs_inode_buf_write_verify,
446 * This routine is called to map an inode to the buffer containing the on-disk
447 * version of the inode. It returns a pointer to the buffer containing the
448 * on-disk inode in the bpp parameter, and in the dipp parameter it returns a
449 * pointer to the on-disk inode within that buffer.
451 * If a non-zero error is returned, then the contents of bpp and dipp are
452 * undefined.
455 xfs_imap_to_bp(
456 struct xfs_mount *mp,
457 struct xfs_trans *tp,
458 struct xfs_imap *imap,
459 struct xfs_dinode **dipp,
460 struct xfs_buf **bpp,
461 uint buf_flags,
462 uint iget_flags)
464 struct xfs_buf *bp;
465 int error;
467 buf_flags |= XBF_UNMAPPED;
468 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, imap->im_blkno,
469 (int)imap->im_len, buf_flags, &bp,
470 &xfs_inode_buf_ops);
471 if (error) {
472 if (error == EAGAIN) {
473 ASSERT(buf_flags & XBF_TRYLOCK);
474 return error;
477 if (error == EFSCORRUPTED &&
478 (iget_flags & XFS_IGET_UNTRUSTED))
479 return XFS_ERROR(EINVAL);
481 xfs_warn(mp, "%s: xfs_trans_read_buf() returned error %d.",
482 __func__, error);
483 return error;
486 *bpp = bp;
487 *dipp = (struct xfs_dinode *)xfs_buf_offset(bp, imap->im_boffset);
488 return 0;
492 * Move inode type and inode format specific information from the
493 * on-disk inode to the in-core inode. For fifos, devs, and sockets
494 * this means set if_rdev to the proper value. For files, directories,
495 * and symlinks this means to bring in the in-line data or extent
496 * pointers. For a file in B-tree format, only the root is immediately
497 * brought in-core. The rest will be in-lined in if_extents when it
498 * is first referenced (see xfs_iread_extents()).
500 STATIC int
501 xfs_iformat(
502 xfs_inode_t *ip,
503 xfs_dinode_t *dip)
505 xfs_attr_shortform_t *atp;
506 int size;
507 int error = 0;
508 xfs_fsize_t di_size;
510 if (unlikely(be32_to_cpu(dip->di_nextents) +
511 be16_to_cpu(dip->di_anextents) >
512 be64_to_cpu(dip->di_nblocks))) {
513 xfs_warn(ip->i_mount,
514 "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.",
515 (unsigned long long)ip->i_ino,
516 (int)(be32_to_cpu(dip->di_nextents) +
517 be16_to_cpu(dip->di_anextents)),
518 (unsigned long long)
519 be64_to_cpu(dip->di_nblocks));
520 XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW,
521 ip->i_mount, dip);
522 return XFS_ERROR(EFSCORRUPTED);
525 if (unlikely(dip->di_forkoff > ip->i_mount->m_sb.sb_inodesize)) {
526 xfs_warn(ip->i_mount, "corrupt dinode %Lu, forkoff = 0x%x.",
527 (unsigned long long)ip->i_ino,
528 dip->di_forkoff);
529 XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW,
530 ip->i_mount, dip);
531 return XFS_ERROR(EFSCORRUPTED);
534 if (unlikely((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) &&
535 !ip->i_mount->m_rtdev_targp)) {
536 xfs_warn(ip->i_mount,
537 "corrupt dinode %Lu, has realtime flag set.",
538 ip->i_ino);
539 XFS_CORRUPTION_ERROR("xfs_iformat(realtime)",
540 XFS_ERRLEVEL_LOW, ip->i_mount, dip);
541 return XFS_ERROR(EFSCORRUPTED);
544 switch (ip->i_d.di_mode & S_IFMT) {
545 case S_IFIFO:
546 case S_IFCHR:
547 case S_IFBLK:
548 case S_IFSOCK:
549 if (unlikely(dip->di_format != XFS_DINODE_FMT_DEV)) {
550 XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW,
551 ip->i_mount, dip);
552 return XFS_ERROR(EFSCORRUPTED);
554 ip->i_d.di_size = 0;
555 ip->i_df.if_u2.if_rdev = xfs_dinode_get_rdev(dip);
556 break;
558 case S_IFREG:
559 case S_IFLNK:
560 case S_IFDIR:
561 switch (dip->di_format) {
562 case XFS_DINODE_FMT_LOCAL:
564 * no local regular files yet
566 if (unlikely(S_ISREG(be16_to_cpu(dip->di_mode)))) {
567 xfs_warn(ip->i_mount,
568 "corrupt inode %Lu (local format for regular file).",
569 (unsigned long long) ip->i_ino);
570 XFS_CORRUPTION_ERROR("xfs_iformat(4)",
571 XFS_ERRLEVEL_LOW,
572 ip->i_mount, dip);
573 return XFS_ERROR(EFSCORRUPTED);
576 di_size = be64_to_cpu(dip->di_size);
577 if (unlikely(di_size > XFS_DFORK_DSIZE(dip, ip->i_mount))) {
578 xfs_warn(ip->i_mount,
579 "corrupt inode %Lu (bad size %Ld for local inode).",
580 (unsigned long long) ip->i_ino,
581 (long long) di_size);
582 XFS_CORRUPTION_ERROR("xfs_iformat(5)",
583 XFS_ERRLEVEL_LOW,
584 ip->i_mount, dip);
585 return XFS_ERROR(EFSCORRUPTED);
588 size = (int)di_size;
589 error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size);
590 break;
591 case XFS_DINODE_FMT_EXTENTS:
592 error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
593 break;
594 case XFS_DINODE_FMT_BTREE:
595 error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
596 break;
597 default:
598 XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW,
599 ip->i_mount);
600 return XFS_ERROR(EFSCORRUPTED);
602 break;
604 default:
605 XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW, ip->i_mount);
606 return XFS_ERROR(EFSCORRUPTED);
608 if (error) {
609 return error;
611 if (!XFS_DFORK_Q(dip))
612 return 0;
614 ASSERT(ip->i_afp == NULL);
615 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP | KM_NOFS);
617 switch (dip->di_aformat) {
618 case XFS_DINODE_FMT_LOCAL:
619 atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
620 size = be16_to_cpu(atp->hdr.totsize);
622 if (unlikely(size < sizeof(struct xfs_attr_sf_hdr))) {
623 xfs_warn(ip->i_mount,
624 "corrupt inode %Lu (bad attr fork size %Ld).",
625 (unsigned long long) ip->i_ino,
626 (long long) size);
627 XFS_CORRUPTION_ERROR("xfs_iformat(8)",
628 XFS_ERRLEVEL_LOW,
629 ip->i_mount, dip);
630 return XFS_ERROR(EFSCORRUPTED);
633 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size);
634 break;
635 case XFS_DINODE_FMT_EXTENTS:
636 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
637 break;
638 case XFS_DINODE_FMT_BTREE:
639 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
640 break;
641 default:
642 error = XFS_ERROR(EFSCORRUPTED);
643 break;
645 if (error) {
646 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
647 ip->i_afp = NULL;
648 xfs_idestroy_fork(ip, XFS_DATA_FORK);
650 return error;
654 * The file is in-lined in the on-disk inode.
655 * If it fits into if_inline_data, then copy
656 * it there, otherwise allocate a buffer for it
657 * and copy the data there. Either way, set
658 * if_data to point at the data.
659 * If we allocate a buffer for the data, make
660 * sure that its size is a multiple of 4 and
661 * record the real size in i_real_bytes.
663 STATIC int
664 xfs_iformat_local(
665 xfs_inode_t *ip,
666 xfs_dinode_t *dip,
667 int whichfork,
668 int size)
670 xfs_ifork_t *ifp;
671 int real_size;
674 * If the size is unreasonable, then something
675 * is wrong and we just bail out rather than crash in
676 * kmem_alloc() or memcpy() below.
678 if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
679 xfs_warn(ip->i_mount,
680 "corrupt inode %Lu (bad size %d for local fork, size = %d).",
681 (unsigned long long) ip->i_ino, size,
682 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
683 XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW,
684 ip->i_mount, dip);
685 return XFS_ERROR(EFSCORRUPTED);
687 ifp = XFS_IFORK_PTR(ip, whichfork);
688 real_size = 0;
689 if (size == 0)
690 ifp->if_u1.if_data = NULL;
691 else if (size <= sizeof(ifp->if_u2.if_inline_data))
692 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
693 else {
694 real_size = roundup(size, 4);
695 ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP | KM_NOFS);
697 ifp->if_bytes = size;
698 ifp->if_real_bytes = real_size;
699 if (size)
700 memcpy(ifp->if_u1.if_data, XFS_DFORK_PTR(dip, whichfork), size);
701 ifp->if_flags &= ~XFS_IFEXTENTS;
702 ifp->if_flags |= XFS_IFINLINE;
703 return 0;
707 * The file consists of a set of extents all
708 * of which fit into the on-disk inode.
709 * If there are few enough extents to fit into
710 * the if_inline_ext, then copy them there.
711 * Otherwise allocate a buffer for them and copy
712 * them into it. Either way, set if_extents
713 * to point at the extents.
715 STATIC int
716 xfs_iformat_extents(
717 xfs_inode_t *ip,
718 xfs_dinode_t *dip,
719 int whichfork)
721 xfs_bmbt_rec_t *dp;
722 xfs_ifork_t *ifp;
723 int nex;
724 int size;
725 int i;
727 ifp = XFS_IFORK_PTR(ip, whichfork);
728 nex = XFS_DFORK_NEXTENTS(dip, whichfork);
729 size = nex * (uint)sizeof(xfs_bmbt_rec_t);
732 * If the number of extents is unreasonable, then something
733 * is wrong and we just bail out rather than crash in
734 * kmem_alloc() or memcpy() below.
736 if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
737 xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).",
738 (unsigned long long) ip->i_ino, nex);
739 XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW,
740 ip->i_mount, dip);
741 return XFS_ERROR(EFSCORRUPTED);
744 ifp->if_real_bytes = 0;
745 if (nex == 0)
746 ifp->if_u1.if_extents = NULL;
747 else if (nex <= XFS_INLINE_EXTS)
748 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
749 else
750 xfs_iext_add(ifp, 0, nex);
752 ifp->if_bytes = size;
753 if (size) {
754 dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
755 xfs_validate_extents(ifp, nex, XFS_EXTFMT_INODE(ip));
756 for (i = 0; i < nex; i++, dp++) {
757 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
758 ep->l0 = get_unaligned_be64(&dp->l0);
759 ep->l1 = get_unaligned_be64(&dp->l1);
761 XFS_BMAP_TRACE_EXLIST(ip, nex, whichfork);
762 if (whichfork != XFS_DATA_FORK ||
763 XFS_EXTFMT_INODE(ip) == XFS_EXTFMT_NOSTATE)
764 if (unlikely(xfs_check_nostate_extents(
765 ifp, 0, nex))) {
766 XFS_ERROR_REPORT("xfs_iformat_extents(2)",
767 XFS_ERRLEVEL_LOW,
768 ip->i_mount);
769 return XFS_ERROR(EFSCORRUPTED);
772 ifp->if_flags |= XFS_IFEXTENTS;
773 return 0;
777 * The file has too many extents to fit into
778 * the inode, so they are in B-tree format.
779 * Allocate a buffer for the root of the B-tree
780 * and copy the root into it. The i_extents
781 * field will remain NULL until all of the
782 * extents are read in (when they are needed).
784 STATIC int
785 xfs_iformat_btree(
786 xfs_inode_t *ip,
787 xfs_dinode_t *dip,
788 int whichfork)
790 struct xfs_mount *mp = ip->i_mount;
791 xfs_bmdr_block_t *dfp;
792 xfs_ifork_t *ifp;
793 /* REFERENCED */
794 int nrecs;
795 int size;
797 ifp = XFS_IFORK_PTR(ip, whichfork);
798 dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
799 size = XFS_BMAP_BROOT_SPACE(mp, dfp);
800 nrecs = be16_to_cpu(dfp->bb_numrecs);
803 * blow out if -- fork has less extents than can fit in
804 * fork (fork shouldn't be a btree format), root btree
805 * block has more records than can fit into the fork,
806 * or the number of extents is greater than the number of
807 * blocks.
809 if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <=
810 XFS_IFORK_MAXEXT(ip, whichfork) ||
811 XFS_BMDR_SPACE_CALC(nrecs) >
812 XFS_DFORK_SIZE(dip, mp, whichfork) ||
813 XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks)) {
814 xfs_warn(mp, "corrupt inode %Lu (btree).",
815 (unsigned long long) ip->i_ino);
816 XFS_CORRUPTION_ERROR("xfs_iformat_btree", XFS_ERRLEVEL_LOW,
817 mp, dip);
818 return XFS_ERROR(EFSCORRUPTED);
821 ifp->if_broot_bytes = size;
822 ifp->if_broot = kmem_alloc(size, KM_SLEEP | KM_NOFS);
823 ASSERT(ifp->if_broot != NULL);
825 * Copy and convert from the on-disk structure
826 * to the in-memory structure.
828 xfs_bmdr_to_bmbt(ip, dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
829 ifp->if_broot, size);
830 ifp->if_flags &= ~XFS_IFEXTENTS;
831 ifp->if_flags |= XFS_IFBROOT;
833 return 0;
836 STATIC void
837 xfs_dinode_from_disk(
838 xfs_icdinode_t *to,
839 xfs_dinode_t *from)
841 to->di_magic = be16_to_cpu(from->di_magic);
842 to->di_mode = be16_to_cpu(from->di_mode);
843 to->di_version = from ->di_version;
844 to->di_format = from->di_format;
845 to->di_onlink = be16_to_cpu(from->di_onlink);
846 to->di_uid = be32_to_cpu(from->di_uid);
847 to->di_gid = be32_to_cpu(from->di_gid);
848 to->di_nlink = be32_to_cpu(from->di_nlink);
849 to->di_projid_lo = be16_to_cpu(from->di_projid_lo);
850 to->di_projid_hi = be16_to_cpu(from->di_projid_hi);
851 memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
852 to->di_flushiter = be16_to_cpu(from->di_flushiter);
853 to->di_atime.t_sec = be32_to_cpu(from->di_atime.t_sec);
854 to->di_atime.t_nsec = be32_to_cpu(from->di_atime.t_nsec);
855 to->di_mtime.t_sec = be32_to_cpu(from->di_mtime.t_sec);
856 to->di_mtime.t_nsec = be32_to_cpu(from->di_mtime.t_nsec);
857 to->di_ctime.t_sec = be32_to_cpu(from->di_ctime.t_sec);
858 to->di_ctime.t_nsec = be32_to_cpu(from->di_ctime.t_nsec);
859 to->di_size = be64_to_cpu(from->di_size);
860 to->di_nblocks = be64_to_cpu(from->di_nblocks);
861 to->di_extsize = be32_to_cpu(from->di_extsize);
862 to->di_nextents = be32_to_cpu(from->di_nextents);
863 to->di_anextents = be16_to_cpu(from->di_anextents);
864 to->di_forkoff = from->di_forkoff;
865 to->di_aformat = from->di_aformat;
866 to->di_dmevmask = be32_to_cpu(from->di_dmevmask);
867 to->di_dmstate = be16_to_cpu(from->di_dmstate);
868 to->di_flags = be16_to_cpu(from->di_flags);
869 to->di_gen = be32_to_cpu(from->di_gen);
871 if (to->di_version == 3) {
872 to->di_changecount = be64_to_cpu(from->di_changecount);
873 to->di_crtime.t_sec = be32_to_cpu(from->di_crtime.t_sec);
874 to->di_crtime.t_nsec = be32_to_cpu(from->di_crtime.t_nsec);
875 to->di_flags2 = be64_to_cpu(from->di_flags2);
876 to->di_ino = be64_to_cpu(from->di_ino);
877 to->di_lsn = be64_to_cpu(from->di_lsn);
878 memcpy(to->di_pad2, from->di_pad2, sizeof(to->di_pad2));
879 uuid_copy(&to->di_uuid, &from->di_uuid);
883 void
884 xfs_dinode_to_disk(
885 xfs_dinode_t *to,
886 xfs_icdinode_t *from)
888 to->di_magic = cpu_to_be16(from->di_magic);
889 to->di_mode = cpu_to_be16(from->di_mode);
890 to->di_version = from ->di_version;
891 to->di_format = from->di_format;
892 to->di_onlink = cpu_to_be16(from->di_onlink);
893 to->di_uid = cpu_to_be32(from->di_uid);
894 to->di_gid = cpu_to_be32(from->di_gid);
895 to->di_nlink = cpu_to_be32(from->di_nlink);
896 to->di_projid_lo = cpu_to_be16(from->di_projid_lo);
897 to->di_projid_hi = cpu_to_be16(from->di_projid_hi);
898 memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
899 to->di_flushiter = cpu_to_be16(from->di_flushiter);
900 to->di_atime.t_sec = cpu_to_be32(from->di_atime.t_sec);
901 to->di_atime.t_nsec = cpu_to_be32(from->di_atime.t_nsec);
902 to->di_mtime.t_sec = cpu_to_be32(from->di_mtime.t_sec);
903 to->di_mtime.t_nsec = cpu_to_be32(from->di_mtime.t_nsec);
904 to->di_ctime.t_sec = cpu_to_be32(from->di_ctime.t_sec);
905 to->di_ctime.t_nsec = cpu_to_be32(from->di_ctime.t_nsec);
906 to->di_size = cpu_to_be64(from->di_size);
907 to->di_nblocks = cpu_to_be64(from->di_nblocks);
908 to->di_extsize = cpu_to_be32(from->di_extsize);
909 to->di_nextents = cpu_to_be32(from->di_nextents);
910 to->di_anextents = cpu_to_be16(from->di_anextents);
911 to->di_forkoff = from->di_forkoff;
912 to->di_aformat = from->di_aformat;
913 to->di_dmevmask = cpu_to_be32(from->di_dmevmask);
914 to->di_dmstate = cpu_to_be16(from->di_dmstate);
915 to->di_flags = cpu_to_be16(from->di_flags);
916 to->di_gen = cpu_to_be32(from->di_gen);
918 if (from->di_version == 3) {
919 to->di_changecount = cpu_to_be64(from->di_changecount);
920 to->di_crtime.t_sec = cpu_to_be32(from->di_crtime.t_sec);
921 to->di_crtime.t_nsec = cpu_to_be32(from->di_crtime.t_nsec);
922 to->di_flags2 = cpu_to_be64(from->di_flags2);
923 to->di_ino = cpu_to_be64(from->di_ino);
924 to->di_lsn = cpu_to_be64(from->di_lsn);
925 memcpy(to->di_pad2, from->di_pad2, sizeof(to->di_pad2));
926 uuid_copy(&to->di_uuid, &from->di_uuid);
930 STATIC uint
931 _xfs_dic2xflags(
932 __uint16_t di_flags)
934 uint flags = 0;
936 if (di_flags & XFS_DIFLAG_ANY) {
937 if (di_flags & XFS_DIFLAG_REALTIME)
938 flags |= XFS_XFLAG_REALTIME;
939 if (di_flags & XFS_DIFLAG_PREALLOC)
940 flags |= XFS_XFLAG_PREALLOC;
941 if (di_flags & XFS_DIFLAG_IMMUTABLE)
942 flags |= XFS_XFLAG_IMMUTABLE;
943 if (di_flags & XFS_DIFLAG_APPEND)
944 flags |= XFS_XFLAG_APPEND;
945 if (di_flags & XFS_DIFLAG_SYNC)
946 flags |= XFS_XFLAG_SYNC;
947 if (di_flags & XFS_DIFLAG_NOATIME)
948 flags |= XFS_XFLAG_NOATIME;
949 if (di_flags & XFS_DIFLAG_NODUMP)
950 flags |= XFS_XFLAG_NODUMP;
951 if (di_flags & XFS_DIFLAG_RTINHERIT)
952 flags |= XFS_XFLAG_RTINHERIT;
953 if (di_flags & XFS_DIFLAG_PROJINHERIT)
954 flags |= XFS_XFLAG_PROJINHERIT;
955 if (di_flags & XFS_DIFLAG_NOSYMLINKS)
956 flags |= XFS_XFLAG_NOSYMLINKS;
957 if (di_flags & XFS_DIFLAG_EXTSIZE)
958 flags |= XFS_XFLAG_EXTSIZE;
959 if (di_flags & XFS_DIFLAG_EXTSZINHERIT)
960 flags |= XFS_XFLAG_EXTSZINHERIT;
961 if (di_flags & XFS_DIFLAG_NODEFRAG)
962 flags |= XFS_XFLAG_NODEFRAG;
963 if (di_flags & XFS_DIFLAG_FILESTREAM)
964 flags |= XFS_XFLAG_FILESTREAM;
967 return flags;
970 uint
971 xfs_ip2xflags(
972 xfs_inode_t *ip)
974 xfs_icdinode_t *dic = &ip->i_d;
976 return _xfs_dic2xflags(dic->di_flags) |
977 (XFS_IFORK_Q(ip) ? XFS_XFLAG_HASATTR : 0);
980 uint
981 xfs_dic2xflags(
982 xfs_dinode_t *dip)
984 return _xfs_dic2xflags(be16_to_cpu(dip->di_flags)) |
985 (XFS_DFORK_Q(dip) ? XFS_XFLAG_HASATTR : 0);
988 static bool
989 xfs_dinode_verify(
990 struct xfs_mount *mp,
991 struct xfs_inode *ip,
992 struct xfs_dinode *dip)
994 if (dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC))
995 return false;
997 /* only version 3 or greater inodes are extensively verified here */
998 if (dip->di_version < 3)
999 return true;
1001 if (!xfs_sb_version_hascrc(&mp->m_sb))
1002 return false;
1003 if (!xfs_verify_cksum((char *)dip, mp->m_sb.sb_inodesize,
1004 offsetof(struct xfs_dinode, di_crc)))
1005 return false;
1006 if (be64_to_cpu(dip->di_ino) != ip->i_ino)
1007 return false;
1008 if (!uuid_equal(&dip->di_uuid, &mp->m_sb.sb_uuid))
1009 return false;
1010 return true;
1013 void
1014 xfs_dinode_calc_crc(
1015 struct xfs_mount *mp,
1016 struct xfs_dinode *dip)
1018 __uint32_t crc;
1020 if (dip->di_version < 3)
1021 return;
1023 ASSERT(xfs_sb_version_hascrc(&mp->m_sb));
1024 crc = xfs_start_cksum((char *)dip, mp->m_sb.sb_inodesize,
1025 offsetof(struct xfs_dinode, di_crc));
1026 dip->di_crc = xfs_end_cksum(crc);
1030 * Read the disk inode attributes into the in-core inode structure.
1032 * If we are initialising a new inode and we are not utilising the
1033 * XFS_MOUNT_IKEEP inode cluster mode, we can simple build the new inode core
1034 * with a random generation number. If we are keeping inodes around, we need to
1035 * read the inode cluster to get the existing generation number off disk.
1038 xfs_iread(
1039 xfs_mount_t *mp,
1040 xfs_trans_t *tp,
1041 xfs_inode_t *ip,
1042 uint iget_flags)
1044 xfs_buf_t *bp;
1045 xfs_dinode_t *dip;
1046 int error;
1049 * Fill in the location information in the in-core inode.
1051 error = xfs_imap(mp, tp, ip->i_ino, &ip->i_imap, iget_flags);
1052 if (error)
1053 return error;
1055 /* shortcut IO on inode allocation if possible */
1056 if ((iget_flags & XFS_IGET_CREATE) &&
1057 !(mp->m_flags & XFS_MOUNT_IKEEP)) {
1058 /* initialise the on-disk inode core */
1059 memset(&ip->i_d, 0, sizeof(ip->i_d));
1060 ip->i_d.di_magic = XFS_DINODE_MAGIC;
1061 ip->i_d.di_gen = prandom_u32();
1062 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1063 ip->i_d.di_version = 3;
1064 ip->i_d.di_ino = ip->i_ino;
1065 uuid_copy(&ip->i_d.di_uuid, &mp->m_sb.sb_uuid);
1066 } else
1067 ip->i_d.di_version = 2;
1068 return 0;
1072 * Get pointers to the on-disk inode and the buffer containing it.
1074 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &bp, 0, iget_flags);
1075 if (error)
1076 return error;
1078 /* even unallocated inodes are verified */
1079 if (!xfs_dinode_verify(mp, ip, dip)) {
1080 xfs_alert(mp, "%s: validation failed for inode %lld failed",
1081 __func__, ip->i_ino);
1083 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, dip);
1084 error = XFS_ERROR(EFSCORRUPTED);
1085 goto out_brelse;
1089 * If the on-disk inode is already linked to a directory
1090 * entry, copy all of the inode into the in-core inode.
1091 * xfs_iformat() handles copying in the inode format
1092 * specific information.
1093 * Otherwise, just get the truly permanent information.
1095 if (dip->di_mode) {
1096 xfs_dinode_from_disk(&ip->i_d, dip);
1097 error = xfs_iformat(ip, dip);
1098 if (error) {
1099 #ifdef DEBUG
1100 xfs_alert(mp, "%s: xfs_iformat() returned error %d",
1101 __func__, error);
1102 #endif /* DEBUG */
1103 goto out_brelse;
1105 } else {
1107 * Partial initialisation of the in-core inode. Just the bits
1108 * that xfs_ialloc won't overwrite or relies on being correct.
1110 ip->i_d.di_magic = be16_to_cpu(dip->di_magic);
1111 ip->i_d.di_version = dip->di_version;
1112 ip->i_d.di_gen = be32_to_cpu(dip->di_gen);
1113 ip->i_d.di_flushiter = be16_to_cpu(dip->di_flushiter);
1115 if (dip->di_version == 3) {
1116 ip->i_d.di_ino = be64_to_cpu(dip->di_ino);
1117 uuid_copy(&ip->i_d.di_uuid, &dip->di_uuid);
1121 * Make sure to pull in the mode here as well in
1122 * case the inode is released without being used.
1123 * This ensures that xfs_inactive() will see that
1124 * the inode is already free and not try to mess
1125 * with the uninitialized part of it.
1127 ip->i_d.di_mode = 0;
1131 * The inode format changed when we moved the link count and
1132 * made it 32 bits long. If this is an old format inode,
1133 * convert it in memory to look like a new one. If it gets
1134 * flushed to disk we will convert back before flushing or
1135 * logging it. We zero out the new projid field and the old link
1136 * count field. We'll handle clearing the pad field (the remains
1137 * of the old uuid field) when we actually convert the inode to
1138 * the new format. We don't change the version number so that we
1139 * can distinguish this from a real new format inode.
1141 if (ip->i_d.di_version == 1) {
1142 ip->i_d.di_nlink = ip->i_d.di_onlink;
1143 ip->i_d.di_onlink = 0;
1144 xfs_set_projid(ip, 0);
1147 ip->i_delayed_blks = 0;
1150 * Mark the buffer containing the inode as something to keep
1151 * around for a while. This helps to keep recently accessed
1152 * meta-data in-core longer.
1154 xfs_buf_set_ref(bp, XFS_INO_REF);
1157 * Use xfs_trans_brelse() to release the buffer containing the on-disk
1158 * inode, because it was acquired with xfs_trans_read_buf() in
1159 * xfs_imap_to_bp() above. If tp is NULL, this is just a normal
1160 * brelse(). If we're within a transaction, then xfs_trans_brelse()
1161 * will only release the buffer if it is not dirty within the
1162 * transaction. It will be OK to release the buffer in this case,
1163 * because inodes on disk are never destroyed and we will be locking the
1164 * new in-core inode before putting it in the cache where other
1165 * processes can find it. Thus we don't have to worry about the inode
1166 * being changed just because we released the buffer.
1168 out_brelse:
1169 xfs_trans_brelse(tp, bp);
1170 return error;
1174 * Read in extents from a btree-format inode.
1175 * Allocate and fill in if_extents. Real work is done in xfs_bmap.c.
1178 xfs_iread_extents(
1179 xfs_trans_t *tp,
1180 xfs_inode_t *ip,
1181 int whichfork)
1183 int error;
1184 xfs_ifork_t *ifp;
1185 xfs_extnum_t nextents;
1187 if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
1188 XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW,
1189 ip->i_mount);
1190 return XFS_ERROR(EFSCORRUPTED);
1192 nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
1193 ifp = XFS_IFORK_PTR(ip, whichfork);
1196 * We know that the size is valid (it's checked in iformat_btree)
1198 ifp->if_bytes = ifp->if_real_bytes = 0;
1199 ifp->if_flags |= XFS_IFEXTENTS;
1200 xfs_iext_add(ifp, 0, nextents);
1201 error = xfs_bmap_read_extents(tp, ip, whichfork);
1202 if (error) {
1203 xfs_iext_destroy(ifp);
1204 ifp->if_flags &= ~XFS_IFEXTENTS;
1205 return error;
1207 xfs_validate_extents(ifp, nextents, XFS_EXTFMT_INODE(ip));
1208 return 0;
1212 * Allocate an inode on disk and return a copy of its in-core version.
1213 * The in-core inode is locked exclusively. Set mode, nlink, and rdev
1214 * appropriately within the inode. The uid and gid for the inode are
1215 * set according to the contents of the given cred structure.
1217 * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc()
1218 * has a free inode available, call xfs_iget() to obtain the in-core
1219 * version of the allocated inode. Finally, fill in the inode and
1220 * log its initial contents. In this case, ialloc_context would be
1221 * set to NULL.
1223 * If xfs_dialloc() does not have an available inode, it will replenish
1224 * its supply by doing an allocation. Since we can only do one
1225 * allocation within a transaction without deadlocks, we must commit
1226 * the current transaction before returning the inode itself.
1227 * In this case, therefore, we will set ialloc_context and return.
1228 * The caller should then commit the current transaction, start a new
1229 * transaction, and call xfs_ialloc() again to actually get the inode.
1231 * To ensure that some other process does not grab the inode that
1232 * was allocated during the first call to xfs_ialloc(), this routine
1233 * also returns the [locked] bp pointing to the head of the freelist
1234 * as ialloc_context. The caller should hold this buffer across
1235 * the commit and pass it back into this routine on the second call.
1237 * If we are allocating quota inodes, we do not have a parent inode
1238 * to attach to or associate with (i.e. pip == NULL) because they
1239 * are not linked into the directory structure - they are attached
1240 * directly to the superblock - and so have no parent.
1243 xfs_ialloc(
1244 xfs_trans_t *tp,
1245 xfs_inode_t *pip,
1246 umode_t mode,
1247 xfs_nlink_t nlink,
1248 xfs_dev_t rdev,
1249 prid_t prid,
1250 int okalloc,
1251 xfs_buf_t **ialloc_context,
1252 xfs_inode_t **ipp)
1254 struct xfs_mount *mp = tp->t_mountp;
1255 xfs_ino_t ino;
1256 xfs_inode_t *ip;
1257 uint flags;
1258 int error;
1259 timespec_t tv;
1260 int filestreams = 0;
1263 * Call the space management code to pick
1264 * the on-disk inode to be allocated.
1266 error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc,
1267 ialloc_context, &ino);
1268 if (error)
1269 return error;
1270 if (*ialloc_context || ino == NULLFSINO) {
1271 *ipp = NULL;
1272 return 0;
1274 ASSERT(*ialloc_context == NULL);
1277 * Get the in-core inode with the lock held exclusively.
1278 * This is because we're setting fields here we need
1279 * to prevent others from looking at until we're done.
1281 error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE,
1282 XFS_ILOCK_EXCL, &ip);
1283 if (error)
1284 return error;
1285 ASSERT(ip != NULL);
1287 ip->i_d.di_mode = mode;
1288 ip->i_d.di_onlink = 0;
1289 ip->i_d.di_nlink = nlink;
1290 ASSERT(ip->i_d.di_nlink == nlink);
1291 ip->i_d.di_uid = current_fsuid();
1292 ip->i_d.di_gid = current_fsgid();
1293 xfs_set_projid(ip, prid);
1294 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
1297 * If the superblock version is up to where we support new format
1298 * inodes and this is currently an old format inode, then change
1299 * the inode version number now. This way we only do the conversion
1300 * here rather than here and in the flush/logging code.
1302 if (xfs_sb_version_hasnlink(&mp->m_sb) &&
1303 ip->i_d.di_version == 1) {
1304 ip->i_d.di_version = 2;
1306 * We've already zeroed the old link count, the projid field,
1307 * and the pad field.
1312 * Project ids won't be stored on disk if we are using a version 1 inode.
1314 if ((prid != 0) && (ip->i_d.di_version == 1))
1315 xfs_bump_ino_vers2(tp, ip);
1317 if (pip && XFS_INHERIT_GID(pip)) {
1318 ip->i_d.di_gid = pip->i_d.di_gid;
1319 if ((pip->i_d.di_mode & S_ISGID) && S_ISDIR(mode)) {
1320 ip->i_d.di_mode |= S_ISGID;
1325 * If the group ID of the new file does not match the effective group
1326 * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
1327 * (and only if the irix_sgid_inherit compatibility variable is set).
1329 if ((irix_sgid_inherit) &&
1330 (ip->i_d.di_mode & S_ISGID) &&
1331 (!in_group_p((gid_t)ip->i_d.di_gid))) {
1332 ip->i_d.di_mode &= ~S_ISGID;
1335 ip->i_d.di_size = 0;
1336 ip->i_d.di_nextents = 0;
1337 ASSERT(ip->i_d.di_nblocks == 0);
1339 nanotime(&tv);
1340 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
1341 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
1342 ip->i_d.di_atime = ip->i_d.di_mtime;
1343 ip->i_d.di_ctime = ip->i_d.di_mtime;
1346 * di_gen will have been taken care of in xfs_iread.
1348 ip->i_d.di_extsize = 0;
1349 ip->i_d.di_dmevmask = 0;
1350 ip->i_d.di_dmstate = 0;
1351 ip->i_d.di_flags = 0;
1353 if (ip->i_d.di_version == 3) {
1354 ASSERT(ip->i_d.di_ino == ino);
1355 ASSERT(uuid_equal(&ip->i_d.di_uuid, &mp->m_sb.sb_uuid));
1356 ip->i_d.di_crc = 0;
1357 ip->i_d.di_changecount = 1;
1358 ip->i_d.di_lsn = 0;
1359 ip->i_d.di_flags2 = 0;
1360 memset(&(ip->i_d.di_pad2[0]), 0, sizeof(ip->i_d.di_pad2));
1361 ip->i_d.di_crtime = ip->i_d.di_mtime;
1365 flags = XFS_ILOG_CORE;
1366 switch (mode & S_IFMT) {
1367 case S_IFIFO:
1368 case S_IFCHR:
1369 case S_IFBLK:
1370 case S_IFSOCK:
1371 ip->i_d.di_format = XFS_DINODE_FMT_DEV;
1372 ip->i_df.if_u2.if_rdev = rdev;
1373 ip->i_df.if_flags = 0;
1374 flags |= XFS_ILOG_DEV;
1375 break;
1376 case S_IFREG:
1378 * we can't set up filestreams until after the VFS inode
1379 * is set up properly.
1381 if (pip && xfs_inode_is_filestream(pip))
1382 filestreams = 1;
1383 /* fall through */
1384 case S_IFDIR:
1385 if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) {
1386 uint di_flags = 0;
1388 if (S_ISDIR(mode)) {
1389 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
1390 di_flags |= XFS_DIFLAG_RTINHERIT;
1391 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
1392 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
1393 ip->i_d.di_extsize = pip->i_d.di_extsize;
1395 } else if (S_ISREG(mode)) {
1396 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
1397 di_flags |= XFS_DIFLAG_REALTIME;
1398 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
1399 di_flags |= XFS_DIFLAG_EXTSIZE;
1400 ip->i_d.di_extsize = pip->i_d.di_extsize;
1403 if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
1404 xfs_inherit_noatime)
1405 di_flags |= XFS_DIFLAG_NOATIME;
1406 if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
1407 xfs_inherit_nodump)
1408 di_flags |= XFS_DIFLAG_NODUMP;
1409 if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
1410 xfs_inherit_sync)
1411 di_flags |= XFS_DIFLAG_SYNC;
1412 if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
1413 xfs_inherit_nosymlinks)
1414 di_flags |= XFS_DIFLAG_NOSYMLINKS;
1415 if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1416 di_flags |= XFS_DIFLAG_PROJINHERIT;
1417 if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
1418 xfs_inherit_nodefrag)
1419 di_flags |= XFS_DIFLAG_NODEFRAG;
1420 if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
1421 di_flags |= XFS_DIFLAG_FILESTREAM;
1422 ip->i_d.di_flags |= di_flags;
1424 /* FALLTHROUGH */
1425 case S_IFLNK:
1426 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
1427 ip->i_df.if_flags = XFS_IFEXTENTS;
1428 ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0;
1429 ip->i_df.if_u1.if_extents = NULL;
1430 break;
1431 default:
1432 ASSERT(0);
1435 * Attribute fork settings for new inode.
1437 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1438 ip->i_d.di_anextents = 0;
1441 * Log the new values stuffed into the inode.
1443 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1444 xfs_trans_log_inode(tp, ip, flags);
1446 /* now that we have an i_mode we can setup inode ops and unlock */
1447 xfs_setup_inode(ip);
1449 /* now we have set up the vfs inode we can associate the filestream */
1450 if (filestreams) {
1451 error = xfs_filestream_associate(pip, ip);
1452 if (error < 0)
1453 return -error;
1454 if (!error)
1455 xfs_iflags_set(ip, XFS_IFILESTREAM);
1458 *ipp = ip;
1459 return 0;
1463 * Free up the underlying blocks past new_size. The new size must be smaller
1464 * than the current size. This routine can be used both for the attribute and
1465 * data fork, and does not modify the inode size, which is left to the caller.
1467 * The transaction passed to this routine must have made a permanent log
1468 * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the
1469 * given transaction and start new ones, so make sure everything involved in
1470 * the transaction is tidy before calling here. Some transaction will be
1471 * returned to the caller to be committed. The incoming transaction must
1472 * already include the inode, and both inode locks must be held exclusively.
1473 * The inode must also be "held" within the transaction. On return the inode
1474 * will be "held" within the returned transaction. This routine does NOT
1475 * require any disk space to be reserved for it within the transaction.
1477 * If we get an error, we must return with the inode locked and linked into the
1478 * current transaction. This keeps things simple for the higher level code,
1479 * because it always knows that the inode is locked and held in the transaction
1480 * that returns to it whether errors occur or not. We don't mark the inode
1481 * dirty on error so that transactions can be easily aborted if possible.
1484 xfs_itruncate_extents(
1485 struct xfs_trans **tpp,
1486 struct xfs_inode *ip,
1487 int whichfork,
1488 xfs_fsize_t new_size)
1490 struct xfs_mount *mp = ip->i_mount;
1491 struct xfs_trans *tp = *tpp;
1492 struct xfs_trans *ntp;
1493 xfs_bmap_free_t free_list;
1494 xfs_fsblock_t first_block;
1495 xfs_fileoff_t first_unmap_block;
1496 xfs_fileoff_t last_block;
1497 xfs_filblks_t unmap_len;
1498 int committed;
1499 int error = 0;
1500 int done = 0;
1502 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1503 ASSERT(!atomic_read(&VFS_I(ip)->i_count) ||
1504 xfs_isilocked(ip, XFS_IOLOCK_EXCL));
1505 ASSERT(new_size <= XFS_ISIZE(ip));
1506 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1507 ASSERT(ip->i_itemp != NULL);
1508 ASSERT(ip->i_itemp->ili_lock_flags == 0);
1509 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1511 trace_xfs_itruncate_extents_start(ip, new_size);
1514 * Since it is possible for space to become allocated beyond
1515 * the end of the file (in a crash where the space is allocated
1516 * but the inode size is not yet updated), simply remove any
1517 * blocks which show up between the new EOF and the maximum
1518 * possible file size. If the first block to be removed is
1519 * beyond the maximum file size (ie it is the same as last_block),
1520 * then there is nothing to do.
1522 first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
1523 last_block = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
1524 if (first_unmap_block == last_block)
1525 return 0;
1527 ASSERT(first_unmap_block < last_block);
1528 unmap_len = last_block - first_unmap_block + 1;
1529 while (!done) {
1530 xfs_bmap_init(&free_list, &first_block);
1531 error = xfs_bunmapi(tp, ip,
1532 first_unmap_block, unmap_len,
1533 xfs_bmapi_aflag(whichfork),
1534 XFS_ITRUNC_MAX_EXTENTS,
1535 &first_block, &free_list,
1536 &done);
1537 if (error)
1538 goto out_bmap_cancel;
1541 * Duplicate the transaction that has the permanent
1542 * reservation and commit the old transaction.
1544 error = xfs_bmap_finish(&tp, &free_list, &committed);
1545 if (committed)
1546 xfs_trans_ijoin(tp, ip, 0);
1547 if (error)
1548 goto out_bmap_cancel;
1550 if (committed) {
1552 * Mark the inode dirty so it will be logged and
1553 * moved forward in the log as part of every commit.
1555 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1558 ntp = xfs_trans_dup(tp);
1559 error = xfs_trans_commit(tp, 0);
1560 tp = ntp;
1562 xfs_trans_ijoin(tp, ip, 0);
1564 if (error)
1565 goto out;
1568 * Transaction commit worked ok so we can drop the extra ticket
1569 * reference that we gained in xfs_trans_dup()
1571 xfs_log_ticket_put(tp->t_ticket);
1572 error = xfs_trans_reserve(tp, 0,
1573 XFS_ITRUNCATE_LOG_RES(mp), 0,
1574 XFS_TRANS_PERM_LOG_RES,
1575 XFS_ITRUNCATE_LOG_COUNT);
1576 if (error)
1577 goto out;
1581 * Always re-log the inode so that our permanent transaction can keep
1582 * on rolling it forward in the log.
1584 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1586 trace_xfs_itruncate_extents_end(ip, new_size);
1588 out:
1589 *tpp = tp;
1590 return error;
1591 out_bmap_cancel:
1593 * If the bunmapi call encounters an error, return to the caller where
1594 * the transaction can be properly aborted. We just need to make sure
1595 * we're not holding any resources that we were not when we came in.
1597 xfs_bmap_cancel(&free_list);
1598 goto out;
1602 * This is called when the inode's link count goes to 0.
1603 * We place the on-disk inode on a list in the AGI. It
1604 * will be pulled from this list when the inode is freed.
1607 xfs_iunlink(
1608 xfs_trans_t *tp,
1609 xfs_inode_t *ip)
1611 xfs_mount_t *mp;
1612 xfs_agi_t *agi;
1613 xfs_dinode_t *dip;
1614 xfs_buf_t *agibp;
1615 xfs_buf_t *ibp;
1616 xfs_agino_t agino;
1617 short bucket_index;
1618 int offset;
1619 int error;
1621 ASSERT(ip->i_d.di_nlink == 0);
1622 ASSERT(ip->i_d.di_mode != 0);
1624 mp = tp->t_mountp;
1627 * Get the agi buffer first. It ensures lock ordering
1628 * on the list.
1630 error = xfs_read_agi(mp, tp, XFS_INO_TO_AGNO(mp, ip->i_ino), &agibp);
1631 if (error)
1632 return error;
1633 agi = XFS_BUF_TO_AGI(agibp);
1636 * Get the index into the agi hash table for the
1637 * list this inode will go on.
1639 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
1640 ASSERT(agino != 0);
1641 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
1642 ASSERT(agi->agi_unlinked[bucket_index]);
1643 ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino);
1645 if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) {
1647 * There is already another inode in the bucket we need
1648 * to add ourselves to. Add us at the front of the list.
1649 * Here we put the head pointer into our next pointer,
1650 * and then we fall through to point the head at us.
1652 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
1653 0, 0);
1654 if (error)
1655 return error;
1657 ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO));
1658 dip->di_next_unlinked = agi->agi_unlinked[bucket_index];
1659 offset = ip->i_imap.im_boffset +
1660 offsetof(xfs_dinode_t, di_next_unlinked);
1662 /* need to recalc the inode CRC if appropriate */
1663 xfs_dinode_calc_crc(mp, dip);
1665 xfs_trans_inode_buf(tp, ibp);
1666 xfs_trans_log_buf(tp, ibp, offset,
1667 (offset + sizeof(xfs_agino_t) - 1));
1668 xfs_inobp_check(mp, ibp);
1672 * Point the bucket head pointer at the inode being inserted.
1674 ASSERT(agino != 0);
1675 agi->agi_unlinked[bucket_index] = cpu_to_be32(agino);
1676 offset = offsetof(xfs_agi_t, agi_unlinked) +
1677 (sizeof(xfs_agino_t) * bucket_index);
1678 xfs_trans_log_buf(tp, agibp, offset,
1679 (offset + sizeof(xfs_agino_t) - 1));
1680 return 0;
1684 * Pull the on-disk inode from the AGI unlinked list.
1686 STATIC int
1687 xfs_iunlink_remove(
1688 xfs_trans_t *tp,
1689 xfs_inode_t *ip)
1691 xfs_ino_t next_ino;
1692 xfs_mount_t *mp;
1693 xfs_agi_t *agi;
1694 xfs_dinode_t *dip;
1695 xfs_buf_t *agibp;
1696 xfs_buf_t *ibp;
1697 xfs_agnumber_t agno;
1698 xfs_agino_t agino;
1699 xfs_agino_t next_agino;
1700 xfs_buf_t *last_ibp;
1701 xfs_dinode_t *last_dip = NULL;
1702 short bucket_index;
1703 int offset, last_offset = 0;
1704 int error;
1706 mp = tp->t_mountp;
1707 agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
1710 * Get the agi buffer first. It ensures lock ordering
1711 * on the list.
1713 error = xfs_read_agi(mp, tp, agno, &agibp);
1714 if (error)
1715 return error;
1717 agi = XFS_BUF_TO_AGI(agibp);
1720 * Get the index into the agi hash table for the
1721 * list this inode will go on.
1723 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
1724 ASSERT(agino != 0);
1725 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
1726 ASSERT(agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO));
1727 ASSERT(agi->agi_unlinked[bucket_index]);
1729 if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) {
1731 * We're at the head of the list. Get the inode's on-disk
1732 * buffer to see if there is anyone after us on the list.
1733 * Only modify our next pointer if it is not already NULLAGINO.
1734 * This saves us the overhead of dealing with the buffer when
1735 * there is no need to change it.
1737 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
1738 0, 0);
1739 if (error) {
1740 xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.",
1741 __func__, error);
1742 return error;
1744 next_agino = be32_to_cpu(dip->di_next_unlinked);
1745 ASSERT(next_agino != 0);
1746 if (next_agino != NULLAGINO) {
1747 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
1748 offset = ip->i_imap.im_boffset +
1749 offsetof(xfs_dinode_t, di_next_unlinked);
1751 /* need to recalc the inode CRC if appropriate */
1752 xfs_dinode_calc_crc(mp, dip);
1754 xfs_trans_inode_buf(tp, ibp);
1755 xfs_trans_log_buf(tp, ibp, offset,
1756 (offset + sizeof(xfs_agino_t) - 1));
1757 xfs_inobp_check(mp, ibp);
1758 } else {
1759 xfs_trans_brelse(tp, ibp);
1762 * Point the bucket head pointer at the next inode.
1764 ASSERT(next_agino != 0);
1765 ASSERT(next_agino != agino);
1766 agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino);
1767 offset = offsetof(xfs_agi_t, agi_unlinked) +
1768 (sizeof(xfs_agino_t) * bucket_index);
1769 xfs_trans_log_buf(tp, agibp, offset,
1770 (offset + sizeof(xfs_agino_t) - 1));
1771 } else {
1773 * We need to search the list for the inode being freed.
1775 next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
1776 last_ibp = NULL;
1777 while (next_agino != agino) {
1778 struct xfs_imap imap;
1780 if (last_ibp)
1781 xfs_trans_brelse(tp, last_ibp);
1783 imap.im_blkno = 0;
1784 next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino);
1786 error = xfs_imap(mp, tp, next_ino, &imap, 0);
1787 if (error) {
1788 xfs_warn(mp,
1789 "%s: xfs_imap returned error %d.",
1790 __func__, error);
1791 return error;
1794 error = xfs_imap_to_bp(mp, tp, &imap, &last_dip,
1795 &last_ibp, 0, 0);
1796 if (error) {
1797 xfs_warn(mp,
1798 "%s: xfs_imap_to_bp returned error %d.",
1799 __func__, error);
1800 return error;
1803 last_offset = imap.im_boffset;
1804 next_agino = be32_to_cpu(last_dip->di_next_unlinked);
1805 ASSERT(next_agino != NULLAGINO);
1806 ASSERT(next_agino != 0);
1810 * Now last_ibp points to the buffer previous to us on the
1811 * unlinked list. Pull us from the list.
1813 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
1814 0, 0);
1815 if (error) {
1816 xfs_warn(mp, "%s: xfs_imap_to_bp(2) returned error %d.",
1817 __func__, error);
1818 return error;
1820 next_agino = be32_to_cpu(dip->di_next_unlinked);
1821 ASSERT(next_agino != 0);
1822 ASSERT(next_agino != agino);
1823 if (next_agino != NULLAGINO) {
1824 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
1825 offset = ip->i_imap.im_boffset +
1826 offsetof(xfs_dinode_t, di_next_unlinked);
1828 /* need to recalc the inode CRC if appropriate */
1829 xfs_dinode_calc_crc(mp, dip);
1831 xfs_trans_inode_buf(tp, ibp);
1832 xfs_trans_log_buf(tp, ibp, offset,
1833 (offset + sizeof(xfs_agino_t) - 1));
1834 xfs_inobp_check(mp, ibp);
1835 } else {
1836 xfs_trans_brelse(tp, ibp);
1839 * Point the previous inode on the list to the next inode.
1841 last_dip->di_next_unlinked = cpu_to_be32(next_agino);
1842 ASSERT(next_agino != 0);
1843 offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked);
1845 /* need to recalc the inode CRC if appropriate */
1846 xfs_dinode_calc_crc(mp, last_dip);
1848 xfs_trans_inode_buf(tp, last_ibp);
1849 xfs_trans_log_buf(tp, last_ibp, offset,
1850 (offset + sizeof(xfs_agino_t) - 1));
1851 xfs_inobp_check(mp, last_ibp);
1853 return 0;
1857 * A big issue when freeing the inode cluster is is that we _cannot_ skip any
1858 * inodes that are in memory - they all must be marked stale and attached to
1859 * the cluster buffer.
1861 STATIC int
1862 xfs_ifree_cluster(
1863 xfs_inode_t *free_ip,
1864 xfs_trans_t *tp,
1865 xfs_ino_t inum)
1867 xfs_mount_t *mp = free_ip->i_mount;
1868 int blks_per_cluster;
1869 int nbufs;
1870 int ninodes;
1871 int i, j;
1872 xfs_daddr_t blkno;
1873 xfs_buf_t *bp;
1874 xfs_inode_t *ip;
1875 xfs_inode_log_item_t *iip;
1876 xfs_log_item_t *lip;
1877 struct xfs_perag *pag;
1879 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum));
1880 if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
1881 blks_per_cluster = 1;
1882 ninodes = mp->m_sb.sb_inopblock;
1883 nbufs = XFS_IALLOC_BLOCKS(mp);
1884 } else {
1885 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
1886 mp->m_sb.sb_blocksize;
1887 ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
1888 nbufs = XFS_IALLOC_BLOCKS(mp) / blks_per_cluster;
1891 for (j = 0; j < nbufs; j++, inum += ninodes) {
1892 blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
1893 XFS_INO_TO_AGBNO(mp, inum));
1896 * We obtain and lock the backing buffer first in the process
1897 * here, as we have to ensure that any dirty inode that we
1898 * can't get the flush lock on is attached to the buffer.
1899 * If we scan the in-memory inodes first, then buffer IO can
1900 * complete before we get a lock on it, and hence we may fail
1901 * to mark all the active inodes on the buffer stale.
1903 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
1904 mp->m_bsize * blks_per_cluster,
1905 XBF_UNMAPPED);
1907 if (!bp)
1908 return ENOMEM;
1911 * This buffer may not have been correctly initialised as we
1912 * didn't read it from disk. That's not important because we are
1913 * only using to mark the buffer as stale in the log, and to
1914 * attach stale cached inodes on it. That means it will never be
1915 * dispatched for IO. If it is, we want to know about it, and we
1916 * want it to fail. We can acheive this by adding a write
1917 * verifier to the buffer.
1919 bp->b_ops = &xfs_inode_buf_ops;
1922 * Walk the inodes already attached to the buffer and mark them
1923 * stale. These will all have the flush locks held, so an
1924 * in-memory inode walk can't lock them. By marking them all
1925 * stale first, we will not attempt to lock them in the loop
1926 * below as the XFS_ISTALE flag will be set.
1928 lip = bp->b_fspriv;
1929 while (lip) {
1930 if (lip->li_type == XFS_LI_INODE) {
1931 iip = (xfs_inode_log_item_t *)lip;
1932 ASSERT(iip->ili_logged == 1);
1933 lip->li_cb = xfs_istale_done;
1934 xfs_trans_ail_copy_lsn(mp->m_ail,
1935 &iip->ili_flush_lsn,
1936 &iip->ili_item.li_lsn);
1937 xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
1939 lip = lip->li_bio_list;
1944 * For each inode in memory attempt to add it to the inode
1945 * buffer and set it up for being staled on buffer IO
1946 * completion. This is safe as we've locked out tail pushing
1947 * and flushing by locking the buffer.
1949 * We have already marked every inode that was part of a
1950 * transaction stale above, which means there is no point in
1951 * even trying to lock them.
1953 for (i = 0; i < ninodes; i++) {
1954 retry:
1955 rcu_read_lock();
1956 ip = radix_tree_lookup(&pag->pag_ici_root,
1957 XFS_INO_TO_AGINO(mp, (inum + i)));
1959 /* Inode not in memory, nothing to do */
1960 if (!ip) {
1961 rcu_read_unlock();
1962 continue;
1966 * because this is an RCU protected lookup, we could
1967 * find a recently freed or even reallocated inode
1968 * during the lookup. We need to check under the
1969 * i_flags_lock for a valid inode here. Skip it if it
1970 * is not valid, the wrong inode or stale.
1972 spin_lock(&ip->i_flags_lock);
1973 if (ip->i_ino != inum + i ||
1974 __xfs_iflags_test(ip, XFS_ISTALE)) {
1975 spin_unlock(&ip->i_flags_lock);
1976 rcu_read_unlock();
1977 continue;
1979 spin_unlock(&ip->i_flags_lock);
1982 * Don't try to lock/unlock the current inode, but we
1983 * _cannot_ skip the other inodes that we did not find
1984 * in the list attached to the buffer and are not
1985 * already marked stale. If we can't lock it, back off
1986 * and retry.
1988 if (ip != free_ip &&
1989 !xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
1990 rcu_read_unlock();
1991 delay(1);
1992 goto retry;
1994 rcu_read_unlock();
1996 xfs_iflock(ip);
1997 xfs_iflags_set(ip, XFS_ISTALE);
2000 * we don't need to attach clean inodes or those only
2001 * with unlogged changes (which we throw away, anyway).
2003 iip = ip->i_itemp;
2004 if (!iip || xfs_inode_clean(ip)) {
2005 ASSERT(ip != free_ip);
2006 xfs_ifunlock(ip);
2007 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2008 continue;
2011 iip->ili_last_fields = iip->ili_fields;
2012 iip->ili_fields = 0;
2013 iip->ili_logged = 1;
2014 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
2015 &iip->ili_item.li_lsn);
2017 xfs_buf_attach_iodone(bp, xfs_istale_done,
2018 &iip->ili_item);
2020 if (ip != free_ip)
2021 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2024 xfs_trans_stale_inode_buf(tp, bp);
2025 xfs_trans_binval(tp, bp);
2028 xfs_perag_put(pag);
2029 return 0;
2033 * This is called to return an inode to the inode free list.
2034 * The inode should already be truncated to 0 length and have
2035 * no pages associated with it. This routine also assumes that
2036 * the inode is already a part of the transaction.
2038 * The on-disk copy of the inode will have been added to the list
2039 * of unlinked inodes in the AGI. We need to remove the inode from
2040 * that list atomically with respect to freeing it here.
2043 xfs_ifree(
2044 xfs_trans_t *tp,
2045 xfs_inode_t *ip,
2046 xfs_bmap_free_t *flist)
2048 int error;
2049 int delete;
2050 xfs_ino_t first_ino;
2052 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
2053 ASSERT(ip->i_d.di_nlink == 0);
2054 ASSERT(ip->i_d.di_nextents == 0);
2055 ASSERT(ip->i_d.di_anextents == 0);
2056 ASSERT(ip->i_d.di_size == 0 || !S_ISREG(ip->i_d.di_mode));
2057 ASSERT(ip->i_d.di_nblocks == 0);
2060 * Pull the on-disk inode from the AGI unlinked list.
2062 error = xfs_iunlink_remove(tp, ip);
2063 if (error)
2064 return error;
2066 error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino);
2067 if (error)
2068 return error;
2070 ip->i_d.di_mode = 0; /* mark incore inode as free */
2071 ip->i_d.di_flags = 0;
2072 ip->i_d.di_dmevmask = 0;
2073 ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */
2074 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
2075 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
2077 * Bump the generation count so no one will be confused
2078 * by reincarnations of this inode.
2080 ip->i_d.di_gen++;
2081 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2083 if (delete)
2084 error = xfs_ifree_cluster(ip, tp, first_ino);
2086 return error;
2090 * Reallocate the space for if_broot based on the number of records
2091 * being added or deleted as indicated in rec_diff. Move the records
2092 * and pointers in if_broot to fit the new size. When shrinking this
2093 * will eliminate holes between the records and pointers created by
2094 * the caller. When growing this will create holes to be filled in
2095 * by the caller.
2097 * The caller must not request to add more records than would fit in
2098 * the on-disk inode root. If the if_broot is currently NULL, then
2099 * if we adding records one will be allocated. The caller must also
2100 * not request that the number of records go below zero, although
2101 * it can go to zero.
2103 * ip -- the inode whose if_broot area is changing
2104 * ext_diff -- the change in the number of records, positive or negative,
2105 * requested for the if_broot array.
2107 void
2108 xfs_iroot_realloc(
2109 xfs_inode_t *ip,
2110 int rec_diff,
2111 int whichfork)
2113 struct xfs_mount *mp = ip->i_mount;
2114 int cur_max;
2115 xfs_ifork_t *ifp;
2116 struct xfs_btree_block *new_broot;
2117 int new_max;
2118 size_t new_size;
2119 char *np;
2120 char *op;
2123 * Handle the degenerate case quietly.
2125 if (rec_diff == 0) {
2126 return;
2129 ifp = XFS_IFORK_PTR(ip, whichfork);
2130 if (rec_diff > 0) {
2132 * If there wasn't any memory allocated before, just
2133 * allocate it now and get out.
2135 if (ifp->if_broot_bytes == 0) {
2136 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
2137 ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
2138 ifp->if_broot_bytes = (int)new_size;
2139 return;
2143 * If there is already an existing if_broot, then we need
2144 * to realloc() it and shift the pointers to their new
2145 * location. The records don't change location because
2146 * they are kept butted up against the btree block header.
2148 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
2149 new_max = cur_max + rec_diff;
2150 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
2151 ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
2152 XFS_BMAP_BROOT_SPACE_CALC(mp, cur_max),
2153 KM_SLEEP | KM_NOFS);
2154 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
2155 ifp->if_broot_bytes);
2156 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
2157 (int)new_size);
2158 ifp->if_broot_bytes = (int)new_size;
2159 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
2160 XFS_IFORK_SIZE(ip, whichfork));
2161 memmove(np, op, cur_max * (uint)sizeof(xfs_dfsbno_t));
2162 return;
2166 * rec_diff is less than 0. In this case, we are shrinking the
2167 * if_broot buffer. It must already exist. If we go to zero
2168 * records, just get rid of the root and clear the status bit.
2170 ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
2171 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
2172 new_max = cur_max + rec_diff;
2173 ASSERT(new_max >= 0);
2174 if (new_max > 0)
2175 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
2176 else
2177 new_size = 0;
2178 if (new_size > 0) {
2179 new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
2181 * First copy over the btree block header.
2183 memcpy(new_broot, ifp->if_broot,
2184 XFS_BMBT_BLOCK_LEN(ip->i_mount));
2185 } else {
2186 new_broot = NULL;
2187 ifp->if_flags &= ~XFS_IFBROOT;
2191 * Only copy the records and pointers if there are any.
2193 if (new_max > 0) {
2195 * First copy the records.
2197 op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
2198 np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
2199 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
2202 * Then copy the pointers.
2204 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
2205 ifp->if_broot_bytes);
2206 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
2207 (int)new_size);
2208 memcpy(np, op, new_max * (uint)sizeof(xfs_dfsbno_t));
2210 kmem_free(ifp->if_broot);
2211 ifp->if_broot = new_broot;
2212 ifp->if_broot_bytes = (int)new_size;
2213 if (ifp->if_broot)
2214 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
2215 XFS_IFORK_SIZE(ip, whichfork));
2216 return;
2221 * This is called when the amount of space needed for if_data
2222 * is increased or decreased. The change in size is indicated by
2223 * the number of bytes that need to be added or deleted in the
2224 * byte_diff parameter.
2226 * If the amount of space needed has decreased below the size of the
2227 * inline buffer, then switch to using the inline buffer. Otherwise,
2228 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
2229 * to what is needed.
2231 * ip -- the inode whose if_data area is changing
2232 * byte_diff -- the change in the number of bytes, positive or negative,
2233 * requested for the if_data array.
2235 void
2236 xfs_idata_realloc(
2237 xfs_inode_t *ip,
2238 int byte_diff,
2239 int whichfork)
2241 xfs_ifork_t *ifp;
2242 int new_size;
2243 int real_size;
2245 if (byte_diff == 0) {
2246 return;
2249 ifp = XFS_IFORK_PTR(ip, whichfork);
2250 new_size = (int)ifp->if_bytes + byte_diff;
2251 ASSERT(new_size >= 0);
2253 if (new_size == 0) {
2254 if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
2255 kmem_free(ifp->if_u1.if_data);
2257 ifp->if_u1.if_data = NULL;
2258 real_size = 0;
2259 } else if (new_size <= sizeof(ifp->if_u2.if_inline_data)) {
2261 * If the valid extents/data can fit in if_inline_ext/data,
2262 * copy them from the malloc'd vector and free it.
2264 if (ifp->if_u1.if_data == NULL) {
2265 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
2266 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
2267 ASSERT(ifp->if_real_bytes != 0);
2268 memcpy(ifp->if_u2.if_inline_data, ifp->if_u1.if_data,
2269 new_size);
2270 kmem_free(ifp->if_u1.if_data);
2271 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
2273 real_size = 0;
2274 } else {
2276 * Stuck with malloc/realloc.
2277 * For inline data, the underlying buffer must be
2278 * a multiple of 4 bytes in size so that it can be
2279 * logged and stay on word boundaries. We enforce
2280 * that here.
2282 real_size = roundup(new_size, 4);
2283 if (ifp->if_u1.if_data == NULL) {
2284 ASSERT(ifp->if_real_bytes == 0);
2285 ifp->if_u1.if_data = kmem_alloc(real_size,
2286 KM_SLEEP | KM_NOFS);
2287 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
2289 * Only do the realloc if the underlying size
2290 * is really changing.
2292 if (ifp->if_real_bytes != real_size) {
2293 ifp->if_u1.if_data =
2294 kmem_realloc(ifp->if_u1.if_data,
2295 real_size,
2296 ifp->if_real_bytes,
2297 KM_SLEEP | KM_NOFS);
2299 } else {
2300 ASSERT(ifp->if_real_bytes == 0);
2301 ifp->if_u1.if_data = kmem_alloc(real_size,
2302 KM_SLEEP | KM_NOFS);
2303 memcpy(ifp->if_u1.if_data, ifp->if_u2.if_inline_data,
2304 ifp->if_bytes);
2307 ifp->if_real_bytes = real_size;
2308 ifp->if_bytes = new_size;
2309 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
2312 void
2313 xfs_idestroy_fork(
2314 xfs_inode_t *ip,
2315 int whichfork)
2317 xfs_ifork_t *ifp;
2319 ifp = XFS_IFORK_PTR(ip, whichfork);
2320 if (ifp->if_broot != NULL) {
2321 kmem_free(ifp->if_broot);
2322 ifp->if_broot = NULL;
2326 * If the format is local, then we can't have an extents
2327 * array so just look for an inline data array. If we're
2328 * not local then we may or may not have an extents list,
2329 * so check and free it up if we do.
2331 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
2332 if ((ifp->if_u1.if_data != ifp->if_u2.if_inline_data) &&
2333 (ifp->if_u1.if_data != NULL)) {
2334 ASSERT(ifp->if_real_bytes != 0);
2335 kmem_free(ifp->if_u1.if_data);
2336 ifp->if_u1.if_data = NULL;
2337 ifp->if_real_bytes = 0;
2339 } else if ((ifp->if_flags & XFS_IFEXTENTS) &&
2340 ((ifp->if_flags & XFS_IFEXTIREC) ||
2341 ((ifp->if_u1.if_extents != NULL) &&
2342 (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext)))) {
2343 ASSERT(ifp->if_real_bytes != 0);
2344 xfs_iext_destroy(ifp);
2346 ASSERT(ifp->if_u1.if_extents == NULL ||
2347 ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext);
2348 ASSERT(ifp->if_real_bytes == 0);
2349 if (whichfork == XFS_ATTR_FORK) {
2350 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
2351 ip->i_afp = NULL;
2356 * This is called to unpin an inode. The caller must have the inode locked
2357 * in at least shared mode so that the buffer cannot be subsequently pinned
2358 * once someone is waiting for it to be unpinned.
2360 static void
2361 xfs_iunpin(
2362 struct xfs_inode *ip)
2364 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
2366 trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
2368 /* Give the log a push to start the unpinning I/O */
2369 xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0);
2373 static void
2374 __xfs_iunpin_wait(
2375 struct xfs_inode *ip)
2377 wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
2378 DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
2380 xfs_iunpin(ip);
2382 do {
2383 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2384 if (xfs_ipincount(ip))
2385 io_schedule();
2386 } while (xfs_ipincount(ip));
2387 finish_wait(wq, &wait.wait);
2390 void
2391 xfs_iunpin_wait(
2392 struct xfs_inode *ip)
2394 if (xfs_ipincount(ip))
2395 __xfs_iunpin_wait(ip);
2399 * xfs_iextents_copy()
2401 * This is called to copy the REAL extents (as opposed to the delayed
2402 * allocation extents) from the inode into the given buffer. It
2403 * returns the number of bytes copied into the buffer.
2405 * If there are no delayed allocation extents, then we can just
2406 * memcpy() the extents into the buffer. Otherwise, we need to
2407 * examine each extent in turn and skip those which are delayed.
2410 xfs_iextents_copy(
2411 xfs_inode_t *ip,
2412 xfs_bmbt_rec_t *dp,
2413 int whichfork)
2415 int copied;
2416 int i;
2417 xfs_ifork_t *ifp;
2418 int nrecs;
2419 xfs_fsblock_t start_block;
2421 ifp = XFS_IFORK_PTR(ip, whichfork);
2422 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
2423 ASSERT(ifp->if_bytes > 0);
2425 nrecs = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
2426 XFS_BMAP_TRACE_EXLIST(ip, nrecs, whichfork);
2427 ASSERT(nrecs > 0);
2430 * There are some delayed allocation extents in the
2431 * inode, so copy the extents one at a time and skip
2432 * the delayed ones. There must be at least one
2433 * non-delayed extent.
2435 copied = 0;
2436 for (i = 0; i < nrecs; i++) {
2437 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
2438 start_block = xfs_bmbt_get_startblock(ep);
2439 if (isnullstartblock(start_block)) {
2441 * It's a delayed allocation extent, so skip it.
2443 continue;
2446 /* Translate to on disk format */
2447 put_unaligned(cpu_to_be64(ep->l0), &dp->l0);
2448 put_unaligned(cpu_to_be64(ep->l1), &dp->l1);
2449 dp++;
2450 copied++;
2452 ASSERT(copied != 0);
2453 xfs_validate_extents(ifp, copied, XFS_EXTFMT_INODE(ip));
2455 return (copied * (uint)sizeof(xfs_bmbt_rec_t));
2459 * Each of the following cases stores data into the same region
2460 * of the on-disk inode, so only one of them can be valid at
2461 * any given time. While it is possible to have conflicting formats
2462 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
2463 * in EXTENTS format, this can only happen when the fork has
2464 * changed formats after being modified but before being flushed.
2465 * In these cases, the format always takes precedence, because the
2466 * format indicates the current state of the fork.
2468 /*ARGSUSED*/
2469 STATIC void
2470 xfs_iflush_fork(
2471 xfs_inode_t *ip,
2472 xfs_dinode_t *dip,
2473 xfs_inode_log_item_t *iip,
2474 int whichfork,
2475 xfs_buf_t *bp)
2477 char *cp;
2478 xfs_ifork_t *ifp;
2479 xfs_mount_t *mp;
2480 static const short brootflag[2] =
2481 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
2482 static const short dataflag[2] =
2483 { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
2484 static const short extflag[2] =
2485 { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
2487 if (!iip)
2488 return;
2489 ifp = XFS_IFORK_PTR(ip, whichfork);
2491 * This can happen if we gave up in iformat in an error path,
2492 * for the attribute fork.
2494 if (!ifp) {
2495 ASSERT(whichfork == XFS_ATTR_FORK);
2496 return;
2498 cp = XFS_DFORK_PTR(dip, whichfork);
2499 mp = ip->i_mount;
2500 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
2501 case XFS_DINODE_FMT_LOCAL:
2502 if ((iip->ili_fields & dataflag[whichfork]) &&
2503 (ifp->if_bytes > 0)) {
2504 ASSERT(ifp->if_u1.if_data != NULL);
2505 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
2506 memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
2508 break;
2510 case XFS_DINODE_FMT_EXTENTS:
2511 ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
2512 !(iip->ili_fields & extflag[whichfork]));
2513 if ((iip->ili_fields & extflag[whichfork]) &&
2514 (ifp->if_bytes > 0)) {
2515 ASSERT(xfs_iext_get_ext(ifp, 0));
2516 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
2517 (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
2518 whichfork);
2520 break;
2522 case XFS_DINODE_FMT_BTREE:
2523 if ((iip->ili_fields & brootflag[whichfork]) &&
2524 (ifp->if_broot_bytes > 0)) {
2525 ASSERT(ifp->if_broot != NULL);
2526 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
2527 XFS_IFORK_SIZE(ip, whichfork));
2528 xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
2529 (xfs_bmdr_block_t *)cp,
2530 XFS_DFORK_SIZE(dip, mp, whichfork));
2532 break;
2534 case XFS_DINODE_FMT_DEV:
2535 if (iip->ili_fields & XFS_ILOG_DEV) {
2536 ASSERT(whichfork == XFS_DATA_FORK);
2537 xfs_dinode_put_rdev(dip, ip->i_df.if_u2.if_rdev);
2539 break;
2541 case XFS_DINODE_FMT_UUID:
2542 if (iip->ili_fields & XFS_ILOG_UUID) {
2543 ASSERT(whichfork == XFS_DATA_FORK);
2544 memcpy(XFS_DFORK_DPTR(dip),
2545 &ip->i_df.if_u2.if_uuid,
2546 sizeof(uuid_t));
2548 break;
2550 default:
2551 ASSERT(0);
2552 break;
2556 STATIC int
2557 xfs_iflush_cluster(
2558 xfs_inode_t *ip,
2559 xfs_buf_t *bp)
2561 xfs_mount_t *mp = ip->i_mount;
2562 struct xfs_perag *pag;
2563 unsigned long first_index, mask;
2564 unsigned long inodes_per_cluster;
2565 int ilist_size;
2566 xfs_inode_t **ilist;
2567 xfs_inode_t *iq;
2568 int nr_found;
2569 int clcount = 0;
2570 int bufwasdelwri;
2571 int i;
2573 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
2575 inodes_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog;
2576 ilist_size = inodes_per_cluster * sizeof(xfs_inode_t *);
2577 ilist = kmem_alloc(ilist_size, KM_MAYFAIL|KM_NOFS);
2578 if (!ilist)
2579 goto out_put;
2581 mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
2582 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask;
2583 rcu_read_lock();
2584 /* really need a gang lookup range call here */
2585 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)ilist,
2586 first_index, inodes_per_cluster);
2587 if (nr_found == 0)
2588 goto out_free;
2590 for (i = 0; i < nr_found; i++) {
2591 iq = ilist[i];
2592 if (iq == ip)
2593 continue;
2596 * because this is an RCU protected lookup, we could find a
2597 * recently freed or even reallocated inode during the lookup.
2598 * We need to check under the i_flags_lock for a valid inode
2599 * here. Skip it if it is not valid or the wrong inode.
2601 spin_lock(&ip->i_flags_lock);
2602 if (!ip->i_ino ||
2603 (XFS_INO_TO_AGINO(mp, iq->i_ino) & mask) != first_index) {
2604 spin_unlock(&ip->i_flags_lock);
2605 continue;
2607 spin_unlock(&ip->i_flags_lock);
2610 * Do an un-protected check to see if the inode is dirty and
2611 * is a candidate for flushing. These checks will be repeated
2612 * later after the appropriate locks are acquired.
2614 if (xfs_inode_clean(iq) && xfs_ipincount(iq) == 0)
2615 continue;
2618 * Try to get locks. If any are unavailable or it is pinned,
2619 * then this inode cannot be flushed and is skipped.
2622 if (!xfs_ilock_nowait(iq, XFS_ILOCK_SHARED))
2623 continue;
2624 if (!xfs_iflock_nowait(iq)) {
2625 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2626 continue;
2628 if (xfs_ipincount(iq)) {
2629 xfs_ifunlock(iq);
2630 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2631 continue;
2635 * arriving here means that this inode can be flushed. First
2636 * re-check that it's dirty before flushing.
2638 if (!xfs_inode_clean(iq)) {
2639 int error;
2640 error = xfs_iflush_int(iq, bp);
2641 if (error) {
2642 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2643 goto cluster_corrupt_out;
2645 clcount++;
2646 } else {
2647 xfs_ifunlock(iq);
2649 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2652 if (clcount) {
2653 XFS_STATS_INC(xs_icluster_flushcnt);
2654 XFS_STATS_ADD(xs_icluster_flushinode, clcount);
2657 out_free:
2658 rcu_read_unlock();
2659 kmem_free(ilist);
2660 out_put:
2661 xfs_perag_put(pag);
2662 return 0;
2665 cluster_corrupt_out:
2667 * Corruption detected in the clustering loop. Invalidate the
2668 * inode buffer and shut down the filesystem.
2670 rcu_read_unlock();
2672 * Clean up the buffer. If it was delwri, just release it --
2673 * brelse can handle it with no problems. If not, shut down the
2674 * filesystem before releasing the buffer.
2676 bufwasdelwri = (bp->b_flags & _XBF_DELWRI_Q);
2677 if (bufwasdelwri)
2678 xfs_buf_relse(bp);
2680 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
2682 if (!bufwasdelwri) {
2684 * Just like incore_relse: if we have b_iodone functions,
2685 * mark the buffer as an error and call them. Otherwise
2686 * mark it as stale and brelse.
2688 if (bp->b_iodone) {
2689 XFS_BUF_UNDONE(bp);
2690 xfs_buf_stale(bp);
2691 xfs_buf_ioerror(bp, EIO);
2692 xfs_buf_ioend(bp, 0);
2693 } else {
2694 xfs_buf_stale(bp);
2695 xfs_buf_relse(bp);
2700 * Unlocks the flush lock
2702 xfs_iflush_abort(iq, false);
2703 kmem_free(ilist);
2704 xfs_perag_put(pag);
2705 return XFS_ERROR(EFSCORRUPTED);
2709 * Flush dirty inode metadata into the backing buffer.
2711 * The caller must have the inode lock and the inode flush lock held. The
2712 * inode lock will still be held upon return to the caller, and the inode
2713 * flush lock will be released after the inode has reached the disk.
2715 * The caller must write out the buffer returned in *bpp and release it.
2718 xfs_iflush(
2719 struct xfs_inode *ip,
2720 struct xfs_buf **bpp)
2722 struct xfs_mount *mp = ip->i_mount;
2723 struct xfs_buf *bp;
2724 struct xfs_dinode *dip;
2725 int error;
2727 XFS_STATS_INC(xs_iflush_count);
2729 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
2730 ASSERT(xfs_isiflocked(ip));
2731 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
2732 ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
2734 *bpp = NULL;
2736 xfs_iunpin_wait(ip);
2739 * For stale inodes we cannot rely on the backing buffer remaining
2740 * stale in cache for the remaining life of the stale inode and so
2741 * xfs_imap_to_bp() below may give us a buffer that no longer contains
2742 * inodes below. We have to check this after ensuring the inode is
2743 * unpinned so that it is safe to reclaim the stale inode after the
2744 * flush call.
2746 if (xfs_iflags_test(ip, XFS_ISTALE)) {
2747 xfs_ifunlock(ip);
2748 return 0;
2752 * This may have been unpinned because the filesystem is shutting
2753 * down forcibly. If that's the case we must not write this inode
2754 * to disk, because the log record didn't make it to disk.
2756 * We also have to remove the log item from the AIL in this case,
2757 * as we wait for an empty AIL as part of the unmount process.
2759 if (XFS_FORCED_SHUTDOWN(mp)) {
2760 error = XFS_ERROR(EIO);
2761 goto abort_out;
2765 * Get the buffer containing the on-disk inode.
2767 error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &bp, XBF_TRYLOCK,
2769 if (error || !bp) {
2770 xfs_ifunlock(ip);
2771 return error;
2775 * First flush out the inode that xfs_iflush was called with.
2777 error = xfs_iflush_int(ip, bp);
2778 if (error)
2779 goto corrupt_out;
2782 * If the buffer is pinned then push on the log now so we won't
2783 * get stuck waiting in the write for too long.
2785 if (xfs_buf_ispinned(bp))
2786 xfs_log_force(mp, 0);
2789 * inode clustering:
2790 * see if other inodes can be gathered into this write
2792 error = xfs_iflush_cluster(ip, bp);
2793 if (error)
2794 goto cluster_corrupt_out;
2796 *bpp = bp;
2797 return 0;
2799 corrupt_out:
2800 xfs_buf_relse(bp);
2801 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
2802 cluster_corrupt_out:
2803 error = XFS_ERROR(EFSCORRUPTED);
2804 abort_out:
2806 * Unlocks the flush lock
2808 xfs_iflush_abort(ip, false);
2809 return error;
2813 STATIC int
2814 xfs_iflush_int(
2815 struct xfs_inode *ip,
2816 struct xfs_buf *bp)
2818 struct xfs_inode_log_item *iip = ip->i_itemp;
2819 struct xfs_dinode *dip;
2820 struct xfs_mount *mp = ip->i_mount;
2822 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
2823 ASSERT(xfs_isiflocked(ip));
2824 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
2825 ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
2826 ASSERT(iip != NULL && iip->ili_fields != 0);
2828 /* set *dip = inode's place in the buffer */
2829 dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
2831 if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
2832 mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) {
2833 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2834 "%s: Bad inode %Lu magic number 0x%x, ptr 0x%p",
2835 __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
2836 goto corrupt_out;
2838 if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC,
2839 mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) {
2840 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2841 "%s: Bad inode %Lu, ptr 0x%p, magic number 0x%x",
2842 __func__, ip->i_ino, ip, ip->i_d.di_magic);
2843 goto corrupt_out;
2845 if (S_ISREG(ip->i_d.di_mode)) {
2846 if (XFS_TEST_ERROR(
2847 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
2848 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE),
2849 mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) {
2850 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2851 "%s: Bad regular inode %Lu, ptr 0x%p",
2852 __func__, ip->i_ino, ip);
2853 goto corrupt_out;
2855 } else if (S_ISDIR(ip->i_d.di_mode)) {
2856 if (XFS_TEST_ERROR(
2857 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
2858 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
2859 (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL),
2860 mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) {
2861 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2862 "%s: Bad directory inode %Lu, ptr 0x%p",
2863 __func__, ip->i_ino, ip);
2864 goto corrupt_out;
2867 if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents >
2868 ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5,
2869 XFS_RANDOM_IFLUSH_5)) {
2870 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2871 "%s: detected corrupt incore inode %Lu, "
2872 "total extents = %d, nblocks = %Ld, ptr 0x%p",
2873 __func__, ip->i_ino,
2874 ip->i_d.di_nextents + ip->i_d.di_anextents,
2875 ip->i_d.di_nblocks, ip);
2876 goto corrupt_out;
2878 if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
2879 mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) {
2880 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2881 "%s: bad inode %Lu, forkoff 0x%x, ptr 0x%p",
2882 __func__, ip->i_ino, ip->i_d.di_forkoff, ip);
2883 goto corrupt_out;
2886 * bump the flush iteration count, used to detect flushes which
2887 * postdate a log record during recovery. This is redundant as we now
2888 * log every change and hence this can't happen. Still, it doesn't hurt.
2890 ip->i_d.di_flushiter++;
2893 * Copy the dirty parts of the inode into the on-disk
2894 * inode. We always copy out the core of the inode,
2895 * because if the inode is dirty at all the core must
2896 * be.
2898 xfs_dinode_to_disk(dip, &ip->i_d);
2900 /* Wrap, we never let the log put out DI_MAX_FLUSH */
2901 if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
2902 ip->i_d.di_flushiter = 0;
2905 * If this is really an old format inode and the superblock version
2906 * has not been updated to support only new format inodes, then
2907 * convert back to the old inode format. If the superblock version
2908 * has been updated, then make the conversion permanent.
2910 ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb));
2911 if (ip->i_d.di_version == 1) {
2912 if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
2914 * Convert it back.
2916 ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
2917 dip->di_onlink = cpu_to_be16(ip->i_d.di_nlink);
2918 } else {
2920 * The superblock version has already been bumped,
2921 * so just make the conversion to the new inode
2922 * format permanent.
2924 ip->i_d.di_version = 2;
2925 dip->di_version = 2;
2926 ip->i_d.di_onlink = 0;
2927 dip->di_onlink = 0;
2928 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
2929 memset(&(dip->di_pad[0]), 0,
2930 sizeof(dip->di_pad));
2931 ASSERT(xfs_get_projid(ip) == 0);
2935 xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK, bp);
2936 if (XFS_IFORK_Q(ip))
2937 xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK, bp);
2938 xfs_inobp_check(mp, bp);
2941 * We've recorded everything logged in the inode, so we'd like to clear
2942 * the ili_fields bits so we don't log and flush things unnecessarily.
2943 * However, we can't stop logging all this information until the data
2944 * we've copied into the disk buffer is written to disk. If we did we
2945 * might overwrite the copy of the inode in the log with all the data
2946 * after re-logging only part of it, and in the face of a crash we
2947 * wouldn't have all the data we need to recover.
2949 * What we do is move the bits to the ili_last_fields field. When
2950 * logging the inode, these bits are moved back to the ili_fields field.
2951 * In the xfs_iflush_done() routine we clear ili_last_fields, since we
2952 * know that the information those bits represent is permanently on
2953 * disk. As long as the flush completes before the inode is logged
2954 * again, then both ili_fields and ili_last_fields will be cleared.
2956 * We can play with the ili_fields bits here, because the inode lock
2957 * must be held exclusively in order to set bits there and the flush
2958 * lock protects the ili_last_fields bits. Set ili_logged so the flush
2959 * done routine can tell whether or not to look in the AIL. Also, store
2960 * the current LSN of the inode so that we can tell whether the item has
2961 * moved in the AIL from xfs_iflush_done(). In order to read the lsn we
2962 * need the AIL lock, because it is a 64 bit value that cannot be read
2963 * atomically.
2965 iip->ili_last_fields = iip->ili_fields;
2966 iip->ili_fields = 0;
2967 iip->ili_logged = 1;
2969 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
2970 &iip->ili_item.li_lsn);
2973 * Attach the function xfs_iflush_done to the inode's
2974 * buffer. This will remove the inode from the AIL
2975 * and unlock the inode's flush lock when the inode is
2976 * completely written to disk.
2978 xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item);
2980 /* update the lsn in the on disk inode if required */
2981 if (ip->i_d.di_version == 3)
2982 dip->di_lsn = cpu_to_be64(iip->ili_item.li_lsn);
2984 /* generate the checksum. */
2985 xfs_dinode_calc_crc(mp, dip);
2987 ASSERT(bp->b_fspriv != NULL);
2988 ASSERT(bp->b_iodone != NULL);
2989 return 0;
2991 corrupt_out:
2992 return XFS_ERROR(EFSCORRUPTED);
2996 * Return a pointer to the extent record at file index idx.
2998 xfs_bmbt_rec_host_t *
2999 xfs_iext_get_ext(
3000 xfs_ifork_t *ifp, /* inode fork pointer */
3001 xfs_extnum_t idx) /* index of target extent */
3003 ASSERT(idx >= 0);
3004 ASSERT(idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
3006 if ((ifp->if_flags & XFS_IFEXTIREC) && (idx == 0)) {
3007 return ifp->if_u1.if_ext_irec->er_extbuf;
3008 } else if (ifp->if_flags & XFS_IFEXTIREC) {
3009 xfs_ext_irec_t *erp; /* irec pointer */
3010 int erp_idx = 0; /* irec index */
3011 xfs_extnum_t page_idx = idx; /* ext index in target list */
3013 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
3014 return &erp->er_extbuf[page_idx];
3015 } else if (ifp->if_bytes) {
3016 return &ifp->if_u1.if_extents[idx];
3017 } else {
3018 return NULL;
3023 * Insert new item(s) into the extent records for incore inode
3024 * fork 'ifp'. 'count' new items are inserted at index 'idx'.
3026 void
3027 xfs_iext_insert(
3028 xfs_inode_t *ip, /* incore inode pointer */
3029 xfs_extnum_t idx, /* starting index of new items */
3030 xfs_extnum_t count, /* number of inserted items */
3031 xfs_bmbt_irec_t *new, /* items to insert */
3032 int state) /* type of extent conversion */
3034 xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
3035 xfs_extnum_t i; /* extent record index */
3037 trace_xfs_iext_insert(ip, idx, new, state, _RET_IP_);
3039 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
3040 xfs_iext_add(ifp, idx, count);
3041 for (i = idx; i < idx + count; i++, new++)
3042 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, i), new);
3046 * This is called when the amount of space required for incore file
3047 * extents needs to be increased. The ext_diff parameter stores the
3048 * number of new extents being added and the idx parameter contains
3049 * the extent index where the new extents will be added. If the new
3050 * extents are being appended, then we just need to (re)allocate and
3051 * initialize the space. Otherwise, if the new extents are being
3052 * inserted into the middle of the existing entries, a bit more work
3053 * is required to make room for the new extents to be inserted. The
3054 * caller is responsible for filling in the new extent entries upon
3055 * return.
3057 void
3058 xfs_iext_add(
3059 xfs_ifork_t *ifp, /* inode fork pointer */
3060 xfs_extnum_t idx, /* index to begin adding exts */
3061 int ext_diff) /* number of extents to add */
3063 int byte_diff; /* new bytes being added */
3064 int new_size; /* size of extents after adding */
3065 xfs_extnum_t nextents; /* number of extents in file */
3067 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3068 ASSERT((idx >= 0) && (idx <= nextents));
3069 byte_diff = ext_diff * sizeof(xfs_bmbt_rec_t);
3070 new_size = ifp->if_bytes + byte_diff;
3072 * If the new number of extents (nextents + ext_diff)
3073 * fits inside the inode, then continue to use the inline
3074 * extent buffer.
3076 if (nextents + ext_diff <= XFS_INLINE_EXTS) {
3077 if (idx < nextents) {
3078 memmove(&ifp->if_u2.if_inline_ext[idx + ext_diff],
3079 &ifp->if_u2.if_inline_ext[idx],
3080 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
3081 memset(&ifp->if_u2.if_inline_ext[idx], 0, byte_diff);
3083 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
3084 ifp->if_real_bytes = 0;
3087 * Otherwise use a linear (direct) extent list.
3088 * If the extents are currently inside the inode,
3089 * xfs_iext_realloc_direct will switch us from
3090 * inline to direct extent allocation mode.
3092 else if (nextents + ext_diff <= XFS_LINEAR_EXTS) {
3093 xfs_iext_realloc_direct(ifp, new_size);
3094 if (idx < nextents) {
3095 memmove(&ifp->if_u1.if_extents[idx + ext_diff],
3096 &ifp->if_u1.if_extents[idx],
3097 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
3098 memset(&ifp->if_u1.if_extents[idx], 0, byte_diff);
3101 /* Indirection array */
3102 else {
3103 xfs_ext_irec_t *erp;
3104 int erp_idx = 0;
3105 int page_idx = idx;
3107 ASSERT(nextents + ext_diff > XFS_LINEAR_EXTS);
3108 if (ifp->if_flags & XFS_IFEXTIREC) {
3109 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 1);
3110 } else {
3111 xfs_iext_irec_init(ifp);
3112 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3113 erp = ifp->if_u1.if_ext_irec;
3115 /* Extents fit in target extent page */
3116 if (erp && erp->er_extcount + ext_diff <= XFS_LINEAR_EXTS) {
3117 if (page_idx < erp->er_extcount) {
3118 memmove(&erp->er_extbuf[page_idx + ext_diff],
3119 &erp->er_extbuf[page_idx],
3120 (erp->er_extcount - page_idx) *
3121 sizeof(xfs_bmbt_rec_t));
3122 memset(&erp->er_extbuf[page_idx], 0, byte_diff);
3124 erp->er_extcount += ext_diff;
3125 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
3127 /* Insert a new extent page */
3128 else if (erp) {
3129 xfs_iext_add_indirect_multi(ifp,
3130 erp_idx, page_idx, ext_diff);
3133 * If extent(s) are being appended to the last page in
3134 * the indirection array and the new extent(s) don't fit
3135 * in the page, then erp is NULL and erp_idx is set to
3136 * the next index needed in the indirection array.
3138 else {
3139 int count = ext_diff;
3141 while (count) {
3142 erp = xfs_iext_irec_new(ifp, erp_idx);
3143 erp->er_extcount = count;
3144 count -= MIN(count, (int)XFS_LINEAR_EXTS);
3145 if (count) {
3146 erp_idx++;
3151 ifp->if_bytes = new_size;
3155 * This is called when incore extents are being added to the indirection
3156 * array and the new extents do not fit in the target extent list. The
3157 * erp_idx parameter contains the irec index for the target extent list
3158 * in the indirection array, and the idx parameter contains the extent
3159 * index within the list. The number of extents being added is stored
3160 * in the count parameter.
3162 * |-------| |-------|
3163 * | | | | idx - number of extents before idx
3164 * | idx | | count |
3165 * | | | | count - number of extents being inserted at idx
3166 * |-------| |-------|
3167 * | count | | nex2 | nex2 - number of extents after idx + count
3168 * |-------| |-------|
3170 void
3171 xfs_iext_add_indirect_multi(
3172 xfs_ifork_t *ifp, /* inode fork pointer */
3173 int erp_idx, /* target extent irec index */
3174 xfs_extnum_t idx, /* index within target list */
3175 int count) /* new extents being added */
3177 int byte_diff; /* new bytes being added */
3178 xfs_ext_irec_t *erp; /* pointer to irec entry */
3179 xfs_extnum_t ext_diff; /* number of extents to add */
3180 xfs_extnum_t ext_cnt; /* new extents still needed */
3181 xfs_extnum_t nex2; /* extents after idx + count */
3182 xfs_bmbt_rec_t *nex2_ep = NULL; /* temp list for nex2 extents */
3183 int nlists; /* number of irec's (lists) */
3185 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3186 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3187 nex2 = erp->er_extcount - idx;
3188 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3191 * Save second part of target extent list
3192 * (all extents past */
3193 if (nex2) {
3194 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
3195 nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_NOFS);
3196 memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff);
3197 erp->er_extcount -= nex2;
3198 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2);
3199 memset(&erp->er_extbuf[idx], 0, byte_diff);
3203 * Add the new extents to the end of the target
3204 * list, then allocate new irec record(s) and
3205 * extent buffer(s) as needed to store the rest
3206 * of the new extents.
3208 ext_cnt = count;
3209 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount);
3210 if (ext_diff) {
3211 erp->er_extcount += ext_diff;
3212 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
3213 ext_cnt -= ext_diff;
3215 while (ext_cnt) {
3216 erp_idx++;
3217 erp = xfs_iext_irec_new(ifp, erp_idx);
3218 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS);
3219 erp->er_extcount = ext_diff;
3220 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
3221 ext_cnt -= ext_diff;
3224 /* Add nex2 extents back to indirection array */
3225 if (nex2) {
3226 xfs_extnum_t ext_avail;
3227 int i;
3229 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
3230 ext_avail = XFS_LINEAR_EXTS - erp->er_extcount;
3231 i = 0;
3233 * If nex2 extents fit in the current page, append
3234 * nex2_ep after the new extents.
3236 if (nex2 <= ext_avail) {
3237 i = erp->er_extcount;
3240 * Otherwise, check if space is available in the
3241 * next page.
3243 else if ((erp_idx < nlists - 1) &&
3244 (nex2 <= (ext_avail = XFS_LINEAR_EXTS -
3245 ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) {
3246 erp_idx++;
3247 erp++;
3248 /* Create a hole for nex2 extents */
3249 memmove(&erp->er_extbuf[nex2], erp->er_extbuf,
3250 erp->er_extcount * sizeof(xfs_bmbt_rec_t));
3253 * Final choice, create a new extent page for
3254 * nex2 extents.
3256 else {
3257 erp_idx++;
3258 erp = xfs_iext_irec_new(ifp, erp_idx);
3260 memmove(&erp->er_extbuf[i], nex2_ep, byte_diff);
3261 kmem_free(nex2_ep);
3262 erp->er_extcount += nex2;
3263 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2);
3268 * This is called when the amount of space required for incore file
3269 * extents needs to be decreased. The ext_diff parameter stores the
3270 * number of extents to be removed and the idx parameter contains
3271 * the extent index where the extents will be removed from.
3273 * If the amount of space needed has decreased below the linear
3274 * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
3275 * extent array. Otherwise, use kmem_realloc() to adjust the
3276 * size to what is needed.
3278 void
3279 xfs_iext_remove(
3280 xfs_inode_t *ip, /* incore inode pointer */
3281 xfs_extnum_t idx, /* index to begin removing exts */
3282 int ext_diff, /* number of extents to remove */
3283 int state) /* type of extent conversion */
3285 xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
3286 xfs_extnum_t nextents; /* number of extents in file */
3287 int new_size; /* size of extents after removal */
3289 trace_xfs_iext_remove(ip, idx, state, _RET_IP_);
3291 ASSERT(ext_diff > 0);
3292 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3293 new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t);
3295 if (new_size == 0) {
3296 xfs_iext_destroy(ifp);
3297 } else if (ifp->if_flags & XFS_IFEXTIREC) {
3298 xfs_iext_remove_indirect(ifp, idx, ext_diff);
3299 } else if (ifp->if_real_bytes) {
3300 xfs_iext_remove_direct(ifp, idx, ext_diff);
3301 } else {
3302 xfs_iext_remove_inline(ifp, idx, ext_diff);
3304 ifp->if_bytes = new_size;
3308 * This removes ext_diff extents from the inline buffer, beginning
3309 * at extent index idx.
3311 void
3312 xfs_iext_remove_inline(
3313 xfs_ifork_t *ifp, /* inode fork pointer */
3314 xfs_extnum_t idx, /* index to begin removing exts */
3315 int ext_diff) /* number of extents to remove */
3317 int nextents; /* number of extents in file */
3319 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
3320 ASSERT(idx < XFS_INLINE_EXTS);
3321 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3322 ASSERT(((nextents - ext_diff) > 0) &&
3323 (nextents - ext_diff) < XFS_INLINE_EXTS);
3325 if (idx + ext_diff < nextents) {
3326 memmove(&ifp->if_u2.if_inline_ext[idx],
3327 &ifp->if_u2.if_inline_ext[idx + ext_diff],
3328 (nextents - (idx + ext_diff)) *
3329 sizeof(xfs_bmbt_rec_t));
3330 memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff],
3331 0, ext_diff * sizeof(xfs_bmbt_rec_t));
3332 } else {
3333 memset(&ifp->if_u2.if_inline_ext[idx], 0,
3334 ext_diff * sizeof(xfs_bmbt_rec_t));
3339 * This removes ext_diff extents from a linear (direct) extent list,
3340 * beginning at extent index idx. If the extents are being removed
3341 * from the end of the list (ie. truncate) then we just need to re-
3342 * allocate the list to remove the extra space. Otherwise, if the
3343 * extents are being removed from the middle of the existing extent
3344 * entries, then we first need to move the extent records beginning
3345 * at idx + ext_diff up in the list to overwrite the records being
3346 * removed, then remove the extra space via kmem_realloc.
3348 void
3349 xfs_iext_remove_direct(
3350 xfs_ifork_t *ifp, /* inode fork pointer */
3351 xfs_extnum_t idx, /* index to begin removing exts */
3352 int ext_diff) /* number of extents to remove */
3354 xfs_extnum_t nextents; /* number of extents in file */
3355 int new_size; /* size of extents after removal */
3357 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
3358 new_size = ifp->if_bytes -
3359 (ext_diff * sizeof(xfs_bmbt_rec_t));
3360 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3362 if (new_size == 0) {
3363 xfs_iext_destroy(ifp);
3364 return;
3366 /* Move extents up in the list (if needed) */
3367 if (idx + ext_diff < nextents) {
3368 memmove(&ifp->if_u1.if_extents[idx],
3369 &ifp->if_u1.if_extents[idx + ext_diff],
3370 (nextents - (idx + ext_diff)) *
3371 sizeof(xfs_bmbt_rec_t));
3373 memset(&ifp->if_u1.if_extents[nextents - ext_diff],
3374 0, ext_diff * sizeof(xfs_bmbt_rec_t));
3376 * Reallocate the direct extent list. If the extents
3377 * will fit inside the inode then xfs_iext_realloc_direct
3378 * will switch from direct to inline extent allocation
3379 * mode for us.
3381 xfs_iext_realloc_direct(ifp, new_size);
3382 ifp->if_bytes = new_size;
3386 * This is called when incore extents are being removed from the
3387 * indirection array and the extents being removed span multiple extent
3388 * buffers. The idx parameter contains the file extent index where we
3389 * want to begin removing extents, and the count parameter contains
3390 * how many extents need to be removed.
3392 * |-------| |-------|
3393 * | nex1 | | | nex1 - number of extents before idx
3394 * |-------| | count |
3395 * | | | | count - number of extents being removed at idx
3396 * | count | |-------|
3397 * | | | nex2 | nex2 - number of extents after idx + count
3398 * |-------| |-------|
3400 void
3401 xfs_iext_remove_indirect(
3402 xfs_ifork_t *ifp, /* inode fork pointer */
3403 xfs_extnum_t idx, /* index to begin removing extents */
3404 int count) /* number of extents to remove */
3406 xfs_ext_irec_t *erp; /* indirection array pointer */
3407 int erp_idx = 0; /* indirection array index */
3408 xfs_extnum_t ext_cnt; /* extents left to remove */
3409 xfs_extnum_t ext_diff; /* extents to remove in current list */
3410 xfs_extnum_t nex1; /* number of extents before idx */
3411 xfs_extnum_t nex2; /* extents after idx + count */
3412 int page_idx = idx; /* index in target extent list */
3414 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3415 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
3416 ASSERT(erp != NULL);
3417 nex1 = page_idx;
3418 ext_cnt = count;
3419 while (ext_cnt) {
3420 nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0);
3421 ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1));
3423 * Check for deletion of entire list;
3424 * xfs_iext_irec_remove() updates extent offsets.
3426 if (ext_diff == erp->er_extcount) {
3427 xfs_iext_irec_remove(ifp, erp_idx);
3428 ext_cnt -= ext_diff;
3429 nex1 = 0;
3430 if (ext_cnt) {
3431 ASSERT(erp_idx < ifp->if_real_bytes /
3432 XFS_IEXT_BUFSZ);
3433 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3434 nex1 = 0;
3435 continue;
3436 } else {
3437 break;
3440 /* Move extents up (if needed) */
3441 if (nex2) {
3442 memmove(&erp->er_extbuf[nex1],
3443 &erp->er_extbuf[nex1 + ext_diff],
3444 nex2 * sizeof(xfs_bmbt_rec_t));
3446 /* Zero out rest of page */
3447 memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ -
3448 ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t))));
3449 /* Update remaining counters */
3450 erp->er_extcount -= ext_diff;
3451 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff);
3452 ext_cnt -= ext_diff;
3453 nex1 = 0;
3454 erp_idx++;
3455 erp++;
3457 ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t);
3458 xfs_iext_irec_compact(ifp);
3462 * Create, destroy, or resize a linear (direct) block of extents.
3464 void
3465 xfs_iext_realloc_direct(
3466 xfs_ifork_t *ifp, /* inode fork pointer */
3467 int new_size) /* new size of extents */
3469 int rnew_size; /* real new size of extents */
3471 rnew_size = new_size;
3473 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) ||
3474 ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) &&
3475 (new_size != ifp->if_real_bytes)));
3477 /* Free extent records */
3478 if (new_size == 0) {
3479 xfs_iext_destroy(ifp);
3481 /* Resize direct extent list and zero any new bytes */
3482 else if (ifp->if_real_bytes) {
3483 /* Check if extents will fit inside the inode */
3484 if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) {
3485 xfs_iext_direct_to_inline(ifp, new_size /
3486 (uint)sizeof(xfs_bmbt_rec_t));
3487 ifp->if_bytes = new_size;
3488 return;
3490 if (!is_power_of_2(new_size)){
3491 rnew_size = roundup_pow_of_two(new_size);
3493 if (rnew_size != ifp->if_real_bytes) {
3494 ifp->if_u1.if_extents =
3495 kmem_realloc(ifp->if_u1.if_extents,
3496 rnew_size,
3497 ifp->if_real_bytes, KM_NOFS);
3499 if (rnew_size > ifp->if_real_bytes) {
3500 memset(&ifp->if_u1.if_extents[ifp->if_bytes /
3501 (uint)sizeof(xfs_bmbt_rec_t)], 0,
3502 rnew_size - ifp->if_real_bytes);
3506 * Switch from the inline extent buffer to a direct
3507 * extent list. Be sure to include the inline extent
3508 * bytes in new_size.
3510 else {
3511 new_size += ifp->if_bytes;
3512 if (!is_power_of_2(new_size)) {
3513 rnew_size = roundup_pow_of_two(new_size);
3515 xfs_iext_inline_to_direct(ifp, rnew_size);
3517 ifp->if_real_bytes = rnew_size;
3518 ifp->if_bytes = new_size;
3522 * Switch from linear (direct) extent records to inline buffer.
3524 void
3525 xfs_iext_direct_to_inline(
3526 xfs_ifork_t *ifp, /* inode fork pointer */
3527 xfs_extnum_t nextents) /* number of extents in file */
3529 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
3530 ASSERT(nextents <= XFS_INLINE_EXTS);
3532 * The inline buffer was zeroed when we switched
3533 * from inline to direct extent allocation mode,
3534 * so we don't need to clear it here.
3536 memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents,
3537 nextents * sizeof(xfs_bmbt_rec_t));
3538 kmem_free(ifp->if_u1.if_extents);
3539 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
3540 ifp->if_real_bytes = 0;
3544 * Switch from inline buffer to linear (direct) extent records.
3545 * new_size should already be rounded up to the next power of 2
3546 * by the caller (when appropriate), so use new_size as it is.
3547 * However, since new_size may be rounded up, we can't update
3548 * if_bytes here. It is the caller's responsibility to update
3549 * if_bytes upon return.
3551 void
3552 xfs_iext_inline_to_direct(
3553 xfs_ifork_t *ifp, /* inode fork pointer */
3554 int new_size) /* number of extents in file */
3556 ifp->if_u1.if_extents = kmem_alloc(new_size, KM_NOFS);
3557 memset(ifp->if_u1.if_extents, 0, new_size);
3558 if (ifp->if_bytes) {
3559 memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext,
3560 ifp->if_bytes);
3561 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
3562 sizeof(xfs_bmbt_rec_t));
3564 ifp->if_real_bytes = new_size;
3568 * Resize an extent indirection array to new_size bytes.
3570 STATIC void
3571 xfs_iext_realloc_indirect(
3572 xfs_ifork_t *ifp, /* inode fork pointer */
3573 int new_size) /* new indirection array size */
3575 int nlists; /* number of irec's (ex lists) */
3576 int size; /* current indirection array size */
3578 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3579 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3580 size = nlists * sizeof(xfs_ext_irec_t);
3581 ASSERT(ifp->if_real_bytes);
3582 ASSERT((new_size >= 0) && (new_size != size));
3583 if (new_size == 0) {
3584 xfs_iext_destroy(ifp);
3585 } else {
3586 ifp->if_u1.if_ext_irec = (xfs_ext_irec_t *)
3587 kmem_realloc(ifp->if_u1.if_ext_irec,
3588 new_size, size, KM_NOFS);
3593 * Switch from indirection array to linear (direct) extent allocations.
3595 STATIC void
3596 xfs_iext_indirect_to_direct(
3597 xfs_ifork_t *ifp) /* inode fork pointer */
3599 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3600 xfs_extnum_t nextents; /* number of extents in file */
3601 int size; /* size of file extents */
3603 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3604 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3605 ASSERT(nextents <= XFS_LINEAR_EXTS);
3606 size = nextents * sizeof(xfs_bmbt_rec_t);
3608 xfs_iext_irec_compact_pages(ifp);
3609 ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ);
3611 ep = ifp->if_u1.if_ext_irec->er_extbuf;
3612 kmem_free(ifp->if_u1.if_ext_irec);
3613 ifp->if_flags &= ~XFS_IFEXTIREC;
3614 ifp->if_u1.if_extents = ep;
3615 ifp->if_bytes = size;
3616 if (nextents < XFS_LINEAR_EXTS) {
3617 xfs_iext_realloc_direct(ifp, size);
3622 * Free incore file extents.
3624 void
3625 xfs_iext_destroy(
3626 xfs_ifork_t *ifp) /* inode fork pointer */
3628 if (ifp->if_flags & XFS_IFEXTIREC) {
3629 int erp_idx;
3630 int nlists;
3632 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3633 for (erp_idx = nlists - 1; erp_idx >= 0 ; erp_idx--) {
3634 xfs_iext_irec_remove(ifp, erp_idx);
3636 ifp->if_flags &= ~XFS_IFEXTIREC;
3637 } else if (ifp->if_real_bytes) {
3638 kmem_free(ifp->if_u1.if_extents);
3639 } else if (ifp->if_bytes) {
3640 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
3641 sizeof(xfs_bmbt_rec_t));
3643 ifp->if_u1.if_extents = NULL;
3644 ifp->if_real_bytes = 0;
3645 ifp->if_bytes = 0;
3649 * Return a pointer to the extent record for file system block bno.
3651 xfs_bmbt_rec_host_t * /* pointer to found extent record */
3652 xfs_iext_bno_to_ext(
3653 xfs_ifork_t *ifp, /* inode fork pointer */
3654 xfs_fileoff_t bno, /* block number to search for */
3655 xfs_extnum_t *idxp) /* index of target extent */
3657 xfs_bmbt_rec_host_t *base; /* pointer to first extent */
3658 xfs_filblks_t blockcount = 0; /* number of blocks in extent */
3659 xfs_bmbt_rec_host_t *ep = NULL; /* pointer to target extent */
3660 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
3661 int high; /* upper boundary in search */
3662 xfs_extnum_t idx = 0; /* index of target extent */
3663 int low; /* lower boundary in search */
3664 xfs_extnum_t nextents; /* number of file extents */
3665 xfs_fileoff_t startoff = 0; /* start offset of extent */
3667 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3668 if (nextents == 0) {
3669 *idxp = 0;
3670 return NULL;
3672 low = 0;
3673 if (ifp->if_flags & XFS_IFEXTIREC) {
3674 /* Find target extent list */
3675 int erp_idx = 0;
3676 erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx);
3677 base = erp->er_extbuf;
3678 high = erp->er_extcount - 1;
3679 } else {
3680 base = ifp->if_u1.if_extents;
3681 high = nextents - 1;
3683 /* Binary search extent records */
3684 while (low <= high) {
3685 idx = (low + high) >> 1;
3686 ep = base + idx;
3687 startoff = xfs_bmbt_get_startoff(ep);
3688 blockcount = xfs_bmbt_get_blockcount(ep);
3689 if (bno < startoff) {
3690 high = idx - 1;
3691 } else if (bno >= startoff + blockcount) {
3692 low = idx + 1;
3693 } else {
3694 /* Convert back to file-based extent index */
3695 if (ifp->if_flags & XFS_IFEXTIREC) {
3696 idx += erp->er_extoff;
3698 *idxp = idx;
3699 return ep;
3702 /* Convert back to file-based extent index */
3703 if (ifp->if_flags & XFS_IFEXTIREC) {
3704 idx += erp->er_extoff;
3706 if (bno >= startoff + blockcount) {
3707 if (++idx == nextents) {
3708 ep = NULL;
3709 } else {
3710 ep = xfs_iext_get_ext(ifp, idx);
3713 *idxp = idx;
3714 return ep;
3718 * Return a pointer to the indirection array entry containing the
3719 * extent record for filesystem block bno. Store the index of the
3720 * target irec in *erp_idxp.
3722 xfs_ext_irec_t * /* pointer to found extent record */
3723 xfs_iext_bno_to_irec(
3724 xfs_ifork_t *ifp, /* inode fork pointer */
3725 xfs_fileoff_t bno, /* block number to search for */
3726 int *erp_idxp) /* irec index of target ext list */
3728 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
3729 xfs_ext_irec_t *erp_next; /* next indirection array entry */
3730 int erp_idx; /* indirection array index */
3731 int nlists; /* number of extent irec's (lists) */
3732 int high; /* binary search upper limit */
3733 int low; /* binary search lower limit */
3735 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3736 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3737 erp_idx = 0;
3738 low = 0;
3739 high = nlists - 1;
3740 while (low <= high) {
3741 erp_idx = (low + high) >> 1;
3742 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3743 erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL;
3744 if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) {
3745 high = erp_idx - 1;
3746 } else if (erp_next && bno >=
3747 xfs_bmbt_get_startoff(erp_next->er_extbuf)) {
3748 low = erp_idx + 1;
3749 } else {
3750 break;
3753 *erp_idxp = erp_idx;
3754 return erp;
3758 * Return a pointer to the indirection array entry containing the
3759 * extent record at file extent index *idxp. Store the index of the
3760 * target irec in *erp_idxp and store the page index of the target
3761 * extent record in *idxp.
3763 xfs_ext_irec_t *
3764 xfs_iext_idx_to_irec(
3765 xfs_ifork_t *ifp, /* inode fork pointer */
3766 xfs_extnum_t *idxp, /* extent index (file -> page) */
3767 int *erp_idxp, /* pointer to target irec */
3768 int realloc) /* new bytes were just added */
3770 xfs_ext_irec_t *prev; /* pointer to previous irec */
3771 xfs_ext_irec_t *erp = NULL; /* pointer to current irec */
3772 int erp_idx; /* indirection array index */
3773 int nlists; /* number of irec's (ex lists) */
3774 int high; /* binary search upper limit */
3775 int low; /* binary search lower limit */
3776 xfs_extnum_t page_idx = *idxp; /* extent index in target list */
3778 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3779 ASSERT(page_idx >= 0);
3780 ASSERT(page_idx <= ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
3781 ASSERT(page_idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t) || realloc);
3783 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3784 erp_idx = 0;
3785 low = 0;
3786 high = nlists - 1;
3788 /* Binary search extent irec's */
3789 while (low <= high) {
3790 erp_idx = (low + high) >> 1;
3791 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3792 prev = erp_idx > 0 ? erp - 1 : NULL;
3793 if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff &&
3794 realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) {
3795 high = erp_idx - 1;
3796 } else if (page_idx > erp->er_extoff + erp->er_extcount ||
3797 (page_idx == erp->er_extoff + erp->er_extcount &&
3798 !realloc)) {
3799 low = erp_idx + 1;
3800 } else if (page_idx == erp->er_extoff + erp->er_extcount &&
3801 erp->er_extcount == XFS_LINEAR_EXTS) {
3802 ASSERT(realloc);
3803 page_idx = 0;
3804 erp_idx++;
3805 erp = erp_idx < nlists ? erp + 1 : NULL;
3806 break;
3807 } else {
3808 page_idx -= erp->er_extoff;
3809 break;
3812 *idxp = page_idx;
3813 *erp_idxp = erp_idx;
3814 return(erp);
3818 * Allocate and initialize an indirection array once the space needed
3819 * for incore extents increases above XFS_IEXT_BUFSZ.
3821 void
3822 xfs_iext_irec_init(
3823 xfs_ifork_t *ifp) /* inode fork pointer */
3825 xfs_ext_irec_t *erp; /* indirection array pointer */
3826 xfs_extnum_t nextents; /* number of extents in file */
3828 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
3829 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3830 ASSERT(nextents <= XFS_LINEAR_EXTS);
3832 erp = kmem_alloc(sizeof(xfs_ext_irec_t), KM_NOFS);
3834 if (nextents == 0) {
3835 ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
3836 } else if (!ifp->if_real_bytes) {
3837 xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ);
3838 } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) {
3839 xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ);
3841 erp->er_extbuf = ifp->if_u1.if_extents;
3842 erp->er_extcount = nextents;
3843 erp->er_extoff = 0;
3845 ifp->if_flags |= XFS_IFEXTIREC;
3846 ifp->if_real_bytes = XFS_IEXT_BUFSZ;
3847 ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t);
3848 ifp->if_u1.if_ext_irec = erp;
3850 return;
3854 * Allocate and initialize a new entry in the indirection array.
3856 xfs_ext_irec_t *
3857 xfs_iext_irec_new(
3858 xfs_ifork_t *ifp, /* inode fork pointer */
3859 int erp_idx) /* index for new irec */
3861 xfs_ext_irec_t *erp; /* indirection array pointer */
3862 int i; /* loop counter */
3863 int nlists; /* number of irec's (ex lists) */
3865 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3866 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3868 /* Resize indirection array */
3869 xfs_iext_realloc_indirect(ifp, ++nlists *
3870 sizeof(xfs_ext_irec_t));
3872 * Move records down in the array so the
3873 * new page can use erp_idx.
3875 erp = ifp->if_u1.if_ext_irec;
3876 for (i = nlists - 1; i > erp_idx; i--) {
3877 memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t));
3879 ASSERT(i == erp_idx);
3881 /* Initialize new extent record */
3882 erp = ifp->if_u1.if_ext_irec;
3883 erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
3884 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
3885 memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ);
3886 erp[erp_idx].er_extcount = 0;
3887 erp[erp_idx].er_extoff = erp_idx > 0 ?
3888 erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0;
3889 return (&erp[erp_idx]);
3893 * Remove a record from the indirection array.
3895 void
3896 xfs_iext_irec_remove(
3897 xfs_ifork_t *ifp, /* inode fork pointer */
3898 int erp_idx) /* irec index to remove */
3900 xfs_ext_irec_t *erp; /* indirection array pointer */
3901 int i; /* loop counter */
3902 int nlists; /* number of irec's (ex lists) */
3904 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3905 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3906 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3907 if (erp->er_extbuf) {
3908 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1,
3909 -erp->er_extcount);
3910 kmem_free(erp->er_extbuf);
3912 /* Compact extent records */
3913 erp = ifp->if_u1.if_ext_irec;
3914 for (i = erp_idx; i < nlists - 1; i++) {
3915 memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t));
3918 * Manually free the last extent record from the indirection
3919 * array. A call to xfs_iext_realloc_indirect() with a size
3920 * of zero would result in a call to xfs_iext_destroy() which
3921 * would in turn call this function again, creating a nasty
3922 * infinite loop.
3924 if (--nlists) {
3925 xfs_iext_realloc_indirect(ifp,
3926 nlists * sizeof(xfs_ext_irec_t));
3927 } else {
3928 kmem_free(ifp->if_u1.if_ext_irec);
3930 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
3934 * This is called to clean up large amounts of unused memory allocated
3935 * by the indirection array. Before compacting anything though, verify
3936 * that the indirection array is still needed and switch back to the
3937 * linear extent list (or even the inline buffer) if possible. The
3938 * compaction policy is as follows:
3940 * Full Compaction: Extents fit into a single page (or inline buffer)
3941 * Partial Compaction: Extents occupy less than 50% of allocated space
3942 * No Compaction: Extents occupy at least 50% of allocated space
3944 void
3945 xfs_iext_irec_compact(
3946 xfs_ifork_t *ifp) /* inode fork pointer */
3948 xfs_extnum_t nextents; /* number of extents in file */
3949 int nlists; /* number of irec's (ex lists) */
3951 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3952 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3953 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3955 if (nextents == 0) {
3956 xfs_iext_destroy(ifp);
3957 } else if (nextents <= XFS_INLINE_EXTS) {
3958 xfs_iext_indirect_to_direct(ifp);
3959 xfs_iext_direct_to_inline(ifp, nextents);
3960 } else if (nextents <= XFS_LINEAR_EXTS) {
3961 xfs_iext_indirect_to_direct(ifp);
3962 } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) {
3963 xfs_iext_irec_compact_pages(ifp);
3968 * Combine extents from neighboring extent pages.
3970 void
3971 xfs_iext_irec_compact_pages(
3972 xfs_ifork_t *ifp) /* inode fork pointer */
3974 xfs_ext_irec_t *erp, *erp_next;/* pointers to irec entries */
3975 int erp_idx = 0; /* indirection array index */
3976 int nlists; /* number of irec's (ex lists) */
3978 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3979 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3980 while (erp_idx < nlists - 1) {
3981 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3982 erp_next = erp + 1;
3983 if (erp_next->er_extcount <=
3984 (XFS_LINEAR_EXTS - erp->er_extcount)) {
3985 memcpy(&erp->er_extbuf[erp->er_extcount],
3986 erp_next->er_extbuf, erp_next->er_extcount *
3987 sizeof(xfs_bmbt_rec_t));
3988 erp->er_extcount += erp_next->er_extcount;
3990 * Free page before removing extent record
3991 * so er_extoffs don't get modified in
3992 * xfs_iext_irec_remove.
3994 kmem_free(erp_next->er_extbuf);
3995 erp_next->er_extbuf = NULL;
3996 xfs_iext_irec_remove(ifp, erp_idx + 1);
3997 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3998 } else {
3999 erp_idx++;
4005 * This is called to update the er_extoff field in the indirection
4006 * array when extents have been added or removed from one of the
4007 * extent lists. erp_idx contains the irec index to begin updating
4008 * at and ext_diff contains the number of extents that were added
4009 * or removed.
4011 void
4012 xfs_iext_irec_update_extoffs(
4013 xfs_ifork_t *ifp, /* inode fork pointer */
4014 int erp_idx, /* irec index to update */
4015 int ext_diff) /* number of new extents */
4017 int i; /* loop counter */
4018 int nlists; /* number of irec's (ex lists */
4020 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
4021 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4022 for (i = erp_idx; i < nlists; i++) {
4023 ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff;
4028 * Test whether it is appropriate to check an inode for and free post EOF
4029 * blocks. The 'force' parameter determines whether we should also consider
4030 * regular files that are marked preallocated or append-only.
4032 bool
4033 xfs_can_free_eofblocks(struct xfs_inode *ip, bool force)
4035 /* prealloc/delalloc exists only on regular files */
4036 if (!S_ISREG(ip->i_d.di_mode))
4037 return false;
4040 * Zero sized files with no cached pages and delalloc blocks will not
4041 * have speculative prealloc/delalloc blocks to remove.
4043 if (VFS_I(ip)->i_size == 0 &&
4044 VN_CACHED(VFS_I(ip)) == 0 &&
4045 ip->i_delayed_blks == 0)
4046 return false;
4048 /* If we haven't read in the extent list, then don't do it now. */
4049 if (!(ip->i_df.if_flags & XFS_IFEXTENTS))
4050 return false;
4053 * Do not free real preallocated or append-only files unless the file
4054 * has delalloc blocks and we are forced to remove them.
4056 if (ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND))
4057 if (!force || ip->i_delayed_blks == 0)
4058 return false;
4060 return true;