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"
28 #include "xfs_mount.h"
29 #include "xfs_inode.h"
30 #include "xfs_trans.h"
31 #include "xfs_inode_item.h"
32 #include "xfs_bmap_btree.h"
34 #include "xfs_error.h"
35 #include "xfs_trace.h"
36 #include "xfs_attr_sf.h"
37 #include "xfs_dinode.h"
39 kmem_zone_t
*xfs_ifork_zone
;
41 STATIC
int xfs_iformat_local(xfs_inode_t
*, xfs_dinode_t
*, int, int);
42 STATIC
int xfs_iformat_extents(xfs_inode_t
*, xfs_dinode_t
*, int);
43 STATIC
int xfs_iformat_btree(xfs_inode_t
*, xfs_dinode_t
*, int);
47 * Make sure that the extents in the given memory buffer
57 xfs_bmbt_rec_host_t rec
;
60 for (i
= 0; i
< nrecs
; i
++) {
61 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, i
);
62 rec
.l0
= get_unaligned(&ep
->l0
);
63 rec
.l1
= get_unaligned(&ep
->l1
);
64 xfs_bmbt_get_all(&rec
, &irec
);
65 if (fmt
== XFS_EXTFMT_NOSTATE
)
66 ASSERT(irec
.br_state
== XFS_EXT_NORM
);
70 #define xfs_validate_extents(ifp, nrecs, fmt)
75 * Move inode type and inode format specific information from the
76 * on-disk inode to the in-core inode. For fifos, devs, and sockets
77 * this means set if_rdev to the proper value. For files, directories,
78 * and symlinks this means to bring in the in-line data or extent
79 * pointers. For a file in B-tree format, only the root is immediately
80 * brought in-core. The rest will be in-lined in if_extents when it
81 * is first referenced (see xfs_iread_extents()).
88 xfs_attr_shortform_t
*atp
;
93 if (unlikely(be32_to_cpu(dip
->di_nextents
) +
94 be16_to_cpu(dip
->di_anextents
) >
95 be64_to_cpu(dip
->di_nblocks
))) {
97 "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.",
98 (unsigned long long)ip
->i_ino
,
99 (int)(be32_to_cpu(dip
->di_nextents
) +
100 be16_to_cpu(dip
->di_anextents
)),
102 be64_to_cpu(dip
->di_nblocks
));
103 XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW
,
105 return XFS_ERROR(EFSCORRUPTED
);
108 if (unlikely(dip
->di_forkoff
> ip
->i_mount
->m_sb
.sb_inodesize
)) {
109 xfs_warn(ip
->i_mount
, "corrupt dinode %Lu, forkoff = 0x%x.",
110 (unsigned long long)ip
->i_ino
,
112 XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW
,
114 return XFS_ERROR(EFSCORRUPTED
);
117 if (unlikely((ip
->i_d
.di_flags
& XFS_DIFLAG_REALTIME
) &&
118 !ip
->i_mount
->m_rtdev_targp
)) {
119 xfs_warn(ip
->i_mount
,
120 "corrupt dinode %Lu, has realtime flag set.",
122 XFS_CORRUPTION_ERROR("xfs_iformat(realtime)",
123 XFS_ERRLEVEL_LOW
, ip
->i_mount
, dip
);
124 return XFS_ERROR(EFSCORRUPTED
);
127 switch (ip
->i_d
.di_mode
& S_IFMT
) {
132 if (unlikely(dip
->di_format
!= XFS_DINODE_FMT_DEV
)) {
133 XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW
,
135 return XFS_ERROR(EFSCORRUPTED
);
138 ip
->i_df
.if_u2
.if_rdev
= xfs_dinode_get_rdev(dip
);
144 switch (dip
->di_format
) {
145 case XFS_DINODE_FMT_LOCAL
:
147 * no local regular files yet
149 if (unlikely(S_ISREG(be16_to_cpu(dip
->di_mode
)))) {
150 xfs_warn(ip
->i_mount
,
151 "corrupt inode %Lu (local format for regular file).",
152 (unsigned long long) ip
->i_ino
);
153 XFS_CORRUPTION_ERROR("xfs_iformat(4)",
156 return XFS_ERROR(EFSCORRUPTED
);
159 di_size
= be64_to_cpu(dip
->di_size
);
160 if (unlikely(di_size
< 0 ||
161 di_size
> XFS_DFORK_DSIZE(dip
, ip
->i_mount
))) {
162 xfs_warn(ip
->i_mount
,
163 "corrupt inode %Lu (bad size %Ld for local inode).",
164 (unsigned long long) ip
->i_ino
,
165 (long long) di_size
);
166 XFS_CORRUPTION_ERROR("xfs_iformat(5)",
169 return XFS_ERROR(EFSCORRUPTED
);
173 error
= xfs_iformat_local(ip
, dip
, XFS_DATA_FORK
, size
);
175 case XFS_DINODE_FMT_EXTENTS
:
176 error
= xfs_iformat_extents(ip
, dip
, XFS_DATA_FORK
);
178 case XFS_DINODE_FMT_BTREE
:
179 error
= xfs_iformat_btree(ip
, dip
, XFS_DATA_FORK
);
182 XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW
,
184 return XFS_ERROR(EFSCORRUPTED
);
189 XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW
, ip
->i_mount
);
190 return XFS_ERROR(EFSCORRUPTED
);
195 if (!XFS_DFORK_Q(dip
))
198 ASSERT(ip
->i_afp
== NULL
);
199 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
| KM_NOFS
);
201 switch (dip
->di_aformat
) {
202 case XFS_DINODE_FMT_LOCAL
:
203 atp
= (xfs_attr_shortform_t
*)XFS_DFORK_APTR(dip
);
204 size
= be16_to_cpu(atp
->hdr
.totsize
);
206 if (unlikely(size
< sizeof(struct xfs_attr_sf_hdr
))) {
207 xfs_warn(ip
->i_mount
,
208 "corrupt inode %Lu (bad attr fork size %Ld).",
209 (unsigned long long) ip
->i_ino
,
211 XFS_CORRUPTION_ERROR("xfs_iformat(8)",
214 return XFS_ERROR(EFSCORRUPTED
);
217 error
= xfs_iformat_local(ip
, dip
, XFS_ATTR_FORK
, size
);
219 case XFS_DINODE_FMT_EXTENTS
:
220 error
= xfs_iformat_extents(ip
, dip
, XFS_ATTR_FORK
);
222 case XFS_DINODE_FMT_BTREE
:
223 error
= xfs_iformat_btree(ip
, dip
, XFS_ATTR_FORK
);
226 error
= XFS_ERROR(EFSCORRUPTED
);
230 kmem_zone_free(xfs_ifork_zone
, ip
->i_afp
);
232 xfs_idestroy_fork(ip
, XFS_DATA_FORK
);
238 * The file is in-lined in the on-disk inode.
239 * If it fits into if_inline_data, then copy
240 * it there, otherwise allocate a buffer for it
241 * and copy the data there. Either way, set
242 * if_data to point at the data.
243 * If we allocate a buffer for the data, make
244 * sure that its size is a multiple of 4 and
245 * record the real size in i_real_bytes.
258 * If the size is unreasonable, then something
259 * is wrong and we just bail out rather than crash in
260 * kmem_alloc() or memcpy() below.
262 if (unlikely(size
> XFS_DFORK_SIZE(dip
, ip
->i_mount
, whichfork
))) {
263 xfs_warn(ip
->i_mount
,
264 "corrupt inode %Lu (bad size %d for local fork, size = %d).",
265 (unsigned long long) ip
->i_ino
, size
,
266 XFS_DFORK_SIZE(dip
, ip
->i_mount
, whichfork
));
267 XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW
,
269 return XFS_ERROR(EFSCORRUPTED
);
271 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
274 ifp
->if_u1
.if_data
= NULL
;
275 else if (size
<= sizeof(ifp
->if_u2
.if_inline_data
))
276 ifp
->if_u1
.if_data
= ifp
->if_u2
.if_inline_data
;
278 real_size
= roundup(size
, 4);
279 ifp
->if_u1
.if_data
= kmem_alloc(real_size
, KM_SLEEP
| KM_NOFS
);
281 ifp
->if_bytes
= size
;
282 ifp
->if_real_bytes
= real_size
;
284 memcpy(ifp
->if_u1
.if_data
, XFS_DFORK_PTR(dip
, whichfork
), size
);
285 ifp
->if_flags
&= ~XFS_IFEXTENTS
;
286 ifp
->if_flags
|= XFS_IFINLINE
;
291 * The file consists of a set of extents all
292 * of which fit into the on-disk inode.
293 * If there are few enough extents to fit into
294 * the if_inline_ext, then copy them there.
295 * Otherwise allocate a buffer for them and copy
296 * them into it. Either way, set if_extents
297 * to point at the extents.
311 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
312 nex
= XFS_DFORK_NEXTENTS(dip
, whichfork
);
313 size
= nex
* (uint
)sizeof(xfs_bmbt_rec_t
);
316 * If the number of extents is unreasonable, then something
317 * is wrong and we just bail out rather than crash in
318 * kmem_alloc() or memcpy() below.
320 if (unlikely(size
< 0 || size
> XFS_DFORK_SIZE(dip
, ip
->i_mount
, whichfork
))) {
321 xfs_warn(ip
->i_mount
, "corrupt inode %Lu ((a)extents = %d).",
322 (unsigned long long) ip
->i_ino
, nex
);
323 XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW
,
325 return XFS_ERROR(EFSCORRUPTED
);
328 ifp
->if_real_bytes
= 0;
330 ifp
->if_u1
.if_extents
= NULL
;
331 else if (nex
<= XFS_INLINE_EXTS
)
332 ifp
->if_u1
.if_extents
= ifp
->if_u2
.if_inline_ext
;
334 xfs_iext_add(ifp
, 0, nex
);
336 ifp
->if_bytes
= size
;
338 dp
= (xfs_bmbt_rec_t
*) XFS_DFORK_PTR(dip
, whichfork
);
339 xfs_validate_extents(ifp
, nex
, XFS_EXTFMT_INODE(ip
));
340 for (i
= 0; i
< nex
; i
++, dp
++) {
341 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, i
);
342 ep
->l0
= get_unaligned_be64(&dp
->l0
);
343 ep
->l1
= get_unaligned_be64(&dp
->l1
);
345 XFS_BMAP_TRACE_EXLIST(ip
, nex
, whichfork
);
346 if (whichfork
!= XFS_DATA_FORK
||
347 XFS_EXTFMT_INODE(ip
) == XFS_EXTFMT_NOSTATE
)
348 if (unlikely(xfs_check_nostate_extents(
350 XFS_ERROR_REPORT("xfs_iformat_extents(2)",
353 return XFS_ERROR(EFSCORRUPTED
);
356 ifp
->if_flags
|= XFS_IFEXTENTS
;
361 * The file has too many extents to fit into
362 * the inode, so they are in B-tree format.
363 * Allocate a buffer for the root of the B-tree
364 * and copy the root into it. The i_extents
365 * field will remain NULL until all of the
366 * extents are read in (when they are needed).
374 struct xfs_mount
*mp
= ip
->i_mount
;
375 xfs_bmdr_block_t
*dfp
;
381 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
382 dfp
= (xfs_bmdr_block_t
*)XFS_DFORK_PTR(dip
, whichfork
);
383 size
= XFS_BMAP_BROOT_SPACE(mp
, dfp
);
384 nrecs
= be16_to_cpu(dfp
->bb_numrecs
);
387 * blow out if -- fork has less extents than can fit in
388 * fork (fork shouldn't be a btree format), root btree
389 * block has more records than can fit into the fork,
390 * or the number of extents is greater than the number of
393 if (unlikely(XFS_IFORK_NEXTENTS(ip
, whichfork
) <=
394 XFS_IFORK_MAXEXT(ip
, whichfork
) ||
395 XFS_BMDR_SPACE_CALC(nrecs
) >
396 XFS_DFORK_SIZE(dip
, mp
, whichfork
) ||
397 XFS_IFORK_NEXTENTS(ip
, whichfork
) > ip
->i_d
.di_nblocks
)) {
398 xfs_warn(mp
, "corrupt inode %Lu (btree).",
399 (unsigned long long) ip
->i_ino
);
400 XFS_CORRUPTION_ERROR("xfs_iformat_btree", XFS_ERRLEVEL_LOW
,
402 return XFS_ERROR(EFSCORRUPTED
);
405 ifp
->if_broot_bytes
= size
;
406 ifp
->if_broot
= kmem_alloc(size
, KM_SLEEP
| KM_NOFS
);
407 ASSERT(ifp
->if_broot
!= NULL
);
409 * Copy and convert from the on-disk structure
410 * to the in-memory structure.
412 xfs_bmdr_to_bmbt(ip
, dfp
, XFS_DFORK_SIZE(dip
, ip
->i_mount
, whichfork
),
413 ifp
->if_broot
, size
);
414 ifp
->if_flags
&= ~XFS_IFEXTENTS
;
415 ifp
->if_flags
|= XFS_IFBROOT
;
421 * Read in extents from a btree-format inode.
422 * Allocate and fill in if_extents. Real work is done in xfs_bmap.c.
432 xfs_extnum_t nextents
;
434 if (unlikely(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
435 XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW
,
437 return XFS_ERROR(EFSCORRUPTED
);
439 nextents
= XFS_IFORK_NEXTENTS(ip
, whichfork
);
440 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
443 * We know that the size is valid (it's checked in iformat_btree)
445 ifp
->if_bytes
= ifp
->if_real_bytes
= 0;
446 ifp
->if_flags
|= XFS_IFEXTENTS
;
447 xfs_iext_add(ifp
, 0, nextents
);
448 error
= xfs_bmap_read_extents(tp
, ip
, whichfork
);
450 xfs_iext_destroy(ifp
);
451 ifp
->if_flags
&= ~XFS_IFEXTENTS
;
454 xfs_validate_extents(ifp
, nextents
, XFS_EXTFMT_INODE(ip
));
458 * Reallocate the space for if_broot based on the number of records
459 * being added or deleted as indicated in rec_diff. Move the records
460 * and pointers in if_broot to fit the new size. When shrinking this
461 * will eliminate holes between the records and pointers created by
462 * the caller. When growing this will create holes to be filled in
465 * The caller must not request to add more records than would fit in
466 * the on-disk inode root. If the if_broot is currently NULL, then
467 * if we are adding records, one will be allocated. The caller must also
468 * not request that the number of records go below zero, although
471 * ip -- the inode whose if_broot area is changing
472 * ext_diff -- the change in the number of records, positive or negative,
473 * requested for the if_broot array.
481 struct xfs_mount
*mp
= ip
->i_mount
;
484 struct xfs_btree_block
*new_broot
;
491 * Handle the degenerate case quietly.
497 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
500 * If there wasn't any memory allocated before, just
501 * allocate it now and get out.
503 if (ifp
->if_broot_bytes
== 0) {
504 new_size
= XFS_BMAP_BROOT_SPACE_CALC(mp
, rec_diff
);
505 ifp
->if_broot
= kmem_alloc(new_size
, KM_SLEEP
| KM_NOFS
);
506 ifp
->if_broot_bytes
= (int)new_size
;
511 * If there is already an existing if_broot, then we need
512 * to realloc() it and shift the pointers to their new
513 * location. The records don't change location because
514 * they are kept butted up against the btree block header.
516 cur_max
= xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0);
517 new_max
= cur_max
+ rec_diff
;
518 new_size
= XFS_BMAP_BROOT_SPACE_CALC(mp
, new_max
);
519 ifp
->if_broot
= kmem_realloc(ifp
->if_broot
, new_size
,
520 XFS_BMAP_BROOT_SPACE_CALC(mp
, cur_max
),
522 op
= (char *)XFS_BMAP_BROOT_PTR_ADDR(mp
, ifp
->if_broot
, 1,
523 ifp
->if_broot_bytes
);
524 np
= (char *)XFS_BMAP_BROOT_PTR_ADDR(mp
, ifp
->if_broot
, 1,
526 ifp
->if_broot_bytes
= (int)new_size
;
527 ASSERT(XFS_BMAP_BMDR_SPACE(ifp
->if_broot
) <=
528 XFS_IFORK_SIZE(ip
, whichfork
));
529 memmove(np
, op
, cur_max
* (uint
)sizeof(xfs_dfsbno_t
));
534 * rec_diff is less than 0. In this case, we are shrinking the
535 * if_broot buffer. It must already exist. If we go to zero
536 * records, just get rid of the root and clear the status bit.
538 ASSERT((ifp
->if_broot
!= NULL
) && (ifp
->if_broot_bytes
> 0));
539 cur_max
= xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0);
540 new_max
= cur_max
+ rec_diff
;
541 ASSERT(new_max
>= 0);
543 new_size
= XFS_BMAP_BROOT_SPACE_CALC(mp
, new_max
);
547 new_broot
= kmem_alloc(new_size
, KM_SLEEP
| KM_NOFS
);
549 * First copy over the btree block header.
551 memcpy(new_broot
, ifp
->if_broot
,
552 XFS_BMBT_BLOCK_LEN(ip
->i_mount
));
555 ifp
->if_flags
&= ~XFS_IFBROOT
;
559 * Only copy the records and pointers if there are any.
563 * First copy the records.
565 op
= (char *)XFS_BMBT_REC_ADDR(mp
, ifp
->if_broot
, 1);
566 np
= (char *)XFS_BMBT_REC_ADDR(mp
, new_broot
, 1);
567 memcpy(np
, op
, new_max
* (uint
)sizeof(xfs_bmbt_rec_t
));
570 * Then copy the pointers.
572 op
= (char *)XFS_BMAP_BROOT_PTR_ADDR(mp
, ifp
->if_broot
, 1,
573 ifp
->if_broot_bytes
);
574 np
= (char *)XFS_BMAP_BROOT_PTR_ADDR(mp
, new_broot
, 1,
576 memcpy(np
, op
, new_max
* (uint
)sizeof(xfs_dfsbno_t
));
578 kmem_free(ifp
->if_broot
);
579 ifp
->if_broot
= new_broot
;
580 ifp
->if_broot_bytes
= (int)new_size
;
582 ASSERT(XFS_BMAP_BMDR_SPACE(ifp
->if_broot
) <=
583 XFS_IFORK_SIZE(ip
, whichfork
));
589 * This is called when the amount of space needed for if_data
590 * is increased or decreased. The change in size is indicated by
591 * the number of bytes that need to be added or deleted in the
592 * byte_diff parameter.
594 * If the amount of space needed has decreased below the size of the
595 * inline buffer, then switch to using the inline buffer. Otherwise,
596 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
599 * ip -- the inode whose if_data area is changing
600 * byte_diff -- the change in the number of bytes, positive or negative,
601 * requested for the if_data array.
613 if (byte_diff
== 0) {
617 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
618 new_size
= (int)ifp
->if_bytes
+ byte_diff
;
619 ASSERT(new_size
>= 0);
622 if (ifp
->if_u1
.if_data
!= ifp
->if_u2
.if_inline_data
) {
623 kmem_free(ifp
->if_u1
.if_data
);
625 ifp
->if_u1
.if_data
= NULL
;
627 } else if (new_size
<= sizeof(ifp
->if_u2
.if_inline_data
)) {
629 * If the valid extents/data can fit in if_inline_ext/data,
630 * copy them from the malloc'd vector and free it.
632 if (ifp
->if_u1
.if_data
== NULL
) {
633 ifp
->if_u1
.if_data
= ifp
->if_u2
.if_inline_data
;
634 } else if (ifp
->if_u1
.if_data
!= ifp
->if_u2
.if_inline_data
) {
635 ASSERT(ifp
->if_real_bytes
!= 0);
636 memcpy(ifp
->if_u2
.if_inline_data
, ifp
->if_u1
.if_data
,
638 kmem_free(ifp
->if_u1
.if_data
);
639 ifp
->if_u1
.if_data
= ifp
->if_u2
.if_inline_data
;
644 * Stuck with malloc/realloc.
645 * For inline data, the underlying buffer must be
646 * a multiple of 4 bytes in size so that it can be
647 * logged and stay on word boundaries. We enforce
650 real_size
= roundup(new_size
, 4);
651 if (ifp
->if_u1
.if_data
== NULL
) {
652 ASSERT(ifp
->if_real_bytes
== 0);
653 ifp
->if_u1
.if_data
= kmem_alloc(real_size
,
655 } else if (ifp
->if_u1
.if_data
!= ifp
->if_u2
.if_inline_data
) {
657 * Only do the realloc if the underlying size
658 * is really changing.
660 if (ifp
->if_real_bytes
!= real_size
) {
662 kmem_realloc(ifp
->if_u1
.if_data
,
668 ASSERT(ifp
->if_real_bytes
== 0);
669 ifp
->if_u1
.if_data
= kmem_alloc(real_size
,
671 memcpy(ifp
->if_u1
.if_data
, ifp
->if_u2
.if_inline_data
,
675 ifp
->if_real_bytes
= real_size
;
676 ifp
->if_bytes
= new_size
;
677 ASSERT(ifp
->if_bytes
<= XFS_IFORK_SIZE(ip
, whichfork
));
687 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
688 if (ifp
->if_broot
!= NULL
) {
689 kmem_free(ifp
->if_broot
);
690 ifp
->if_broot
= NULL
;
694 * If the format is local, then we can't have an extents
695 * array so just look for an inline data array. If we're
696 * not local then we may or may not have an extents list,
697 * so check and free it up if we do.
699 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
700 if ((ifp
->if_u1
.if_data
!= ifp
->if_u2
.if_inline_data
) &&
701 (ifp
->if_u1
.if_data
!= NULL
)) {
702 ASSERT(ifp
->if_real_bytes
!= 0);
703 kmem_free(ifp
->if_u1
.if_data
);
704 ifp
->if_u1
.if_data
= NULL
;
705 ifp
->if_real_bytes
= 0;
707 } else if ((ifp
->if_flags
& XFS_IFEXTENTS
) &&
708 ((ifp
->if_flags
& XFS_IFEXTIREC
) ||
709 ((ifp
->if_u1
.if_extents
!= NULL
) &&
710 (ifp
->if_u1
.if_extents
!= ifp
->if_u2
.if_inline_ext
)))) {
711 ASSERT(ifp
->if_real_bytes
!= 0);
712 xfs_iext_destroy(ifp
);
714 ASSERT(ifp
->if_u1
.if_extents
== NULL
||
715 ifp
->if_u1
.if_extents
== ifp
->if_u2
.if_inline_ext
);
716 ASSERT(ifp
->if_real_bytes
== 0);
717 if (whichfork
== XFS_ATTR_FORK
) {
718 kmem_zone_free(xfs_ifork_zone
, ip
->i_afp
);
724 * xfs_iextents_copy()
726 * This is called to copy the REAL extents (as opposed to the delayed
727 * allocation extents) from the inode into the given buffer. It
728 * returns the number of bytes copied into the buffer.
730 * If there are no delayed allocation extents, then we can just
731 * memcpy() the extents into the buffer. Otherwise, we need to
732 * examine each extent in turn and skip those which are delayed.
744 xfs_fsblock_t start_block
;
746 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
747 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
|XFS_ILOCK_SHARED
));
748 ASSERT(ifp
->if_bytes
> 0);
750 nrecs
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
751 XFS_BMAP_TRACE_EXLIST(ip
, nrecs
, whichfork
);
755 * There are some delayed allocation extents in the
756 * inode, so copy the extents one at a time and skip
757 * the delayed ones. There must be at least one
758 * non-delayed extent.
761 for (i
= 0; i
< nrecs
; i
++) {
762 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, i
);
763 start_block
= xfs_bmbt_get_startblock(ep
);
764 if (isnullstartblock(start_block
)) {
766 * It's a delayed allocation extent, so skip it.
771 /* Translate to on disk format */
772 put_unaligned_be64(ep
->l0
, &dp
->l0
);
773 put_unaligned_be64(ep
->l1
, &dp
->l1
);
778 xfs_validate_extents(ifp
, copied
, XFS_EXTFMT_INODE(ip
));
780 return (copied
* (uint
)sizeof(xfs_bmbt_rec_t
));
784 * Each of the following cases stores data into the same region
785 * of the on-disk inode, so only one of them can be valid at
786 * any given time. While it is possible to have conflicting formats
787 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
788 * in EXTENTS format, this can only happen when the fork has
789 * changed formats after being modified but before being flushed.
790 * In these cases, the format always takes precedence, because the
791 * format indicates the current state of the fork.
797 xfs_inode_log_item_t
*iip
,
804 static const short brootflag
[2] =
805 { XFS_ILOG_DBROOT
, XFS_ILOG_ABROOT
};
806 static const short dataflag
[2] =
807 { XFS_ILOG_DDATA
, XFS_ILOG_ADATA
};
808 static const short extflag
[2] =
809 { XFS_ILOG_DEXT
, XFS_ILOG_AEXT
};
813 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
815 * This can happen if we gave up in iformat in an error path,
816 * for the attribute fork.
819 ASSERT(whichfork
== XFS_ATTR_FORK
);
822 cp
= XFS_DFORK_PTR(dip
, whichfork
);
824 switch (XFS_IFORK_FORMAT(ip
, whichfork
)) {
825 case XFS_DINODE_FMT_LOCAL
:
826 if ((iip
->ili_fields
& dataflag
[whichfork
]) &&
827 (ifp
->if_bytes
> 0)) {
828 ASSERT(ifp
->if_u1
.if_data
!= NULL
);
829 ASSERT(ifp
->if_bytes
<= XFS_IFORK_SIZE(ip
, whichfork
));
830 memcpy(cp
, ifp
->if_u1
.if_data
, ifp
->if_bytes
);
834 case XFS_DINODE_FMT_EXTENTS
:
835 ASSERT((ifp
->if_flags
& XFS_IFEXTENTS
) ||
836 !(iip
->ili_fields
& extflag
[whichfork
]));
837 if ((iip
->ili_fields
& extflag
[whichfork
]) &&
838 (ifp
->if_bytes
> 0)) {
839 ASSERT(xfs_iext_get_ext(ifp
, 0));
840 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) > 0);
841 (void)xfs_iextents_copy(ip
, (xfs_bmbt_rec_t
*)cp
,
846 case XFS_DINODE_FMT_BTREE
:
847 if ((iip
->ili_fields
& brootflag
[whichfork
]) &&
848 (ifp
->if_broot_bytes
> 0)) {
849 ASSERT(ifp
->if_broot
!= NULL
);
850 ASSERT(XFS_BMAP_BMDR_SPACE(ifp
->if_broot
) <=
851 XFS_IFORK_SIZE(ip
, whichfork
));
852 xfs_bmbt_to_bmdr(mp
, ifp
->if_broot
, ifp
->if_broot_bytes
,
853 (xfs_bmdr_block_t
*)cp
,
854 XFS_DFORK_SIZE(dip
, mp
, whichfork
));
858 case XFS_DINODE_FMT_DEV
:
859 if (iip
->ili_fields
& XFS_ILOG_DEV
) {
860 ASSERT(whichfork
== XFS_DATA_FORK
);
861 xfs_dinode_put_rdev(dip
, ip
->i_df
.if_u2
.if_rdev
);
865 case XFS_DINODE_FMT_UUID
:
866 if (iip
->ili_fields
& XFS_ILOG_UUID
) {
867 ASSERT(whichfork
== XFS_DATA_FORK
);
868 memcpy(XFS_DFORK_DPTR(dip
),
869 &ip
->i_df
.if_u2
.if_uuid
,
881 * Return a pointer to the extent record at file index idx.
883 xfs_bmbt_rec_host_t
*
885 xfs_ifork_t
*ifp
, /* inode fork pointer */
886 xfs_extnum_t idx
) /* index of target extent */
889 ASSERT(idx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
));
891 if ((ifp
->if_flags
& XFS_IFEXTIREC
) && (idx
== 0)) {
892 return ifp
->if_u1
.if_ext_irec
->er_extbuf
;
893 } else if (ifp
->if_flags
& XFS_IFEXTIREC
) {
894 xfs_ext_irec_t
*erp
; /* irec pointer */
895 int erp_idx
= 0; /* irec index */
896 xfs_extnum_t page_idx
= idx
; /* ext index in target list */
898 erp
= xfs_iext_idx_to_irec(ifp
, &page_idx
, &erp_idx
, 0);
899 return &erp
->er_extbuf
[page_idx
];
900 } else if (ifp
->if_bytes
) {
901 return &ifp
->if_u1
.if_extents
[idx
];
908 * Insert new item(s) into the extent records for incore inode
909 * fork 'ifp'. 'count' new items are inserted at index 'idx'.
913 xfs_inode_t
*ip
, /* incore inode pointer */
914 xfs_extnum_t idx
, /* starting index of new items */
915 xfs_extnum_t count
, /* number of inserted items */
916 xfs_bmbt_irec_t
*new, /* items to insert */
917 int state
) /* type of extent conversion */
919 xfs_ifork_t
*ifp
= (state
& BMAP_ATTRFORK
) ? ip
->i_afp
: &ip
->i_df
;
920 xfs_extnum_t i
; /* extent record index */
922 trace_xfs_iext_insert(ip
, idx
, new, state
, _RET_IP_
);
924 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
925 xfs_iext_add(ifp
, idx
, count
);
926 for (i
= idx
; i
< idx
+ count
; i
++, new++)
927 xfs_bmbt_set_all(xfs_iext_get_ext(ifp
, i
), new);
931 * This is called when the amount of space required for incore file
932 * extents needs to be increased. The ext_diff parameter stores the
933 * number of new extents being added and the idx parameter contains
934 * the extent index where the new extents will be added. If the new
935 * extents are being appended, then we just need to (re)allocate and
936 * initialize the space. Otherwise, if the new extents are being
937 * inserted into the middle of the existing entries, a bit more work
938 * is required to make room for the new extents to be inserted. The
939 * caller is responsible for filling in the new extent entries upon
944 xfs_ifork_t
*ifp
, /* inode fork pointer */
945 xfs_extnum_t idx
, /* index to begin adding exts */
946 int ext_diff
) /* number of extents to add */
948 int byte_diff
; /* new bytes being added */
949 int new_size
; /* size of extents after adding */
950 xfs_extnum_t nextents
; /* number of extents in file */
952 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
953 ASSERT((idx
>= 0) && (idx
<= nextents
));
954 byte_diff
= ext_diff
* sizeof(xfs_bmbt_rec_t
);
955 new_size
= ifp
->if_bytes
+ byte_diff
;
957 * If the new number of extents (nextents + ext_diff)
958 * fits inside the inode, then continue to use the inline
961 if (nextents
+ ext_diff
<= XFS_INLINE_EXTS
) {
962 if (idx
< nextents
) {
963 memmove(&ifp
->if_u2
.if_inline_ext
[idx
+ ext_diff
],
964 &ifp
->if_u2
.if_inline_ext
[idx
],
965 (nextents
- idx
) * sizeof(xfs_bmbt_rec_t
));
966 memset(&ifp
->if_u2
.if_inline_ext
[idx
], 0, byte_diff
);
968 ifp
->if_u1
.if_extents
= ifp
->if_u2
.if_inline_ext
;
969 ifp
->if_real_bytes
= 0;
972 * Otherwise use a linear (direct) extent list.
973 * If the extents are currently inside the inode,
974 * xfs_iext_realloc_direct will switch us from
975 * inline to direct extent allocation mode.
977 else if (nextents
+ ext_diff
<= XFS_LINEAR_EXTS
) {
978 xfs_iext_realloc_direct(ifp
, new_size
);
979 if (idx
< nextents
) {
980 memmove(&ifp
->if_u1
.if_extents
[idx
+ ext_diff
],
981 &ifp
->if_u1
.if_extents
[idx
],
982 (nextents
- idx
) * sizeof(xfs_bmbt_rec_t
));
983 memset(&ifp
->if_u1
.if_extents
[idx
], 0, byte_diff
);
986 /* Indirection array */
992 ASSERT(nextents
+ ext_diff
> XFS_LINEAR_EXTS
);
993 if (ifp
->if_flags
& XFS_IFEXTIREC
) {
994 erp
= xfs_iext_idx_to_irec(ifp
, &page_idx
, &erp_idx
, 1);
996 xfs_iext_irec_init(ifp
);
997 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
998 erp
= ifp
->if_u1
.if_ext_irec
;
1000 /* Extents fit in target extent page */
1001 if (erp
&& erp
->er_extcount
+ ext_diff
<= XFS_LINEAR_EXTS
) {
1002 if (page_idx
< erp
->er_extcount
) {
1003 memmove(&erp
->er_extbuf
[page_idx
+ ext_diff
],
1004 &erp
->er_extbuf
[page_idx
],
1005 (erp
->er_extcount
- page_idx
) *
1006 sizeof(xfs_bmbt_rec_t
));
1007 memset(&erp
->er_extbuf
[page_idx
], 0, byte_diff
);
1009 erp
->er_extcount
+= ext_diff
;
1010 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, ext_diff
);
1012 /* Insert a new extent page */
1014 xfs_iext_add_indirect_multi(ifp
,
1015 erp_idx
, page_idx
, ext_diff
);
1018 * If extent(s) are being appended to the last page in
1019 * the indirection array and the new extent(s) don't fit
1020 * in the page, then erp is NULL and erp_idx is set to
1021 * the next index needed in the indirection array.
1024 uint count
= ext_diff
;
1027 erp
= xfs_iext_irec_new(ifp
, erp_idx
);
1028 erp
->er_extcount
= min(count
, XFS_LINEAR_EXTS
);
1029 count
-= erp
->er_extcount
;
1035 ifp
->if_bytes
= new_size
;
1039 * This is called when incore extents are being added to the indirection
1040 * array and the new extents do not fit in the target extent list. The
1041 * erp_idx parameter contains the irec index for the target extent list
1042 * in the indirection array, and the idx parameter contains the extent
1043 * index within the list. The number of extents being added is stored
1044 * in the count parameter.
1046 * |-------| |-------|
1047 * | | | | idx - number of extents before idx
1049 * | | | | count - number of extents being inserted at idx
1050 * |-------| |-------|
1051 * | count | | nex2 | nex2 - number of extents after idx + count
1052 * |-------| |-------|
1055 xfs_iext_add_indirect_multi(
1056 xfs_ifork_t
*ifp
, /* inode fork pointer */
1057 int erp_idx
, /* target extent irec index */
1058 xfs_extnum_t idx
, /* index within target list */
1059 int count
) /* new extents being added */
1061 int byte_diff
; /* new bytes being added */
1062 xfs_ext_irec_t
*erp
; /* pointer to irec entry */
1063 xfs_extnum_t ext_diff
; /* number of extents to add */
1064 xfs_extnum_t ext_cnt
; /* new extents still needed */
1065 xfs_extnum_t nex2
; /* extents after idx + count */
1066 xfs_bmbt_rec_t
*nex2_ep
= NULL
; /* temp list for nex2 extents */
1067 int nlists
; /* number of irec's (lists) */
1069 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1070 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1071 nex2
= erp
->er_extcount
- idx
;
1072 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1075 * Save second part of target extent list
1076 * (all extents past */
1078 byte_diff
= nex2
* sizeof(xfs_bmbt_rec_t
);
1079 nex2_ep
= (xfs_bmbt_rec_t
*) kmem_alloc(byte_diff
, KM_NOFS
);
1080 memmove(nex2_ep
, &erp
->er_extbuf
[idx
], byte_diff
);
1081 erp
->er_extcount
-= nex2
;
1082 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, -nex2
);
1083 memset(&erp
->er_extbuf
[idx
], 0, byte_diff
);
1087 * Add the new extents to the end of the target
1088 * list, then allocate new irec record(s) and
1089 * extent buffer(s) as needed to store the rest
1090 * of the new extents.
1093 ext_diff
= MIN(ext_cnt
, (int)XFS_LINEAR_EXTS
- erp
->er_extcount
);
1095 erp
->er_extcount
+= ext_diff
;
1096 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, ext_diff
);
1097 ext_cnt
-= ext_diff
;
1101 erp
= xfs_iext_irec_new(ifp
, erp_idx
);
1102 ext_diff
= MIN(ext_cnt
, (int)XFS_LINEAR_EXTS
);
1103 erp
->er_extcount
= ext_diff
;
1104 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, ext_diff
);
1105 ext_cnt
-= ext_diff
;
1108 /* Add nex2 extents back to indirection array */
1110 xfs_extnum_t ext_avail
;
1113 byte_diff
= nex2
* sizeof(xfs_bmbt_rec_t
);
1114 ext_avail
= XFS_LINEAR_EXTS
- erp
->er_extcount
;
1117 * If nex2 extents fit in the current page, append
1118 * nex2_ep after the new extents.
1120 if (nex2
<= ext_avail
) {
1121 i
= erp
->er_extcount
;
1124 * Otherwise, check if space is available in the
1127 else if ((erp_idx
< nlists
- 1) &&
1128 (nex2
<= (ext_avail
= XFS_LINEAR_EXTS
-
1129 ifp
->if_u1
.if_ext_irec
[erp_idx
+1].er_extcount
))) {
1132 /* Create a hole for nex2 extents */
1133 memmove(&erp
->er_extbuf
[nex2
], erp
->er_extbuf
,
1134 erp
->er_extcount
* sizeof(xfs_bmbt_rec_t
));
1137 * Final choice, create a new extent page for
1142 erp
= xfs_iext_irec_new(ifp
, erp_idx
);
1144 memmove(&erp
->er_extbuf
[i
], nex2_ep
, byte_diff
);
1146 erp
->er_extcount
+= nex2
;
1147 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, nex2
);
1152 * This is called when the amount of space required for incore file
1153 * extents needs to be decreased. The ext_diff parameter stores the
1154 * number of extents to be removed and the idx parameter contains
1155 * the extent index where the extents will be removed from.
1157 * If the amount of space needed has decreased below the linear
1158 * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
1159 * extent array. Otherwise, use kmem_realloc() to adjust the
1160 * size to what is needed.
1164 xfs_inode_t
*ip
, /* incore inode pointer */
1165 xfs_extnum_t idx
, /* index to begin removing exts */
1166 int ext_diff
, /* number of extents to remove */
1167 int state
) /* type of extent conversion */
1169 xfs_ifork_t
*ifp
= (state
& BMAP_ATTRFORK
) ? ip
->i_afp
: &ip
->i_df
;
1170 xfs_extnum_t nextents
; /* number of extents in file */
1171 int new_size
; /* size of extents after removal */
1173 trace_xfs_iext_remove(ip
, idx
, state
, _RET_IP_
);
1175 ASSERT(ext_diff
> 0);
1176 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1177 new_size
= (nextents
- ext_diff
) * sizeof(xfs_bmbt_rec_t
);
1179 if (new_size
== 0) {
1180 xfs_iext_destroy(ifp
);
1181 } else if (ifp
->if_flags
& XFS_IFEXTIREC
) {
1182 xfs_iext_remove_indirect(ifp
, idx
, ext_diff
);
1183 } else if (ifp
->if_real_bytes
) {
1184 xfs_iext_remove_direct(ifp
, idx
, ext_diff
);
1186 xfs_iext_remove_inline(ifp
, idx
, ext_diff
);
1188 ifp
->if_bytes
= new_size
;
1192 * This removes ext_diff extents from the inline buffer, beginning
1193 * at extent index idx.
1196 xfs_iext_remove_inline(
1197 xfs_ifork_t
*ifp
, /* inode fork pointer */
1198 xfs_extnum_t idx
, /* index to begin removing exts */
1199 int ext_diff
) /* number of extents to remove */
1201 int nextents
; /* number of extents in file */
1203 ASSERT(!(ifp
->if_flags
& XFS_IFEXTIREC
));
1204 ASSERT(idx
< XFS_INLINE_EXTS
);
1205 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1206 ASSERT(((nextents
- ext_diff
) > 0) &&
1207 (nextents
- ext_diff
) < XFS_INLINE_EXTS
);
1209 if (idx
+ ext_diff
< nextents
) {
1210 memmove(&ifp
->if_u2
.if_inline_ext
[idx
],
1211 &ifp
->if_u2
.if_inline_ext
[idx
+ ext_diff
],
1212 (nextents
- (idx
+ ext_diff
)) *
1213 sizeof(xfs_bmbt_rec_t
));
1214 memset(&ifp
->if_u2
.if_inline_ext
[nextents
- ext_diff
],
1215 0, ext_diff
* sizeof(xfs_bmbt_rec_t
));
1217 memset(&ifp
->if_u2
.if_inline_ext
[idx
], 0,
1218 ext_diff
* sizeof(xfs_bmbt_rec_t
));
1223 * This removes ext_diff extents from a linear (direct) extent list,
1224 * beginning at extent index idx. If the extents are being removed
1225 * from the end of the list (ie. truncate) then we just need to re-
1226 * allocate the list to remove the extra space. Otherwise, if the
1227 * extents are being removed from the middle of the existing extent
1228 * entries, then we first need to move the extent records beginning
1229 * at idx + ext_diff up in the list to overwrite the records being
1230 * removed, then remove the extra space via kmem_realloc.
1233 xfs_iext_remove_direct(
1234 xfs_ifork_t
*ifp
, /* inode fork pointer */
1235 xfs_extnum_t idx
, /* index to begin removing exts */
1236 int ext_diff
) /* number of extents to remove */
1238 xfs_extnum_t nextents
; /* number of extents in file */
1239 int new_size
; /* size of extents after removal */
1241 ASSERT(!(ifp
->if_flags
& XFS_IFEXTIREC
));
1242 new_size
= ifp
->if_bytes
-
1243 (ext_diff
* sizeof(xfs_bmbt_rec_t
));
1244 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1246 if (new_size
== 0) {
1247 xfs_iext_destroy(ifp
);
1250 /* Move extents up in the list (if needed) */
1251 if (idx
+ ext_diff
< nextents
) {
1252 memmove(&ifp
->if_u1
.if_extents
[idx
],
1253 &ifp
->if_u1
.if_extents
[idx
+ ext_diff
],
1254 (nextents
- (idx
+ ext_diff
)) *
1255 sizeof(xfs_bmbt_rec_t
));
1257 memset(&ifp
->if_u1
.if_extents
[nextents
- ext_diff
],
1258 0, ext_diff
* sizeof(xfs_bmbt_rec_t
));
1260 * Reallocate the direct extent list. If the extents
1261 * will fit inside the inode then xfs_iext_realloc_direct
1262 * will switch from direct to inline extent allocation
1265 xfs_iext_realloc_direct(ifp
, new_size
);
1266 ifp
->if_bytes
= new_size
;
1270 * This is called when incore extents are being removed from the
1271 * indirection array and the extents being removed span multiple extent
1272 * buffers. The idx parameter contains the file extent index where we
1273 * want to begin removing extents, and the count parameter contains
1274 * how many extents need to be removed.
1276 * |-------| |-------|
1277 * | nex1 | | | nex1 - number of extents before idx
1278 * |-------| | count |
1279 * | | | | count - number of extents being removed at idx
1280 * | count | |-------|
1281 * | | | nex2 | nex2 - number of extents after idx + count
1282 * |-------| |-------|
1285 xfs_iext_remove_indirect(
1286 xfs_ifork_t
*ifp
, /* inode fork pointer */
1287 xfs_extnum_t idx
, /* index to begin removing extents */
1288 int count
) /* number of extents to remove */
1290 xfs_ext_irec_t
*erp
; /* indirection array pointer */
1291 int erp_idx
= 0; /* indirection array index */
1292 xfs_extnum_t ext_cnt
; /* extents left to remove */
1293 xfs_extnum_t ext_diff
; /* extents to remove in current list */
1294 xfs_extnum_t nex1
; /* number of extents before idx */
1295 xfs_extnum_t nex2
; /* extents after idx + count */
1296 int page_idx
= idx
; /* index in target extent list */
1298 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1299 erp
= xfs_iext_idx_to_irec(ifp
, &page_idx
, &erp_idx
, 0);
1300 ASSERT(erp
!= NULL
);
1304 nex2
= MAX((erp
->er_extcount
- (nex1
+ ext_cnt
)), 0);
1305 ext_diff
= MIN(ext_cnt
, (erp
->er_extcount
- nex1
));
1307 * Check for deletion of entire list;
1308 * xfs_iext_irec_remove() updates extent offsets.
1310 if (ext_diff
== erp
->er_extcount
) {
1311 xfs_iext_irec_remove(ifp
, erp_idx
);
1312 ext_cnt
-= ext_diff
;
1315 ASSERT(erp_idx
< ifp
->if_real_bytes
/
1317 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1324 /* Move extents up (if needed) */
1326 memmove(&erp
->er_extbuf
[nex1
],
1327 &erp
->er_extbuf
[nex1
+ ext_diff
],
1328 nex2
* sizeof(xfs_bmbt_rec_t
));
1330 /* Zero out rest of page */
1331 memset(&erp
->er_extbuf
[nex1
+ nex2
], 0, (XFS_IEXT_BUFSZ
-
1332 ((nex1
+ nex2
) * sizeof(xfs_bmbt_rec_t
))));
1333 /* Update remaining counters */
1334 erp
->er_extcount
-= ext_diff
;
1335 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, -ext_diff
);
1336 ext_cnt
-= ext_diff
;
1341 ifp
->if_bytes
-= count
* sizeof(xfs_bmbt_rec_t
);
1342 xfs_iext_irec_compact(ifp
);
1346 * Create, destroy, or resize a linear (direct) block of extents.
1349 xfs_iext_realloc_direct(
1350 xfs_ifork_t
*ifp
, /* inode fork pointer */
1351 int new_size
) /* new size of extents after adding */
1353 int rnew_size
; /* real new size of extents */
1355 rnew_size
= new_size
;
1357 ASSERT(!(ifp
->if_flags
& XFS_IFEXTIREC
) ||
1358 ((new_size
>= 0) && (new_size
<= XFS_IEXT_BUFSZ
) &&
1359 (new_size
!= ifp
->if_real_bytes
)));
1361 /* Free extent records */
1362 if (new_size
== 0) {
1363 xfs_iext_destroy(ifp
);
1365 /* Resize direct extent list and zero any new bytes */
1366 else if (ifp
->if_real_bytes
) {
1367 /* Check if extents will fit inside the inode */
1368 if (new_size
<= XFS_INLINE_EXTS
* sizeof(xfs_bmbt_rec_t
)) {
1369 xfs_iext_direct_to_inline(ifp
, new_size
/
1370 (uint
)sizeof(xfs_bmbt_rec_t
));
1371 ifp
->if_bytes
= new_size
;
1374 if (!is_power_of_2(new_size
)){
1375 rnew_size
= roundup_pow_of_two(new_size
);
1377 if (rnew_size
!= ifp
->if_real_bytes
) {
1378 ifp
->if_u1
.if_extents
=
1379 kmem_realloc(ifp
->if_u1
.if_extents
,
1381 ifp
->if_real_bytes
, KM_NOFS
);
1383 if (rnew_size
> ifp
->if_real_bytes
) {
1384 memset(&ifp
->if_u1
.if_extents
[ifp
->if_bytes
/
1385 (uint
)sizeof(xfs_bmbt_rec_t
)], 0,
1386 rnew_size
- ifp
->if_real_bytes
);
1389 /* Switch from the inline extent buffer to a direct extent list */
1391 if (!is_power_of_2(new_size
)) {
1392 rnew_size
= roundup_pow_of_two(new_size
);
1394 xfs_iext_inline_to_direct(ifp
, rnew_size
);
1396 ifp
->if_real_bytes
= rnew_size
;
1397 ifp
->if_bytes
= new_size
;
1401 * Switch from linear (direct) extent records to inline buffer.
1404 xfs_iext_direct_to_inline(
1405 xfs_ifork_t
*ifp
, /* inode fork pointer */
1406 xfs_extnum_t nextents
) /* number of extents in file */
1408 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1409 ASSERT(nextents
<= XFS_INLINE_EXTS
);
1411 * The inline buffer was zeroed when we switched
1412 * from inline to direct extent allocation mode,
1413 * so we don't need to clear it here.
1415 memcpy(ifp
->if_u2
.if_inline_ext
, ifp
->if_u1
.if_extents
,
1416 nextents
* sizeof(xfs_bmbt_rec_t
));
1417 kmem_free(ifp
->if_u1
.if_extents
);
1418 ifp
->if_u1
.if_extents
= ifp
->if_u2
.if_inline_ext
;
1419 ifp
->if_real_bytes
= 0;
1423 * Switch from inline buffer to linear (direct) extent records.
1424 * new_size should already be rounded up to the next power of 2
1425 * by the caller (when appropriate), so use new_size as it is.
1426 * However, since new_size may be rounded up, we can't update
1427 * if_bytes here. It is the caller's responsibility to update
1428 * if_bytes upon return.
1431 xfs_iext_inline_to_direct(
1432 xfs_ifork_t
*ifp
, /* inode fork pointer */
1433 int new_size
) /* number of extents in file */
1435 ifp
->if_u1
.if_extents
= kmem_alloc(new_size
, KM_NOFS
);
1436 memset(ifp
->if_u1
.if_extents
, 0, new_size
);
1437 if (ifp
->if_bytes
) {
1438 memcpy(ifp
->if_u1
.if_extents
, ifp
->if_u2
.if_inline_ext
,
1440 memset(ifp
->if_u2
.if_inline_ext
, 0, XFS_INLINE_EXTS
*
1441 sizeof(xfs_bmbt_rec_t
));
1443 ifp
->if_real_bytes
= new_size
;
1447 * Resize an extent indirection array to new_size bytes.
1450 xfs_iext_realloc_indirect(
1451 xfs_ifork_t
*ifp
, /* inode fork pointer */
1452 int new_size
) /* new indirection array size */
1454 int nlists
; /* number of irec's (ex lists) */
1455 int size
; /* current indirection array size */
1457 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1458 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1459 size
= nlists
* sizeof(xfs_ext_irec_t
);
1460 ASSERT(ifp
->if_real_bytes
);
1461 ASSERT((new_size
>= 0) && (new_size
!= size
));
1462 if (new_size
== 0) {
1463 xfs_iext_destroy(ifp
);
1465 ifp
->if_u1
.if_ext_irec
= (xfs_ext_irec_t
*)
1466 kmem_realloc(ifp
->if_u1
.if_ext_irec
,
1467 new_size
, size
, KM_NOFS
);
1472 * Switch from indirection array to linear (direct) extent allocations.
1475 xfs_iext_indirect_to_direct(
1476 xfs_ifork_t
*ifp
) /* inode fork pointer */
1478 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1479 xfs_extnum_t nextents
; /* number of extents in file */
1480 int size
; /* size of file extents */
1482 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1483 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1484 ASSERT(nextents
<= XFS_LINEAR_EXTS
);
1485 size
= nextents
* sizeof(xfs_bmbt_rec_t
);
1487 xfs_iext_irec_compact_pages(ifp
);
1488 ASSERT(ifp
->if_real_bytes
== XFS_IEXT_BUFSZ
);
1490 ep
= ifp
->if_u1
.if_ext_irec
->er_extbuf
;
1491 kmem_free(ifp
->if_u1
.if_ext_irec
);
1492 ifp
->if_flags
&= ~XFS_IFEXTIREC
;
1493 ifp
->if_u1
.if_extents
= ep
;
1494 ifp
->if_bytes
= size
;
1495 if (nextents
< XFS_LINEAR_EXTS
) {
1496 xfs_iext_realloc_direct(ifp
, size
);
1501 * Free incore file extents.
1505 xfs_ifork_t
*ifp
) /* inode fork pointer */
1507 if (ifp
->if_flags
& XFS_IFEXTIREC
) {
1511 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1512 for (erp_idx
= nlists
- 1; erp_idx
>= 0 ; erp_idx
--) {
1513 xfs_iext_irec_remove(ifp
, erp_idx
);
1515 ifp
->if_flags
&= ~XFS_IFEXTIREC
;
1516 } else if (ifp
->if_real_bytes
) {
1517 kmem_free(ifp
->if_u1
.if_extents
);
1518 } else if (ifp
->if_bytes
) {
1519 memset(ifp
->if_u2
.if_inline_ext
, 0, XFS_INLINE_EXTS
*
1520 sizeof(xfs_bmbt_rec_t
));
1522 ifp
->if_u1
.if_extents
= NULL
;
1523 ifp
->if_real_bytes
= 0;
1528 * Return a pointer to the extent record for file system block bno.
1530 xfs_bmbt_rec_host_t
* /* pointer to found extent record */
1531 xfs_iext_bno_to_ext(
1532 xfs_ifork_t
*ifp
, /* inode fork pointer */
1533 xfs_fileoff_t bno
, /* block number to search for */
1534 xfs_extnum_t
*idxp
) /* index of target extent */
1536 xfs_bmbt_rec_host_t
*base
; /* pointer to first extent */
1537 xfs_filblks_t blockcount
= 0; /* number of blocks in extent */
1538 xfs_bmbt_rec_host_t
*ep
= NULL
; /* pointer to target extent */
1539 xfs_ext_irec_t
*erp
= NULL
; /* indirection array pointer */
1540 int high
; /* upper boundary in search */
1541 xfs_extnum_t idx
= 0; /* index of target extent */
1542 int low
; /* lower boundary in search */
1543 xfs_extnum_t nextents
; /* number of file extents */
1544 xfs_fileoff_t startoff
= 0; /* start offset of extent */
1546 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1547 if (nextents
== 0) {
1552 if (ifp
->if_flags
& XFS_IFEXTIREC
) {
1553 /* Find target extent list */
1555 erp
= xfs_iext_bno_to_irec(ifp
, bno
, &erp_idx
);
1556 base
= erp
->er_extbuf
;
1557 high
= erp
->er_extcount
- 1;
1559 base
= ifp
->if_u1
.if_extents
;
1560 high
= nextents
- 1;
1562 /* Binary search extent records */
1563 while (low
<= high
) {
1564 idx
= (low
+ high
) >> 1;
1566 startoff
= xfs_bmbt_get_startoff(ep
);
1567 blockcount
= xfs_bmbt_get_blockcount(ep
);
1568 if (bno
< startoff
) {
1570 } else if (bno
>= startoff
+ blockcount
) {
1573 /* Convert back to file-based extent index */
1574 if (ifp
->if_flags
& XFS_IFEXTIREC
) {
1575 idx
+= erp
->er_extoff
;
1581 /* Convert back to file-based extent index */
1582 if (ifp
->if_flags
& XFS_IFEXTIREC
) {
1583 idx
+= erp
->er_extoff
;
1585 if (bno
>= startoff
+ blockcount
) {
1586 if (++idx
== nextents
) {
1589 ep
= xfs_iext_get_ext(ifp
, idx
);
1597 * Return a pointer to the indirection array entry containing the
1598 * extent record for filesystem block bno. Store the index of the
1599 * target irec in *erp_idxp.
1601 xfs_ext_irec_t
* /* pointer to found extent record */
1602 xfs_iext_bno_to_irec(
1603 xfs_ifork_t
*ifp
, /* inode fork pointer */
1604 xfs_fileoff_t bno
, /* block number to search for */
1605 int *erp_idxp
) /* irec index of target ext list */
1607 xfs_ext_irec_t
*erp
= NULL
; /* indirection array pointer */
1608 xfs_ext_irec_t
*erp_next
; /* next indirection array entry */
1609 int erp_idx
; /* indirection array index */
1610 int nlists
; /* number of extent irec's (lists) */
1611 int high
; /* binary search upper limit */
1612 int low
; /* binary search lower limit */
1614 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1615 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1619 while (low
<= high
) {
1620 erp_idx
= (low
+ high
) >> 1;
1621 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1622 erp_next
= erp_idx
< nlists
- 1 ? erp
+ 1 : NULL
;
1623 if (bno
< xfs_bmbt_get_startoff(erp
->er_extbuf
)) {
1625 } else if (erp_next
&& bno
>=
1626 xfs_bmbt_get_startoff(erp_next
->er_extbuf
)) {
1632 *erp_idxp
= erp_idx
;
1637 * Return a pointer to the indirection array entry containing the
1638 * extent record at file extent index *idxp. Store the index of the
1639 * target irec in *erp_idxp and store the page index of the target
1640 * extent record in *idxp.
1643 xfs_iext_idx_to_irec(
1644 xfs_ifork_t
*ifp
, /* inode fork pointer */
1645 xfs_extnum_t
*idxp
, /* extent index (file -> page) */
1646 int *erp_idxp
, /* pointer to target irec */
1647 int realloc
) /* new bytes were just added */
1649 xfs_ext_irec_t
*prev
; /* pointer to previous irec */
1650 xfs_ext_irec_t
*erp
= NULL
; /* pointer to current irec */
1651 int erp_idx
; /* indirection array index */
1652 int nlists
; /* number of irec's (ex lists) */
1653 int high
; /* binary search upper limit */
1654 int low
; /* binary search lower limit */
1655 xfs_extnum_t page_idx
= *idxp
; /* extent index in target list */
1657 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1658 ASSERT(page_idx
>= 0);
1659 ASSERT(page_idx
<= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
));
1660 ASSERT(page_idx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
) || realloc
);
1662 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1667 /* Binary search extent irec's */
1668 while (low
<= high
) {
1669 erp_idx
= (low
+ high
) >> 1;
1670 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1671 prev
= erp_idx
> 0 ? erp
- 1 : NULL
;
1672 if (page_idx
< erp
->er_extoff
|| (page_idx
== erp
->er_extoff
&&
1673 realloc
&& prev
&& prev
->er_extcount
< XFS_LINEAR_EXTS
)) {
1675 } else if (page_idx
> erp
->er_extoff
+ erp
->er_extcount
||
1676 (page_idx
== erp
->er_extoff
+ erp
->er_extcount
&&
1679 } else if (page_idx
== erp
->er_extoff
+ erp
->er_extcount
&&
1680 erp
->er_extcount
== XFS_LINEAR_EXTS
) {
1684 erp
= erp_idx
< nlists
? erp
+ 1 : NULL
;
1687 page_idx
-= erp
->er_extoff
;
1692 *erp_idxp
= erp_idx
;
1697 * Allocate and initialize an indirection array once the space needed
1698 * for incore extents increases above XFS_IEXT_BUFSZ.
1702 xfs_ifork_t
*ifp
) /* inode fork pointer */
1704 xfs_ext_irec_t
*erp
; /* indirection array pointer */
1705 xfs_extnum_t nextents
; /* number of extents in file */
1707 ASSERT(!(ifp
->if_flags
& XFS_IFEXTIREC
));
1708 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1709 ASSERT(nextents
<= XFS_LINEAR_EXTS
);
1711 erp
= kmem_alloc(sizeof(xfs_ext_irec_t
), KM_NOFS
);
1713 if (nextents
== 0) {
1714 ifp
->if_u1
.if_extents
= kmem_alloc(XFS_IEXT_BUFSZ
, KM_NOFS
);
1715 } else if (!ifp
->if_real_bytes
) {
1716 xfs_iext_inline_to_direct(ifp
, XFS_IEXT_BUFSZ
);
1717 } else if (ifp
->if_real_bytes
< XFS_IEXT_BUFSZ
) {
1718 xfs_iext_realloc_direct(ifp
, XFS_IEXT_BUFSZ
);
1720 erp
->er_extbuf
= ifp
->if_u1
.if_extents
;
1721 erp
->er_extcount
= nextents
;
1724 ifp
->if_flags
|= XFS_IFEXTIREC
;
1725 ifp
->if_real_bytes
= XFS_IEXT_BUFSZ
;
1726 ifp
->if_bytes
= nextents
* sizeof(xfs_bmbt_rec_t
);
1727 ifp
->if_u1
.if_ext_irec
= erp
;
1733 * Allocate and initialize a new entry in the indirection array.
1737 xfs_ifork_t
*ifp
, /* inode fork pointer */
1738 int erp_idx
) /* index for new irec */
1740 xfs_ext_irec_t
*erp
; /* indirection array pointer */
1741 int i
; /* loop counter */
1742 int nlists
; /* number of irec's (ex lists) */
1744 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1745 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1747 /* Resize indirection array */
1748 xfs_iext_realloc_indirect(ifp
, ++nlists
*
1749 sizeof(xfs_ext_irec_t
));
1751 * Move records down in the array so the
1752 * new page can use erp_idx.
1754 erp
= ifp
->if_u1
.if_ext_irec
;
1755 for (i
= nlists
- 1; i
> erp_idx
; i
--) {
1756 memmove(&erp
[i
], &erp
[i
-1], sizeof(xfs_ext_irec_t
));
1758 ASSERT(i
== erp_idx
);
1760 /* Initialize new extent record */
1761 erp
= ifp
->if_u1
.if_ext_irec
;
1762 erp
[erp_idx
].er_extbuf
= kmem_alloc(XFS_IEXT_BUFSZ
, KM_NOFS
);
1763 ifp
->if_real_bytes
= nlists
* XFS_IEXT_BUFSZ
;
1764 memset(erp
[erp_idx
].er_extbuf
, 0, XFS_IEXT_BUFSZ
);
1765 erp
[erp_idx
].er_extcount
= 0;
1766 erp
[erp_idx
].er_extoff
= erp_idx
> 0 ?
1767 erp
[erp_idx
-1].er_extoff
+ erp
[erp_idx
-1].er_extcount
: 0;
1768 return (&erp
[erp_idx
]);
1772 * Remove a record from the indirection array.
1775 xfs_iext_irec_remove(
1776 xfs_ifork_t
*ifp
, /* inode fork pointer */
1777 int erp_idx
) /* irec index to remove */
1779 xfs_ext_irec_t
*erp
; /* indirection array pointer */
1780 int i
; /* loop counter */
1781 int nlists
; /* number of irec's (ex lists) */
1783 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1784 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1785 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1786 if (erp
->er_extbuf
) {
1787 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1,
1789 kmem_free(erp
->er_extbuf
);
1791 /* Compact extent records */
1792 erp
= ifp
->if_u1
.if_ext_irec
;
1793 for (i
= erp_idx
; i
< nlists
- 1; i
++) {
1794 memmove(&erp
[i
], &erp
[i
+1], sizeof(xfs_ext_irec_t
));
1797 * Manually free the last extent record from the indirection
1798 * array. A call to xfs_iext_realloc_indirect() with a size
1799 * of zero would result in a call to xfs_iext_destroy() which
1800 * would in turn call this function again, creating a nasty
1804 xfs_iext_realloc_indirect(ifp
,
1805 nlists
* sizeof(xfs_ext_irec_t
));
1807 kmem_free(ifp
->if_u1
.if_ext_irec
);
1809 ifp
->if_real_bytes
= nlists
* XFS_IEXT_BUFSZ
;
1813 * This is called to clean up large amounts of unused memory allocated
1814 * by the indirection array. Before compacting anything though, verify
1815 * that the indirection array is still needed and switch back to the
1816 * linear extent list (or even the inline buffer) if possible. The
1817 * compaction policy is as follows:
1819 * Full Compaction: Extents fit into a single page (or inline buffer)
1820 * Partial Compaction: Extents occupy less than 50% of allocated space
1821 * No Compaction: Extents occupy at least 50% of allocated space
1824 xfs_iext_irec_compact(
1825 xfs_ifork_t
*ifp
) /* inode fork pointer */
1827 xfs_extnum_t nextents
; /* number of extents in file */
1828 int nlists
; /* number of irec's (ex lists) */
1830 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1831 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1832 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1834 if (nextents
== 0) {
1835 xfs_iext_destroy(ifp
);
1836 } else if (nextents
<= XFS_INLINE_EXTS
) {
1837 xfs_iext_indirect_to_direct(ifp
);
1838 xfs_iext_direct_to_inline(ifp
, nextents
);
1839 } else if (nextents
<= XFS_LINEAR_EXTS
) {
1840 xfs_iext_indirect_to_direct(ifp
);
1841 } else if (nextents
< (nlists
* XFS_LINEAR_EXTS
) >> 1) {
1842 xfs_iext_irec_compact_pages(ifp
);
1847 * Combine extents from neighboring extent pages.
1850 xfs_iext_irec_compact_pages(
1851 xfs_ifork_t
*ifp
) /* inode fork pointer */
1853 xfs_ext_irec_t
*erp
, *erp_next
;/* pointers to irec entries */
1854 int erp_idx
= 0; /* indirection array index */
1855 int nlists
; /* number of irec's (ex lists) */
1857 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1858 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1859 while (erp_idx
< nlists
- 1) {
1860 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1862 if (erp_next
->er_extcount
<=
1863 (XFS_LINEAR_EXTS
- erp
->er_extcount
)) {
1864 memcpy(&erp
->er_extbuf
[erp
->er_extcount
],
1865 erp_next
->er_extbuf
, erp_next
->er_extcount
*
1866 sizeof(xfs_bmbt_rec_t
));
1867 erp
->er_extcount
+= erp_next
->er_extcount
;
1869 * Free page before removing extent record
1870 * so er_extoffs don't get modified in
1871 * xfs_iext_irec_remove.
1873 kmem_free(erp_next
->er_extbuf
);
1874 erp_next
->er_extbuf
= NULL
;
1875 xfs_iext_irec_remove(ifp
, erp_idx
+ 1);
1876 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1884 * This is called to update the er_extoff field in the indirection
1885 * array when extents have been added or removed from one of the
1886 * extent lists. erp_idx contains the irec index to begin updating
1887 * at and ext_diff contains the number of extents that were added
1891 xfs_iext_irec_update_extoffs(
1892 xfs_ifork_t
*ifp
, /* inode fork pointer */
1893 int erp_idx
, /* irec index to update */
1894 int ext_diff
) /* number of new extents */
1896 int i
; /* loop counter */
1897 int nlists
; /* number of irec's (ex lists */
1899 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1900 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1901 for (i
= erp_idx
; i
< nlists
; i
++) {
1902 ifp
->if_u1
.if_ext_irec
[i
].er_extoff
+= ext_diff
;