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);
46 * Move inode type and inode format specific information from the
47 * on-disk inode to the in-core inode. For fifos, devs, and sockets
48 * this means set if_rdev to the proper value. For files, directories,
49 * and symlinks this means to bring in the in-line data or extent
50 * pointers. For a file in B-tree format, only the root is immediately
51 * brought in-core. The rest will be in-lined in if_extents when it
52 * is first referenced (see xfs_iread_extents()).
59 xfs_attr_shortform_t *atp;
64 if (unlikely(be32_to_cpu(dip->di_nextents) +
65 be16_to_cpu(dip->di_anextents) >
66 be64_to_cpu(dip->di_nblocks))) {
68 "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.",
69 (unsigned long long)ip->i_ino,
70 (int)(be32_to_cpu(dip->di_nextents) +
71 be16_to_cpu(dip->di_anextents)),
73 be64_to_cpu(dip->di_nblocks));
74 XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW,
79 if (unlikely(dip->di_forkoff > ip->i_mount->m_sb.sb_inodesize)) {
80 xfs_warn(ip->i_mount, "corrupt dinode %Lu, forkoff = 0x%x.",
81 (unsigned long long)ip->i_ino,
83 XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW,
88 if (unlikely((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) &&
89 !ip->i_mount->m_rtdev_targp)) {
91 "corrupt dinode %Lu, has realtime flag set.",
93 XFS_CORRUPTION_ERROR("xfs_iformat(realtime)",
94 XFS_ERRLEVEL_LOW, ip->i_mount, dip);
98 if (unlikely(xfs_is_reflink_inode(ip) &&
99 (VFS_I(ip)->i_mode & S_IFMT) != S_IFREG)) {
100 xfs_warn(ip->i_mount,
101 "corrupt dinode %llu, wrong file type for reflink.",
103 XFS_CORRUPTION_ERROR("xfs_iformat(reflink)",
104 XFS_ERRLEVEL_LOW, ip->i_mount, dip);
105 return -EFSCORRUPTED;
108 if (unlikely(xfs_is_reflink_inode(ip) &&
109 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME))) {
110 xfs_warn(ip->i_mount,
111 "corrupt dinode %llu, has reflink+realtime flag set.",
113 XFS_CORRUPTION_ERROR("xfs_iformat(reflink)",
114 XFS_ERRLEVEL_LOW, ip->i_mount, dip);
115 return -EFSCORRUPTED;
118 switch (VFS_I(ip)->i_mode & S_IFMT) {
123 if (unlikely(dip->di_format != XFS_DINODE_FMT_DEV)) {
124 XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW,
126 return -EFSCORRUPTED;
129 ip->i_df.if_u2.if_rdev = xfs_dinode_get_rdev(dip);
135 switch (dip->di_format) {
136 case XFS_DINODE_FMT_LOCAL:
138 * no local regular files yet
140 if (unlikely(S_ISREG(be16_to_cpu(dip->di_mode)))) {
141 xfs_warn(ip->i_mount,
142 "corrupt inode %Lu (local format for regular file).",
143 (unsigned long long) ip->i_ino);
144 XFS_CORRUPTION_ERROR("xfs_iformat(4)",
147 return -EFSCORRUPTED;
150 di_size = be64_to_cpu(dip->di_size);
151 if (unlikely(di_size < 0 ||
152 di_size > XFS_DFORK_DSIZE(dip, ip->i_mount))) {
153 xfs_warn(ip->i_mount,
154 "corrupt inode %Lu (bad size %Ld for local inode).",
155 (unsigned long long) ip->i_ino,
156 (long long) di_size);
157 XFS_CORRUPTION_ERROR("xfs_iformat(5)",
160 return -EFSCORRUPTED;
164 error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size);
166 case XFS_DINODE_FMT_EXTENTS:
167 error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
169 case XFS_DINODE_FMT_BTREE:
170 error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
173 XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW,
175 return -EFSCORRUPTED;
180 XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW, ip->i_mount);
181 return -EFSCORRUPTED;
186 /* Check inline dir contents. */
187 if (S_ISDIR(VFS_I(ip)->i_mode) &&
188 dip->di_format == XFS_DINODE_FMT_LOCAL) {
189 error = xfs_dir2_sf_verify(ip);
191 xfs_idestroy_fork(ip, XFS_DATA_FORK);
196 if (xfs_is_reflink_inode(ip)) {
197 ASSERT(ip->i_cowfp == NULL);
198 xfs_ifork_init_cow(ip);
201 if (!XFS_DFORK_Q(dip))
204 ASSERT(ip->i_afp == NULL);
205 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP | KM_NOFS);
207 switch (dip->di_aformat) {
208 case XFS_DINODE_FMT_LOCAL:
209 atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
210 size = be16_to_cpu(atp->hdr.totsize);
212 if (unlikely(size < sizeof(struct xfs_attr_sf_hdr))) {
213 xfs_warn(ip->i_mount,
214 "corrupt inode %Lu (bad attr fork size %Ld).",
215 (unsigned long long) ip->i_ino,
217 XFS_CORRUPTION_ERROR("xfs_iformat(8)",
220 error = -EFSCORRUPTED;
224 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size);
226 case XFS_DINODE_FMT_EXTENTS:
227 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
229 case XFS_DINODE_FMT_BTREE:
230 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
233 error = -EFSCORRUPTED;
237 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
240 kmem_zone_free(xfs_ifork_zone, ip->i_cowfp);
242 xfs_idestroy_fork(ip, XFS_DATA_FORK);
249 struct xfs_inode *ip,
254 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
255 int mem_size = size, real_size = 0;
259 * If we are using the local fork to store a symlink body we need to
260 * zero-terminate it so that we can pass it back to the VFS directly.
261 * Overallocate the in-memory fork by one for that and add a zero
262 * to terminate it below.
264 zero_terminate = S_ISLNK(VFS_I(ip)->i_mode);
269 ifp->if_u1.if_data = NULL;
270 else if (mem_size <= sizeof(ifp->if_u2.if_inline_data))
271 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
273 real_size = roundup(mem_size, 4);
274 ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP | KM_NOFS);
278 memcpy(ifp->if_u1.if_data, data, size);
280 ifp->if_u1.if_data[size] = '\0';
283 ifp->if_bytes = size;
284 ifp->if_real_bytes = real_size;
285 ifp->if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
286 ifp->if_flags |= XFS_IFINLINE;
290 * The file is in-lined in the on-disk inode.
291 * If it fits into if_inline_data, then copy
292 * it there, otherwise allocate a buffer for it
293 * and copy the data there. Either way, set
294 * if_data to point at the data.
295 * If we allocate a buffer for the data, make
296 * sure that its size is a multiple of 4 and
297 * record the real size in i_real_bytes.
307 * If the size is unreasonable, then something
308 * is wrong and we just bail out rather than crash in
309 * kmem_alloc() or memcpy() below.
311 if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
312 xfs_warn(ip->i_mount,
313 "corrupt inode %Lu (bad size %d for local fork, size = %d).",
314 (unsigned long long) ip->i_ino, size,
315 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
316 XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW,
318 return -EFSCORRUPTED;
321 xfs_init_local_fork(ip, whichfork, XFS_DFORK_PTR(dip, whichfork), size);
326 * The file consists of a set of extents all of which fit into the on-disk
327 * inode. If there are few enough extents to fit into the if_inline_ext, then
328 * copy them there. Otherwise allocate a buffer for them and copy them into it.
329 * Either way, set if_extents to point at the extents.
333 struct xfs_inode *ip,
334 struct xfs_dinode *dip,
337 struct xfs_mount *mp = ip->i_mount;
338 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
339 int nex = XFS_DFORK_NEXTENTS(dip, whichfork);
340 int size = nex * sizeof(xfs_bmbt_rec_t);
341 struct xfs_bmbt_rec *dp;
345 * If the number of extents is unreasonable, then something is wrong and
346 * we just bail out rather than crash in kmem_alloc() or memcpy() below.
348 if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, mp, whichfork))) {
349 xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).",
350 (unsigned long long) ip->i_ino, nex);
351 XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW,
353 return -EFSCORRUPTED;
356 ifp->if_real_bytes = 0;
358 ifp->if_u1.if_extents = NULL;
359 else if (nex <= XFS_INLINE_EXTS)
360 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
362 xfs_iext_add(ifp, 0, nex);
364 ifp->if_bytes = size;
366 dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
367 for (i = 0; i < nex; i++, dp++) {
368 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
369 ep->l0 = get_unaligned_be64(&dp->l0);
370 ep->l1 = get_unaligned_be64(&dp->l1);
371 if (!xfs_bmbt_validate_extent(mp, whichfork, ep)) {
372 XFS_ERROR_REPORT("xfs_iformat_extents(2)",
373 XFS_ERRLEVEL_LOW, mp);
374 return -EFSCORRUPTED;
377 XFS_BMAP_TRACE_EXLIST(ip, nex, whichfork);
379 ifp->if_flags |= XFS_IFEXTENTS;
384 * The file has too many extents to fit into
385 * the inode, so they are in B-tree format.
386 * Allocate a buffer for the root of the B-tree
387 * and copy the root into it. The i_extents
388 * field will remain NULL until all of the
389 * extents are read in (when they are needed).
397 struct xfs_mount *mp = ip->i_mount;
398 xfs_bmdr_block_t *dfp;
405 ifp = XFS_IFORK_PTR(ip, whichfork);
406 dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
407 size = XFS_BMAP_BROOT_SPACE(mp, dfp);
408 nrecs = be16_to_cpu(dfp->bb_numrecs);
409 level = be16_to_cpu(dfp->bb_level);
412 * blow out if -- fork has less extents than can fit in
413 * fork (fork shouldn't be a btree format), root btree
414 * block has more records than can fit into the fork,
415 * or the number of extents is greater than the number of
418 if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <=
419 XFS_IFORK_MAXEXT(ip, whichfork) ||
420 XFS_BMDR_SPACE_CALC(nrecs) >
421 XFS_DFORK_SIZE(dip, mp, whichfork) ||
422 XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks) ||
423 level == 0 || level > XFS_BTREE_MAXLEVELS) {
424 xfs_warn(mp, "corrupt inode %Lu (btree).",
425 (unsigned long long) ip->i_ino);
426 XFS_CORRUPTION_ERROR("xfs_iformat_btree", XFS_ERRLEVEL_LOW,
428 return -EFSCORRUPTED;
431 ifp->if_broot_bytes = size;
432 ifp->if_broot = kmem_alloc(size, KM_SLEEP | KM_NOFS);
433 ASSERT(ifp->if_broot != NULL);
435 * Copy and convert from the on-disk structure
436 * to the in-memory structure.
438 xfs_bmdr_to_bmbt(ip, dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
439 ifp->if_broot, size);
440 ifp->if_flags &= ~XFS_IFEXTENTS;
441 ifp->if_flags |= XFS_IFBROOT;
447 * Read in extents from a btree-format inode.
448 * Allocate and fill in if_extents. Real work is done in xfs_bmap.c.
458 xfs_extnum_t nextents;
460 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
462 if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
463 XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW,
465 return -EFSCORRUPTED;
467 nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
468 ifp = XFS_IFORK_PTR(ip, whichfork);
471 * We know that the size is valid (it's checked in iformat_btree)
473 ifp->if_bytes = ifp->if_real_bytes = 0;
474 xfs_iext_add(ifp, 0, nextents);
475 error = xfs_bmap_read_extents(tp, ip, whichfork);
477 xfs_iext_destroy(ifp);
480 ifp->if_flags |= XFS_IFEXTENTS;
484 * Reallocate the space for if_broot based on the number of records
485 * being added or deleted as indicated in rec_diff. Move the records
486 * and pointers in if_broot to fit the new size. When shrinking this
487 * will eliminate holes between the records and pointers created by
488 * the caller. When growing this will create holes to be filled in
491 * The caller must not request to add more records than would fit in
492 * the on-disk inode root. If the if_broot is currently NULL, then
493 * if we are adding records, one will be allocated. The caller must also
494 * not request that the number of records go below zero, although
497 * ip -- the inode whose if_broot area is changing
498 * ext_diff -- the change in the number of records, positive or negative,
499 * requested for the if_broot array.
507 struct xfs_mount *mp = ip->i_mount;
510 struct xfs_btree_block *new_broot;
517 * Handle the degenerate case quietly.
523 ifp = XFS_IFORK_PTR(ip, whichfork);
526 * If there wasn't any memory allocated before, just
527 * allocate it now and get out.
529 if (ifp->if_broot_bytes == 0) {
530 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
531 ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
532 ifp->if_broot_bytes = (int)new_size;
537 * If there is already an existing if_broot, then we need
538 * to realloc() it and shift the pointers to their new
539 * location. The records don't change location because
540 * they are kept butted up against the btree block header.
542 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
543 new_max = cur_max + rec_diff;
544 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
545 ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
547 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
548 ifp->if_broot_bytes);
549 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
551 ifp->if_broot_bytes = (int)new_size;
552 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
553 XFS_IFORK_SIZE(ip, whichfork));
554 memmove(np, op, cur_max * (uint)sizeof(xfs_fsblock_t));
559 * rec_diff is less than 0. In this case, we are shrinking the
560 * if_broot buffer. It must already exist. If we go to zero
561 * records, just get rid of the root and clear the status bit.
563 ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
564 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
565 new_max = cur_max + rec_diff;
566 ASSERT(new_max >= 0);
568 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
572 new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
574 * First copy over the btree block header.
576 memcpy(new_broot, ifp->if_broot,
577 XFS_BMBT_BLOCK_LEN(ip->i_mount));
580 ifp->if_flags &= ~XFS_IFBROOT;
584 * Only copy the records and pointers if there are any.
588 * First copy the records.
590 op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
591 np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
592 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
595 * Then copy the pointers.
597 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
598 ifp->if_broot_bytes);
599 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
601 memcpy(np, op, new_max * (uint)sizeof(xfs_fsblock_t));
603 kmem_free(ifp->if_broot);
604 ifp->if_broot = new_broot;
605 ifp->if_broot_bytes = (int)new_size;
607 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
608 XFS_IFORK_SIZE(ip, whichfork));
614 * This is called when the amount of space needed for if_data
615 * is increased or decreased. The change in size is indicated by
616 * the number of bytes that need to be added or deleted in the
617 * byte_diff parameter.
619 * If the amount of space needed has decreased below the size of the
620 * inline buffer, then switch to using the inline buffer. Otherwise,
621 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
624 * ip -- the inode whose if_data area is changing
625 * byte_diff -- the change in the number of bytes, positive or negative,
626 * requested for the if_data array.
638 if (byte_diff == 0) {
642 ifp = XFS_IFORK_PTR(ip, whichfork);
643 new_size = (int)ifp->if_bytes + byte_diff;
644 ASSERT(new_size >= 0);
647 if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
648 kmem_free(ifp->if_u1.if_data);
650 ifp->if_u1.if_data = NULL;
652 } else if (new_size <= sizeof(ifp->if_u2.if_inline_data)) {
654 * If the valid extents/data can fit in if_inline_ext/data,
655 * copy them from the malloc'd vector and free it.
657 if (ifp->if_u1.if_data == NULL) {
658 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
659 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
660 ASSERT(ifp->if_real_bytes != 0);
661 memcpy(ifp->if_u2.if_inline_data, ifp->if_u1.if_data,
663 kmem_free(ifp->if_u1.if_data);
664 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
669 * Stuck with malloc/realloc.
670 * For inline data, the underlying buffer must be
671 * a multiple of 4 bytes in size so that it can be
672 * logged and stay on word boundaries. We enforce
675 real_size = roundup(new_size, 4);
676 if (ifp->if_u1.if_data == NULL) {
677 ASSERT(ifp->if_real_bytes == 0);
678 ifp->if_u1.if_data = kmem_alloc(real_size,
680 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
682 * Only do the realloc if the underlying size
683 * is really changing.
685 if (ifp->if_real_bytes != real_size) {
687 kmem_realloc(ifp->if_u1.if_data,
692 ASSERT(ifp->if_real_bytes == 0);
693 ifp->if_u1.if_data = kmem_alloc(real_size,
695 memcpy(ifp->if_u1.if_data, ifp->if_u2.if_inline_data,
699 ifp->if_real_bytes = real_size;
700 ifp->if_bytes = new_size;
701 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
711 ifp = XFS_IFORK_PTR(ip, whichfork);
712 if (ifp->if_broot != NULL) {
713 kmem_free(ifp->if_broot);
714 ifp->if_broot = NULL;
718 * If the format is local, then we can't have an extents
719 * array so just look for an inline data array. If we're
720 * not local then we may or may not have an extents list,
721 * so check and free it up if we do.
723 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
724 if ((ifp->if_u1.if_data != ifp->if_u2.if_inline_data) &&
725 (ifp->if_u1.if_data != NULL)) {
726 ASSERT(ifp->if_real_bytes != 0);
727 kmem_free(ifp->if_u1.if_data);
728 ifp->if_u1.if_data = NULL;
729 ifp->if_real_bytes = 0;
731 } else if ((ifp->if_flags & XFS_IFEXTENTS) &&
732 ((ifp->if_flags & XFS_IFEXTIREC) ||
733 ((ifp->if_u1.if_extents != NULL) &&
734 (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext)))) {
735 ASSERT(ifp->if_real_bytes != 0);
736 xfs_iext_destroy(ifp);
738 ASSERT(ifp->if_u1.if_extents == NULL ||
739 ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext);
740 ASSERT(ifp->if_real_bytes == 0);
741 if (whichfork == XFS_ATTR_FORK) {
742 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
744 } else if (whichfork == XFS_COW_FORK) {
745 kmem_zone_free(xfs_ifork_zone, ip->i_cowfp);
750 /* Count number of incore extents based on if_bytes */
752 xfs_iext_count(struct xfs_ifork *ifp)
754 return ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
758 * Convert in-core extents to on-disk form
760 * For either the data or attr fork in extent format, we need to endian convert
761 * the in-core extent as we place them into the on-disk inode.
763 * In the case of the data fork, the in-core and on-disk fork sizes can be
764 * different due to delayed allocation extents. We only copy on-disk extents
765 * here, so callers must always use the physical fork size to determine the
766 * size of the buffer passed to this routine. We will return the size actually
779 xfs_fsblock_t start_block;
781 ifp = XFS_IFORK_PTR(ip, whichfork);
782 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
783 ASSERT(ifp->if_bytes > 0);
785 nrecs = xfs_iext_count(ifp);
786 XFS_BMAP_TRACE_EXLIST(ip, nrecs, whichfork);
790 * There are some delayed allocation extents in the
791 * inode, so copy the extents one at a time and skip
792 * the delayed ones. There must be at least one
793 * non-delayed extent.
796 for (i = 0; i < nrecs; i++) {
797 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
799 ASSERT(xfs_bmbt_validate_extent(ip->i_mount, whichfork, ep));
801 start_block = xfs_bmbt_get_startblock(ep);
802 if (isnullstartblock(start_block)) {
804 * It's a delayed allocation extent, so skip it.
809 /* Translate to on disk format */
810 put_unaligned_be64(ep->l0, &dp->l0);
811 put_unaligned_be64(ep->l1, &dp->l1);
817 return (copied * (uint)sizeof(xfs_bmbt_rec_t));
821 * Each of the following cases stores data into the same region
822 * of the on-disk inode, so only one of them can be valid at
823 * any given time. While it is possible to have conflicting formats
824 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
825 * in EXTENTS format, this can only happen when the fork has
826 * changed formats after being modified but before being flushed.
827 * In these cases, the format always takes precedence, because the
828 * format indicates the current state of the fork.
834 xfs_inode_log_item_t *iip,
840 static const short brootflag[2] =
841 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
842 static const short dataflag[2] =
843 { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
844 static const short extflag[2] =
845 { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
849 ifp = XFS_IFORK_PTR(ip, whichfork);
851 * This can happen if we gave up in iformat in an error path,
852 * for the attribute fork.
855 ASSERT(whichfork == XFS_ATTR_FORK);
858 cp = XFS_DFORK_PTR(dip, whichfork);
860 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
861 case XFS_DINODE_FMT_LOCAL:
862 if ((iip->ili_fields & dataflag[whichfork]) &&
863 (ifp->if_bytes > 0)) {
864 ASSERT(ifp->if_u1.if_data != NULL);
865 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
866 memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
870 case XFS_DINODE_FMT_EXTENTS:
871 ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
872 !(iip->ili_fields & extflag[whichfork]));
873 if ((iip->ili_fields & extflag[whichfork]) &&
874 (ifp->if_bytes > 0)) {
875 ASSERT(xfs_iext_get_ext(ifp, 0));
876 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
877 (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
882 case XFS_DINODE_FMT_BTREE:
883 if ((iip->ili_fields & brootflag[whichfork]) &&
884 (ifp->if_broot_bytes > 0)) {
885 ASSERT(ifp->if_broot != NULL);
886 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
887 XFS_IFORK_SIZE(ip, whichfork));
888 xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
889 (xfs_bmdr_block_t *)cp,
890 XFS_DFORK_SIZE(dip, mp, whichfork));
894 case XFS_DINODE_FMT_DEV:
895 if (iip->ili_fields & XFS_ILOG_DEV) {
896 ASSERT(whichfork == XFS_DATA_FORK);
897 xfs_dinode_put_rdev(dip, ip->i_df.if_u2.if_rdev);
901 case XFS_DINODE_FMT_UUID:
902 if (iip->ili_fields & XFS_ILOG_UUID) {
903 ASSERT(whichfork == XFS_DATA_FORK);
904 memcpy(XFS_DFORK_DPTR(dip),
905 &ip->i_df.if_u2.if_uuid,
917 * Return a pointer to the extent record at file index idx.
919 xfs_bmbt_rec_host_t *
921 xfs_ifork_t *ifp, /* inode fork pointer */
922 xfs_extnum_t idx) /* index of target extent */
925 ASSERT(idx < xfs_iext_count(ifp));
927 if ((ifp->if_flags & XFS_IFEXTIREC) && (idx == 0)) {
928 return ifp->if_u1.if_ext_irec->er_extbuf;
929 } else if (ifp->if_flags & XFS_IFEXTIREC) {
930 xfs_ext_irec_t *erp; /* irec pointer */
931 int erp_idx = 0; /* irec index */
932 xfs_extnum_t page_idx = idx; /* ext index in target list */
934 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
935 return &erp->er_extbuf[page_idx];
936 } else if (ifp->if_bytes) {
937 return &ifp->if_u1.if_extents[idx];
943 /* Convert bmap state flags to an inode fork. */
945 xfs_iext_state_to_fork(
946 struct xfs_inode *ip,
949 if (state & BMAP_COWFORK)
951 else if (state & BMAP_ATTRFORK)
957 * Insert new item(s) into the extent records for incore inode
958 * fork 'ifp'. 'count' new items are inserted at index 'idx'.
962 xfs_inode_t *ip, /* incore inode pointer */
963 xfs_extnum_t idx, /* starting index of new items */
964 xfs_extnum_t count, /* number of inserted items */
965 xfs_bmbt_irec_t *new, /* items to insert */
966 int state) /* type of extent conversion */
968 xfs_ifork_t *ifp = xfs_iext_state_to_fork(ip, state);
969 xfs_extnum_t i; /* extent record index */
971 trace_xfs_iext_insert(ip, idx, new, state, _RET_IP_);
973 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
974 xfs_iext_add(ifp, idx, count);
975 for (i = idx; i < idx + count; i++, new++)
976 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, i), new);
980 * This is called when the amount of space required for incore file
981 * extents needs to be increased. The ext_diff parameter stores the
982 * number of new extents being added and the idx parameter contains
983 * the extent index where the new extents will be added. If the new
984 * extents are being appended, then we just need to (re)allocate and
985 * initialize the space. Otherwise, if the new extents are being
986 * inserted into the middle of the existing entries, a bit more work
987 * is required to make room for the new extents to be inserted. The
988 * caller is responsible for filling in the new extent entries upon
993 xfs_ifork_t *ifp, /* inode fork pointer */
994 xfs_extnum_t idx, /* index to begin adding exts */
995 int ext_diff) /* number of extents to add */
997 int byte_diff; /* new bytes being added */
998 int new_size; /* size of extents after adding */
999 xfs_extnum_t nextents; /* number of extents in file */
1001 nextents = xfs_iext_count(ifp);
1002 ASSERT((idx >= 0) && (idx <= nextents));
1003 byte_diff = ext_diff * sizeof(xfs_bmbt_rec_t);
1004 new_size = ifp->if_bytes + byte_diff;
1006 * If the new number of extents (nextents + ext_diff)
1007 * fits inside the inode, then continue to use the inline
1010 if (nextents + ext_diff <= XFS_INLINE_EXTS) {
1011 if (idx < nextents) {
1012 memmove(&ifp->if_u2.if_inline_ext[idx + ext_diff],
1013 &ifp->if_u2.if_inline_ext[idx],
1014 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
1015 memset(&ifp->if_u2.if_inline_ext[idx], 0, byte_diff);
1017 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
1018 ifp->if_real_bytes = 0;
1021 * Otherwise use a linear (direct) extent list.
1022 * If the extents are currently inside the inode,
1023 * xfs_iext_realloc_direct will switch us from
1024 * inline to direct extent allocation mode.
1026 else if (nextents + ext_diff <= XFS_LINEAR_EXTS) {
1027 xfs_iext_realloc_direct(ifp, new_size);
1028 if (idx < nextents) {
1029 memmove(&ifp->if_u1.if_extents[idx + ext_diff],
1030 &ifp->if_u1.if_extents[idx],
1031 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
1032 memset(&ifp->if_u1.if_extents[idx], 0, byte_diff);
1035 /* Indirection array */
1037 xfs_ext_irec_t *erp;
1041 ASSERT(nextents + ext_diff > XFS_LINEAR_EXTS);
1042 if (ifp->if_flags & XFS_IFEXTIREC) {
1043 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 1);
1045 xfs_iext_irec_init(ifp);
1046 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1047 erp = ifp->if_u1.if_ext_irec;
1049 /* Extents fit in target extent page */
1050 if (erp && erp->er_extcount + ext_diff <= XFS_LINEAR_EXTS) {
1051 if (page_idx < erp->er_extcount) {
1052 memmove(&erp->er_extbuf[page_idx + ext_diff],
1053 &erp->er_extbuf[page_idx],
1054 (erp->er_extcount - page_idx) *
1055 sizeof(xfs_bmbt_rec_t));
1056 memset(&erp->er_extbuf[page_idx], 0, byte_diff);
1058 erp->er_extcount += ext_diff;
1059 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
1061 /* Insert a new extent page */
1063 xfs_iext_add_indirect_multi(ifp,
1064 erp_idx, page_idx, ext_diff);
1067 * If extent(s) are being appended to the last page in
1068 * the indirection array and the new extent(s) don't fit
1069 * in the page, then erp is NULL and erp_idx is set to
1070 * the next index needed in the indirection array.
1073 uint count = ext_diff;
1076 erp = xfs_iext_irec_new(ifp, erp_idx);
1077 erp->er_extcount = min(count, XFS_LINEAR_EXTS);
1078 count -= erp->er_extcount;
1084 ifp->if_bytes = new_size;
1088 * This is called when incore extents are being added to the indirection
1089 * array and the new extents do not fit in the target extent list. The
1090 * erp_idx parameter contains the irec index for the target extent list
1091 * in the indirection array, and the idx parameter contains the extent
1092 * index within the list. The number of extents being added is stored
1093 * in the count parameter.
1095 * |-------| |-------|
1096 * | | | | idx - number of extents before idx
1098 * | | | | count - number of extents being inserted at idx
1099 * |-------| |-------|
1100 * | count | | nex2 | nex2 - number of extents after idx + count
1101 * |-------| |-------|
1104 xfs_iext_add_indirect_multi(
1105 xfs_ifork_t *ifp, /* inode fork pointer */
1106 int erp_idx, /* target extent irec index */
1107 xfs_extnum_t idx, /* index within target list */
1108 int count) /* new extents being added */
1110 int byte_diff; /* new bytes being added */
1111 xfs_ext_irec_t *erp; /* pointer to irec entry */
1112 xfs_extnum_t ext_diff; /* number of extents to add */
1113 xfs_extnum_t ext_cnt; /* new extents still needed */
1114 xfs_extnum_t nex2; /* extents after idx + count */
1115 xfs_bmbt_rec_t *nex2_ep = NULL; /* temp list for nex2 extents */
1116 int nlists; /* number of irec's (lists) */
1118 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1119 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1120 nex2 = erp->er_extcount - idx;
1121 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1124 * Save second part of target extent list
1125 * (all extents past */
1127 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
1128 nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_NOFS);
1129 memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff);
1130 erp->er_extcount -= nex2;
1131 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2);
1132 memset(&erp->er_extbuf[idx], 0, byte_diff);
1136 * Add the new extents to the end of the target
1137 * list, then allocate new irec record(s) and
1138 * extent buffer(s) as needed to store the rest
1139 * of the new extents.
1142 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount);
1144 erp->er_extcount += ext_diff;
1145 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
1146 ext_cnt -= ext_diff;
1150 erp = xfs_iext_irec_new(ifp, erp_idx);
1151 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS);
1152 erp->er_extcount = ext_diff;
1153 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
1154 ext_cnt -= ext_diff;
1157 /* Add nex2 extents back to indirection array */
1159 xfs_extnum_t ext_avail;
1162 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
1163 ext_avail = XFS_LINEAR_EXTS - erp->er_extcount;
1166 * If nex2 extents fit in the current page, append
1167 * nex2_ep after the new extents.
1169 if (nex2 <= ext_avail) {
1170 i = erp->er_extcount;
1173 * Otherwise, check if space is available in the
1176 else if ((erp_idx < nlists - 1) &&
1177 (nex2 <= (ext_avail = XFS_LINEAR_EXTS -
1178 ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) {
1181 /* Create a hole for nex2 extents */
1182 memmove(&erp->er_extbuf[nex2], erp->er_extbuf,
1183 erp->er_extcount * sizeof(xfs_bmbt_rec_t));
1186 * Final choice, create a new extent page for
1191 erp = xfs_iext_irec_new(ifp, erp_idx);
1193 memmove(&erp->er_extbuf[i], nex2_ep, byte_diff);
1195 erp->er_extcount += nex2;
1196 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2);
1201 * This is called when the amount of space required for incore file
1202 * extents needs to be decreased. The ext_diff parameter stores the
1203 * number of extents to be removed and the idx parameter contains
1204 * the extent index where the extents will be removed from.
1206 * If the amount of space needed has decreased below the linear
1207 * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
1208 * extent array. Otherwise, use kmem_realloc() to adjust the
1209 * size to what is needed.
1213 xfs_inode_t *ip, /* incore inode pointer */
1214 xfs_extnum_t idx, /* index to begin removing exts */
1215 int ext_diff, /* number of extents to remove */
1216 int state) /* type of extent conversion */
1218 xfs_ifork_t *ifp = xfs_iext_state_to_fork(ip, state);
1219 xfs_extnum_t nextents; /* number of extents in file */
1220 int new_size; /* size of extents after removal */
1222 trace_xfs_iext_remove(ip, idx, state, _RET_IP_);
1224 ASSERT(ext_diff > 0);
1225 nextents = xfs_iext_count(ifp);
1226 new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t);
1228 if (new_size == 0) {
1229 xfs_iext_destroy(ifp);
1230 } else if (ifp->if_flags & XFS_IFEXTIREC) {
1231 xfs_iext_remove_indirect(ifp, idx, ext_diff);
1232 } else if (ifp->if_real_bytes) {
1233 xfs_iext_remove_direct(ifp, idx, ext_diff);
1235 xfs_iext_remove_inline(ifp, idx, ext_diff);
1237 ifp->if_bytes = new_size;
1241 * This removes ext_diff extents from the inline buffer, beginning
1242 * at extent index idx.
1245 xfs_iext_remove_inline(
1246 xfs_ifork_t *ifp, /* inode fork pointer */
1247 xfs_extnum_t idx, /* index to begin removing exts */
1248 int ext_diff) /* number of extents to remove */
1250 int nextents; /* number of extents in file */
1252 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
1253 ASSERT(idx < XFS_INLINE_EXTS);
1254 nextents = xfs_iext_count(ifp);
1255 ASSERT(((nextents - ext_diff) > 0) &&
1256 (nextents - ext_diff) < XFS_INLINE_EXTS);
1258 if (idx + ext_diff < nextents) {
1259 memmove(&ifp->if_u2.if_inline_ext[idx],
1260 &ifp->if_u2.if_inline_ext[idx + ext_diff],
1261 (nextents - (idx + ext_diff)) *
1262 sizeof(xfs_bmbt_rec_t));
1263 memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff],
1264 0, ext_diff * sizeof(xfs_bmbt_rec_t));
1266 memset(&ifp->if_u2.if_inline_ext[idx], 0,
1267 ext_diff * sizeof(xfs_bmbt_rec_t));
1272 * This removes ext_diff extents from a linear (direct) extent list,
1273 * beginning at extent index idx. If the extents are being removed
1274 * from the end of the list (ie. truncate) then we just need to re-
1275 * allocate the list to remove the extra space. Otherwise, if the
1276 * extents are being removed from the middle of the existing extent
1277 * entries, then we first need to move the extent records beginning
1278 * at idx + ext_diff up in the list to overwrite the records being
1279 * removed, then remove the extra space via kmem_realloc.
1282 xfs_iext_remove_direct(
1283 xfs_ifork_t *ifp, /* inode fork pointer */
1284 xfs_extnum_t idx, /* index to begin removing exts */
1285 int ext_diff) /* number of extents to remove */
1287 xfs_extnum_t nextents; /* number of extents in file */
1288 int new_size; /* size of extents after removal */
1290 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
1291 new_size = ifp->if_bytes -
1292 (ext_diff * sizeof(xfs_bmbt_rec_t));
1293 nextents = xfs_iext_count(ifp);
1295 if (new_size == 0) {
1296 xfs_iext_destroy(ifp);
1299 /* Move extents up in the list (if needed) */
1300 if (idx + ext_diff < nextents) {
1301 memmove(&ifp->if_u1.if_extents[idx],
1302 &ifp->if_u1.if_extents[idx + ext_diff],
1303 (nextents - (idx + ext_diff)) *
1304 sizeof(xfs_bmbt_rec_t));
1306 memset(&ifp->if_u1.if_extents[nextents - ext_diff],
1307 0, ext_diff * sizeof(xfs_bmbt_rec_t));
1309 * Reallocate the direct extent list. If the extents
1310 * will fit inside the inode then xfs_iext_realloc_direct
1311 * will switch from direct to inline extent allocation
1314 xfs_iext_realloc_direct(ifp, new_size);
1315 ifp->if_bytes = new_size;
1319 * This is called when incore extents are being removed from the
1320 * indirection array and the extents being removed span multiple extent
1321 * buffers. The idx parameter contains the file extent index where we
1322 * want to begin removing extents, and the count parameter contains
1323 * how many extents need to be removed.
1325 * |-------| |-------|
1326 * | nex1 | | | nex1 - number of extents before idx
1327 * |-------| | count |
1328 * | | | | count - number of extents being removed at idx
1329 * | count | |-------|
1330 * | | | nex2 | nex2 - number of extents after idx + count
1331 * |-------| |-------|
1334 xfs_iext_remove_indirect(
1335 xfs_ifork_t *ifp, /* inode fork pointer */
1336 xfs_extnum_t idx, /* index to begin removing extents */
1337 int count) /* number of extents to remove */
1339 xfs_ext_irec_t *erp; /* indirection array pointer */
1340 int erp_idx = 0; /* indirection array index */
1341 xfs_extnum_t ext_cnt; /* extents left to remove */
1342 xfs_extnum_t ext_diff; /* extents to remove in current list */
1343 xfs_extnum_t nex1; /* number of extents before idx */
1344 xfs_extnum_t nex2; /* extents after idx + count */
1345 int page_idx = idx; /* index in target extent list */
1347 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1348 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
1349 ASSERT(erp != NULL);
1353 nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0);
1354 ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1));
1356 * Check for deletion of entire list;
1357 * xfs_iext_irec_remove() updates extent offsets.
1359 if (ext_diff == erp->er_extcount) {
1360 xfs_iext_irec_remove(ifp, erp_idx);
1361 ext_cnt -= ext_diff;
1364 ASSERT(erp_idx < ifp->if_real_bytes /
1366 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1373 /* Move extents up (if needed) */
1375 memmove(&erp->er_extbuf[nex1],
1376 &erp->er_extbuf[nex1 + ext_diff],
1377 nex2 * sizeof(xfs_bmbt_rec_t));
1379 /* Zero out rest of page */
1380 memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ -
1381 ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t))));
1382 /* Update remaining counters */
1383 erp->er_extcount -= ext_diff;
1384 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff);
1385 ext_cnt -= ext_diff;
1390 ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t);
1391 xfs_iext_irec_compact(ifp);
1395 * Create, destroy, or resize a linear (direct) block of extents.
1398 xfs_iext_realloc_direct(
1399 xfs_ifork_t *ifp, /* inode fork pointer */
1400 int new_size) /* new size of extents after adding */
1402 int rnew_size; /* real new size of extents */
1404 rnew_size = new_size;
1406 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) ||
1407 ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) &&
1408 (new_size != ifp->if_real_bytes)));
1410 /* Free extent records */
1411 if (new_size == 0) {
1412 xfs_iext_destroy(ifp);
1414 /* Resize direct extent list and zero any new bytes */
1415 else if (ifp->if_real_bytes) {
1416 /* Check if extents will fit inside the inode */
1417 if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) {
1418 xfs_iext_direct_to_inline(ifp, new_size /
1419 (uint)sizeof(xfs_bmbt_rec_t));
1420 ifp->if_bytes = new_size;
1423 if (!is_power_of_2(new_size)){
1424 rnew_size = roundup_pow_of_two(new_size);
1426 if (rnew_size != ifp->if_real_bytes) {
1427 ifp->if_u1.if_extents =
1428 kmem_realloc(ifp->if_u1.if_extents,
1429 rnew_size, KM_NOFS);
1431 if (rnew_size > ifp->if_real_bytes) {
1432 memset(&ifp->if_u1.if_extents[ifp->if_bytes /
1433 (uint)sizeof(xfs_bmbt_rec_t)], 0,
1434 rnew_size - ifp->if_real_bytes);
1437 /* Switch from the inline extent buffer to a direct extent list */
1439 if (!is_power_of_2(new_size)) {
1440 rnew_size = roundup_pow_of_two(new_size);
1442 xfs_iext_inline_to_direct(ifp, rnew_size);
1444 ifp->if_real_bytes = rnew_size;
1445 ifp->if_bytes = new_size;
1449 * Switch from linear (direct) extent records to inline buffer.
1452 xfs_iext_direct_to_inline(
1453 xfs_ifork_t *ifp, /* inode fork pointer */
1454 xfs_extnum_t nextents) /* number of extents in file */
1456 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1457 ASSERT(nextents <= XFS_INLINE_EXTS);
1459 * The inline buffer was zeroed when we switched
1460 * from inline to direct extent allocation mode,
1461 * so we don't need to clear it here.
1463 memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents,
1464 nextents * sizeof(xfs_bmbt_rec_t));
1465 kmem_free(ifp->if_u1.if_extents);
1466 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
1467 ifp->if_real_bytes = 0;
1471 * Switch from inline buffer to linear (direct) extent records.
1472 * new_size should already be rounded up to the next power of 2
1473 * by the caller (when appropriate), so use new_size as it is.
1474 * However, since new_size may be rounded up, we can't update
1475 * if_bytes here. It is the caller's responsibility to update
1476 * if_bytes upon return.
1479 xfs_iext_inline_to_direct(
1480 xfs_ifork_t *ifp, /* inode fork pointer */
1481 int new_size) /* number of extents in file */
1483 ifp->if_u1.if_extents = kmem_alloc(new_size, KM_NOFS);
1484 memset(ifp->if_u1.if_extents, 0, new_size);
1485 if (ifp->if_bytes) {
1486 memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext,
1488 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
1489 sizeof(xfs_bmbt_rec_t));
1491 ifp->if_real_bytes = new_size;
1495 * Resize an extent indirection array to new_size bytes.
1498 xfs_iext_realloc_indirect(
1499 xfs_ifork_t *ifp, /* inode fork pointer */
1500 int new_size) /* new indirection array size */
1502 int nlists; /* number of irec's (ex lists) */
1503 int size; /* current indirection array size */
1505 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1506 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1507 size = nlists * sizeof(xfs_ext_irec_t);
1508 ASSERT(ifp->if_real_bytes);
1509 ASSERT((new_size >= 0) && (new_size != size));
1510 if (new_size == 0) {
1511 xfs_iext_destroy(ifp);
1513 ifp->if_u1.if_ext_irec =
1514 kmem_realloc(ifp->if_u1.if_ext_irec, new_size, KM_NOFS);
1519 * Switch from indirection array to linear (direct) extent allocations.
1522 xfs_iext_indirect_to_direct(
1523 xfs_ifork_t *ifp) /* inode fork pointer */
1525 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1526 xfs_extnum_t nextents; /* number of extents in file */
1527 int size; /* size of file extents */
1529 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1530 nextents = xfs_iext_count(ifp);
1531 ASSERT(nextents <= XFS_LINEAR_EXTS);
1532 size = nextents * sizeof(xfs_bmbt_rec_t);
1534 xfs_iext_irec_compact_pages(ifp);
1535 ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ);
1537 ep = ifp->if_u1.if_ext_irec->er_extbuf;
1538 kmem_free(ifp->if_u1.if_ext_irec);
1539 ifp->if_flags &= ~XFS_IFEXTIREC;
1540 ifp->if_u1.if_extents = ep;
1541 ifp->if_bytes = size;
1542 if (nextents < XFS_LINEAR_EXTS) {
1543 xfs_iext_realloc_direct(ifp, size);
1548 * Remove all records from the indirection array.
1551 xfs_iext_irec_remove_all(
1552 struct xfs_ifork *ifp)
1557 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1558 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1559 for (i = 0; i < nlists; i++)
1560 kmem_free(ifp->if_u1.if_ext_irec[i].er_extbuf);
1561 kmem_free(ifp->if_u1.if_ext_irec);
1562 ifp->if_flags &= ~XFS_IFEXTIREC;
1566 * Free incore file extents.
1570 xfs_ifork_t *ifp) /* inode fork pointer */
1572 if (ifp->if_flags & XFS_IFEXTIREC) {
1573 xfs_iext_irec_remove_all(ifp);
1574 } else if (ifp->if_real_bytes) {
1575 kmem_free(ifp->if_u1.if_extents);
1576 } else if (ifp->if_bytes) {
1577 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
1578 sizeof(xfs_bmbt_rec_t));
1580 ifp->if_u1.if_extents = NULL;
1581 ifp->if_real_bytes = 0;
1586 * Return a pointer to the extent record for file system block bno.
1588 xfs_bmbt_rec_host_t * /* pointer to found extent record */
1589 xfs_iext_bno_to_ext(
1590 xfs_ifork_t *ifp, /* inode fork pointer */
1591 xfs_fileoff_t bno, /* block number to search for */
1592 xfs_extnum_t *idxp) /* index of target extent */
1594 xfs_bmbt_rec_host_t *base; /* pointer to first extent */
1595 xfs_filblks_t blockcount = 0; /* number of blocks in extent */
1596 xfs_bmbt_rec_host_t *ep = NULL; /* pointer to target extent */
1597 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
1598 int high; /* upper boundary in search */
1599 xfs_extnum_t idx = 0; /* index of target extent */
1600 int low; /* lower boundary in search */
1601 xfs_extnum_t nextents; /* number of file extents */
1602 xfs_fileoff_t startoff = 0; /* start offset of extent */
1604 nextents = xfs_iext_count(ifp);
1605 if (nextents == 0) {
1610 if (ifp->if_flags & XFS_IFEXTIREC) {
1611 /* Find target extent list */
1613 erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx);
1614 base = erp->er_extbuf;
1615 high = erp->er_extcount - 1;
1617 base = ifp->if_u1.if_extents;
1618 high = nextents - 1;
1620 /* Binary search extent records */
1621 while (low <= high) {
1622 idx = (low + high) >> 1;
1624 startoff = xfs_bmbt_get_startoff(ep);
1625 blockcount = xfs_bmbt_get_blockcount(ep);
1626 if (bno < startoff) {
1628 } else if (bno >= startoff + blockcount) {
1631 /* Convert back to file-based extent index */
1632 if (ifp->if_flags & XFS_IFEXTIREC) {
1633 idx += erp->er_extoff;
1639 /* Convert back to file-based extent index */
1640 if (ifp->if_flags & XFS_IFEXTIREC) {
1641 idx += erp->er_extoff;
1643 if (bno >= startoff + blockcount) {
1644 if (++idx == nextents) {
1647 ep = xfs_iext_get_ext(ifp, idx);
1655 * Return a pointer to the indirection array entry containing the
1656 * extent record for filesystem block bno. Store the index of the
1657 * target irec in *erp_idxp.
1659 xfs_ext_irec_t * /* pointer to found extent record */
1660 xfs_iext_bno_to_irec(
1661 xfs_ifork_t *ifp, /* inode fork pointer */
1662 xfs_fileoff_t bno, /* block number to search for */
1663 int *erp_idxp) /* irec index of target ext list */
1665 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
1666 xfs_ext_irec_t *erp_next; /* next indirection array entry */
1667 int erp_idx; /* indirection array index */
1668 int nlists; /* number of extent irec's (lists) */
1669 int high; /* binary search upper limit */
1670 int low; /* binary search lower limit */
1672 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1673 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1677 while (low <= high) {
1678 erp_idx = (low + high) >> 1;
1679 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1680 erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL;
1681 if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) {
1683 } else if (erp_next && bno >=
1684 xfs_bmbt_get_startoff(erp_next->er_extbuf)) {
1690 *erp_idxp = erp_idx;
1695 * Return a pointer to the indirection array entry containing the
1696 * extent record at file extent index *idxp. Store the index of the
1697 * target irec in *erp_idxp and store the page index of the target
1698 * extent record in *idxp.
1701 xfs_iext_idx_to_irec(
1702 xfs_ifork_t *ifp, /* inode fork pointer */
1703 xfs_extnum_t *idxp, /* extent index (file -> page) */
1704 int *erp_idxp, /* pointer to target irec */
1705 int realloc) /* new bytes were just added */
1707 xfs_ext_irec_t *prev; /* pointer to previous irec */
1708 xfs_ext_irec_t *erp = NULL; /* pointer to current irec */
1709 int erp_idx; /* indirection array index */
1710 int nlists; /* number of irec's (ex lists) */
1711 int high; /* binary search upper limit */
1712 int low; /* binary search lower limit */
1713 xfs_extnum_t page_idx = *idxp; /* extent index in target list */
1715 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1716 ASSERT(page_idx >= 0);
1717 ASSERT(page_idx <= xfs_iext_count(ifp));
1718 ASSERT(page_idx < xfs_iext_count(ifp) || realloc);
1720 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1725 /* Binary search extent irec's */
1726 while (low <= high) {
1727 erp_idx = (low + high) >> 1;
1728 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1729 prev = erp_idx > 0 ? erp - 1 : NULL;
1730 if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff &&
1731 realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) {
1733 } else if (page_idx > erp->er_extoff + erp->er_extcount ||
1734 (page_idx == erp->er_extoff + erp->er_extcount &&
1737 } else if (page_idx == erp->er_extoff + erp->er_extcount &&
1738 erp->er_extcount == XFS_LINEAR_EXTS) {
1742 erp = erp_idx < nlists ? erp + 1 : NULL;
1745 page_idx -= erp->er_extoff;
1750 *erp_idxp = erp_idx;
1755 * Allocate and initialize an indirection array once the space needed
1756 * for incore extents increases above XFS_IEXT_BUFSZ.
1760 xfs_ifork_t *ifp) /* inode fork pointer */
1762 xfs_ext_irec_t *erp; /* indirection array pointer */
1763 xfs_extnum_t nextents; /* number of extents in file */
1765 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
1766 nextents = xfs_iext_count(ifp);
1767 ASSERT(nextents <= XFS_LINEAR_EXTS);
1769 erp = kmem_alloc(sizeof(xfs_ext_irec_t), KM_NOFS);
1771 if (nextents == 0) {
1772 ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
1773 } else if (!ifp->if_real_bytes) {
1774 xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ);
1775 } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) {
1776 xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ);
1778 erp->er_extbuf = ifp->if_u1.if_extents;
1779 erp->er_extcount = nextents;
1782 ifp->if_flags |= XFS_IFEXTIREC;
1783 ifp->if_real_bytes = XFS_IEXT_BUFSZ;
1784 ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t);
1785 ifp->if_u1.if_ext_irec = erp;
1791 * Allocate and initialize a new entry in the indirection array.
1795 xfs_ifork_t *ifp, /* inode fork pointer */
1796 int erp_idx) /* index for new irec */
1798 xfs_ext_irec_t *erp; /* indirection array pointer */
1799 int i; /* loop counter */
1800 int nlists; /* number of irec's (ex lists) */
1802 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1803 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1805 /* Resize indirection array */
1806 xfs_iext_realloc_indirect(ifp, ++nlists *
1807 sizeof(xfs_ext_irec_t));
1809 * Move records down in the array so the
1810 * new page can use erp_idx.
1812 erp = ifp->if_u1.if_ext_irec;
1813 for (i = nlists - 1; i > erp_idx; i--) {
1814 memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t));
1816 ASSERT(i == erp_idx);
1818 /* Initialize new extent record */
1819 erp = ifp->if_u1.if_ext_irec;
1820 erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
1821 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
1822 memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ);
1823 erp[erp_idx].er_extcount = 0;
1824 erp[erp_idx].er_extoff = erp_idx > 0 ?
1825 erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0;
1826 return (&erp[erp_idx]);
1830 * Remove a record from the indirection array.
1833 xfs_iext_irec_remove(
1834 xfs_ifork_t *ifp, /* inode fork pointer */
1835 int erp_idx) /* irec index to remove */
1837 xfs_ext_irec_t *erp; /* indirection array pointer */
1838 int i; /* loop counter */
1839 int nlists; /* number of irec's (ex lists) */
1841 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1842 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1843 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1844 if (erp->er_extbuf) {
1845 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1,
1847 kmem_free(erp->er_extbuf);
1849 /* Compact extent records */
1850 erp = ifp->if_u1.if_ext_irec;
1851 for (i = erp_idx; i < nlists - 1; i++) {
1852 memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t));
1855 * Manually free the last extent record from the indirection
1856 * array. A call to xfs_iext_realloc_indirect() with a size
1857 * of zero would result in a call to xfs_iext_destroy() which
1858 * would in turn call this function again, creating a nasty
1862 xfs_iext_realloc_indirect(ifp,
1863 nlists * sizeof(xfs_ext_irec_t));
1865 kmem_free(ifp->if_u1.if_ext_irec);
1867 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
1871 * This is called to clean up large amounts of unused memory allocated
1872 * by the indirection array. Before compacting anything though, verify
1873 * that the indirection array is still needed and switch back to the
1874 * linear extent list (or even the inline buffer) if possible. The
1875 * compaction policy is as follows:
1877 * Full Compaction: Extents fit into a single page (or inline buffer)
1878 * Partial Compaction: Extents occupy less than 50% of allocated space
1879 * No Compaction: Extents occupy at least 50% of allocated space
1882 xfs_iext_irec_compact(
1883 xfs_ifork_t *ifp) /* inode fork pointer */
1885 xfs_extnum_t nextents; /* number of extents in file */
1886 int nlists; /* number of irec's (ex lists) */
1888 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1889 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1890 nextents = xfs_iext_count(ifp);
1892 if (nextents == 0) {
1893 xfs_iext_destroy(ifp);
1894 } else if (nextents <= XFS_INLINE_EXTS) {
1895 xfs_iext_indirect_to_direct(ifp);
1896 xfs_iext_direct_to_inline(ifp, nextents);
1897 } else if (nextents <= XFS_LINEAR_EXTS) {
1898 xfs_iext_indirect_to_direct(ifp);
1899 } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) {
1900 xfs_iext_irec_compact_pages(ifp);
1905 * Combine extents from neighboring extent pages.
1908 xfs_iext_irec_compact_pages(
1909 xfs_ifork_t *ifp) /* inode fork pointer */
1911 xfs_ext_irec_t *erp, *erp_next;/* pointers to irec entries */
1912 int erp_idx = 0; /* indirection array index */
1913 int nlists; /* number of irec's (ex lists) */
1915 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1916 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1917 while (erp_idx < nlists - 1) {
1918 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1920 if (erp_next->er_extcount <=
1921 (XFS_LINEAR_EXTS - erp->er_extcount)) {
1922 memcpy(&erp->er_extbuf[erp->er_extcount],
1923 erp_next->er_extbuf, erp_next->er_extcount *
1924 sizeof(xfs_bmbt_rec_t));
1925 erp->er_extcount += erp_next->er_extcount;
1927 * Free page before removing extent record
1928 * so er_extoffs don't get modified in
1929 * xfs_iext_irec_remove.
1931 kmem_free(erp_next->er_extbuf);
1932 erp_next->er_extbuf = NULL;
1933 xfs_iext_irec_remove(ifp, erp_idx + 1);
1934 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1942 * This is called to update the er_extoff field in the indirection
1943 * array when extents have been added or removed from one of the
1944 * extent lists. erp_idx contains the irec index to begin updating
1945 * at and ext_diff contains the number of extents that were added
1949 xfs_iext_irec_update_extoffs(
1950 xfs_ifork_t *ifp, /* inode fork pointer */
1951 int erp_idx, /* irec index to update */
1952 int ext_diff) /* number of new extents */
1954 int i; /* loop counter */
1955 int nlists; /* number of irec's (ex lists */
1957 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1958 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1959 for (i = erp_idx; i < nlists; i++) {
1960 ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff;
1965 * Initialize an inode's copy-on-write fork.
1969 struct xfs_inode *ip)
1974 ip->i_cowfp = kmem_zone_zalloc(xfs_ifork_zone,
1975 KM_SLEEP | KM_NOFS);
1976 ip->i_cowfp->if_flags = XFS_IFEXTENTS;
1977 ip->i_cformat = XFS_DINODE_FMT_EXTENTS;
1978 ip->i_cnextents = 0;
1982 * Lookup the extent covering bno.
1984 * If there is an extent covering bno return the extent index, and store the
1985 * expanded extent structure in *gotp, and the extent index in *idx.
1986 * If there is no extent covering bno, but there is an extent after it (e.g.
1987 * it lies in a hole) return that extent in *gotp and its index in *idx
1989 * If bno is beyond the last extent return false, and return the index after
1990 * the last valid index in *idxp.
1993 xfs_iext_lookup_extent(
1994 struct xfs_inode *ip,
1995 struct xfs_ifork *ifp,
1998 struct xfs_bmbt_irec *gotp)
2000 struct xfs_bmbt_rec_host *ep;
2002 XFS_STATS_INC(ip->i_mount, xs_look_exlist);
2004 ep = xfs_iext_bno_to_ext(ifp, bno, idxp);
2007 xfs_bmbt_get_all(ep, gotp);
2012 * Return true if there is an extent at index idx, and return the expanded
2013 * extent structure at idx in that case. Else return false.
2016 xfs_iext_get_extent(
2017 struct xfs_ifork *ifp,
2019 struct xfs_bmbt_irec *gotp)
2021 if (idx < 0 || idx >= xfs_iext_count(ifp))
2023 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), gotp);