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"
37 kmem_zone_t
*xfs_ifork_zone
;
39 STATIC
int xfs_iformat_local(xfs_inode_t
*, xfs_dinode_t
*, int, int);
40 STATIC
int xfs_iformat_extents(xfs_inode_t
*, xfs_dinode_t
*, int);
41 STATIC
int xfs_iformat_btree(xfs_inode_t
*, xfs_dinode_t
*, int);
45 * Make sure that the extents in the given memory buffer
55 xfs_bmbt_rec_host_t rec
;
58 for (i
= 0; i
< nrecs
; i
++) {
59 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, i
);
60 rec
.l0
= get_unaligned(&ep
->l0
);
61 rec
.l1
= get_unaligned(&ep
->l1
);
62 xfs_bmbt_get_all(&rec
, &irec
);
63 if (fmt
== XFS_EXTFMT_NOSTATE
)
64 ASSERT(irec
.br_state
== XFS_EXT_NORM
);
68 #define xfs_validate_extents(ifp, nrecs, fmt)
73 * Move inode type and inode format specific information from the
74 * on-disk inode to the in-core inode. For fifos, devs, and sockets
75 * this means set if_rdev to the proper value. For files, directories,
76 * and symlinks this means to bring in the in-line data or extent
77 * pointers. For a file in B-tree format, only the root is immediately
78 * brought in-core. The rest will be in-lined in if_extents when it
79 * is first referenced (see xfs_iread_extents()).
86 xfs_attr_shortform_t
*atp
;
91 if (unlikely(be32_to_cpu(dip
->di_nextents
) +
92 be16_to_cpu(dip
->di_anextents
) >
93 be64_to_cpu(dip
->di_nblocks
))) {
95 "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.",
96 (unsigned long long)ip
->i_ino
,
97 (int)(be32_to_cpu(dip
->di_nextents
) +
98 be16_to_cpu(dip
->di_anextents
)),
100 be64_to_cpu(dip
->di_nblocks
));
101 XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW
,
103 return -EFSCORRUPTED
;
106 if (unlikely(dip
->di_forkoff
> ip
->i_mount
->m_sb
.sb_inodesize
)) {
107 xfs_warn(ip
->i_mount
, "corrupt dinode %Lu, forkoff = 0x%x.",
108 (unsigned long long)ip
->i_ino
,
110 XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW
,
112 return -EFSCORRUPTED
;
115 if (unlikely((ip
->i_d
.di_flags
& XFS_DIFLAG_REALTIME
) &&
116 !ip
->i_mount
->m_rtdev_targp
)) {
117 xfs_warn(ip
->i_mount
,
118 "corrupt dinode %Lu, has realtime flag set.",
120 XFS_CORRUPTION_ERROR("xfs_iformat(realtime)",
121 XFS_ERRLEVEL_LOW
, ip
->i_mount
, dip
);
122 return -EFSCORRUPTED
;
125 if (unlikely(xfs_is_reflink_inode(ip
) &&
126 (VFS_I(ip
)->i_mode
& S_IFMT
) != S_IFREG
)) {
127 xfs_warn(ip
->i_mount
,
128 "corrupt dinode %llu, wrong file type for reflink.",
130 XFS_CORRUPTION_ERROR("xfs_iformat(reflink)",
131 XFS_ERRLEVEL_LOW
, ip
->i_mount
, dip
);
132 return -EFSCORRUPTED
;
135 if (unlikely(xfs_is_reflink_inode(ip
) &&
136 (ip
->i_d
.di_flags
& XFS_DIFLAG_REALTIME
))) {
137 xfs_warn(ip
->i_mount
,
138 "corrupt dinode %llu, has reflink+realtime flag set.",
140 XFS_CORRUPTION_ERROR("xfs_iformat(reflink)",
141 XFS_ERRLEVEL_LOW
, ip
->i_mount
, dip
);
142 return -EFSCORRUPTED
;
145 switch (VFS_I(ip
)->i_mode
& S_IFMT
) {
150 if (unlikely(dip
->di_format
!= XFS_DINODE_FMT_DEV
)) {
151 XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW
,
153 return -EFSCORRUPTED
;
156 ip
->i_df
.if_u2
.if_rdev
= xfs_dinode_get_rdev(dip
);
162 switch (dip
->di_format
) {
163 case XFS_DINODE_FMT_LOCAL
:
165 * no local regular files yet
167 if (unlikely(S_ISREG(be16_to_cpu(dip
->di_mode
)))) {
168 xfs_warn(ip
->i_mount
,
169 "corrupt inode %Lu (local format for regular file).",
170 (unsigned long long) ip
->i_ino
);
171 XFS_CORRUPTION_ERROR("xfs_iformat(4)",
174 return -EFSCORRUPTED
;
177 di_size
= be64_to_cpu(dip
->di_size
);
178 if (unlikely(di_size
< 0 ||
179 di_size
> XFS_DFORK_DSIZE(dip
, ip
->i_mount
))) {
180 xfs_warn(ip
->i_mount
,
181 "corrupt inode %Lu (bad size %Ld for local inode).",
182 (unsigned long long) ip
->i_ino
,
183 (long long) di_size
);
184 XFS_CORRUPTION_ERROR("xfs_iformat(5)",
187 return -EFSCORRUPTED
;
191 error
= xfs_iformat_local(ip
, dip
, XFS_DATA_FORK
, size
);
193 case XFS_DINODE_FMT_EXTENTS
:
194 error
= xfs_iformat_extents(ip
, dip
, XFS_DATA_FORK
);
196 case XFS_DINODE_FMT_BTREE
:
197 error
= xfs_iformat_btree(ip
, dip
, XFS_DATA_FORK
);
200 XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW
,
202 return -EFSCORRUPTED
;
207 XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW
, ip
->i_mount
);
208 return -EFSCORRUPTED
;
213 if (xfs_is_reflink_inode(ip
)) {
214 ASSERT(ip
->i_cowfp
== NULL
);
215 xfs_ifork_init_cow(ip
);
218 if (!XFS_DFORK_Q(dip
))
221 ASSERT(ip
->i_afp
== NULL
);
222 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
| KM_NOFS
);
224 switch (dip
->di_aformat
) {
225 case XFS_DINODE_FMT_LOCAL
:
226 atp
= (xfs_attr_shortform_t
*)XFS_DFORK_APTR(dip
);
227 size
= be16_to_cpu(atp
->hdr
.totsize
);
229 if (unlikely(size
< sizeof(struct xfs_attr_sf_hdr
))) {
230 xfs_warn(ip
->i_mount
,
231 "corrupt inode %Lu (bad attr fork size %Ld).",
232 (unsigned long long) ip
->i_ino
,
234 XFS_CORRUPTION_ERROR("xfs_iformat(8)",
237 error
= -EFSCORRUPTED
;
241 error
= xfs_iformat_local(ip
, dip
, XFS_ATTR_FORK
, size
);
243 case XFS_DINODE_FMT_EXTENTS
:
244 error
= xfs_iformat_extents(ip
, dip
, XFS_ATTR_FORK
);
246 case XFS_DINODE_FMT_BTREE
:
247 error
= xfs_iformat_btree(ip
, dip
, XFS_ATTR_FORK
);
250 error
= -EFSCORRUPTED
;
254 kmem_zone_free(xfs_ifork_zone
, ip
->i_afp
);
257 kmem_zone_free(xfs_ifork_zone
, ip
->i_cowfp
);
259 xfs_idestroy_fork(ip
, XFS_DATA_FORK
);
266 struct xfs_inode
*ip
,
271 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
272 int mem_size
= size
, real_size
= 0;
276 * If we are using the local fork to store a symlink body we need to
277 * zero-terminate it so that we can pass it back to the VFS directly.
278 * Overallocate the in-memory fork by one for that and add a zero
279 * to terminate it below.
281 zero_terminate
= S_ISLNK(VFS_I(ip
)->i_mode
);
286 ifp
->if_u1
.if_data
= NULL
;
287 else if (mem_size
<= sizeof(ifp
->if_u2
.if_inline_data
))
288 ifp
->if_u1
.if_data
= ifp
->if_u2
.if_inline_data
;
290 real_size
= roundup(mem_size
, 4);
291 ifp
->if_u1
.if_data
= kmem_alloc(real_size
, KM_SLEEP
| KM_NOFS
);
295 memcpy(ifp
->if_u1
.if_data
, data
, size
);
297 ifp
->if_u1
.if_data
[size
] = '\0';
300 ifp
->if_bytes
= size
;
301 ifp
->if_real_bytes
= real_size
;
302 ifp
->if_flags
&= ~(XFS_IFEXTENTS
| XFS_IFBROOT
);
303 ifp
->if_flags
|= XFS_IFINLINE
;
307 * The file is in-lined in the on-disk inode.
308 * If it fits into if_inline_data, then copy
309 * it there, otherwise allocate a buffer for it
310 * and copy the data there. Either way, set
311 * if_data to point at the data.
312 * If we allocate a buffer for the data, make
313 * sure that its size is a multiple of 4 and
314 * record the real size in i_real_bytes.
325 * If the size is unreasonable, then something
326 * is wrong and we just bail out rather than crash in
327 * kmem_alloc() or memcpy() below.
329 if (unlikely(size
> XFS_DFORK_SIZE(dip
, ip
->i_mount
, whichfork
))) {
330 xfs_warn(ip
->i_mount
,
331 "corrupt inode %Lu (bad size %d for local fork, size = %d).",
332 (unsigned long long) ip
->i_ino
, size
,
333 XFS_DFORK_SIZE(dip
, ip
->i_mount
, whichfork
));
334 XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW
,
336 return -EFSCORRUPTED
;
339 xfs_init_local_fork(ip
, whichfork
, XFS_DFORK_PTR(dip
, whichfork
), size
);
344 * The file consists of a set of extents all
345 * of which fit into the on-disk inode.
346 * If there are few enough extents to fit into
347 * the if_inline_ext, then copy them there.
348 * Otherwise allocate a buffer for them and copy
349 * them into it. Either way, set if_extents
350 * to point at the extents.
364 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
365 nex
= XFS_DFORK_NEXTENTS(dip
, whichfork
);
366 size
= nex
* (uint
)sizeof(xfs_bmbt_rec_t
);
369 * If the number of extents is unreasonable, then something
370 * is wrong and we just bail out rather than crash in
371 * kmem_alloc() or memcpy() below.
373 if (unlikely(size
< 0 || size
> XFS_DFORK_SIZE(dip
, ip
->i_mount
, whichfork
))) {
374 xfs_warn(ip
->i_mount
, "corrupt inode %Lu ((a)extents = %d).",
375 (unsigned long long) ip
->i_ino
, nex
);
376 XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW
,
378 return -EFSCORRUPTED
;
381 ifp
->if_real_bytes
= 0;
383 ifp
->if_u1
.if_extents
= NULL
;
384 else if (nex
<= XFS_INLINE_EXTS
)
385 ifp
->if_u1
.if_extents
= ifp
->if_u2
.if_inline_ext
;
387 xfs_iext_add(ifp
, 0, nex
);
389 ifp
->if_bytes
= size
;
391 dp
= (xfs_bmbt_rec_t
*) XFS_DFORK_PTR(dip
, whichfork
);
392 xfs_validate_extents(ifp
, nex
, XFS_EXTFMT_INODE(ip
));
393 for (i
= 0; i
< nex
; i
++, dp
++) {
394 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, i
);
395 ep
->l0
= get_unaligned_be64(&dp
->l0
);
396 ep
->l1
= get_unaligned_be64(&dp
->l1
);
398 XFS_BMAP_TRACE_EXLIST(ip
, nex
, whichfork
);
399 if (whichfork
!= XFS_DATA_FORK
||
400 XFS_EXTFMT_INODE(ip
) == XFS_EXTFMT_NOSTATE
)
401 if (unlikely(xfs_check_nostate_extents(
403 XFS_ERROR_REPORT("xfs_iformat_extents(2)",
406 return -EFSCORRUPTED
;
409 ifp
->if_flags
|= XFS_IFEXTENTS
;
414 * The file has too many extents to fit into
415 * the inode, so they are in B-tree format.
416 * Allocate a buffer for the root of the B-tree
417 * and copy the root into it. The i_extents
418 * field will remain NULL until all of the
419 * extents are read in (when they are needed).
427 struct xfs_mount
*mp
= ip
->i_mount
;
428 xfs_bmdr_block_t
*dfp
;
435 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
436 dfp
= (xfs_bmdr_block_t
*)XFS_DFORK_PTR(dip
, whichfork
);
437 size
= XFS_BMAP_BROOT_SPACE(mp
, dfp
);
438 nrecs
= be16_to_cpu(dfp
->bb_numrecs
);
439 level
= be16_to_cpu(dfp
->bb_level
);
442 * blow out if -- fork has less extents than can fit in
443 * fork (fork shouldn't be a btree format), root btree
444 * block has more records than can fit into the fork,
445 * or the number of extents is greater than the number of
448 if (unlikely(XFS_IFORK_NEXTENTS(ip
, whichfork
) <=
449 XFS_IFORK_MAXEXT(ip
, whichfork
) ||
450 XFS_BMDR_SPACE_CALC(nrecs
) >
451 XFS_DFORK_SIZE(dip
, mp
, whichfork
) ||
452 XFS_IFORK_NEXTENTS(ip
, whichfork
) > ip
->i_d
.di_nblocks
) ||
453 level
== 0 || level
> XFS_BTREE_MAXLEVELS
) {
454 xfs_warn(mp
, "corrupt inode %Lu (btree).",
455 (unsigned long long) ip
->i_ino
);
456 XFS_CORRUPTION_ERROR("xfs_iformat_btree", XFS_ERRLEVEL_LOW
,
458 return -EFSCORRUPTED
;
461 ifp
->if_broot_bytes
= size
;
462 ifp
->if_broot
= kmem_alloc(size
, KM_SLEEP
| KM_NOFS
);
463 ASSERT(ifp
->if_broot
!= NULL
);
465 * Copy and convert from the on-disk structure
466 * to the in-memory structure.
468 xfs_bmdr_to_bmbt(ip
, dfp
, XFS_DFORK_SIZE(dip
, ip
->i_mount
, whichfork
),
469 ifp
->if_broot
, size
);
470 ifp
->if_flags
&= ~XFS_IFEXTENTS
;
471 ifp
->if_flags
|= XFS_IFBROOT
;
477 * Read in extents from a btree-format inode.
478 * Allocate and fill in if_extents. Real work is done in xfs_bmap.c.
488 xfs_extnum_t nextents
;
490 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
492 if (unlikely(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
493 XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW
,
495 return -EFSCORRUPTED
;
497 nextents
= XFS_IFORK_NEXTENTS(ip
, whichfork
);
498 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
501 * We know that the size is valid (it's checked in iformat_btree)
503 ifp
->if_bytes
= ifp
->if_real_bytes
= 0;
504 xfs_iext_add(ifp
, 0, nextents
);
505 error
= xfs_bmap_read_extents(tp
, ip
, whichfork
);
507 xfs_iext_destroy(ifp
);
510 xfs_validate_extents(ifp
, nextents
, XFS_EXTFMT_INODE(ip
));
511 ifp
->if_flags
|= XFS_IFEXTENTS
;
515 * Reallocate the space for if_broot based on the number of records
516 * being added or deleted as indicated in rec_diff. Move the records
517 * and pointers in if_broot to fit the new size. When shrinking this
518 * will eliminate holes between the records and pointers created by
519 * the caller. When growing this will create holes to be filled in
522 * The caller must not request to add more records than would fit in
523 * the on-disk inode root. If the if_broot is currently NULL, then
524 * if we are adding records, one will be allocated. The caller must also
525 * not request that the number of records go below zero, although
528 * ip -- the inode whose if_broot area is changing
529 * ext_diff -- the change in the number of records, positive or negative,
530 * requested for the if_broot array.
538 struct xfs_mount
*mp
= ip
->i_mount
;
541 struct xfs_btree_block
*new_broot
;
548 * Handle the degenerate case quietly.
554 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
557 * If there wasn't any memory allocated before, just
558 * allocate it now and get out.
560 if (ifp
->if_broot_bytes
== 0) {
561 new_size
= XFS_BMAP_BROOT_SPACE_CALC(mp
, rec_diff
);
562 ifp
->if_broot
= kmem_alloc(new_size
, KM_SLEEP
| KM_NOFS
);
563 ifp
->if_broot_bytes
= (int)new_size
;
568 * If there is already an existing if_broot, then we need
569 * to realloc() it and shift the pointers to their new
570 * location. The records don't change location because
571 * they are kept butted up against the btree block header.
573 cur_max
= xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0);
574 new_max
= cur_max
+ rec_diff
;
575 new_size
= XFS_BMAP_BROOT_SPACE_CALC(mp
, new_max
);
576 ifp
->if_broot
= kmem_realloc(ifp
->if_broot
, new_size
,
578 op
= (char *)XFS_BMAP_BROOT_PTR_ADDR(mp
, ifp
->if_broot
, 1,
579 ifp
->if_broot_bytes
);
580 np
= (char *)XFS_BMAP_BROOT_PTR_ADDR(mp
, ifp
->if_broot
, 1,
582 ifp
->if_broot_bytes
= (int)new_size
;
583 ASSERT(XFS_BMAP_BMDR_SPACE(ifp
->if_broot
) <=
584 XFS_IFORK_SIZE(ip
, whichfork
));
585 memmove(np
, op
, cur_max
* (uint
)sizeof(xfs_fsblock_t
));
590 * rec_diff is less than 0. In this case, we are shrinking the
591 * if_broot buffer. It must already exist. If we go to zero
592 * records, just get rid of the root and clear the status bit.
594 ASSERT((ifp
->if_broot
!= NULL
) && (ifp
->if_broot_bytes
> 0));
595 cur_max
= xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0);
596 new_max
= cur_max
+ rec_diff
;
597 ASSERT(new_max
>= 0);
599 new_size
= XFS_BMAP_BROOT_SPACE_CALC(mp
, new_max
);
603 new_broot
= kmem_alloc(new_size
, KM_SLEEP
| KM_NOFS
);
605 * First copy over the btree block header.
607 memcpy(new_broot
, ifp
->if_broot
,
608 XFS_BMBT_BLOCK_LEN(ip
->i_mount
));
611 ifp
->if_flags
&= ~XFS_IFBROOT
;
615 * Only copy the records and pointers if there are any.
619 * First copy the records.
621 op
= (char *)XFS_BMBT_REC_ADDR(mp
, ifp
->if_broot
, 1);
622 np
= (char *)XFS_BMBT_REC_ADDR(mp
, new_broot
, 1);
623 memcpy(np
, op
, new_max
* (uint
)sizeof(xfs_bmbt_rec_t
));
626 * Then copy the pointers.
628 op
= (char *)XFS_BMAP_BROOT_PTR_ADDR(mp
, ifp
->if_broot
, 1,
629 ifp
->if_broot_bytes
);
630 np
= (char *)XFS_BMAP_BROOT_PTR_ADDR(mp
, new_broot
, 1,
632 memcpy(np
, op
, new_max
* (uint
)sizeof(xfs_fsblock_t
));
634 kmem_free(ifp
->if_broot
);
635 ifp
->if_broot
= new_broot
;
636 ifp
->if_broot_bytes
= (int)new_size
;
638 ASSERT(XFS_BMAP_BMDR_SPACE(ifp
->if_broot
) <=
639 XFS_IFORK_SIZE(ip
, whichfork
));
645 * This is called when the amount of space needed for if_data
646 * is increased or decreased. The change in size is indicated by
647 * the number of bytes that need to be added or deleted in the
648 * byte_diff parameter.
650 * If the amount of space needed has decreased below the size of the
651 * inline buffer, then switch to using the inline buffer. Otherwise,
652 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
655 * ip -- the inode whose if_data area is changing
656 * byte_diff -- the change in the number of bytes, positive or negative,
657 * requested for the if_data array.
669 if (byte_diff
== 0) {
673 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
674 new_size
= (int)ifp
->if_bytes
+ byte_diff
;
675 ASSERT(new_size
>= 0);
678 if (ifp
->if_u1
.if_data
!= ifp
->if_u2
.if_inline_data
) {
679 kmem_free(ifp
->if_u1
.if_data
);
681 ifp
->if_u1
.if_data
= NULL
;
683 } else if (new_size
<= sizeof(ifp
->if_u2
.if_inline_data
)) {
685 * If the valid extents/data can fit in if_inline_ext/data,
686 * copy them from the malloc'd vector and free it.
688 if (ifp
->if_u1
.if_data
== NULL
) {
689 ifp
->if_u1
.if_data
= ifp
->if_u2
.if_inline_data
;
690 } else if (ifp
->if_u1
.if_data
!= ifp
->if_u2
.if_inline_data
) {
691 ASSERT(ifp
->if_real_bytes
!= 0);
692 memcpy(ifp
->if_u2
.if_inline_data
, ifp
->if_u1
.if_data
,
694 kmem_free(ifp
->if_u1
.if_data
);
695 ifp
->if_u1
.if_data
= ifp
->if_u2
.if_inline_data
;
700 * Stuck with malloc/realloc.
701 * For inline data, the underlying buffer must be
702 * a multiple of 4 bytes in size so that it can be
703 * logged and stay on word boundaries. We enforce
706 real_size
= roundup(new_size
, 4);
707 if (ifp
->if_u1
.if_data
== NULL
) {
708 ASSERT(ifp
->if_real_bytes
== 0);
709 ifp
->if_u1
.if_data
= kmem_alloc(real_size
,
711 } else if (ifp
->if_u1
.if_data
!= ifp
->if_u2
.if_inline_data
) {
713 * Only do the realloc if the underlying size
714 * is really changing.
716 if (ifp
->if_real_bytes
!= real_size
) {
718 kmem_realloc(ifp
->if_u1
.if_data
,
723 ASSERT(ifp
->if_real_bytes
== 0);
724 ifp
->if_u1
.if_data
= kmem_alloc(real_size
,
726 memcpy(ifp
->if_u1
.if_data
, ifp
->if_u2
.if_inline_data
,
730 ifp
->if_real_bytes
= real_size
;
731 ifp
->if_bytes
= new_size
;
732 ASSERT(ifp
->if_bytes
<= XFS_IFORK_SIZE(ip
, whichfork
));
742 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
743 if (ifp
->if_broot
!= NULL
) {
744 kmem_free(ifp
->if_broot
);
745 ifp
->if_broot
= NULL
;
749 * If the format is local, then we can't have an extents
750 * array so just look for an inline data array. If we're
751 * not local then we may or may not have an extents list,
752 * so check and free it up if we do.
754 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
755 if ((ifp
->if_u1
.if_data
!= ifp
->if_u2
.if_inline_data
) &&
756 (ifp
->if_u1
.if_data
!= NULL
)) {
757 ASSERT(ifp
->if_real_bytes
!= 0);
758 kmem_free(ifp
->if_u1
.if_data
);
759 ifp
->if_u1
.if_data
= NULL
;
760 ifp
->if_real_bytes
= 0;
762 } else if ((ifp
->if_flags
& XFS_IFEXTENTS
) &&
763 ((ifp
->if_flags
& XFS_IFEXTIREC
) ||
764 ((ifp
->if_u1
.if_extents
!= NULL
) &&
765 (ifp
->if_u1
.if_extents
!= ifp
->if_u2
.if_inline_ext
)))) {
766 ASSERT(ifp
->if_real_bytes
!= 0);
767 xfs_iext_destroy(ifp
);
769 ASSERT(ifp
->if_u1
.if_extents
== NULL
||
770 ifp
->if_u1
.if_extents
== ifp
->if_u2
.if_inline_ext
);
771 ASSERT(ifp
->if_real_bytes
== 0);
772 if (whichfork
== XFS_ATTR_FORK
) {
773 kmem_zone_free(xfs_ifork_zone
, ip
->i_afp
);
775 } else if (whichfork
== XFS_COW_FORK
) {
776 kmem_zone_free(xfs_ifork_zone
, ip
->i_cowfp
);
781 /* Count number of incore extents based on if_bytes */
783 xfs_iext_count(struct xfs_ifork
*ifp
)
785 return ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
789 * Convert in-core extents to on-disk form
791 * For either the data or attr fork in extent format, we need to endian convert
792 * the in-core extent as we place them into the on-disk inode.
794 * In the case of the data fork, the in-core and on-disk fork sizes can be
795 * different due to delayed allocation extents. We only copy on-disk extents
796 * here, so callers must always use the physical fork size to determine the
797 * size of the buffer passed to this routine. We will return the size actually
810 xfs_fsblock_t start_block
;
812 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
813 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
|XFS_ILOCK_SHARED
));
814 ASSERT(ifp
->if_bytes
> 0);
816 nrecs
= xfs_iext_count(ifp
);
817 XFS_BMAP_TRACE_EXLIST(ip
, nrecs
, whichfork
);
821 * There are some delayed allocation extents in the
822 * inode, so copy the extents one at a time and skip
823 * the delayed ones. There must be at least one
824 * non-delayed extent.
827 for (i
= 0; i
< nrecs
; i
++) {
828 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, i
);
829 start_block
= xfs_bmbt_get_startblock(ep
);
830 if (isnullstartblock(start_block
)) {
832 * It's a delayed allocation extent, so skip it.
837 /* Translate to on disk format */
838 put_unaligned_be64(ep
->l0
, &dp
->l0
);
839 put_unaligned_be64(ep
->l1
, &dp
->l1
);
844 xfs_validate_extents(ifp
, copied
, XFS_EXTFMT_INODE(ip
));
846 return (copied
* (uint
)sizeof(xfs_bmbt_rec_t
));
850 * Each of the following cases stores data into the same region
851 * of the on-disk inode, so only one of them can be valid at
852 * any given time. While it is possible to have conflicting formats
853 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
854 * in EXTENTS format, this can only happen when the fork has
855 * changed formats after being modified but before being flushed.
856 * In these cases, the format always takes precedence, because the
857 * format indicates the current state of the fork.
863 xfs_inode_log_item_t
*iip
,
869 static const short brootflag
[2] =
870 { XFS_ILOG_DBROOT
, XFS_ILOG_ABROOT
};
871 static const short dataflag
[2] =
872 { XFS_ILOG_DDATA
, XFS_ILOG_ADATA
};
873 static const short extflag
[2] =
874 { XFS_ILOG_DEXT
, XFS_ILOG_AEXT
};
878 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
880 * This can happen if we gave up in iformat in an error path,
881 * for the attribute fork.
884 ASSERT(whichfork
== XFS_ATTR_FORK
);
887 cp
= XFS_DFORK_PTR(dip
, whichfork
);
889 switch (XFS_IFORK_FORMAT(ip
, whichfork
)) {
890 case XFS_DINODE_FMT_LOCAL
:
891 if ((iip
->ili_fields
& dataflag
[whichfork
]) &&
892 (ifp
->if_bytes
> 0)) {
893 ASSERT(ifp
->if_u1
.if_data
!= NULL
);
894 ASSERT(ifp
->if_bytes
<= XFS_IFORK_SIZE(ip
, whichfork
));
895 memcpy(cp
, ifp
->if_u1
.if_data
, ifp
->if_bytes
);
899 case XFS_DINODE_FMT_EXTENTS
:
900 ASSERT((ifp
->if_flags
& XFS_IFEXTENTS
) ||
901 !(iip
->ili_fields
& extflag
[whichfork
]));
902 if ((iip
->ili_fields
& extflag
[whichfork
]) &&
903 (ifp
->if_bytes
> 0)) {
904 ASSERT(xfs_iext_get_ext(ifp
, 0));
905 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) > 0);
906 (void)xfs_iextents_copy(ip
, (xfs_bmbt_rec_t
*)cp
,
911 case XFS_DINODE_FMT_BTREE
:
912 if ((iip
->ili_fields
& brootflag
[whichfork
]) &&
913 (ifp
->if_broot_bytes
> 0)) {
914 ASSERT(ifp
->if_broot
!= NULL
);
915 ASSERT(XFS_BMAP_BMDR_SPACE(ifp
->if_broot
) <=
916 XFS_IFORK_SIZE(ip
, whichfork
));
917 xfs_bmbt_to_bmdr(mp
, ifp
->if_broot
, ifp
->if_broot_bytes
,
918 (xfs_bmdr_block_t
*)cp
,
919 XFS_DFORK_SIZE(dip
, mp
, whichfork
));
923 case XFS_DINODE_FMT_DEV
:
924 if (iip
->ili_fields
& XFS_ILOG_DEV
) {
925 ASSERT(whichfork
== XFS_DATA_FORK
);
926 xfs_dinode_put_rdev(dip
, ip
->i_df
.if_u2
.if_rdev
);
930 case XFS_DINODE_FMT_UUID
:
931 if (iip
->ili_fields
& XFS_ILOG_UUID
) {
932 ASSERT(whichfork
== XFS_DATA_FORK
);
933 memcpy(XFS_DFORK_DPTR(dip
),
934 &ip
->i_df
.if_u2
.if_uuid
,
946 * Return a pointer to the extent record at file index idx.
948 xfs_bmbt_rec_host_t
*
950 xfs_ifork_t
*ifp
, /* inode fork pointer */
951 xfs_extnum_t idx
) /* index of target extent */
954 ASSERT(idx
< xfs_iext_count(ifp
));
956 if ((ifp
->if_flags
& XFS_IFEXTIREC
) && (idx
== 0)) {
957 return ifp
->if_u1
.if_ext_irec
->er_extbuf
;
958 } else if (ifp
->if_flags
& XFS_IFEXTIREC
) {
959 xfs_ext_irec_t
*erp
; /* irec pointer */
960 int erp_idx
= 0; /* irec index */
961 xfs_extnum_t page_idx
= idx
; /* ext index in target list */
963 erp
= xfs_iext_idx_to_irec(ifp
, &page_idx
, &erp_idx
, 0);
964 return &erp
->er_extbuf
[page_idx
];
965 } else if (ifp
->if_bytes
) {
966 return &ifp
->if_u1
.if_extents
[idx
];
972 /* Convert bmap state flags to an inode fork. */
974 xfs_iext_state_to_fork(
975 struct xfs_inode
*ip
,
978 if (state
& BMAP_COWFORK
)
980 else if (state
& BMAP_ATTRFORK
)
986 * Insert new item(s) into the extent records for incore inode
987 * fork 'ifp'. 'count' new items are inserted at index 'idx'.
991 xfs_inode_t
*ip
, /* incore inode pointer */
992 xfs_extnum_t idx
, /* starting index of new items */
993 xfs_extnum_t count
, /* number of inserted items */
994 xfs_bmbt_irec_t
*new, /* items to insert */
995 int state
) /* type of extent conversion */
997 xfs_ifork_t
*ifp
= xfs_iext_state_to_fork(ip
, state
);
998 xfs_extnum_t i
; /* extent record index */
1000 trace_xfs_iext_insert(ip
, idx
, new, state
, _RET_IP_
);
1002 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1003 xfs_iext_add(ifp
, idx
, count
);
1004 for (i
= idx
; i
< idx
+ count
; i
++, new++)
1005 xfs_bmbt_set_all(xfs_iext_get_ext(ifp
, i
), new);
1009 * This is called when the amount of space required for incore file
1010 * extents needs to be increased. The ext_diff parameter stores the
1011 * number of new extents being added and the idx parameter contains
1012 * the extent index where the new extents will be added. If the new
1013 * extents are being appended, then we just need to (re)allocate and
1014 * initialize the space. Otherwise, if the new extents are being
1015 * inserted into the middle of the existing entries, a bit more work
1016 * is required to make room for the new extents to be inserted. The
1017 * caller is responsible for filling in the new extent entries upon
1022 xfs_ifork_t
*ifp
, /* inode fork pointer */
1023 xfs_extnum_t idx
, /* index to begin adding exts */
1024 int ext_diff
) /* number of extents to add */
1026 int byte_diff
; /* new bytes being added */
1027 int new_size
; /* size of extents after adding */
1028 xfs_extnum_t nextents
; /* number of extents in file */
1030 nextents
= xfs_iext_count(ifp
);
1031 ASSERT((idx
>= 0) && (idx
<= nextents
));
1032 byte_diff
= ext_diff
* sizeof(xfs_bmbt_rec_t
);
1033 new_size
= ifp
->if_bytes
+ byte_diff
;
1035 * If the new number of extents (nextents + ext_diff)
1036 * fits inside the inode, then continue to use the inline
1039 if (nextents
+ ext_diff
<= XFS_INLINE_EXTS
) {
1040 if (idx
< nextents
) {
1041 memmove(&ifp
->if_u2
.if_inline_ext
[idx
+ ext_diff
],
1042 &ifp
->if_u2
.if_inline_ext
[idx
],
1043 (nextents
- idx
) * sizeof(xfs_bmbt_rec_t
));
1044 memset(&ifp
->if_u2
.if_inline_ext
[idx
], 0, byte_diff
);
1046 ifp
->if_u1
.if_extents
= ifp
->if_u2
.if_inline_ext
;
1047 ifp
->if_real_bytes
= 0;
1050 * Otherwise use a linear (direct) extent list.
1051 * If the extents are currently inside the inode,
1052 * xfs_iext_realloc_direct will switch us from
1053 * inline to direct extent allocation mode.
1055 else if (nextents
+ ext_diff
<= XFS_LINEAR_EXTS
) {
1056 xfs_iext_realloc_direct(ifp
, new_size
);
1057 if (idx
< nextents
) {
1058 memmove(&ifp
->if_u1
.if_extents
[idx
+ ext_diff
],
1059 &ifp
->if_u1
.if_extents
[idx
],
1060 (nextents
- idx
) * sizeof(xfs_bmbt_rec_t
));
1061 memset(&ifp
->if_u1
.if_extents
[idx
], 0, byte_diff
);
1064 /* Indirection array */
1066 xfs_ext_irec_t
*erp
;
1070 ASSERT(nextents
+ ext_diff
> XFS_LINEAR_EXTS
);
1071 if (ifp
->if_flags
& XFS_IFEXTIREC
) {
1072 erp
= xfs_iext_idx_to_irec(ifp
, &page_idx
, &erp_idx
, 1);
1074 xfs_iext_irec_init(ifp
);
1075 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1076 erp
= ifp
->if_u1
.if_ext_irec
;
1078 /* Extents fit in target extent page */
1079 if (erp
&& erp
->er_extcount
+ ext_diff
<= XFS_LINEAR_EXTS
) {
1080 if (page_idx
< erp
->er_extcount
) {
1081 memmove(&erp
->er_extbuf
[page_idx
+ ext_diff
],
1082 &erp
->er_extbuf
[page_idx
],
1083 (erp
->er_extcount
- page_idx
) *
1084 sizeof(xfs_bmbt_rec_t
));
1085 memset(&erp
->er_extbuf
[page_idx
], 0, byte_diff
);
1087 erp
->er_extcount
+= ext_diff
;
1088 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, ext_diff
);
1090 /* Insert a new extent page */
1092 xfs_iext_add_indirect_multi(ifp
,
1093 erp_idx
, page_idx
, ext_diff
);
1096 * If extent(s) are being appended to the last page in
1097 * the indirection array and the new extent(s) don't fit
1098 * in the page, then erp is NULL and erp_idx is set to
1099 * the next index needed in the indirection array.
1102 uint count
= ext_diff
;
1105 erp
= xfs_iext_irec_new(ifp
, erp_idx
);
1106 erp
->er_extcount
= min(count
, XFS_LINEAR_EXTS
);
1107 count
-= erp
->er_extcount
;
1113 ifp
->if_bytes
= new_size
;
1117 * This is called when incore extents are being added to the indirection
1118 * array and the new extents do not fit in the target extent list. The
1119 * erp_idx parameter contains the irec index for the target extent list
1120 * in the indirection array, and the idx parameter contains the extent
1121 * index within the list. The number of extents being added is stored
1122 * in the count parameter.
1124 * |-------| |-------|
1125 * | | | | idx - number of extents before idx
1127 * | | | | count - number of extents being inserted at idx
1128 * |-------| |-------|
1129 * | count | | nex2 | nex2 - number of extents after idx + count
1130 * |-------| |-------|
1133 xfs_iext_add_indirect_multi(
1134 xfs_ifork_t
*ifp
, /* inode fork pointer */
1135 int erp_idx
, /* target extent irec index */
1136 xfs_extnum_t idx
, /* index within target list */
1137 int count
) /* new extents being added */
1139 int byte_diff
; /* new bytes being added */
1140 xfs_ext_irec_t
*erp
; /* pointer to irec entry */
1141 xfs_extnum_t ext_diff
; /* number of extents to add */
1142 xfs_extnum_t ext_cnt
; /* new extents still needed */
1143 xfs_extnum_t nex2
; /* extents after idx + count */
1144 xfs_bmbt_rec_t
*nex2_ep
= NULL
; /* temp list for nex2 extents */
1145 int nlists
; /* number of irec's (lists) */
1147 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1148 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1149 nex2
= erp
->er_extcount
- idx
;
1150 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1153 * Save second part of target extent list
1154 * (all extents past */
1156 byte_diff
= nex2
* sizeof(xfs_bmbt_rec_t
);
1157 nex2_ep
= (xfs_bmbt_rec_t
*) kmem_alloc(byte_diff
, KM_NOFS
);
1158 memmove(nex2_ep
, &erp
->er_extbuf
[idx
], byte_diff
);
1159 erp
->er_extcount
-= nex2
;
1160 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, -nex2
);
1161 memset(&erp
->er_extbuf
[idx
], 0, byte_diff
);
1165 * Add the new extents to the end of the target
1166 * list, then allocate new irec record(s) and
1167 * extent buffer(s) as needed to store the rest
1168 * of the new extents.
1171 ext_diff
= MIN(ext_cnt
, (int)XFS_LINEAR_EXTS
- erp
->er_extcount
);
1173 erp
->er_extcount
+= ext_diff
;
1174 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, ext_diff
);
1175 ext_cnt
-= ext_diff
;
1179 erp
= xfs_iext_irec_new(ifp
, erp_idx
);
1180 ext_diff
= MIN(ext_cnt
, (int)XFS_LINEAR_EXTS
);
1181 erp
->er_extcount
= ext_diff
;
1182 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, ext_diff
);
1183 ext_cnt
-= ext_diff
;
1186 /* Add nex2 extents back to indirection array */
1188 xfs_extnum_t ext_avail
;
1191 byte_diff
= nex2
* sizeof(xfs_bmbt_rec_t
);
1192 ext_avail
= XFS_LINEAR_EXTS
- erp
->er_extcount
;
1195 * If nex2 extents fit in the current page, append
1196 * nex2_ep after the new extents.
1198 if (nex2
<= ext_avail
) {
1199 i
= erp
->er_extcount
;
1202 * Otherwise, check if space is available in the
1205 else if ((erp_idx
< nlists
- 1) &&
1206 (nex2
<= (ext_avail
= XFS_LINEAR_EXTS
-
1207 ifp
->if_u1
.if_ext_irec
[erp_idx
+1].er_extcount
))) {
1210 /* Create a hole for nex2 extents */
1211 memmove(&erp
->er_extbuf
[nex2
], erp
->er_extbuf
,
1212 erp
->er_extcount
* sizeof(xfs_bmbt_rec_t
));
1215 * Final choice, create a new extent page for
1220 erp
= xfs_iext_irec_new(ifp
, erp_idx
);
1222 memmove(&erp
->er_extbuf
[i
], nex2_ep
, byte_diff
);
1224 erp
->er_extcount
+= nex2
;
1225 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, nex2
);
1230 * This is called when the amount of space required for incore file
1231 * extents needs to be decreased. The ext_diff parameter stores the
1232 * number of extents to be removed and the idx parameter contains
1233 * the extent index where the extents will be removed from.
1235 * If the amount of space needed has decreased below the linear
1236 * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
1237 * extent array. Otherwise, use kmem_realloc() to adjust the
1238 * size to what is needed.
1242 xfs_inode_t
*ip
, /* incore inode pointer */
1243 xfs_extnum_t idx
, /* index to begin removing exts */
1244 int ext_diff
, /* number of extents to remove */
1245 int state
) /* type of extent conversion */
1247 xfs_ifork_t
*ifp
= xfs_iext_state_to_fork(ip
, state
);
1248 xfs_extnum_t nextents
; /* number of extents in file */
1249 int new_size
; /* size of extents after removal */
1251 trace_xfs_iext_remove(ip
, idx
, state
, _RET_IP_
);
1253 ASSERT(ext_diff
> 0);
1254 nextents
= xfs_iext_count(ifp
);
1255 new_size
= (nextents
- ext_diff
) * sizeof(xfs_bmbt_rec_t
);
1257 if (new_size
== 0) {
1258 xfs_iext_destroy(ifp
);
1259 } else if (ifp
->if_flags
& XFS_IFEXTIREC
) {
1260 xfs_iext_remove_indirect(ifp
, idx
, ext_diff
);
1261 } else if (ifp
->if_real_bytes
) {
1262 xfs_iext_remove_direct(ifp
, idx
, ext_diff
);
1264 xfs_iext_remove_inline(ifp
, idx
, ext_diff
);
1266 ifp
->if_bytes
= new_size
;
1270 * This removes ext_diff extents from the inline buffer, beginning
1271 * at extent index idx.
1274 xfs_iext_remove_inline(
1275 xfs_ifork_t
*ifp
, /* inode fork pointer */
1276 xfs_extnum_t idx
, /* index to begin removing exts */
1277 int ext_diff
) /* number of extents to remove */
1279 int nextents
; /* number of extents in file */
1281 ASSERT(!(ifp
->if_flags
& XFS_IFEXTIREC
));
1282 ASSERT(idx
< XFS_INLINE_EXTS
);
1283 nextents
= xfs_iext_count(ifp
);
1284 ASSERT(((nextents
- ext_diff
) > 0) &&
1285 (nextents
- ext_diff
) < XFS_INLINE_EXTS
);
1287 if (idx
+ ext_diff
< nextents
) {
1288 memmove(&ifp
->if_u2
.if_inline_ext
[idx
],
1289 &ifp
->if_u2
.if_inline_ext
[idx
+ ext_diff
],
1290 (nextents
- (idx
+ ext_diff
)) *
1291 sizeof(xfs_bmbt_rec_t
));
1292 memset(&ifp
->if_u2
.if_inline_ext
[nextents
- ext_diff
],
1293 0, ext_diff
* sizeof(xfs_bmbt_rec_t
));
1295 memset(&ifp
->if_u2
.if_inline_ext
[idx
], 0,
1296 ext_diff
* sizeof(xfs_bmbt_rec_t
));
1301 * This removes ext_diff extents from a linear (direct) extent list,
1302 * beginning at extent index idx. If the extents are being removed
1303 * from the end of the list (ie. truncate) then we just need to re-
1304 * allocate the list to remove the extra space. Otherwise, if the
1305 * extents are being removed from the middle of the existing extent
1306 * entries, then we first need to move the extent records beginning
1307 * at idx + ext_diff up in the list to overwrite the records being
1308 * removed, then remove the extra space via kmem_realloc.
1311 xfs_iext_remove_direct(
1312 xfs_ifork_t
*ifp
, /* inode fork pointer */
1313 xfs_extnum_t idx
, /* index to begin removing exts */
1314 int ext_diff
) /* number of extents to remove */
1316 xfs_extnum_t nextents
; /* number of extents in file */
1317 int new_size
; /* size of extents after removal */
1319 ASSERT(!(ifp
->if_flags
& XFS_IFEXTIREC
));
1320 new_size
= ifp
->if_bytes
-
1321 (ext_diff
* sizeof(xfs_bmbt_rec_t
));
1322 nextents
= xfs_iext_count(ifp
);
1324 if (new_size
== 0) {
1325 xfs_iext_destroy(ifp
);
1328 /* Move extents up in the list (if needed) */
1329 if (idx
+ ext_diff
< nextents
) {
1330 memmove(&ifp
->if_u1
.if_extents
[idx
],
1331 &ifp
->if_u1
.if_extents
[idx
+ ext_diff
],
1332 (nextents
- (idx
+ ext_diff
)) *
1333 sizeof(xfs_bmbt_rec_t
));
1335 memset(&ifp
->if_u1
.if_extents
[nextents
- ext_diff
],
1336 0, ext_diff
* sizeof(xfs_bmbt_rec_t
));
1338 * Reallocate the direct extent list. If the extents
1339 * will fit inside the inode then xfs_iext_realloc_direct
1340 * will switch from direct to inline extent allocation
1343 xfs_iext_realloc_direct(ifp
, new_size
);
1344 ifp
->if_bytes
= new_size
;
1348 * This is called when incore extents are being removed from the
1349 * indirection array and the extents being removed span multiple extent
1350 * buffers. The idx parameter contains the file extent index where we
1351 * want to begin removing extents, and the count parameter contains
1352 * how many extents need to be removed.
1354 * |-------| |-------|
1355 * | nex1 | | | nex1 - number of extents before idx
1356 * |-------| | count |
1357 * | | | | count - number of extents being removed at idx
1358 * | count | |-------|
1359 * | | | nex2 | nex2 - number of extents after idx + count
1360 * |-------| |-------|
1363 xfs_iext_remove_indirect(
1364 xfs_ifork_t
*ifp
, /* inode fork pointer */
1365 xfs_extnum_t idx
, /* index to begin removing extents */
1366 int count
) /* number of extents to remove */
1368 xfs_ext_irec_t
*erp
; /* indirection array pointer */
1369 int erp_idx
= 0; /* indirection array index */
1370 xfs_extnum_t ext_cnt
; /* extents left to remove */
1371 xfs_extnum_t ext_diff
; /* extents to remove in current list */
1372 xfs_extnum_t nex1
; /* number of extents before idx */
1373 xfs_extnum_t nex2
; /* extents after idx + count */
1374 int page_idx
= idx
; /* index in target extent list */
1376 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1377 erp
= xfs_iext_idx_to_irec(ifp
, &page_idx
, &erp_idx
, 0);
1378 ASSERT(erp
!= NULL
);
1382 nex2
= MAX((erp
->er_extcount
- (nex1
+ ext_cnt
)), 0);
1383 ext_diff
= MIN(ext_cnt
, (erp
->er_extcount
- nex1
));
1385 * Check for deletion of entire list;
1386 * xfs_iext_irec_remove() updates extent offsets.
1388 if (ext_diff
== erp
->er_extcount
) {
1389 xfs_iext_irec_remove(ifp
, erp_idx
);
1390 ext_cnt
-= ext_diff
;
1393 ASSERT(erp_idx
< ifp
->if_real_bytes
/
1395 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1402 /* Move extents up (if needed) */
1404 memmove(&erp
->er_extbuf
[nex1
],
1405 &erp
->er_extbuf
[nex1
+ ext_diff
],
1406 nex2
* sizeof(xfs_bmbt_rec_t
));
1408 /* Zero out rest of page */
1409 memset(&erp
->er_extbuf
[nex1
+ nex2
], 0, (XFS_IEXT_BUFSZ
-
1410 ((nex1
+ nex2
) * sizeof(xfs_bmbt_rec_t
))));
1411 /* Update remaining counters */
1412 erp
->er_extcount
-= ext_diff
;
1413 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, -ext_diff
);
1414 ext_cnt
-= ext_diff
;
1419 ifp
->if_bytes
-= count
* sizeof(xfs_bmbt_rec_t
);
1420 xfs_iext_irec_compact(ifp
);
1424 * Create, destroy, or resize a linear (direct) block of extents.
1427 xfs_iext_realloc_direct(
1428 xfs_ifork_t
*ifp
, /* inode fork pointer */
1429 int new_size
) /* new size of extents after adding */
1431 int rnew_size
; /* real new size of extents */
1433 rnew_size
= new_size
;
1435 ASSERT(!(ifp
->if_flags
& XFS_IFEXTIREC
) ||
1436 ((new_size
>= 0) && (new_size
<= XFS_IEXT_BUFSZ
) &&
1437 (new_size
!= ifp
->if_real_bytes
)));
1439 /* Free extent records */
1440 if (new_size
== 0) {
1441 xfs_iext_destroy(ifp
);
1443 /* Resize direct extent list and zero any new bytes */
1444 else if (ifp
->if_real_bytes
) {
1445 /* Check if extents will fit inside the inode */
1446 if (new_size
<= XFS_INLINE_EXTS
* sizeof(xfs_bmbt_rec_t
)) {
1447 xfs_iext_direct_to_inline(ifp
, new_size
/
1448 (uint
)sizeof(xfs_bmbt_rec_t
));
1449 ifp
->if_bytes
= new_size
;
1452 if (!is_power_of_2(new_size
)){
1453 rnew_size
= roundup_pow_of_two(new_size
);
1455 if (rnew_size
!= ifp
->if_real_bytes
) {
1456 ifp
->if_u1
.if_extents
=
1457 kmem_realloc(ifp
->if_u1
.if_extents
,
1458 rnew_size
, KM_NOFS
);
1460 if (rnew_size
> ifp
->if_real_bytes
) {
1461 memset(&ifp
->if_u1
.if_extents
[ifp
->if_bytes
/
1462 (uint
)sizeof(xfs_bmbt_rec_t
)], 0,
1463 rnew_size
- ifp
->if_real_bytes
);
1466 /* Switch from the inline extent buffer to a direct extent list */
1468 if (!is_power_of_2(new_size
)) {
1469 rnew_size
= roundup_pow_of_two(new_size
);
1471 xfs_iext_inline_to_direct(ifp
, rnew_size
);
1473 ifp
->if_real_bytes
= rnew_size
;
1474 ifp
->if_bytes
= new_size
;
1478 * Switch from linear (direct) extent records to inline buffer.
1481 xfs_iext_direct_to_inline(
1482 xfs_ifork_t
*ifp
, /* inode fork pointer */
1483 xfs_extnum_t nextents
) /* number of extents in file */
1485 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1486 ASSERT(nextents
<= XFS_INLINE_EXTS
);
1488 * The inline buffer was zeroed when we switched
1489 * from inline to direct extent allocation mode,
1490 * so we don't need to clear it here.
1492 memcpy(ifp
->if_u2
.if_inline_ext
, ifp
->if_u1
.if_extents
,
1493 nextents
* sizeof(xfs_bmbt_rec_t
));
1494 kmem_free(ifp
->if_u1
.if_extents
);
1495 ifp
->if_u1
.if_extents
= ifp
->if_u2
.if_inline_ext
;
1496 ifp
->if_real_bytes
= 0;
1500 * Switch from inline buffer to linear (direct) extent records.
1501 * new_size should already be rounded up to the next power of 2
1502 * by the caller (when appropriate), so use new_size as it is.
1503 * However, since new_size may be rounded up, we can't update
1504 * if_bytes here. It is the caller's responsibility to update
1505 * if_bytes upon return.
1508 xfs_iext_inline_to_direct(
1509 xfs_ifork_t
*ifp
, /* inode fork pointer */
1510 int new_size
) /* number of extents in file */
1512 ifp
->if_u1
.if_extents
= kmem_alloc(new_size
, KM_NOFS
);
1513 memset(ifp
->if_u1
.if_extents
, 0, new_size
);
1514 if (ifp
->if_bytes
) {
1515 memcpy(ifp
->if_u1
.if_extents
, ifp
->if_u2
.if_inline_ext
,
1517 memset(ifp
->if_u2
.if_inline_ext
, 0, XFS_INLINE_EXTS
*
1518 sizeof(xfs_bmbt_rec_t
));
1520 ifp
->if_real_bytes
= new_size
;
1524 * Resize an extent indirection array to new_size bytes.
1527 xfs_iext_realloc_indirect(
1528 xfs_ifork_t
*ifp
, /* inode fork pointer */
1529 int new_size
) /* new indirection array size */
1531 int nlists
; /* number of irec's (ex lists) */
1532 int size
; /* current indirection array size */
1534 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1535 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1536 size
= nlists
* sizeof(xfs_ext_irec_t
);
1537 ASSERT(ifp
->if_real_bytes
);
1538 ASSERT((new_size
>= 0) && (new_size
!= size
));
1539 if (new_size
== 0) {
1540 xfs_iext_destroy(ifp
);
1542 ifp
->if_u1
.if_ext_irec
=
1543 kmem_realloc(ifp
->if_u1
.if_ext_irec
, new_size
, KM_NOFS
);
1548 * Switch from indirection array to linear (direct) extent allocations.
1551 xfs_iext_indirect_to_direct(
1552 xfs_ifork_t
*ifp
) /* inode fork pointer */
1554 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1555 xfs_extnum_t nextents
; /* number of extents in file */
1556 int size
; /* size of file extents */
1558 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1559 nextents
= xfs_iext_count(ifp
);
1560 ASSERT(nextents
<= XFS_LINEAR_EXTS
);
1561 size
= nextents
* sizeof(xfs_bmbt_rec_t
);
1563 xfs_iext_irec_compact_pages(ifp
);
1564 ASSERT(ifp
->if_real_bytes
== XFS_IEXT_BUFSZ
);
1566 ep
= ifp
->if_u1
.if_ext_irec
->er_extbuf
;
1567 kmem_free(ifp
->if_u1
.if_ext_irec
);
1568 ifp
->if_flags
&= ~XFS_IFEXTIREC
;
1569 ifp
->if_u1
.if_extents
= ep
;
1570 ifp
->if_bytes
= size
;
1571 if (nextents
< XFS_LINEAR_EXTS
) {
1572 xfs_iext_realloc_direct(ifp
, size
);
1577 * Remove all records from the indirection array.
1580 xfs_iext_irec_remove_all(
1581 struct xfs_ifork
*ifp
)
1586 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1587 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1588 for (i
= 0; i
< nlists
; i
++)
1589 kmem_free(ifp
->if_u1
.if_ext_irec
[i
].er_extbuf
);
1590 kmem_free(ifp
->if_u1
.if_ext_irec
);
1591 ifp
->if_flags
&= ~XFS_IFEXTIREC
;
1595 * Free incore file extents.
1599 xfs_ifork_t
*ifp
) /* inode fork pointer */
1601 if (ifp
->if_flags
& XFS_IFEXTIREC
) {
1602 xfs_iext_irec_remove_all(ifp
);
1603 } else if (ifp
->if_real_bytes
) {
1604 kmem_free(ifp
->if_u1
.if_extents
);
1605 } else if (ifp
->if_bytes
) {
1606 memset(ifp
->if_u2
.if_inline_ext
, 0, XFS_INLINE_EXTS
*
1607 sizeof(xfs_bmbt_rec_t
));
1609 ifp
->if_u1
.if_extents
= NULL
;
1610 ifp
->if_real_bytes
= 0;
1615 * Return a pointer to the extent record for file system block bno.
1617 xfs_bmbt_rec_host_t
* /* pointer to found extent record */
1618 xfs_iext_bno_to_ext(
1619 xfs_ifork_t
*ifp
, /* inode fork pointer */
1620 xfs_fileoff_t bno
, /* block number to search for */
1621 xfs_extnum_t
*idxp
) /* index of target extent */
1623 xfs_bmbt_rec_host_t
*base
; /* pointer to first extent */
1624 xfs_filblks_t blockcount
= 0; /* number of blocks in extent */
1625 xfs_bmbt_rec_host_t
*ep
= NULL
; /* pointer to target extent */
1626 xfs_ext_irec_t
*erp
= NULL
; /* indirection array pointer */
1627 int high
; /* upper boundary in search */
1628 xfs_extnum_t idx
= 0; /* index of target extent */
1629 int low
; /* lower boundary in search */
1630 xfs_extnum_t nextents
; /* number of file extents */
1631 xfs_fileoff_t startoff
= 0; /* start offset of extent */
1633 nextents
= xfs_iext_count(ifp
);
1634 if (nextents
== 0) {
1639 if (ifp
->if_flags
& XFS_IFEXTIREC
) {
1640 /* Find target extent list */
1642 erp
= xfs_iext_bno_to_irec(ifp
, bno
, &erp_idx
);
1643 base
= erp
->er_extbuf
;
1644 high
= erp
->er_extcount
- 1;
1646 base
= ifp
->if_u1
.if_extents
;
1647 high
= nextents
- 1;
1649 /* Binary search extent records */
1650 while (low
<= high
) {
1651 idx
= (low
+ high
) >> 1;
1653 startoff
= xfs_bmbt_get_startoff(ep
);
1654 blockcount
= xfs_bmbt_get_blockcount(ep
);
1655 if (bno
< startoff
) {
1657 } else if (bno
>= startoff
+ blockcount
) {
1660 /* Convert back to file-based extent index */
1661 if (ifp
->if_flags
& XFS_IFEXTIREC
) {
1662 idx
+= erp
->er_extoff
;
1668 /* Convert back to file-based extent index */
1669 if (ifp
->if_flags
& XFS_IFEXTIREC
) {
1670 idx
+= erp
->er_extoff
;
1672 if (bno
>= startoff
+ blockcount
) {
1673 if (++idx
== nextents
) {
1676 ep
= xfs_iext_get_ext(ifp
, idx
);
1684 * Return a pointer to the indirection array entry containing the
1685 * extent record for filesystem block bno. Store the index of the
1686 * target irec in *erp_idxp.
1688 xfs_ext_irec_t
* /* pointer to found extent record */
1689 xfs_iext_bno_to_irec(
1690 xfs_ifork_t
*ifp
, /* inode fork pointer */
1691 xfs_fileoff_t bno
, /* block number to search for */
1692 int *erp_idxp
) /* irec index of target ext list */
1694 xfs_ext_irec_t
*erp
= NULL
; /* indirection array pointer */
1695 xfs_ext_irec_t
*erp_next
; /* next indirection array entry */
1696 int erp_idx
; /* indirection array index */
1697 int nlists
; /* number of extent irec's (lists) */
1698 int high
; /* binary search upper limit */
1699 int low
; /* binary search lower limit */
1701 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1702 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1706 while (low
<= high
) {
1707 erp_idx
= (low
+ high
) >> 1;
1708 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1709 erp_next
= erp_idx
< nlists
- 1 ? erp
+ 1 : NULL
;
1710 if (bno
< xfs_bmbt_get_startoff(erp
->er_extbuf
)) {
1712 } else if (erp_next
&& bno
>=
1713 xfs_bmbt_get_startoff(erp_next
->er_extbuf
)) {
1719 *erp_idxp
= erp_idx
;
1724 * Return a pointer to the indirection array entry containing the
1725 * extent record at file extent index *idxp. Store the index of the
1726 * target irec in *erp_idxp and store the page index of the target
1727 * extent record in *idxp.
1730 xfs_iext_idx_to_irec(
1731 xfs_ifork_t
*ifp
, /* inode fork pointer */
1732 xfs_extnum_t
*idxp
, /* extent index (file -> page) */
1733 int *erp_idxp
, /* pointer to target irec */
1734 int realloc
) /* new bytes were just added */
1736 xfs_ext_irec_t
*prev
; /* pointer to previous irec */
1737 xfs_ext_irec_t
*erp
= NULL
; /* pointer to current irec */
1738 int erp_idx
; /* indirection array index */
1739 int nlists
; /* number of irec's (ex lists) */
1740 int high
; /* binary search upper limit */
1741 int low
; /* binary search lower limit */
1742 xfs_extnum_t page_idx
= *idxp
; /* extent index in target list */
1744 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1745 ASSERT(page_idx
>= 0);
1746 ASSERT(page_idx
<= xfs_iext_count(ifp
));
1747 ASSERT(page_idx
< xfs_iext_count(ifp
) || realloc
);
1749 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1754 /* Binary search extent irec's */
1755 while (low
<= high
) {
1756 erp_idx
= (low
+ high
) >> 1;
1757 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1758 prev
= erp_idx
> 0 ? erp
- 1 : NULL
;
1759 if (page_idx
< erp
->er_extoff
|| (page_idx
== erp
->er_extoff
&&
1760 realloc
&& prev
&& prev
->er_extcount
< XFS_LINEAR_EXTS
)) {
1762 } else if (page_idx
> erp
->er_extoff
+ erp
->er_extcount
||
1763 (page_idx
== erp
->er_extoff
+ erp
->er_extcount
&&
1766 } else if (page_idx
== erp
->er_extoff
+ erp
->er_extcount
&&
1767 erp
->er_extcount
== XFS_LINEAR_EXTS
) {
1771 erp
= erp_idx
< nlists
? erp
+ 1 : NULL
;
1774 page_idx
-= erp
->er_extoff
;
1779 *erp_idxp
= erp_idx
;
1784 * Allocate and initialize an indirection array once the space needed
1785 * for incore extents increases above XFS_IEXT_BUFSZ.
1789 xfs_ifork_t
*ifp
) /* inode fork pointer */
1791 xfs_ext_irec_t
*erp
; /* indirection array pointer */
1792 xfs_extnum_t nextents
; /* number of extents in file */
1794 ASSERT(!(ifp
->if_flags
& XFS_IFEXTIREC
));
1795 nextents
= xfs_iext_count(ifp
);
1796 ASSERT(nextents
<= XFS_LINEAR_EXTS
);
1798 erp
= kmem_alloc(sizeof(xfs_ext_irec_t
), KM_NOFS
);
1800 if (nextents
== 0) {
1801 ifp
->if_u1
.if_extents
= kmem_alloc(XFS_IEXT_BUFSZ
, KM_NOFS
);
1802 } else if (!ifp
->if_real_bytes
) {
1803 xfs_iext_inline_to_direct(ifp
, XFS_IEXT_BUFSZ
);
1804 } else if (ifp
->if_real_bytes
< XFS_IEXT_BUFSZ
) {
1805 xfs_iext_realloc_direct(ifp
, XFS_IEXT_BUFSZ
);
1807 erp
->er_extbuf
= ifp
->if_u1
.if_extents
;
1808 erp
->er_extcount
= nextents
;
1811 ifp
->if_flags
|= XFS_IFEXTIREC
;
1812 ifp
->if_real_bytes
= XFS_IEXT_BUFSZ
;
1813 ifp
->if_bytes
= nextents
* sizeof(xfs_bmbt_rec_t
);
1814 ifp
->if_u1
.if_ext_irec
= erp
;
1820 * Allocate and initialize a new entry in the indirection array.
1824 xfs_ifork_t
*ifp
, /* inode fork pointer */
1825 int erp_idx
) /* index for new irec */
1827 xfs_ext_irec_t
*erp
; /* indirection array pointer */
1828 int i
; /* loop counter */
1829 int nlists
; /* number of irec's (ex lists) */
1831 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1832 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1834 /* Resize indirection array */
1835 xfs_iext_realloc_indirect(ifp
, ++nlists
*
1836 sizeof(xfs_ext_irec_t
));
1838 * Move records down in the array so the
1839 * new page can use erp_idx.
1841 erp
= ifp
->if_u1
.if_ext_irec
;
1842 for (i
= nlists
- 1; i
> erp_idx
; i
--) {
1843 memmove(&erp
[i
], &erp
[i
-1], sizeof(xfs_ext_irec_t
));
1845 ASSERT(i
== erp_idx
);
1847 /* Initialize new extent record */
1848 erp
= ifp
->if_u1
.if_ext_irec
;
1849 erp
[erp_idx
].er_extbuf
= kmem_alloc(XFS_IEXT_BUFSZ
, KM_NOFS
);
1850 ifp
->if_real_bytes
= nlists
* XFS_IEXT_BUFSZ
;
1851 memset(erp
[erp_idx
].er_extbuf
, 0, XFS_IEXT_BUFSZ
);
1852 erp
[erp_idx
].er_extcount
= 0;
1853 erp
[erp_idx
].er_extoff
= erp_idx
> 0 ?
1854 erp
[erp_idx
-1].er_extoff
+ erp
[erp_idx
-1].er_extcount
: 0;
1855 return (&erp
[erp_idx
]);
1859 * Remove a record from the indirection array.
1862 xfs_iext_irec_remove(
1863 xfs_ifork_t
*ifp
, /* inode fork pointer */
1864 int erp_idx
) /* irec index to remove */
1866 xfs_ext_irec_t
*erp
; /* indirection array pointer */
1867 int i
; /* loop counter */
1868 int nlists
; /* number of irec's (ex lists) */
1870 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1871 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1872 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1873 if (erp
->er_extbuf
) {
1874 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1,
1876 kmem_free(erp
->er_extbuf
);
1878 /* Compact extent records */
1879 erp
= ifp
->if_u1
.if_ext_irec
;
1880 for (i
= erp_idx
; i
< nlists
- 1; i
++) {
1881 memmove(&erp
[i
], &erp
[i
+1], sizeof(xfs_ext_irec_t
));
1884 * Manually free the last extent record from the indirection
1885 * array. A call to xfs_iext_realloc_indirect() with a size
1886 * of zero would result in a call to xfs_iext_destroy() which
1887 * would in turn call this function again, creating a nasty
1891 xfs_iext_realloc_indirect(ifp
,
1892 nlists
* sizeof(xfs_ext_irec_t
));
1894 kmem_free(ifp
->if_u1
.if_ext_irec
);
1896 ifp
->if_real_bytes
= nlists
* XFS_IEXT_BUFSZ
;
1900 * This is called to clean up large amounts of unused memory allocated
1901 * by the indirection array. Before compacting anything though, verify
1902 * that the indirection array is still needed and switch back to the
1903 * linear extent list (or even the inline buffer) if possible. The
1904 * compaction policy is as follows:
1906 * Full Compaction: Extents fit into a single page (or inline buffer)
1907 * Partial Compaction: Extents occupy less than 50% of allocated space
1908 * No Compaction: Extents occupy at least 50% of allocated space
1911 xfs_iext_irec_compact(
1912 xfs_ifork_t
*ifp
) /* inode fork pointer */
1914 xfs_extnum_t nextents
; /* number of extents in file */
1915 int nlists
; /* number of irec's (ex lists) */
1917 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1918 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1919 nextents
= xfs_iext_count(ifp
);
1921 if (nextents
== 0) {
1922 xfs_iext_destroy(ifp
);
1923 } else if (nextents
<= XFS_INLINE_EXTS
) {
1924 xfs_iext_indirect_to_direct(ifp
);
1925 xfs_iext_direct_to_inline(ifp
, nextents
);
1926 } else if (nextents
<= XFS_LINEAR_EXTS
) {
1927 xfs_iext_indirect_to_direct(ifp
);
1928 } else if (nextents
< (nlists
* XFS_LINEAR_EXTS
) >> 1) {
1929 xfs_iext_irec_compact_pages(ifp
);
1934 * Combine extents from neighboring extent pages.
1937 xfs_iext_irec_compact_pages(
1938 xfs_ifork_t
*ifp
) /* inode fork pointer */
1940 xfs_ext_irec_t
*erp
, *erp_next
;/* pointers to irec entries */
1941 int erp_idx
= 0; /* indirection array index */
1942 int nlists
; /* number of irec's (ex lists) */
1944 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1945 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1946 while (erp_idx
< nlists
- 1) {
1947 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1949 if (erp_next
->er_extcount
<=
1950 (XFS_LINEAR_EXTS
- erp
->er_extcount
)) {
1951 memcpy(&erp
->er_extbuf
[erp
->er_extcount
],
1952 erp_next
->er_extbuf
, erp_next
->er_extcount
*
1953 sizeof(xfs_bmbt_rec_t
));
1954 erp
->er_extcount
+= erp_next
->er_extcount
;
1956 * Free page before removing extent record
1957 * so er_extoffs don't get modified in
1958 * xfs_iext_irec_remove.
1960 kmem_free(erp_next
->er_extbuf
);
1961 erp_next
->er_extbuf
= NULL
;
1962 xfs_iext_irec_remove(ifp
, erp_idx
+ 1);
1963 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1971 * This is called to update the er_extoff field in the indirection
1972 * array when extents have been added or removed from one of the
1973 * extent lists. erp_idx contains the irec index to begin updating
1974 * at and ext_diff contains the number of extents that were added
1978 xfs_iext_irec_update_extoffs(
1979 xfs_ifork_t
*ifp
, /* inode fork pointer */
1980 int erp_idx
, /* irec index to update */
1981 int ext_diff
) /* number of new extents */
1983 int i
; /* loop counter */
1984 int nlists
; /* number of irec's (ex lists */
1986 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1987 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1988 for (i
= erp_idx
; i
< nlists
; i
++) {
1989 ifp
->if_u1
.if_ext_irec
[i
].er_extoff
+= ext_diff
;
1994 * Initialize an inode's copy-on-write fork.
1998 struct xfs_inode
*ip
)
2003 ip
->i_cowfp
= kmem_zone_zalloc(xfs_ifork_zone
,
2004 KM_SLEEP
| KM_NOFS
);
2005 ip
->i_cowfp
->if_flags
= XFS_IFEXTENTS
;
2006 ip
->i_cformat
= XFS_DINODE_FMT_EXTENTS
;
2007 ip
->i_cnextents
= 0;
2011 * Lookup the extent covering bno.
2013 * If there is an extent covering bno return the extent index, and store the
2014 * expanded extent structure in *gotp, and the extent index in *idx.
2015 * If there is no extent covering bno, but there is an extent after it (e.g.
2016 * it lies in a hole) return that extent in *gotp and its index in *idx
2018 * If bno is beyond the last extent return false, and return the index after
2019 * the last valid index in *idxp.
2022 xfs_iext_lookup_extent(
2023 struct xfs_inode
*ip
,
2024 struct xfs_ifork
*ifp
,
2027 struct xfs_bmbt_irec
*gotp
)
2029 struct xfs_bmbt_rec_host
*ep
;
2031 XFS_STATS_INC(ip
->i_mount
, xs_look_exlist
);
2033 ep
= xfs_iext_bno_to_ext(ifp
, bno
, idxp
);
2036 xfs_bmbt_get_all(ep
, gotp
);
2041 * Return true if there is an extent at index idx, and return the expanded
2042 * extent structure at idx in that case. Else return false.
2045 xfs_iext_get_extent(
2046 struct xfs_ifork
*ifp
,
2048 struct xfs_bmbt_irec
*gotp
)
2050 if (idx
< 0 || idx
>= xfs_iext_count(ifp
))
2052 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, idx
), gotp
);