blob: 17610a9149c9c14c9d8b8c5d0e7cb6bbe253e2fc [file] [log] [blame]
Mike Frysinger1fd98e02005-05-09 22:10:42 +00001/*
2 * ext_attr.c --- extended attribute blocks
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00003 *
Mike Frysinger1fd98e02005-05-09 22:10:42 +00004 * 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>
Mike Frysinger1fd98e02005-05-09 22:10:42 +000015#include <unistd.h>
Mike Frysinger1fd98e02005-05-09 22:10:42 +000016#include <string.h>
17#include <time.h>
18
19#include "ext2_fs.h"
20#include "ext2_ext_attr.h"
Mike Frysinger1fd98e02005-05-09 22:10:42 +000021#include "ext2fs.h"
22
23errcode_t ext2fs_read_ext_attr(ext2_filsys fs, blk_t block, void *buf)
24{
25 errcode_t retval;
26
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000027 retval = io_channel_read_blk(fs->io, block, 1, buf);
Mike Frysinger1fd98e02005-05-09 22:10:42 +000028 if (retval)
29 return retval;
Rob Landley7c94bed2006-05-03 21:58:45 +000030#if BB_BIG_ENDIAN
Mike Frysinger1fd98e02005-05-09 22:10:42 +000031 if ((fs->flags & (EXT2_FLAG_SWAP_BYTES|
32 EXT2_FLAG_SWAP_BYTES_READ)) != 0)
33 ext2fs_swap_ext_attr(buf, buf, fs->blocksize, 1);
34#endif
35 return 0;
36}
37
38errcode_t ext2fs_write_ext_attr(ext2_filsys fs, blk_t block, void *inbuf)
39{
40 errcode_t retval;
41 char *write_buf;
42 char *buf = NULL;
43
Rob Landley7c94bed2006-05-03 21:58:45 +000044#if BB_BIG_ENDIAN
Mike Frysinger1fd98e02005-05-09 22:10:42 +000045 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
46 (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE)) {
47 retval = ext2fs_get_mem(fs->blocksize, &buf);
48 if (retval)
49 return retval;
50 write_buf = buf;
51 ext2fs_swap_ext_attr(buf, inbuf, fs->blocksize, 1);
52 } else
53#endif
54 write_buf = (char *) inbuf;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000055 retval = io_channel_write_blk(fs->io, block, 1, write_buf);
Mike Frysinger1fd98e02005-05-09 22:10:42 +000056 if (buf)
57 ext2fs_free_mem(&buf);
58 if (!retval)
59 ext2fs_mark_changed(fs);
60 return retval;
61}
62
63/*
64 * This function adjusts the reference count of the EA block.
65 */
66errcode_t ext2fs_adjust_ea_refcount(ext2_filsys fs, blk_t blk,
67 char *block_buf, int adjust,
68 __u32 *newcount)
69{
70 errcode_t retval;
71 struct ext2_ext_attr_header *header;
72 char *buf = 0;
73
74 if ((blk >= fs->super->s_blocks_count) ||
75 (blk < fs->super->s_first_data_block))
76 return EXT2_ET_BAD_EA_BLOCK_NUM;
77
78 if (!block_buf) {
79 retval = ext2fs_get_mem(fs->blocksize, &buf);
80 if (retval)
81 return retval;
82 block_buf = buf;
83 }
84
85 retval = ext2fs_read_ext_attr(fs, blk, block_buf);
86 if (retval)
87 goto errout;
88
89 header = (struct ext2_ext_attr_header *) block_buf;
90 header->h_refcount += adjust;
91 if (newcount)
92 *newcount = header->h_refcount;
93
94 retval = ext2fs_write_ext_attr(fs, blk, block_buf);
95 if (retval)
96 goto errout;
97
98errout:
99 if (buf)
100 ext2fs_free_mem(&buf);
101 return retval;
102}