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"
28 #include "xfs_mount.h"
29 #include "xfs_inode.h"
30 #include "xfs_trans.h"
31 #include "xfs_inode_item.h"
32 #include "xfs_bmap_btree.h"
34 #include "xfs_error.h"
35 #include "xfs_trace.h"
36 #include "xfs_attr_sf.h"
38 kmem_zone_t *xfs_ifork_zone;
40 STATIC int xfs_iformat_local(xfs_inode_t *, xfs_dinode_t *, int, int);
41 STATIC int xfs_iformat_extents(xfs_inode_t *, xfs_dinode_t *, int);
42 STATIC int xfs_iformat_btree(xfs_inode_t *, xfs_dinode_t *, int);
46 * Make sure that the extents in the given memory buffer
56 xfs_bmbt_rec_host_t rec;
59 for (i = 0; i < nrecs; i++) {
60 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
61 rec.l0 = get_unaligned(&ep->l0);
62 rec.l1 = get_unaligned(&ep->l1);
63 xfs_bmbt_get_all(&rec, &irec);
64 if (fmt == XFS_EXTFMT_NOSTATE)
65 ASSERT(irec.br_state == XFS_EXT_NORM);
69 #define xfs_validate_extents(ifp, nrecs, fmt)
74 * Move inode type and inode format specific information from the
75 * on-disk inode to the in-core inode. For fifos, devs, and sockets
76 * this means set if_rdev to the proper value. For files, directories,
77 * and symlinks this means to bring in the in-line data or extent
78 * pointers. For a file in B-tree format, only the root is immediately
79 * brought in-core. The rest will be in-lined in if_extents when it
80 * is first referenced (see xfs_iread_extents()).
87 xfs_attr_shortform_t *atp;
92 if (unlikely(be32_to_cpu(dip->di_nextents) +
93 be16_to_cpu(dip->di_anextents) >
94 be64_to_cpu(dip->di_nblocks))) {
96 "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.",
97 (unsigned long long)ip->i_ino,
98 (int)(be32_to_cpu(dip->di_nextents) +
99 be16_to_cpu(dip->di_anextents)),
101 be64_to_cpu(dip->di_nblocks));
102 XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW,
104 return -EFSCORRUPTED;
107 if (unlikely(dip->di_forkoff > ip->i_mount->m_sb.sb_inodesize)) {
108 xfs_warn(ip->i_mount, "corrupt dinode %Lu, forkoff = 0x%x.",
109 (unsigned long long)ip->i_ino,
111 XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW,
113 return -EFSCORRUPTED;
116 if (unlikely((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) &&
117 !ip->i_mount->m_rtdev_targp)) {
118 xfs_warn(ip->i_mount,
119 "corrupt dinode %Lu, has realtime flag set.",
121 XFS_CORRUPTION_ERROR("xfs_iformat(realtime)",
122 XFS_ERRLEVEL_LOW, ip->i_mount, dip);
123 return -EFSCORRUPTED;
126 switch (ip->i_d.di_mode & S_IFMT) {
131 if (unlikely(dip->di_format != XFS_DINODE_FMT_DEV)) {
132 XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW,
134 return -EFSCORRUPTED;
137 ip->i_df.if_u2.if_rdev = xfs_dinode_get_rdev(dip);
143 switch (dip->di_format) {
144 case XFS_DINODE_FMT_LOCAL:
146 * no local regular files yet
148 if (unlikely(S_ISREG(be16_to_cpu(dip->di_mode)))) {
149 xfs_warn(ip->i_mount,
150 "corrupt inode %Lu (local format for regular file).",
151 (unsigned long long) ip->i_ino);
152 XFS_CORRUPTION_ERROR("xfs_iformat(4)",
155 return -EFSCORRUPTED;
158 di_size = be64_to_cpu(dip->di_size);
159 if (unlikely(di_size < 0 ||
160 di_size > XFS_DFORK_DSIZE(dip, ip->i_mount))) {
161 xfs_warn(ip->i_mount,
162 "corrupt inode %Lu (bad size %Ld for local inode).",
163 (unsigned long long) ip->i_ino,
164 (long long) di_size);
165 XFS_CORRUPTION_ERROR("xfs_iformat(5)",
168 return -EFSCORRUPTED;
172 error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size);
174 case XFS_DINODE_FMT_EXTENTS:
175 error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
177 case XFS_DINODE_FMT_BTREE:
178 error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
181 XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW,
183 return -EFSCORRUPTED;
188 XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW, ip->i_mount);
189 return -EFSCORRUPTED;
194 if (!XFS_DFORK_Q(dip))
197 ASSERT(ip->i_afp == NULL);
198 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP | KM_NOFS);
200 switch (dip->di_aformat) {
201 case XFS_DINODE_FMT_LOCAL:
202 atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
203 size = be16_to_cpu(atp->hdr.totsize);
205 if (unlikely(size < sizeof(struct xfs_attr_sf_hdr))) {
206 xfs_warn(ip->i_mount,
207 "corrupt inode %Lu (bad attr fork size %Ld).",
208 (unsigned long long) ip->i_ino,
210 XFS_CORRUPTION_ERROR("xfs_iformat(8)",
213 return -EFSCORRUPTED;
216 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size);
218 case XFS_DINODE_FMT_EXTENTS:
219 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
221 case XFS_DINODE_FMT_BTREE:
222 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
225 error = -EFSCORRUPTED;
229 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
231 xfs_idestroy_fork(ip, XFS_DATA_FORK);
237 * The file is in-lined in the on-disk inode.
238 * If it fits into if_inline_data, then copy
239 * it there, otherwise allocate a buffer for it
240 * and copy the data there. Either way, set
241 * if_data to point at the data.
242 * If we allocate a buffer for the data, make
243 * sure that its size is a multiple of 4 and
244 * record the real size in i_real_bytes.
257 * If the size is unreasonable, then something
258 * is wrong and we just bail out rather than crash in
259 * kmem_alloc() or memcpy() below.
261 if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
262 xfs_warn(ip->i_mount,
263 "corrupt inode %Lu (bad size %d for local fork, size = %d).",
264 (unsigned long long) ip->i_ino, size,
265 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
266 XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW,
268 return -EFSCORRUPTED;
270 ifp = XFS_IFORK_PTR(ip, whichfork);
273 ifp->if_u1.if_data = NULL;
274 else if (size <= sizeof(ifp->if_u2.if_inline_data))
275 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
277 real_size = roundup(size, 4);
278 ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP | KM_NOFS);
280 ifp->if_bytes = size;
281 ifp->if_real_bytes = real_size;
283 memcpy(ifp->if_u1.if_data, XFS_DFORK_PTR(dip, whichfork), size);
284 ifp->if_flags &= ~XFS_IFEXTENTS;
285 ifp->if_flags |= XFS_IFINLINE;
290 * The file consists of a set of extents all
291 * of which fit into the on-disk inode.
292 * If there are few enough extents to fit into
293 * the if_inline_ext, then copy them there.
294 * Otherwise allocate a buffer for them and copy
295 * them into it. Either way, set if_extents
296 * to point at the extents.
310 ifp = XFS_IFORK_PTR(ip, whichfork);
311 nex = XFS_DFORK_NEXTENTS(dip, whichfork);
312 size = nex * (uint)sizeof(xfs_bmbt_rec_t);
315 * If the number of extents is unreasonable, then something
316 * is wrong and we just bail out rather than crash in
317 * kmem_alloc() or memcpy() below.
319 if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
320 xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).",
321 (unsigned long long) ip->i_ino, nex);
322 XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW,
324 return -EFSCORRUPTED;
327 ifp->if_real_bytes = 0;
329 ifp->if_u1.if_extents = NULL;
330 else if (nex <= XFS_INLINE_EXTS)
331 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
333 xfs_iext_add(ifp, 0, nex);
335 ifp->if_bytes = size;
337 dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
338 xfs_validate_extents(ifp, nex, XFS_EXTFMT_INODE(ip));
339 for (i = 0; i < nex; i++, dp++) {
340 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
341 ep->l0 = get_unaligned_be64(&dp->l0);
342 ep->l1 = get_unaligned_be64(&dp->l1);
344 XFS_BMAP_TRACE_EXLIST(ip, nex, whichfork);
345 if (whichfork != XFS_DATA_FORK ||
346 XFS_EXTFMT_INODE(ip) == XFS_EXTFMT_NOSTATE)
347 if (unlikely(xfs_check_nostate_extents(
349 XFS_ERROR_REPORT("xfs_iformat_extents(2)",
352 return -EFSCORRUPTED;
355 ifp->if_flags |= XFS_IFEXTENTS;
360 * The file has too many extents to fit into
361 * the inode, so they are in B-tree format.
362 * Allocate a buffer for the root of the B-tree
363 * and copy the root into it. The i_extents
364 * field will remain NULL until all of the
365 * extents are read in (when they are needed).
373 struct xfs_mount *mp = ip->i_mount;
374 xfs_bmdr_block_t *dfp;
380 ifp = XFS_IFORK_PTR(ip, whichfork);
381 dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
382 size = XFS_BMAP_BROOT_SPACE(mp, dfp);
383 nrecs = be16_to_cpu(dfp->bb_numrecs);
386 * blow out if -- fork has less extents than can fit in
387 * fork (fork shouldn't be a btree format), root btree
388 * block has more records than can fit into the fork,
389 * or the number of extents is greater than the number of
392 if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <=
393 XFS_IFORK_MAXEXT(ip, whichfork) ||
394 XFS_BMDR_SPACE_CALC(nrecs) >
395 XFS_DFORK_SIZE(dip, mp, whichfork) ||
396 XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks)) {
397 xfs_warn(mp, "corrupt inode %Lu (btree).",
398 (unsigned long long) ip->i_ino);
399 XFS_CORRUPTION_ERROR("xfs_iformat_btree", XFS_ERRLEVEL_LOW,
401 return -EFSCORRUPTED;
404 ifp->if_broot_bytes = size;
405 ifp->if_broot = kmem_alloc(size, KM_SLEEP | KM_NOFS);
406 ASSERT(ifp->if_broot != NULL);
408 * Copy and convert from the on-disk structure
409 * to the in-memory structure.
411 xfs_bmdr_to_bmbt(ip, dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
412 ifp->if_broot, size);
413 ifp->if_flags &= ~XFS_IFEXTENTS;
414 ifp->if_flags |= XFS_IFBROOT;
420 * Read in extents from a btree-format inode.
421 * Allocate and fill in if_extents. Real work is done in xfs_bmap.c.
431 xfs_extnum_t nextents;
433 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
435 if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
436 XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW,
438 return -EFSCORRUPTED;
440 nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
441 ifp = XFS_IFORK_PTR(ip, whichfork);
444 * We know that the size is valid (it's checked in iformat_btree)
446 ifp->if_bytes = ifp->if_real_bytes = 0;
447 ifp->if_flags |= XFS_IFEXTENTS;
448 xfs_iext_add(ifp, 0, nextents);
449 error = xfs_bmap_read_extents(tp, ip, whichfork);
451 xfs_iext_destroy(ifp);
452 ifp->if_flags &= ~XFS_IFEXTENTS;
455 xfs_validate_extents(ifp, nextents, XFS_EXTFMT_INODE(ip));
459 * Reallocate the space for if_broot based on the number of records
460 * being added or deleted as indicated in rec_diff. Move the records
461 * and pointers in if_broot to fit the new size. When shrinking this
462 * will eliminate holes between the records and pointers created by
463 * the caller. When growing this will create holes to be filled in
466 * The caller must not request to add more records than would fit in
467 * the on-disk inode root. If the if_broot is currently NULL, then
468 * if we are adding records, one will be allocated. The caller must also
469 * not request that the number of records go below zero, although
472 * ip -- the inode whose if_broot area is changing
473 * ext_diff -- the change in the number of records, positive or negative,
474 * requested for the if_broot array.
482 struct xfs_mount *mp = ip->i_mount;
485 struct xfs_btree_block *new_broot;
492 * Handle the degenerate case quietly.
498 ifp = XFS_IFORK_PTR(ip, whichfork);
501 * If there wasn't any memory allocated before, just
502 * allocate it now and get out.
504 if (ifp->if_broot_bytes == 0) {
505 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
506 ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
507 ifp->if_broot_bytes = (int)new_size;
512 * If there is already an existing if_broot, then we need
513 * to realloc() it and shift the pointers to their new
514 * location. The records don't change location because
515 * they are kept butted up against the btree block header.
517 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
518 new_max = cur_max + rec_diff;
519 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
520 ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
521 XFS_BMAP_BROOT_SPACE_CALC(mp, cur_max),
523 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
524 ifp->if_broot_bytes);
525 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
527 ifp->if_broot_bytes = (int)new_size;
528 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
529 XFS_IFORK_SIZE(ip, whichfork));
530 memmove(np, op, cur_max * (uint)sizeof(xfs_fsblock_t));
535 * rec_diff is less than 0. In this case, we are shrinking the
536 * if_broot buffer. It must already exist. If we go to zero
537 * records, just get rid of the root and clear the status bit.
539 ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
540 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
541 new_max = cur_max + rec_diff;
542 ASSERT(new_max >= 0);
544 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
548 new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
550 * First copy over the btree block header.
552 memcpy(new_broot, ifp->if_broot,
553 XFS_BMBT_BLOCK_LEN(ip->i_mount));
556 ifp->if_flags &= ~XFS_IFBROOT;
560 * Only copy the records and pointers if there are any.
564 * First copy the records.
566 op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
567 np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
568 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
571 * Then copy the pointers.
573 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
574 ifp->if_broot_bytes);
575 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
577 memcpy(np, op, new_max * (uint)sizeof(xfs_fsblock_t));
579 kmem_free(ifp->if_broot);
580 ifp->if_broot = new_broot;
581 ifp->if_broot_bytes = (int)new_size;
583 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
584 XFS_IFORK_SIZE(ip, whichfork));
590 * This is called when the amount of space needed for if_data
591 * is increased or decreased. The change in size is indicated by
592 * the number of bytes that need to be added or deleted in the
593 * byte_diff parameter.
595 * If the amount of space needed has decreased below the size of the
596 * inline buffer, then switch to using the inline buffer. Otherwise,
597 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
600 * ip -- the inode whose if_data area is changing
601 * byte_diff -- the change in the number of bytes, positive or negative,
602 * requested for the if_data array.
614 if (byte_diff == 0) {
618 ifp = XFS_IFORK_PTR(ip, whichfork);
619 new_size = (int)ifp->if_bytes + byte_diff;
620 ASSERT(new_size >= 0);
623 if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
624 kmem_free(ifp->if_u1.if_data);
626 ifp->if_u1.if_data = NULL;
628 } else if (new_size <= sizeof(ifp->if_u2.if_inline_data)) {
630 * If the valid extents/data can fit in if_inline_ext/data,
631 * copy them from the malloc'd vector and free it.
633 if (ifp->if_u1.if_data == NULL) {
634 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
635 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
636 ASSERT(ifp->if_real_bytes != 0);
637 memcpy(ifp->if_u2.if_inline_data, ifp->if_u1.if_data,
639 kmem_free(ifp->if_u1.if_data);
640 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
645 * Stuck with malloc/realloc.
646 * For inline data, the underlying buffer must be
647 * a multiple of 4 bytes in size so that it can be
648 * logged and stay on word boundaries. We enforce
651 real_size = roundup(new_size, 4);
652 if (ifp->if_u1.if_data == NULL) {
653 ASSERT(ifp->if_real_bytes == 0);
654 ifp->if_u1.if_data = kmem_alloc(real_size,
656 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
658 * Only do the realloc if the underlying size
659 * is really changing.
661 if (ifp->if_real_bytes != real_size) {
663 kmem_realloc(ifp->if_u1.if_data,
669 ASSERT(ifp->if_real_bytes == 0);
670 ifp->if_u1.if_data = kmem_alloc(real_size,
672 memcpy(ifp->if_u1.if_data, ifp->if_u2.if_inline_data,
676 ifp->if_real_bytes = real_size;
677 ifp->if_bytes = new_size;
678 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
688 ifp = XFS_IFORK_PTR(ip, whichfork);
689 if (ifp->if_broot != NULL) {
690 kmem_free(ifp->if_broot);
691 ifp->if_broot = NULL;
695 * If the format is local, then we can't have an extents
696 * array so just look for an inline data array. If we're
697 * not local then we may or may not have an extents list,
698 * so check and free it up if we do.
700 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
701 if ((ifp->if_u1.if_data != ifp->if_u2.if_inline_data) &&
702 (ifp->if_u1.if_data != NULL)) {
703 ASSERT(ifp->if_real_bytes != 0);
704 kmem_free(ifp->if_u1.if_data);
705 ifp->if_u1.if_data = NULL;
706 ifp->if_real_bytes = 0;
708 } else if ((ifp->if_flags & XFS_IFEXTENTS) &&
709 ((ifp->if_flags & XFS_IFEXTIREC) ||
710 ((ifp->if_u1.if_extents != NULL) &&
711 (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext)))) {
712 ASSERT(ifp->if_real_bytes != 0);
713 xfs_iext_destroy(ifp);
715 ASSERT(ifp->if_u1.if_extents == NULL ||
716 ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext);
717 ASSERT(ifp->if_real_bytes == 0);
718 if (whichfork == XFS_ATTR_FORK) {
719 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
725 * Convert in-core extents to on-disk form
727 * For either the data or attr fork in extent format, we need to endian convert
728 * the in-core extent as we place them into the on-disk inode.
730 * In the case of the data fork, the in-core and on-disk fork sizes can be
731 * different due to delayed allocation extents. We only copy on-disk extents
732 * here, so callers must always use the physical fork size to determine the
733 * size of the buffer passed to this routine. We will return the size actually
746 xfs_fsblock_t start_block;
748 ifp = XFS_IFORK_PTR(ip, whichfork);
749 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
750 ASSERT(ifp->if_bytes > 0);
752 nrecs = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
753 XFS_BMAP_TRACE_EXLIST(ip, nrecs, whichfork);
757 * There are some delayed allocation extents in the
758 * inode, so copy the extents one at a time and skip
759 * the delayed ones. There must be at least one
760 * non-delayed extent.
763 for (i = 0; i < nrecs; i++) {
764 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
765 start_block = xfs_bmbt_get_startblock(ep);
766 if (isnullstartblock(start_block)) {
768 * It's a delayed allocation extent, so skip it.
773 /* Translate to on disk format */
774 put_unaligned_be64(ep->l0, &dp->l0);
775 put_unaligned_be64(ep->l1, &dp->l1);
780 xfs_validate_extents(ifp, copied, XFS_EXTFMT_INODE(ip));
782 return (copied * (uint)sizeof(xfs_bmbt_rec_t));
786 * Each of the following cases stores data into the same region
787 * of the on-disk inode, so only one of them can be valid at
788 * any given time. While it is possible to have conflicting formats
789 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
790 * in EXTENTS format, this can only happen when the fork has
791 * changed formats after being modified but before being flushed.
792 * In these cases, the format always takes precedence, because the
793 * format indicates the current state of the fork.
799 xfs_inode_log_item_t *iip,
805 static const short brootflag[2] =
806 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
807 static const short dataflag[2] =
808 { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
809 static const short extflag[2] =
810 { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
814 ifp = XFS_IFORK_PTR(ip, whichfork);
816 * This can happen if we gave up in iformat in an error path,
817 * for the attribute fork.
820 ASSERT(whichfork == XFS_ATTR_FORK);
823 cp = XFS_DFORK_PTR(dip, whichfork);
825 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
826 case XFS_DINODE_FMT_LOCAL:
827 if ((iip->ili_fields & dataflag[whichfork]) &&
828 (ifp->if_bytes > 0)) {
829 ASSERT(ifp->if_u1.if_data != NULL);
830 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
831 memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
835 case XFS_DINODE_FMT_EXTENTS:
836 ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
837 !(iip->ili_fields & extflag[whichfork]));
838 if ((iip->ili_fields & extflag[whichfork]) &&
839 (ifp->if_bytes > 0)) {
840 ASSERT(xfs_iext_get_ext(ifp, 0));
841 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
842 (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
847 case XFS_DINODE_FMT_BTREE:
848 if ((iip->ili_fields & brootflag[whichfork]) &&
849 (ifp->if_broot_bytes > 0)) {
850 ASSERT(ifp->if_broot != NULL);
851 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
852 XFS_IFORK_SIZE(ip, whichfork));
853 xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
854 (xfs_bmdr_block_t *)cp,
855 XFS_DFORK_SIZE(dip, mp, whichfork));
859 case XFS_DINODE_FMT_DEV:
860 if (iip->ili_fields & XFS_ILOG_DEV) {
861 ASSERT(whichfork == XFS_DATA_FORK);
862 xfs_dinode_put_rdev(dip, ip->i_df.if_u2.if_rdev);
866 case XFS_DINODE_FMT_UUID:
867 if (iip->ili_fields & XFS_ILOG_UUID) {
868 ASSERT(whichfork == XFS_DATA_FORK);
869 memcpy(XFS_DFORK_DPTR(dip),
870 &ip->i_df.if_u2.if_uuid,
882 * Return a pointer to the extent record at file index idx.
884 xfs_bmbt_rec_host_t *
886 xfs_ifork_t *ifp, /* inode fork pointer */
887 xfs_extnum_t idx) /* index of target extent */
890 ASSERT(idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
892 if ((ifp->if_flags & XFS_IFEXTIREC) && (idx == 0)) {
893 return ifp->if_u1.if_ext_irec->er_extbuf;
894 } else if (ifp->if_flags & XFS_IFEXTIREC) {
895 xfs_ext_irec_t *erp; /* irec pointer */
896 int erp_idx = 0; /* irec index */
897 xfs_extnum_t page_idx = idx; /* ext index in target list */
899 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
900 return &erp->er_extbuf[page_idx];
901 } else if (ifp->if_bytes) {
902 return &ifp->if_u1.if_extents[idx];
909 * Insert new item(s) into the extent records for incore inode
910 * fork 'ifp'. 'count' new items are inserted at index 'idx'.
914 xfs_inode_t *ip, /* incore inode pointer */
915 xfs_extnum_t idx, /* starting index of new items */
916 xfs_extnum_t count, /* number of inserted items */
917 xfs_bmbt_irec_t *new, /* items to insert */
918 int state) /* type of extent conversion */
920 xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
921 xfs_extnum_t i; /* extent record index */
923 trace_xfs_iext_insert(ip, idx, new, state, _RET_IP_);
925 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
926 xfs_iext_add(ifp, idx, count);
927 for (i = idx; i < idx + count; i++, new++)
928 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, i), new);
932 * This is called when the amount of space required for incore file
933 * extents needs to be increased. The ext_diff parameter stores the
934 * number of new extents being added and the idx parameter contains
935 * the extent index where the new extents will be added. If the new
936 * extents are being appended, then we just need to (re)allocate and
937 * initialize the space. Otherwise, if the new extents are being
938 * inserted into the middle of the existing entries, a bit more work
939 * is required to make room for the new extents to be inserted. The
940 * caller is responsible for filling in the new extent entries upon
945 xfs_ifork_t *ifp, /* inode fork pointer */
946 xfs_extnum_t idx, /* index to begin adding exts */
947 int ext_diff) /* number of extents to add */
949 int byte_diff; /* new bytes being added */
950 int new_size; /* size of extents after adding */
951 xfs_extnum_t nextents; /* number of extents in file */
953 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
954 ASSERT((idx >= 0) && (idx <= nextents));
955 byte_diff = ext_diff * sizeof(xfs_bmbt_rec_t);
956 new_size = ifp->if_bytes + byte_diff;
958 * If the new number of extents (nextents + ext_diff)
959 * fits inside the inode, then continue to use the inline
962 if (nextents + ext_diff <= XFS_INLINE_EXTS) {
963 if (idx < nextents) {
964 memmove(&ifp->if_u2.if_inline_ext[idx + ext_diff],
965 &ifp->if_u2.if_inline_ext[idx],
966 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
967 memset(&ifp->if_u2.if_inline_ext[idx], 0, byte_diff);
969 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
970 ifp->if_real_bytes = 0;
973 * Otherwise use a linear (direct) extent list.
974 * If the extents are currently inside the inode,
975 * xfs_iext_realloc_direct will switch us from
976 * inline to direct extent allocation mode.
978 else if (nextents + ext_diff <= XFS_LINEAR_EXTS) {
979 xfs_iext_realloc_direct(ifp, new_size);
980 if (idx < nextents) {
981 memmove(&ifp->if_u1.if_extents[idx + ext_diff],
982 &ifp->if_u1.if_extents[idx],
983 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
984 memset(&ifp->if_u1.if_extents[idx], 0, byte_diff);
987 /* Indirection array */
993 ASSERT(nextents + ext_diff > XFS_LINEAR_EXTS);
994 if (ifp->if_flags & XFS_IFEXTIREC) {
995 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 1);
997 xfs_iext_irec_init(ifp);
998 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
999 erp = ifp->if_u1.if_ext_irec;
1001 /* Extents fit in target extent page */
1002 if (erp && erp->er_extcount + ext_diff <= XFS_LINEAR_EXTS) {
1003 if (page_idx < erp->er_extcount) {
1004 memmove(&erp->er_extbuf[page_idx + ext_diff],
1005 &erp->er_extbuf[page_idx],
1006 (erp->er_extcount - page_idx) *
1007 sizeof(xfs_bmbt_rec_t));
1008 memset(&erp->er_extbuf[page_idx], 0, byte_diff);
1010 erp->er_extcount += ext_diff;
1011 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
1013 /* Insert a new extent page */
1015 xfs_iext_add_indirect_multi(ifp,
1016 erp_idx, page_idx, ext_diff);
1019 * If extent(s) are being appended to the last page in
1020 * the indirection array and the new extent(s) don't fit
1021 * in the page, then erp is NULL and erp_idx is set to
1022 * the next index needed in the indirection array.
1025 uint count = ext_diff;
1028 erp = xfs_iext_irec_new(ifp, erp_idx);
1029 erp->er_extcount = min(count, XFS_LINEAR_EXTS);
1030 count -= erp->er_extcount;
1036 ifp->if_bytes = new_size;
1040 * This is called when incore extents are being added to the indirection
1041 * array and the new extents do not fit in the target extent list. The
1042 * erp_idx parameter contains the irec index for the target extent list
1043 * in the indirection array, and the idx parameter contains the extent
1044 * index within the list. The number of extents being added is stored
1045 * in the count parameter.
1047 * |-------| |-------|
1048 * | | | | idx - number of extents before idx
1050 * | | | | count - number of extents being inserted at idx
1051 * |-------| |-------|
1052 * | count | | nex2 | nex2 - number of extents after idx + count
1053 * |-------| |-------|
1056 xfs_iext_add_indirect_multi(
1057 xfs_ifork_t *ifp, /* inode fork pointer */
1058 int erp_idx, /* target extent irec index */
1059 xfs_extnum_t idx, /* index within target list */
1060 int count) /* new extents being added */
1062 int byte_diff; /* new bytes being added */
1063 xfs_ext_irec_t *erp; /* pointer to irec entry */
1064 xfs_extnum_t ext_diff; /* number of extents to add */
1065 xfs_extnum_t ext_cnt; /* new extents still needed */
1066 xfs_extnum_t nex2; /* extents after idx + count */
1067 xfs_bmbt_rec_t *nex2_ep = NULL; /* temp list for nex2 extents */
1068 int nlists; /* number of irec's (lists) */
1070 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1071 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1072 nex2 = erp->er_extcount - idx;
1073 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1076 * Save second part of target extent list
1077 * (all extents past */
1079 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
1080 nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_NOFS);
1081 memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff);
1082 erp->er_extcount -= nex2;
1083 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2);
1084 memset(&erp->er_extbuf[idx], 0, byte_diff);
1088 * Add the new extents to the end of the target
1089 * list, then allocate new irec record(s) and
1090 * extent buffer(s) as needed to store the rest
1091 * of the new extents.
1094 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount);
1096 erp->er_extcount += ext_diff;
1097 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
1098 ext_cnt -= ext_diff;
1102 erp = xfs_iext_irec_new(ifp, erp_idx);
1103 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS);
1104 erp->er_extcount = ext_diff;
1105 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
1106 ext_cnt -= ext_diff;
1109 /* Add nex2 extents back to indirection array */
1111 xfs_extnum_t ext_avail;
1114 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
1115 ext_avail = XFS_LINEAR_EXTS - erp->er_extcount;
1118 * If nex2 extents fit in the current page, append
1119 * nex2_ep after the new extents.
1121 if (nex2 <= ext_avail) {
1122 i = erp->er_extcount;
1125 * Otherwise, check if space is available in the
1128 else if ((erp_idx < nlists - 1) &&
1129 (nex2 <= (ext_avail = XFS_LINEAR_EXTS -
1130 ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) {
1133 /* Create a hole for nex2 extents */
1134 memmove(&erp->er_extbuf[nex2], erp->er_extbuf,
1135 erp->er_extcount * sizeof(xfs_bmbt_rec_t));
1138 * Final choice, create a new extent page for
1143 erp = xfs_iext_irec_new(ifp, erp_idx);
1145 memmove(&erp->er_extbuf[i], nex2_ep, byte_diff);
1147 erp->er_extcount += nex2;
1148 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2);
1153 * This is called when the amount of space required for incore file
1154 * extents needs to be decreased. The ext_diff parameter stores the
1155 * number of extents to be removed and the idx parameter contains
1156 * the extent index where the extents will be removed from.
1158 * If the amount of space needed has decreased below the linear
1159 * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
1160 * extent array. Otherwise, use kmem_realloc() to adjust the
1161 * size to what is needed.
1165 xfs_inode_t *ip, /* incore inode pointer */
1166 xfs_extnum_t idx, /* index to begin removing exts */
1167 int ext_diff, /* number of extents to remove */
1168 int state) /* type of extent conversion */
1170 xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
1171 xfs_extnum_t nextents; /* number of extents in file */
1172 int new_size; /* size of extents after removal */
1174 trace_xfs_iext_remove(ip, idx, state, _RET_IP_);
1176 ASSERT(ext_diff > 0);
1177 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1178 new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t);
1180 if (new_size == 0) {
1181 xfs_iext_destroy(ifp);
1182 } else if (ifp->if_flags & XFS_IFEXTIREC) {
1183 xfs_iext_remove_indirect(ifp, idx, ext_diff);
1184 } else if (ifp->if_real_bytes) {
1185 xfs_iext_remove_direct(ifp, idx, ext_diff);
1187 xfs_iext_remove_inline(ifp, idx, ext_diff);
1189 ifp->if_bytes = new_size;
1193 * This removes ext_diff extents from the inline buffer, beginning
1194 * at extent index idx.
1197 xfs_iext_remove_inline(
1198 xfs_ifork_t *ifp, /* inode fork pointer */
1199 xfs_extnum_t idx, /* index to begin removing exts */
1200 int ext_diff) /* number of extents to remove */
1202 int nextents; /* number of extents in file */
1204 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
1205 ASSERT(idx < XFS_INLINE_EXTS);
1206 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1207 ASSERT(((nextents - ext_diff) > 0) &&
1208 (nextents - ext_diff) < XFS_INLINE_EXTS);
1210 if (idx + ext_diff < nextents) {
1211 memmove(&ifp->if_u2.if_inline_ext[idx],
1212 &ifp->if_u2.if_inline_ext[idx + ext_diff],
1213 (nextents - (idx + ext_diff)) *
1214 sizeof(xfs_bmbt_rec_t));
1215 memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff],
1216 0, ext_diff * sizeof(xfs_bmbt_rec_t));
1218 memset(&ifp->if_u2.if_inline_ext[idx], 0,
1219 ext_diff * sizeof(xfs_bmbt_rec_t));
1224 * This removes ext_diff extents from a linear (direct) extent list,
1225 * beginning at extent index idx. If the extents are being removed
1226 * from the end of the list (ie. truncate) then we just need to re-
1227 * allocate the list to remove the extra space. Otherwise, if the
1228 * extents are being removed from the middle of the existing extent
1229 * entries, then we first need to move the extent records beginning
1230 * at idx + ext_diff up in the list to overwrite the records being
1231 * removed, then remove the extra space via kmem_realloc.
1234 xfs_iext_remove_direct(
1235 xfs_ifork_t *ifp, /* inode fork pointer */
1236 xfs_extnum_t idx, /* index to begin removing exts */
1237 int ext_diff) /* number of extents to remove */
1239 xfs_extnum_t nextents; /* number of extents in file */
1240 int new_size; /* size of extents after removal */
1242 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
1243 new_size = ifp->if_bytes -
1244 (ext_diff * sizeof(xfs_bmbt_rec_t));
1245 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1247 if (new_size == 0) {
1248 xfs_iext_destroy(ifp);
1251 /* Move extents up in the list (if needed) */
1252 if (idx + ext_diff < nextents) {
1253 memmove(&ifp->if_u1.if_extents[idx],
1254 &ifp->if_u1.if_extents[idx + ext_diff],
1255 (nextents - (idx + ext_diff)) *
1256 sizeof(xfs_bmbt_rec_t));
1258 memset(&ifp->if_u1.if_extents[nextents - ext_diff],
1259 0, ext_diff * sizeof(xfs_bmbt_rec_t));
1261 * Reallocate the direct extent list. If the extents
1262 * will fit inside the inode then xfs_iext_realloc_direct
1263 * will switch from direct to inline extent allocation
1266 xfs_iext_realloc_direct(ifp, new_size);
1267 ifp->if_bytes = new_size;
1271 * This is called when incore extents are being removed from the
1272 * indirection array and the extents being removed span multiple extent
1273 * buffers. The idx parameter contains the file extent index where we
1274 * want to begin removing extents, and the count parameter contains
1275 * how many extents need to be removed.
1277 * |-------| |-------|
1278 * | nex1 | | | nex1 - number of extents before idx
1279 * |-------| | count |
1280 * | | | | count - number of extents being removed at idx
1281 * | count | |-------|
1282 * | | | nex2 | nex2 - number of extents after idx + count
1283 * |-------| |-------|
1286 xfs_iext_remove_indirect(
1287 xfs_ifork_t *ifp, /* inode fork pointer */
1288 xfs_extnum_t idx, /* index to begin removing extents */
1289 int count) /* number of extents to remove */
1291 xfs_ext_irec_t *erp; /* indirection array pointer */
1292 int erp_idx = 0; /* indirection array index */
1293 xfs_extnum_t ext_cnt; /* extents left to remove */
1294 xfs_extnum_t ext_diff; /* extents to remove in current list */
1295 xfs_extnum_t nex1; /* number of extents before idx */
1296 xfs_extnum_t nex2; /* extents after idx + count */
1297 int page_idx = idx; /* index in target extent list */
1299 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1300 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
1301 ASSERT(erp != NULL);
1305 nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0);
1306 ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1));
1308 * Check for deletion of entire list;
1309 * xfs_iext_irec_remove() updates extent offsets.
1311 if (ext_diff == erp->er_extcount) {
1312 xfs_iext_irec_remove(ifp, erp_idx);
1313 ext_cnt -= ext_diff;
1316 ASSERT(erp_idx < ifp->if_real_bytes /
1318 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1325 /* Move extents up (if needed) */
1327 memmove(&erp->er_extbuf[nex1],
1328 &erp->er_extbuf[nex1 + ext_diff],
1329 nex2 * sizeof(xfs_bmbt_rec_t));
1331 /* Zero out rest of page */
1332 memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ -
1333 ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t))));
1334 /* Update remaining counters */
1335 erp->er_extcount -= ext_diff;
1336 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff);
1337 ext_cnt -= ext_diff;
1342 ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t);
1343 xfs_iext_irec_compact(ifp);
1347 * Create, destroy, or resize a linear (direct) block of extents.
1350 xfs_iext_realloc_direct(
1351 xfs_ifork_t *ifp, /* inode fork pointer */
1352 int new_size) /* new size of extents after adding */
1354 int rnew_size; /* real new size of extents */
1356 rnew_size = new_size;
1358 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) ||
1359 ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) &&
1360 (new_size != ifp->if_real_bytes)));
1362 /* Free extent records */
1363 if (new_size == 0) {
1364 xfs_iext_destroy(ifp);
1366 /* Resize direct extent list and zero any new bytes */
1367 else if (ifp->if_real_bytes) {
1368 /* Check if extents will fit inside the inode */
1369 if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) {
1370 xfs_iext_direct_to_inline(ifp, new_size /
1371 (uint)sizeof(xfs_bmbt_rec_t));
1372 ifp->if_bytes = new_size;
1375 if (!is_power_of_2(new_size)){
1376 rnew_size = roundup_pow_of_two(new_size);
1378 if (rnew_size != ifp->if_real_bytes) {
1379 ifp->if_u1.if_extents =
1380 kmem_realloc(ifp->if_u1.if_extents,
1382 ifp->if_real_bytes, KM_NOFS);
1384 if (rnew_size > ifp->if_real_bytes) {
1385 memset(&ifp->if_u1.if_extents[ifp->if_bytes /
1386 (uint)sizeof(xfs_bmbt_rec_t)], 0,
1387 rnew_size - ifp->if_real_bytes);
1390 /* Switch from the inline extent buffer to a direct extent list */
1392 if (!is_power_of_2(new_size)) {
1393 rnew_size = roundup_pow_of_two(new_size);
1395 xfs_iext_inline_to_direct(ifp, rnew_size);
1397 ifp->if_real_bytes = rnew_size;
1398 ifp->if_bytes = new_size;
1402 * Switch from linear (direct) extent records to inline buffer.
1405 xfs_iext_direct_to_inline(
1406 xfs_ifork_t *ifp, /* inode fork pointer */
1407 xfs_extnum_t nextents) /* number of extents in file */
1409 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1410 ASSERT(nextents <= XFS_INLINE_EXTS);
1412 * The inline buffer was zeroed when we switched
1413 * from inline to direct extent allocation mode,
1414 * so we don't need to clear it here.
1416 memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents,
1417 nextents * sizeof(xfs_bmbt_rec_t));
1418 kmem_free(ifp->if_u1.if_extents);
1419 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
1420 ifp->if_real_bytes = 0;
1424 * Switch from inline buffer to linear (direct) extent records.
1425 * new_size should already be rounded up to the next power of 2
1426 * by the caller (when appropriate), so use new_size as it is.
1427 * However, since new_size may be rounded up, we can't update
1428 * if_bytes here. It is the caller's responsibility to update
1429 * if_bytes upon return.
1432 xfs_iext_inline_to_direct(
1433 xfs_ifork_t *ifp, /* inode fork pointer */
1434 int new_size) /* number of extents in file */
1436 ifp->if_u1.if_extents = kmem_alloc(new_size, KM_NOFS);
1437 memset(ifp->if_u1.if_extents, 0, new_size);
1438 if (ifp->if_bytes) {
1439 memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext,
1441 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
1442 sizeof(xfs_bmbt_rec_t));
1444 ifp->if_real_bytes = new_size;
1448 * Resize an extent indirection array to new_size bytes.
1451 xfs_iext_realloc_indirect(
1452 xfs_ifork_t *ifp, /* inode fork pointer */
1453 int new_size) /* new indirection array size */
1455 int nlists; /* number of irec's (ex lists) */
1456 int size; /* current indirection array size */
1458 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1459 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1460 size = nlists * sizeof(xfs_ext_irec_t);
1461 ASSERT(ifp->if_real_bytes);
1462 ASSERT((new_size >= 0) && (new_size != size));
1463 if (new_size == 0) {
1464 xfs_iext_destroy(ifp);
1466 ifp->if_u1.if_ext_irec = (xfs_ext_irec_t *)
1467 kmem_realloc(ifp->if_u1.if_ext_irec,
1468 new_size, size, KM_NOFS);
1473 * Switch from indirection array to linear (direct) extent allocations.
1476 xfs_iext_indirect_to_direct(
1477 xfs_ifork_t *ifp) /* inode fork pointer */
1479 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1480 xfs_extnum_t nextents; /* number of extents in file */
1481 int size; /* size of file extents */
1483 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1484 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1485 ASSERT(nextents <= XFS_LINEAR_EXTS);
1486 size = nextents * sizeof(xfs_bmbt_rec_t);
1488 xfs_iext_irec_compact_pages(ifp);
1489 ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ);
1491 ep = ifp->if_u1.if_ext_irec->er_extbuf;
1492 kmem_free(ifp->if_u1.if_ext_irec);
1493 ifp->if_flags &= ~XFS_IFEXTIREC;
1494 ifp->if_u1.if_extents = ep;
1495 ifp->if_bytes = size;
1496 if (nextents < XFS_LINEAR_EXTS) {
1497 xfs_iext_realloc_direct(ifp, size);
1502 * Free incore file extents.
1506 xfs_ifork_t *ifp) /* inode fork pointer */
1508 if (ifp->if_flags & XFS_IFEXTIREC) {
1512 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1513 for (erp_idx = nlists - 1; erp_idx >= 0 ; erp_idx--) {
1514 xfs_iext_irec_remove(ifp, erp_idx);
1516 ifp->if_flags &= ~XFS_IFEXTIREC;
1517 } else if (ifp->if_real_bytes) {
1518 kmem_free(ifp->if_u1.if_extents);
1519 } else if (ifp->if_bytes) {
1520 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
1521 sizeof(xfs_bmbt_rec_t));
1523 ifp->if_u1.if_extents = NULL;
1524 ifp->if_real_bytes = 0;
1529 * Return a pointer to the extent record for file system block bno.
1531 xfs_bmbt_rec_host_t * /* pointer to found extent record */
1532 xfs_iext_bno_to_ext(
1533 xfs_ifork_t *ifp, /* inode fork pointer */
1534 xfs_fileoff_t bno, /* block number to search for */
1535 xfs_extnum_t *idxp) /* index of target extent */
1537 xfs_bmbt_rec_host_t *base; /* pointer to first extent */
1538 xfs_filblks_t blockcount = 0; /* number of blocks in extent */
1539 xfs_bmbt_rec_host_t *ep = NULL; /* pointer to target extent */
1540 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
1541 int high; /* upper boundary in search */
1542 xfs_extnum_t idx = 0; /* index of target extent */
1543 int low; /* lower boundary in search */
1544 xfs_extnum_t nextents; /* number of file extents */
1545 xfs_fileoff_t startoff = 0; /* start offset of extent */
1547 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1548 if (nextents == 0) {
1553 if (ifp->if_flags & XFS_IFEXTIREC) {
1554 /* Find target extent list */
1556 erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx);
1557 base = erp->er_extbuf;
1558 high = erp->er_extcount - 1;
1560 base = ifp->if_u1.if_extents;
1561 high = nextents - 1;
1563 /* Binary search extent records */
1564 while (low <= high) {
1565 idx = (low + high) >> 1;
1567 startoff = xfs_bmbt_get_startoff(ep);
1568 blockcount = xfs_bmbt_get_blockcount(ep);
1569 if (bno < startoff) {
1571 } else if (bno >= startoff + blockcount) {
1574 /* Convert back to file-based extent index */
1575 if (ifp->if_flags & XFS_IFEXTIREC) {
1576 idx += erp->er_extoff;
1582 /* Convert back to file-based extent index */
1583 if (ifp->if_flags & XFS_IFEXTIREC) {
1584 idx += erp->er_extoff;
1586 if (bno >= startoff + blockcount) {
1587 if (++idx == nextents) {
1590 ep = xfs_iext_get_ext(ifp, idx);
1598 * Return a pointer to the indirection array entry containing the
1599 * extent record for filesystem block bno. Store the index of the
1600 * target irec in *erp_idxp.
1602 xfs_ext_irec_t * /* pointer to found extent record */
1603 xfs_iext_bno_to_irec(
1604 xfs_ifork_t *ifp, /* inode fork pointer */
1605 xfs_fileoff_t bno, /* block number to search for */
1606 int *erp_idxp) /* irec index of target ext list */
1608 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
1609 xfs_ext_irec_t *erp_next; /* next indirection array entry */
1610 int erp_idx; /* indirection array index */
1611 int nlists; /* number of extent irec's (lists) */
1612 int high; /* binary search upper limit */
1613 int low; /* binary search lower limit */
1615 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1616 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1620 while (low <= high) {
1621 erp_idx = (low + high) >> 1;
1622 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1623 erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL;
1624 if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) {
1626 } else if (erp_next && bno >=
1627 xfs_bmbt_get_startoff(erp_next->er_extbuf)) {
1633 *erp_idxp = erp_idx;
1638 * Return a pointer to the indirection array entry containing the
1639 * extent record at file extent index *idxp. Store the index of the
1640 * target irec in *erp_idxp and store the page index of the target
1641 * extent record in *idxp.
1644 xfs_iext_idx_to_irec(
1645 xfs_ifork_t *ifp, /* inode fork pointer */
1646 xfs_extnum_t *idxp, /* extent index (file -> page) */
1647 int *erp_idxp, /* pointer to target irec */
1648 int realloc) /* new bytes were just added */
1650 xfs_ext_irec_t *prev; /* pointer to previous irec */
1651 xfs_ext_irec_t *erp = NULL; /* pointer to current irec */
1652 int erp_idx; /* indirection array index */
1653 int nlists; /* number of irec's (ex lists) */
1654 int high; /* binary search upper limit */
1655 int low; /* binary search lower limit */
1656 xfs_extnum_t page_idx = *idxp; /* extent index in target list */
1658 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1659 ASSERT(page_idx >= 0);
1660 ASSERT(page_idx <= ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
1661 ASSERT(page_idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t) || realloc);
1663 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1668 /* Binary search extent irec's */
1669 while (low <= high) {
1670 erp_idx = (low + high) >> 1;
1671 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1672 prev = erp_idx > 0 ? erp - 1 : NULL;
1673 if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff &&
1674 realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) {
1676 } else if (page_idx > erp->er_extoff + erp->er_extcount ||
1677 (page_idx == erp->er_extoff + erp->er_extcount &&
1680 } else if (page_idx == erp->er_extoff + erp->er_extcount &&
1681 erp->er_extcount == XFS_LINEAR_EXTS) {
1685 erp = erp_idx < nlists ? erp + 1 : NULL;
1688 page_idx -= erp->er_extoff;
1693 *erp_idxp = erp_idx;
1698 * Allocate and initialize an indirection array once the space needed
1699 * for incore extents increases above XFS_IEXT_BUFSZ.
1703 xfs_ifork_t *ifp) /* inode fork pointer */
1705 xfs_ext_irec_t *erp; /* indirection array pointer */
1706 xfs_extnum_t nextents; /* number of extents in file */
1708 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
1709 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1710 ASSERT(nextents <= XFS_LINEAR_EXTS);
1712 erp = kmem_alloc(sizeof(xfs_ext_irec_t), KM_NOFS);
1714 if (nextents == 0) {
1715 ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
1716 } else if (!ifp->if_real_bytes) {
1717 xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ);
1718 } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) {
1719 xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ);
1721 erp->er_extbuf = ifp->if_u1.if_extents;
1722 erp->er_extcount = nextents;
1725 ifp->if_flags |= XFS_IFEXTIREC;
1726 ifp->if_real_bytes = XFS_IEXT_BUFSZ;
1727 ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t);
1728 ifp->if_u1.if_ext_irec = erp;
1734 * Allocate and initialize a new entry in the indirection array.
1738 xfs_ifork_t *ifp, /* inode fork pointer */
1739 int erp_idx) /* index for new irec */
1741 xfs_ext_irec_t *erp; /* indirection array pointer */
1742 int i; /* loop counter */
1743 int nlists; /* number of irec's (ex lists) */
1745 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1746 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1748 /* Resize indirection array */
1749 xfs_iext_realloc_indirect(ifp, ++nlists *
1750 sizeof(xfs_ext_irec_t));
1752 * Move records down in the array so the
1753 * new page can use erp_idx.
1755 erp = ifp->if_u1.if_ext_irec;
1756 for (i = nlists - 1; i > erp_idx; i--) {
1757 memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t));
1759 ASSERT(i == erp_idx);
1761 /* Initialize new extent record */
1762 erp = ifp->if_u1.if_ext_irec;
1763 erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
1764 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
1765 memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ);
1766 erp[erp_idx].er_extcount = 0;
1767 erp[erp_idx].er_extoff = erp_idx > 0 ?
1768 erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0;
1769 return (&erp[erp_idx]);
1773 * Remove a record from the indirection array.
1776 xfs_iext_irec_remove(
1777 xfs_ifork_t *ifp, /* inode fork pointer */
1778 int erp_idx) /* irec index to remove */
1780 xfs_ext_irec_t *erp; /* indirection array pointer */
1781 int i; /* loop counter */
1782 int nlists; /* number of irec's (ex lists) */
1784 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1785 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1786 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1787 if (erp->er_extbuf) {
1788 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1,
1790 kmem_free(erp->er_extbuf);
1792 /* Compact extent records */
1793 erp = ifp->if_u1.if_ext_irec;
1794 for (i = erp_idx; i < nlists - 1; i++) {
1795 memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t));
1798 * Manually free the last extent record from the indirection
1799 * array. A call to xfs_iext_realloc_indirect() with a size
1800 * of zero would result in a call to xfs_iext_destroy() which
1801 * would in turn call this function again, creating a nasty
1805 xfs_iext_realloc_indirect(ifp,
1806 nlists * sizeof(xfs_ext_irec_t));
1808 kmem_free(ifp->if_u1.if_ext_irec);
1810 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
1814 * This is called to clean up large amounts of unused memory allocated
1815 * by the indirection array. Before compacting anything though, verify
1816 * that the indirection array is still needed and switch back to the
1817 * linear extent list (or even the inline buffer) if possible. The
1818 * compaction policy is as follows:
1820 * Full Compaction: Extents fit into a single page (or inline buffer)
1821 * Partial Compaction: Extents occupy less than 50% of allocated space
1822 * No Compaction: Extents occupy at least 50% of allocated space
1825 xfs_iext_irec_compact(
1826 xfs_ifork_t *ifp) /* inode fork pointer */
1828 xfs_extnum_t nextents; /* number of extents in file */
1829 int nlists; /* number of irec's (ex lists) */
1831 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1832 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1833 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1835 if (nextents == 0) {
1836 xfs_iext_destroy(ifp);
1837 } else if (nextents <= XFS_INLINE_EXTS) {
1838 xfs_iext_indirect_to_direct(ifp);
1839 xfs_iext_direct_to_inline(ifp, nextents);
1840 } else if (nextents <= XFS_LINEAR_EXTS) {
1841 xfs_iext_indirect_to_direct(ifp);
1842 } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) {
1843 xfs_iext_irec_compact_pages(ifp);
1848 * Combine extents from neighboring extent pages.
1851 xfs_iext_irec_compact_pages(
1852 xfs_ifork_t *ifp) /* inode fork pointer */
1854 xfs_ext_irec_t *erp, *erp_next;/* pointers to irec entries */
1855 int erp_idx = 0; /* indirection array index */
1856 int nlists; /* number of irec's (ex lists) */
1858 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1859 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1860 while (erp_idx < nlists - 1) {
1861 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1863 if (erp_next->er_extcount <=
1864 (XFS_LINEAR_EXTS - erp->er_extcount)) {
1865 memcpy(&erp->er_extbuf[erp->er_extcount],
1866 erp_next->er_extbuf, erp_next->er_extcount *
1867 sizeof(xfs_bmbt_rec_t));
1868 erp->er_extcount += erp_next->er_extcount;
1870 * Free page before removing extent record
1871 * so er_extoffs don't get modified in
1872 * xfs_iext_irec_remove.
1874 kmem_free(erp_next->er_extbuf);
1875 erp_next->er_extbuf = NULL;
1876 xfs_iext_irec_remove(ifp, erp_idx + 1);
1877 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1885 * This is called to update the er_extoff field in the indirection
1886 * array when extents have been added or removed from one of the
1887 * extent lists. erp_idx contains the irec index to begin updating
1888 * at and ext_diff contains the number of extents that were added
1892 xfs_iext_irec_update_extoffs(
1893 xfs_ifork_t *ifp, /* inode fork pointer */
1894 int erp_idx, /* irec index to update */
1895 int ext_diff) /* number of new extents */
1897 int i; /* loop counter */
1898 int nlists; /* number of irec's (ex lists */
1900 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1901 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1902 for (i = erp_idx; i < nlists; i++) {
1903 ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff;