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
20 kmem_zone_t
*xfs_ifork_zone
;
22 STATIC
int xfs_iformat_local(xfs_inode_t
*, xfs_dinode_t
*, int, int);
23 STATIC
int xfs_iformat_extents(xfs_inode_t
*, xfs_dinode_t
*, int);
24 STATIC
int xfs_iformat_btree(xfs_inode_t
*, xfs_dinode_t
*, int);
28 * Make sure that the extents in the given memory buffer
38 xfs_bmbt_rec_host_t rec
;
41 for (i
= 0; i
< nrecs
; i
++) {
42 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, i
);
43 rec
.l0
= get_unaligned(&ep
->l0
);
44 rec
.l1
= get_unaligned(&ep
->l1
);
45 xfs_bmbt_get_all(&rec
, &irec
);
46 if (fmt
== XFS_EXTFMT_NOSTATE
)
47 ASSERT(irec
.br_state
== XFS_EXT_NORM
);
51 #define xfs_validate_extents(ifp, nrecs, fmt)
56 * Move inode type and inode format specific information from the
57 * on-disk inode to the in-core inode. For fifos, devs, and sockets
58 * this means set if_rdev to the proper value. For files, directories,
59 * and symlinks this means to bring in the in-line data or extent
60 * pointers. For a file in B-tree format, only the root is immediately
61 * brought in-core. The rest will be in-lined in if_extents when it
62 * is first referenced (see xfs_iread_extents()).
69 xfs_attr_shortform_t
*atp
;
74 if (unlikely(be32_to_cpu(dip
->di_nextents
) +
75 be16_to_cpu(dip
->di_anextents
) >
76 be64_to_cpu(dip
->di_nblocks
))) {
78 "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.",
79 (unsigned long long)ip
->i_ino
,
80 (int)(be32_to_cpu(dip
->di_nextents
) +
81 be16_to_cpu(dip
->di_anextents
)),
83 be64_to_cpu(dip
->di_nblocks
));
84 XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW
,
86 return XFS_ERROR(EFSCORRUPTED
);
89 if (unlikely(dip
->di_forkoff
> ip
->i_mount
->m_sb
.sb_inodesize
)) {
90 xfs_warn(ip
->i_mount
, "corrupt dinode %Lu, forkoff = 0x%x.",
91 (unsigned long long)ip
->i_ino
,
93 XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW
,
95 return XFS_ERROR(EFSCORRUPTED
);
98 if (unlikely((ip
->i_d
.di_flags
& XFS_DIFLAG_REALTIME
) &&
99 !ip
->i_mount
->m_rtdev_targp
)) {
100 xfs_warn(ip
->i_mount
,
101 "corrupt dinode %Lu, has realtime flag set.",
103 XFS_CORRUPTION_ERROR("xfs_iformat(realtime)",
104 XFS_ERRLEVEL_LOW
, ip
->i_mount
, dip
);
105 return XFS_ERROR(EFSCORRUPTED
);
108 switch (ip
->i_d
.di_mode
& S_IFMT
) {
113 if (unlikely(dip
->di_format
!= XFS_DINODE_FMT_DEV
)) {
114 XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW
,
116 return XFS_ERROR(EFSCORRUPTED
);
119 ip
->i_df
.if_u2
.if_rdev
= xfs_dinode_get_rdev(dip
);
125 switch (dip
->di_format
) {
126 case XFS_DINODE_FMT_LOCAL
:
128 * no local regular files yet
130 if (unlikely(S_ISREG(be16_to_cpu(dip
->di_mode
)))) {
131 xfs_warn(ip
->i_mount
,
132 "corrupt inode %Lu (local format for regular file).",
133 (unsigned long long) ip
->i_ino
);
134 XFS_CORRUPTION_ERROR("xfs_iformat(4)",
137 return XFS_ERROR(EFSCORRUPTED
);
140 di_size
= be64_to_cpu(dip
->di_size
);
141 if (unlikely(di_size
< 0 ||
142 di_size
> XFS_DFORK_DSIZE(dip
, ip
->i_mount
))) {
143 xfs_warn(ip
->i_mount
,
144 "corrupt inode %Lu (bad size %Ld for local inode).",
145 (unsigned long long) ip
->i_ino
,
146 (long long) di_size
);
147 XFS_CORRUPTION_ERROR("xfs_iformat(5)",
150 return XFS_ERROR(EFSCORRUPTED
);
154 error
= xfs_iformat_local(ip
, dip
, XFS_DATA_FORK
, size
);
156 case XFS_DINODE_FMT_EXTENTS
:
157 error
= xfs_iformat_extents(ip
, dip
, XFS_DATA_FORK
);
159 case XFS_DINODE_FMT_BTREE
:
160 error
= xfs_iformat_btree(ip
, dip
, XFS_DATA_FORK
);
163 XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW
,
165 return XFS_ERROR(EFSCORRUPTED
);
170 XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW
, ip
->i_mount
);
171 return XFS_ERROR(EFSCORRUPTED
);
176 if (!XFS_DFORK_Q(dip
))
179 ASSERT(ip
->i_afp
== NULL
);
180 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
| KM_NOFS
);
182 switch (dip
->di_aformat
) {
183 case XFS_DINODE_FMT_LOCAL
:
184 atp
= (xfs_attr_shortform_t
*)XFS_DFORK_APTR(dip
);
185 size
= be16_to_cpu(atp
->hdr
.totsize
);
187 if (unlikely(size
< sizeof(struct xfs_attr_sf_hdr
))) {
188 xfs_warn(ip
->i_mount
,
189 "corrupt inode %Lu (bad attr fork size %Ld).",
190 (unsigned long long) ip
->i_ino
,
192 XFS_CORRUPTION_ERROR("xfs_iformat(8)",
195 return XFS_ERROR(EFSCORRUPTED
);
198 error
= xfs_iformat_local(ip
, dip
, XFS_ATTR_FORK
, size
);
200 case XFS_DINODE_FMT_EXTENTS
:
201 error
= xfs_iformat_extents(ip
, dip
, XFS_ATTR_FORK
);
203 case XFS_DINODE_FMT_BTREE
:
204 error
= xfs_iformat_btree(ip
, dip
, XFS_ATTR_FORK
);
207 error
= XFS_ERROR(EFSCORRUPTED
);
211 kmem_zone_free(xfs_ifork_zone
, ip
->i_afp
);
213 xfs_idestroy_fork(ip
, XFS_DATA_FORK
);
219 * The file is in-lined in the on-disk inode.
220 * If it fits into if_inline_data, then copy
221 * it there, otherwise allocate a buffer for it
222 * and copy the data there. Either way, set
223 * if_data to point at the data.
224 * If we allocate a buffer for the data, make
225 * sure that its size is a multiple of 4 and
226 * record the real size in i_real_bytes.
239 * If the size is unreasonable, then something
240 * is wrong and we just bail out rather than crash in
241 * kmem_alloc() or memcpy() below.
243 if (unlikely(size
> XFS_DFORK_SIZE(dip
, ip
->i_mount
, whichfork
))) {
244 xfs_warn(ip
->i_mount
,
245 "corrupt inode %Lu (bad size %d for local fork, size = %d).",
246 (unsigned long long) ip
->i_ino
, size
,
247 XFS_DFORK_SIZE(dip
, ip
->i_mount
, whichfork
));
248 XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW
,
250 return XFS_ERROR(EFSCORRUPTED
);
252 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
255 ifp
->if_u1
.if_data
= NULL
;
256 else if (size
<= sizeof(ifp
->if_u2
.if_inline_data
))
257 ifp
->if_u1
.if_data
= ifp
->if_u2
.if_inline_data
;
259 real_size
= roundup(size
, 4);
260 ifp
->if_u1
.if_data
= kmem_alloc(real_size
, KM_SLEEP
| KM_NOFS
);
262 ifp
->if_bytes
= size
;
263 ifp
->if_real_bytes
= real_size
;
265 memcpy(ifp
->if_u1
.if_data
, XFS_DFORK_PTR(dip
, whichfork
), size
);
266 ifp
->if_flags
&= ~XFS_IFEXTENTS
;
267 ifp
->if_flags
|= XFS_IFINLINE
;
272 * The file consists of a set of extents all
273 * of which fit into the on-disk inode.
274 * If there are few enough extents to fit into
275 * the if_inline_ext, then copy them there.
276 * Otherwise allocate a buffer for them and copy
277 * them into it. Either way, set if_extents
278 * to point at the extents.
292 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
293 nex
= XFS_DFORK_NEXTENTS(dip
, whichfork
);
294 size
= nex
* (uint
)sizeof(xfs_bmbt_rec_t
);
297 * If the number of extents is unreasonable, then something
298 * is wrong and we just bail out rather than crash in
299 * kmem_alloc() or memcpy() below.
301 if (unlikely(size
< 0 || size
> XFS_DFORK_SIZE(dip
, ip
->i_mount
, whichfork
))) {
302 xfs_warn(ip
->i_mount
, "corrupt inode %Lu ((a)extents = %d).",
303 (unsigned long long) ip
->i_ino
, nex
);
304 XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW
,
306 return XFS_ERROR(EFSCORRUPTED
);
309 ifp
->if_real_bytes
= 0;
311 ifp
->if_u1
.if_extents
= NULL
;
312 else if (nex
<= XFS_INLINE_EXTS
)
313 ifp
->if_u1
.if_extents
= ifp
->if_u2
.if_inline_ext
;
315 xfs_iext_add(ifp
, 0, nex
);
317 ifp
->if_bytes
= size
;
319 dp
= (xfs_bmbt_rec_t
*) XFS_DFORK_PTR(dip
, whichfork
);
320 xfs_validate_extents(ifp
, nex
, XFS_EXTFMT_INODE(ip
));
321 for (i
= 0; i
< nex
; i
++, dp
++) {
322 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, i
);
323 ep
->l0
= get_unaligned_be64(&dp
->l0
);
324 ep
->l1
= get_unaligned_be64(&dp
->l1
);
326 XFS_BMAP_TRACE_EXLIST(ip
, nex
, whichfork
);
327 if (whichfork
!= XFS_DATA_FORK
||
328 XFS_EXTFMT_INODE(ip
) == XFS_EXTFMT_NOSTATE
)
329 if (unlikely(xfs_check_nostate_extents(
331 XFS_ERROR_REPORT("xfs_iformat_extents(2)",
334 return XFS_ERROR(EFSCORRUPTED
);
337 ifp
->if_flags
|= XFS_IFEXTENTS
;
342 * The file has too many extents to fit into
343 * the inode, so they are in B-tree format.
344 * Allocate a buffer for the root of the B-tree
345 * and copy the root into it. The i_extents
346 * field will remain NULL until all of the
347 * extents are read in (when they are needed).
355 struct xfs_mount
*mp
= ip
->i_mount
;
356 xfs_bmdr_block_t
*dfp
;
362 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
363 dfp
= (xfs_bmdr_block_t
*)XFS_DFORK_PTR(dip
, whichfork
);
364 size
= XFS_BMAP_BROOT_SPACE(mp
, dfp
);
365 nrecs
= be16_to_cpu(dfp
->bb_numrecs
);
368 * blow out if -- fork has less extents than can fit in
369 * fork (fork shouldn't be a btree format), root btree
370 * block has more records than can fit into the fork,
371 * or the number of extents is greater than the number of
374 if (unlikely(XFS_IFORK_NEXTENTS(ip
, whichfork
) <=
375 XFS_IFORK_MAXEXT(ip
, whichfork
) ||
376 XFS_BMDR_SPACE_CALC(nrecs
) >
377 XFS_DFORK_SIZE(dip
, mp
, whichfork
) ||
378 XFS_IFORK_NEXTENTS(ip
, whichfork
) > ip
->i_d
.di_nblocks
)) {
379 xfs_warn(mp
, "corrupt inode %Lu (btree).",
380 (unsigned long long) ip
->i_ino
);
381 XFS_CORRUPTION_ERROR("xfs_iformat_btree", XFS_ERRLEVEL_LOW
,
383 return XFS_ERROR(EFSCORRUPTED
);
386 ifp
->if_broot_bytes
= size
;
387 ifp
->if_broot
= kmem_alloc(size
, KM_SLEEP
| KM_NOFS
);
388 ASSERT(ifp
->if_broot
!= NULL
);
390 * Copy and convert from the on-disk structure
391 * to the in-memory structure.
393 xfs_bmdr_to_bmbt(ip
, dfp
, XFS_DFORK_SIZE(dip
, ip
->i_mount
, whichfork
),
394 ifp
->if_broot
, size
);
395 ifp
->if_flags
&= ~XFS_IFEXTENTS
;
396 ifp
->if_flags
|= XFS_IFBROOT
;
402 * Read in extents from a btree-format inode.
403 * Allocate and fill in if_extents. Real work is done in xfs_bmap.c.
413 xfs_extnum_t nextents
;
415 if (unlikely(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
416 XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW
,
418 return XFS_ERROR(EFSCORRUPTED
);
420 nextents
= XFS_IFORK_NEXTENTS(ip
, whichfork
);
421 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
424 * We know that the size is valid (it's checked in iformat_btree)
426 ifp
->if_bytes
= ifp
->if_real_bytes
= 0;
427 ifp
->if_flags
|= XFS_IFEXTENTS
;
428 xfs_iext_add(ifp
, 0, nextents
);
429 error
= xfs_bmap_read_extents(tp
, ip
, whichfork
);
431 xfs_iext_destroy(ifp
);
432 ifp
->if_flags
&= ~XFS_IFEXTENTS
;
435 xfs_validate_extents(ifp
, nextents
, XFS_EXTFMT_INODE(ip
));
439 * Reallocate the space for if_broot based on the number of records
440 * being added or deleted as indicated in rec_diff. Move the records
441 * and pointers in if_broot to fit the new size. When shrinking this
442 * will eliminate holes between the records and pointers created by
443 * the caller. When growing this will create holes to be filled in
446 * The caller must not request to add more records than would fit in
447 * the on-disk inode root. If the if_broot is currently NULL, then
448 * if we are adding records, one will be allocated. The caller must also
449 * not request that the number of records go below zero, although
452 * ip -- the inode whose if_broot area is changing
453 * ext_diff -- the change in the number of records, positive or negative,
454 * requested for the if_broot array.
462 struct xfs_mount
*mp
= ip
->i_mount
;
465 struct xfs_btree_block
*new_broot
;
472 * Handle the degenerate case quietly.
478 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
481 * If there wasn't any memory allocated before, just
482 * allocate it now and get out.
484 if (ifp
->if_broot_bytes
== 0) {
485 new_size
= XFS_BMAP_BROOT_SPACE_CALC(mp
, rec_diff
);
486 ifp
->if_broot
= kmem_alloc(new_size
, KM_SLEEP
| KM_NOFS
);
487 ifp
->if_broot_bytes
= (int)new_size
;
492 * If there is already an existing if_broot, then we need
493 * to realloc() it and shift the pointers to their new
494 * location. The records don't change location because
495 * they are kept butted up against the btree block header.
497 cur_max
= xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0);
498 new_max
= cur_max
+ rec_diff
;
499 new_size
= XFS_BMAP_BROOT_SPACE_CALC(mp
, new_max
);
500 ifp
->if_broot
= kmem_realloc(ifp
->if_broot
, new_size
,
501 XFS_BMAP_BROOT_SPACE_CALC(mp
, cur_max
),
503 op
= (char *)XFS_BMAP_BROOT_PTR_ADDR(mp
, ifp
->if_broot
, 1,
504 ifp
->if_broot_bytes
);
505 np
= (char *)XFS_BMAP_BROOT_PTR_ADDR(mp
, ifp
->if_broot
, 1,
507 ifp
->if_broot_bytes
= (int)new_size
;
508 ASSERT(XFS_BMAP_BMDR_SPACE(ifp
->if_broot
) <=
509 XFS_IFORK_SIZE(ip
, whichfork
));
510 memmove(np
, op
, cur_max
* (uint
)sizeof(xfs_dfsbno_t
));
515 * rec_diff is less than 0. In this case, we are shrinking the
516 * if_broot buffer. It must already exist. If we go to zero
517 * records, just get rid of the root and clear the status bit.
519 ASSERT((ifp
->if_broot
!= NULL
) && (ifp
->if_broot_bytes
> 0));
520 cur_max
= xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0);
521 new_max
= cur_max
+ rec_diff
;
522 ASSERT(new_max
>= 0);
524 new_size
= XFS_BMAP_BROOT_SPACE_CALC(mp
, new_max
);
528 new_broot
= kmem_alloc(new_size
, KM_SLEEP
| KM_NOFS
);
530 * First copy over the btree block header.
532 memcpy(new_broot
, ifp
->if_broot
,
533 XFS_BMBT_BLOCK_LEN(ip
->i_mount
));
536 ifp
->if_flags
&= ~XFS_IFBROOT
;
540 * Only copy the records and pointers if there are any.
544 * First copy the records.
546 op
= (char *)XFS_BMBT_REC_ADDR(mp
, ifp
->if_broot
, 1);
547 np
= (char *)XFS_BMBT_REC_ADDR(mp
, new_broot
, 1);
548 memcpy(np
, op
, new_max
* (uint
)sizeof(xfs_bmbt_rec_t
));
551 * Then copy the pointers.
553 op
= (char *)XFS_BMAP_BROOT_PTR_ADDR(mp
, ifp
->if_broot
, 1,
554 ifp
->if_broot_bytes
);
555 np
= (char *)XFS_BMAP_BROOT_PTR_ADDR(mp
, new_broot
, 1,
557 memcpy(np
, op
, new_max
* (uint
)sizeof(xfs_dfsbno_t
));
559 kmem_free(ifp
->if_broot
);
560 ifp
->if_broot
= new_broot
;
561 ifp
->if_broot_bytes
= (int)new_size
;
563 ASSERT(XFS_BMAP_BMDR_SPACE(ifp
->if_broot
) <=
564 XFS_IFORK_SIZE(ip
, whichfork
));
570 * This is called when the amount of space needed for if_data
571 * is increased or decreased. The change in size is indicated by
572 * the number of bytes that need to be added or deleted in the
573 * byte_diff parameter.
575 * If the amount of space needed has decreased below the size of the
576 * inline buffer, then switch to using the inline buffer. Otherwise,
577 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
580 * ip -- the inode whose if_data area is changing
581 * byte_diff -- the change in the number of bytes, positive or negative,
582 * requested for the if_data array.
594 if (byte_diff
== 0) {
598 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
599 new_size
= (int)ifp
->if_bytes
+ byte_diff
;
600 ASSERT(new_size
>= 0);
603 if (ifp
->if_u1
.if_data
!= ifp
->if_u2
.if_inline_data
) {
604 kmem_free(ifp
->if_u1
.if_data
);
606 ifp
->if_u1
.if_data
= NULL
;
608 } else if (new_size
<= sizeof(ifp
->if_u2
.if_inline_data
)) {
610 * If the valid extents/data can fit in if_inline_ext/data,
611 * copy them from the malloc'd vector and free it.
613 if (ifp
->if_u1
.if_data
== NULL
) {
614 ifp
->if_u1
.if_data
= ifp
->if_u2
.if_inline_data
;
615 } else if (ifp
->if_u1
.if_data
!= ifp
->if_u2
.if_inline_data
) {
616 ASSERT(ifp
->if_real_bytes
!= 0);
617 memcpy(ifp
->if_u2
.if_inline_data
, ifp
->if_u1
.if_data
,
619 kmem_free(ifp
->if_u1
.if_data
);
620 ifp
->if_u1
.if_data
= ifp
->if_u2
.if_inline_data
;
625 * Stuck with malloc/realloc.
626 * For inline data, the underlying buffer must be
627 * a multiple of 4 bytes in size so that it can be
628 * logged and stay on word boundaries. We enforce
631 real_size
= roundup(new_size
, 4);
632 if (ifp
->if_u1
.if_data
== NULL
) {
633 ASSERT(ifp
->if_real_bytes
== 0);
634 ifp
->if_u1
.if_data
= kmem_alloc(real_size
,
636 } else if (ifp
->if_u1
.if_data
!= ifp
->if_u2
.if_inline_data
) {
638 * Only do the realloc if the underlying size
639 * is really changing.
641 if (ifp
->if_real_bytes
!= real_size
) {
643 kmem_realloc(ifp
->if_u1
.if_data
,
649 ASSERT(ifp
->if_real_bytes
== 0);
650 ifp
->if_u1
.if_data
= kmem_alloc(real_size
,
652 memcpy(ifp
->if_u1
.if_data
, ifp
->if_u2
.if_inline_data
,
656 ifp
->if_real_bytes
= real_size
;
657 ifp
->if_bytes
= new_size
;
658 ASSERT(ifp
->if_bytes
<= XFS_IFORK_SIZE(ip
, whichfork
));
668 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
669 if (ifp
->if_broot
!= NULL
) {
670 kmem_free(ifp
->if_broot
);
671 ifp
->if_broot
= NULL
;
675 * If the format is local, then we can't have an extents
676 * array so just look for an inline data array. If we're
677 * not local then we may or may not have an extents list,
678 * so check and free it up if we do.
680 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
681 if ((ifp
->if_u1
.if_data
!= ifp
->if_u2
.if_inline_data
) &&
682 (ifp
->if_u1
.if_data
!= NULL
)) {
683 ASSERT(ifp
->if_real_bytes
!= 0);
684 kmem_free(ifp
->if_u1
.if_data
);
685 ifp
->if_u1
.if_data
= NULL
;
686 ifp
->if_real_bytes
= 0;
688 } else if ((ifp
->if_flags
& XFS_IFEXTENTS
) &&
689 ((ifp
->if_flags
& XFS_IFEXTIREC
) ||
690 ((ifp
->if_u1
.if_extents
!= NULL
) &&
691 (ifp
->if_u1
.if_extents
!= ifp
->if_u2
.if_inline_ext
)))) {
692 ASSERT(ifp
->if_real_bytes
!= 0);
693 xfs_iext_destroy(ifp
);
695 ASSERT(ifp
->if_u1
.if_extents
== NULL
||
696 ifp
->if_u1
.if_extents
== ifp
->if_u2
.if_inline_ext
);
697 ASSERT(ifp
->if_real_bytes
== 0);
698 if (whichfork
== XFS_ATTR_FORK
) {
699 kmem_zone_free(xfs_ifork_zone
, ip
->i_afp
);
705 * xfs_iextents_copy()
707 * This is called to copy the REAL extents (as opposed to the delayed
708 * allocation extents) from the inode into the given buffer. It
709 * returns the number of bytes copied into the buffer.
711 * If there are no delayed allocation extents, then we can just
712 * memcpy() the extents into the buffer. Otherwise, we need to
713 * examine each extent in turn and skip those which are delayed.
725 xfs_fsblock_t start_block
;
727 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
728 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
|XFS_ILOCK_SHARED
));
729 ASSERT(ifp
->if_bytes
> 0);
731 nrecs
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
732 XFS_BMAP_TRACE_EXLIST(ip
, nrecs
, whichfork
);
736 * There are some delayed allocation extents in the
737 * inode, so copy the extents one at a time and skip
738 * the delayed ones. There must be at least one
739 * non-delayed extent.
742 for (i
= 0; i
< nrecs
; i
++) {
743 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, i
);
744 start_block
= xfs_bmbt_get_startblock(ep
);
745 if (isnullstartblock(start_block
)) {
747 * It's a delayed allocation extent, so skip it.
752 /* Translate to on disk format */
753 put_unaligned_be64(ep
->l0
, &dp
->l0
);
754 put_unaligned_be64(ep
->l1
, &dp
->l1
);
759 xfs_validate_extents(ifp
, copied
, XFS_EXTFMT_INODE(ip
));
761 return (copied
* (uint
)sizeof(xfs_bmbt_rec_t
));
765 * Each of the following cases stores data into the same region
766 * of the on-disk inode, so only one of them can be valid at
767 * any given time. While it is possible to have conflicting formats
768 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
769 * in EXTENTS format, this can only happen when the fork has
770 * changed formats after being modified but before being flushed.
771 * In these cases, the format always takes precedence, because the
772 * format indicates the current state of the fork.
778 xfs_inode_log_item_t
*iip
,
785 static const short brootflag
[2] =
786 { XFS_ILOG_DBROOT
, XFS_ILOG_ABROOT
};
787 static const short dataflag
[2] =
788 { XFS_ILOG_DDATA
, XFS_ILOG_ADATA
};
789 static const short extflag
[2] =
790 { XFS_ILOG_DEXT
, XFS_ILOG_AEXT
};
794 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
796 * This can happen if we gave up in iformat in an error path,
797 * for the attribute fork.
800 ASSERT(whichfork
== XFS_ATTR_FORK
);
803 cp
= XFS_DFORK_PTR(dip
, whichfork
);
805 switch (XFS_IFORK_FORMAT(ip
, whichfork
)) {
806 case XFS_DINODE_FMT_LOCAL
:
807 if ((iip
->ili_fields
& dataflag
[whichfork
]) &&
808 (ifp
->if_bytes
> 0)) {
809 ASSERT(ifp
->if_u1
.if_data
!= NULL
);
810 ASSERT(ifp
->if_bytes
<= XFS_IFORK_SIZE(ip
, whichfork
));
811 memcpy(cp
, ifp
->if_u1
.if_data
, ifp
->if_bytes
);
815 case XFS_DINODE_FMT_EXTENTS
:
816 ASSERT((ifp
->if_flags
& XFS_IFEXTENTS
) ||
817 !(iip
->ili_fields
& extflag
[whichfork
]));
818 if ((iip
->ili_fields
& extflag
[whichfork
]) &&
819 (ifp
->if_bytes
> 0)) {
820 ASSERT(xfs_iext_get_ext(ifp
, 0));
821 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) > 0);
822 (void)xfs_iextents_copy(ip
, (xfs_bmbt_rec_t
*)cp
,
827 case XFS_DINODE_FMT_BTREE
:
828 if ((iip
->ili_fields
& brootflag
[whichfork
]) &&
829 (ifp
->if_broot_bytes
> 0)) {
830 ASSERT(ifp
->if_broot
!= NULL
);
831 ASSERT(XFS_BMAP_BMDR_SPACE(ifp
->if_broot
) <=
832 XFS_IFORK_SIZE(ip
, whichfork
));
833 xfs_bmbt_to_bmdr(mp
, ifp
->if_broot
, ifp
->if_broot_bytes
,
834 (xfs_bmdr_block_t
*)cp
,
835 XFS_DFORK_SIZE(dip
, mp
, whichfork
));
839 case XFS_DINODE_FMT_DEV
:
840 if (iip
->ili_fields
& XFS_ILOG_DEV
) {
841 ASSERT(whichfork
== XFS_DATA_FORK
);
842 xfs_dinode_put_rdev(dip
, ip
->i_df
.if_u2
.if_rdev
);
846 case XFS_DINODE_FMT_UUID
:
847 if (iip
->ili_fields
& XFS_ILOG_UUID
) {
848 ASSERT(whichfork
== XFS_DATA_FORK
);
849 memcpy(XFS_DFORK_DPTR(dip
),
850 &ip
->i_df
.if_u2
.if_uuid
,
862 * Return a pointer to the extent record at file index idx.
864 xfs_bmbt_rec_host_t
*
866 xfs_ifork_t
*ifp
, /* inode fork pointer */
867 xfs_extnum_t idx
) /* index of target extent */
870 ASSERT(idx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
));
872 if ((ifp
->if_flags
& XFS_IFEXTIREC
) && (idx
== 0)) {
873 return ifp
->if_u1
.if_ext_irec
->er_extbuf
;
874 } else if (ifp
->if_flags
& XFS_IFEXTIREC
) {
875 xfs_ext_irec_t
*erp
; /* irec pointer */
876 int erp_idx
= 0; /* irec index */
877 xfs_extnum_t page_idx
= idx
; /* ext index in target list */
879 erp
= xfs_iext_idx_to_irec(ifp
, &page_idx
, &erp_idx
, 0);
880 return &erp
->er_extbuf
[page_idx
];
881 } else if (ifp
->if_bytes
) {
882 return &ifp
->if_u1
.if_extents
[idx
];
889 * Insert new item(s) into the extent records for incore inode
890 * fork 'ifp'. 'count' new items are inserted at index 'idx'.
894 xfs_inode_t
*ip
, /* incore inode pointer */
895 xfs_extnum_t idx
, /* starting index of new items */
896 xfs_extnum_t count
, /* number of inserted items */
897 xfs_bmbt_irec_t
*new, /* items to insert */
898 int state
) /* type of extent conversion */
900 xfs_ifork_t
*ifp
= (state
& BMAP_ATTRFORK
) ? ip
->i_afp
: &ip
->i_df
;
901 xfs_extnum_t i
; /* extent record index */
903 trace_xfs_iext_insert(ip
, idx
, new, state
, _RET_IP_
);
905 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
906 xfs_iext_add(ifp
, idx
, count
);
907 for (i
= idx
; i
< idx
+ count
; i
++, new++)
908 xfs_bmbt_set_all(xfs_iext_get_ext(ifp
, i
), new);
912 * This is called when the amount of space required for incore file
913 * extents needs to be increased. The ext_diff parameter stores the
914 * number of new extents being added and the idx parameter contains
915 * the extent index where the new extents will be added. If the new
916 * extents are being appended, then we just need to (re)allocate and
917 * initialize the space. Otherwise, if the new extents are being
918 * inserted into the middle of the existing entries, a bit more work
919 * is required to make room for the new extents to be inserted. The
920 * caller is responsible for filling in the new extent entries upon
925 xfs_ifork_t
*ifp
, /* inode fork pointer */
926 xfs_extnum_t idx
, /* index to begin adding exts */
927 int ext_diff
) /* number of extents to add */
929 int byte_diff
; /* new bytes being added */
930 int new_size
; /* size of extents after adding */
931 xfs_extnum_t nextents
; /* number of extents in file */
933 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
934 ASSERT((idx
>= 0) && (idx
<= nextents
));
935 byte_diff
= ext_diff
* sizeof(xfs_bmbt_rec_t
);
936 new_size
= ifp
->if_bytes
+ byte_diff
;
938 * If the new number of extents (nextents + ext_diff)
939 * fits inside the inode, then continue to use the inline
942 if (nextents
+ ext_diff
<= XFS_INLINE_EXTS
) {
943 if (idx
< nextents
) {
944 memmove(&ifp
->if_u2
.if_inline_ext
[idx
+ ext_diff
],
945 &ifp
->if_u2
.if_inline_ext
[idx
],
946 (nextents
- idx
) * sizeof(xfs_bmbt_rec_t
));
947 memset(&ifp
->if_u2
.if_inline_ext
[idx
], 0, byte_diff
);
949 ifp
->if_u1
.if_extents
= ifp
->if_u2
.if_inline_ext
;
950 ifp
->if_real_bytes
= 0;
953 * Otherwise use a linear (direct) extent list.
954 * If the extents are currently inside the inode,
955 * xfs_iext_realloc_direct will switch us from
956 * inline to direct extent allocation mode.
958 else if (nextents
+ ext_diff
<= XFS_LINEAR_EXTS
) {
959 xfs_iext_realloc_direct(ifp
, new_size
);
960 if (idx
< nextents
) {
961 memmove(&ifp
->if_u1
.if_extents
[idx
+ ext_diff
],
962 &ifp
->if_u1
.if_extents
[idx
],
963 (nextents
- idx
) * sizeof(xfs_bmbt_rec_t
));
964 memset(&ifp
->if_u1
.if_extents
[idx
], 0, byte_diff
);
967 /* Indirection array */
973 ASSERT(nextents
+ ext_diff
> XFS_LINEAR_EXTS
);
974 if (ifp
->if_flags
& XFS_IFEXTIREC
) {
975 erp
= xfs_iext_idx_to_irec(ifp
, &page_idx
, &erp_idx
, 1);
977 xfs_iext_irec_init(ifp
);
978 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
979 erp
= ifp
->if_u1
.if_ext_irec
;
981 /* Extents fit in target extent page */
982 if (erp
&& erp
->er_extcount
+ ext_diff
<= XFS_LINEAR_EXTS
) {
983 if (page_idx
< erp
->er_extcount
) {
984 memmove(&erp
->er_extbuf
[page_idx
+ ext_diff
],
985 &erp
->er_extbuf
[page_idx
],
986 (erp
->er_extcount
- page_idx
) *
987 sizeof(xfs_bmbt_rec_t
));
988 memset(&erp
->er_extbuf
[page_idx
], 0, byte_diff
);
990 erp
->er_extcount
+= ext_diff
;
991 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, ext_diff
);
993 /* Insert a new extent page */
995 xfs_iext_add_indirect_multi(ifp
,
996 erp_idx
, page_idx
, ext_diff
);
999 * If extent(s) are being appended to the last page in
1000 * the indirection array and the new extent(s) don't fit
1001 * in the page, then erp is NULL and erp_idx is set to
1002 * the next index needed in the indirection array.
1005 int count
= ext_diff
;
1008 erp
= xfs_iext_irec_new(ifp
, erp_idx
);
1009 erp
->er_extcount
= count
;
1010 count
-= MIN(count
, (int)XFS_LINEAR_EXTS
);
1017 ifp
->if_bytes
= new_size
;
1021 * This is called when incore extents are being added to the indirection
1022 * array and the new extents do not fit in the target extent list. The
1023 * erp_idx parameter contains the irec index for the target extent list
1024 * in the indirection array, and the idx parameter contains the extent
1025 * index within the list. The number of extents being added is stored
1026 * in the count parameter.
1028 * |-------| |-------|
1029 * | | | | idx - number of extents before idx
1031 * | | | | count - number of extents being inserted at idx
1032 * |-------| |-------|
1033 * | count | | nex2 | nex2 - number of extents after idx + count
1034 * |-------| |-------|
1037 xfs_iext_add_indirect_multi(
1038 xfs_ifork_t
*ifp
, /* inode fork pointer */
1039 int erp_idx
, /* target extent irec index */
1040 xfs_extnum_t idx
, /* index within target list */
1041 int count
) /* new extents being added */
1043 int byte_diff
; /* new bytes being added */
1044 xfs_ext_irec_t
*erp
; /* pointer to irec entry */
1045 xfs_extnum_t ext_diff
; /* number of extents to add */
1046 xfs_extnum_t ext_cnt
; /* new extents still needed */
1047 xfs_extnum_t nex2
; /* extents after idx + count */
1048 xfs_bmbt_rec_t
*nex2_ep
= NULL
; /* temp list for nex2 extents */
1049 int nlists
; /* number of irec's (lists) */
1051 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1052 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1053 nex2
= erp
->er_extcount
- idx
;
1054 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1057 * Save second part of target extent list
1058 * (all extents past */
1060 byte_diff
= nex2
* sizeof(xfs_bmbt_rec_t
);
1061 nex2_ep
= (xfs_bmbt_rec_t
*) kmem_alloc(byte_diff
, KM_NOFS
);
1062 memmove(nex2_ep
, &erp
->er_extbuf
[idx
], byte_diff
);
1063 erp
->er_extcount
-= nex2
;
1064 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, -nex2
);
1065 memset(&erp
->er_extbuf
[idx
], 0, byte_diff
);
1069 * Add the new extents to the end of the target
1070 * list, then allocate new irec record(s) and
1071 * extent buffer(s) as needed to store the rest
1072 * of the new extents.
1075 ext_diff
= MIN(ext_cnt
, (int)XFS_LINEAR_EXTS
- erp
->er_extcount
);
1077 erp
->er_extcount
+= ext_diff
;
1078 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, ext_diff
);
1079 ext_cnt
-= ext_diff
;
1083 erp
= xfs_iext_irec_new(ifp
, erp_idx
);
1084 ext_diff
= MIN(ext_cnt
, (int)XFS_LINEAR_EXTS
);
1085 erp
->er_extcount
= ext_diff
;
1086 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, ext_diff
);
1087 ext_cnt
-= ext_diff
;
1090 /* Add nex2 extents back to indirection array */
1092 xfs_extnum_t ext_avail
;
1095 byte_diff
= nex2
* sizeof(xfs_bmbt_rec_t
);
1096 ext_avail
= XFS_LINEAR_EXTS
- erp
->er_extcount
;
1099 * If nex2 extents fit in the current page, append
1100 * nex2_ep after the new extents.
1102 if (nex2
<= ext_avail
) {
1103 i
= erp
->er_extcount
;
1106 * Otherwise, check if space is available in the
1109 else if ((erp_idx
< nlists
- 1) &&
1110 (nex2
<= (ext_avail
= XFS_LINEAR_EXTS
-
1111 ifp
->if_u1
.if_ext_irec
[erp_idx
+1].er_extcount
))) {
1114 /* Create a hole for nex2 extents */
1115 memmove(&erp
->er_extbuf
[nex2
], erp
->er_extbuf
,
1116 erp
->er_extcount
* sizeof(xfs_bmbt_rec_t
));
1119 * Final choice, create a new extent page for
1124 erp
= xfs_iext_irec_new(ifp
, erp_idx
);
1126 memmove(&erp
->er_extbuf
[i
], nex2_ep
, byte_diff
);
1128 erp
->er_extcount
+= nex2
;
1129 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, nex2
);
1134 * This is called when the amount of space required for incore file
1135 * extents needs to be decreased. The ext_diff parameter stores the
1136 * number of extents to be removed and the idx parameter contains
1137 * the extent index where the extents will be removed from.
1139 * If the amount of space needed has decreased below the linear
1140 * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
1141 * extent array. Otherwise, use kmem_realloc() to adjust the
1142 * size to what is needed.
1146 xfs_inode_t
*ip
, /* incore inode pointer */
1147 xfs_extnum_t idx
, /* index to begin removing exts */
1148 int ext_diff
, /* number of extents to remove */
1149 int state
) /* type of extent conversion */
1151 xfs_ifork_t
*ifp
= (state
& BMAP_ATTRFORK
) ? ip
->i_afp
: &ip
->i_df
;
1152 xfs_extnum_t nextents
; /* number of extents in file */
1153 int new_size
; /* size of extents after removal */
1155 trace_xfs_iext_remove(ip
, idx
, state
, _RET_IP_
);
1157 ASSERT(ext_diff
> 0);
1158 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1159 new_size
= (nextents
- ext_diff
) * sizeof(xfs_bmbt_rec_t
);
1161 if (new_size
== 0) {
1162 xfs_iext_destroy(ifp
);
1163 } else if (ifp
->if_flags
& XFS_IFEXTIREC
) {
1164 xfs_iext_remove_indirect(ifp
, idx
, ext_diff
);
1165 } else if (ifp
->if_real_bytes
) {
1166 xfs_iext_remove_direct(ifp
, idx
, ext_diff
);
1168 xfs_iext_remove_inline(ifp
, idx
, ext_diff
);
1170 ifp
->if_bytes
= new_size
;
1174 * This removes ext_diff extents from the inline buffer, beginning
1175 * at extent index idx.
1178 xfs_iext_remove_inline(
1179 xfs_ifork_t
*ifp
, /* inode fork pointer */
1180 xfs_extnum_t idx
, /* index to begin removing exts */
1181 int ext_diff
) /* number of extents to remove */
1183 int nextents
; /* number of extents in file */
1185 ASSERT(!(ifp
->if_flags
& XFS_IFEXTIREC
));
1186 ASSERT(idx
< XFS_INLINE_EXTS
);
1187 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1188 ASSERT(((nextents
- ext_diff
) > 0) &&
1189 (nextents
- ext_diff
) < XFS_INLINE_EXTS
);
1191 if (idx
+ ext_diff
< nextents
) {
1192 memmove(&ifp
->if_u2
.if_inline_ext
[idx
],
1193 &ifp
->if_u2
.if_inline_ext
[idx
+ ext_diff
],
1194 (nextents
- (idx
+ ext_diff
)) *
1195 sizeof(xfs_bmbt_rec_t
));
1196 memset(&ifp
->if_u2
.if_inline_ext
[nextents
- ext_diff
],
1197 0, ext_diff
* sizeof(xfs_bmbt_rec_t
));
1199 memset(&ifp
->if_u2
.if_inline_ext
[idx
], 0,
1200 ext_diff
* sizeof(xfs_bmbt_rec_t
));
1205 * This removes ext_diff extents from a linear (direct) extent list,
1206 * beginning at extent index idx. If the extents are being removed
1207 * from the end of the list (ie. truncate) then we just need to re-
1208 * allocate the list to remove the extra space. Otherwise, if the
1209 * extents are being removed from the middle of the existing extent
1210 * entries, then we first need to move the extent records beginning
1211 * at idx + ext_diff up in the list to overwrite the records being
1212 * removed, then remove the extra space via kmem_realloc.
1215 xfs_iext_remove_direct(
1216 xfs_ifork_t
*ifp
, /* inode fork pointer */
1217 xfs_extnum_t idx
, /* index to begin removing exts */
1218 int ext_diff
) /* number of extents to remove */
1220 xfs_extnum_t nextents
; /* number of extents in file */
1221 int new_size
; /* size of extents after removal */
1223 ASSERT(!(ifp
->if_flags
& XFS_IFEXTIREC
));
1224 new_size
= ifp
->if_bytes
-
1225 (ext_diff
* sizeof(xfs_bmbt_rec_t
));
1226 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1228 if (new_size
== 0) {
1229 xfs_iext_destroy(ifp
);
1232 /* Move extents up in the list (if needed) */
1233 if (idx
+ ext_diff
< nextents
) {
1234 memmove(&ifp
->if_u1
.if_extents
[idx
],
1235 &ifp
->if_u1
.if_extents
[idx
+ ext_diff
],
1236 (nextents
- (idx
+ ext_diff
)) *
1237 sizeof(xfs_bmbt_rec_t
));
1239 memset(&ifp
->if_u1
.if_extents
[nextents
- ext_diff
],
1240 0, ext_diff
* sizeof(xfs_bmbt_rec_t
));
1242 * Reallocate the direct extent list. If the extents
1243 * will fit inside the inode then xfs_iext_realloc_direct
1244 * will switch from direct to inline extent allocation
1247 xfs_iext_realloc_direct(ifp
, new_size
);
1248 ifp
->if_bytes
= new_size
;
1252 * This is called when incore extents are being removed from the
1253 * indirection array and the extents being removed span multiple extent
1254 * buffers. The idx parameter contains the file extent index where we
1255 * want to begin removing extents, and the count parameter contains
1256 * how many extents need to be removed.
1258 * |-------| |-------|
1259 * | nex1 | | | nex1 - number of extents before idx
1260 * |-------| | count |
1261 * | | | | count - number of extents being removed at idx
1262 * | count | |-------|
1263 * | | | nex2 | nex2 - number of extents after idx + count
1264 * |-------| |-------|
1267 xfs_iext_remove_indirect(
1268 xfs_ifork_t
*ifp
, /* inode fork pointer */
1269 xfs_extnum_t idx
, /* index to begin removing extents */
1270 int count
) /* number of extents to remove */
1272 xfs_ext_irec_t
*erp
; /* indirection array pointer */
1273 int erp_idx
= 0; /* indirection array index */
1274 xfs_extnum_t ext_cnt
; /* extents left to remove */
1275 xfs_extnum_t ext_diff
; /* extents to remove in current list */
1276 xfs_extnum_t nex1
; /* number of extents before idx */
1277 xfs_extnum_t nex2
; /* extents after idx + count */
1278 int page_idx
= idx
; /* index in target extent list */
1280 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1281 erp
= xfs_iext_idx_to_irec(ifp
, &page_idx
, &erp_idx
, 0);
1282 ASSERT(erp
!= NULL
);
1286 nex2
= MAX((erp
->er_extcount
- (nex1
+ ext_cnt
)), 0);
1287 ext_diff
= MIN(ext_cnt
, (erp
->er_extcount
- nex1
));
1289 * Check for deletion of entire list;
1290 * xfs_iext_irec_remove() updates extent offsets.
1292 if (ext_diff
== erp
->er_extcount
) {
1293 xfs_iext_irec_remove(ifp
, erp_idx
);
1294 ext_cnt
-= ext_diff
;
1297 ASSERT(erp_idx
< ifp
->if_real_bytes
/
1299 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1306 /* Move extents up (if needed) */
1308 memmove(&erp
->er_extbuf
[nex1
],
1309 &erp
->er_extbuf
[nex1
+ ext_diff
],
1310 nex2
* sizeof(xfs_bmbt_rec_t
));
1312 /* Zero out rest of page */
1313 memset(&erp
->er_extbuf
[nex1
+ nex2
], 0, (XFS_IEXT_BUFSZ
-
1314 ((nex1
+ nex2
) * sizeof(xfs_bmbt_rec_t
))));
1315 /* Update remaining counters */
1316 erp
->er_extcount
-= ext_diff
;
1317 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, -ext_diff
);
1318 ext_cnt
-= ext_diff
;
1323 ifp
->if_bytes
-= count
* sizeof(xfs_bmbt_rec_t
);
1324 xfs_iext_irec_compact(ifp
);
1328 * Create, destroy, or resize a linear (direct) block of extents.
1331 xfs_iext_realloc_direct(
1332 xfs_ifork_t
*ifp
, /* inode fork pointer */
1333 int new_size
) /* new size of extents after adding */
1335 int rnew_size
; /* real new size of extents */
1337 rnew_size
= new_size
;
1339 ASSERT(!(ifp
->if_flags
& XFS_IFEXTIREC
) ||
1340 ((new_size
>= 0) && (new_size
<= XFS_IEXT_BUFSZ
) &&
1341 (new_size
!= ifp
->if_real_bytes
)));
1343 /* Free extent records */
1344 if (new_size
== 0) {
1345 xfs_iext_destroy(ifp
);
1347 /* Resize direct extent list and zero any new bytes */
1348 else if (ifp
->if_real_bytes
) {
1349 /* Check if extents will fit inside the inode */
1350 if (new_size
<= XFS_INLINE_EXTS
* sizeof(xfs_bmbt_rec_t
)) {
1351 xfs_iext_direct_to_inline(ifp
, new_size
/
1352 (uint
)sizeof(xfs_bmbt_rec_t
));
1353 ifp
->if_bytes
= new_size
;
1356 if (!is_power_of_2(new_size
)){
1357 rnew_size
= roundup_pow_of_two(new_size
);
1359 if (rnew_size
!= ifp
->if_real_bytes
) {
1360 ifp
->if_u1
.if_extents
=
1361 kmem_realloc(ifp
->if_u1
.if_extents
,
1363 ifp
->if_real_bytes
, KM_NOFS
);
1365 if (rnew_size
> ifp
->if_real_bytes
) {
1366 memset(&ifp
->if_u1
.if_extents
[ifp
->if_bytes
/
1367 (uint
)sizeof(xfs_bmbt_rec_t
)], 0,
1368 rnew_size
- ifp
->if_real_bytes
);
1371 /* Switch from the inline extent buffer to a direct extent list */
1373 if (!is_power_of_2(new_size
)) {
1374 rnew_size
= roundup_pow_of_two(new_size
);
1376 xfs_iext_inline_to_direct(ifp
, rnew_size
);
1378 ifp
->if_real_bytes
= rnew_size
;
1379 ifp
->if_bytes
= new_size
;
1383 * Switch from linear (direct) extent records to inline buffer.
1386 xfs_iext_direct_to_inline(
1387 xfs_ifork_t
*ifp
, /* inode fork pointer */
1388 xfs_extnum_t nextents
) /* number of extents in file */
1390 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1391 ASSERT(nextents
<= XFS_INLINE_EXTS
);
1393 * The inline buffer was zeroed when we switched
1394 * from inline to direct extent allocation mode,
1395 * so we don't need to clear it here.
1397 memcpy(ifp
->if_u2
.if_inline_ext
, ifp
->if_u1
.if_extents
,
1398 nextents
* sizeof(xfs_bmbt_rec_t
));
1399 kmem_free(ifp
->if_u1
.if_extents
);
1400 ifp
->if_u1
.if_extents
= ifp
->if_u2
.if_inline_ext
;
1401 ifp
->if_real_bytes
= 0;
1405 * Switch from inline buffer to linear (direct) extent records.
1406 * new_size should already be rounded up to the next power of 2
1407 * by the caller (when appropriate), so use new_size as it is.
1408 * However, since new_size may be rounded up, we can't update
1409 * if_bytes here. It is the caller's responsibility to update
1410 * if_bytes upon return.
1413 xfs_iext_inline_to_direct(
1414 xfs_ifork_t
*ifp
, /* inode fork pointer */
1415 int new_size
) /* number of extents in file */
1417 ifp
->if_u1
.if_extents
= kmem_alloc(new_size
, KM_NOFS
);
1418 memset(ifp
->if_u1
.if_extents
, 0, new_size
);
1419 if (ifp
->if_bytes
) {
1420 memcpy(ifp
->if_u1
.if_extents
, ifp
->if_u2
.if_inline_ext
,
1422 memset(ifp
->if_u2
.if_inline_ext
, 0, XFS_INLINE_EXTS
*
1423 sizeof(xfs_bmbt_rec_t
));
1425 ifp
->if_real_bytes
= new_size
;
1429 * Resize an extent indirection array to new_size bytes.
1432 xfs_iext_realloc_indirect(
1433 xfs_ifork_t
*ifp
, /* inode fork pointer */
1434 int new_size
) /* new indirection array size */
1436 int nlists
; /* number of irec's (ex lists) */
1437 int size
; /* current indirection array size */
1439 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1440 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1441 size
= nlists
* sizeof(xfs_ext_irec_t
);
1442 ASSERT(ifp
->if_real_bytes
);
1443 ASSERT((new_size
>= 0) && (new_size
!= size
));
1444 if (new_size
== 0) {
1445 xfs_iext_destroy(ifp
);
1447 ifp
->if_u1
.if_ext_irec
= (xfs_ext_irec_t
*)
1448 kmem_realloc(ifp
->if_u1
.if_ext_irec
,
1449 new_size
, size
, KM_NOFS
);
1454 * Switch from indirection array to linear (direct) extent allocations.
1457 xfs_iext_indirect_to_direct(
1458 xfs_ifork_t
*ifp
) /* inode fork pointer */
1460 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1461 xfs_extnum_t nextents
; /* number of extents in file */
1462 int size
; /* size of file extents */
1464 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1465 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1466 ASSERT(nextents
<= XFS_LINEAR_EXTS
);
1467 size
= nextents
* sizeof(xfs_bmbt_rec_t
);
1469 xfs_iext_irec_compact_pages(ifp
);
1470 ASSERT(ifp
->if_real_bytes
== XFS_IEXT_BUFSZ
);
1472 ep
= ifp
->if_u1
.if_ext_irec
->er_extbuf
;
1473 kmem_free(ifp
->if_u1
.if_ext_irec
);
1474 ifp
->if_flags
&= ~XFS_IFEXTIREC
;
1475 ifp
->if_u1
.if_extents
= ep
;
1476 ifp
->if_bytes
= size
;
1477 if (nextents
< XFS_LINEAR_EXTS
) {
1478 xfs_iext_realloc_direct(ifp
, size
);
1483 * Free incore file extents.
1487 xfs_ifork_t
*ifp
) /* inode fork pointer */
1489 if (ifp
->if_flags
& XFS_IFEXTIREC
) {
1493 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1494 for (erp_idx
= nlists
- 1; erp_idx
>= 0 ; erp_idx
--) {
1495 xfs_iext_irec_remove(ifp
, erp_idx
);
1497 ifp
->if_flags
&= ~XFS_IFEXTIREC
;
1498 } else if (ifp
->if_real_bytes
) {
1499 kmem_free(ifp
->if_u1
.if_extents
);
1500 } else if (ifp
->if_bytes
) {
1501 memset(ifp
->if_u2
.if_inline_ext
, 0, XFS_INLINE_EXTS
*
1502 sizeof(xfs_bmbt_rec_t
));
1504 ifp
->if_u1
.if_extents
= NULL
;
1505 ifp
->if_real_bytes
= 0;
1510 * Return a pointer to the extent record for file system block bno.
1512 xfs_bmbt_rec_host_t
* /* pointer to found extent record */
1513 xfs_iext_bno_to_ext(
1514 xfs_ifork_t
*ifp
, /* inode fork pointer */
1515 xfs_fileoff_t bno
, /* block number to search for */
1516 xfs_extnum_t
*idxp
) /* index of target extent */
1518 xfs_bmbt_rec_host_t
*base
; /* pointer to first extent */
1519 xfs_filblks_t blockcount
= 0; /* number of blocks in extent */
1520 xfs_bmbt_rec_host_t
*ep
= NULL
; /* pointer to target extent */
1521 xfs_ext_irec_t
*erp
= NULL
; /* indirection array pointer */
1522 int high
; /* upper boundary in search */
1523 xfs_extnum_t idx
= 0; /* index of target extent */
1524 int low
; /* lower boundary in search */
1525 xfs_extnum_t nextents
; /* number of file extents */
1526 xfs_fileoff_t startoff
= 0; /* start offset of extent */
1528 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1529 if (nextents
== 0) {
1534 if (ifp
->if_flags
& XFS_IFEXTIREC
) {
1535 /* Find target extent list */
1537 erp
= xfs_iext_bno_to_irec(ifp
, bno
, &erp_idx
);
1538 base
= erp
->er_extbuf
;
1539 high
= erp
->er_extcount
- 1;
1541 base
= ifp
->if_u1
.if_extents
;
1542 high
= nextents
- 1;
1544 /* Binary search extent records */
1545 while (low
<= high
) {
1546 idx
= (low
+ high
) >> 1;
1548 startoff
= xfs_bmbt_get_startoff(ep
);
1549 blockcount
= xfs_bmbt_get_blockcount(ep
);
1550 if (bno
< startoff
) {
1552 } else if (bno
>= startoff
+ blockcount
) {
1555 /* Convert back to file-based extent index */
1556 if (ifp
->if_flags
& XFS_IFEXTIREC
) {
1557 idx
+= erp
->er_extoff
;
1563 /* Convert back to file-based extent index */
1564 if (ifp
->if_flags
& XFS_IFEXTIREC
) {
1565 idx
+= erp
->er_extoff
;
1567 if (bno
>= startoff
+ blockcount
) {
1568 if (++idx
== nextents
) {
1571 ep
= xfs_iext_get_ext(ifp
, idx
);
1579 * Return a pointer to the indirection array entry containing the
1580 * extent record for filesystem block bno. Store the index of the
1581 * target irec in *erp_idxp.
1583 xfs_ext_irec_t
* /* pointer to found extent record */
1584 xfs_iext_bno_to_irec(
1585 xfs_ifork_t
*ifp
, /* inode fork pointer */
1586 xfs_fileoff_t bno
, /* block number to search for */
1587 int *erp_idxp
) /* irec index of target ext list */
1589 xfs_ext_irec_t
*erp
= NULL
; /* indirection array pointer */
1590 xfs_ext_irec_t
*erp_next
; /* next indirection array entry */
1591 int erp_idx
; /* indirection array index */
1592 int nlists
; /* number of extent irec's (lists) */
1593 int high
; /* binary search upper limit */
1594 int low
; /* binary search lower limit */
1596 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1597 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1601 while (low
<= high
) {
1602 erp_idx
= (low
+ high
) >> 1;
1603 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1604 erp_next
= erp_idx
< nlists
- 1 ? erp
+ 1 : NULL
;
1605 if (bno
< xfs_bmbt_get_startoff(erp
->er_extbuf
)) {
1607 } else if (erp_next
&& bno
>=
1608 xfs_bmbt_get_startoff(erp_next
->er_extbuf
)) {
1614 *erp_idxp
= erp_idx
;
1619 * Return a pointer to the indirection array entry containing the
1620 * extent record at file extent index *idxp. Store the index of the
1621 * target irec in *erp_idxp and store the page index of the target
1622 * extent record in *idxp.
1625 xfs_iext_idx_to_irec(
1626 xfs_ifork_t
*ifp
, /* inode fork pointer */
1627 xfs_extnum_t
*idxp
, /* extent index (file -> page) */
1628 int *erp_idxp
, /* pointer to target irec */
1629 int realloc
) /* new bytes were just added */
1631 xfs_ext_irec_t
*prev
; /* pointer to previous irec */
1632 xfs_ext_irec_t
*erp
= NULL
; /* pointer to current irec */
1633 int erp_idx
; /* indirection array index */
1634 int nlists
; /* number of irec's (ex lists) */
1635 int high
; /* binary search upper limit */
1636 int low
; /* binary search lower limit */
1637 xfs_extnum_t page_idx
= *idxp
; /* extent index in target list */
1639 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1640 ASSERT(page_idx
>= 0);
1641 ASSERT(page_idx
<= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
));
1642 ASSERT(page_idx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
) || realloc
);
1644 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1649 /* Binary search extent irec's */
1650 while (low
<= high
) {
1651 erp_idx
= (low
+ high
) >> 1;
1652 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1653 prev
= erp_idx
> 0 ? erp
- 1 : NULL
;
1654 if (page_idx
< erp
->er_extoff
|| (page_idx
== erp
->er_extoff
&&
1655 realloc
&& prev
&& prev
->er_extcount
< XFS_LINEAR_EXTS
)) {
1657 } else if (page_idx
> erp
->er_extoff
+ erp
->er_extcount
||
1658 (page_idx
== erp
->er_extoff
+ erp
->er_extcount
&&
1661 } else if (page_idx
== erp
->er_extoff
+ erp
->er_extcount
&&
1662 erp
->er_extcount
== XFS_LINEAR_EXTS
) {
1666 erp
= erp_idx
< nlists
? erp
+ 1 : NULL
;
1669 page_idx
-= erp
->er_extoff
;
1674 *erp_idxp
= erp_idx
;
1679 * Allocate and initialize an indirection array once the space needed
1680 * for incore extents increases above XFS_IEXT_BUFSZ.
1684 xfs_ifork_t
*ifp
) /* inode fork pointer */
1686 xfs_ext_irec_t
*erp
; /* indirection array pointer */
1687 xfs_extnum_t nextents
; /* number of extents in file */
1689 ASSERT(!(ifp
->if_flags
& XFS_IFEXTIREC
));
1690 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1691 ASSERT(nextents
<= XFS_LINEAR_EXTS
);
1693 erp
= kmem_alloc(sizeof(xfs_ext_irec_t
), KM_NOFS
);
1695 if (nextents
== 0) {
1696 ifp
->if_u1
.if_extents
= kmem_alloc(XFS_IEXT_BUFSZ
, KM_NOFS
);
1697 } else if (!ifp
->if_real_bytes
) {
1698 xfs_iext_inline_to_direct(ifp
, XFS_IEXT_BUFSZ
);
1699 } else if (ifp
->if_real_bytes
< XFS_IEXT_BUFSZ
) {
1700 xfs_iext_realloc_direct(ifp
, XFS_IEXT_BUFSZ
);
1702 erp
->er_extbuf
= ifp
->if_u1
.if_extents
;
1703 erp
->er_extcount
= nextents
;
1706 ifp
->if_flags
|= XFS_IFEXTIREC
;
1707 ifp
->if_real_bytes
= XFS_IEXT_BUFSZ
;
1708 ifp
->if_bytes
= nextents
* sizeof(xfs_bmbt_rec_t
);
1709 ifp
->if_u1
.if_ext_irec
= erp
;
1715 * Allocate and initialize a new entry in the indirection array.
1719 xfs_ifork_t
*ifp
, /* inode fork pointer */
1720 int erp_idx
) /* index for new irec */
1722 xfs_ext_irec_t
*erp
; /* indirection array pointer */
1723 int i
; /* loop counter */
1724 int nlists
; /* number of irec's (ex lists) */
1726 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1727 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1729 /* Resize indirection array */
1730 xfs_iext_realloc_indirect(ifp
, ++nlists
*
1731 sizeof(xfs_ext_irec_t
));
1733 * Move records down in the array so the
1734 * new page can use erp_idx.
1736 erp
= ifp
->if_u1
.if_ext_irec
;
1737 for (i
= nlists
- 1; i
> erp_idx
; i
--) {
1738 memmove(&erp
[i
], &erp
[i
-1], sizeof(xfs_ext_irec_t
));
1740 ASSERT(i
== erp_idx
);
1742 /* Initialize new extent record */
1743 erp
= ifp
->if_u1
.if_ext_irec
;
1744 erp
[erp_idx
].er_extbuf
= kmem_alloc(XFS_IEXT_BUFSZ
, KM_NOFS
);
1745 ifp
->if_real_bytes
= nlists
* XFS_IEXT_BUFSZ
;
1746 memset(erp
[erp_idx
].er_extbuf
, 0, XFS_IEXT_BUFSZ
);
1747 erp
[erp_idx
].er_extcount
= 0;
1748 erp
[erp_idx
].er_extoff
= erp_idx
> 0 ?
1749 erp
[erp_idx
-1].er_extoff
+ erp
[erp_idx
-1].er_extcount
: 0;
1750 return (&erp
[erp_idx
]);
1754 * Remove a record from the indirection array.
1757 xfs_iext_irec_remove(
1758 xfs_ifork_t
*ifp
, /* inode fork pointer */
1759 int erp_idx
) /* irec index to remove */
1761 xfs_ext_irec_t
*erp
; /* indirection array pointer */
1762 int i
; /* loop counter */
1763 int nlists
; /* number of irec's (ex lists) */
1765 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1766 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1767 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1768 if (erp
->er_extbuf
) {
1769 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1,
1771 kmem_free(erp
->er_extbuf
);
1773 /* Compact extent records */
1774 erp
= ifp
->if_u1
.if_ext_irec
;
1775 for (i
= erp_idx
; i
< nlists
- 1; i
++) {
1776 memmove(&erp
[i
], &erp
[i
+1], sizeof(xfs_ext_irec_t
));
1779 * Manually free the last extent record from the indirection
1780 * array. A call to xfs_iext_realloc_indirect() with a size
1781 * of zero would result in a call to xfs_iext_destroy() which
1782 * would in turn call this function again, creating a nasty
1786 xfs_iext_realloc_indirect(ifp
,
1787 nlists
* sizeof(xfs_ext_irec_t
));
1789 kmem_free(ifp
->if_u1
.if_ext_irec
);
1791 ifp
->if_real_bytes
= nlists
* XFS_IEXT_BUFSZ
;
1795 * This is called to clean up large amounts of unused memory allocated
1796 * by the indirection array. Before compacting anything though, verify
1797 * that the indirection array is still needed and switch back to the
1798 * linear extent list (or even the inline buffer) if possible. The
1799 * compaction policy is as follows:
1801 * Full Compaction: Extents fit into a single page (or inline buffer)
1802 * Partial Compaction: Extents occupy less than 50% of allocated space
1803 * No Compaction: Extents occupy at least 50% of allocated space
1806 xfs_iext_irec_compact(
1807 xfs_ifork_t
*ifp
) /* inode fork pointer */
1809 xfs_extnum_t nextents
; /* number of extents in file */
1810 int nlists
; /* number of irec's (ex lists) */
1812 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1813 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1814 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1816 if (nextents
== 0) {
1817 xfs_iext_destroy(ifp
);
1818 } else if (nextents
<= XFS_INLINE_EXTS
) {
1819 xfs_iext_indirect_to_direct(ifp
);
1820 xfs_iext_direct_to_inline(ifp
, nextents
);
1821 } else if (nextents
<= XFS_LINEAR_EXTS
) {
1822 xfs_iext_indirect_to_direct(ifp
);
1823 } else if (nextents
< (nlists
* XFS_LINEAR_EXTS
) >> 1) {
1824 xfs_iext_irec_compact_pages(ifp
);
1829 * Combine extents from neighboring extent pages.
1832 xfs_iext_irec_compact_pages(
1833 xfs_ifork_t
*ifp
) /* inode fork pointer */
1835 xfs_ext_irec_t
*erp
, *erp_next
;/* pointers to irec entries */
1836 int erp_idx
= 0; /* indirection array index */
1837 int nlists
; /* number of irec's (ex lists) */
1839 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1840 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1841 while (erp_idx
< nlists
- 1) {
1842 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1844 if (erp_next
->er_extcount
<=
1845 (XFS_LINEAR_EXTS
- erp
->er_extcount
)) {
1846 memcpy(&erp
->er_extbuf
[erp
->er_extcount
],
1847 erp_next
->er_extbuf
, erp_next
->er_extcount
*
1848 sizeof(xfs_bmbt_rec_t
));
1849 erp
->er_extcount
+= erp_next
->er_extcount
;
1851 * Free page before removing extent record
1852 * so er_extoffs don't get modified in
1853 * xfs_iext_irec_remove.
1855 kmem_free(erp_next
->er_extbuf
);
1856 erp_next
->er_extbuf
= NULL
;
1857 xfs_iext_irec_remove(ifp
, erp_idx
+ 1);
1858 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1866 * This is called to update the er_extoff field in the indirection
1867 * array when extents have been added or removed from one of the
1868 * extent lists. erp_idx contains the irec index to begin updating
1869 * at and ext_diff contains the number of extents that were added
1873 xfs_iext_irec_update_extoffs(
1874 xfs_ifork_t
*ifp
, /* inode fork pointer */
1875 int erp_idx
, /* irec index to update */
1876 int ext_diff
) /* number of new extents */
1878 int i
; /* loop counter */
1879 int nlists
; /* number of irec's (ex lists */
1881 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1882 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1883 for (i
= erp_idx
; i
< nlists
; i
++) {
1884 ifp
->if_u1
.if_ext_irec
[i
].er_extoff
+= ext_diff
;