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"
25 #include "xfs_trans.h"
26 #include "xfs_trans_priv.h"
29 #include "xfs_mount.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_attr_sf.h"
34 #include "xfs_dinode.h"
35 #include "xfs_inode.h"
36 #include "xfs_buf_item.h"
37 #include "xfs_inode_item.h"
38 #include "xfs_btree.h"
39 #include "xfs_alloc.h"
40 #include "xfs_ialloc.h"
42 #include "xfs_error.h"
43 #include "xfs_quota.h"
44 #include "xfs_filestream.h"
45 #include "xfs_cksum.h"
46 #include "xfs_trace.h"
47 #include "xfs_icache.h"
49 kmem_zone_t
*xfs_ifork_zone
;
51 STATIC
int xfs_iformat_local(xfs_inode_t
*, xfs_dinode_t
*, int, int);
52 STATIC
int xfs_iformat_extents(xfs_inode_t
*, xfs_dinode_t
*, int);
53 STATIC
int xfs_iformat_btree(xfs_inode_t
*, xfs_dinode_t
*, int);
57 * Make sure that the extents in the given memory buffer
67 xfs_bmbt_rec_host_t rec
;
70 for (i
= 0; i
< nrecs
; i
++) {
71 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, i
);
72 rec
.l0
= get_unaligned(&ep
->l0
);
73 rec
.l1
= get_unaligned(&ep
->l1
);
74 xfs_bmbt_get_all(&rec
, &irec
);
75 if (fmt
== XFS_EXTFMT_NOSTATE
)
76 ASSERT(irec
.br_state
== XFS_EXT_NORM
);
80 #define xfs_validate_extents(ifp, nrecs, fmt)
85 * Move inode type and inode format specific information from the
86 * on-disk inode to the in-core inode. For fifos, devs, and sockets
87 * this means set if_rdev to the proper value. For files, directories,
88 * and symlinks this means to bring in the in-line data or extent
89 * pointers. For a file in B-tree format, only the root is immediately
90 * brought in-core. The rest will be in-lined in if_extents when it
91 * is first referenced (see xfs_iread_extents()).
98 xfs_attr_shortform_t
*atp
;
103 if (unlikely(be32_to_cpu(dip
->di_nextents
) +
104 be16_to_cpu(dip
->di_anextents
) >
105 be64_to_cpu(dip
->di_nblocks
))) {
106 xfs_warn(ip
->i_mount
,
107 "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.",
108 (unsigned long long)ip
->i_ino
,
109 (int)(be32_to_cpu(dip
->di_nextents
) +
110 be16_to_cpu(dip
->di_anextents
)),
112 be64_to_cpu(dip
->di_nblocks
));
113 XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW
,
115 return XFS_ERROR(EFSCORRUPTED
);
118 if (unlikely(dip
->di_forkoff
> ip
->i_mount
->m_sb
.sb_inodesize
)) {
119 xfs_warn(ip
->i_mount
, "corrupt dinode %Lu, forkoff = 0x%x.",
120 (unsigned long long)ip
->i_ino
,
122 XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW
,
124 return XFS_ERROR(EFSCORRUPTED
);
127 if (unlikely((ip
->i_d
.di_flags
& XFS_DIFLAG_REALTIME
) &&
128 !ip
->i_mount
->m_rtdev_targp
)) {
129 xfs_warn(ip
->i_mount
,
130 "corrupt dinode %Lu, has realtime flag set.",
132 XFS_CORRUPTION_ERROR("xfs_iformat(realtime)",
133 XFS_ERRLEVEL_LOW
, ip
->i_mount
, dip
);
134 return XFS_ERROR(EFSCORRUPTED
);
137 switch (ip
->i_d
.di_mode
& S_IFMT
) {
142 if (unlikely(dip
->di_format
!= XFS_DINODE_FMT_DEV
)) {
143 XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW
,
145 return XFS_ERROR(EFSCORRUPTED
);
148 ip
->i_df
.if_u2
.if_rdev
= xfs_dinode_get_rdev(dip
);
154 switch (dip
->di_format
) {
155 case XFS_DINODE_FMT_LOCAL
:
157 * no local regular files yet
159 if (unlikely(S_ISREG(be16_to_cpu(dip
->di_mode
)))) {
160 xfs_warn(ip
->i_mount
,
161 "corrupt inode %Lu (local format for regular file).",
162 (unsigned long long) ip
->i_ino
);
163 XFS_CORRUPTION_ERROR("xfs_iformat(4)",
166 return XFS_ERROR(EFSCORRUPTED
);
169 di_size
= be64_to_cpu(dip
->di_size
);
170 if (unlikely(di_size
< 0 ||
171 di_size
> XFS_DFORK_DSIZE(dip
, ip
->i_mount
))) {
172 xfs_warn(ip
->i_mount
,
173 "corrupt inode %Lu (bad size %Ld for local inode).",
174 (unsigned long long) ip
->i_ino
,
175 (long long) di_size
);
176 XFS_CORRUPTION_ERROR("xfs_iformat(5)",
179 return XFS_ERROR(EFSCORRUPTED
);
183 error
= xfs_iformat_local(ip
, dip
, XFS_DATA_FORK
, size
);
185 case XFS_DINODE_FMT_EXTENTS
:
186 error
= xfs_iformat_extents(ip
, dip
, XFS_DATA_FORK
);
188 case XFS_DINODE_FMT_BTREE
:
189 error
= xfs_iformat_btree(ip
, dip
, XFS_DATA_FORK
);
192 XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW
,
194 return XFS_ERROR(EFSCORRUPTED
);
199 XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW
, ip
->i_mount
);
200 return XFS_ERROR(EFSCORRUPTED
);
205 if (!XFS_DFORK_Q(dip
))
208 ASSERT(ip
->i_afp
== NULL
);
209 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
| KM_NOFS
);
211 switch (dip
->di_aformat
) {
212 case XFS_DINODE_FMT_LOCAL
:
213 atp
= (xfs_attr_shortform_t
*)XFS_DFORK_APTR(dip
);
214 size
= be16_to_cpu(atp
->hdr
.totsize
);
216 if (unlikely(size
< sizeof(struct xfs_attr_sf_hdr
))) {
217 xfs_warn(ip
->i_mount
,
218 "corrupt inode %Lu (bad attr fork size %Ld).",
219 (unsigned long long) ip
->i_ino
,
221 XFS_CORRUPTION_ERROR("xfs_iformat(8)",
224 return XFS_ERROR(EFSCORRUPTED
);
227 error
= xfs_iformat_local(ip
, dip
, XFS_ATTR_FORK
, size
);
229 case XFS_DINODE_FMT_EXTENTS
:
230 error
= xfs_iformat_extents(ip
, dip
, XFS_ATTR_FORK
);
232 case XFS_DINODE_FMT_BTREE
:
233 error
= xfs_iformat_btree(ip
, dip
, XFS_ATTR_FORK
);
236 error
= XFS_ERROR(EFSCORRUPTED
);
240 kmem_zone_free(xfs_ifork_zone
, ip
->i_afp
);
242 xfs_idestroy_fork(ip
, XFS_DATA_FORK
);
248 * The file is in-lined in the on-disk inode.
249 * If it fits into if_inline_data, then copy
250 * it there, otherwise allocate a buffer for it
251 * and copy the data there. Either way, set
252 * if_data to point at the data.
253 * If we allocate a buffer for the data, make
254 * sure that its size is a multiple of 4 and
255 * record the real size in i_real_bytes.
268 * If the size is unreasonable, then something
269 * is wrong and we just bail out rather than crash in
270 * kmem_alloc() or memcpy() below.
272 if (unlikely(size
> XFS_DFORK_SIZE(dip
, ip
->i_mount
, whichfork
))) {
273 xfs_warn(ip
->i_mount
,
274 "corrupt inode %Lu (bad size %d for local fork, size = %d).",
275 (unsigned long long) ip
->i_ino
, size
,
276 XFS_DFORK_SIZE(dip
, ip
->i_mount
, whichfork
));
277 XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW
,
279 return XFS_ERROR(EFSCORRUPTED
);
281 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
284 ifp
->if_u1
.if_data
= NULL
;
285 else if (size
<= sizeof(ifp
->if_u2
.if_inline_data
))
286 ifp
->if_u1
.if_data
= ifp
->if_u2
.if_inline_data
;
288 real_size
= roundup(size
, 4);
289 ifp
->if_u1
.if_data
= kmem_alloc(real_size
, KM_SLEEP
| KM_NOFS
);
291 ifp
->if_bytes
= size
;
292 ifp
->if_real_bytes
= real_size
;
294 memcpy(ifp
->if_u1
.if_data
, XFS_DFORK_PTR(dip
, whichfork
), size
);
295 ifp
->if_flags
&= ~XFS_IFEXTENTS
;
296 ifp
->if_flags
|= XFS_IFINLINE
;
301 * The file consists of a set of extents all
302 * of which fit into the on-disk inode.
303 * If there are few enough extents to fit into
304 * the if_inline_ext, then copy them there.
305 * Otherwise allocate a buffer for them and copy
306 * them into it. Either way, set if_extents
307 * to point at the extents.
321 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
322 nex
= XFS_DFORK_NEXTENTS(dip
, whichfork
);
323 size
= nex
* (uint
)sizeof(xfs_bmbt_rec_t
);
326 * If the number of extents is unreasonable, then something
327 * is wrong and we just bail out rather than crash in
328 * kmem_alloc() or memcpy() below.
330 if (unlikely(size
< 0 || size
> XFS_DFORK_SIZE(dip
, ip
->i_mount
, whichfork
))) {
331 xfs_warn(ip
->i_mount
, "corrupt inode %Lu ((a)extents = %d).",
332 (unsigned long long) ip
->i_ino
, nex
);
333 XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW
,
335 return XFS_ERROR(EFSCORRUPTED
);
338 ifp
->if_real_bytes
= 0;
340 ifp
->if_u1
.if_extents
= NULL
;
341 else if (nex
<= XFS_INLINE_EXTS
)
342 ifp
->if_u1
.if_extents
= ifp
->if_u2
.if_inline_ext
;
344 xfs_iext_add(ifp
, 0, nex
);
346 ifp
->if_bytes
= size
;
348 dp
= (xfs_bmbt_rec_t
*) XFS_DFORK_PTR(dip
, whichfork
);
349 xfs_validate_extents(ifp
, nex
, XFS_EXTFMT_INODE(ip
));
350 for (i
= 0; i
< nex
; i
++, dp
++) {
351 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, i
);
352 ep
->l0
= get_unaligned_be64(&dp
->l0
);
353 ep
->l1
= get_unaligned_be64(&dp
->l1
);
355 XFS_BMAP_TRACE_EXLIST(ip
, nex
, whichfork
);
356 if (whichfork
!= XFS_DATA_FORK
||
357 XFS_EXTFMT_INODE(ip
) == XFS_EXTFMT_NOSTATE
)
358 if (unlikely(xfs_check_nostate_extents(
360 XFS_ERROR_REPORT("xfs_iformat_extents(2)",
363 return XFS_ERROR(EFSCORRUPTED
);
366 ifp
->if_flags
|= XFS_IFEXTENTS
;
371 * The file has too many extents to fit into
372 * the inode, so they are in B-tree format.
373 * Allocate a buffer for the root of the B-tree
374 * and copy the root into it. The i_extents
375 * field will remain NULL until all of the
376 * extents are read in (when they are needed).
384 struct xfs_mount
*mp
= ip
->i_mount
;
385 xfs_bmdr_block_t
*dfp
;
391 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
392 dfp
= (xfs_bmdr_block_t
*)XFS_DFORK_PTR(dip
, whichfork
);
393 size
= XFS_BMAP_BROOT_SPACE(mp
, dfp
);
394 nrecs
= be16_to_cpu(dfp
->bb_numrecs
);
397 * blow out if -- fork has less extents than can fit in
398 * fork (fork shouldn't be a btree format), root btree
399 * block has more records than can fit into the fork,
400 * or the number of extents is greater than the number of
403 if (unlikely(XFS_IFORK_NEXTENTS(ip
, whichfork
) <=
404 XFS_IFORK_MAXEXT(ip
, whichfork
) ||
405 XFS_BMDR_SPACE_CALC(nrecs
) >
406 XFS_DFORK_SIZE(dip
, mp
, whichfork
) ||
407 XFS_IFORK_NEXTENTS(ip
, whichfork
) > ip
->i_d
.di_nblocks
)) {
408 xfs_warn(mp
, "corrupt inode %Lu (btree).",
409 (unsigned long long) ip
->i_ino
);
410 XFS_CORRUPTION_ERROR("xfs_iformat_btree", XFS_ERRLEVEL_LOW
,
412 return XFS_ERROR(EFSCORRUPTED
);
415 ifp
->if_broot_bytes
= size
;
416 ifp
->if_broot
= kmem_alloc(size
, KM_SLEEP
| KM_NOFS
);
417 ASSERT(ifp
->if_broot
!= NULL
);
419 * Copy and convert from the on-disk structure
420 * to the in-memory structure.
422 xfs_bmdr_to_bmbt(ip
, dfp
, XFS_DFORK_SIZE(dip
, ip
->i_mount
, whichfork
),
423 ifp
->if_broot
, size
);
424 ifp
->if_flags
&= ~XFS_IFEXTENTS
;
425 ifp
->if_flags
|= XFS_IFBROOT
;
431 * Read in extents from a btree-format inode.
432 * Allocate and fill in if_extents. Real work is done in xfs_bmap.c.
442 xfs_extnum_t nextents
;
444 if (unlikely(XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
)) {
445 XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW
,
447 return XFS_ERROR(EFSCORRUPTED
);
449 nextents
= XFS_IFORK_NEXTENTS(ip
, whichfork
);
450 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
453 * We know that the size is valid (it's checked in iformat_btree)
455 ifp
->if_bytes
= ifp
->if_real_bytes
= 0;
456 ifp
->if_flags
|= XFS_IFEXTENTS
;
457 xfs_iext_add(ifp
, 0, nextents
);
458 error
= xfs_bmap_read_extents(tp
, ip
, whichfork
);
460 xfs_iext_destroy(ifp
);
461 ifp
->if_flags
&= ~XFS_IFEXTENTS
;
464 xfs_validate_extents(ifp
, nextents
, XFS_EXTFMT_INODE(ip
));
468 * Reallocate the space for if_broot based on the number of records
469 * being added or deleted as indicated in rec_diff. Move the records
470 * and pointers in if_broot to fit the new size. When shrinking this
471 * will eliminate holes between the records and pointers created by
472 * the caller. When growing this will create holes to be filled in
475 * The caller must not request to add more records than would fit in
476 * the on-disk inode root. If the if_broot is currently NULL, then
477 * if we are adding records, one will be allocated. The caller must also
478 * not request that the number of records go below zero, although
481 * ip -- the inode whose if_broot area is changing
482 * ext_diff -- the change in the number of records, positive or negative,
483 * requested for the if_broot array.
491 struct xfs_mount
*mp
= ip
->i_mount
;
494 struct xfs_btree_block
*new_broot
;
501 * Handle the degenerate case quietly.
507 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
510 * If there wasn't any memory allocated before, just
511 * allocate it now and get out.
513 if (ifp
->if_broot_bytes
== 0) {
514 new_size
= XFS_BMAP_BROOT_SPACE_CALC(mp
, rec_diff
);
515 ifp
->if_broot
= kmem_alloc(new_size
, KM_SLEEP
| KM_NOFS
);
516 ifp
->if_broot_bytes
= (int)new_size
;
521 * If there is already an existing if_broot, then we need
522 * to realloc() it and shift the pointers to their new
523 * location. The records don't change location because
524 * they are kept butted up against the btree block header.
526 cur_max
= xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0);
527 new_max
= cur_max
+ rec_diff
;
528 new_size
= XFS_BMAP_BROOT_SPACE_CALC(mp
, new_max
);
529 ifp
->if_broot
= kmem_realloc(ifp
->if_broot
, new_size
,
530 XFS_BMAP_BROOT_SPACE_CALC(mp
, cur_max
),
532 op
= (char *)XFS_BMAP_BROOT_PTR_ADDR(mp
, ifp
->if_broot
, 1,
533 ifp
->if_broot_bytes
);
534 np
= (char *)XFS_BMAP_BROOT_PTR_ADDR(mp
, ifp
->if_broot
, 1,
536 ifp
->if_broot_bytes
= (int)new_size
;
537 ASSERT(XFS_BMAP_BMDR_SPACE(ifp
->if_broot
) <=
538 XFS_IFORK_SIZE(ip
, whichfork
));
539 memmove(np
, op
, cur_max
* (uint
)sizeof(xfs_dfsbno_t
));
544 * rec_diff is less than 0. In this case, we are shrinking the
545 * if_broot buffer. It must already exist. If we go to zero
546 * records, just get rid of the root and clear the status bit.
548 ASSERT((ifp
->if_broot
!= NULL
) && (ifp
->if_broot_bytes
> 0));
549 cur_max
= xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0);
550 new_max
= cur_max
+ rec_diff
;
551 ASSERT(new_max
>= 0);
553 new_size
= XFS_BMAP_BROOT_SPACE_CALC(mp
, new_max
);
557 new_broot
= kmem_alloc(new_size
, KM_SLEEP
| KM_NOFS
);
559 * First copy over the btree block header.
561 memcpy(new_broot
, ifp
->if_broot
,
562 XFS_BMBT_BLOCK_LEN(ip
->i_mount
));
565 ifp
->if_flags
&= ~XFS_IFBROOT
;
569 * Only copy the records and pointers if there are any.
573 * First copy the records.
575 op
= (char *)XFS_BMBT_REC_ADDR(mp
, ifp
->if_broot
, 1);
576 np
= (char *)XFS_BMBT_REC_ADDR(mp
, new_broot
, 1);
577 memcpy(np
, op
, new_max
* (uint
)sizeof(xfs_bmbt_rec_t
));
580 * Then copy the pointers.
582 op
= (char *)XFS_BMAP_BROOT_PTR_ADDR(mp
, ifp
->if_broot
, 1,
583 ifp
->if_broot_bytes
);
584 np
= (char *)XFS_BMAP_BROOT_PTR_ADDR(mp
, new_broot
, 1,
586 memcpy(np
, op
, new_max
* (uint
)sizeof(xfs_dfsbno_t
));
588 kmem_free(ifp
->if_broot
);
589 ifp
->if_broot
= new_broot
;
590 ifp
->if_broot_bytes
= (int)new_size
;
592 ASSERT(XFS_BMAP_BMDR_SPACE(ifp
->if_broot
) <=
593 XFS_IFORK_SIZE(ip
, whichfork
));
599 * This is called when the amount of space needed for if_data
600 * is increased or decreased. The change in size is indicated by
601 * the number of bytes that need to be added or deleted in the
602 * byte_diff parameter.
604 * If the amount of space needed has decreased below the size of the
605 * inline buffer, then switch to using the inline buffer. Otherwise,
606 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
609 * ip -- the inode whose if_data area is changing
610 * byte_diff -- the change in the number of bytes, positive or negative,
611 * requested for the if_data array.
623 if (byte_diff
== 0) {
627 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
628 new_size
= (int)ifp
->if_bytes
+ byte_diff
;
629 ASSERT(new_size
>= 0);
632 if (ifp
->if_u1
.if_data
!= ifp
->if_u2
.if_inline_data
) {
633 kmem_free(ifp
->if_u1
.if_data
);
635 ifp
->if_u1
.if_data
= NULL
;
637 } else if (new_size
<= sizeof(ifp
->if_u2
.if_inline_data
)) {
639 * If the valid extents/data can fit in if_inline_ext/data,
640 * copy them from the malloc'd vector and free it.
642 if (ifp
->if_u1
.if_data
== NULL
) {
643 ifp
->if_u1
.if_data
= ifp
->if_u2
.if_inline_data
;
644 } else if (ifp
->if_u1
.if_data
!= ifp
->if_u2
.if_inline_data
) {
645 ASSERT(ifp
->if_real_bytes
!= 0);
646 memcpy(ifp
->if_u2
.if_inline_data
, ifp
->if_u1
.if_data
,
648 kmem_free(ifp
->if_u1
.if_data
);
649 ifp
->if_u1
.if_data
= ifp
->if_u2
.if_inline_data
;
654 * Stuck with malloc/realloc.
655 * For inline data, the underlying buffer must be
656 * a multiple of 4 bytes in size so that it can be
657 * logged and stay on word boundaries. We enforce
660 real_size
= roundup(new_size
, 4);
661 if (ifp
->if_u1
.if_data
== NULL
) {
662 ASSERT(ifp
->if_real_bytes
== 0);
663 ifp
->if_u1
.if_data
= kmem_alloc(real_size
,
665 } else if (ifp
->if_u1
.if_data
!= ifp
->if_u2
.if_inline_data
) {
667 * Only do the realloc if the underlying size
668 * is really changing.
670 if (ifp
->if_real_bytes
!= real_size
) {
672 kmem_realloc(ifp
->if_u1
.if_data
,
678 ASSERT(ifp
->if_real_bytes
== 0);
679 ifp
->if_u1
.if_data
= kmem_alloc(real_size
,
681 memcpy(ifp
->if_u1
.if_data
, ifp
->if_u2
.if_inline_data
,
685 ifp
->if_real_bytes
= real_size
;
686 ifp
->if_bytes
= new_size
;
687 ASSERT(ifp
->if_bytes
<= XFS_IFORK_SIZE(ip
, whichfork
));
697 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
698 if (ifp
->if_broot
!= NULL
) {
699 kmem_free(ifp
->if_broot
);
700 ifp
->if_broot
= NULL
;
704 * If the format is local, then we can't have an extents
705 * array so just look for an inline data array. If we're
706 * not local then we may or may not have an extents list,
707 * so check and free it up if we do.
709 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
710 if ((ifp
->if_u1
.if_data
!= ifp
->if_u2
.if_inline_data
) &&
711 (ifp
->if_u1
.if_data
!= NULL
)) {
712 ASSERT(ifp
->if_real_bytes
!= 0);
713 kmem_free(ifp
->if_u1
.if_data
);
714 ifp
->if_u1
.if_data
= NULL
;
715 ifp
->if_real_bytes
= 0;
717 } else if ((ifp
->if_flags
& XFS_IFEXTENTS
) &&
718 ((ifp
->if_flags
& XFS_IFEXTIREC
) ||
719 ((ifp
->if_u1
.if_extents
!= NULL
) &&
720 (ifp
->if_u1
.if_extents
!= ifp
->if_u2
.if_inline_ext
)))) {
721 ASSERT(ifp
->if_real_bytes
!= 0);
722 xfs_iext_destroy(ifp
);
724 ASSERT(ifp
->if_u1
.if_extents
== NULL
||
725 ifp
->if_u1
.if_extents
== ifp
->if_u2
.if_inline_ext
);
726 ASSERT(ifp
->if_real_bytes
== 0);
727 if (whichfork
== XFS_ATTR_FORK
) {
728 kmem_zone_free(xfs_ifork_zone
, ip
->i_afp
);
734 * xfs_iextents_copy()
736 * This is called to copy the REAL extents (as opposed to the delayed
737 * allocation extents) from the inode into the given buffer. It
738 * returns the number of bytes copied into the buffer.
740 * If there are no delayed allocation extents, then we can just
741 * memcpy() the extents into the buffer. Otherwise, we need to
742 * examine each extent in turn and skip those which are delayed.
754 xfs_fsblock_t start_block
;
756 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
757 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
|XFS_ILOCK_SHARED
));
758 ASSERT(ifp
->if_bytes
> 0);
760 nrecs
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
761 XFS_BMAP_TRACE_EXLIST(ip
, nrecs
, whichfork
);
765 * There are some delayed allocation extents in the
766 * inode, so copy the extents one at a time and skip
767 * the delayed ones. There must be at least one
768 * non-delayed extent.
771 for (i
= 0; i
< nrecs
; i
++) {
772 xfs_bmbt_rec_host_t
*ep
= xfs_iext_get_ext(ifp
, i
);
773 start_block
= xfs_bmbt_get_startblock(ep
);
774 if (isnullstartblock(start_block
)) {
776 * It's a delayed allocation extent, so skip it.
781 /* Translate to on disk format */
782 put_unaligned_be64(ep
->l0
, &dp
->l0
);
783 put_unaligned_be64(ep
->l1
, &dp
->l1
);
788 xfs_validate_extents(ifp
, copied
, XFS_EXTFMT_INODE(ip
));
790 return (copied
* (uint
)sizeof(xfs_bmbt_rec_t
));
794 * Each of the following cases stores data into the same region
795 * of the on-disk inode, so only one of them can be valid at
796 * any given time. While it is possible to have conflicting formats
797 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
798 * in EXTENTS format, this can only happen when the fork has
799 * changed formats after being modified but before being flushed.
800 * In these cases, the format always takes precedence, because the
801 * format indicates the current state of the fork.
807 xfs_inode_log_item_t
*iip
,
814 static const short brootflag
[2] =
815 { XFS_ILOG_DBROOT
, XFS_ILOG_ABROOT
};
816 static const short dataflag
[2] =
817 { XFS_ILOG_DDATA
, XFS_ILOG_ADATA
};
818 static const short extflag
[2] =
819 { XFS_ILOG_DEXT
, XFS_ILOG_AEXT
};
823 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
825 * This can happen if we gave up in iformat in an error path,
826 * for the attribute fork.
829 ASSERT(whichfork
== XFS_ATTR_FORK
);
832 cp
= XFS_DFORK_PTR(dip
, whichfork
);
834 switch (XFS_IFORK_FORMAT(ip
, whichfork
)) {
835 case XFS_DINODE_FMT_LOCAL
:
836 if ((iip
->ili_fields
& dataflag
[whichfork
]) &&
837 (ifp
->if_bytes
> 0)) {
838 ASSERT(ifp
->if_u1
.if_data
!= NULL
);
839 ASSERT(ifp
->if_bytes
<= XFS_IFORK_SIZE(ip
, whichfork
));
840 memcpy(cp
, ifp
->if_u1
.if_data
, ifp
->if_bytes
);
844 case XFS_DINODE_FMT_EXTENTS
:
845 ASSERT((ifp
->if_flags
& XFS_IFEXTENTS
) ||
846 !(iip
->ili_fields
& extflag
[whichfork
]));
847 if ((iip
->ili_fields
& extflag
[whichfork
]) &&
848 (ifp
->if_bytes
> 0)) {
849 ASSERT(xfs_iext_get_ext(ifp
, 0));
850 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) > 0);
851 (void)xfs_iextents_copy(ip
, (xfs_bmbt_rec_t
*)cp
,
856 case XFS_DINODE_FMT_BTREE
:
857 if ((iip
->ili_fields
& brootflag
[whichfork
]) &&
858 (ifp
->if_broot_bytes
> 0)) {
859 ASSERT(ifp
->if_broot
!= NULL
);
860 ASSERT(XFS_BMAP_BMDR_SPACE(ifp
->if_broot
) <=
861 XFS_IFORK_SIZE(ip
, whichfork
));
862 xfs_bmbt_to_bmdr(mp
, ifp
->if_broot
, ifp
->if_broot_bytes
,
863 (xfs_bmdr_block_t
*)cp
,
864 XFS_DFORK_SIZE(dip
, mp
, whichfork
));
868 case XFS_DINODE_FMT_DEV
:
869 if (iip
->ili_fields
& XFS_ILOG_DEV
) {
870 ASSERT(whichfork
== XFS_DATA_FORK
);
871 xfs_dinode_put_rdev(dip
, ip
->i_df
.if_u2
.if_rdev
);
875 case XFS_DINODE_FMT_UUID
:
876 if (iip
->ili_fields
& XFS_ILOG_UUID
) {
877 ASSERT(whichfork
== XFS_DATA_FORK
);
878 memcpy(XFS_DFORK_DPTR(dip
),
879 &ip
->i_df
.if_u2
.if_uuid
,
891 * Return a pointer to the extent record at file index idx.
893 xfs_bmbt_rec_host_t
*
895 xfs_ifork_t
*ifp
, /* inode fork pointer */
896 xfs_extnum_t idx
) /* index of target extent */
899 ASSERT(idx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
));
901 if ((ifp
->if_flags
& XFS_IFEXTIREC
) && (idx
== 0)) {
902 return ifp
->if_u1
.if_ext_irec
->er_extbuf
;
903 } else if (ifp
->if_flags
& XFS_IFEXTIREC
) {
904 xfs_ext_irec_t
*erp
; /* irec pointer */
905 int erp_idx
= 0; /* irec index */
906 xfs_extnum_t page_idx
= idx
; /* ext index in target list */
908 erp
= xfs_iext_idx_to_irec(ifp
, &page_idx
, &erp_idx
, 0);
909 return &erp
->er_extbuf
[page_idx
];
910 } else if (ifp
->if_bytes
) {
911 return &ifp
->if_u1
.if_extents
[idx
];
918 * Insert new item(s) into the extent records for incore inode
919 * fork 'ifp'. 'count' new items are inserted at index 'idx'.
923 xfs_inode_t
*ip
, /* incore inode pointer */
924 xfs_extnum_t idx
, /* starting index of new items */
925 xfs_extnum_t count
, /* number of inserted items */
926 xfs_bmbt_irec_t
*new, /* items to insert */
927 int state
) /* type of extent conversion */
929 xfs_ifork_t
*ifp
= (state
& BMAP_ATTRFORK
) ? ip
->i_afp
: &ip
->i_df
;
930 xfs_extnum_t i
; /* extent record index */
932 trace_xfs_iext_insert(ip
, idx
, new, state
, _RET_IP_
);
934 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
935 xfs_iext_add(ifp
, idx
, count
);
936 for (i
= idx
; i
< idx
+ count
; i
++, new++)
937 xfs_bmbt_set_all(xfs_iext_get_ext(ifp
, i
), new);
941 * This is called when the amount of space required for incore file
942 * extents needs to be increased. The ext_diff parameter stores the
943 * number of new extents being added and the idx parameter contains
944 * the extent index where the new extents will be added. If the new
945 * extents are being appended, then we just need to (re)allocate and
946 * initialize the space. Otherwise, if the new extents are being
947 * inserted into the middle of the existing entries, a bit more work
948 * is required to make room for the new extents to be inserted. The
949 * caller is responsible for filling in the new extent entries upon
954 xfs_ifork_t
*ifp
, /* inode fork pointer */
955 xfs_extnum_t idx
, /* index to begin adding exts */
956 int ext_diff
) /* number of extents to add */
958 int byte_diff
; /* new bytes being added */
959 int new_size
; /* size of extents after adding */
960 xfs_extnum_t nextents
; /* number of extents in file */
962 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
963 ASSERT((idx
>= 0) && (idx
<= nextents
));
964 byte_diff
= ext_diff
* sizeof(xfs_bmbt_rec_t
);
965 new_size
= ifp
->if_bytes
+ byte_diff
;
967 * If the new number of extents (nextents + ext_diff)
968 * fits inside the inode, then continue to use the inline
971 if (nextents
+ ext_diff
<= XFS_INLINE_EXTS
) {
972 if (idx
< nextents
) {
973 memmove(&ifp
->if_u2
.if_inline_ext
[idx
+ ext_diff
],
974 &ifp
->if_u2
.if_inline_ext
[idx
],
975 (nextents
- idx
) * sizeof(xfs_bmbt_rec_t
));
976 memset(&ifp
->if_u2
.if_inline_ext
[idx
], 0, byte_diff
);
978 ifp
->if_u1
.if_extents
= ifp
->if_u2
.if_inline_ext
;
979 ifp
->if_real_bytes
= 0;
982 * Otherwise use a linear (direct) extent list.
983 * If the extents are currently inside the inode,
984 * xfs_iext_realloc_direct will switch us from
985 * inline to direct extent allocation mode.
987 else if (nextents
+ ext_diff
<= XFS_LINEAR_EXTS
) {
988 xfs_iext_realloc_direct(ifp
, new_size
);
989 if (idx
< nextents
) {
990 memmove(&ifp
->if_u1
.if_extents
[idx
+ ext_diff
],
991 &ifp
->if_u1
.if_extents
[idx
],
992 (nextents
- idx
) * sizeof(xfs_bmbt_rec_t
));
993 memset(&ifp
->if_u1
.if_extents
[idx
], 0, byte_diff
);
996 /* Indirection array */
1002 ASSERT(nextents
+ ext_diff
> XFS_LINEAR_EXTS
);
1003 if (ifp
->if_flags
& XFS_IFEXTIREC
) {
1004 erp
= xfs_iext_idx_to_irec(ifp
, &page_idx
, &erp_idx
, 1);
1006 xfs_iext_irec_init(ifp
);
1007 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1008 erp
= ifp
->if_u1
.if_ext_irec
;
1010 /* Extents fit in target extent page */
1011 if (erp
&& erp
->er_extcount
+ ext_diff
<= XFS_LINEAR_EXTS
) {
1012 if (page_idx
< erp
->er_extcount
) {
1013 memmove(&erp
->er_extbuf
[page_idx
+ ext_diff
],
1014 &erp
->er_extbuf
[page_idx
],
1015 (erp
->er_extcount
- page_idx
) *
1016 sizeof(xfs_bmbt_rec_t
));
1017 memset(&erp
->er_extbuf
[page_idx
], 0, byte_diff
);
1019 erp
->er_extcount
+= ext_diff
;
1020 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, ext_diff
);
1022 /* Insert a new extent page */
1024 xfs_iext_add_indirect_multi(ifp
,
1025 erp_idx
, page_idx
, ext_diff
);
1028 * If extent(s) are being appended to the last page in
1029 * the indirection array and the new extent(s) don't fit
1030 * in the page, then erp is NULL and erp_idx is set to
1031 * the next index needed in the indirection array.
1034 uint count
= ext_diff
;
1037 erp
= xfs_iext_irec_new(ifp
, erp_idx
);
1038 erp
->er_extcount
= min(count
, XFS_LINEAR_EXTS
);
1039 count
-= erp
->er_extcount
;
1045 ifp
->if_bytes
= new_size
;
1049 * This is called when incore extents are being added to the indirection
1050 * array and the new extents do not fit in the target extent list. The
1051 * erp_idx parameter contains the irec index for the target extent list
1052 * in the indirection array, and the idx parameter contains the extent
1053 * index within the list. The number of extents being added is stored
1054 * in the count parameter.
1056 * |-------| |-------|
1057 * | | | | idx - number of extents before idx
1059 * | | | | count - number of extents being inserted at idx
1060 * |-------| |-------|
1061 * | count | | nex2 | nex2 - number of extents after idx + count
1062 * |-------| |-------|
1065 xfs_iext_add_indirect_multi(
1066 xfs_ifork_t
*ifp
, /* inode fork pointer */
1067 int erp_idx
, /* target extent irec index */
1068 xfs_extnum_t idx
, /* index within target list */
1069 int count
) /* new extents being added */
1071 int byte_diff
; /* new bytes being added */
1072 xfs_ext_irec_t
*erp
; /* pointer to irec entry */
1073 xfs_extnum_t ext_diff
; /* number of extents to add */
1074 xfs_extnum_t ext_cnt
; /* new extents still needed */
1075 xfs_extnum_t nex2
; /* extents after idx + count */
1076 xfs_bmbt_rec_t
*nex2_ep
= NULL
; /* temp list for nex2 extents */
1077 int nlists
; /* number of irec's (lists) */
1079 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1080 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1081 nex2
= erp
->er_extcount
- idx
;
1082 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1085 * Save second part of target extent list
1086 * (all extents past */
1088 byte_diff
= nex2
* sizeof(xfs_bmbt_rec_t
);
1089 nex2_ep
= (xfs_bmbt_rec_t
*) kmem_alloc(byte_diff
, KM_NOFS
);
1090 memmove(nex2_ep
, &erp
->er_extbuf
[idx
], byte_diff
);
1091 erp
->er_extcount
-= nex2
;
1092 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, -nex2
);
1093 memset(&erp
->er_extbuf
[idx
], 0, byte_diff
);
1097 * Add the new extents to the end of the target
1098 * list, then allocate new irec record(s) and
1099 * extent buffer(s) as needed to store the rest
1100 * of the new extents.
1103 ext_diff
= MIN(ext_cnt
, (int)XFS_LINEAR_EXTS
- erp
->er_extcount
);
1105 erp
->er_extcount
+= ext_diff
;
1106 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, ext_diff
);
1107 ext_cnt
-= ext_diff
;
1111 erp
= xfs_iext_irec_new(ifp
, erp_idx
);
1112 ext_diff
= MIN(ext_cnt
, (int)XFS_LINEAR_EXTS
);
1113 erp
->er_extcount
= ext_diff
;
1114 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, ext_diff
);
1115 ext_cnt
-= ext_diff
;
1118 /* Add nex2 extents back to indirection array */
1120 xfs_extnum_t ext_avail
;
1123 byte_diff
= nex2
* sizeof(xfs_bmbt_rec_t
);
1124 ext_avail
= XFS_LINEAR_EXTS
- erp
->er_extcount
;
1127 * If nex2 extents fit in the current page, append
1128 * nex2_ep after the new extents.
1130 if (nex2
<= ext_avail
) {
1131 i
= erp
->er_extcount
;
1134 * Otherwise, check if space is available in the
1137 else if ((erp_idx
< nlists
- 1) &&
1138 (nex2
<= (ext_avail
= XFS_LINEAR_EXTS
-
1139 ifp
->if_u1
.if_ext_irec
[erp_idx
+1].er_extcount
))) {
1142 /* Create a hole for nex2 extents */
1143 memmove(&erp
->er_extbuf
[nex2
], erp
->er_extbuf
,
1144 erp
->er_extcount
* sizeof(xfs_bmbt_rec_t
));
1147 * Final choice, create a new extent page for
1152 erp
= xfs_iext_irec_new(ifp
, erp_idx
);
1154 memmove(&erp
->er_extbuf
[i
], nex2_ep
, byte_diff
);
1156 erp
->er_extcount
+= nex2
;
1157 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, nex2
);
1162 * This is called when the amount of space required for incore file
1163 * extents needs to be decreased. The ext_diff parameter stores the
1164 * number of extents to be removed and the idx parameter contains
1165 * the extent index where the extents will be removed from.
1167 * If the amount of space needed has decreased below the linear
1168 * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
1169 * extent array. Otherwise, use kmem_realloc() to adjust the
1170 * size to what is needed.
1174 xfs_inode_t
*ip
, /* incore inode pointer */
1175 xfs_extnum_t idx
, /* index to begin removing exts */
1176 int ext_diff
, /* number of extents to remove */
1177 int state
) /* type of extent conversion */
1179 xfs_ifork_t
*ifp
= (state
& BMAP_ATTRFORK
) ? ip
->i_afp
: &ip
->i_df
;
1180 xfs_extnum_t nextents
; /* number of extents in file */
1181 int new_size
; /* size of extents after removal */
1183 trace_xfs_iext_remove(ip
, idx
, state
, _RET_IP_
);
1185 ASSERT(ext_diff
> 0);
1186 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1187 new_size
= (nextents
- ext_diff
) * sizeof(xfs_bmbt_rec_t
);
1189 if (new_size
== 0) {
1190 xfs_iext_destroy(ifp
);
1191 } else if (ifp
->if_flags
& XFS_IFEXTIREC
) {
1192 xfs_iext_remove_indirect(ifp
, idx
, ext_diff
);
1193 } else if (ifp
->if_real_bytes
) {
1194 xfs_iext_remove_direct(ifp
, idx
, ext_diff
);
1196 xfs_iext_remove_inline(ifp
, idx
, ext_diff
);
1198 ifp
->if_bytes
= new_size
;
1202 * This removes ext_diff extents from the inline buffer, beginning
1203 * at extent index idx.
1206 xfs_iext_remove_inline(
1207 xfs_ifork_t
*ifp
, /* inode fork pointer */
1208 xfs_extnum_t idx
, /* index to begin removing exts */
1209 int ext_diff
) /* number of extents to remove */
1211 int nextents
; /* number of extents in file */
1213 ASSERT(!(ifp
->if_flags
& XFS_IFEXTIREC
));
1214 ASSERT(idx
< XFS_INLINE_EXTS
);
1215 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1216 ASSERT(((nextents
- ext_diff
) > 0) &&
1217 (nextents
- ext_diff
) < XFS_INLINE_EXTS
);
1219 if (idx
+ ext_diff
< nextents
) {
1220 memmove(&ifp
->if_u2
.if_inline_ext
[idx
],
1221 &ifp
->if_u2
.if_inline_ext
[idx
+ ext_diff
],
1222 (nextents
- (idx
+ ext_diff
)) *
1223 sizeof(xfs_bmbt_rec_t
));
1224 memset(&ifp
->if_u2
.if_inline_ext
[nextents
- ext_diff
],
1225 0, ext_diff
* sizeof(xfs_bmbt_rec_t
));
1227 memset(&ifp
->if_u2
.if_inline_ext
[idx
], 0,
1228 ext_diff
* sizeof(xfs_bmbt_rec_t
));
1233 * This removes ext_diff extents from a linear (direct) extent list,
1234 * beginning at extent index idx. If the extents are being removed
1235 * from the end of the list (ie. truncate) then we just need to re-
1236 * allocate the list to remove the extra space. Otherwise, if the
1237 * extents are being removed from the middle of the existing extent
1238 * entries, then we first need to move the extent records beginning
1239 * at idx + ext_diff up in the list to overwrite the records being
1240 * removed, then remove the extra space via kmem_realloc.
1243 xfs_iext_remove_direct(
1244 xfs_ifork_t
*ifp
, /* inode fork pointer */
1245 xfs_extnum_t idx
, /* index to begin removing exts */
1246 int ext_diff
) /* number of extents to remove */
1248 xfs_extnum_t nextents
; /* number of extents in file */
1249 int new_size
; /* size of extents after removal */
1251 ASSERT(!(ifp
->if_flags
& XFS_IFEXTIREC
));
1252 new_size
= ifp
->if_bytes
-
1253 (ext_diff
* sizeof(xfs_bmbt_rec_t
));
1254 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1256 if (new_size
== 0) {
1257 xfs_iext_destroy(ifp
);
1260 /* Move extents up in the list (if needed) */
1261 if (idx
+ ext_diff
< nextents
) {
1262 memmove(&ifp
->if_u1
.if_extents
[idx
],
1263 &ifp
->if_u1
.if_extents
[idx
+ ext_diff
],
1264 (nextents
- (idx
+ ext_diff
)) *
1265 sizeof(xfs_bmbt_rec_t
));
1267 memset(&ifp
->if_u1
.if_extents
[nextents
- ext_diff
],
1268 0, ext_diff
* sizeof(xfs_bmbt_rec_t
));
1270 * Reallocate the direct extent list. If the extents
1271 * will fit inside the inode then xfs_iext_realloc_direct
1272 * will switch from direct to inline extent allocation
1275 xfs_iext_realloc_direct(ifp
, new_size
);
1276 ifp
->if_bytes
= new_size
;
1280 * This is called when incore extents are being removed from the
1281 * indirection array and the extents being removed span multiple extent
1282 * buffers. The idx parameter contains the file extent index where we
1283 * want to begin removing extents, and the count parameter contains
1284 * how many extents need to be removed.
1286 * |-------| |-------|
1287 * | nex1 | | | nex1 - number of extents before idx
1288 * |-------| | count |
1289 * | | | | count - number of extents being removed at idx
1290 * | count | |-------|
1291 * | | | nex2 | nex2 - number of extents after idx + count
1292 * |-------| |-------|
1295 xfs_iext_remove_indirect(
1296 xfs_ifork_t
*ifp
, /* inode fork pointer */
1297 xfs_extnum_t idx
, /* index to begin removing extents */
1298 int count
) /* number of extents to remove */
1300 xfs_ext_irec_t
*erp
; /* indirection array pointer */
1301 int erp_idx
= 0; /* indirection array index */
1302 xfs_extnum_t ext_cnt
; /* extents left to remove */
1303 xfs_extnum_t ext_diff
; /* extents to remove in current list */
1304 xfs_extnum_t nex1
; /* number of extents before idx */
1305 xfs_extnum_t nex2
; /* extents after idx + count */
1306 int page_idx
= idx
; /* index in target extent list */
1308 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1309 erp
= xfs_iext_idx_to_irec(ifp
, &page_idx
, &erp_idx
, 0);
1310 ASSERT(erp
!= NULL
);
1314 nex2
= MAX((erp
->er_extcount
- (nex1
+ ext_cnt
)), 0);
1315 ext_diff
= MIN(ext_cnt
, (erp
->er_extcount
- nex1
));
1317 * Check for deletion of entire list;
1318 * xfs_iext_irec_remove() updates extent offsets.
1320 if (ext_diff
== erp
->er_extcount
) {
1321 xfs_iext_irec_remove(ifp
, erp_idx
);
1322 ext_cnt
-= ext_diff
;
1325 ASSERT(erp_idx
< ifp
->if_real_bytes
/
1327 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1334 /* Move extents up (if needed) */
1336 memmove(&erp
->er_extbuf
[nex1
],
1337 &erp
->er_extbuf
[nex1
+ ext_diff
],
1338 nex2
* sizeof(xfs_bmbt_rec_t
));
1340 /* Zero out rest of page */
1341 memset(&erp
->er_extbuf
[nex1
+ nex2
], 0, (XFS_IEXT_BUFSZ
-
1342 ((nex1
+ nex2
) * sizeof(xfs_bmbt_rec_t
))));
1343 /* Update remaining counters */
1344 erp
->er_extcount
-= ext_diff
;
1345 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1, -ext_diff
);
1346 ext_cnt
-= ext_diff
;
1351 ifp
->if_bytes
-= count
* sizeof(xfs_bmbt_rec_t
);
1352 xfs_iext_irec_compact(ifp
);
1356 * Create, destroy, or resize a linear (direct) block of extents.
1359 xfs_iext_realloc_direct(
1360 xfs_ifork_t
*ifp
, /* inode fork pointer */
1361 int new_size
) /* new size of extents after adding */
1363 int rnew_size
; /* real new size of extents */
1365 rnew_size
= new_size
;
1367 ASSERT(!(ifp
->if_flags
& XFS_IFEXTIREC
) ||
1368 ((new_size
>= 0) && (new_size
<= XFS_IEXT_BUFSZ
) &&
1369 (new_size
!= ifp
->if_real_bytes
)));
1371 /* Free extent records */
1372 if (new_size
== 0) {
1373 xfs_iext_destroy(ifp
);
1375 /* Resize direct extent list and zero any new bytes */
1376 else if (ifp
->if_real_bytes
) {
1377 /* Check if extents will fit inside the inode */
1378 if (new_size
<= XFS_INLINE_EXTS
* sizeof(xfs_bmbt_rec_t
)) {
1379 xfs_iext_direct_to_inline(ifp
, new_size
/
1380 (uint
)sizeof(xfs_bmbt_rec_t
));
1381 ifp
->if_bytes
= new_size
;
1384 if (!is_power_of_2(new_size
)){
1385 rnew_size
= roundup_pow_of_two(new_size
);
1387 if (rnew_size
!= ifp
->if_real_bytes
) {
1388 ifp
->if_u1
.if_extents
=
1389 kmem_realloc(ifp
->if_u1
.if_extents
,
1391 ifp
->if_real_bytes
, KM_NOFS
);
1393 if (rnew_size
> ifp
->if_real_bytes
) {
1394 memset(&ifp
->if_u1
.if_extents
[ifp
->if_bytes
/
1395 (uint
)sizeof(xfs_bmbt_rec_t
)], 0,
1396 rnew_size
- ifp
->if_real_bytes
);
1399 /* Switch from the inline extent buffer to a direct extent list */
1401 if (!is_power_of_2(new_size
)) {
1402 rnew_size
= roundup_pow_of_two(new_size
);
1404 xfs_iext_inline_to_direct(ifp
, rnew_size
);
1406 ifp
->if_real_bytes
= rnew_size
;
1407 ifp
->if_bytes
= new_size
;
1411 * Switch from linear (direct) extent records to inline buffer.
1414 xfs_iext_direct_to_inline(
1415 xfs_ifork_t
*ifp
, /* inode fork pointer */
1416 xfs_extnum_t nextents
) /* number of extents in file */
1418 ASSERT(ifp
->if_flags
& XFS_IFEXTENTS
);
1419 ASSERT(nextents
<= XFS_INLINE_EXTS
);
1421 * The inline buffer was zeroed when we switched
1422 * from inline to direct extent allocation mode,
1423 * so we don't need to clear it here.
1425 memcpy(ifp
->if_u2
.if_inline_ext
, ifp
->if_u1
.if_extents
,
1426 nextents
* sizeof(xfs_bmbt_rec_t
));
1427 kmem_free(ifp
->if_u1
.if_extents
);
1428 ifp
->if_u1
.if_extents
= ifp
->if_u2
.if_inline_ext
;
1429 ifp
->if_real_bytes
= 0;
1433 * Switch from inline buffer to linear (direct) extent records.
1434 * new_size should already be rounded up to the next power of 2
1435 * by the caller (when appropriate), so use new_size as it is.
1436 * However, since new_size may be rounded up, we can't update
1437 * if_bytes here. It is the caller's responsibility to update
1438 * if_bytes upon return.
1441 xfs_iext_inline_to_direct(
1442 xfs_ifork_t
*ifp
, /* inode fork pointer */
1443 int new_size
) /* number of extents in file */
1445 ifp
->if_u1
.if_extents
= kmem_alloc(new_size
, KM_NOFS
);
1446 memset(ifp
->if_u1
.if_extents
, 0, new_size
);
1447 if (ifp
->if_bytes
) {
1448 memcpy(ifp
->if_u1
.if_extents
, ifp
->if_u2
.if_inline_ext
,
1450 memset(ifp
->if_u2
.if_inline_ext
, 0, XFS_INLINE_EXTS
*
1451 sizeof(xfs_bmbt_rec_t
));
1453 ifp
->if_real_bytes
= new_size
;
1457 * Resize an extent indirection array to new_size bytes.
1460 xfs_iext_realloc_indirect(
1461 xfs_ifork_t
*ifp
, /* inode fork pointer */
1462 int new_size
) /* new indirection array size */
1464 int nlists
; /* number of irec's (ex lists) */
1465 int size
; /* current indirection array size */
1467 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1468 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1469 size
= nlists
* sizeof(xfs_ext_irec_t
);
1470 ASSERT(ifp
->if_real_bytes
);
1471 ASSERT((new_size
>= 0) && (new_size
!= size
));
1472 if (new_size
== 0) {
1473 xfs_iext_destroy(ifp
);
1475 ifp
->if_u1
.if_ext_irec
= (xfs_ext_irec_t
*)
1476 kmem_realloc(ifp
->if_u1
.if_ext_irec
,
1477 new_size
, size
, KM_NOFS
);
1482 * Switch from indirection array to linear (direct) extent allocations.
1485 xfs_iext_indirect_to_direct(
1486 xfs_ifork_t
*ifp
) /* inode fork pointer */
1488 xfs_bmbt_rec_host_t
*ep
; /* extent record pointer */
1489 xfs_extnum_t nextents
; /* number of extents in file */
1490 int size
; /* size of file extents */
1492 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1493 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1494 ASSERT(nextents
<= XFS_LINEAR_EXTS
);
1495 size
= nextents
* sizeof(xfs_bmbt_rec_t
);
1497 xfs_iext_irec_compact_pages(ifp
);
1498 ASSERT(ifp
->if_real_bytes
== XFS_IEXT_BUFSZ
);
1500 ep
= ifp
->if_u1
.if_ext_irec
->er_extbuf
;
1501 kmem_free(ifp
->if_u1
.if_ext_irec
);
1502 ifp
->if_flags
&= ~XFS_IFEXTIREC
;
1503 ifp
->if_u1
.if_extents
= ep
;
1504 ifp
->if_bytes
= size
;
1505 if (nextents
< XFS_LINEAR_EXTS
) {
1506 xfs_iext_realloc_direct(ifp
, size
);
1511 * Free incore file extents.
1515 xfs_ifork_t
*ifp
) /* inode fork pointer */
1517 if (ifp
->if_flags
& XFS_IFEXTIREC
) {
1521 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1522 for (erp_idx
= nlists
- 1; erp_idx
>= 0 ; erp_idx
--) {
1523 xfs_iext_irec_remove(ifp
, erp_idx
);
1525 ifp
->if_flags
&= ~XFS_IFEXTIREC
;
1526 } else if (ifp
->if_real_bytes
) {
1527 kmem_free(ifp
->if_u1
.if_extents
);
1528 } else if (ifp
->if_bytes
) {
1529 memset(ifp
->if_u2
.if_inline_ext
, 0, XFS_INLINE_EXTS
*
1530 sizeof(xfs_bmbt_rec_t
));
1532 ifp
->if_u1
.if_extents
= NULL
;
1533 ifp
->if_real_bytes
= 0;
1538 * Return a pointer to the extent record for file system block bno.
1540 xfs_bmbt_rec_host_t
* /* pointer to found extent record */
1541 xfs_iext_bno_to_ext(
1542 xfs_ifork_t
*ifp
, /* inode fork pointer */
1543 xfs_fileoff_t bno
, /* block number to search for */
1544 xfs_extnum_t
*idxp
) /* index of target extent */
1546 xfs_bmbt_rec_host_t
*base
; /* pointer to first extent */
1547 xfs_filblks_t blockcount
= 0; /* number of blocks in extent */
1548 xfs_bmbt_rec_host_t
*ep
= NULL
; /* pointer to target extent */
1549 xfs_ext_irec_t
*erp
= NULL
; /* indirection array pointer */
1550 int high
; /* upper boundary in search */
1551 xfs_extnum_t idx
= 0; /* index of target extent */
1552 int low
; /* lower boundary in search */
1553 xfs_extnum_t nextents
; /* number of file extents */
1554 xfs_fileoff_t startoff
= 0; /* start offset of extent */
1556 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1557 if (nextents
== 0) {
1562 if (ifp
->if_flags
& XFS_IFEXTIREC
) {
1563 /* Find target extent list */
1565 erp
= xfs_iext_bno_to_irec(ifp
, bno
, &erp_idx
);
1566 base
= erp
->er_extbuf
;
1567 high
= erp
->er_extcount
- 1;
1569 base
= ifp
->if_u1
.if_extents
;
1570 high
= nextents
- 1;
1572 /* Binary search extent records */
1573 while (low
<= high
) {
1574 idx
= (low
+ high
) >> 1;
1576 startoff
= xfs_bmbt_get_startoff(ep
);
1577 blockcount
= xfs_bmbt_get_blockcount(ep
);
1578 if (bno
< startoff
) {
1580 } else if (bno
>= startoff
+ blockcount
) {
1583 /* Convert back to file-based extent index */
1584 if (ifp
->if_flags
& XFS_IFEXTIREC
) {
1585 idx
+= erp
->er_extoff
;
1591 /* Convert back to file-based extent index */
1592 if (ifp
->if_flags
& XFS_IFEXTIREC
) {
1593 idx
+= erp
->er_extoff
;
1595 if (bno
>= startoff
+ blockcount
) {
1596 if (++idx
== nextents
) {
1599 ep
= xfs_iext_get_ext(ifp
, idx
);
1607 * Return a pointer to the indirection array entry containing the
1608 * extent record for filesystem block bno. Store the index of the
1609 * target irec in *erp_idxp.
1611 xfs_ext_irec_t
* /* pointer to found extent record */
1612 xfs_iext_bno_to_irec(
1613 xfs_ifork_t
*ifp
, /* inode fork pointer */
1614 xfs_fileoff_t bno
, /* block number to search for */
1615 int *erp_idxp
) /* irec index of target ext list */
1617 xfs_ext_irec_t
*erp
= NULL
; /* indirection array pointer */
1618 xfs_ext_irec_t
*erp_next
; /* next indirection array entry */
1619 int erp_idx
; /* indirection array index */
1620 int nlists
; /* number of extent irec's (lists) */
1621 int high
; /* binary search upper limit */
1622 int low
; /* binary search lower limit */
1624 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1625 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1629 while (low
<= high
) {
1630 erp_idx
= (low
+ high
) >> 1;
1631 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1632 erp_next
= erp_idx
< nlists
- 1 ? erp
+ 1 : NULL
;
1633 if (bno
< xfs_bmbt_get_startoff(erp
->er_extbuf
)) {
1635 } else if (erp_next
&& bno
>=
1636 xfs_bmbt_get_startoff(erp_next
->er_extbuf
)) {
1642 *erp_idxp
= erp_idx
;
1647 * Return a pointer to the indirection array entry containing the
1648 * extent record at file extent index *idxp. Store the index of the
1649 * target irec in *erp_idxp and store the page index of the target
1650 * extent record in *idxp.
1653 xfs_iext_idx_to_irec(
1654 xfs_ifork_t
*ifp
, /* inode fork pointer */
1655 xfs_extnum_t
*idxp
, /* extent index (file -> page) */
1656 int *erp_idxp
, /* pointer to target irec */
1657 int realloc
) /* new bytes were just added */
1659 xfs_ext_irec_t
*prev
; /* pointer to previous irec */
1660 xfs_ext_irec_t
*erp
= NULL
; /* pointer to current irec */
1661 int erp_idx
; /* indirection array index */
1662 int nlists
; /* number of irec's (ex lists) */
1663 int high
; /* binary search upper limit */
1664 int low
; /* binary search lower limit */
1665 xfs_extnum_t page_idx
= *idxp
; /* extent index in target list */
1667 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1668 ASSERT(page_idx
>= 0);
1669 ASSERT(page_idx
<= ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
));
1670 ASSERT(page_idx
< ifp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
) || realloc
);
1672 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1677 /* Binary search extent irec's */
1678 while (low
<= high
) {
1679 erp_idx
= (low
+ high
) >> 1;
1680 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1681 prev
= erp_idx
> 0 ? erp
- 1 : NULL
;
1682 if (page_idx
< erp
->er_extoff
|| (page_idx
== erp
->er_extoff
&&
1683 realloc
&& prev
&& prev
->er_extcount
< XFS_LINEAR_EXTS
)) {
1685 } else if (page_idx
> erp
->er_extoff
+ erp
->er_extcount
||
1686 (page_idx
== erp
->er_extoff
+ erp
->er_extcount
&&
1689 } else if (page_idx
== erp
->er_extoff
+ erp
->er_extcount
&&
1690 erp
->er_extcount
== XFS_LINEAR_EXTS
) {
1694 erp
= erp_idx
< nlists
? erp
+ 1 : NULL
;
1697 page_idx
-= erp
->er_extoff
;
1702 *erp_idxp
= erp_idx
;
1707 * Allocate and initialize an indirection array once the space needed
1708 * for incore extents increases above XFS_IEXT_BUFSZ.
1712 xfs_ifork_t
*ifp
) /* inode fork pointer */
1714 xfs_ext_irec_t
*erp
; /* indirection array pointer */
1715 xfs_extnum_t nextents
; /* number of extents in file */
1717 ASSERT(!(ifp
->if_flags
& XFS_IFEXTIREC
));
1718 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1719 ASSERT(nextents
<= XFS_LINEAR_EXTS
);
1721 erp
= kmem_alloc(sizeof(xfs_ext_irec_t
), KM_NOFS
);
1723 if (nextents
== 0) {
1724 ifp
->if_u1
.if_extents
= kmem_alloc(XFS_IEXT_BUFSZ
, KM_NOFS
);
1725 } else if (!ifp
->if_real_bytes
) {
1726 xfs_iext_inline_to_direct(ifp
, XFS_IEXT_BUFSZ
);
1727 } else if (ifp
->if_real_bytes
< XFS_IEXT_BUFSZ
) {
1728 xfs_iext_realloc_direct(ifp
, XFS_IEXT_BUFSZ
);
1730 erp
->er_extbuf
= ifp
->if_u1
.if_extents
;
1731 erp
->er_extcount
= nextents
;
1734 ifp
->if_flags
|= XFS_IFEXTIREC
;
1735 ifp
->if_real_bytes
= XFS_IEXT_BUFSZ
;
1736 ifp
->if_bytes
= nextents
* sizeof(xfs_bmbt_rec_t
);
1737 ifp
->if_u1
.if_ext_irec
= erp
;
1743 * Allocate and initialize a new entry in the indirection array.
1747 xfs_ifork_t
*ifp
, /* inode fork pointer */
1748 int erp_idx
) /* index for new irec */
1750 xfs_ext_irec_t
*erp
; /* indirection array pointer */
1751 int i
; /* loop counter */
1752 int nlists
; /* number of irec's (ex lists) */
1754 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1755 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1757 /* Resize indirection array */
1758 xfs_iext_realloc_indirect(ifp
, ++nlists
*
1759 sizeof(xfs_ext_irec_t
));
1761 * Move records down in the array so the
1762 * new page can use erp_idx.
1764 erp
= ifp
->if_u1
.if_ext_irec
;
1765 for (i
= nlists
- 1; i
> erp_idx
; i
--) {
1766 memmove(&erp
[i
], &erp
[i
-1], sizeof(xfs_ext_irec_t
));
1768 ASSERT(i
== erp_idx
);
1770 /* Initialize new extent record */
1771 erp
= ifp
->if_u1
.if_ext_irec
;
1772 erp
[erp_idx
].er_extbuf
= kmem_alloc(XFS_IEXT_BUFSZ
, KM_NOFS
);
1773 ifp
->if_real_bytes
= nlists
* XFS_IEXT_BUFSZ
;
1774 memset(erp
[erp_idx
].er_extbuf
, 0, XFS_IEXT_BUFSZ
);
1775 erp
[erp_idx
].er_extcount
= 0;
1776 erp
[erp_idx
].er_extoff
= erp_idx
> 0 ?
1777 erp
[erp_idx
-1].er_extoff
+ erp
[erp_idx
-1].er_extcount
: 0;
1778 return (&erp
[erp_idx
]);
1782 * Remove a record from the indirection array.
1785 xfs_iext_irec_remove(
1786 xfs_ifork_t
*ifp
, /* inode fork pointer */
1787 int erp_idx
) /* irec index to remove */
1789 xfs_ext_irec_t
*erp
; /* indirection array pointer */
1790 int i
; /* loop counter */
1791 int nlists
; /* number of irec's (ex lists) */
1793 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1794 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1795 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1796 if (erp
->er_extbuf
) {
1797 xfs_iext_irec_update_extoffs(ifp
, erp_idx
+ 1,
1799 kmem_free(erp
->er_extbuf
);
1801 /* Compact extent records */
1802 erp
= ifp
->if_u1
.if_ext_irec
;
1803 for (i
= erp_idx
; i
< nlists
- 1; i
++) {
1804 memmove(&erp
[i
], &erp
[i
+1], sizeof(xfs_ext_irec_t
));
1807 * Manually free the last extent record from the indirection
1808 * array. A call to xfs_iext_realloc_indirect() with a size
1809 * of zero would result in a call to xfs_iext_destroy() which
1810 * would in turn call this function again, creating a nasty
1814 xfs_iext_realloc_indirect(ifp
,
1815 nlists
* sizeof(xfs_ext_irec_t
));
1817 kmem_free(ifp
->if_u1
.if_ext_irec
);
1819 ifp
->if_real_bytes
= nlists
* XFS_IEXT_BUFSZ
;
1823 * This is called to clean up large amounts of unused memory allocated
1824 * by the indirection array. Before compacting anything though, verify
1825 * that the indirection array is still needed and switch back to the
1826 * linear extent list (or even the inline buffer) if possible. The
1827 * compaction policy is as follows:
1829 * Full Compaction: Extents fit into a single page (or inline buffer)
1830 * Partial Compaction: Extents occupy less than 50% of allocated space
1831 * No Compaction: Extents occupy at least 50% of allocated space
1834 xfs_iext_irec_compact(
1835 xfs_ifork_t
*ifp
) /* inode fork pointer */
1837 xfs_extnum_t nextents
; /* number of extents in file */
1838 int nlists
; /* number of irec's (ex lists) */
1840 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1841 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1842 nextents
= ifp
->if_bytes
/ (uint
)sizeof(xfs_bmbt_rec_t
);
1844 if (nextents
== 0) {
1845 xfs_iext_destroy(ifp
);
1846 } else if (nextents
<= XFS_INLINE_EXTS
) {
1847 xfs_iext_indirect_to_direct(ifp
);
1848 xfs_iext_direct_to_inline(ifp
, nextents
);
1849 } else if (nextents
<= XFS_LINEAR_EXTS
) {
1850 xfs_iext_indirect_to_direct(ifp
);
1851 } else if (nextents
< (nlists
* XFS_LINEAR_EXTS
) >> 1) {
1852 xfs_iext_irec_compact_pages(ifp
);
1857 * Combine extents from neighboring extent pages.
1860 xfs_iext_irec_compact_pages(
1861 xfs_ifork_t
*ifp
) /* inode fork pointer */
1863 xfs_ext_irec_t
*erp
, *erp_next
;/* pointers to irec entries */
1864 int erp_idx
= 0; /* indirection array index */
1865 int nlists
; /* number of irec's (ex lists) */
1867 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1868 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1869 while (erp_idx
< nlists
- 1) {
1870 erp
= &ifp
->if_u1
.if_ext_irec
[erp_idx
];
1872 if (erp_next
->er_extcount
<=
1873 (XFS_LINEAR_EXTS
- erp
->er_extcount
)) {
1874 memcpy(&erp
->er_extbuf
[erp
->er_extcount
],
1875 erp_next
->er_extbuf
, erp_next
->er_extcount
*
1876 sizeof(xfs_bmbt_rec_t
));
1877 erp
->er_extcount
+= erp_next
->er_extcount
;
1879 * Free page before removing extent record
1880 * so er_extoffs don't get modified in
1881 * xfs_iext_irec_remove.
1883 kmem_free(erp_next
->er_extbuf
);
1884 erp_next
->er_extbuf
= NULL
;
1885 xfs_iext_irec_remove(ifp
, erp_idx
+ 1);
1886 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1894 * This is called to update the er_extoff field in the indirection
1895 * array when extents have been added or removed from one of the
1896 * extent lists. erp_idx contains the irec index to begin updating
1897 * at and ext_diff contains the number of extents that were added
1901 xfs_iext_irec_update_extoffs(
1902 xfs_ifork_t
*ifp
, /* inode fork pointer */
1903 int erp_idx
, /* irec index to update */
1904 int ext_diff
) /* number of new extents */
1906 int i
; /* loop counter */
1907 int nlists
; /* number of irec's (ex lists */
1909 ASSERT(ifp
->if_flags
& XFS_IFEXTIREC
);
1910 nlists
= ifp
->if_real_bytes
/ XFS_IEXT_BUFSZ
;
1911 for (i
= erp_idx
; i
< nlists
; i
++) {
1912 ifp
->if_u1
.if_ext_irec
[i
].er_extoff
+= ext_diff
;