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 Library
10 * General Public License, version 2.
23 #include "ext2_ext_attr.h"
27 #define NAME_HASH_SHIFT 5
28 #define VALUE_HASH_SHIFT 16
31 * ext2_xattr_hash_entry()
33 * Compute the hash of an extended attribute.
35 __u32 ext2fs_ext_attr_hash_entry(struct ext2_ext_attr_entry *entry, void *data)
38 char *name = ((char *) entry) + sizeof(struct ext2_ext_attr_entry);
41 for (n = 0; n < entry->e_name_len; n++) {
42 hash = (hash << NAME_HASH_SHIFT) ^
43 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
47 /* The hash needs to be calculated on the data in little-endian. */
48 if (entry->e_value_block == 0 && entry->e_value_size != 0) {
49 __u32 *value = (__u32 *)data;
50 for (n = (entry->e_value_size + EXT2_EXT_ATTR_ROUND) >>
51 EXT2_EXT_ATTR_PAD_BITS; n; n--) {
52 hash = (hash << VALUE_HASH_SHIFT) ^
53 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
54 ext2fs_le32_to_cpu(*value++);
61 #undef NAME_HASH_SHIFT
62 #undef VALUE_HASH_SHIFT
64 errcode_t ext2fs_read_ext_attr2(ext2_filsys fs, blk64_t block, void *buf)
68 retval = io_channel_read_blk64(fs->io, block, 1, buf);
71 #ifdef WORDS_BIGENDIAN
72 ext2fs_swap_ext_attr(buf, buf, fs->blocksize, 1);
77 errcode_t ext2fs_read_ext_attr(ext2_filsys fs, blk_t block, void *buf)
79 return ext2fs_read_ext_attr2(fs, block, buf);
82 errcode_t ext2fs_write_ext_attr2(ext2_filsys fs, blk64_t block, void *inbuf)
86 #ifdef WORDS_BIGENDIAN
89 retval = ext2fs_get_mem(fs->blocksize, &buf);
93 ext2fs_swap_ext_attr(buf, inbuf, fs->blocksize, 1);
95 write_buf = (char *) inbuf;
97 retval = io_channel_write_blk64(fs->io, block, 1, write_buf);
98 #ifdef WORDS_BIGENDIAN
99 ext2fs_free_mem(&buf);
102 ext2fs_mark_changed(fs);
106 errcode_t ext2fs_write_ext_attr(ext2_filsys fs, blk_t block, void *inbuf)
108 return ext2fs_write_ext_attr2(fs, block, inbuf);
112 * This function adjusts the reference count of the EA block.
114 errcode_t ext2fs_adjust_ea_refcount2(ext2_filsys fs, blk64_t blk,
115 char *block_buf, int adjust,
119 struct ext2_ext_attr_header *header;
122 if ((blk >= ext2fs_blocks_count(fs->super)) ||
123 (blk < fs->super->s_first_data_block))
124 return EXT2_ET_BAD_EA_BLOCK_NUM;
127 retval = ext2fs_get_mem(fs->blocksize, &buf);
133 retval = ext2fs_read_ext_attr2(fs, blk, block_buf);
137 header = (struct ext2_ext_attr_header *) block_buf;
138 header->h_refcount += adjust;
140 *newcount = header->h_refcount;
142 retval = ext2fs_write_ext_attr2(fs, blk, block_buf);
148 ext2fs_free_mem(&buf);
152 errcode_t ext2fs_adjust_ea_refcount(ext2_filsys fs, blk_t blk,
153 char *block_buf, int adjust,
156 return ext2fs_adjust_ea_refcount(fs, blk, block_buf, adjust, newcount);