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 "libxfs_priv.h"
20 #include "xfs_format.h"
21 #include "xfs_log_format.h"
22 #include "xfs_trans_resv.h"
23 #include "xfs_mount.h"
24 #include "xfs_inode.h"
25 #include "xfs_trans.h"
26 #include "xfs_btree.h"
27 #include "xfs_bmap_btree.h"
29 #include "xfs_trace.h"
30 #include "xfs_attr_sf.h"
31 #include "xfs_da_format.h"
32 #include "xfs_da_btree.h"
33 #include "xfs_dir2_priv.h"
36 kmem_zone_t
*xfs_ifork_zone
;
38 STATIC
int xfs_iformat_local(xfs_inode_t
*, xfs_dinode_t
*, int, int);
39 STATIC
int xfs_iformat_extents(xfs_inode_t
*, xfs_dinode_t
*, int);
40 STATIC
int xfs_iformat_btree(xfs_inode_t
*, xfs_dinode_t
*, int);
44 * Make sure that the extents in the given memory buffer
54 xfs_bmbt_rec_host_t rec
;
57 for (i
= 0; i
< nrecs
; i
++) {
58 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, i
);
59 rec
.l0
= get_unaligned(&ep
->l0
);
60 rec
.l1
= get_unaligned(&ep
->l1
);
61 xfs_bmbt_get_all(&rec
, &irec
);
62 if (fmt
== XFS_EXTFMT_NOSTATE
)
63 ASSERT(irec
.br_state
== XFS_EXT_NORM
);
67 #define xfs_validate_extents(ifp, nrecs, fmt)
72 * Move inode type and inode format specific information from the
73 * on-disk inode to the in-core inode. For fifos, devs, and sockets
74 * this means set if_rdev to the proper value. For files, directories,
75 * and symlinks this means to bring in the in-line data or extent
76 * pointers. For a file in B-tree format, only the root is immediately
77 * brought in-core. The rest will be in-lined in if_extents when it
78 * is first referenced (see xfs_iread_extents()).
85 xfs_attr_shortform_t
*atp
;
90 if (unlikely(be32_to_cpu(dip
->di_nextents
) +
91 be16_to_cpu(dip
->di_anextents
) >
92 be64_to_cpu(dip
->di_nblocks
))) {
94 "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.",
95 (unsigned long long)ip
->i_ino
,
96 (int)(be32_to_cpu(dip
->di_nextents
) +
97 be16_to_cpu(dip
->di_anextents
)),
99 be64_to_cpu(dip
->di_nblocks
));
100 XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW
,
102 return -EFSCORRUPTED
;
105 if (unlikely(dip
->di_forkoff
> ip
->i_mount
->m_sb
.sb_inodesize
)) {
106 xfs_warn(ip
->i_mount
, "corrupt dinode %Lu, forkoff = 0x%x.",
107 (unsigned long long)ip
->i_ino
,
109 XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW
,
111 return -EFSCORRUPTED
;
114 if (unlikely((ip
->i_d
.di_flags
& XFS_DIFLAG_REALTIME
) &&
115 !ip
->i_mount
->m_rtdev_targp
)) {
116 xfs_warn(ip
->i_mount
,
117 "corrupt dinode %Lu, has realtime flag set.",
119 XFS_CORRUPTION_ERROR("xfs_iformat(realtime)",
120 XFS_ERRLEVEL_LOW
, ip
->i_mount
, dip
);
121 return -EFSCORRUPTED
;
124 if (unlikely(xfs_is_reflink_inode(ip
) &&
125 (VFS_I(ip
)->i_mode
& S_IFMT
) != S_IFREG
)) {
126 xfs_warn(ip
->i_mount
,
127 "corrupt dinode %llu, wrong file type for reflink.",
129 XFS_CORRUPTION_ERROR("xfs_iformat(reflink)",
130 XFS_ERRLEVEL_LOW
, ip
->i_mount
, dip
);
131 return -EFSCORRUPTED
;
134 if (unlikely(xfs_is_reflink_inode(ip
) &&
135 (ip
->i_d
.di_flags
& XFS_DIFLAG_REALTIME
))) {
136 xfs_warn(ip
->i_mount
,
137 "corrupt dinode %llu, has reflink+realtime flag set.",
139 XFS_CORRUPTION_ERROR("xfs_iformat(reflink)",
140 XFS_ERRLEVEL_LOW
, ip
->i_mount
, dip
);
141 return -EFSCORRUPTED
;
144 switch (VFS_I(ip
)->i_mode
& S_IFMT
) {
149 if (unlikely(dip
->di_format
!= XFS_DINODE_FMT_DEV
)) {
150 XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW
,
152 return -EFSCORRUPTED
;
155 ip
->i_df
.if_u2
.if_rdev
= xfs_dinode_get_rdev(dip
);
161 switch (dip
->di_format
) {
162 case XFS_DINODE_FMT_LOCAL
:
164 * no local regular files yet
166 if (unlikely(S_ISREG(be16_to_cpu(dip
->di_mode
)))) {
167 xfs_warn(ip
->i_mount
,
168 "corrupt inode %Lu (local format for regular file).",
169 (unsigned long long) ip
->i_ino
);
170 XFS_CORRUPTION_ERROR("xfs_iformat(4)",
173 return -EFSCORRUPTED
;
176 di_size
= be64_to_cpu(dip
->di_size
);
177 if (unlikely(di_size
< 0 ||
178 di_size
> XFS_DFORK_DSIZE(dip
, ip
->i_mount
))) {
179 xfs_warn(ip
->i_mount
,
180 "corrupt inode %Lu (bad size %Ld for local inode).",
181 (unsigned long long) ip
->i_ino
,
182 (long long) di_size
);
183 XFS_CORRUPTION_ERROR("xfs_iformat(5)",
186 return -EFSCORRUPTED
;
190 error
= xfs_iformat_local(ip
, dip
, XFS_DATA_FORK
, size
);
192 case XFS_DINODE_FMT_EXTENTS
:
193 error
= xfs_iformat_extents(ip
, dip
, XFS_DATA_FORK
);
195 case XFS_DINODE_FMT_BTREE
:
196 error
= xfs_iformat_btree(ip
, dip
, XFS_DATA_FORK
);
199 XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW
,
201 return -EFSCORRUPTED
;
206 XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW
, ip
->i_mount
);
207 return -EFSCORRUPTED
;
212 if (xfs_is_reflink_inode(ip
)) {
213 ASSERT(ip
->i_cowfp
== NULL
);
214 xfs_ifork_init_cow(ip
);
217 if (!XFS_DFORK_Q(dip
))
220 ASSERT(ip
->i_afp
== NULL
);
221 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
| KM_NOFS
);
223 switch (dip
->di_aformat
) {
224 case XFS_DINODE_FMT_LOCAL
:
225 atp
= (xfs_attr_shortform_t
*)XFS_DFORK_APTR(dip
);
226 size
= be16_to_cpu(atp
->hdr
.totsize
);
228 if (unlikely(size
< sizeof(struct xfs_attr_sf_hdr
))) {
229 xfs_warn(ip
->i_mount
,
230 "corrupt inode %Lu (bad attr fork size %Ld).",
231 (unsigned long long) ip
->i_ino
,
233 XFS_CORRUPTION_ERROR("xfs_iformat(8)",
236 error
= -EFSCORRUPTED
;
240 error
= xfs_iformat_local(ip
, dip
, XFS_ATTR_FORK
, size
);
242 case XFS_DINODE_FMT_EXTENTS
:
243 error
= xfs_iformat_extents(ip
, dip
, XFS_ATTR_FORK
);
245 case XFS_DINODE_FMT_BTREE
:
246 error
= xfs_iformat_btree(ip
, dip
, XFS_ATTR_FORK
);
249 error
= -EFSCORRUPTED
;
253 kmem_zone_free(xfs_ifork_zone
, ip
->i_afp
);
256 kmem_zone_free(xfs_ifork_zone
, ip
->i_cowfp
);
258 xfs_idestroy_fork(ip
, XFS_DATA_FORK
);
265 struct xfs_inode
*ip
,
270 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
271 int mem_size
= size
, real_size
= 0;
275 * If we are using the local fork to store a symlink body we need to
276 * zero-terminate it so that we can pass it back to the VFS directly.
277 * Overallocate the in-memory fork by one for that and add a zero
278 * to terminate it below.
280 zero_terminate
= S_ISLNK(VFS_I(ip
)->i_mode
);
285 ifp
->if_u1
.if_data
= NULL
;
286 else if (mem_size
<= sizeof(ifp
->if_u2
.if_inline_data
))
287 ifp
->if_u1
.if_data
= ifp
->if_u2
.if_inline_data
;
289 real_size
= roundup(mem_size
, 4);
290 ifp
->if_u1
.if_data
= kmem_alloc(real_size
, KM_SLEEP
| KM_NOFS
);
294 memcpy(ifp
->if_u1
.if_data
, data
, size
);
296 ifp
->if_u1
.if_data
[size
] = '\0';
299 ifp
->if_bytes
= size
;
300 ifp
->if_real_bytes
= real_size
;
301 ifp
->if_flags
&= ~(XFS_IFEXTENTS
| XFS_IFBROOT
);
302 ifp
->if_flags
|= XFS_IFINLINE
;
306 * The file is in-lined in the on-disk inode.
307 * If it fits into if_inline_data, then copy
308 * it there, otherwise allocate a buffer for it
309 * and copy the data there. Either way, set
310 * if_data to point at the data.
311 * If we allocate a buffer for the data, make
312 * sure that its size is a multiple of 4 and
313 * record the real size in i_real_bytes.
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 if (S_ISDIR(VFS_I(ip
)->i_mode
) && whichfork
== XFS_DATA_FORK
) {
340 error
= xfs_dir2_sf_verify(ip
->i_mount
,
341 (struct xfs_dir2_sf_hdr
*)XFS_DFORK_DPTR(dip
),
347 xfs_init_local_fork(ip
, whichfork
, XFS_DFORK_PTR(dip
, whichfork
), size
);
352 * The file consists of a set of extents all
353 * of which fit into the on-disk inode.
354 * If there are few enough extents to fit into
355 * the if_inline_ext, then copy them there.
356 * Otherwise allocate a buffer for them and copy
357 * them into it. Either way, set if_extents
358 * to point at the extents.
372 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
373 nex
= XFS_DFORK_NEXTENTS(dip
, whichfork
);
374 size
= nex
* (uint
)sizeof(xfs_bmbt_rec_t
);
377 * If the number of extents is unreasonable, then something
378 * is wrong and we just bail out rather than crash in
379 * kmem_alloc() or memcpy() below.
381 if (unlikely(size
< 0 || size
> XFS_DFORK_SIZE(dip
, ip
->i_mount
, whichfork
))) {
382 xfs_warn(ip
->i_mount
, "corrupt inode %Lu ((a)extents = %d).",
383 (unsigned long long) ip
->i_ino
, nex
);
384 XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW
,
386 return -EFSCORRUPTED
;
389 ifp
->if_real_bytes
= 0;
391 ifp
->if_u1
.if_extents
= NULL
;
392 else if (nex
<= XFS_INLINE_EXTS
)
393 ifp
->if_u1
.if_extents
= ifp
->if_u2
.if_inline_ext
;
395 xfs_iext_add(ifp
, 0, nex
);
397 ifp
->if_bytes
= size
;
399 dp
= (xfs_bmbt_rec_t
*) XFS_DFORK_PTR(dip
, whichfork
);
400 xfs_validate_extents(ifp
, nex
, XFS_EXTFMT_INODE(ip
));
401 for (i
= 0; i
< nex
; i
++, dp
++) {
402 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, i
);
403 ep
->l0
= get_unaligned_be64(&dp
->l0
);
404 ep
->l1
= get_unaligned_be64(&dp
->l1
);
406 XFS_BMAP_TRACE_EXLIST(ip
, nex
, whichfork
);
407 if (whichfork
!= XFS_DATA_FORK
||
408 XFS_EXTFMT_INODE(ip
) == XFS_EXTFMT_NOSTATE
)
409 if (unlikely(xfs_check_nostate_extents(
411 XFS_ERROR_REPORT("xfs_iformat_extents(2)",
414 return -EFSCORRUPTED
;
417 ifp
->if_flags
|= XFS_IFEXTENTS
;
422 * The file has too many extents to fit into
423 * the inode, so they are in B-tree format.
424 * Allocate a buffer for the root of the B-tree
425 * and copy the root into it. The i_extents
426 * field will remain NULL until all of the
427 * extents are read in (when they are needed).
435 struct xfs_mount
*mp
= ip
->i_mount
;
436 xfs_bmdr_block_t
*dfp
;
443 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
444 dfp
= (xfs_bmdr_block_t
*)XFS_DFORK_PTR(dip
, whichfork
);
445 size
= XFS_BMAP_BROOT_SPACE(mp
, dfp
);
446 nrecs
= be16_to_cpu(dfp
->bb_numrecs
);
447 level
= be16_to_cpu(dfp
->bb_level
);
450 * blow out if -- fork has less extents than can fit in
451 * fork (fork shouldn't be a btree format), root btree
452 * block has more records than can fit into the fork,
453 * or the number of extents is greater than the number of
456 if (unlikely(XFS_IFORK_NEXTENTS(ip
, whichfork
) <=
457 XFS_IFORK_MAXEXT(ip
, whichfork
) ||
458 XFS_BMDR_SPACE_CALC(nrecs
) >
459 XFS_DFORK_SIZE(dip
, mp
, whichfork
) ||
460 XFS_IFORK_NEXTENTS(ip
, whichfork
) > ip
->i_d
.di_nblocks
) ||
461 level
== 0 || level
> XFS_BTREE_MAXLEVELS
) {
462 xfs_warn(mp
, "corrupt inode %Lu (btree).",
463 (unsigned long long) ip
->i_ino
);
464 XFS_CORRUPTION_ERROR("xfs_iformat_btree", XFS_ERRLEVEL_LOW
,
466 return -EFSCORRUPTED
;
469 ifp
->if_broot_bytes
= size
;
470 ifp
->if_broot
= kmem_alloc(size
, KM_SLEEP
| KM_NOFS
);
471 ASSERT(ifp
->if_broot
!= NULL
);
473 * Copy and convert from the on-disk structure
474 * to the in-memory structure.
476 xfs_bmdr_to_bmbt(ip
, dfp
, XFS_DFORK_SIZE(dip
, ip
->i_mount
, whichfork
),
477 ifp
->if_broot
, size
);
478 ifp
->if_flags
&= ~XFS_IFEXTENTS
;
479 ifp
->if_flags
|= XFS_IFBROOT
;
485 * Read in extents from a btree-format inode.
486 * Allocate and fill in if_extents. Real work is done in xfs_bmap.c.
496 xfs_extnum_t nextents
;
498 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
500 if (unlikely(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
501 XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW
,
503 return -EFSCORRUPTED
;
505 nextents
= XFS_IFORK_NEXTENTS(ip
, whichfork
);
506 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
509 * We know that the size is valid (it's checked in iformat_btree)
511 ifp
->if_bytes
= ifp
->if_real_bytes
= 0;
512 xfs_iext_add(ifp
, 0, nextents
);
513 error
= xfs_bmap_read_extents(tp
, ip
, whichfork
);
515 xfs_iext_destroy(ifp
);
518 xfs_validate_extents(ifp
, nextents
, XFS_EXTFMT_INODE(ip
));
519 ifp
->if_flags
|= XFS_IFEXTENTS
;
523 * Reallocate the space for if_broot based on the number of records
524 * being added or deleted as indicated in rec_diff. Move the records
525 * and pointers in if_broot to fit the new size. When shrinking this
526 * will eliminate holes between the records and pointers created by
527 * the caller. When growing this will create holes to be filled in
530 * The caller must not request to add more records than would fit in
531 * the on-disk inode root. If the if_broot is currently NULL, then
532 * if we are adding records, one will be allocated. The caller must also
533 * not request that the number of records go below zero, although
536 * ip -- the inode whose if_broot area is changing
537 * ext_diff -- the change in the number of records, positive or negative,
538 * requested for the if_broot array.
546 struct xfs_mount
*mp
= ip
->i_mount
;
549 struct xfs_btree_block
*new_broot
;
556 * Handle the degenerate case quietly.
562 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
565 * If there wasn't any memory allocated before, just
566 * allocate it now and get out.
568 if (ifp
->if_broot_bytes
== 0) {
569 new_size
= XFS_BMAP_BROOT_SPACE_CALC(mp
, rec_diff
);
570 ifp
->if_broot
= kmem_alloc(new_size
, KM_SLEEP
| KM_NOFS
);
571 ifp
->if_broot_bytes
= (int)new_size
;
576 * If there is already an existing if_broot, then we need
577 * to realloc() it and shift the pointers to their new
578 * location. The records don't change location because
579 * they are kept butted up against the btree block header.
581 cur_max
= xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0);
582 new_max
= cur_max
+ rec_diff
;
583 new_size
= XFS_BMAP_BROOT_SPACE_CALC(mp
, new_max
);
584 ifp
->if_broot
= kmem_realloc(ifp
->if_broot
, new_size
,
586 op
= (char *)XFS_BMAP_BROOT_PTR_ADDR(mp
, ifp
->if_broot
, 1,
587 ifp
->if_broot_bytes
);
588 np
= (char *)XFS_BMAP_BROOT_PTR_ADDR(mp
, ifp
->if_broot
, 1,
590 ifp
->if_broot_bytes
= (int)new_size
;
591 ASSERT(XFS_BMAP_BMDR_SPACE(ifp
->if_broot
) <=
592 XFS_IFORK_SIZE(ip
, whichfork
));
593 memmove(np
, op
, cur_max
* (uint
)sizeof(xfs_fsblock_t
));
598 * rec_diff is less than 0. In this case, we are shrinking the
599 * if_broot buffer. It must already exist. If we go to zero
600 * records, just get rid of the root and clear the status bit.
602 ASSERT((ifp
->if_broot
!= NULL
) && (ifp
->if_broot_bytes
> 0));
603 cur_max
= xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0);
604 new_max
= cur_max
+ rec_diff
;
605 ASSERT(new_max
>= 0);
607 new_size
= XFS_BMAP_BROOT_SPACE_CALC(mp
, new_max
);
611 new_broot
= kmem_alloc(new_size
, KM_SLEEP
| KM_NOFS
);
613 * First copy over the btree block header.
615 memcpy(new_broot
, ifp
->if_broot
,
616 XFS_BMBT_BLOCK_LEN(ip
->i_mount
));
619 ifp
->if_flags
&= ~XFS_IFBROOT
;
623 * Only copy the records and pointers if there are any.
627 * First copy the records.
629 op
= (char *)XFS_BMBT_REC_ADDR(mp
, ifp
->if_broot
, 1);
630 np
= (char *)XFS_BMBT_REC_ADDR(mp
, new_broot
, 1);
631 memcpy(np
, op
, new_max
* (uint
)sizeof(xfs_bmbt_rec_t
));
634 * Then copy the pointers.
636 op
= (char *)XFS_BMAP_BROOT_PTR_ADDR(mp
, ifp
->if_broot
, 1,
637 ifp
->if_broot_bytes
);
638 np
= (char *)XFS_BMAP_BROOT_PTR_ADDR(mp
, new_broot
, 1,
640 memcpy(np
, op
, new_max
* (uint
)sizeof(xfs_fsblock_t
));
642 kmem_free(ifp
->if_broot
);
643 ifp
->if_broot
= new_broot
;
644 ifp
->if_broot_bytes
= (int)new_size
;
646 ASSERT(XFS_BMAP_BMDR_SPACE(ifp
->if_broot
) <=
647 XFS_IFORK_SIZE(ip
, whichfork
));
653 * This is called when the amount of space needed for if_data
654 * is increased or decreased. The change in size is indicated by
655 * the number of bytes that need to be added or deleted in the
656 * byte_diff parameter.
658 * If the amount of space needed has decreased below the size of the
659 * inline buffer, then switch to using the inline buffer. Otherwise,
660 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
663 * ip -- the inode whose if_data area is changing
664 * byte_diff -- the change in the number of bytes, positive or negative,
665 * requested for the if_data array.
677 if (byte_diff
== 0) {
681 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
682 new_size
= (int)ifp
->if_bytes
+ byte_diff
;
683 ASSERT(new_size
>= 0);
686 if (ifp
->if_u1
.if_data
!= ifp
->if_u2
.if_inline_data
) {
687 kmem_free(ifp
->if_u1
.if_data
);
689 ifp
->if_u1
.if_data
= NULL
;
691 } else if (new_size
<= sizeof(ifp
->if_u2
.if_inline_data
)) {
693 * If the valid extents/data can fit in if_inline_ext/data,
694 * copy them from the malloc'd vector and free it.
696 if (ifp
->if_u1
.if_data
== NULL
) {
697 ifp
->if_u1
.if_data
= ifp
->if_u2
.if_inline_data
;
698 } else if (ifp
->if_u1
.if_data
!= ifp
->if_u2
.if_inline_data
) {
699 ASSERT(ifp
->if_real_bytes
!= 0);
700 memcpy(ifp
->if_u2
.if_inline_data
, ifp
->if_u1
.if_data
,
702 kmem_free(ifp
->if_u1
.if_data
);
703 ifp
->if_u1
.if_data
= ifp
->if_u2
.if_inline_data
;
708 * Stuck with malloc/realloc.
709 * For inline data, the underlying buffer must be
710 * a multiple of 4 bytes in size so that it can be
711 * logged and stay on word boundaries. We enforce
714 real_size
= roundup(new_size
, 4);
715 if (ifp
->if_u1
.if_data
== NULL
) {
716 ASSERT(ifp
->if_real_bytes
== 0);
717 ifp
->if_u1
.if_data
= kmem_alloc(real_size
,
719 } else if (ifp
->if_u1
.if_data
!= ifp
->if_u2
.if_inline_data
) {
721 * Only do the realloc if the underlying size
722 * is really changing.
724 if (ifp
->if_real_bytes
!= real_size
) {
726 kmem_realloc(ifp
->if_u1
.if_data
,
731 ASSERT(ifp
->if_real_bytes
== 0);
732 ifp
->if_u1
.if_data
= kmem_alloc(real_size
,
734 memcpy(ifp
->if_u1
.if_data
, ifp
->if_u2
.if_inline_data
,
738 ifp
->if_real_bytes
= real_size
;
739 ifp
->if_bytes
= new_size
;
740 ASSERT(ifp
->if_bytes
<= XFS_IFORK_SIZE(ip
, whichfork
));
750 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
751 if (ifp
->if_broot
!= NULL
) {
752 kmem_free(ifp
->if_broot
);
753 ifp
->if_broot
= NULL
;
757 * If the format is local, then we can't have an extents
758 * array so just look for an inline data array. If we're
759 * not local then we may or may not have an extents list,
760 * so check and free it up if we do.
762 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
763 if ((ifp
->if_u1
.if_data
!= ifp
->if_u2
.if_inline_data
) &&
764 (ifp
->if_u1
.if_data
!= NULL
)) {
765 ASSERT(ifp
->if_real_bytes
!= 0);
766 kmem_free(ifp
->if_u1
.if_data
);
767 ifp
->if_u1
.if_data
= NULL
;
768 ifp
->if_real_bytes
= 0;
770 } else if ((ifp
->if_flags
& XFS_IFEXTENTS
) &&
771 ((ifp
->if_flags
& XFS_IFEXTIREC
) ||
772 ((ifp
->if_u1
.if_extents
!= NULL
) &&
773 (ifp
->if_u1
.if_extents
!= ifp
->if_u2
.if_inline_ext
)))) {
774 ASSERT(ifp
->if_real_bytes
!= 0);
775 xfs_iext_destroy(ifp
);
777 ASSERT(ifp
->if_u1
.if_extents
== NULL
||
778 ifp
->if_u1
.if_extents
== ifp
->if_u2
.if_inline_ext
);
779 ASSERT(ifp
->if_real_bytes
== 0);
780 if (whichfork
== XFS_ATTR_FORK
) {
781 kmem_zone_free(xfs_ifork_zone
, ip
->i_afp
);
783 } else if (whichfork
== XFS_COW_FORK
) {
784 kmem_zone_free(xfs_ifork_zone
, ip
->i_cowfp
);
789 /* Count number of incore extents based on if_bytes */
791 xfs_iext_count(struct xfs_ifork
*ifp
)
793 return ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
797 * Convert in-core extents to on-disk form
799 * For either the data or attr fork in extent format, we need to endian convert
800 * the in-core extent as we place them into the on-disk inode.
802 * In the case of the data fork, the in-core and on-disk fork sizes can be
803 * different due to delayed allocation extents. We only copy on-disk extents
804 * here, so callers must always use the physical fork size to determine the
805 * size of the buffer passed to this routine. We will return the size actually
818 xfs_fsblock_t start_block
;
820 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
821 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
|XFS_ILOCK_SHARED
));
822 ASSERT(ifp
->if_bytes
> 0);
824 nrecs
= xfs_iext_count(ifp
);
825 XFS_BMAP_TRACE_EXLIST(ip
, nrecs
, whichfork
);
829 * There are some delayed allocation extents in the
830 * inode, so copy the extents one at a time and skip
831 * the delayed ones. There must be at least one
832 * non-delayed extent.
835 for (i
= 0; i
< nrecs
; i
++) {
836 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, i
);
837 start_block
= xfs_bmbt_get_startblock(ep
);
838 if (isnullstartblock(start_block
)) {
840 * It's a delayed allocation extent, so skip it.
845 /* Translate to on disk format */
846 put_unaligned_be64(ep
->l0
, &dp
->l0
);
847 put_unaligned_be64(ep
->l1
, &dp
->l1
);
852 xfs_validate_extents(ifp
, copied
, XFS_EXTFMT_INODE(ip
));
854 return (copied
* (uint
)sizeof(xfs_bmbt_rec_t
));
858 * Each of the following cases stores data into the same region
859 * of the on-disk inode, so only one of them can be valid at
860 * any given time. While it is possible to have conflicting formats
861 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
862 * in EXTENTS format, this can only happen when the fork has
863 * changed formats after being modified but before being flushed.
864 * In these cases, the format always takes precedence, because the
865 * format indicates the current state of the fork.
871 xfs_inode_log_item_t
*iip
,
878 static const short brootflag
[2] =
879 { XFS_ILOG_DBROOT
, XFS_ILOG_ABROOT
};
880 static const short dataflag
[2] =
881 { XFS_ILOG_DDATA
, XFS_ILOG_ADATA
};
882 static const short extflag
[2] =
883 { XFS_ILOG_DEXT
, XFS_ILOG_AEXT
};
887 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
889 * This can happen if we gave up in iformat in an error path,
890 * for the attribute fork.
893 ASSERT(whichfork
== XFS_ATTR_FORK
);
896 cp
= XFS_DFORK_PTR(dip
, whichfork
);
898 switch (XFS_IFORK_FORMAT(ip
, whichfork
)) {
899 case XFS_DINODE_FMT_LOCAL
:
900 if (S_ISDIR(VFS_I(ip
)->i_mode
) && whichfork
== XFS_DATA_FORK
) {
901 error
= xfs_dir2_sf_verify(mp
,
902 (struct xfs_dir2_sf_hdr
*)ifp
->if_u1
.if_data
,
907 if ((iip
->ili_fields
& dataflag
[whichfork
]) &&
908 (ifp
->if_bytes
> 0)) {
909 ASSERT(ifp
->if_u1
.if_data
!= NULL
);
910 ASSERT(ifp
->if_bytes
<= XFS_IFORK_SIZE(ip
, whichfork
));
911 memcpy(cp
, ifp
->if_u1
.if_data
, ifp
->if_bytes
);
915 case XFS_DINODE_FMT_EXTENTS
:
916 ASSERT((ifp
->if_flags
& XFS_IFEXTENTS
) ||
917 !(iip
->ili_fields
& extflag
[whichfork
]));
918 if ((iip
->ili_fields
& extflag
[whichfork
]) &&
919 (ifp
->if_bytes
> 0)) {
920 ASSERT(xfs_iext_get_ext(ifp
, 0));
921 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) > 0);
922 (void)xfs_iextents_copy(ip
, (xfs_bmbt_rec_t
*)cp
,
927 case XFS_DINODE_FMT_BTREE
:
928 if ((iip
->ili_fields
& brootflag
[whichfork
]) &&
929 (ifp
->if_broot_bytes
> 0)) {
930 ASSERT(ifp
->if_broot
!= NULL
);
931 ASSERT(XFS_BMAP_BMDR_SPACE(ifp
->if_broot
) <=
932 XFS_IFORK_SIZE(ip
, whichfork
));
933 xfs_bmbt_to_bmdr(mp
, ifp
->if_broot
, ifp
->if_broot_bytes
,
934 (xfs_bmdr_block_t
*)cp
,
935 XFS_DFORK_SIZE(dip
, mp
, whichfork
));
939 case XFS_DINODE_FMT_DEV
:
940 if (iip
->ili_fields
& XFS_ILOG_DEV
) {
941 ASSERT(whichfork
== XFS_DATA_FORK
);
942 xfs_dinode_put_rdev(dip
, ip
->i_df
.if_u2
.if_rdev
);
946 case XFS_DINODE_FMT_UUID
:
947 if (iip
->ili_fields
& XFS_ILOG_UUID
) {
948 ASSERT(whichfork
== XFS_DATA_FORK
);
949 memcpy(XFS_DFORK_DPTR(dip
),
950 &ip
->i_df
.if_u2
.if_uuid
,
963 * Return a pointer to the extent record at file index idx.
965 xfs_bmbt_rec_host_t
*
967 xfs_ifork_t
*ifp
, /* inode fork pointer */
968 xfs_extnum_t idx
) /* index of target extent */
971 ASSERT(idx
< xfs_iext_count(ifp
));
973 if ((ifp
->if_flags
& XFS_IFEXTIREC
) && (idx
== 0)) {
974 return ifp
->if_u1
.if_ext_irec
->er_extbuf
;
975 } else if (ifp
->if_flags
& XFS_IFEXTIREC
) {
976 xfs_ext_irec_t
*erp
; /* irec pointer */
977 int erp_idx
= 0; /* irec index */
978 xfs_extnum_t page_idx
= idx
; /* ext index in target list */
980 erp
= xfs_iext_idx_to_irec(ifp
, &page_idx
, &erp_idx
, 0);
981 return &erp
->er_extbuf
[page_idx
];
982 } else if (ifp
->if_bytes
) {
983 return &ifp
->if_u1
.if_extents
[idx
];
989 /* Convert bmap state flags to an inode fork. */
991 xfs_iext_state_to_fork(
992 struct xfs_inode
*ip
,
995 if (state
& BMAP_COWFORK
)
997 else if (state
& BMAP_ATTRFORK
)
1003 * Insert new item(s) into the extent records for incore inode
1004 * fork 'ifp'. 'count' new items are inserted at index 'idx'.
1008 xfs_inode_t
*ip
, /* incore inode pointer */
1009 xfs_extnum_t idx
, /* starting index of new items */
1010 xfs_extnum_t count
, /* number of inserted items */
1011 xfs_bmbt_irec_t
*new, /* items to insert */
1012 int state
) /* type of extent conversion */
1014 xfs_ifork_t
*ifp
= xfs_iext_state_to_fork(ip
, state
);
1015 xfs_extnum_t i
; /* extent record index */
1017 trace_xfs_iext_insert(ip
, idx
, new, state
, _RET_IP_
);
1019 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1020 xfs_iext_add(ifp
, idx
, count
);
1021 for (i
= idx
; i
< idx
+ count
; i
++, new++)
1022 xfs_bmbt_set_all(xfs_iext_get_ext(ifp
, i
), new);
1026 * This is called when the amount of space required for incore file
1027 * extents needs to be increased. The ext_diff parameter stores the
1028 * number of new extents being added and the idx parameter contains
1029 * the extent index where the new extents will be added. If the new
1030 * extents are being appended, then we just need to (re)allocate and
1031 * initialize the space. Otherwise, if the new extents are being
1032 * inserted into the middle of the existing entries, a bit more work
1033 * is required to make room for the new extents to be inserted. The
1034 * caller is responsible for filling in the new extent entries upon
1039 xfs_ifork_t
*ifp
, /* inode fork pointer */
1040 xfs_extnum_t idx
, /* index to begin adding exts */
1041 int ext_diff
) /* number of extents to add */
1043 int byte_diff
; /* new bytes being added */
1044 int new_size
; /* size of extents after adding */
1045 xfs_extnum_t nextents
; /* number of extents in file */
1047 nextents
= xfs_iext_count(ifp
);
1048 ASSERT((idx
>= 0) && (idx
<= nextents
));
1049 byte_diff
= ext_diff
* sizeof(xfs_bmbt_rec_t
);
1050 new_size
= ifp
->if_bytes
+ byte_diff
;
1052 * If the new number of extents (nextents + ext_diff)
1053 * fits inside the inode, then continue to use the inline
1056 if (nextents
+ ext_diff
<= XFS_INLINE_EXTS
) {
1057 if (idx
< nextents
) {
1058 memmove(&ifp
->if_u2
.if_inline_ext
[idx
+ ext_diff
],
1059 &ifp
->if_u2
.if_inline_ext
[idx
],
1060 (nextents
- idx
) * sizeof(xfs_bmbt_rec_t
));
1061 memset(&ifp
->if_u2
.if_inline_ext
[idx
], 0, byte_diff
);
1063 ifp
->if_u1
.if_extents
= ifp
->if_u2
.if_inline_ext
;
1064 ifp
->if_real_bytes
= 0;
1067 * Otherwise use a linear (direct) extent list.
1068 * If the extents are currently inside the inode,
1069 * xfs_iext_realloc_direct will switch us from
1070 * inline to direct extent allocation mode.
1072 else if (nextents
+ ext_diff
<= XFS_LINEAR_EXTS
) {
1073 xfs_iext_realloc_direct(ifp
, new_size
);
1074 if (idx
< nextents
) {
1075 memmove(&ifp
->if_u1
.if_extents
[idx
+ ext_diff
],
1076 &ifp
->if_u1
.if_extents
[idx
],
1077 (nextents
- idx
) * sizeof(xfs_bmbt_rec_t
));
1078 memset(&ifp
->if_u1
.if_extents
[idx
], 0, byte_diff
);
1081 /* Indirection array */
1083 xfs_ext_irec_t
*erp
;
1087 ASSERT(nextents
+ ext_diff
> XFS_LINEAR_EXTS
);
1088 if (ifp
->if_flags
& XFS_IFEXTIREC
) {
1089 erp
= xfs_iext_idx_to_irec(ifp
, &page_idx
, &erp_idx
, 1);
1091 xfs_iext_irec_init(ifp
);
1092 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1093 erp
= ifp
->if_u1
.if_ext_irec
;
1095 /* Extents fit in target extent page */
1096 if (erp
&& erp
->er_extcount
+ ext_diff
<= XFS_LINEAR_EXTS
) {
1097 if (page_idx
< erp
->er_extcount
) {
1098 memmove(&erp
->er_extbuf
[page_idx
+ ext_diff
],
1099 &erp
->er_extbuf
[page_idx
],
1100 (erp
->er_extcount
- page_idx
) *
1101 sizeof(xfs_bmbt_rec_t
));
1102 memset(&erp
->er_extbuf
[page_idx
], 0, byte_diff
);
1104 erp
->er_extcount
+= ext_diff
;
1105 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, ext_diff
);
1107 /* Insert a new extent page */
1109 xfs_iext_add_indirect_multi(ifp
,
1110 erp_idx
, page_idx
, ext_diff
);
1113 * If extent(s) are being appended to the last page in
1114 * the indirection array and the new extent(s) don't fit
1115 * in the page, then erp is NULL and erp_idx is set to
1116 * the next index needed in the indirection array.
1119 uint count
= ext_diff
;
1122 erp
= xfs_iext_irec_new(ifp
, erp_idx
);
1123 erp
->er_extcount
= min(count
, XFS_LINEAR_EXTS
);
1124 count
-= erp
->er_extcount
;
1130 ifp
->if_bytes
= new_size
;
1134 * This is called when incore extents are being added to the indirection
1135 * array and the new extents do not fit in the target extent list. The
1136 * erp_idx parameter contains the irec index for the target extent list
1137 * in the indirection array, and the idx parameter contains the extent
1138 * index within the list. The number of extents being added is stored
1139 * in the count parameter.
1141 * |-------| |-------|
1142 * | | | | idx - number of extents before idx
1144 * | | | | count - number of extents being inserted at idx
1145 * |-------| |-------|
1146 * | count | | nex2 | nex2 - number of extents after idx + count
1147 * |-------| |-------|
1150 xfs_iext_add_indirect_multi(
1151 xfs_ifork_t
*ifp
, /* inode fork pointer */
1152 int erp_idx
, /* target extent irec index */
1153 xfs_extnum_t idx
, /* index within target list */
1154 int count
) /* new extents being added */
1156 int byte_diff
; /* new bytes being added */
1157 xfs_ext_irec_t
*erp
; /* pointer to irec entry */
1158 xfs_extnum_t ext_diff
; /* number of extents to add */
1159 xfs_extnum_t ext_cnt
; /* new extents still needed */
1160 xfs_extnum_t nex2
; /* extents after idx + count */
1161 xfs_bmbt_rec_t
*nex2_ep
= NULL
; /* temp list for nex2 extents */
1162 int nlists
; /* number of irec's (lists) */
1164 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1165 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1166 nex2
= erp
->er_extcount
- idx
;
1167 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1170 * Save second part of target extent list
1171 * (all extents past */
1173 byte_diff
= nex2
* sizeof(xfs_bmbt_rec_t
);
1174 nex2_ep
= (xfs_bmbt_rec_t
*) kmem_alloc(byte_diff
, KM_NOFS
);
1175 memmove(nex2_ep
, &erp
->er_extbuf
[idx
], byte_diff
);
1176 erp
->er_extcount
-= nex2
;
1177 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, -nex2
);
1178 memset(&erp
->er_extbuf
[idx
], 0, byte_diff
);
1182 * Add the new extents to the end of the target
1183 * list, then allocate new irec record(s) and
1184 * extent buffer(s) as needed to store the rest
1185 * of the new extents.
1188 ext_diff
= MIN(ext_cnt
, (int)XFS_LINEAR_EXTS
- erp
->er_extcount
);
1190 erp
->er_extcount
+= ext_diff
;
1191 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, ext_diff
);
1192 ext_cnt
-= ext_diff
;
1196 erp
= xfs_iext_irec_new(ifp
, erp_idx
);
1197 ext_diff
= MIN(ext_cnt
, (int)XFS_LINEAR_EXTS
);
1198 erp
->er_extcount
= ext_diff
;
1199 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, ext_diff
);
1200 ext_cnt
-= ext_diff
;
1203 /* Add nex2 extents back to indirection array */
1205 xfs_extnum_t ext_avail
;
1208 byte_diff
= nex2
* sizeof(xfs_bmbt_rec_t
);
1209 ext_avail
= XFS_LINEAR_EXTS
- erp
->er_extcount
;
1212 * If nex2 extents fit in the current page, append
1213 * nex2_ep after the new extents.
1215 if (nex2
<= ext_avail
) {
1216 i
= erp
->er_extcount
;
1219 * Otherwise, check if space is available in the
1222 else if ((erp_idx
< nlists
- 1) &&
1223 (nex2
<= (ext_avail
= XFS_LINEAR_EXTS
-
1224 ifp
->if_u1
.if_ext_irec
[erp_idx
+1].er_extcount
))) {
1227 /* Create a hole for nex2 extents */
1228 memmove(&erp
->er_extbuf
[nex2
], erp
->er_extbuf
,
1229 erp
->er_extcount
* sizeof(xfs_bmbt_rec_t
));
1232 * Final choice, create a new extent page for
1237 erp
= xfs_iext_irec_new(ifp
, erp_idx
);
1239 memmove(&erp
->er_extbuf
[i
], nex2_ep
, byte_diff
);
1241 erp
->er_extcount
+= nex2
;
1242 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, nex2
);
1247 * This is called when the amount of space required for incore file
1248 * extents needs to be decreased. The ext_diff parameter stores the
1249 * number of extents to be removed and the idx parameter contains
1250 * the extent index where the extents will be removed from.
1252 * If the amount of space needed has decreased below the linear
1253 * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
1254 * extent array. Otherwise, use kmem_realloc() to adjust the
1255 * size to what is needed.
1259 xfs_inode_t
*ip
, /* incore inode pointer */
1260 xfs_extnum_t idx
, /* index to begin removing exts */
1261 int ext_diff
, /* number of extents to remove */
1262 int state
) /* type of extent conversion */
1264 xfs_ifork_t
*ifp
= xfs_iext_state_to_fork(ip
, state
);
1265 xfs_extnum_t nextents
; /* number of extents in file */
1266 int new_size
; /* size of extents after removal */
1268 trace_xfs_iext_remove(ip
, idx
, state
, _RET_IP_
);
1270 ASSERT(ext_diff
> 0);
1271 nextents
= xfs_iext_count(ifp
);
1272 new_size
= (nextents
- ext_diff
) * sizeof(xfs_bmbt_rec_t
);
1274 if (new_size
== 0) {
1275 xfs_iext_destroy(ifp
);
1276 } else if (ifp
->if_flags
& XFS_IFEXTIREC
) {
1277 xfs_iext_remove_indirect(ifp
, idx
, ext_diff
);
1278 } else if (ifp
->if_real_bytes
) {
1279 xfs_iext_remove_direct(ifp
, idx
, ext_diff
);
1281 xfs_iext_remove_inline(ifp
, idx
, ext_diff
);
1283 ifp
->if_bytes
= new_size
;
1287 * This removes ext_diff extents from the inline buffer, beginning
1288 * at extent index idx.
1291 xfs_iext_remove_inline(
1292 xfs_ifork_t
*ifp
, /* inode fork pointer */
1293 xfs_extnum_t idx
, /* index to begin removing exts */
1294 int ext_diff
) /* number of extents to remove */
1296 int nextents
; /* number of extents in file */
1298 ASSERT(!(ifp
->if_flags
& XFS_IFEXTIREC
));
1299 ASSERT(idx
< XFS_INLINE_EXTS
);
1300 nextents
= xfs_iext_count(ifp
);
1301 ASSERT(((nextents
- ext_diff
) > 0) &&
1302 (nextents
- ext_diff
) < XFS_INLINE_EXTS
);
1304 if (idx
+ ext_diff
< nextents
) {
1305 memmove(&ifp
->if_u2
.if_inline_ext
[idx
],
1306 &ifp
->if_u2
.if_inline_ext
[idx
+ ext_diff
],
1307 (nextents
- (idx
+ ext_diff
)) *
1308 sizeof(xfs_bmbt_rec_t
));
1309 memset(&ifp
->if_u2
.if_inline_ext
[nextents
- ext_diff
],
1310 0, ext_diff
* sizeof(xfs_bmbt_rec_t
));
1312 memset(&ifp
->if_u2
.if_inline_ext
[idx
], 0,
1313 ext_diff
* sizeof(xfs_bmbt_rec_t
));
1318 * This removes ext_diff extents from a linear (direct) extent list,
1319 * beginning at extent index idx. If the extents are being removed
1320 * from the end of the list (ie. truncate) then we just need to re-
1321 * allocate the list to remove the extra space. Otherwise, if the
1322 * extents are being removed from the middle of the existing extent
1323 * entries, then we first need to move the extent records beginning
1324 * at idx + ext_diff up in the list to overwrite the records being
1325 * removed, then remove the extra space via kmem_realloc.
1328 xfs_iext_remove_direct(
1329 xfs_ifork_t
*ifp
, /* inode fork pointer */
1330 xfs_extnum_t idx
, /* index to begin removing exts */
1331 int ext_diff
) /* number of extents to remove */
1333 xfs_extnum_t nextents
; /* number of extents in file */
1334 int new_size
; /* size of extents after removal */
1336 ASSERT(!(ifp
->if_flags
& XFS_IFEXTIREC
));
1337 new_size
= ifp
->if_bytes
-
1338 (ext_diff
* sizeof(xfs_bmbt_rec_t
));
1339 nextents
= xfs_iext_count(ifp
);
1341 if (new_size
== 0) {
1342 xfs_iext_destroy(ifp
);
1345 /* Move extents up in the list (if needed) */
1346 if (idx
+ ext_diff
< nextents
) {
1347 memmove(&ifp
->if_u1
.if_extents
[idx
],
1348 &ifp
->if_u1
.if_extents
[idx
+ ext_diff
],
1349 (nextents
- (idx
+ ext_diff
)) *
1350 sizeof(xfs_bmbt_rec_t
));
1352 memset(&ifp
->if_u1
.if_extents
[nextents
- ext_diff
],
1353 0, ext_diff
* sizeof(xfs_bmbt_rec_t
));
1355 * Reallocate the direct extent list. If the extents
1356 * will fit inside the inode then xfs_iext_realloc_direct
1357 * will switch from direct to inline extent allocation
1360 xfs_iext_realloc_direct(ifp
, new_size
);
1361 ifp
->if_bytes
= new_size
;
1365 * This is called when incore extents are being removed from the
1366 * indirection array and the extents being removed span multiple extent
1367 * buffers. The idx parameter contains the file extent index where we
1368 * want to begin removing extents, and the count parameter contains
1369 * how many extents need to be removed.
1371 * |-------| |-------|
1372 * | nex1 | | | nex1 - number of extents before idx
1373 * |-------| | count |
1374 * | | | | count - number of extents being removed at idx
1375 * | count | |-------|
1376 * | | | nex2 | nex2 - number of extents after idx + count
1377 * |-------| |-------|
1380 xfs_iext_remove_indirect(
1381 xfs_ifork_t
*ifp
, /* inode fork pointer */
1382 xfs_extnum_t idx
, /* index to begin removing extents */
1383 int count
) /* number of extents to remove */
1385 xfs_ext_irec_t
*erp
; /* indirection array pointer */
1386 int erp_idx
= 0; /* indirection array index */
1387 xfs_extnum_t ext_cnt
; /* extents left to remove */
1388 xfs_extnum_t ext_diff
; /* extents to remove in current list */
1389 xfs_extnum_t nex1
; /* number of extents before idx */
1390 xfs_extnum_t nex2
; /* extents after idx + count */
1391 int page_idx
= idx
; /* index in target extent list */
1393 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1394 erp
= xfs_iext_idx_to_irec(ifp
, &page_idx
, &erp_idx
, 0);
1395 ASSERT(erp
!= NULL
);
1399 nex2
= MAX((erp
->er_extcount
- (nex1
+ ext_cnt
)), 0);
1400 ext_diff
= MIN(ext_cnt
, (erp
->er_extcount
- nex1
));
1402 * Check for deletion of entire list;
1403 * xfs_iext_irec_remove() updates extent offsets.
1405 if (ext_diff
== erp
->er_extcount
) {
1406 xfs_iext_irec_remove(ifp
, erp_idx
);
1407 ext_cnt
-= ext_diff
;
1410 ASSERT(erp_idx
< ifp
->if_real_bytes
/
1412 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1419 /* Move extents up (if needed) */
1421 memmove(&erp
->er_extbuf
[nex1
],
1422 &erp
->er_extbuf
[nex1
+ ext_diff
],
1423 nex2
* sizeof(xfs_bmbt_rec_t
));
1425 /* Zero out rest of page */
1426 memset(&erp
->er_extbuf
[nex1
+ nex2
], 0, (XFS_IEXT_BUFSZ
-
1427 ((nex1
+ nex2
) * sizeof(xfs_bmbt_rec_t
))));
1428 /* Update remaining counters */
1429 erp
->er_extcount
-= ext_diff
;
1430 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, -ext_diff
);
1431 ext_cnt
-= ext_diff
;
1436 ifp
->if_bytes
-= count
* sizeof(xfs_bmbt_rec_t
);
1437 xfs_iext_irec_compact(ifp
);
1441 * Create, destroy, or resize a linear (direct) block of extents.
1444 xfs_iext_realloc_direct(
1445 xfs_ifork_t
*ifp
, /* inode fork pointer */
1446 int new_size
) /* new size of extents after adding */
1448 int rnew_size
; /* real new size of extents */
1450 rnew_size
= new_size
;
1452 ASSERT(!(ifp
->if_flags
& XFS_IFEXTIREC
) ||
1453 ((new_size
>= 0) && (new_size
<= XFS_IEXT_BUFSZ
) &&
1454 (new_size
!= ifp
->if_real_bytes
)));
1456 /* Free extent records */
1457 if (new_size
== 0) {
1458 xfs_iext_destroy(ifp
);
1460 /* Resize direct extent list and zero any new bytes */
1461 else if (ifp
->if_real_bytes
) {
1462 /* Check if extents will fit inside the inode */
1463 if (new_size
<= XFS_INLINE_EXTS
* sizeof(xfs_bmbt_rec_t
)) {
1464 xfs_iext_direct_to_inline(ifp
, new_size
/
1465 (uint
)sizeof(xfs_bmbt_rec_t
));
1466 ifp
->if_bytes
= new_size
;
1469 if (!is_power_of_2(new_size
)){
1470 rnew_size
= roundup_pow_of_two(new_size
);
1472 if (rnew_size
!= ifp
->if_real_bytes
) {
1473 ifp
->if_u1
.if_extents
=
1474 kmem_realloc(ifp
->if_u1
.if_extents
,
1475 rnew_size
, KM_NOFS
);
1477 if (rnew_size
> ifp
->if_real_bytes
) {
1478 memset(&ifp
->if_u1
.if_extents
[ifp
->if_bytes
/
1479 (uint
)sizeof(xfs_bmbt_rec_t
)], 0,
1480 rnew_size
- ifp
->if_real_bytes
);
1483 /* Switch from the inline extent buffer to a direct extent list */
1485 if (!is_power_of_2(new_size
)) {
1486 rnew_size
= roundup_pow_of_two(new_size
);
1488 xfs_iext_inline_to_direct(ifp
, rnew_size
);
1490 ifp
->if_real_bytes
= rnew_size
;
1491 ifp
->if_bytes
= new_size
;
1495 * Switch from linear (direct) extent records to inline buffer.
1498 xfs_iext_direct_to_inline(
1499 xfs_ifork_t
*ifp
, /* inode fork pointer */
1500 xfs_extnum_t nextents
) /* number of extents in file */
1502 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1503 ASSERT(nextents
<= XFS_INLINE_EXTS
);
1505 * The inline buffer was zeroed when we switched
1506 * from inline to direct extent allocation mode,
1507 * so we don't need to clear it here.
1509 memcpy(ifp
->if_u2
.if_inline_ext
, ifp
->if_u1
.if_extents
,
1510 nextents
* sizeof(xfs_bmbt_rec_t
));
1511 kmem_free(ifp
->if_u1
.if_extents
);
1512 ifp
->if_u1
.if_extents
= ifp
->if_u2
.if_inline_ext
;
1513 ifp
->if_real_bytes
= 0;
1517 * Switch from inline buffer to linear (direct) extent records.
1518 * new_size should already be rounded up to the next power of 2
1519 * by the caller (when appropriate), so use new_size as it is.
1520 * However, since new_size may be rounded up, we can't update
1521 * if_bytes here. It is the caller's responsibility to update
1522 * if_bytes upon return.
1525 xfs_iext_inline_to_direct(
1526 xfs_ifork_t
*ifp
, /* inode fork pointer */
1527 int new_size
) /* number of extents in file */
1529 ifp
->if_u1
.if_extents
= kmem_alloc(new_size
, KM_NOFS
);
1530 memset(ifp
->if_u1
.if_extents
, 0, new_size
);
1531 if (ifp
->if_bytes
) {
1532 memcpy(ifp
->if_u1
.if_extents
, ifp
->if_u2
.if_inline_ext
,
1534 memset(ifp
->if_u2
.if_inline_ext
, 0, XFS_INLINE_EXTS
*
1535 sizeof(xfs_bmbt_rec_t
));
1537 ifp
->if_real_bytes
= new_size
;
1541 * Resize an extent indirection array to new_size bytes.
1544 xfs_iext_realloc_indirect(
1545 xfs_ifork_t
*ifp
, /* inode fork pointer */
1546 int new_size
) /* new indirection array size */
1549 int nlists
; /* number of irec's (ex lists) */
1550 int size
; /* current indirection array size */
1552 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1553 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1554 size
= nlists
* sizeof(xfs_ext_irec_t
);
1555 ASSERT(ifp
->if_real_bytes
);
1556 ASSERT((new_size
>= 0) && (new_size
!= size
));
1558 if (new_size
== 0) {
1559 xfs_iext_destroy(ifp
);
1561 ifp
->if_u1
.if_ext_irec
=
1562 kmem_realloc(ifp
->if_u1
.if_ext_irec
, new_size
, KM_NOFS
);
1567 * Switch from indirection array to linear (direct) extent allocations.
1570 xfs_iext_indirect_to_direct(
1571 xfs_ifork_t
*ifp
) /* inode fork pointer */
1573 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1574 xfs_extnum_t nextents
; /* number of extents in file */
1575 int size
; /* size of file extents */
1577 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1578 nextents
= xfs_iext_count(ifp
);
1579 ASSERT(nextents
<= XFS_LINEAR_EXTS
);
1580 size
= nextents
* sizeof(xfs_bmbt_rec_t
);
1582 xfs_iext_irec_compact_pages(ifp
);
1583 ASSERT(ifp
->if_real_bytes
== XFS_IEXT_BUFSZ
);
1585 ep
= ifp
->if_u1
.if_ext_irec
->er_extbuf
;
1586 kmem_free(ifp
->if_u1
.if_ext_irec
);
1587 ifp
->if_flags
&= ~XFS_IFEXTIREC
;
1588 ifp
->if_u1
.if_extents
= ep
;
1589 ifp
->if_bytes
= size
;
1590 if (nextents
< XFS_LINEAR_EXTS
) {
1591 xfs_iext_realloc_direct(ifp
, size
);
1596 * Remove all records from the indirection array.
1599 xfs_iext_irec_remove_all(
1600 struct xfs_ifork
*ifp
)
1605 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1606 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1607 for (i
= 0; i
< nlists
; i
++)
1608 kmem_free(ifp
->if_u1
.if_ext_irec
[i
].er_extbuf
);
1609 kmem_free(ifp
->if_u1
.if_ext_irec
);
1610 ifp
->if_flags
&= ~XFS_IFEXTIREC
;
1614 * Free incore file extents.
1618 xfs_ifork_t
*ifp
) /* inode fork pointer */
1620 if (ifp
->if_flags
& XFS_IFEXTIREC
) {
1621 xfs_iext_irec_remove_all(ifp
);
1622 } else if (ifp
->if_real_bytes
) {
1623 kmem_free(ifp
->if_u1
.if_extents
);
1624 } else if (ifp
->if_bytes
) {
1625 memset(ifp
->if_u2
.if_inline_ext
, 0, XFS_INLINE_EXTS
*
1626 sizeof(xfs_bmbt_rec_t
));
1628 ifp
->if_u1
.if_extents
= NULL
;
1629 ifp
->if_real_bytes
= 0;
1634 * Return a pointer to the extent record for file system block bno.
1636 xfs_bmbt_rec_host_t
* /* pointer to found extent record */
1637 xfs_iext_bno_to_ext(
1638 xfs_ifork_t
*ifp
, /* inode fork pointer */
1639 xfs_fileoff_t bno
, /* block number to search for */
1640 xfs_extnum_t
*idxp
) /* index of target extent */
1642 xfs_bmbt_rec_host_t
*base
; /* pointer to first extent */
1643 xfs_filblks_t blockcount
= 0; /* number of blocks in extent */
1644 xfs_bmbt_rec_host_t
*ep
= NULL
; /* pointer to target extent */
1645 xfs_ext_irec_t
*erp
= NULL
; /* indirection array pointer */
1646 int high
; /* upper boundary in search */
1647 xfs_extnum_t idx
= 0; /* index of target extent */
1648 int low
; /* lower boundary in search */
1649 xfs_extnum_t nextents
; /* number of file extents */
1650 xfs_fileoff_t startoff
= 0; /* start offset of extent */
1652 nextents
= xfs_iext_count(ifp
);
1653 if (nextents
== 0) {
1658 if (ifp
->if_flags
& XFS_IFEXTIREC
) {
1659 /* Find target extent list */
1661 erp
= xfs_iext_bno_to_irec(ifp
, bno
, &erp_idx
);
1662 base
= erp
->er_extbuf
;
1663 high
= erp
->er_extcount
- 1;
1665 base
= ifp
->if_u1
.if_extents
;
1666 high
= nextents
- 1;
1668 /* Binary search extent records */
1669 while (low
<= high
) {
1670 idx
= (low
+ high
) >> 1;
1672 startoff
= xfs_bmbt_get_startoff(ep
);
1673 blockcount
= xfs_bmbt_get_blockcount(ep
);
1674 if (bno
< startoff
) {
1676 } else if (bno
>= startoff
+ blockcount
) {
1679 /* Convert back to file-based extent index */
1680 if (ifp
->if_flags
& XFS_IFEXTIREC
) {
1681 idx
+= erp
->er_extoff
;
1687 /* Convert back to file-based extent index */
1688 if (ifp
->if_flags
& XFS_IFEXTIREC
) {
1689 idx
+= erp
->er_extoff
;
1691 if (bno
>= startoff
+ blockcount
) {
1692 if (++idx
== nextents
) {
1695 ep
= xfs_iext_get_ext(ifp
, idx
);
1703 * Return a pointer to the indirection array entry containing the
1704 * extent record for filesystem block bno. Store the index of the
1705 * target irec in *erp_idxp.
1707 xfs_ext_irec_t
* /* pointer to found extent record */
1708 xfs_iext_bno_to_irec(
1709 xfs_ifork_t
*ifp
, /* inode fork pointer */
1710 xfs_fileoff_t bno
, /* block number to search for */
1711 int *erp_idxp
) /* irec index of target ext list */
1713 xfs_ext_irec_t
*erp
= NULL
; /* indirection array pointer */
1714 xfs_ext_irec_t
*erp_next
; /* next indirection array entry */
1715 int erp_idx
; /* indirection array index */
1716 int nlists
; /* number of extent irec's (lists) */
1717 int high
; /* binary search upper limit */
1718 int low
; /* binary search lower limit */
1720 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1721 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1725 while (low
<= high
) {
1726 erp_idx
= (low
+ high
) >> 1;
1727 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1728 erp_next
= erp_idx
< nlists
- 1 ? erp
+ 1 : NULL
;
1729 if (bno
< xfs_bmbt_get_startoff(erp
->er_extbuf
)) {
1731 } else if (erp_next
&& bno
>=
1732 xfs_bmbt_get_startoff(erp_next
->er_extbuf
)) {
1738 *erp_idxp
= erp_idx
;
1743 * Return a pointer to the indirection array entry containing the
1744 * extent record at file extent index *idxp. Store the index of the
1745 * target irec in *erp_idxp and store the page index of the target
1746 * extent record in *idxp.
1749 xfs_iext_idx_to_irec(
1750 xfs_ifork_t
*ifp
, /* inode fork pointer */
1751 xfs_extnum_t
*idxp
, /* extent index (file -> page) */
1752 int *erp_idxp
, /* pointer to target irec */
1753 int realloc
) /* new bytes were just added */
1755 xfs_ext_irec_t
*prev
; /* pointer to previous irec */
1756 xfs_ext_irec_t
*erp
= NULL
; /* pointer to current irec */
1757 int erp_idx
; /* indirection array index */
1758 int nlists
; /* number of irec's (ex lists) */
1759 int high
; /* binary search upper limit */
1760 int low
; /* binary search lower limit */
1761 xfs_extnum_t page_idx
= *idxp
; /* extent index in target list */
1763 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1764 ASSERT(page_idx
>= 0);
1765 ASSERT(page_idx
<= xfs_iext_count(ifp
));
1766 ASSERT(page_idx
< xfs_iext_count(ifp
) || realloc
);
1768 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1773 /* Binary search extent irec's */
1774 while (low
<= high
) {
1775 erp_idx
= (low
+ high
) >> 1;
1776 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1777 prev
= erp_idx
> 0 ? erp
- 1 : NULL
;
1778 if (page_idx
< erp
->er_extoff
|| (page_idx
== erp
->er_extoff
&&
1779 realloc
&& prev
&& prev
->er_extcount
< XFS_LINEAR_EXTS
)) {
1781 } else if (page_idx
> erp
->er_extoff
+ erp
->er_extcount
||
1782 (page_idx
== erp
->er_extoff
+ erp
->er_extcount
&&
1785 } else if (page_idx
== erp
->er_extoff
+ erp
->er_extcount
&&
1786 erp
->er_extcount
== XFS_LINEAR_EXTS
) {
1790 erp
= erp_idx
< nlists
? erp
+ 1 : NULL
;
1793 page_idx
-= erp
->er_extoff
;
1798 *erp_idxp
= erp_idx
;
1803 * Allocate and initialize an indirection array once the space needed
1804 * for incore extents increases above XFS_IEXT_BUFSZ.
1808 xfs_ifork_t
*ifp
) /* inode fork pointer */
1810 xfs_ext_irec_t
*erp
; /* indirection array pointer */
1811 xfs_extnum_t nextents
; /* number of extents in file */
1813 ASSERT(!(ifp
->if_flags
& XFS_IFEXTIREC
));
1814 nextents
= xfs_iext_count(ifp
);
1815 ASSERT(nextents
<= XFS_LINEAR_EXTS
);
1817 erp
= kmem_alloc(sizeof(xfs_ext_irec_t
), KM_NOFS
);
1819 if (nextents
== 0) {
1820 ifp
->if_u1
.if_extents
= kmem_alloc(XFS_IEXT_BUFSZ
, KM_NOFS
);
1821 } else if (!ifp
->if_real_bytes
) {
1822 xfs_iext_inline_to_direct(ifp
, XFS_IEXT_BUFSZ
);
1823 } else if (ifp
->if_real_bytes
< XFS_IEXT_BUFSZ
) {
1824 xfs_iext_realloc_direct(ifp
, XFS_IEXT_BUFSZ
);
1826 erp
->er_extbuf
= ifp
->if_u1
.if_extents
;
1827 erp
->er_extcount
= nextents
;
1830 ifp
->if_flags
|= XFS_IFEXTIREC
;
1831 ifp
->if_real_bytes
= XFS_IEXT_BUFSZ
;
1832 ifp
->if_bytes
= nextents
* sizeof(xfs_bmbt_rec_t
);
1833 ifp
->if_u1
.if_ext_irec
= erp
;
1839 * Allocate and initialize a new entry in the indirection array.
1843 xfs_ifork_t
*ifp
, /* inode fork pointer */
1844 int erp_idx
) /* index for new irec */
1846 xfs_ext_irec_t
*erp
; /* indirection array pointer */
1847 int i
; /* loop counter */
1848 int nlists
; /* number of irec's (ex lists) */
1850 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1851 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1853 /* Resize indirection array */
1854 xfs_iext_realloc_indirect(ifp
, ++nlists
*
1855 sizeof(xfs_ext_irec_t
));
1857 * Move records down in the array so the
1858 * new page can use erp_idx.
1860 erp
= ifp
->if_u1
.if_ext_irec
;
1861 for (i
= nlists
- 1; i
> erp_idx
; i
--) {
1862 memmove(&erp
[i
], &erp
[i
-1], sizeof(xfs_ext_irec_t
));
1864 ASSERT(i
== erp_idx
);
1866 /* Initialize new extent record */
1867 erp
= ifp
->if_u1
.if_ext_irec
;
1868 erp
[erp_idx
].er_extbuf
= kmem_alloc(XFS_IEXT_BUFSZ
, KM_NOFS
);
1869 ifp
->if_real_bytes
= nlists
* XFS_IEXT_BUFSZ
;
1870 memset(erp
[erp_idx
].er_extbuf
, 0, XFS_IEXT_BUFSZ
);
1871 erp
[erp_idx
].er_extcount
= 0;
1872 erp
[erp_idx
].er_extoff
= erp_idx
> 0 ?
1873 erp
[erp_idx
-1].er_extoff
+ erp
[erp_idx
-1].er_extcount
: 0;
1874 return (&erp
[erp_idx
]);
1878 * Remove a record from the indirection array.
1881 xfs_iext_irec_remove(
1882 xfs_ifork_t
*ifp
, /* inode fork pointer */
1883 int erp_idx
) /* irec index to remove */
1885 xfs_ext_irec_t
*erp
; /* indirection array pointer */
1886 int i
; /* loop counter */
1887 int nlists
; /* number of irec's (ex lists) */
1889 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1890 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1891 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1892 if (erp
->er_extbuf
) {
1893 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1,
1895 kmem_free(erp
->er_extbuf
);
1897 /* Compact extent records */
1898 erp
= ifp
->if_u1
.if_ext_irec
;
1899 for (i
= erp_idx
; i
< nlists
- 1; i
++) {
1900 memmove(&erp
[i
], &erp
[i
+1], sizeof(xfs_ext_irec_t
));
1903 * Manually free the last extent record from the indirection
1904 * array. A call to xfs_iext_realloc_indirect() with a size
1905 * of zero would result in a call to xfs_iext_destroy() which
1906 * would in turn call this function again, creating a nasty
1910 xfs_iext_realloc_indirect(ifp
,
1911 nlists
* sizeof(xfs_ext_irec_t
));
1913 kmem_free(ifp
->if_u1
.if_ext_irec
);
1915 ifp
->if_real_bytes
= nlists
* XFS_IEXT_BUFSZ
;
1919 * This is called to clean up large amounts of unused memory allocated
1920 * by the indirection array. Before compacting anything though, verify
1921 * that the indirection array is still needed and switch back to the
1922 * linear extent list (or even the inline buffer) if possible. The
1923 * compaction policy is as follows:
1925 * Full Compaction: Extents fit into a single page (or inline buffer)
1926 * Partial Compaction: Extents occupy less than 50% of allocated space
1927 * No Compaction: Extents occupy at least 50% of allocated space
1930 xfs_iext_irec_compact(
1931 xfs_ifork_t
*ifp
) /* inode fork pointer */
1933 xfs_extnum_t nextents
; /* number of extents in file */
1934 int nlists
; /* number of irec's (ex lists) */
1936 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1937 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1938 nextents
= xfs_iext_count(ifp
);
1940 if (nextents
== 0) {
1941 xfs_iext_destroy(ifp
);
1942 } else if (nextents
<= XFS_INLINE_EXTS
) {
1943 xfs_iext_indirect_to_direct(ifp
);
1944 xfs_iext_direct_to_inline(ifp
, nextents
);
1945 } else if (nextents
<= XFS_LINEAR_EXTS
) {
1946 xfs_iext_indirect_to_direct(ifp
);
1947 } else if (nextents
< (nlists
* XFS_LINEAR_EXTS
) >> 1) {
1948 xfs_iext_irec_compact_pages(ifp
);
1953 * Combine extents from neighboring extent pages.
1956 xfs_iext_irec_compact_pages(
1957 xfs_ifork_t
*ifp
) /* inode fork pointer */
1959 xfs_ext_irec_t
*erp
, *erp_next
;/* pointers to irec entries */
1960 int erp_idx
= 0; /* indirection array index */
1961 int nlists
; /* number of irec's (ex lists) */
1963 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1964 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1965 while (erp_idx
< nlists
- 1) {
1966 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1968 if (erp_next
->er_extcount
<=
1969 (XFS_LINEAR_EXTS
- erp
->er_extcount
)) {
1970 memcpy(&erp
->er_extbuf
[erp
->er_extcount
],
1971 erp_next
->er_extbuf
, erp_next
->er_extcount
*
1972 sizeof(xfs_bmbt_rec_t
));
1973 erp
->er_extcount
+= erp_next
->er_extcount
;
1975 * Free page before removing extent record
1976 * so er_extoffs don't get modified in
1977 * xfs_iext_irec_remove.
1979 kmem_free(erp_next
->er_extbuf
);
1980 erp_next
->er_extbuf
= NULL
;
1981 xfs_iext_irec_remove(ifp
, erp_idx
+ 1);
1982 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1990 * This is called to update the er_extoff field in the indirection
1991 * array when extents have been added or removed from one of the
1992 * extent lists. erp_idx contains the irec index to begin updating
1993 * at and ext_diff contains the number of extents that were added
1997 xfs_iext_irec_update_extoffs(
1998 xfs_ifork_t
*ifp
, /* inode fork pointer */
1999 int erp_idx
, /* irec index to update */
2000 int ext_diff
) /* number of new extents */
2002 int i
; /* loop counter */
2003 int nlists
; /* number of irec's (ex lists */
2005 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
2006 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
2007 for (i
= erp_idx
; i
< nlists
; i
++) {
2008 ifp
->if_u1
.if_ext_irec
[i
].er_extoff
+= ext_diff
;
2013 * Initialize an inode's copy-on-write fork.
2017 struct xfs_inode
*ip
)
2022 ip
->i_cowfp
= kmem_zone_zalloc(xfs_ifork_zone
,
2023 KM_SLEEP
| KM_NOFS
);
2024 ip
->i_cowfp
->if_flags
= XFS_IFEXTENTS
;
2025 ip
->i_cformat
= XFS_DINODE_FMT_EXTENTS
;
2026 ip
->i_cnextents
= 0;
2030 * Lookup the extent covering bno.
2032 * If there is an extent covering bno return the extent index, and store the
2033 * expanded extent structure in *gotp, and the extent index in *idx.
2034 * If there is no extent covering bno, but there is an extent after it (e.g.
2035 * it lies in a hole) return that extent in *gotp and its index in *idx
2037 * If bno is beyond the last extent return false, and return the index after
2038 * the last valid index in *idxp.
2041 xfs_iext_lookup_extent(
2042 struct xfs_inode
*ip
,
2043 struct xfs_ifork
*ifp
,
2046 struct xfs_bmbt_irec
*gotp
)
2048 struct xfs_bmbt_rec_host
*ep
;
2050 XFS_STATS_INC(ip
->i_mount
, xs_look_exlist
);
2052 ep
= xfs_iext_bno_to_ext(ifp
, bno
, idxp
);
2055 xfs_bmbt_get_all(ep
, gotp
);
2060 * Return true if there is an extent at index idx, and return the expanded
2061 * extent structure at idx in that case. Else return false.
2064 xfs_iext_get_extent(
2065 struct xfs_ifork
*ifp
,
2067 struct xfs_bmbt_irec
*gotp
)
2069 if (idx
< 0 || idx
>= xfs_iext_count(ifp
))
2071 xfs_bmbt_get_all(xfs_iext_get_ext(ifp
, idx
), gotp
);