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_trace.h"
49 kmem_zone_t *xfs_ifork_zone;
50 kmem_zone_t *xfs_inode_zone;
53 * Used in xfs_itruncate_extents(). This is the maximum number of extents
54 * freed from a file in a single transaction.
56 #define XFS_ITRUNC_MAX_EXTENTS 2
58 STATIC int xfs_iflush_int(xfs_inode_t *, xfs_buf_t *);
59 STATIC int xfs_iformat_local(xfs_inode_t *, xfs_dinode_t *, int, int);
60 STATIC int xfs_iformat_extents(xfs_inode_t *, xfs_dinode_t *, int);
61 STATIC int xfs_iformat_btree(xfs_inode_t *, xfs_dinode_t *, int);
64 * helper function to extract extent size hint from inode
70 if ((ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE) && ip->i_d.di_extsize)
71 return ip->i_d.di_extsize;
72 if (XFS_IS_REALTIME_INODE(ip))
73 return ip->i_mount->m_sb.sb_rextsize;
79 * Make sure that the extents in the given memory buffer
89 xfs_bmbt_rec_host_t rec;
92 for (i = 0; i < nrecs; i++) {
93 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
94 rec.l0 = get_unaligned(&ep->l0);
95 rec.l1 = get_unaligned(&ep->l1);
96 xfs_bmbt_get_all(&rec, &irec);
97 if (fmt == XFS_EXTFMT_NOSTATE)
98 ASSERT(irec.br_state == XFS_EXT_NORM);
102 #define xfs_validate_extents(ifp, nrecs, fmt)
106 * Check that none of the inode's in the buffer have a next
107 * unlinked field of 0.
119 j = mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog;
121 for (i = 0; i < j; i++) {
122 dip = (xfs_dinode_t *)xfs_buf_offset(bp,
123 i * mp->m_sb.sb_inodesize);
124 if (!dip->di_next_unlinked) {
126 "Detected bogus zero next_unlinked field in incore inode buffer 0x%p.",
128 ASSERT(dip->di_next_unlinked);
135 * Find the buffer associated with the given inode map
136 * We do basic validation checks on the buffer once it has been
137 * retrieved from disk.
143 struct xfs_imap *imap,
153 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, imap->im_blkno,
154 (int)imap->im_len, buf_flags, &bp);
156 if (error != EAGAIN) {
158 "%s: xfs_trans_read_buf() returned error %d.",
161 ASSERT(buf_flags & XBF_TRYLOCK);
167 * Validate the magic number and version of every inode in the buffer
168 * (if DEBUG kernel) or the first inode in the buffer, otherwise.
171 ni = BBTOB(imap->im_len) >> mp->m_sb.sb_inodelog;
172 #else /* usual case */
176 for (i = 0; i < ni; i++) {
180 dip = (xfs_dinode_t *)xfs_buf_offset(bp,
181 (i << mp->m_sb.sb_inodelog));
182 di_ok = dip->di_magic == cpu_to_be16(XFS_DINODE_MAGIC) &&
183 XFS_DINODE_GOOD_VERSION(dip->di_version);
184 if (unlikely(XFS_TEST_ERROR(!di_ok, mp,
185 XFS_ERRTAG_ITOBP_INOTOBP,
186 XFS_RANDOM_ITOBP_INOTOBP))) {
187 if (iget_flags & XFS_IGET_UNTRUSTED) {
188 xfs_trans_brelse(tp, bp);
189 return XFS_ERROR(EINVAL);
191 XFS_CORRUPTION_ERROR("xfs_imap_to_bp",
192 XFS_ERRLEVEL_HIGH, mp, dip);
195 "bad inode magic/vsn daddr %lld #%d (magic=%x)",
196 (unsigned long long)imap->im_blkno, i,
197 be16_to_cpu(dip->di_magic));
200 xfs_trans_brelse(tp, bp);
201 return XFS_ERROR(EFSCORRUPTED);
205 xfs_inobp_check(mp, bp);
211 * This routine is called to map an inode number within a file
212 * system to the buffer containing the on-disk version of the
213 * inode. It returns a pointer to the buffer containing the
214 * on-disk inode in the bpp parameter, and in the dip parameter
215 * it returns a pointer to the on-disk inode within that buffer.
217 * If a non-zero error is returned, then the contents of bpp and
218 * dipp are undefined.
220 * Use xfs_imap() to determine the size and location of the
221 * buffer to read from disk.
233 struct xfs_imap imap;
238 error = xfs_imap(mp, tp, ino, &imap, imap_flags);
242 error = xfs_imap_to_bp(mp, tp, &imap, &bp, 0, imap_flags);
246 *dipp = (xfs_dinode_t *)xfs_buf_offset(bp, imap.im_boffset);
248 *offset = imap.im_boffset;
254 * This routine is called to map an inode to the buffer containing
255 * the on-disk version of the inode. It returns a pointer to the
256 * buffer containing the on-disk inode in the bpp parameter, and in
257 * the dip parameter it returns a pointer to the on-disk inode within
260 * If a non-zero error is returned, then the contents of bpp and
261 * dipp are undefined.
263 * The inode is expected to already been mapped to its buffer and read
264 * in once, thus we can use the mapping information stored in the inode
265 * rather than calling xfs_imap(). This allows us to avoid the overhead
266 * of looking at the inode btree for small block file systems
281 ASSERT(ip->i_imap.im_blkno != 0);
283 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &bp, buf_flags, 0);
288 ASSERT(buf_flags & XBF_TRYLOCK);
294 *dipp = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
300 * Move inode type and inode format specific information from the
301 * on-disk inode to the in-core inode. For fifos, devs, and sockets
302 * this means set if_rdev to the proper value. For files, directories,
303 * and symlinks this means to bring in the in-line data or extent
304 * pointers. For a file in B-tree format, only the root is immediately
305 * brought in-core. The rest will be in-lined in if_extents when it
306 * is first referenced (see xfs_iread_extents()).
313 xfs_attr_shortform_t *atp;
318 if (unlikely(be32_to_cpu(dip->di_nextents) +
319 be16_to_cpu(dip->di_anextents) >
320 be64_to_cpu(dip->di_nblocks))) {
321 xfs_warn(ip->i_mount,
322 "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.",
323 (unsigned long long)ip->i_ino,
324 (int)(be32_to_cpu(dip->di_nextents) +
325 be16_to_cpu(dip->di_anextents)),
327 be64_to_cpu(dip->di_nblocks));
328 XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW,
330 return XFS_ERROR(EFSCORRUPTED);
333 if (unlikely(dip->di_forkoff > ip->i_mount->m_sb.sb_inodesize)) {
334 xfs_warn(ip->i_mount, "corrupt dinode %Lu, forkoff = 0x%x.",
335 (unsigned long long)ip->i_ino,
337 XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW,
339 return XFS_ERROR(EFSCORRUPTED);
342 if (unlikely((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) &&
343 !ip->i_mount->m_rtdev_targp)) {
344 xfs_warn(ip->i_mount,
345 "corrupt dinode %Lu, has realtime flag set.",
347 XFS_CORRUPTION_ERROR("xfs_iformat(realtime)",
348 XFS_ERRLEVEL_LOW, ip->i_mount, dip);
349 return XFS_ERROR(EFSCORRUPTED);
352 switch (ip->i_d.di_mode & S_IFMT) {
357 if (unlikely(dip->di_format != XFS_DINODE_FMT_DEV)) {
358 XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW,
360 return XFS_ERROR(EFSCORRUPTED);
363 ip->i_df.if_u2.if_rdev = xfs_dinode_get_rdev(dip);
369 switch (dip->di_format) {
370 case XFS_DINODE_FMT_LOCAL:
372 * no local regular files yet
374 if (unlikely(S_ISREG(be16_to_cpu(dip->di_mode)))) {
375 xfs_warn(ip->i_mount,
376 "corrupt inode %Lu (local format for regular file).",
377 (unsigned long long) ip->i_ino);
378 XFS_CORRUPTION_ERROR("xfs_iformat(4)",
381 return XFS_ERROR(EFSCORRUPTED);
384 di_size = be64_to_cpu(dip->di_size);
385 if (unlikely(di_size > XFS_DFORK_DSIZE(dip, ip->i_mount))) {
386 xfs_warn(ip->i_mount,
387 "corrupt inode %Lu (bad size %Ld for local inode).",
388 (unsigned long long) ip->i_ino,
389 (long long) di_size);
390 XFS_CORRUPTION_ERROR("xfs_iformat(5)",
393 return XFS_ERROR(EFSCORRUPTED);
397 error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size);
399 case XFS_DINODE_FMT_EXTENTS:
400 error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
402 case XFS_DINODE_FMT_BTREE:
403 error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
406 XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW,
408 return XFS_ERROR(EFSCORRUPTED);
413 XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW, ip->i_mount);
414 return XFS_ERROR(EFSCORRUPTED);
419 if (!XFS_DFORK_Q(dip))
422 ASSERT(ip->i_afp == NULL);
423 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP | KM_NOFS);
425 switch (dip->di_aformat) {
426 case XFS_DINODE_FMT_LOCAL:
427 atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
428 size = be16_to_cpu(atp->hdr.totsize);
430 if (unlikely(size < sizeof(struct xfs_attr_sf_hdr))) {
431 xfs_warn(ip->i_mount,
432 "corrupt inode %Lu (bad attr fork size %Ld).",
433 (unsigned long long) ip->i_ino,
435 XFS_CORRUPTION_ERROR("xfs_iformat(8)",
438 return XFS_ERROR(EFSCORRUPTED);
441 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size);
443 case XFS_DINODE_FMT_EXTENTS:
444 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
446 case XFS_DINODE_FMT_BTREE:
447 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
450 error = XFS_ERROR(EFSCORRUPTED);
454 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
456 xfs_idestroy_fork(ip, XFS_DATA_FORK);
462 * The file is in-lined in the on-disk inode.
463 * If it fits into if_inline_data, then copy
464 * it there, otherwise allocate a buffer for it
465 * and copy the data there. Either way, set
466 * if_data to point at the data.
467 * If we allocate a buffer for the data, make
468 * sure that its size is a multiple of 4 and
469 * record the real size in i_real_bytes.
482 * If the size is unreasonable, then something
483 * is wrong and we just bail out rather than crash in
484 * kmem_alloc() or memcpy() below.
486 if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
487 xfs_warn(ip->i_mount,
488 "corrupt inode %Lu (bad size %d for local fork, size = %d).",
489 (unsigned long long) ip->i_ino, size,
490 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
491 XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW,
493 return XFS_ERROR(EFSCORRUPTED);
495 ifp = XFS_IFORK_PTR(ip, whichfork);
498 ifp->if_u1.if_data = NULL;
499 else if (size <= sizeof(ifp->if_u2.if_inline_data))
500 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
502 real_size = roundup(size, 4);
503 ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP | KM_NOFS);
505 ifp->if_bytes = size;
506 ifp->if_real_bytes = real_size;
508 memcpy(ifp->if_u1.if_data, XFS_DFORK_PTR(dip, whichfork), size);
509 ifp->if_flags &= ~XFS_IFEXTENTS;
510 ifp->if_flags |= XFS_IFINLINE;
515 * The file consists of a set of extents all
516 * of which fit into the on-disk inode.
517 * If there are few enough extents to fit into
518 * the if_inline_ext, then copy them there.
519 * Otherwise allocate a buffer for them and copy
520 * them into it. Either way, set if_extents
521 * to point at the extents.
535 ifp = XFS_IFORK_PTR(ip, whichfork);
536 nex = XFS_DFORK_NEXTENTS(dip, whichfork);
537 size = nex * (uint)sizeof(xfs_bmbt_rec_t);
540 * If the number of extents is unreasonable, then something
541 * is wrong and we just bail out rather than crash in
542 * kmem_alloc() or memcpy() below.
544 if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
545 xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).",
546 (unsigned long long) ip->i_ino, nex);
547 XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW,
549 return XFS_ERROR(EFSCORRUPTED);
552 ifp->if_real_bytes = 0;
554 ifp->if_u1.if_extents = NULL;
555 else if (nex <= XFS_INLINE_EXTS)
556 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
558 xfs_iext_add(ifp, 0, nex);
560 ifp->if_bytes = size;
562 dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
563 xfs_validate_extents(ifp, nex, XFS_EXTFMT_INODE(ip));
564 for (i = 0; i < nex; i++, dp++) {
565 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
566 ep->l0 = get_unaligned_be64(&dp->l0);
567 ep->l1 = get_unaligned_be64(&dp->l1);
569 XFS_BMAP_TRACE_EXLIST(ip, nex, whichfork);
570 if (whichfork != XFS_DATA_FORK ||
571 XFS_EXTFMT_INODE(ip) == XFS_EXTFMT_NOSTATE)
572 if (unlikely(xfs_check_nostate_extents(
574 XFS_ERROR_REPORT("xfs_iformat_extents(2)",
577 return XFS_ERROR(EFSCORRUPTED);
580 ifp->if_flags |= XFS_IFEXTENTS;
585 * The file has too many extents to fit into
586 * the inode, so they are in B-tree format.
587 * Allocate a buffer for the root of the B-tree
588 * and copy the root into it. The i_extents
589 * field will remain NULL until all of the
590 * extents are read in (when they are needed).
598 xfs_bmdr_block_t *dfp;
604 ifp = XFS_IFORK_PTR(ip, whichfork);
605 dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
606 size = XFS_BMAP_BROOT_SPACE(dfp);
607 nrecs = be16_to_cpu(dfp->bb_numrecs);
610 * blow out if -- fork has less extents than can fit in
611 * fork (fork shouldn't be a btree format), root btree
612 * block has more records than can fit into the fork,
613 * or the number of extents is greater than the number of
616 if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <=
617 XFS_IFORK_MAXEXT(ip, whichfork) ||
618 XFS_BMDR_SPACE_CALC(nrecs) >
619 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork) ||
620 XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks)) {
621 xfs_warn(ip->i_mount, "corrupt inode %Lu (btree).",
622 (unsigned long long) ip->i_ino);
623 XFS_CORRUPTION_ERROR("xfs_iformat_btree", XFS_ERRLEVEL_LOW,
625 return XFS_ERROR(EFSCORRUPTED);
628 ifp->if_broot_bytes = size;
629 ifp->if_broot = kmem_alloc(size, KM_SLEEP | KM_NOFS);
630 ASSERT(ifp->if_broot != NULL);
632 * Copy and convert from the on-disk structure
633 * to the in-memory structure.
635 xfs_bmdr_to_bmbt(ip->i_mount, dfp,
636 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
637 ifp->if_broot, size);
638 ifp->if_flags &= ~XFS_IFEXTENTS;
639 ifp->if_flags |= XFS_IFBROOT;
645 xfs_dinode_from_disk(
649 to->di_magic = be16_to_cpu(from->di_magic);
650 to->di_mode = be16_to_cpu(from->di_mode);
651 to->di_version = from ->di_version;
652 to->di_format = from->di_format;
653 to->di_onlink = be16_to_cpu(from->di_onlink);
654 to->di_uid = be32_to_cpu(from->di_uid);
655 to->di_gid = be32_to_cpu(from->di_gid);
656 to->di_nlink = be32_to_cpu(from->di_nlink);
657 to->di_projid_lo = be16_to_cpu(from->di_projid_lo);
658 to->di_projid_hi = be16_to_cpu(from->di_projid_hi);
659 memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
660 to->di_flushiter = be16_to_cpu(from->di_flushiter);
661 to->di_atime.t_sec = be32_to_cpu(from->di_atime.t_sec);
662 to->di_atime.t_nsec = be32_to_cpu(from->di_atime.t_nsec);
663 to->di_mtime.t_sec = be32_to_cpu(from->di_mtime.t_sec);
664 to->di_mtime.t_nsec = be32_to_cpu(from->di_mtime.t_nsec);
665 to->di_ctime.t_sec = be32_to_cpu(from->di_ctime.t_sec);
666 to->di_ctime.t_nsec = be32_to_cpu(from->di_ctime.t_nsec);
667 to->di_size = be64_to_cpu(from->di_size);
668 to->di_nblocks = be64_to_cpu(from->di_nblocks);
669 to->di_extsize = be32_to_cpu(from->di_extsize);
670 to->di_nextents = be32_to_cpu(from->di_nextents);
671 to->di_anextents = be16_to_cpu(from->di_anextents);
672 to->di_forkoff = from->di_forkoff;
673 to->di_aformat = from->di_aformat;
674 to->di_dmevmask = be32_to_cpu(from->di_dmevmask);
675 to->di_dmstate = be16_to_cpu(from->di_dmstate);
676 to->di_flags = be16_to_cpu(from->di_flags);
677 to->di_gen = be32_to_cpu(from->di_gen);
683 xfs_icdinode_t *from)
685 to->di_magic = cpu_to_be16(from->di_magic);
686 to->di_mode = cpu_to_be16(from->di_mode);
687 to->di_version = from ->di_version;
688 to->di_format = from->di_format;
689 to->di_onlink = cpu_to_be16(from->di_onlink);
690 to->di_uid = cpu_to_be32(from->di_uid);
691 to->di_gid = cpu_to_be32(from->di_gid);
692 to->di_nlink = cpu_to_be32(from->di_nlink);
693 to->di_projid_lo = cpu_to_be16(from->di_projid_lo);
694 to->di_projid_hi = cpu_to_be16(from->di_projid_hi);
695 memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
696 to->di_flushiter = cpu_to_be16(from->di_flushiter);
697 to->di_atime.t_sec = cpu_to_be32(from->di_atime.t_sec);
698 to->di_atime.t_nsec = cpu_to_be32(from->di_atime.t_nsec);
699 to->di_mtime.t_sec = cpu_to_be32(from->di_mtime.t_sec);
700 to->di_mtime.t_nsec = cpu_to_be32(from->di_mtime.t_nsec);
701 to->di_ctime.t_sec = cpu_to_be32(from->di_ctime.t_sec);
702 to->di_ctime.t_nsec = cpu_to_be32(from->di_ctime.t_nsec);
703 to->di_size = cpu_to_be64(from->di_size);
704 to->di_nblocks = cpu_to_be64(from->di_nblocks);
705 to->di_extsize = cpu_to_be32(from->di_extsize);
706 to->di_nextents = cpu_to_be32(from->di_nextents);
707 to->di_anextents = cpu_to_be16(from->di_anextents);
708 to->di_forkoff = from->di_forkoff;
709 to->di_aformat = from->di_aformat;
710 to->di_dmevmask = cpu_to_be32(from->di_dmevmask);
711 to->di_dmstate = cpu_to_be16(from->di_dmstate);
712 to->di_flags = cpu_to_be16(from->di_flags);
713 to->di_gen = cpu_to_be32(from->di_gen);
722 if (di_flags & XFS_DIFLAG_ANY) {
723 if (di_flags & XFS_DIFLAG_REALTIME)
724 flags |= XFS_XFLAG_REALTIME;
725 if (di_flags & XFS_DIFLAG_PREALLOC)
726 flags |= XFS_XFLAG_PREALLOC;
727 if (di_flags & XFS_DIFLAG_IMMUTABLE)
728 flags |= XFS_XFLAG_IMMUTABLE;
729 if (di_flags & XFS_DIFLAG_APPEND)
730 flags |= XFS_XFLAG_APPEND;
731 if (di_flags & XFS_DIFLAG_SYNC)
732 flags |= XFS_XFLAG_SYNC;
733 if (di_flags & XFS_DIFLAG_NOATIME)
734 flags |= XFS_XFLAG_NOATIME;
735 if (di_flags & XFS_DIFLAG_NODUMP)
736 flags |= XFS_XFLAG_NODUMP;
737 if (di_flags & XFS_DIFLAG_RTINHERIT)
738 flags |= XFS_XFLAG_RTINHERIT;
739 if (di_flags & XFS_DIFLAG_PROJINHERIT)
740 flags |= XFS_XFLAG_PROJINHERIT;
741 if (di_flags & XFS_DIFLAG_NOSYMLINKS)
742 flags |= XFS_XFLAG_NOSYMLINKS;
743 if (di_flags & XFS_DIFLAG_EXTSIZE)
744 flags |= XFS_XFLAG_EXTSIZE;
745 if (di_flags & XFS_DIFLAG_EXTSZINHERIT)
746 flags |= XFS_XFLAG_EXTSZINHERIT;
747 if (di_flags & XFS_DIFLAG_NODEFRAG)
748 flags |= XFS_XFLAG_NODEFRAG;
749 if (di_flags & XFS_DIFLAG_FILESTREAM)
750 flags |= XFS_XFLAG_FILESTREAM;
760 xfs_icdinode_t *dic = &ip->i_d;
762 return _xfs_dic2xflags(dic->di_flags) |
763 (XFS_IFORK_Q(ip) ? XFS_XFLAG_HASATTR : 0);
770 return _xfs_dic2xflags(be16_to_cpu(dip->di_flags)) |
771 (XFS_DFORK_Q(dip) ? XFS_XFLAG_HASATTR : 0);
775 * Read the disk inode attributes into the in-core inode structure.
789 * Fill in the location information in the in-core inode.
791 error = xfs_imap(mp, tp, ip->i_ino, &ip->i_imap, iget_flags);
796 * Get pointers to the on-disk inode and the buffer containing it.
798 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &bp, 0, iget_flags);
801 dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
804 * If we got something that isn't an inode it means someone
805 * (nfs or dmi) has a stale handle.
807 if (dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC)) {
810 "%s: dip->di_magic (0x%x) != XFS_DINODE_MAGIC (0x%x)",
811 __func__, be16_to_cpu(dip->di_magic), XFS_DINODE_MAGIC);
813 error = XFS_ERROR(EINVAL);
818 * If the on-disk inode is already linked to a directory
819 * entry, copy all of the inode into the in-core inode.
820 * xfs_iformat() handles copying in the inode format
821 * specific information.
822 * Otherwise, just get the truly permanent information.
825 xfs_dinode_from_disk(&ip->i_d, dip);
826 error = xfs_iformat(ip, dip);
829 xfs_alert(mp, "%s: xfs_iformat() returned error %d",
835 ip->i_d.di_magic = be16_to_cpu(dip->di_magic);
836 ip->i_d.di_version = dip->di_version;
837 ip->i_d.di_gen = be32_to_cpu(dip->di_gen);
838 ip->i_d.di_flushiter = be16_to_cpu(dip->di_flushiter);
840 * Make sure to pull in the mode here as well in
841 * case the inode is released without being used.
842 * This ensures that xfs_inactive() will see that
843 * the inode is already free and not try to mess
844 * with the uninitialized part of it.
850 * The inode format changed when we moved the link count and
851 * made it 32 bits long. If this is an old format inode,
852 * convert it in memory to look like a new one. If it gets
853 * flushed to disk we will convert back before flushing or
854 * logging it. We zero out the new projid field and the old link
855 * count field. We'll handle clearing the pad field (the remains
856 * of the old uuid field) when we actually convert the inode to
857 * the new format. We don't change the version number so that we
858 * can distinguish this from a real new format inode.
860 if (ip->i_d.di_version == 1) {
861 ip->i_d.di_nlink = ip->i_d.di_onlink;
862 ip->i_d.di_onlink = 0;
863 xfs_set_projid(ip, 0);
866 ip->i_delayed_blks = 0;
869 * Mark the buffer containing the inode as something to keep
870 * around for a while. This helps to keep recently accessed
871 * meta-data in-core longer.
873 xfs_buf_set_ref(bp, XFS_INO_REF);
876 * Use xfs_trans_brelse() to release the buffer containing the
877 * on-disk inode, because it was acquired with xfs_trans_read_buf()
878 * in xfs_itobp() above. If tp is NULL, this is just a normal
879 * brelse(). If we're within a transaction, then xfs_trans_brelse()
880 * will only release the buffer if it is not dirty within the
881 * transaction. It will be OK to release the buffer in this case,
882 * because inodes on disk are never destroyed and we will be
883 * locking the new in-core inode before putting it in the hash
884 * table where other processes can find it. Thus we don't have
885 * to worry about the inode being changed just because we released
889 xfs_trans_brelse(tp, bp);
894 * Read in extents from a btree-format inode.
895 * Allocate and fill in if_extents. Real work is done in xfs_bmap.c.
905 xfs_extnum_t nextents;
907 if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
908 XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW,
910 return XFS_ERROR(EFSCORRUPTED);
912 nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
913 ifp = XFS_IFORK_PTR(ip, whichfork);
916 * We know that the size is valid (it's checked in iformat_btree)
918 ifp->if_bytes = ifp->if_real_bytes = 0;
919 ifp->if_flags |= XFS_IFEXTENTS;
920 xfs_iext_add(ifp, 0, nextents);
921 error = xfs_bmap_read_extents(tp, ip, whichfork);
923 xfs_iext_destroy(ifp);
924 ifp->if_flags &= ~XFS_IFEXTENTS;
927 xfs_validate_extents(ifp, nextents, XFS_EXTFMT_INODE(ip));
932 * Allocate an inode on disk and return a copy of its in-core version.
933 * The in-core inode is locked exclusively. Set mode, nlink, and rdev
934 * appropriately within the inode. The uid and gid for the inode are
935 * set according to the contents of the given cred structure.
937 * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc()
938 * has a free inode available, call xfs_iget()
939 * to obtain the in-core version of the allocated inode. Finally,
940 * fill in the inode and log its initial contents. In this case,
941 * ialloc_context would be set to NULL and call_again set to false.
943 * If xfs_dialloc() does not have an available inode,
944 * it will replenish its supply by doing an allocation. Since we can
945 * only do one allocation within a transaction without deadlocks, we
946 * must commit the current transaction before returning the inode itself.
947 * In this case, therefore, we will set call_again to true and return.
948 * The caller should then commit the current transaction, start a new
949 * transaction, and call xfs_ialloc() again to actually get the inode.
951 * To ensure that some other process does not grab the inode that
952 * was allocated during the first call to xfs_ialloc(), this routine
953 * also returns the [locked] bp pointing to the head of the freelist
954 * as ialloc_context. The caller should hold this buffer across
955 * the commit and pass it back into this routine on the second call.
957 * If we are allocating quota inodes, we do not have a parent inode
958 * to attach to or associate with (i.e. pip == NULL) because they
959 * are not linked into the directory structure - they are attached
960 * directly to the superblock - and so have no parent.
971 xfs_buf_t **ialloc_context,
972 boolean_t *call_again,
983 * Call the space management code to pick
984 * the on-disk inode to be allocated.
986 error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc,
987 ialloc_context, call_again, &ino);
990 if (*call_again || ino == NULLFSINO) {
994 ASSERT(*ialloc_context == NULL);
997 * Get the in-core inode with the lock held exclusively.
998 * This is because we're setting fields here we need
999 * to prevent others from looking at until we're done.
1001 error = xfs_iget(tp->t_mountp, tp, ino, XFS_IGET_CREATE,
1002 XFS_ILOCK_EXCL, &ip);
1007 ip->i_d.di_mode = mode;
1008 ip->i_d.di_onlink = 0;
1009 ip->i_d.di_nlink = nlink;
1010 ASSERT(ip->i_d.di_nlink == nlink);
1011 ip->i_d.di_uid = current_fsuid();
1012 ip->i_d.di_gid = current_fsgid();
1013 xfs_set_projid(ip, prid);
1014 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
1017 * If the superblock version is up to where we support new format
1018 * inodes and this is currently an old format inode, then change
1019 * the inode version number now. This way we only do the conversion
1020 * here rather than here and in the flush/logging code.
1022 if (xfs_sb_version_hasnlink(&tp->t_mountp->m_sb) &&
1023 ip->i_d.di_version == 1) {
1024 ip->i_d.di_version = 2;
1026 * We've already zeroed the old link count, the projid field,
1027 * and the pad field.
1032 * Project ids won't be stored on disk if we are using a version 1 inode.
1034 if ((prid != 0) && (ip->i_d.di_version == 1))
1035 xfs_bump_ino_vers2(tp, ip);
1037 if (pip && XFS_INHERIT_GID(pip)) {
1038 ip->i_d.di_gid = pip->i_d.di_gid;
1039 if ((pip->i_d.di_mode & S_ISGID) && S_ISDIR(mode)) {
1040 ip->i_d.di_mode |= S_ISGID;
1045 * If the group ID of the new file does not match the effective group
1046 * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
1047 * (and only if the irix_sgid_inherit compatibility variable is set).
1049 if ((irix_sgid_inherit) &&
1050 (ip->i_d.di_mode & S_ISGID) &&
1051 (!in_group_p((gid_t)ip->i_d.di_gid))) {
1052 ip->i_d.di_mode &= ~S_ISGID;
1055 ip->i_d.di_size = 0;
1056 ip->i_d.di_nextents = 0;
1057 ASSERT(ip->i_d.di_nblocks == 0);
1060 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
1061 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
1062 ip->i_d.di_atime = ip->i_d.di_mtime;
1063 ip->i_d.di_ctime = ip->i_d.di_mtime;
1066 * di_gen will have been taken care of in xfs_iread.
1068 ip->i_d.di_extsize = 0;
1069 ip->i_d.di_dmevmask = 0;
1070 ip->i_d.di_dmstate = 0;
1071 ip->i_d.di_flags = 0;
1072 flags = XFS_ILOG_CORE;
1073 switch (mode & S_IFMT) {
1078 ip->i_d.di_format = XFS_DINODE_FMT_DEV;
1079 ip->i_df.if_u2.if_rdev = rdev;
1080 ip->i_df.if_flags = 0;
1081 flags |= XFS_ILOG_DEV;
1085 * we can't set up filestreams until after the VFS inode
1086 * is set up properly.
1088 if (pip && xfs_inode_is_filestream(pip))
1092 if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) {
1095 if (S_ISDIR(mode)) {
1096 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
1097 di_flags |= XFS_DIFLAG_RTINHERIT;
1098 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
1099 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
1100 ip->i_d.di_extsize = pip->i_d.di_extsize;
1102 } else if (S_ISREG(mode)) {
1103 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
1104 di_flags |= XFS_DIFLAG_REALTIME;
1105 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
1106 di_flags |= XFS_DIFLAG_EXTSIZE;
1107 ip->i_d.di_extsize = pip->i_d.di_extsize;
1110 if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
1111 xfs_inherit_noatime)
1112 di_flags |= XFS_DIFLAG_NOATIME;
1113 if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
1115 di_flags |= XFS_DIFLAG_NODUMP;
1116 if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
1118 di_flags |= XFS_DIFLAG_SYNC;
1119 if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
1120 xfs_inherit_nosymlinks)
1121 di_flags |= XFS_DIFLAG_NOSYMLINKS;
1122 if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1123 di_flags |= XFS_DIFLAG_PROJINHERIT;
1124 if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
1125 xfs_inherit_nodefrag)
1126 di_flags |= XFS_DIFLAG_NODEFRAG;
1127 if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
1128 di_flags |= XFS_DIFLAG_FILESTREAM;
1129 ip->i_d.di_flags |= di_flags;
1133 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
1134 ip->i_df.if_flags = XFS_IFEXTENTS;
1135 ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0;
1136 ip->i_df.if_u1.if_extents = NULL;
1142 * Attribute fork settings for new inode.
1144 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1145 ip->i_d.di_anextents = 0;
1148 * Log the new values stuffed into the inode.
1150 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1151 xfs_trans_log_inode(tp, ip, flags);
1153 /* now that we have an i_mode we can setup inode ops and unlock */
1154 xfs_setup_inode(ip);
1156 /* now we have set up the vfs inode we can associate the filestream */
1158 error = xfs_filestream_associate(pip, ip);
1162 xfs_iflags_set(ip, XFS_IFILESTREAM);
1170 * Free up the underlying blocks past new_size. The new size must be smaller
1171 * than the current size. This routine can be used both for the attribute and
1172 * data fork, and does not modify the inode size, which is left to the caller.
1174 * The transaction passed to this routine must have made a permanent log
1175 * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the
1176 * given transaction and start new ones, so make sure everything involved in
1177 * the transaction is tidy before calling here. Some transaction will be
1178 * returned to the caller to be committed. The incoming transaction must
1179 * already include the inode, and both inode locks must be held exclusively.
1180 * The inode must also be "held" within the transaction. On return the inode
1181 * will be "held" within the returned transaction. This routine does NOT
1182 * require any disk space to be reserved for it within the transaction.
1184 * If we get an error, we must return with the inode locked and linked into the
1185 * current transaction. This keeps things simple for the higher level code,
1186 * because it always knows that the inode is locked and held in the transaction
1187 * that returns to it whether errors occur or not. We don't mark the inode
1188 * dirty on error so that transactions can be easily aborted if possible.
1191 xfs_itruncate_extents(
1192 struct xfs_trans **tpp,
1193 struct xfs_inode *ip,
1195 xfs_fsize_t new_size)
1197 struct xfs_mount *mp = ip->i_mount;
1198 struct xfs_trans *tp = *tpp;
1199 struct xfs_trans *ntp;
1200 xfs_bmap_free_t free_list;
1201 xfs_fsblock_t first_block;
1202 xfs_fileoff_t first_unmap_block;
1203 xfs_fileoff_t last_block;
1204 xfs_filblks_t unmap_len;
1209 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
1210 ASSERT(new_size <= XFS_ISIZE(ip));
1211 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1212 ASSERT(ip->i_itemp != NULL);
1213 ASSERT(ip->i_itemp->ili_lock_flags == 0);
1214 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1216 trace_xfs_itruncate_extents_start(ip, new_size);
1219 * Since it is possible for space to become allocated beyond
1220 * the end of the file (in a crash where the space is allocated
1221 * but the inode size is not yet updated), simply remove any
1222 * blocks which show up between the new EOF and the maximum
1223 * possible file size. If the first block to be removed is
1224 * beyond the maximum file size (ie it is the same as last_block),
1225 * then there is nothing to do.
1227 first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
1228 last_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
1229 if (first_unmap_block == last_block)
1232 ASSERT(first_unmap_block < last_block);
1233 unmap_len = last_block - first_unmap_block + 1;
1235 xfs_bmap_init(&free_list, &first_block);
1236 error = xfs_bunmapi(tp, ip,
1237 first_unmap_block, unmap_len,
1238 xfs_bmapi_aflag(whichfork),
1239 XFS_ITRUNC_MAX_EXTENTS,
1240 &first_block, &free_list,
1243 goto out_bmap_cancel;
1246 * Duplicate the transaction that has the permanent
1247 * reservation and commit the old transaction.
1249 error = xfs_bmap_finish(&tp, &free_list, &committed);
1251 xfs_trans_ijoin(tp, ip, 0);
1253 goto out_bmap_cancel;
1257 * Mark the inode dirty so it will be logged and
1258 * moved forward in the log as part of every commit.
1260 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1263 ntp = xfs_trans_dup(tp);
1264 error = xfs_trans_commit(tp, 0);
1267 xfs_trans_ijoin(tp, ip, 0);
1273 * Transaction commit worked ok so we can drop the extra ticket
1274 * reference that we gained in xfs_trans_dup()
1276 xfs_log_ticket_put(tp->t_ticket);
1277 error = xfs_trans_reserve(tp, 0,
1278 XFS_ITRUNCATE_LOG_RES(mp), 0,
1279 XFS_TRANS_PERM_LOG_RES,
1280 XFS_ITRUNCATE_LOG_COUNT);
1286 * Always re-log the inode so that our permanent transaction can keep
1287 * on rolling it forward in the log.
1289 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1291 trace_xfs_itruncate_extents_end(ip, new_size);
1298 * If the bunmapi call encounters an error, return to the caller where
1299 * the transaction can be properly aborted. We just need to make sure
1300 * we're not holding any resources that we were not when we came in.
1302 xfs_bmap_cancel(&free_list);
1307 * This is called when the inode's link count goes to 0.
1308 * We place the on-disk inode on a list in the AGI. It
1309 * will be pulled from this list when the inode is freed.
1326 ASSERT(ip->i_d.di_nlink == 0);
1327 ASSERT(ip->i_d.di_mode != 0);
1332 * Get the agi buffer first. It ensures lock ordering
1335 error = xfs_read_agi(mp, tp, XFS_INO_TO_AGNO(mp, ip->i_ino), &agibp);
1338 agi = XFS_BUF_TO_AGI(agibp);
1341 * Get the index into the agi hash table for the
1342 * list this inode will go on.
1344 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
1346 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
1347 ASSERT(agi->agi_unlinked[bucket_index]);
1348 ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino);
1350 if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) {
1352 * There is already another inode in the bucket we need
1353 * to add ourselves to. Add us at the front of the list.
1354 * Here we put the head pointer into our next pointer,
1355 * and then we fall through to point the head at us.
1357 error = xfs_itobp(mp, tp, ip, &dip, &ibp, 0);
1361 ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO));
1362 dip->di_next_unlinked = agi->agi_unlinked[bucket_index];
1363 offset = ip->i_imap.im_boffset +
1364 offsetof(xfs_dinode_t, di_next_unlinked);
1365 xfs_trans_inode_buf(tp, ibp);
1366 xfs_trans_log_buf(tp, ibp, offset,
1367 (offset + sizeof(xfs_agino_t) - 1));
1368 xfs_inobp_check(mp, ibp);
1372 * Point the bucket head pointer at the inode being inserted.
1375 agi->agi_unlinked[bucket_index] = cpu_to_be32(agino);
1376 offset = offsetof(xfs_agi_t, agi_unlinked) +
1377 (sizeof(xfs_agino_t) * bucket_index);
1378 xfs_trans_log_buf(tp, agibp, offset,
1379 (offset + sizeof(xfs_agino_t) - 1));
1384 * Pull the on-disk inode from the AGI unlinked list.
1397 xfs_agnumber_t agno;
1399 xfs_agino_t next_agino;
1400 xfs_buf_t *last_ibp;
1401 xfs_dinode_t *last_dip = NULL;
1403 int offset, last_offset = 0;
1407 agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
1410 * Get the agi buffer first. It ensures lock ordering
1413 error = xfs_read_agi(mp, tp, agno, &agibp);
1417 agi = XFS_BUF_TO_AGI(agibp);
1420 * Get the index into the agi hash table for the
1421 * list this inode will go on.
1423 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
1425 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
1426 ASSERT(agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO));
1427 ASSERT(agi->agi_unlinked[bucket_index]);
1429 if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) {
1431 * We're at the head of the list. Get the inode's
1432 * on-disk buffer to see if there is anyone after us
1433 * on the list. Only modify our next pointer if it
1434 * is not already NULLAGINO. This saves us the overhead
1435 * of dealing with the buffer when there is no need to
1438 error = xfs_itobp(mp, tp, ip, &dip, &ibp, 0);
1440 xfs_warn(mp, "%s: xfs_itobp() returned error %d.",
1444 next_agino = be32_to_cpu(dip->di_next_unlinked);
1445 ASSERT(next_agino != 0);
1446 if (next_agino != NULLAGINO) {
1447 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
1448 offset = ip->i_imap.im_boffset +
1449 offsetof(xfs_dinode_t, di_next_unlinked);
1450 xfs_trans_inode_buf(tp, ibp);
1451 xfs_trans_log_buf(tp, ibp, offset,
1452 (offset + sizeof(xfs_agino_t) - 1));
1453 xfs_inobp_check(mp, ibp);
1455 xfs_trans_brelse(tp, ibp);
1458 * Point the bucket head pointer at the next inode.
1460 ASSERT(next_agino != 0);
1461 ASSERT(next_agino != agino);
1462 agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino);
1463 offset = offsetof(xfs_agi_t, agi_unlinked) +
1464 (sizeof(xfs_agino_t) * bucket_index);
1465 xfs_trans_log_buf(tp, agibp, offset,
1466 (offset + sizeof(xfs_agino_t) - 1));
1469 * We need to search the list for the inode being freed.
1471 next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
1473 while (next_agino != agino) {
1475 * If the last inode wasn't the one pointing to
1476 * us, then release its buffer since we're not
1477 * going to do anything with it.
1479 if (last_ibp != NULL) {
1480 xfs_trans_brelse(tp, last_ibp);
1482 next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino);
1483 error = xfs_inotobp(mp, tp, next_ino, &last_dip,
1484 &last_ibp, &last_offset, 0);
1487 "%s: xfs_inotobp() returned error %d.",
1491 next_agino = be32_to_cpu(last_dip->di_next_unlinked);
1492 ASSERT(next_agino != NULLAGINO);
1493 ASSERT(next_agino != 0);
1496 * Now last_ibp points to the buffer previous to us on
1497 * the unlinked list. Pull us from the list.
1499 error = xfs_itobp(mp, tp, ip, &dip, &ibp, 0);
1501 xfs_warn(mp, "%s: xfs_itobp(2) returned error %d.",
1505 next_agino = be32_to_cpu(dip->di_next_unlinked);
1506 ASSERT(next_agino != 0);
1507 ASSERT(next_agino != agino);
1508 if (next_agino != NULLAGINO) {
1509 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
1510 offset = ip->i_imap.im_boffset +
1511 offsetof(xfs_dinode_t, di_next_unlinked);
1512 xfs_trans_inode_buf(tp, ibp);
1513 xfs_trans_log_buf(tp, ibp, offset,
1514 (offset + sizeof(xfs_agino_t) - 1));
1515 xfs_inobp_check(mp, ibp);
1517 xfs_trans_brelse(tp, ibp);
1520 * Point the previous inode on the list to the next inode.
1522 last_dip->di_next_unlinked = cpu_to_be32(next_agino);
1523 ASSERT(next_agino != 0);
1524 offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked);
1525 xfs_trans_inode_buf(tp, last_ibp);
1526 xfs_trans_log_buf(tp, last_ibp, offset,
1527 (offset + sizeof(xfs_agino_t) - 1));
1528 xfs_inobp_check(mp, last_ibp);
1534 * A big issue when freeing the inode cluster is is that we _cannot_ skip any
1535 * inodes that are in memory - they all must be marked stale and attached to
1536 * the cluster buffer.
1540 xfs_inode_t *free_ip,
1544 xfs_mount_t *mp = free_ip->i_mount;
1545 int blks_per_cluster;
1552 xfs_inode_log_item_t *iip;
1553 xfs_log_item_t *lip;
1554 struct xfs_perag *pag;
1556 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum));
1557 if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
1558 blks_per_cluster = 1;
1559 ninodes = mp->m_sb.sb_inopblock;
1560 nbufs = XFS_IALLOC_BLOCKS(mp);
1562 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
1563 mp->m_sb.sb_blocksize;
1564 ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
1565 nbufs = XFS_IALLOC_BLOCKS(mp) / blks_per_cluster;
1568 for (j = 0; j < nbufs; j++, inum += ninodes) {
1569 blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
1570 XFS_INO_TO_AGBNO(mp, inum));
1573 * We obtain and lock the backing buffer first in the process
1574 * here, as we have to ensure that any dirty inode that we
1575 * can't get the flush lock on is attached to the buffer.
1576 * If we scan the in-memory inodes first, then buffer IO can
1577 * complete before we get a lock on it, and hence we may fail
1578 * to mark all the active inodes on the buffer stale.
1580 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
1581 mp->m_bsize * blks_per_cluster, 0);
1586 * Walk the inodes already attached to the buffer and mark them
1587 * stale. These will all have the flush locks held, so an
1588 * in-memory inode walk can't lock them. By marking them all
1589 * stale first, we will not attempt to lock them in the loop
1590 * below as the XFS_ISTALE flag will be set.
1594 if (lip->li_type == XFS_LI_INODE) {
1595 iip = (xfs_inode_log_item_t *)lip;
1596 ASSERT(iip->ili_logged == 1);
1597 lip->li_cb = xfs_istale_done;
1598 xfs_trans_ail_copy_lsn(mp->m_ail,
1599 &iip->ili_flush_lsn,
1600 &iip->ili_item.li_lsn);
1601 xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
1603 lip = lip->li_bio_list;
1608 * For each inode in memory attempt to add it to the inode
1609 * buffer and set it up for being staled on buffer IO
1610 * completion. This is safe as we've locked out tail pushing
1611 * and flushing by locking the buffer.
1613 * We have already marked every inode that was part of a
1614 * transaction stale above, which means there is no point in
1615 * even trying to lock them.
1617 for (i = 0; i < ninodes; i++) {
1620 ip = radix_tree_lookup(&pag->pag_ici_root,
1621 XFS_INO_TO_AGINO(mp, (inum + i)));
1623 /* Inode not in memory, nothing to do */
1630 * because this is an RCU protected lookup, we could
1631 * find a recently freed or even reallocated inode
1632 * during the lookup. We need to check under the
1633 * i_flags_lock for a valid inode here. Skip it if it
1634 * is not valid, the wrong inode or stale.
1636 spin_lock(&ip->i_flags_lock);
1637 if (ip->i_ino != inum + i ||
1638 __xfs_iflags_test(ip, XFS_ISTALE)) {
1639 spin_unlock(&ip->i_flags_lock);
1643 spin_unlock(&ip->i_flags_lock);
1646 * Don't try to lock/unlock the current inode, but we
1647 * _cannot_ skip the other inodes that we did not find
1648 * in the list attached to the buffer and are not
1649 * already marked stale. If we can't lock it, back off
1652 if (ip != free_ip &&
1653 !xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
1661 xfs_iflags_set(ip, XFS_ISTALE);
1664 * we don't need to attach clean inodes or those only
1665 * with unlogged changes (which we throw away, anyway).
1668 if (!iip || xfs_inode_clean(ip)) {
1669 ASSERT(ip != free_ip);
1671 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1675 iip->ili_last_fields = iip->ili_fields;
1676 iip->ili_fields = 0;
1677 iip->ili_logged = 1;
1678 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
1679 &iip->ili_item.li_lsn);
1681 xfs_buf_attach_iodone(bp, xfs_istale_done,
1685 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1688 xfs_trans_stale_inode_buf(tp, bp);
1689 xfs_trans_binval(tp, bp);
1697 * This is called to return an inode to the inode free list.
1698 * The inode should already be truncated to 0 length and have
1699 * no pages associated with it. This routine also assumes that
1700 * the inode is already a part of the transaction.
1702 * The on-disk copy of the inode will have been added to the list
1703 * of unlinked inodes in the AGI. We need to remove the inode from
1704 * that list atomically with respect to freeing it here.
1710 xfs_bmap_free_t *flist)
1714 xfs_ino_t first_ino;
1718 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1719 ASSERT(ip->i_d.di_nlink == 0);
1720 ASSERT(ip->i_d.di_nextents == 0);
1721 ASSERT(ip->i_d.di_anextents == 0);
1722 ASSERT(ip->i_d.di_size == 0 || !S_ISREG(ip->i_d.di_mode));
1723 ASSERT(ip->i_d.di_nblocks == 0);
1726 * Pull the on-disk inode from the AGI unlinked list.
1728 error = xfs_iunlink_remove(tp, ip);
1733 error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino);
1737 ip->i_d.di_mode = 0; /* mark incore inode as free */
1738 ip->i_d.di_flags = 0;
1739 ip->i_d.di_dmevmask = 0;
1740 ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */
1741 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
1742 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1744 * Bump the generation count so no one will be confused
1745 * by reincarnations of this inode.
1749 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1751 error = xfs_itobp(ip->i_mount, tp, ip, &dip, &ibp, 0);
1756 * Clear the on-disk di_mode. This is to prevent xfs_bulkstat
1757 * from picking up this inode when it is reclaimed (its incore state
1758 * initialzed but not flushed to disk yet). The in-core di_mode is
1759 * already cleared and a corresponding transaction logged.
1760 * The hack here just synchronizes the in-core to on-disk
1761 * di_mode value in advance before the actual inode sync to disk.
1762 * This is OK because the inode is already unlinked and would never
1763 * change its di_mode again for this inode generation.
1764 * This is a temporary hack that would require a proper fix
1770 error = xfs_ifree_cluster(ip, tp, first_ino);
1777 * Reallocate the space for if_broot based on the number of records
1778 * being added or deleted as indicated in rec_diff. Move the records
1779 * and pointers in if_broot to fit the new size. When shrinking this
1780 * will eliminate holes between the records and pointers created by
1781 * the caller. When growing this will create holes to be filled in
1784 * The caller must not request to add more records than would fit in
1785 * the on-disk inode root. If the if_broot is currently NULL, then
1786 * if we adding records one will be allocated. The caller must also
1787 * not request that the number of records go below zero, although
1788 * it can go to zero.
1790 * ip -- the inode whose if_broot area is changing
1791 * ext_diff -- the change in the number of records, positive or negative,
1792 * requested for the if_broot array.
1800 struct xfs_mount *mp = ip->i_mount;
1803 struct xfs_btree_block *new_broot;
1810 * Handle the degenerate case quietly.
1812 if (rec_diff == 0) {
1816 ifp = XFS_IFORK_PTR(ip, whichfork);
1819 * If there wasn't any memory allocated before, just
1820 * allocate it now and get out.
1822 if (ifp->if_broot_bytes == 0) {
1823 new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(rec_diff);
1824 ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
1825 ifp->if_broot_bytes = (int)new_size;
1830 * If there is already an existing if_broot, then we need
1831 * to realloc() it and shift the pointers to their new
1832 * location. The records don't change location because
1833 * they are kept butted up against the btree block header.
1835 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
1836 new_max = cur_max + rec_diff;
1837 new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max);
1838 ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
1839 (size_t)XFS_BMAP_BROOT_SPACE_CALC(cur_max), /* old size */
1840 KM_SLEEP | KM_NOFS);
1841 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
1842 ifp->if_broot_bytes);
1843 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
1845 ifp->if_broot_bytes = (int)new_size;
1846 ASSERT(ifp->if_broot_bytes <=
1847 XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ);
1848 memmove(np, op, cur_max * (uint)sizeof(xfs_dfsbno_t));
1853 * rec_diff is less than 0. In this case, we are shrinking the
1854 * if_broot buffer. It must already exist. If we go to zero
1855 * records, just get rid of the root and clear the status bit.
1857 ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
1858 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
1859 new_max = cur_max + rec_diff;
1860 ASSERT(new_max >= 0);
1862 new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max);
1866 new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
1868 * First copy over the btree block header.
1870 memcpy(new_broot, ifp->if_broot, XFS_BTREE_LBLOCK_LEN);
1873 ifp->if_flags &= ~XFS_IFBROOT;
1877 * Only copy the records and pointers if there are any.
1881 * First copy the records.
1883 op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
1884 np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
1885 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
1888 * Then copy the pointers.
1890 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
1891 ifp->if_broot_bytes);
1892 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
1894 memcpy(np, op, new_max * (uint)sizeof(xfs_dfsbno_t));
1896 kmem_free(ifp->if_broot);
1897 ifp->if_broot = new_broot;
1898 ifp->if_broot_bytes = (int)new_size;
1899 ASSERT(ifp->if_broot_bytes <=
1900 XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ);
1906 * This is called when the amount of space needed for if_data
1907 * is increased or decreased. The change in size is indicated by
1908 * the number of bytes that need to be added or deleted in the
1909 * byte_diff parameter.
1911 * If the amount of space needed has decreased below the size of the
1912 * inline buffer, then switch to using the inline buffer. Otherwise,
1913 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
1914 * to what is needed.
1916 * ip -- the inode whose if_data area is changing
1917 * byte_diff -- the change in the number of bytes, positive or negative,
1918 * requested for the if_data array.
1930 if (byte_diff == 0) {
1934 ifp = XFS_IFORK_PTR(ip, whichfork);
1935 new_size = (int)ifp->if_bytes + byte_diff;
1936 ASSERT(new_size >= 0);
1938 if (new_size == 0) {
1939 if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
1940 kmem_free(ifp->if_u1.if_data);
1942 ifp->if_u1.if_data = NULL;
1944 } else if (new_size <= sizeof(ifp->if_u2.if_inline_data)) {
1946 * If the valid extents/data can fit in if_inline_ext/data,
1947 * copy them from the malloc'd vector and free it.
1949 if (ifp->if_u1.if_data == NULL) {
1950 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
1951 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
1952 ASSERT(ifp->if_real_bytes != 0);
1953 memcpy(ifp->if_u2.if_inline_data, ifp->if_u1.if_data,
1955 kmem_free(ifp->if_u1.if_data);
1956 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
1961 * Stuck with malloc/realloc.
1962 * For inline data, the underlying buffer must be
1963 * a multiple of 4 bytes in size so that it can be
1964 * logged and stay on word boundaries. We enforce
1967 real_size = roundup(new_size, 4);
1968 if (ifp->if_u1.if_data == NULL) {
1969 ASSERT(ifp->if_real_bytes == 0);
1970 ifp->if_u1.if_data = kmem_alloc(real_size,
1971 KM_SLEEP | KM_NOFS);
1972 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
1974 * Only do the realloc if the underlying size
1975 * is really changing.
1977 if (ifp->if_real_bytes != real_size) {
1978 ifp->if_u1.if_data =
1979 kmem_realloc(ifp->if_u1.if_data,
1982 KM_SLEEP | KM_NOFS);
1985 ASSERT(ifp->if_real_bytes == 0);
1986 ifp->if_u1.if_data = kmem_alloc(real_size,
1987 KM_SLEEP | KM_NOFS);
1988 memcpy(ifp->if_u1.if_data, ifp->if_u2.if_inline_data,
1992 ifp->if_real_bytes = real_size;
1993 ifp->if_bytes = new_size;
1994 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
2004 ifp = XFS_IFORK_PTR(ip, whichfork);
2005 if (ifp->if_broot != NULL) {
2006 kmem_free(ifp->if_broot);
2007 ifp->if_broot = NULL;
2011 * If the format is local, then we can't have an extents
2012 * array so just look for an inline data array. If we're
2013 * not local then we may or may not have an extents list,
2014 * so check and free it up if we do.
2016 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
2017 if ((ifp->if_u1.if_data != ifp->if_u2.if_inline_data) &&
2018 (ifp->if_u1.if_data != NULL)) {
2019 ASSERT(ifp->if_real_bytes != 0);
2020 kmem_free(ifp->if_u1.if_data);
2021 ifp->if_u1.if_data = NULL;
2022 ifp->if_real_bytes = 0;
2024 } else if ((ifp->if_flags & XFS_IFEXTENTS) &&
2025 ((ifp->if_flags & XFS_IFEXTIREC) ||
2026 ((ifp->if_u1.if_extents != NULL) &&
2027 (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext)))) {
2028 ASSERT(ifp->if_real_bytes != 0);
2029 xfs_iext_destroy(ifp);
2031 ASSERT(ifp->if_u1.if_extents == NULL ||
2032 ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext);
2033 ASSERT(ifp->if_real_bytes == 0);
2034 if (whichfork == XFS_ATTR_FORK) {
2035 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
2041 * This is called to unpin an inode. The caller must have the inode locked
2042 * in at least shared mode so that the buffer cannot be subsequently pinned
2043 * once someone is waiting for it to be unpinned.
2047 struct xfs_inode *ip)
2049 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
2051 trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
2053 /* Give the log a push to start the unpinning I/O */
2054 xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0);
2060 struct xfs_inode *ip)
2062 wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
2063 DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
2068 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2069 if (xfs_ipincount(ip))
2071 } while (xfs_ipincount(ip));
2072 finish_wait(wq, &wait.wait);
2077 struct xfs_inode *ip)
2079 if (xfs_ipincount(ip))
2080 __xfs_iunpin_wait(ip);
2084 * xfs_iextents_copy()
2086 * This is called to copy the REAL extents (as opposed to the delayed
2087 * allocation extents) from the inode into the given buffer. It
2088 * returns the number of bytes copied into the buffer.
2090 * If there are no delayed allocation extents, then we can just
2091 * memcpy() the extents into the buffer. Otherwise, we need to
2092 * examine each extent in turn and skip those which are delayed.
2104 xfs_fsblock_t start_block;
2106 ifp = XFS_IFORK_PTR(ip, whichfork);
2107 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
2108 ASSERT(ifp->if_bytes > 0);
2110 nrecs = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
2111 XFS_BMAP_TRACE_EXLIST(ip, nrecs, whichfork);
2115 * There are some delayed allocation extents in the
2116 * inode, so copy the extents one at a time and skip
2117 * the delayed ones. There must be at least one
2118 * non-delayed extent.
2121 for (i = 0; i < nrecs; i++) {
2122 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
2123 start_block = xfs_bmbt_get_startblock(ep);
2124 if (isnullstartblock(start_block)) {
2126 * It's a delayed allocation extent, so skip it.
2131 /* Translate to on disk format */
2132 put_unaligned(cpu_to_be64(ep->l0), &dp->l0);
2133 put_unaligned(cpu_to_be64(ep->l1), &dp->l1);
2137 ASSERT(copied != 0);
2138 xfs_validate_extents(ifp, copied, XFS_EXTFMT_INODE(ip));
2140 return (copied * (uint)sizeof(xfs_bmbt_rec_t));
2144 * Each of the following cases stores data into the same region
2145 * of the on-disk inode, so only one of them can be valid at
2146 * any given time. While it is possible to have conflicting formats
2147 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
2148 * in EXTENTS format, this can only happen when the fork has
2149 * changed formats after being modified but before being flushed.
2150 * In these cases, the format always takes precedence, because the
2151 * format indicates the current state of the fork.
2158 xfs_inode_log_item_t *iip,
2165 #ifdef XFS_TRANS_DEBUG
2168 static const short brootflag[2] =
2169 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
2170 static const short dataflag[2] =
2171 { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
2172 static const short extflag[2] =
2173 { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
2177 ifp = XFS_IFORK_PTR(ip, whichfork);
2179 * This can happen if we gave up in iformat in an error path,
2180 * for the attribute fork.
2183 ASSERT(whichfork == XFS_ATTR_FORK);
2186 cp = XFS_DFORK_PTR(dip, whichfork);
2188 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
2189 case XFS_DINODE_FMT_LOCAL:
2190 if ((iip->ili_fields & dataflag[whichfork]) &&
2191 (ifp->if_bytes > 0)) {
2192 ASSERT(ifp->if_u1.if_data != NULL);
2193 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
2194 memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
2198 case XFS_DINODE_FMT_EXTENTS:
2199 ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
2200 !(iip->ili_fields & extflag[whichfork]));
2201 if ((iip->ili_fields & extflag[whichfork]) &&
2202 (ifp->if_bytes > 0)) {
2203 ASSERT(xfs_iext_get_ext(ifp, 0));
2204 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
2205 (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
2210 case XFS_DINODE_FMT_BTREE:
2211 if ((iip->ili_fields & brootflag[whichfork]) &&
2212 (ifp->if_broot_bytes > 0)) {
2213 ASSERT(ifp->if_broot != NULL);
2214 ASSERT(ifp->if_broot_bytes <=
2215 (XFS_IFORK_SIZE(ip, whichfork) +
2216 XFS_BROOT_SIZE_ADJ));
2217 xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
2218 (xfs_bmdr_block_t *)cp,
2219 XFS_DFORK_SIZE(dip, mp, whichfork));
2223 case XFS_DINODE_FMT_DEV:
2224 if (iip->ili_fields & XFS_ILOG_DEV) {
2225 ASSERT(whichfork == XFS_DATA_FORK);
2226 xfs_dinode_put_rdev(dip, ip->i_df.if_u2.if_rdev);
2230 case XFS_DINODE_FMT_UUID:
2231 if (iip->ili_fields & XFS_ILOG_UUID) {
2232 ASSERT(whichfork == XFS_DATA_FORK);
2233 memcpy(XFS_DFORK_DPTR(dip),
2234 &ip->i_df.if_u2.if_uuid,
2250 xfs_mount_t *mp = ip->i_mount;
2251 struct xfs_perag *pag;
2252 unsigned long first_index, mask;
2253 unsigned long inodes_per_cluster;
2255 xfs_inode_t **ilist;
2262 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
2264 inodes_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog;
2265 ilist_size = inodes_per_cluster * sizeof(xfs_inode_t *);
2266 ilist = kmem_alloc(ilist_size, KM_MAYFAIL|KM_NOFS);
2270 mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
2271 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask;
2273 /* really need a gang lookup range call here */
2274 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)ilist,
2275 first_index, inodes_per_cluster);
2279 for (i = 0; i < nr_found; i++) {
2285 * because this is an RCU protected lookup, we could find a
2286 * recently freed or even reallocated inode during the lookup.
2287 * We need to check under the i_flags_lock for a valid inode
2288 * here. Skip it if it is not valid or the wrong inode.
2290 spin_lock(&ip->i_flags_lock);
2292 (XFS_INO_TO_AGINO(mp, iq->i_ino) & mask) != first_index) {
2293 spin_unlock(&ip->i_flags_lock);
2296 spin_unlock(&ip->i_flags_lock);
2299 * Do an un-protected check to see if the inode is dirty and
2300 * is a candidate for flushing. These checks will be repeated
2301 * later after the appropriate locks are acquired.
2303 if (xfs_inode_clean(iq) && xfs_ipincount(iq) == 0)
2307 * Try to get locks. If any are unavailable or it is pinned,
2308 * then this inode cannot be flushed and is skipped.
2311 if (!xfs_ilock_nowait(iq, XFS_ILOCK_SHARED))
2313 if (!xfs_iflock_nowait(iq)) {
2314 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2317 if (xfs_ipincount(iq)) {
2319 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2324 * arriving here means that this inode can be flushed. First
2325 * re-check that it's dirty before flushing.
2327 if (!xfs_inode_clean(iq)) {
2329 error = xfs_iflush_int(iq, bp);
2331 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2332 goto cluster_corrupt_out;
2338 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2342 XFS_STATS_INC(xs_icluster_flushcnt);
2343 XFS_STATS_ADD(xs_icluster_flushinode, clcount);
2354 cluster_corrupt_out:
2356 * Corruption detected in the clustering loop. Invalidate the
2357 * inode buffer and shut down the filesystem.
2361 * Clean up the buffer. If it was delwri, just release it --
2362 * brelse can handle it with no problems. If not, shut down the
2363 * filesystem before releasing the buffer.
2365 bufwasdelwri = (bp->b_flags & _XBF_DELWRI_Q);
2369 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
2371 if (!bufwasdelwri) {
2373 * Just like incore_relse: if we have b_iodone functions,
2374 * mark the buffer as an error and call them. Otherwise
2375 * mark it as stale and brelse.
2380 xfs_buf_ioerror(bp, EIO);
2381 xfs_buf_ioend(bp, 0);
2389 * Unlocks the flush lock
2391 xfs_iflush_abort(iq, false);
2394 return XFS_ERROR(EFSCORRUPTED);
2398 * Flush dirty inode metadata into the backing buffer.
2400 * The caller must have the inode lock and the inode flush lock held. The
2401 * inode lock will still be held upon return to the caller, and the inode
2402 * flush lock will be released after the inode has reached the disk.
2404 * The caller must write out the buffer returned in *bpp and release it.
2408 struct xfs_inode *ip,
2409 struct xfs_buf **bpp)
2411 struct xfs_mount *mp = ip->i_mount;
2413 struct xfs_dinode *dip;
2416 XFS_STATS_INC(xs_iflush_count);
2418 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
2419 ASSERT(xfs_isiflocked(ip));
2420 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
2421 ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
2425 xfs_iunpin_wait(ip);
2428 * For stale inodes we cannot rely on the backing buffer remaining
2429 * stale in cache for the remaining life of the stale inode and so
2430 * xfs_itobp() below may give us a buffer that no longer contains
2431 * inodes below. We have to check this after ensuring the inode is
2432 * unpinned so that it is safe to reclaim the stale inode after the
2435 if (xfs_iflags_test(ip, XFS_ISTALE)) {
2441 * This may have been unpinned because the filesystem is shutting
2442 * down forcibly. If that's the case we must not write this inode
2443 * to disk, because the log record didn't make it to disk.
2445 * We also have to remove the log item from the AIL in this case,
2446 * as we wait for an empty AIL as part of the unmount process.
2448 if (XFS_FORCED_SHUTDOWN(mp)) {
2449 error = XFS_ERROR(EIO);
2454 * Get the buffer containing the on-disk inode.
2456 error = xfs_itobp(mp, NULL, ip, &dip, &bp, XBF_TRYLOCK);
2463 * First flush out the inode that xfs_iflush was called with.
2465 error = xfs_iflush_int(ip, bp);
2470 * If the buffer is pinned then push on the log now so we won't
2471 * get stuck waiting in the write for too long.
2473 if (xfs_buf_ispinned(bp))
2474 xfs_log_force(mp, 0);
2478 * see if other inodes can be gathered into this write
2480 error = xfs_iflush_cluster(ip, bp);
2482 goto cluster_corrupt_out;
2489 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
2490 cluster_corrupt_out:
2491 error = XFS_ERROR(EFSCORRUPTED);
2494 * Unlocks the flush lock
2496 xfs_iflush_abort(ip, false);
2506 xfs_inode_log_item_t *iip;
2509 #ifdef XFS_TRANS_DEBUG
2513 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
2514 ASSERT(xfs_isiflocked(ip));
2515 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
2516 ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
2521 /* set *dip = inode's place in the buffer */
2522 dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
2524 if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
2525 mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) {
2526 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2527 "%s: Bad inode %Lu magic number 0x%x, ptr 0x%p",
2528 __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
2531 if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC,
2532 mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) {
2533 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2534 "%s: Bad inode %Lu, ptr 0x%p, magic number 0x%x",
2535 __func__, ip->i_ino, ip, ip->i_d.di_magic);
2538 if (S_ISREG(ip->i_d.di_mode)) {
2540 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
2541 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE),
2542 mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) {
2543 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2544 "%s: Bad regular inode %Lu, ptr 0x%p",
2545 __func__, ip->i_ino, ip);
2548 } else if (S_ISDIR(ip->i_d.di_mode)) {
2550 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
2551 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
2552 (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL),
2553 mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) {
2554 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2555 "%s: Bad directory inode %Lu, ptr 0x%p",
2556 __func__, ip->i_ino, ip);
2560 if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents >
2561 ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5,
2562 XFS_RANDOM_IFLUSH_5)) {
2563 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2564 "%s: detected corrupt incore inode %Lu, "
2565 "total extents = %d, nblocks = %Ld, ptr 0x%p",
2566 __func__, ip->i_ino,
2567 ip->i_d.di_nextents + ip->i_d.di_anextents,
2568 ip->i_d.di_nblocks, ip);
2571 if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
2572 mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) {
2573 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2574 "%s: bad inode %Lu, forkoff 0x%x, ptr 0x%p",
2575 __func__, ip->i_ino, ip->i_d.di_forkoff, ip);
2579 * bump the flush iteration count, used to detect flushes which
2580 * postdate a log record during recovery.
2583 ip->i_d.di_flushiter++;
2586 * Copy the dirty parts of the inode into the on-disk
2587 * inode. We always copy out the core of the inode,
2588 * because if the inode is dirty at all the core must
2591 xfs_dinode_to_disk(dip, &ip->i_d);
2593 /* Wrap, we never let the log put out DI_MAX_FLUSH */
2594 if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
2595 ip->i_d.di_flushiter = 0;
2598 * If this is really an old format inode and the superblock version
2599 * has not been updated to support only new format inodes, then
2600 * convert back to the old inode format. If the superblock version
2601 * has been updated, then make the conversion permanent.
2603 ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb));
2604 if (ip->i_d.di_version == 1) {
2605 if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
2609 ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
2610 dip->di_onlink = cpu_to_be16(ip->i_d.di_nlink);
2613 * The superblock version has already been bumped,
2614 * so just make the conversion to the new inode
2617 ip->i_d.di_version = 2;
2618 dip->di_version = 2;
2619 ip->i_d.di_onlink = 0;
2621 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
2622 memset(&(dip->di_pad[0]), 0,
2623 sizeof(dip->di_pad));
2624 ASSERT(xfs_get_projid(ip) == 0);
2628 xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK, bp);
2629 if (XFS_IFORK_Q(ip))
2630 xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK, bp);
2631 xfs_inobp_check(mp, bp);
2634 * We've recorded everything logged in the inode, so we'd like to clear
2635 * the ili_fields bits so we don't log and flush things unnecessarily.
2636 * However, we can't stop logging all this information until the data
2637 * we've copied into the disk buffer is written to disk. If we did we
2638 * might overwrite the copy of the inode in the log with all the data
2639 * after re-logging only part of it, and in the face of a crash we
2640 * wouldn't have all the data we need to recover.
2642 * What we do is move the bits to the ili_last_fields field. When
2643 * logging the inode, these bits are moved back to the ili_fields field.
2644 * In the xfs_iflush_done() routine we clear ili_last_fields, since we
2645 * know that the information those bits represent is permanently on
2646 * disk. As long as the flush completes before the inode is logged
2647 * again, then both ili_fields and ili_last_fields will be cleared.
2649 * We can play with the ili_fields bits here, because the inode lock
2650 * must be held exclusively in order to set bits there and the flush
2651 * lock protects the ili_last_fields bits. Set ili_logged so the flush
2652 * done routine can tell whether or not to look in the AIL. Also, store
2653 * the current LSN of the inode so that we can tell whether the item has
2654 * moved in the AIL from xfs_iflush_done(). In order to read the lsn we
2655 * need the AIL lock, because it is a 64 bit value that cannot be read
2658 if (iip != NULL && iip->ili_fields != 0) {
2659 iip->ili_last_fields = iip->ili_fields;
2660 iip->ili_fields = 0;
2661 iip->ili_logged = 1;
2663 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
2664 &iip->ili_item.li_lsn);
2667 * Attach the function xfs_iflush_done to the inode's
2668 * buffer. This will remove the inode from the AIL
2669 * and unlock the inode's flush lock when the inode is
2670 * completely written to disk.
2672 xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item);
2674 ASSERT(bp->b_fspriv != NULL);
2675 ASSERT(bp->b_iodone != NULL);
2678 * We're flushing an inode which is not in the AIL and has
2679 * not been logged. For this case we can immediately drop
2680 * the inode flush lock because we can avoid the whole
2681 * AIL state thing. It's OK to drop the flush lock now,
2682 * because we've already locked the buffer and to do anything
2683 * you really need both.
2686 ASSERT(iip->ili_logged == 0);
2687 ASSERT(iip->ili_last_fields == 0);
2688 ASSERT((iip->ili_item.li_flags & XFS_LI_IN_AIL) == 0);
2696 return XFS_ERROR(EFSCORRUPTED);
2700 * Return a pointer to the extent record at file index idx.
2702 xfs_bmbt_rec_host_t *
2704 xfs_ifork_t *ifp, /* inode fork pointer */
2705 xfs_extnum_t idx) /* index of target extent */
2708 ASSERT(idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
2710 if ((ifp->if_flags & XFS_IFEXTIREC) && (idx == 0)) {
2711 return ifp->if_u1.if_ext_irec->er_extbuf;
2712 } else if (ifp->if_flags & XFS_IFEXTIREC) {
2713 xfs_ext_irec_t *erp; /* irec pointer */
2714 int erp_idx = 0; /* irec index */
2715 xfs_extnum_t page_idx = idx; /* ext index in target list */
2717 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
2718 return &erp->er_extbuf[page_idx];
2719 } else if (ifp->if_bytes) {
2720 return &ifp->if_u1.if_extents[idx];
2727 * Insert new item(s) into the extent records for incore inode
2728 * fork 'ifp'. 'count' new items are inserted at index 'idx'.
2732 xfs_inode_t *ip, /* incore inode pointer */
2733 xfs_extnum_t idx, /* starting index of new items */
2734 xfs_extnum_t count, /* number of inserted items */
2735 xfs_bmbt_irec_t *new, /* items to insert */
2736 int state) /* type of extent conversion */
2738 xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
2739 xfs_extnum_t i; /* extent record index */
2741 trace_xfs_iext_insert(ip, idx, new, state, _RET_IP_);
2743 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2744 xfs_iext_add(ifp, idx, count);
2745 for (i = idx; i < idx + count; i++, new++)
2746 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, i), new);
2750 * This is called when the amount of space required for incore file
2751 * extents needs to be increased. The ext_diff parameter stores the
2752 * number of new extents being added and the idx parameter contains
2753 * the extent index where the new extents will be added. If the new
2754 * extents are being appended, then we just need to (re)allocate and
2755 * initialize the space. Otherwise, if the new extents are being
2756 * inserted into the middle of the existing entries, a bit more work
2757 * is required to make room for the new extents to be inserted. The
2758 * caller is responsible for filling in the new extent entries upon
2763 xfs_ifork_t *ifp, /* inode fork pointer */
2764 xfs_extnum_t idx, /* index to begin adding exts */
2765 int ext_diff) /* number of extents to add */
2767 int byte_diff; /* new bytes being added */
2768 int new_size; /* size of extents after adding */
2769 xfs_extnum_t nextents; /* number of extents in file */
2771 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
2772 ASSERT((idx >= 0) && (idx <= nextents));
2773 byte_diff = ext_diff * sizeof(xfs_bmbt_rec_t);
2774 new_size = ifp->if_bytes + byte_diff;
2776 * If the new number of extents (nextents + ext_diff)
2777 * fits inside the inode, then continue to use the inline
2780 if (nextents + ext_diff <= XFS_INLINE_EXTS) {
2781 if (idx < nextents) {
2782 memmove(&ifp->if_u2.if_inline_ext[idx + ext_diff],
2783 &ifp->if_u2.if_inline_ext[idx],
2784 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
2785 memset(&ifp->if_u2.if_inline_ext[idx], 0, byte_diff);
2787 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
2788 ifp->if_real_bytes = 0;
2791 * Otherwise use a linear (direct) extent list.
2792 * If the extents are currently inside the inode,
2793 * xfs_iext_realloc_direct will switch us from
2794 * inline to direct extent allocation mode.
2796 else if (nextents + ext_diff <= XFS_LINEAR_EXTS) {
2797 xfs_iext_realloc_direct(ifp, new_size);
2798 if (idx < nextents) {
2799 memmove(&ifp->if_u1.if_extents[idx + ext_diff],
2800 &ifp->if_u1.if_extents[idx],
2801 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
2802 memset(&ifp->if_u1.if_extents[idx], 0, byte_diff);
2805 /* Indirection array */
2807 xfs_ext_irec_t *erp;
2811 ASSERT(nextents + ext_diff > XFS_LINEAR_EXTS);
2812 if (ifp->if_flags & XFS_IFEXTIREC) {
2813 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 1);
2815 xfs_iext_irec_init(ifp);
2816 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
2817 erp = ifp->if_u1.if_ext_irec;
2819 /* Extents fit in target extent page */
2820 if (erp && erp->er_extcount + ext_diff <= XFS_LINEAR_EXTS) {
2821 if (page_idx < erp->er_extcount) {
2822 memmove(&erp->er_extbuf[page_idx + ext_diff],
2823 &erp->er_extbuf[page_idx],
2824 (erp->er_extcount - page_idx) *
2825 sizeof(xfs_bmbt_rec_t));
2826 memset(&erp->er_extbuf[page_idx], 0, byte_diff);
2828 erp->er_extcount += ext_diff;
2829 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
2831 /* Insert a new extent page */
2833 xfs_iext_add_indirect_multi(ifp,
2834 erp_idx, page_idx, ext_diff);
2837 * If extent(s) are being appended to the last page in
2838 * the indirection array and the new extent(s) don't fit
2839 * in the page, then erp is NULL and erp_idx is set to
2840 * the next index needed in the indirection array.
2843 int count = ext_diff;
2846 erp = xfs_iext_irec_new(ifp, erp_idx);
2847 erp->er_extcount = count;
2848 count -= MIN(count, (int)XFS_LINEAR_EXTS);
2855 ifp->if_bytes = new_size;
2859 * This is called when incore extents are being added to the indirection
2860 * array and the new extents do not fit in the target extent list. The
2861 * erp_idx parameter contains the irec index for the target extent list
2862 * in the indirection array, and the idx parameter contains the extent
2863 * index within the list. The number of extents being added is stored
2864 * in the count parameter.
2866 * |-------| |-------|
2867 * | | | | idx - number of extents before idx
2869 * | | | | count - number of extents being inserted at idx
2870 * |-------| |-------|
2871 * | count | | nex2 | nex2 - number of extents after idx + count
2872 * |-------| |-------|
2875 xfs_iext_add_indirect_multi(
2876 xfs_ifork_t *ifp, /* inode fork pointer */
2877 int erp_idx, /* target extent irec index */
2878 xfs_extnum_t idx, /* index within target list */
2879 int count) /* new extents being added */
2881 int byte_diff; /* new bytes being added */
2882 xfs_ext_irec_t *erp; /* pointer to irec entry */
2883 xfs_extnum_t ext_diff; /* number of extents to add */
2884 xfs_extnum_t ext_cnt; /* new extents still needed */
2885 xfs_extnum_t nex2; /* extents after idx + count */
2886 xfs_bmbt_rec_t *nex2_ep = NULL; /* temp list for nex2 extents */
2887 int nlists; /* number of irec's (lists) */
2889 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
2890 erp = &ifp->if_u1.if_ext_irec[erp_idx];
2891 nex2 = erp->er_extcount - idx;
2892 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
2895 * Save second part of target extent list
2896 * (all extents past */
2898 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
2899 nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_NOFS);
2900 memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff);
2901 erp->er_extcount -= nex2;
2902 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2);
2903 memset(&erp->er_extbuf[idx], 0, byte_diff);
2907 * Add the new extents to the end of the target
2908 * list, then allocate new irec record(s) and
2909 * extent buffer(s) as needed to store the rest
2910 * of the new extents.
2913 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount);
2915 erp->er_extcount += ext_diff;
2916 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
2917 ext_cnt -= ext_diff;
2921 erp = xfs_iext_irec_new(ifp, erp_idx);
2922 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS);
2923 erp->er_extcount = ext_diff;
2924 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
2925 ext_cnt -= ext_diff;
2928 /* Add nex2 extents back to indirection array */
2930 xfs_extnum_t ext_avail;
2933 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
2934 ext_avail = XFS_LINEAR_EXTS - erp->er_extcount;
2937 * If nex2 extents fit in the current page, append
2938 * nex2_ep after the new extents.
2940 if (nex2 <= ext_avail) {
2941 i = erp->er_extcount;
2944 * Otherwise, check if space is available in the
2947 else if ((erp_idx < nlists - 1) &&
2948 (nex2 <= (ext_avail = XFS_LINEAR_EXTS -
2949 ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) {
2952 /* Create a hole for nex2 extents */
2953 memmove(&erp->er_extbuf[nex2], erp->er_extbuf,
2954 erp->er_extcount * sizeof(xfs_bmbt_rec_t));
2957 * Final choice, create a new extent page for
2962 erp = xfs_iext_irec_new(ifp, erp_idx);
2964 memmove(&erp->er_extbuf[i], nex2_ep, byte_diff);
2966 erp->er_extcount += nex2;
2967 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2);
2972 * This is called when the amount of space required for incore file
2973 * extents needs to be decreased. The ext_diff parameter stores the
2974 * number of extents to be removed and the idx parameter contains
2975 * the extent index where the extents will be removed from.
2977 * If the amount of space needed has decreased below the linear
2978 * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
2979 * extent array. Otherwise, use kmem_realloc() to adjust the
2980 * size to what is needed.
2984 xfs_inode_t *ip, /* incore inode pointer */
2985 xfs_extnum_t idx, /* index to begin removing exts */
2986 int ext_diff, /* number of extents to remove */
2987 int state) /* type of extent conversion */
2989 xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
2990 xfs_extnum_t nextents; /* number of extents in file */
2991 int new_size; /* size of extents after removal */
2993 trace_xfs_iext_remove(ip, idx, state, _RET_IP_);
2995 ASSERT(ext_diff > 0);
2996 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
2997 new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t);
2999 if (new_size == 0) {
3000 xfs_iext_destroy(ifp);
3001 } else if (ifp->if_flags & XFS_IFEXTIREC) {
3002 xfs_iext_remove_indirect(ifp, idx, ext_diff);
3003 } else if (ifp->if_real_bytes) {
3004 xfs_iext_remove_direct(ifp, idx, ext_diff);
3006 xfs_iext_remove_inline(ifp, idx, ext_diff);
3008 ifp->if_bytes = new_size;
3012 * This removes ext_diff extents from the inline buffer, beginning
3013 * at extent index idx.
3016 xfs_iext_remove_inline(
3017 xfs_ifork_t *ifp, /* inode fork pointer */
3018 xfs_extnum_t idx, /* index to begin removing exts */
3019 int ext_diff) /* number of extents to remove */
3021 int nextents; /* number of extents in file */
3023 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
3024 ASSERT(idx < XFS_INLINE_EXTS);
3025 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3026 ASSERT(((nextents - ext_diff) > 0) &&
3027 (nextents - ext_diff) < XFS_INLINE_EXTS);
3029 if (idx + ext_diff < nextents) {
3030 memmove(&ifp->if_u2.if_inline_ext[idx],
3031 &ifp->if_u2.if_inline_ext[idx + ext_diff],
3032 (nextents - (idx + ext_diff)) *
3033 sizeof(xfs_bmbt_rec_t));
3034 memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff],
3035 0, ext_diff * sizeof(xfs_bmbt_rec_t));
3037 memset(&ifp->if_u2.if_inline_ext[idx], 0,
3038 ext_diff * sizeof(xfs_bmbt_rec_t));
3043 * This removes ext_diff extents from a linear (direct) extent list,
3044 * beginning at extent index idx. If the extents are being removed
3045 * from the end of the list (ie. truncate) then we just need to re-
3046 * allocate the list to remove the extra space. Otherwise, if the
3047 * extents are being removed from the middle of the existing extent
3048 * entries, then we first need to move the extent records beginning
3049 * at idx + ext_diff up in the list to overwrite the records being
3050 * removed, then remove the extra space via kmem_realloc.
3053 xfs_iext_remove_direct(
3054 xfs_ifork_t *ifp, /* inode fork pointer */
3055 xfs_extnum_t idx, /* index to begin removing exts */
3056 int ext_diff) /* number of extents to remove */
3058 xfs_extnum_t nextents; /* number of extents in file */
3059 int new_size; /* size of extents after removal */
3061 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
3062 new_size = ifp->if_bytes -
3063 (ext_diff * sizeof(xfs_bmbt_rec_t));
3064 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3066 if (new_size == 0) {
3067 xfs_iext_destroy(ifp);
3070 /* Move extents up in the list (if needed) */
3071 if (idx + ext_diff < nextents) {
3072 memmove(&ifp->if_u1.if_extents[idx],
3073 &ifp->if_u1.if_extents[idx + ext_diff],
3074 (nextents - (idx + ext_diff)) *
3075 sizeof(xfs_bmbt_rec_t));
3077 memset(&ifp->if_u1.if_extents[nextents - ext_diff],
3078 0, ext_diff * sizeof(xfs_bmbt_rec_t));
3080 * Reallocate the direct extent list. If the extents
3081 * will fit inside the inode then xfs_iext_realloc_direct
3082 * will switch from direct to inline extent allocation
3085 xfs_iext_realloc_direct(ifp, new_size);
3086 ifp->if_bytes = new_size;
3090 * This is called when incore extents are being removed from the
3091 * indirection array and the extents being removed span multiple extent
3092 * buffers. The idx parameter contains the file extent index where we
3093 * want to begin removing extents, and the count parameter contains
3094 * how many extents need to be removed.
3096 * |-------| |-------|
3097 * | nex1 | | | nex1 - number of extents before idx
3098 * |-------| | count |
3099 * | | | | count - number of extents being removed at idx
3100 * | count | |-------|
3101 * | | | nex2 | nex2 - number of extents after idx + count
3102 * |-------| |-------|
3105 xfs_iext_remove_indirect(
3106 xfs_ifork_t *ifp, /* inode fork pointer */
3107 xfs_extnum_t idx, /* index to begin removing extents */
3108 int count) /* number of extents to remove */
3110 xfs_ext_irec_t *erp; /* indirection array pointer */
3111 int erp_idx = 0; /* indirection array index */
3112 xfs_extnum_t ext_cnt; /* extents left to remove */
3113 xfs_extnum_t ext_diff; /* extents to remove in current list */
3114 xfs_extnum_t nex1; /* number of extents before idx */
3115 xfs_extnum_t nex2; /* extents after idx + count */
3116 int page_idx = idx; /* index in target extent list */
3118 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3119 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
3120 ASSERT(erp != NULL);
3124 nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0);
3125 ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1));
3127 * Check for deletion of entire list;
3128 * xfs_iext_irec_remove() updates extent offsets.
3130 if (ext_diff == erp->er_extcount) {
3131 xfs_iext_irec_remove(ifp, erp_idx);
3132 ext_cnt -= ext_diff;
3135 ASSERT(erp_idx < ifp->if_real_bytes /
3137 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3144 /* Move extents up (if needed) */
3146 memmove(&erp->er_extbuf[nex1],
3147 &erp->er_extbuf[nex1 + ext_diff],
3148 nex2 * sizeof(xfs_bmbt_rec_t));
3150 /* Zero out rest of page */
3151 memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ -
3152 ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t))));
3153 /* Update remaining counters */
3154 erp->er_extcount -= ext_diff;
3155 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff);
3156 ext_cnt -= ext_diff;
3161 ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t);
3162 xfs_iext_irec_compact(ifp);
3166 * Create, destroy, or resize a linear (direct) block of extents.
3169 xfs_iext_realloc_direct(
3170 xfs_ifork_t *ifp, /* inode fork pointer */
3171 int new_size) /* new size of extents */
3173 int rnew_size; /* real new size of extents */
3175 rnew_size = new_size;
3177 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) ||
3178 ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) &&
3179 (new_size != ifp->if_real_bytes)));
3181 /* Free extent records */
3182 if (new_size == 0) {
3183 xfs_iext_destroy(ifp);
3185 /* Resize direct extent list and zero any new bytes */
3186 else if (ifp->if_real_bytes) {
3187 /* Check if extents will fit inside the inode */
3188 if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) {
3189 xfs_iext_direct_to_inline(ifp, new_size /
3190 (uint)sizeof(xfs_bmbt_rec_t));
3191 ifp->if_bytes = new_size;
3194 if (!is_power_of_2(new_size)){
3195 rnew_size = roundup_pow_of_two(new_size);
3197 if (rnew_size != ifp->if_real_bytes) {
3198 ifp->if_u1.if_extents =
3199 kmem_realloc(ifp->if_u1.if_extents,
3201 ifp->if_real_bytes, KM_NOFS);
3203 if (rnew_size > ifp->if_real_bytes) {
3204 memset(&ifp->if_u1.if_extents[ifp->if_bytes /
3205 (uint)sizeof(xfs_bmbt_rec_t)], 0,
3206 rnew_size - ifp->if_real_bytes);
3210 * Switch from the inline extent buffer to a direct
3211 * extent list. Be sure to include the inline extent
3212 * bytes in new_size.
3215 new_size += ifp->if_bytes;
3216 if (!is_power_of_2(new_size)) {
3217 rnew_size = roundup_pow_of_two(new_size);
3219 xfs_iext_inline_to_direct(ifp, rnew_size);
3221 ifp->if_real_bytes = rnew_size;
3222 ifp->if_bytes = new_size;
3226 * Switch from linear (direct) extent records to inline buffer.
3229 xfs_iext_direct_to_inline(
3230 xfs_ifork_t *ifp, /* inode fork pointer */
3231 xfs_extnum_t nextents) /* number of extents in file */
3233 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
3234 ASSERT(nextents <= XFS_INLINE_EXTS);
3236 * The inline buffer was zeroed when we switched
3237 * from inline to direct extent allocation mode,
3238 * so we don't need to clear it here.
3240 memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents,
3241 nextents * sizeof(xfs_bmbt_rec_t));
3242 kmem_free(ifp->if_u1.if_extents);
3243 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
3244 ifp->if_real_bytes = 0;
3248 * Switch from inline buffer to linear (direct) extent records.
3249 * new_size should already be rounded up to the next power of 2
3250 * by the caller (when appropriate), so use new_size as it is.
3251 * However, since new_size may be rounded up, we can't update
3252 * if_bytes here. It is the caller's responsibility to update
3253 * if_bytes upon return.
3256 xfs_iext_inline_to_direct(
3257 xfs_ifork_t *ifp, /* inode fork pointer */
3258 int new_size) /* number of extents in file */
3260 ifp->if_u1.if_extents = kmem_alloc(new_size, KM_NOFS);
3261 memset(ifp->if_u1.if_extents, 0, new_size);
3262 if (ifp->if_bytes) {
3263 memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext,
3265 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
3266 sizeof(xfs_bmbt_rec_t));
3268 ifp->if_real_bytes = new_size;
3272 * Resize an extent indirection array to new_size bytes.
3275 xfs_iext_realloc_indirect(
3276 xfs_ifork_t *ifp, /* inode fork pointer */
3277 int new_size) /* new indirection array size */
3279 int nlists; /* number of irec's (ex lists) */
3280 int size; /* current indirection array size */
3282 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3283 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3284 size = nlists * sizeof(xfs_ext_irec_t);
3285 ASSERT(ifp->if_real_bytes);
3286 ASSERT((new_size >= 0) && (new_size != size));
3287 if (new_size == 0) {
3288 xfs_iext_destroy(ifp);
3290 ifp->if_u1.if_ext_irec = (xfs_ext_irec_t *)
3291 kmem_realloc(ifp->if_u1.if_ext_irec,
3292 new_size, size, KM_NOFS);
3297 * Switch from indirection array to linear (direct) extent allocations.
3300 xfs_iext_indirect_to_direct(
3301 xfs_ifork_t *ifp) /* inode fork pointer */
3303 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3304 xfs_extnum_t nextents; /* number of extents in file */
3305 int size; /* size of file extents */
3307 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3308 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3309 ASSERT(nextents <= XFS_LINEAR_EXTS);
3310 size = nextents * sizeof(xfs_bmbt_rec_t);
3312 xfs_iext_irec_compact_pages(ifp);
3313 ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ);
3315 ep = ifp->if_u1.if_ext_irec->er_extbuf;
3316 kmem_free(ifp->if_u1.if_ext_irec);
3317 ifp->if_flags &= ~XFS_IFEXTIREC;
3318 ifp->if_u1.if_extents = ep;
3319 ifp->if_bytes = size;
3320 if (nextents < XFS_LINEAR_EXTS) {
3321 xfs_iext_realloc_direct(ifp, size);
3326 * Free incore file extents.
3330 xfs_ifork_t *ifp) /* inode fork pointer */
3332 if (ifp->if_flags & XFS_IFEXTIREC) {
3336 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3337 for (erp_idx = nlists - 1; erp_idx >= 0 ; erp_idx--) {
3338 xfs_iext_irec_remove(ifp, erp_idx);
3340 ifp->if_flags &= ~XFS_IFEXTIREC;
3341 } else if (ifp->if_real_bytes) {
3342 kmem_free(ifp->if_u1.if_extents);
3343 } else if (ifp->if_bytes) {
3344 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
3345 sizeof(xfs_bmbt_rec_t));
3347 ifp->if_u1.if_extents = NULL;
3348 ifp->if_real_bytes = 0;
3353 * Return a pointer to the extent record for file system block bno.
3355 xfs_bmbt_rec_host_t * /* pointer to found extent record */
3356 xfs_iext_bno_to_ext(
3357 xfs_ifork_t *ifp, /* inode fork pointer */
3358 xfs_fileoff_t bno, /* block number to search for */
3359 xfs_extnum_t *idxp) /* index of target extent */
3361 xfs_bmbt_rec_host_t *base; /* pointer to first extent */
3362 xfs_filblks_t blockcount = 0; /* number of blocks in extent */
3363 xfs_bmbt_rec_host_t *ep = NULL; /* pointer to target extent */
3364 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
3365 int high; /* upper boundary in search */
3366 xfs_extnum_t idx = 0; /* index of target extent */
3367 int low; /* lower boundary in search */
3368 xfs_extnum_t nextents; /* number of file extents */
3369 xfs_fileoff_t startoff = 0; /* start offset of extent */
3371 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3372 if (nextents == 0) {
3377 if (ifp->if_flags & XFS_IFEXTIREC) {
3378 /* Find target extent list */
3380 erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx);
3381 base = erp->er_extbuf;
3382 high = erp->er_extcount - 1;
3384 base = ifp->if_u1.if_extents;
3385 high = nextents - 1;
3387 /* Binary search extent records */
3388 while (low <= high) {
3389 idx = (low + high) >> 1;
3391 startoff = xfs_bmbt_get_startoff(ep);
3392 blockcount = xfs_bmbt_get_blockcount(ep);
3393 if (bno < startoff) {
3395 } else if (bno >= startoff + blockcount) {
3398 /* Convert back to file-based extent index */
3399 if (ifp->if_flags & XFS_IFEXTIREC) {
3400 idx += erp->er_extoff;
3406 /* Convert back to file-based extent index */
3407 if (ifp->if_flags & XFS_IFEXTIREC) {
3408 idx += erp->er_extoff;
3410 if (bno >= startoff + blockcount) {
3411 if (++idx == nextents) {
3414 ep = xfs_iext_get_ext(ifp, idx);
3422 * Return a pointer to the indirection array entry containing the
3423 * extent record for filesystem block bno. Store the index of the
3424 * target irec in *erp_idxp.
3426 xfs_ext_irec_t * /* pointer to found extent record */
3427 xfs_iext_bno_to_irec(
3428 xfs_ifork_t *ifp, /* inode fork pointer */
3429 xfs_fileoff_t bno, /* block number to search for */
3430 int *erp_idxp) /* irec index of target ext list */
3432 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
3433 xfs_ext_irec_t *erp_next; /* next indirection array entry */
3434 int erp_idx; /* indirection array index */
3435 int nlists; /* number of extent irec's (lists) */
3436 int high; /* binary search upper limit */
3437 int low; /* binary search lower limit */
3439 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3440 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3444 while (low <= high) {
3445 erp_idx = (low + high) >> 1;
3446 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3447 erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL;
3448 if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) {
3450 } else if (erp_next && bno >=
3451 xfs_bmbt_get_startoff(erp_next->er_extbuf)) {
3457 *erp_idxp = erp_idx;
3462 * Return a pointer to the indirection array entry containing the
3463 * extent record at file extent index *idxp. Store the index of the
3464 * target irec in *erp_idxp and store the page index of the target
3465 * extent record in *idxp.
3468 xfs_iext_idx_to_irec(
3469 xfs_ifork_t *ifp, /* inode fork pointer */
3470 xfs_extnum_t *idxp, /* extent index (file -> page) */
3471 int *erp_idxp, /* pointer to target irec */
3472 int realloc) /* new bytes were just added */
3474 xfs_ext_irec_t *prev; /* pointer to previous irec */
3475 xfs_ext_irec_t *erp = NULL; /* pointer to current irec */
3476 int erp_idx; /* indirection array index */
3477 int nlists; /* number of irec's (ex lists) */
3478 int high; /* binary search upper limit */
3479 int low; /* binary search lower limit */
3480 xfs_extnum_t page_idx = *idxp; /* extent index in target list */
3482 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3483 ASSERT(page_idx >= 0);
3484 ASSERT(page_idx <= ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
3485 ASSERT(page_idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t) || realloc);
3487 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3492 /* Binary search extent irec's */
3493 while (low <= high) {
3494 erp_idx = (low + high) >> 1;
3495 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3496 prev = erp_idx > 0 ? erp - 1 : NULL;
3497 if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff &&
3498 realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) {
3500 } else if (page_idx > erp->er_extoff + erp->er_extcount ||
3501 (page_idx == erp->er_extoff + erp->er_extcount &&
3504 } else if (page_idx == erp->er_extoff + erp->er_extcount &&
3505 erp->er_extcount == XFS_LINEAR_EXTS) {
3509 erp = erp_idx < nlists ? erp + 1 : NULL;
3512 page_idx -= erp->er_extoff;
3517 *erp_idxp = erp_idx;
3522 * Allocate and initialize an indirection array once the space needed
3523 * for incore extents increases above XFS_IEXT_BUFSZ.
3527 xfs_ifork_t *ifp) /* inode fork pointer */
3529 xfs_ext_irec_t *erp; /* indirection array pointer */
3530 xfs_extnum_t nextents; /* number of extents in file */
3532 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
3533 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3534 ASSERT(nextents <= XFS_LINEAR_EXTS);
3536 erp = kmem_alloc(sizeof(xfs_ext_irec_t), KM_NOFS);
3538 if (nextents == 0) {
3539 ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
3540 } else if (!ifp->if_real_bytes) {
3541 xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ);
3542 } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) {
3543 xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ);
3545 erp->er_extbuf = ifp->if_u1.if_extents;
3546 erp->er_extcount = nextents;
3549 ifp->if_flags |= XFS_IFEXTIREC;
3550 ifp->if_real_bytes = XFS_IEXT_BUFSZ;
3551 ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t);
3552 ifp->if_u1.if_ext_irec = erp;
3558 * Allocate and initialize a new entry in the indirection array.
3562 xfs_ifork_t *ifp, /* inode fork pointer */
3563 int erp_idx) /* index for new irec */
3565 xfs_ext_irec_t *erp; /* indirection array pointer */
3566 int i; /* loop counter */
3567 int nlists; /* number of irec's (ex lists) */
3569 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3570 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3572 /* Resize indirection array */
3573 xfs_iext_realloc_indirect(ifp, ++nlists *
3574 sizeof(xfs_ext_irec_t));
3576 * Move records down in the array so the
3577 * new page can use erp_idx.
3579 erp = ifp->if_u1.if_ext_irec;
3580 for (i = nlists - 1; i > erp_idx; i--) {
3581 memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t));
3583 ASSERT(i == erp_idx);
3585 /* Initialize new extent record */
3586 erp = ifp->if_u1.if_ext_irec;
3587 erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
3588 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
3589 memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ);
3590 erp[erp_idx].er_extcount = 0;
3591 erp[erp_idx].er_extoff = erp_idx > 0 ?
3592 erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0;
3593 return (&erp[erp_idx]);
3597 * Remove a record from the indirection array.
3600 xfs_iext_irec_remove(
3601 xfs_ifork_t *ifp, /* inode fork pointer */
3602 int erp_idx) /* irec index to remove */
3604 xfs_ext_irec_t *erp; /* indirection array pointer */
3605 int i; /* loop counter */
3606 int nlists; /* number of irec's (ex lists) */
3608 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3609 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3610 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3611 if (erp->er_extbuf) {
3612 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1,
3614 kmem_free(erp->er_extbuf);
3616 /* Compact extent records */
3617 erp = ifp->if_u1.if_ext_irec;
3618 for (i = erp_idx; i < nlists - 1; i++) {
3619 memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t));
3622 * Manually free the last extent record from the indirection
3623 * array. A call to xfs_iext_realloc_indirect() with a size
3624 * of zero would result in a call to xfs_iext_destroy() which
3625 * would in turn call this function again, creating a nasty
3629 xfs_iext_realloc_indirect(ifp,
3630 nlists * sizeof(xfs_ext_irec_t));
3632 kmem_free(ifp->if_u1.if_ext_irec);
3634 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
3638 * This is called to clean up large amounts of unused memory allocated
3639 * by the indirection array. Before compacting anything though, verify
3640 * that the indirection array is still needed and switch back to the
3641 * linear extent list (or even the inline buffer) if possible. The
3642 * compaction policy is as follows:
3644 * Full Compaction: Extents fit into a single page (or inline buffer)
3645 * Partial Compaction: Extents occupy less than 50% of allocated space
3646 * No Compaction: Extents occupy at least 50% of allocated space
3649 xfs_iext_irec_compact(
3650 xfs_ifork_t *ifp) /* inode fork pointer */
3652 xfs_extnum_t nextents; /* number of extents in file */
3653 int nlists; /* number of irec's (ex lists) */
3655 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3656 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3657 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3659 if (nextents == 0) {
3660 xfs_iext_destroy(ifp);
3661 } else if (nextents <= XFS_INLINE_EXTS) {
3662 xfs_iext_indirect_to_direct(ifp);
3663 xfs_iext_direct_to_inline(ifp, nextents);
3664 } else if (nextents <= XFS_LINEAR_EXTS) {
3665 xfs_iext_indirect_to_direct(ifp);
3666 } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) {
3667 xfs_iext_irec_compact_pages(ifp);
3672 * Combine extents from neighboring extent pages.
3675 xfs_iext_irec_compact_pages(
3676 xfs_ifork_t *ifp) /* inode fork pointer */
3678 xfs_ext_irec_t *erp, *erp_next;/* pointers to irec entries */
3679 int erp_idx = 0; /* indirection array index */
3680 int nlists; /* number of irec's (ex lists) */
3682 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3683 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3684 while (erp_idx < nlists - 1) {
3685 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3687 if (erp_next->er_extcount <=
3688 (XFS_LINEAR_EXTS - erp->er_extcount)) {
3689 memcpy(&erp->er_extbuf[erp->er_extcount],
3690 erp_next->er_extbuf, erp_next->er_extcount *
3691 sizeof(xfs_bmbt_rec_t));
3692 erp->er_extcount += erp_next->er_extcount;
3694 * Free page before removing extent record
3695 * so er_extoffs don't get modified in
3696 * xfs_iext_irec_remove.
3698 kmem_free(erp_next->er_extbuf);
3699 erp_next->er_extbuf = NULL;
3700 xfs_iext_irec_remove(ifp, erp_idx + 1);
3701 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3709 * This is called to update the er_extoff field in the indirection
3710 * array when extents have been added or removed from one of the
3711 * extent lists. erp_idx contains the irec index to begin updating
3712 * at and ext_diff contains the number of extents that were added
3716 xfs_iext_irec_update_extoffs(
3717 xfs_ifork_t *ifp, /* inode fork pointer */
3718 int erp_idx, /* irec index to update */
3719 int ext_diff) /* number of new extents */
3721 int i; /* loop counter */
3722 int nlists; /* number of irec's (ex lists */
3724 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3725 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3726 for (i = erp_idx; i < nlists; i++) {
3727 ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff;