2 * ext_attr.c --- extended attribute blocks
4 * Copyright (C) 2001 Andreas Gruenbacher, <a.gruenbacher@computer.org>
6 * Copyright (C) 2002 Theodore Ts'o.
9 * This file may be redistributed under the terms of the GNU Public
22 #include "ext2_ext_attr.h"
26 #ifdef EXT2FS_ENABLE_SWAPFS
27 void ext2fs_swap_ext_attr(ext2_filsys fs
, char *to
, char *from
)
29 struct ext2_ext_attr_header
*from_header
=
30 (struct ext2_ext_attr_header
*)from
;
31 struct ext2_ext_attr_header
*to_header
=
32 (struct ext2_ext_attr_header
*)to
;
33 struct ext2_ext_attr_entry
*from_entry
, *to_entry
;
34 char *from_end
= (char *)from_header
+ fs
->blocksize
;
37 if (to_header
!= from_header
)
38 memcpy(to_header
, from_header
, fs
->blocksize
);
40 to_header
->h_magic
= ext2fs_swab32(from_header
->h_magic
);
41 to_header
->h_blocks
= ext2fs_swab32(from_header
->h_blocks
);
42 to_header
->h_refcount
= ext2fs_swab32(from_header
->h_refcount
);
44 to_header
->h_reserved
[n
] =
45 ext2fs_swab32(from_header
->h_reserved
[n
]);
47 from_entry
= (struct ext2_ext_attr_entry
*)(from_header
+1);
48 to_entry
= (struct ext2_ext_attr_entry
*)(to_header
+1);
49 while ((char *)from_entry
< from_end
&& *(__u32
*)from_entry
) {
50 to_entry
->e_value_offs
=
51 ext2fs_swab16(from_entry
->e_value_offs
);
52 to_entry
->e_value_block
=
53 ext2fs_swab32(from_entry
->e_value_block
);
54 to_entry
->e_value_size
=
55 ext2fs_swab32(from_entry
->e_value_size
);
56 from_entry
= EXT2_EXT_ATTR_NEXT(from_entry
);
57 to_entry
= EXT2_EXT_ATTR_NEXT(to_entry
);
62 errcode_t
ext2fs_read_ext_attr(ext2_filsys fs
, blk_t block
, void *buf
)
66 retval
= io_channel_read_blk(fs
->io
, block
, 1, buf
);
69 #ifdef EXT2FS_ENABLE_SWAPFS
70 if ((fs
->flags
& (EXT2_FLAG_SWAP_BYTES
|
71 EXT2_FLAG_SWAP_BYTES_READ
)) != 0)
72 ext2fs_swap_ext_attr(fs
, buf
, buf
);
77 errcode_t
ext2fs_write_ext_attr(ext2_filsys fs
, blk_t block
, void *inbuf
)
83 #ifdef EXT2FS_ENABLE_SWAPFS
84 if ((fs
->flags
& EXT2_FLAG_SWAP_BYTES
) ||
85 (fs
->flags
& EXT2_FLAG_SWAP_BYTES_WRITE
)) {
86 retval
= ext2fs_get_mem(fs
->blocksize
, (void **) &buf
);
90 ext2fs_swap_ext_attr(fs
, buf
, inbuf
);
93 write_buf
= (char *) inbuf
;
94 retval
= io_channel_write_blk(fs
->io
, block
, 1, write_buf
);
96 ext2fs_free_mem((void **) &buf
);
98 ext2fs_mark_changed(fs
);
103 * This function adjusts the reference count of the EA block.
105 errcode_t
ext2fs_adjust_ea_refcount(ext2_filsys fs
, blk_t blk
,
106 char *block_buf
, int adjust
,
110 struct ext2_ext_attr_header
*header
;
113 if ((blk
>= fs
->super
->s_blocks_count
) ||
114 (blk
< fs
->super
->s_first_data_block
))
115 return EXT2_ET_BAD_EA_BLOCK_NUM
;
118 retval
= ext2fs_get_mem(fs
->blocksize
, (void **) &buf
);
124 retval
= ext2fs_read_ext_attr(fs
, blk
, block_buf
);
128 header
= (struct ext2_ext_attr_header
*) block_buf
;
129 header
->h_refcount
+= adjust
;
131 *newcount
= header
->h_refcount
;
133 retval
= ext2fs_write_ext_attr(fs
, blk
, block_buf
);
139 ext2fs_free_mem((void **) &buf
);