1 /*
2 * ext_attr.c --- extended attribute blocks
3 *
4 * Copyright (C) 2001 Andreas Gruenbacher, <a.gruenbacher@computer.org>
5 *
6 * Copyright (C) 2002 Theodore Ts'o.
7 *
8 * %Begin-Header%
9 * This file may be redistributed under the terms of the GNU Public
10 * License.
11 * %End-Header%
12 */
13
14 #include <stdio.h>
15 #if HAVE_UNISTD_H
16 #include <unistd.h>
17 #endif
18 #include <string.h>
19 #include <time.h>
20
21 #include "ext2_fs.h"
22 #include "ext2_ext_attr.h"
23
24 #include "ext2fs.h"
25
ext2fs_read_ext_attr(ext2_filsys fs,blk_t block,void * buf)26 errcode_t ext2fs_read_ext_attr(ext2_filsys fs, blk_t block, void *buf)
27 {
28 errcode_t retval;
29
30 retval = io_channel_read_blk(fs->io, block, 1, buf);
31 if (retval)
32 return retval;
33 #ifdef EXT2FS_ENABLE_SWAPFS
34 if ((fs->flags & (EXT2_FLAG_SWAP_BYTES|
35 EXT2_FLAG_SWAP_BYTES_READ)) != 0)
36 ext2fs_swap_ext_attr(buf, buf, fs->blocksize, 1);
37 #endif
38 return 0;
39 }
40
ext2fs_write_ext_attr(ext2_filsys fs,blk_t block,void * inbuf)41 errcode_t ext2fs_write_ext_attr(ext2_filsys fs, blk_t block, void *inbuf)
42 {
43 errcode_t retval;
44 char *write_buf;
45 char *buf = NULL;
46
47 #ifdef EXT2FS_ENABLE_SWAPFS
48 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
49 (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE)) {
50 retval = ext2fs_get_mem(fs->blocksize, &buf);
51 if (retval)
52 return retval;
53 write_buf = buf;
54 ext2fs_swap_ext_attr(buf, inbuf, fs->blocksize, 1);
55 } else
56 #endif
57 write_buf = (char *) inbuf;
58 retval = io_channel_write_blk(fs->io, block, 1, write_buf);
59 if (buf)
60 ext2fs_free_mem(&buf);
61 if (!retval)
62 ext2fs_mark_changed(fs);
63 return retval;
64 }
65
66 /*
67 * This function adjusts the reference count of the EA block.
68 */
ext2fs_adjust_ea_refcount(ext2_filsys fs,blk_t blk,char * block_buf,int adjust,__u32 * newcount)69 errcode_t ext2fs_adjust_ea_refcount(ext2_filsys fs, blk_t blk,
70 char *block_buf, int adjust,
71 __u32 *newcount)
72 {
73 errcode_t retval;
74 struct ext2_ext_attr_header *header;
75 char *buf = 0;
76
77 if ((blk >= fs->super->s_blocks_count) ||
78 (blk < fs->super->s_first_data_block))
79 return EXT2_ET_BAD_EA_BLOCK_NUM;
80
81 if (!block_buf) {
82 retval = ext2fs_get_mem(fs->blocksize, &buf);
83 if (retval)
84 return retval;
85 block_buf = buf;
86 }
87
88 retval = ext2fs_read_ext_attr(fs, blk, block_buf);
89 if (retval)
90 goto errout;
91
92 header = (struct ext2_ext_attr_header *) block_buf;
93 header->h_refcount += adjust;
94 if (newcount)
95 *newcount = header->h_refcount;
96
97 retval = ext2fs_write_ext_attr(fs, blk, block_buf);
98 if (retval)
99 goto errout;
100
101 errout:
102 if (buf)
103 ext2fs_free_mem(&buf);
104 return retval;
105 }
106