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_bmap_btree.h"
31 #include "xfs_error.h"
32 #include "xfs_trace.h"
33 #include "xfs_attr_sf.h"
34 #include "xfs_da_format.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.
324 * If the size is unreasonable, then something
325 * is wrong and we just bail out rather than crash in
326 * kmem_alloc() or memcpy() below.
328 if (unlikely(size
> XFS_DFORK_SIZE(dip
, ip
->i_mount
, whichfork
))) {
329 xfs_warn(ip
->i_mount
,
330 "corrupt inode %Lu (bad size %d for local fork, size = %d).",
331 (unsigned long long) ip
->i_ino
, size
,
332 XFS_DFORK_SIZE(dip
, ip
->i_mount
, whichfork
));
333 XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW
,
335 return -EFSCORRUPTED
;
338 xfs_init_local_fork(ip
, whichfork
, XFS_DFORK_PTR(dip
, whichfork
), size
);
343 * The file consists of a set of extents all
344 * of which fit into the on-disk inode.
345 * If there are few enough extents to fit into
346 * the if_inline_ext, then copy them there.
347 * Otherwise allocate a buffer for them and copy
348 * them into it. Either way, set if_extents
349 * to point at the extents.
363 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
364 nex
= XFS_DFORK_NEXTENTS(dip
, whichfork
);
365 size
= nex
* (uint
)sizeof(xfs_bmbt_rec_t
);
368 * If the number of extents is unreasonable, then something
369 * is wrong and we just bail out rather than crash in
370 * kmem_alloc() or memcpy() below.
372 if (unlikely(size
< 0 || size
> XFS_DFORK_SIZE(dip
, ip
->i_mount
, whichfork
))) {
373 xfs_warn(ip
->i_mount
, "corrupt inode %Lu ((a)extents = %d).",
374 (unsigned long long) ip
->i_ino
, nex
);
375 XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW
,
377 return -EFSCORRUPTED
;
380 ifp
->if_real_bytes
= 0;
382 ifp
->if_u1
.if_extents
= NULL
;
383 else if (nex
<= XFS_INLINE_EXTS
)
384 ifp
->if_u1
.if_extents
= ifp
->if_u2
.if_inline_ext
;
386 xfs_iext_add(ifp
, 0, nex
);
388 ifp
->if_bytes
= size
;
390 dp
= (xfs_bmbt_rec_t
*) XFS_DFORK_PTR(dip
, whichfork
);
391 xfs_validate_extents(ifp
, nex
, XFS_EXTFMT_INODE(ip
));
392 for (i
= 0; i
< nex
; i
++, dp
++) {
393 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, i
);
394 ep
->l0
= get_unaligned_be64(&dp
->l0
);
395 ep
->l1
= get_unaligned_be64(&dp
->l1
);
397 XFS_BMAP_TRACE_EXLIST(ip
, nex
, whichfork
);
398 if (whichfork
!= XFS_DATA_FORK
||
399 XFS_EXTFMT_INODE(ip
) == XFS_EXTFMT_NOSTATE
)
400 if (unlikely(xfs_check_nostate_extents(
402 XFS_ERROR_REPORT("xfs_iformat_extents(2)",
405 return -EFSCORRUPTED
;
408 ifp
->if_flags
|= XFS_IFEXTENTS
;
413 * The file has too many extents to fit into
414 * the inode, so they are in B-tree format.
415 * Allocate a buffer for the root of the B-tree
416 * and copy the root into it. The i_extents
417 * field will remain NULL until all of the
418 * extents are read in (when they are needed).
426 struct xfs_mount
*mp
= ip
->i_mount
;
427 xfs_bmdr_block_t
*dfp
;
433 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
434 dfp
= (xfs_bmdr_block_t
*)XFS_DFORK_PTR(dip
, whichfork
);
435 size
= XFS_BMAP_BROOT_SPACE(mp
, dfp
);
436 nrecs
= be16_to_cpu(dfp
->bb_numrecs
);
439 * blow out if -- fork has less extents than can fit in
440 * fork (fork shouldn't be a btree format), root btree
441 * block has more records than can fit into the fork,
442 * or the number of extents is greater than the number of
445 if (unlikely(XFS_IFORK_NEXTENTS(ip
, whichfork
) <=
446 XFS_IFORK_MAXEXT(ip
, whichfork
) ||
447 XFS_BMDR_SPACE_CALC(nrecs
) >
448 XFS_DFORK_SIZE(dip
, mp
, whichfork
) ||
449 XFS_IFORK_NEXTENTS(ip
, whichfork
) > ip
->i_d
.di_nblocks
)) {
450 xfs_warn(mp
, "corrupt inode %Lu (btree).",
451 (unsigned long long) ip
->i_ino
);
452 XFS_CORRUPTION_ERROR("xfs_iformat_btree", XFS_ERRLEVEL_LOW
,
454 return -EFSCORRUPTED
;
457 ifp
->if_broot_bytes
= size
;
458 ifp
->if_broot
= kmem_alloc(size
, KM_SLEEP
| KM_NOFS
);
459 ASSERT(ifp
->if_broot
!= NULL
);
461 * Copy and convert from the on-disk structure
462 * to the in-memory structure.
464 xfs_bmdr_to_bmbt(ip
, dfp
, XFS_DFORK_SIZE(dip
, ip
->i_mount
, whichfork
),
465 ifp
->if_broot
, size
);
466 ifp
->if_flags
&= ~XFS_IFEXTENTS
;
467 ifp
->if_flags
|= XFS_IFBROOT
;
473 * Read in extents from a btree-format inode.
474 * Allocate and fill in if_extents. Real work is done in xfs_bmap.c.
484 xfs_extnum_t nextents
;
486 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
488 if (unlikely(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
489 XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW
,
491 return -EFSCORRUPTED
;
493 nextents
= XFS_IFORK_NEXTENTS(ip
, whichfork
);
494 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
497 * We know that the size is valid (it's checked in iformat_btree)
499 ifp
->if_bytes
= ifp
->if_real_bytes
= 0;
500 ifp
->if_flags
|= XFS_IFEXTENTS
;
501 xfs_iext_add(ifp
, 0, nextents
);
502 error
= xfs_bmap_read_extents(tp
, ip
, whichfork
);
504 xfs_iext_destroy(ifp
);
505 ifp
->if_flags
&= ~XFS_IFEXTENTS
;
508 xfs_validate_extents(ifp
, nextents
, XFS_EXTFMT_INODE(ip
));
512 * Reallocate the space for if_broot based on the number of records
513 * being added or deleted as indicated in rec_diff. Move the records
514 * and pointers in if_broot to fit the new size. When shrinking this
515 * will eliminate holes between the records and pointers created by
516 * the caller. When growing this will create holes to be filled in
519 * The caller must not request to add more records than would fit in
520 * the on-disk inode root. If the if_broot is currently NULL, then
521 * if we are adding records, one will be allocated. The caller must also
522 * not request that the number of records go below zero, although
525 * ip -- the inode whose if_broot area is changing
526 * ext_diff -- the change in the number of records, positive or negative,
527 * requested for the if_broot array.
535 struct xfs_mount
*mp
= ip
->i_mount
;
538 struct xfs_btree_block
*new_broot
;
545 * Handle the degenerate case quietly.
551 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
554 * If there wasn't any memory allocated before, just
555 * allocate it now and get out.
557 if (ifp
->if_broot_bytes
== 0) {
558 new_size
= XFS_BMAP_BROOT_SPACE_CALC(mp
, rec_diff
);
559 ifp
->if_broot
= kmem_alloc(new_size
, KM_SLEEP
| KM_NOFS
);
560 ifp
->if_broot_bytes
= (int)new_size
;
565 * If there is already an existing if_broot, then we need
566 * to realloc() it and shift the pointers to their new
567 * location. The records don't change location because
568 * they are kept butted up against the btree block header.
570 cur_max
= xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0);
571 new_max
= cur_max
+ rec_diff
;
572 new_size
= XFS_BMAP_BROOT_SPACE_CALC(mp
, new_max
);
573 ifp
->if_broot
= kmem_realloc(ifp
->if_broot
, new_size
,
575 op
= (char *)XFS_BMAP_BROOT_PTR_ADDR(mp
, ifp
->if_broot
, 1,
576 ifp
->if_broot_bytes
);
577 np
= (char *)XFS_BMAP_BROOT_PTR_ADDR(mp
, ifp
->if_broot
, 1,
579 ifp
->if_broot_bytes
= (int)new_size
;
580 ASSERT(XFS_BMAP_BMDR_SPACE(ifp
->if_broot
) <=
581 XFS_IFORK_SIZE(ip
, whichfork
));
582 memmove(np
, op
, cur_max
* (uint
)sizeof(xfs_fsblock_t
));
587 * rec_diff is less than 0. In this case, we are shrinking the
588 * if_broot buffer. It must already exist. If we go to zero
589 * records, just get rid of the root and clear the status bit.
591 ASSERT((ifp
->if_broot
!= NULL
) && (ifp
->if_broot_bytes
> 0));
592 cur_max
= xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0);
593 new_max
= cur_max
+ rec_diff
;
594 ASSERT(new_max
>= 0);
596 new_size
= XFS_BMAP_BROOT_SPACE_CALC(mp
, new_max
);
600 new_broot
= kmem_alloc(new_size
, KM_SLEEP
| KM_NOFS
);
602 * First copy over the btree block header.
604 memcpy(new_broot
, ifp
->if_broot
,
605 XFS_BMBT_BLOCK_LEN(ip
->i_mount
));
608 ifp
->if_flags
&= ~XFS_IFBROOT
;
612 * Only copy the records and pointers if there are any.
616 * First copy the records.
618 op
= (char *)XFS_BMBT_REC_ADDR(mp
, ifp
->if_broot
, 1);
619 np
= (char *)XFS_BMBT_REC_ADDR(mp
, new_broot
, 1);
620 memcpy(np
, op
, new_max
* (uint
)sizeof(xfs_bmbt_rec_t
));
623 * Then copy the pointers.
625 op
= (char *)XFS_BMAP_BROOT_PTR_ADDR(mp
, ifp
->if_broot
, 1,
626 ifp
->if_broot_bytes
);
627 np
= (char *)XFS_BMAP_BROOT_PTR_ADDR(mp
, new_broot
, 1,
629 memcpy(np
, op
, new_max
* (uint
)sizeof(xfs_fsblock_t
));
631 kmem_free(ifp
->if_broot
);
632 ifp
->if_broot
= new_broot
;
633 ifp
->if_broot_bytes
= (int)new_size
;
635 ASSERT(XFS_BMAP_BMDR_SPACE(ifp
->if_broot
) <=
636 XFS_IFORK_SIZE(ip
, whichfork
));
642 * This is called when the amount of space needed for if_data
643 * is increased or decreased. The change in size is indicated by
644 * the number of bytes that need to be added or deleted in the
645 * byte_diff parameter.
647 * If the amount of space needed has decreased below the size of the
648 * inline buffer, then switch to using the inline buffer. Otherwise,
649 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
652 * ip -- the inode whose if_data area is changing
653 * byte_diff -- the change in the number of bytes, positive or negative,
654 * requested for the if_data array.
666 if (byte_diff
== 0) {
670 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
671 new_size
= (int)ifp
->if_bytes
+ byte_diff
;
672 ASSERT(new_size
>= 0);
675 if (ifp
->if_u1
.if_data
!= ifp
->if_u2
.if_inline_data
) {
676 kmem_free(ifp
->if_u1
.if_data
);
678 ifp
->if_u1
.if_data
= NULL
;
680 } else if (new_size
<= sizeof(ifp
->if_u2
.if_inline_data
)) {
682 * If the valid extents/data can fit in if_inline_ext/data,
683 * copy them from the malloc'd vector and free it.
685 if (ifp
->if_u1
.if_data
== NULL
) {
686 ifp
->if_u1
.if_data
= ifp
->if_u2
.if_inline_data
;
687 } else if (ifp
->if_u1
.if_data
!= ifp
->if_u2
.if_inline_data
) {
688 ASSERT(ifp
->if_real_bytes
!= 0);
689 memcpy(ifp
->if_u2
.if_inline_data
, ifp
->if_u1
.if_data
,
691 kmem_free(ifp
->if_u1
.if_data
);
692 ifp
->if_u1
.if_data
= ifp
->if_u2
.if_inline_data
;
697 * Stuck with malloc/realloc.
698 * For inline data, the underlying buffer must be
699 * a multiple of 4 bytes in size so that it can be
700 * logged and stay on word boundaries. We enforce
703 real_size
= roundup(new_size
, 4);
704 if (ifp
->if_u1
.if_data
== NULL
) {
705 ASSERT(ifp
->if_real_bytes
== 0);
706 ifp
->if_u1
.if_data
= kmem_alloc(real_size
,
708 } else if (ifp
->if_u1
.if_data
!= ifp
->if_u2
.if_inline_data
) {
710 * Only do the realloc if the underlying size
711 * is really changing.
713 if (ifp
->if_real_bytes
!= real_size
) {
715 kmem_realloc(ifp
->if_u1
.if_data
,
720 ASSERT(ifp
->if_real_bytes
== 0);
721 ifp
->if_u1
.if_data
= kmem_alloc(real_size
,
723 memcpy(ifp
->if_u1
.if_data
, ifp
->if_u2
.if_inline_data
,
727 ifp
->if_real_bytes
= real_size
;
728 ifp
->if_bytes
= new_size
;
729 ASSERT(ifp
->if_bytes
<= XFS_IFORK_SIZE(ip
, whichfork
));
739 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
740 if (ifp
->if_broot
!= NULL
) {
741 kmem_free(ifp
->if_broot
);
742 ifp
->if_broot
= NULL
;
746 * If the format is local, then we can't have an extents
747 * array so just look for an inline data array. If we're
748 * not local then we may or may not have an extents list,
749 * so check and free it up if we do.
751 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
752 if ((ifp
->if_u1
.if_data
!= ifp
->if_u2
.if_inline_data
) &&
753 (ifp
->if_u1
.if_data
!= NULL
)) {
754 ASSERT(ifp
->if_real_bytes
!= 0);
755 kmem_free(ifp
->if_u1
.if_data
);
756 ifp
->if_u1
.if_data
= NULL
;
757 ifp
->if_real_bytes
= 0;
759 } else if ((ifp
->if_flags
& XFS_IFEXTENTS
) &&
760 ((ifp
->if_flags
& XFS_IFEXTIREC
) ||
761 ((ifp
->if_u1
.if_extents
!= NULL
) &&
762 (ifp
->if_u1
.if_extents
!= ifp
->if_u2
.if_inline_ext
)))) {
763 ASSERT(ifp
->if_real_bytes
!= 0);
764 xfs_iext_destroy(ifp
);
766 ASSERT(ifp
->if_u1
.if_extents
== NULL
||
767 ifp
->if_u1
.if_extents
== ifp
->if_u2
.if_inline_ext
);
768 ASSERT(ifp
->if_real_bytes
== 0);
769 if (whichfork
== XFS_ATTR_FORK
) {
770 kmem_zone_free(xfs_ifork_zone
, ip
->i_afp
);
772 } else if (whichfork
== XFS_COW_FORK
) {
773 kmem_zone_free(xfs_ifork_zone
, ip
->i_cowfp
);
779 * Convert in-core extents to on-disk form
781 * For either the data or attr fork in extent format, we need to endian convert
782 * the in-core extent as we place them into the on-disk inode.
784 * In the case of the data fork, the in-core and on-disk fork sizes can be
785 * different due to delayed allocation extents. We only copy on-disk extents
786 * here, so callers must always use the physical fork size to determine the
787 * size of the buffer passed to this routine. We will return the size actually
800 xfs_fsblock_t start_block
;
802 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
803 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
|XFS_ILOCK_SHARED
));
804 ASSERT(ifp
->if_bytes
> 0);
806 nrecs
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
807 XFS_BMAP_TRACE_EXLIST(ip
, nrecs
, whichfork
);
811 * There are some delayed allocation extents in the
812 * inode, so copy the extents one at a time and skip
813 * the delayed ones. There must be at least one
814 * non-delayed extent.
817 for (i
= 0; i
< nrecs
; i
++) {
818 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, i
);
819 start_block
= xfs_bmbt_get_startblock(ep
);
820 if (isnullstartblock(start_block
)) {
822 * It's a delayed allocation extent, so skip it.
827 /* Translate to on disk format */
828 put_unaligned_be64(ep
->l0
, &dp
->l0
);
829 put_unaligned_be64(ep
->l1
, &dp
->l1
);
834 xfs_validate_extents(ifp
, copied
, XFS_EXTFMT_INODE(ip
));
836 return (copied
* (uint
)sizeof(xfs_bmbt_rec_t
));
840 * Each of the following cases stores data into the same region
841 * of the on-disk inode, so only one of them can be valid at
842 * any given time. While it is possible to have conflicting formats
843 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
844 * in EXTENTS format, this can only happen when the fork has
845 * changed formats after being modified but before being flushed.
846 * In these cases, the format always takes precedence, because the
847 * format indicates the current state of the fork.
853 xfs_inode_log_item_t
*iip
,
859 static const short brootflag
[2] =
860 { XFS_ILOG_DBROOT
, XFS_ILOG_ABROOT
};
861 static const short dataflag
[2] =
862 { XFS_ILOG_DDATA
, XFS_ILOG_ADATA
};
863 static const short extflag
[2] =
864 { XFS_ILOG_DEXT
, XFS_ILOG_AEXT
};
868 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
870 * This can happen if we gave up in iformat in an error path,
871 * for the attribute fork.
874 ASSERT(whichfork
== XFS_ATTR_FORK
);
877 cp
= XFS_DFORK_PTR(dip
, whichfork
);
879 switch (XFS_IFORK_FORMAT(ip
, whichfork
)) {
880 case XFS_DINODE_FMT_LOCAL
:
881 if ((iip
->ili_fields
& dataflag
[whichfork
]) &&
882 (ifp
->if_bytes
> 0)) {
883 ASSERT(ifp
->if_u1
.if_data
!= NULL
);
884 ASSERT(ifp
->if_bytes
<= XFS_IFORK_SIZE(ip
, whichfork
));
885 memcpy(cp
, ifp
->if_u1
.if_data
, ifp
->if_bytes
);
889 case XFS_DINODE_FMT_EXTENTS
:
890 ASSERT((ifp
->if_flags
& XFS_IFEXTENTS
) ||
891 !(iip
->ili_fields
& extflag
[whichfork
]));
892 if ((iip
->ili_fields
& extflag
[whichfork
]) &&
893 (ifp
->if_bytes
> 0)) {
894 ASSERT(xfs_iext_get_ext(ifp
, 0));
895 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) > 0);
896 (void)xfs_iextents_copy(ip
, (xfs_bmbt_rec_t
*)cp
,
901 case XFS_DINODE_FMT_BTREE
:
902 if ((iip
->ili_fields
& brootflag
[whichfork
]) &&
903 (ifp
->if_broot_bytes
> 0)) {
904 ASSERT(ifp
->if_broot
!= NULL
);
905 ASSERT(XFS_BMAP_BMDR_SPACE(ifp
->if_broot
) <=
906 XFS_IFORK_SIZE(ip
, whichfork
));
907 xfs_bmbt_to_bmdr(mp
, ifp
->if_broot
, ifp
->if_broot_bytes
,
908 (xfs_bmdr_block_t
*)cp
,
909 XFS_DFORK_SIZE(dip
, mp
, whichfork
));
913 case XFS_DINODE_FMT_DEV
:
914 if (iip
->ili_fields
& XFS_ILOG_DEV
) {
915 ASSERT(whichfork
== XFS_DATA_FORK
);
916 xfs_dinode_put_rdev(dip
, ip
->i_df
.if_u2
.if_rdev
);
920 case XFS_DINODE_FMT_UUID
:
921 if (iip
->ili_fields
& XFS_ILOG_UUID
) {
922 ASSERT(whichfork
== XFS_DATA_FORK
);
923 memcpy(XFS_DFORK_DPTR(dip
),
924 &ip
->i_df
.if_u2
.if_uuid
,
936 * Return a pointer to the extent record at file index idx.
938 xfs_bmbt_rec_host_t
*
940 xfs_ifork_t
*ifp
, /* inode fork pointer */
941 xfs_extnum_t idx
) /* index of target extent */
944 ASSERT(idx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
));
946 if ((ifp
->if_flags
& XFS_IFEXTIREC
) && (idx
== 0)) {
947 return ifp
->if_u1
.if_ext_irec
->er_extbuf
;
948 } else if (ifp
->if_flags
& XFS_IFEXTIREC
) {
949 xfs_ext_irec_t
*erp
; /* irec pointer */
950 int erp_idx
= 0; /* irec index */
951 xfs_extnum_t page_idx
= idx
; /* ext index in target list */
953 erp
= xfs_iext_idx_to_irec(ifp
, &page_idx
, &erp_idx
, 0);
954 return &erp
->er_extbuf
[page_idx
];
955 } else if (ifp
->if_bytes
) {
956 return &ifp
->if_u1
.if_extents
[idx
];
962 /* Convert bmap state flags to an inode fork. */
964 xfs_iext_state_to_fork(
965 struct xfs_inode
*ip
,
968 if (state
& BMAP_COWFORK
)
970 else if (state
& BMAP_ATTRFORK
)
976 * Insert new item(s) into the extent records for incore inode
977 * fork 'ifp'. 'count' new items are inserted at index 'idx'.
981 xfs_inode_t
*ip
, /* incore inode pointer */
982 xfs_extnum_t idx
, /* starting index of new items */
983 xfs_extnum_t count
, /* number of inserted items */
984 xfs_bmbt_irec_t
*new, /* items to insert */
985 int state
) /* type of extent conversion */
987 xfs_ifork_t
*ifp
= xfs_iext_state_to_fork(ip
, state
);
988 xfs_extnum_t i
; /* extent record index */
990 trace_xfs_iext_insert(ip
, idx
, new, state
, _RET_IP_
);
992 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
993 xfs_iext_add(ifp
, idx
, count
);
994 for (i
= idx
; i
< idx
+ count
; i
++, new++)
995 xfs_bmbt_set_all(xfs_iext_get_ext(ifp
, i
), new);
999 * This is called when the amount of space required for incore file
1000 * extents needs to be increased. The ext_diff parameter stores the
1001 * number of new extents being added and the idx parameter contains
1002 * the extent index where the new extents will be added. If the new
1003 * extents are being appended, then we just need to (re)allocate and
1004 * initialize the space. Otherwise, if the new extents are being
1005 * inserted into the middle of the existing entries, a bit more work
1006 * is required to make room for the new extents to be inserted. The
1007 * caller is responsible for filling in the new extent entries upon
1012 xfs_ifork_t
*ifp
, /* inode fork pointer */
1013 xfs_extnum_t idx
, /* index to begin adding exts */
1014 int ext_diff
) /* number of extents to add */
1016 int byte_diff
; /* new bytes being added */
1017 int new_size
; /* size of extents after adding */
1018 xfs_extnum_t nextents
; /* number of extents in file */
1020 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1021 ASSERT((idx
>= 0) && (idx
<= nextents
));
1022 byte_diff
= ext_diff
* sizeof(xfs_bmbt_rec_t
);
1023 new_size
= ifp
->if_bytes
+ byte_diff
;
1025 * If the new number of extents (nextents + ext_diff)
1026 * fits inside the inode, then continue to use the inline
1029 if (nextents
+ ext_diff
<= XFS_INLINE_EXTS
) {
1030 if (idx
< nextents
) {
1031 memmove(&ifp
->if_u2
.if_inline_ext
[idx
+ ext_diff
],
1032 &ifp
->if_u2
.if_inline_ext
[idx
],
1033 (nextents
- idx
) * sizeof(xfs_bmbt_rec_t
));
1034 memset(&ifp
->if_u2
.if_inline_ext
[idx
], 0, byte_diff
);
1036 ifp
->if_u1
.if_extents
= ifp
->if_u2
.if_inline_ext
;
1037 ifp
->if_real_bytes
= 0;
1040 * Otherwise use a linear (direct) extent list.
1041 * If the extents are currently inside the inode,
1042 * xfs_iext_realloc_direct will switch us from
1043 * inline to direct extent allocation mode.
1045 else if (nextents
+ ext_diff
<= XFS_LINEAR_EXTS
) {
1046 xfs_iext_realloc_direct(ifp
, new_size
);
1047 if (idx
< nextents
) {
1048 memmove(&ifp
->if_u1
.if_extents
[idx
+ ext_diff
],
1049 &ifp
->if_u1
.if_extents
[idx
],
1050 (nextents
- idx
) * sizeof(xfs_bmbt_rec_t
));
1051 memset(&ifp
->if_u1
.if_extents
[idx
], 0, byte_diff
);
1054 /* Indirection array */
1056 xfs_ext_irec_t
*erp
;
1060 ASSERT(nextents
+ ext_diff
> XFS_LINEAR_EXTS
);
1061 if (ifp
->if_flags
& XFS_IFEXTIREC
) {
1062 erp
= xfs_iext_idx_to_irec(ifp
, &page_idx
, &erp_idx
, 1);
1064 xfs_iext_irec_init(ifp
);
1065 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1066 erp
= ifp
->if_u1
.if_ext_irec
;
1068 /* Extents fit in target extent page */
1069 if (erp
&& erp
->er_extcount
+ ext_diff
<= XFS_LINEAR_EXTS
) {
1070 if (page_idx
< erp
->er_extcount
) {
1071 memmove(&erp
->er_extbuf
[page_idx
+ ext_diff
],
1072 &erp
->er_extbuf
[page_idx
],
1073 (erp
->er_extcount
- page_idx
) *
1074 sizeof(xfs_bmbt_rec_t
));
1075 memset(&erp
->er_extbuf
[page_idx
], 0, byte_diff
);
1077 erp
->er_extcount
+= ext_diff
;
1078 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, ext_diff
);
1080 /* Insert a new extent page */
1082 xfs_iext_add_indirect_multi(ifp
,
1083 erp_idx
, page_idx
, ext_diff
);
1086 * If extent(s) are being appended to the last page in
1087 * the indirection array and the new extent(s) don't fit
1088 * in the page, then erp is NULL and erp_idx is set to
1089 * the next index needed in the indirection array.
1092 uint count
= ext_diff
;
1095 erp
= xfs_iext_irec_new(ifp
, erp_idx
);
1096 erp
->er_extcount
= min(count
, XFS_LINEAR_EXTS
);
1097 count
-= erp
->er_extcount
;
1103 ifp
->if_bytes
= new_size
;
1107 * This is called when incore extents are being added to the indirection
1108 * array and the new extents do not fit in the target extent list. The
1109 * erp_idx parameter contains the irec index for the target extent list
1110 * in the indirection array, and the idx parameter contains the extent
1111 * index within the list. The number of extents being added is stored
1112 * in the count parameter.
1114 * |-------| |-------|
1115 * | | | | idx - number of extents before idx
1117 * | | | | count - number of extents being inserted at idx
1118 * |-------| |-------|
1119 * | count | | nex2 | nex2 - number of extents after idx + count
1120 * |-------| |-------|
1123 xfs_iext_add_indirect_multi(
1124 xfs_ifork_t
*ifp
, /* inode fork pointer */
1125 int erp_idx
, /* target extent irec index */
1126 xfs_extnum_t idx
, /* index within target list */
1127 int count
) /* new extents being added */
1129 int byte_diff
; /* new bytes being added */
1130 xfs_ext_irec_t
*erp
; /* pointer to irec entry */
1131 xfs_extnum_t ext_diff
; /* number of extents to add */
1132 xfs_extnum_t ext_cnt
; /* new extents still needed */
1133 xfs_extnum_t nex2
; /* extents after idx + count */
1134 xfs_bmbt_rec_t
*nex2_ep
= NULL
; /* temp list for nex2 extents */
1135 int nlists
; /* number of irec's (lists) */
1137 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1138 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1139 nex2
= erp
->er_extcount
- idx
;
1140 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1143 * Save second part of target extent list
1144 * (all extents past */
1146 byte_diff
= nex2
* sizeof(xfs_bmbt_rec_t
);
1147 nex2_ep
= (xfs_bmbt_rec_t
*) kmem_alloc(byte_diff
, KM_NOFS
);
1148 memmove(nex2_ep
, &erp
->er_extbuf
[idx
], byte_diff
);
1149 erp
->er_extcount
-= nex2
;
1150 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, -nex2
);
1151 memset(&erp
->er_extbuf
[idx
], 0, byte_diff
);
1155 * Add the new extents to the end of the target
1156 * list, then allocate new irec record(s) and
1157 * extent buffer(s) as needed to store the rest
1158 * of the new extents.
1161 ext_diff
= MIN(ext_cnt
, (int)XFS_LINEAR_EXTS
- erp
->er_extcount
);
1163 erp
->er_extcount
+= ext_diff
;
1164 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, ext_diff
);
1165 ext_cnt
-= ext_diff
;
1169 erp
= xfs_iext_irec_new(ifp
, erp_idx
);
1170 ext_diff
= MIN(ext_cnt
, (int)XFS_LINEAR_EXTS
);
1171 erp
->er_extcount
= ext_diff
;
1172 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, ext_diff
);
1173 ext_cnt
-= ext_diff
;
1176 /* Add nex2 extents back to indirection array */
1178 xfs_extnum_t ext_avail
;
1181 byte_diff
= nex2
* sizeof(xfs_bmbt_rec_t
);
1182 ext_avail
= XFS_LINEAR_EXTS
- erp
->er_extcount
;
1185 * If nex2 extents fit in the current page, append
1186 * nex2_ep after the new extents.
1188 if (nex2
<= ext_avail
) {
1189 i
= erp
->er_extcount
;
1192 * Otherwise, check if space is available in the
1195 else if ((erp_idx
< nlists
- 1) &&
1196 (nex2
<= (ext_avail
= XFS_LINEAR_EXTS
-
1197 ifp
->if_u1
.if_ext_irec
[erp_idx
+1].er_extcount
))) {
1200 /* Create a hole for nex2 extents */
1201 memmove(&erp
->er_extbuf
[nex2
], erp
->er_extbuf
,
1202 erp
->er_extcount
* sizeof(xfs_bmbt_rec_t
));
1205 * Final choice, create a new extent page for
1210 erp
= xfs_iext_irec_new(ifp
, erp_idx
);
1212 memmove(&erp
->er_extbuf
[i
], nex2_ep
, byte_diff
);
1214 erp
->er_extcount
+= nex2
;
1215 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, nex2
);
1220 * This is called when the amount of space required for incore file
1221 * extents needs to be decreased. The ext_diff parameter stores the
1222 * number of extents to be removed and the idx parameter contains
1223 * the extent index where the extents will be removed from.
1225 * If the amount of space needed has decreased below the linear
1226 * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
1227 * extent array. Otherwise, use kmem_realloc() to adjust the
1228 * size to what is needed.
1232 xfs_inode_t
*ip
, /* incore inode pointer */
1233 xfs_extnum_t idx
, /* index to begin removing exts */
1234 int ext_diff
, /* number of extents to remove */
1235 int state
) /* type of extent conversion */
1237 xfs_ifork_t
*ifp
= xfs_iext_state_to_fork(ip
, state
);
1238 xfs_extnum_t nextents
; /* number of extents in file */
1239 int new_size
; /* size of extents after removal */
1241 trace_xfs_iext_remove(ip
, idx
, state
, _RET_IP_
);
1243 ASSERT(ext_diff
> 0);
1244 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1245 new_size
= (nextents
- ext_diff
) * sizeof(xfs_bmbt_rec_t
);
1247 if (new_size
== 0) {
1248 xfs_iext_destroy(ifp
);
1249 } else if (ifp
->if_flags
& XFS_IFEXTIREC
) {
1250 xfs_iext_remove_indirect(ifp
, idx
, ext_diff
);
1251 } else if (ifp
->if_real_bytes
) {
1252 xfs_iext_remove_direct(ifp
, idx
, ext_diff
);
1254 xfs_iext_remove_inline(ifp
, idx
, ext_diff
);
1256 ifp
->if_bytes
= new_size
;
1260 * This removes ext_diff extents from the inline buffer, beginning
1261 * at extent index idx.
1264 xfs_iext_remove_inline(
1265 xfs_ifork_t
*ifp
, /* inode fork pointer */
1266 xfs_extnum_t idx
, /* index to begin removing exts */
1267 int ext_diff
) /* number of extents to remove */
1269 int nextents
; /* number of extents in file */
1271 ASSERT(!(ifp
->if_flags
& XFS_IFEXTIREC
));
1272 ASSERT(idx
< XFS_INLINE_EXTS
);
1273 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1274 ASSERT(((nextents
- ext_diff
) > 0) &&
1275 (nextents
- ext_diff
) < XFS_INLINE_EXTS
);
1277 if (idx
+ ext_diff
< nextents
) {
1278 memmove(&ifp
->if_u2
.if_inline_ext
[idx
],
1279 &ifp
->if_u2
.if_inline_ext
[idx
+ ext_diff
],
1280 (nextents
- (idx
+ ext_diff
)) *
1281 sizeof(xfs_bmbt_rec_t
));
1282 memset(&ifp
->if_u2
.if_inline_ext
[nextents
- ext_diff
],
1283 0, ext_diff
* sizeof(xfs_bmbt_rec_t
));
1285 memset(&ifp
->if_u2
.if_inline_ext
[idx
], 0,
1286 ext_diff
* sizeof(xfs_bmbt_rec_t
));
1291 * This removes ext_diff extents from a linear (direct) extent list,
1292 * beginning at extent index idx. If the extents are being removed
1293 * from the end of the list (ie. truncate) then we just need to re-
1294 * allocate the list to remove the extra space. Otherwise, if the
1295 * extents are being removed from the middle of the existing extent
1296 * entries, then we first need to move the extent records beginning
1297 * at idx + ext_diff up in the list to overwrite the records being
1298 * removed, then remove the extra space via kmem_realloc.
1301 xfs_iext_remove_direct(
1302 xfs_ifork_t
*ifp
, /* inode fork pointer */
1303 xfs_extnum_t idx
, /* index to begin removing exts */
1304 int ext_diff
) /* number of extents to remove */
1306 xfs_extnum_t nextents
; /* number of extents in file */
1307 int new_size
; /* size of extents after removal */
1309 ASSERT(!(ifp
->if_flags
& XFS_IFEXTIREC
));
1310 new_size
= ifp
->if_bytes
-
1311 (ext_diff
* sizeof(xfs_bmbt_rec_t
));
1312 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1314 if (new_size
== 0) {
1315 xfs_iext_destroy(ifp
);
1318 /* Move extents up in the list (if needed) */
1319 if (idx
+ ext_diff
< nextents
) {
1320 memmove(&ifp
->if_u1
.if_extents
[idx
],
1321 &ifp
->if_u1
.if_extents
[idx
+ ext_diff
],
1322 (nextents
- (idx
+ ext_diff
)) *
1323 sizeof(xfs_bmbt_rec_t
));
1325 memset(&ifp
->if_u1
.if_extents
[nextents
- ext_diff
],
1326 0, ext_diff
* sizeof(xfs_bmbt_rec_t
));
1328 * Reallocate the direct extent list. If the extents
1329 * will fit inside the inode then xfs_iext_realloc_direct
1330 * will switch from direct to inline extent allocation
1333 xfs_iext_realloc_direct(ifp
, new_size
);
1334 ifp
->if_bytes
= new_size
;
1338 * This is called when incore extents are being removed from the
1339 * indirection array and the extents being removed span multiple extent
1340 * buffers. The idx parameter contains the file extent index where we
1341 * want to begin removing extents, and the count parameter contains
1342 * how many extents need to be removed.
1344 * |-------| |-------|
1345 * | nex1 | | | nex1 - number of extents before idx
1346 * |-------| | count |
1347 * | | | | count - number of extents being removed at idx
1348 * | count | |-------|
1349 * | | | nex2 | nex2 - number of extents after idx + count
1350 * |-------| |-------|
1353 xfs_iext_remove_indirect(
1354 xfs_ifork_t
*ifp
, /* inode fork pointer */
1355 xfs_extnum_t idx
, /* index to begin removing extents */
1356 int count
) /* number of extents to remove */
1358 xfs_ext_irec_t
*erp
; /* indirection array pointer */
1359 int erp_idx
= 0; /* indirection array index */
1360 xfs_extnum_t ext_cnt
; /* extents left to remove */
1361 xfs_extnum_t ext_diff
; /* extents to remove in current list */
1362 xfs_extnum_t nex1
; /* number of extents before idx */
1363 xfs_extnum_t nex2
; /* extents after idx + count */
1364 int page_idx
= idx
; /* index in target extent list */
1366 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1367 erp
= xfs_iext_idx_to_irec(ifp
, &page_idx
, &erp_idx
, 0);
1368 ASSERT(erp
!= NULL
);
1372 nex2
= MAX((erp
->er_extcount
- (nex1
+ ext_cnt
)), 0);
1373 ext_diff
= MIN(ext_cnt
, (erp
->er_extcount
- nex1
));
1375 * Check for deletion of entire list;
1376 * xfs_iext_irec_remove() updates extent offsets.
1378 if (ext_diff
== erp
->er_extcount
) {
1379 xfs_iext_irec_remove(ifp
, erp_idx
);
1380 ext_cnt
-= ext_diff
;
1383 ASSERT(erp_idx
< ifp
->if_real_bytes
/
1385 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1392 /* Move extents up (if needed) */
1394 memmove(&erp
->er_extbuf
[nex1
],
1395 &erp
->er_extbuf
[nex1
+ ext_diff
],
1396 nex2
* sizeof(xfs_bmbt_rec_t
));
1398 /* Zero out rest of page */
1399 memset(&erp
->er_extbuf
[nex1
+ nex2
], 0, (XFS_IEXT_BUFSZ
-
1400 ((nex1
+ nex2
) * sizeof(xfs_bmbt_rec_t
))));
1401 /* Update remaining counters */
1402 erp
->er_extcount
-= ext_diff
;
1403 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, -ext_diff
);
1404 ext_cnt
-= ext_diff
;
1409 ifp
->if_bytes
-= count
* sizeof(xfs_bmbt_rec_t
);
1410 xfs_iext_irec_compact(ifp
);
1414 * Create, destroy, or resize a linear (direct) block of extents.
1417 xfs_iext_realloc_direct(
1418 xfs_ifork_t
*ifp
, /* inode fork pointer */
1419 int new_size
) /* new size of extents after adding */
1421 int rnew_size
; /* real new size of extents */
1423 rnew_size
= new_size
;
1425 ASSERT(!(ifp
->if_flags
& XFS_IFEXTIREC
) ||
1426 ((new_size
>= 0) && (new_size
<= XFS_IEXT_BUFSZ
) &&
1427 (new_size
!= ifp
->if_real_bytes
)));
1429 /* Free extent records */
1430 if (new_size
== 0) {
1431 xfs_iext_destroy(ifp
);
1433 /* Resize direct extent list and zero any new bytes */
1434 else if (ifp
->if_real_bytes
) {
1435 /* Check if extents will fit inside the inode */
1436 if (new_size
<= XFS_INLINE_EXTS
* sizeof(xfs_bmbt_rec_t
)) {
1437 xfs_iext_direct_to_inline(ifp
, new_size
/
1438 (uint
)sizeof(xfs_bmbt_rec_t
));
1439 ifp
->if_bytes
= new_size
;
1442 if (!is_power_of_2(new_size
)){
1443 rnew_size
= roundup_pow_of_two(new_size
);
1445 if (rnew_size
!= ifp
->if_real_bytes
) {
1446 ifp
->if_u1
.if_extents
=
1447 kmem_realloc(ifp
->if_u1
.if_extents
,
1448 rnew_size
, KM_NOFS
);
1450 if (rnew_size
> ifp
->if_real_bytes
) {
1451 memset(&ifp
->if_u1
.if_extents
[ifp
->if_bytes
/
1452 (uint
)sizeof(xfs_bmbt_rec_t
)], 0,
1453 rnew_size
- ifp
->if_real_bytes
);
1456 /* Switch from the inline extent buffer to a direct extent list */
1458 if (!is_power_of_2(new_size
)) {
1459 rnew_size
= roundup_pow_of_two(new_size
);
1461 xfs_iext_inline_to_direct(ifp
, rnew_size
);
1463 ifp
->if_real_bytes
= rnew_size
;
1464 ifp
->if_bytes
= new_size
;
1468 * Switch from linear (direct) extent records to inline buffer.
1471 xfs_iext_direct_to_inline(
1472 xfs_ifork_t
*ifp
, /* inode fork pointer */
1473 xfs_extnum_t nextents
) /* number of extents in file */
1475 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1476 ASSERT(nextents
<= XFS_INLINE_EXTS
);
1478 * The inline buffer was zeroed when we switched
1479 * from inline to direct extent allocation mode,
1480 * so we don't need to clear it here.
1482 memcpy(ifp
->if_u2
.if_inline_ext
, ifp
->if_u1
.if_extents
,
1483 nextents
* sizeof(xfs_bmbt_rec_t
));
1484 kmem_free(ifp
->if_u1
.if_extents
);
1485 ifp
->if_u1
.if_extents
= ifp
->if_u2
.if_inline_ext
;
1486 ifp
->if_real_bytes
= 0;
1490 * Switch from inline buffer to linear (direct) extent records.
1491 * new_size should already be rounded up to the next power of 2
1492 * by the caller (when appropriate), so use new_size as it is.
1493 * However, since new_size may be rounded up, we can't update
1494 * if_bytes here. It is the caller's responsibility to update
1495 * if_bytes upon return.
1498 xfs_iext_inline_to_direct(
1499 xfs_ifork_t
*ifp
, /* inode fork pointer */
1500 int new_size
) /* number of extents in file */
1502 ifp
->if_u1
.if_extents
= kmem_alloc(new_size
, KM_NOFS
);
1503 memset(ifp
->if_u1
.if_extents
, 0, new_size
);
1504 if (ifp
->if_bytes
) {
1505 memcpy(ifp
->if_u1
.if_extents
, ifp
->if_u2
.if_inline_ext
,
1507 memset(ifp
->if_u2
.if_inline_ext
, 0, XFS_INLINE_EXTS
*
1508 sizeof(xfs_bmbt_rec_t
));
1510 ifp
->if_real_bytes
= new_size
;
1514 * Resize an extent indirection array to new_size bytes.
1517 xfs_iext_realloc_indirect(
1518 xfs_ifork_t
*ifp
, /* inode fork pointer */
1519 int new_size
) /* new indirection array size */
1521 int nlists
; /* number of irec's (ex lists) */
1522 int size
; /* current indirection array size */
1524 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1525 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1526 size
= nlists
* sizeof(xfs_ext_irec_t
);
1527 ASSERT(ifp
->if_real_bytes
);
1528 ASSERT((new_size
>= 0) && (new_size
!= size
));
1529 if (new_size
== 0) {
1530 xfs_iext_destroy(ifp
);
1532 ifp
->if_u1
.if_ext_irec
=
1533 kmem_realloc(ifp
->if_u1
.if_ext_irec
, new_size
, KM_NOFS
);
1538 * Switch from indirection array to linear (direct) extent allocations.
1541 xfs_iext_indirect_to_direct(
1542 xfs_ifork_t
*ifp
) /* inode fork pointer */
1544 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1545 xfs_extnum_t nextents
; /* number of extents in file */
1546 int size
; /* size of file extents */
1548 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1549 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1550 ASSERT(nextents
<= XFS_LINEAR_EXTS
);
1551 size
= nextents
* sizeof(xfs_bmbt_rec_t
);
1553 xfs_iext_irec_compact_pages(ifp
);
1554 ASSERT(ifp
->if_real_bytes
== XFS_IEXT_BUFSZ
);
1556 ep
= ifp
->if_u1
.if_ext_irec
->er_extbuf
;
1557 kmem_free(ifp
->if_u1
.if_ext_irec
);
1558 ifp
->if_flags
&= ~XFS_IFEXTIREC
;
1559 ifp
->if_u1
.if_extents
= ep
;
1560 ifp
->if_bytes
= size
;
1561 if (nextents
< XFS_LINEAR_EXTS
) {
1562 xfs_iext_realloc_direct(ifp
, size
);
1567 * Remove all records from the indirection array.
1570 xfs_iext_irec_remove_all(
1571 struct xfs_ifork
*ifp
)
1576 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1577 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1578 for (i
= 0; i
< nlists
; i
++)
1579 kmem_free(ifp
->if_u1
.if_ext_irec
[i
].er_extbuf
);
1580 kmem_free(ifp
->if_u1
.if_ext_irec
);
1581 ifp
->if_flags
&= ~XFS_IFEXTIREC
;
1585 * Free incore file extents.
1589 xfs_ifork_t
*ifp
) /* inode fork pointer */
1591 if (ifp
->if_flags
& XFS_IFEXTIREC
) {
1592 xfs_iext_irec_remove_all(ifp
);
1593 } else if (ifp
->if_real_bytes
) {
1594 kmem_free(ifp
->if_u1
.if_extents
);
1595 } else if (ifp
->if_bytes
) {
1596 memset(ifp
->if_u2
.if_inline_ext
, 0, XFS_INLINE_EXTS
*
1597 sizeof(xfs_bmbt_rec_t
));
1599 ifp
->if_u1
.if_extents
= NULL
;
1600 ifp
->if_real_bytes
= 0;
1605 * Return a pointer to the extent record for file system block bno.
1607 xfs_bmbt_rec_host_t
* /* pointer to found extent record */
1608 xfs_iext_bno_to_ext(
1609 xfs_ifork_t
*ifp
, /* inode fork pointer */
1610 xfs_fileoff_t bno
, /* block number to search for */
1611 xfs_extnum_t
*idxp
) /* index of target extent */
1613 xfs_bmbt_rec_host_t
*base
; /* pointer to first extent */
1614 xfs_filblks_t blockcount
= 0; /* number of blocks in extent */
1615 xfs_bmbt_rec_host_t
*ep
= NULL
; /* pointer to target extent */
1616 xfs_ext_irec_t
*erp
= NULL
; /* indirection array pointer */
1617 int high
; /* upper boundary in search */
1618 xfs_extnum_t idx
= 0; /* index of target extent */
1619 int low
; /* lower boundary in search */
1620 xfs_extnum_t nextents
; /* number of file extents */
1621 xfs_fileoff_t startoff
= 0; /* start offset of extent */
1623 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1624 if (nextents
== 0) {
1629 if (ifp
->if_flags
& XFS_IFEXTIREC
) {
1630 /* Find target extent list */
1632 erp
= xfs_iext_bno_to_irec(ifp
, bno
, &erp_idx
);
1633 base
= erp
->er_extbuf
;
1634 high
= erp
->er_extcount
- 1;
1636 base
= ifp
->if_u1
.if_extents
;
1637 high
= nextents
- 1;
1639 /* Binary search extent records */
1640 while (low
<= high
) {
1641 idx
= (low
+ high
) >> 1;
1643 startoff
= xfs_bmbt_get_startoff(ep
);
1644 blockcount
= xfs_bmbt_get_blockcount(ep
);
1645 if (bno
< startoff
) {
1647 } else if (bno
>= startoff
+ blockcount
) {
1650 /* Convert back to file-based extent index */
1651 if (ifp
->if_flags
& XFS_IFEXTIREC
) {
1652 idx
+= erp
->er_extoff
;
1658 /* Convert back to file-based extent index */
1659 if (ifp
->if_flags
& XFS_IFEXTIREC
) {
1660 idx
+= erp
->er_extoff
;
1662 if (bno
>= startoff
+ blockcount
) {
1663 if (++idx
== nextents
) {
1666 ep
= xfs_iext_get_ext(ifp
, idx
);
1674 * Return a pointer to the indirection array entry containing the
1675 * extent record for filesystem block bno. Store the index of the
1676 * target irec in *erp_idxp.
1678 xfs_ext_irec_t
* /* pointer to found extent record */
1679 xfs_iext_bno_to_irec(
1680 xfs_ifork_t
*ifp
, /* inode fork pointer */
1681 xfs_fileoff_t bno
, /* block number to search for */
1682 int *erp_idxp
) /* irec index of target ext list */
1684 xfs_ext_irec_t
*erp
= NULL
; /* indirection array pointer */
1685 xfs_ext_irec_t
*erp_next
; /* next indirection array entry */
1686 int erp_idx
; /* indirection array index */
1687 int nlists
; /* number of extent irec's (lists) */
1688 int high
; /* binary search upper limit */
1689 int low
; /* binary search lower limit */
1691 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1692 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1696 while (low
<= high
) {
1697 erp_idx
= (low
+ high
) >> 1;
1698 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1699 erp_next
= erp_idx
< nlists
- 1 ? erp
+ 1 : NULL
;
1700 if (bno
< xfs_bmbt_get_startoff(erp
->er_extbuf
)) {
1702 } else if (erp_next
&& bno
>=
1703 xfs_bmbt_get_startoff(erp_next
->er_extbuf
)) {
1709 *erp_idxp
= erp_idx
;
1714 * Return a pointer to the indirection array entry containing the
1715 * extent record at file extent index *idxp. Store the index of the
1716 * target irec in *erp_idxp and store the page index of the target
1717 * extent record in *idxp.
1720 xfs_iext_idx_to_irec(
1721 xfs_ifork_t
*ifp
, /* inode fork pointer */
1722 xfs_extnum_t
*idxp
, /* extent index (file -> page) */
1723 int *erp_idxp
, /* pointer to target irec */
1724 int realloc
) /* new bytes were just added */
1726 xfs_ext_irec_t
*prev
; /* pointer to previous irec */
1727 xfs_ext_irec_t
*erp
= NULL
; /* pointer to current irec */
1728 int erp_idx
; /* indirection array index */
1729 int nlists
; /* number of irec's (ex lists) */
1730 int high
; /* binary search upper limit */
1731 int low
; /* binary search lower limit */
1732 xfs_extnum_t page_idx
= *idxp
; /* extent index in target list */
1734 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1735 ASSERT(page_idx
>= 0);
1736 ASSERT(page_idx
<= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
));
1737 ASSERT(page_idx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
) || realloc
);
1739 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1744 /* Binary search extent irec's */
1745 while (low
<= high
) {
1746 erp_idx
= (low
+ high
) >> 1;
1747 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1748 prev
= erp_idx
> 0 ? erp
- 1 : NULL
;
1749 if (page_idx
< erp
->er_extoff
|| (page_idx
== erp
->er_extoff
&&
1750 realloc
&& prev
&& prev
->er_extcount
< XFS_LINEAR_EXTS
)) {
1752 } else if (page_idx
> erp
->er_extoff
+ erp
->er_extcount
||
1753 (page_idx
== erp
->er_extoff
+ erp
->er_extcount
&&
1756 } else if (page_idx
== erp
->er_extoff
+ erp
->er_extcount
&&
1757 erp
->er_extcount
== XFS_LINEAR_EXTS
) {
1761 erp
= erp_idx
< nlists
? erp
+ 1 : NULL
;
1764 page_idx
-= erp
->er_extoff
;
1769 *erp_idxp
= erp_idx
;
1774 * Allocate and initialize an indirection array once the space needed
1775 * for incore extents increases above XFS_IEXT_BUFSZ.
1779 xfs_ifork_t
*ifp
) /* inode fork pointer */
1781 xfs_ext_irec_t
*erp
; /* indirection array pointer */
1782 xfs_extnum_t nextents
; /* number of extents in file */
1784 ASSERT(!(ifp
->if_flags
& XFS_IFEXTIREC
));
1785 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1786 ASSERT(nextents
<= XFS_LINEAR_EXTS
);
1788 erp
= kmem_alloc(sizeof(xfs_ext_irec_t
), KM_NOFS
);
1790 if (nextents
== 0) {
1791 ifp
->if_u1
.if_extents
= kmem_alloc(XFS_IEXT_BUFSZ
, KM_NOFS
);
1792 } else if (!ifp
->if_real_bytes
) {
1793 xfs_iext_inline_to_direct(ifp
, XFS_IEXT_BUFSZ
);
1794 } else if (ifp
->if_real_bytes
< XFS_IEXT_BUFSZ
) {
1795 xfs_iext_realloc_direct(ifp
, XFS_IEXT_BUFSZ
);
1797 erp
->er_extbuf
= ifp
->if_u1
.if_extents
;
1798 erp
->er_extcount
= nextents
;
1801 ifp
->if_flags
|= XFS_IFEXTIREC
;
1802 ifp
->if_real_bytes
= XFS_IEXT_BUFSZ
;
1803 ifp
->if_bytes
= nextents
* sizeof(xfs_bmbt_rec_t
);
1804 ifp
->if_u1
.if_ext_irec
= erp
;
1810 * Allocate and initialize a new entry in the indirection array.
1814 xfs_ifork_t
*ifp
, /* inode fork pointer */
1815 int erp_idx
) /* index for new irec */
1817 xfs_ext_irec_t
*erp
; /* indirection array pointer */
1818 int i
; /* loop counter */
1819 int nlists
; /* number of irec's (ex lists) */
1821 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1822 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1824 /* Resize indirection array */
1825 xfs_iext_realloc_indirect(ifp
, ++nlists
*
1826 sizeof(xfs_ext_irec_t
));
1828 * Move records down in the array so the
1829 * new page can use erp_idx.
1831 erp
= ifp
->if_u1
.if_ext_irec
;
1832 for (i
= nlists
- 1; i
> erp_idx
; i
--) {
1833 memmove(&erp
[i
], &erp
[i
-1], sizeof(xfs_ext_irec_t
));
1835 ASSERT(i
== erp_idx
);
1837 /* Initialize new extent record */
1838 erp
= ifp
->if_u1
.if_ext_irec
;
1839 erp
[erp_idx
].er_extbuf
= kmem_alloc(XFS_IEXT_BUFSZ
, KM_NOFS
);
1840 ifp
->if_real_bytes
= nlists
* XFS_IEXT_BUFSZ
;
1841 memset(erp
[erp_idx
].er_extbuf
, 0, XFS_IEXT_BUFSZ
);
1842 erp
[erp_idx
].er_extcount
= 0;
1843 erp
[erp_idx
].er_extoff
= erp_idx
> 0 ?
1844 erp
[erp_idx
-1].er_extoff
+ erp
[erp_idx
-1].er_extcount
: 0;
1845 return (&erp
[erp_idx
]);
1849 * Remove a record from the indirection array.
1852 xfs_iext_irec_remove(
1853 xfs_ifork_t
*ifp
, /* inode fork pointer */
1854 int erp_idx
) /* irec index to remove */
1856 xfs_ext_irec_t
*erp
; /* indirection array pointer */
1857 int i
; /* loop counter */
1858 int nlists
; /* number of irec's (ex lists) */
1860 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1861 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1862 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1863 if (erp
->er_extbuf
) {
1864 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1,
1866 kmem_free(erp
->er_extbuf
);
1868 /* Compact extent records */
1869 erp
= ifp
->if_u1
.if_ext_irec
;
1870 for (i
= erp_idx
; i
< nlists
- 1; i
++) {
1871 memmove(&erp
[i
], &erp
[i
+1], sizeof(xfs_ext_irec_t
));
1874 * Manually free the last extent record from the indirection
1875 * array. A call to xfs_iext_realloc_indirect() with a size
1876 * of zero would result in a call to xfs_iext_destroy() which
1877 * would in turn call this function again, creating a nasty
1881 xfs_iext_realloc_indirect(ifp
,
1882 nlists
* sizeof(xfs_ext_irec_t
));
1884 kmem_free(ifp
->if_u1
.if_ext_irec
);
1886 ifp
->if_real_bytes
= nlists
* XFS_IEXT_BUFSZ
;
1890 * This is called to clean up large amounts of unused memory allocated
1891 * by the indirection array. Before compacting anything though, verify
1892 * that the indirection array is still needed and switch back to the
1893 * linear extent list (or even the inline buffer) if possible. The
1894 * compaction policy is as follows:
1896 * Full Compaction: Extents fit into a single page (or inline buffer)
1897 * Partial Compaction: Extents occupy less than 50% of allocated space
1898 * No Compaction: Extents occupy at least 50% of allocated space
1901 xfs_iext_irec_compact(
1902 xfs_ifork_t
*ifp
) /* inode fork pointer */
1904 xfs_extnum_t nextents
; /* number of extents in file */
1905 int nlists
; /* number of irec's (ex lists) */
1907 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1908 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1909 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1911 if (nextents
== 0) {
1912 xfs_iext_destroy(ifp
);
1913 } else if (nextents
<= XFS_INLINE_EXTS
) {
1914 xfs_iext_indirect_to_direct(ifp
);
1915 xfs_iext_direct_to_inline(ifp
, nextents
);
1916 } else if (nextents
<= XFS_LINEAR_EXTS
) {
1917 xfs_iext_indirect_to_direct(ifp
);
1918 } else if (nextents
< (nlists
* XFS_LINEAR_EXTS
) >> 1) {
1919 xfs_iext_irec_compact_pages(ifp
);
1924 * Combine extents from neighboring extent pages.
1927 xfs_iext_irec_compact_pages(
1928 xfs_ifork_t
*ifp
) /* inode fork pointer */
1930 xfs_ext_irec_t
*erp
, *erp_next
;/* pointers to irec entries */
1931 int erp_idx
= 0; /* indirection array index */
1932 int nlists
; /* number of irec's (ex lists) */
1934 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1935 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1936 while (erp_idx
< nlists
- 1) {
1937 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1939 if (erp_next
->er_extcount
<=
1940 (XFS_LINEAR_EXTS
- erp
->er_extcount
)) {
1941 memcpy(&erp
->er_extbuf
[erp
->er_extcount
],
1942 erp_next
->er_extbuf
, erp_next
->er_extcount
*
1943 sizeof(xfs_bmbt_rec_t
));
1944 erp
->er_extcount
+= erp_next
->er_extcount
;
1946 * Free page before removing extent record
1947 * so er_extoffs don't get modified in
1948 * xfs_iext_irec_remove.
1950 kmem_free(erp_next
->er_extbuf
);
1951 erp_next
->er_extbuf
= NULL
;
1952 xfs_iext_irec_remove(ifp
, erp_idx
+ 1);
1953 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1961 * This is called to update the er_extoff field in the indirection
1962 * array when extents have been added or removed from one of the
1963 * extent lists. erp_idx contains the irec index to begin updating
1964 * at and ext_diff contains the number of extents that were added
1968 xfs_iext_irec_update_extoffs(
1969 xfs_ifork_t
*ifp
, /* inode fork pointer */
1970 int erp_idx
, /* irec index to update */
1971 int ext_diff
) /* number of new extents */
1973 int i
; /* loop counter */
1974 int nlists
; /* number of irec's (ex lists */
1976 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1977 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1978 for (i
= erp_idx
; i
< nlists
; i
++) {
1979 ifp
->if_u1
.if_ext_irec
[i
].er_extoff
+= ext_diff
;
1984 * Initialize an inode's copy-on-write fork.
1988 struct xfs_inode
*ip
)
1993 ip
->i_cowfp
= kmem_zone_zalloc(xfs_ifork_zone
,
1994 KM_SLEEP
| KM_NOFS
);
1995 ip
->i_cowfp
->if_flags
= XFS_IFEXTENTS
;
1996 ip
->i_cformat
= XFS_DINODE_FMT_EXTENTS
;
1997 ip
->i_cnextents
= 0;