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"
26 #include "xfs_trans.h"
27 #include "xfs_trans_priv.h"
30 #include "xfs_mount.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_alloc_btree.h"
33 #include "xfs_ialloc_btree.h"
34 #include "xfs_attr_sf.h"
35 #include "xfs_dinode.h"
36 #include "xfs_inode.h"
37 #include "xfs_buf_item.h"
38 #include "xfs_inode_item.h"
39 #include "xfs_btree.h"
40 #include "xfs_alloc.h"
41 #include "xfs_ialloc.h"
43 #include "xfs_error.h"
44 #include "xfs_utils.h"
45 #include "xfs_quota.h"
46 #include "xfs_filestream.h"
47 #include "xfs_vnodeops.h"
48 #include "xfs_trace.h"
50 kmem_zone_t *xfs_ifork_zone;
51 kmem_zone_t *xfs_inode_zone;
54 * Used in xfs_itruncate_extents(). This is the maximum number of extents
55 * freed from a file in a single transaction.
57 #define XFS_ITRUNC_MAX_EXTENTS 2
59 STATIC int xfs_iflush_int(xfs_inode_t *, xfs_buf_t *);
60 STATIC int xfs_iformat_local(xfs_inode_t *, xfs_dinode_t *, int, int);
61 STATIC int xfs_iformat_extents(xfs_inode_t *, xfs_dinode_t *, int);
62 STATIC int xfs_iformat_btree(xfs_inode_t *, xfs_dinode_t *, int);
66 * Make sure that the extents in the given memory buffer
76 xfs_bmbt_rec_host_t rec;
79 for (i = 0; i < nrecs; i++) {
80 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
81 rec.l0 = get_unaligned(&ep->l0);
82 rec.l1 = get_unaligned(&ep->l1);
83 xfs_bmbt_get_all(&rec, &irec);
84 if (fmt == XFS_EXTFMT_NOSTATE)
85 ASSERT(irec.br_state == XFS_EXT_NORM);
89 #define xfs_validate_extents(ifp, nrecs, fmt)
93 * Check that none of the inode's in the buffer have a next
94 * unlinked field of 0.
106 j = mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog;
108 for (i = 0; i < j; i++) {
109 dip = (xfs_dinode_t *)xfs_buf_offset(bp,
110 i * mp->m_sb.sb_inodesize);
111 if (!dip->di_next_unlinked) {
113 "Detected bogus zero next_unlinked field in incore inode buffer 0x%p.",
115 ASSERT(dip->di_next_unlinked);
122 * Find the buffer associated with the given inode map
123 * We do basic validation checks on the buffer once it has been
124 * retrieved from disk.
130 struct xfs_imap *imap,
140 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, imap->im_blkno,
141 (int)imap->im_len, buf_flags, &bp);
143 if (error != EAGAIN) {
145 "%s: xfs_trans_read_buf() returned error %d.",
148 ASSERT(buf_flags & XBF_TRYLOCK);
154 * Validate the magic number and version of every inode in the buffer
155 * (if DEBUG kernel) or the first inode in the buffer, otherwise.
158 ni = BBTOB(imap->im_len) >> mp->m_sb.sb_inodelog;
159 #else /* usual case */
163 for (i = 0; i < ni; i++) {
167 dip = (xfs_dinode_t *)xfs_buf_offset(bp,
168 (i << mp->m_sb.sb_inodelog));
169 di_ok = dip->di_magic == cpu_to_be16(XFS_DINODE_MAGIC) &&
170 XFS_DINODE_GOOD_VERSION(dip->di_version);
171 if (unlikely(XFS_TEST_ERROR(!di_ok, mp,
172 XFS_ERRTAG_ITOBP_INOTOBP,
173 XFS_RANDOM_ITOBP_INOTOBP))) {
174 if (iget_flags & XFS_IGET_UNTRUSTED) {
175 xfs_trans_brelse(tp, bp);
176 return XFS_ERROR(EINVAL);
178 XFS_CORRUPTION_ERROR("xfs_imap_to_bp",
179 XFS_ERRLEVEL_HIGH, mp, dip);
182 "bad inode magic/vsn daddr %lld #%d (magic=%x)",
183 (unsigned long long)imap->im_blkno, i,
184 be16_to_cpu(dip->di_magic));
187 xfs_trans_brelse(tp, bp);
188 return XFS_ERROR(EFSCORRUPTED);
192 xfs_inobp_check(mp, bp);
198 * This routine is called to map an inode number within a file
199 * system to the buffer containing the on-disk version of the
200 * inode. It returns a pointer to the buffer containing the
201 * on-disk inode in the bpp parameter, and in the dip parameter
202 * it returns a pointer to the on-disk inode within that buffer.
204 * If a non-zero error is returned, then the contents of bpp and
205 * dipp are undefined.
207 * Use xfs_imap() to determine the size and location of the
208 * buffer to read from disk.
220 struct xfs_imap imap;
225 error = xfs_imap(mp, tp, ino, &imap, imap_flags);
229 error = xfs_imap_to_bp(mp, tp, &imap, &bp, XBF_LOCK, imap_flags);
233 *dipp = (xfs_dinode_t *)xfs_buf_offset(bp, imap.im_boffset);
235 *offset = imap.im_boffset;
241 * This routine is called to map an inode to the buffer containing
242 * the on-disk version of the inode. It returns a pointer to the
243 * buffer containing the on-disk inode in the bpp parameter, and in
244 * the dip parameter it returns a pointer to the on-disk inode within
247 * If a non-zero error is returned, then the contents of bpp and
248 * dipp are undefined.
250 * The inode is expected to already been mapped to its buffer and read
251 * in once, thus we can use the mapping information stored in the inode
252 * rather than calling xfs_imap(). This allows us to avoid the overhead
253 * of looking at the inode btree for small block file systems
268 ASSERT(ip->i_imap.im_blkno != 0);
270 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &bp, buf_flags, 0);
275 ASSERT(buf_flags & XBF_TRYLOCK);
281 *dipp = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
287 * Move inode type and inode format specific information from the
288 * on-disk inode to the in-core inode. For fifos, devs, and sockets
289 * this means set if_rdev to the proper value. For files, directories,
290 * and symlinks this means to bring in the in-line data or extent
291 * pointers. For a file in B-tree format, only the root is immediately
292 * brought in-core. The rest will be in-lined in if_extents when it
293 * is first referenced (see xfs_iread_extents()).
300 xfs_attr_shortform_t *atp;
305 if (unlikely(be32_to_cpu(dip->di_nextents) +
306 be16_to_cpu(dip->di_anextents) >
307 be64_to_cpu(dip->di_nblocks))) {
308 xfs_warn(ip->i_mount,
309 "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.",
310 (unsigned long long)ip->i_ino,
311 (int)(be32_to_cpu(dip->di_nextents) +
312 be16_to_cpu(dip->di_anextents)),
314 be64_to_cpu(dip->di_nblocks));
315 XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW,
317 return XFS_ERROR(EFSCORRUPTED);
320 if (unlikely(dip->di_forkoff > ip->i_mount->m_sb.sb_inodesize)) {
321 xfs_warn(ip->i_mount, "corrupt dinode %Lu, forkoff = 0x%x.",
322 (unsigned long long)ip->i_ino,
324 XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW,
326 return XFS_ERROR(EFSCORRUPTED);
329 if (unlikely((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) &&
330 !ip->i_mount->m_rtdev_targp)) {
331 xfs_warn(ip->i_mount,
332 "corrupt dinode %Lu, has realtime flag set.",
334 XFS_CORRUPTION_ERROR("xfs_iformat(realtime)",
335 XFS_ERRLEVEL_LOW, ip->i_mount, dip);
336 return XFS_ERROR(EFSCORRUPTED);
339 switch (ip->i_d.di_mode & S_IFMT) {
344 if (unlikely(dip->di_format != XFS_DINODE_FMT_DEV)) {
345 XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW,
347 return XFS_ERROR(EFSCORRUPTED);
351 ip->i_df.if_u2.if_rdev = xfs_dinode_get_rdev(dip);
357 switch (dip->di_format) {
358 case XFS_DINODE_FMT_LOCAL:
360 * no local regular files yet
362 if (unlikely(S_ISREG(be16_to_cpu(dip->di_mode)))) {
363 xfs_warn(ip->i_mount,
364 "corrupt inode %Lu (local format for regular file).",
365 (unsigned long long) ip->i_ino);
366 XFS_CORRUPTION_ERROR("xfs_iformat(4)",
369 return XFS_ERROR(EFSCORRUPTED);
372 di_size = be64_to_cpu(dip->di_size);
373 if (unlikely(di_size > XFS_DFORK_DSIZE(dip, ip->i_mount))) {
374 xfs_warn(ip->i_mount,
375 "corrupt inode %Lu (bad size %Ld for local inode).",
376 (unsigned long long) ip->i_ino,
377 (long long) di_size);
378 XFS_CORRUPTION_ERROR("xfs_iformat(5)",
381 return XFS_ERROR(EFSCORRUPTED);
385 error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size);
387 case XFS_DINODE_FMT_EXTENTS:
388 error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
390 case XFS_DINODE_FMT_BTREE:
391 error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
394 XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW,
396 return XFS_ERROR(EFSCORRUPTED);
401 XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW, ip->i_mount);
402 return XFS_ERROR(EFSCORRUPTED);
407 if (!XFS_DFORK_Q(dip))
410 ASSERT(ip->i_afp == NULL);
411 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP | KM_NOFS);
413 switch (dip->di_aformat) {
414 case XFS_DINODE_FMT_LOCAL:
415 atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
416 size = be16_to_cpu(atp->hdr.totsize);
418 if (unlikely(size < sizeof(struct xfs_attr_sf_hdr))) {
419 xfs_warn(ip->i_mount,
420 "corrupt inode %Lu (bad attr fork size %Ld).",
421 (unsigned long long) ip->i_ino,
423 XFS_CORRUPTION_ERROR("xfs_iformat(8)",
426 return XFS_ERROR(EFSCORRUPTED);
429 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size);
431 case XFS_DINODE_FMT_EXTENTS:
432 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
434 case XFS_DINODE_FMT_BTREE:
435 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
438 error = XFS_ERROR(EFSCORRUPTED);
442 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
444 xfs_idestroy_fork(ip, XFS_DATA_FORK);
450 * The file is in-lined in the on-disk inode.
451 * If it fits into if_inline_data, then copy
452 * it there, otherwise allocate a buffer for it
453 * and copy the data there. Either way, set
454 * if_data to point at the data.
455 * If we allocate a buffer for the data, make
456 * sure that its size is a multiple of 4 and
457 * record the real size in i_real_bytes.
470 * If the size is unreasonable, then something
471 * is wrong and we just bail out rather than crash in
472 * kmem_alloc() or memcpy() below.
474 if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
475 xfs_warn(ip->i_mount,
476 "corrupt inode %Lu (bad size %d for local fork, size = %d).",
477 (unsigned long long) ip->i_ino, size,
478 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
479 XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW,
481 return XFS_ERROR(EFSCORRUPTED);
483 ifp = XFS_IFORK_PTR(ip, whichfork);
486 ifp->if_u1.if_data = NULL;
487 else if (size <= sizeof(ifp->if_u2.if_inline_data))
488 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
490 real_size = roundup(size, 4);
491 ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP | KM_NOFS);
493 ifp->if_bytes = size;
494 ifp->if_real_bytes = real_size;
496 memcpy(ifp->if_u1.if_data, XFS_DFORK_PTR(dip, whichfork), size);
497 ifp->if_flags &= ~XFS_IFEXTENTS;
498 ifp->if_flags |= XFS_IFINLINE;
503 * The file consists of a set of extents all
504 * of which fit into the on-disk inode.
505 * If there are few enough extents to fit into
506 * the if_inline_ext, then copy them there.
507 * Otherwise allocate a buffer for them and copy
508 * them into it. Either way, set if_extents
509 * to point at the extents.
523 ifp = XFS_IFORK_PTR(ip, whichfork);
524 nex = XFS_DFORK_NEXTENTS(dip, whichfork);
525 size = nex * (uint)sizeof(xfs_bmbt_rec_t);
528 * If the number of extents is unreasonable, then something
529 * is wrong and we just bail out rather than crash in
530 * kmem_alloc() or memcpy() below.
532 if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
533 xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).",
534 (unsigned long long) ip->i_ino, nex);
535 XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW,
537 return XFS_ERROR(EFSCORRUPTED);
540 ifp->if_real_bytes = 0;
542 ifp->if_u1.if_extents = NULL;
543 else if (nex <= XFS_INLINE_EXTS)
544 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
546 xfs_iext_add(ifp, 0, nex);
548 ifp->if_bytes = size;
550 dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
551 xfs_validate_extents(ifp, nex, XFS_EXTFMT_INODE(ip));
552 for (i = 0; i < nex; i++, dp++) {
553 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
554 ep->l0 = get_unaligned_be64(&dp->l0);
555 ep->l1 = get_unaligned_be64(&dp->l1);
557 XFS_BMAP_TRACE_EXLIST(ip, nex, whichfork);
558 if (whichfork != XFS_DATA_FORK ||
559 XFS_EXTFMT_INODE(ip) == XFS_EXTFMT_NOSTATE)
560 if (unlikely(xfs_check_nostate_extents(
562 XFS_ERROR_REPORT("xfs_iformat_extents(2)",
565 return XFS_ERROR(EFSCORRUPTED);
568 ifp->if_flags |= XFS_IFEXTENTS;
573 * The file has too many extents to fit into
574 * the inode, so they are in B-tree format.
575 * Allocate a buffer for the root of the B-tree
576 * and copy the root into it. The i_extents
577 * field will remain NULL until all of the
578 * extents are read in (when they are needed).
586 xfs_bmdr_block_t *dfp;
592 ifp = XFS_IFORK_PTR(ip, whichfork);
593 dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
594 size = XFS_BMAP_BROOT_SPACE(dfp);
595 nrecs = be16_to_cpu(dfp->bb_numrecs);
598 * blow out if -- fork has less extents than can fit in
599 * fork (fork shouldn't be a btree format), root btree
600 * block has more records than can fit into the fork,
601 * or the number of extents is greater than the number of
604 if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <=
605 XFS_IFORK_MAXEXT(ip, whichfork) ||
606 XFS_BMDR_SPACE_CALC(nrecs) >
607 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork) ||
608 XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks)) {
609 xfs_warn(ip->i_mount, "corrupt inode %Lu (btree).",
610 (unsigned long long) ip->i_ino);
611 XFS_CORRUPTION_ERROR("xfs_iformat_btree", XFS_ERRLEVEL_LOW,
613 return XFS_ERROR(EFSCORRUPTED);
616 ifp->if_broot_bytes = size;
617 ifp->if_broot = kmem_alloc(size, KM_SLEEP | KM_NOFS);
618 ASSERT(ifp->if_broot != NULL);
620 * Copy and convert from the on-disk structure
621 * to the in-memory structure.
623 xfs_bmdr_to_bmbt(ip->i_mount, dfp,
624 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
625 ifp->if_broot, size);
626 ifp->if_flags &= ~XFS_IFEXTENTS;
627 ifp->if_flags |= XFS_IFBROOT;
633 xfs_dinode_from_disk(
637 to->di_magic = be16_to_cpu(from->di_magic);
638 to->di_mode = be16_to_cpu(from->di_mode);
639 to->di_version = from ->di_version;
640 to->di_format = from->di_format;
641 to->di_onlink = be16_to_cpu(from->di_onlink);
642 to->di_uid = be32_to_cpu(from->di_uid);
643 to->di_gid = be32_to_cpu(from->di_gid);
644 to->di_nlink = be32_to_cpu(from->di_nlink);
645 to->di_projid_lo = be16_to_cpu(from->di_projid_lo);
646 to->di_projid_hi = be16_to_cpu(from->di_projid_hi);
647 memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
648 to->di_flushiter = be16_to_cpu(from->di_flushiter);
649 to->di_atime.t_sec = be32_to_cpu(from->di_atime.t_sec);
650 to->di_atime.t_nsec = be32_to_cpu(from->di_atime.t_nsec);
651 to->di_mtime.t_sec = be32_to_cpu(from->di_mtime.t_sec);
652 to->di_mtime.t_nsec = be32_to_cpu(from->di_mtime.t_nsec);
653 to->di_ctime.t_sec = be32_to_cpu(from->di_ctime.t_sec);
654 to->di_ctime.t_nsec = be32_to_cpu(from->di_ctime.t_nsec);
655 to->di_size = be64_to_cpu(from->di_size);
656 to->di_nblocks = be64_to_cpu(from->di_nblocks);
657 to->di_extsize = be32_to_cpu(from->di_extsize);
658 to->di_nextents = be32_to_cpu(from->di_nextents);
659 to->di_anextents = be16_to_cpu(from->di_anextents);
660 to->di_forkoff = from->di_forkoff;
661 to->di_aformat = from->di_aformat;
662 to->di_dmevmask = be32_to_cpu(from->di_dmevmask);
663 to->di_dmstate = be16_to_cpu(from->di_dmstate);
664 to->di_flags = be16_to_cpu(from->di_flags);
665 to->di_gen = be32_to_cpu(from->di_gen);
671 xfs_icdinode_t *from)
673 to->di_magic = cpu_to_be16(from->di_magic);
674 to->di_mode = cpu_to_be16(from->di_mode);
675 to->di_version = from ->di_version;
676 to->di_format = from->di_format;
677 to->di_onlink = cpu_to_be16(from->di_onlink);
678 to->di_uid = cpu_to_be32(from->di_uid);
679 to->di_gid = cpu_to_be32(from->di_gid);
680 to->di_nlink = cpu_to_be32(from->di_nlink);
681 to->di_projid_lo = cpu_to_be16(from->di_projid_lo);
682 to->di_projid_hi = cpu_to_be16(from->di_projid_hi);
683 memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
684 to->di_flushiter = cpu_to_be16(from->di_flushiter);
685 to->di_atime.t_sec = cpu_to_be32(from->di_atime.t_sec);
686 to->di_atime.t_nsec = cpu_to_be32(from->di_atime.t_nsec);
687 to->di_mtime.t_sec = cpu_to_be32(from->di_mtime.t_sec);
688 to->di_mtime.t_nsec = cpu_to_be32(from->di_mtime.t_nsec);
689 to->di_ctime.t_sec = cpu_to_be32(from->di_ctime.t_sec);
690 to->di_ctime.t_nsec = cpu_to_be32(from->di_ctime.t_nsec);
691 to->di_size = cpu_to_be64(from->di_size);
692 to->di_nblocks = cpu_to_be64(from->di_nblocks);
693 to->di_extsize = cpu_to_be32(from->di_extsize);
694 to->di_nextents = cpu_to_be32(from->di_nextents);
695 to->di_anextents = cpu_to_be16(from->di_anextents);
696 to->di_forkoff = from->di_forkoff;
697 to->di_aformat = from->di_aformat;
698 to->di_dmevmask = cpu_to_be32(from->di_dmevmask);
699 to->di_dmstate = cpu_to_be16(from->di_dmstate);
700 to->di_flags = cpu_to_be16(from->di_flags);
701 to->di_gen = cpu_to_be32(from->di_gen);
710 if (di_flags & XFS_DIFLAG_ANY) {
711 if (di_flags & XFS_DIFLAG_REALTIME)
712 flags |= XFS_XFLAG_REALTIME;
713 if (di_flags & XFS_DIFLAG_PREALLOC)
714 flags |= XFS_XFLAG_PREALLOC;
715 if (di_flags & XFS_DIFLAG_IMMUTABLE)
716 flags |= XFS_XFLAG_IMMUTABLE;
717 if (di_flags & XFS_DIFLAG_APPEND)
718 flags |= XFS_XFLAG_APPEND;
719 if (di_flags & XFS_DIFLAG_SYNC)
720 flags |= XFS_XFLAG_SYNC;
721 if (di_flags & XFS_DIFLAG_NOATIME)
722 flags |= XFS_XFLAG_NOATIME;
723 if (di_flags & XFS_DIFLAG_NODUMP)
724 flags |= XFS_XFLAG_NODUMP;
725 if (di_flags & XFS_DIFLAG_RTINHERIT)
726 flags |= XFS_XFLAG_RTINHERIT;
727 if (di_flags & XFS_DIFLAG_PROJINHERIT)
728 flags |= XFS_XFLAG_PROJINHERIT;
729 if (di_flags & XFS_DIFLAG_NOSYMLINKS)
730 flags |= XFS_XFLAG_NOSYMLINKS;
731 if (di_flags & XFS_DIFLAG_EXTSIZE)
732 flags |= XFS_XFLAG_EXTSIZE;
733 if (di_flags & XFS_DIFLAG_EXTSZINHERIT)
734 flags |= XFS_XFLAG_EXTSZINHERIT;
735 if (di_flags & XFS_DIFLAG_NODEFRAG)
736 flags |= XFS_XFLAG_NODEFRAG;
737 if (di_flags & XFS_DIFLAG_FILESTREAM)
738 flags |= XFS_XFLAG_FILESTREAM;
748 xfs_icdinode_t *dic = &ip->i_d;
750 return _xfs_dic2xflags(dic->di_flags) |
751 (XFS_IFORK_Q(ip) ? XFS_XFLAG_HASATTR : 0);
758 return _xfs_dic2xflags(be16_to_cpu(dip->di_flags)) |
759 (XFS_DFORK_Q(dip) ? XFS_XFLAG_HASATTR : 0);
763 * Read the disk inode attributes into the in-core inode structure.
777 * Fill in the location information in the in-core inode.
779 error = xfs_imap(mp, tp, ip->i_ino, &ip->i_imap, iget_flags);
784 * Get pointers to the on-disk inode and the buffer containing it.
786 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &bp,
787 XBF_LOCK, iget_flags);
790 dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
793 * If we got something that isn't an inode it means someone
794 * (nfs or dmi) has a stale handle.
796 if (dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC)) {
799 "%s: dip->di_magic (0x%x) != XFS_DINODE_MAGIC (0x%x)",
800 __func__, be16_to_cpu(dip->di_magic), XFS_DINODE_MAGIC);
802 error = XFS_ERROR(EINVAL);
807 * If the on-disk inode is already linked to a directory
808 * entry, copy all of the inode into the in-core inode.
809 * xfs_iformat() handles copying in the inode format
810 * specific information.
811 * Otherwise, just get the truly permanent information.
814 xfs_dinode_from_disk(&ip->i_d, dip);
815 error = xfs_iformat(ip, dip);
818 xfs_alert(mp, "%s: xfs_iformat() returned error %d",
824 ip->i_d.di_magic = be16_to_cpu(dip->di_magic);
825 ip->i_d.di_version = dip->di_version;
826 ip->i_d.di_gen = be32_to_cpu(dip->di_gen);
827 ip->i_d.di_flushiter = be16_to_cpu(dip->di_flushiter);
829 * Make sure to pull in the mode here as well in
830 * case the inode is released without being used.
831 * This ensures that xfs_inactive() will see that
832 * the inode is already free and not try to mess
833 * with the uninitialized part of it.
839 * The inode format changed when we moved the link count and
840 * made it 32 bits long. If this is an old format inode,
841 * convert it in memory to look like a new one. If it gets
842 * flushed to disk we will convert back before flushing or
843 * logging it. We zero out the new projid field and the old link
844 * count field. We'll handle clearing the pad field (the remains
845 * of the old uuid field) when we actually convert the inode to
846 * the new format. We don't change the version number so that we
847 * can distinguish this from a real new format inode.
849 if (ip->i_d.di_version == 1) {
850 ip->i_d.di_nlink = ip->i_d.di_onlink;
851 ip->i_d.di_onlink = 0;
852 xfs_set_projid(ip, 0);
855 ip->i_delayed_blks = 0;
856 ip->i_size = ip->i_d.di_size;
859 * Mark the buffer containing the inode as something to keep
860 * around for a while. This helps to keep recently accessed
861 * meta-data in-core longer.
863 xfs_buf_set_ref(bp, XFS_INO_REF);
866 * Use xfs_trans_brelse() to release the buffer containing the
867 * on-disk inode, because it was acquired with xfs_trans_read_buf()
868 * in xfs_itobp() above. If tp is NULL, this is just a normal
869 * brelse(). If we're within a transaction, then xfs_trans_brelse()
870 * will only release the buffer if it is not dirty within the
871 * transaction. It will be OK to release the buffer in this case,
872 * because inodes on disk are never destroyed and we will be
873 * locking the new in-core inode before putting it in the hash
874 * table where other processes can find it. Thus we don't have
875 * to worry about the inode being changed just because we released
879 xfs_trans_brelse(tp, bp);
884 * Read in extents from a btree-format inode.
885 * Allocate and fill in if_extents. Real work is done in xfs_bmap.c.
895 xfs_extnum_t nextents;
897 if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
898 XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW,
900 return XFS_ERROR(EFSCORRUPTED);
902 nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
903 ifp = XFS_IFORK_PTR(ip, whichfork);
906 * We know that the size is valid (it's checked in iformat_btree)
908 ifp->if_bytes = ifp->if_real_bytes = 0;
909 ifp->if_flags |= XFS_IFEXTENTS;
910 xfs_iext_add(ifp, 0, nextents);
911 error = xfs_bmap_read_extents(tp, ip, whichfork);
913 xfs_iext_destroy(ifp);
914 ifp->if_flags &= ~XFS_IFEXTENTS;
917 xfs_validate_extents(ifp, nextents, XFS_EXTFMT_INODE(ip));
922 * Allocate an inode on disk and return a copy of its in-core version.
923 * The in-core inode is locked exclusively. Set mode, nlink, and rdev
924 * appropriately within the inode. The uid and gid for the inode are
925 * set according to the contents of the given cred structure.
927 * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc()
928 * has a free inode available, call xfs_iget()
929 * to obtain the in-core version of the allocated inode. Finally,
930 * fill in the inode and log its initial contents. In this case,
931 * ialloc_context would be set to NULL and call_again set to false.
933 * If xfs_dialloc() does not have an available inode,
934 * it will replenish its supply by doing an allocation. Since we can
935 * only do one allocation within a transaction without deadlocks, we
936 * must commit the current transaction before returning the inode itself.
937 * In this case, therefore, we will set call_again to true and return.
938 * The caller should then commit the current transaction, start a new
939 * transaction, and call xfs_ialloc() again to actually get the inode.
941 * To ensure that some other process does not grab the inode that
942 * was allocated during the first call to xfs_ialloc(), this routine
943 * also returns the [locked] bp pointing to the head of the freelist
944 * as ialloc_context. The caller should hold this buffer across
945 * the commit and pass it back into this routine on the second call.
947 * If we are allocating quota inodes, we do not have a parent inode
948 * to attach to or associate with (i.e. pip == NULL) because they
949 * are not linked into the directory structure - they are attached
950 * directly to the superblock - and so have no parent.
961 xfs_buf_t **ialloc_context,
962 boolean_t *call_again,
973 * Call the space management code to pick
974 * the on-disk inode to be allocated.
976 error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc,
977 ialloc_context, call_again, &ino);
980 if (*call_again || ino == NULLFSINO) {
984 ASSERT(*ialloc_context == NULL);
987 * Get the in-core inode with the lock held exclusively.
988 * This is because we're setting fields here we need
989 * to prevent others from looking at until we're done.
991 error = xfs_iget(tp->t_mountp, tp, ino, XFS_IGET_CREATE,
992 XFS_ILOCK_EXCL, &ip);
997 ip->i_d.di_mode = mode;
998 ip->i_d.di_onlink = 0;
999 ip->i_d.di_nlink = nlink;
1000 ASSERT(ip->i_d.di_nlink == nlink);
1001 ip->i_d.di_uid = current_fsuid();
1002 ip->i_d.di_gid = current_fsgid();
1003 xfs_set_projid(ip, prid);
1004 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
1007 * If the superblock version is up to where we support new format
1008 * inodes and this is currently an old format inode, then change
1009 * the inode version number now. This way we only do the conversion
1010 * here rather than here and in the flush/logging code.
1012 if (xfs_sb_version_hasnlink(&tp->t_mountp->m_sb) &&
1013 ip->i_d.di_version == 1) {
1014 ip->i_d.di_version = 2;
1016 * We've already zeroed the old link count, the projid field,
1017 * and the pad field.
1022 * Project ids won't be stored on disk if we are using a version 1 inode.
1024 if ((prid != 0) && (ip->i_d.di_version == 1))
1025 xfs_bump_ino_vers2(tp, ip);
1027 if (pip && XFS_INHERIT_GID(pip)) {
1028 ip->i_d.di_gid = pip->i_d.di_gid;
1029 if ((pip->i_d.di_mode & S_ISGID) && S_ISDIR(mode)) {
1030 ip->i_d.di_mode |= S_ISGID;
1035 * If the group ID of the new file does not match the effective group
1036 * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
1037 * (and only if the irix_sgid_inherit compatibility variable is set).
1039 if ((irix_sgid_inherit) &&
1040 (ip->i_d.di_mode & S_ISGID) &&
1041 (!in_group_p((gid_t)ip->i_d.di_gid))) {
1042 ip->i_d.di_mode &= ~S_ISGID;
1045 ip->i_d.di_size = 0;
1047 ip->i_d.di_nextents = 0;
1048 ASSERT(ip->i_d.di_nblocks == 0);
1051 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
1052 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
1053 ip->i_d.di_atime = ip->i_d.di_mtime;
1054 ip->i_d.di_ctime = ip->i_d.di_mtime;
1057 * di_gen will have been taken care of in xfs_iread.
1059 ip->i_d.di_extsize = 0;
1060 ip->i_d.di_dmevmask = 0;
1061 ip->i_d.di_dmstate = 0;
1062 ip->i_d.di_flags = 0;
1063 flags = XFS_ILOG_CORE;
1064 switch (mode & S_IFMT) {
1069 ip->i_d.di_format = XFS_DINODE_FMT_DEV;
1070 ip->i_df.if_u2.if_rdev = rdev;
1071 ip->i_df.if_flags = 0;
1072 flags |= XFS_ILOG_DEV;
1076 * we can't set up filestreams until after the VFS inode
1077 * is set up properly.
1079 if (pip && xfs_inode_is_filestream(pip))
1083 if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) {
1086 if (S_ISDIR(mode)) {
1087 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
1088 di_flags |= XFS_DIFLAG_RTINHERIT;
1089 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
1090 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
1091 ip->i_d.di_extsize = pip->i_d.di_extsize;
1093 } else if (S_ISREG(mode)) {
1094 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
1095 di_flags |= XFS_DIFLAG_REALTIME;
1096 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
1097 di_flags |= XFS_DIFLAG_EXTSIZE;
1098 ip->i_d.di_extsize = pip->i_d.di_extsize;
1101 if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
1102 xfs_inherit_noatime)
1103 di_flags |= XFS_DIFLAG_NOATIME;
1104 if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
1106 di_flags |= XFS_DIFLAG_NODUMP;
1107 if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
1109 di_flags |= XFS_DIFLAG_SYNC;
1110 if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
1111 xfs_inherit_nosymlinks)
1112 di_flags |= XFS_DIFLAG_NOSYMLINKS;
1113 if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1114 di_flags |= XFS_DIFLAG_PROJINHERIT;
1115 if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
1116 xfs_inherit_nodefrag)
1117 di_flags |= XFS_DIFLAG_NODEFRAG;
1118 if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
1119 di_flags |= XFS_DIFLAG_FILESTREAM;
1120 ip->i_d.di_flags |= di_flags;
1124 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
1125 ip->i_df.if_flags = XFS_IFEXTENTS;
1126 ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0;
1127 ip->i_df.if_u1.if_extents = NULL;
1133 * Attribute fork settings for new inode.
1135 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1136 ip->i_d.di_anextents = 0;
1139 * Log the new values stuffed into the inode.
1141 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1142 xfs_trans_log_inode(tp, ip, flags);
1144 /* now that we have an i_mode we can setup inode ops and unlock */
1145 xfs_setup_inode(ip);
1147 /* now we have set up the vfs inode we can associate the filestream */
1149 error = xfs_filestream_associate(pip, ip);
1153 xfs_iflags_set(ip, XFS_IFILESTREAM);
1161 * Free up the underlying blocks past new_size. The new size must be smaller
1162 * than the current size. This routine can be used both for the attribute and
1163 * data fork, and does not modify the inode size, which is left to the caller.
1165 * The transaction passed to this routine must have made a permanent log
1166 * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the
1167 * given transaction and start new ones, so make sure everything involved in
1168 * the transaction is tidy before calling here. Some transaction will be
1169 * returned to the caller to be committed. The incoming transaction must
1170 * already include the inode, and both inode locks must be held exclusively.
1171 * The inode must also be "held" within the transaction. On return the inode
1172 * will be "held" within the returned transaction. This routine does NOT
1173 * require any disk space to be reserved for it within the transaction.
1175 * If we get an error, we must return with the inode locked and linked into the
1176 * current transaction. This keeps things simple for the higher level code,
1177 * because it always knows that the inode is locked and held in the transaction
1178 * that returns to it whether errors occur or not. We don't mark the inode
1179 * dirty on error so that transactions can be easily aborted if possible.
1182 xfs_itruncate_extents(
1183 struct xfs_trans **tpp,
1184 struct xfs_inode *ip,
1186 xfs_fsize_t new_size)
1188 struct xfs_mount *mp = ip->i_mount;
1189 struct xfs_trans *tp = *tpp;
1190 struct xfs_trans *ntp;
1191 xfs_bmap_free_t free_list;
1192 xfs_fsblock_t first_block;
1193 xfs_fileoff_t first_unmap_block;
1194 xfs_fileoff_t last_block;
1195 xfs_filblks_t unmap_len;
1200 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
1201 ASSERT(new_size <= ip->i_size);
1202 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1203 ASSERT(ip->i_itemp != NULL);
1204 ASSERT(ip->i_itemp->ili_lock_flags == 0);
1205 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1207 trace_xfs_itruncate_extents_start(ip, new_size);
1210 * Since it is possible for space to become allocated beyond
1211 * the end of the file (in a crash where the space is allocated
1212 * but the inode size is not yet updated), simply remove any
1213 * blocks which show up between the new EOF and the maximum
1214 * possible file size. If the first block to be removed is
1215 * beyond the maximum file size (ie it is the same as last_block),
1216 * then there is nothing to do.
1218 first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
1219 last_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
1220 if (first_unmap_block == last_block)
1223 ASSERT(first_unmap_block < last_block);
1224 unmap_len = last_block - first_unmap_block + 1;
1226 xfs_bmap_init(&free_list, &first_block);
1227 error = xfs_bunmapi(tp, ip,
1228 first_unmap_block, unmap_len,
1229 xfs_bmapi_aflag(whichfork),
1230 XFS_ITRUNC_MAX_EXTENTS,
1231 &first_block, &free_list,
1234 goto out_bmap_cancel;
1237 * Duplicate the transaction that has the permanent
1238 * reservation and commit the old transaction.
1240 error = xfs_bmap_finish(&tp, &free_list, &committed);
1242 xfs_trans_ijoin(tp, ip, 0);
1244 goto out_bmap_cancel;
1248 * Mark the inode dirty so it will be logged and
1249 * moved forward in the log as part of every commit.
1251 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1254 ntp = xfs_trans_dup(tp);
1255 error = xfs_trans_commit(tp, 0);
1258 xfs_trans_ijoin(tp, ip, 0);
1264 * Transaction commit worked ok so we can drop the extra ticket
1265 * reference that we gained in xfs_trans_dup()
1267 xfs_log_ticket_put(tp->t_ticket);
1268 error = xfs_trans_reserve(tp, 0,
1269 XFS_ITRUNCATE_LOG_RES(mp), 0,
1270 XFS_TRANS_PERM_LOG_RES,
1271 XFS_ITRUNCATE_LOG_COUNT);
1277 * Always re-log the inode so that our permanent transaction can keep
1278 * on rolling it forward in the log.
1280 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1282 trace_xfs_itruncate_extents_end(ip, new_size);
1289 * If the bunmapi call encounters an error, return to the caller where
1290 * the transaction can be properly aborted. We just need to make sure
1291 * we're not holding any resources that we were not when we came in.
1293 xfs_bmap_cancel(&free_list);
1298 * This is called when the inode's link count goes to 0.
1299 * We place the on-disk inode on a list in the AGI. It
1300 * will be pulled from this list when the inode is freed.
1317 ASSERT(ip->i_d.di_nlink == 0);
1318 ASSERT(ip->i_d.di_mode != 0);
1323 * Get the agi buffer first. It ensures lock ordering
1326 error = xfs_read_agi(mp, tp, XFS_INO_TO_AGNO(mp, ip->i_ino), &agibp);
1329 agi = XFS_BUF_TO_AGI(agibp);
1332 * Get the index into the agi hash table for the
1333 * list this inode will go on.
1335 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
1337 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
1338 ASSERT(agi->agi_unlinked[bucket_index]);
1339 ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino);
1341 if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) {
1343 * There is already another inode in the bucket we need
1344 * to add ourselves to. Add us at the front of the list.
1345 * Here we put the head pointer into our next pointer,
1346 * and then we fall through to point the head at us.
1348 error = xfs_itobp(mp, tp, ip, &dip, &ibp, XBF_LOCK);
1352 ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO));
1353 dip->di_next_unlinked = agi->agi_unlinked[bucket_index];
1354 offset = ip->i_imap.im_boffset +
1355 offsetof(xfs_dinode_t, di_next_unlinked);
1356 xfs_trans_inode_buf(tp, ibp);
1357 xfs_trans_log_buf(tp, ibp, offset,
1358 (offset + sizeof(xfs_agino_t) - 1));
1359 xfs_inobp_check(mp, ibp);
1363 * Point the bucket head pointer at the inode being inserted.
1366 agi->agi_unlinked[bucket_index] = cpu_to_be32(agino);
1367 offset = offsetof(xfs_agi_t, agi_unlinked) +
1368 (sizeof(xfs_agino_t) * bucket_index);
1369 xfs_trans_log_buf(tp, agibp, offset,
1370 (offset + sizeof(xfs_agino_t) - 1));
1375 * Pull the on-disk inode from the AGI unlinked list.
1388 xfs_agnumber_t agno;
1390 xfs_agino_t next_agino;
1391 xfs_buf_t *last_ibp;
1392 xfs_dinode_t *last_dip = NULL;
1394 int offset, last_offset = 0;
1398 agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
1401 * Get the agi buffer first. It ensures lock ordering
1404 error = xfs_read_agi(mp, tp, agno, &agibp);
1408 agi = XFS_BUF_TO_AGI(agibp);
1411 * Get the index into the agi hash table for the
1412 * list this inode will go on.
1414 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
1416 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
1417 ASSERT(agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO));
1418 ASSERT(agi->agi_unlinked[bucket_index]);
1420 if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) {
1422 * We're at the head of the list. Get the inode's
1423 * on-disk buffer to see if there is anyone after us
1424 * on the list. Only modify our next pointer if it
1425 * is not already NULLAGINO. This saves us the overhead
1426 * of dealing with the buffer when there is no need to
1429 error = xfs_itobp(mp, tp, ip, &dip, &ibp, XBF_LOCK);
1431 xfs_warn(mp, "%s: xfs_itobp() returned error %d.",
1435 next_agino = be32_to_cpu(dip->di_next_unlinked);
1436 ASSERT(next_agino != 0);
1437 if (next_agino != NULLAGINO) {
1438 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
1439 offset = ip->i_imap.im_boffset +
1440 offsetof(xfs_dinode_t, di_next_unlinked);
1441 xfs_trans_inode_buf(tp, ibp);
1442 xfs_trans_log_buf(tp, ibp, offset,
1443 (offset + sizeof(xfs_agino_t) - 1));
1444 xfs_inobp_check(mp, ibp);
1446 xfs_trans_brelse(tp, ibp);
1449 * Point the bucket head pointer at the next inode.
1451 ASSERT(next_agino != 0);
1452 ASSERT(next_agino != agino);
1453 agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino);
1454 offset = offsetof(xfs_agi_t, agi_unlinked) +
1455 (sizeof(xfs_agino_t) * bucket_index);
1456 xfs_trans_log_buf(tp, agibp, offset,
1457 (offset + sizeof(xfs_agino_t) - 1));
1460 * We need to search the list for the inode being freed.
1462 next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
1464 while (next_agino != agino) {
1466 * If the last inode wasn't the one pointing to
1467 * us, then release its buffer since we're not
1468 * going to do anything with it.
1470 if (last_ibp != NULL) {
1471 xfs_trans_brelse(tp, last_ibp);
1473 next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino);
1474 error = xfs_inotobp(mp, tp, next_ino, &last_dip,
1475 &last_ibp, &last_offset, 0);
1478 "%s: xfs_inotobp() returned error %d.",
1482 next_agino = be32_to_cpu(last_dip->di_next_unlinked);
1483 ASSERT(next_agino != NULLAGINO);
1484 ASSERT(next_agino != 0);
1487 * Now last_ibp points to the buffer previous to us on
1488 * the unlinked list. Pull us from the list.
1490 error = xfs_itobp(mp, tp, ip, &dip, &ibp, XBF_LOCK);
1492 xfs_warn(mp, "%s: xfs_itobp(2) returned error %d.",
1496 next_agino = be32_to_cpu(dip->di_next_unlinked);
1497 ASSERT(next_agino != 0);
1498 ASSERT(next_agino != agino);
1499 if (next_agino != NULLAGINO) {
1500 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
1501 offset = ip->i_imap.im_boffset +
1502 offsetof(xfs_dinode_t, di_next_unlinked);
1503 xfs_trans_inode_buf(tp, ibp);
1504 xfs_trans_log_buf(tp, ibp, offset,
1505 (offset + sizeof(xfs_agino_t) - 1));
1506 xfs_inobp_check(mp, ibp);
1508 xfs_trans_brelse(tp, ibp);
1511 * Point the previous inode on the list to the next inode.
1513 last_dip->di_next_unlinked = cpu_to_be32(next_agino);
1514 ASSERT(next_agino != 0);
1515 offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked);
1516 xfs_trans_inode_buf(tp, last_ibp);
1517 xfs_trans_log_buf(tp, last_ibp, offset,
1518 (offset + sizeof(xfs_agino_t) - 1));
1519 xfs_inobp_check(mp, last_ibp);
1525 * A big issue when freeing the inode cluster is is that we _cannot_ skip any
1526 * inodes that are in memory - they all must be marked stale and attached to
1527 * the cluster buffer.
1531 xfs_inode_t *free_ip,
1535 xfs_mount_t *mp = free_ip->i_mount;
1536 int blks_per_cluster;
1543 xfs_inode_log_item_t *iip;
1544 xfs_log_item_t *lip;
1545 struct xfs_perag *pag;
1547 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum));
1548 if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
1549 blks_per_cluster = 1;
1550 ninodes = mp->m_sb.sb_inopblock;
1551 nbufs = XFS_IALLOC_BLOCKS(mp);
1553 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
1554 mp->m_sb.sb_blocksize;
1555 ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
1556 nbufs = XFS_IALLOC_BLOCKS(mp) / blks_per_cluster;
1559 for (j = 0; j < nbufs; j++, inum += ninodes) {
1560 blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
1561 XFS_INO_TO_AGBNO(mp, inum));
1564 * We obtain and lock the backing buffer first in the process
1565 * here, as we have to ensure that any dirty inode that we
1566 * can't get the flush lock on is attached to the buffer.
1567 * If we scan the in-memory inodes first, then buffer IO can
1568 * complete before we get a lock on it, and hence we may fail
1569 * to mark all the active inodes on the buffer stale.
1571 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
1572 mp->m_bsize * blks_per_cluster,
1578 * Walk the inodes already attached to the buffer and mark them
1579 * stale. These will all have the flush locks held, so an
1580 * in-memory inode walk can't lock them. By marking them all
1581 * stale first, we will not attempt to lock them in the loop
1582 * below as the XFS_ISTALE flag will be set.
1586 if (lip->li_type == XFS_LI_INODE) {
1587 iip = (xfs_inode_log_item_t *)lip;
1588 ASSERT(iip->ili_logged == 1);
1589 lip->li_cb = xfs_istale_done;
1590 xfs_trans_ail_copy_lsn(mp->m_ail,
1591 &iip->ili_flush_lsn,
1592 &iip->ili_item.li_lsn);
1593 xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
1595 lip = lip->li_bio_list;
1600 * For each inode in memory attempt to add it to the inode
1601 * buffer and set it up for being staled on buffer IO
1602 * completion. This is safe as we've locked out tail pushing
1603 * and flushing by locking the buffer.
1605 * We have already marked every inode that was part of a
1606 * transaction stale above, which means there is no point in
1607 * even trying to lock them.
1609 for (i = 0; i < ninodes; i++) {
1612 ip = radix_tree_lookup(&pag->pag_ici_root,
1613 XFS_INO_TO_AGINO(mp, (inum + i)));
1615 /* Inode not in memory, nothing to do */
1622 * because this is an RCU protected lookup, we could
1623 * find a recently freed or even reallocated inode
1624 * during the lookup. We need to check under the
1625 * i_flags_lock for a valid inode here. Skip it if it
1626 * is not valid, the wrong inode or stale.
1628 spin_lock(&ip->i_flags_lock);
1629 if (ip->i_ino != inum + i ||
1630 __xfs_iflags_test(ip, XFS_ISTALE)) {
1631 spin_unlock(&ip->i_flags_lock);
1635 spin_unlock(&ip->i_flags_lock);
1638 * Don't try to lock/unlock the current inode, but we
1639 * _cannot_ skip the other inodes that we did not find
1640 * in the list attached to the buffer and are not
1641 * already marked stale. If we can't lock it, back off
1644 if (ip != free_ip &&
1645 !xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
1653 xfs_iflags_set(ip, XFS_ISTALE);
1656 * we don't need to attach clean inodes or those only
1657 * with unlogged changes (which we throw away, anyway).
1660 if (!iip || xfs_inode_clean(ip)) {
1661 ASSERT(ip != free_ip);
1662 ip->i_update_core = 0;
1664 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1668 iip->ili_last_fields = iip->ili_format.ilf_fields;
1669 iip->ili_format.ilf_fields = 0;
1670 iip->ili_logged = 1;
1671 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
1672 &iip->ili_item.li_lsn);
1674 xfs_buf_attach_iodone(bp, xfs_istale_done,
1678 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1681 xfs_trans_stale_inode_buf(tp, bp);
1682 xfs_trans_binval(tp, bp);
1690 * This is called to return an inode to the inode free list.
1691 * The inode should already be truncated to 0 length and have
1692 * no pages associated with it. This routine also assumes that
1693 * the inode is already a part of the transaction.
1695 * The on-disk copy of the inode will have been added to the list
1696 * of unlinked inodes in the AGI. We need to remove the inode from
1697 * that list atomically with respect to freeing it here.
1703 xfs_bmap_free_t *flist)
1707 xfs_ino_t first_ino;
1711 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1712 ASSERT(ip->i_d.di_nlink == 0);
1713 ASSERT(ip->i_d.di_nextents == 0);
1714 ASSERT(ip->i_d.di_anextents == 0);
1715 ASSERT((ip->i_d.di_size == 0 && ip->i_size == 0) ||
1716 (!S_ISREG(ip->i_d.di_mode)));
1717 ASSERT(ip->i_d.di_nblocks == 0);
1720 * Pull the on-disk inode from the AGI unlinked list.
1722 error = xfs_iunlink_remove(tp, ip);
1727 error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino);
1731 ip->i_d.di_mode = 0; /* mark incore inode as free */
1732 ip->i_d.di_flags = 0;
1733 ip->i_d.di_dmevmask = 0;
1734 ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */
1735 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
1736 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1738 * Bump the generation count so no one will be confused
1739 * by reincarnations of this inode.
1743 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1745 error = xfs_itobp(ip->i_mount, tp, ip, &dip, &ibp, XBF_LOCK);
1750 * Clear the on-disk di_mode. This is to prevent xfs_bulkstat
1751 * from picking up this inode when it is reclaimed (its incore state
1752 * initialzed but not flushed to disk yet). The in-core di_mode is
1753 * already cleared and a corresponding transaction logged.
1754 * The hack here just synchronizes the in-core to on-disk
1755 * di_mode value in advance before the actual inode sync to disk.
1756 * This is OK because the inode is already unlinked and would never
1757 * change its di_mode again for this inode generation.
1758 * This is a temporary hack that would require a proper fix
1764 error = xfs_ifree_cluster(ip, tp, first_ino);
1771 * Reallocate the space for if_broot based on the number of records
1772 * being added or deleted as indicated in rec_diff. Move the records
1773 * and pointers in if_broot to fit the new size. When shrinking this
1774 * will eliminate holes between the records and pointers created by
1775 * the caller. When growing this will create holes to be filled in
1778 * The caller must not request to add more records than would fit in
1779 * the on-disk inode root. If the if_broot is currently NULL, then
1780 * if we adding records one will be allocated. The caller must also
1781 * not request that the number of records go below zero, although
1782 * it can go to zero.
1784 * ip -- the inode whose if_broot area is changing
1785 * ext_diff -- the change in the number of records, positive or negative,
1786 * requested for the if_broot array.
1794 struct xfs_mount *mp = ip->i_mount;
1797 struct xfs_btree_block *new_broot;
1804 * Handle the degenerate case quietly.
1806 if (rec_diff == 0) {
1810 ifp = XFS_IFORK_PTR(ip, whichfork);
1813 * If there wasn't any memory allocated before, just
1814 * allocate it now and get out.
1816 if (ifp->if_broot_bytes == 0) {
1817 new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(rec_diff);
1818 ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
1819 ifp->if_broot_bytes = (int)new_size;
1824 * If there is already an existing if_broot, then we need
1825 * to realloc() it and shift the pointers to their new
1826 * location. The records don't change location because
1827 * they are kept butted up against the btree block header.
1829 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
1830 new_max = cur_max + rec_diff;
1831 new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max);
1832 ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
1833 (size_t)XFS_BMAP_BROOT_SPACE_CALC(cur_max), /* old size */
1834 KM_SLEEP | KM_NOFS);
1835 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
1836 ifp->if_broot_bytes);
1837 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
1839 ifp->if_broot_bytes = (int)new_size;
1840 ASSERT(ifp->if_broot_bytes <=
1841 XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ);
1842 memmove(np, op, cur_max * (uint)sizeof(xfs_dfsbno_t));
1847 * rec_diff is less than 0. In this case, we are shrinking the
1848 * if_broot buffer. It must already exist. If we go to zero
1849 * records, just get rid of the root and clear the status bit.
1851 ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
1852 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
1853 new_max = cur_max + rec_diff;
1854 ASSERT(new_max >= 0);
1856 new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max);
1860 new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
1862 * First copy over the btree block header.
1864 memcpy(new_broot, ifp->if_broot, XFS_BTREE_LBLOCK_LEN);
1867 ifp->if_flags &= ~XFS_IFBROOT;
1871 * Only copy the records and pointers if there are any.
1875 * First copy the records.
1877 op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
1878 np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
1879 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
1882 * Then copy the pointers.
1884 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
1885 ifp->if_broot_bytes);
1886 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
1888 memcpy(np, op, new_max * (uint)sizeof(xfs_dfsbno_t));
1890 kmem_free(ifp->if_broot);
1891 ifp->if_broot = new_broot;
1892 ifp->if_broot_bytes = (int)new_size;
1893 ASSERT(ifp->if_broot_bytes <=
1894 XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ);
1900 * This is called when the amount of space needed for if_data
1901 * is increased or decreased. The change in size is indicated by
1902 * the number of bytes that need to be added or deleted in the
1903 * byte_diff parameter.
1905 * If the amount of space needed has decreased below the size of the
1906 * inline buffer, then switch to using the inline buffer. Otherwise,
1907 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
1908 * to what is needed.
1910 * ip -- the inode whose if_data area is changing
1911 * byte_diff -- the change in the number of bytes, positive or negative,
1912 * requested for the if_data array.
1924 if (byte_diff == 0) {
1928 ifp = XFS_IFORK_PTR(ip, whichfork);
1929 new_size = (int)ifp->if_bytes + byte_diff;
1930 ASSERT(new_size >= 0);
1932 if (new_size == 0) {
1933 if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
1934 kmem_free(ifp->if_u1.if_data);
1936 ifp->if_u1.if_data = NULL;
1938 } else if (new_size <= sizeof(ifp->if_u2.if_inline_data)) {
1940 * If the valid extents/data can fit in if_inline_ext/data,
1941 * copy them from the malloc'd vector and free it.
1943 if (ifp->if_u1.if_data == NULL) {
1944 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
1945 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
1946 ASSERT(ifp->if_real_bytes != 0);
1947 memcpy(ifp->if_u2.if_inline_data, ifp->if_u1.if_data,
1949 kmem_free(ifp->if_u1.if_data);
1950 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
1955 * Stuck with malloc/realloc.
1956 * For inline data, the underlying buffer must be
1957 * a multiple of 4 bytes in size so that it can be
1958 * logged and stay on word boundaries. We enforce
1961 real_size = roundup(new_size, 4);
1962 if (ifp->if_u1.if_data == NULL) {
1963 ASSERT(ifp->if_real_bytes == 0);
1964 ifp->if_u1.if_data = kmem_alloc(real_size,
1965 KM_SLEEP | KM_NOFS);
1966 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
1968 * Only do the realloc if the underlying size
1969 * is really changing.
1971 if (ifp->if_real_bytes != real_size) {
1972 ifp->if_u1.if_data =
1973 kmem_realloc(ifp->if_u1.if_data,
1976 KM_SLEEP | KM_NOFS);
1979 ASSERT(ifp->if_real_bytes == 0);
1980 ifp->if_u1.if_data = kmem_alloc(real_size,
1981 KM_SLEEP | KM_NOFS);
1982 memcpy(ifp->if_u1.if_data, ifp->if_u2.if_inline_data,
1986 ifp->if_real_bytes = real_size;
1987 ifp->if_bytes = new_size;
1988 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
1998 ifp = XFS_IFORK_PTR(ip, whichfork);
1999 if (ifp->if_broot != NULL) {
2000 kmem_free(ifp->if_broot);
2001 ifp->if_broot = NULL;
2005 * If the format is local, then we can't have an extents
2006 * array so just look for an inline data array. If we're
2007 * not local then we may or may not have an extents list,
2008 * so check and free it up if we do.
2010 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
2011 if ((ifp->if_u1.if_data != ifp->if_u2.if_inline_data) &&
2012 (ifp->if_u1.if_data != NULL)) {
2013 ASSERT(ifp->if_real_bytes != 0);
2014 kmem_free(ifp->if_u1.if_data);
2015 ifp->if_u1.if_data = NULL;
2016 ifp->if_real_bytes = 0;
2018 } else if ((ifp->if_flags & XFS_IFEXTENTS) &&
2019 ((ifp->if_flags & XFS_IFEXTIREC) ||
2020 ((ifp->if_u1.if_extents != NULL) &&
2021 (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext)))) {
2022 ASSERT(ifp->if_real_bytes != 0);
2023 xfs_iext_destroy(ifp);
2025 ASSERT(ifp->if_u1.if_extents == NULL ||
2026 ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext);
2027 ASSERT(ifp->if_real_bytes == 0);
2028 if (whichfork == XFS_ATTR_FORK) {
2029 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
2035 * This is called to unpin an inode. The caller must have the inode locked
2036 * in at least shared mode so that the buffer cannot be subsequently pinned
2037 * once someone is waiting for it to be unpinned.
2041 struct xfs_inode *ip)
2043 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
2045 trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
2047 /* Give the log a push to start the unpinning I/O */
2048 xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0);
2054 struct xfs_inode *ip)
2056 if (xfs_ipincount(ip)) {
2057 xfs_iunpin_nowait(ip);
2058 wait_event(ip->i_ipin_wait, (xfs_ipincount(ip) == 0));
2063 * xfs_iextents_copy()
2065 * This is called to copy the REAL extents (as opposed to the delayed
2066 * allocation extents) from the inode into the given buffer. It
2067 * returns the number of bytes copied into the buffer.
2069 * If there are no delayed allocation extents, then we can just
2070 * memcpy() the extents into the buffer. Otherwise, we need to
2071 * examine each extent in turn and skip those which are delayed.
2083 xfs_fsblock_t start_block;
2085 ifp = XFS_IFORK_PTR(ip, whichfork);
2086 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
2087 ASSERT(ifp->if_bytes > 0);
2089 nrecs = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
2090 XFS_BMAP_TRACE_EXLIST(ip, nrecs, whichfork);
2094 * There are some delayed allocation extents in the
2095 * inode, so copy the extents one at a time and skip
2096 * the delayed ones. There must be at least one
2097 * non-delayed extent.
2100 for (i = 0; i < nrecs; i++) {
2101 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
2102 start_block = xfs_bmbt_get_startblock(ep);
2103 if (isnullstartblock(start_block)) {
2105 * It's a delayed allocation extent, so skip it.
2110 /* Translate to on disk format */
2111 put_unaligned(cpu_to_be64(ep->l0), &dp->l0);
2112 put_unaligned(cpu_to_be64(ep->l1), &dp->l1);
2116 ASSERT(copied != 0);
2117 xfs_validate_extents(ifp, copied, XFS_EXTFMT_INODE(ip));
2119 return (copied * (uint)sizeof(xfs_bmbt_rec_t));
2123 * Each of the following cases stores data into the same region
2124 * of the on-disk inode, so only one of them can be valid at
2125 * any given time. While it is possible to have conflicting formats
2126 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
2127 * in EXTENTS format, this can only happen when the fork has
2128 * changed formats after being modified but before being flushed.
2129 * In these cases, the format always takes precedence, because the
2130 * format indicates the current state of the fork.
2137 xfs_inode_log_item_t *iip,
2144 #ifdef XFS_TRANS_DEBUG
2147 static const short brootflag[2] =
2148 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
2149 static const short dataflag[2] =
2150 { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
2151 static const short extflag[2] =
2152 { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
2156 ifp = XFS_IFORK_PTR(ip, whichfork);
2158 * This can happen if we gave up in iformat in an error path,
2159 * for the attribute fork.
2162 ASSERT(whichfork == XFS_ATTR_FORK);
2165 cp = XFS_DFORK_PTR(dip, whichfork);
2167 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
2168 case XFS_DINODE_FMT_LOCAL:
2169 if ((iip->ili_format.ilf_fields & dataflag[whichfork]) &&
2170 (ifp->if_bytes > 0)) {
2171 ASSERT(ifp->if_u1.if_data != NULL);
2172 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
2173 memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
2177 case XFS_DINODE_FMT_EXTENTS:
2178 ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
2179 !(iip->ili_format.ilf_fields & extflag[whichfork]));
2180 if ((iip->ili_format.ilf_fields & extflag[whichfork]) &&
2181 (ifp->if_bytes > 0)) {
2182 ASSERT(xfs_iext_get_ext(ifp, 0));
2183 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
2184 (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
2189 case XFS_DINODE_FMT_BTREE:
2190 if ((iip->ili_format.ilf_fields & brootflag[whichfork]) &&
2191 (ifp->if_broot_bytes > 0)) {
2192 ASSERT(ifp->if_broot != NULL);
2193 ASSERT(ifp->if_broot_bytes <=
2194 (XFS_IFORK_SIZE(ip, whichfork) +
2195 XFS_BROOT_SIZE_ADJ));
2196 xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
2197 (xfs_bmdr_block_t *)cp,
2198 XFS_DFORK_SIZE(dip, mp, whichfork));
2202 case XFS_DINODE_FMT_DEV:
2203 if (iip->ili_format.ilf_fields & XFS_ILOG_DEV) {
2204 ASSERT(whichfork == XFS_DATA_FORK);
2205 xfs_dinode_put_rdev(dip, ip->i_df.if_u2.if_rdev);
2209 case XFS_DINODE_FMT_UUID:
2210 if (iip->ili_format.ilf_fields & XFS_ILOG_UUID) {
2211 ASSERT(whichfork == XFS_DATA_FORK);
2212 memcpy(XFS_DFORK_DPTR(dip),
2213 &ip->i_df.if_u2.if_uuid,
2229 xfs_mount_t *mp = ip->i_mount;
2230 struct xfs_perag *pag;
2231 unsigned long first_index, mask;
2232 unsigned long inodes_per_cluster;
2234 xfs_inode_t **ilist;
2241 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
2243 inodes_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog;
2244 ilist_size = inodes_per_cluster * sizeof(xfs_inode_t *);
2245 ilist = kmem_alloc(ilist_size, KM_MAYFAIL|KM_NOFS);
2249 mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
2250 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask;
2252 /* really need a gang lookup range call here */
2253 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)ilist,
2254 first_index, inodes_per_cluster);
2258 for (i = 0; i < nr_found; i++) {
2264 * because this is an RCU protected lookup, we could find a
2265 * recently freed or even reallocated inode during the lookup.
2266 * We need to check under the i_flags_lock for a valid inode
2267 * here. Skip it if it is not valid or the wrong inode.
2269 spin_lock(&ip->i_flags_lock);
2271 (XFS_INO_TO_AGINO(mp, iq->i_ino) & mask) != first_index) {
2272 spin_unlock(&ip->i_flags_lock);
2275 spin_unlock(&ip->i_flags_lock);
2278 * Do an un-protected check to see if the inode is dirty and
2279 * is a candidate for flushing. These checks will be repeated
2280 * later after the appropriate locks are acquired.
2282 if (xfs_inode_clean(iq) && xfs_ipincount(iq) == 0)
2286 * Try to get locks. If any are unavailable or it is pinned,
2287 * then this inode cannot be flushed and is skipped.
2290 if (!xfs_ilock_nowait(iq, XFS_ILOCK_SHARED))
2292 if (!xfs_iflock_nowait(iq)) {
2293 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2296 if (xfs_ipincount(iq)) {
2298 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2303 * arriving here means that this inode can be flushed. First
2304 * re-check that it's dirty before flushing.
2306 if (!xfs_inode_clean(iq)) {
2308 error = xfs_iflush_int(iq, bp);
2310 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2311 goto cluster_corrupt_out;
2317 xfs_iunlock(iq, XFS_ILOCK_SHARED);
2321 XFS_STATS_INC(xs_icluster_flushcnt);
2322 XFS_STATS_ADD(xs_icluster_flushinode, clcount);
2333 cluster_corrupt_out:
2335 * Corruption detected in the clustering loop. Invalidate the
2336 * inode buffer and shut down the filesystem.
2340 * Clean up the buffer. If it was B_DELWRI, just release it --
2341 * brelse can handle it with no problems. If not, shut down the
2342 * filesystem before releasing the buffer.
2344 bufwasdelwri = XFS_BUF_ISDELAYWRITE(bp);
2348 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
2350 if (!bufwasdelwri) {
2352 * Just like incore_relse: if we have b_iodone functions,
2353 * mark the buffer as an error and call them. Otherwise
2354 * mark it as stale and brelse.
2359 xfs_buf_ioerror(bp, EIO);
2360 xfs_buf_ioend(bp, 0);
2368 * Unlocks the flush lock
2370 xfs_iflush_abort(iq);
2373 return XFS_ERROR(EFSCORRUPTED);
2377 * xfs_iflush() will write a modified inode's changes out to the
2378 * inode's on disk home. The caller must have the inode lock held
2379 * in at least shared mode and the inode flush completion must be
2380 * active as well. The inode lock will still be held upon return from
2381 * the call and the caller is free to unlock it.
2382 * The inode flush will be completed when the inode reaches the disk.
2383 * The flags indicate how the inode's buffer should be written out.
2390 xfs_inode_log_item_t *iip;
2396 XFS_STATS_INC(xs_iflush_count);
2398 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
2399 ASSERT(xfs_isiflocked(ip));
2400 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
2401 ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
2407 * We can't flush the inode until it is unpinned, so wait for it if we
2408 * are allowed to block. We know no one new can pin it, because we are
2409 * holding the inode lock shared and you need to hold it exclusively to
2412 * If we are not allowed to block, force the log out asynchronously so
2413 * that when we come back the inode will be unpinned. If other inodes
2414 * in the same cluster are dirty, they will probably write the inode
2415 * out for us if they occur after the log force completes.
2417 if (!(flags & SYNC_WAIT) && xfs_ipincount(ip)) {
2418 xfs_iunpin_nowait(ip);
2422 xfs_iunpin_wait(ip);
2425 * For stale inodes we cannot rely on the backing buffer remaining
2426 * stale in cache for the remaining life of the stale inode and so
2427 * xfs_itobp() below may give us a buffer that no longer contains
2428 * inodes below. We have to check this after ensuring the inode is
2429 * unpinned so that it is safe to reclaim the stale inode after the
2432 if (xfs_iflags_test(ip, XFS_ISTALE)) {
2438 * This may have been unpinned because the filesystem is shutting
2439 * down forcibly. If that's the case we must not write this inode
2440 * to disk, because the log record didn't make it to disk!
2442 if (XFS_FORCED_SHUTDOWN(mp)) {
2443 ip->i_update_core = 0;
2445 iip->ili_format.ilf_fields = 0;
2447 return XFS_ERROR(EIO);
2451 * Get the buffer containing the on-disk inode.
2453 error = xfs_itobp(mp, NULL, ip, &dip, &bp,
2454 (flags & SYNC_TRYLOCK) ? XBF_TRYLOCK : XBF_LOCK);
2461 * First flush out the inode that xfs_iflush was called with.
2463 error = xfs_iflush_int(ip, bp);
2468 * If the buffer is pinned then push on the log now so we won't
2469 * get stuck waiting in the write for too long.
2471 if (xfs_buf_ispinned(bp))
2472 xfs_log_force(mp, 0);
2476 * see if other inodes can be gathered into this write
2478 error = xfs_iflush_cluster(ip, bp);
2480 goto cluster_corrupt_out;
2482 if (flags & SYNC_WAIT)
2483 error = xfs_bwrite(bp);
2485 xfs_buf_delwri_queue(bp);
2492 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
2493 cluster_corrupt_out:
2495 * Unlocks the flush lock
2497 xfs_iflush_abort(ip);
2498 return XFS_ERROR(EFSCORRUPTED);
2507 xfs_inode_log_item_t *iip;
2510 #ifdef XFS_TRANS_DEBUG
2514 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
2515 ASSERT(xfs_isiflocked(ip));
2516 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
2517 ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
2522 /* set *dip = inode's place in the buffer */
2523 dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
2526 * Clear i_update_core before copying out the data.
2527 * This is for coordination with our timestamp updates
2528 * that don't hold the inode lock. They will always
2529 * update the timestamps BEFORE setting i_update_core,
2530 * so if we clear i_update_core after they set it we
2531 * are guaranteed to see their updates to the timestamps.
2532 * I believe that this depends on strongly ordered memory
2533 * semantics, but we have that. We use the SYNCHRONIZE
2534 * macro to make sure that the compiler does not reorder
2535 * the i_update_core access below the data copy below.
2537 ip->i_update_core = 0;
2541 * Make sure to get the latest timestamps from the Linux inode.
2543 xfs_synchronize_times(ip);
2545 if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
2546 mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) {
2547 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2548 "%s: Bad inode %Lu magic number 0x%x, ptr 0x%p",
2549 __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
2552 if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC,
2553 mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) {
2554 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2555 "%s: Bad inode %Lu, ptr 0x%p, magic number 0x%x",
2556 __func__, ip->i_ino, ip, ip->i_d.di_magic);
2559 if (S_ISREG(ip->i_d.di_mode)) {
2561 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
2562 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE),
2563 mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) {
2564 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2565 "%s: Bad regular inode %Lu, ptr 0x%p",
2566 __func__, ip->i_ino, ip);
2569 } else if (S_ISDIR(ip->i_d.di_mode)) {
2571 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
2572 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
2573 (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL),
2574 mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) {
2575 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2576 "%s: Bad directory inode %Lu, ptr 0x%p",
2577 __func__, ip->i_ino, ip);
2581 if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents >
2582 ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5,
2583 XFS_RANDOM_IFLUSH_5)) {
2584 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2585 "%s: detected corrupt incore inode %Lu, "
2586 "total extents = %d, nblocks = %Ld, ptr 0x%p",
2587 __func__, ip->i_ino,
2588 ip->i_d.di_nextents + ip->i_d.di_anextents,
2589 ip->i_d.di_nblocks, ip);
2592 if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
2593 mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) {
2594 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
2595 "%s: bad inode %Lu, forkoff 0x%x, ptr 0x%p",
2596 __func__, ip->i_ino, ip->i_d.di_forkoff, ip);
2600 * bump the flush iteration count, used to detect flushes which
2601 * postdate a log record during recovery.
2604 ip->i_d.di_flushiter++;
2607 * Copy the dirty parts of the inode into the on-disk
2608 * inode. We always copy out the core of the inode,
2609 * because if the inode is dirty at all the core must
2612 xfs_dinode_to_disk(dip, &ip->i_d);
2614 /* Wrap, we never let the log put out DI_MAX_FLUSH */
2615 if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
2616 ip->i_d.di_flushiter = 0;
2619 * If this is really an old format inode and the superblock version
2620 * has not been updated to support only new format inodes, then
2621 * convert back to the old inode format. If the superblock version
2622 * has been updated, then make the conversion permanent.
2624 ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb));
2625 if (ip->i_d.di_version == 1) {
2626 if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
2630 ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
2631 dip->di_onlink = cpu_to_be16(ip->i_d.di_nlink);
2634 * The superblock version has already been bumped,
2635 * so just make the conversion to the new inode
2638 ip->i_d.di_version = 2;
2639 dip->di_version = 2;
2640 ip->i_d.di_onlink = 0;
2642 memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
2643 memset(&(dip->di_pad[0]), 0,
2644 sizeof(dip->di_pad));
2645 ASSERT(xfs_get_projid(ip) == 0);
2649 xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK, bp);
2650 if (XFS_IFORK_Q(ip))
2651 xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK, bp);
2652 xfs_inobp_check(mp, bp);
2655 * We've recorded everything logged in the inode, so we'd
2656 * like to clear the ilf_fields bits so we don't log and
2657 * flush things unnecessarily. However, we can't stop
2658 * logging all this information until the data we've copied
2659 * into the disk buffer is written to disk. If we did we might
2660 * overwrite the copy of the inode in the log with all the
2661 * data after re-logging only part of it, and in the face of
2662 * a crash we wouldn't have all the data we need to recover.
2664 * What we do is move the bits to the ili_last_fields field.
2665 * When logging the inode, these bits are moved back to the
2666 * ilf_fields field. In the xfs_iflush_done() routine we
2667 * clear ili_last_fields, since we know that the information
2668 * those bits represent is permanently on disk. As long as
2669 * the flush completes before the inode is logged again, then
2670 * both ilf_fields and ili_last_fields will be cleared.
2672 * We can play with the ilf_fields bits here, because the inode
2673 * lock must be held exclusively in order to set bits there
2674 * and the flush lock protects the ili_last_fields bits.
2675 * Set ili_logged so the flush done
2676 * routine can tell whether or not to look in the AIL.
2677 * Also, store the current LSN of the inode so that we can tell
2678 * whether the item has moved in the AIL from xfs_iflush_done().
2679 * In order to read the lsn we need the AIL lock, because
2680 * it is a 64 bit value that cannot be read atomically.
2682 if (iip != NULL && iip->ili_format.ilf_fields != 0) {
2683 iip->ili_last_fields = iip->ili_format.ilf_fields;
2684 iip->ili_format.ilf_fields = 0;
2685 iip->ili_logged = 1;
2687 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
2688 &iip->ili_item.li_lsn);
2691 * Attach the function xfs_iflush_done to the inode's
2692 * buffer. This will remove the inode from the AIL
2693 * and unlock the inode's flush lock when the inode is
2694 * completely written to disk.
2696 xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item);
2698 ASSERT(bp->b_fspriv != NULL);
2699 ASSERT(bp->b_iodone != NULL);
2702 * We're flushing an inode which is not in the AIL and has
2703 * not been logged but has i_update_core set. For this
2704 * case we can use a B_DELWRI flush and immediately drop
2705 * the inode flush lock because we can avoid the whole
2706 * AIL state thing. It's OK to drop the flush lock now,
2707 * because we've already locked the buffer and to do anything
2708 * you really need both.
2711 ASSERT(iip->ili_logged == 0);
2712 ASSERT(iip->ili_last_fields == 0);
2713 ASSERT((iip->ili_item.li_flags & XFS_LI_IN_AIL) == 0);
2721 return XFS_ERROR(EFSCORRUPTED);
2726 struct xfs_inode *ip)
2730 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
2732 bp = xfs_incore(ip->i_mount->m_ddev_targp, ip->i_imap.im_blkno,
2733 ip->i_imap.im_len, XBF_TRYLOCK);
2737 if (XFS_BUF_ISDELAYWRITE(bp)) {
2738 xfs_buf_delwri_promote(bp);
2739 wake_up_process(ip->i_mount->m_ddev_targp->bt_task);
2746 * Return a pointer to the extent record at file index idx.
2748 xfs_bmbt_rec_host_t *
2750 xfs_ifork_t *ifp, /* inode fork pointer */
2751 xfs_extnum_t idx) /* index of target extent */
2754 ASSERT(idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
2756 if ((ifp->if_flags & XFS_IFEXTIREC) && (idx == 0)) {
2757 return ifp->if_u1.if_ext_irec->er_extbuf;
2758 } else if (ifp->if_flags & XFS_IFEXTIREC) {
2759 xfs_ext_irec_t *erp; /* irec pointer */
2760 int erp_idx = 0; /* irec index */
2761 xfs_extnum_t page_idx = idx; /* ext index in target list */
2763 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
2764 return &erp->er_extbuf[page_idx];
2765 } else if (ifp->if_bytes) {
2766 return &ifp->if_u1.if_extents[idx];
2773 * Insert new item(s) into the extent records for incore inode
2774 * fork 'ifp'. 'count' new items are inserted at index 'idx'.
2778 xfs_inode_t *ip, /* incore inode pointer */
2779 xfs_extnum_t idx, /* starting index of new items */
2780 xfs_extnum_t count, /* number of inserted items */
2781 xfs_bmbt_irec_t *new, /* items to insert */
2782 int state) /* type of extent conversion */
2784 xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
2785 xfs_extnum_t i; /* extent record index */
2787 trace_xfs_iext_insert(ip, idx, new, state, _RET_IP_);
2789 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2790 xfs_iext_add(ifp, idx, count);
2791 for (i = idx; i < idx + count; i++, new++)
2792 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, i), new);
2796 * This is called when the amount of space required for incore file
2797 * extents needs to be increased. The ext_diff parameter stores the
2798 * number of new extents being added and the idx parameter contains
2799 * the extent index where the new extents will be added. If the new
2800 * extents are being appended, then we just need to (re)allocate and
2801 * initialize the space. Otherwise, if the new extents are being
2802 * inserted into the middle of the existing entries, a bit more work
2803 * is required to make room for the new extents to be inserted. The
2804 * caller is responsible for filling in the new extent entries upon
2809 xfs_ifork_t *ifp, /* inode fork pointer */
2810 xfs_extnum_t idx, /* index to begin adding exts */
2811 int ext_diff) /* number of extents to add */
2813 int byte_diff; /* new bytes being added */
2814 int new_size; /* size of extents after adding */
2815 xfs_extnum_t nextents; /* number of extents in file */
2817 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
2818 ASSERT((idx >= 0) && (idx <= nextents));
2819 byte_diff = ext_diff * sizeof(xfs_bmbt_rec_t);
2820 new_size = ifp->if_bytes + byte_diff;
2822 * If the new number of extents (nextents + ext_diff)
2823 * fits inside the inode, then continue to use the inline
2826 if (nextents + ext_diff <= XFS_INLINE_EXTS) {
2827 if (idx < nextents) {
2828 memmove(&ifp->if_u2.if_inline_ext[idx + ext_diff],
2829 &ifp->if_u2.if_inline_ext[idx],
2830 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
2831 memset(&ifp->if_u2.if_inline_ext[idx], 0, byte_diff);
2833 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
2834 ifp->if_real_bytes = 0;
2837 * Otherwise use a linear (direct) extent list.
2838 * If the extents are currently inside the inode,
2839 * xfs_iext_realloc_direct will switch us from
2840 * inline to direct extent allocation mode.
2842 else if (nextents + ext_diff <= XFS_LINEAR_EXTS) {
2843 xfs_iext_realloc_direct(ifp, new_size);
2844 if (idx < nextents) {
2845 memmove(&ifp->if_u1.if_extents[idx + ext_diff],
2846 &ifp->if_u1.if_extents[idx],
2847 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
2848 memset(&ifp->if_u1.if_extents[idx], 0, byte_diff);
2851 /* Indirection array */
2853 xfs_ext_irec_t *erp;
2857 ASSERT(nextents + ext_diff > XFS_LINEAR_EXTS);
2858 if (ifp->if_flags & XFS_IFEXTIREC) {
2859 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 1);
2861 xfs_iext_irec_init(ifp);
2862 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
2863 erp = ifp->if_u1.if_ext_irec;
2865 /* Extents fit in target extent page */
2866 if (erp && erp->er_extcount + ext_diff <= XFS_LINEAR_EXTS) {
2867 if (page_idx < erp->er_extcount) {
2868 memmove(&erp->er_extbuf[page_idx + ext_diff],
2869 &erp->er_extbuf[page_idx],
2870 (erp->er_extcount - page_idx) *
2871 sizeof(xfs_bmbt_rec_t));
2872 memset(&erp->er_extbuf[page_idx], 0, byte_diff);
2874 erp->er_extcount += ext_diff;
2875 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
2877 /* Insert a new extent page */
2879 xfs_iext_add_indirect_multi(ifp,
2880 erp_idx, page_idx, ext_diff);
2883 * If extent(s) are being appended to the last page in
2884 * the indirection array and the new extent(s) don't fit
2885 * in the page, then erp is NULL and erp_idx is set to
2886 * the next index needed in the indirection array.
2889 int count = ext_diff;
2892 erp = xfs_iext_irec_new(ifp, erp_idx);
2893 erp->er_extcount = count;
2894 count -= MIN(count, (int)XFS_LINEAR_EXTS);
2901 ifp->if_bytes = new_size;
2905 * This is called when incore extents are being added to the indirection
2906 * array and the new extents do not fit in the target extent list. The
2907 * erp_idx parameter contains the irec index for the target extent list
2908 * in the indirection array, and the idx parameter contains the extent
2909 * index within the list. The number of extents being added is stored
2910 * in the count parameter.
2912 * |-------| |-------|
2913 * | | | | idx - number of extents before idx
2915 * | | | | count - number of extents being inserted at idx
2916 * |-------| |-------|
2917 * | count | | nex2 | nex2 - number of extents after idx + count
2918 * |-------| |-------|
2921 xfs_iext_add_indirect_multi(
2922 xfs_ifork_t *ifp, /* inode fork pointer */
2923 int erp_idx, /* target extent irec index */
2924 xfs_extnum_t idx, /* index within target list */
2925 int count) /* new extents being added */
2927 int byte_diff; /* new bytes being added */
2928 xfs_ext_irec_t *erp; /* pointer to irec entry */
2929 xfs_extnum_t ext_diff; /* number of extents to add */
2930 xfs_extnum_t ext_cnt; /* new extents still needed */
2931 xfs_extnum_t nex2; /* extents after idx + count */
2932 xfs_bmbt_rec_t *nex2_ep = NULL; /* temp list for nex2 extents */
2933 int nlists; /* number of irec's (lists) */
2935 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
2936 erp = &ifp->if_u1.if_ext_irec[erp_idx];
2937 nex2 = erp->er_extcount - idx;
2938 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
2941 * Save second part of target extent list
2942 * (all extents past */
2944 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
2945 nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_NOFS);
2946 memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff);
2947 erp->er_extcount -= nex2;
2948 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2);
2949 memset(&erp->er_extbuf[idx], 0, byte_diff);
2953 * Add the new extents to the end of the target
2954 * list, then allocate new irec record(s) and
2955 * extent buffer(s) as needed to store the rest
2956 * of the new extents.
2959 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount);
2961 erp->er_extcount += ext_diff;
2962 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
2963 ext_cnt -= ext_diff;
2967 erp = xfs_iext_irec_new(ifp, erp_idx);
2968 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS);
2969 erp->er_extcount = ext_diff;
2970 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
2971 ext_cnt -= ext_diff;
2974 /* Add nex2 extents back to indirection array */
2976 xfs_extnum_t ext_avail;
2979 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
2980 ext_avail = XFS_LINEAR_EXTS - erp->er_extcount;
2983 * If nex2 extents fit in the current page, append
2984 * nex2_ep after the new extents.
2986 if (nex2 <= ext_avail) {
2987 i = erp->er_extcount;
2990 * Otherwise, check if space is available in the
2993 else if ((erp_idx < nlists - 1) &&
2994 (nex2 <= (ext_avail = XFS_LINEAR_EXTS -
2995 ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) {
2998 /* Create a hole for nex2 extents */
2999 memmove(&erp->er_extbuf[nex2], erp->er_extbuf,
3000 erp->er_extcount * sizeof(xfs_bmbt_rec_t));
3003 * Final choice, create a new extent page for
3008 erp = xfs_iext_irec_new(ifp, erp_idx);
3010 memmove(&erp->er_extbuf[i], nex2_ep, byte_diff);
3012 erp->er_extcount += nex2;
3013 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2);
3018 * This is called when the amount of space required for incore file
3019 * extents needs to be decreased. The ext_diff parameter stores the
3020 * number of extents to be removed and the idx parameter contains
3021 * the extent index where the extents will be removed from.
3023 * If the amount of space needed has decreased below the linear
3024 * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
3025 * extent array. Otherwise, use kmem_realloc() to adjust the
3026 * size to what is needed.
3030 xfs_inode_t *ip, /* incore inode pointer */
3031 xfs_extnum_t idx, /* index to begin removing exts */
3032 int ext_diff, /* number of extents to remove */
3033 int state) /* type of extent conversion */
3035 xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
3036 xfs_extnum_t nextents; /* number of extents in file */
3037 int new_size; /* size of extents after removal */
3039 trace_xfs_iext_remove(ip, idx, state, _RET_IP_);
3041 ASSERT(ext_diff > 0);
3042 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3043 new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t);
3045 if (new_size == 0) {
3046 xfs_iext_destroy(ifp);
3047 } else if (ifp->if_flags & XFS_IFEXTIREC) {
3048 xfs_iext_remove_indirect(ifp, idx, ext_diff);
3049 } else if (ifp->if_real_bytes) {
3050 xfs_iext_remove_direct(ifp, idx, ext_diff);
3052 xfs_iext_remove_inline(ifp, idx, ext_diff);
3054 ifp->if_bytes = new_size;
3058 * This removes ext_diff extents from the inline buffer, beginning
3059 * at extent index idx.
3062 xfs_iext_remove_inline(
3063 xfs_ifork_t *ifp, /* inode fork pointer */
3064 xfs_extnum_t idx, /* index to begin removing exts */
3065 int ext_diff) /* number of extents to remove */
3067 int nextents; /* number of extents in file */
3069 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
3070 ASSERT(idx < XFS_INLINE_EXTS);
3071 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3072 ASSERT(((nextents - ext_diff) > 0) &&
3073 (nextents - ext_diff) < XFS_INLINE_EXTS);
3075 if (idx + ext_diff < nextents) {
3076 memmove(&ifp->if_u2.if_inline_ext[idx],
3077 &ifp->if_u2.if_inline_ext[idx + ext_diff],
3078 (nextents - (idx + ext_diff)) *
3079 sizeof(xfs_bmbt_rec_t));
3080 memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff],
3081 0, ext_diff * sizeof(xfs_bmbt_rec_t));
3083 memset(&ifp->if_u2.if_inline_ext[idx], 0,
3084 ext_diff * sizeof(xfs_bmbt_rec_t));
3089 * This removes ext_diff extents from a linear (direct) extent list,
3090 * beginning at extent index idx. If the extents are being removed
3091 * from the end of the list (ie. truncate) then we just need to re-
3092 * allocate the list to remove the extra space. Otherwise, if the
3093 * extents are being removed from the middle of the existing extent
3094 * entries, then we first need to move the extent records beginning
3095 * at idx + ext_diff up in the list to overwrite the records being
3096 * removed, then remove the extra space via kmem_realloc.
3099 xfs_iext_remove_direct(
3100 xfs_ifork_t *ifp, /* inode fork pointer */
3101 xfs_extnum_t idx, /* index to begin removing exts */
3102 int ext_diff) /* number of extents to remove */
3104 xfs_extnum_t nextents; /* number of extents in file */
3105 int new_size; /* size of extents after removal */
3107 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
3108 new_size = ifp->if_bytes -
3109 (ext_diff * sizeof(xfs_bmbt_rec_t));
3110 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3112 if (new_size == 0) {
3113 xfs_iext_destroy(ifp);
3116 /* Move extents up in the list (if needed) */
3117 if (idx + ext_diff < nextents) {
3118 memmove(&ifp->if_u1.if_extents[idx],
3119 &ifp->if_u1.if_extents[idx + ext_diff],
3120 (nextents - (idx + ext_diff)) *
3121 sizeof(xfs_bmbt_rec_t));
3123 memset(&ifp->if_u1.if_extents[nextents - ext_diff],
3124 0, ext_diff * sizeof(xfs_bmbt_rec_t));
3126 * Reallocate the direct extent list. If the extents
3127 * will fit inside the inode then xfs_iext_realloc_direct
3128 * will switch from direct to inline extent allocation
3131 xfs_iext_realloc_direct(ifp, new_size);
3132 ifp->if_bytes = new_size;
3136 * This is called when incore extents are being removed from the
3137 * indirection array and the extents being removed span multiple extent
3138 * buffers. The idx parameter contains the file extent index where we
3139 * want to begin removing extents, and the count parameter contains
3140 * how many extents need to be removed.
3142 * |-------| |-------|
3143 * | nex1 | | | nex1 - number of extents before idx
3144 * |-------| | count |
3145 * | | | | count - number of extents being removed at idx
3146 * | count | |-------|
3147 * | | | nex2 | nex2 - number of extents after idx + count
3148 * |-------| |-------|
3151 xfs_iext_remove_indirect(
3152 xfs_ifork_t *ifp, /* inode fork pointer */
3153 xfs_extnum_t idx, /* index to begin removing extents */
3154 int count) /* number of extents to remove */
3156 xfs_ext_irec_t *erp; /* indirection array pointer */
3157 int erp_idx = 0; /* indirection array index */
3158 xfs_extnum_t ext_cnt; /* extents left to remove */
3159 xfs_extnum_t ext_diff; /* extents to remove in current list */
3160 xfs_extnum_t nex1; /* number of extents before idx */
3161 xfs_extnum_t nex2; /* extents after idx + count */
3162 int page_idx = idx; /* index in target extent list */
3164 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3165 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
3166 ASSERT(erp != NULL);
3170 nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0);
3171 ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1));
3173 * Check for deletion of entire list;
3174 * xfs_iext_irec_remove() updates extent offsets.
3176 if (ext_diff == erp->er_extcount) {
3177 xfs_iext_irec_remove(ifp, erp_idx);
3178 ext_cnt -= ext_diff;
3181 ASSERT(erp_idx < ifp->if_real_bytes /
3183 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3190 /* Move extents up (if needed) */
3192 memmove(&erp->er_extbuf[nex1],
3193 &erp->er_extbuf[nex1 + ext_diff],
3194 nex2 * sizeof(xfs_bmbt_rec_t));
3196 /* Zero out rest of page */
3197 memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ -
3198 ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t))));
3199 /* Update remaining counters */
3200 erp->er_extcount -= ext_diff;
3201 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff);
3202 ext_cnt -= ext_diff;
3207 ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t);
3208 xfs_iext_irec_compact(ifp);
3212 * Create, destroy, or resize a linear (direct) block of extents.
3215 xfs_iext_realloc_direct(
3216 xfs_ifork_t *ifp, /* inode fork pointer */
3217 int new_size) /* new size of extents */
3219 int rnew_size; /* real new size of extents */
3221 rnew_size = new_size;
3223 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) ||
3224 ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) &&
3225 (new_size != ifp->if_real_bytes)));
3227 /* Free extent records */
3228 if (new_size == 0) {
3229 xfs_iext_destroy(ifp);
3231 /* Resize direct extent list and zero any new bytes */
3232 else if (ifp->if_real_bytes) {
3233 /* Check if extents will fit inside the inode */
3234 if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) {
3235 xfs_iext_direct_to_inline(ifp, new_size /
3236 (uint)sizeof(xfs_bmbt_rec_t));
3237 ifp->if_bytes = new_size;
3240 if (!is_power_of_2(new_size)){
3241 rnew_size = roundup_pow_of_two(new_size);
3243 if (rnew_size != ifp->if_real_bytes) {
3244 ifp->if_u1.if_extents =
3245 kmem_realloc(ifp->if_u1.if_extents,
3247 ifp->if_real_bytes, KM_NOFS);
3249 if (rnew_size > ifp->if_real_bytes) {
3250 memset(&ifp->if_u1.if_extents[ifp->if_bytes /
3251 (uint)sizeof(xfs_bmbt_rec_t)], 0,
3252 rnew_size - ifp->if_real_bytes);
3256 * Switch from the inline extent buffer to a direct
3257 * extent list. Be sure to include the inline extent
3258 * bytes in new_size.
3261 new_size += ifp->if_bytes;
3262 if (!is_power_of_2(new_size)) {
3263 rnew_size = roundup_pow_of_two(new_size);
3265 xfs_iext_inline_to_direct(ifp, rnew_size);
3267 ifp->if_real_bytes = rnew_size;
3268 ifp->if_bytes = new_size;
3272 * Switch from linear (direct) extent records to inline buffer.
3275 xfs_iext_direct_to_inline(
3276 xfs_ifork_t *ifp, /* inode fork pointer */
3277 xfs_extnum_t nextents) /* number of extents in file */
3279 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
3280 ASSERT(nextents <= XFS_INLINE_EXTS);
3282 * The inline buffer was zeroed when we switched
3283 * from inline to direct extent allocation mode,
3284 * so we don't need to clear it here.
3286 memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents,
3287 nextents * sizeof(xfs_bmbt_rec_t));
3288 kmem_free(ifp->if_u1.if_extents);
3289 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
3290 ifp->if_real_bytes = 0;
3294 * Switch from inline buffer to linear (direct) extent records.
3295 * new_size should already be rounded up to the next power of 2
3296 * by the caller (when appropriate), so use new_size as it is.
3297 * However, since new_size may be rounded up, we can't update
3298 * if_bytes here. It is the caller's responsibility to update
3299 * if_bytes upon return.
3302 xfs_iext_inline_to_direct(
3303 xfs_ifork_t *ifp, /* inode fork pointer */
3304 int new_size) /* number of extents in file */
3306 ifp->if_u1.if_extents = kmem_alloc(new_size, KM_NOFS);
3307 memset(ifp->if_u1.if_extents, 0, new_size);
3308 if (ifp->if_bytes) {
3309 memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext,
3311 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
3312 sizeof(xfs_bmbt_rec_t));
3314 ifp->if_real_bytes = new_size;
3318 * Resize an extent indirection array to new_size bytes.
3321 xfs_iext_realloc_indirect(
3322 xfs_ifork_t *ifp, /* inode fork pointer */
3323 int new_size) /* new indirection array size */
3325 int nlists; /* number of irec's (ex lists) */
3326 int size; /* current indirection array size */
3328 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3329 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3330 size = nlists * sizeof(xfs_ext_irec_t);
3331 ASSERT(ifp->if_real_bytes);
3332 ASSERT((new_size >= 0) && (new_size != size));
3333 if (new_size == 0) {
3334 xfs_iext_destroy(ifp);
3336 ifp->if_u1.if_ext_irec = (xfs_ext_irec_t *)
3337 kmem_realloc(ifp->if_u1.if_ext_irec,
3338 new_size, size, KM_NOFS);
3343 * Switch from indirection array to linear (direct) extent allocations.
3346 xfs_iext_indirect_to_direct(
3347 xfs_ifork_t *ifp) /* inode fork pointer */
3349 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3350 xfs_extnum_t nextents; /* number of extents in file */
3351 int size; /* size of file extents */
3353 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3354 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3355 ASSERT(nextents <= XFS_LINEAR_EXTS);
3356 size = nextents * sizeof(xfs_bmbt_rec_t);
3358 xfs_iext_irec_compact_pages(ifp);
3359 ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ);
3361 ep = ifp->if_u1.if_ext_irec->er_extbuf;
3362 kmem_free(ifp->if_u1.if_ext_irec);
3363 ifp->if_flags &= ~XFS_IFEXTIREC;
3364 ifp->if_u1.if_extents = ep;
3365 ifp->if_bytes = size;
3366 if (nextents < XFS_LINEAR_EXTS) {
3367 xfs_iext_realloc_direct(ifp, size);
3372 * Free incore file extents.
3376 xfs_ifork_t *ifp) /* inode fork pointer */
3378 if (ifp->if_flags & XFS_IFEXTIREC) {
3382 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3383 for (erp_idx = nlists - 1; erp_idx >= 0 ; erp_idx--) {
3384 xfs_iext_irec_remove(ifp, erp_idx);
3386 ifp->if_flags &= ~XFS_IFEXTIREC;
3387 } else if (ifp->if_real_bytes) {
3388 kmem_free(ifp->if_u1.if_extents);
3389 } else if (ifp->if_bytes) {
3390 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
3391 sizeof(xfs_bmbt_rec_t));
3393 ifp->if_u1.if_extents = NULL;
3394 ifp->if_real_bytes = 0;
3399 * Return a pointer to the extent record for file system block bno.
3401 xfs_bmbt_rec_host_t * /* pointer to found extent record */
3402 xfs_iext_bno_to_ext(
3403 xfs_ifork_t *ifp, /* inode fork pointer */
3404 xfs_fileoff_t bno, /* block number to search for */
3405 xfs_extnum_t *idxp) /* index of target extent */
3407 xfs_bmbt_rec_host_t *base; /* pointer to first extent */
3408 xfs_filblks_t blockcount = 0; /* number of blocks in extent */
3409 xfs_bmbt_rec_host_t *ep = NULL; /* pointer to target extent */
3410 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
3411 int high; /* upper boundary in search */
3412 xfs_extnum_t idx = 0; /* index of target extent */
3413 int low; /* lower boundary in search */
3414 xfs_extnum_t nextents; /* number of file extents */
3415 xfs_fileoff_t startoff = 0; /* start offset of extent */
3417 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3418 if (nextents == 0) {
3423 if (ifp->if_flags & XFS_IFEXTIREC) {
3424 /* Find target extent list */
3426 erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx);
3427 base = erp->er_extbuf;
3428 high = erp->er_extcount - 1;
3430 base = ifp->if_u1.if_extents;
3431 high = nextents - 1;
3433 /* Binary search extent records */
3434 while (low <= high) {
3435 idx = (low + high) >> 1;
3437 startoff = xfs_bmbt_get_startoff(ep);
3438 blockcount = xfs_bmbt_get_blockcount(ep);
3439 if (bno < startoff) {
3441 } else if (bno >= startoff + blockcount) {
3444 /* Convert back to file-based extent index */
3445 if (ifp->if_flags & XFS_IFEXTIREC) {
3446 idx += erp->er_extoff;
3452 /* Convert back to file-based extent index */
3453 if (ifp->if_flags & XFS_IFEXTIREC) {
3454 idx += erp->er_extoff;
3456 if (bno >= startoff + blockcount) {
3457 if (++idx == nextents) {
3460 ep = xfs_iext_get_ext(ifp, idx);
3468 * Return a pointer to the indirection array entry containing the
3469 * extent record for filesystem block bno. Store the index of the
3470 * target irec in *erp_idxp.
3472 xfs_ext_irec_t * /* pointer to found extent record */
3473 xfs_iext_bno_to_irec(
3474 xfs_ifork_t *ifp, /* inode fork pointer */
3475 xfs_fileoff_t bno, /* block number to search for */
3476 int *erp_idxp) /* irec index of target ext list */
3478 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
3479 xfs_ext_irec_t *erp_next; /* next indirection array entry */
3480 int erp_idx; /* indirection array index */
3481 int nlists; /* number of extent irec's (lists) */
3482 int high; /* binary search upper limit */
3483 int low; /* binary search lower limit */
3485 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3486 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3490 while (low <= high) {
3491 erp_idx = (low + high) >> 1;
3492 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3493 erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL;
3494 if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) {
3496 } else if (erp_next && bno >=
3497 xfs_bmbt_get_startoff(erp_next->er_extbuf)) {
3503 *erp_idxp = erp_idx;
3508 * Return a pointer to the indirection array entry containing the
3509 * extent record at file extent index *idxp. Store the index of the
3510 * target irec in *erp_idxp and store the page index of the target
3511 * extent record in *idxp.
3514 xfs_iext_idx_to_irec(
3515 xfs_ifork_t *ifp, /* inode fork pointer */
3516 xfs_extnum_t *idxp, /* extent index (file -> page) */
3517 int *erp_idxp, /* pointer to target irec */
3518 int realloc) /* new bytes were just added */
3520 xfs_ext_irec_t *prev; /* pointer to previous irec */
3521 xfs_ext_irec_t *erp = NULL; /* pointer to current irec */
3522 int erp_idx; /* indirection array index */
3523 int nlists; /* number of irec's (ex lists) */
3524 int high; /* binary search upper limit */
3525 int low; /* binary search lower limit */
3526 xfs_extnum_t page_idx = *idxp; /* extent index in target list */
3528 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3529 ASSERT(page_idx >= 0);
3530 ASSERT(page_idx <= ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
3531 ASSERT(page_idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t) || realloc);
3533 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3538 /* Binary search extent irec's */
3539 while (low <= high) {
3540 erp_idx = (low + high) >> 1;
3541 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3542 prev = erp_idx > 0 ? erp - 1 : NULL;
3543 if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff &&
3544 realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) {
3546 } else if (page_idx > erp->er_extoff + erp->er_extcount ||
3547 (page_idx == erp->er_extoff + erp->er_extcount &&
3550 } else if (page_idx == erp->er_extoff + erp->er_extcount &&
3551 erp->er_extcount == XFS_LINEAR_EXTS) {
3555 erp = erp_idx < nlists ? erp + 1 : NULL;
3558 page_idx -= erp->er_extoff;
3563 *erp_idxp = erp_idx;
3568 * Allocate and initialize an indirection array once the space needed
3569 * for incore extents increases above XFS_IEXT_BUFSZ.
3573 xfs_ifork_t *ifp) /* inode fork pointer */
3575 xfs_ext_irec_t *erp; /* indirection array pointer */
3576 xfs_extnum_t nextents; /* number of extents in file */
3578 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
3579 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3580 ASSERT(nextents <= XFS_LINEAR_EXTS);
3582 erp = kmem_alloc(sizeof(xfs_ext_irec_t), KM_NOFS);
3584 if (nextents == 0) {
3585 ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
3586 } else if (!ifp->if_real_bytes) {
3587 xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ);
3588 } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) {
3589 xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ);
3591 erp->er_extbuf = ifp->if_u1.if_extents;
3592 erp->er_extcount = nextents;
3595 ifp->if_flags |= XFS_IFEXTIREC;
3596 ifp->if_real_bytes = XFS_IEXT_BUFSZ;
3597 ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t);
3598 ifp->if_u1.if_ext_irec = erp;
3604 * Allocate and initialize a new entry in the indirection array.
3608 xfs_ifork_t *ifp, /* inode fork pointer */
3609 int erp_idx) /* index for new irec */
3611 xfs_ext_irec_t *erp; /* indirection array pointer */
3612 int i; /* loop counter */
3613 int nlists; /* number of irec's (ex lists) */
3615 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3616 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3618 /* Resize indirection array */
3619 xfs_iext_realloc_indirect(ifp, ++nlists *
3620 sizeof(xfs_ext_irec_t));
3622 * Move records down in the array so the
3623 * new page can use erp_idx.
3625 erp = ifp->if_u1.if_ext_irec;
3626 for (i = nlists - 1; i > erp_idx; i--) {
3627 memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t));
3629 ASSERT(i == erp_idx);
3631 /* Initialize new extent record */
3632 erp = ifp->if_u1.if_ext_irec;
3633 erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
3634 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
3635 memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ);
3636 erp[erp_idx].er_extcount = 0;
3637 erp[erp_idx].er_extoff = erp_idx > 0 ?
3638 erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0;
3639 return (&erp[erp_idx]);
3643 * Remove a record from the indirection array.
3646 xfs_iext_irec_remove(
3647 xfs_ifork_t *ifp, /* inode fork pointer */
3648 int erp_idx) /* irec index to remove */
3650 xfs_ext_irec_t *erp; /* indirection array pointer */
3651 int i; /* loop counter */
3652 int nlists; /* number of irec's (ex lists) */
3654 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3655 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3656 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3657 if (erp->er_extbuf) {
3658 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1,
3660 kmem_free(erp->er_extbuf);
3662 /* Compact extent records */
3663 erp = ifp->if_u1.if_ext_irec;
3664 for (i = erp_idx; i < nlists - 1; i++) {
3665 memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t));
3668 * Manually free the last extent record from the indirection
3669 * array. A call to xfs_iext_realloc_indirect() with a size
3670 * of zero would result in a call to xfs_iext_destroy() which
3671 * would in turn call this function again, creating a nasty
3675 xfs_iext_realloc_indirect(ifp,
3676 nlists * sizeof(xfs_ext_irec_t));
3678 kmem_free(ifp->if_u1.if_ext_irec);
3680 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
3684 * This is called to clean up large amounts of unused memory allocated
3685 * by the indirection array. Before compacting anything though, verify
3686 * that the indirection array is still needed and switch back to the
3687 * linear extent list (or even the inline buffer) if possible. The
3688 * compaction policy is as follows:
3690 * Full Compaction: Extents fit into a single page (or inline buffer)
3691 * Partial Compaction: Extents occupy less than 50% of allocated space
3692 * No Compaction: Extents occupy at least 50% of allocated space
3695 xfs_iext_irec_compact(
3696 xfs_ifork_t *ifp) /* inode fork pointer */
3698 xfs_extnum_t nextents; /* number of extents in file */
3699 int nlists; /* number of irec's (ex lists) */
3701 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3702 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3703 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3705 if (nextents == 0) {
3706 xfs_iext_destroy(ifp);
3707 } else if (nextents <= XFS_INLINE_EXTS) {
3708 xfs_iext_indirect_to_direct(ifp);
3709 xfs_iext_direct_to_inline(ifp, nextents);
3710 } else if (nextents <= XFS_LINEAR_EXTS) {
3711 xfs_iext_indirect_to_direct(ifp);
3712 } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) {
3713 xfs_iext_irec_compact_pages(ifp);
3718 * Combine extents from neighboring extent pages.
3721 xfs_iext_irec_compact_pages(
3722 xfs_ifork_t *ifp) /* inode fork pointer */
3724 xfs_ext_irec_t *erp, *erp_next;/* pointers to irec entries */
3725 int erp_idx = 0; /* indirection array index */
3726 int nlists; /* number of irec's (ex lists) */
3728 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3729 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3730 while (erp_idx < nlists - 1) {
3731 erp = &ifp->if_u1.if_ext_irec[erp_idx];
3733 if (erp_next->er_extcount <=
3734 (XFS_LINEAR_EXTS - erp->er_extcount)) {
3735 memcpy(&erp->er_extbuf[erp->er_extcount],
3736 erp_next->er_extbuf, erp_next->er_extcount *
3737 sizeof(xfs_bmbt_rec_t));
3738 erp->er_extcount += erp_next->er_extcount;
3740 * Free page before removing extent record
3741 * so er_extoffs don't get modified in
3742 * xfs_iext_irec_remove.
3744 kmem_free(erp_next->er_extbuf);
3745 erp_next->er_extbuf = NULL;
3746 xfs_iext_irec_remove(ifp, erp_idx + 1);
3747 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3755 * This is called to update the er_extoff field in the indirection
3756 * array when extents have been added or removed from one of the
3757 * extent lists. erp_idx contains the irec index to begin updating
3758 * at and ext_diff contains the number of extents that were added
3762 xfs_iext_irec_update_extoffs(
3763 xfs_ifork_t *ifp, /* inode fork pointer */
3764 int erp_idx, /* irec index to update */
3765 int ext_diff) /* number of new extents */
3767 int i; /* loop counter */
3768 int nlists; /* number of irec's (ex lists */
3770 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
3771 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
3772 for (i = erp_idx; i < nlists; i++) {
3773 ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff;