]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/xfs_inode_fork.c
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 "libxfs_priv.h"
20 #include "xfs_format.h"
21 #include "xfs_log_format.h"
22 #include "xfs_trans_resv.h"
23 #include "xfs_mount.h"
24 #include "xfs_inode.h"
25 #include "xfs_trans.h"
26 #include "xfs_btree.h"
27 #include "xfs_bmap_btree.h"
29 #include "xfs_trace.h"
30 #include "xfs_attr_sf.h"
31 #include "xfs_da_format.h"
32 #include "xfs_da_btree.h"
33 #include "xfs_dir2_priv.h"
36 kmem_zone_t
*xfs_ifork_zone
;
38 STATIC
int xfs_iformat_local(xfs_inode_t
*, xfs_dinode_t
*, int, int);
39 STATIC
int xfs_iformat_extents(xfs_inode_t
*, xfs_dinode_t
*, int);
40 STATIC
int xfs_iformat_btree(xfs_inode_t
*, xfs_dinode_t
*, int);
42 static inline dev_t
xfs_to_linux_dev_t(xfs_dev_t dev
)
44 return MKDEV(sysv_major(dev
) & 0x1ff, sysv_minor(dev
));
48 * Copy inode type and data and attr format specific information from the
49 * on-disk inode to the in-core inode and fork structures. For fifos, devices,
50 * and sockets this means set i_rdev to the proper value. For files,
51 * directories, and symlinks this means to bring in the in-line data or extent
52 * pointers as well as the attribute fork. For a fork in B-tree format, only
53 * the root is immediately brought in-core. The rest will be read in later when
54 * first referenced (see xfs_iread_extents()).
59 struct xfs_dinode
*dip
)
61 struct inode
*inode
= VFS_I(ip
);
62 struct xfs_attr_shortform
*atp
;
67 if (unlikely(be32_to_cpu(dip
->di_nextents
) +
68 be16_to_cpu(dip
->di_anextents
) >
69 be64_to_cpu(dip
->di_nblocks
))) {
71 "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.",
72 (unsigned long long)ip
->i_ino
,
73 (int)(be32_to_cpu(dip
->di_nextents
) +
74 be16_to_cpu(dip
->di_anextents
)),
76 be64_to_cpu(dip
->di_nblocks
));
77 XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW
,
82 if (unlikely(dip
->di_forkoff
> ip
->i_mount
->m_sb
.sb_inodesize
)) {
83 xfs_warn(ip
->i_mount
, "corrupt dinode %Lu, forkoff = 0x%x.",
84 (unsigned long long)ip
->i_ino
,
86 XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW
,
91 if (unlikely((ip
->i_d
.di_flags
& XFS_DIFLAG_REALTIME
) &&
92 !ip
->i_mount
->m_rtdev_targp
)) {
94 "corrupt dinode %Lu, has realtime flag set.",
96 XFS_CORRUPTION_ERROR("xfs_iformat(realtime)",
97 XFS_ERRLEVEL_LOW
, ip
->i_mount
, dip
);
101 if (unlikely(xfs_is_reflink_inode(ip
) && !S_ISREG(inode
->i_mode
))) {
102 xfs_warn(ip
->i_mount
,
103 "corrupt dinode %llu, wrong file type for reflink.",
105 XFS_CORRUPTION_ERROR("xfs_iformat(reflink)",
106 XFS_ERRLEVEL_LOW
, ip
->i_mount
, dip
);
107 return -EFSCORRUPTED
;
110 if (unlikely(xfs_is_reflink_inode(ip
) &&
111 (ip
->i_d
.di_flags
& XFS_DIFLAG_REALTIME
))) {
112 xfs_warn(ip
->i_mount
,
113 "corrupt dinode %llu, has reflink+realtime flag set.",
115 XFS_CORRUPTION_ERROR("xfs_iformat(reflink)",
116 XFS_ERRLEVEL_LOW
, ip
->i_mount
, dip
);
117 return -EFSCORRUPTED
;
120 switch (inode
->i_mode
& S_IFMT
) {
125 if (unlikely(dip
->di_format
!= XFS_DINODE_FMT_DEV
)) {
126 XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW
,
128 return -EFSCORRUPTED
;
131 inode
->i_rdev
= xfs_to_linux_dev_t(xfs_dinode_get_rdev(dip
));
137 switch (dip
->di_format
) {
138 case XFS_DINODE_FMT_LOCAL
:
140 * no local regular files yet
142 if (unlikely(S_ISREG(be16_to_cpu(dip
->di_mode
)))) {
143 xfs_warn(ip
->i_mount
,
144 "corrupt inode %Lu (local format for regular file).",
145 (unsigned long long) ip
->i_ino
);
146 XFS_CORRUPTION_ERROR("xfs_iformat(4)",
149 return -EFSCORRUPTED
;
152 di_size
= be64_to_cpu(dip
->di_size
);
153 if (unlikely(di_size
< 0 ||
154 di_size
> XFS_DFORK_DSIZE(dip
, ip
->i_mount
))) {
155 xfs_warn(ip
->i_mount
,
156 "corrupt inode %Lu (bad size %Ld for local inode).",
157 (unsigned long long) ip
->i_ino
,
158 (long long) di_size
);
159 XFS_CORRUPTION_ERROR("xfs_iformat(5)",
162 return -EFSCORRUPTED
;
166 error
= xfs_iformat_local(ip
, dip
, XFS_DATA_FORK
, size
);
168 case XFS_DINODE_FMT_EXTENTS
:
169 error
= xfs_iformat_extents(ip
, dip
, XFS_DATA_FORK
);
171 case XFS_DINODE_FMT_BTREE
:
172 error
= xfs_iformat_btree(ip
, dip
, XFS_DATA_FORK
);
175 XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW
,
177 return -EFSCORRUPTED
;
182 XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW
, ip
->i_mount
);
183 return -EFSCORRUPTED
;
188 /* Check inline dir contents. */
189 if (S_ISDIR(inode
->i_mode
) && dip
->di_format
== XFS_DINODE_FMT_LOCAL
) {
190 error
= xfs_dir2_sf_verify(ip
);
192 xfs_idestroy_fork(ip
, XFS_DATA_FORK
);
197 if (xfs_is_reflink_inode(ip
)) {
198 ASSERT(ip
->i_cowfp
== NULL
);
199 xfs_ifork_init_cow(ip
);
202 if (!XFS_DFORK_Q(dip
))
205 ASSERT(ip
->i_afp
== NULL
);
206 ip
->i_afp
= kmem_zone_zalloc(xfs_ifork_zone
, KM_SLEEP
| KM_NOFS
);
208 switch (dip
->di_aformat
) {
209 case XFS_DINODE_FMT_LOCAL
:
210 atp
= (xfs_attr_shortform_t
*)XFS_DFORK_APTR(dip
);
211 size
= be16_to_cpu(atp
->hdr
.totsize
);
213 if (unlikely(size
< sizeof(struct xfs_attr_sf_hdr
))) {
214 xfs_warn(ip
->i_mount
,
215 "corrupt inode %Lu (bad attr fork size %Ld).",
216 (unsigned long long) ip
->i_ino
,
218 XFS_CORRUPTION_ERROR("xfs_iformat(8)",
221 error
= -EFSCORRUPTED
;
225 error
= xfs_iformat_local(ip
, dip
, XFS_ATTR_FORK
, size
);
227 case XFS_DINODE_FMT_EXTENTS
:
228 error
= xfs_iformat_extents(ip
, dip
, XFS_ATTR_FORK
);
230 case XFS_DINODE_FMT_BTREE
:
231 error
= xfs_iformat_btree(ip
, dip
, XFS_ATTR_FORK
);
234 error
= -EFSCORRUPTED
;
238 kmem_zone_free(xfs_ifork_zone
, ip
->i_afp
);
241 kmem_zone_free(xfs_ifork_zone
, ip
->i_cowfp
);
243 xfs_idestroy_fork(ip
, XFS_DATA_FORK
);
250 struct xfs_inode
*ip
,
255 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
256 int mem_size
= size
, real_size
= 0;
260 * If we are using the local fork to store a symlink body we need to
261 * zero-terminate it so that we can pass it back to the VFS directly.
262 * Overallocate the in-memory fork by one for that and add a zero
263 * to terminate it below.
265 zero_terminate
= S_ISLNK(VFS_I(ip
)->i_mode
);
270 real_size
= roundup(mem_size
, 4);
271 ifp
->if_u1
.if_data
= kmem_alloc(real_size
, KM_SLEEP
| KM_NOFS
);
272 memcpy(ifp
->if_u1
.if_data
, data
, size
);
274 ifp
->if_u1
.if_data
[size
] = '\0';
276 ifp
->if_u1
.if_data
= NULL
;
279 ifp
->if_bytes
= size
;
280 ifp
->if_real_bytes
= real_size
;
281 ifp
->if_flags
&= ~(XFS_IFEXTENTS
| XFS_IFBROOT
);
282 ifp
->if_flags
|= XFS_IFINLINE
;
286 * The file is in-lined in the on-disk inode.
296 * If the size is unreasonable, then something
297 * is wrong and we just bail out rather than crash in
298 * kmem_alloc() or memcpy() below.
300 if (unlikely(size
> XFS_DFORK_SIZE(dip
, ip
->i_mount
, whichfork
))) {
301 xfs_warn(ip
->i_mount
,
302 "corrupt inode %Lu (bad size %d for local fork, size = %d).",
303 (unsigned long long) ip
->i_ino
, size
,
304 XFS_DFORK_SIZE(dip
, ip
->i_mount
, whichfork
));
305 XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW
,
307 return -EFSCORRUPTED
;
310 xfs_init_local_fork(ip
, whichfork
, XFS_DFORK_PTR(dip
, whichfork
), size
);
315 * The file consists of a set of extents all of which fit into the on-disk
320 struct xfs_inode
*ip
,
321 struct xfs_dinode
*dip
,
324 struct xfs_mount
*mp
= ip
->i_mount
;
325 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
326 int state
= xfs_bmap_fork_to_state(whichfork
);
327 int nex
= XFS_DFORK_NEXTENTS(dip
, whichfork
);
328 int size
= nex
* sizeof(xfs_bmbt_rec_t
);
329 struct xfs_iext_cursor icur
;
330 struct xfs_bmbt_rec
*dp
;
331 struct xfs_bmbt_irec
new;
335 * If the number of extents is unreasonable, then something is wrong and
336 * we just bail out rather than crash in kmem_alloc() or memcpy() below.
338 if (unlikely(size
< 0 || size
> XFS_DFORK_SIZE(dip
, mp
, whichfork
))) {
339 xfs_warn(ip
->i_mount
, "corrupt inode %Lu ((a)extents = %d).",
340 (unsigned long long) ip
->i_ino
, nex
);
341 XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW
,
343 return -EFSCORRUPTED
;
346 ifp
->if_real_bytes
= 0;
348 ifp
->if_u1
.if_root
= NULL
;
351 dp
= (xfs_bmbt_rec_t
*) XFS_DFORK_PTR(dip
, whichfork
);
353 xfs_iext_first(ifp
, &icur
);
354 for (i
= 0; i
< nex
; i
++, dp
++) {
355 if (!xfs_bmbt_validate_extent(mp
, whichfork
, dp
)) {
356 XFS_ERROR_REPORT("xfs_iformat_extents(2)",
357 XFS_ERRLEVEL_LOW
, mp
);
358 return -EFSCORRUPTED
;
361 xfs_bmbt_disk_get_all(dp
, &new);
362 xfs_iext_insert(ip
, &icur
, 1, &new, state
);
363 trace_xfs_read_extent(ip
, &icur
, state
, _THIS_IP_
);
364 xfs_iext_next(ifp
, &icur
);
367 ifp
->if_flags
|= XFS_IFEXTENTS
;
372 * The file has too many extents to fit into
373 * the inode, so they are in B-tree format.
374 * Allocate a buffer for the root of the B-tree
375 * and copy the root into it. The i_extents
376 * field will remain NULL until all of the
377 * extents are read in (when they are needed).
385 struct xfs_mount
*mp
= ip
->i_mount
;
386 xfs_bmdr_block_t
*dfp
;
393 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
394 dfp
= (xfs_bmdr_block_t
*)XFS_DFORK_PTR(dip
, whichfork
);
395 size
= XFS_BMAP_BROOT_SPACE(mp
, dfp
);
396 nrecs
= be16_to_cpu(dfp
->bb_numrecs
);
397 level
= be16_to_cpu(dfp
->bb_level
);
400 * blow out if -- fork has less extents than can fit in
401 * fork (fork shouldn't be a btree format), root btree
402 * block has more records than can fit into the fork,
403 * or the number of extents is greater than the number of
406 if (unlikely(XFS_IFORK_NEXTENTS(ip
, whichfork
) <=
407 XFS_IFORK_MAXEXT(ip
, whichfork
) ||
408 XFS_BMDR_SPACE_CALC(nrecs
) >
409 XFS_DFORK_SIZE(dip
, mp
, whichfork
) ||
410 XFS_IFORK_NEXTENTS(ip
, whichfork
) > ip
->i_d
.di_nblocks
) ||
411 level
== 0 || level
> XFS_BTREE_MAXLEVELS
) {
412 xfs_warn(mp
, "corrupt inode %Lu (btree).",
413 (unsigned long long) ip
->i_ino
);
414 XFS_CORRUPTION_ERROR("xfs_iformat_btree", XFS_ERRLEVEL_LOW
,
416 return -EFSCORRUPTED
;
419 ifp
->if_broot_bytes
= size
;
420 ifp
->if_broot
= kmem_alloc(size
, KM_SLEEP
| KM_NOFS
);
421 ASSERT(ifp
->if_broot
!= NULL
);
423 * Copy and convert from the on-disk structure
424 * to the in-memory structure.
426 xfs_bmdr_to_bmbt(ip
, dfp
, XFS_DFORK_SIZE(dip
, ip
->i_mount
, whichfork
),
427 ifp
->if_broot
, size
);
428 ifp
->if_flags
&= ~XFS_IFEXTENTS
;
429 ifp
->if_flags
|= XFS_IFBROOT
;
431 ifp
->if_real_bytes
= 0;
433 ifp
->if_u1
.if_root
= NULL
;
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
,
502 op
= (char *)XFS_BMAP_BROOT_PTR_ADDR(mp
, ifp
->if_broot
, 1,
503 ifp
->if_broot_bytes
);
504 np
= (char *)XFS_BMAP_BROOT_PTR_ADDR(mp
, ifp
->if_broot
, 1,
506 ifp
->if_broot_bytes
= (int)new_size
;
507 ASSERT(XFS_BMAP_BMDR_SPACE(ifp
->if_broot
) <=
508 XFS_IFORK_SIZE(ip
, whichfork
));
509 memmove(np
, op
, cur_max
* (uint
)sizeof(xfs_fsblock_t
));
514 * rec_diff is less than 0. In this case, we are shrinking the
515 * if_broot buffer. It must already exist. If we go to zero
516 * records, just get rid of the root and clear the status bit.
518 ASSERT((ifp
->if_broot
!= NULL
) && (ifp
->if_broot_bytes
> 0));
519 cur_max
= xfs_bmbt_maxrecs(mp
, ifp
->if_broot_bytes
, 0);
520 new_max
= cur_max
+ rec_diff
;
521 ASSERT(new_max
>= 0);
523 new_size
= XFS_BMAP_BROOT_SPACE_CALC(mp
, new_max
);
527 new_broot
= kmem_alloc(new_size
, KM_SLEEP
| KM_NOFS
);
529 * First copy over the btree block header.
531 memcpy(new_broot
, ifp
->if_broot
,
532 XFS_BMBT_BLOCK_LEN(ip
->i_mount
));
535 ifp
->if_flags
&= ~XFS_IFBROOT
;
539 * Only copy the records and pointers if there are any.
543 * First copy the records.
545 op
= (char *)XFS_BMBT_REC_ADDR(mp
, ifp
->if_broot
, 1);
546 np
= (char *)XFS_BMBT_REC_ADDR(mp
, new_broot
, 1);
547 memcpy(np
, op
, new_max
* (uint
)sizeof(xfs_bmbt_rec_t
));
550 * Then copy the pointers.
552 op
= (char *)XFS_BMAP_BROOT_PTR_ADDR(mp
, ifp
->if_broot
, 1,
553 ifp
->if_broot_bytes
);
554 np
= (char *)XFS_BMAP_BROOT_PTR_ADDR(mp
, new_broot
, 1,
556 memcpy(np
, op
, new_max
* (uint
)sizeof(xfs_fsblock_t
));
558 kmem_free(ifp
->if_broot
);
559 ifp
->if_broot
= new_broot
;
560 ifp
->if_broot_bytes
= (int)new_size
;
562 ASSERT(XFS_BMAP_BMDR_SPACE(ifp
->if_broot
) <=
563 XFS_IFORK_SIZE(ip
, whichfork
));
569 * This is called when the amount of space needed for if_data
570 * is increased or decreased. The change in size is indicated by
571 * the number of bytes that need to be added or deleted in the
572 * byte_diff parameter.
574 * If the amount of space needed has decreased below the size of the
575 * inline buffer, then switch to using the inline buffer. Otherwise,
576 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
579 * ip -- the inode whose if_data area is changing
580 * byte_diff -- the change in the number of bytes, positive or negative,
581 * requested for the if_data array.
593 if (byte_diff
== 0) {
597 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
598 new_size
= (int)ifp
->if_bytes
+ byte_diff
;
599 ASSERT(new_size
>= 0);
602 kmem_free(ifp
->if_u1
.if_data
);
603 ifp
->if_u1
.if_data
= NULL
;
607 * Stuck with malloc/realloc.
608 * For inline data, the underlying buffer must be
609 * a multiple of 4 bytes in size so that it can be
610 * logged and stay on word boundaries. We enforce
613 real_size
= roundup(new_size
, 4);
614 if (ifp
->if_u1
.if_data
== NULL
) {
615 ASSERT(ifp
->if_real_bytes
== 0);
616 ifp
->if_u1
.if_data
= kmem_alloc(real_size
,
620 * Only do the realloc if the underlying size
621 * is really changing.
623 if (ifp
->if_real_bytes
!= real_size
) {
625 kmem_realloc(ifp
->if_u1
.if_data
,
631 ifp
->if_real_bytes
= real_size
;
632 ifp
->if_bytes
= new_size
;
633 ASSERT(ifp
->if_bytes
<= XFS_IFORK_SIZE(ip
, whichfork
));
643 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
644 if (ifp
->if_broot
!= NULL
) {
645 kmem_free(ifp
->if_broot
);
646 ifp
->if_broot
= NULL
;
650 * If the format is local, then we can't have an extents
651 * array so just look for an inline data array. If we're
652 * not local then we may or may not have an extents list,
653 * so check and free it up if we do.
655 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
656 if (ifp
->if_u1
.if_data
!= NULL
) {
657 ASSERT(ifp
->if_real_bytes
!= 0);
658 kmem_free(ifp
->if_u1
.if_data
);
659 ifp
->if_u1
.if_data
= NULL
;
660 ifp
->if_real_bytes
= 0;
662 } else if ((ifp
->if_flags
& XFS_IFEXTENTS
) && ifp
->if_height
) {
663 xfs_iext_destroy(ifp
);
666 ASSERT(ifp
->if_real_bytes
== 0);
668 if (whichfork
== XFS_ATTR_FORK
) {
669 kmem_zone_free(xfs_ifork_zone
, ip
->i_afp
);
671 } else if (whichfork
== XFS_COW_FORK
) {
672 kmem_zone_free(xfs_ifork_zone
, ip
->i_cowfp
);
678 * Convert in-core extents to on-disk form
680 * In the case of the data fork, the in-core and on-disk fork sizes can be
681 * different due to delayed allocation extents. We only copy on-disk extents
682 * here, so callers must always use the physical fork size to determine the
683 * size of the buffer passed to this routine. We will return the size actually
688 struct xfs_inode
*ip
,
689 struct xfs_bmbt_rec
*dp
,
692 int state
= xfs_bmap_fork_to_state(whichfork
);
693 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
694 struct xfs_iext_cursor icur
;
695 struct xfs_bmbt_irec rec
;
698 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
| XFS_ILOCK_SHARED
));
699 ASSERT(ifp
->if_bytes
> 0);
701 for_each_xfs_iext(ifp
, &icur
, &rec
) {
702 if (isnullstartblock(rec
.br_startblock
))
704 xfs_bmbt_disk_set_all(dp
, &rec
);
705 trace_xfs_write_extent(ip
, &icur
, state
, _RET_IP_
);
706 ASSERT(xfs_bmbt_validate_extent(ip
->i_mount
, whichfork
, dp
));
707 copied
+= sizeof(struct xfs_bmbt_rec
);
712 ASSERT(copied
<= ifp
->if_bytes
);
717 * Each of the following cases stores data into the same region
718 * of the on-disk inode, so only one of them can be valid at
719 * any given time. While it is possible to have conflicting formats
720 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
721 * in EXTENTS format, this can only happen when the fork has
722 * changed formats after being modified but before being flushed.
723 * In these cases, the format always takes precedence, because the
724 * format indicates the current state of the fork.
730 xfs_inode_log_item_t
*iip
,
736 static const short brootflag
[2] =
737 { XFS_ILOG_DBROOT
, XFS_ILOG_ABROOT
};
738 static const short dataflag
[2] =
739 { XFS_ILOG_DDATA
, XFS_ILOG_ADATA
};
740 static const short extflag
[2] =
741 { XFS_ILOG_DEXT
, XFS_ILOG_AEXT
};
745 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
747 * This can happen if we gave up in iformat in an error path,
748 * for the attribute fork.
751 ASSERT(whichfork
== XFS_ATTR_FORK
);
754 cp
= XFS_DFORK_PTR(dip
, whichfork
);
756 switch (XFS_IFORK_FORMAT(ip
, whichfork
)) {
757 case XFS_DINODE_FMT_LOCAL
:
758 if ((iip
->ili_fields
& dataflag
[whichfork
]) &&
759 (ifp
->if_bytes
> 0)) {
760 ASSERT(ifp
->if_u1
.if_data
!= NULL
);
761 ASSERT(ifp
->if_bytes
<= XFS_IFORK_SIZE(ip
, whichfork
));
762 memcpy(cp
, ifp
->if_u1
.if_data
, ifp
->if_bytes
);
766 case XFS_DINODE_FMT_EXTENTS
:
767 ASSERT((ifp
->if_flags
& XFS_IFEXTENTS
) ||
768 !(iip
->ili_fields
& extflag
[whichfork
]));
769 if ((iip
->ili_fields
& extflag
[whichfork
]) &&
770 (ifp
->if_bytes
> 0)) {
771 ASSERT(XFS_IFORK_NEXTENTS(ip
, whichfork
) > 0);
772 (void)xfs_iextents_copy(ip
, (xfs_bmbt_rec_t
*)cp
,
777 case XFS_DINODE_FMT_BTREE
:
778 if ((iip
->ili_fields
& brootflag
[whichfork
]) &&
779 (ifp
->if_broot_bytes
> 0)) {
780 ASSERT(ifp
->if_broot
!= NULL
);
781 ASSERT(XFS_BMAP_BMDR_SPACE(ifp
->if_broot
) <=
782 XFS_IFORK_SIZE(ip
, whichfork
));
783 xfs_bmbt_to_bmdr(mp
, ifp
->if_broot
, ifp
->if_broot_bytes
,
784 (xfs_bmdr_block_t
*)cp
,
785 XFS_DFORK_SIZE(dip
, mp
, whichfork
));
789 case XFS_DINODE_FMT_DEV
:
790 if (iip
->ili_fields
& XFS_ILOG_DEV
) {
791 ASSERT(whichfork
== XFS_DATA_FORK
);
792 xfs_dinode_put_rdev(dip
, sysv_encode_dev(VFS_I(ip
)->i_rdev
));
802 /* Convert bmap state flags to an inode fork. */
804 xfs_iext_state_to_fork(
805 struct xfs_inode
*ip
,
808 if (state
& BMAP_COWFORK
)
810 else if (state
& BMAP_ATTRFORK
)
816 * Initialize an inode's copy-on-write fork.
820 struct xfs_inode
*ip
)
825 ip
->i_cowfp
= kmem_zone_zalloc(xfs_ifork_zone
,
827 ip
->i_cowfp
->if_flags
= XFS_IFEXTENTS
;
828 ip
->i_cformat
= XFS_DINODE_FMT_EXTENTS
;