blob: 7ee41f234d0407b23a89a747e2ae02b343f78bce [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysinger1fd98e02005-05-09 22:10:42 +00002/*
3 * ext_attr.c --- extended attribute blocks
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00004 *
Mike Frysinger1fd98e02005-05-09 22:10:42 +00005 * Copyright (C) 2001 Andreas Gruenbacher, <a.gruenbacher@computer.org>
6 *
7 * Copyright (C) 2002 Theodore Ts'o.
8 *
9 * %Begin-Header%
10 * This file may be redistributed under the terms of the GNU Public
11 * License.
12 * %End-Header%
13 */
14
15#include <stdio.h>
Mike Frysinger1fd98e02005-05-09 22:10:42 +000016#include <unistd.h>
Mike Frysinger1fd98e02005-05-09 22:10:42 +000017#include <string.h>
18#include <time.h>
19
20#include "ext2_fs.h"
21#include "ext2_ext_attr.h"
Mike Frysinger1fd98e02005-05-09 22:10:42 +000022#include "ext2fs.h"
23
24errcode_t ext2fs_read_ext_attr(ext2_filsys fs, blk_t block, void *buf)
25{
26 errcode_t retval;
27
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000028 retval = io_channel_read_blk(fs->io, block, 1, buf);
Mike Frysinger1fd98e02005-05-09 22:10:42 +000029 if (retval)
30 return retval;
Rob Landley7c94bed2006-05-03 21:58:45 +000031#if BB_BIG_ENDIAN
Mike Frysinger1fd98e02005-05-09 22:10:42 +000032 if ((fs->flags & (EXT2_FLAG_SWAP_BYTES|
33 EXT2_FLAG_SWAP_BYTES_READ)) != 0)
34 ext2fs_swap_ext_attr(buf, buf, fs->blocksize, 1);
35#endif
36 return 0;
37}
38
39errcode_t ext2fs_write_ext_attr(ext2_filsys fs, blk_t block, void *inbuf)
40{
41 errcode_t retval;
42 char *write_buf;
43 char *buf = NULL;
44
Rob Landley11c7a7b2006-06-25 22:39:24 +000045 if (BB_BIG_ENDIAN && ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
46 (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE))) {
Mike Frysinger1fd98e02005-05-09 22:10:42 +000047 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
Mike Frysinger1fd98e02005-05-09 22:10:42 +000053 write_buf = (char *) inbuf;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000054 retval = io_channel_write_blk(fs->io, block, 1, write_buf);
Mike Frysinger1fd98e02005-05-09 22:10:42 +000055 if (buf)
56 ext2fs_free_mem(&buf);
57 if (!retval)
58 ext2fs_mark_changed(fs);
59 return retval;
60}
61
62/*
63 * This function adjusts the reference count of the EA block.
64 */
65errcode_t ext2fs_adjust_ea_refcount(ext2_filsys fs, blk_t blk,
66 char *block_buf, int adjust,
67 __u32 *newcount)
68{
69 errcode_t retval;
70 struct ext2_ext_attr_header *header;
71 char *buf = 0;
72
73 if ((blk >= fs->super->s_blocks_count) ||
74 (blk < fs->super->s_first_data_block))
75 return EXT2_ET_BAD_EA_BLOCK_NUM;
76
77 if (!block_buf) {
78 retval = ext2fs_get_mem(fs->blocksize, &buf);
79 if (retval)
80 return retval;
81 block_buf = buf;
82 }
83
84 retval = ext2fs_read_ext_attr(fs, blk, block_buf);
85 if (retval)
86 goto errout;
87
88 header = (struct ext2_ext_attr_header *) block_buf;
89 header->h_refcount += adjust;
90 if (newcount)
91 *newcount = header->h_refcount;
92
93 retval = ext2fs_write_ext_attr(fs, blk, block_buf);
94 if (retval)
95 goto errout;
96
97errout:
98 if (buf)
99 ext2fs_free_mem(&buf);
100 return retval;
101}