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_btree.h"
30 #include "xfs_bmap_btree.h"
32 #include "xfs_error.h"
33 #include "xfs_trace.h"
34 #include "xfs_attr_sf.h"
35 #include "xfs_da_format.h"
36 #include "xfs_da_btree.h"
37 #include "xfs_dir2_priv.h"
39 kmem_zone_t *xfs_ifork_zone;
41 STATIC int xfs_iformat_local(xfs_inode_t *, xfs_dinode_t *, int, int);
42 STATIC int xfs_iformat_extents(xfs_inode_t *, xfs_dinode_t *, int);
43 STATIC int xfs_iformat_btree(xfs_inode_t *, xfs_dinode_t *, int);
47 * Make sure that the extents in the given memory buffer
57 xfs_bmbt_rec_host_t rec;
60 for (i = 0; i < nrecs; i++) {
61 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
62 rec.l0 = get_unaligned(&ep->l0);
63 rec.l1 = get_unaligned(&ep->l1);
64 xfs_bmbt_get_all(&rec, &irec);
65 if (fmt == XFS_EXTFMT_NOSTATE)
66 ASSERT(irec.br_state == XFS_EXT_NORM);
70 #define xfs_validate_extents(ifp, nrecs, fmt)
75 * Move inode type and inode format specific information from the
76 * on-disk inode to the in-core inode. For fifos, devs, and sockets
77 * this means set if_rdev to the proper value. For files, directories,
78 * and symlinks this means to bring in the in-line data or extent
79 * pointers. For a file in B-tree format, only the root is immediately
80 * brought in-core. The rest will be in-lined in if_extents when it
81 * is first referenced (see xfs_iread_extents()).
88 xfs_attr_shortform_t *atp;
93 if (unlikely(be32_to_cpu(dip->di_nextents) +
94 be16_to_cpu(dip->di_anextents) >
95 be64_to_cpu(dip->di_nblocks))) {
97 "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.",
98 (unsigned long long)ip->i_ino,
99 (int)(be32_to_cpu(dip->di_nextents) +
100 be16_to_cpu(dip->di_anextents)),
102 be64_to_cpu(dip->di_nblocks));
103 XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW,
105 return -EFSCORRUPTED;
108 if (unlikely(dip->di_forkoff > ip->i_mount->m_sb.sb_inodesize)) {
109 xfs_warn(ip->i_mount, "corrupt dinode %Lu, forkoff = 0x%x.",
110 (unsigned long long)ip->i_ino,
112 XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW,
114 return -EFSCORRUPTED;
117 if (unlikely((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) &&
118 !ip->i_mount->m_rtdev_targp)) {
119 xfs_warn(ip->i_mount,
120 "corrupt dinode %Lu, has realtime flag set.",
122 XFS_CORRUPTION_ERROR("xfs_iformat(realtime)",
123 XFS_ERRLEVEL_LOW, ip->i_mount, dip);
124 return -EFSCORRUPTED;
127 if (unlikely(xfs_is_reflink_inode(ip) &&
128 (VFS_I(ip)->i_mode & S_IFMT) != S_IFREG)) {
129 xfs_warn(ip->i_mount,
130 "corrupt dinode %llu, wrong file type for reflink.",
132 XFS_CORRUPTION_ERROR("xfs_iformat(reflink)",
133 XFS_ERRLEVEL_LOW, ip->i_mount, dip);
134 return -EFSCORRUPTED;
137 if (unlikely(xfs_is_reflink_inode(ip) &&
138 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME))) {
139 xfs_warn(ip->i_mount,
140 "corrupt dinode %llu, has reflink+realtime flag set.",
142 XFS_CORRUPTION_ERROR("xfs_iformat(reflink)",
143 XFS_ERRLEVEL_LOW, ip->i_mount, dip);
144 return -EFSCORRUPTED;
147 switch (VFS_I(ip)->i_mode & S_IFMT) {
152 if (unlikely(dip->di_format != XFS_DINODE_FMT_DEV)) {
153 XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW,
155 return -EFSCORRUPTED;
158 ip->i_df.if_u2.if_rdev = xfs_dinode_get_rdev(dip);
164 switch (dip->di_format) {
165 case XFS_DINODE_FMT_LOCAL:
167 * no local regular files yet
169 if (unlikely(S_ISREG(be16_to_cpu(dip->di_mode)))) {
170 xfs_warn(ip->i_mount,
171 "corrupt inode %Lu (local format for regular file).",
172 (unsigned long long) ip->i_ino);
173 XFS_CORRUPTION_ERROR("xfs_iformat(4)",
176 return -EFSCORRUPTED;
179 di_size = be64_to_cpu(dip->di_size);
180 if (unlikely(di_size < 0 ||
181 di_size > XFS_DFORK_DSIZE(dip, ip->i_mount))) {
182 xfs_warn(ip->i_mount,
183 "corrupt inode %Lu (bad size %Ld for local inode).",
184 (unsigned long long) ip->i_ino,
185 (long long) di_size);
186 XFS_CORRUPTION_ERROR("xfs_iformat(5)",
189 return -EFSCORRUPTED;
193 error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size);
195 case XFS_DINODE_FMT_EXTENTS:
196 error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
198 case XFS_DINODE_FMT_BTREE:
199 error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
202 XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW,
204 return -EFSCORRUPTED;
209 XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW, ip->i_mount);
210 return -EFSCORRUPTED;
215 /* Check inline dir contents. */
216 if (S_ISDIR(VFS_I(ip)->i_mode) &&
217 dip->di_format == XFS_DINODE_FMT_LOCAL) {
218 error = xfs_dir2_sf_verify(ip);
220 xfs_idestroy_fork(ip, XFS_DATA_FORK);
225 if (xfs_is_reflink_inode(ip)) {
226 ASSERT(ip->i_cowfp == NULL);
227 xfs_ifork_init_cow(ip);
230 if (!XFS_DFORK_Q(dip))
233 ASSERT(ip->i_afp == NULL);
234 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP | KM_NOFS);
236 switch (dip->di_aformat) {
237 case XFS_DINODE_FMT_LOCAL:
238 atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
239 size = be16_to_cpu(atp->hdr.totsize);
241 if (unlikely(size < sizeof(struct xfs_attr_sf_hdr))) {
242 xfs_warn(ip->i_mount,
243 "corrupt inode %Lu (bad attr fork size %Ld).",
244 (unsigned long long) ip->i_ino,
246 XFS_CORRUPTION_ERROR("xfs_iformat(8)",
249 error = -EFSCORRUPTED;
253 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size);
255 case XFS_DINODE_FMT_EXTENTS:
256 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
258 case XFS_DINODE_FMT_BTREE:
259 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
262 error = -EFSCORRUPTED;
266 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
269 kmem_zone_free(xfs_ifork_zone, ip->i_cowfp);
271 xfs_idestroy_fork(ip, XFS_DATA_FORK);
278 struct xfs_inode *ip,
283 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
284 int mem_size = size, real_size = 0;
288 * If we are using the local fork to store a symlink body we need to
289 * zero-terminate it so that we can pass it back to the VFS directly.
290 * Overallocate the in-memory fork by one for that and add a zero
291 * to terminate it below.
293 zero_terminate = S_ISLNK(VFS_I(ip)->i_mode);
298 ifp->if_u1.if_data = NULL;
299 else if (mem_size <= sizeof(ifp->if_u2.if_inline_data))
300 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
302 real_size = roundup(mem_size, 4);
303 ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP | KM_NOFS);
307 memcpy(ifp->if_u1.if_data, data, size);
309 ifp->if_u1.if_data[size] = '\0';
312 ifp->if_bytes = size;
313 ifp->if_real_bytes = real_size;
314 ifp->if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
315 ifp->if_flags |= XFS_IFINLINE;
319 * The file is in-lined in the on-disk inode.
320 * If it fits into if_inline_data, then copy
321 * it there, otherwise allocate a buffer for it
322 * and copy the data there. Either way, set
323 * if_data to point at the data.
324 * If we allocate a buffer for the data, make
325 * sure that its size is a multiple of 4 and
326 * record the real size in i_real_bytes.
336 * If the size is unreasonable, then something
337 * is wrong and we just bail out rather than crash in
338 * kmem_alloc() or memcpy() below.
340 if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
341 xfs_warn(ip->i_mount,
342 "corrupt inode %Lu (bad size %d for local fork, size = %d).",
343 (unsigned long long) ip->i_ino, size,
344 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
345 XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW,
347 return -EFSCORRUPTED;
350 xfs_init_local_fork(ip, whichfork, XFS_DFORK_PTR(dip, whichfork), size);
355 * The file consists of a set of extents all
356 * of which fit into the on-disk inode.
357 * If there are few enough extents to fit into
358 * the if_inline_ext, then copy them there.
359 * Otherwise allocate a buffer for them and copy
360 * them into it. Either way, set if_extents
361 * to point at the extents.
375 ifp = XFS_IFORK_PTR(ip, whichfork);
376 nex = XFS_DFORK_NEXTENTS(dip, whichfork);
377 size = nex * (uint)sizeof(xfs_bmbt_rec_t);
380 * If the number of extents is unreasonable, then something
381 * is wrong and we just bail out rather than crash in
382 * kmem_alloc() or memcpy() below.
384 if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
385 xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).",
386 (unsigned long long) ip->i_ino, nex);
387 XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW,
389 return -EFSCORRUPTED;
392 ifp->if_real_bytes = 0;
394 ifp->if_u1.if_extents = NULL;
395 else if (nex <= XFS_INLINE_EXTS)
396 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
398 xfs_iext_add(ifp, 0, nex);
400 ifp->if_bytes = size;
402 dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
403 xfs_validate_extents(ifp, nex, XFS_EXTFMT_INODE(ip));
404 for (i = 0; i < nex; i++, dp++) {
405 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
406 ep->l0 = get_unaligned_be64(&dp->l0);
407 ep->l1 = get_unaligned_be64(&dp->l1);
409 XFS_BMAP_TRACE_EXLIST(ip, nex, whichfork);
410 if (whichfork != XFS_DATA_FORK ||
411 XFS_EXTFMT_INODE(ip) == XFS_EXTFMT_NOSTATE)
412 if (unlikely(xfs_check_nostate_extents(
414 XFS_ERROR_REPORT("xfs_iformat_extents(2)",
417 return -EFSCORRUPTED;
420 ifp->if_flags |= XFS_IFEXTENTS;
425 * The file has too many extents to fit into
426 * the inode, so they are in B-tree format.
427 * Allocate a buffer for the root of the B-tree
428 * and copy the root into it. The i_extents
429 * field will remain NULL until all of the
430 * extents are read in (when they are needed).
438 struct xfs_mount *mp = ip->i_mount;
439 xfs_bmdr_block_t *dfp;
446 ifp = XFS_IFORK_PTR(ip, whichfork);
447 dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
448 size = XFS_BMAP_BROOT_SPACE(mp, dfp);
449 nrecs = be16_to_cpu(dfp->bb_numrecs);
450 level = be16_to_cpu(dfp->bb_level);
453 * blow out if -- fork has less extents than can fit in
454 * fork (fork shouldn't be a btree format), root btree
455 * block has more records than can fit into the fork,
456 * or the number of extents is greater than the number of
459 if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <=
460 XFS_IFORK_MAXEXT(ip, whichfork) ||
461 XFS_BMDR_SPACE_CALC(nrecs) >
462 XFS_DFORK_SIZE(dip, mp, whichfork) ||
463 XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks) ||
464 level == 0 || level > XFS_BTREE_MAXLEVELS) {
465 xfs_warn(mp, "corrupt inode %Lu (btree).",
466 (unsigned long long) ip->i_ino);
467 XFS_CORRUPTION_ERROR("xfs_iformat_btree", XFS_ERRLEVEL_LOW,
469 return -EFSCORRUPTED;
472 ifp->if_broot_bytes = size;
473 ifp->if_broot = kmem_alloc(size, KM_SLEEP | KM_NOFS);
474 ASSERT(ifp->if_broot != NULL);
476 * Copy and convert from the on-disk structure
477 * to the in-memory structure.
479 xfs_bmdr_to_bmbt(ip, dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
480 ifp->if_broot, size);
481 ifp->if_flags &= ~XFS_IFEXTENTS;
482 ifp->if_flags |= XFS_IFBROOT;
488 * Read in extents from a btree-format inode.
489 * Allocate and fill in if_extents. Real work is done in xfs_bmap.c.
499 xfs_extnum_t nextents;
501 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
503 if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
504 XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW,
506 return -EFSCORRUPTED;
508 nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
509 ifp = XFS_IFORK_PTR(ip, whichfork);
512 * We know that the size is valid (it's checked in iformat_btree)
514 ifp->if_bytes = ifp->if_real_bytes = 0;
515 xfs_iext_add(ifp, 0, nextents);
516 error = xfs_bmap_read_extents(tp, ip, whichfork);
518 xfs_iext_destroy(ifp);
521 xfs_validate_extents(ifp, nextents, XFS_EXTFMT_INODE(ip));
522 ifp->if_flags |= XFS_IFEXTENTS;
526 * Reallocate the space for if_broot based on the number of records
527 * being added or deleted as indicated in rec_diff. Move the records
528 * and pointers in if_broot to fit the new size. When shrinking this
529 * will eliminate holes between the records and pointers created by
530 * the caller. When growing this will create holes to be filled in
533 * The caller must not request to add more records than would fit in
534 * the on-disk inode root. If the if_broot is currently NULL, then
535 * if we are adding records, one will be allocated. The caller must also
536 * not request that the number of records go below zero, although
539 * ip -- the inode whose if_broot area is changing
540 * ext_diff -- the change in the number of records, positive or negative,
541 * requested for the if_broot array.
549 struct xfs_mount *mp = ip->i_mount;
552 struct xfs_btree_block *new_broot;
559 * Handle the degenerate case quietly.
565 ifp = XFS_IFORK_PTR(ip, whichfork);
568 * If there wasn't any memory allocated before, just
569 * allocate it now and get out.
571 if (ifp->if_broot_bytes == 0) {
572 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
573 ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
574 ifp->if_broot_bytes = (int)new_size;
579 * If there is already an existing if_broot, then we need
580 * to realloc() it and shift the pointers to their new
581 * location. The records don't change location because
582 * they are kept butted up against the btree block header.
584 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
585 new_max = cur_max + rec_diff;
586 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
587 ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
589 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
590 ifp->if_broot_bytes);
591 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
593 ifp->if_broot_bytes = (int)new_size;
594 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
595 XFS_IFORK_SIZE(ip, whichfork));
596 memmove(np, op, cur_max * (uint)sizeof(xfs_fsblock_t));
601 * rec_diff is less than 0. In this case, we are shrinking the
602 * if_broot buffer. It must already exist. If we go to zero
603 * records, just get rid of the root and clear the status bit.
605 ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
606 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
607 new_max = cur_max + rec_diff;
608 ASSERT(new_max >= 0);
610 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
614 new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
616 * First copy over the btree block header.
618 memcpy(new_broot, ifp->if_broot,
619 XFS_BMBT_BLOCK_LEN(ip->i_mount));
622 ifp->if_flags &= ~XFS_IFBROOT;
626 * Only copy the records and pointers if there are any.
630 * First copy the records.
632 op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
633 np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
634 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
637 * Then copy the pointers.
639 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
640 ifp->if_broot_bytes);
641 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
643 memcpy(np, op, new_max * (uint)sizeof(xfs_fsblock_t));
645 kmem_free(ifp->if_broot);
646 ifp->if_broot = new_broot;
647 ifp->if_broot_bytes = (int)new_size;
649 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
650 XFS_IFORK_SIZE(ip, whichfork));
656 * This is called when the amount of space needed for if_data
657 * is increased or decreased. The change in size is indicated by
658 * the number of bytes that need to be added or deleted in the
659 * byte_diff parameter.
661 * If the amount of space needed has decreased below the size of the
662 * inline buffer, then switch to using the inline buffer. Otherwise,
663 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
666 * ip -- the inode whose if_data area is changing
667 * byte_diff -- the change in the number of bytes, positive or negative,
668 * requested for the if_data array.
680 if (byte_diff == 0) {
684 ifp = XFS_IFORK_PTR(ip, whichfork);
685 new_size = (int)ifp->if_bytes + byte_diff;
686 ASSERT(new_size >= 0);
689 if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
690 kmem_free(ifp->if_u1.if_data);
692 ifp->if_u1.if_data = NULL;
694 } else if (new_size <= sizeof(ifp->if_u2.if_inline_data)) {
696 * If the valid extents/data can fit in if_inline_ext/data,
697 * copy them from the malloc'd vector and free it.
699 if (ifp->if_u1.if_data == NULL) {
700 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
701 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
702 ASSERT(ifp->if_real_bytes != 0);
703 memcpy(ifp->if_u2.if_inline_data, ifp->if_u1.if_data,
705 kmem_free(ifp->if_u1.if_data);
706 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
711 * Stuck with malloc/realloc.
712 * For inline data, the underlying buffer must be
713 * a multiple of 4 bytes in size so that it can be
714 * logged and stay on word boundaries. We enforce
717 real_size = roundup(new_size, 4);
718 if (ifp->if_u1.if_data == NULL) {
719 ASSERT(ifp->if_real_bytes == 0);
720 ifp->if_u1.if_data = kmem_alloc(real_size,
722 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
724 * Only do the realloc if the underlying size
725 * is really changing.
727 if (ifp->if_real_bytes != real_size) {
729 kmem_realloc(ifp->if_u1.if_data,
734 ASSERT(ifp->if_real_bytes == 0);
735 ifp->if_u1.if_data = kmem_alloc(real_size,
737 memcpy(ifp->if_u1.if_data, ifp->if_u2.if_inline_data,
741 ifp->if_real_bytes = real_size;
742 ifp->if_bytes = new_size;
743 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
753 ifp = XFS_IFORK_PTR(ip, whichfork);
754 if (ifp->if_broot != NULL) {
755 kmem_free(ifp->if_broot);
756 ifp->if_broot = NULL;
760 * If the format is local, then we can't have an extents
761 * array so just look for an inline data array. If we're
762 * not local then we may or may not have an extents list,
763 * so check and free it up if we do.
765 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
766 if ((ifp->if_u1.if_data != ifp->if_u2.if_inline_data) &&
767 (ifp->if_u1.if_data != NULL)) {
768 ASSERT(ifp->if_real_bytes != 0);
769 kmem_free(ifp->if_u1.if_data);
770 ifp->if_u1.if_data = NULL;
771 ifp->if_real_bytes = 0;
773 } else if ((ifp->if_flags & XFS_IFEXTENTS) &&
774 ((ifp->if_flags & XFS_IFEXTIREC) ||
775 ((ifp->if_u1.if_extents != NULL) &&
776 (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext)))) {
777 ASSERT(ifp->if_real_bytes != 0);
778 xfs_iext_destroy(ifp);
780 ASSERT(ifp->if_u1.if_extents == NULL ||
781 ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext);
782 ASSERT(ifp->if_real_bytes == 0);
783 if (whichfork == XFS_ATTR_FORK) {
784 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
786 } else if (whichfork == XFS_COW_FORK) {
787 kmem_zone_free(xfs_ifork_zone, ip->i_cowfp);
792 /* Count number of incore extents based on if_bytes */
794 xfs_iext_count(struct xfs_ifork *ifp)
796 return ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
800 * Convert in-core extents to on-disk form
802 * For either the data or attr fork in extent format, we need to endian convert
803 * the in-core extent as we place them into the on-disk inode.
805 * In the case of the data fork, the in-core and on-disk fork sizes can be
806 * different due to delayed allocation extents. We only copy on-disk extents
807 * here, so callers must always use the physical fork size to determine the
808 * size of the buffer passed to this routine. We will return the size actually
821 xfs_fsblock_t start_block;
823 ifp = XFS_IFORK_PTR(ip, whichfork);
824 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
825 ASSERT(ifp->if_bytes > 0);
827 nrecs = xfs_iext_count(ifp);
828 XFS_BMAP_TRACE_EXLIST(ip, nrecs, whichfork);
832 * There are some delayed allocation extents in the
833 * inode, so copy the extents one at a time and skip
834 * the delayed ones. There must be at least one
835 * non-delayed extent.
838 for (i = 0; i < nrecs; i++) {
839 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
840 start_block = xfs_bmbt_get_startblock(ep);
841 if (isnullstartblock(start_block)) {
843 * It's a delayed allocation extent, so skip it.
848 /* Translate to on disk format */
849 put_unaligned_be64(ep->l0, &dp->l0);
850 put_unaligned_be64(ep->l1, &dp->l1);
855 xfs_validate_extents(ifp, copied, XFS_EXTFMT_INODE(ip));
857 return (copied * (uint)sizeof(xfs_bmbt_rec_t));
861 * Each of the following cases stores data into the same region
862 * of the on-disk inode, so only one of them can be valid at
863 * any given time. While it is possible to have conflicting formats
864 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
865 * in EXTENTS format, this can only happen when the fork has
866 * changed formats after being modified but before being flushed.
867 * In these cases, the format always takes precedence, because the
868 * format indicates the current state of the fork.
874 xfs_inode_log_item_t *iip,
880 static const short brootflag[2] =
881 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
882 static const short dataflag[2] =
883 { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
884 static const short extflag[2] =
885 { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
889 ifp = XFS_IFORK_PTR(ip, whichfork);
891 * This can happen if we gave up in iformat in an error path,
892 * for the attribute fork.
895 ASSERT(whichfork == XFS_ATTR_FORK);
898 cp = XFS_DFORK_PTR(dip, whichfork);
900 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
901 case XFS_DINODE_FMT_LOCAL:
902 if ((iip->ili_fields & dataflag[whichfork]) &&
903 (ifp->if_bytes > 0)) {
904 ASSERT(ifp->if_u1.if_data != NULL);
905 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
906 memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
910 case XFS_DINODE_FMT_EXTENTS:
911 ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
912 !(iip->ili_fields & extflag[whichfork]));
913 if ((iip->ili_fields & extflag[whichfork]) &&
914 (ifp->if_bytes > 0)) {
915 ASSERT(xfs_iext_get_ext(ifp, 0));
916 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
917 (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
922 case XFS_DINODE_FMT_BTREE:
923 if ((iip->ili_fields & brootflag[whichfork]) &&
924 (ifp->if_broot_bytes > 0)) {
925 ASSERT(ifp->if_broot != NULL);
926 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
927 XFS_IFORK_SIZE(ip, whichfork));
928 xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
929 (xfs_bmdr_block_t *)cp,
930 XFS_DFORK_SIZE(dip, mp, whichfork));
934 case XFS_DINODE_FMT_DEV:
935 if (iip->ili_fields & XFS_ILOG_DEV) {
936 ASSERT(whichfork == XFS_DATA_FORK);
937 xfs_dinode_put_rdev(dip, ip->i_df.if_u2.if_rdev);
941 case XFS_DINODE_FMT_UUID:
942 if (iip->ili_fields & XFS_ILOG_UUID) {
943 ASSERT(whichfork == XFS_DATA_FORK);
944 memcpy(XFS_DFORK_DPTR(dip),
945 &ip->i_df.if_u2.if_uuid,
957 * Return a pointer to the extent record at file index idx.
959 xfs_bmbt_rec_host_t *
961 xfs_ifork_t *ifp, /* inode fork pointer */
962 xfs_extnum_t idx) /* index of target extent */
965 ASSERT(idx < xfs_iext_count(ifp));
967 if ((ifp->if_flags & XFS_IFEXTIREC) && (idx == 0)) {
968 return ifp->if_u1.if_ext_irec->er_extbuf;
969 } else if (ifp->if_flags & XFS_IFEXTIREC) {
970 xfs_ext_irec_t *erp; /* irec pointer */
971 int erp_idx = 0; /* irec index */
972 xfs_extnum_t page_idx = idx; /* ext index in target list */
974 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
975 return &erp->er_extbuf[page_idx];
976 } else if (ifp->if_bytes) {
977 return &ifp->if_u1.if_extents[idx];
983 /* Convert bmap state flags to an inode fork. */
985 xfs_iext_state_to_fork(
986 struct xfs_inode *ip,
989 if (state & BMAP_COWFORK)
991 else if (state & BMAP_ATTRFORK)
997 * Insert new item(s) into the extent records for incore inode
998 * fork 'ifp'. 'count' new items are inserted at index 'idx'.
1002 xfs_inode_t *ip, /* incore inode pointer */
1003 xfs_extnum_t idx, /* starting index of new items */
1004 xfs_extnum_t count, /* number of inserted items */
1005 xfs_bmbt_irec_t *new, /* items to insert */
1006 int state) /* type of extent conversion */
1008 xfs_ifork_t *ifp = xfs_iext_state_to_fork(ip, state);
1009 xfs_extnum_t i; /* extent record index */
1011 trace_xfs_iext_insert(ip, idx, new, state, _RET_IP_);
1013 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1014 xfs_iext_add(ifp, idx, count);
1015 for (i = idx; i < idx + count; i++, new++)
1016 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, i), new);
1020 * This is called when the amount of space required for incore file
1021 * extents needs to be increased. The ext_diff parameter stores the
1022 * number of new extents being added and the idx parameter contains
1023 * the extent index where the new extents will be added. If the new
1024 * extents are being appended, then we just need to (re)allocate and
1025 * initialize the space. Otherwise, if the new extents are being
1026 * inserted into the middle of the existing entries, a bit more work
1027 * is required to make room for the new extents to be inserted. The
1028 * caller is responsible for filling in the new extent entries upon
1033 xfs_ifork_t *ifp, /* inode fork pointer */
1034 xfs_extnum_t idx, /* index to begin adding exts */
1035 int ext_diff) /* number of extents to add */
1037 int byte_diff; /* new bytes being added */
1038 int new_size; /* size of extents after adding */
1039 xfs_extnum_t nextents; /* number of extents in file */
1041 nextents = xfs_iext_count(ifp);
1042 ASSERT((idx >= 0) && (idx <= nextents));
1043 byte_diff = ext_diff * sizeof(xfs_bmbt_rec_t);
1044 new_size = ifp->if_bytes + byte_diff;
1046 * If the new number of extents (nextents + ext_diff)
1047 * fits inside the inode, then continue to use the inline
1050 if (nextents + ext_diff <= XFS_INLINE_EXTS) {
1051 if (idx < nextents) {
1052 memmove(&ifp->if_u2.if_inline_ext[idx + ext_diff],
1053 &ifp->if_u2.if_inline_ext[idx],
1054 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
1055 memset(&ifp->if_u2.if_inline_ext[idx], 0, byte_diff);
1057 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
1058 ifp->if_real_bytes = 0;
1061 * Otherwise use a linear (direct) extent list.
1062 * If the extents are currently inside the inode,
1063 * xfs_iext_realloc_direct will switch us from
1064 * inline to direct extent allocation mode.
1066 else if (nextents + ext_diff <= XFS_LINEAR_EXTS) {
1067 xfs_iext_realloc_direct(ifp, new_size);
1068 if (idx < nextents) {
1069 memmove(&ifp->if_u1.if_extents[idx + ext_diff],
1070 &ifp->if_u1.if_extents[idx],
1071 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
1072 memset(&ifp->if_u1.if_extents[idx], 0, byte_diff);
1075 /* Indirection array */
1077 xfs_ext_irec_t *erp;
1081 ASSERT(nextents + ext_diff > XFS_LINEAR_EXTS);
1082 if (ifp->if_flags & XFS_IFEXTIREC) {
1083 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 1);
1085 xfs_iext_irec_init(ifp);
1086 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1087 erp = ifp->if_u1.if_ext_irec;
1089 /* Extents fit in target extent page */
1090 if (erp && erp->er_extcount + ext_diff <= XFS_LINEAR_EXTS) {
1091 if (page_idx < erp->er_extcount) {
1092 memmove(&erp->er_extbuf[page_idx + ext_diff],
1093 &erp->er_extbuf[page_idx],
1094 (erp->er_extcount - page_idx) *
1095 sizeof(xfs_bmbt_rec_t));
1096 memset(&erp->er_extbuf[page_idx], 0, byte_diff);
1098 erp->er_extcount += ext_diff;
1099 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
1101 /* Insert a new extent page */
1103 xfs_iext_add_indirect_multi(ifp,
1104 erp_idx, page_idx, ext_diff);
1107 * If extent(s) are being appended to the last page in
1108 * the indirection array and the new extent(s) don't fit
1109 * in the page, then erp is NULL and erp_idx is set to
1110 * the next index needed in the indirection array.
1113 uint count = ext_diff;
1116 erp = xfs_iext_irec_new(ifp, erp_idx);
1117 erp->er_extcount = min(count, XFS_LINEAR_EXTS);
1118 count -= erp->er_extcount;
1124 ifp->if_bytes = new_size;
1128 * This is called when incore extents are being added to the indirection
1129 * array and the new extents do not fit in the target extent list. The
1130 * erp_idx parameter contains the irec index for the target extent list
1131 * in the indirection array, and the idx parameter contains the extent
1132 * index within the list. The number of extents being added is stored
1133 * in the count parameter.
1135 * |-------| |-------|
1136 * | | | | idx - number of extents before idx
1138 * | | | | count - number of extents being inserted at idx
1139 * |-------| |-------|
1140 * | count | | nex2 | nex2 - number of extents after idx + count
1141 * |-------| |-------|
1144 xfs_iext_add_indirect_multi(
1145 xfs_ifork_t *ifp, /* inode fork pointer */
1146 int erp_idx, /* target extent irec index */
1147 xfs_extnum_t idx, /* index within target list */
1148 int count) /* new extents being added */
1150 int byte_diff; /* new bytes being added */
1151 xfs_ext_irec_t *erp; /* pointer to irec entry */
1152 xfs_extnum_t ext_diff; /* number of extents to add */
1153 xfs_extnum_t ext_cnt; /* new extents still needed */
1154 xfs_extnum_t nex2; /* extents after idx + count */
1155 xfs_bmbt_rec_t *nex2_ep = NULL; /* temp list for nex2 extents */
1156 int nlists; /* number of irec's (lists) */
1158 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1159 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1160 nex2 = erp->er_extcount - idx;
1161 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1164 * Save second part of target extent list
1165 * (all extents past */
1167 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
1168 nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_NOFS);
1169 memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff);
1170 erp->er_extcount -= nex2;
1171 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2);
1172 memset(&erp->er_extbuf[idx], 0, byte_diff);
1176 * Add the new extents to the end of the target
1177 * list, then allocate new irec record(s) and
1178 * extent buffer(s) as needed to store the rest
1179 * of the new extents.
1182 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount);
1184 erp->er_extcount += ext_diff;
1185 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
1186 ext_cnt -= ext_diff;
1190 erp = xfs_iext_irec_new(ifp, erp_idx);
1191 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS);
1192 erp->er_extcount = ext_diff;
1193 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
1194 ext_cnt -= ext_diff;
1197 /* Add nex2 extents back to indirection array */
1199 xfs_extnum_t ext_avail;
1202 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
1203 ext_avail = XFS_LINEAR_EXTS - erp->er_extcount;
1206 * If nex2 extents fit in the current page, append
1207 * nex2_ep after the new extents.
1209 if (nex2 <= ext_avail) {
1210 i = erp->er_extcount;
1213 * Otherwise, check if space is available in the
1216 else if ((erp_idx < nlists - 1) &&
1217 (nex2 <= (ext_avail = XFS_LINEAR_EXTS -
1218 ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) {
1221 /* Create a hole for nex2 extents */
1222 memmove(&erp->er_extbuf[nex2], erp->er_extbuf,
1223 erp->er_extcount * sizeof(xfs_bmbt_rec_t));
1226 * Final choice, create a new extent page for
1231 erp = xfs_iext_irec_new(ifp, erp_idx);
1233 memmove(&erp->er_extbuf[i], nex2_ep, byte_diff);
1235 erp->er_extcount += nex2;
1236 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2);
1241 * This is called when the amount of space required for incore file
1242 * extents needs to be decreased. The ext_diff parameter stores the
1243 * number of extents to be removed and the idx parameter contains
1244 * the extent index where the extents will be removed from.
1246 * If the amount of space needed has decreased below the linear
1247 * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
1248 * extent array. Otherwise, use kmem_realloc() to adjust the
1249 * size to what is needed.
1253 xfs_inode_t *ip, /* incore inode pointer */
1254 xfs_extnum_t idx, /* index to begin removing exts */
1255 int ext_diff, /* number of extents to remove */
1256 int state) /* type of extent conversion */
1258 xfs_ifork_t *ifp = xfs_iext_state_to_fork(ip, state);
1259 xfs_extnum_t nextents; /* number of extents in file */
1260 int new_size; /* size of extents after removal */
1262 trace_xfs_iext_remove(ip, idx, state, _RET_IP_);
1264 ASSERT(ext_diff > 0);
1265 nextents = xfs_iext_count(ifp);
1266 new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t);
1268 if (new_size == 0) {
1269 xfs_iext_destroy(ifp);
1270 } else if (ifp->if_flags & XFS_IFEXTIREC) {
1271 xfs_iext_remove_indirect(ifp, idx, ext_diff);
1272 } else if (ifp->if_real_bytes) {
1273 xfs_iext_remove_direct(ifp, idx, ext_diff);
1275 xfs_iext_remove_inline(ifp, idx, ext_diff);
1277 ifp->if_bytes = new_size;
1281 * This removes ext_diff extents from the inline buffer, beginning
1282 * at extent index idx.
1285 xfs_iext_remove_inline(
1286 xfs_ifork_t *ifp, /* inode fork pointer */
1287 xfs_extnum_t idx, /* index to begin removing exts */
1288 int ext_diff) /* number of extents to remove */
1290 int nextents; /* number of extents in file */
1292 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
1293 ASSERT(idx < XFS_INLINE_EXTS);
1294 nextents = xfs_iext_count(ifp);
1295 ASSERT(((nextents - ext_diff) > 0) &&
1296 (nextents - ext_diff) < XFS_INLINE_EXTS);
1298 if (idx + ext_diff < nextents) {
1299 memmove(&ifp->if_u2.if_inline_ext[idx],
1300 &ifp->if_u2.if_inline_ext[idx + ext_diff],
1301 (nextents - (idx + ext_diff)) *
1302 sizeof(xfs_bmbt_rec_t));
1303 memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff],
1304 0, ext_diff * sizeof(xfs_bmbt_rec_t));
1306 memset(&ifp->if_u2.if_inline_ext[idx], 0,
1307 ext_diff * sizeof(xfs_bmbt_rec_t));
1312 * This removes ext_diff extents from a linear (direct) extent list,
1313 * beginning at extent index idx. If the extents are being removed
1314 * from the end of the list (ie. truncate) then we just need to re-
1315 * allocate the list to remove the extra space. Otherwise, if the
1316 * extents are being removed from the middle of the existing extent
1317 * entries, then we first need to move the extent records beginning
1318 * at idx + ext_diff up in the list to overwrite the records being
1319 * removed, then remove the extra space via kmem_realloc.
1322 xfs_iext_remove_direct(
1323 xfs_ifork_t *ifp, /* inode fork pointer */
1324 xfs_extnum_t idx, /* index to begin removing exts */
1325 int ext_diff) /* number of extents to remove */
1327 xfs_extnum_t nextents; /* number of extents in file */
1328 int new_size; /* size of extents after removal */
1330 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
1331 new_size = ifp->if_bytes -
1332 (ext_diff * sizeof(xfs_bmbt_rec_t));
1333 nextents = xfs_iext_count(ifp);
1335 if (new_size == 0) {
1336 xfs_iext_destroy(ifp);
1339 /* Move extents up in the list (if needed) */
1340 if (idx + ext_diff < nextents) {
1341 memmove(&ifp->if_u1.if_extents[idx],
1342 &ifp->if_u1.if_extents[idx + ext_diff],
1343 (nextents - (idx + ext_diff)) *
1344 sizeof(xfs_bmbt_rec_t));
1346 memset(&ifp->if_u1.if_extents[nextents - ext_diff],
1347 0, ext_diff * sizeof(xfs_bmbt_rec_t));
1349 * Reallocate the direct extent list. If the extents
1350 * will fit inside the inode then xfs_iext_realloc_direct
1351 * will switch from direct to inline extent allocation
1354 xfs_iext_realloc_direct(ifp, new_size);
1355 ifp->if_bytes = new_size;
1359 * This is called when incore extents are being removed from the
1360 * indirection array and the extents being removed span multiple extent
1361 * buffers. The idx parameter contains the file extent index where we
1362 * want to begin removing extents, and the count parameter contains
1363 * how many extents need to be removed.
1365 * |-------| |-------|
1366 * | nex1 | | | nex1 - number of extents before idx
1367 * |-------| | count |
1368 * | | | | count - number of extents being removed at idx
1369 * | count | |-------|
1370 * | | | nex2 | nex2 - number of extents after idx + count
1371 * |-------| |-------|
1374 xfs_iext_remove_indirect(
1375 xfs_ifork_t *ifp, /* inode fork pointer */
1376 xfs_extnum_t idx, /* index to begin removing extents */
1377 int count) /* number of extents to remove */
1379 xfs_ext_irec_t *erp; /* indirection array pointer */
1380 int erp_idx = 0; /* indirection array index */
1381 xfs_extnum_t ext_cnt; /* extents left to remove */
1382 xfs_extnum_t ext_diff; /* extents to remove in current list */
1383 xfs_extnum_t nex1; /* number of extents before idx */
1384 xfs_extnum_t nex2; /* extents after idx + count */
1385 int page_idx = idx; /* index in target extent list */
1387 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1388 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
1389 ASSERT(erp != NULL);
1393 nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0);
1394 ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1));
1396 * Check for deletion of entire list;
1397 * xfs_iext_irec_remove() updates extent offsets.
1399 if (ext_diff == erp->er_extcount) {
1400 xfs_iext_irec_remove(ifp, erp_idx);
1401 ext_cnt -= ext_diff;
1404 ASSERT(erp_idx < ifp->if_real_bytes /
1406 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1413 /* Move extents up (if needed) */
1415 memmove(&erp->er_extbuf[nex1],
1416 &erp->er_extbuf[nex1 + ext_diff],
1417 nex2 * sizeof(xfs_bmbt_rec_t));
1419 /* Zero out rest of page */
1420 memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ -
1421 ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t))));
1422 /* Update remaining counters */
1423 erp->er_extcount -= ext_diff;
1424 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff);
1425 ext_cnt -= ext_diff;
1430 ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t);
1431 xfs_iext_irec_compact(ifp);
1435 * Create, destroy, or resize a linear (direct) block of extents.
1438 xfs_iext_realloc_direct(
1439 xfs_ifork_t *ifp, /* inode fork pointer */
1440 int new_size) /* new size of extents after adding */
1442 int rnew_size; /* real new size of extents */
1444 rnew_size = new_size;
1446 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) ||
1447 ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) &&
1448 (new_size != ifp->if_real_bytes)));
1450 /* Free extent records */
1451 if (new_size == 0) {
1452 xfs_iext_destroy(ifp);
1454 /* Resize direct extent list and zero any new bytes */
1455 else if (ifp->if_real_bytes) {
1456 /* Check if extents will fit inside the inode */
1457 if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) {
1458 xfs_iext_direct_to_inline(ifp, new_size /
1459 (uint)sizeof(xfs_bmbt_rec_t));
1460 ifp->if_bytes = new_size;
1463 if (!is_power_of_2(new_size)){
1464 rnew_size = roundup_pow_of_two(new_size);
1466 if (rnew_size != ifp->if_real_bytes) {
1467 ifp->if_u1.if_extents =
1468 kmem_realloc(ifp->if_u1.if_extents,
1469 rnew_size, KM_NOFS);
1471 if (rnew_size > ifp->if_real_bytes) {
1472 memset(&ifp->if_u1.if_extents[ifp->if_bytes /
1473 (uint)sizeof(xfs_bmbt_rec_t)], 0,
1474 rnew_size - ifp->if_real_bytes);
1477 /* Switch from the inline extent buffer to a direct extent list */
1479 if (!is_power_of_2(new_size)) {
1480 rnew_size = roundup_pow_of_two(new_size);
1482 xfs_iext_inline_to_direct(ifp, rnew_size);
1484 ifp->if_real_bytes = rnew_size;
1485 ifp->if_bytes = new_size;
1489 * Switch from linear (direct) extent records to inline buffer.
1492 xfs_iext_direct_to_inline(
1493 xfs_ifork_t *ifp, /* inode fork pointer */
1494 xfs_extnum_t nextents) /* number of extents in file */
1496 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1497 ASSERT(nextents <= XFS_INLINE_EXTS);
1499 * The inline buffer was zeroed when we switched
1500 * from inline to direct extent allocation mode,
1501 * so we don't need to clear it here.
1503 memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents,
1504 nextents * sizeof(xfs_bmbt_rec_t));
1505 kmem_free(ifp->if_u1.if_extents);
1506 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
1507 ifp->if_real_bytes = 0;
1511 * Switch from inline buffer to linear (direct) extent records.
1512 * new_size should already be rounded up to the next power of 2
1513 * by the caller (when appropriate), so use new_size as it is.
1514 * However, since new_size may be rounded up, we can't update
1515 * if_bytes here. It is the caller's responsibility to update
1516 * if_bytes upon return.
1519 xfs_iext_inline_to_direct(
1520 xfs_ifork_t *ifp, /* inode fork pointer */
1521 int new_size) /* number of extents in file */
1523 ifp->if_u1.if_extents = kmem_alloc(new_size, KM_NOFS);
1524 memset(ifp->if_u1.if_extents, 0, new_size);
1525 if (ifp->if_bytes) {
1526 memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext,
1528 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
1529 sizeof(xfs_bmbt_rec_t));
1531 ifp->if_real_bytes = new_size;
1535 * Resize an extent indirection array to new_size bytes.
1538 xfs_iext_realloc_indirect(
1539 xfs_ifork_t *ifp, /* inode fork pointer */
1540 int new_size) /* new indirection array size */
1542 int nlists; /* number of irec's (ex lists) */
1543 int size; /* current indirection array size */
1545 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1546 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1547 size = nlists * sizeof(xfs_ext_irec_t);
1548 ASSERT(ifp->if_real_bytes);
1549 ASSERT((new_size >= 0) && (new_size != size));
1550 if (new_size == 0) {
1551 xfs_iext_destroy(ifp);
1553 ifp->if_u1.if_ext_irec =
1554 kmem_realloc(ifp->if_u1.if_ext_irec, new_size, KM_NOFS);
1559 * Switch from indirection array to linear (direct) extent allocations.
1562 xfs_iext_indirect_to_direct(
1563 xfs_ifork_t *ifp) /* inode fork pointer */
1565 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1566 xfs_extnum_t nextents; /* number of extents in file */
1567 int size; /* size of file extents */
1569 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1570 nextents = xfs_iext_count(ifp);
1571 ASSERT(nextents <= XFS_LINEAR_EXTS);
1572 size = nextents * sizeof(xfs_bmbt_rec_t);
1574 xfs_iext_irec_compact_pages(ifp);
1575 ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ);
1577 ep = ifp->if_u1.if_ext_irec->er_extbuf;
1578 kmem_free(ifp->if_u1.if_ext_irec);
1579 ifp->if_flags &= ~XFS_IFEXTIREC;
1580 ifp->if_u1.if_extents = ep;
1581 ifp->if_bytes = size;
1582 if (nextents < XFS_LINEAR_EXTS) {
1583 xfs_iext_realloc_direct(ifp, size);
1588 * Remove all records from the indirection array.
1591 xfs_iext_irec_remove_all(
1592 struct xfs_ifork *ifp)
1597 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1598 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1599 for (i = 0; i < nlists; i++)
1600 kmem_free(ifp->if_u1.if_ext_irec[i].er_extbuf);
1601 kmem_free(ifp->if_u1.if_ext_irec);
1602 ifp->if_flags &= ~XFS_IFEXTIREC;
1606 * Free incore file extents.
1610 xfs_ifork_t *ifp) /* inode fork pointer */
1612 if (ifp->if_flags & XFS_IFEXTIREC) {
1613 xfs_iext_irec_remove_all(ifp);
1614 } else if (ifp->if_real_bytes) {
1615 kmem_free(ifp->if_u1.if_extents);
1616 } else if (ifp->if_bytes) {
1617 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
1618 sizeof(xfs_bmbt_rec_t));
1620 ifp->if_u1.if_extents = NULL;
1621 ifp->if_real_bytes = 0;
1626 * Return a pointer to the extent record for file system block bno.
1628 xfs_bmbt_rec_host_t * /* pointer to found extent record */
1629 xfs_iext_bno_to_ext(
1630 xfs_ifork_t *ifp, /* inode fork pointer */
1631 xfs_fileoff_t bno, /* block number to search for */
1632 xfs_extnum_t *idxp) /* index of target extent */
1634 xfs_bmbt_rec_host_t *base; /* pointer to first extent */
1635 xfs_filblks_t blockcount = 0; /* number of blocks in extent */
1636 xfs_bmbt_rec_host_t *ep = NULL; /* pointer to target extent */
1637 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
1638 int high; /* upper boundary in search */
1639 xfs_extnum_t idx = 0; /* index of target extent */
1640 int low; /* lower boundary in search */
1641 xfs_extnum_t nextents; /* number of file extents */
1642 xfs_fileoff_t startoff = 0; /* start offset of extent */
1644 nextents = xfs_iext_count(ifp);
1645 if (nextents == 0) {
1650 if (ifp->if_flags & XFS_IFEXTIREC) {
1651 /* Find target extent list */
1653 erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx);
1654 base = erp->er_extbuf;
1655 high = erp->er_extcount - 1;
1657 base = ifp->if_u1.if_extents;
1658 high = nextents - 1;
1660 /* Binary search extent records */
1661 while (low <= high) {
1662 idx = (low + high) >> 1;
1664 startoff = xfs_bmbt_get_startoff(ep);
1665 blockcount = xfs_bmbt_get_blockcount(ep);
1666 if (bno < startoff) {
1668 } else if (bno >= startoff + blockcount) {
1671 /* Convert back to file-based extent index */
1672 if (ifp->if_flags & XFS_IFEXTIREC) {
1673 idx += erp->er_extoff;
1679 /* Convert back to file-based extent index */
1680 if (ifp->if_flags & XFS_IFEXTIREC) {
1681 idx += erp->er_extoff;
1683 if (bno >= startoff + blockcount) {
1684 if (++idx == nextents) {
1687 ep = xfs_iext_get_ext(ifp, idx);
1695 * Return a pointer to the indirection array entry containing the
1696 * extent record for filesystem block bno. Store the index of the
1697 * target irec in *erp_idxp.
1699 xfs_ext_irec_t * /* pointer to found extent record */
1700 xfs_iext_bno_to_irec(
1701 xfs_ifork_t *ifp, /* inode fork pointer */
1702 xfs_fileoff_t bno, /* block number to search for */
1703 int *erp_idxp) /* irec index of target ext list */
1705 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
1706 xfs_ext_irec_t *erp_next; /* next indirection array entry */
1707 int erp_idx; /* indirection array index */
1708 int nlists; /* number of extent irec's (lists) */
1709 int high; /* binary search upper limit */
1710 int low; /* binary search lower limit */
1712 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1713 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1717 while (low <= high) {
1718 erp_idx = (low + high) >> 1;
1719 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1720 erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL;
1721 if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) {
1723 } else if (erp_next && bno >=
1724 xfs_bmbt_get_startoff(erp_next->er_extbuf)) {
1730 *erp_idxp = erp_idx;
1735 * Return a pointer to the indirection array entry containing the
1736 * extent record at file extent index *idxp. Store the index of the
1737 * target irec in *erp_idxp and store the page index of the target
1738 * extent record in *idxp.
1741 xfs_iext_idx_to_irec(
1742 xfs_ifork_t *ifp, /* inode fork pointer */
1743 xfs_extnum_t *idxp, /* extent index (file -> page) */
1744 int *erp_idxp, /* pointer to target irec */
1745 int realloc) /* new bytes were just added */
1747 xfs_ext_irec_t *prev; /* pointer to previous irec */
1748 xfs_ext_irec_t *erp = NULL; /* pointer to current irec */
1749 int erp_idx; /* indirection array index */
1750 int nlists; /* number of irec's (ex lists) */
1751 int high; /* binary search upper limit */
1752 int low; /* binary search lower limit */
1753 xfs_extnum_t page_idx = *idxp; /* extent index in target list */
1755 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1756 ASSERT(page_idx >= 0);
1757 ASSERT(page_idx <= xfs_iext_count(ifp));
1758 ASSERT(page_idx < xfs_iext_count(ifp) || realloc);
1760 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1765 /* Binary search extent irec's */
1766 while (low <= high) {
1767 erp_idx = (low + high) >> 1;
1768 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1769 prev = erp_idx > 0 ? erp - 1 : NULL;
1770 if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff &&
1771 realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) {
1773 } else if (page_idx > erp->er_extoff + erp->er_extcount ||
1774 (page_idx == erp->er_extoff + erp->er_extcount &&
1777 } else if (page_idx == erp->er_extoff + erp->er_extcount &&
1778 erp->er_extcount == XFS_LINEAR_EXTS) {
1782 erp = erp_idx < nlists ? erp + 1 : NULL;
1785 page_idx -= erp->er_extoff;
1790 *erp_idxp = erp_idx;
1795 * Allocate and initialize an indirection array once the space needed
1796 * for incore extents increases above XFS_IEXT_BUFSZ.
1800 xfs_ifork_t *ifp) /* inode fork pointer */
1802 xfs_ext_irec_t *erp; /* indirection array pointer */
1803 xfs_extnum_t nextents; /* number of extents in file */
1805 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
1806 nextents = xfs_iext_count(ifp);
1807 ASSERT(nextents <= XFS_LINEAR_EXTS);
1809 erp = kmem_alloc(sizeof(xfs_ext_irec_t), KM_NOFS);
1811 if (nextents == 0) {
1812 ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
1813 } else if (!ifp->if_real_bytes) {
1814 xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ);
1815 } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) {
1816 xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ);
1818 erp->er_extbuf = ifp->if_u1.if_extents;
1819 erp->er_extcount = nextents;
1822 ifp->if_flags |= XFS_IFEXTIREC;
1823 ifp->if_real_bytes = XFS_IEXT_BUFSZ;
1824 ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t);
1825 ifp->if_u1.if_ext_irec = erp;
1831 * Allocate and initialize a new entry in the indirection array.
1835 xfs_ifork_t *ifp, /* inode fork pointer */
1836 int erp_idx) /* index for new irec */
1838 xfs_ext_irec_t *erp; /* indirection array pointer */
1839 int i; /* loop counter */
1840 int nlists; /* number of irec's (ex lists) */
1842 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1843 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1845 /* Resize indirection array */
1846 xfs_iext_realloc_indirect(ifp, ++nlists *
1847 sizeof(xfs_ext_irec_t));
1849 * Move records down in the array so the
1850 * new page can use erp_idx.
1852 erp = ifp->if_u1.if_ext_irec;
1853 for (i = nlists - 1; i > erp_idx; i--) {
1854 memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t));
1856 ASSERT(i == erp_idx);
1858 /* Initialize new extent record */
1859 erp = ifp->if_u1.if_ext_irec;
1860 erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
1861 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
1862 memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ);
1863 erp[erp_idx].er_extcount = 0;
1864 erp[erp_idx].er_extoff = erp_idx > 0 ?
1865 erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0;
1866 return (&erp[erp_idx]);
1870 * Remove a record from the indirection array.
1873 xfs_iext_irec_remove(
1874 xfs_ifork_t *ifp, /* inode fork pointer */
1875 int erp_idx) /* irec index to remove */
1877 xfs_ext_irec_t *erp; /* indirection array pointer */
1878 int i; /* loop counter */
1879 int nlists; /* number of irec's (ex lists) */
1881 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1882 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1883 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1884 if (erp->er_extbuf) {
1885 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1,
1887 kmem_free(erp->er_extbuf);
1889 /* Compact extent records */
1890 erp = ifp->if_u1.if_ext_irec;
1891 for (i = erp_idx; i < nlists - 1; i++) {
1892 memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t));
1895 * Manually free the last extent record from the indirection
1896 * array. A call to xfs_iext_realloc_indirect() with a size
1897 * of zero would result in a call to xfs_iext_destroy() which
1898 * would in turn call this function again, creating a nasty
1902 xfs_iext_realloc_indirect(ifp,
1903 nlists * sizeof(xfs_ext_irec_t));
1905 kmem_free(ifp->if_u1.if_ext_irec);
1907 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
1911 * This is called to clean up large amounts of unused memory allocated
1912 * by the indirection array. Before compacting anything though, verify
1913 * that the indirection array is still needed and switch back to the
1914 * linear extent list (or even the inline buffer) if possible. The
1915 * compaction policy is as follows:
1917 * Full Compaction: Extents fit into a single page (or inline buffer)
1918 * Partial Compaction: Extents occupy less than 50% of allocated space
1919 * No Compaction: Extents occupy at least 50% of allocated space
1922 xfs_iext_irec_compact(
1923 xfs_ifork_t *ifp) /* inode fork pointer */
1925 xfs_extnum_t nextents; /* number of extents in file */
1926 int nlists; /* number of irec's (ex lists) */
1928 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1929 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1930 nextents = xfs_iext_count(ifp);
1932 if (nextents == 0) {
1933 xfs_iext_destroy(ifp);
1934 } else if (nextents <= XFS_INLINE_EXTS) {
1935 xfs_iext_indirect_to_direct(ifp);
1936 xfs_iext_direct_to_inline(ifp, nextents);
1937 } else if (nextents <= XFS_LINEAR_EXTS) {
1938 xfs_iext_indirect_to_direct(ifp);
1939 } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) {
1940 xfs_iext_irec_compact_pages(ifp);
1945 * Combine extents from neighboring extent pages.
1948 xfs_iext_irec_compact_pages(
1949 xfs_ifork_t *ifp) /* inode fork pointer */
1951 xfs_ext_irec_t *erp, *erp_next;/* pointers to irec entries */
1952 int erp_idx = 0; /* indirection array index */
1953 int nlists; /* number of irec's (ex lists) */
1955 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1956 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1957 while (erp_idx < nlists - 1) {
1958 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1960 if (erp_next->er_extcount <=
1961 (XFS_LINEAR_EXTS - erp->er_extcount)) {
1962 memcpy(&erp->er_extbuf[erp->er_extcount],
1963 erp_next->er_extbuf, erp_next->er_extcount *
1964 sizeof(xfs_bmbt_rec_t));
1965 erp->er_extcount += erp_next->er_extcount;
1967 * Free page before removing extent record
1968 * so er_extoffs don't get modified in
1969 * xfs_iext_irec_remove.
1971 kmem_free(erp_next->er_extbuf);
1972 erp_next->er_extbuf = NULL;
1973 xfs_iext_irec_remove(ifp, erp_idx + 1);
1974 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1982 * This is called to update the er_extoff field in the indirection
1983 * array when extents have been added or removed from one of the
1984 * extent lists. erp_idx contains the irec index to begin updating
1985 * at and ext_diff contains the number of extents that were added
1989 xfs_iext_irec_update_extoffs(
1990 xfs_ifork_t *ifp, /* inode fork pointer */
1991 int erp_idx, /* irec index to update */
1992 int ext_diff) /* number of new extents */
1994 int i; /* loop counter */
1995 int nlists; /* number of irec's (ex lists */
1997 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1998 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1999 for (i = erp_idx; i < nlists; i++) {
2000 ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff;
2005 * Initialize an inode's copy-on-write fork.
2009 struct xfs_inode *ip)
2014 ip->i_cowfp = kmem_zone_zalloc(xfs_ifork_zone,
2015 KM_SLEEP | KM_NOFS);
2016 ip->i_cowfp->if_flags = XFS_IFEXTENTS;
2017 ip->i_cformat = XFS_DINODE_FMT_EXTENTS;
2018 ip->i_cnextents = 0;
2022 * Lookup the extent covering bno.
2024 * If there is an extent covering bno return the extent index, and store the
2025 * expanded extent structure in *gotp, and the extent index in *idx.
2026 * If there is no extent covering bno, but there is an extent after it (e.g.
2027 * it lies in a hole) return that extent in *gotp and its index in *idx
2029 * If bno is beyond the last extent return false, and return the index after
2030 * the last valid index in *idxp.
2033 xfs_iext_lookup_extent(
2034 struct xfs_inode *ip,
2035 struct xfs_ifork *ifp,
2038 struct xfs_bmbt_irec *gotp)
2040 struct xfs_bmbt_rec_host *ep;
2042 XFS_STATS_INC(ip->i_mount, xs_look_exlist);
2044 ep = xfs_iext_bno_to_ext(ifp, bno, idxp);
2047 xfs_bmbt_get_all(ep, gotp);
2052 * Return true if there is an extent at index idx, and return the expanded
2053 * extent structure at idx in that case. Else return false.
2056 xfs_iext_get_extent(
2057 struct xfs_ifork *ifp,
2059 struct xfs_bmbt_irec *gotp)
2061 if (idx < 0 || idx >= xfs_iext_count(ifp))
2063 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), gotp);