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 #define NAME_HASH_SHIFT 5
27 #define VALUE_HASH_SHIFT 16
30 * ext2_xattr_hash_entry()
32 * Compute the hash of an extended attribute.
34 __u32 ext2fs_ext_attr_hash_entry(struct ext2_ext_attr_entry *entry, void *data)
37 char *name = ((char *) entry) + sizeof(struct ext2_ext_attr_entry);
40 for (n = 0; n < entry->e_name_len; n++) {
41 hash = (hash << NAME_HASH_SHIFT) ^
42 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
46 /* The hash needs to be calculated on the data in little-endian. */
47 if (entry->e_value_block == 0 && entry->e_value_size != 0) {
48 __u32 *value = (__u32 *)data;
49 for (n = (entry->e_value_size + EXT2_EXT_ATTR_ROUND) >>
50 EXT2_EXT_ATTR_PAD_BITS; n; n--) {
51 hash = (hash << VALUE_HASH_SHIFT) ^
52 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
53 ext2fs_le32_to_cpu(*value++);
60 #undef NAME_HASH_SHIFT
61 #undef VALUE_HASH_SHIFT
63 errcode_t ext2fs_read_ext_attr(ext2_filsys fs, blk_t block, void *buf)
67 retval = io_channel_read_blk(fs->io, block, 1, buf);
70 #ifdef WORDS_BIGENDIAN
71 ext2fs_swap_ext_attr(buf, buf, fs->blocksize, 1);
76 errcode_t ext2fs_write_ext_attr(ext2_filsys fs, blk_t block, void *inbuf)
82 #ifdef WORDS_BIGENDIAN
83 retval = ext2fs_get_mem(fs->blocksize, &buf);
87 ext2fs_swap_ext_attr(buf, inbuf, fs->blocksize, 1);
89 write_buf = (char *) inbuf;
91 retval = io_channel_write_blk(fs->io, block, 1, write_buf);
93 ext2fs_free_mem(&buf);
95 ext2fs_mark_changed(fs);
100 * This function adjusts the reference count of the EA block.
102 errcode_t ext2fs_adjust_ea_refcount(ext2_filsys fs, blk_t blk,
103 char *block_buf, int adjust,
107 struct ext2_ext_attr_header *header;
110 if ((blk >= fs->super->s_blocks_count) ||
111 (blk < fs->super->s_first_data_block))
112 return EXT2_ET_BAD_EA_BLOCK_NUM;
115 retval = ext2fs_get_mem(fs->blocksize, &buf);
121 retval = ext2fs_read_ext_attr(fs, blk, block_buf);
125 header = (struct ext2_ext_attr_header *) block_buf;
126 header->h_refcount += adjust;
128 *newcount = header->h_refcount;
130 retval = ext2fs_write_ext_attr(fs, blk, block_buf);
136 ext2fs_free_mem(&buf);