linux/fs/ocfs2/xattr.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * xattr.c
   4 *
   5 * Copyright (C) 2004, 2008 Oracle.  All rights reserved.
   6 *
   7 * CREDITS:
   8 * Lots of code in this file is copy from linux/fs/ext3/xattr.c.
   9 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
  10 */
  11
  12#include <linux/capability.h>
  13#include <linux/fs.h>
  14#include <linux/types.h>
  15#include <linux/slab.h>
  16#include <linux/highmem.h>
  17#include <linux/pagemap.h>
  18#include <linux/uio.h>
  19#include <linux/sched.h>
  20#include <linux/splice.h>
  21#include <linux/mount.h>
  22#include <linux/writeback.h>
  23#include <linux/falloc.h>
  24#include <linux/sort.h>
  25#include <linux/init.h>
  26#include <linux/module.h>
  27#include <linux/string.h>
  28#include <linux/security.h>
  29
  30#include <cluster/masklog.h>
  31
  32#include "ocfs2.h"
  33#include "alloc.h"
  34#include "blockcheck.h"
  35#include "dlmglue.h"
  36#include "file.h"
  37#include "symlink.h"
  38#include "sysfile.h"
  39#include "inode.h"
  40#include "journal.h"
  41#include "ocfs2_fs.h"
  42#include "suballoc.h"
  43#include "uptodate.h"
  44#include "buffer_head_io.h"
  45#include "super.h"
  46#include "xattr.h"
  47#include "refcounttree.h"
  48#include "acl.h"
  49#include "ocfs2_trace.h"
  50
  51struct ocfs2_xattr_def_value_root {
  52        struct ocfs2_xattr_value_root   xv;
  53        struct ocfs2_extent_rec         er;
  54};
  55
  56struct ocfs2_xattr_bucket {
  57        /* The inode these xattrs are associated with */
  58        struct inode *bu_inode;
  59
  60        /* The actual buffers that make up the bucket */
  61        struct buffer_head *bu_bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
  62
  63        /* How many blocks make up one bucket for this filesystem */
  64        int bu_blocks;
  65};
  66
  67struct ocfs2_xattr_set_ctxt {
  68        handle_t *handle;
  69        struct ocfs2_alloc_context *meta_ac;
  70        struct ocfs2_alloc_context *data_ac;
  71        struct ocfs2_cached_dealloc_ctxt dealloc;
  72        int set_abort;
  73};
  74
  75#define OCFS2_XATTR_ROOT_SIZE   (sizeof(struct ocfs2_xattr_def_value_root))
  76#define OCFS2_XATTR_INLINE_SIZE 80
  77#define OCFS2_XATTR_HEADER_GAP  4
  78#define OCFS2_XATTR_FREE_IN_IBODY       (OCFS2_MIN_XATTR_INLINE_SIZE \
  79                                         - sizeof(struct ocfs2_xattr_header) \
  80                                         - OCFS2_XATTR_HEADER_GAP)
  81#define OCFS2_XATTR_FREE_IN_BLOCK(ptr)  ((ptr)->i_sb->s_blocksize \
  82                                         - sizeof(struct ocfs2_xattr_block) \
  83                                         - sizeof(struct ocfs2_xattr_header) \
  84                                         - OCFS2_XATTR_HEADER_GAP)
  85
  86static struct ocfs2_xattr_def_value_root def_xv = {
  87        .xv.xr_list.l_count = cpu_to_le16(1),
  88};
  89
  90const struct xattr_handler *ocfs2_xattr_handlers[] = {
  91        &ocfs2_xattr_user_handler,
  92        &posix_acl_access_xattr_handler,
  93        &posix_acl_default_xattr_handler,
  94        &ocfs2_xattr_trusted_handler,
  95        &ocfs2_xattr_security_handler,
  96        NULL
  97};
  98
  99static const struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
 100        [OCFS2_XATTR_INDEX_USER]        = &ocfs2_xattr_user_handler,
 101        [OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS]
 102                                        = &posix_acl_access_xattr_handler,
 103        [OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT]
 104                                        = &posix_acl_default_xattr_handler,
 105        [OCFS2_XATTR_INDEX_TRUSTED]     = &ocfs2_xattr_trusted_handler,
 106        [OCFS2_XATTR_INDEX_SECURITY]    = &ocfs2_xattr_security_handler,
 107};
 108
 109struct ocfs2_xattr_info {
 110        int             xi_name_index;
 111        const char      *xi_name;
 112        int             xi_name_len;
 113        const void      *xi_value;
 114        size_t          xi_value_len;
 115};
 116
 117struct ocfs2_xattr_search {
 118        struct buffer_head *inode_bh;
 119        /*
 120         * xattr_bh point to the block buffer head which has extended attribute
 121         * when extended attribute in inode, xattr_bh is equal to inode_bh.
 122         */
 123        struct buffer_head *xattr_bh;
 124        struct ocfs2_xattr_header *header;
 125        struct ocfs2_xattr_bucket *bucket;
 126        void *base;
 127        void *end;
 128        struct ocfs2_xattr_entry *here;
 129        int not_found;
 130};
 131
 132/* Operations on struct ocfs2_xa_entry */
 133struct ocfs2_xa_loc;
 134struct ocfs2_xa_loc_operations {
 135        /*
 136         * Journal functions
 137         */
 138        int (*xlo_journal_access)(handle_t *handle, struct ocfs2_xa_loc *loc,
 139                                  int type);
 140        void (*xlo_journal_dirty)(handle_t *handle, struct ocfs2_xa_loc *loc);
 141
 142        /*
 143         * Return a pointer to the appropriate buffer in loc->xl_storage
 144         * at the given offset from loc->xl_header.
 145         */
 146        void *(*xlo_offset_pointer)(struct ocfs2_xa_loc *loc, int offset);
 147
 148        /* Can we reuse the existing entry for the new value? */
 149        int (*xlo_can_reuse)(struct ocfs2_xa_loc *loc,
 150                             struct ocfs2_xattr_info *xi);
 151
 152        /* How much space is needed for the new value? */
 153        int (*xlo_check_space)(struct ocfs2_xa_loc *loc,
 154                               struct ocfs2_xattr_info *xi);
 155
 156        /*
 157         * Return the offset of the first name+value pair.  This is
 158         * the start of our downward-filling free space.
 159         */
 160        int (*xlo_get_free_start)(struct ocfs2_xa_loc *loc);
 161
 162        /*
 163         * Remove the name+value at this location.  Do whatever is
 164         * appropriate with the remaining name+value pairs.
 165         */
 166        void (*xlo_wipe_namevalue)(struct ocfs2_xa_loc *loc);
 167
 168        /* Fill xl_entry with a new entry */
 169        void (*xlo_add_entry)(struct ocfs2_xa_loc *loc, u32 name_hash);
 170
 171        /* Add name+value storage to an entry */
 172        void (*xlo_add_namevalue)(struct ocfs2_xa_loc *loc, int size);
 173
 174        /*
 175         * Initialize the value buf's access and bh fields for this entry.
 176         * ocfs2_xa_fill_value_buf() will handle the xv pointer.
 177         */
 178        void (*xlo_fill_value_buf)(struct ocfs2_xa_loc *loc,
 179                                   struct ocfs2_xattr_value_buf *vb);
 180};
 181
 182/*
 183 * Describes an xattr entry location.  This is a memory structure
 184 * tracking the on-disk structure.
 185 */
 186struct ocfs2_xa_loc {
 187        /* This xattr belongs to this inode */
 188        struct inode *xl_inode;
 189
 190        /* The ocfs2_xattr_header inside the on-disk storage. Not NULL. */
 191        struct ocfs2_xattr_header *xl_header;
 192
 193        /* Bytes from xl_header to the end of the storage */
 194        int xl_size;
 195
 196        /*
 197         * The ocfs2_xattr_entry this location describes.  If this is
 198         * NULL, this location describes the on-disk structure where it
 199         * would have been.
 200         */
 201        struct ocfs2_xattr_entry *xl_entry;
 202
 203        /*
 204         * Internal housekeeping
 205         */
 206
 207        /* Buffer(s) containing this entry */
 208        void *xl_storage;
 209
 210        /* Operations on the storage backing this location */
 211        const struct ocfs2_xa_loc_operations *xl_ops;
 212};
 213
 214/*
 215 * Convenience functions to calculate how much space is needed for a
 216 * given name+value pair
 217 */
 218static int namevalue_size(int name_len, uint64_t value_len)
 219{
 220        if (value_len > OCFS2_XATTR_INLINE_SIZE)
 221                return OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
 222        else
 223                return OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
 224}
 225
 226static int namevalue_size_xi(struct ocfs2_xattr_info *xi)
 227{
 228        return namevalue_size(xi->xi_name_len, xi->xi_value_len);
 229}
 230
 231static int namevalue_size_xe(struct ocfs2_xattr_entry *xe)
 232{
 233        u64 value_len = le64_to_cpu(xe->xe_value_size);
 234
 235        BUG_ON((value_len > OCFS2_XATTR_INLINE_SIZE) &&
 236               ocfs2_xattr_is_local(xe));
 237        return namevalue_size(xe->xe_name_len, value_len);
 238}
 239
 240
 241static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
 242                                             struct ocfs2_xattr_header *xh,
 243                                             int index,
 244                                             int *block_off,
 245                                             int *new_offset);
 246
 247static int ocfs2_xattr_block_find(struct inode *inode,
 248                                  int name_index,
 249                                  const char *name,
 250                                  struct ocfs2_xattr_search *xs);
 251static int ocfs2_xattr_index_block_find(struct inode *inode,
 252                                        struct buffer_head *root_bh,
 253                                        int name_index,
 254                                        const char *name,
 255                                        struct ocfs2_xattr_search *xs);
 256
 257static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
 258                                        struct buffer_head *blk_bh,
 259                                        char *buffer,
 260                                        size_t buffer_size);
 261
 262static int ocfs2_xattr_create_index_block(struct inode *inode,
 263                                          struct ocfs2_xattr_search *xs,
 264                                          struct ocfs2_xattr_set_ctxt *ctxt);
 265
 266static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
 267                                             struct ocfs2_xattr_info *xi,
 268                                             struct ocfs2_xattr_search *xs,
 269                                             struct ocfs2_xattr_set_ctxt *ctxt);
 270
 271typedef int (xattr_tree_rec_func)(struct inode *inode,
 272                                  struct buffer_head *root_bh,
 273                                  u64 blkno, u32 cpos, u32 len, void *para);
 274static int ocfs2_iterate_xattr_index_block(struct inode *inode,
 275                                           struct buffer_head *root_bh,
 276                                           xattr_tree_rec_func *rec_func,
 277                                           void *para);
 278static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
 279                                        struct ocfs2_xattr_bucket *bucket,
 280                                        void *para);
 281static int ocfs2_rm_xattr_cluster(struct inode *inode,
 282                                  struct buffer_head *root_bh,
 283                                  u64 blkno,
 284                                  u32 cpos,
 285                                  u32 len,
 286                                  void *para);
 287
 288static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
 289                                  u64 src_blk, u64 last_blk, u64 to_blk,
 290                                  unsigned int start_bucket,
 291                                  u32 *first_hash);
 292static int ocfs2_prepare_refcount_xattr(struct inode *inode,
 293                                        struct ocfs2_dinode *di,
 294                                        struct ocfs2_xattr_info *xi,
 295                                        struct ocfs2_xattr_search *xis,
 296                                        struct ocfs2_xattr_search *xbs,
 297                                        struct ocfs2_refcount_tree **ref_tree,
 298                                        int *meta_need,
 299                                        int *credits);
 300static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
 301                                           struct ocfs2_xattr_bucket *bucket,
 302                                           int offset,
 303                                           struct ocfs2_xattr_value_root **xv,
 304                                           struct buffer_head **bh);
 305
 306static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
 307{
 308        return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
 309}
 310
 311static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
 312{
 313        return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
 314}
 315
 316#define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
 317#define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
 318#define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
 319
 320static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
 321{
 322        struct ocfs2_xattr_bucket *bucket;
 323        int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
 324
 325        BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
 326
 327        bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
 328        if (bucket) {
 329                bucket->bu_inode = inode;
 330                bucket->bu_blocks = blks;
 331        }
 332
 333        return bucket;
 334}
 335
 336static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
 337{
 338        int i;
 339
 340        for (i = 0; i < bucket->bu_blocks; i++) {
 341                brelse(bucket->bu_bhs[i]);
 342                bucket->bu_bhs[i] = NULL;
 343        }
 344}
 345
 346static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
 347{
 348        if (bucket) {
 349                ocfs2_xattr_bucket_relse(bucket);
 350                bucket->bu_inode = NULL;
 351                kfree(bucket);
 352        }
 353}
 354
 355/*
 356 * A bucket that has never been written to disk doesn't need to be
 357 * read.  We just need the buffer_heads.  Don't call this for
 358 * buckets that are already on disk.  ocfs2_read_xattr_bucket() initializes
 359 * them fully.
 360 */
 361static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
 362                                   u64 xb_blkno, int new)
 363{
 364        int i, rc = 0;
 365
 366        for (i = 0; i < bucket->bu_blocks; i++) {
 367                bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
 368                                              xb_blkno + i);
 369                if (!bucket->bu_bhs[i]) {
 370                        rc = -ENOMEM;
 371                        mlog_errno(rc);
 372                        break;
 373                }
 374
 375                if (!ocfs2_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
 376                                           bucket->bu_bhs[i])) {
 377                        if (new)
 378                                ocfs2_set_new_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
 379                                                              bucket->bu_bhs[i]);
 380                        else {
 381                                set_buffer_uptodate(bucket->bu_bhs[i]);
 382                                ocfs2_set_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
 383                                                          bucket->bu_bhs[i]);
 384                        }
 385                }
 386        }
 387
 388        if (rc)
 389                ocfs2_xattr_bucket_relse(bucket);
 390        return rc;
 391}
 392
 393/* Read the xattr bucket at xb_blkno */
 394static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
 395                                   u64 xb_blkno)
 396{
 397        int rc;
 398
 399        rc = ocfs2_read_blocks(INODE_CACHE(bucket->bu_inode), xb_blkno,
 400                               bucket->bu_blocks, bucket->bu_bhs, 0,
 401                               NULL);
 402        if (!rc) {
 403                spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
 404                rc = ocfs2_validate_meta_ecc_bhs(bucket->bu_inode->i_sb,
 405                                                 bucket->bu_bhs,
 406                                                 bucket->bu_blocks,
 407                                                 &bucket_xh(bucket)->xh_check);
 408                spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
 409                if (rc)
 410                        mlog_errno(rc);
 411        }
 412
 413        if (rc)
 414                ocfs2_xattr_bucket_relse(bucket);
 415        return rc;
 416}
 417
 418static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
 419                                             struct ocfs2_xattr_bucket *bucket,
 420                                             int type)
 421{
 422        int i, rc = 0;
 423
 424        for (i = 0; i < bucket->bu_blocks; i++) {
 425                rc = ocfs2_journal_access(handle,
 426                                          INODE_CACHE(bucket->bu_inode),
 427                                          bucket->bu_bhs[i], type);
 428                if (rc) {
 429                        mlog_errno(rc);
 430                        break;
 431                }
 432        }
 433
 434        return rc;
 435}
 436
 437static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
 438                                             struct ocfs2_xattr_bucket *bucket)
 439{
 440        int i;
 441
 442        spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
 443        ocfs2_compute_meta_ecc_bhs(bucket->bu_inode->i_sb,
 444                                   bucket->bu_bhs, bucket->bu_blocks,
 445                                   &bucket_xh(bucket)->xh_check);
 446        spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
 447
 448        for (i = 0; i < bucket->bu_blocks; i++)
 449                ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
 450}
 451
 452static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
 453                                         struct ocfs2_xattr_bucket *src)
 454{
 455        int i;
 456        int blocksize = src->bu_inode->i_sb->s_blocksize;
 457
 458        BUG_ON(dest->bu_blocks != src->bu_blocks);
 459        BUG_ON(dest->bu_inode != src->bu_inode);
 460
 461        for (i = 0; i < src->bu_blocks; i++) {
 462                memcpy(bucket_block(dest, i), bucket_block(src, i),
 463                       blocksize);
 464        }
 465}
 466
 467static int ocfs2_validate_xattr_block(struct super_block *sb,
 468                                      struct buffer_head *bh)
 469{
 470        int rc;
 471        struct ocfs2_xattr_block *xb =
 472                (struct ocfs2_xattr_block *)bh->b_data;
 473
 474        trace_ocfs2_validate_xattr_block((unsigned long long)bh->b_blocknr);
 475
 476        BUG_ON(!buffer_uptodate(bh));
 477
 478        /*
 479         * If the ecc fails, we return the error but otherwise
 480         * leave the filesystem running.  We know any error is
 481         * local to this block.
 482         */
 483        rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &xb->xb_check);
 484        if (rc)
 485                return rc;
 486
 487        /*
 488         * Errors after here are fatal
 489         */
 490
 491        if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
 492                return ocfs2_error(sb,
 493                                   "Extended attribute block #%llu has bad signature %.*s\n",
 494                                   (unsigned long long)bh->b_blocknr, 7,
 495                                   xb->xb_signature);
 496        }
 497
 498        if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) {
 499                return ocfs2_error(sb,
 500                                   "Extended attribute block #%llu has an invalid xb_blkno of %llu\n",
 501                                   (unsigned long long)bh->b_blocknr,
 502                                   (unsigned long long)le64_to_cpu(xb->xb_blkno));
 503        }
 504
 505        if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) {
 506                return ocfs2_error(sb,
 507                                   "Extended attribute block #%llu has an invalid xb_fs_generation of #%u\n",
 508                                   (unsigned long long)bh->b_blocknr,
 509                                   le32_to_cpu(xb->xb_fs_generation));
 510        }
 511
 512        return 0;
 513}
 514
 515static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno,
 516                                  struct buffer_head **bh)
 517{
 518        int rc;
 519        struct buffer_head *tmp = *bh;
 520
 521        rc = ocfs2_read_block(INODE_CACHE(inode), xb_blkno, &tmp,
 522                              ocfs2_validate_xattr_block);
 523
 524        /* If ocfs2_read_block() got us a new bh, pass it up. */
 525        if (!rc && !*bh)
 526                *bh = tmp;
 527
 528        return rc;
 529}
 530
 531static inline const char *ocfs2_xattr_prefix(int name_index)
 532{
 533        const struct xattr_handler *handler = NULL;
 534
 535        if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
 536                handler = ocfs2_xattr_handler_map[name_index];
 537        return handler ? xattr_prefix(handler) : NULL;
 538}
 539
 540static u32 ocfs2_xattr_name_hash(struct inode *inode,
 541                                 const char *name,
 542                                 int name_len)
 543{
 544        /* Get hash value of uuid from super block */
 545        u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
 546        int i;
 547
 548        /* hash extended attribute name */
 549        for (i = 0; i < name_len; i++) {
 550                hash = (hash << OCFS2_HASH_SHIFT) ^
 551                       (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
 552                       *name++;
 553        }
 554
 555        return hash;
 556}
 557
 558static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
 559{
 560        return namevalue_size(name_len, value_len) +
 561                sizeof(struct ocfs2_xattr_entry);
 562}
 563
 564static int ocfs2_xi_entry_usage(struct ocfs2_xattr_info *xi)
 565{
 566        return namevalue_size_xi(xi) +
 567                sizeof(struct ocfs2_xattr_entry);
 568}
 569
 570static int ocfs2_xe_entry_usage(struct ocfs2_xattr_entry *xe)
 571{
 572        return namevalue_size_xe(xe) +
 573                sizeof(struct ocfs2_xattr_entry);
 574}
 575
 576int ocfs2_calc_security_init(struct inode *dir,
 577                             struct ocfs2_security_xattr_info *si,
 578                             int *want_clusters,
 579                             int *xattr_credits,
 580                             struct ocfs2_alloc_context **xattr_ac)
 581{
 582        int ret = 0;
 583        struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
 584        int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
 585                                                 si->value_len);
 586
 587        /*
 588         * The max space of security xattr taken inline is
 589         * 256(name) + 80(value) + 16(entry) = 352 bytes,
 590         * So reserve one metadata block for it is ok.
 591         */
 592        if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
 593            s_size > OCFS2_XATTR_FREE_IN_IBODY) {
 594                ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
 595                if (ret) {
 596                        mlog_errno(ret);
 597                        return ret;
 598                }
 599                *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
 600        }
 601
 602        /* reserve clusters for xattr value which will be set in B tree*/
 603        if (si->value_len > OCFS2_XATTR_INLINE_SIZE) {
 604                int new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
 605                                                            si->value_len);
 606
 607                *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
 608                                                           new_clusters);
 609                *want_clusters += new_clusters;
 610        }
 611        return ret;
 612}
 613
 614int ocfs2_calc_xattr_init(struct inode *dir,
 615                          struct buffer_head *dir_bh,
 616                          umode_t mode,
 617                          struct ocfs2_security_xattr_info *si,
 618                          int *want_clusters,
 619                          int *xattr_credits,
 620                          int *want_meta)
 621{
 622        int ret = 0;
 623        struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
 624        int s_size = 0, a_size = 0, acl_len = 0, new_clusters;
 625
 626        if (si->enable)
 627                s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
 628                                                     si->value_len);
 629
 630        if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
 631                down_read(&OCFS2_I(dir)->ip_xattr_sem);
 632                acl_len = ocfs2_xattr_get_nolock(dir, dir_bh,
 633                                        OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
 634                                        "", NULL, 0);
 635                up_read(&OCFS2_I(dir)->ip_xattr_sem);
 636                if (acl_len > 0) {
 637                        a_size = ocfs2_xattr_entry_real_size(0, acl_len);
 638                        if (S_ISDIR(mode))
 639                                a_size <<= 1;
 640                } else if (acl_len != 0 && acl_len != -ENODATA) {
 641                        ret = acl_len;
 642                        mlog_errno(ret);
 643                        return ret;
 644                }
 645        }
 646
 647        if (!(s_size + a_size))
 648                return ret;
 649
 650        /*
 651         * The max space of security xattr taken inline is
 652         * 256(name) + 80(value) + 16(entry) = 352 bytes,
 653         * The max space of acl xattr taken inline is
 654         * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
 655         * when blocksize = 512, may reserve one more cluser for
 656         * xattr bucket, otherwise reserve one metadata block
 657         * for them is ok.
 658         * If this is a new directory with inline data,
 659         * we choose to reserve the entire inline area for
 660         * directory contents and force an external xattr block.
 661         */
 662        if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
 663            (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) ||
 664            (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) {
 665                *want_meta = *want_meta + 1;
 666                *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
 667        }
 668
 669        if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
 670            (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
 671                *want_clusters += 1;
 672                *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
 673        }
 674
 675        /*
 676         * reserve credits and clusters for xattrs which has large value
 677         * and have to be set outside
 678         */
 679        if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE) {
 680                new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
 681                                                        si->value_len);
 682                *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
 683                                                           new_clusters);
 684                *want_clusters += new_clusters;
 685        }
 686        if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
 687            acl_len > OCFS2_XATTR_INLINE_SIZE) {
 688                /* for directory, it has DEFAULT and ACCESS two types of acls */
 689                new_clusters = (S_ISDIR(mode) ? 2 : 1) *
 690                                ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
 691                *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
 692                                                           new_clusters);
 693                *want_clusters += new_clusters;
 694        }
 695
 696        return ret;
 697}
 698
 699static int ocfs2_xattr_extend_allocation(struct inode *inode,
 700                                         u32 clusters_to_add,
 701                                         struct ocfs2_xattr_value_buf *vb,
 702                                         struct ocfs2_xattr_set_ctxt *ctxt)
 703{
 704        int status = 0, credits;
 705        handle_t *handle = ctxt->handle;
 706        enum ocfs2_alloc_restarted why;
 707        u32 prev_clusters, logical_start = le32_to_cpu(vb->vb_xv->xr_clusters);
 708        struct ocfs2_extent_tree et;
 709
 710        ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
 711
 712        while (clusters_to_add) {
 713                trace_ocfs2_xattr_extend_allocation(clusters_to_add);
 714
 715                status = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
 716                                       OCFS2_JOURNAL_ACCESS_WRITE);
 717                if (status < 0) {
 718                        mlog_errno(status);
 719                        break;
 720                }
 721
 722                prev_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
 723                status = ocfs2_add_clusters_in_btree(handle,
 724                                                     &et,
 725                                                     &logical_start,
 726                                                     clusters_to_add,
 727                                                     0,
 728                                                     ctxt->data_ac,
 729                                                     ctxt->meta_ac,
 730                                                     &why);
 731                if ((status < 0) && (status != -EAGAIN)) {
 732                        if (status != -ENOSPC)
 733                                mlog_errno(status);
 734                        break;
 735                }
 736
 737                ocfs2_journal_dirty(handle, vb->vb_bh);
 738
 739                clusters_to_add -= le32_to_cpu(vb->vb_xv->xr_clusters) -
 740                                         prev_clusters;
 741
 742                if (why != RESTART_NONE && clusters_to_add) {
 743                        /*
 744                         * We can only fail in case the alloc file doesn't give
 745                         * up enough clusters.
 746                         */
 747                        BUG_ON(why == RESTART_META);
 748
 749                        credits = ocfs2_calc_extend_credits(inode->i_sb,
 750                                                            &vb->vb_xv->xr_list);
 751                        status = ocfs2_extend_trans(handle, credits);
 752                        if (status < 0) {
 753                                status = -ENOMEM;
 754                                mlog_errno(status);
 755                                break;
 756                        }
 757                }
 758        }
 759
 760        return status;
 761}
 762
 763static int __ocfs2_remove_xattr_range(struct inode *inode,
 764                                      struct ocfs2_xattr_value_buf *vb,
 765                                      u32 cpos, u32 phys_cpos, u32 len,
 766                                      unsigned int ext_flags,
 767                                      struct ocfs2_xattr_set_ctxt *ctxt)
 768{
 769        int ret;
 770        u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
 771        handle_t *handle = ctxt->handle;
 772        struct ocfs2_extent_tree et;
 773
 774        ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
 775
 776        ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
 777                            OCFS2_JOURNAL_ACCESS_WRITE);
 778        if (ret) {
 779                mlog_errno(ret);
 780                goto out;
 781        }
 782
 783        ret = ocfs2_remove_extent(handle, &et, cpos, len, ctxt->meta_ac,
 784                                  &ctxt->dealloc);
 785        if (ret) {
 786                mlog_errno(ret);
 787                goto out;
 788        }
 789
 790        le32_add_cpu(&vb->vb_xv->xr_clusters, -len);
 791        ocfs2_journal_dirty(handle, vb->vb_bh);
 792
 793        if (ext_flags & OCFS2_EXT_REFCOUNTED)
 794                ret = ocfs2_decrease_refcount(inode, handle,
 795                                        ocfs2_blocks_to_clusters(inode->i_sb,
 796                                                                 phys_blkno),
 797                                        len, ctxt->meta_ac, &ctxt->dealloc, 1);
 798        else
 799                ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc,
 800                                                  phys_blkno, len);
 801        if (ret)
 802                mlog_errno(ret);
 803
 804out:
 805        return ret;
 806}
 807
 808static int ocfs2_xattr_shrink_size(struct inode *inode,
 809                                   u32 old_clusters,
 810                                   u32 new_clusters,
 811                                   struct ocfs2_xattr_value_buf *vb,
 812                                   struct ocfs2_xattr_set_ctxt *ctxt)
 813{
 814        int ret = 0;
 815        unsigned int ext_flags;
 816        u32 trunc_len, cpos, phys_cpos, alloc_size;
 817        u64 block;
 818
 819        if (old_clusters <= new_clusters)
 820                return 0;
 821
 822        cpos = new_clusters;
 823        trunc_len = old_clusters - new_clusters;
 824        while (trunc_len) {
 825                ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
 826                                               &alloc_size,
 827                                               &vb->vb_xv->xr_list, &ext_flags);
 828                if (ret) {
 829                        mlog_errno(ret);
 830                        goto out;
 831                }
 832
 833                if (alloc_size > trunc_len)
 834                        alloc_size = trunc_len;
 835
 836                ret = __ocfs2_remove_xattr_range(inode, vb, cpos,
 837                                                 phys_cpos, alloc_size,
 838                                                 ext_flags, ctxt);
 839                if (ret) {
 840                        mlog_errno(ret);
 841                        goto out;
 842                }
 843
 844                block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
 845                ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode),
 846                                                       block, alloc_size);
 847                cpos += alloc_size;
 848                trunc_len -= alloc_size;
 849        }
 850
 851out:
 852        return ret;
 853}
 854
 855static int ocfs2_xattr_value_truncate(struct inode *inode,
 856                                      struct ocfs2_xattr_value_buf *vb,
 857                                      int len,
 858                                      struct ocfs2_xattr_set_ctxt *ctxt)
 859{
 860        int ret;
 861        u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
 862        u32 old_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
 863
 864        if (new_clusters == old_clusters)
 865                return 0;
 866
 867        if (new_clusters > old_clusters)
 868                ret = ocfs2_xattr_extend_allocation(inode,
 869                                                    new_clusters - old_clusters,
 870                                                    vb, ctxt);
 871        else
 872                ret = ocfs2_xattr_shrink_size(inode,
 873                                              old_clusters, new_clusters,
 874                                              vb, ctxt);
 875
 876        return ret;
 877}
 878
 879static int ocfs2_xattr_list_entry(struct super_block *sb,
 880                                  char *buffer, size_t size,
 881                                  size_t *result, int type,
 882                                  const char *name, int name_len)
 883{
 884        char *p = buffer + *result;
 885        const char *prefix;
 886        int prefix_len;
 887        int total_len;
 888
 889        switch(type) {
 890        case OCFS2_XATTR_INDEX_USER:
 891                if (OCFS2_SB(sb)->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
 892                        return 0;
 893                break;
 894
 895        case OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS:
 896        case OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT:
 897                if (!(sb->s_flags & SB_POSIXACL))
 898                        return 0;
 899                break;
 900
 901        case OCFS2_XATTR_INDEX_TRUSTED:
 902                if (!capable(CAP_SYS_ADMIN))
 903                        return 0;
 904                break;
 905        }
 906
 907        prefix = ocfs2_xattr_prefix(type);
 908        if (!prefix)
 909                return 0;
 910        prefix_len = strlen(prefix);
 911        total_len = prefix_len + name_len + 1;
 912        *result += total_len;
 913
 914        /* we are just looking for how big our buffer needs to be */
 915        if (!size)
 916                return 0;
 917
 918        if (*result > size)
 919                return -ERANGE;
 920
 921        memcpy(p, prefix, prefix_len);
 922        memcpy(p + prefix_len, name, name_len);
 923        p[prefix_len + name_len] = '\0';
 924
 925        return 0;
 926}
 927
 928static int ocfs2_xattr_list_entries(struct inode *inode,
 929                                    struct ocfs2_xattr_header *header,
 930                                    char *buffer, size_t buffer_size)
 931{
 932        size_t result = 0;
 933        int i, type, ret;
 934        const char *name;
 935
 936        for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
 937                struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
 938                type = ocfs2_xattr_get_type(entry);
 939                name = (const char *)header +
 940                        le16_to_cpu(entry->xe_name_offset);
 941
 942                ret = ocfs2_xattr_list_entry(inode->i_sb,
 943                                             buffer, buffer_size,
 944                                             &result, type, name,
 945                                             entry->xe_name_len);
 946                if (ret)
 947                        return ret;
 948        }
 949
 950        return result;
 951}
 952
 953int ocfs2_has_inline_xattr_value_outside(struct inode *inode,
 954                                         struct ocfs2_dinode *di)
 955{
 956        struct ocfs2_xattr_header *xh;
 957        int i;
 958
 959        xh = (struct ocfs2_xattr_header *)
 960                 ((void *)di + inode->i_sb->s_blocksize -
 961                 le16_to_cpu(di->i_xattr_inline_size));
 962
 963        for (i = 0; i < le16_to_cpu(xh->xh_count); i++)
 964                if (!ocfs2_xattr_is_local(&xh->xh_entries[i]))
 965                        return 1;
 966
 967        return 0;
 968}
 969
 970static int ocfs2_xattr_ibody_list(struct inode *inode,
 971                                  struct ocfs2_dinode *di,
 972                                  char *buffer,
 973                                  size_t buffer_size)
 974{
 975        struct ocfs2_xattr_header *header = NULL;
 976        struct ocfs2_inode_info *oi = OCFS2_I(inode);
 977        int ret = 0;
 978
 979        if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
 980                return ret;
 981
 982        header = (struct ocfs2_xattr_header *)
 983                 ((void *)di + inode->i_sb->s_blocksize -
 984                 le16_to_cpu(di->i_xattr_inline_size));
 985
 986        ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
 987
 988        return ret;
 989}
 990
 991static int ocfs2_xattr_block_list(struct inode *inode,
 992                                  struct ocfs2_dinode *di,
 993                                  char *buffer,
 994                                  size_t buffer_size)
 995{
 996        struct buffer_head *blk_bh = NULL;
 997        struct ocfs2_xattr_block *xb;
 998        int ret = 0;
 999
1000        if (!di->i_xattr_loc)
1001                return ret;
1002
1003        ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
1004                                     &blk_bh);
1005        if (ret < 0) {
1006                mlog_errno(ret);
1007                return ret;
1008        }
1009
1010        xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1011        if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1012                struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
1013                ret = ocfs2_xattr_list_entries(inode, header,
1014                                               buffer, buffer_size);
1015        } else
1016                ret = ocfs2_xattr_tree_list_index_block(inode, blk_bh,
1017                                                   buffer, buffer_size);
1018
1019        brelse(blk_bh);
1020
1021        return ret;
1022}
1023
1024ssize_t ocfs2_listxattr(struct dentry *dentry,
1025                        char *buffer,
1026                        size_t size)
1027{
1028        int ret = 0, i_ret = 0, b_ret = 0;
1029        struct buffer_head *di_bh = NULL;
1030        struct ocfs2_dinode *di = NULL;
1031        struct ocfs2_inode_info *oi = OCFS2_I(d_inode(dentry));
1032
1033        if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
1034                return -EOPNOTSUPP;
1035
1036        if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1037                return ret;
1038
1039        ret = ocfs2_inode_lock(d_inode(dentry), &di_bh, 0);
1040        if (ret < 0) {
1041                mlog_errno(ret);
1042                return ret;
1043        }
1044
1045        di = (struct ocfs2_dinode *)di_bh->b_data;
1046
1047        down_read(&oi->ip_xattr_sem);
1048        i_ret = ocfs2_xattr_ibody_list(d_inode(dentry), di, buffer, size);
1049        if (i_ret < 0)
1050                b_ret = 0;
1051        else {
1052                if (buffer) {
1053                        buffer += i_ret;
1054                        size -= i_ret;
1055                }
1056                b_ret = ocfs2_xattr_block_list(d_inode(dentry), di,
1057                                               buffer, size);
1058                if (b_ret < 0)
1059                        i_ret = 0;
1060        }
1061        up_read(&oi->ip_xattr_sem);
1062        ocfs2_inode_unlock(d_inode(dentry), 0);
1063
1064        brelse(di_bh);
1065
1066        return i_ret + b_ret;
1067}
1068
1069static int ocfs2_xattr_find_entry(int name_index,
1070                                  const char *name,
1071                                  struct ocfs2_xattr_search *xs)
1072{
1073        struct ocfs2_xattr_entry *entry;
1074        size_t name_len;
1075        int i, cmp = 1;
1076
1077        if (name == NULL)
1078                return -EINVAL;
1079
1080        name_len = strlen(name);
1081        entry = xs->here;
1082        for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
1083                cmp = name_index - ocfs2_xattr_get_type(entry);
1084                if (!cmp)
1085                        cmp = name_len - entry->xe_name_len;
1086                if (!cmp)
1087                        cmp = memcmp(name, (xs->base +
1088                                     le16_to_cpu(entry->xe_name_offset)),
1089                                     name_len);
1090                if (cmp == 0)
1091                        break;
1092                entry += 1;
1093        }
1094        xs->here = entry;
1095
1096        return cmp ? -ENODATA : 0;
1097}
1098
1099static int ocfs2_xattr_get_value_outside(struct inode *inode,
1100                                         struct ocfs2_xattr_value_root *xv,
1101                                         void *buffer,
1102                                         size_t len)
1103{
1104        u32 cpos, p_cluster, num_clusters, bpc, clusters;
1105        u64 blkno;
1106        int i, ret = 0;
1107        size_t cplen, blocksize;
1108        struct buffer_head *bh = NULL;
1109        struct ocfs2_extent_list *el;
1110
1111        el = &xv->xr_list;
1112        clusters = le32_to_cpu(xv->xr_clusters);
1113        bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1114        blocksize = inode->i_sb->s_blocksize;
1115
1116        cpos = 0;
1117        while (cpos < clusters) {
1118                ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1119                                               &num_clusters, el, NULL);
1120                if (ret) {
1121                        mlog_errno(ret);
1122                        goto out;
1123                }
1124
1125                blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1126                /* Copy ocfs2_xattr_value */
1127                for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1128                        ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1129                                               &bh, NULL);
1130                        if (ret) {
1131                                mlog_errno(ret);
1132                                goto out;
1133                        }
1134
1135                        cplen = len >= blocksize ? blocksize : len;
1136                        memcpy(buffer, bh->b_data, cplen);
1137                        len -= cplen;
1138                        buffer += cplen;
1139
1140                        brelse(bh);
1141                        bh = NULL;
1142                        if (len == 0)
1143                                break;
1144                }
1145                cpos += num_clusters;
1146        }
1147out:
1148        return ret;
1149}
1150
1151static int ocfs2_xattr_ibody_get(struct inode *inode,
1152                                 int name_index,
1153                                 const char *name,
1154                                 void *buffer,
1155                                 size_t buffer_size,
1156                                 struct ocfs2_xattr_search *xs)
1157{
1158        struct ocfs2_inode_info *oi = OCFS2_I(inode);
1159        struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1160        struct ocfs2_xattr_value_root *xv;
1161        size_t size;
1162        int ret = 0;
1163
1164        if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
1165                return -ENODATA;
1166
1167        xs->end = (void *)di + inode->i_sb->s_blocksize;
1168        xs->header = (struct ocfs2_xattr_header *)
1169                        (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1170        xs->base = (void *)xs->header;
1171        xs->here = xs->header->xh_entries;
1172
1173        ret = ocfs2_xattr_find_entry(name_index, name, xs);
1174        if (ret)
1175                return ret;
1176        size = le64_to_cpu(xs->here->xe_value_size);
1177        if (buffer) {
1178                if (size > buffer_size)
1179                        return -ERANGE;
1180                if (ocfs2_xattr_is_local(xs->here)) {
1181                        memcpy(buffer, (void *)xs->base +
1182                               le16_to_cpu(xs->here->xe_name_offset) +
1183                               OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
1184                } else {
1185                        xv = (struct ocfs2_xattr_value_root *)
1186                                (xs->base + le16_to_cpu(
1187                                 xs->here->xe_name_offset) +
1188                                OCFS2_XATTR_SIZE(xs->here->xe_name_len));
1189                        ret = ocfs2_xattr_get_value_outside(inode, xv,
1190                                                            buffer, size);
1191                        if (ret < 0) {
1192                                mlog_errno(ret);
1193                                return ret;
1194                        }
1195                }
1196        }
1197
1198        return size;
1199}
1200
1201static int ocfs2_xattr_block_get(struct inode *inode,
1202                                 int name_index,
1203                                 const char *name,
1204                                 void *buffer,
1205                                 size_t buffer_size,
1206                                 struct ocfs2_xattr_search *xs)
1207{
1208        struct ocfs2_xattr_block *xb;
1209        struct ocfs2_xattr_value_root *xv;
1210        size_t size;
1211        int ret = -ENODATA, name_offset, name_len, i;
1212        int block_off;
1213
1214        xs->bucket = ocfs2_xattr_bucket_new(inode);
1215        if (!xs->bucket) {
1216                ret = -ENOMEM;
1217                mlog_errno(ret);
1218                goto cleanup;
1219        }
1220
1221        ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
1222        if (ret) {
1223                mlog_errno(ret);
1224                goto cleanup;
1225        }
1226
1227        if (xs->not_found) {
1228                ret = -ENODATA;
1229                goto cleanup;
1230        }
1231
1232        xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1233        size = le64_to_cpu(xs->here->xe_value_size);
1234        if (buffer) {
1235                ret = -ERANGE;
1236                if (size > buffer_size)
1237                        goto cleanup;
1238
1239                name_offset = le16_to_cpu(xs->here->xe_name_offset);
1240                name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
1241                i = xs->here - xs->header->xh_entries;
1242
1243                if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
1244                        ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
1245                                                                bucket_xh(xs->bucket),
1246                                                                i,
1247                                                                &block_off,
1248                                                                &name_offset);
1249                        if (ret) {
1250                                mlog_errno(ret);
1251                                goto cleanup;
1252                        }
1253                        xs->base = bucket_block(xs->bucket, block_off);
1254                }
1255                if (ocfs2_xattr_is_local(xs->here)) {
1256                        memcpy(buffer, (void *)xs->base +
1257                               name_offset + name_len, size);
1258                } else {
1259                        xv = (struct ocfs2_xattr_value_root *)
1260                                (xs->base + name_offset + name_len);
1261                        ret = ocfs2_xattr_get_value_outside(inode, xv,
1262                                                            buffer, size);
1263                        if (ret < 0) {
1264                                mlog_errno(ret);
1265                                goto cleanup;
1266                        }
1267                }
1268        }
1269        ret = size;
1270cleanup:
1271        ocfs2_xattr_bucket_free(xs->bucket);
1272
1273        brelse(xs->xattr_bh);
1274        xs->xattr_bh = NULL;
1275        return ret;
1276}
1277
1278int ocfs2_xattr_get_nolock(struct inode *inode,
1279                           struct buffer_head *di_bh,
1280                           int name_index,
1281                           const char *name,
1282                           void *buffer,
1283                           size_t buffer_size)
1284{
1285        int ret;
1286        struct ocfs2_dinode *di = NULL;
1287        struct ocfs2_inode_info *oi = OCFS2_I(inode);
1288        struct ocfs2_xattr_search xis = {
1289                .not_found = -ENODATA,
1290        };
1291        struct ocfs2_xattr_search xbs = {
1292                .not_found = -ENODATA,
1293        };
1294
1295        if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1296                return -EOPNOTSUPP;
1297
1298        if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1299                return -ENODATA;
1300
1301        xis.inode_bh = xbs.inode_bh = di_bh;
1302        di = (struct ocfs2_dinode *)di_bh->b_data;
1303
1304        ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1305                                    buffer_size, &xis);
1306        if (ret == -ENODATA && di->i_xattr_loc)
1307                ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1308                                            buffer_size, &xbs);
1309
1310        return ret;
1311}
1312
1313/* ocfs2_xattr_get()
1314 *
1315 * Copy an extended attribute into the buffer provided.
1316 * Buffer is NULL to compute the size of buffer required.
1317 */
1318static int ocfs2_xattr_get(struct inode *inode,
1319                           int name_index,
1320                           const char *name,
1321                           void *buffer,
1322                           size_t buffer_size)
1323{
1324        int ret, had_lock;
1325        struct buffer_head *di_bh = NULL;
1326        struct ocfs2_lock_holder oh;
1327
1328        had_lock = ocfs2_inode_lock_tracker(inode, &di_bh, 0, &oh);
1329        if (had_lock < 0) {
1330                mlog_errno(had_lock);
1331                return had_lock;
1332        }
1333        down_read(&OCFS2_I(inode)->ip_xattr_sem);
1334        ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
1335                                     name, buffer, buffer_size);
1336        up_read(&OCFS2_I(inode)->ip_xattr_sem);
1337
1338        ocfs2_inode_unlock_tracker(inode, 0, &oh, had_lock);
1339
1340        brelse(di_bh);
1341
1342        return ret;
1343}
1344
1345static int __ocfs2_xattr_set_value_outside(struct inode *inode,
1346                                           handle_t *handle,
1347                                           struct ocfs2_xattr_value_buf *vb,
1348                                           const void *value,
1349                                           int value_len)
1350{
1351        int ret = 0, i, cp_len;
1352        u16 blocksize = inode->i_sb->s_blocksize;
1353        u32 p_cluster, num_clusters;
1354        u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1355        u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1356        u64 blkno;
1357        struct buffer_head *bh = NULL;
1358        unsigned int ext_flags;
1359        struct ocfs2_xattr_value_root *xv = vb->vb_xv;
1360
1361        BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1362
1363        while (cpos < clusters) {
1364                ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1365                                               &num_clusters, &xv->xr_list,
1366                                               &ext_flags);
1367                if (ret) {
1368                        mlog_errno(ret);
1369                        goto out;
1370                }
1371
1372                BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED);
1373
1374                blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1375
1376                for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1377                        ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1378                                               &bh, NULL);
1379                        if (ret) {
1380                                mlog_errno(ret);
1381                                goto out;
1382                        }
1383
1384                        ret = ocfs2_journal_access(handle,
1385                                                   INODE_CACHE(inode),
1386                                                   bh,
1387                                                   OCFS2_JOURNAL_ACCESS_WRITE);
1388                        if (ret < 0) {
1389                                mlog_errno(ret);
1390                                goto out;
1391                        }
1392
1393                        cp_len = value_len > blocksize ? blocksize : value_len;
1394                        memcpy(bh->b_data, value, cp_len);
1395                        value_len -= cp_len;
1396                        value += cp_len;
1397                        if (cp_len < blocksize)
1398                                memset(bh->b_data + cp_len, 0,
1399                                       blocksize - cp_len);
1400
1401                        ocfs2_journal_dirty(handle, bh);
1402                        brelse(bh);
1403                        bh = NULL;
1404
1405                        /*
1406                         * XXX: do we need to empty all the following
1407                         * blocks in this cluster?
1408                         */
1409                        if (!value_len)
1410                                break;
1411                }
1412                cpos += num_clusters;
1413        }
1414out:
1415        brelse(bh);
1416
1417        return ret;
1418}
1419
1420static int ocfs2_xa_check_space_helper(int needed_space, int free_start,
1421                                       int num_entries)
1422{
1423        int free_space;
1424
1425        if (!needed_space)
1426                return 0;
1427
1428        free_space = free_start -
1429                sizeof(struct ocfs2_xattr_header) -
1430                (num_entries * sizeof(struct ocfs2_xattr_entry)) -
1431                OCFS2_XATTR_HEADER_GAP;
1432        if (free_space < 0)
1433                return -EIO;
1434        if (free_space < needed_space)
1435                return -ENOSPC;
1436
1437        return 0;
1438}
1439
1440static int ocfs2_xa_journal_access(handle_t *handle, struct ocfs2_xa_loc *loc,
1441                                   int type)
1442{
1443        return loc->xl_ops->xlo_journal_access(handle, loc, type);
1444}
1445
1446static void ocfs2_xa_journal_dirty(handle_t *handle, struct ocfs2_xa_loc *loc)
1447{
1448        loc->xl_ops->xlo_journal_dirty(handle, loc);
1449}
1450
1451/* Give a pointer into the storage for the given offset */
1452static void *ocfs2_xa_offset_pointer(struct ocfs2_xa_loc *loc, int offset)
1453{
1454        BUG_ON(offset >= loc->xl_size);
1455        return loc->xl_ops->xlo_offset_pointer(loc, offset);
1456}
1457
1458/*
1459 * Wipe the name+value pair and allow the storage to reclaim it.  This
1460 * must be followed by either removal of the entry or a call to
1461 * ocfs2_xa_add_namevalue().
1462 */
1463static void ocfs2_xa_wipe_namevalue(struct ocfs2_xa_loc *loc)
1464{
1465        loc->xl_ops->xlo_wipe_namevalue(loc);
1466}
1467
1468/*
1469 * Find lowest offset to a name+value pair.  This is the start of our
1470 * downward-growing free space.
1471 */
1472static int ocfs2_xa_get_free_start(struct ocfs2_xa_loc *loc)
1473{
1474        return loc->xl_ops->xlo_get_free_start(loc);
1475}
1476
1477/* Can we reuse loc->xl_entry for xi? */
1478static int ocfs2_xa_can_reuse_entry(struct ocfs2_xa_loc *loc,
1479                                    struct ocfs2_xattr_info *xi)
1480{
1481        return loc->xl_ops->xlo_can_reuse(loc, xi);
1482}
1483
1484/* How much free space is needed to set the new value */
1485static int ocfs2_xa_check_space(struct ocfs2_xa_loc *loc,
1486                                struct ocfs2_xattr_info *xi)
1487{
1488        return loc->xl_ops->xlo_check_space(loc, xi);
1489}
1490
1491static void ocfs2_xa_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1492{
1493        loc->xl_ops->xlo_add_entry(loc, name_hash);
1494        loc->xl_entry->xe_name_hash = cpu_to_le32(name_hash);
1495        /*
1496         * We can't leave the new entry's xe_name_offset at zero or
1497         * add_namevalue() will go nuts.  We set it to the size of our
1498         * storage so that it can never be less than any other entry.
1499         */
1500        loc->xl_entry->xe_name_offset = cpu_to_le16(loc->xl_size);
1501}
1502
1503static void ocfs2_xa_add_namevalue(struct ocfs2_xa_loc *loc,
1504                                   struct ocfs2_xattr_info *xi)
1505{
1506        int size = namevalue_size_xi(xi);
1507        int nameval_offset;
1508        char *nameval_buf;
1509
1510        loc->xl_ops->xlo_add_namevalue(loc, size);
1511        loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
1512        loc->xl_entry->xe_name_len = xi->xi_name_len;
1513        ocfs2_xattr_set_type(loc->xl_entry, xi->xi_name_index);
1514        ocfs2_xattr_set_local(loc->xl_entry,
1515                              xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE);
1516
1517        nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1518        nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
1519        memset(nameval_buf, 0, size);
1520        memcpy(nameval_buf, xi->xi_name, xi->xi_name_len);
1521}
1522
1523static void ocfs2_xa_fill_value_buf(struct ocfs2_xa_loc *loc,
1524                                    struct ocfs2_xattr_value_buf *vb)
1525{
1526        int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1527        int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len);
1528
1529        /* Value bufs are for value trees */
1530        BUG_ON(ocfs2_xattr_is_local(loc->xl_entry));
1531        BUG_ON(namevalue_size_xe(loc->xl_entry) !=
1532               (name_size + OCFS2_XATTR_ROOT_SIZE));
1533
1534        loc->xl_ops->xlo_fill_value_buf(loc, vb);
1535        vb->vb_xv =
1536                (struct ocfs2_xattr_value_root *)ocfs2_xa_offset_pointer(loc,
1537                                                        nameval_offset +
1538                                                        name_size);
1539}
1540
1541static int ocfs2_xa_block_journal_access(handle_t *handle,
1542                                         struct ocfs2_xa_loc *loc, int type)
1543{
1544        struct buffer_head *bh = loc->xl_storage;
1545        ocfs2_journal_access_func access;
1546
1547        if (loc->xl_size == (bh->b_size -
1548                             offsetof(struct ocfs2_xattr_block,
1549                                      xb_attrs.xb_header)))
1550                access = ocfs2_journal_access_xb;
1551        else
1552                access = ocfs2_journal_access_di;
1553        return access(handle, INODE_CACHE(loc->xl_inode), bh, type);
1554}
1555
1556static void ocfs2_xa_block_journal_dirty(handle_t *handle,
1557                                         struct ocfs2_xa_loc *loc)
1558{
1559        struct buffer_head *bh = loc->xl_storage;
1560
1561        ocfs2_journal_dirty(handle, bh);
1562}
1563
1564static void *ocfs2_xa_block_offset_pointer(struct ocfs2_xa_loc *loc,
1565                                           int offset)
1566{
1567        return (char *)loc->xl_header + offset;
1568}
1569
1570static int ocfs2_xa_block_can_reuse(struct ocfs2_xa_loc *loc,
1571                                    struct ocfs2_xattr_info *xi)
1572{
1573        /*
1574         * Block storage is strict.  If the sizes aren't exact, we will
1575         * remove the old one and reinsert the new.
1576         */
1577        return namevalue_size_xe(loc->xl_entry) ==
1578                namevalue_size_xi(xi);
1579}
1580
1581static int ocfs2_xa_block_get_free_start(struct ocfs2_xa_loc *loc)
1582{
1583        struct ocfs2_xattr_header *xh = loc->xl_header;
1584        int i, count = le16_to_cpu(xh->xh_count);
1585        int offset, free_start = loc->xl_size;
1586
1587        for (i = 0; i < count; i++) {
1588                offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1589                if (offset < free_start)
1590                        free_start = offset;
1591        }
1592
1593        return free_start;
1594}
1595
1596static int ocfs2_xa_block_check_space(struct ocfs2_xa_loc *loc,
1597                                      struct ocfs2_xattr_info *xi)
1598{
1599        int count = le16_to_cpu(loc->xl_header->xh_count);
1600        int free_start = ocfs2_xa_get_free_start(loc);
1601        int needed_space = ocfs2_xi_entry_usage(xi);
1602
1603        /*
1604         * Block storage will reclaim the original entry before inserting
1605         * the new value, so we only need the difference.  If the new
1606         * entry is smaller than the old one, we don't need anything.
1607         */
1608        if (loc->xl_entry) {
1609                /* Don't need space if we're reusing! */
1610                if (ocfs2_xa_can_reuse_entry(loc, xi))
1611                        needed_space = 0;
1612                else
1613                        needed_space -= ocfs2_xe_entry_usage(loc->xl_entry);
1614        }
1615        if (needed_space < 0)
1616                needed_space = 0;
1617        return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1618}
1619
1620/*
1621 * Block storage for xattrs keeps the name+value pairs compacted.  When
1622 * we remove one, we have to shift any that preceded it towards the end.
1623 */
1624static void ocfs2_xa_block_wipe_namevalue(struct ocfs2_xa_loc *loc)
1625{
1626        int i, offset;
1627        int namevalue_offset, first_namevalue_offset, namevalue_size;
1628        struct ocfs2_xattr_entry *entry = loc->xl_entry;
1629        struct ocfs2_xattr_header *xh = loc->xl_header;
1630        int count = le16_to_cpu(xh->xh_count);
1631
1632        namevalue_offset = le16_to_cpu(entry->xe_name_offset);
1633        namevalue_size = namevalue_size_xe(entry);
1634        first_namevalue_offset = ocfs2_xa_get_free_start(loc);
1635
1636        /* Shift the name+value pairs */
1637        memmove((char *)xh + first_namevalue_offset + namevalue_size,
1638                (char *)xh + first_namevalue_offset,
1639                namevalue_offset - first_namevalue_offset);
1640        memset((char *)xh + first_namevalue_offset, 0, namevalue_size);
1641
1642        /* Now tell xh->xh_entries about it */
1643        for (i = 0; i < count; i++) {
1644                offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1645                if (offset <= namevalue_offset)
1646                        le16_add_cpu(&xh->xh_entries[i].xe_name_offset,
1647                                     namevalue_size);
1648        }
1649
1650        /*
1651         * Note that we don't update xh_free_start or xh_name_value_len
1652         * because they're not used in block-stored xattrs.
1653         */
1654}
1655
1656static void ocfs2_xa_block_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1657{
1658        int count = le16_to_cpu(loc->xl_header->xh_count);
1659        loc->xl_entry = &(loc->xl_header->xh_entries[count]);
1660        le16_add_cpu(&loc->xl_header->xh_count, 1);
1661        memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1662}
1663
1664static void ocfs2_xa_block_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1665{
1666        int free_start = ocfs2_xa_get_free_start(loc);
1667
1668        loc->xl_entry->xe_name_offset = cpu_to_le16(free_start - size);
1669}
1670
1671static void ocfs2_xa_block_fill_value_buf(struct ocfs2_xa_loc *loc,
1672                                          struct ocfs2_xattr_value_buf *vb)
1673{
1674        struct buffer_head *bh = loc->xl_storage;
1675
1676        if (loc->xl_size == (bh->b_size -
1677                             offsetof(struct ocfs2_xattr_block,
1678                                      xb_attrs.xb_header)))
1679                vb->vb_access = ocfs2_journal_access_xb;
1680        else
1681                vb->vb_access = ocfs2_journal_access_di;
1682        vb->vb_bh = bh;
1683}
1684
1685/*
1686 * Operations for xattrs stored in blocks.  This includes inline inode
1687 * storage and unindexed ocfs2_xattr_blocks.
1688 */
1689static const struct ocfs2_xa_loc_operations ocfs2_xa_block_loc_ops = {
1690        .xlo_journal_access     = ocfs2_xa_block_journal_access,
1691        .xlo_journal_dirty      = ocfs2_xa_block_journal_dirty,
1692        .xlo_offset_pointer     = ocfs2_xa_block_offset_pointer,
1693        .xlo_check_space        = ocfs2_xa_block_check_space,
1694        .xlo_can_reuse          = ocfs2_xa_block_can_reuse,
1695        .xlo_get_free_start     = ocfs2_xa_block_get_free_start,
1696        .xlo_wipe_namevalue     = ocfs2_xa_block_wipe_namevalue,
1697        .xlo_add_entry          = ocfs2_xa_block_add_entry,
1698        .xlo_add_namevalue      = ocfs2_xa_block_add_namevalue,
1699        .xlo_fill_value_buf     = ocfs2_xa_block_fill_value_buf,
1700};
1701
1702static int ocfs2_xa_bucket_journal_access(handle_t *handle,
1703                                          struct ocfs2_xa_loc *loc, int type)
1704{
1705        struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1706
1707        return ocfs2_xattr_bucket_journal_access(handle, bucket, type);
1708}
1709
1710static void ocfs2_xa_bucket_journal_dirty(handle_t *handle,
1711                                          struct ocfs2_xa_loc *loc)
1712{
1713        struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1714
1715        ocfs2_xattr_bucket_journal_dirty(handle, bucket);
1716}
1717
1718static void *ocfs2_xa_bucket_offset_pointer(struct ocfs2_xa_loc *loc,
1719                                            int offset)
1720{
1721        struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1722        int block, block_offset;
1723
1724        /* The header is at the front of the bucket */
1725        block = offset >> loc->xl_inode->i_sb->s_blocksize_bits;
1726        block_offset = offset % loc->xl_inode->i_sb->s_blocksize;
1727
1728        return bucket_block(bucket, block) + block_offset;
1729}
1730
1731static int ocfs2_xa_bucket_can_reuse(struct ocfs2_xa_loc *loc,
1732                                     struct ocfs2_xattr_info *xi)
1733{
1734        return namevalue_size_xe(loc->xl_entry) >=
1735                namevalue_size_xi(xi);
1736}
1737
1738static int ocfs2_xa_bucket_get_free_start(struct ocfs2_xa_loc *loc)
1739{
1740        struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1741        return le16_to_cpu(bucket_xh(bucket)->xh_free_start);
1742}
1743
1744static int ocfs2_bucket_align_free_start(struct super_block *sb,
1745                                         int free_start, int size)
1746{
1747        /*
1748         * We need to make sure that the name+value pair fits within
1749         * one block.
1750         */
1751        if (((free_start - size) >> sb->s_blocksize_bits) !=
1752            ((free_start - 1) >> sb->s_blocksize_bits))
1753                free_start -= free_start % sb->s_blocksize;
1754
1755        return free_start;
1756}
1757
1758static int ocfs2_xa_bucket_check_space(struct ocfs2_xa_loc *loc,
1759                                       struct ocfs2_xattr_info *xi)
1760{
1761        int rc;
1762        int count = le16_to_cpu(loc->xl_header->xh_count);
1763        int free_start = ocfs2_xa_get_free_start(loc);
1764        int needed_space = ocfs2_xi_entry_usage(xi);
1765        int size = namevalue_size_xi(xi);
1766        struct super_block *sb = loc->xl_inode->i_sb;
1767
1768        /*
1769         * Bucket storage does not reclaim name+value pairs it cannot
1770         * reuse.  They live as holes until the bucket fills, and then
1771         * the bucket is defragmented.  However, the bucket can reclaim
1772         * the ocfs2_xattr_entry.
1773         */
1774        if (loc->xl_entry) {
1775                /* Don't need space if we're reusing! */
1776                if (ocfs2_xa_can_reuse_entry(loc, xi))
1777                        needed_space = 0;
1778                else
1779                        needed_space -= sizeof(struct ocfs2_xattr_entry);
1780        }
1781        BUG_ON(needed_space < 0);
1782
1783        if (free_start < size) {
1784                if (needed_space)
1785                        return -ENOSPC;
1786        } else {
1787                /*
1788                 * First we check if it would fit in the first place.
1789                 * Below, we align the free start to a block.  This may
1790                 * slide us below the minimum gap.  By checking unaligned
1791                 * first, we avoid that error.
1792                 */
1793                rc = ocfs2_xa_check_space_helper(needed_space, free_start,
1794                                                 count);
1795                if (rc)
1796                        return rc;
1797                free_start = ocfs2_bucket_align_free_start(sb, free_start,
1798                                                           size);
1799        }
1800        return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1801}
1802
1803static void ocfs2_xa_bucket_wipe_namevalue(struct ocfs2_xa_loc *loc)
1804{
1805        le16_add_cpu(&loc->xl_header->xh_name_value_len,
1806                     -namevalue_size_xe(loc->xl_entry));
1807}
1808
1809static void ocfs2_xa_bucket_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1810{
1811        struct ocfs2_xattr_header *xh = loc->xl_header;
1812        int count = le16_to_cpu(xh->xh_count);
1813        int low = 0, high = count - 1, tmp;
1814        struct ocfs2_xattr_entry *tmp_xe;
1815
1816        /*
1817         * We keep buckets sorted by name_hash, so we need to find
1818         * our insert place.
1819         */
1820        while (low <= high && count) {
1821                tmp = (low + high) / 2;
1822                tmp_xe = &xh->xh_entries[tmp];
1823
1824                if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
1825                        low = tmp + 1;
1826                else if (name_hash < le32_to_cpu(tmp_xe->xe_name_hash))
1827                        high = tmp - 1;
1828                else {
1829                        low = tmp;
1830                        break;
1831                }
1832        }
1833
1834        if (low != count)
1835                memmove(&xh->xh_entries[low + 1],
1836                        &xh->xh_entries[low],
1837                        ((count - low) * sizeof(struct ocfs2_xattr_entry)));
1838
1839        le16_add_cpu(&xh->xh_count, 1);
1840        loc->xl_entry = &xh->xh_entries[low];
1841        memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1842}
1843
1844static void ocfs2_xa_bucket_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1845{
1846        int free_start = ocfs2_xa_get_free_start(loc);
1847        struct ocfs2_xattr_header *xh = loc->xl_header;
1848        struct super_block *sb = loc->xl_inode->i_sb;
1849        int nameval_offset;
1850
1851        free_start = ocfs2_bucket_align_free_start(sb, free_start, size);
1852        nameval_offset = free_start - size;
1853        loc->xl_entry->xe_name_offset = cpu_to_le16(nameval_offset);
1854        xh->xh_free_start = cpu_to_le16(nameval_offset);
1855        le16_add_cpu(&xh->xh_name_value_len, size);
1856
1857}
1858
1859static void ocfs2_xa_bucket_fill_value_buf(struct ocfs2_xa_loc *loc,
1860                                           struct ocfs2_xattr_value_buf *vb)
1861{
1862        struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1863        struct super_block *sb = loc->xl_inode->i_sb;
1864        int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1865        int size = namevalue_size_xe(loc->xl_entry);
1866        int block_offset = nameval_offset >> sb->s_blocksize_bits;
1867
1868        /* Values are not allowed to straddle block boundaries */
1869        BUG_ON(block_offset !=
1870               ((nameval_offset + size - 1) >> sb->s_blocksize_bits));
1871        /* We expect the bucket to be filled in */
1872        BUG_ON(!bucket->bu_bhs[block_offset]);
1873
1874        vb->vb_access = ocfs2_journal_access;
1875        vb->vb_bh = bucket->bu_bhs[block_offset];
1876}
1877
1878/* Operations for xattrs stored in buckets. */
1879static const struct ocfs2_xa_loc_operations ocfs2_xa_bucket_loc_ops = {
1880        .xlo_journal_access     = ocfs2_xa_bucket_journal_access,
1881        .xlo_journal_dirty      = ocfs2_xa_bucket_journal_dirty,
1882        .xlo_offset_pointer     = ocfs2_xa_bucket_offset_pointer,
1883        .xlo_check_space        = ocfs2_xa_bucket_check_space,
1884        .xlo_can_reuse          = ocfs2_xa_bucket_can_reuse,
1885        .xlo_get_free_start     = ocfs2_xa_bucket_get_free_start,
1886        .xlo_wipe_namevalue     = ocfs2_xa_bucket_wipe_namevalue,
1887        .xlo_add_entry          = ocfs2_xa_bucket_add_entry,
1888        .xlo_add_namevalue      = ocfs2_xa_bucket_add_namevalue,
1889        .xlo_fill_value_buf     = ocfs2_xa_bucket_fill_value_buf,
1890};
1891
1892static unsigned int ocfs2_xa_value_clusters(struct ocfs2_xa_loc *loc)
1893{
1894        struct ocfs2_xattr_value_buf vb;
1895
1896        if (ocfs2_xattr_is_local(loc->xl_entry))
1897                return 0;
1898
1899        ocfs2_xa_fill_value_buf(loc, &vb);
1900        return le32_to_cpu(vb.vb_xv->xr_clusters);
1901}
1902
1903static int ocfs2_xa_value_truncate(struct ocfs2_xa_loc *loc, u64 bytes,
1904                                   struct ocfs2_xattr_set_ctxt *ctxt)
1905{
1906        int trunc_rc, access_rc;
1907        struct ocfs2_xattr_value_buf vb;
1908
1909        ocfs2_xa_fill_value_buf(loc, &vb);
1910        trunc_rc = ocfs2_xattr_value_truncate(loc->xl_inode, &vb, bytes,
1911                                              ctxt);
1912
1913        /*
1914         * The caller of ocfs2_xa_value_truncate() has already called
1915         * ocfs2_xa_journal_access on the loc.  However, The truncate code
1916         * calls ocfs2_extend_trans().  This may commit the previous
1917         * transaction and open a new one.  If this is a bucket, truncate
1918         * could leave only vb->vb_bh set up for journaling.  Meanwhile,
1919         * the caller is expecting to dirty the entire bucket.  So we must
1920         * reset the journal work.  We do this even if truncate has failed,
1921         * as it could have failed after committing the extend.
1922         */
1923        access_rc = ocfs2_xa_journal_access(ctxt->handle, loc,
1924                                            OCFS2_JOURNAL_ACCESS_WRITE);
1925
1926        /* Errors in truncate take precedence */
1927        return trunc_rc ? trunc_rc : access_rc;
1928}
1929
1930static void ocfs2_xa_remove_entry(struct ocfs2_xa_loc *loc)
1931{
1932        int index, count;
1933        struct ocfs2_xattr_header *xh = loc->xl_header;
1934        struct ocfs2_xattr_entry *entry = loc->xl_entry;
1935
1936        ocfs2_xa_wipe_namevalue(loc);
1937        loc->xl_entry = NULL;
1938
1939        le16_add_cpu(&xh->xh_count, -1);
1940        count = le16_to_cpu(xh->xh_count);
1941
1942        /*
1943         * Only zero out the entry if there are more remaining.  This is
1944         * important for an empty bucket, as it keeps track of the
1945         * bucket's hash value.  It doesn't hurt empty block storage.
1946         */
1947        if (count) {
1948                index = ((char *)entry - (char *)&xh->xh_entries) /
1949                        sizeof(struct ocfs2_xattr_entry);
1950                memmove(&xh->xh_entries[index], &xh->xh_entries[index + 1],
1951                        (count - index) * sizeof(struct ocfs2_xattr_entry));
1952                memset(&xh->xh_entries[count], 0,
1953                       sizeof(struct ocfs2_xattr_entry));
1954        }
1955}
1956
1957/*
1958 * If we have a problem adjusting the size of an external value during
1959 * ocfs2_xa_prepare_entry() or ocfs2_xa_remove(), we may have an xattr
1960 * in an intermediate state.  For example, the value may be partially
1961 * truncated.
1962 *
1963 * If the value tree hasn't changed, the extend/truncate went nowhere.
1964 * We have nothing to do.  The caller can treat it as a straight error.
1965 *
1966 * If the value tree got partially truncated, we now have a corrupted
1967 * extended attribute.  We're going to wipe its entry and leak the
1968 * clusters.  Better to leak some storage than leave a corrupt entry.
1969 *
1970 * If the value tree grew, it obviously didn't grow enough for the
1971 * new entry.  We're not going to try and reclaim those clusters either.
1972 * If there was already an external value there (orig_clusters != 0),
1973 * the new clusters are attached safely and we can just leave the old
1974 * value in place.  If there was no external value there, we remove
1975 * the entry.
1976 *
1977 * This way, the xattr block we store in the journal will be consistent.
1978 * If the size change broke because of the journal, no changes will hit
1979 * disk anyway.
1980 */
1981static void ocfs2_xa_cleanup_value_truncate(struct ocfs2_xa_loc *loc,
1982                                            const char *what,
1983                                            unsigned int orig_clusters)
1984{
1985        unsigned int new_clusters = ocfs2_xa_value_clusters(loc);
1986        char *nameval_buf = ocfs2_xa_offset_pointer(loc,
1987                                le16_to_cpu(loc->xl_entry->xe_name_offset));
1988
1989        if (new_clusters < orig_clusters) {
1990                mlog(ML_ERROR,
1991                     "Partial truncate while %s xattr %.*s.  Leaking "
1992                     "%u clusters and removing the entry\n",
1993                     what, loc->xl_entry->xe_name_len, nameval_buf,
1994                     orig_clusters - new_clusters);
1995                ocfs2_xa_remove_entry(loc);
1996        } else if (!orig_clusters) {
1997                mlog(ML_ERROR,
1998                     "Unable to allocate an external value for xattr "
1999                     "%.*s safely.  Leaking %u clusters and removing the "
2000                     "entry\n",
2001                     loc->xl_entry->xe_name_len, nameval_buf,
2002                     new_clusters - orig_clusters);
2003                ocfs2_xa_remove_entry(loc);
2004        } else if (new_clusters > orig_clusters)
2005                mlog(ML_ERROR,
2006                     "Unable to grow xattr %.*s safely.  %u new clusters "
2007                     "have been added, but the value will not be "
2008                     "modified\n",
2009                     loc->xl_entry->xe_name_len, nameval_buf,
2010                     new_clusters - orig_clusters);
2011}
2012
2013static int ocfs2_xa_remove(struct ocfs2_xa_loc *loc,
2014                           struct ocfs2_xattr_set_ctxt *ctxt)
2015{
2016        int rc = 0;
2017        unsigned int orig_clusters;
2018
2019        if (!ocfs2_xattr_is_local(loc->xl_entry)) {
2020                orig_clusters = ocfs2_xa_value_clusters(loc);
2021                rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
2022                if (rc) {
2023                        mlog_errno(rc);
2024                        /*
2025                         * Since this is remove, we can return 0 if
2026                         * ocfs2_xa_cleanup_value_truncate() is going to
2027                         * wipe the entry anyway.  So we check the
2028                         * cluster count as well.
2029                         */
2030                        if (orig_clusters != ocfs2_xa_value_clusters(loc))
2031                                rc = 0;
2032                        ocfs2_xa_cleanup_value_truncate(loc, "removing",
2033                                                        orig_clusters);
2034                        if (rc)
2035                                goto out;
2036                }
2037        }
2038
2039        ocfs2_xa_remove_entry(loc);
2040
2041out:
2042        return rc;
2043}
2044
2045static void ocfs2_xa_install_value_root(struct ocfs2_xa_loc *loc)
2046{
2047        int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len);
2048        char *nameval_buf;
2049
2050        nameval_buf = ocfs2_xa_offset_pointer(loc,
2051                                le16_to_cpu(loc->xl_entry->xe_name_offset));
2052        memcpy(nameval_buf + name_size, &def_xv, OCFS2_XATTR_ROOT_SIZE);
2053}
2054
2055/*
2056 * Take an existing entry and make it ready for the new value.  This
2057 * won't allocate space, but it may free space.  It should be ready for
2058 * ocfs2_xa_prepare_entry() to finish the work.
2059 */
2060static int ocfs2_xa_reuse_entry(struct ocfs2_xa_loc *loc,
2061                                struct ocfs2_xattr_info *xi,
2062                                struct ocfs2_xattr_set_ctxt *ctxt)
2063{
2064        int rc = 0;
2065        int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
2066        unsigned int orig_clusters;
2067        char *nameval_buf;
2068        int xe_local = ocfs2_xattr_is_local(loc->xl_entry);
2069        int xi_local = xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE;
2070
2071        BUG_ON(OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len) !=
2072               name_size);
2073
2074        nameval_buf = ocfs2_xa_offset_pointer(loc,
2075                                le16_to_cpu(loc->xl_entry->xe_name_offset));
2076        if (xe_local) {
2077                memset(nameval_buf + name_size, 0,
2078                       namevalue_size_xe(loc->xl_entry) - name_size);
2079                if (!xi_local)
2080                        ocfs2_xa_install_value_root(loc);
2081        } else {
2082                orig_clusters = ocfs2_xa_value_clusters(loc);
2083                if (xi_local) {
2084                        rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
2085                        if (rc < 0)
2086                                mlog_errno(rc);
2087                        else
2088                                memset(nameval_buf + name_size, 0,
2089                                       namevalue_size_xe(loc->xl_entry) -
2090                                       name_size);
2091                } else if (le64_to_cpu(loc->xl_entry->xe_value_size) >
2092                           xi->xi_value_len) {
2093                        rc = ocfs2_xa_value_truncate(loc, xi->xi_value_len,
2094                                                     ctxt);
2095                        if (rc < 0)
2096                                mlog_errno(rc);
2097                }
2098
2099                if (rc) {
2100                        ocfs2_xa_cleanup_value_truncate(loc, "reusing",
2101                                                        orig_clusters);
2102                        goto out;
2103                }
2104        }
2105
2106        loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
2107        ocfs2_xattr_set_local(loc->xl_entry, xi_local);
2108
2109out:
2110        return rc;
2111}
2112
2113/*
2114 * Prepares loc->xl_entry to receive the new xattr.  This includes
2115 * properly setting up the name+value pair region.  If loc->xl_entry
2116 * already exists, it will take care of modifying it appropriately.
2117 *
2118 * Note that this modifies the data.  You did journal_access already,
2119 * right?
2120 */
2121static int ocfs2_xa_prepare_entry(struct ocfs2_xa_loc *loc,
2122                                  struct ocfs2_xattr_info *xi,
2123                                  u32 name_hash,
2124                                  struct ocfs2_xattr_set_ctxt *ctxt)
2125{
2126        int rc = 0;
2127        unsigned int orig_clusters;
2128        __le64 orig_value_size = 0;
2129
2130        rc = ocfs2_xa_check_space(loc, xi);
2131        if (rc)
2132                goto out;
2133
2134        if (loc->xl_entry) {
2135                if (ocfs2_xa_can_reuse_entry(loc, xi)) {
2136                        orig_value_size = loc->xl_entry->xe_value_size;
2137                        rc = ocfs2_xa_reuse_entry(loc, xi, ctxt);
2138                        if (rc)
2139                                goto out;
2140                        goto alloc_value;
2141                }
2142
2143                if (!ocfs2_xattr_is_local(loc->xl_entry)) {
2144                        orig_clusters = ocfs2_xa_value_clusters(loc);
2145                        rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
2146                        if (rc) {
2147                                mlog_errno(rc);
2148                                ocfs2_xa_cleanup_value_truncate(loc,
2149                                                                "overwriting",
2150                                                                orig_clusters);
2151                                goto out;
2152                        }
2153                }
2154                ocfs2_xa_wipe_namevalue(loc);
2155        } else
2156                ocfs2_xa_add_entry(loc, name_hash);
2157
2158        /*
2159         * If we get here, we have a blank entry.  Fill it.  We grow our
2160         * name+value pair back from the end.
2161         */
2162        ocfs2_xa_add_namevalue(loc, xi);
2163        if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
2164                ocfs2_xa_install_value_root(loc);
2165
2166alloc_value:
2167        if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2168                orig_clusters = ocfs2_xa_value_clusters(loc);
2169                rc = ocfs2_xa_value_truncate(loc, xi->xi_value_len, ctxt);
2170                if (rc < 0) {
2171                        ctxt->set_abort = 1;
2172                        ocfs2_xa_cleanup_value_truncate(loc, "growing",
2173                                                        orig_clusters);
2174                        /*
2175                         * If we were growing an existing value,
2176                         * ocfs2_xa_cleanup_value_truncate() won't remove
2177                         * the entry. We need to restore the original value
2178                         * size.
2179                         */
2180                        if (loc->xl_entry) {
2181                                BUG_ON(!orig_value_size);
2182                                loc->xl_entry->xe_value_size = orig_value_size;
2183                        }
2184                        mlog_errno(rc);
2185                }
2186        }
2187
2188out:
2189        return rc;
2190}
2191
2192/*
2193 * Store the value portion of the name+value pair.  This will skip
2194 * values that are stored externally.  Their tree roots were set up
2195 * by ocfs2_xa_prepare_entry().
2196 */
2197static int ocfs2_xa_store_value(struct ocfs2_xa_loc *loc,
2198                                struct ocfs2_xattr_info *xi,
2199                                struct ocfs2_xattr_set_ctxt *ctxt)
2200{
2201        int rc = 0;
2202        int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
2203        int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
2204        char *nameval_buf;
2205        struct ocfs2_xattr_value_buf vb;
2206
2207        nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
2208        if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2209                ocfs2_xa_fill_value_buf(loc, &vb);
2210                rc = __ocfs2_xattr_set_value_outside(loc->xl_inode,
2211                                                     ctxt->handle, &vb,
2212                                                     xi->xi_value,
2213                                                     xi->xi_value_len);
2214        } else
2215                memcpy(nameval_buf + name_size, xi->xi_value, xi->xi_value_len);
2216
2217        return rc;
2218}
2219
2220static int ocfs2_xa_set(struct ocfs2_xa_loc *loc,
2221                        struct ocfs2_xattr_info *xi,
2222                        struct ocfs2_xattr_set_ctxt *ctxt)
2223{
2224        int ret;
2225        u32 name_hash = ocfs2_xattr_name_hash(loc->xl_inode, xi->xi_name,
2226                                              xi->xi_name_len);
2227
2228        ret = ocfs2_xa_journal_access(ctxt->handle, loc,
2229                                      OCFS2_JOURNAL_ACCESS_WRITE);
2230        if (ret) {
2231                mlog_errno(ret);
2232                goto out;
2233        }
2234
2235        /*
2236         * From here on out, everything is going to modify the buffer a
2237         * little.  Errors are going to leave the xattr header in a
2238         * sane state.  Thus, even with errors we dirty the sucker.
2239         */
2240
2241        /* Don't worry, we are never called with !xi_value and !xl_entry */
2242        if (!xi->xi_value) {
2243                ret = ocfs2_xa_remove(loc, ctxt);
2244                goto out_dirty;
2245        }
2246
2247        ret = ocfs2_xa_prepare_entry(loc, xi, name_hash, ctxt);
2248        if (ret) {
2249                if (ret != -ENOSPC)
2250                        mlog_errno(ret);
2251                goto out_dirty;
2252        }
2253
2254        ret = ocfs2_xa_store_value(loc, xi, ctxt);
2255        if (ret)
2256                mlog_errno(ret);
2257
2258out_dirty:
2259        ocfs2_xa_journal_dirty(ctxt->handle, loc);
2260
2261out:
2262        return ret;
2263}
2264
2265static void ocfs2_init_dinode_xa_loc(struct ocfs2_xa_loc *loc,
2266                                     struct inode *inode,
2267                                     struct buffer_head *bh,
2268                                     struct ocfs2_xattr_entry *entry)
2269{
2270        struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
2271
2272        BUG_ON(!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_XATTR_FL));
2273
2274        loc->xl_inode = inode;
2275        loc->xl_ops = &ocfs2_xa_block_loc_ops;
2276        loc->xl_storage = bh;
2277        loc->xl_entry = entry;
2278        loc->xl_size = le16_to_cpu(di->i_xattr_inline_size);
2279        loc->xl_header =
2280                (struct ocfs2_xattr_header *)(bh->b_data + bh->b_size -
2281                                              loc->xl_size);
2282}
2283
2284static void ocfs2_init_xattr_block_xa_loc(struct ocfs2_xa_loc *loc,
2285                                          struct inode *inode,
2286                                          struct buffer_head *bh,
2287                                          struct ocfs2_xattr_entry *entry)
2288{
2289        struct ocfs2_xattr_block *xb =
2290                (struct ocfs2_xattr_block *)bh->b_data;
2291
2292        BUG_ON(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED);
2293
2294        loc->xl_inode = inode;
2295        loc->xl_ops = &ocfs2_xa_block_loc_ops;
2296        loc->xl_storage = bh;
2297        loc->xl_header = &(xb->xb_attrs.xb_header);
2298        loc->xl_entry = entry;
2299        loc->xl_size = bh->b_size - offsetof(struct ocfs2_xattr_block,
2300                                             xb_attrs.xb_header);
2301}
2302
2303static void ocfs2_init_xattr_bucket_xa_loc(struct ocfs2_xa_loc *loc,
2304                                           struct ocfs2_xattr_bucket *bucket,
2305                                           struct ocfs2_xattr_entry *entry)
2306{
2307        loc->xl_inode = bucket->bu_inode;
2308        loc->xl_ops = &ocfs2_xa_bucket_loc_ops;
2309        loc->xl_storage = bucket;
2310        loc->xl_header = bucket_xh(bucket);
2311        loc->xl_entry = entry;
2312        loc->xl_size = OCFS2_XATTR_BUCKET_SIZE;
2313}
2314
2315/*
2316 * In xattr remove, if it is stored outside and refcounted, we may have
2317 * the chance to split the refcount tree. So need the allocators.
2318 */
2319static int ocfs2_lock_xattr_remove_allocators(struct inode *inode,
2320                                        struct ocfs2_xattr_value_root *xv,
2321                                        struct ocfs2_caching_info *ref_ci,
2322                                        struct buffer_head *ref_root_bh,
2323                                        struct ocfs2_alloc_context **meta_ac,
2324                                        int *ref_credits)
2325{
2326        int ret, meta_add = 0;
2327        u32 p_cluster, num_clusters;
2328        unsigned int ext_flags;
2329
2330        *ref_credits = 0;
2331        ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
2332                                       &num_clusters,
2333                                       &xv->xr_list,
2334                                       &ext_flags);
2335        if (ret) {
2336                mlog_errno(ret);
2337                goto out;
2338        }
2339
2340        if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
2341                goto out;
2342
2343        ret = ocfs2_refcounted_xattr_delete_need(inode, ref_ci,
2344                                                 ref_root_bh, xv,
2345                                                 &meta_add, ref_credits);
2346        if (ret) {
2347                mlog_errno(ret);
2348                goto out;
2349        }
2350
2351        ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
2352                                                meta_add, meta_ac);
2353        if (ret)
2354                mlog_errno(ret);
2355
2356out:
2357        return ret;
2358}
2359
2360static int ocfs2_remove_value_outside(struct inode*inode,
2361                                      struct ocfs2_xattr_value_buf *vb,
2362                                      struct ocfs2_xattr_header *header,
2363                                      struct ocfs2_caching_info *ref_ci,
2364                                      struct buffer_head *ref_root_bh)
2365{
2366        int ret = 0, i, ref_credits;
2367        struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2368        struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
2369        void *val;
2370
2371        ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
2372
2373        for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
2374                struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
2375
2376                if (ocfs2_xattr_is_local(entry))
2377                        continue;
2378
2379                val = (void *)header +
2380                        le16_to_cpu(entry->xe_name_offset);
2381                vb->vb_xv = (struct ocfs2_xattr_value_root *)
2382                        (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
2383
2384                ret = ocfs2_lock_xattr_remove_allocators(inode, vb->vb_xv,
2385                                                         ref_ci, ref_root_bh,
2386                                                         &ctxt.meta_ac,
2387                                                         &ref_credits);
2388
2389                ctxt.handle = ocfs2_start_trans(osb, ref_credits +
2390                                        ocfs2_remove_extent_credits(osb->sb));
2391                if (IS_ERR(ctxt.handle)) {
2392                        ret = PTR_ERR(ctxt.handle);
2393                        mlog_errno(ret);
2394                        break;
2395                }
2396
2397                ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt);
2398
2399                ocfs2_commit_trans(osb, ctxt.handle);
2400                if (ctxt.meta_ac) {
2401                        ocfs2_free_alloc_context(ctxt.meta_ac);
2402                        ctxt.meta_ac = NULL;
2403                }
2404
2405                if (ret < 0) {
2406                        mlog_errno(ret);
2407                        break;
2408                }
2409
2410        }
2411
2412        if (ctxt.meta_ac)
2413                ocfs2_free_alloc_context(ctxt.meta_ac);
2414        ocfs2_schedule_truncate_log_flush(osb, 1);
2415        ocfs2_run_deallocs(osb, &ctxt.dealloc);
2416        return ret;
2417}
2418
2419static int ocfs2_xattr_ibody_remove(struct inode *inode,
2420                                    struct buffer_head *di_bh,
2421                                    struct ocfs2_caching_info *ref_ci,
2422                                    struct buffer_head *ref_root_bh)
2423{
2424
2425        struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2426        struct ocfs2_xattr_header *header;
2427        int ret;
2428        struct ocfs2_xattr_value_buf vb = {
2429                .vb_bh = di_bh,
2430                .vb_access = ocfs2_journal_access_di,
2431        };
2432
2433        header = (struct ocfs2_xattr_header *)
2434                 ((void *)di + inode->i_sb->s_blocksize -
2435                 le16_to_cpu(di->i_xattr_inline_size));
2436
2437        ret = ocfs2_remove_value_outside(inode, &vb, header,
2438                                         ref_ci, ref_root_bh);
2439
2440        return ret;
2441}
2442
2443struct ocfs2_rm_xattr_bucket_para {
2444        struct ocfs2_caching_info *ref_ci;
2445        struct buffer_head *ref_root_bh;
2446};
2447
2448static int ocfs2_xattr_block_remove(struct inode *inode,
2449                                    struct buffer_head *blk_bh,
2450                                    struct ocfs2_caching_info *ref_ci,
2451                                    struct buffer_head *ref_root_bh)
2452{
2453        struct ocfs2_xattr_block *xb;
2454        int ret = 0;
2455        struct ocfs2_xattr_value_buf vb = {
2456                .vb_bh = blk_bh,
2457                .vb_access = ocfs2_journal_access_xb,
2458        };
2459        struct ocfs2_rm_xattr_bucket_para args = {
2460                .ref_ci = ref_ci,
2461                .ref_root_bh = ref_root_bh,
2462        };
2463
2464        xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2465        if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2466                struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
2467                ret = ocfs2_remove_value_outside(inode, &vb, header,
2468                                                 ref_ci, ref_root_bh);
2469        } else
2470                ret = ocfs2_iterate_xattr_index_block(inode,
2471                                                blk_bh,
2472                                                ocfs2_rm_xattr_cluster,
2473                                                &args);
2474
2475        return ret;
2476}
2477
2478static int ocfs2_xattr_free_block(struct inode *inode,
2479                                  u64 block,
2480                                  struct ocfs2_caching_info *ref_ci,
2481                                  struct buffer_head *ref_root_bh)
2482{
2483        struct inode *xb_alloc_inode;
2484        struct buffer_head *xb_alloc_bh = NULL;
2485        struct buffer_head *blk_bh = NULL;
2486        struct ocfs2_xattr_block *xb;
2487        struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2488        handle_t *handle;
2489        int ret = 0;
2490        u64 blk, bg_blkno;
2491        u16 bit;
2492
2493        ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
2494        if (ret < 0) {
2495                mlog_errno(ret);
2496                goto out;
2497        }
2498
2499        ret = ocfs2_xattr_block_remove(inode, blk_bh, ref_ci, ref_root_bh);
2500        if (ret < 0) {
2501                mlog_errno(ret);
2502                goto out;
2503        }
2504
2505        xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2506        blk = le64_to_cpu(xb->xb_blkno);
2507        bit = le16_to_cpu(xb->xb_suballoc_bit);
2508        if (xb->xb_suballoc_loc)
2509                bg_blkno = le64_to_cpu(xb->xb_suballoc_loc);
2510        else
2511                bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2512
2513        xb_alloc_inode = ocfs2_get_system_file_inode(osb,
2514                                EXTENT_ALLOC_SYSTEM_INODE,
2515                                le16_to_cpu(xb->xb_suballoc_slot));
2516        if (!xb_alloc_inode) {
2517                ret = -ENOMEM;
2518                mlog_errno(ret);
2519                goto out;
2520        }
2521        inode_lock(xb_alloc_inode);
2522
2523        ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
2524        if (ret < 0) {
2525                mlog_errno(ret);
2526                goto out_mutex;
2527        }
2528
2529        handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
2530        if (IS_ERR(handle)) {
2531                ret = PTR_ERR(handle);
2532                mlog_errno(ret);
2533                goto out_unlock;
2534        }
2535
2536        ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
2537                                       bit, bg_blkno, 1);
2538        if (ret < 0)
2539                mlog_errno(ret);
2540
2541        ocfs2_commit_trans(osb, handle);
2542out_unlock:
2543        ocfs2_inode_unlock(xb_alloc_inode, 1);
2544        brelse(xb_alloc_bh);
2545out_mutex:
2546        inode_unlock(xb_alloc_inode);
2547        iput(xb_alloc_inode);
2548out:
2549        brelse(blk_bh);
2550        return ret;
2551}
2552
2553/*
2554 * ocfs2_xattr_remove()
2555 *
2556 * Free extended attribute resources associated with this inode.
2557 */
2558int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
2559{
2560        struct ocfs2_inode_info *oi = OCFS2_I(inode);
2561        struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2562        struct ocfs2_refcount_tree *ref_tree = NULL;
2563        struct buffer_head *ref_root_bh = NULL;
2564        struct ocfs2_caching_info *ref_ci = NULL;
2565        handle_t *handle;
2566        int ret;
2567
2568        if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2569                return 0;
2570
2571        if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
2572                return 0;
2573
2574        if (ocfs2_is_refcount_inode(inode)) {
2575                ret = ocfs2_lock_refcount_tree(OCFS2_SB(inode->i_sb),
2576                                               le64_to_cpu(di->i_refcount_loc),
2577                                               1, &ref_tree, &ref_root_bh);
2578                if (ret) {
2579                        mlog_errno(ret);
2580                        goto out;
2581                }
2582                ref_ci = &ref_tree->rf_ci;
2583
2584        }
2585
2586        if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2587                ret = ocfs2_xattr_ibody_remove(inode, di_bh,
2588                                               ref_ci, ref_root_bh);
2589                if (ret < 0) {
2590                        mlog_errno(ret);
2591                        goto out;
2592                }
2593        }
2594
2595        if (di->i_xattr_loc) {
2596                ret = ocfs2_xattr_free_block(inode,
2597                                             le64_to_cpu(di->i_xattr_loc),
2598                                             ref_ci, ref_root_bh);
2599                if (ret < 0) {
2600                        mlog_errno(ret);
2601                        goto out;
2602                }
2603        }
2604
2605        handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
2606                                   OCFS2_INODE_UPDATE_CREDITS);
2607        if (IS_ERR(handle)) {
2608                ret = PTR_ERR(handle);
2609                mlog_errno(ret);
2610                goto out;
2611        }
2612        ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
2613                                      OCFS2_JOURNAL_ACCESS_WRITE);
2614        if (ret) {
2615                mlog_errno(ret);
2616                goto out_commit;
2617        }
2618
2619        di->i_xattr_loc = 0;
2620
2621        spin_lock(&oi->ip_lock);
2622        oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
2623        di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2624        spin_unlock(&oi->ip_lock);
2625        ocfs2_update_inode_fsync_trans(handle, inode, 0);
2626
2627        ocfs2_journal_dirty(handle, di_bh);
2628out_commit:
2629        ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
2630out:
2631        if (ref_tree)
2632                ocfs2_unlock_refcount_tree(OCFS2_SB(inode->i_sb), ref_tree, 1);
2633        brelse(ref_root_bh);
2634        return ret;
2635}
2636
2637static int ocfs2_xattr_has_space_inline(struct inode *inode,
2638                                        struct ocfs2_dinode *di)
2639{
2640        struct ocfs2_inode_info *oi = OCFS2_I(inode);
2641        unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
2642        int free;
2643
2644        if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
2645                return 0;
2646
2647        if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2648                struct ocfs2_inline_data *idata = &di->id2.i_data;
2649                free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
2650        } else if (ocfs2_inode_is_fast_symlink(inode)) {
2651                free = ocfs2_fast_symlink_chars(inode->i_sb) -
2652                        le64_to_cpu(di->i_size);
2653        } else {
2654                struct ocfs2_extent_list *el = &di->id2.i_list;
2655                free = (le16_to_cpu(el->l_count) -
2656                        le16_to_cpu(el->l_next_free_rec)) *
2657                        sizeof(struct ocfs2_extent_rec);
2658        }
2659        if (free >= xattrsize)
2660                return 1;
2661
2662        return 0;
2663}
2664
2665/*
2666 * ocfs2_xattr_ibody_find()
2667 *
2668 * Find extended attribute in inode block and
2669 * fill search info into struct ocfs2_xattr_search.
2670 */
2671static int ocfs2_xattr_ibody_find(struct inode *inode,
2672                                  int name_index,
2673                                  const char *name,
2674                                  struct ocfs2_xattr_search *xs)
2675{
2676        struct ocfs2_inode_info *oi = OCFS2_I(inode);
2677        struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2678        int ret;
2679        int has_space = 0;
2680
2681        if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2682                return 0;
2683
2684        if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2685                down_read(&oi->ip_alloc_sem);
2686                has_space = ocfs2_xattr_has_space_inline(inode, di);
2687                up_read(&oi->ip_alloc_sem);
2688                if (!has_space)
2689                        return 0;
2690        }
2691
2692        xs->xattr_bh = xs->inode_bh;
2693        xs->end = (void *)di + inode->i_sb->s_blocksize;
2694        if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
2695                xs->header = (struct ocfs2_xattr_header *)
2696                        (xs->end - le16_to_cpu(di->i_xattr_inline_size));
2697        else
2698                xs->header = (struct ocfs2_xattr_header *)
2699                        (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
2700        xs->base = (void *)xs->header;
2701        xs->here = xs->header->xh_entries;
2702
2703        /* Find the named attribute. */
2704        if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2705                ret = ocfs2_xattr_find_entry(name_index, name, xs);
2706                if (ret && ret != -ENODATA)
2707                        return ret;
2708                xs->not_found = ret;
2709        }
2710
2711        return 0;
2712}
2713
2714static int ocfs2_xattr_ibody_init(struct inode *inode,
2715                                  struct buffer_head *di_bh,
2716                                  struct ocfs2_xattr_set_ctxt *ctxt)
2717{
2718        int ret;
2719        struct ocfs2_inode_info *oi = OCFS2_I(inode);
2720        struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2721        struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2722        unsigned int xattrsize = osb->s_xattr_inline_size;
2723
2724        if (!ocfs2_xattr_has_space_inline(inode, di)) {
2725                ret = -ENOSPC;
2726                goto out;
2727        }
2728
2729        ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode), di_bh,
2730                                      OCFS2_JOURNAL_ACCESS_WRITE);
2731        if (ret) {
2732                mlog_errno(ret);
2733                goto out;
2734        }
2735
2736        /*
2737         * Adjust extent record count or inline data size
2738         * to reserve space for extended attribute.
2739         */
2740        if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2741                struct ocfs2_inline_data *idata = &di->id2.i_data;
2742                le16_add_cpu(&idata->id_count, -xattrsize);
2743        } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
2744                struct ocfs2_extent_list *el = &di->id2.i_list;
2745                le16_add_cpu(&el->l_count, -(xattrsize /
2746                                             sizeof(struct ocfs2_extent_rec)));
2747        }
2748        di->i_xattr_inline_size = cpu_to_le16(xattrsize);
2749
2750        spin_lock(&oi->ip_lock);
2751        oi->ip_dyn_features |= OCFS2_INLINE_XATTR_FL|OCFS2_HAS_XATTR_FL;
2752        di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2753        spin_unlock(&oi->ip_lock);
2754
2755        ocfs2_journal_dirty(ctxt->handle, di_bh);
2756
2757out:
2758        return ret;
2759}
2760
2761/*
2762 * ocfs2_xattr_ibody_set()
2763 *
2764 * Set, replace or remove an extended attribute into inode block.
2765 *
2766 */
2767static int ocfs2_xattr_ibody_set(struct inode *inode,
2768                                 struct ocfs2_xattr_info *xi,
2769                                 struct ocfs2_xattr_search *xs,
2770                                 struct ocfs2_xattr_set_ctxt *ctxt)
2771{
2772        int ret;
2773        struct ocfs2_inode_info *oi = OCFS2_I(inode);
2774        struct ocfs2_xa_loc loc;
2775
2776        if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2777                return -ENOSPC;
2778
2779        down_write(&oi->ip_alloc_sem);
2780        if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2781                ret = ocfs2_xattr_ibody_init(inode, xs->inode_bh, ctxt);
2782                if (ret) {
2783                        if (ret != -ENOSPC)
2784                                mlog_errno(ret);
2785                        goto out;
2786                }
2787        }
2788
2789        ocfs2_init_dinode_xa_loc(&loc, inode, xs->inode_bh,
2790                                 xs->not_found ? NULL : xs->here);
2791        ret = ocfs2_xa_set(&loc, xi, ctxt);
2792        if (ret) {
2793                if (ret != -ENOSPC)
2794                        mlog_errno(ret);
2795                goto out;
2796        }
2797        xs->here = loc.xl_entry;
2798
2799out:
2800        up_write(&oi->ip_alloc_sem);
2801
2802        return ret;
2803}
2804
2805/*
2806 * ocfs2_xattr_block_find()
2807 *
2808 * Find extended attribute in external block and
2809 * fill search info into struct ocfs2_xattr_search.
2810 */
2811static int ocfs2_xattr_block_find(struct inode *inode,
2812                                  int name_index,
2813                                  const char *name,
2814                                  struct ocfs2_xattr_search *xs)
2815{
2816        struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2817        struct buffer_head *blk_bh = NULL;
2818        struct ocfs2_xattr_block *xb;
2819        int ret = 0;
2820
2821        if (!di->i_xattr_loc)
2822                return ret;
2823
2824        ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
2825                                     &blk_bh);
2826        if (ret < 0) {
2827                mlog_errno(ret);
2828                return ret;
2829        }
2830
2831        xs->xattr_bh = blk_bh;
2832        xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2833
2834        if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2835                xs->header = &xb->xb_attrs.xb_header;
2836                xs->base = (void *)xs->header;
2837                xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
2838                xs->here = xs->header->xh_entries;
2839
2840                ret = ocfs2_xattr_find_entry(name_index, name, xs);
2841        } else
2842                ret = ocfs2_xattr_index_block_find(inode, blk_bh,
2843                                                   name_index,
2844                                                   name, xs);
2845
2846        if (ret && ret != -ENODATA) {
2847                xs->xattr_bh = NULL;
2848                goto cleanup;
2849        }
2850        xs->not_found = ret;
2851        return 0;
2852cleanup:
2853        brelse(blk_bh);
2854
2855        return ret;
2856}
2857
2858static int ocfs2_create_xattr_block(struct inode *inode,
2859                                    struct buffer_head *inode_bh,
2860                                    struct ocfs2_xattr_set_ctxt *ctxt,
2861                                    int indexed,
2862                                    struct buffer_head **ret_bh)
2863{
2864        int ret;
2865        u16 suballoc_bit_start;
2866        u32 num_got;
2867        u64 suballoc_loc, first_blkno;
2868        struct ocfs2_dinode *di =  (struct ocfs2_dinode *)inode_bh->b_data;
2869        struct buffer_head *new_bh = NULL;
2870        struct ocfs2_xattr_block *xblk;
2871
2872        ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
2873                                      inode_bh, OCFS2_JOURNAL_ACCESS_CREATE);
2874        if (ret < 0) {
2875                mlog_errno(ret);
2876                goto end;
2877        }
2878
2879        ret = ocfs2_claim_metadata(ctxt->handle, ctxt->meta_ac, 1,
2880                                   &suballoc_loc, &suballoc_bit_start,
2881                                   &num_got, &first_blkno);
2882        if (ret < 0) {
2883                mlog_errno(ret);
2884                goto end;
2885        }
2886
2887        new_bh = sb_getblk(inode->i_sb, first_blkno);
2888        if (!new_bh) {
2889                ret = -ENOMEM;
2890                mlog_errno(ret);
2891                goto end;
2892        }
2893
2894        ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
2895
2896        ret = ocfs2_journal_access_xb(ctxt->handle, INODE_CACHE(inode),
2897                                      new_bh,
2898                                      OCFS2_JOURNAL_ACCESS_CREATE);
2899        if (ret < 0) {
2900                mlog_errno(ret);
2901                goto end;
2902        }
2903
2904        /* Initialize ocfs2_xattr_block */
2905        xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2906        memset(xblk, 0, inode->i_sb->s_blocksize);
2907        strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
2908        xblk->xb_suballoc_slot = cpu_to_le16(ctxt->meta_ac->ac_alloc_slot);
2909        xblk->xb_suballoc_loc = cpu_to_le64(suballoc_loc);
2910        xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
2911        xblk->xb_fs_generation =
2912                cpu_to_le32(OCFS2_SB(inode->i_sb)->fs_generation);
2913        xblk->xb_blkno = cpu_to_le64(first_blkno);
2914        if (indexed) {
2915                struct ocfs2_xattr_tree_root *xr = &xblk->xb_attrs.xb_root;
2916                xr->xt_clusters = cpu_to_le32(1);
2917                xr->xt_last_eb_blk = 0;
2918                xr->xt_list.l_tree_depth = 0;
2919                xr->xt_list.l_count = cpu_to_le16(
2920                                        ocfs2_xattr_recs_per_xb(inode->i_sb));
2921                xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2922                xblk->xb_flags = cpu_to_le16(OCFS2_XATTR_INDEXED);
2923        }
2924        ocfs2_journal_dirty(ctxt->handle, new_bh);
2925
2926        /* Add it to the inode */
2927        di->i_xattr_loc = cpu_to_le64(first_blkno);
2928
2929        spin_lock(&OCFS2_I(inode)->ip_lock);
2930        OCFS2_I(inode)->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
2931        di->i_dyn_features = cpu_to_le16(OCFS2_I(inode)->ip_dyn_features);
2932        spin_unlock(&OCFS2_I(inode)->ip_lock);
2933
2934        ocfs2_journal_dirty(ctxt->handle, inode_bh);
2935
2936        *ret_bh = new_bh;
2937        new_bh = NULL;
2938
2939end:
2940        brelse(new_bh);
2941        return ret;
2942}
2943
2944/*
2945 * ocfs2_xattr_block_set()
2946 *
2947 * Set, replace or remove an extended attribute into external block.
2948 *
2949 */
2950static int ocfs2_xattr_block_set(struct inode *inode,
2951                                 struct ocfs2_xattr_info *xi,
2952                                 struct ocfs2_xattr_search *xs,
2953                                 struct ocfs2_xattr_set_ctxt *ctxt)
2954{
2955        struct buffer_head *new_bh = NULL;
2956        struct ocfs2_xattr_block *xblk = NULL;
2957        int ret;
2958        struct ocfs2_xa_loc loc;
2959
2960        if (!xs->xattr_bh) {
2961                ret = ocfs2_create_xattr_block(inode, xs->inode_bh, ctxt,
2962                                               0, &new_bh);
2963                if (ret) {
2964                        mlog_errno(ret);
2965                        goto end;
2966                }
2967
2968                xs->xattr_bh = new_bh;
2969                xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2970                xs->header = &xblk->xb_attrs.xb_header;
2971                xs->base = (void *)xs->header;
2972                xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2973                xs->here = xs->header->xh_entries;
2974        } else
2975                xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2976
2977        if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
2978                ocfs2_init_xattr_block_xa_loc(&loc, inode, xs->xattr_bh,
2979                                              xs->not_found ? NULL : xs->here);
2980
2981                ret = ocfs2_xa_set(&loc, xi, ctxt);
2982                if (!ret)
2983                        xs->here = loc.xl_entry;
2984                else if ((ret != -ENOSPC) || ctxt->set_abort)
2985                        goto end;
2986                else {
2987                        ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
2988                        if (ret)
2989                                goto end;
2990                }
2991        }
2992
2993        if (le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)
2994                ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
2995
2996end:
2997        return ret;
2998}
2999
3000/* Check whether the new xattr can be inserted into the inode. */
3001static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
3002                                       struct ocfs2_xattr_info *xi,
3003                                       struct ocfs2_xattr_search *xs)
3004{
3005        struct ocfs2_xattr_entry *last;
3006        int free, i;
3007        size_t min_offs = xs->end - xs->base;
3008
3009        if (!xs->header)
3010                return 0;
3011
3012        last = xs->header->xh_entries;
3013
3014        for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
3015                size_t offs = le16_to_cpu(last->xe_name_offset);
3016                if (offs < min_offs)
3017                        min_offs = offs;
3018                last += 1;
3019        }
3020
3021        free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
3022        if (free < 0)
3023                return 0;
3024
3025        BUG_ON(!xs->not_found);
3026
3027        if (free >= (sizeof(struct ocfs2_xattr_entry) + namevalue_size_xi(xi)))
3028                return 1;
3029
3030        return 0;
3031}
3032
3033static int ocfs2_calc_xattr_set_need(struct inode *inode,
3034                                     struct ocfs2_dinode *di,
3035                                     struct ocfs2_xattr_info *xi,
3036                                     struct ocfs2_xattr_search *xis,
3037                                     struct ocfs2_xattr_search *xbs,
3038                                     int *clusters_need,
3039                                     int *meta_need,
3040                                     int *credits_need)
3041{
3042        int ret = 0, old_in_xb = 0;
3043        int clusters_add = 0, meta_add = 0, credits = 0;
3044        struct buffer_head *bh = NULL;
3045        struct ocfs2_xattr_block *xb = NULL;
3046        struct ocfs2_xattr_entry *xe = NULL;
3047        struct ocfs2_xattr_value_root *xv = NULL;
3048        char *base = NULL;
3049        int name_offset, name_len = 0;
3050        u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
3051                                                    xi->xi_value_len);
3052        u64 value_size;
3053
3054        /*
3055         * Calculate the clusters we need to write.
3056         * No matter whether we replace an old one or add a new one,
3057         * we need this for writing.
3058         */
3059        if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
3060                credits += new_clusters *
3061                           ocfs2_clusters_to_blocks(inode->i_sb, 1);
3062
3063        if (xis->not_found && xbs->not_found) {
3064                credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3065
3066                if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
3067                        clusters_add += new_clusters;
3068                        credits += ocfs2_calc_extend_credits(inode->i_sb,
3069                                                        &def_xv.xv.xr_list);
3070                }
3071
3072                goto meta_guess;
3073        }
3074
3075        if (!xis->not_found) {
3076                xe = xis->here;
3077                name_offset = le16_to_cpu(xe->xe_name_offset);
3078                name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3079                base = xis->base;
3080                credits += OCFS2_INODE_UPDATE_CREDITS;
3081        } else {
3082                int i, block_off = 0;
3083                xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
3084                xe = xbs->here;
3085                name_offset = le16_to_cpu(xe->xe_name_offset);
3086                name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3087                i = xbs->here - xbs->header->xh_entries;
3088                old_in_xb = 1;
3089
3090                if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
3091                        ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
3092                                                        bucket_xh(xbs->bucket),
3093                                                        i, &block_off,
3094                                                        &name_offset);
3095                        base = bucket_block(xbs->bucket, block_off);
3096                        credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3097                } else {
3098                        base = xbs->base;
3099                        credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
3100                }
3101        }
3102
3103        /*
3104         * delete a xattr doesn't need metadata and cluster allocation.
3105         * so just calculate the credits and return.
3106         *
3107         * The credits for removing the value tree will be extended
3108         * by ocfs2_remove_extent itself.
3109         */
3110        if (!xi->xi_value) {
3111                if (!ocfs2_xattr_is_local(xe))
3112                        credits += ocfs2_remove_extent_credits(inode->i_sb);
3113
3114                goto out;
3115        }
3116
3117        /* do cluster allocation guess first. */
3118        value_size = le64_to_cpu(xe->xe_value_size);
3119
3120        if (old_in_xb) {
3121                /*
3122                 * In xattr set, we always try to set the xe in inode first,
3123                 * so if it can be inserted into inode successfully, the old
3124                 * one will be removed from the xattr block, and this xattr
3125                 * will be inserted into inode as a new xattr in inode.
3126                 */
3127                if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
3128                        clusters_add += new_clusters;
3129                        credits += ocfs2_remove_extent_credits(inode->i_sb) +
3130                                    OCFS2_INODE_UPDATE_CREDITS;
3131                        if (!ocfs2_xattr_is_local(xe))
3132                                credits += ocfs2_calc_extend_credits(
3133                                                        inode->i_sb,
3134                                                        &def_xv.xv.xr_list);
3135                        goto out;
3136                }
3137        }
3138
3139        if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
3140                /* the new values will be stored outside. */
3141                u32 old_clusters = 0;
3142
3143                if (!ocfs2_xattr_is_local(xe)) {
3144                        old_clusters =  ocfs2_clusters_for_bytes(inode->i_sb,
3145                                                                 value_size);
3146                        xv = (struct ocfs2_xattr_value_root *)
3147                             (base + name_offset + name_len);
3148                        value_size = OCFS2_XATTR_ROOT_SIZE;
3149                } else
3150                        xv = &def_xv.xv;
3151
3152                if (old_clusters >= new_clusters) {
3153                        credits += ocfs2_remove_extent_credits(inode->i_sb);
3154                        goto out;
3155                } else {
3156                        meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
3157                        clusters_add += new_clusters - old_clusters;
3158                        credits += ocfs2_calc_extend_credits(inode->i_sb,
3159                                                             &xv->xr_list);
3160                        if (value_size >= OCFS2_XATTR_ROOT_SIZE)
3161                                goto out;
3162                }
3163        } else {
3164                /*
3165                 * Now the new value will be stored inside. So if the new
3166                 * value is smaller than the size of value root or the old
3167                 * value, we don't need any allocation, otherwise we have
3168                 * to guess metadata allocation.
3169                 */
3170                if ((ocfs2_xattr_is_local(xe) &&
3171                     (value_size >= xi->xi_value_len)) ||
3172                    (!ocfs2_xattr_is_local(xe) &&
3173                     OCFS2_XATTR_ROOT_SIZE >= xi->xi_value_len))
3174                        goto out;
3175        }
3176
3177meta_guess:
3178        /* calculate metadata allocation. */
3179        if (di->i_xattr_loc) {
3180                if (!xbs->xattr_bh) {
3181                        ret = ocfs2_read_xattr_block(inode,
3182                                                     le64_to_cpu(di->i_xattr_loc),
3183                                                     &bh);
3184                        if (ret) {
3185                                mlog_errno(ret);
3186                                goto out;
3187                        }
3188
3189                        xb = (struct ocfs2_xattr_block *)bh->b_data;
3190                } else
3191                        xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
3192
3193                /*
3194                 * If there is already an xattr tree, good, we can calculate
3195                 * like other b-trees. Otherwise we may have the chance of
3196                 * create a tree, the credit calculation is borrowed from
3197                 * ocfs2_calc_extend_credits with root_el = NULL. And the
3198                 * new tree will be cluster based, so no meta is needed.
3199                 */
3200                if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
3201                        struct ocfs2_extent_list *el =
3202                                 &xb->xb_attrs.xb_root.xt_list;
3203                        meta_add += ocfs2_extend_meta_needed(el);
3204                        credits += ocfs2_calc_extend_credits(inode->i_sb,
3205                                                             el);
3206                } else
3207                        credits += OCFS2_SUBALLOC_ALLOC + 1;
3208
3209                /*
3210                 * This cluster will be used either for new bucket or for
3211                 * new xattr block.
3212                 * If the cluster size is the same as the bucket size, one
3213                 * more is needed since we may need to extend the bucket
3214                 * also.
3215                 */
3216                clusters_add += 1;
3217                credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3218                if (OCFS2_XATTR_BUCKET_SIZE ==
3219                        OCFS2_SB(inode->i_sb)->s_clustersize) {
3220                        credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3221                        clusters_add += 1;
3222                }
3223        } else {
3224                credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
3225                if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
3226                        struct ocfs2_extent_list *el = &def_xv.xv.xr_list;
3227                        meta_add += ocfs2_extend_meta_needed(el);
3228                        credits += ocfs2_calc_extend_credits(inode->i_sb,
3229                                                             el);
3230                } else {
3231                        meta_add += 1;
3232                }
3233        }
3234out:
3235        if (clusters_need)
3236                *clusters_need = clusters_add;
3237        if (meta_need)
3238                *meta_need = meta_add;
3239        if (credits_need)
3240                *credits_need = credits;
3241        brelse(bh);
3242        return ret;
3243}
3244
3245static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
3246                                     struct ocfs2_dinode *di,
3247                                     struct ocfs2_xattr_info *xi,
3248                                     struct ocfs2_xattr_search *xis,
3249                                     struct ocfs2_xattr_search *xbs,
3250                                     struct ocfs2_xattr_set_ctxt *ctxt,
3251                                     int extra_meta,
3252                                     int *credits)
3253{
3254        int clusters_add, meta_add, ret;
3255        struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3256
3257        memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
3258
3259        ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
3260
3261        ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
3262                                        &clusters_add, &meta_add, credits);
3263        if (ret) {
3264                mlog_errno(ret);
3265                return ret;
3266        }
3267
3268        meta_add += extra_meta;
3269        trace_ocfs2_init_xattr_set_ctxt(xi->xi_name, meta_add,
3270                                        clusters_add, *credits);
3271
3272        if (meta_add) {
3273                ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
3274                                                        &ctxt->meta_ac);
3275                if (ret) {
3276                        mlog_errno(ret);
3277                        goto out;
3278                }
3279        }
3280
3281        if (clusters_add) {
3282                ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
3283                if (ret)
3284                        mlog_errno(ret);
3285        }
3286out:
3287        if (ret) {
3288                if (ctxt->meta_ac) {
3289                        ocfs2_free_alloc_context(ctxt->meta_ac);
3290                        ctxt->meta_ac = NULL;
3291                }
3292
3293                /*
3294                 * We cannot have an error and a non null ctxt->data_ac.
3295                 */
3296        }
3297
3298        return ret;
3299}
3300
3301static int __ocfs2_xattr_set_handle(struct inode *inode,
3302                                    struct ocfs2_dinode *di,
3303                                    struct ocfs2_xattr_info *xi,
3304                                    struct ocfs2_xattr_search *xis,
3305                                    struct ocfs2_xattr_search *xbs,
3306                                    struct ocfs2_xattr_set_ctxt *ctxt)
3307{
3308        int ret = 0, credits, old_found;
3309
3310        if (!xi->xi_value) {
3311                /* Remove existing extended attribute */
3312                if (!xis->not_found)
3313                        ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3314                else if (!xbs->not_found)
3315                        ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3316        } else {
3317                /* We always try to set extended attribute into inode first*/
3318                ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3319                if (!ret && !xbs->not_found) {
3320                        /*
3321                         * If succeed and that extended attribute existing in
3322                         * external block, then we will remove it.
3323                         */
3324                        xi->xi_value = NULL;
3325                        xi->xi_value_len = 0;
3326
3327                        old_found = xis->not_found;
3328                        xis->not_found = -ENODATA;
3329                        ret = ocfs2_calc_xattr_set_need(inode,
3330                                                        di,
3331                                                        xi,
3332                                                        xis,
3333                                                        xbs,
3334                                                        NULL,
3335                                                        NULL,
3336                                                        &credits);
3337                        xis->not_found = old_found;
3338                        if (ret) {
3339                                mlog_errno(ret);
3340                                goto out;
3341                        }
3342
3343                        ret = ocfs2_extend_trans(ctxt->handle, credits);
3344                        if (ret) {
3345                                mlog_errno(ret);
3346                                goto out;
3347                        }
3348                        ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3349                } else if ((ret == -ENOSPC) && !ctxt->set_abort) {
3350                        if (di->i_xattr_loc && !xbs->xattr_bh) {
3351                                ret = ocfs2_xattr_block_find(inode,
3352                                                             xi->xi_name_index,
3353                                                             xi->xi_name, xbs);
3354                                if (ret)
3355                                        goto out;
3356
3357                                old_found = xis->not_found;
3358                                xis->not_found = -ENODATA;
3359                                ret = ocfs2_calc_xattr_set_need(inode,
3360                                                                di,
3361                                                                xi,
3362                                                                xis,
3363                                                                xbs,
3364                                                                NULL,
3365                                                                NULL,
3366                                                                &credits);
3367                                xis->not_found = old_found;
3368                                if (ret) {
3369                                        mlog_errno(ret);
3370                                        goto out;
3371                                }
3372
3373                                ret = ocfs2_extend_trans(ctxt->handle, credits);
3374                                if (ret) {
3375                                        mlog_errno(ret);
3376                                        goto out;
3377                                }
3378                        }
3379                        /*
3380                         * If no space in inode, we will set extended attribute
3381                         * into external block.
3382                         */
3383                        ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3384                        if (ret)
3385                                goto out;
3386                        if (!xis->not_found) {
3387                                /*
3388                                 * If succeed and that extended attribute
3389                                 * existing in inode, we will remove it.
3390                                 */
3391                                xi->xi_value = NULL;
3392                                xi->xi_value_len = 0;
3393                                xbs->not_found = -ENODATA;
3394                                ret = ocfs2_calc_xattr_set_need(inode,
3395                                                                di,
3396                                                                xi,
3397                                                                xis,
3398                                                                xbs,
3399                                                                NULL,
3400                                                                NULL,
3401                                                                &credits);
3402                                if (ret) {
3403                                        mlog_errno(ret);
3404                                        goto out;
3405                                }
3406
3407                                ret = ocfs2_extend_trans(ctxt->handle, credits);
3408                                if (ret) {
3409                                        mlog_errno(ret);
3410                                        goto out;
3411                                }
3412                                ret = ocfs2_xattr_ibody_set(inode, xi,
3413                                                            xis, ctxt);
3414                        }
3415                }
3416        }
3417
3418        if (!ret) {
3419                /* Update inode ctime. */
3420                ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
3421                                              xis->inode_bh,
3422                                              OCFS2_JOURNAL_ACCESS_WRITE);
3423                if (ret) {
3424                        mlog_errno(ret);
3425                        goto out;
3426                }
3427
3428                inode->i_ctime = current_time(inode);
3429                di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
3430                di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
3431                ocfs2_journal_dirty(ctxt->handle, xis->inode_bh);
3432        }
3433out:
3434        return ret;
3435}
3436
3437/*
3438 * This function only called duing creating inode
3439 * for init security/acl xattrs of the new inode.
3440 * All transanction credits have been reserved in mknod.
3441 */
3442int ocfs2_xattr_set_handle(handle_t *handle,
3443                           struct inode *inode,
3444                           struct buffer_head *di_bh,
3445                           int name_index,
3446                           const char *name,
3447                           const void *value,
3448                           size_t value_len,
3449                           int flags,
3450                           struct ocfs2_alloc_context *meta_ac,
3451                           struct ocfs2_alloc_context *data_ac)
3452{
3453        struct ocfs2_dinode *di;
3454        int ret;
3455
3456        struct ocfs2_xattr_info xi = {
3457                .xi_name_index = name_index,
3458                .xi_name = name,
3459                .xi_name_len = strlen(name),
3460                .xi_value = value,
3461                .xi_value_len = value_len,
3462        };
3463
3464        struct ocfs2_xattr_search xis = {
3465                .not_found = -ENODATA,
3466        };
3467
3468        struct ocfs2_xattr_search xbs = {
3469                .not_found = -ENODATA,
3470        };
3471
3472        struct ocfs2_xattr_set_ctxt ctxt = {
3473                .handle = handle,
3474                .meta_ac = meta_ac,
3475                .data_ac = data_ac,
3476        };
3477
3478        if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3479                return -EOPNOTSUPP;
3480
3481        /*
3482         * In extreme situation, may need xattr bucket when
3483         * block size is too small. And we have already reserved
3484         * the credits for bucket in mknod.
3485         */
3486        if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) {
3487                xbs.bucket = ocfs2_xattr_bucket_new(inode);
3488                if (!xbs.bucket) {
3489                        mlog_errno(-ENOMEM);
3490                        return -ENOMEM;
3491                }
3492        }
3493
3494        xis.inode_bh = xbs.inode_bh = di_bh;
3495        di = (struct ocfs2_dinode *)di_bh->b_data;
3496
3497        down_write(&OCFS2_I(inode)->ip_xattr_sem);
3498
3499        ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3500        if (ret)
3501                goto cleanup;
3502        if (xis.not_found) {
3503                ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3504                if (ret)
3505                        goto cleanup;
3506        }
3507
3508        ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3509
3510cleanup:
3511        up_write(&OCFS2_I(inode)->ip_xattr_sem);
3512        brelse(xbs.xattr_bh);
3513        ocfs2_xattr_bucket_free(xbs.bucket);
3514
3515        return ret;
3516}
3517
3518/*
3519 * ocfs2_xattr_set()
3520 *
3521 * Set, replace or remove an extended attribute for this inode.
3522 * value is NULL to remove an existing extended attribute, else either
3523 * create or replace an extended attribute.
3524 */
3525int ocfs2_xattr_set(struct inode *inode,
3526                    int name_index,
3527                    const char *name,
3528                    const void *value,
3529                    size_t value_len,
3530                    int flags)
3531{
3532        struct buffer_head *di_bh = NULL;
3533        struct ocfs2_dinode *di;
3534        int ret, credits, had_lock, ref_meta = 0, ref_credits = 0;
3535        struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3536        struct inode *tl_inode = osb->osb_tl_inode;
3537        struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, NULL, };
3538        struct ocfs2_refcount_tree *ref_tree = NULL;
3539        struct ocfs2_lock_holder oh;
3540
3541        struct ocfs2_xattr_info xi = {
3542                .xi_name_index = name_index,
3543                .xi_name = name,
3544                .xi_name_len = strlen(name),
3545                .xi_value = value,
3546                .xi_value_len = value_len,
3547        };
3548
3549        struct ocfs2_xattr_search xis = {
3550                .not_found = -ENODATA,
3551        };
3552
3553        struct ocfs2_xattr_search xbs = {
3554                .not_found = -ENODATA,
3555        };
3556
3557        if (!ocfs2_supports_xattr(osb))
3558                return -EOPNOTSUPP;
3559
3560        /*
3561         * Only xbs will be used on indexed trees.  xis doesn't need a
3562         * bucket.
3563         */
3564        xbs.bucket = ocfs2_xattr_bucket_new(inode);
3565        if (!xbs.bucket) {
3566                mlog_errno(-ENOMEM);
3567                return -ENOMEM;
3568        }
3569
3570        had_lock = ocfs2_inode_lock_tracker(inode, &di_bh, 1, &oh);
3571        if (had_lock < 0) {
3572                ret = had_lock;
3573                mlog_errno(ret);
3574                goto cleanup_nolock;
3575        }
3576        xis.inode_bh = xbs.inode_bh = di_bh;
3577        di = (struct ocfs2_dinode *)di_bh->b_data;
3578
3579        down_write(&OCFS2_I(inode)->ip_xattr_sem);
3580        /*
3581         * Scan inode and external block to find the same name
3582         * extended attribute and collect search information.
3583         */
3584        ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3585        if (ret)
3586                goto cleanup;
3587        if (xis.not_found) {
3588                ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3589                if (ret)
3590                        goto cleanup;
3591        }
3592
3593        if (xis.not_found && xbs.not_found) {
3594                ret = -ENODATA;
3595                if (flags & XATTR_REPLACE)
3596                        goto cleanup;
3597                ret = 0;
3598                if (!value)
3599                        goto cleanup;
3600        } else {
3601                ret = -EEXIST;
3602                if (flags & XATTR_CREATE)
3603                        goto cleanup;
3604        }
3605
3606        /* Check whether the value is refcounted and do some preparation. */
3607        if (ocfs2_is_refcount_inode(inode) &&
3608            (!xis.not_found || !xbs.not_found)) {
3609                ret = ocfs2_prepare_refcount_xattr(inode, di, &xi,
3610                                                   &xis, &xbs, &ref_tree,
3611                                                   &ref_meta, &ref_credits);
3612                if (ret) {
3613                        mlog_errno(ret);
3614                        goto cleanup;
3615                }
3616        }
3617
3618        inode_lock(tl_inode);
3619
3620        if (ocfs2_truncate_log_needs_flush(osb)) {
3621                ret = __ocfs2_flush_truncate_log(osb);
3622                if (ret < 0) {
3623                        inode_unlock(tl_inode);
3624                        mlog_errno(ret);
3625                        goto cleanup;
3626                }
3627        }
3628        inode_unlock(tl_inode);
3629
3630        ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
3631                                        &xbs, &ctxt, ref_meta, &credits);
3632        if (ret) {
3633                mlog_errno(ret);
3634                goto cleanup;
3635        }
3636
3637        /* we need to update inode's ctime field, so add credit for it. */
3638        credits += OCFS2_INODE_UPDATE_CREDITS;
3639        ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
3640        if (IS_ERR(ctxt.handle)) {
3641                ret = PTR_ERR(ctxt.handle);
3642                mlog_errno(ret);
3643                goto out_free_ac;
3644        }
3645
3646        ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3647        ocfs2_update_inode_fsync_trans(ctxt.handle, inode, 0);
3648
3649        ocfs2_commit_trans(osb, ctxt.handle);
3650
3651out_free_ac:
3652        if (ctxt.data_ac)
3653                ocfs2_free_alloc_context(ctxt.data_ac);
3654        if (ctxt.meta_ac)
3655                ocfs2_free_alloc_context(ctxt.meta_ac);
3656        if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
3657                ocfs2_schedule_truncate_log_flush(osb, 1);
3658        ocfs2_run_deallocs(osb, &ctxt.dealloc);
3659
3660cleanup:
3661        if (ref_tree)
3662                ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
3663        up_write(&OCFS2_I(inode)->ip_xattr_sem);
3664        if (!value && !ret) {
3665                ret = ocfs2_try_remove_refcount_tree(inode, di_bh);
3666                if (ret)
3667                        mlog_errno(ret);
3668        }
3669        ocfs2_inode_unlock_tracker(inode, 1, &oh, had_lock);
3670cleanup_nolock:
3671        brelse(di_bh);
3672        brelse(xbs.xattr_bh);
3673        ocfs2_xattr_bucket_free(xbs.bucket);
3674
3675        return ret;
3676}
3677
3678/*
3679 * Find the xattr extent rec which may contains name_hash.
3680 * e_cpos will be the first name hash of the xattr rec.
3681 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
3682 */
3683static int ocfs2_xattr_get_rec(struct inode *inode,
3684                               u32 name_hash,
3685                               u64 *p_blkno,
3686                               u32 *e_cpos,
3687                               u32 *num_clusters,
3688                               struct ocfs2_extent_list *el)
3689{
3690        int ret = 0, i;
3691        struct buffer_head *eb_bh = NULL;
3692        struct ocfs2_extent_block *eb;
3693        struct ocfs2_extent_rec *rec = NULL;
3694        u64 e_blkno = 0;
3695
3696        if (el->l_tree_depth) {
3697                ret = ocfs2_find_leaf(INODE_CACHE(inode), el, name_hash,
3698                                      &eb_bh);
3699                if (ret) {
3700                        mlog_errno(ret);
3701                        goto out;
3702                }
3703
3704                eb = (struct ocfs2_extent_block *) eb_bh->b_data;
3705                el = &eb->h_list;
3706
3707                if (el->l_tree_depth) {
3708                        ret = ocfs2_error(inode->i_sb,
3709                                          "Inode %lu has non zero tree depth in xattr tree block %llu\n",
3710                                          inode->i_ino,
3711                                          (unsigned long long)eb_bh->b_blocknr);
3712                        goto out;
3713                }
3714        }
3715
3716        for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
3717                rec = &el->l_recs[i];
3718
3719                if (le32_to_cpu(rec->e_cpos) <= name_hash) {
3720                        e_blkno = le64_to_cpu(rec->e_blkno);
3721                        break;
3722                }
3723        }
3724
3725        if (!e_blkno) {
3726                ret = ocfs2_error(inode->i_sb, "Inode %lu has bad extent record (%u, %u, 0) in xattr\n",
3727                                  inode->i_ino,
3728                                  le32_to_cpu(rec->e_cpos),
3729                                  ocfs2_rec_clusters(el, rec));
3730                goto out;
3731        }
3732
3733        *p_blkno = le64_to_cpu(rec->e_blkno);
3734        *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
3735        if (e_cpos)
3736                *e_cpos = le32_to_cpu(rec->e_cpos);
3737out:
3738        brelse(eb_bh);
3739        return ret;
3740}
3741
3742typedef int (xattr_bucket_func)(struct inode *inode,
3743                                struct ocfs2_xattr_bucket *bucket,
3744                                void *para);
3745
3746static int ocfs2_find_xe_in_bucket(struct inode *inode,
3747                                   struct ocfs2_xattr_bucket *bucket,
3748                                   int name_index,
3749                                   const char *name,
3750                                   u32 name_hash,
3751                                   u16 *xe_index,
3752                                   int *found)
3753{
3754        int i, ret = 0, cmp = 1, block_off, new_offset;
3755        struct ocfs2_xattr_header *xh = bucket_xh(bucket);
3756        size_t name_len = strlen(name);
3757        struct ocfs2_xattr_entry *xe = NULL;
3758        char *xe_name;
3759
3760        /*
3761         * We don't use binary search in the bucket because there
3762         * may be multiple entries with the same name hash.
3763         */
3764        for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3765                xe = &xh->xh_entries[i];
3766
3767                if (name_hash > le32_to_cpu(xe->xe_name_hash))
3768                        continue;
3769                else if (name_hash < le32_to_cpu(xe->xe_name_hash))
3770                        break;
3771
3772                cmp = name_index - ocfs2_xattr_get_type(xe);
3773                if (!cmp)
3774                        cmp = name_len - xe->xe_name_len;
3775                if (cmp)
3776                        continue;
3777
3778                ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
3779                                                        xh,
3780                                                        i,
3781                                                        &block_off,
3782                                                        &new_offset);
3783                if (ret) {
3784                        mlog_errno(ret);
3785                        break;
3786                }
3787
3788
3789                xe_name = bucket_block(bucket, block_off) + new_offset;
3790                if (!memcmp(name, xe_name, name_len)) {
3791                        *xe_index = i;
3792                        *found = 1;
3793                        ret = 0;
3794                        break;
3795                }
3796        }
3797
3798        return ret;
3799}
3800
3801/*
3802 * Find the specified xattr entry in a series of buckets.
3803 * This series start from p_blkno and last for num_clusters.
3804 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
3805 * the num of the valid buckets.
3806 *
3807 * Return the buffer_head this xattr should reside in. And if the xattr's
3808 * hash is in the gap of 2 buckets, return the lower bucket.
3809 */
3810static int ocfs2_xattr_bucket_find(struct inode *inode,
3811                                   int name_index,
3812                                   const char *name,
3813                                   u32 name_hash,
3814                                   u64 p_blkno,
3815                                   u32 first_hash,
3816                                   u32 num_clusters,
3817                                   struct ocfs2_xattr_search *xs)
3818{
3819        int ret, found = 0;
3820        struct ocfs2_xattr_header *xh = NULL;
3821        struct ocfs2_xattr_entry *xe = NULL;
3822        u16 index = 0;
3823        u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3824        int low_bucket = 0, bucket, high_bucket;
3825        struct ocfs2_xattr_bucket *search;
3826        u64 blkno, lower_blkno = 0;
3827
3828        search = ocfs2_xattr_bucket_new(inode);
3829        if (!search) {
3830                ret = -ENOMEM;
3831                mlog_errno(ret);
3832                goto out;
3833        }
3834
3835        ret = ocfs2_read_xattr_bucket(search, p_blkno);
3836        if (ret) {
3837                mlog_errno(ret);
3838                goto out;
3839        }
3840
3841        xh = bucket_xh(search);
3842        high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
3843        while (low_bucket <= high_bucket) {
3844                ocfs2_xattr_bucket_relse(search);
3845
3846                bucket = (low_bucket + high_bucket) / 2;
3847                blkno = p_blkno + bucket * blk_per_bucket;
3848                ret = ocfs2_read_xattr_bucket(search, blkno);
3849                if (ret) {
3850                        mlog_errno(ret);
3851                        goto out;
3852                }
3853
3854                xh = bucket_xh(search);
3855                xe = &xh->xh_entries[0];
3856                if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
3857                        high_bucket = bucket - 1;
3858                        continue;
3859                }
3860
3861                /*
3862                 * Check whether the hash of the last entry in our
3863                 * bucket is larger than the search one. for an empty
3864                 * bucket, the last one is also the first one.
3865                 */
3866                if (xh->xh_count)
3867                        xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
3868
3869                /* record lower_blkno which may be the insert place. */
3870                lower_blkno = blkno;
3871
3872                if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
3873                        low_bucket = bucket + 1;
3874                        continue;
3875                }
3876
3877                /* the searched xattr should reside in this bucket if exists. */
3878                ret = ocfs2_find_xe_in_bucket(inode, search,
3879                                              name_index, name, name_hash,
3880                                              &index, &found);
3881                if (ret) {
3882                        mlog_errno(ret);
3883                        goto out;
3884                }
3885                break;
3886        }
3887
3888        /*
3889         * Record the bucket we have found.
3890         * When the xattr's hash value is in the gap of 2 buckets, we will
3891         * always set it to the previous bucket.
3892         */
3893        if (!lower_blkno)
3894                lower_blkno = p_blkno;
3895
3896        /* This should be in cache - we just read it during the search */
3897        ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
3898        if (ret) {
3899                mlog_errno(ret);
3900                goto out;
3901        }
3902
3903        xs->header = bucket_xh(xs->bucket);
3904        xs->base = bucket_block(xs->bucket, 0);
3905        xs->end = xs->base + inode->i_sb->s_blocksize;
3906
3907        if (found) {
3908                xs->here = &xs->header->xh_entries[index];
3909                trace_ocfs2_xattr_bucket_find(OCFS2_I(inode)->ip_blkno,
3910                        name, name_index, name_hash,
3911                        (unsigned long long)bucket_blkno(xs->bucket),
3912                        index);
3913        } else
3914                ret = -ENODATA;
3915
3916out:
3917        ocfs2_xattr_bucket_free(search);
3918        return ret;
3919}
3920
3921static int ocfs2_xattr_index_block_find(struct inode *inode,
3922                                        struct buffer_head *root_bh,
3923                                        int name_index,
3924                                        const char *name,
3925                                        struct ocfs2_xattr_search *xs)
3926{
3927        int ret;
3928        struct ocfs2_xattr_block *xb =
3929                        (struct ocfs2_xattr_block *)root_bh->b_data;
3930        struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3931        struct ocfs2_extent_list *el = &xb_root->xt_list;
3932        u64 p_blkno = 0;
3933        u32 first_hash, num_clusters = 0;
3934        u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
3935
3936        if (le16_to_cpu(el->l_next_free_rec) == 0)
3937                return -ENODATA;
3938
3939        trace_ocfs2_xattr_index_block_find(OCFS2_I(inode)->ip_blkno,
3940                                        name, name_index, name_hash,
3941                                        (unsigned long long)root_bh->b_blocknr,
3942                                        -1);
3943
3944        ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
3945                                  &num_clusters, el);
3946        if (ret) {
3947                mlog_errno(ret);
3948                goto out;
3949        }
3950
3951        BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
3952
3953        trace_ocfs2_xattr_index_block_find_rec(OCFS2_I(inode)->ip_blkno,
3954                                        name, name_index, first_hash,
3955                                        (unsigned long long)p_blkno,
3956                                        num_clusters);
3957
3958        ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
3959                                      p_blkno, first_hash, num_clusters, xs);
3960
3961out:
3962        return ret;
3963}
3964
3965static int ocfs2_iterate_xattr_buckets(struct inode *inode,
3966                                       u64 blkno,
3967                                       u32 clusters,
3968                                       xattr_bucket_func *func,
3969                                       void *para)
3970{
3971        int i, ret = 0;
3972        u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
3973        u32 num_buckets = clusters * bpc;
3974        struct ocfs2_xattr_bucket *bucket;
3975
3976        bucket = ocfs2_xattr_bucket_new(inode);
3977        if (!bucket) {
3978                mlog_errno(-ENOMEM);
3979                return -ENOMEM;
3980        }
3981
3982        trace_ocfs2_iterate_xattr_buckets(
3983                (unsigned long long)OCFS2_I(inode)->ip_blkno,
3984                (unsigned long long)blkno, clusters);
3985
3986        for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
3987                ret = ocfs2_read_xattr_bucket(bucket, blkno);
3988                if (ret) {
3989                        mlog_errno(ret);
3990                        break;
3991                }
3992
3993                /*
3994                 * The real bucket num in this series of blocks is stored
3995                 * in the 1st bucket.
3996                 */
3997                if (i == 0)
3998                        num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
3999
4000                trace_ocfs2_iterate_xattr_bucket((unsigned long long)blkno,
4001                     le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
4002                if (func) {
4003                        ret = func(inode, bucket, para);
4004                        if (ret && ret != -ERANGE)
4005                                mlog_errno(ret);
4006                        /* Fall through to bucket_relse() */
4007                }
4008
4009                ocfs2_xattr_bucket_relse(bucket);
4010                if (ret)
4011                        break;
4012        }
4013
4014        ocfs2_xattr_bucket_free(bucket);
4015        return ret;
4016}
4017
4018struct ocfs2_xattr_tree_list {
4019        char *buffer;
4020        size_t buffer_size;
4021        size_t result;
4022};
4023
4024static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
4025                                             struct ocfs2_xattr_header *xh,
4026                                             int index,
4027                                             int *block_off,
4028                                             int *new_offset)
4029{
4030        u16 name_offset;
4031
4032        if (index < 0 || index >= le16_to_cpu(xh->xh_count))
4033                return -EINVAL;
4034
4035        name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
4036
4037        *block_off = name_offset >> sb->s_blocksize_bits;
4038        *new_offset = name_offset % sb->s_blocksize;
4039
4040        return 0;
4041}
4042
4043static int ocfs2_list_xattr_bucket(struct inode *inode,
4044                                   struct ocfs2_xattr_bucket *bucket,
4045                                   void *para)
4046{
4047        int ret = 0, type;
4048        struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
4049        int i, block_off, new_offset;
4050        const char *name;
4051
4052        for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
4053                struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
4054                type = ocfs2_xattr_get_type(entry);
4055
4056                ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
4057                                                        bucket_xh(bucket),
4058                                                        i,
4059                                                        &block_off,
4060                                                        &new_offset);
4061                if (ret)
4062                        break;
4063
4064                name = (const char *)bucket_block(bucket, block_off) +
4065                        new_offset;
4066                ret = ocfs2_xattr_list_entry(inode->i_sb,
4067                                             xl->buffer,
4068                                             xl->buffer_size,
4069                                             &xl->result,
4070                                             type, name,
4071                                             entry->xe_name_len);
4072                if (ret)
4073                        break;
4074        }
4075
4076        return ret;
4077}
4078
4079static int ocfs2_iterate_xattr_index_block(struct inode *inode,
4080                                           struct buffer_head *blk_bh,
4081                                           xattr_tree_rec_func *rec_func,
4082                                           void *para)
4083{
4084        struct ocfs2_xattr_block *xb =
4085                        (struct ocfs2_xattr_block *)blk_bh->b_data;
4086        struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
4087        int ret = 0;
4088        u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
4089        u64 p_blkno = 0;
4090
4091        if (!el->l_next_free_rec || !rec_func)
4092                return 0;
4093
4094        while (name_hash > 0) {
4095                ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
4096                                          &e_cpos, &num_clusters, el);
4097                if (ret) {
4098                        mlog_errno(ret);
4099                        break;
4100                }
4101
4102                ret = rec_func(inode, blk_bh, p_blkno, e_cpos,
4103                               num_clusters, para);
4104                if (ret) {
4105                        if (ret != -ERANGE)
4106                                mlog_errno(ret);
4107                        break;
4108                }
4109
4110                if (e_cpos == 0)
4111                        break;
4112
4113                name_hash = e_cpos - 1;
4114        }
4115
4116        return ret;
4117
4118}
4119
4120static int ocfs2_list_xattr_tree_rec(struct inode *inode,
4121                                     struct buffer_head *root_bh,
4122                                     u64 blkno, u32 cpos, u32 len, void *para)
4123{
4124        return ocfs2_iterate_xattr_buckets(inode, blkno, len,
4125                                           ocfs2_list_xattr_bucket, para);
4126}
4127
4128static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
4129                                             struct buffer_head *blk_bh,
4130                                             char *buffer,
4131                                             size_t buffer_size)
4132{
4133        int ret;
4134        struct ocfs2_xattr_tree_list xl = {
4135                .buffer = buffer,
4136                .buffer_size = buffer_size,
4137                .result = 0,
4138        };
4139
4140        ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
4141                                              ocfs2_list_xattr_tree_rec, &xl);
4142        if (ret) {
4143                mlog_errno(ret);
4144                goto out;
4145        }
4146
4147        ret = xl.result;
4148out:
4149        return ret;
4150}
4151
4152static int cmp_xe(const void *a, const void *b)
4153{
4154        const struct ocfs2_xattr_entry *l = a, *r = b;
4155        u32 l_hash = le32_to_cpu(l->xe_name_hash);
4156        u32 r_hash = le32_to_cpu(r->xe_name_hash);
4157
4158        if (l_hash > r_hash)
4159                return 1;
4160        if (l_hash < r_hash)
4161                return -1;
4162        return 0;
4163}
4164
4165static void swap_xe(void *a, void *b, int size)
4166{
4167        struct ocfs2_xattr_entry *l = a, *r = b, tmp;
4168
4169        tmp = *l;
4170        memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
4171        memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
4172}
4173
4174/*
4175 * When the ocfs2_xattr_block is filled up, new bucket will be created
4176 * and all the xattr entries will be moved to the new bucket.
4177 * The header goes at the start of the bucket, and the names+values are
4178 * filled from the end.  This is why *target starts as the last buffer.
4179 * Note: we need to sort the entries since they are not saved in order
4180 * in the ocfs2_xattr_block.
4181 */
4182static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
4183                                           struct buffer_head *xb_bh,
4184                                           struct ocfs2_xattr_bucket *bucket)
4185{
4186        int i, blocksize = inode->i_sb->s_blocksize;
4187        int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4188        u16 offset, size, off_change;
4189        struct ocfs2_xattr_entry *xe;
4190        struct ocfs2_xattr_block *xb =
4191                                (struct ocfs2_xattr_block *)xb_bh->b_data;
4192        struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
4193        struct ocfs2_xattr_header *xh = bucket_xh(bucket);
4194        u16 count = le16_to_cpu(xb_xh->xh_count);
4195        char *src = xb_bh->b_data;
4196        char *target = bucket_block(bucket, blks - 1);
4197
4198        trace_ocfs2_cp_xattr_block_to_bucket_begin(
4199                                (unsigned long long)xb_bh->b_blocknr,
4200                                (unsigned long long)bucket_blkno(bucket));
4201
4202        for (i = 0; i < blks; i++)
4203                memset(bucket_block(bucket, i), 0, blocksize);
4204
4205        /*
4206         * Since the xe_name_offset is based on ocfs2_xattr_header,
4207         * there is a offset change corresponding to the change of
4208         * ocfs2_xattr_header's position.
4209         */
4210        off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
4211        xe = &xb_xh->xh_entries[count - 1];
4212        offset = le16_to_cpu(xe->xe_name_offset) + off_change;
4213        size = blocksize - offset;
4214
4215        /* copy all the names and values. */
4216        memcpy(target + offset, src + offset, size);
4217
4218        /* Init new header now. */
4219        xh->xh_count = xb_xh->xh_count;
4220        xh->xh_num_buckets = cpu_to_le16(1);
4221        xh->xh_name_value_len = cpu_to_le16(size);
4222        xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
4223
4224        /* copy all the entries. */
4225        target = bucket_block(bucket, 0);
4226        offset = offsetof(struct ocfs2_xattr_header, xh_entries);
4227        size = count * sizeof(struct ocfs2_xattr_entry);
4228        memcpy(target + offset, (char *)xb_xh + offset, size);
4229
4230        /* Change the xe offset for all the xe because of the move. */
4231        off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
4232                 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
4233        for (i = 0; i < count; i++)
4234                le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
4235
4236        trace_ocfs2_cp_xattr_block_to_bucket_end(offset, size, off_change);
4237
4238        sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
4239             cmp_xe, swap_xe);
4240}
4241
4242/*
4243 * After we move xattr from block to index btree, we have to
4244 * update ocfs2_xattr_search to the new xe and base.
4245 *
4246 * When the entry is in xattr block, xattr_bh indicates the storage place.
4247 * While if the entry is in index b-tree, "bucket" indicates the
4248 * real place of the xattr.
4249 */
4250static void ocfs2_xattr_update_xattr_search(struct inode *inode,
4251                                            struct ocfs2_xattr_search *xs,
4252                                            struct buffer_head *old_bh)
4253{
4254        char *buf = old_bh->b_data;
4255        struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
4256        struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
4257        int i;
4258
4259        xs->header = bucket_xh(xs->bucket);
4260        xs->base = bucket_block(xs->bucket, 0);
4261        xs->end = xs->base + inode->i_sb->s_blocksize;
4262
4263        if (xs->not_found)
4264                return;
4265
4266        i = xs->here - old_xh->xh_entries;
4267        xs->here = &xs->header->xh_entries[i];
4268}
4269
4270static int ocfs2_xattr_create_index_block(struct inode *inode,
4271                                          struct ocfs2_xattr_search *xs,
4272                                          struct ocfs2_xattr_set_ctxt *ctxt)
4273{
4274        int ret;
4275        u32 bit_off, len;
4276        u64 blkno;
4277        handle_t *handle = ctxt->handle;
4278        struct ocfs2_inode_info *oi = OCFS2_I(inode);
4279        struct buffer_head *xb_bh = xs->xattr_bh;
4280        struct ocfs2_xattr_block *xb =
4281                        (struct ocfs2_xattr_block *)xb_bh->b_data;
4282        struct ocfs2_xattr_tree_root *xr;
4283        u16 xb_flags = le16_to_cpu(xb->xb_flags);
4284
4285        trace_ocfs2_xattr_create_index_block_begin(
4286                                (unsigned long long)xb_bh->b_blocknr);
4287
4288        BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
4289        BUG_ON(!xs->bucket);
4290
4291        /*
4292         * XXX:
4293         * We can use this lock for now, and maybe move to a dedicated mutex
4294         * if performance becomes a problem later.
4295         */
4296        down_write(&oi->ip_alloc_sem);
4297
4298        ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), xb_bh,
4299                                      OCFS2_JOURNAL_ACCESS_WRITE);
4300        if (ret) {
4301                mlog_errno(ret);
4302                goto out;
4303        }
4304
4305        ret = __ocfs2_claim_clusters(handle, ctxt->data_ac,
4306                                     1, 1, &bit_off, &len);
4307        if (ret) {
4308                mlog_errno(ret);
4309                goto out;
4310        }
4311
4312        /*
4313         * The bucket may spread in many blocks, and
4314         * we will only touch the 1st block and the last block
4315         * in the whole bucket(one for entry and one for data).
4316         */
4317        blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
4318
4319        trace_ocfs2_xattr_create_index_block((unsigned long long)blkno);
4320
4321        ret = ocfs2_init_xattr_bucket(xs->bucket, blkno, 1);
4322        if (ret) {
4323                mlog_errno(ret);
4324                goto out;
4325        }
4326
4327        ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
4328                                                OCFS2_JOURNAL_ACCESS_CREATE);
4329        if (ret) {
4330                mlog_errno(ret);
4331                goto out;
4332        }
4333
4334        ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
4335        ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
4336
4337        ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
4338
4339        /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
4340        memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
4341               offsetof(struct ocfs2_xattr_block, xb_attrs));
4342
4343        xr = &xb->xb_attrs.xb_root;
4344        xr->xt_clusters = cpu_to_le32(1);
4345        xr->xt_last_eb_blk = 0;
4346        xr->xt_list.l_tree_depth = 0;
4347        xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
4348        xr->xt_list.l_next_free_rec = cpu_to_le16(1);
4349
4350        xr->xt_list.l_recs[0].e_cpos = 0;
4351        xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
4352        xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
4353
4354        xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
4355
4356        ocfs2_journal_dirty(handle, xb_bh);
4357
4358out:
4359        up_write(&oi->ip_alloc_sem);
4360
4361        return ret;
4362}
4363
4364static int cmp_xe_offset(const void *a, const void *b)
4365{
4366        const struct ocfs2_xattr_entry *l = a, *r = b;
4367        u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
4368        u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
4369
4370        if (l_name_offset < r_name_offset)
4371                return 1;
4372        if (l_name_offset > r_name_offset)
4373                return -1;
4374        return 0;
4375}
4376
4377/*
4378 * defrag a xattr bucket if we find that the bucket has some
4379 * holes beteen name/value pairs.
4380 * We will move all the name/value pairs to the end of the bucket
4381 * so that we can spare some space for insertion.
4382 */
4383static int ocfs2_defrag_xattr_bucket(struct inode *inode,
4384                                     handle_t *handle,
4385                                     struct ocfs2_xattr_bucket *bucket)
4386{
4387        int ret, i;
4388        size_t end, offset, len;
4389        struct ocfs2_xattr_header *xh;
4390        char *entries, *buf, *bucket_buf = NULL;
4391        u64 blkno = bucket_blkno(bucket);
4392        u16 xh_free_start;
4393        size_t blocksize = inode->i_sb->s_blocksize;
4394        struct ocfs2_xattr_entry *xe;
4395
4396        /*
4397         * In order to make the operation more efficient and generic,
4398         * we copy all the blocks into a contiguous memory and do the
4399         * defragment there, so if anything is error, we will not touch
4400         * the real block.
4401         */
4402        bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
4403        if (!bucket_buf) {
4404                ret = -EIO;
4405                goto out;
4406        }
4407
4408        buf = bucket_buf;
4409        for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4410                memcpy(buf, bucket_block(bucket, i), blocksize);
4411
4412        ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
4413                                                OCFS2_JOURNAL_ACCESS_WRITE);
4414        if (ret < 0) {
4415                mlog_errno(ret);
4416                goto out;
4417        }
4418
4419        xh = (struct ocfs2_xattr_header *)bucket_buf;
4420        entries = (char *)xh->xh_entries;
4421        xh_free_start = le16_to_cpu(xh->xh_free_start);
4422
4423        trace_ocfs2_defrag_xattr_bucket(
4424             (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
4425             xh_free_start, le16_to_cpu(xh->xh_name_value_len));
4426
4427        /*
4428         * sort all the entries by their offset.
4429         * the largest will be the first, so that we can
4430         * move them to the end one by one.
4431         */
4432        sort(entries, le16_to_cpu(xh->xh_count),
4433             sizeof(struct ocfs2_xattr_entry),
4434             cmp_xe_offset, swap_xe);
4435
4436        /* Move all name/values to the end of the bucket. */
4437        xe = xh->xh_entries;
4438        end = OCFS2_XATTR_BUCKET_SIZE;
4439        for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
4440                offset = le16_to_cpu(xe->xe_name_offset);
4441                len = namevalue_size_xe(xe);
4442
4443                /*
4444                 * We must make sure that the name/value pair
4445                 * exist in the same block. So adjust end to
4446                 * the previous block end if needed.
4447                 */
4448                if (((end - len) / blocksize !=
4449                        (end - 1) / blocksize))
4450                        end = end - end % blocksize;
4451
4452                if (end > offset + len) {
4453                        memmove(bucket_buf + end - len,
4454                                bucket_buf + offset, len);
4455                        xe->xe_name_offset = cpu_to_le16(end - len);
4456                }
4457
4458                mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
4459                                "bucket %llu\n", (unsigned long long)blkno);
4460
4461                end -= len;
4462        }
4463
4464        mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
4465                        "bucket %llu\n", (unsigned long long)blkno);
4466
4467        if (xh_free_start == end)
4468                goto out;
4469
4470        memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
4471        xh->xh_free_start = cpu_to_le16(end);
4472
4473        /* sort the entries by their name_hash. */
4474        sort(entries, le16_to_cpu(xh->xh_count),
4475             sizeof(struct ocfs2_xattr_entry),
4476             cmp_xe, swap_xe);
4477
4478        buf = bucket_buf;
4479        for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4480                memcpy(bucket_block(bucket, i), buf, blocksize);
4481        ocfs2_xattr_bucket_journal_dirty(handle, bucket);
4482
4483out:
4484        kfree(bucket_buf);
4485        return ret;
4486}
4487
4488/*
4489 * prev_blkno points to the start of an existing extent.  new_blkno
4490 * points to a newly allocated extent.  Because we know each of our
4491 * clusters contains more than bucket, we can easily split one cluster
4492 * at a bucket boundary.  So we take the last cluster of the existing
4493 * extent and split it down the middle.  We move the last half of the
4494 * buckets in the last cluster of the existing extent over to the new
4495 * extent.
4496 *
4497 * first_bh is the buffer at prev_blkno so we can update the existing
4498 * extent's bucket count.  header_bh is the bucket were we were hoping
4499 * to insert our xattr.  If the bucket move places the target in the new
4500 * extent, we'll update first_bh and header_bh after modifying the old
4501 * extent.
4502 *
4503 * first_hash will be set as the 1st xe's name_hash in the new extent.
4504 */
4505static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
4506                                               handle_t *handle,
4507                                               struct ocfs2_xattr_bucket *first,
4508                                               struct ocfs2_xattr_bucket *target,
4509                                               u64 new_blkno,
4510                                               u32 num_clusters,
4511                                               u32 *first_hash)
4512{
4513        int ret;
4514        struct super_block *sb = inode->i_sb;
4515        int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb);
4516        int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
4517        int to_move = num_buckets / 2;
4518        u64 src_blkno;
4519        u64 last_cluster_blkno = bucket_blkno(first) +
4520                ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1));
4521
4522        BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets);
4523        BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize);
4524
4525        trace_ocfs2_mv_xattr_bucket_cross_cluster(
4526                                (unsigned long long)last_cluster_blkno,
4527                                (unsigned long long)new_blkno);
4528