linux/fs/ext3/xattr.c
<<
>>
Prefs
   1/*
   2 * linux/fs/ext3/xattr.c
   3 *
   4 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
   5 *
   6 * Fix by Harrison Xing <harrison@mountainviewdata.com>.
   7 * Ext3 code with a lot of help from Eric Jarman <ejarman@acm.org>.
   8 * Extended attributes for symlinks and special files added per
   9 *  suggestion of Luka Renko <luka.renko@hermes.si>.
  10 * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
  11 *  Red Hat Inc.
  12 * ea-in-inode support by Alex Tomas <alex@clusterfs.com> aka bzzz
  13 *  and Andreas Gruenbacher <agruen@suse.de>.
  14 */
  15
  16/*
  17 * Extended attributes are stored directly in inodes (on file systems with
  18 * inodes bigger than 128 bytes) and on additional disk blocks. The i_file_acl
  19 * field contains the block number if an inode uses an additional block. All
  20 * attributes must fit in the inode and one additional block. Blocks that
  21 * contain the identical set of attributes may be shared among several inodes.
  22 * Identical blocks are detected by keeping a cache of blocks that have
  23 * recently been accessed.
  24 *
  25 * The attributes in inodes and on blocks have a different header; the entries
  26 * are stored in the same format:
  27 *
  28 *   +------------------+
  29 *   | header           |
  30 *   | entry 1          | |
  31 *   | entry 2          | | growing downwards
  32 *   | entry 3          | v
  33 *   | four null bytes  |
  34 *   | . . .            |
  35 *   | value 1          | ^
  36 *   | value 3          | | growing upwards
  37 *   | value 2          | |
  38 *   +------------------+
  39 *
  40 * The header is followed by multiple entry descriptors. In disk blocks, the
  41 * entry descriptors are kept sorted. In inodes, they are unsorted. The
  42 * attribute values are aligned to the end of the block in no specific order.
  43 *
  44 * Locking strategy
  45 * ----------------
  46 * EXT3_I(inode)->i_file_acl is protected by EXT3_I(inode)->xattr_sem.
  47 * EA blocks are only changed if they are exclusive to an inode, so
  48 * holding xattr_sem also means that nothing but the EA block's reference
  49 * count can change. Multiple writers to the same block are synchronized
  50 * by the buffer lock.
  51 */
  52
  53#include <linux/init.h>
  54#include <linux/fs.h>
  55#include <linux/slab.h>
  56#include <linux/ext3_jbd.h>
  57#include <linux/ext3_fs.h>
  58#include <linux/mbcache.h>
  59#include <linux/quotaops.h>
  60#include <linux/rwsem.h>
  61#include "xattr.h"
  62#include "acl.h"
  63
  64#define BHDR(bh) ((struct ext3_xattr_header *)((bh)->b_data))
  65#define ENTRY(ptr) ((struct ext3_xattr_entry *)(ptr))
  66#define BFIRST(bh) ENTRY(BHDR(bh)+1)
  67#define IS_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
  68
  69#define IHDR(inode, raw_inode) \
  70        ((struct ext3_xattr_ibody_header *) \
  71                ((void *)raw_inode + \
  72                 EXT3_GOOD_OLD_INODE_SIZE + \
  73                 EXT3_I(inode)->i_extra_isize))
  74#define IFIRST(hdr) ((struct ext3_xattr_entry *)((hdr)+1))
  75
  76#ifdef EXT3_XATTR_DEBUG
  77# define ea_idebug(inode, f...) do { \
  78                printk(KERN_DEBUG "inode %s:%lu: ", \
  79                        inode->i_sb->s_id, inode->i_ino); \
  80                printk(f); \
  81                printk("\n"); \
  82        } while (0)
  83# define ea_bdebug(bh, f...) do { \
  84                char b[BDEVNAME_SIZE]; \
  85                printk(KERN_DEBUG "block %s:%lu: ", \
  86                        bdevname(bh->b_bdev, b), \
  87                        (unsigned long) bh->b_blocknr); \
  88                printk(f); \
  89                printk("\n"); \
  90        } while (0)
  91#else
  92# define ea_idebug(f...)
  93# define ea_bdebug(f...)
  94#endif
  95
  96static void ext3_xattr_cache_insert(struct buffer_head *);
  97static struct buffer_head *ext3_xattr_cache_find(struct inode *,
  98                                                 struct ext3_xattr_header *,
  99                                                 struct mb_cache_entry **);
 100static void ext3_xattr_rehash(struct ext3_xattr_header *,
 101                              struct ext3_xattr_entry *);
 102static int ext3_xattr_list(struct dentry *dentry, char *buffer,
 103                           size_t buffer_size);
 104
 105static struct mb_cache *ext3_xattr_cache;
 106
 107static const struct xattr_handler *ext3_xattr_handler_map[] = {
 108        [EXT3_XATTR_INDEX_USER]              = &ext3_xattr_user_handler,
 109#ifdef CONFIG_EXT3_FS_POSIX_ACL
 110        [EXT3_XATTR_INDEX_POSIX_ACL_ACCESS]  = &ext3_xattr_acl_access_handler,
 111        [EXT3_XATTR_INDEX_POSIX_ACL_DEFAULT] = &ext3_xattr_acl_default_handler,
 112#endif
 113        [EXT3_XATTR_INDEX_TRUSTED]           = &ext3_xattr_trusted_handler,
 114#ifdef CONFIG_EXT3_FS_SECURITY
 115        [EXT3_XATTR_INDEX_SECURITY]          = &ext3_xattr_security_handler,
 116#endif
 117};
 118
 119const struct xattr_handler *ext3_xattr_handlers[] = {
 120        &ext3_xattr_user_handler,
 121        &ext3_xattr_trusted_handler,
 122#ifdef CONFIG_EXT3_FS_POSIX_ACL
 123        &ext3_xattr_acl_access_handler,
 124        &ext3_xattr_acl_default_handler,
 125#endif
 126#ifdef CONFIG_EXT3_FS_SECURITY
 127        &ext3_xattr_security_handler,
 128#endif
 129        NULL
 130};
 131
 132static inline const struct xattr_handler *
 133ext3_xattr_handler(int name_index)
 134{
 135        const struct xattr_handler *handler = NULL;
 136
 137        if (name_index > 0 && name_index < ARRAY_SIZE(ext3_xattr_handler_map))
 138                handler = ext3_xattr_handler_map[name_index];
 139        return handler;
 140}
 141
 142/*
 143 * Inode operation listxattr()
 144 *
 145 * dentry->d_inode->i_mutex: don't care
 146 */
 147ssize_t
 148ext3_listxattr(struct dentry *dentry, char *buffer, size_t size)
 149{
 150        return ext3_xattr_list(dentry, buffer, size);
 151}
 152
 153static int
 154ext3_xattr_check_names(struct ext3_xattr_entry *entry, void *end)
 155{
 156        while (!IS_LAST_ENTRY(entry)) {
 157                struct ext3_xattr_entry *next = EXT3_XATTR_NEXT(entry);
 158                if ((void *)next >= end)
 159                        return -EIO;
 160                entry = next;
 161        }
 162        return 0;
 163}
 164
 165static inline int
 166ext3_xattr_check_block(struct buffer_head *bh)
 167{
 168        int error;
 169
 170        if (BHDR(bh)->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC) ||
 171            BHDR(bh)->h_blocks != cpu_to_le32(1))
 172                return -EIO;
 173        error = ext3_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size);
 174        return error;
 175}
 176
 177static inline int
 178ext3_xattr_check_entry(struct ext3_xattr_entry *entry, size_t size)
 179{
 180        size_t value_size = le32_to_cpu(entry->e_value_size);
 181
 182        if (entry->e_value_block != 0 || value_size > size ||
 183            le16_to_cpu(entry->e_value_offs) + value_size > size)
 184                return -EIO;
 185        return 0;
 186}
 187
 188static int
 189ext3_xattr_find_entry(struct ext3_xattr_entry **pentry, int name_index,
 190                      const char *name, size_t size, int sorted)
 191{
 192        struct ext3_xattr_entry *entry;
 193        size_t name_len;
 194        int cmp = 1;
 195
 196        if (name == NULL)
 197                return -EINVAL;
 198        name_len = strlen(name);
 199        entry = *pentry;
 200        for (; !IS_LAST_ENTRY(entry); entry = EXT3_XATTR_NEXT(entry)) {
 201                cmp = name_index - entry->e_name_index;
 202                if (!cmp)
 203                        cmp = name_len - entry->e_name_len;
 204                if (!cmp)
 205                        cmp = memcmp(name, entry->e_name, name_len);
 206                if (cmp <= 0 && (sorted || cmp == 0))
 207                        break;
 208        }
 209        *pentry = entry;
 210        if (!cmp && ext3_xattr_check_entry(entry, size))
 211                        return -EIO;
 212        return cmp ? -ENODATA : 0;
 213}
 214
 215static int
 216ext3_xattr_block_get(struct inode *inode, int name_index, const char *name,
 217                     void *buffer, size_t buffer_size)
 218{
 219        struct buffer_head *bh = NULL;
 220        struct ext3_xattr_entry *entry;
 221        size_t size;
 222        int error;
 223
 224        ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
 225                  name_index, name, buffer, (long)buffer_size);
 226
 227        error = -ENODATA;
 228        if (!EXT3_I(inode)->i_file_acl)
 229                goto cleanup;
 230        ea_idebug(inode, "reading block %u", EXT3_I(inode)->i_file_acl);
 231        bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
 232        if (!bh)
 233                goto cleanup;
 234        ea_bdebug(bh, "b_count=%d, refcount=%d",
 235                atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
 236        if (ext3_xattr_check_block(bh)) {
 237bad_block:      ext3_error(inode->i_sb, __func__,
 238                           "inode %lu: bad block "E3FSBLK, inode->i_ino,
 239                           EXT3_I(inode)->i_file_acl);
 240                error = -EIO;
 241                goto cleanup;
 242        }
 243        ext3_xattr_cache_insert(bh);
 244        entry = BFIRST(bh);
 245        error = ext3_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
 246        if (error == -EIO)
 247                goto bad_block;
 248        if (error)
 249                goto cleanup;
 250        size = le32_to_cpu(entry->e_value_size);
 251        if (buffer) {
 252                error = -ERANGE;
 253                if (size > buffer_size)
 254                        goto cleanup;
 255                memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
 256                       size);
 257        }
 258        error = size;
 259
 260cleanup:
 261        brelse(bh);
 262        return error;
 263}
 264
 265static int
 266ext3_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
 267                     void *buffer, size_t buffer_size)
 268{
 269        struct ext3_xattr_ibody_header *header;
 270        struct ext3_xattr_entry *entry;
 271        struct ext3_inode *raw_inode;
 272        struct ext3_iloc iloc;
 273        size_t size;
 274        void *end;
 275        int error;
 276
 277        if (!ext3_test_inode_state(inode, EXT3_STATE_XATTR))
 278                return -ENODATA;
 279        error = ext3_get_inode_loc(inode, &iloc);
 280        if (error)
 281                return error;
 282        raw_inode = ext3_raw_inode(&iloc);
 283        header = IHDR(inode, raw_inode);
 284        entry = IFIRST(header);
 285        end = (void *)raw_inode + EXT3_SB(inode->i_sb)->s_inode_size;
 286        error = ext3_xattr_check_names(entry, end);
 287        if (error)
 288                goto cleanup;
 289        error = ext3_xattr_find_entry(&entry, name_index, name,
 290                                      end - (void *)entry, 0);
 291        if (error)
 292                goto cleanup;
 293        size = le32_to_cpu(entry->e_value_size);
 294        if (buffer) {
 295                error = -ERANGE;
 296                if (size > buffer_size)
 297                        goto cleanup;
 298                memcpy(buffer, (void *)IFIRST(header) +
 299                       le16_to_cpu(entry->e_value_offs), size);
 300        }
 301        error = size;
 302
 303cleanup:
 304        brelse(iloc.bh);
 305        return error;
 306}
 307
 308/*
 309 * ext3_xattr_get()
 310 *
 311 * Copy an extended attribute into the buffer
 312 * provided, or compute the buffer size required.
 313 * Buffer is NULL to compute the size of the buffer required.
 314 *
 315 * Returns a negative error number on failure, or the number of bytes
 316 * used / required on success.
 317 */
 318int
 319ext3_xattr_get(struct inode *inode, int name_index, const char *name,
 320               void *buffer, size_t buffer_size)
 321{
 322        int error;
 323
 324        down_read(&EXT3_I(inode)->xattr_sem);
 325        error = ext3_xattr_ibody_get(inode, name_index, name, buffer,
 326                                     buffer_size);
 327        if (error == -ENODATA)
 328                error = ext3_xattr_block_get(inode, name_index, name, buffer,
 329                                             buffer_size);
 330        up_read(&EXT3_I(inode)->xattr_sem);
 331        return error;
 332}
 333
 334static int
 335ext3_xattr_list_entries(struct dentry *dentry, struct ext3_xattr_entry *entry,
 336                        char *buffer, size_t buffer_size)
 337{
 338        size_t rest = buffer_size;
 339
 340        for (; !IS_LAST_ENTRY(entry); entry = EXT3_XATTR_NEXT(entry)) {
 341                const struct xattr_handler *handler =
 342                        ext3_xattr_handler(entry->e_name_index);
 343
 344                if (handler) {
 345                        size_t size = handler->list(dentry, buffer, rest,
 346                                                    entry->e_name,
 347                                                    entry->e_name_len,
 348                                                    handler->flags);
 349                        if (buffer) {
 350                                if (size > rest)
 351                                        return -ERANGE;
 352                                buffer += size;
 353                        }
 354                        rest -= size;
 355                }
 356        }
 357        return buffer_size - rest;
 358}
 359
 360static int
 361ext3_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
 362{
 363        struct inode *inode = dentry->d_inode;
 364        struct buffer_head *bh = NULL;
 365        int error;
 366
 367        ea_idebug(inode, "buffer=%p, buffer_size=%ld",
 368                  buffer, (long)buffer_size);
 369
 370        error = 0;
 371        if (!EXT3_I(inode)->i_file_acl)
 372                goto cleanup;
 373        ea_idebug(inode, "reading block %u", EXT3_I(inode)->i_file_acl);
 374        bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
 375        error = -EIO;
 376        if (!bh)
 377                goto cleanup;
 378        ea_bdebug(bh, "b_count=%d, refcount=%d",
 379                atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
 380        if (ext3_xattr_check_block(bh)) {
 381                ext3_error(inode->i_sb, __func__,
 382                           "inode %lu: bad block "E3FSBLK, inode->i_ino,
 383                           EXT3_I(inode)->i_file_acl);
 384                error = -EIO;
 385                goto cleanup;
 386        }
 387        ext3_xattr_cache_insert(bh);
 388        error = ext3_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
 389
 390cleanup:
 391        brelse(bh);
 392
 393        return error;
 394}
 395
 396static int
 397ext3_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
 398{
 399        struct inode *inode = dentry->d_inode;
 400        struct ext3_xattr_ibody_header *header;
 401        struct ext3_inode *raw_inode;
 402        struct ext3_iloc iloc;
 403        void *end;
 404        int error;
 405
 406        if (!ext3_test_inode_state(inode, EXT3_STATE_XATTR))
 407                return 0;
 408        error = ext3_get_inode_loc(inode, &iloc);
 409        if (error)
 410                return error;
 411        raw_inode = ext3_raw_inode(&iloc);
 412        header = IHDR(inode, raw_inode);
 413        end = (void *)raw_inode + EXT3_SB(inode->i_sb)->s_inode_size;
 414        error = ext3_xattr_check_names(IFIRST(header), end);
 415        if (error)
 416                goto cleanup;
 417        error = ext3_xattr_list_entries(dentry, IFIRST(header),
 418                                        buffer, buffer_size);
 419
 420cleanup:
 421        brelse(iloc.bh);
 422        return error;
 423}
 424
 425/*
 426 * ext3_xattr_list()
 427 *
 428 * Copy a list of attribute names into the buffer
 429 * provided, or compute the buffer size required.
 430 * Buffer is NULL to compute the size of the buffer required.
 431 *
 432 * Returns a negative error number on failure, or the number of bytes
 433 * used / required on success.
 434 */
 435static int
 436ext3_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
 437{
 438        int i_error, b_error;
 439
 440        down_read(&EXT3_I(dentry->d_inode)->xattr_sem);
 441        i_error = ext3_xattr_ibody_list(dentry, buffer, buffer_size);
 442        if (i_error < 0) {
 443                b_error = 0;
 444        } else {
 445                if (buffer) {
 446                        buffer += i_error;
 447                        buffer_size -= i_error;
 448                }
 449                b_error = ext3_xattr_block_list(dentry, buffer, buffer_size);
 450                if (b_error < 0)
 451                        i_error = 0;
 452        }
 453        up_read(&EXT3_I(dentry->d_inode)->xattr_sem);
 454        return i_error + b_error;
 455}
 456
 457/*
 458 * If the EXT3_FEATURE_COMPAT_EXT_ATTR feature of this file system is
 459 * not set, set it.
 460 */
 461static void ext3_xattr_update_super_block(handle_t *handle,
 462                                          struct super_block *sb)
 463{
 464        if (EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_EXT_ATTR))
 465                return;
 466
 467        if (ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh) == 0) {
 468                EXT3_SET_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_EXT_ATTR);
 469                ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
 470        }
 471}
 472
 473/*
 474 * Release the xattr block BH: If the reference count is > 1, decrement
 475 * it; otherwise free the block.
 476 */
 477static void
 478ext3_xattr_release_block(handle_t *handle, struct inode *inode,
 479                         struct buffer_head *bh)
 480{
 481        struct mb_cache_entry *ce = NULL;
 482        int error = 0;
 483
 484        ce = mb_cache_entry_get(ext3_xattr_cache, bh->b_bdev, bh->b_blocknr);
 485        error = ext3_journal_get_write_access(handle, bh);
 486        if (error)
 487                 goto out;
 488
 489        lock_buffer(bh);
 490
 491        if (BHDR(bh)->h_refcount == cpu_to_le32(1)) {
 492                ea_bdebug(bh, "refcount now=0; freeing");
 493                if (ce)
 494                        mb_cache_entry_free(ce);
 495                ext3_free_blocks(handle, inode, bh->b_blocknr, 1);
 496                get_bh(bh);
 497                ext3_forget(handle, 1, inode, bh, bh->b_blocknr);
 498        } else {
 499                le32_add_cpu(&BHDR(bh)->h_refcount, -1);
 500                error = ext3_journal_dirty_metadata(handle, bh);
 501                if (IS_SYNC(inode))
 502                        handle->h_sync = 1;
 503                dquot_free_block(inode, 1);
 504                ea_bdebug(bh, "refcount now=%d; releasing",
 505                          le32_to_cpu(BHDR(bh)->h_refcount));
 506                if (ce)
 507                        mb_cache_entry_release(ce);
 508        }
 509        unlock_buffer(bh);
 510out:
 511        ext3_std_error(inode->i_sb, error);
 512        return;
 513}
 514
 515struct ext3_xattr_info {
 516        int name_index;
 517        const char *name;
 518        const void *value;
 519        size_t value_len;
 520};
 521
 522struct ext3_xattr_search {
 523        struct ext3_xattr_entry *first;
 524        void *base;
 525        void *end;
 526        struct ext3_xattr_entry *here;
 527        int not_found;
 528};
 529
 530static int
 531ext3_xattr_set_entry(struct ext3_xattr_info *i, struct ext3_xattr_search *s)
 532{
 533        struct ext3_xattr_entry *last;
 534        size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
 535
 536        /* Compute min_offs and last. */
 537        last = s->first;
 538        for (; !IS_LAST_ENTRY(last); last = EXT3_XATTR_NEXT(last)) {
 539                if (!last->e_value_block && last->e_value_size) {
 540                        size_t offs = le16_to_cpu(last->e_value_offs);
 541                        if (offs < min_offs)
 542                                min_offs = offs;
 543                }
 544        }
 545        free = min_offs - ((void *)last - s->base) - sizeof(__u32);
 546        if (!s->not_found) {
 547                if (!s->here->e_value_block && s->here->e_value_size) {
 548                        size_t size = le32_to_cpu(s->here->e_value_size);
 549                        free += EXT3_XATTR_SIZE(size);
 550                }
 551                free += EXT3_XATTR_LEN(name_len);
 552        }
 553        if (i->value) {
 554                if (free < EXT3_XATTR_SIZE(i->value_len) ||
 555                    free < EXT3_XATTR_LEN(name_len) +
 556                           EXT3_XATTR_SIZE(i->value_len))
 557                        return -ENOSPC;
 558        }
 559
 560        if (i->value && s->not_found) {
 561                /* Insert the new name. */
 562                size_t size = EXT3_XATTR_LEN(name_len);
 563                size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
 564                memmove((void *)s->here + size, s->here, rest);
 565                memset(s->here, 0, size);
 566                s->here->e_name_index = i->name_index;
 567                s->here->e_name_len = name_len;
 568                memcpy(s->here->e_name, i->name, name_len);
 569        } else {
 570                if (!s->here->e_value_block && s->here->e_value_size) {
 571                        void *first_val = s->base + min_offs;
 572                        size_t offs = le16_to_cpu(s->here->e_value_offs);
 573                        void *val = s->base + offs;
 574                        size_t size = EXT3_XATTR_SIZE(
 575                                le32_to_cpu(s->here->e_value_size));
 576
 577                        if (i->value && size == EXT3_XATTR_SIZE(i->value_len)) {
 578                                /* The old and the new value have the same
 579                                   size. Just replace. */
 580                                s->here->e_value_size =
 581                                        cpu_to_le32(i->value_len);
 582                                memset(val + size - EXT3_XATTR_PAD, 0,
 583                                       EXT3_XATTR_PAD); /* Clear pad bytes. */
 584                                memcpy(val, i->value, i->value_len);
 585                                return 0;
 586                        }
 587
 588                        /* Remove the old value. */
 589                        memmove(first_val + size, first_val, val - first_val);
 590                        memset(first_val, 0, size);
 591                        s->here->e_value_size = 0;
 592                        s->here->e_value_offs = 0;
 593                        min_offs += size;
 594
 595                        /* Adjust all value offsets. */
 596                        last = s->first;
 597                        while (!IS_LAST_ENTRY(last)) {
 598                                size_t o = le16_to_cpu(last->e_value_offs);
 599                                if (!last->e_value_block &&
 600                                    last->e_value_size && o < offs)
 601                                        last->e_value_offs =
 602                                                cpu_to_le16(o + size);
 603                                last = EXT3_XATTR_NEXT(last);
 604                        }
 605                }
 606                if (!i->value) {
 607                        /* Remove the old name. */
 608                        size_t size = EXT3_XATTR_LEN(name_len);
 609                        last = ENTRY((void *)last - size);
 610                        memmove(s->here, (void *)s->here + size,
 611                                (void *)last - (void *)s->here + sizeof(__u32));
 612                        memset(last, 0, size);
 613                }
 614        }
 615
 616        if (i->value) {
 617                /* Insert the new value. */
 618                s->here->e_value_size = cpu_to_le32(i->value_len);
 619                if (i->value_len) {
 620                        size_t size = EXT3_XATTR_SIZE(i->value_len);
 621                        void *val = s->base + min_offs - size;
 622                        s->here->e_value_offs = cpu_to_le16(min_offs - size);
 623                        memset(val + size - EXT3_XATTR_PAD, 0,
 624                               EXT3_XATTR_PAD); /* Clear the pad bytes. */
 625                        memcpy(val, i->value, i->value_len);
 626                }
 627        }
 628        return 0;
 629}
 630
 631struct ext3_xattr_block_find {
 632        struct ext3_xattr_search s;
 633        struct buffer_head *bh;
 634};
 635
 636static int
 637ext3_xattr_block_find(struct inode *inode, struct ext3_xattr_info *i,
 638                      struct ext3_xattr_block_find *bs)
 639{
 640        struct super_block *sb = inode->i_sb;
 641        int error;
 642
 643        ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
 644                  i->name_index, i->name, i->value, (long)i->value_len);
 645
 646        if (EXT3_I(inode)->i_file_acl) {
 647                /* The inode already has an extended attribute block. */
 648                bs->bh = sb_bread(sb, EXT3_I(inode)->i_file_acl);
 649                error = -EIO;
 650                if (!bs->bh)
 651                        goto cleanup;
 652                ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
 653                        atomic_read(&(bs->bh->b_count)),
 654                        le32_to_cpu(BHDR(bs->bh)->h_refcount));
 655                if (ext3_xattr_check_block(bs->bh)) {
 656                        ext3_error(sb, __func__,
 657                                "inode %lu: bad block "E3FSBLK, inode->i_ino,
 658                                EXT3_I(inode)->i_file_acl);
 659                        error = -EIO;
 660                        goto cleanup;
 661                }
 662                /* Find the named attribute. */
 663                bs->s.base = BHDR(bs->bh);
 664                bs->s.first = BFIRST(bs->bh);
 665                bs->s.end = bs->bh->b_data + bs->bh->b_size;
 666                bs->s.here = bs->s.first;
 667                error = ext3_xattr_find_entry(&bs->s.here, i->name_index,
 668                                              i->name, bs->bh->b_size, 1);
 669                if (error && error != -ENODATA)
 670                        goto cleanup;
 671                bs->s.not_found = error;
 672        }
 673        error = 0;
 674
 675cleanup:
 676        return error;
 677}
 678
 679static int
 680ext3_xattr_block_set(handle_t *handle, struct inode *inode,
 681                     struct ext3_xattr_info *i,
 682                     struct ext3_xattr_block_find *bs)
 683{
 684        struct super_block *sb = inode->i_sb;
 685        struct buffer_head *new_bh = NULL;
 686        struct ext3_xattr_search *s = &bs->s;
 687        struct mb_cache_entry *ce = NULL;
 688        int error = 0;
 689
 690#define header(x) ((struct ext3_xattr_header *)(x))
 691
 692        if (i->value && i->value_len > sb->s_blocksize)
 693                return -ENOSPC;
 694        if (s->base) {
 695                ce = mb_cache_entry_get(ext3_xattr_cache, bs->bh->b_bdev,
 696                                        bs->bh->b_blocknr);
 697                error = ext3_journal_get_write_access(handle, bs->bh);
 698                if (error)
 699                        goto cleanup;
 700                lock_buffer(bs->bh);
 701
 702                if (header(s->base)->h_refcount == cpu_to_le32(1)) {
 703                        if (ce) {
 704                                mb_cache_entry_free(ce);
 705                                ce = NULL;
 706                        }
 707                        ea_bdebug(bs->bh, "modifying in-place");
 708                        error = ext3_xattr_set_entry(i, s);
 709                        if (!error) {
 710                                if (!IS_LAST_ENTRY(s->first))
 711                                        ext3_xattr_rehash(header(s->base),
 712                                                          s->here);
 713                                ext3_xattr_cache_insert(bs->bh);
 714                        }
 715                        unlock_buffer(bs->bh);
 716                        if (error == -EIO)
 717                                goto bad_block;
 718                        if (!error)
 719                                error = ext3_journal_dirty_metadata(handle,
 720                                                                    bs->bh);
 721                        if (error)
 722                                goto cleanup;
 723                        goto inserted;
 724                } else {
 725                        int offset = (char *)s->here - bs->bh->b_data;
 726
 727                        unlock_buffer(bs->bh);
 728                        journal_release_buffer(handle, bs->bh);
 729
 730                        if (ce) {
 731                                mb_cache_entry_release(ce);
 732                                ce = NULL;
 733                        }
 734                        ea_bdebug(bs->bh, "cloning");
 735                        s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
 736                        error = -ENOMEM;
 737                        if (s->base == NULL)
 738                                goto cleanup;
 739                        memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
 740                        s->first = ENTRY(header(s->base)+1);
 741                        header(s->base)->h_refcount = cpu_to_le32(1);
 742                        s->here = ENTRY(s->base + offset);
 743                        s->end = s->base + bs->bh->b_size;
 744                }
 745        } else {
 746                /* Allocate a buffer where we construct the new block. */
 747                s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
 748                /* assert(header == s->base) */
 749                error = -ENOMEM;
 750                if (s->base == NULL)
 751                        goto cleanup;
 752                header(s->base)->h_magic = cpu_to_le32(EXT3_XATTR_MAGIC);
 753                header(s->base)->h_blocks = cpu_to_le32(1);
 754                header(s->base)->h_refcount = cpu_to_le32(1);
 755                s->first = ENTRY(header(s->base)+1);
 756                s->here = ENTRY(header(s->base)+1);
 757                s->end = s->base + sb->s_blocksize;
 758        }
 759
 760        error = ext3_xattr_set_entry(i, s);
 761        if (error == -EIO)
 762                goto bad_block;
 763        if (error)
 764                goto cleanup;
 765        if (!IS_LAST_ENTRY(s->first))
 766                ext3_xattr_rehash(header(s->base), s->here);
 767
 768inserted:
 769        if (!IS_LAST_ENTRY(s->first)) {
 770                new_bh = ext3_xattr_cache_find(inode, header(s->base), &ce);
 771                if (new_bh) {
 772                        /* We found an identical block in the cache. */
 773                        if (new_bh == bs->bh)
 774                                ea_bdebug(new_bh, "keeping");
 775                        else {
 776                                /* The old block is released after updating
 777                                   the inode. */
 778                                error = dquot_alloc_block(inode, 1);
 779                                if (error)
 780                                        goto cleanup;
 781                                error = ext3_journal_get_write_access(handle,
 782                                                                      new_bh);
 783                                if (error)
 784                                        goto cleanup_dquot;
 785                                lock_buffer(new_bh);
 786                                le32_add_cpu(&BHDR(new_bh)->h_refcount, 1);
 787                                ea_bdebug(new_bh, "reusing; refcount now=%d",
 788                                        le32_to_cpu(BHDR(new_bh)->h_refcount));
 789                                unlock_buffer(new_bh);
 790                                error = ext3_journal_dirty_metadata(handle,
 791                                                                    new_bh);
 792                                if (error)
 793                                        goto cleanup_dquot;
 794                        }
 795                        mb_cache_entry_release(ce);
 796                        ce = NULL;
 797                } else if (bs->bh && s->base == bs->bh->b_data) {
 798                        /* We were modifying this block in-place. */
 799                        ea_bdebug(bs->bh, "keeping this block");
 800                        new_bh = bs->bh;
 801                        get_bh(new_bh);
 802                } else {
 803                        /* We need to allocate a new block */
 804                        ext3_fsblk_t goal = ext3_group_first_block_no(sb,
 805                                                EXT3_I(inode)->i_block_group);
 806                        ext3_fsblk_t block = ext3_new_block(handle, inode,
 807                                                        goal, &error);
 808                        if (error)
 809                                goto cleanup;
 810                        ea_idebug(inode, "creating block %d", block);
 811
 812                        new_bh = sb_getblk(sb, block);
 813                        if (!new_bh) {
 814getblk_failed:
 815                                ext3_free_blocks(handle, inode, block, 1);
 816                                error = -EIO;
 817                                goto cleanup;
 818                        }
 819                        lock_buffer(new_bh);
 820                        error = ext3_journal_get_create_access(handle, new_bh);
 821                        if (error) {
 822                                unlock_buffer(new_bh);
 823                                goto getblk_failed;
 824                        }
 825                        memcpy(new_bh->b_data, s->base, new_bh->b_size);
 826                        set_buffer_uptodate(new_bh);
 827                        unlock_buffer(new_bh);
 828                        ext3_xattr_cache_insert(new_bh);
 829                        error = ext3_journal_dirty_metadata(handle, new_bh);
 830                        if (error)
 831                                goto cleanup;
 832                }
 833        }
 834
 835        /* Update the inode. */
 836        EXT3_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
 837
 838        /* Drop the previous xattr block. */
 839        if (bs->bh && bs->bh != new_bh)
 840                ext3_xattr_release_block(handle, inode, bs->bh);
 841        error = 0;
 842
 843cleanup:
 844        if (ce)
 845                mb_cache_entry_release(ce);
 846        brelse(new_bh);
 847        if (!(bs->bh && s->base == bs->bh->b_data))
 848                kfree(s->base);
 849
 850        return error;
 851
 852cleanup_dquot:
 853        dquot_free_block(inode, 1);
 854        goto cleanup;
 855
 856bad_block:
 857        ext3_error(inode->i_sb, __func__,
 858                   "inode %lu: bad block "E3FSBLK, inode->i_ino,
 859                   EXT3_I(inode)->i_file_acl);
 860        goto cleanup;
 861
 862#undef header
 863}
 864
 865struct ext3_xattr_ibody_find {
 866        struct ext3_xattr_search s;
 867        struct ext3_iloc iloc;
 868};
 869
 870static int
 871ext3_xattr_ibody_find(struct inode *inode, struct ext3_xattr_info *i,
 872                      struct ext3_xattr_ibody_find *is)
 873{
 874        struct ext3_xattr_ibody_header *header;
 875        struct ext3_inode *raw_inode;
 876        int error;
 877
 878        if (EXT3_I(inode)->i_extra_isize == 0)
 879                return 0;
 880        raw_inode = ext3_raw_inode(&is->iloc);
 881        header = IHDR(inode, raw_inode);
 882        is->s.base = is->s.first = IFIRST(header);
 883        is->s.here = is->s.first;
 884        is->s.end = (void *)raw_inode + EXT3_SB(inode->i_sb)->s_inode_size;
 885        if (ext3_test_inode_state(inode, EXT3_STATE_XATTR)) {
 886                error = ext3_xattr_check_names(IFIRST(header), is->s.end);
 887                if (error)
 888                        return error;
 889                /* Find the named attribute. */
 890                error = ext3_xattr_find_entry(&is->s.here, i->name_index,
 891                                              i->name, is->s.end -
 892                                              (void *)is->s.base, 0);
 893                if (error && error != -ENODATA)
 894                        return error;
 895                is->s.not_found = error;
 896        }
 897        return 0;
 898}
 899
 900static int
 901ext3_xattr_ibody_set(handle_t *handle, struct inode *inode,
 902                     struct ext3_xattr_info *i,
 903                     struct ext3_xattr_ibody_find *is)
 904{
 905        struct ext3_xattr_ibody_header *header;
 906        struct ext3_xattr_search *s = &is->s;
 907        int error;
 908
 909        if (EXT3_I(inode)->i_extra_isize == 0)
 910                return -ENOSPC;
 911        error = ext3_xattr_set_entry(i, s);
 912        if (error)
 913                return error;
 914        header = IHDR(inode, ext3_raw_inode(&is->iloc));
 915        if (!IS_LAST_ENTRY(s->first)) {
 916                header->h_magic = cpu_to_le32(EXT3_XATTR_MAGIC);
 917                ext3_set_inode_state(inode, EXT3_STATE_XATTR);
 918        } else {
 919                header->h_magic = cpu_to_le32(0);
 920                ext3_clear_inode_state(inode, EXT3_STATE_XATTR);
 921        }
 922        return 0;
 923}
 924
 925/*
 926 * ext3_xattr_set_handle()
 927 *
 928 * Create, replace or remove an extended attribute for this inode.  Value
 929 * is NULL to remove an existing extended attribute, and non-NULL to
 930 * either replace an existing extended attribute, or create a new extended
 931 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
 932 * specify that an extended attribute must exist and must not exist
 933 * previous to the call, respectively.
 934 *
 935 * Returns 0, or a negative error number on failure.
 936 */
 937int
 938ext3_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
 939                      const char *name, const void *value, size_t value_len,
 940                      int flags)
 941{
 942        struct ext3_xattr_info i = {
 943                .name_index = name_index,
 944                .name = name,
 945                .value = value,
 946                .value_len = value_len,
 947
 948        };
 949        struct ext3_xattr_ibody_find is = {
 950                .s = { .not_found = -ENODATA, },
 951        };
 952        struct ext3_xattr_block_find bs = {
 953                .s = { .not_found = -ENODATA, },
 954        };
 955        int error;
 956
 957        if (!name)
 958                return -EINVAL;
 959        if (strlen(name) > 255)
 960                return -ERANGE;
 961        down_write(&EXT3_I(inode)->xattr_sem);
 962        error = ext3_get_inode_loc(inode, &is.iloc);
 963        if (error)
 964                goto cleanup;
 965
 966        error = ext3_journal_get_write_access(handle, is.iloc.bh);
 967        if (error)
 968                goto cleanup;
 969
 970        if (ext3_test_inode_state(inode, EXT3_STATE_NEW)) {
 971                struct ext3_inode *raw_inode = ext3_raw_inode(&is.iloc);
 972                memset(raw_inode, 0, EXT3_SB(inode->i_sb)->s_inode_size);
 973                ext3_clear_inode_state(inode, EXT3_STATE_NEW);
 974        }
 975
 976        error = ext3_xattr_ibody_find(inode, &i, &is);
 977        if (error)
 978                goto cleanup;
 979        if (is.s.not_found)
 980                error = ext3_xattr_block_find(inode, &i, &bs);
 981        if (error)
 982                goto cleanup;
 983        if (is.s.not_found && bs.s.not_found) {
 984                error = -ENODATA;
 985                if (flags & XATTR_REPLACE)
 986                        goto cleanup;
 987                error = 0;
 988                if (!value)
 989                        goto cleanup;
 990        } else {
 991                error = -EEXIST;
 992                if (flags & XATTR_CREATE)
 993                        goto cleanup;
 994        }
 995        if (!value) {
 996                if (!is.s.not_found)
 997                        error = ext3_xattr_ibody_set(handle, inode, &i, &is);
 998                else if (!bs.s.not_found)
 999                        error = ext3_xattr_block_set(handle, inode, &i, &bs);
1000        } else {
1001                error = ext3_xattr_ibody_set(handle, inode, &i, &is);
1002                if (!error && !bs.s.not_found) {
1003                        i.value = NULL;
1004                        error = ext3_xattr_block_set(handle, inode, &i, &bs);
1005                } else if (error == -ENOSPC) {
1006                        if (EXT3_I(inode)->i_file_acl && !bs.s.base) {
1007                                error = ext3_xattr_block_find(inode, &i, &bs);
1008                                if (error)
1009                                        goto cleanup;
1010                        }
1011                        error = ext3_xattr_block_set(handle, inode, &i, &bs);
1012                        if (error)
1013                                goto cleanup;
1014                        if (!is.s.not_found) {
1015                                i.value = NULL;
1016                                error = ext3_xattr_ibody_set(handle, inode, &i,
1017                                                             &is);
1018                        }
1019                }
1020        }
1021        if (!error) {
1022                ext3_xattr_update_super_block(handle, inode->i_sb);
1023                inode->i_ctime = CURRENT_TIME_SEC;
1024                error = ext3_mark_iloc_dirty(handle, inode, &is.iloc);
1025                /*
1026                 * The bh is consumed by ext3_mark_iloc_dirty, even with
1027                 * error != 0.
1028                 */
1029                is.iloc.bh = NULL;
1030                if (IS_SYNC(inode))
1031                        handle->h_sync = 1;
1032        }
1033
1034cleanup:
1035        brelse(is.iloc.bh);
1036        brelse(bs.bh);
1037        up_write(&EXT3_I(inode)->xattr_sem);
1038        return error;
1039}
1040
1041/*
1042 * ext3_xattr_set()
1043 *
1044 * Like ext3_xattr_set_handle, but start from an inode. This extended
1045 * attribute modification is a filesystem transaction by itself.
1046 *
1047 * Returns 0, or a negative error number on failure.
1048 */
1049int
1050ext3_xattr_set(struct inode *inode, int name_index, const char *name,
1051               const void *value, size_t value_len, int flags)
1052{
1053        handle_t *handle;
1054        int error, retries = 0;
1055
1056retry:
1057        handle = ext3_journal_start(inode, EXT3_DATA_TRANS_BLOCKS(inode->i_sb));
1058        if (IS_ERR(handle)) {
1059                error = PTR_ERR(handle);
1060        } else {
1061                int error2;
1062
1063                error = ext3_xattr_set_handle(handle, inode, name_index, name,
1064                                              value, value_len, flags);
1065                error2 = ext3_journal_stop(handle);
1066                if (error == -ENOSPC &&
1067                    ext3_should_retry_alloc(inode->i_sb, &retries))
1068                        goto retry;
1069                if (error == 0)
1070                        error = error2;
1071        }
1072
1073        return error;
1074}
1075
1076/*
1077 * ext3_xattr_delete_inode()
1078 *
1079 * Free extended attribute resources associated with this inode. This
1080 * is called immediately before an inode is freed. We have exclusive
1081 * access to the inode.
1082 */
1083void
1084ext3_xattr_delete_inode(handle_t *handle, struct inode *inode)
1085{
1086        struct buffer_head *bh = NULL;
1087
1088        if (!EXT3_I(inode)->i_file_acl)
1089                goto cleanup;
1090        bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
1091        if (!bh) {
1092                ext3_error(inode->i_sb, __func__,
1093                        "inode %lu: block "E3FSBLK" read error", inode->i_ino,
1094                        EXT3_I(inode)->i_file_acl);
1095                goto cleanup;
1096        }
1097        if (BHDR(bh)->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC) ||
1098            BHDR(bh)->h_blocks != cpu_to_le32(1)) {
1099                ext3_error(inode->i_sb, __func__,
1100                        "inode %lu: bad block "E3FSBLK, inode->i_ino,
1101                        EXT3_I(inode)->i_file_acl);
1102                goto cleanup;
1103        }
1104        ext3_xattr_release_block(handle, inode, bh);
1105        EXT3_I(inode)->i_file_acl = 0;
1106
1107cleanup:
1108        brelse(bh);
1109}
1110
1111/*
1112 * ext3_xattr_put_super()
1113 *
1114 * This is called when a file system is unmounted.
1115 */
1116void
1117ext3_xattr_put_super(struct super_block *sb)
1118{
1119        mb_cache_shrink(sb->s_bdev);
1120}
1121
1122/*
1123 * ext3_xattr_cache_insert()
1124 *
1125 * Create a new entry in the extended attribute cache, and insert
1126 * it unless such an entry is already in the cache.
1127 *
1128 * Returns 0, or a negative error number on failure.
1129 */
1130static void
1131ext3_xattr_cache_insert(struct buffer_head *bh)
1132{
1133        __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
1134        struct mb_cache_entry *ce;
1135        int error;
1136
1137        ce = mb_cache_entry_alloc(ext3_xattr_cache, GFP_NOFS);
1138        if (!ce) {
1139                ea_bdebug(bh, "out of memory");
1140                return;
1141        }
1142        error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, hash);
1143        if (error) {
1144                mb_cache_entry_free(ce);
1145                if (error == -EBUSY) {
1146                        ea_bdebug(bh, "already in cache");
1147                        error = 0;
1148                }
1149        } else {
1150                ea_bdebug(bh, "inserting [%x]", (int)hash);
1151                mb_cache_entry_release(ce);
1152        }
1153}
1154
1155/*
1156 * ext3_xattr_cmp()
1157 *
1158 * Compare two extended attribute blocks for equality.
1159 *
1160 * Returns 0 if the blocks are equal, 1 if they differ, and
1161 * a negative error number on errors.
1162 */
1163static int
1164ext3_xattr_cmp(struct ext3_xattr_header *header1,
1165               struct ext3_xattr_header *header2)
1166{
1167        struct ext3_xattr_entry *entry1, *entry2;
1168
1169        entry1 = ENTRY(header1+1);
1170        entry2 = ENTRY(header2+1);
1171        while (!IS_LAST_ENTRY(entry1)) {
1172                if (IS_LAST_ENTRY(entry2))
1173                        return 1;
1174                if (entry1->e_hash != entry2->e_hash ||
1175                    entry1->e_name_index != entry2->e_name_index ||
1176                    entry1->e_name_len != entry2->e_name_len ||
1177                    entry1->e_value_size != entry2->e_value_size ||
1178                    memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1179                        return 1;
1180                if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
1181                        return -EIO;
1182                if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1183                           (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1184                           le32_to_cpu(entry1->e_value_size)))
1185                        return 1;
1186
1187                entry1 = EXT3_XATTR_NEXT(entry1);
1188                entry2 = EXT3_XATTR_NEXT(entry2);
1189        }
1190        if (!IS_LAST_ENTRY(entry2))
1191                return 1;
1192        return 0;
1193}
1194
1195/*
1196 * ext3_xattr_cache_find()
1197 *
1198 * Find an identical extended attribute block.
1199 *
1200 * Returns a pointer to the block found, or NULL if such a block was
1201 * not found or an error occurred.
1202 */
1203static struct buffer_head *
1204ext3_xattr_cache_find(struct inode *inode, struct ext3_xattr_header *header,
1205                      struct mb_cache_entry **pce)
1206{
1207        __u32 hash = le32_to_cpu(header->h_hash);
1208        struct mb_cache_entry *ce;
1209
1210        if (!header->h_hash)
1211                return NULL;  /* never share */
1212        ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
1213again:
1214        ce = mb_cache_entry_find_first(ext3_xattr_cache, inode->i_sb->s_bdev,
1215                                       hash);
1216        while (ce) {
1217                struct buffer_head *bh;
1218
1219                if (IS_ERR(ce)) {
1220                        if (PTR_ERR(ce) == -EAGAIN)
1221                                goto again;
1222                        break;
1223                }
1224                bh = sb_bread(inode->i_sb, ce->e_block);
1225                if (!bh) {
1226                        ext3_error(inode->i_sb, __func__,
1227                                "inode %lu: block %lu read error",
1228                                inode->i_ino, (unsigned long) ce->e_block);
1229                } else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
1230                                EXT3_XATTR_REFCOUNT_MAX) {
1231                        ea_idebug(inode, "block %lu refcount %d>=%d",
1232                                  (unsigned long) ce->e_block,
1233                                  le32_to_cpu(BHDR(bh)->h_refcount),
1234                                          EXT3_XATTR_REFCOUNT_MAX);
1235                } else if (ext3_xattr_cmp(header, BHDR(bh)) == 0) {
1236                        *pce = ce;
1237                        return bh;
1238                }
1239                brelse(bh);
1240                ce = mb_cache_entry_find_next(ce, inode->i_sb->s_bdev, hash);
1241        }
1242        return NULL;
1243}
1244
1245#define NAME_HASH_SHIFT 5
1246#define VALUE_HASH_SHIFT 16
1247
1248/*
1249 * ext3_xattr_hash_entry()
1250 *
1251 * Compute the hash of an extended attribute.
1252 */
1253static inline void ext3_xattr_hash_entry(struct ext3_xattr_header *header,
1254                                         struct ext3_xattr_entry *entry)
1255{
1256        __u32 hash = 0;
1257        char *name = entry->e_name;
1258        int n;
1259
1260        for (n=0; n < entry->e_name_len; n++) {
1261                hash = (hash << NAME_HASH_SHIFT) ^
1262                       (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1263                       *name++;
1264        }
1265
1266        if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1267                __le32 *value = (__le32 *)((char *)header +
1268                        le16_to_cpu(entry->e_value_offs));
1269                for (n = (le32_to_cpu(entry->e_value_size) +
1270                     EXT3_XATTR_ROUND) >> EXT3_XATTR_PAD_BITS; n; n--) {
1271                        hash = (hash << VALUE_HASH_SHIFT) ^
1272                               (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1273                               le32_to_cpu(*value++);
1274                }
1275        }
1276        entry->e_hash = cpu_to_le32(hash);
1277}
1278
1279#undef NAME_HASH_SHIFT
1280#undef VALUE_HASH_SHIFT
1281
1282#define BLOCK_HASH_SHIFT 16
1283
1284/*
1285 * ext3_xattr_rehash()
1286 *
1287 * Re-compute the extended attribute hash value after an entry has changed.
1288 */
1289static void ext3_xattr_rehash(struct ext3_xattr_header *header,
1290                              struct ext3_xattr_entry *entry)
1291{
1292        struct ext3_xattr_entry *here;
1293        __u32 hash = 0;
1294
1295        ext3_xattr_hash_entry(header, entry);
1296        here = ENTRY(header+1);
1297        while (!IS_LAST_ENTRY(here)) {
1298                if (!here->e_hash) {
1299                        /* Block is not shared if an entry's hash value == 0 */
1300                        hash = 0;
1301                        break;
1302                }
1303                hash = (hash << BLOCK_HASH_SHIFT) ^
1304                       (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1305                       le32_to_cpu(here->e_hash);
1306                here = EXT3_XATTR_NEXT(here);
1307        }
1308        header->h_hash = cpu_to_le32(hash);
1309}
1310
1311#undef BLOCK_HASH_SHIFT
1312
1313int __init
1314init_ext3_xattr(void)
1315{
1316        ext3_xattr_cache = mb_cache_create("ext3_xattr", 6);
1317        if (!ext3_xattr_cache)
1318                return -ENOMEM;
1319        return 0;
1320}
1321
1322void
1323exit_ext3_xattr(void)
1324{
1325        if (ext3_xattr_cache)
1326                mb_cache_destroy(ext3_xattr_cache);
1327        ext3_xattr_cache = NULL;
1328}
1329