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_format.h"
23 #include "xfs_log_format.h"
24 #include "xfs_trans_resv.h"
25 #include "xfs_mount.h"
26 #include "xfs_inode.h"
27 #include "xfs_trans.h"
28 #include "xfs_inode_item.h"
29 #include "xfs_bmap_btree.h"
31 #include "xfs_error.h"
32 #include "xfs_trace.h"
33 #include "xfs_attr_sf.h"
34 #include "xfs_da_format.h"
36 kmem_zone_t *xfs_ifork_zone;
38 STATIC int xfs_iformat_local(xfs_inode_t *, xfs_dinode_t *, int, int);
39 STATIC int xfs_iformat_extents(xfs_inode_t *, xfs_dinode_t *, int);
40 STATIC int xfs_iformat_btree(xfs_inode_t *, xfs_dinode_t *, int);
44 * Make sure that the extents in the given memory buffer
54 xfs_bmbt_rec_host_t rec;
57 for (i = 0; i < nrecs; i++) {
58 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
59 rec.l0 = get_unaligned(&ep->l0);
60 rec.l1 = get_unaligned(&ep->l1);
61 xfs_bmbt_get_all(&rec, &irec);
62 if (fmt == XFS_EXTFMT_NOSTATE)
63 ASSERT(irec.br_state == XFS_EXT_NORM);
67 #define xfs_validate_extents(ifp, nrecs, fmt)
72 * Move inode type and inode format specific information from the
73 * on-disk inode to the in-core inode. For fifos, devs, and sockets
74 * this means set if_rdev to the proper value. For files, directories,
75 * and symlinks this means to bring in the in-line data or extent
76 * pointers. For a file in B-tree format, only the root is immediately
77 * brought in-core. The rest will be in-lined in if_extents when it
78 * is first referenced (see xfs_iread_extents()).
85 xfs_attr_shortform_t *atp;
90 if (unlikely(be32_to_cpu(dip->di_nextents) +
91 be16_to_cpu(dip->di_anextents) >
92 be64_to_cpu(dip->di_nblocks))) {
94 "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.",
95 (unsigned long long)ip->i_ino,
96 (int)(be32_to_cpu(dip->di_nextents) +
97 be16_to_cpu(dip->di_anextents)),
99 be64_to_cpu(dip->di_nblocks));
100 XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW,
102 return -EFSCORRUPTED;
105 if (unlikely(dip->di_forkoff > ip->i_mount->m_sb.sb_inodesize)) {
106 xfs_warn(ip->i_mount, "corrupt dinode %Lu, forkoff = 0x%x.",
107 (unsigned long long)ip->i_ino,
109 XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW,
111 return -EFSCORRUPTED;
114 if (unlikely((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) &&
115 !ip->i_mount->m_rtdev_targp)) {
116 xfs_warn(ip->i_mount,
117 "corrupt dinode %Lu, has realtime flag set.",
119 XFS_CORRUPTION_ERROR("xfs_iformat(realtime)",
120 XFS_ERRLEVEL_LOW, ip->i_mount, dip);
121 return -EFSCORRUPTED;
124 if (unlikely(xfs_is_reflink_inode(ip) &&
125 (VFS_I(ip)->i_mode & S_IFMT) != S_IFREG)) {
126 xfs_warn(ip->i_mount,
127 "corrupt dinode %llu, wrong file type for reflink.",
129 XFS_CORRUPTION_ERROR("xfs_iformat(reflink)",
130 XFS_ERRLEVEL_LOW, ip->i_mount, dip);
131 return -EFSCORRUPTED;
134 if (unlikely(xfs_is_reflink_inode(ip) &&
135 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME))) {
136 xfs_warn(ip->i_mount,
137 "corrupt dinode %llu, has reflink+realtime flag set.",
139 XFS_CORRUPTION_ERROR("xfs_iformat(reflink)",
140 XFS_ERRLEVEL_LOW, ip->i_mount, dip);
141 return -EFSCORRUPTED;
144 switch (VFS_I(ip)->i_mode & S_IFMT) {
149 if (unlikely(dip->di_format != XFS_DINODE_FMT_DEV)) {
150 XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW,
152 return -EFSCORRUPTED;
155 ip->i_df.if_u2.if_rdev = xfs_dinode_get_rdev(dip);
161 switch (dip->di_format) {
162 case XFS_DINODE_FMT_LOCAL:
164 * no local regular files yet
166 if (unlikely(S_ISREG(be16_to_cpu(dip->di_mode)))) {
167 xfs_warn(ip->i_mount,
168 "corrupt inode %Lu (local format for regular file).",
169 (unsigned long long) ip->i_ino);
170 XFS_CORRUPTION_ERROR("xfs_iformat(4)",
173 return -EFSCORRUPTED;
176 di_size = be64_to_cpu(dip->di_size);
177 if (unlikely(di_size < 0 ||
178 di_size > XFS_DFORK_DSIZE(dip, ip->i_mount))) {
179 xfs_warn(ip->i_mount,
180 "corrupt inode %Lu (bad size %Ld for local inode).",
181 (unsigned long long) ip->i_ino,
182 (long long) di_size);
183 XFS_CORRUPTION_ERROR("xfs_iformat(5)",
186 return -EFSCORRUPTED;
190 error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size);
192 case XFS_DINODE_FMT_EXTENTS:
193 error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
195 case XFS_DINODE_FMT_BTREE:
196 error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
199 XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW,
201 return -EFSCORRUPTED;
206 XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW, ip->i_mount);
207 return -EFSCORRUPTED;
212 if (xfs_is_reflink_inode(ip)) {
213 ASSERT(ip->i_cowfp == NULL);
214 xfs_ifork_init_cow(ip);
217 if (!XFS_DFORK_Q(dip))
220 ASSERT(ip->i_afp == NULL);
221 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP | KM_NOFS);
223 switch (dip->di_aformat) {
224 case XFS_DINODE_FMT_LOCAL:
225 atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
226 size = be16_to_cpu(atp->hdr.totsize);
228 if (unlikely(size < sizeof(struct xfs_attr_sf_hdr))) {
229 xfs_warn(ip->i_mount,
230 "corrupt inode %Lu (bad attr fork size %Ld).",
231 (unsigned long long) ip->i_ino,
233 XFS_CORRUPTION_ERROR("xfs_iformat(8)",
236 error = -EFSCORRUPTED;
240 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size);
242 case XFS_DINODE_FMT_EXTENTS:
243 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
245 case XFS_DINODE_FMT_BTREE:
246 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
249 error = -EFSCORRUPTED;
253 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
256 kmem_zone_free(xfs_ifork_zone, ip->i_cowfp);
258 xfs_idestroy_fork(ip, XFS_DATA_FORK);
265 struct xfs_inode *ip,
270 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
271 int mem_size = size, real_size = 0;
275 * If we are using the local fork to store a symlink body we need to
276 * zero-terminate it so that we can pass it back to the VFS directly.
277 * Overallocate the in-memory fork by one for that and add a zero
278 * to terminate it below.
280 zero_terminate = S_ISLNK(VFS_I(ip)->i_mode);
285 ifp->if_u1.if_data = NULL;
286 else if (mem_size <= sizeof(ifp->if_u2.if_inline_data))
287 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
289 real_size = roundup(mem_size, 4);
290 ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP | KM_NOFS);
294 memcpy(ifp->if_u1.if_data, data, size);
296 ifp->if_u1.if_data[size] = '\0';
299 ifp->if_bytes = size;
300 ifp->if_real_bytes = real_size;
301 ifp->if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
302 ifp->if_flags |= XFS_IFINLINE;
306 * The file is in-lined in the on-disk inode.
307 * If it fits into if_inline_data, then copy
308 * it there, otherwise allocate a buffer for it
309 * and copy the data there. Either way, set
310 * if_data to point at the data.
311 * If we allocate a buffer for the data, make
312 * sure that its size is a multiple of 4 and
313 * record the real size in i_real_bytes.
324 * If the size is unreasonable, then something
325 * is wrong and we just bail out rather than crash in
326 * kmem_alloc() or memcpy() below.
328 if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
329 xfs_warn(ip->i_mount,
330 "corrupt inode %Lu (bad size %d for local fork, size = %d).",
331 (unsigned long long) ip->i_ino, size,
332 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
333 XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW,
335 return -EFSCORRUPTED;
338 xfs_init_local_fork(ip, whichfork, XFS_DFORK_PTR(dip, whichfork), size);
343 * The file consists of a set of extents all
344 * of which fit into the on-disk inode.
345 * If there are few enough extents to fit into
346 * the if_inline_ext, then copy them there.
347 * Otherwise allocate a buffer for them and copy
348 * them into it. Either way, set if_extents
349 * to point at the extents.
363 ifp = XFS_IFORK_PTR(ip, whichfork);
364 nex = XFS_DFORK_NEXTENTS(dip, whichfork);
365 size = nex * (uint)sizeof(xfs_bmbt_rec_t);
368 * If the number of extents is unreasonable, then something
369 * is wrong and we just bail out rather than crash in
370 * kmem_alloc() or memcpy() below.
372 if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
373 xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).",
374 (unsigned long long) ip->i_ino, nex);
375 XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW,
377 return -EFSCORRUPTED;
380 ifp->if_real_bytes = 0;
382 ifp->if_u1.if_extents = NULL;
383 else if (nex <= XFS_INLINE_EXTS)
384 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
386 xfs_iext_add(ifp, 0, nex);
388 ifp->if_bytes = size;
390 dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
391 xfs_validate_extents(ifp, nex, XFS_EXTFMT_INODE(ip));
392 for (i = 0; i < nex; i++, dp++) {
393 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
394 ep->l0 = get_unaligned_be64(&dp->l0);
395 ep->l1 = get_unaligned_be64(&dp->l1);
397 XFS_BMAP_TRACE_EXLIST(ip, nex, whichfork);
398 if (whichfork != XFS_DATA_FORK ||
399 XFS_EXTFMT_INODE(ip) == XFS_EXTFMT_NOSTATE)
400 if (unlikely(xfs_check_nostate_extents(
402 XFS_ERROR_REPORT("xfs_iformat_extents(2)",
405 return -EFSCORRUPTED;
408 ifp->if_flags |= XFS_IFEXTENTS;
413 * The file has too many extents to fit into
414 * the inode, so they are in B-tree format.
415 * Allocate a buffer for the root of the B-tree
416 * and copy the root into it. The i_extents
417 * field will remain NULL until all of the
418 * extents are read in (when they are needed).
426 struct xfs_mount *mp = ip->i_mount;
427 xfs_bmdr_block_t *dfp;
433 ifp = XFS_IFORK_PTR(ip, whichfork);
434 dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
435 size = XFS_BMAP_BROOT_SPACE(mp, dfp);
436 nrecs = be16_to_cpu(dfp->bb_numrecs);
439 * blow out if -- fork has less extents than can fit in
440 * fork (fork shouldn't be a btree format), root btree
441 * block has more records than can fit into the fork,
442 * or the number of extents is greater than the number of
445 if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <=
446 XFS_IFORK_MAXEXT(ip, whichfork) ||
447 XFS_BMDR_SPACE_CALC(nrecs) >
448 XFS_DFORK_SIZE(dip, mp, whichfork) ||
449 XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks)) {
450 xfs_warn(mp, "corrupt inode %Lu (btree).",
451 (unsigned long long) ip->i_ino);
452 XFS_CORRUPTION_ERROR("xfs_iformat_btree", XFS_ERRLEVEL_LOW,
454 return -EFSCORRUPTED;
457 ifp->if_broot_bytes = size;
458 ifp->if_broot = kmem_alloc(size, KM_SLEEP | KM_NOFS);
459 ASSERT(ifp->if_broot != NULL);
461 * Copy and convert from the on-disk structure
462 * to the in-memory structure.
464 xfs_bmdr_to_bmbt(ip, dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
465 ifp->if_broot, size);
466 ifp->if_flags &= ~XFS_IFEXTENTS;
467 ifp->if_flags |= XFS_IFBROOT;
473 * Read in extents from a btree-format inode.
474 * Allocate and fill in if_extents. Real work is done in xfs_bmap.c.
484 xfs_extnum_t nextents;
486 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
488 if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
489 XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW,
491 return -EFSCORRUPTED;
493 nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
494 ifp = XFS_IFORK_PTR(ip, whichfork);
497 * We know that the size is valid (it's checked in iformat_btree)
499 ifp->if_bytes = ifp->if_real_bytes = 0;
500 ifp->if_flags |= XFS_IFEXTENTS;
501 xfs_iext_add(ifp, 0, nextents);
502 error = xfs_bmap_read_extents(tp, ip, whichfork);
504 xfs_iext_destroy(ifp);
505 ifp->if_flags &= ~XFS_IFEXTENTS;
508 xfs_validate_extents(ifp, nextents, XFS_EXTFMT_INODE(ip));
512 * Reallocate the space for if_broot based on the number of records
513 * being added or deleted as indicated in rec_diff. Move the records
514 * and pointers in if_broot to fit the new size. When shrinking this
515 * will eliminate holes between the records and pointers created by
516 * the caller. When growing this will create holes to be filled in
519 * The caller must not request to add more records than would fit in
520 * the on-disk inode root. If the if_broot is currently NULL, then
521 * if we are adding records, one will be allocated. The caller must also
522 * not request that the number of records go below zero, although
525 * ip -- the inode whose if_broot area is changing
526 * ext_diff -- the change in the number of records, positive or negative,
527 * requested for the if_broot array.
535 struct xfs_mount *mp = ip->i_mount;
538 struct xfs_btree_block *new_broot;
545 * Handle the degenerate case quietly.
551 ifp = XFS_IFORK_PTR(ip, whichfork);
554 * If there wasn't any memory allocated before, just
555 * allocate it now and get out.
557 if (ifp->if_broot_bytes == 0) {
558 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
559 ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
560 ifp->if_broot_bytes = (int)new_size;
565 * If there is already an existing if_broot, then we need
566 * to realloc() it and shift the pointers to their new
567 * location. The records don't change location because
568 * they are kept butted up against the btree block header.
570 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
571 new_max = cur_max + rec_diff;
572 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
573 ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
575 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
576 ifp->if_broot_bytes);
577 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
579 ifp->if_broot_bytes = (int)new_size;
580 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
581 XFS_IFORK_SIZE(ip, whichfork));
582 memmove(np, op, cur_max * (uint)sizeof(xfs_fsblock_t));
587 * rec_diff is less than 0. In this case, we are shrinking the
588 * if_broot buffer. It must already exist. If we go to zero
589 * records, just get rid of the root and clear the status bit.
591 ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
592 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
593 new_max = cur_max + rec_diff;
594 ASSERT(new_max >= 0);
596 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
600 new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
602 * First copy over the btree block header.
604 memcpy(new_broot, ifp->if_broot,
605 XFS_BMBT_BLOCK_LEN(ip->i_mount));
608 ifp->if_flags &= ~XFS_IFBROOT;
612 * Only copy the records and pointers if there are any.
616 * First copy the records.
618 op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
619 np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
620 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
623 * Then copy the pointers.
625 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
626 ifp->if_broot_bytes);
627 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
629 memcpy(np, op, new_max * (uint)sizeof(xfs_fsblock_t));
631 kmem_free(ifp->if_broot);
632 ifp->if_broot = new_broot;
633 ifp->if_broot_bytes = (int)new_size;
635 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
636 XFS_IFORK_SIZE(ip, whichfork));
642 * This is called when the amount of space needed for if_data
643 * is increased or decreased. The change in size is indicated by
644 * the number of bytes that need to be added or deleted in the
645 * byte_diff parameter.
647 * If the amount of space needed has decreased below the size of the
648 * inline buffer, then switch to using the inline buffer. Otherwise,
649 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
652 * ip -- the inode whose if_data area is changing
653 * byte_diff -- the change in the number of bytes, positive or negative,
654 * requested for the if_data array.
666 if (byte_diff == 0) {
670 ifp = XFS_IFORK_PTR(ip, whichfork);
671 new_size = (int)ifp->if_bytes + byte_diff;
672 ASSERT(new_size >= 0);
675 if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
676 kmem_free(ifp->if_u1.if_data);
678 ifp->if_u1.if_data = NULL;
680 } else if (new_size <= sizeof(ifp->if_u2.if_inline_data)) {
682 * If the valid extents/data can fit in if_inline_ext/data,
683 * copy them from the malloc'd vector and free it.
685 if (ifp->if_u1.if_data == NULL) {
686 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
687 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
688 ASSERT(ifp->if_real_bytes != 0);
689 memcpy(ifp->if_u2.if_inline_data, ifp->if_u1.if_data,
691 kmem_free(ifp->if_u1.if_data);
692 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
697 * Stuck with malloc/realloc.
698 * For inline data, the underlying buffer must be
699 * a multiple of 4 bytes in size so that it can be
700 * logged and stay on word boundaries. We enforce
703 real_size = roundup(new_size, 4);
704 if (ifp->if_u1.if_data == NULL) {
705 ASSERT(ifp->if_real_bytes == 0);
706 ifp->if_u1.if_data = kmem_alloc(real_size,
708 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
710 * Only do the realloc if the underlying size
711 * is really changing.
713 if (ifp->if_real_bytes != real_size) {
715 kmem_realloc(ifp->if_u1.if_data,
720 ASSERT(ifp->if_real_bytes == 0);
721 ifp->if_u1.if_data = kmem_alloc(real_size,
723 memcpy(ifp->if_u1.if_data, ifp->if_u2.if_inline_data,
727 ifp->if_real_bytes = real_size;
728 ifp->if_bytes = new_size;
729 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
739 ifp = XFS_IFORK_PTR(ip, whichfork);
740 if (ifp->if_broot != NULL) {
741 kmem_free(ifp->if_broot);
742 ifp->if_broot = NULL;
746 * If the format is local, then we can't have an extents
747 * array so just look for an inline data array. If we're
748 * not local then we may or may not have an extents list,
749 * so check and free it up if we do.
751 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
752 if ((ifp->if_u1.if_data != ifp->if_u2.if_inline_data) &&
753 (ifp->if_u1.if_data != NULL)) {
754 ASSERT(ifp->if_real_bytes != 0);
755 kmem_free(ifp->if_u1.if_data);
756 ifp->if_u1.if_data = NULL;
757 ifp->if_real_bytes = 0;
759 } else if ((ifp->if_flags & XFS_IFEXTENTS) &&
760 ((ifp->if_flags & XFS_IFEXTIREC) ||
761 ((ifp->if_u1.if_extents != NULL) &&
762 (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext)))) {
763 ASSERT(ifp->if_real_bytes != 0);
764 xfs_iext_destroy(ifp);
766 ASSERT(ifp->if_u1.if_extents == NULL ||
767 ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext);
768 ASSERT(ifp->if_real_bytes == 0);
769 if (whichfork == XFS_ATTR_FORK) {
770 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
772 } else if (whichfork == XFS_COW_FORK) {
773 kmem_zone_free(xfs_ifork_zone, ip->i_cowfp);
778 /* Count number of incore extents based on if_bytes */
780 xfs_iext_count(struct xfs_ifork *ifp)
782 return ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
786 * Convert in-core extents to on-disk form
788 * For either the data or attr fork in extent format, we need to endian convert
789 * the in-core extent as we place them into the on-disk inode.
791 * In the case of the data fork, the in-core and on-disk fork sizes can be
792 * different due to delayed allocation extents. We only copy on-disk extents
793 * here, so callers must always use the physical fork size to determine the
794 * size of the buffer passed to this routine. We will return the size actually
807 xfs_fsblock_t start_block;
809 ifp = XFS_IFORK_PTR(ip, whichfork);
810 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
811 ASSERT(ifp->if_bytes > 0);
813 nrecs = xfs_iext_count(ifp);
814 XFS_BMAP_TRACE_EXLIST(ip, nrecs, whichfork);
818 * There are some delayed allocation extents in the
819 * inode, so copy the extents one at a time and skip
820 * the delayed ones. There must be at least one
821 * non-delayed extent.
824 for (i = 0; i < nrecs; i++) {
825 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
826 start_block = xfs_bmbt_get_startblock(ep);
827 if (isnullstartblock(start_block)) {
829 * It's a delayed allocation extent, so skip it.
834 /* Translate to on disk format */
835 put_unaligned_be64(ep->l0, &dp->l0);
836 put_unaligned_be64(ep->l1, &dp->l1);
841 xfs_validate_extents(ifp, copied, XFS_EXTFMT_INODE(ip));
843 return (copied * (uint)sizeof(xfs_bmbt_rec_t));
847 * Each of the following cases stores data into the same region
848 * of the on-disk inode, so only one of them can be valid at
849 * any given time. While it is possible to have conflicting formats
850 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
851 * in EXTENTS format, this can only happen when the fork has
852 * changed formats after being modified but before being flushed.
853 * In these cases, the format always takes precedence, because the
854 * format indicates the current state of the fork.
860 xfs_inode_log_item_t *iip,
866 static const short brootflag[2] =
867 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
868 static const short dataflag[2] =
869 { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
870 static const short extflag[2] =
871 { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
875 ifp = XFS_IFORK_PTR(ip, whichfork);
877 * This can happen if we gave up in iformat in an error path,
878 * for the attribute fork.
881 ASSERT(whichfork == XFS_ATTR_FORK);
884 cp = XFS_DFORK_PTR(dip, whichfork);
886 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
887 case XFS_DINODE_FMT_LOCAL:
888 if ((iip->ili_fields & dataflag[whichfork]) &&
889 (ifp->if_bytes > 0)) {
890 ASSERT(ifp->if_u1.if_data != NULL);
891 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
892 memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
896 case XFS_DINODE_FMT_EXTENTS:
897 ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
898 !(iip->ili_fields & extflag[whichfork]));
899 if ((iip->ili_fields & extflag[whichfork]) &&
900 (ifp->if_bytes > 0)) {
901 ASSERT(xfs_iext_get_ext(ifp, 0));
902 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
903 (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
908 case XFS_DINODE_FMT_BTREE:
909 if ((iip->ili_fields & brootflag[whichfork]) &&
910 (ifp->if_broot_bytes > 0)) {
911 ASSERT(ifp->if_broot != NULL);
912 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
913 XFS_IFORK_SIZE(ip, whichfork));
914 xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
915 (xfs_bmdr_block_t *)cp,
916 XFS_DFORK_SIZE(dip, mp, whichfork));
920 case XFS_DINODE_FMT_DEV:
921 if (iip->ili_fields & XFS_ILOG_DEV) {
922 ASSERT(whichfork == XFS_DATA_FORK);
923 xfs_dinode_put_rdev(dip, ip->i_df.if_u2.if_rdev);
927 case XFS_DINODE_FMT_UUID:
928 if (iip->ili_fields & XFS_ILOG_UUID) {
929 ASSERT(whichfork == XFS_DATA_FORK);
930 memcpy(XFS_DFORK_DPTR(dip),
931 &ip->i_df.if_u2.if_uuid,
943 * Return a pointer to the extent record at file index idx.
945 xfs_bmbt_rec_host_t *
947 xfs_ifork_t *ifp, /* inode fork pointer */
948 xfs_extnum_t idx) /* index of target extent */
951 ASSERT(idx < xfs_iext_count(ifp));
953 if ((ifp->if_flags & XFS_IFEXTIREC) && (idx == 0)) {
954 return ifp->if_u1.if_ext_irec->er_extbuf;
955 } else if (ifp->if_flags & XFS_IFEXTIREC) {
956 xfs_ext_irec_t *erp; /* irec pointer */
957 int erp_idx = 0; /* irec index */
958 xfs_extnum_t page_idx = idx; /* ext index in target list */
960 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
961 return &erp->er_extbuf[page_idx];
962 } else if (ifp->if_bytes) {
963 return &ifp->if_u1.if_extents[idx];
969 /* Convert bmap state flags to an inode fork. */
971 xfs_iext_state_to_fork(
972 struct xfs_inode *ip,
975 if (state & BMAP_COWFORK)
977 else if (state & BMAP_ATTRFORK)
983 * Insert new item(s) into the extent records for incore inode
984 * fork 'ifp'. 'count' new items are inserted at index 'idx'.
988 xfs_inode_t *ip, /* incore inode pointer */
989 xfs_extnum_t idx, /* starting index of new items */
990 xfs_extnum_t count, /* number of inserted items */
991 xfs_bmbt_irec_t *new, /* items to insert */
992 int state) /* type of extent conversion */
994 xfs_ifork_t *ifp = xfs_iext_state_to_fork(ip, state);
995 xfs_extnum_t i; /* extent record index */
997 trace_xfs_iext_insert(ip, idx, new, state, _RET_IP_);
999 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1000 xfs_iext_add(ifp, idx, count);
1001 for (i = idx; i < idx + count; i++, new++)
1002 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, i), new);
1006 * This is called when the amount of space required for incore file
1007 * extents needs to be increased. The ext_diff parameter stores the
1008 * number of new extents being added and the idx parameter contains
1009 * the extent index where the new extents will be added. If the new
1010 * extents are being appended, then we just need to (re)allocate and
1011 * initialize the space. Otherwise, if the new extents are being
1012 * inserted into the middle of the existing entries, a bit more work
1013 * is required to make room for the new extents to be inserted. The
1014 * caller is responsible for filling in the new extent entries upon
1019 xfs_ifork_t *ifp, /* inode fork pointer */
1020 xfs_extnum_t idx, /* index to begin adding exts */
1021 int ext_diff) /* number of extents to add */
1023 int byte_diff; /* new bytes being added */
1024 int new_size; /* size of extents after adding */
1025 xfs_extnum_t nextents; /* number of extents in file */
1027 nextents = xfs_iext_count(ifp);
1028 ASSERT((idx >= 0) && (idx <= nextents));
1029 byte_diff = ext_diff * sizeof(xfs_bmbt_rec_t);
1030 new_size = ifp->if_bytes + byte_diff;
1032 * If the new number of extents (nextents + ext_diff)
1033 * fits inside the inode, then continue to use the inline
1036 if (nextents + ext_diff <= XFS_INLINE_EXTS) {
1037 if (idx < nextents) {
1038 memmove(&ifp->if_u2.if_inline_ext[idx + ext_diff],
1039 &ifp->if_u2.if_inline_ext[idx],
1040 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
1041 memset(&ifp->if_u2.if_inline_ext[idx], 0, byte_diff);
1043 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
1044 ifp->if_real_bytes = 0;
1047 * Otherwise use a linear (direct) extent list.
1048 * If the extents are currently inside the inode,
1049 * xfs_iext_realloc_direct will switch us from
1050 * inline to direct extent allocation mode.
1052 else if (nextents + ext_diff <= XFS_LINEAR_EXTS) {
1053 xfs_iext_realloc_direct(ifp, new_size);
1054 if (idx < nextents) {
1055 memmove(&ifp->if_u1.if_extents[idx + ext_diff],
1056 &ifp->if_u1.if_extents[idx],
1057 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
1058 memset(&ifp->if_u1.if_extents[idx], 0, byte_diff);
1061 /* Indirection array */
1063 xfs_ext_irec_t *erp;
1067 ASSERT(nextents + ext_diff > XFS_LINEAR_EXTS);
1068 if (ifp->if_flags & XFS_IFEXTIREC) {
1069 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 1);
1071 xfs_iext_irec_init(ifp);
1072 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1073 erp = ifp->if_u1.if_ext_irec;
1075 /* Extents fit in target extent page */
1076 if (erp && erp->er_extcount + ext_diff <= XFS_LINEAR_EXTS) {
1077 if (page_idx < erp->er_extcount) {
1078 memmove(&erp->er_extbuf[page_idx + ext_diff],
1079 &erp->er_extbuf[page_idx],
1080 (erp->er_extcount - page_idx) *
1081 sizeof(xfs_bmbt_rec_t));
1082 memset(&erp->er_extbuf[page_idx], 0, byte_diff);
1084 erp->er_extcount += ext_diff;
1085 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
1087 /* Insert a new extent page */
1089 xfs_iext_add_indirect_multi(ifp,
1090 erp_idx, page_idx, ext_diff);
1093 * If extent(s) are being appended to the last page in
1094 * the indirection array and the new extent(s) don't fit
1095 * in the page, then erp is NULL and erp_idx is set to
1096 * the next index needed in the indirection array.
1099 uint count = ext_diff;
1102 erp = xfs_iext_irec_new(ifp, erp_idx);
1103 erp->er_extcount = min(count, XFS_LINEAR_EXTS);
1104 count -= erp->er_extcount;
1110 ifp->if_bytes = new_size;
1114 * This is called when incore extents are being added to the indirection
1115 * array and the new extents do not fit in the target extent list. The
1116 * erp_idx parameter contains the irec index for the target extent list
1117 * in the indirection array, and the idx parameter contains the extent
1118 * index within the list. The number of extents being added is stored
1119 * in the count parameter.
1121 * |-------| |-------|
1122 * | | | | idx - number of extents before idx
1124 * | | | | count - number of extents being inserted at idx
1125 * |-------| |-------|
1126 * | count | | nex2 | nex2 - number of extents after idx + count
1127 * |-------| |-------|
1130 xfs_iext_add_indirect_multi(
1131 xfs_ifork_t *ifp, /* inode fork pointer */
1132 int erp_idx, /* target extent irec index */
1133 xfs_extnum_t idx, /* index within target list */
1134 int count) /* new extents being added */
1136 int byte_diff; /* new bytes being added */
1137 xfs_ext_irec_t *erp; /* pointer to irec entry */
1138 xfs_extnum_t ext_diff; /* number of extents to add */
1139 xfs_extnum_t ext_cnt; /* new extents still needed */
1140 xfs_extnum_t nex2; /* extents after idx + count */
1141 xfs_bmbt_rec_t *nex2_ep = NULL; /* temp list for nex2 extents */
1142 int nlists; /* number of irec's (lists) */
1144 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1145 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1146 nex2 = erp->er_extcount - idx;
1147 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1150 * Save second part of target extent list
1151 * (all extents past */
1153 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
1154 nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_NOFS);
1155 memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff);
1156 erp->er_extcount -= nex2;
1157 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2);
1158 memset(&erp->er_extbuf[idx], 0, byte_diff);
1162 * Add the new extents to the end of the target
1163 * list, then allocate new irec record(s) and
1164 * extent buffer(s) as needed to store the rest
1165 * of the new extents.
1168 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount);
1170 erp->er_extcount += ext_diff;
1171 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
1172 ext_cnt -= ext_diff;
1176 erp = xfs_iext_irec_new(ifp, erp_idx);
1177 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS);
1178 erp->er_extcount = ext_diff;
1179 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
1180 ext_cnt -= ext_diff;
1183 /* Add nex2 extents back to indirection array */
1185 xfs_extnum_t ext_avail;
1188 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
1189 ext_avail = XFS_LINEAR_EXTS - erp->er_extcount;
1192 * If nex2 extents fit in the current page, append
1193 * nex2_ep after the new extents.
1195 if (nex2 <= ext_avail) {
1196 i = erp->er_extcount;
1199 * Otherwise, check if space is available in the
1202 else if ((erp_idx < nlists - 1) &&
1203 (nex2 <= (ext_avail = XFS_LINEAR_EXTS -
1204 ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) {
1207 /* Create a hole for nex2 extents */
1208 memmove(&erp->er_extbuf[nex2], erp->er_extbuf,
1209 erp->er_extcount * sizeof(xfs_bmbt_rec_t));
1212 * Final choice, create a new extent page for
1217 erp = xfs_iext_irec_new(ifp, erp_idx);
1219 memmove(&erp->er_extbuf[i], nex2_ep, byte_diff);
1221 erp->er_extcount += nex2;
1222 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2);
1227 * This is called when the amount of space required for incore file
1228 * extents needs to be decreased. The ext_diff parameter stores the
1229 * number of extents to be removed and the idx parameter contains
1230 * the extent index where the extents will be removed from.
1232 * If the amount of space needed has decreased below the linear
1233 * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
1234 * extent array. Otherwise, use kmem_realloc() to adjust the
1235 * size to what is needed.
1239 xfs_inode_t *ip, /* incore inode pointer */
1240 xfs_extnum_t idx, /* index to begin removing exts */
1241 int ext_diff, /* number of extents to remove */
1242 int state) /* type of extent conversion */
1244 xfs_ifork_t *ifp = xfs_iext_state_to_fork(ip, state);
1245 xfs_extnum_t nextents; /* number of extents in file */
1246 int new_size; /* size of extents after removal */
1248 trace_xfs_iext_remove(ip, idx, state, _RET_IP_);
1250 ASSERT(ext_diff > 0);
1251 nextents = xfs_iext_count(ifp);
1252 new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t);
1254 if (new_size == 0) {
1255 xfs_iext_destroy(ifp);
1256 } else if (ifp->if_flags & XFS_IFEXTIREC) {
1257 xfs_iext_remove_indirect(ifp, idx, ext_diff);
1258 } else if (ifp->if_real_bytes) {
1259 xfs_iext_remove_direct(ifp, idx, ext_diff);
1261 xfs_iext_remove_inline(ifp, idx, ext_diff);
1263 ifp->if_bytes = new_size;
1267 * This removes ext_diff extents from the inline buffer, beginning
1268 * at extent index idx.
1271 xfs_iext_remove_inline(
1272 xfs_ifork_t *ifp, /* inode fork pointer */
1273 xfs_extnum_t idx, /* index to begin removing exts */
1274 int ext_diff) /* number of extents to remove */
1276 int nextents; /* number of extents in file */
1278 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
1279 ASSERT(idx < XFS_INLINE_EXTS);
1280 nextents = xfs_iext_count(ifp);
1281 ASSERT(((nextents - ext_diff) > 0) &&
1282 (nextents - ext_diff) < XFS_INLINE_EXTS);
1284 if (idx + ext_diff < nextents) {
1285 memmove(&ifp->if_u2.if_inline_ext[idx],
1286 &ifp->if_u2.if_inline_ext[idx + ext_diff],
1287 (nextents - (idx + ext_diff)) *
1288 sizeof(xfs_bmbt_rec_t));
1289 memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff],
1290 0, ext_diff * sizeof(xfs_bmbt_rec_t));
1292 memset(&ifp->if_u2.if_inline_ext[idx], 0,
1293 ext_diff * sizeof(xfs_bmbt_rec_t));
1298 * This removes ext_diff extents from a linear (direct) extent list,
1299 * beginning at extent index idx. If the extents are being removed
1300 * from the end of the list (ie. truncate) then we just need to re-
1301 * allocate the list to remove the extra space. Otherwise, if the
1302 * extents are being removed from the middle of the existing extent
1303 * entries, then we first need to move the extent records beginning
1304 * at idx + ext_diff up in the list to overwrite the records being
1305 * removed, then remove the extra space via kmem_realloc.
1308 xfs_iext_remove_direct(
1309 xfs_ifork_t *ifp, /* inode fork pointer */
1310 xfs_extnum_t idx, /* index to begin removing exts */
1311 int ext_diff) /* number of extents to remove */
1313 xfs_extnum_t nextents; /* number of extents in file */
1314 int new_size; /* size of extents after removal */
1316 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
1317 new_size = ifp->if_bytes -
1318 (ext_diff * sizeof(xfs_bmbt_rec_t));
1319 nextents = xfs_iext_count(ifp);
1321 if (new_size == 0) {
1322 xfs_iext_destroy(ifp);
1325 /* Move extents up in the list (if needed) */
1326 if (idx + ext_diff < nextents) {
1327 memmove(&ifp->if_u1.if_extents[idx],
1328 &ifp->if_u1.if_extents[idx + ext_diff],
1329 (nextents - (idx + ext_diff)) *
1330 sizeof(xfs_bmbt_rec_t));
1332 memset(&ifp->if_u1.if_extents[nextents - ext_diff],
1333 0, ext_diff * sizeof(xfs_bmbt_rec_t));
1335 * Reallocate the direct extent list. If the extents
1336 * will fit inside the inode then xfs_iext_realloc_direct
1337 * will switch from direct to inline extent allocation
1340 xfs_iext_realloc_direct(ifp, new_size);
1341 ifp->if_bytes = new_size;
1345 * This is called when incore extents are being removed from the
1346 * indirection array and the extents being removed span multiple extent
1347 * buffers. The idx parameter contains the file extent index where we
1348 * want to begin removing extents, and the count parameter contains
1349 * how many extents need to be removed.
1351 * |-------| |-------|
1352 * | nex1 | | | nex1 - number of extents before idx
1353 * |-------| | count |
1354 * | | | | count - number of extents being removed at idx
1355 * | count | |-------|
1356 * | | | nex2 | nex2 - number of extents after idx + count
1357 * |-------| |-------|
1360 xfs_iext_remove_indirect(
1361 xfs_ifork_t *ifp, /* inode fork pointer */
1362 xfs_extnum_t idx, /* index to begin removing extents */
1363 int count) /* number of extents to remove */
1365 xfs_ext_irec_t *erp; /* indirection array pointer */
1366 int erp_idx = 0; /* indirection array index */
1367 xfs_extnum_t ext_cnt; /* extents left to remove */
1368 xfs_extnum_t ext_diff; /* extents to remove in current list */
1369 xfs_extnum_t nex1; /* number of extents before idx */
1370 xfs_extnum_t nex2; /* extents after idx + count */
1371 int page_idx = idx; /* index in target extent list */
1373 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1374 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
1375 ASSERT(erp != NULL);
1379 nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0);
1380 ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1));
1382 * Check for deletion of entire list;
1383 * xfs_iext_irec_remove() updates extent offsets.
1385 if (ext_diff == erp->er_extcount) {
1386 xfs_iext_irec_remove(ifp, erp_idx);
1387 ext_cnt -= ext_diff;
1390 ASSERT(erp_idx < ifp->if_real_bytes /
1392 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1399 /* Move extents up (if needed) */
1401 memmove(&erp->er_extbuf[nex1],
1402 &erp->er_extbuf[nex1 + ext_diff],
1403 nex2 * sizeof(xfs_bmbt_rec_t));
1405 /* Zero out rest of page */
1406 memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ -
1407 ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t))));
1408 /* Update remaining counters */
1409 erp->er_extcount -= ext_diff;
1410 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff);
1411 ext_cnt -= ext_diff;
1416 ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t);
1417 xfs_iext_irec_compact(ifp);
1421 * Create, destroy, or resize a linear (direct) block of extents.
1424 xfs_iext_realloc_direct(
1425 xfs_ifork_t *ifp, /* inode fork pointer */
1426 int new_size) /* new size of extents after adding */
1428 int rnew_size; /* real new size of extents */
1430 rnew_size = new_size;
1432 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) ||
1433 ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) &&
1434 (new_size != ifp->if_real_bytes)));
1436 /* Free extent records */
1437 if (new_size == 0) {
1438 xfs_iext_destroy(ifp);
1440 /* Resize direct extent list and zero any new bytes */
1441 else if (ifp->if_real_bytes) {
1442 /* Check if extents will fit inside the inode */
1443 if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) {
1444 xfs_iext_direct_to_inline(ifp, new_size /
1445 (uint)sizeof(xfs_bmbt_rec_t));
1446 ifp->if_bytes = new_size;
1449 if (!is_power_of_2(new_size)){
1450 rnew_size = roundup_pow_of_two(new_size);
1452 if (rnew_size != ifp->if_real_bytes) {
1453 ifp->if_u1.if_extents =
1454 kmem_realloc(ifp->if_u1.if_extents,
1455 rnew_size, KM_NOFS);
1457 if (rnew_size > ifp->if_real_bytes) {
1458 memset(&ifp->if_u1.if_extents[ifp->if_bytes /
1459 (uint)sizeof(xfs_bmbt_rec_t)], 0,
1460 rnew_size - ifp->if_real_bytes);
1463 /* Switch from the inline extent buffer to a direct extent list */
1465 if (!is_power_of_2(new_size)) {
1466 rnew_size = roundup_pow_of_two(new_size);
1468 xfs_iext_inline_to_direct(ifp, rnew_size);
1470 ifp->if_real_bytes = rnew_size;
1471 ifp->if_bytes = new_size;
1475 * Switch from linear (direct) extent records to inline buffer.
1478 xfs_iext_direct_to_inline(
1479 xfs_ifork_t *ifp, /* inode fork pointer */
1480 xfs_extnum_t nextents) /* number of extents in file */
1482 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1483 ASSERT(nextents <= XFS_INLINE_EXTS);
1485 * The inline buffer was zeroed when we switched
1486 * from inline to direct extent allocation mode,
1487 * so we don't need to clear it here.
1489 memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents,
1490 nextents * sizeof(xfs_bmbt_rec_t));
1491 kmem_free(ifp->if_u1.if_extents);
1492 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
1493 ifp->if_real_bytes = 0;
1497 * Switch from inline buffer to linear (direct) extent records.
1498 * new_size should already be rounded up to the next power of 2
1499 * by the caller (when appropriate), so use new_size as it is.
1500 * However, since new_size may be rounded up, we can't update
1501 * if_bytes here. It is the caller's responsibility to update
1502 * if_bytes upon return.
1505 xfs_iext_inline_to_direct(
1506 xfs_ifork_t *ifp, /* inode fork pointer */
1507 int new_size) /* number of extents in file */
1509 ifp->if_u1.if_extents = kmem_alloc(new_size, KM_NOFS);
1510 memset(ifp->if_u1.if_extents, 0, new_size);
1511 if (ifp->if_bytes) {
1512 memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext,
1514 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
1515 sizeof(xfs_bmbt_rec_t));
1517 ifp->if_real_bytes = new_size;
1521 * Resize an extent indirection array to new_size bytes.
1524 xfs_iext_realloc_indirect(
1525 xfs_ifork_t *ifp, /* inode fork pointer */
1526 int new_size) /* new indirection array size */
1528 int nlists; /* number of irec's (ex lists) */
1529 int size; /* current indirection array size */
1531 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1532 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1533 size = nlists * sizeof(xfs_ext_irec_t);
1534 ASSERT(ifp->if_real_bytes);
1535 ASSERT((new_size >= 0) && (new_size != size));
1536 if (new_size == 0) {
1537 xfs_iext_destroy(ifp);
1539 ifp->if_u1.if_ext_irec =
1540 kmem_realloc(ifp->if_u1.if_ext_irec, new_size, KM_NOFS);
1545 * Switch from indirection array to linear (direct) extent allocations.
1548 xfs_iext_indirect_to_direct(
1549 xfs_ifork_t *ifp) /* inode fork pointer */
1551 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1552 xfs_extnum_t nextents; /* number of extents in file */
1553 int size; /* size of file extents */
1555 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1556 nextents = xfs_iext_count(ifp);
1557 ASSERT(nextents <= XFS_LINEAR_EXTS);
1558 size = nextents * sizeof(xfs_bmbt_rec_t);
1560 xfs_iext_irec_compact_pages(ifp);
1561 ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ);
1563 ep = ifp->if_u1.if_ext_irec->er_extbuf;
1564 kmem_free(ifp->if_u1.if_ext_irec);
1565 ifp->if_flags &= ~XFS_IFEXTIREC;
1566 ifp->if_u1.if_extents = ep;
1567 ifp->if_bytes = size;
1568 if (nextents < XFS_LINEAR_EXTS) {
1569 xfs_iext_realloc_direct(ifp, size);
1574 * Remove all records from the indirection array.
1577 xfs_iext_irec_remove_all(
1578 struct xfs_ifork *ifp)
1583 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1584 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1585 for (i = 0; i < nlists; i++)
1586 kmem_free(ifp->if_u1.if_ext_irec[i].er_extbuf);
1587 kmem_free(ifp->if_u1.if_ext_irec);
1588 ifp->if_flags &= ~XFS_IFEXTIREC;
1592 * Free incore file extents.
1596 xfs_ifork_t *ifp) /* inode fork pointer */
1598 if (ifp->if_flags & XFS_IFEXTIREC) {
1599 xfs_iext_irec_remove_all(ifp);
1600 } else if (ifp->if_real_bytes) {
1601 kmem_free(ifp->if_u1.if_extents);
1602 } else if (ifp->if_bytes) {
1603 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
1604 sizeof(xfs_bmbt_rec_t));
1606 ifp->if_u1.if_extents = NULL;
1607 ifp->if_real_bytes = 0;
1612 * Return a pointer to the extent record for file system block bno.
1614 xfs_bmbt_rec_host_t * /* pointer to found extent record */
1615 xfs_iext_bno_to_ext(
1616 xfs_ifork_t *ifp, /* inode fork pointer */
1617 xfs_fileoff_t bno, /* block number to search for */
1618 xfs_extnum_t *idxp) /* index of target extent */
1620 xfs_bmbt_rec_host_t *base; /* pointer to first extent */
1621 xfs_filblks_t blockcount = 0; /* number of blocks in extent */
1622 xfs_bmbt_rec_host_t *ep = NULL; /* pointer to target extent */
1623 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
1624 int high; /* upper boundary in search */
1625 xfs_extnum_t idx = 0; /* index of target extent */
1626 int low; /* lower boundary in search */
1627 xfs_extnum_t nextents; /* number of file extents */
1628 xfs_fileoff_t startoff = 0; /* start offset of extent */
1630 nextents = xfs_iext_count(ifp);
1631 if (nextents == 0) {
1636 if (ifp->if_flags & XFS_IFEXTIREC) {
1637 /* Find target extent list */
1639 erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx);
1640 base = erp->er_extbuf;
1641 high = erp->er_extcount - 1;
1643 base = ifp->if_u1.if_extents;
1644 high = nextents - 1;
1646 /* Binary search extent records */
1647 while (low <= high) {
1648 idx = (low + high) >> 1;
1650 startoff = xfs_bmbt_get_startoff(ep);
1651 blockcount = xfs_bmbt_get_blockcount(ep);
1652 if (bno < startoff) {
1654 } else if (bno >= startoff + blockcount) {
1657 /* Convert back to file-based extent index */
1658 if (ifp->if_flags & XFS_IFEXTIREC) {
1659 idx += erp->er_extoff;
1665 /* Convert back to file-based extent index */
1666 if (ifp->if_flags & XFS_IFEXTIREC) {
1667 idx += erp->er_extoff;
1669 if (bno >= startoff + blockcount) {
1670 if (++idx == nextents) {
1673 ep = xfs_iext_get_ext(ifp, idx);
1681 * Return a pointer to the indirection array entry containing the
1682 * extent record for filesystem block bno. Store the index of the
1683 * target irec in *erp_idxp.
1685 xfs_ext_irec_t * /* pointer to found extent record */
1686 xfs_iext_bno_to_irec(
1687 xfs_ifork_t *ifp, /* inode fork pointer */
1688 xfs_fileoff_t bno, /* block number to search for */
1689 int *erp_idxp) /* irec index of target ext list */
1691 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
1692 xfs_ext_irec_t *erp_next; /* next indirection array entry */
1693 int erp_idx; /* indirection array index */
1694 int nlists; /* number of extent irec's (lists) */
1695 int high; /* binary search upper limit */
1696 int low; /* binary search lower limit */
1698 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1699 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1703 while (low <= high) {
1704 erp_idx = (low + high) >> 1;
1705 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1706 erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL;
1707 if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) {
1709 } else if (erp_next && bno >=
1710 xfs_bmbt_get_startoff(erp_next->er_extbuf)) {
1716 *erp_idxp = erp_idx;
1721 * Return a pointer to the indirection array entry containing the
1722 * extent record at file extent index *idxp. Store the index of the
1723 * target irec in *erp_idxp and store the page index of the target
1724 * extent record in *idxp.
1727 xfs_iext_idx_to_irec(
1728 xfs_ifork_t *ifp, /* inode fork pointer */
1729 xfs_extnum_t *idxp, /* extent index (file -> page) */
1730 int *erp_idxp, /* pointer to target irec */
1731 int realloc) /* new bytes were just added */
1733 xfs_ext_irec_t *prev; /* pointer to previous irec */
1734 xfs_ext_irec_t *erp = NULL; /* pointer to current irec */
1735 int erp_idx; /* indirection array index */
1736 int nlists; /* number of irec's (ex lists) */
1737 int high; /* binary search upper limit */
1738 int low; /* binary search lower limit */
1739 xfs_extnum_t page_idx = *idxp; /* extent index in target list */
1741 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1742 ASSERT(page_idx >= 0);
1743 ASSERT(page_idx <= xfs_iext_count(ifp));
1744 ASSERT(page_idx < xfs_iext_count(ifp) || realloc);
1746 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1751 /* Binary search extent irec's */
1752 while (low <= high) {
1753 erp_idx = (low + high) >> 1;
1754 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1755 prev = erp_idx > 0 ? erp - 1 : NULL;
1756 if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff &&
1757 realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) {
1759 } else if (page_idx > erp->er_extoff + erp->er_extcount ||
1760 (page_idx == erp->er_extoff + erp->er_extcount &&
1763 } else if (page_idx == erp->er_extoff + erp->er_extcount &&
1764 erp->er_extcount == XFS_LINEAR_EXTS) {
1768 erp = erp_idx < nlists ? erp + 1 : NULL;
1771 page_idx -= erp->er_extoff;
1776 *erp_idxp = erp_idx;
1781 * Allocate and initialize an indirection array once the space needed
1782 * for incore extents increases above XFS_IEXT_BUFSZ.
1786 xfs_ifork_t *ifp) /* inode fork pointer */
1788 xfs_ext_irec_t *erp; /* indirection array pointer */
1789 xfs_extnum_t nextents; /* number of extents in file */
1791 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
1792 nextents = xfs_iext_count(ifp);
1793 ASSERT(nextents <= XFS_LINEAR_EXTS);
1795 erp = kmem_alloc(sizeof(xfs_ext_irec_t), KM_NOFS);
1797 if (nextents == 0) {
1798 ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
1799 } else if (!ifp->if_real_bytes) {
1800 xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ);
1801 } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) {
1802 xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ);
1804 erp->er_extbuf = ifp->if_u1.if_extents;
1805 erp->er_extcount = nextents;
1808 ifp->if_flags |= XFS_IFEXTIREC;
1809 ifp->if_real_bytes = XFS_IEXT_BUFSZ;
1810 ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t);
1811 ifp->if_u1.if_ext_irec = erp;
1817 * Allocate and initialize a new entry in the indirection array.
1821 xfs_ifork_t *ifp, /* inode fork pointer */
1822 int erp_idx) /* index for new irec */
1824 xfs_ext_irec_t *erp; /* indirection array pointer */
1825 int i; /* loop counter */
1826 int nlists; /* number of irec's (ex lists) */
1828 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1829 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1831 /* Resize indirection array */
1832 xfs_iext_realloc_indirect(ifp, ++nlists *
1833 sizeof(xfs_ext_irec_t));
1835 * Move records down in the array so the
1836 * new page can use erp_idx.
1838 erp = ifp->if_u1.if_ext_irec;
1839 for (i = nlists - 1; i > erp_idx; i--) {
1840 memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t));
1842 ASSERT(i == erp_idx);
1844 /* Initialize new extent record */
1845 erp = ifp->if_u1.if_ext_irec;
1846 erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
1847 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
1848 memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ);
1849 erp[erp_idx].er_extcount = 0;
1850 erp[erp_idx].er_extoff = erp_idx > 0 ?
1851 erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0;
1852 return (&erp[erp_idx]);
1856 * Remove a record from the indirection array.
1859 xfs_iext_irec_remove(
1860 xfs_ifork_t *ifp, /* inode fork pointer */
1861 int erp_idx) /* irec index to remove */
1863 xfs_ext_irec_t *erp; /* indirection array pointer */
1864 int i; /* loop counter */
1865 int nlists; /* number of irec's (ex lists) */
1867 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1868 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1869 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1870 if (erp->er_extbuf) {
1871 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1,
1873 kmem_free(erp->er_extbuf);
1875 /* Compact extent records */
1876 erp = ifp->if_u1.if_ext_irec;
1877 for (i = erp_idx; i < nlists - 1; i++) {
1878 memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t));
1881 * Manually free the last extent record from the indirection
1882 * array. A call to xfs_iext_realloc_indirect() with a size
1883 * of zero would result in a call to xfs_iext_destroy() which
1884 * would in turn call this function again, creating a nasty
1888 xfs_iext_realloc_indirect(ifp,
1889 nlists * sizeof(xfs_ext_irec_t));
1891 kmem_free(ifp->if_u1.if_ext_irec);
1893 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
1897 * This is called to clean up large amounts of unused memory allocated
1898 * by the indirection array. Before compacting anything though, verify
1899 * that the indirection array is still needed and switch back to the
1900 * linear extent list (or even the inline buffer) if possible. The
1901 * compaction policy is as follows:
1903 * Full Compaction: Extents fit into a single page (or inline buffer)
1904 * Partial Compaction: Extents occupy less than 50% of allocated space
1905 * No Compaction: Extents occupy at least 50% of allocated space
1908 xfs_iext_irec_compact(
1909 xfs_ifork_t *ifp) /* inode fork pointer */
1911 xfs_extnum_t nextents; /* number of extents in file */
1912 int nlists; /* number of irec's (ex lists) */
1914 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1915 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1916 nextents = xfs_iext_count(ifp);
1918 if (nextents == 0) {
1919 xfs_iext_destroy(ifp);
1920 } else if (nextents <= XFS_INLINE_EXTS) {
1921 xfs_iext_indirect_to_direct(ifp);
1922 xfs_iext_direct_to_inline(ifp, nextents);
1923 } else if (nextents <= XFS_LINEAR_EXTS) {
1924 xfs_iext_indirect_to_direct(ifp);
1925 } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) {
1926 xfs_iext_irec_compact_pages(ifp);
1931 * Combine extents from neighboring extent pages.
1934 xfs_iext_irec_compact_pages(
1935 xfs_ifork_t *ifp) /* inode fork pointer */
1937 xfs_ext_irec_t *erp, *erp_next;/* pointers to irec entries */
1938 int erp_idx = 0; /* indirection array index */
1939 int nlists; /* number of irec's (ex lists) */
1941 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1942 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1943 while (erp_idx < nlists - 1) {
1944 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1946 if (erp_next->er_extcount <=
1947 (XFS_LINEAR_EXTS - erp->er_extcount)) {
1948 memcpy(&erp->er_extbuf[erp->er_extcount],
1949 erp_next->er_extbuf, erp_next->er_extcount *
1950 sizeof(xfs_bmbt_rec_t));
1951 erp->er_extcount += erp_next->er_extcount;
1953 * Free page before removing extent record
1954 * so er_extoffs don't get modified in
1955 * xfs_iext_irec_remove.
1957 kmem_free(erp_next->er_extbuf);
1958 erp_next->er_extbuf = NULL;
1959 xfs_iext_irec_remove(ifp, erp_idx + 1);
1960 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1968 * This is called to update the er_extoff field in the indirection
1969 * array when extents have been added or removed from one of the
1970 * extent lists. erp_idx contains the irec index to begin updating
1971 * at and ext_diff contains the number of extents that were added
1975 xfs_iext_irec_update_extoffs(
1976 xfs_ifork_t *ifp, /* inode fork pointer */
1977 int erp_idx, /* irec index to update */
1978 int ext_diff) /* number of new extents */
1980 int i; /* loop counter */
1981 int nlists; /* number of irec's (ex lists */
1983 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1984 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1985 for (i = erp_idx; i < nlists; i++) {
1986 ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff;
1991 * Initialize an inode's copy-on-write fork.
1995 struct xfs_inode *ip)
2000 ip->i_cowfp = kmem_zone_zalloc(xfs_ifork_zone,
2001 KM_SLEEP | KM_NOFS);
2002 ip->i_cowfp->if_flags = XFS_IFEXTENTS;
2003 ip->i_cformat = XFS_DINODE_FMT_EXTENTS;
2004 ip->i_cnextents = 0;
2008 * Lookup the extent covering bno.
2010 * If there is an extent covering bno return the extent index, and store the
2011 * expanded extent structure in *gotp, and the extent index in *idx.
2012 * If there is no extent covering bno, but there is an extent after it (e.g.
2013 * it lies in a hole) return that extent in *gotp and its index in *idx
2015 * If bno is beyond the last extent return false, and return the index after
2016 * the last valid index in *idxp.
2019 xfs_iext_lookup_extent(
2020 struct xfs_inode *ip,
2021 struct xfs_ifork *ifp,
2024 struct xfs_bmbt_irec *gotp)
2026 struct xfs_bmbt_rec_host *ep;
2028 XFS_STATS_INC(ip->i_mount, xs_look_exlist);
2030 ep = xfs_iext_bno_to_ext(ifp, bno, idxp);
2033 xfs_bmbt_get_all(ep, gotp);
2038 * Return true if there is an extent at index idx, and return the expanded
2039 * extent structure at idx in that case. Else return false.
2042 xfs_iext_get_extent(
2043 struct xfs_ifork *ifp,
2045 struct xfs_bmbt_irec *gotp)
2047 if (idx < 0 || idx >= xfs_iext_count(ifp))
2049 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), gotp);