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 switch (VFS_I(ip
)->i_mode
& S_IFMT
) {
129 if (unlikely(dip
->di_format
!= XFS_DINODE_FMT_DEV
)) {
130 XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW
,
132 return -EFSCORRUPTED
;
135 ip
->i_df
.if_u2
.if_rdev
= xfs_dinode_get_rdev(dip
);
141 switch (dip
->di_format
) {
142 case XFS_DINODE_FMT_LOCAL
:
144 * no local regular files yet
146 if (unlikely(S_ISREG(be16_to_cpu(dip
->di_mode
)))) {
147 xfs_warn(ip
->i_mount
,
148 "corrupt inode %Lu (local format for regular file).",
149 (unsigned long long) ip
->i_ino
);
150 XFS_CORRUPTION_ERROR("xfs_iformat(4)",
153 return -EFSCORRUPTED
;
156 di_size
= be64_to_cpu(dip
->di_size
);
157 if (unlikely(di_size
< 0 ||
158 di_size
> XFS_DFORK_DSIZE(dip
, ip
->i_mount
))) {
159 xfs_warn(ip
->i_mount
,
160 "corrupt inode %Lu (bad size %Ld for local inode).",
161 (unsigned long long) ip
->i_ino
,
162 (long long) di_size
);
163 XFS_CORRUPTION_ERROR("xfs_iformat(5)",
166 return -EFSCORRUPTED
;
170 error
= xfs_iformat_local(ip
, dip
, XFS_DATA_FORK
, size
);
172 case XFS_DINODE_FMT_EXTENTS
:
173 error
= xfs_iformat_extents(ip
, dip
, XFS_DATA_FORK
);
175 case XFS_DINODE_FMT_BTREE
:
176 error
= xfs_iformat_btree(ip
, dip
, XFS_DATA_FORK
);
179 XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW
,
181 return -EFSCORRUPTED
;
186 XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW
, ip
->i_mount
);
187 return -EFSCORRUPTED
;
192 if (!XFS_DFORK_Q(dip
))
195 ASSERT(ip
->i_afp
== NULL
);
196 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
| KM_NOFS
);
198 switch (dip
->di_aformat
) {
199 case XFS_DINODE_FMT_LOCAL
:
200 atp
= (xfs_attr_shortform_t
*)XFS_DFORK_APTR(dip
);
201 size
= be16_to_cpu(atp
->hdr
.totsize
);
203 if (unlikely(size
< sizeof(struct xfs_attr_sf_hdr
))) {
204 xfs_warn(ip
->i_mount
,
205 "corrupt inode %Lu (bad attr fork size %Ld).",
206 (unsigned long long) ip
->i_ino
,
208 XFS_CORRUPTION_ERROR("xfs_iformat(8)",
211 return -EFSCORRUPTED
;
214 error
= xfs_iformat_local(ip
, dip
, XFS_ATTR_FORK
, size
);
216 case XFS_DINODE_FMT_EXTENTS
:
217 error
= xfs_iformat_extents(ip
, dip
, XFS_ATTR_FORK
);
219 case XFS_DINODE_FMT_BTREE
:
220 error
= xfs_iformat_btree(ip
, dip
, XFS_ATTR_FORK
);
223 error
= -EFSCORRUPTED
;
227 kmem_zone_free(xfs_ifork_zone
, ip
->i_afp
);
229 xfs_idestroy_fork(ip
, XFS_DATA_FORK
);
235 * The file is in-lined in the on-disk inode.
236 * If it fits into if_inline_data, then copy
237 * it there, otherwise allocate a buffer for it
238 * and copy the data there. Either way, set
239 * if_data to point at the data.
240 * If we allocate a buffer for the data, make
241 * sure that its size is a multiple of 4 and
242 * record the real size in i_real_bytes.
255 * If the size is unreasonable, then something
256 * is wrong and we just bail out rather than crash in
257 * kmem_alloc() or memcpy() below.
259 if (unlikely(size
> XFS_DFORK_SIZE(dip
, ip
->i_mount
, whichfork
))) {
260 xfs_warn(ip
->i_mount
,
261 "corrupt inode %Lu (bad size %d for local fork, size = %d).",
262 (unsigned long long) ip
->i_ino
, size
,
263 XFS_DFORK_SIZE(dip
, ip
->i_mount
, whichfork
));
264 XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW
,
266 return -EFSCORRUPTED
;
268 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
271 ifp
->if_u1
.if_data
= NULL
;
272 else if (size
<= sizeof(ifp
->if_u2
.if_inline_data
))
273 ifp
->if_u1
.if_data
= ifp
->if_u2
.if_inline_data
;
275 real_size
= roundup(size
, 4);
276 ifp
->if_u1
.if_data
= kmem_alloc(real_size
, KM_SLEEP
| KM_NOFS
);
278 ifp
->if_bytes
= size
;
279 ifp
->if_real_bytes
= real_size
;
281 memcpy(ifp
->if_u1
.if_data
, XFS_DFORK_PTR(dip
, whichfork
), size
);
282 ifp
->if_flags
&= ~XFS_IFEXTENTS
;
283 ifp
->if_flags
|= XFS_IFINLINE
;
288 * The file consists of a set of extents all
289 * of which fit into the on-disk inode.
290 * If there are few enough extents to fit into
291 * the if_inline_ext, then copy them there.
292 * Otherwise allocate a buffer for them and copy
293 * them into it. Either way, set if_extents
294 * to point at the extents.
308 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
309 nex
= XFS_DFORK_NEXTENTS(dip
, whichfork
);
310 size
= nex
* (uint
)sizeof(xfs_bmbt_rec_t
);
313 * If the number of extents is unreasonable, then something
314 * is wrong and we just bail out rather than crash in
315 * kmem_alloc() or memcpy() below.
317 if (unlikely(size
< 0 || size
> XFS_DFORK_SIZE(dip
, ip
->i_mount
, whichfork
))) {
318 xfs_warn(ip
->i_mount
, "corrupt inode %Lu ((a)extents = %d).",
319 (unsigned long long) ip
->i_ino
, nex
);
320 XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW
,
322 return -EFSCORRUPTED
;
325 ifp
->if_real_bytes
= 0;
327 ifp
->if_u1
.if_extents
= NULL
;
328 else if (nex
<= XFS_INLINE_EXTS
)
329 ifp
->if_u1
.if_extents
= ifp
->if_u2
.if_inline_ext
;
331 xfs_iext_add(ifp
, 0, nex
);
333 ifp
->if_bytes
= size
;
335 dp
= (xfs_bmbt_rec_t
*) XFS_DFORK_PTR(dip
, whichfork
);
336 xfs_validate_extents(ifp
, nex
, XFS_EXTFMT_INODE(ip
));
337 for (i
= 0; i
< nex
; i
++, dp
++) {
338 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, i
);
339 ep
->l0
= get_unaligned_be64(&dp
->l0
);
340 ep
->l1
= get_unaligned_be64(&dp
->l1
);
342 XFS_BMAP_TRACE_EXLIST(ip
, nex
, whichfork
);
343 if (whichfork
!= XFS_DATA_FORK
||
344 XFS_EXTFMT_INODE(ip
) == XFS_EXTFMT_NOSTATE
)
345 if (unlikely(xfs_check_nostate_extents(
347 XFS_ERROR_REPORT("xfs_iformat_extents(2)",
350 return -EFSCORRUPTED
;
353 ifp
->if_flags
|= XFS_IFEXTENTS
;
358 * The file has too many extents to fit into
359 * the inode, so they are in B-tree format.
360 * Allocate a buffer for the root of the B-tree
361 * and copy the root into it. The i_extents
362 * field will remain NULL until all of the
363 * extents are read in (when they are needed).
371 struct xfs_mount
*mp
= ip
->i_mount
;
372 xfs_bmdr_block_t
*dfp
;
378 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
379 dfp
= (xfs_bmdr_block_t
*)XFS_DFORK_PTR(dip
, whichfork
);
380 size
= XFS_BMAP_BROOT_SPACE(mp
, dfp
);
381 nrecs
= be16_to_cpu(dfp
->bb_numrecs
);
384 * blow out if -- fork has less extents than can fit in
385 * fork (fork shouldn't be a btree format), root btree
386 * block has more records than can fit into the fork,
387 * or the number of extents is greater than the number of
390 if (unlikely(XFS_IFORK_NEXTENTS(ip
, whichfork
) <=
391 XFS_IFORK_MAXEXT(ip
, whichfork
) ||
392 XFS_BMDR_SPACE_CALC(nrecs
) >
393 XFS_DFORK_SIZE(dip
, mp
, whichfork
) ||
394 XFS_IFORK_NEXTENTS(ip
, whichfork
) > ip
->i_d
.di_nblocks
)) {
395 xfs_warn(mp
, "corrupt inode %Lu (btree).",
396 (unsigned long long) ip
->i_ino
);
397 XFS_CORRUPTION_ERROR("xfs_iformat_btree", XFS_ERRLEVEL_LOW
,
399 return -EFSCORRUPTED
;
402 ifp
->if_broot_bytes
= size
;
403 ifp
->if_broot
= kmem_alloc(size
, KM_SLEEP
| KM_NOFS
);
404 ASSERT(ifp
->if_broot
!= NULL
);
406 * Copy and convert from the on-disk structure
407 * to the in-memory structure.
409 xfs_bmdr_to_bmbt(ip
, dfp
, XFS_DFORK_SIZE(dip
, ip
->i_mount
, whichfork
),
410 ifp
->if_broot
, size
);
411 ifp
->if_flags
&= ~XFS_IFEXTENTS
;
412 ifp
->if_flags
|= XFS_IFBROOT
;
418 * Read in extents from a btree-format inode.
419 * Allocate and fill in if_extents. Real work is done in xfs_bmap.c.
429 xfs_extnum_t nextents
;
431 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
433 if (unlikely(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
434 XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW
,
436 return -EFSCORRUPTED
;
438 nextents
= XFS_IFORK_NEXTENTS(ip
, whichfork
);
439 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
442 * We know that the size is valid (it's checked in iformat_btree)
444 ifp
->if_bytes
= ifp
->if_real_bytes
= 0;
445 ifp
->if_flags
|= XFS_IFEXTENTS
;
446 xfs_iext_add(ifp
, 0, nextents
);
447 error
= xfs_bmap_read_extents(tp
, ip
, whichfork
);
449 xfs_iext_destroy(ifp
);
450 ifp
->if_flags
&= ~XFS_IFEXTENTS
;
453 xfs_validate_extents(ifp
, nextents
, XFS_EXTFMT_INODE(ip
));
457 * Reallocate the space for if_broot based on the number of records
458 * being added or deleted as indicated in rec_diff. Move the records
459 * and pointers in if_broot to fit the new size. When shrinking this
460 * will eliminate holes between the records and pointers created by
461 * the caller. When growing this will create holes to be filled in
464 * The caller must not request to add more records than would fit in
465 * the on-disk inode root. If the if_broot is currently NULL, then
466 * if we are adding records, one will be allocated. The caller must also
467 * not request that the number of records go below zero, although
470 * ip -- the inode whose if_broot area is changing
471 * ext_diff -- the change in the number of records, positive or negative,
472 * requested for the if_broot array.
480 struct xfs_mount
*mp
= ip
->i_mount
;
483 struct xfs_btree_block
*new_broot
;
490 * Handle the degenerate case quietly.
496 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
499 * If there wasn't any memory allocated before, just
500 * allocate it now and get out.
502 if (ifp
->if_broot_bytes
== 0) {
503 new_size
= XFS_BMAP_BROOT_SPACE_CALC(mp
, rec_diff
);
504 ifp
->if_broot
= kmem_alloc(new_size
, KM_SLEEP
| KM_NOFS
);
505 ifp
->if_broot_bytes
= (int)new_size
;
510 * If there is already an existing if_broot, then we need
511 * to realloc() it and shift the pointers to their new
512 * location. The records don't change location because
513 * they are kept butted up against the btree block header.
515 cur_max
= xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0);
516 new_max
= cur_max
+ rec_diff
;
517 new_size
= XFS_BMAP_BROOT_SPACE_CALC(mp
, new_max
);
518 ifp
->if_broot
= kmem_realloc(ifp
->if_broot
, new_size
,
520 op
= (char *)XFS_BMAP_BROOT_PTR_ADDR(mp
, ifp
->if_broot
, 1,
521 ifp
->if_broot_bytes
);
522 np
= (char *)XFS_BMAP_BROOT_PTR_ADDR(mp
, ifp
->if_broot
, 1,
524 ifp
->if_broot_bytes
= (int)new_size
;
525 ASSERT(XFS_BMAP_BMDR_SPACE(ifp
->if_broot
) <=
526 XFS_IFORK_SIZE(ip
, whichfork
));
527 memmove(np
, op
, cur_max
* (uint
)sizeof(xfs_fsblock_t
));
532 * rec_diff is less than 0. In this case, we are shrinking the
533 * if_broot buffer. It must already exist. If we go to zero
534 * records, just get rid of the root and clear the status bit.
536 ASSERT((ifp
->if_broot
!= NULL
) && (ifp
->if_broot_bytes
> 0));
537 cur_max
= xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0);
538 new_max
= cur_max
+ rec_diff
;
539 ASSERT(new_max
>= 0);
541 new_size
= XFS_BMAP_BROOT_SPACE_CALC(mp
, new_max
);
545 new_broot
= kmem_alloc(new_size
, KM_SLEEP
| KM_NOFS
);
547 * First copy over the btree block header.
549 memcpy(new_broot
, ifp
->if_broot
,
550 XFS_BMBT_BLOCK_LEN(ip
->i_mount
));
553 ifp
->if_flags
&= ~XFS_IFBROOT
;
557 * Only copy the records and pointers if there are any.
561 * First copy the records.
563 op
= (char *)XFS_BMBT_REC_ADDR(mp
, ifp
->if_broot
, 1);
564 np
= (char *)XFS_BMBT_REC_ADDR(mp
, new_broot
, 1);
565 memcpy(np
, op
, new_max
* (uint
)sizeof(xfs_bmbt_rec_t
));
568 * Then copy the pointers.
570 op
= (char *)XFS_BMAP_BROOT_PTR_ADDR(mp
, ifp
->if_broot
, 1,
571 ifp
->if_broot_bytes
);
572 np
= (char *)XFS_BMAP_BROOT_PTR_ADDR(mp
, new_broot
, 1,
574 memcpy(np
, op
, new_max
* (uint
)sizeof(xfs_fsblock_t
));
576 kmem_free(ifp
->if_broot
);
577 ifp
->if_broot
= new_broot
;
578 ifp
->if_broot_bytes
= (int)new_size
;
580 ASSERT(XFS_BMAP_BMDR_SPACE(ifp
->if_broot
) <=
581 XFS_IFORK_SIZE(ip
, whichfork
));
587 * This is called when the amount of space needed for if_data
588 * is increased or decreased. The change in size is indicated by
589 * the number of bytes that need to be added or deleted in the
590 * byte_diff parameter.
592 * If the amount of space needed has decreased below the size of the
593 * inline buffer, then switch to using the inline buffer. Otherwise,
594 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
597 * ip -- the inode whose if_data area is changing
598 * byte_diff -- the change in the number of bytes, positive or negative,
599 * requested for the if_data array.
611 if (byte_diff
== 0) {
615 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
616 new_size
= (int)ifp
->if_bytes
+ byte_diff
;
617 ASSERT(new_size
>= 0);
620 if (ifp
->if_u1
.if_data
!= ifp
->if_u2
.if_inline_data
) {
621 kmem_free(ifp
->if_u1
.if_data
);
623 ifp
->if_u1
.if_data
= NULL
;
625 } else if (new_size
<= sizeof(ifp
->if_u2
.if_inline_data
)) {
627 * If the valid extents/data can fit in if_inline_ext/data,
628 * copy them from the malloc'd vector and free it.
630 if (ifp
->if_u1
.if_data
== NULL
) {
631 ifp
->if_u1
.if_data
= ifp
->if_u2
.if_inline_data
;
632 } else if (ifp
->if_u1
.if_data
!= ifp
->if_u2
.if_inline_data
) {
633 ASSERT(ifp
->if_real_bytes
!= 0);
634 memcpy(ifp
->if_u2
.if_inline_data
, ifp
->if_u1
.if_data
,
636 kmem_free(ifp
->if_u1
.if_data
);
637 ifp
->if_u1
.if_data
= ifp
->if_u2
.if_inline_data
;
642 * Stuck with malloc/realloc.
643 * For inline data, the underlying buffer must be
644 * a multiple of 4 bytes in size so that it can be
645 * logged and stay on word boundaries. We enforce
648 real_size
= roundup(new_size
, 4);
649 if (ifp
->if_u1
.if_data
== NULL
) {
650 ASSERT(ifp
->if_real_bytes
== 0);
651 ifp
->if_u1
.if_data
= kmem_alloc(real_size
,
653 } else if (ifp
->if_u1
.if_data
!= ifp
->if_u2
.if_inline_data
) {
655 * Only do the realloc if the underlying size
656 * is really changing.
658 if (ifp
->if_real_bytes
!= real_size
) {
660 kmem_realloc(ifp
->if_u1
.if_data
,
665 ASSERT(ifp
->if_real_bytes
== 0);
666 ifp
->if_u1
.if_data
= kmem_alloc(real_size
,
668 memcpy(ifp
->if_u1
.if_data
, ifp
->if_u2
.if_inline_data
,
672 ifp
->if_real_bytes
= real_size
;
673 ifp
->if_bytes
= new_size
;
674 ASSERT(ifp
->if_bytes
<= XFS_IFORK_SIZE(ip
, whichfork
));
684 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
685 if (ifp
->if_broot
!= NULL
) {
686 kmem_free(ifp
->if_broot
);
687 ifp
->if_broot
= NULL
;
691 * If the format is local, then we can't have an extents
692 * array so just look for an inline data array. If we're
693 * not local then we may or may not have an extents list,
694 * so check and free it up if we do.
696 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
697 if ((ifp
->if_u1
.if_data
!= ifp
->if_u2
.if_inline_data
) &&
698 (ifp
->if_u1
.if_data
!= NULL
)) {
699 ASSERT(ifp
->if_real_bytes
!= 0);
700 kmem_free(ifp
->if_u1
.if_data
);
701 ifp
->if_u1
.if_data
= NULL
;
702 ifp
->if_real_bytes
= 0;
704 } else if ((ifp
->if_flags
& XFS_IFEXTENTS
) &&
705 ((ifp
->if_flags
& XFS_IFEXTIREC
) ||
706 ((ifp
->if_u1
.if_extents
!= NULL
) &&
707 (ifp
->if_u1
.if_extents
!= ifp
->if_u2
.if_inline_ext
)))) {
708 ASSERT(ifp
->if_real_bytes
!= 0);
709 xfs_iext_destroy(ifp
);
711 ASSERT(ifp
->if_u1
.if_extents
== NULL
||
712 ifp
->if_u1
.if_extents
== ifp
->if_u2
.if_inline_ext
);
713 ASSERT(ifp
->if_real_bytes
== 0);
714 if (whichfork
== XFS_ATTR_FORK
) {
715 kmem_zone_free(xfs_ifork_zone
, ip
->i_afp
);
721 * Convert in-core extents to on-disk form
723 * For either the data or attr fork in extent format, we need to endian convert
724 * the in-core extent as we place them into the on-disk inode.
726 * In the case of the data fork, the in-core and on-disk fork sizes can be
727 * different due to delayed allocation extents. We only copy on-disk extents
728 * here, so callers must always use the physical fork size to determine the
729 * size of the buffer passed to this routine. We will return the size actually
742 xfs_fsblock_t start_block
;
744 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
745 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
|XFS_ILOCK_SHARED
));
746 ASSERT(ifp
->if_bytes
> 0);
748 nrecs
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
749 XFS_BMAP_TRACE_EXLIST(ip
, nrecs
, whichfork
);
753 * There are some delayed allocation extents in the
754 * inode, so copy the extents one at a time and skip
755 * the delayed ones. There must be at least one
756 * non-delayed extent.
759 for (i
= 0; i
< nrecs
; i
++) {
760 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, i
);
761 start_block
= xfs_bmbt_get_startblock(ep
);
762 if (isnullstartblock(start_block
)) {
764 * It's a delayed allocation extent, so skip it.
769 /* Translate to on disk format */
770 put_unaligned_be64(ep
->l0
, &dp
->l0
);
771 put_unaligned_be64(ep
->l1
, &dp
->l1
);
776 xfs_validate_extents(ifp
, copied
, XFS_EXTFMT_INODE(ip
));
778 return (copied
* (uint
)sizeof(xfs_bmbt_rec_t
));
782 * Each of the following cases stores data into the same region
783 * of the on-disk inode, so only one of them can be valid at
784 * any given time. While it is possible to have conflicting formats
785 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
786 * in EXTENTS format, this can only happen when the fork has
787 * changed formats after being modified but before being flushed.
788 * In these cases, the format always takes precedence, because the
789 * format indicates the current state of the fork.
795 xfs_inode_log_item_t
*iip
,
801 static const short brootflag
[2] =
802 { XFS_ILOG_DBROOT
, XFS_ILOG_ABROOT
};
803 static const short dataflag
[2] =
804 { XFS_ILOG_DDATA
, XFS_ILOG_ADATA
};
805 static const short extflag
[2] =
806 { XFS_ILOG_DEXT
, XFS_ILOG_AEXT
};
810 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
812 * This can happen if we gave up in iformat in an error path,
813 * for the attribute fork.
816 ASSERT(whichfork
== XFS_ATTR_FORK
);
819 cp
= XFS_DFORK_PTR(dip
, whichfork
);
821 switch (XFS_IFORK_FORMAT(ip
, whichfork
)) {
822 case XFS_DINODE_FMT_LOCAL
:
823 if ((iip
->ili_fields
& dataflag
[whichfork
]) &&
824 (ifp
->if_bytes
> 0)) {
825 ASSERT(ifp
->if_u1
.if_data
!= NULL
);
826 ASSERT(ifp
->if_bytes
<= XFS_IFORK_SIZE(ip
, whichfork
));
827 memcpy(cp
, ifp
->if_u1
.if_data
, ifp
->if_bytes
);
831 case XFS_DINODE_FMT_EXTENTS
:
832 ASSERT((ifp
->if_flags
& XFS_IFEXTENTS
) ||
833 !(iip
->ili_fields
& extflag
[whichfork
]));
834 if ((iip
->ili_fields
& extflag
[whichfork
]) &&
835 (ifp
->if_bytes
> 0)) {
836 ASSERT(xfs_iext_get_ext(ifp
, 0));
837 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) > 0);
838 (void)xfs_iextents_copy(ip
, (xfs_bmbt_rec_t
*)cp
,
843 case XFS_DINODE_FMT_BTREE
:
844 if ((iip
->ili_fields
& brootflag
[whichfork
]) &&
845 (ifp
->if_broot_bytes
> 0)) {
846 ASSERT(ifp
->if_broot
!= NULL
);
847 ASSERT(XFS_BMAP_BMDR_SPACE(ifp
->if_broot
) <=
848 XFS_IFORK_SIZE(ip
, whichfork
));
849 xfs_bmbt_to_bmdr(mp
, ifp
->if_broot
, ifp
->if_broot_bytes
,
850 (xfs_bmdr_block_t
*)cp
,
851 XFS_DFORK_SIZE(dip
, mp
, whichfork
));
855 case XFS_DINODE_FMT_DEV
:
856 if (iip
->ili_fields
& XFS_ILOG_DEV
) {
857 ASSERT(whichfork
== XFS_DATA_FORK
);
858 xfs_dinode_put_rdev(dip
, ip
->i_df
.if_u2
.if_rdev
);
862 case XFS_DINODE_FMT_UUID
:
863 if (iip
->ili_fields
& XFS_ILOG_UUID
) {
864 ASSERT(whichfork
== XFS_DATA_FORK
);
865 memcpy(XFS_DFORK_DPTR(dip
),
866 &ip
->i_df
.if_u2
.if_uuid
,
878 * Return a pointer to the extent record at file index idx.
880 xfs_bmbt_rec_host_t
*
882 xfs_ifork_t
*ifp
, /* inode fork pointer */
883 xfs_extnum_t idx
) /* index of target extent */
886 ASSERT(idx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
));
888 if ((ifp
->if_flags
& XFS_IFEXTIREC
) && (idx
== 0)) {
889 return ifp
->if_u1
.if_ext_irec
->er_extbuf
;
890 } else if (ifp
->if_flags
& XFS_IFEXTIREC
) {
891 xfs_ext_irec_t
*erp
; /* irec pointer */
892 int erp_idx
= 0; /* irec index */
893 xfs_extnum_t page_idx
= idx
; /* ext index in target list */
895 erp
= xfs_iext_idx_to_irec(ifp
, &page_idx
, &erp_idx
, 0);
896 return &erp
->er_extbuf
[page_idx
];
897 } else if (ifp
->if_bytes
) {
898 return &ifp
->if_u1
.if_extents
[idx
];
905 * Insert new item(s) into the extent records for incore inode
906 * fork 'ifp'. 'count' new items are inserted at index 'idx'.
910 xfs_inode_t
*ip
, /* incore inode pointer */
911 xfs_extnum_t idx
, /* starting index of new items */
912 xfs_extnum_t count
, /* number of inserted items */
913 xfs_bmbt_irec_t
*new, /* items to insert */
914 int state
) /* type of extent conversion */
916 xfs_ifork_t
*ifp
= (state
& BMAP_ATTRFORK
) ? ip
->i_afp
: &ip
->i_df
;
917 xfs_extnum_t i
; /* extent record index */
919 trace_xfs_iext_insert(ip
, idx
, new, state
, _RET_IP_
);
921 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
922 xfs_iext_add(ifp
, idx
, count
);
923 for (i
= idx
; i
< idx
+ count
; i
++, new++)
924 xfs_bmbt_set_all(xfs_iext_get_ext(ifp
, i
), new);
928 * This is called when the amount of space required for incore file
929 * extents needs to be increased. The ext_diff parameter stores the
930 * number of new extents being added and the idx parameter contains
931 * the extent index where the new extents will be added. If the new
932 * extents are being appended, then we just need to (re)allocate and
933 * initialize the space. Otherwise, if the new extents are being
934 * inserted into the middle of the existing entries, a bit more work
935 * is required to make room for the new extents to be inserted. The
936 * caller is responsible for filling in the new extent entries upon
941 xfs_ifork_t
*ifp
, /* inode fork pointer */
942 xfs_extnum_t idx
, /* index to begin adding exts */
943 int ext_diff
) /* number of extents to add */
945 int byte_diff
; /* new bytes being added */
946 int new_size
; /* size of extents after adding */
947 xfs_extnum_t nextents
; /* number of extents in file */
949 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
950 ASSERT((idx
>= 0) && (idx
<= nextents
));
951 byte_diff
= ext_diff
* sizeof(xfs_bmbt_rec_t
);
952 new_size
= ifp
->if_bytes
+ byte_diff
;
954 * If the new number of extents (nextents + ext_diff)
955 * fits inside the inode, then continue to use the inline
958 if (nextents
+ ext_diff
<= XFS_INLINE_EXTS
) {
959 if (idx
< nextents
) {
960 memmove(&ifp
->if_u2
.if_inline_ext
[idx
+ ext_diff
],
961 &ifp
->if_u2
.if_inline_ext
[idx
],
962 (nextents
- idx
) * sizeof(xfs_bmbt_rec_t
));
963 memset(&ifp
->if_u2
.if_inline_ext
[idx
], 0, byte_diff
);
965 ifp
->if_u1
.if_extents
= ifp
->if_u2
.if_inline_ext
;
966 ifp
->if_real_bytes
= 0;
969 * Otherwise use a linear (direct) extent list.
970 * If the extents are currently inside the inode,
971 * xfs_iext_realloc_direct will switch us from
972 * inline to direct extent allocation mode.
974 else if (nextents
+ ext_diff
<= XFS_LINEAR_EXTS
) {
975 xfs_iext_realloc_direct(ifp
, new_size
);
976 if (idx
< nextents
) {
977 memmove(&ifp
->if_u1
.if_extents
[idx
+ ext_diff
],
978 &ifp
->if_u1
.if_extents
[idx
],
979 (nextents
- idx
) * sizeof(xfs_bmbt_rec_t
));
980 memset(&ifp
->if_u1
.if_extents
[idx
], 0, byte_diff
);
983 /* Indirection array */
989 ASSERT(nextents
+ ext_diff
> XFS_LINEAR_EXTS
);
990 if (ifp
->if_flags
& XFS_IFEXTIREC
) {
991 erp
= xfs_iext_idx_to_irec(ifp
, &page_idx
, &erp_idx
, 1);
993 xfs_iext_irec_init(ifp
);
994 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
995 erp
= ifp
->if_u1
.if_ext_irec
;
997 /* Extents fit in target extent page */
998 if (erp
&& erp
->er_extcount
+ ext_diff
<= XFS_LINEAR_EXTS
) {
999 if (page_idx
< erp
->er_extcount
) {
1000 memmove(&erp
->er_extbuf
[page_idx
+ ext_diff
],
1001 &erp
->er_extbuf
[page_idx
],
1002 (erp
->er_extcount
- page_idx
) *
1003 sizeof(xfs_bmbt_rec_t
));
1004 memset(&erp
->er_extbuf
[page_idx
], 0, byte_diff
);
1006 erp
->er_extcount
+= ext_diff
;
1007 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, ext_diff
);
1009 /* Insert a new extent page */
1011 xfs_iext_add_indirect_multi(ifp
,
1012 erp_idx
, page_idx
, ext_diff
);
1015 * If extent(s) are being appended to the last page in
1016 * the indirection array and the new extent(s) don't fit
1017 * in the page, then erp is NULL and erp_idx is set to
1018 * the next index needed in the indirection array.
1021 uint count
= ext_diff
;
1024 erp
= xfs_iext_irec_new(ifp
, erp_idx
);
1025 erp
->er_extcount
= min(count
, XFS_LINEAR_EXTS
);
1026 count
-= erp
->er_extcount
;
1032 ifp
->if_bytes
= new_size
;
1036 * This is called when incore extents are being added to the indirection
1037 * array and the new extents do not fit in the target extent list. The
1038 * erp_idx parameter contains the irec index for the target extent list
1039 * in the indirection array, and the idx parameter contains the extent
1040 * index within the list. The number of extents being added is stored
1041 * in the count parameter.
1043 * |-------| |-------|
1044 * | | | | idx - number of extents before idx
1046 * | | | | count - number of extents being inserted at idx
1047 * |-------| |-------|
1048 * | count | | nex2 | nex2 - number of extents after idx + count
1049 * |-------| |-------|
1052 xfs_iext_add_indirect_multi(
1053 xfs_ifork_t
*ifp
, /* inode fork pointer */
1054 int erp_idx
, /* target extent irec index */
1055 xfs_extnum_t idx
, /* index within target list */
1056 int count
) /* new extents being added */
1058 int byte_diff
; /* new bytes being added */
1059 xfs_ext_irec_t
*erp
; /* pointer to irec entry */
1060 xfs_extnum_t ext_diff
; /* number of extents to add */
1061 xfs_extnum_t ext_cnt
; /* new extents still needed */
1062 xfs_extnum_t nex2
; /* extents after idx + count */
1063 xfs_bmbt_rec_t
*nex2_ep
= NULL
; /* temp list for nex2 extents */
1064 int nlists
; /* number of irec's (lists) */
1066 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1067 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1068 nex2
= erp
->er_extcount
- idx
;
1069 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1072 * Save second part of target extent list
1073 * (all extents past */
1075 byte_diff
= nex2
* sizeof(xfs_bmbt_rec_t
);
1076 nex2_ep
= (xfs_bmbt_rec_t
*) kmem_alloc(byte_diff
, KM_NOFS
);
1077 memmove(nex2_ep
, &erp
->er_extbuf
[idx
], byte_diff
);
1078 erp
->er_extcount
-= nex2
;
1079 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, -nex2
);
1080 memset(&erp
->er_extbuf
[idx
], 0, byte_diff
);
1084 * Add the new extents to the end of the target
1085 * list, then allocate new irec record(s) and
1086 * extent buffer(s) as needed to store the rest
1087 * of the new extents.
1090 ext_diff
= MIN(ext_cnt
, (int)XFS_LINEAR_EXTS
- erp
->er_extcount
);
1092 erp
->er_extcount
+= ext_diff
;
1093 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, ext_diff
);
1094 ext_cnt
-= ext_diff
;
1098 erp
= xfs_iext_irec_new(ifp
, erp_idx
);
1099 ext_diff
= MIN(ext_cnt
, (int)XFS_LINEAR_EXTS
);
1100 erp
->er_extcount
= ext_diff
;
1101 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, ext_diff
);
1102 ext_cnt
-= ext_diff
;
1105 /* Add nex2 extents back to indirection array */
1107 xfs_extnum_t ext_avail
;
1110 byte_diff
= nex2
* sizeof(xfs_bmbt_rec_t
);
1111 ext_avail
= XFS_LINEAR_EXTS
- erp
->er_extcount
;
1114 * If nex2 extents fit in the current page, append
1115 * nex2_ep after the new extents.
1117 if (nex2
<= ext_avail
) {
1118 i
= erp
->er_extcount
;
1121 * Otherwise, check if space is available in the
1124 else if ((erp_idx
< nlists
- 1) &&
1125 (nex2
<= (ext_avail
= XFS_LINEAR_EXTS
-
1126 ifp
->if_u1
.if_ext_irec
[erp_idx
+1].er_extcount
))) {
1129 /* Create a hole for nex2 extents */
1130 memmove(&erp
->er_extbuf
[nex2
], erp
->er_extbuf
,
1131 erp
->er_extcount
* sizeof(xfs_bmbt_rec_t
));
1134 * Final choice, create a new extent page for
1139 erp
= xfs_iext_irec_new(ifp
, erp_idx
);
1141 memmove(&erp
->er_extbuf
[i
], nex2_ep
, byte_diff
);
1143 erp
->er_extcount
+= nex2
;
1144 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, nex2
);
1149 * This is called when the amount of space required for incore file
1150 * extents needs to be decreased. The ext_diff parameter stores the
1151 * number of extents to be removed and the idx parameter contains
1152 * the extent index where the extents will be removed from.
1154 * If the amount of space needed has decreased below the linear
1155 * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
1156 * extent array. Otherwise, use kmem_realloc() to adjust the
1157 * size to what is needed.
1161 xfs_inode_t
*ip
, /* incore inode pointer */
1162 xfs_extnum_t idx
, /* index to begin removing exts */
1163 int ext_diff
, /* number of extents to remove */
1164 int state
) /* type of extent conversion */
1166 xfs_ifork_t
*ifp
= (state
& BMAP_ATTRFORK
) ? ip
->i_afp
: &ip
->i_df
;
1167 xfs_extnum_t nextents
; /* number of extents in file */
1168 int new_size
; /* size of extents after removal */
1170 trace_xfs_iext_remove(ip
, idx
, state
, _RET_IP_
);
1172 ASSERT(ext_diff
> 0);
1173 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1174 new_size
= (nextents
- ext_diff
) * sizeof(xfs_bmbt_rec_t
);
1176 if (new_size
== 0) {
1177 xfs_iext_destroy(ifp
);
1178 } else if (ifp
->if_flags
& XFS_IFEXTIREC
) {
1179 xfs_iext_remove_indirect(ifp
, idx
, ext_diff
);
1180 } else if (ifp
->if_real_bytes
) {
1181 xfs_iext_remove_direct(ifp
, idx
, ext_diff
);
1183 xfs_iext_remove_inline(ifp
, idx
, ext_diff
);
1185 ifp
->if_bytes
= new_size
;
1189 * This removes ext_diff extents from the inline buffer, beginning
1190 * at extent index idx.
1193 xfs_iext_remove_inline(
1194 xfs_ifork_t
*ifp
, /* inode fork pointer */
1195 xfs_extnum_t idx
, /* index to begin removing exts */
1196 int ext_diff
) /* number of extents to remove */
1198 int nextents
; /* number of extents in file */
1200 ASSERT(!(ifp
->if_flags
& XFS_IFEXTIREC
));
1201 ASSERT(idx
< XFS_INLINE_EXTS
);
1202 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1203 ASSERT(((nextents
- ext_diff
) > 0) &&
1204 (nextents
- ext_diff
) < XFS_INLINE_EXTS
);
1206 if (idx
+ ext_diff
< nextents
) {
1207 memmove(&ifp
->if_u2
.if_inline_ext
[idx
],
1208 &ifp
->if_u2
.if_inline_ext
[idx
+ ext_diff
],
1209 (nextents
- (idx
+ ext_diff
)) *
1210 sizeof(xfs_bmbt_rec_t
));
1211 memset(&ifp
->if_u2
.if_inline_ext
[nextents
- ext_diff
],
1212 0, ext_diff
* sizeof(xfs_bmbt_rec_t
));
1214 memset(&ifp
->if_u2
.if_inline_ext
[idx
], 0,
1215 ext_diff
* sizeof(xfs_bmbt_rec_t
));
1220 * This removes ext_diff extents from a linear (direct) extent list,
1221 * beginning at extent index idx. If the extents are being removed
1222 * from the end of the list (ie. truncate) then we just need to re-
1223 * allocate the list to remove the extra space. Otherwise, if the
1224 * extents are being removed from the middle of the existing extent
1225 * entries, then we first need to move the extent records beginning
1226 * at idx + ext_diff up in the list to overwrite the records being
1227 * removed, then remove the extra space via kmem_realloc.
1230 xfs_iext_remove_direct(
1231 xfs_ifork_t
*ifp
, /* inode fork pointer */
1232 xfs_extnum_t idx
, /* index to begin removing exts */
1233 int ext_diff
) /* number of extents to remove */
1235 xfs_extnum_t nextents
; /* number of extents in file */
1236 int new_size
; /* size of extents after removal */
1238 ASSERT(!(ifp
->if_flags
& XFS_IFEXTIREC
));
1239 new_size
= ifp
->if_bytes
-
1240 (ext_diff
* sizeof(xfs_bmbt_rec_t
));
1241 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1243 if (new_size
== 0) {
1244 xfs_iext_destroy(ifp
);
1247 /* Move extents up in the list (if needed) */
1248 if (idx
+ ext_diff
< nextents
) {
1249 memmove(&ifp
->if_u1
.if_extents
[idx
],
1250 &ifp
->if_u1
.if_extents
[idx
+ ext_diff
],
1251 (nextents
- (idx
+ ext_diff
)) *
1252 sizeof(xfs_bmbt_rec_t
));
1254 memset(&ifp
->if_u1
.if_extents
[nextents
- ext_diff
],
1255 0, ext_diff
* sizeof(xfs_bmbt_rec_t
));
1257 * Reallocate the direct extent list. If the extents
1258 * will fit inside the inode then xfs_iext_realloc_direct
1259 * will switch from direct to inline extent allocation
1262 xfs_iext_realloc_direct(ifp
, new_size
);
1263 ifp
->if_bytes
= new_size
;
1267 * This is called when incore extents are being removed from the
1268 * indirection array and the extents being removed span multiple extent
1269 * buffers. The idx parameter contains the file extent index where we
1270 * want to begin removing extents, and the count parameter contains
1271 * how many extents need to be removed.
1273 * |-------| |-------|
1274 * | nex1 | | | nex1 - number of extents before idx
1275 * |-------| | count |
1276 * | | | | count - number of extents being removed at idx
1277 * | count | |-------|
1278 * | | | nex2 | nex2 - number of extents after idx + count
1279 * |-------| |-------|
1282 xfs_iext_remove_indirect(
1283 xfs_ifork_t
*ifp
, /* inode fork pointer */
1284 xfs_extnum_t idx
, /* index to begin removing extents */
1285 int count
) /* number of extents to remove */
1287 xfs_ext_irec_t
*erp
; /* indirection array pointer */
1288 int erp_idx
= 0; /* indirection array index */
1289 xfs_extnum_t ext_cnt
; /* extents left to remove */
1290 xfs_extnum_t ext_diff
; /* extents to remove in current list */
1291 xfs_extnum_t nex1
; /* number of extents before idx */
1292 xfs_extnum_t nex2
; /* extents after idx + count */
1293 int page_idx
= idx
; /* index in target extent list */
1295 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1296 erp
= xfs_iext_idx_to_irec(ifp
, &page_idx
, &erp_idx
, 0);
1297 ASSERT(erp
!= NULL
);
1301 nex2
= MAX((erp
->er_extcount
- (nex1
+ ext_cnt
)), 0);
1302 ext_diff
= MIN(ext_cnt
, (erp
->er_extcount
- nex1
));
1304 * Check for deletion of entire list;
1305 * xfs_iext_irec_remove() updates extent offsets.
1307 if (ext_diff
== erp
->er_extcount
) {
1308 xfs_iext_irec_remove(ifp
, erp_idx
);
1309 ext_cnt
-= ext_diff
;
1312 ASSERT(erp_idx
< ifp
->if_real_bytes
/
1314 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1321 /* Move extents up (if needed) */
1323 memmove(&erp
->er_extbuf
[nex1
],
1324 &erp
->er_extbuf
[nex1
+ ext_diff
],
1325 nex2
* sizeof(xfs_bmbt_rec_t
));
1327 /* Zero out rest of page */
1328 memset(&erp
->er_extbuf
[nex1
+ nex2
], 0, (XFS_IEXT_BUFSZ
-
1329 ((nex1
+ nex2
) * sizeof(xfs_bmbt_rec_t
))));
1330 /* Update remaining counters */
1331 erp
->er_extcount
-= ext_diff
;
1332 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, -ext_diff
);
1333 ext_cnt
-= ext_diff
;
1338 ifp
->if_bytes
-= count
* sizeof(xfs_bmbt_rec_t
);
1339 xfs_iext_irec_compact(ifp
);
1343 * Create, destroy, or resize a linear (direct) block of extents.
1346 xfs_iext_realloc_direct(
1347 xfs_ifork_t
*ifp
, /* inode fork pointer */
1348 int new_size
) /* new size of extents after adding */
1350 int rnew_size
; /* real new size of extents */
1352 rnew_size
= new_size
;
1354 ASSERT(!(ifp
->if_flags
& XFS_IFEXTIREC
) ||
1355 ((new_size
>= 0) && (new_size
<= XFS_IEXT_BUFSZ
) &&
1356 (new_size
!= ifp
->if_real_bytes
)));
1358 /* Free extent records */
1359 if (new_size
== 0) {
1360 xfs_iext_destroy(ifp
);
1362 /* Resize direct extent list and zero any new bytes */
1363 else if (ifp
->if_real_bytes
) {
1364 /* Check if extents will fit inside the inode */
1365 if (new_size
<= XFS_INLINE_EXTS
* sizeof(xfs_bmbt_rec_t
)) {
1366 xfs_iext_direct_to_inline(ifp
, new_size
/
1367 (uint
)sizeof(xfs_bmbt_rec_t
));
1368 ifp
->if_bytes
= new_size
;
1371 if (!is_power_of_2(new_size
)){
1372 rnew_size
= roundup_pow_of_two(new_size
);
1374 if (rnew_size
!= ifp
->if_real_bytes
) {
1375 ifp
->if_u1
.if_extents
=
1376 kmem_realloc(ifp
->if_u1
.if_extents
,
1377 rnew_size
, KM_NOFS
);
1379 if (rnew_size
> ifp
->if_real_bytes
) {
1380 memset(&ifp
->if_u1
.if_extents
[ifp
->if_bytes
/
1381 (uint
)sizeof(xfs_bmbt_rec_t
)], 0,
1382 rnew_size
- ifp
->if_real_bytes
);
1385 /* Switch from the inline extent buffer to a direct extent list */
1387 if (!is_power_of_2(new_size
)) {
1388 rnew_size
= roundup_pow_of_two(new_size
);
1390 xfs_iext_inline_to_direct(ifp
, rnew_size
);
1392 ifp
->if_real_bytes
= rnew_size
;
1393 ifp
->if_bytes
= new_size
;
1397 * Switch from linear (direct) extent records to inline buffer.
1400 xfs_iext_direct_to_inline(
1401 xfs_ifork_t
*ifp
, /* inode fork pointer */
1402 xfs_extnum_t nextents
) /* number of extents in file */
1404 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1405 ASSERT(nextents
<= XFS_INLINE_EXTS
);
1407 * The inline buffer was zeroed when we switched
1408 * from inline to direct extent allocation mode,
1409 * so we don't need to clear it here.
1411 memcpy(ifp
->if_u2
.if_inline_ext
, ifp
->if_u1
.if_extents
,
1412 nextents
* sizeof(xfs_bmbt_rec_t
));
1413 kmem_free(ifp
->if_u1
.if_extents
);
1414 ifp
->if_u1
.if_extents
= ifp
->if_u2
.if_inline_ext
;
1415 ifp
->if_real_bytes
= 0;
1419 * Switch from inline buffer to linear (direct) extent records.
1420 * new_size should already be rounded up to the next power of 2
1421 * by the caller (when appropriate), so use new_size as it is.
1422 * However, since new_size may be rounded up, we can't update
1423 * if_bytes here. It is the caller's responsibility to update
1424 * if_bytes upon return.
1427 xfs_iext_inline_to_direct(
1428 xfs_ifork_t
*ifp
, /* inode fork pointer */
1429 int new_size
) /* number of extents in file */
1431 ifp
->if_u1
.if_extents
= kmem_alloc(new_size
, KM_NOFS
);
1432 memset(ifp
->if_u1
.if_extents
, 0, new_size
);
1433 if (ifp
->if_bytes
) {
1434 memcpy(ifp
->if_u1
.if_extents
, ifp
->if_u2
.if_inline_ext
,
1436 memset(ifp
->if_u2
.if_inline_ext
, 0, XFS_INLINE_EXTS
*
1437 sizeof(xfs_bmbt_rec_t
));
1439 ifp
->if_real_bytes
= new_size
;
1443 * Resize an extent indirection array to new_size bytes.
1446 xfs_iext_realloc_indirect(
1447 xfs_ifork_t
*ifp
, /* inode fork pointer */
1448 int new_size
) /* new indirection array size */
1450 int nlists
; /* number of irec's (ex lists) */
1451 int size
; /* current indirection array size */
1453 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1454 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1455 size
= nlists
* sizeof(xfs_ext_irec_t
);
1456 ASSERT(ifp
->if_real_bytes
);
1457 ASSERT((new_size
>= 0) && (new_size
!= size
));
1458 if (new_size
== 0) {
1459 xfs_iext_destroy(ifp
);
1461 ifp
->if_u1
.if_ext_irec
=
1462 kmem_realloc(ifp
->if_u1
.if_ext_irec
, new_size
, KM_NOFS
);
1467 * Switch from indirection array to linear (direct) extent allocations.
1470 xfs_iext_indirect_to_direct(
1471 xfs_ifork_t
*ifp
) /* inode fork pointer */
1473 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1474 xfs_extnum_t nextents
; /* number of extents in file */
1475 int size
; /* size of file extents */
1477 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1478 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1479 ASSERT(nextents
<= XFS_LINEAR_EXTS
);
1480 size
= nextents
* sizeof(xfs_bmbt_rec_t
);
1482 xfs_iext_irec_compact_pages(ifp
);
1483 ASSERT(ifp
->if_real_bytes
== XFS_IEXT_BUFSZ
);
1485 ep
= ifp
->if_u1
.if_ext_irec
->er_extbuf
;
1486 kmem_free(ifp
->if_u1
.if_ext_irec
);
1487 ifp
->if_flags
&= ~XFS_IFEXTIREC
;
1488 ifp
->if_u1
.if_extents
= ep
;
1489 ifp
->if_bytes
= size
;
1490 if (nextents
< XFS_LINEAR_EXTS
) {
1491 xfs_iext_realloc_direct(ifp
, size
);
1496 * Free incore file extents.
1500 xfs_ifork_t
*ifp
) /* inode fork pointer */
1502 if (ifp
->if_flags
& XFS_IFEXTIREC
) {
1506 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1507 for (erp_idx
= nlists
- 1; erp_idx
>= 0 ; erp_idx
--) {
1508 xfs_iext_irec_remove(ifp
, erp_idx
);
1510 ifp
->if_flags
&= ~XFS_IFEXTIREC
;
1511 } else if (ifp
->if_real_bytes
) {
1512 kmem_free(ifp
->if_u1
.if_extents
);
1513 } else if (ifp
->if_bytes
) {
1514 memset(ifp
->if_u2
.if_inline_ext
, 0, XFS_INLINE_EXTS
*
1515 sizeof(xfs_bmbt_rec_t
));
1517 ifp
->if_u1
.if_extents
= NULL
;
1518 ifp
->if_real_bytes
= 0;
1523 * Return a pointer to the extent record for file system block bno.
1525 xfs_bmbt_rec_host_t
* /* pointer to found extent record */
1526 xfs_iext_bno_to_ext(
1527 xfs_ifork_t
*ifp
, /* inode fork pointer */
1528 xfs_fileoff_t bno
, /* block number to search for */
1529 xfs_extnum_t
*idxp
) /* index of target extent */
1531 xfs_bmbt_rec_host_t
*base
; /* pointer to first extent */
1532 xfs_filblks_t blockcount
= 0; /* number of blocks in extent */
1533 xfs_bmbt_rec_host_t
*ep
= NULL
; /* pointer to target extent */
1534 xfs_ext_irec_t
*erp
= NULL
; /* indirection array pointer */
1535 int high
; /* upper boundary in search */
1536 xfs_extnum_t idx
= 0; /* index of target extent */
1537 int low
; /* lower boundary in search */
1538 xfs_extnum_t nextents
; /* number of file extents */
1539 xfs_fileoff_t startoff
= 0; /* start offset of extent */
1541 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1542 if (nextents
== 0) {
1547 if (ifp
->if_flags
& XFS_IFEXTIREC
) {
1548 /* Find target extent list */
1550 erp
= xfs_iext_bno_to_irec(ifp
, bno
, &erp_idx
);
1551 base
= erp
->er_extbuf
;
1552 high
= erp
->er_extcount
- 1;
1554 base
= ifp
->if_u1
.if_extents
;
1555 high
= nextents
- 1;
1557 /* Binary search extent records */
1558 while (low
<= high
) {
1559 idx
= (low
+ high
) >> 1;
1561 startoff
= xfs_bmbt_get_startoff(ep
);
1562 blockcount
= xfs_bmbt_get_blockcount(ep
);
1563 if (bno
< startoff
) {
1565 } else if (bno
>= startoff
+ blockcount
) {
1568 /* Convert back to file-based extent index */
1569 if (ifp
->if_flags
& XFS_IFEXTIREC
) {
1570 idx
+= erp
->er_extoff
;
1576 /* Convert back to file-based extent index */
1577 if (ifp
->if_flags
& XFS_IFEXTIREC
) {
1578 idx
+= erp
->er_extoff
;
1580 if (bno
>= startoff
+ blockcount
) {
1581 if (++idx
== nextents
) {
1584 ep
= xfs_iext_get_ext(ifp
, idx
);
1592 * Return a pointer to the indirection array entry containing the
1593 * extent record for filesystem block bno. Store the index of the
1594 * target irec in *erp_idxp.
1596 xfs_ext_irec_t
* /* pointer to found extent record */
1597 xfs_iext_bno_to_irec(
1598 xfs_ifork_t
*ifp
, /* inode fork pointer */
1599 xfs_fileoff_t bno
, /* block number to search for */
1600 int *erp_idxp
) /* irec index of target ext list */
1602 xfs_ext_irec_t
*erp
= NULL
; /* indirection array pointer */
1603 xfs_ext_irec_t
*erp_next
; /* next indirection array entry */
1604 int erp_idx
; /* indirection array index */
1605 int nlists
; /* number of extent irec's (lists) */
1606 int high
; /* binary search upper limit */
1607 int low
; /* binary search lower limit */
1609 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1610 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1614 while (low
<= high
) {
1615 erp_idx
= (low
+ high
) >> 1;
1616 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1617 erp_next
= erp_idx
< nlists
- 1 ? erp
+ 1 : NULL
;
1618 if (bno
< xfs_bmbt_get_startoff(erp
->er_extbuf
)) {
1620 } else if (erp_next
&& bno
>=
1621 xfs_bmbt_get_startoff(erp_next
->er_extbuf
)) {
1627 *erp_idxp
= erp_idx
;
1632 * Return a pointer to the indirection array entry containing the
1633 * extent record at file extent index *idxp. Store the index of the
1634 * target irec in *erp_idxp and store the page index of the target
1635 * extent record in *idxp.
1638 xfs_iext_idx_to_irec(
1639 xfs_ifork_t
*ifp
, /* inode fork pointer */
1640 xfs_extnum_t
*idxp
, /* extent index (file -> page) */
1641 int *erp_idxp
, /* pointer to target irec */
1642 int realloc
) /* new bytes were just added */
1644 xfs_ext_irec_t
*prev
; /* pointer to previous irec */
1645 xfs_ext_irec_t
*erp
= NULL
; /* pointer to current irec */
1646 int erp_idx
; /* indirection array index */
1647 int nlists
; /* number of irec's (ex lists) */
1648 int high
; /* binary search upper limit */
1649 int low
; /* binary search lower limit */
1650 xfs_extnum_t page_idx
= *idxp
; /* extent index in target list */
1652 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1653 ASSERT(page_idx
>= 0);
1654 ASSERT(page_idx
<= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
));
1655 ASSERT(page_idx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
) || realloc
);
1657 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1662 /* Binary search extent irec's */
1663 while (low
<= high
) {
1664 erp_idx
= (low
+ high
) >> 1;
1665 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1666 prev
= erp_idx
> 0 ? erp
- 1 : NULL
;
1667 if (page_idx
< erp
->er_extoff
|| (page_idx
== erp
->er_extoff
&&
1668 realloc
&& prev
&& prev
->er_extcount
< XFS_LINEAR_EXTS
)) {
1670 } else if (page_idx
> erp
->er_extoff
+ erp
->er_extcount
||
1671 (page_idx
== erp
->er_extoff
+ erp
->er_extcount
&&
1674 } else if (page_idx
== erp
->er_extoff
+ erp
->er_extcount
&&
1675 erp
->er_extcount
== XFS_LINEAR_EXTS
) {
1679 erp
= erp_idx
< nlists
? erp
+ 1 : NULL
;
1682 page_idx
-= erp
->er_extoff
;
1687 *erp_idxp
= erp_idx
;
1692 * Allocate and initialize an indirection array once the space needed
1693 * for incore extents increases above XFS_IEXT_BUFSZ.
1697 xfs_ifork_t
*ifp
) /* inode fork pointer */
1699 xfs_ext_irec_t
*erp
; /* indirection array pointer */
1700 xfs_extnum_t nextents
; /* number of extents in file */
1702 ASSERT(!(ifp
->if_flags
& XFS_IFEXTIREC
));
1703 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1704 ASSERT(nextents
<= XFS_LINEAR_EXTS
);
1706 erp
= kmem_alloc(sizeof(xfs_ext_irec_t
), KM_NOFS
);
1708 if (nextents
== 0) {
1709 ifp
->if_u1
.if_extents
= kmem_alloc(XFS_IEXT_BUFSZ
, KM_NOFS
);
1710 } else if (!ifp
->if_real_bytes
) {
1711 xfs_iext_inline_to_direct(ifp
, XFS_IEXT_BUFSZ
);
1712 } else if (ifp
->if_real_bytes
< XFS_IEXT_BUFSZ
) {
1713 xfs_iext_realloc_direct(ifp
, XFS_IEXT_BUFSZ
);
1715 erp
->er_extbuf
= ifp
->if_u1
.if_extents
;
1716 erp
->er_extcount
= nextents
;
1719 ifp
->if_flags
|= XFS_IFEXTIREC
;
1720 ifp
->if_real_bytes
= XFS_IEXT_BUFSZ
;
1721 ifp
->if_bytes
= nextents
* sizeof(xfs_bmbt_rec_t
);
1722 ifp
->if_u1
.if_ext_irec
= erp
;
1728 * Allocate and initialize a new entry in the indirection array.
1732 xfs_ifork_t
*ifp
, /* inode fork pointer */
1733 int erp_idx
) /* index for new irec */
1735 xfs_ext_irec_t
*erp
; /* indirection array pointer */
1736 int i
; /* loop counter */
1737 int nlists
; /* number of irec's (ex lists) */
1739 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1740 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1742 /* Resize indirection array */
1743 xfs_iext_realloc_indirect(ifp
, ++nlists
*
1744 sizeof(xfs_ext_irec_t
));
1746 * Move records down in the array so the
1747 * new page can use erp_idx.
1749 erp
= ifp
->if_u1
.if_ext_irec
;
1750 for (i
= nlists
- 1; i
> erp_idx
; i
--) {
1751 memmove(&erp
[i
], &erp
[i
-1], sizeof(xfs_ext_irec_t
));
1753 ASSERT(i
== erp_idx
);
1755 /* Initialize new extent record */
1756 erp
= ifp
->if_u1
.if_ext_irec
;
1757 erp
[erp_idx
].er_extbuf
= kmem_alloc(XFS_IEXT_BUFSZ
, KM_NOFS
);
1758 ifp
->if_real_bytes
= nlists
* XFS_IEXT_BUFSZ
;
1759 memset(erp
[erp_idx
].er_extbuf
, 0, XFS_IEXT_BUFSZ
);
1760 erp
[erp_idx
].er_extcount
= 0;
1761 erp
[erp_idx
].er_extoff
= erp_idx
> 0 ?
1762 erp
[erp_idx
-1].er_extoff
+ erp
[erp_idx
-1].er_extcount
: 0;
1763 return (&erp
[erp_idx
]);
1767 * Remove a record from the indirection array.
1770 xfs_iext_irec_remove(
1771 xfs_ifork_t
*ifp
, /* inode fork pointer */
1772 int erp_idx
) /* irec index to remove */
1774 xfs_ext_irec_t
*erp
; /* indirection array pointer */
1775 int i
; /* loop counter */
1776 int nlists
; /* number of irec's (ex lists) */
1778 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1779 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1780 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1781 if (erp
->er_extbuf
) {
1782 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1,
1784 kmem_free(erp
->er_extbuf
);
1786 /* Compact extent records */
1787 erp
= ifp
->if_u1
.if_ext_irec
;
1788 for (i
= erp_idx
; i
< nlists
- 1; i
++) {
1789 memmove(&erp
[i
], &erp
[i
+1], sizeof(xfs_ext_irec_t
));
1792 * Manually free the last extent record from the indirection
1793 * array. A call to xfs_iext_realloc_indirect() with a size
1794 * of zero would result in a call to xfs_iext_destroy() which
1795 * would in turn call this function again, creating a nasty
1799 xfs_iext_realloc_indirect(ifp
,
1800 nlists
* sizeof(xfs_ext_irec_t
));
1802 kmem_free(ifp
->if_u1
.if_ext_irec
);
1804 ifp
->if_real_bytes
= nlists
* XFS_IEXT_BUFSZ
;
1808 * This is called to clean up large amounts of unused memory allocated
1809 * by the indirection array. Before compacting anything though, verify
1810 * that the indirection array is still needed and switch back to the
1811 * linear extent list (or even the inline buffer) if possible. The
1812 * compaction policy is as follows:
1814 * Full Compaction: Extents fit into a single page (or inline buffer)
1815 * Partial Compaction: Extents occupy less than 50% of allocated space
1816 * No Compaction: Extents occupy at least 50% of allocated space
1819 xfs_iext_irec_compact(
1820 xfs_ifork_t
*ifp
) /* inode fork pointer */
1822 xfs_extnum_t nextents
; /* number of extents in file */
1823 int nlists
; /* number of irec's (ex lists) */
1825 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1826 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1827 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1829 if (nextents
== 0) {
1830 xfs_iext_destroy(ifp
);
1831 } else if (nextents
<= XFS_INLINE_EXTS
) {
1832 xfs_iext_indirect_to_direct(ifp
);
1833 xfs_iext_direct_to_inline(ifp
, nextents
);
1834 } else if (nextents
<= XFS_LINEAR_EXTS
) {
1835 xfs_iext_indirect_to_direct(ifp
);
1836 } else if (nextents
< (nlists
* XFS_LINEAR_EXTS
) >> 1) {
1837 xfs_iext_irec_compact_pages(ifp
);
1842 * Combine extents from neighboring extent pages.
1845 xfs_iext_irec_compact_pages(
1846 xfs_ifork_t
*ifp
) /* inode fork pointer */
1848 xfs_ext_irec_t
*erp
, *erp_next
;/* pointers to irec entries */
1849 int erp_idx
= 0; /* indirection array index */
1850 int nlists
; /* number of irec's (ex lists) */
1852 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1853 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1854 while (erp_idx
< nlists
- 1) {
1855 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1857 if (erp_next
->er_extcount
<=
1858 (XFS_LINEAR_EXTS
- erp
->er_extcount
)) {
1859 memcpy(&erp
->er_extbuf
[erp
->er_extcount
],
1860 erp_next
->er_extbuf
, erp_next
->er_extcount
*
1861 sizeof(xfs_bmbt_rec_t
));
1862 erp
->er_extcount
+= erp_next
->er_extcount
;
1864 * Free page before removing extent record
1865 * so er_extoffs don't get modified in
1866 * xfs_iext_irec_remove.
1868 kmem_free(erp_next
->er_extbuf
);
1869 erp_next
->er_extbuf
= NULL
;
1870 xfs_iext_irec_remove(ifp
, erp_idx
+ 1);
1871 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1879 * This is called to update the er_extoff field in the indirection
1880 * array when extents have been added or removed from one of the
1881 * extent lists. erp_idx contains the irec index to begin updating
1882 * at and ext_diff contains the number of extents that were added
1886 xfs_iext_irec_update_extoffs(
1887 xfs_ifork_t
*ifp
, /* inode fork pointer */
1888 int erp_idx
, /* irec index to update */
1889 int ext_diff
) /* number of new extents */
1891 int i
; /* loop counter */
1892 int nlists
; /* number of irec's (ex lists */
1894 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1895 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1896 for (i
= erp_idx
; i
< nlists
; i
++) {
1897 ifp
->if_u1
.if_ext_irec
[i
].er_extoff
+= ext_diff
;