2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
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>
22 #include "xfs_types.h"
25 #include "xfs_trans.h"
26 #include "xfs_trans_priv.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"
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
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;
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
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;
104 lock_mode = XFS_ILOCK_SHARED;
107 xfs_ilock(ip, 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.
117 xfs_iunlock_map_shared(
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:
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
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
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))
206 } else if (lock_flags & XFS_IOLOCK_SHARED) {
207 if (!mrtryaccess(&ip->i_iolock))
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;
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);
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.
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.
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)
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);
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);
321 prepare_to_wait_exclusive(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
322 if (xfs_isiflocked(ip))
324 } while (!xfs_iflock_nowait(ip));
326 finish_wait(wq, &wait.wait);
331 * Make sure that the extents in the given memory buffer
335 xfs_validate_extents(
340 xfs_bmbt_irec_t irec;
341 xfs_bmbt_rec_host_t rec;
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);
354 #define xfs_validate_extents(ifp, nrecs, fmt)
358 * Check that none of the inode's in the buffer have a next
359 * unlinked field of 0.
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) {
378 "Detected bogus zero next_unlinked field in incore inode buffer 0x%p.",
380 ASSERT(dip->di_next_unlinked);
387 xfs_inode_buf_verify(
390 struct xfs_mount *mp = bp->b_target->bt_mount;
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++) {
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,
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));
421 xfs_inobp_check(mp, bp);
426 xfs_inode_buf_read_verify(
429 xfs_inode_buf_verify(bp);
433 xfs_inode_buf_write_verify(
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
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,
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,
472 if (error == EAGAIN) {
473 ASSERT(buf_flags & XBF_TRYLOCK);
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.",
487 *dipp = (struct xfs_dinode *)xfs_buf_offset(bp, imap->im_boffset);
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()).
505 xfs_attr_shortform_t *atp;
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)),
519 be64_to_cpu(dip->di_nblocks));
520 XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW,
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,
529 XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW,
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.",
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) {
549 if (unlikely(dip->di_format != XFS_DINODE_FMT_DEV)) {
550 XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW,
552 return XFS_ERROR(EFSCORRUPTED);
555 ip->i_df.if_u2.if_rdev = xfs_dinode_get_rdev(dip);
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)",
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)",
585 return XFS_ERROR(EFSCORRUPTED);
589 error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size);
591 case XFS_DINODE_FMT_EXTENTS:
592 error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
594 case XFS_DINODE_FMT_BTREE:
595 error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
598 XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW,
600 return XFS_ERROR(EFSCORRUPTED);
605 XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW, ip->i_mount);
606 return XFS_ERROR(EFSCORRUPTED);
611 if (!XFS_DFORK_Q(dip))
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,
627 XFS_CORRUPTION_ERROR("xfs_iformat(8)",
630 return XFS_ERROR(EFSCORRUPTED);
633 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size);
635 case XFS_DINODE_FMT_EXTENTS:
636 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
638 case XFS_DINODE_FMT_BTREE:
639 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
642 error = XFS_ERROR(EFSCORRUPTED);
646 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
648 xfs_idestroy_fork(ip, XFS_DATA_FORK);
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.
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,
685 return XFS_ERROR(EFSCORRUPTED);
687 ifp = XFS_IFORK_PTR(ip, whichfork);
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;
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;
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;
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.
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,
741 return XFS_ERROR(EFSCORRUPTED);
744 ifp->if_real_bytes = 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;
750 xfs_iext_add(ifp, 0, nex);
752 ifp->if_bytes = 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(
766 XFS_ERROR_REPORT("xfs_iformat_extents(2)",
769 return XFS_ERROR(EFSCORRUPTED);
772 ifp->if_flags |= XFS_IFEXTENTS;
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).
790 struct xfs_mount *mp = ip->i_mount;
791 xfs_bmdr_block_t *dfp;
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
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,
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;
837 xfs_dinode_from_disk(
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);
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);
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;
974 xfs_icdinode_t *dic = &ip->i_d;
976 return _xfs_dic2xflags(dic->di_flags) |
977 (XFS_IFORK_Q(ip) ? XFS_XFLAG_HASATTR : 0);
984 return _xfs_dic2xflags(be16_to_cpu(dip->di_flags)) |
985 (XFS_DFORK_Q(dip) ? XFS_XFLAG_HASATTR : 0);
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))
997 /* only version 3 or greater inodes are extensively verified here */
998 if (dip->di_version < 3)
1001 if (!xfs_sb_version_hascrc(&mp->m_sb))
1003 if (!xfs_verify_cksum((char *)dip, mp->m_sb.sb_inodesize,
1004 offsetof(struct xfs_dinode, di_crc)))
1006 if (be64_to_cpu(dip->di_ino) != ip->i_ino)
1008 if (!uuid_equal(&dip->di_uuid, &mp->m_sb.sb_uuid))
1014 xfs_dinode_calc_crc(
1015 struct xfs_mount *mp,
1016 struct xfs_dinode *dip)
1020 if (dip->di_version < 3)
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.
1044 * Fill in the location information in the in-core inode.
1046 error = xfs_imap(mp, tp, ip->i_ino, &ip->i_imap, iget_flags);
1051 * Get pointers to the on-disk inode and the buffer containing it.
1053 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &bp, 0, iget_flags);
1057 /* even unallocated inodes are verified */
1058 if (!xfs_dinode_verify(mp, ip, dip)) {
1059 xfs_alert(mp, "%s: validation failed for inode %lld failed",
1060 __func__, ip->i_ino);
1062 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, dip);
1063 error = XFS_ERROR(EFSCORRUPTED);
1068 * If the on-disk inode is already linked to a directory
1069 * entry, copy all of the inode into the in-core inode.
1070 * xfs_iformat() handles copying in the inode format
1071 * specific information.
1072 * Otherwise, just get the truly permanent information.
1075 xfs_dinode_from_disk(&ip->i_d, dip);
1076 error = xfs_iformat(ip, dip);
1079 xfs_alert(mp, "%s: xfs_iformat() returned error %d",
1086 * Partial initialisation of the in-core inode. Just the bits
1087 * that xfs_ialloc won't overwrite or relies on being correct.
1089 ip->i_d.di_magic = be16_to_cpu(dip->di_magic);
1090 ip->i_d.di_version = dip->di_version;
1091 ip->i_d.di_gen = be32_to_cpu(dip->di_gen);
1092 ip->i_d.di_flushiter = be16_to_cpu(dip->di_flushiter);
1094 if (dip->di_version == 3) {
1095 ip->i_d.di_ino = be64_to_cpu(dip->di_ino);
1096 uuid_copy(&ip->i_d.di_uuid, &dip->di_uuid);
1100 * Make sure to pull in the mode here as well in
1101 * case the inode is released without being used.
1102 * This ensures that xfs_inactive() will see that
1103 * the inode is already free and not try to mess
1104 * with the uninitialized part of it.
1106 ip->i_d.di_mode = 0;
1110 * The inode format changed when we moved the link count and
1111 * made it 32 bits long. If this is an old format inode,
1112 * convert it in memory to look like a new one. If it gets
1113 * flushed to disk we will convert back before flushing or
1114 * logging it. We zero out the new projid field and the old link
1115 * count field. We'll handle clearing the pad field (the remains
1116 * of the old uuid field) when we actually convert the inode to
1117 * the new format. We don't change the version number so that we
1118 * can distinguish this from a real new format inode.
1120 if (ip->i_d.di_version == 1) {
1121 ip->i_d.di_nlink = ip->i_d.di_onlink;
1122 ip->i_d.di_onlink = 0;
1123 xfs_set_projid(ip, 0);
1126 ip->i_delayed_blks = 0;
1129 * Mark the buffer containing the inode as something to keep
1130 * around for a while. This helps to keep recently accessed
1131 * meta-data in-core longer.
1133 xfs_buf_set_ref(bp, XFS_INO_REF);
1136 * Use xfs_trans_brelse() to release the buffer containing the
1137 * on-disk inode, because it was acquired with xfs_trans_read_buf()
1138 * in xfs_imap_to_bp() above. If tp is NULL, this is just a normal
1139 * brelse(). If we're within a transaction, then xfs_trans_brelse()
1140 * will only release the buffer if it is not dirty within the
1141 * transaction. It will be OK to release the buffer in this case,
1142 * because inodes on disk are never destroyed and we will be
1143 * locking the new in-core inode before putting it in the hash
1144 * table where other processes can find it. Thus we don't have
1145 * to worry about the inode being changed just because we released
1149 xfs_trans_brelse(tp, bp);
1154 * Read in extents from a btree-format inode.
1155 * Allocate and fill in if_extents. Real work is done in xfs_bmap.c.
1165 xfs_extnum_t nextents;
1167 if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
1168 XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW,
1170 return XFS_ERROR(EFSCORRUPTED);
1172 nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
1173 ifp = XFS_IFORK_PTR(ip, whichfork);
1176 * We know that the size is valid (it's checked in iformat_btree)
1178 ifp->if_bytes = ifp->if_real_bytes = 0;
1179 ifp->if_flags |= XFS_IFEXTENTS;
1180 xfs_iext_add(ifp, 0, nextents);
1181 error = xfs_bmap_read_extents(tp, ip, whichfork);
1183 xfs_iext_destroy(ifp);
1184 ifp->if_flags &= ~XFS_IFEXTENTS;
1187 xfs_validate_extents(ifp, nextents, XFS_EXTFMT_INODE(ip));
1192 * Allocate an inode on disk and return a copy of its in-core version.
1193 * The in-core inode is locked exclusively. Set mode, nlink, and rdev
1194 * appropriately within the inode. The uid and gid for the inode are
1195 * set according to the contents of the given cred structure.
1197 * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc()
1198 * has a free inode available, call xfs_iget() to obtain the in-core
1199 * version of the allocated inode. Finally, fill in the inode and
1200 * log its initial contents. In this case, ialloc_context would be
1203 * If xfs_dialloc() does not have an available inode, it will replenish
1204 * its supply by doing an allocation. Since we can only do one
1205 * allocation within a transaction without deadlocks, we must commit
1206 * the current transaction before returning the inode itself.
1207 * In this case, therefore, we will set ialloc_context and return.
1208 * The caller should then commit the current transaction, start a new
1209 * transaction, and call xfs_ialloc() again to actually get the inode.
1211 * To ensure that some other process does not grab the inode that
1212 * was allocated during the first call to xfs_ialloc(), this routine
1213 * also returns the [locked] bp pointing to the head of the freelist
1214 * as ialloc_context. The caller should hold this buffer across
1215 * the commit and pass it back into this routine on the second call.
1217 * If we are allocating quota inodes, we do not have a parent inode
1218 * to attach to or associate with (i.e. pip == NULL) because they
1219 * are not linked into the directory structure - they are attached
1220 * directly to the superblock - and so have no parent.
1231 xfs_buf_t **ialloc_context,
1234 struct xfs_mount *mp = tp->t_mountp;
1240 int filestreams = 0;
1243 * Call the space management code to pick
1244 * the on-disk inode to be allocated.
1246 error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc,
1247 ialloc_context, &ino);
1250 if (*ialloc_context || ino == NULLFSINO) {
1254 ASSERT(*ialloc_context == NULL);
1257 * Get the in-core inode with the lock held exclusively.
1258 * This is because we're setting fields here we need
1259 * to prevent others from looking at until we're done.
1261 error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE,
1262 XFS_ILOCK_EXCL, &ip);
1267 ip->i_d.di_mode = mode;
1268 ip->i_d.di_onlink = 0;
1269 ip->i_d.di_nlink = nlink;
1270 ASSERT(ip->i_d.di_nlink == nlink);
1271 ip->i_d.di_uid = current_fsuid();
1272 ip->i_d.di_gid = current_fsgid();
1273 xfs_set_projid(ip, prid);
1274 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
1277 * If the superblock version is up to where we support new format
1278 * inodes and this is currently an old format inode, then change
1279 * the inode version number now. This way we only do the conversion
1280 * here rather than here and in the flush/logging code.
1282 if (xfs_sb_version_hasnlink(&mp->m_sb) &&
1283 ip->i_d.di_version == 1) {
1284 ip->i_d.di_version = 2;
1286 * We've already zeroed the old link count, the projid field,
1287 * and the pad field.
1292 * Project ids won't be stored on disk if we are using a version 1 inode.
1294 if ((prid != 0) && (ip->i_d.di_version == 1))
1295 xfs_bump_ino_vers2(tp, ip);
1297 if (pip && XFS_INHERIT_GID(pip)) {
1298 ip->i_d.di_gid = pip->i_d.di_gid;
1299 if ((pip->i_d.di_mode & S_ISGID) && S_ISDIR(mode)) {
1300 ip->i_d.di_mode |= S_ISGID;
1305 * If the group ID of the new file does not match the effective group
1306 * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
1307 * (and only if the irix_sgid_inherit compatibility variable is set).
1309 if ((irix_sgid_inherit) &&
1310 (ip->i_d.di_mode & S_ISGID) &&
1311 (!in_group_p((gid_t)ip->i_d.di_gid))) {
1312 ip->i_d.di_mode &= ~S_ISGID;
1315 ip->i_d.di_size = 0;
1316 ip->i_d.di_nextents = 0;
1317 ASSERT(ip->i_d.di_nblocks == 0);
1320 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
1321 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
1322 ip->i_d.di_atime = ip->i_d.di_mtime;
1323 ip->i_d.di_ctime = ip->i_d.di_mtime;
1326 * di_gen will have been taken care of in xfs_iread.
1328 ip->i_d.di_extsize = 0;
1329 ip->i_d.di_dmevmask = 0;
1330 ip->i_d.di_dmstate = 0;
1331 ip->i_d.di_flags = 0;
1333 if (ip->i_d.di_version == 3) {
1334 ASSERT(ip->i_d.di_ino == ino);
1335 ASSERT(uuid_equal(&ip->i_d.di_uuid, &mp->m_sb.sb_uuid));
1337 ip->i_d.di_changecount = 1;
1339 ip->i_d.di_flags2 = 0;
1340 memset(&(ip->i_d.di_pad2[0]), 0, sizeof(ip->i_d.di_pad2));
1341 ip->i_d.di_crtime = ip->i_d.di_mtime;
1345 flags = XFS_ILOG_CORE;
1346 switch (mode & S_IFMT) {
1351 ip->i_d.di_format = XFS_DINODE_FMT_DEV;
1352 ip->i_df.if_u2.if_rdev = rdev;
1353 ip->i_df.if_flags = 0;
1354 flags |= XFS_ILOG_DEV;
1358 * we can't set up filestreams until after the VFS inode
1359 * is set up properly.
1361 if (pip && xfs_inode_is_filestream(pip))
1365 if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) {
1368 if (S_ISDIR(mode)) {
1369 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
1370 di_flags |= XFS_DIFLAG_RTINHERIT;
1371 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
1372 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
1373 ip->i_d.di_extsize = pip->i_d.di_extsize;
1375 } else if (S_ISREG(mode)) {
1376 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
1377 di_flags |= XFS_DIFLAG_REALTIME;
1378 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
1379 di_flags |= XFS_DIFLAG_EXTSIZE;
1380 ip->i_d.di_extsize = pip->i_d.di_extsize;
1383 if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
1384 xfs_inherit_noatime)
1385 di_flags |= XFS_DIFLAG_NOATIME;
1386 if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
1388 di_flags |= XFS_DIFLAG_NODUMP;
1389 if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
1391 di_flags |= XFS_DIFLAG_SYNC;
1392 if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
1393 xfs_inherit_nosymlinks)
1394 di_flags |= XFS_DIFLAG_NOSYMLINKS;
1395 if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1396 di_flags |= XFS_DIFLAG_PROJINHERIT;
1397 if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
1398 xfs_inherit_nodefrag)
1399 di_flags |= XFS_DIFLAG_NODEFRAG;
1400 if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
1401 di_flags |= XFS_DIFLAG_FILESTREAM;
1402 ip->i_d.di_flags |= di_flags;
1406 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
1407 ip->i_df.if_flags = XFS_IFEXTENTS;
1408 ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0;
1409 ip->i_df.if_u1.if_extents = NULL;
1415 * Attribute fork settings for new inode.
1417 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1418 ip->i_d.di_anextents = 0;
1421 * Log the new values stuffed into the inode.
1423 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1424 xfs_trans_log_inode(tp, ip, flags);
1426 /* now that we have an i_mode we can setup inode ops and unlock */
1427 xfs_setup_inode(ip);
1429 /* now we have set up the vfs inode we can associate the filestream */
1431 error = xfs_filestream_associate(pip, ip);
1435 xfs_iflags_set(ip, XFS_IFILESTREAM);
1443 * Free up the underlying blocks past new_size. The new size must be smaller
1444 * than the current size. This routine can be used both for the attribute and
1445 * data fork, and does not modify the inode size, which is left to the caller.
1447 * The transaction passed to this routine must have made a permanent log
1448 * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the
1449 * given transaction and start new ones, so make sure everything involved in
1450 * the transaction is tidy before calling here. Some transaction will be
1451 * returned to the caller to be committed. The incoming transaction must
1452 * already include the inode, and both inode locks must be held exclusively.
1453 * The inode must also be "held" within the transaction. On return the inode
1454 * will be "held" within the returned transaction. This routine does NOT
1455 * require any disk space to be reserved for it within the transaction.
1457 * If we get an error, we must return with the inode locked and linked into the
1458 * current transaction. This keeps things simple for the higher level code,
1459 * because it always knows that the inode is locked and held in the transaction
1460 * that returns to it whether errors occur or not. We don't mark the inode
1461 * dirty on error so that transactions can be easily aborted if possible.
1464 xfs_itruncate_extents(
1465 struct xfs_trans **tpp,
1466 struct xfs_inode *ip,
1468 xfs_fsize_t new_size)
1470 struct xfs_mount *mp = ip->i_mount;
1471 struct xfs_trans *tp = *tpp;
1472 struct xfs_trans *ntp;
1473 xfs_bmap_free_t free_list;
1474 xfs_fsblock_t first_block;
1475 xfs_fileoff_t first_unmap_block;
1476 xfs_fileoff_t last_block;
1477 xfs_filblks_t unmap_len;
1482 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1483 ASSERT(!atomic_read(&VFS_I(ip)->i_count) ||
1484 xfs_isilocked(ip, XFS_IOLOCK_EXCL));
1485 ASSERT(new_size <= XFS_ISIZE(ip));
1486 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1487 ASSERT(ip->i_itemp != NULL);
1488 ASSERT(ip->i_itemp->ili_lock_flags == 0);
1489 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1491 trace_xfs_itruncate_extents_start(ip, new_size);
1494 * Since it is possible for space to become allocated beyond
1495 * the end of the file (in a crash where the space is allocated
1496 * but the inode size is not yet updated), simply remove any
1497 * blocks which show up between the new EOF and the maximum
1498 * possible file size. If the first block to be removed is
1499 * beyond the maximum file size (ie it is the same as last_block),
1500 * then there is nothing to do.
1502 first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
1503 last_block = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
1504 if (first_unmap_block == last_block)
1507 ASSERT(first_unmap_block < last_block);
1508 unmap_len = last_block - first_unmap_block + 1;
1510 xfs_bmap_init(&free_list, &first_block);
1511 error = xfs_bunmapi(tp, ip,
1512 first_unmap_block, unmap_len,
1513 xfs_bmapi_aflag(whichfork),
1514 XFS_ITRUNC_MAX_EXTENTS,
1515 &first_block, &free_list,
1518 goto out_bmap_cancel;
1521 * Duplicate the transaction that has the permanent
1522 * reservation and commit the old transaction.
1524 error = xfs_bmap_finish(&tp, &free_list, &committed);
1526 xfs_trans_ijoin(tp, ip, 0);
1528 goto out_bmap_cancel;
1532 * Mark the inode dirty so it will be logged and
1533 * moved forward in the log as part of every commit.
1535 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1538 ntp = xfs_trans_dup(tp);
1539 error = xfs_trans_commit(tp, 0);
1542 xfs_trans_ijoin(tp, ip, 0);
1548 * Transaction commit worked ok so we can drop the extra ticket
1549 * reference that we gained in xfs_trans_dup()
1551 xfs_log_ticket_put(tp->t_ticket);
1552 error = xfs_trans_reserve(tp, 0,
1553 XFS_ITRUNCATE_LOG_RES(mp), 0,
1554 XFS_TRANS_PERM_LOG_RES,
1555 XFS_ITRUNCATE_LOG_COUNT);
1561 * Always re-log the inode so that our permanent transaction can keep
1562 * on rolling it forward in the log.
1564 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1566 trace_xfs_itruncate_extents_end(ip, new_size);
1573 * If the bunmapi call encounters an error, return to the caller where
1574 * the transaction can be properly aborted. We just need to make sure
1575 * we're not holding any resources that we were not when we came in.
1577 xfs_bmap_cancel(&free_list);
1582 * This is called when the inode's link count goes to 0.
1583 * We place the on-disk inode on a list in the AGI. It
1584 * will be pulled from this list when the inode is freed.
1601 ASSERT(ip->i_d.di_nlink == 0);
1602 ASSERT(ip->i_d.di_mode != 0);
1607 * Get the agi buffer first. It ensures lock ordering
1610 error = xfs_read_agi(mp, tp, XFS_INO_TO_AGNO(mp, ip->i_ino), &agibp);
1613 agi = XFS_BUF_TO_AGI(agibp);
1616 * Get the index into the agi hash table for the
1617 * list this inode will go on.
1619 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
1621 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
1622 ASSERT(agi->agi_unlinked[bucket_index]);
1623 ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino);
1625 if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) {
1627 * There is already another inode in the bucket we need
1628 * to add ourselves to. Add us at the front of the list.
1629 * Here we put the head pointer into our next pointer,
1630 * and then we fall through to point the head at us.
1632 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
1637 ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO));
1638 dip->di_next_unlinked = agi->agi_unlinked[bucket_index];
1639 offset = ip->i_imap.im_boffset +
1640 offsetof(xfs_dinode_t, di_next_unlinked);
1641 xfs_trans_inode_buf(tp, ibp);
1642 xfs_trans_log_buf(tp, ibp, offset,
1643 (offset + sizeof(xfs_agino_t) - 1));
1644 xfs_inobp_check(mp, ibp);
1648 * Point the bucket head pointer at the inode being inserted.
1651 agi->agi_unlinked[bucket_index] = cpu_to_be32(agino);
1652 offset = offsetof(xfs_agi_t, agi_unlinked) +
1653 (sizeof(xfs_agino_t) * bucket_index);
1654 xfs_trans_log_buf(tp, agibp, offset,
1655 (offset + sizeof(xfs_agino_t) - 1));
1660 * Pull the on-disk inode from the AGI unlinked list.
1673 xfs_agnumber_t agno;
1675 xfs_agino_t next_agino;
1676 xfs_buf_t *last_ibp;
1677 xfs_dinode_t *last_dip = NULL;
1679 int offset, last_offset = 0;
1683 agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
1686 * Get the agi buffer first. It ensures lock ordering
1689 error = xfs_read_agi(mp, tp, agno, &agibp);
1693 agi = XFS_BUF_TO_AGI(agibp);
1696 * Get the index into the agi hash table for the
1697 * list this inode will go on.
1699 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
1701 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
1702 ASSERT(agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO));
1703 ASSERT(agi->agi_unlinked[bucket_index]);
1705 if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) {
1707 * We're at the head of the list. Get the inode's on-disk
1708 * buffer to see if there is anyone after us on the list.
1709 * Only modify our next pointer if it is not already NULLAGINO.
1710 * This saves us the overhead of dealing with the buffer when
1711 * there is no need to change it.
1713 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
1716 xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.",
1720 next_agino = be32_to_cpu(dip->di_next_unlinked);
1721 ASSERT(next_agino != 0);
1722 if (next_agino != NULLAGINO) {
1723 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
1724 offset = ip->i_imap.im_boffset +
1725 offsetof(xfs_dinode_t, di_next_unlinked);
1726 xfs_trans_inode_buf(tp, ibp);
1727 xfs_trans_log_buf(tp, ibp, offset,
1728 (offset + sizeof(xfs_agino_t) - 1));
1729 xfs_inobp_check(mp, ibp);
1731 xfs_trans_brelse(tp, ibp);
1734 * Point the bucket head pointer at the next inode.
1736 ASSERT(next_agino != 0);
1737 ASSERT(next_agino != agino);
1738 agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino);
1739 offset = offsetof(xfs_agi_t, agi_unlinked) +
1740 (sizeof(xfs_agino_t) * bucket_index);
1741 xfs_trans_log_buf(tp, agibp, offset,
1742 (offset + sizeof(xfs_agino_t) - 1));
1745 * We need to search the list for the inode being freed.
1747 next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
1749 while (next_agino != agino) {
1750 struct xfs_imap imap;
1753 xfs_trans_brelse(tp, last_ibp);
1756 next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino);
1758 error = xfs_imap(mp, tp, next_ino, &imap, 0);
1761 "%s: xfs_imap returned error %d.",
1766 error = xfs_imap_to_bp(mp, tp, &imap, &last_dip,
1770 "%s: xfs_imap_to_bp returned error %d.",
1775 last_offset = imap.im_boffset;
1776 next_agino = be32_to_cpu(last_dip->di_next_unlinked);
1777 ASSERT(next_agino != NULLAGINO);
1778 ASSERT(next_agino != 0);
1782 * Now last_ibp points to the buffer previous to us on the
1783 * unlinked list. Pull us from the list.
1785 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
1788 xfs_warn(mp, "%s: xfs_imap_to_bp(2) returned error %d.",
1792 next_agino = be32_to_cpu(dip->di_next_unlinked);
1793 ASSERT(next_agino != 0);
1794 ASSERT(next_agino != agino);
1795 if (next_agino != NULLAGINO) {
1796 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
1797 offset = ip->i_imap.im_boffset +
1798 offsetof(xfs_dinode_t, di_next_unlinked);
1799 xfs_trans_inode_buf(tp, ibp);
1800 xfs_trans_log_buf(tp, ibp, offset,
1801 (offset + sizeof(xfs_agino_t) - 1));
1802 xfs_inobp_check(mp, ibp);
1804 xfs_trans_brelse(tp, ibp);
1807 * Point the previous inode on the list to the next inode.
1809 last_dip->di_next_unlinked = cpu_to_be32(next_agino);
1810 ASSERT(next_agino != 0);
1811 offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked);
1812 xfs_trans_inode_buf(tp, last_ibp);
1813 xfs_trans_log_buf(tp, last_ibp, offset,
1814 (offset + sizeof(xfs_agino_t) - 1));
1815 xfs_inobp_check(mp, last_ibp);
1821 * A big issue when freeing the inode cluster is is that we _cannot_ skip any
1822 * inodes that are in memory - they all must be marked stale and attached to
1823 * the cluster buffer.
1827 xfs_inode_t *free_ip,
1831 xfs_mount_t *mp = free_ip->i_mount;
1832 int blks_per_cluster;
1839 xfs_inode_log_item_t *iip;
1840 xfs_log_item_t *lip;
1841 struct xfs_perag *pag;
1843 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum));
1844 if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
1845 blks_per_cluster = 1;
1846 ninodes = mp->m_sb.sb_inopblock;
1847 nbufs = XFS_IALLOC_BLOCKS(mp);
1849 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
1850 mp->m_sb.sb_blocksize;
1851 ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
1852 nbufs = XFS_IALLOC_BLOCKS(mp) / blks_per_cluster;
1855 for (j = 0; j < nbufs; j++, inum += ninodes) {
1856 blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
1857 XFS_INO_TO_AGBNO(mp, inum));
1860 * We obtain and lock the backing buffer first in the process
1861 * here, as we have to ensure that any dirty inode that we
1862 * can't get the flush lock on is attached to the buffer.
1863 * If we scan the in-memory inodes first, then buffer IO can
1864 * complete before we get a lock on it, and hence we may fail
1865 * to mark all the active inodes on the buffer stale.
1867 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
1868 mp->m_bsize * blks_per_cluster,
1875 * This buffer may not have been correctly initialised as we
1876 * didn't read it from disk. That's not important because we are
1877 * only using to mark the buffer as stale in the log, and to
1878 * attach stale cached inodes on it. That means it will never be
1879 * dispatched for IO. If it is, we want to know about it, and we
1880 * want it to fail. We can acheive this by adding a write
1881 * verifier to the buffer.
1883 bp->b_ops = &xfs_inode_buf_ops;
1886 * Walk the inodes already attached to the buffer and mark them
1887 * stale. These will all have the flush locks held, so an
1888 * in-memory inode walk can't lock them. By marking them all
1889 * stale first, we will not attempt to lock them in the loop
1890 * below as the XFS_ISTALE flag will be set.
1894 if (lip->li_type == XFS_LI_INODE) {
1895 iip = (xfs_inode_log_item_t *)lip;
1896 ASSERT(iip->ili_logged == 1);
1897 lip->li_cb = xfs_istale_done;
1898 xfs_trans_ail_copy_lsn(mp->m_ail,
1899 &iip->ili_flush_lsn,
1900 &iip->ili_item.li_lsn);
1901 xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
1903 lip = lip->li_bio_list;
1908 * For each inode in memory attempt to add it to the inode
1909 * buffer and set it up for being staled on buffer IO
1910 * completion. This is safe as we've locked out tail pushing
1911 * and flushing by locking the buffer.
1913 * We have already marked every inode that was part of a
1914 * transaction stale above, which means there is no point in
1915 * even trying to lock them.
1917 for (i = 0; i < ninodes; i++) {
1920 ip = radix_tree_lookup(&pag->pag_ici_root,
1921 XFS_INO_TO_AGINO(mp, (inum + i)));
1923 /* Inode not in memory, nothing to do */
1930 * because this is an RCU protected lookup, we could
1931 * find a recently freed or even reallocated inode
1932 * during the lookup. We need to check under the
1933 * i_flags_lock for a valid inode here. Skip it if it
1934 * is not valid, the wrong inode or stale.
1936 spin_lock(&ip->i_flags_lock);
1937 if (ip->i_ino != inum + i ||
1938 __xfs_iflags_test(ip, XFS_ISTALE)) {
1939 spin_unlock(&ip->i_flags_lock);
1943 spin_unlock(&ip->i_flags_lock);
1946 * Don't try to lock/unlock the current inode, but we
1947 * _cannot_ skip the other inodes that we did not find
1948 * in the list attached to the buffer and are not
1949 * already marked stale. If we can't lock it, back off
1952 if (ip != free_ip &&
1953 !xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
1961 xfs_iflags_set(ip, XFS_ISTALE);
1964 * we don't need to attach clean inodes or those only
1965 * with unlogged changes (which we throw away, anyway).
1968 if (!iip || xfs_inode_clean(ip)) {
1969 ASSERT(ip != free_ip);
1971 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1975 iip->ili_last_fields = iip->ili_fields;
1976 iip->ili_fields = 0;
1977 iip->ili_logged = 1;
1978 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
1979 &iip->ili_item.li_lsn);
1981 xfs_buf_attach_iodone(bp, xfs_istale_done,
1985 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1988 xfs_trans_stale_inode_buf(tp, bp);
1989 xfs_trans_binval(tp, bp);
1997 * This is called to return an inode to the inode free list.
1998 * The inode should already be truncated to 0 length and have
1999 * no pages associated with it. This routine also assumes that
2000 * the inode is already a part of the transaction.
2002 * The on-disk copy of the inode will have been added to the list
2003 * of unlinked inodes in the AGI. We need to remove the inode from
2004 * that list atomically with respect to freeing it here.
2010 xfs_bmap_free_t *flist)
2014 xfs_ino_t first_ino;
2018 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
2019 ASSERT(ip->i_d.di_nlink == 0);
2020 ASSERT(ip->i_d.di_nextents == 0);
2021 ASSERT(ip->i_d.di_anextents == 0);
2022 ASSERT(ip->i_d.di_size == 0 || !S_ISREG(ip->i_d.di_mode));
2023 ASSERT(ip->i_d.di_nblocks == 0);
2026 * Pull the on-disk inode from the AGI unlinked list.
2028 error = xfs_iunlink_remove(tp, ip);
2033 error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino);
2037 ip->i_d.di_mode = 0; /* mark incore inode as free */
2038 ip->i_d.di_flags = 0;
2039 ip->i_d.di_dmevmask = 0;
2040 ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */
2041 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
2042 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
2044 * Bump the generation count so no one will be confused
2045 * by reincarnations of this inode.
2049 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2051 error = xfs_imap_to_bp(ip->i_mount, tp, &ip->i_imap, &dip, &ibp,
2057 * Clear the on-disk di_mode. This is to prevent xfs_bulkstat
2058 * from picking up this inode when it is reclaimed (its incore state
2059 * initialzed but not flushed to disk yet). The in-core di_mode is
2060 * already cleared and a corresponding transaction logged.
2061 * The hack here just synchronizes the in-core to on-disk
2062 * di_mode value in advance before the actual inode sync to disk.
2063 * This is OK because the inode is already unlinked and would never
2064 * change its di_mode again for this inode generation.
2065 * This is a temporary hack that would require a proper fix
2071 error = xfs_ifree_cluster(ip, tp, first_ino);
2078 * Reallocate the space for if_broot based on the number of records
2079 * being added or deleted as indicated in rec_diff. Move the records
2080 * and pointers in if_broot to fit the new size. When shrinking this
2081 * will eliminate holes between the records and pointers created by
2082 * the caller. When growing this will create holes to be filled in
2085 * The caller must not request to add more records than would fit in
2086 * the on-disk inode root. If the if_broot is currently NULL, then
2087 * if we adding records one will be allocated. The caller must also
2088 * not request that the number of records go below zero, although
2089 * it can go to zero.
2091 * ip -- the inode whose if_broot area is changing
2092 * ext_diff -- the change in the number of records, positive or negative,
2093 * requested for the if_broot array.
2101 struct xfs_mount *mp = ip->i_mount;
2104 struct xfs_btree_block *new_broot;
2111 * Handle the degenerate case quietly.
2113 if (rec_diff == 0) {
2117 ifp = XFS_IFORK_PTR(ip, whichfork);
2120 * If there wasn't any memory allocated before, just
2121 * allocate it now and get out.
2123 if (ifp->if_broot_bytes == 0) {
2124 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
2125 ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
2126 ifp->if_broot_bytes = (int)new_size;
2131 * If there is already an existing if_broot, then we need
2132 * to realloc() it and shift the pointers to their new
2133 * location. The records don't change location because
2134 * they are kept butted up against the btree block header.
2136 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
2137 new_max = cur_max + rec_diff;
2138 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
2139 ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
2140 XFS_BMAP_BROOT_SPACE_CALC(mp, cur_max),
2141 KM_SLEEP | KM_NOFS);
2142 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
2143 ifp->if_broot_bytes);
2144 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
2146 ifp->if_broot_bytes = (int)new_size;
2147 ASSERT(ifp->if_broot_bytes <=
2148 XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ(ip));
2149 memmove(np, op, cur_max * (uint)sizeof(xfs_dfsbno_t));
2154 * rec_diff is less than 0. In this case, we are shrinking the
2155 * if_broot buffer. It must already exist. If we go to zero
2156 * records, just get rid of the root and clear the status bit.
2158 ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
2159 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
2160 new_max = cur_max + rec_diff;
2161 ASSERT(new_max >= 0);
2163 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
2167 new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
2169 * First copy over the btree block header.
2171 memcpy(new_broot, ifp->if_broot,
2172 XFS_BMBT_BLOCK_LEN(ip->i_mount));
2175 ifp->if_flags &= ~XFS_IFBROOT;
2179 * Only copy the records and pointers if there are any.
2183 * First copy the records.
2185 op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
2186 np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
2187 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
2190 * Then copy the pointers.
2192 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
2193 ifp->if_broot_bytes);
2194 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
2196 memcpy(np, op, new_max * (uint)sizeof(xfs_dfsbno_t));
2198 kmem_free(ifp->if_broot);
2199 ifp->if_broot = new_broot;
2200 ifp->if_broot_bytes = (int)new_size;
2201 ASSERT(ifp->if_broot_bytes <=
2202 XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ(ip));
2208 * This is called when the amount of space needed for if_data
2209 * is increased or decreased. The change in size is indicated by
2210 * the number of bytes that need to be added or deleted in the
2211 * byte_diff parameter.
2213 * If the amount of space needed has decreased below the size of the
2214 * inline buffer, then switch to using the inline buffer. Otherwise,
2215 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
2216 * to what is needed.
2218 * ip -- the inode whose if_data area is changing
2219 * byte_diff -- the change in the number of bytes, positive or negative,
2220 * requested for the if_data array.
2232 if (byte_diff == 0) {
2236 ifp = XFS_IFORK_PTR(ip, whichfork);
2237 new_size = (int)ifp->if_bytes + byte_diff;
2238 ASSERT(new_size >= 0);
2240 if (new_size == 0) {
2241 if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
2242 kmem_free(ifp->if_u1.if_data);
2244 ifp->if_u1.if_data = NULL;
2246 } else if (new_size <= sizeof(ifp->if_u2.if_inline_data)) {
2248 * If the valid extents/data can fit in if_inline_ext/data,
2249 * copy them from the malloc'd vector and free it.
2251 if (ifp->if_u1.if_data == NULL) {
2252 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
2253 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
2254 ASSERT(ifp->if_real_bytes != 0);
2255 memcpy(ifp->if_u2.if_inline_data, ifp->if_u1.if_data,
2257 kmem_free(ifp->if_u1.if_data);
2258 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
2263 * Stuck with malloc/realloc.
2264 * For inline data, the underlying buffer must be
2265 * a multiple of 4 bytes in size so that it can be
2266 * logged and stay on word boundaries. We enforce
2269 real_size = roundup(new_size, 4);
2270 if (ifp->if_u1.if_data == NULL) {
2271 ASSERT(ifp->if_real_bytes == 0);
2272 ifp->if_u1.if_data = kmem_alloc(real_size,
2273 KM_SLEEP | KM_NOFS);
2274 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
2276 * Only do the realloc if the underlying size
2277 * is really changing.
2279 if (ifp->if_real_bytes != real_size) {
2280 ifp->if_u1.if_data =
2281 kmem_realloc(ifp->if_u1.if_data,
2284 KM_SLEEP | KM_NOFS);
2287 ASSERT(ifp->if_real_bytes == 0);
2288 ifp->if_u1.if_data = kmem_alloc(real_size,
2289 KM_SLEEP | KM_NOFS);
2290 memcpy(ifp->if_u1.if_data, ifp->if_u2.if_inline_data,
2294 ifp->if_real_bytes = real_size;
2295 ifp->if_bytes = new_size;
2296 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
2306 ifp = XFS_IFORK_PTR(ip, whichfork);
2307 if (ifp->if_broot != NULL) {
2308 kmem_free(ifp->if_broot);
2309 ifp->if_broot = NULL;
2313 * If the format is local, then we can't have an extents
2314 * array so just look for an inline data array. If we're
2315 * not local then we may or may not have an extents list,
2316 * so check and free it up if we do.
2318 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
2319 if ((ifp->if_u1.if_data != ifp->if_u2.if_inline_data) &&
2320 (ifp->if_u1.if_data != NULL)) {
2321 ASSERT(ifp->if_real_bytes != 0);
2322 kmem_free(ifp->if_u1.if_data);
2323 ifp->if_u1.if_data = NULL;
2324 ifp->if_real_bytes = 0;
2326 } else if ((ifp->if_flags & XFS_IFEXTENTS) &&
2327 ((ifp->if_flags & XFS_IFEXTIREC) ||
2328 ((ifp->if_u1.if_extents != NULL) &&
2329 (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext)))) {
2330 ASSERT(ifp->if_real_bytes != 0);
2331 xfs_iext_destroy(ifp);
2333 ASSERT(ifp->if_u1.if_extents == NULL ||
2334 ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext);
2335 ASSERT(ifp->if_real_bytes == 0);
2336 if (whichfork == XFS_ATTR_FORK) {
2337 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
2343 * This is called to unpin an inode. The caller must have the inode locked
2344 * in at least shared mode so that the buffer cannot be subsequently pinned
2345 * once someone is waiting for it to be unpinned.
2349 struct xfs_inode *ip)
2351 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
2353 trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
2355 /* Give the log a push to start the unpinning I/O */
2356 xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0);
2362 struct xfs_inode *ip)
2364 wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
2365 DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
2370 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2371 if (xfs_ipincount(ip))
2373 } while (xfs_ipincount(ip));
2374 finish_wait(wq, &wait.wait);
2379 struct xfs_inode *ip)
2381 if (xfs_ipincount(ip))
2382 __xfs_iunpin_wait(ip);
2386 * xfs_iextents_copy()
2388 * This is called to copy the REAL extents (as opposed to the delayed
2389 * allocation extents) from the inode into the given buffer. It
2390 * returns the number of bytes copied into the buffer.
2392 * If there are no delayed allocation extents, then we can just
2393 * memcpy() the extents into the buffer. Otherwise, we need to
2394 * examine each extent in turn and skip those which are delayed.
2406 xfs_fsblock_t start_block;
2408 ifp = XFS_IFORK_PTR(ip, whichfork);
2409 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
2410 ASSERT(ifp->if_bytes > 0);
2412 nrecs = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
2413 XFS_BMAP_TRACE_EXLIST(ip, nrecs, whichfork);
2417 * There are some delayed allocation extents in the
2418 * inode, so copy the extents one at a time and skip
2419 * the delayed ones. There must be at least one
2420 * non-delayed extent.
2423 for (i = 0; i < nrecs; i++) {
2424 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
2425 start_block = xfs_bmbt_get_startblock(ep);
2426 if (isnullstartblock(start_block)) {
2428 * It's a delayed allocation extent, so skip it.
2433 /* Translate to on disk format */
2434 put_unaligned(cpu_to_be64(ep->l0), &dp->l0);
2435 put_unaligned(cpu_to_be64(ep->l1), &dp->l1);
2439 ASSERT(copied != 0);
2440 xfs_validate_extents(ifp, copied, XFS_EXTFMT_INODE(ip));
2442 return (copied * (uint)sizeof(xfs_bmbt_rec_t));
2446 * Each of the following cases stores data into the same region
2447 * of the on-disk inode, so only one of them can be valid at
2448 * any given time. While it is possible to have conflicting formats
2449 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
2450 * in EXTENTS format, this can only happen when the fork has
2451 * changed formats after being modified but before being flushed.
2452 * In these cases, the format always takes precedence, because the
2453 * format indicates the current state of the fork.
2460 xfs_inode_log_item_t *iip,
2467 static const short brootflag[2] =
2468 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
2469 static const short dataflag[2] =
2470 { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
2471 static const short extflag[2] =
2472 { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
2476 ifp = XFS_IFORK_PTR(ip, whichfork);
2478 * This can happen if we gave up in iformat in an error path,
2479 * for the attribute fork.
2482 ASSERT(whichfork == XFS_ATTR_FORK);
2485 cp = XFS_DFORK_PTR(dip, whichfork);
2487 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
2488 case XFS_DINODE_FMT_LOCAL:
2489 if ((iip->ili_fields & dataflag[whichfork]) &&
2490 (ifp->if_bytes > 0)) {
2491 ASSERT(ifp->if_u1.if_data != NULL);
2492 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
2493 memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
2497 case XFS_DINODE_FMT_EXTENTS:
2498 ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
2499 !(iip->ili_fields & extflag[whichfork]));
2500 if ((iip->ili_fields & extflag[whichfork]) &&
2501 (ifp->if_bytes > 0)) {
2502 ASSERT(xfs_iext_get_ext(ifp, 0));
2503 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
2504 (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
2509 case XFS_DINODE_FMT_BTREE:
2510 if ((iip->ili_fields & brootflag[whichfork]) &&
2511 (ifp->if_broot_bytes > 0)) {
2512 ASSERT(ifp->if_broot != NULL);
2513 ASSERT(ifp->if_broot_bytes <=
2514 (XFS_IFORK_SIZE(ip, whichfork) +
2515 XFS_BROOT_SIZE_ADJ(ip)));
2516 xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
2517 (xfs_bmdr_block_t *)cp,
2518 XFS_DFORK_SIZE(dip, mp, whichfork));
2522 case XFS_DINODE_FMT_DEV:
2523 if (iip->ili_fields & XFS_ILOG_DEV) {
2524 ASSERT(whichfork == XFS_DATA_FORK);
2525 xfs_dinode_put_rdev(dip, ip->i_df.if_u2.if_rdev);
2529 case XFS_DINODE_FMT_UUID:
2530 if (iip->ili_fields & XFS_ILOG_UUID) {
2531 ASSERT(whichfork == XFS_DATA_FORK);
2532 memcpy(XFS_DFORK_DPTR(dip),
2533 &ip->i_df.if_u2.if_uuid,
2549 xfs_mount_t *mp = ip->i_mount;
2550 struct xfs_perag *pag;
2551 unsigned long first_index, mask;
2552 unsigned long inodes_per_cluster;
2554 xfs_inode_t **ilist;
2561 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
2563 inodes_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog;
2564 ilist_size = inodes_per_cluster * sizeof(xfs_inode_t *);
2565 ilist = kmem_alloc(ilist_size, KM_MAYFAIL|KM_NOFS);
2569 mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
2570 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask;
2572 /* really need a gang lookup range call here */
2573 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)ilist,
2574 first_index, inodes_per_cluster);
2578 for (i = 0; i < nr_found; i++) {
2584 * because this is an RCU protected lookup, we could find a
2585 * recently freed or even reallocated inode during the lookup.
2586 * We need to check under the i_flags_lock for a valid inode
2587 * here. Skip it if it is not valid or the wrong inode.
2589 spin_lock(&ip->i_flags_lock);
2591 (XFS_INO_TO_AGINO(mp, iq->i_ino) & mask) != first_index) {
2592 spin_unlock(&ip->i_flags_lock);
2595 spin_unlock(&ip->i_flags_lock);
2598 * Do an un-protected check to see if the inode is dirty and
2599 * is a candidate for flushing. These checks will be repeated
2600 * later after the appropriate locks are acquired.
2602 if (xfs_inode_clean(iq) && xfs_ipincount(iq) == 0)
2606 * Try to get locks. If any are unavailable or it is pinned,
2607 * then this inode cannot be flushed and is skipped.
2610 if (!xfs_ilock_nowait(iq, XFS_ILOCK_SHARED))
2612 if (!xfs_iflock_nowait(iq)) {
2613 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2616 if (xfs_ipincount(iq)) {
2618 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2623 * arriving here means that this inode can be flushed. First
2624 * re-check that it's dirty before flushing.
2626 if (!xfs_inode_clean(iq)) {
2628 error = xfs_iflush_int(iq, bp);
2630 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2631 goto cluster_corrupt_out;
2637 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2641 XFS_STATS_INC(xs_icluster_flushcnt);
2642 XFS_STATS_ADD(xs_icluster_flushinode, clcount);
2653 cluster_corrupt_out:
2655 * Corruption detected in the clustering loop. Invalidate the
2656 * inode buffer and shut down the filesystem.
2660 * Clean up the buffer. If it was delwri, just release it --
2661 * brelse can handle it with no problems. If not, shut down the
2662 * filesystem before releasing the buffer.
2664 bufwasdelwri = (bp->b_flags & _XBF_DELWRI_Q);
2668 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
2670 if (!bufwasdelwri) {
2672 * Just like incore_relse: if we have b_iodone functions,
2673 * mark the buffer as an error and call them. Otherwise
2674 * mark it as stale and brelse.
2679 xfs_buf_ioerror(bp, EIO);
2680 xfs_buf_ioend(bp, 0);
2688 * Unlocks the flush lock
2690 xfs_iflush_abort(iq, false);
2693 return XFS_ERROR(EFSCORRUPTED);
2697 * Flush dirty inode metadata into the backing buffer.
2699 * The caller must have the inode lock and the inode flush lock held. The
2700 * inode lock will still be held upon return to the caller, and the inode
2701 * flush lock will be released after the inode has reached the disk.
2703 * The caller must write out the buffer returned in *bpp and release it.
2707 struct xfs_inode *ip,
2708 struct xfs_buf **bpp)
2710 struct xfs_mount *mp = ip->i_mount;
2712 struct xfs_dinode *dip;
2715 XFS_STATS_INC(xs_iflush_count);
2717 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
2718 ASSERT(xfs_isiflocked(ip));
2719 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
2720 ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
2724 xfs_iunpin_wait(ip);
2727 * For stale inodes we cannot rely on the backing buffer remaining
2728 * stale in cache for the remaining life of the stale inode and so
2729 * xfs_imap_to_bp() below may give us a buffer that no longer contains
2730 * inodes below. We have to check this after ensuring the inode is
2731 * unpinned so that it is safe to reclaim the stale inode after the
2734 if (xfs_iflags_test(ip, XFS_ISTALE)) {
2740 * This may have been unpinned because the filesystem is shutting
2741 * down forcibly. If that's the case we must not write this inode
2742 * to disk, because the log record didn't make it to disk.
2744 * We also have to remove the log item from the AIL in this case,
2745 * as we wait for an empty AIL as part of the unmount process.
2747 if (XFS_FORCED_SHUTDOWN(mp)) {
2748 error = XFS_ERROR(EIO);
2753 * Get the buffer containing the on-disk inode.
2755 error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &bp, XBF_TRYLOCK,
2763 * First flush out the inode that xfs_iflush was called with.
2765 error = xfs_iflush_int(ip, bp);
2770 * If the buffer is pinned then push on the log now so we won't
2771 * get stuck waiting in the write for too long.
2773 if (xfs_buf_ispinned(bp))
2774 xfs_log_force(mp, 0);
2778 * see if other inodes can be gathered into this write
2780 error = xfs_iflush_cluster(ip, bp);
2782 goto cluster_corrupt_out;
2789 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
2790 cluster_corrupt_out:
2791 error = XFS_ERROR(EFSCORRUPTED);
2794 * Unlocks the flush lock
2796 xfs_iflush_abort(ip, false);
2803 struct xfs_inode *ip,
2806 struct xfs_inode_log_item *iip = ip->i_itemp;
2807 struct xfs_dinode *dip;
2808 struct xfs_mount *mp = ip->i_mount;
2810 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
2811 ASSERT(xfs_isiflocked(ip));
2812 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
2813 ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
2814 ASSERT(iip != NULL && iip->ili_fields != 0);
2816 /* set *dip = inode's place in the buffer */
2817 dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
2819 if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
2820 mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) {
2821 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2822 "%s: Bad inode %Lu magic number 0x%x, ptr 0x%p",
2823 __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
2826 if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC,
2827 mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) {
2828 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2829 "%s: Bad inode %Lu, ptr 0x%p, magic number 0x%x",
2830 __func__, ip->i_ino, ip, ip->i_d.di_magic);
2833 if (S_ISREG(ip->i_d.di_mode)) {
2835 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
2836 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE),
2837 mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) {
2838 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2839 "%s: Bad regular inode %Lu, ptr 0x%p",
2840 __func__, ip->i_ino, ip);
2843 } else if (S_ISDIR(ip->i_d.di_mode)) {
2845 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
2846 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
2847 (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL),
2848 mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) {
2849 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2850 "%s: Bad directory inode %Lu, ptr 0x%p",
2851 __func__, ip->i_ino, ip);
2855 if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents >
2856 ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5,
2857 XFS_RANDOM_IFLUSH_5)) {
2858 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2859 "%s: detected corrupt incore inode %Lu, "
2860 "total extents = %d, nblocks = %Ld, ptr 0x%p",
2861 __func__, ip->i_ino,
2862 ip->i_d.di_nextents + ip->i_d.di_anextents,
2863 ip->i_d.di_nblocks, ip);
2866 if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
2867 mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) {
2868 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2869 "%s: bad inode %Lu, forkoff 0x%x, ptr 0x%p",
2870 __func__, ip->i_ino, ip->i_d.di_forkoff, ip);
2874 * bump the flush iteration count, used to detect flushes which
2875 * postdate a log record during recovery. This is redundant as we now
2876 * log every change and hence this can't happen. Still, it doesn't hurt.
2878 ip->i_d.di_flushiter++;
2881 * Copy the dirty parts of the inode into the on-disk
2882 * inode. We always copy out the core of the inode,
2883 * because if the inode is dirty at all the core must
2886 xfs_dinode_to_disk(dip, &ip->i_d);
2888 /* Wrap, we never let the log put out DI_MAX_FLUSH */
2889 if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
2890 ip->i_d.di_flushiter = 0;
2893 * If this is really an old format inode and the superblock version
2894 * has not been updated to support only new format inodes, then
2895 * convert back to the old inode format. If the superblock version
2896 * has been updated, then make the conversion permanent.
2898 ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb));
2899 if (ip->i_d.di_version == 1) {
2900 if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
2904 ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
2905 dip->di_onlink = cpu_to_be16(ip->i_d.di_nlink);
2908 * The superblock version has already been bumped,
2909 * so just make the conversion to the new inode
2912 ip->i_d.di_version = 2;
2913 dip->di_version = 2;
2914 ip->i_d.di_onlink = 0;
2916 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
2917 memset(&(dip->di_pad[0]), 0,
2918 sizeof(dip->di_pad));
2919 ASSERT(xfs_get_projid(ip) == 0);
2923 xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK, bp);
2924 if (XFS_IFORK_Q(ip))
2925 xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK, bp);
2926 xfs_inobp_check(mp, bp);
2929 * We've recorded everything logged in the inode, so we'd like to clear
2930 * the ili_fields bits so we don't log and flush things unnecessarily.
2931 * However, we can't stop logging all this information until the data
2932 * we've copied into the disk buffer is written to disk. If we did we
2933 * might overwrite the copy of the inode in the log with all the data
2934 * after re-logging only part of it, and in the face of a crash we
2935 * wouldn't have all the data we need to recover.
2937 * What we do is move the bits to the ili_last_fields field. When
2938 * logging the inode, these bits are moved back to the ili_fields field.
2939 * In the xfs_iflush_done() routine we clear ili_last_fields, since we
2940 * know that the information those bits represent is permanently on
2941 * disk. As long as the flush completes before the inode is logged
2942 * again, then both ili_fields and ili_last_fields will be cleared.
2944 * We can play with the ili_fields bits here, because the inode lock
2945 * must be held exclusively in order to set bits there and the flush
2946 * lock protects the ili_last_fields bits. Set ili_logged so the flush
2947 * done routine can tell whether or not to look in the AIL. Also, store
2948 * the current LSN of the inode so that we can tell whether the item has
2949 * moved in the AIL from xfs_iflush_done(). In order to read the lsn we
2950 * need the AIL lock, because it is a 64 bit value that cannot be read
2953 iip->ili_last_fields = iip->ili_fields;
2954 iip->ili_fields = 0;
2955 iip->ili_logged = 1;
2957 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
2958 &iip->ili_item.li_lsn);
2961 * Attach the function xfs_iflush_done to the inode's
2962 * buffer. This will remove the inode from the AIL
2963 * and unlock the inode's flush lock when the inode is
2964 * completely written to disk.
2966 xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item);
2968 /* update the lsn in the on disk inode if required */
2969 if (ip->i_d.di_version == 3)
2970 dip->di_lsn = cpu_to_be64(iip->ili_item.li_lsn);
2972 /* generate the checksum. */
2973 xfs_dinode_calc_crc(mp, dip);
2975 ASSERT(bp->b_fspriv != NULL);
2976 ASSERT(bp->b_iodone != NULL);
2980 return XFS_ERROR(EFSCORRUPTED);
2984 * Return a pointer to the extent record at file index idx.
2986 xfs_bmbt_rec_host_t *
2988 xfs_ifork_t *ifp, /* inode fork pointer */
2989 xfs_extnum_t idx) /* index of target extent */
2992 ASSERT(idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
2994 if ((ifp->if_flags & XFS_IFEXTIREC) && (idx == 0)) {
2995 return ifp->if_u1.if_ext_irec->er_extbuf;
2996 } else if (ifp->if_flags & XFS_IFEXTIREC) {
2997 xfs_ext_irec_t *erp; /* irec pointer */
2998 int erp_idx = 0; /* irec index */
2999 xfs_extnum_t page_idx = idx; /* ext index in target list */
3001 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
3002 return &erp->er_extbuf[page_idx];
3003 } else if (ifp->if_bytes) {
3004 return &ifp->if_u1.if_extents[idx];
3011 * Insert new item(s) into the extent records for incore inode
3012 * fork 'ifp'. 'count' new items are inserted at index 'idx'.
3016 xfs_inode_t *ip, /* incore inode pointer */
3017 xfs_extnum_t idx, /* starting index of new items */
3018 xfs_extnum_t count, /* number of inserted items */
3019 xfs_bmbt_irec_t *new, /* items to insert */
3020 int state) /* type of extent conversion */
3022 xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
3023 xfs_extnum_t i; /* extent record index */
3025 trace_xfs_iext_insert(ip, idx, new, state, _RET_IP_);
3027 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
3028 xfs_iext_add(ifp, idx, count);
3029 for (i = idx; i < idx + count; i++, new++)
3030 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, i), new);
3034 * This is called when the amount of space required for incore file
3035 * extents needs to be increased. The ext_diff parameter stores the
3036 * number of new extents being added and the idx parameter contains
3037 * the extent index where the new extents will be added. If the new
3038 * extents are being appended, then we just need to (re)allocate and
3039 * initialize the space. Otherwise, if the new extents are being
3040 * inserted into the middle of the existing entries, a bit more work
3041 * is required to make room for the new extents to be inserted. The
3042 * caller is responsible for filling in the new extent entries upon
3047 xfs_ifork_t *ifp, /* inode fork pointer */
3048 xfs_extnum_t idx, /* index to begin adding exts */
3049 int ext_diff) /* number of extents to add */
3051 int byte_diff; /* new bytes being added */
3052 int new_size; /* size of extents after adding */
3053 xfs_extnum_t nextents; /* number of extents in file */
3055 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3056 ASSERT((idx >= 0) && (idx <= nextents));
3057 byte_diff = ext_diff * sizeof(xfs_bmbt_rec_t);
3058 new_size = ifp->if_bytes + byte_diff;
3060 * If the new number of extents (nextents + ext_diff)
3061 * fits inside the inode, then continue to use the inline
3064 if (nextents + ext_diff <= XFS_INLINE_EXTS) {
3065 if (idx < nextents) {
3066 memmove(&ifp->if_u2.if_inline_ext[idx + ext_diff],
3067 &ifp->if_u2.if_inline_ext[idx],
3068 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
3069 memset(&ifp->if_u2.if_inline_ext[idx], 0, byte_diff);
3071 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
3072 ifp->if_real_bytes = 0;
3075 * Otherwise use a linear (direct) extent list.
3076 * If the extents are currently inside the inode,
3077 * xfs_iext_realloc_direct will switch us from
3078 * inline to direct extent allocation mode.
3080 else if (nextents + ext_diff <= XFS_LINEAR_EXTS) {
3081 xfs_iext_realloc_direct(ifp, new_size);
3082 if (idx < nextents) {
3083 memmove(&ifp->if_u1.if_extents[idx + ext_diff],
3084 &ifp->if_u1.if_extents[idx],
3085 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
3086 memset(&ifp->if_u1.if_extents[idx], 0, byte_diff);
3089 /* Indirection array */
3091 xfs_ext_irec_t *erp;
3095 ASSERT(nextents + ext_diff > XFS_LINEAR_EXTS);
3096 if (ifp->if_flags & XFS_IFEXTIREC) {
3097 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 1);
3099 xfs_iext_irec_init(ifp);
3100 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3101 erp = ifp->if_u1.if_ext_irec;
3103 /* Extents fit in target extent page */
3104 if (erp && erp->er_extcount + ext_diff <= XFS_LINEAR_EXTS) {
3105 if (page_idx < erp->er_extcount) {
3106 memmove(&erp->er_extbuf[page_idx + ext_diff],
3107 &erp->er_extbuf[page_idx],
3108 (erp->er_extcount - page_idx) *
3109 sizeof(xfs_bmbt_rec_t));
3110 memset(&erp->er_extbuf[page_idx], 0, byte_diff);
3112 erp->er_extcount += ext_diff;
3113 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
3115 /* Insert a new extent page */
3117 xfs_iext_add_indirect_multi(ifp,
3118 erp_idx, page_idx, ext_diff);
3121 * If extent(s) are being appended to the last page in
3122 * the indirection array and the new extent(s) don't fit
3123 * in the page, then erp is NULL and erp_idx is set to
3124 * the next index needed in the indirection array.
3127 int count = ext_diff;
3130 erp = xfs_iext_irec_new(ifp, erp_idx);
3131 erp->er_extcount = count;
3132 count -= MIN(count, (int)XFS_LINEAR_EXTS);
3139 ifp->if_bytes = new_size;
3143 * This is called when incore extents are being added to the indirection
3144 * array and the new extents do not fit in the target extent list. The
3145 * erp_idx parameter contains the irec index for the target extent list
3146 * in the indirection array, and the idx parameter contains the extent
3147 * index within the list. The number of extents being added is stored
3148 * in the count parameter.
3150 * |-------| |-------|
3151 * | | | | idx - number of extents before idx
3153 * | | | | count - number of extents being inserted at idx
3154 * |-------| |-------|
3155 * | count | | nex2 | nex2 - number of extents after idx + count
3156 * |-------| |-------|
3159 xfs_iext_add_indirect_multi(
3160 xfs_ifork_t *ifp, /* inode fork pointer */
3161 int erp_idx, /* target extent irec index */
3162 xfs_extnum_t idx, /* index within target list */
3163 int count) /* new extents being added */
3165 int byte_diff; /* new bytes being added */
3166 xfs_ext_irec_t *erp; /* pointer to irec entry */
3167 xfs_extnum_t ext_diff; /* number of extents to add */
3168 xfs_extnum_t ext_cnt; /* new extents still needed */
3169 xfs_extnum_t nex2; /* extents after idx + count */
3170 xfs_bmbt_rec_t *nex2_ep = NULL; /* temp list for nex2 extents */
3171 int nlists; /* number of irec's (lists) */
3173 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3174 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3175 nex2 = erp->er_extcount - idx;
3176 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3179 * Save second part of target extent list
3180 * (all extents past */
3182 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
3183 nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_NOFS);
3184 memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff);
3185 erp->er_extcount -= nex2;
3186 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2);
3187 memset(&erp->er_extbuf[idx], 0, byte_diff);
3191 * Add the new extents to the end of the target
3192 * list, then allocate new irec record(s) and
3193 * extent buffer(s) as needed to store the rest
3194 * of the new extents.
3197 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount);
3199 erp->er_extcount += ext_diff;
3200 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
3201 ext_cnt -= ext_diff;
3205 erp = xfs_iext_irec_new(ifp, erp_idx);
3206 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS);
3207 erp->er_extcount = ext_diff;
3208 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
3209 ext_cnt -= ext_diff;
3212 /* Add nex2 extents back to indirection array */
3214 xfs_extnum_t ext_avail;
3217 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
3218 ext_avail = XFS_LINEAR_EXTS - erp->er_extcount;
3221 * If nex2 extents fit in the current page, append
3222 * nex2_ep after the new extents.
3224 if (nex2 <= ext_avail) {
3225 i = erp->er_extcount;
3228 * Otherwise, check if space is available in the
3231 else if ((erp_idx < nlists - 1) &&
3232 (nex2 <= (ext_avail = XFS_LINEAR_EXTS -
3233 ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) {
3236 /* Create a hole for nex2 extents */
3237 memmove(&erp->er_extbuf[nex2], erp->er_extbuf,
3238 erp->er_extcount * sizeof(xfs_bmbt_rec_t));
3241 * Final choice, create a new extent page for
3246 erp = xfs_iext_irec_new(ifp, erp_idx);
3248 memmove(&erp->er_extbuf[i], nex2_ep, byte_diff);
3250 erp->er_extcount += nex2;
3251 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2);
3256 * This is called when the amount of space required for incore file
3257 * extents needs to be decreased. The ext_diff parameter stores the
3258 * number of extents to be removed and the idx parameter contains
3259 * the extent index where the extents will be removed from.
3261 * If the amount of space needed has decreased below the linear
3262 * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
3263 * extent array. Otherwise, use kmem_realloc() to adjust the
3264 * size to what is needed.
3268 xfs_inode_t *ip, /* incore inode pointer */
3269 xfs_extnum_t idx, /* index to begin removing exts */
3270 int ext_diff, /* number of extents to remove */
3271 int state) /* type of extent conversion */
3273 xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
3274 xfs_extnum_t nextents; /* number of extents in file */
3275 int new_size; /* size of extents after removal */
3277 trace_xfs_iext_remove(ip, idx, state, _RET_IP_);
3279 ASSERT(ext_diff > 0);
3280 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3281 new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t);
3283 if (new_size == 0) {
3284 xfs_iext_destroy(ifp);
3285 } else if (ifp->if_flags & XFS_IFEXTIREC) {
3286 xfs_iext_remove_indirect(ifp, idx, ext_diff);
3287 } else if (ifp->if_real_bytes) {
3288 xfs_iext_remove_direct(ifp, idx, ext_diff);
3290 xfs_iext_remove_inline(ifp, idx, ext_diff);
3292 ifp->if_bytes = new_size;
3296 * This removes ext_diff extents from the inline buffer, beginning
3297 * at extent index idx.
3300 xfs_iext_remove_inline(
3301 xfs_ifork_t *ifp, /* inode fork pointer */
3302 xfs_extnum_t idx, /* index to begin removing exts */
3303 int ext_diff) /* number of extents to remove */
3305 int nextents; /* number of extents in file */
3307 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
3308 ASSERT(idx < XFS_INLINE_EXTS);
3309 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3310 ASSERT(((nextents - ext_diff) > 0) &&
3311 (nextents - ext_diff) < XFS_INLINE_EXTS);
3313 if (idx + ext_diff < nextents) {
3314 memmove(&ifp->if_u2.if_inline_ext[idx],
3315 &ifp->if_u2.if_inline_ext[idx + ext_diff],
3316 (nextents - (idx + ext_diff)) *
3317 sizeof(xfs_bmbt_rec_t));
3318 memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff],
3319 0, ext_diff * sizeof(xfs_bmbt_rec_t));
3321 memset(&ifp->if_u2.if_inline_ext[idx], 0,
3322 ext_diff * sizeof(xfs_bmbt_rec_t));
3327 * This removes ext_diff extents from a linear (direct) extent list,
3328 * beginning at extent index idx. If the extents are being removed
3329 * from the end of the list (ie. truncate) then we just need to re-
3330 * allocate the list to remove the extra space. Otherwise, if the
3331 * extents are being removed from the middle of the existing extent
3332 * entries, then we first need to move the extent records beginning
3333 * at idx + ext_diff up in the list to overwrite the records being
3334 * removed, then remove the extra space via kmem_realloc.
3337 xfs_iext_remove_direct(
3338 xfs_ifork_t *ifp, /* inode fork pointer */
3339 xfs_extnum_t idx, /* index to begin removing exts */
3340 int ext_diff) /* number of extents to remove */
3342 xfs_extnum_t nextents; /* number of extents in file */
3343 int new_size; /* size of extents after removal */
3345 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
3346 new_size = ifp->if_bytes -
3347 (ext_diff * sizeof(xfs_bmbt_rec_t));
3348 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3350 if (new_size == 0) {
3351 xfs_iext_destroy(ifp);
3354 /* Move extents up in the list (if needed) */
3355 if (idx + ext_diff < nextents) {
3356 memmove(&ifp->if_u1.if_extents[idx],
3357 &ifp->if_u1.if_extents[idx + ext_diff],
3358 (nextents - (idx + ext_diff)) *
3359 sizeof(xfs_bmbt_rec_t));
3361 memset(&ifp->if_u1.if_extents[nextents - ext_diff],
3362 0, ext_diff * sizeof(xfs_bmbt_rec_t));
3364 * Reallocate the direct extent list. If the extents
3365 * will fit inside the inode then xfs_iext_realloc_direct
3366 * will switch from direct to inline extent allocation
3369 xfs_iext_realloc_direct(ifp, new_size);
3370 ifp->if_bytes = new_size;
3374 * This is called when incore extents are being removed from the
3375 * indirection array and the extents being removed span multiple extent
3376 * buffers. The idx parameter contains the file extent index where we
3377 * want to begin removing extents, and the count parameter contains
3378 * how many extents need to be removed.
3380 * |-------| |-------|
3381 * | nex1 | | | nex1 - number of extents before idx
3382 * |-------| | count |
3383 * | | | | count - number of extents being removed at idx
3384 * | count | |-------|
3385 * | | | nex2 | nex2 - number of extents after idx + count
3386 * |-------| |-------|
3389 xfs_iext_remove_indirect(
3390 xfs_ifork_t *ifp, /* inode fork pointer */
3391 xfs_extnum_t idx, /* index to begin removing extents */
3392 int count) /* number of extents to remove */
3394 xfs_ext_irec_t *erp; /* indirection array pointer */
3395 int erp_idx = 0; /* indirection array index */
3396 xfs_extnum_t ext_cnt; /* extents left to remove */
3397 xfs_extnum_t ext_diff; /* extents to remove in current list */
3398 xfs_extnum_t nex1; /* number of extents before idx */
3399 xfs_extnum_t nex2; /* extents after idx + count */
3400 int page_idx = idx; /* index in target extent list */
3402 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3403 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
3404 ASSERT(erp != NULL);
3408 nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0);
3409 ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1));
3411 * Check for deletion of entire list;
3412 * xfs_iext_irec_remove() updates extent offsets.
3414 if (ext_diff == erp->er_extcount) {
3415 xfs_iext_irec_remove(ifp, erp_idx);
3416 ext_cnt -= ext_diff;
3419 ASSERT(erp_idx < ifp->if_real_bytes /
3421 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3428 /* Move extents up (if needed) */
3430 memmove(&erp->er_extbuf[nex1],
3431 &erp->er_extbuf[nex1 + ext_diff],
3432 nex2 * sizeof(xfs_bmbt_rec_t));
3434 /* Zero out rest of page */
3435 memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ -
3436 ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t))));
3437 /* Update remaining counters */
3438 erp->er_extcount -= ext_diff;
3439 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff);
3440 ext_cnt -= ext_diff;
3445 ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t);
3446 xfs_iext_irec_compact(ifp);
3450 * Create, destroy, or resize a linear (direct) block of extents.
3453 xfs_iext_realloc_direct(
3454 xfs_ifork_t *ifp, /* inode fork pointer */
3455 int new_size) /* new size of extents */
3457 int rnew_size; /* real new size of extents */
3459 rnew_size = new_size;
3461 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) ||
3462 ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) &&
3463 (new_size != ifp->if_real_bytes)));
3465 /* Free extent records */
3466 if (new_size == 0) {
3467 xfs_iext_destroy(ifp);
3469 /* Resize direct extent list and zero any new bytes */
3470 else if (ifp->if_real_bytes) {
3471 /* Check if extents will fit inside the inode */
3472 if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) {
3473 xfs_iext_direct_to_inline(ifp, new_size /
3474 (uint)sizeof(xfs_bmbt_rec_t));
3475 ifp->if_bytes = new_size;
3478 if (!is_power_of_2(new_size)){
3479 rnew_size = roundup_pow_of_two(new_size);
3481 if (rnew_size != ifp->if_real_bytes) {
3482 ifp->if_u1.if_extents =
3483 kmem_realloc(ifp->if_u1.if_extents,
3485 ifp->if_real_bytes, KM_NOFS);
3487 if (rnew_size > ifp->if_real_bytes) {
3488 memset(&ifp->if_u1.if_extents[ifp->if_bytes /
3489 (uint)sizeof(xfs_bmbt_rec_t)], 0,
3490 rnew_size - ifp->if_real_bytes);
3494 * Switch from the inline extent buffer to a direct
3495 * extent list. Be sure to include the inline extent
3496 * bytes in new_size.
3499 new_size += ifp->if_bytes;
3500 if (!is_power_of_2(new_size)) {
3501 rnew_size = roundup_pow_of_two(new_size);
3503 xfs_iext_inline_to_direct(ifp, rnew_size);
3505 ifp->if_real_bytes = rnew_size;
3506 ifp->if_bytes = new_size;
3510 * Switch from linear (direct) extent records to inline buffer.
3513 xfs_iext_direct_to_inline(
3514 xfs_ifork_t *ifp, /* inode fork pointer */
3515 xfs_extnum_t nextents) /* number of extents in file */
3517 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
3518 ASSERT(nextents <= XFS_INLINE_EXTS);
3520 * The inline buffer was zeroed when we switched
3521 * from inline to direct extent allocation mode,
3522 * so we don't need to clear it here.
3524 memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents,
3525 nextents * sizeof(xfs_bmbt_rec_t));
3526 kmem_free(ifp->if_u1.if_extents);
3527 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
3528 ifp->if_real_bytes = 0;
3532 * Switch from inline buffer to linear (direct) extent records.
3533 * new_size should already be rounded up to the next power of 2
3534 * by the caller (when appropriate), so use new_size as it is.
3535 * However, since new_size may be rounded up, we can't update
3536 * if_bytes here. It is the caller's responsibility to update
3537 * if_bytes upon return.
3540 xfs_iext_inline_to_direct(
3541 xfs_ifork_t *ifp, /* inode fork pointer */
3542 int new_size) /* number of extents in file */
3544 ifp->if_u1.if_extents = kmem_alloc(new_size, KM_NOFS);
3545 memset(ifp->if_u1.if_extents, 0, new_size);
3546 if (ifp->if_bytes) {
3547 memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext,
3549 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
3550 sizeof(xfs_bmbt_rec_t));
3552 ifp->if_real_bytes = new_size;
3556 * Resize an extent indirection array to new_size bytes.
3559 xfs_iext_realloc_indirect(
3560 xfs_ifork_t *ifp, /* inode fork pointer */
3561 int new_size) /* new indirection array size */
3563 int nlists; /* number of irec's (ex lists) */
3564 int size; /* current indirection array size */
3566 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3567 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3568 size = nlists * sizeof(xfs_ext_irec_t);
3569 ASSERT(ifp->if_real_bytes);
3570 ASSERT((new_size >= 0) && (new_size != size));
3571 if (new_size == 0) {
3572 xfs_iext_destroy(ifp);
3574 ifp->if_u1.if_ext_irec = (xfs_ext_irec_t *)
3575 kmem_realloc(ifp->if_u1.if_ext_irec,
3576 new_size, size, KM_NOFS);
3581 * Switch from indirection array to linear (direct) extent allocations.
3584 xfs_iext_indirect_to_direct(
3585 xfs_ifork_t *ifp) /* inode fork pointer */
3587 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3588 xfs_extnum_t nextents; /* number of extents in file */
3589 int size; /* size of file extents */
3591 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3592 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3593 ASSERT(nextents <= XFS_LINEAR_EXTS);
3594 size = nextents * sizeof(xfs_bmbt_rec_t);
3596 xfs_iext_irec_compact_pages(ifp);
3597 ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ);
3599 ep = ifp->if_u1.if_ext_irec->er_extbuf;
3600 kmem_free(ifp->if_u1.if_ext_irec);
3601 ifp->if_flags &= ~XFS_IFEXTIREC;
3602 ifp->if_u1.if_extents = ep;
3603 ifp->if_bytes = size;
3604 if (nextents < XFS_LINEAR_EXTS) {
3605 xfs_iext_realloc_direct(ifp, size);
3610 * Free incore file extents.
3614 xfs_ifork_t *ifp) /* inode fork pointer */
3616 if (ifp->if_flags & XFS_IFEXTIREC) {
3620 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3621 for (erp_idx = nlists - 1; erp_idx >= 0 ; erp_idx--) {
3622 xfs_iext_irec_remove(ifp, erp_idx);
3624 ifp->if_flags &= ~XFS_IFEXTIREC;
3625 } else if (ifp->if_real_bytes) {
3626 kmem_free(ifp->if_u1.if_extents);
3627 } else if (ifp->if_bytes) {
3628 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
3629 sizeof(xfs_bmbt_rec_t));
3631 ifp->if_u1.if_extents = NULL;
3632 ifp->if_real_bytes = 0;
3637 * Return a pointer to the extent record for file system block bno.
3639 xfs_bmbt_rec_host_t * /* pointer to found extent record */
3640 xfs_iext_bno_to_ext(
3641 xfs_ifork_t *ifp, /* inode fork pointer */
3642 xfs_fileoff_t bno, /* block number to search for */
3643 xfs_extnum_t *idxp) /* index of target extent */
3645 xfs_bmbt_rec_host_t *base; /* pointer to first extent */
3646 xfs_filblks_t blockcount = 0; /* number of blocks in extent */
3647 xfs_bmbt_rec_host_t *ep = NULL; /* pointer to target extent */
3648 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
3649 int high; /* upper boundary in search */
3650 xfs_extnum_t idx = 0; /* index of target extent */
3651 int low; /* lower boundary in search */
3652 xfs_extnum_t nextents; /* number of file extents */
3653 xfs_fileoff_t startoff = 0; /* start offset of extent */
3655 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3656 if (nextents == 0) {
3661 if (ifp->if_flags & XFS_IFEXTIREC) {
3662 /* Find target extent list */
3664 erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx);
3665 base = erp->er_extbuf;
3666 high = erp->er_extcount - 1;
3668 base = ifp->if_u1.if_extents;
3669 high = nextents - 1;
3671 /* Binary search extent records */
3672 while (low <= high) {
3673 idx = (low + high) >> 1;
3675 startoff = xfs_bmbt_get_startoff(ep);
3676 blockcount = xfs_bmbt_get_blockcount(ep);
3677 if (bno < startoff) {
3679 } else if (bno >= startoff + blockcount) {
3682 /* Convert back to file-based extent index */
3683 if (ifp->if_flags & XFS_IFEXTIREC) {
3684 idx += erp->er_extoff;
3690 /* Convert back to file-based extent index */
3691 if (ifp->if_flags & XFS_IFEXTIREC) {
3692 idx += erp->er_extoff;
3694 if (bno >= startoff + blockcount) {
3695 if (++idx == nextents) {
3698 ep = xfs_iext_get_ext(ifp, idx);
3706 * Return a pointer to the indirection array entry containing the
3707 * extent record for filesystem block bno. Store the index of the
3708 * target irec in *erp_idxp.
3710 xfs_ext_irec_t * /* pointer to found extent record */
3711 xfs_iext_bno_to_irec(
3712 xfs_ifork_t *ifp, /* inode fork pointer */
3713 xfs_fileoff_t bno, /* block number to search for */
3714 int *erp_idxp) /* irec index of target ext list */
3716 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
3717 xfs_ext_irec_t *erp_next; /* next indirection array entry */
3718 int erp_idx; /* indirection array index */
3719 int nlists; /* number of extent irec's (lists) */
3720 int high; /* binary search upper limit */
3721 int low; /* binary search lower limit */
3723 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3724 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3728 while (low <= high) {
3729 erp_idx = (low + high) >> 1;
3730 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3731 erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL;
3732 if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) {
3734 } else if (erp_next && bno >=
3735 xfs_bmbt_get_startoff(erp_next->er_extbuf)) {
3741 *erp_idxp = erp_idx;
3746 * Return a pointer to the indirection array entry containing the
3747 * extent record at file extent index *idxp. Store the index of the
3748 * target irec in *erp_idxp and store the page index of the target
3749 * extent record in *idxp.
3752 xfs_iext_idx_to_irec(
3753 xfs_ifork_t *ifp, /* inode fork pointer */
3754 xfs_extnum_t *idxp, /* extent index (file -> page) */
3755 int *erp_idxp, /* pointer to target irec */
3756 int realloc) /* new bytes were just added */
3758 xfs_ext_irec_t *prev; /* pointer to previous irec */
3759 xfs_ext_irec_t *erp = NULL; /* pointer to current irec */
3760 int erp_idx; /* indirection array index */
3761 int nlists; /* number of irec's (ex lists) */
3762 int high; /* binary search upper limit */
3763 int low; /* binary search lower limit */
3764 xfs_extnum_t page_idx = *idxp; /* extent index in target list */
3766 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3767 ASSERT(page_idx >= 0);
3768 ASSERT(page_idx <= ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
3769 ASSERT(page_idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t) || realloc);
3771 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3776 /* Binary search extent irec's */
3777 while (low <= high) {
3778 erp_idx = (low + high) >> 1;
3779 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3780 prev = erp_idx > 0 ? erp - 1 : NULL;
3781 if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff &&
3782 realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) {
3784 } else if (page_idx > erp->er_extoff + erp->er_extcount ||
3785 (page_idx == erp->er_extoff + erp->er_extcount &&
3788 } else if (page_idx == erp->er_extoff + erp->er_extcount &&
3789 erp->er_extcount == XFS_LINEAR_EXTS) {
3793 erp = erp_idx < nlists ? erp + 1 : NULL;
3796 page_idx -= erp->er_extoff;
3801 *erp_idxp = erp_idx;
3806 * Allocate and initialize an indirection array once the space needed
3807 * for incore extents increases above XFS_IEXT_BUFSZ.
3811 xfs_ifork_t *ifp) /* inode fork pointer */
3813 xfs_ext_irec_t *erp; /* indirection array pointer */
3814 xfs_extnum_t nextents; /* number of extents in file */
3816 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
3817 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3818 ASSERT(nextents <= XFS_LINEAR_EXTS);
3820 erp = kmem_alloc(sizeof(xfs_ext_irec_t), KM_NOFS);
3822 if (nextents == 0) {
3823 ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
3824 } else if (!ifp->if_real_bytes) {
3825 xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ);
3826 } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) {
3827 xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ);
3829 erp->er_extbuf = ifp->if_u1.if_extents;
3830 erp->er_extcount = nextents;
3833 ifp->if_flags |= XFS_IFEXTIREC;
3834 ifp->if_real_bytes = XFS_IEXT_BUFSZ;
3835 ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t);
3836 ifp->if_u1.if_ext_irec = erp;
3842 * Allocate and initialize a new entry in the indirection array.
3846 xfs_ifork_t *ifp, /* inode fork pointer */
3847 int erp_idx) /* index for new irec */
3849 xfs_ext_irec_t *erp; /* indirection array pointer */
3850 int i; /* loop counter */
3851 int nlists; /* number of irec's (ex lists) */
3853 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3854 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3856 /* Resize indirection array */
3857 xfs_iext_realloc_indirect(ifp, ++nlists *
3858 sizeof(xfs_ext_irec_t));
3860 * Move records down in the array so the
3861 * new page can use erp_idx.
3863 erp = ifp->if_u1.if_ext_irec;
3864 for (i = nlists - 1; i > erp_idx; i--) {
3865 memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t));
3867 ASSERT(i == erp_idx);
3869 /* Initialize new extent record */
3870 erp = ifp->if_u1.if_ext_irec;
3871 erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
3872 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
3873 memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ);
3874 erp[erp_idx].er_extcount = 0;
3875 erp[erp_idx].er_extoff = erp_idx > 0 ?
3876 erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0;
3877 return (&erp[erp_idx]);
3881 * Remove a record from the indirection array.
3884 xfs_iext_irec_remove(
3885 xfs_ifork_t *ifp, /* inode fork pointer */
3886 int erp_idx) /* irec index to remove */
3888 xfs_ext_irec_t *erp; /* indirection array pointer */
3889 int i; /* loop counter */
3890 int nlists; /* number of irec's (ex lists) */
3892 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3893 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3894 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3895 if (erp->er_extbuf) {
3896 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1,
3898 kmem_free(erp->er_extbuf);
3900 /* Compact extent records */
3901 erp = ifp->if_u1.if_ext_irec;
3902 for (i = erp_idx; i < nlists - 1; i++) {
3903 memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t));
3906 * Manually free the last extent record from the indirection
3907 * array. A call to xfs_iext_realloc_indirect() with a size
3908 * of zero would result in a call to xfs_iext_destroy() which
3909 * would in turn call this function again, creating a nasty
3913 xfs_iext_realloc_indirect(ifp,
3914 nlists * sizeof(xfs_ext_irec_t));
3916 kmem_free(ifp->if_u1.if_ext_irec);
3918 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
3922 * This is called to clean up large amounts of unused memory allocated
3923 * by the indirection array. Before compacting anything though, verify
3924 * that the indirection array is still needed and switch back to the
3925 * linear extent list (or even the inline buffer) if possible. The
3926 * compaction policy is as follows:
3928 * Full Compaction: Extents fit into a single page (or inline buffer)
3929 * Partial Compaction: Extents occupy less than 50% of allocated space
3930 * No Compaction: Extents occupy at least 50% of allocated space
3933 xfs_iext_irec_compact(
3934 xfs_ifork_t *ifp) /* inode fork pointer */
3936 xfs_extnum_t nextents; /* number of extents in file */
3937 int nlists; /* number of irec's (ex lists) */
3939 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3940 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3941 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3943 if (nextents == 0) {
3944 xfs_iext_destroy(ifp);
3945 } else if (nextents <= XFS_INLINE_EXTS) {
3946 xfs_iext_indirect_to_direct(ifp);
3947 xfs_iext_direct_to_inline(ifp, nextents);
3948 } else if (nextents <= XFS_LINEAR_EXTS) {
3949 xfs_iext_indirect_to_direct(ifp);
3950 } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) {
3951 xfs_iext_irec_compact_pages(ifp);
3956 * Combine extents from neighboring extent pages.
3959 xfs_iext_irec_compact_pages(
3960 xfs_ifork_t *ifp) /* inode fork pointer */
3962 xfs_ext_irec_t *erp, *erp_next;/* pointers to irec entries */
3963 int erp_idx = 0; /* indirection array index */
3964 int nlists; /* number of irec's (ex lists) */
3966 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3967 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3968 while (erp_idx < nlists - 1) {
3969 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3971 if (erp_next->er_extcount <=
3972 (XFS_LINEAR_EXTS - erp->er_extcount)) {
3973 memcpy(&erp->er_extbuf[erp->er_extcount],
3974 erp_next->er_extbuf, erp_next->er_extcount *
3975 sizeof(xfs_bmbt_rec_t));
3976 erp->er_extcount += erp_next->er_extcount;
3978 * Free page before removing extent record
3979 * so er_extoffs don't get modified in
3980 * xfs_iext_irec_remove.
3982 kmem_free(erp_next->er_extbuf);
3983 erp_next->er_extbuf = NULL;
3984 xfs_iext_irec_remove(ifp, erp_idx + 1);
3985 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3993 * This is called to update the er_extoff field in the indirection
3994 * array when extents have been added or removed from one of the
3995 * extent lists. erp_idx contains the irec index to begin updating
3996 * at and ext_diff contains the number of extents that were added
4000 xfs_iext_irec_update_extoffs(
4001 xfs_ifork_t *ifp, /* inode fork pointer */
4002 int erp_idx, /* irec index to update */
4003 int ext_diff) /* number of new extents */
4005 int i; /* loop counter */
4006 int nlists; /* number of irec's (ex lists */
4008 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
4009 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
4010 for (i = erp_idx; i < nlists; i++) {
4011 ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff;
4016 * Test whether it is appropriate to check an inode for and free post EOF
4017 * blocks. The 'force' parameter determines whether we should also consider
4018 * regular files that are marked preallocated or append-only.
4021 xfs_can_free_eofblocks(struct xfs_inode *ip, bool force)
4023 /* prealloc/delalloc exists only on regular files */
4024 if (!S_ISREG(ip->i_d.di_mode))
4028 * Zero sized files with no cached pages and delalloc blocks will not
4029 * have speculative prealloc/delalloc blocks to remove.
4031 if (VFS_I(ip)->i_size == 0 &&
4032 VN_CACHED(VFS_I(ip)) == 0 &&
4033 ip->i_delayed_blks == 0)
4036 /* If we haven't read in the extent list, then don't do it now. */
4037 if (!(ip->i_df.if_flags & XFS_IFEXTENTS))
4041 * Do not free real preallocated or append-only files unless the file
4042 * has delalloc blocks and we are forced to remove them.
4044 if (ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND))
4045 if (!force || ip->i_delayed_blks == 0)