ocfs2: Reset xattr value size after xa_cleanup_value_truncate().
[safe/jmp/linux-2.6] / fs / ocfs2 / xattr.c
1 /* -*- mode: c; c-basic-offset: 8; -*-
2  * vim: noexpandtab sw=8 ts=8 sts=0:
3  *
4  * xattr.c
5  *
6  * Copyright (C) 2004, 2008 Oracle.  All rights reserved.
7  *
8  * CREDITS:
9  * Lots of code in this file is copy from linux/fs/ext3/xattr.c.
10  * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public
14  * License version 2 as published by the Free Software Foundation.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * General Public License for more details.
20  */
21
22 #include <linux/capability.h>
23 #include <linux/fs.h>
24 #include <linux/types.h>
25 #include <linux/slab.h>
26 #include <linux/highmem.h>
27 #include <linux/pagemap.h>
28 #include <linux/uio.h>
29 #include <linux/sched.h>
30 #include <linux/splice.h>
31 #include <linux/mount.h>
32 #include <linux/writeback.h>
33 #include <linux/falloc.h>
34 #include <linux/sort.h>
35 #include <linux/init.h>
36 #include <linux/module.h>
37 #include <linux/string.h>
38 #include <linux/security.h>
39
40 #define MLOG_MASK_PREFIX ML_XATTR
41 #include <cluster/masklog.h>
42
43 #include "ocfs2.h"
44 #include "alloc.h"
45 #include "blockcheck.h"
46 #include "dlmglue.h"
47 #include "file.h"
48 #include "symlink.h"
49 #include "sysfile.h"
50 #include "inode.h"
51 #include "journal.h"
52 #include "ocfs2_fs.h"
53 #include "suballoc.h"
54 #include "uptodate.h"
55 #include "buffer_head_io.h"
56 #include "super.h"
57 #include "xattr.h"
58 #include "refcounttree.h"
59 #include "acl.h"
60
61 struct ocfs2_xattr_def_value_root {
62         struct ocfs2_xattr_value_root   xv;
63         struct ocfs2_extent_rec         er;
64 };
65
66 struct ocfs2_xattr_bucket {
67         /* The inode these xattrs are associated with */
68         struct inode *bu_inode;
69
70         /* The actual buffers that make up the bucket */
71         struct buffer_head *bu_bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
72
73         /* How many blocks make up one bucket for this filesystem */
74         int bu_blocks;
75 };
76
77 struct ocfs2_xattr_set_ctxt {
78         handle_t *handle;
79         struct ocfs2_alloc_context *meta_ac;
80         struct ocfs2_alloc_context *data_ac;
81         struct ocfs2_cached_dealloc_ctxt dealloc;
82 };
83
84 #define OCFS2_XATTR_ROOT_SIZE   (sizeof(struct ocfs2_xattr_def_value_root))
85 #define OCFS2_XATTR_INLINE_SIZE 80
86 #define OCFS2_XATTR_HEADER_GAP  4
87 #define OCFS2_XATTR_FREE_IN_IBODY       (OCFS2_MIN_XATTR_INLINE_SIZE \
88                                          - sizeof(struct ocfs2_xattr_header) \
89                                          - OCFS2_XATTR_HEADER_GAP)
90 #define OCFS2_XATTR_FREE_IN_BLOCK(ptr)  ((ptr)->i_sb->s_blocksize \
91                                          - sizeof(struct ocfs2_xattr_block) \
92                                          - sizeof(struct ocfs2_xattr_header) \
93                                          - OCFS2_XATTR_HEADER_GAP)
94
95 static struct ocfs2_xattr_def_value_root def_xv = {
96         .xv.xr_list.l_count = cpu_to_le16(1),
97 };
98
99 struct xattr_handler *ocfs2_xattr_handlers[] = {
100         &ocfs2_xattr_user_handler,
101         &ocfs2_xattr_acl_access_handler,
102         &ocfs2_xattr_acl_default_handler,
103         &ocfs2_xattr_trusted_handler,
104         &ocfs2_xattr_security_handler,
105         NULL
106 };
107
108 static struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
109         [OCFS2_XATTR_INDEX_USER]        = &ocfs2_xattr_user_handler,
110         [OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS]
111                                         = &ocfs2_xattr_acl_access_handler,
112         [OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT]
113                                         = &ocfs2_xattr_acl_default_handler,
114         [OCFS2_XATTR_INDEX_TRUSTED]     = &ocfs2_xattr_trusted_handler,
115         [OCFS2_XATTR_INDEX_SECURITY]    = &ocfs2_xattr_security_handler,
116 };
117
118 struct ocfs2_xattr_info {
119         int             xi_name_index;
120         const char      *xi_name;
121         int             xi_name_len;
122         const void      *xi_value;
123         size_t          xi_value_len;
124 };
125
126 struct ocfs2_xattr_search {
127         struct buffer_head *inode_bh;
128         /*
129          * xattr_bh point to the block buffer head which has extended attribute
130          * when extended attribute in inode, xattr_bh is equal to inode_bh.
131          */
132         struct buffer_head *xattr_bh;
133         struct ocfs2_xattr_header *header;
134         struct ocfs2_xattr_bucket *bucket;
135         void *base;
136         void *end;
137         struct ocfs2_xattr_entry *here;
138         int not_found;
139 };
140
141 /* Operations on struct ocfs2_xa_entry */
142 struct ocfs2_xa_loc;
143 struct ocfs2_xa_loc_operations {
144         /*
145          * Journal functions
146          */
147         int (*xlo_journal_access)(handle_t *handle, struct ocfs2_xa_loc *loc,
148                                   int type);
149         void (*xlo_journal_dirty)(handle_t *handle, struct ocfs2_xa_loc *loc);
150
151         /*
152          * Return a pointer to the appropriate buffer in loc->xl_storage
153          * at the given offset from loc->xl_header.
154          */
155         void *(*xlo_offset_pointer)(struct ocfs2_xa_loc *loc, int offset);
156
157         /* Can we reuse the existing entry for the new value? */
158         int (*xlo_can_reuse)(struct ocfs2_xa_loc *loc,
159                              struct ocfs2_xattr_info *xi);
160
161         /* How much space is needed for the new value? */
162         int (*xlo_check_space)(struct ocfs2_xa_loc *loc,
163                                struct ocfs2_xattr_info *xi);
164
165         /*
166          * Return the offset of the first name+value pair.  This is
167          * the start of our downward-filling free space.
168          */
169         int (*xlo_get_free_start)(struct ocfs2_xa_loc *loc);
170
171         /*
172          * Remove the name+value at this location.  Do whatever is
173          * appropriate with the remaining name+value pairs.
174          */
175         void (*xlo_wipe_namevalue)(struct ocfs2_xa_loc *loc);
176
177         /* Fill xl_entry with a new entry */
178         void (*xlo_add_entry)(struct ocfs2_xa_loc *loc, u32 name_hash);
179
180         /* Add name+value storage to an entry */
181         void (*xlo_add_namevalue)(struct ocfs2_xa_loc *loc, int size);
182
183         /*
184          * Initialize the value buf's access and bh fields for this entry.
185          * ocfs2_xa_fill_value_buf() will handle the xv pointer.
186          */
187         void (*xlo_fill_value_buf)(struct ocfs2_xa_loc *loc,
188                                    struct ocfs2_xattr_value_buf *vb);
189 };
190
191 /*
192  * Describes an xattr entry location.  This is a memory structure
193  * tracking the on-disk structure.
194  */
195 struct ocfs2_xa_loc {
196         /* This xattr belongs to this inode */
197         struct inode *xl_inode;
198
199         /* The ocfs2_xattr_header inside the on-disk storage. Not NULL. */
200         struct ocfs2_xattr_header *xl_header;
201
202         /* Bytes from xl_header to the end of the storage */
203         int xl_size;
204
205         /*
206          * The ocfs2_xattr_entry this location describes.  If this is
207          * NULL, this location describes the on-disk structure where it
208          * would have been.
209          */
210         struct ocfs2_xattr_entry *xl_entry;
211
212         /*
213          * Internal housekeeping
214          */
215
216         /* Buffer(s) containing this entry */
217         void *xl_storage;
218
219         /* Operations on the storage backing this location */
220         const struct ocfs2_xa_loc_operations *xl_ops;
221 };
222
223 /*
224  * Convenience functions to calculate how much space is needed for a
225  * given name+value pair
226  */
227 static int namevalue_size(int name_len, uint64_t value_len)
228 {
229         if (value_len > OCFS2_XATTR_INLINE_SIZE)
230                 return OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
231         else
232                 return OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
233 }
234
235 static int namevalue_size_xi(struct ocfs2_xattr_info *xi)
236 {
237         return namevalue_size(xi->xi_name_len, xi->xi_value_len);
238 }
239
240 static int namevalue_size_xe(struct ocfs2_xattr_entry *xe)
241 {
242         u64 value_len = le64_to_cpu(xe->xe_value_size);
243
244         BUG_ON((value_len > OCFS2_XATTR_INLINE_SIZE) &&
245                ocfs2_xattr_is_local(xe));
246         return namevalue_size(xe->xe_name_len, value_len);
247 }
248
249
250 static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
251                                              struct ocfs2_xattr_header *xh,
252                                              int index,
253                                              int *block_off,
254                                              int *new_offset);
255
256 static int ocfs2_xattr_block_find(struct inode *inode,
257                                   int name_index,
258                                   const char *name,
259                                   struct ocfs2_xattr_search *xs);
260 static int ocfs2_xattr_index_block_find(struct inode *inode,
261                                         struct buffer_head *root_bh,
262                                         int name_index,
263                                         const char *name,
264                                         struct ocfs2_xattr_search *xs);
265
266 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
267                                         struct buffer_head *blk_bh,
268                                         char *buffer,
269                                         size_t buffer_size);
270
271 static int ocfs2_xattr_create_index_block(struct inode *inode,
272                                           struct ocfs2_xattr_search *xs,
273                                           struct ocfs2_xattr_set_ctxt *ctxt);
274
275 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
276                                              struct ocfs2_xattr_info *xi,
277                                              struct ocfs2_xattr_search *xs,
278                                              struct ocfs2_xattr_set_ctxt *ctxt);
279
280 typedef int (xattr_tree_rec_func)(struct inode *inode,
281                                   struct buffer_head *root_bh,
282                                   u64 blkno, u32 cpos, u32 len, void *para);
283 static int ocfs2_iterate_xattr_index_block(struct inode *inode,
284                                            struct buffer_head *root_bh,
285                                            xattr_tree_rec_func *rec_func,
286                                            void *para);
287 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
288                                         struct ocfs2_xattr_bucket *bucket,
289                                         void *para);
290 static int ocfs2_rm_xattr_cluster(struct inode *inode,
291                                   struct buffer_head *root_bh,
292                                   u64 blkno,
293                                   u32 cpos,
294                                   u32 len,
295                                   void *para);
296
297 static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
298                                   u64 src_blk, u64 last_blk, u64 to_blk,
299                                   unsigned int start_bucket,
300                                   u32 *first_hash);
301 static int ocfs2_prepare_refcount_xattr(struct inode *inode,
302                                         struct ocfs2_dinode *di,
303                                         struct ocfs2_xattr_info *xi,
304                                         struct ocfs2_xattr_search *xis,
305                                         struct ocfs2_xattr_search *xbs,
306                                         struct ocfs2_refcount_tree **ref_tree,
307                                         int *meta_need,
308                                         int *credits);
309 static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
310                                            struct ocfs2_xattr_bucket *bucket,
311                                            int offset,
312                                            struct ocfs2_xattr_value_root **xv,
313                                            struct buffer_head **bh);
314
315 static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
316 {
317         return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
318 }
319
320 static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
321 {
322         return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
323 }
324
325 #define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
326 #define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
327 #define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
328
329 static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
330 {
331         struct ocfs2_xattr_bucket *bucket;
332         int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
333
334         BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
335
336         bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
337         if (bucket) {
338                 bucket->bu_inode = inode;
339                 bucket->bu_blocks = blks;
340         }
341
342         return bucket;
343 }
344
345 static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
346 {
347         int i;
348
349         for (i = 0; i < bucket->bu_blocks; i++) {
350                 brelse(bucket->bu_bhs[i]);
351                 bucket->bu_bhs[i] = NULL;
352         }
353 }
354
355 static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
356 {
357         if (bucket) {
358                 ocfs2_xattr_bucket_relse(bucket);
359                 bucket->bu_inode = NULL;
360                 kfree(bucket);
361         }
362 }
363
364 /*
365  * A bucket that has never been written to disk doesn't need to be
366  * read.  We just need the buffer_heads.  Don't call this for
367  * buckets that are already on disk.  ocfs2_read_xattr_bucket() initializes
368  * them fully.
369  */
370 static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
371                                    u64 xb_blkno)
372 {
373         int i, rc = 0;
374
375         for (i = 0; i < bucket->bu_blocks; i++) {
376                 bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
377                                               xb_blkno + i);
378                 if (!bucket->bu_bhs[i]) {
379                         rc = -EIO;
380                         mlog_errno(rc);
381                         break;
382                 }
383
384                 if (!ocfs2_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
385                                            bucket->bu_bhs[i]))
386                         ocfs2_set_new_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
387                                                       bucket->bu_bhs[i]);
388         }
389
390         if (rc)
391                 ocfs2_xattr_bucket_relse(bucket);
392         return rc;
393 }
394
395 /* Read the xattr bucket at xb_blkno */
396 static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
397                                    u64 xb_blkno)
398 {
399         int rc;
400
401         rc = ocfs2_read_blocks(INODE_CACHE(bucket->bu_inode), xb_blkno,
402                                bucket->bu_blocks, bucket->bu_bhs, 0,
403                                NULL);
404         if (!rc) {
405                 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
406                 rc = ocfs2_validate_meta_ecc_bhs(bucket->bu_inode->i_sb,
407                                                  bucket->bu_bhs,
408                                                  bucket->bu_blocks,
409                                                  &bucket_xh(bucket)->xh_check);
410                 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
411                 if (rc)
412                         mlog_errno(rc);
413         }
414
415         if (rc)
416                 ocfs2_xattr_bucket_relse(bucket);
417         return rc;
418 }
419
420 static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
421                                              struct ocfs2_xattr_bucket *bucket,
422                                              int type)
423 {
424         int i, rc = 0;
425
426         for (i = 0; i < bucket->bu_blocks; i++) {
427                 rc = ocfs2_journal_access(handle,
428                                           INODE_CACHE(bucket->bu_inode),
429                                           bucket->bu_bhs[i], type);
430                 if (rc) {
431                         mlog_errno(rc);
432                         break;
433                 }
434         }
435
436         return rc;
437 }
438
439 static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
440                                              struct ocfs2_xattr_bucket *bucket)
441 {
442         int i;
443
444         spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
445         ocfs2_compute_meta_ecc_bhs(bucket->bu_inode->i_sb,
446                                    bucket->bu_bhs, bucket->bu_blocks,
447                                    &bucket_xh(bucket)->xh_check);
448         spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
449
450         for (i = 0; i < bucket->bu_blocks; i++)
451                 ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
452 }
453
454 static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
455                                          struct ocfs2_xattr_bucket *src)
456 {
457         int i;
458         int blocksize = src->bu_inode->i_sb->s_blocksize;
459
460         BUG_ON(dest->bu_blocks != src->bu_blocks);
461         BUG_ON(dest->bu_inode != src->bu_inode);
462
463         for (i = 0; i < src->bu_blocks; i++) {
464                 memcpy(bucket_block(dest, i), bucket_block(src, i),
465                        blocksize);
466         }
467 }
468
469 static int ocfs2_validate_xattr_block(struct super_block *sb,
470                                       struct buffer_head *bh)
471 {
472         int rc;
473         struct ocfs2_xattr_block *xb =
474                 (struct ocfs2_xattr_block *)bh->b_data;
475
476         mlog(0, "Validating xattr block %llu\n",
477              (unsigned long long)bh->b_blocknr);
478
479         BUG_ON(!buffer_uptodate(bh));
480
481         /*
482          * If the ecc fails, we return the error but otherwise
483          * leave the filesystem running.  We know any error is
484          * local to this block.
485          */
486         rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &xb->xb_check);
487         if (rc)
488                 return rc;
489
490         /*
491          * Errors after here are fatal
492          */
493
494         if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
495                 ocfs2_error(sb,
496                             "Extended attribute block #%llu has bad "
497                             "signature %.*s",
498                             (unsigned long long)bh->b_blocknr, 7,
499                             xb->xb_signature);
500                 return -EINVAL;
501         }
502
503         if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) {
504                 ocfs2_error(sb,
505                             "Extended attribute block #%llu has an "
506                             "invalid xb_blkno of %llu",
507                             (unsigned long long)bh->b_blocknr,
508                             (unsigned long long)le64_to_cpu(xb->xb_blkno));
509                 return -EINVAL;
510         }
511
512         if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) {
513                 ocfs2_error(sb,
514                             "Extended attribute block #%llu has an invalid "
515                             "xb_fs_generation of #%u",
516                             (unsigned long long)bh->b_blocknr,
517                             le32_to_cpu(xb->xb_fs_generation));
518                 return -EINVAL;
519         }
520
521         return 0;
522 }
523
524 static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno,
525                                   struct buffer_head **bh)
526 {
527         int rc;
528         struct buffer_head *tmp = *bh;
529
530         rc = ocfs2_read_block(INODE_CACHE(inode), xb_blkno, &tmp,
531                               ocfs2_validate_xattr_block);
532
533         /* If ocfs2_read_block() got us a new bh, pass it up. */
534         if (!rc && !*bh)
535                 *bh = tmp;
536
537         return rc;
538 }
539
540 static inline const char *ocfs2_xattr_prefix(int name_index)
541 {
542         struct xattr_handler *handler = NULL;
543
544         if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
545                 handler = ocfs2_xattr_handler_map[name_index];
546
547         return handler ? handler->prefix : NULL;
548 }
549
550 static u32 ocfs2_xattr_name_hash(struct inode *inode,
551                                  const char *name,
552                                  int name_len)
553 {
554         /* Get hash value of uuid from super block */
555         u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
556         int i;
557
558         /* hash extended attribute name */
559         for (i = 0; i < name_len; i++) {
560                 hash = (hash << OCFS2_HASH_SHIFT) ^
561                        (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
562                        *name++;
563         }
564
565         return hash;
566 }
567
568 static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
569 {
570         return namevalue_size(name_len, value_len) +
571                 sizeof(struct ocfs2_xattr_entry);
572 }
573
574 static int ocfs2_xi_entry_usage(struct ocfs2_xattr_info *xi)
575 {
576         return namevalue_size_xi(xi) +
577                 sizeof(struct ocfs2_xattr_entry);
578 }
579
580 static int ocfs2_xe_entry_usage(struct ocfs2_xattr_entry *xe)
581 {
582         return namevalue_size_xe(xe) +
583                 sizeof(struct ocfs2_xattr_entry);
584 }
585
586 int ocfs2_calc_security_init(struct inode *dir,
587                              struct ocfs2_security_xattr_info *si,
588                              int *want_clusters,
589                              int *xattr_credits,
590                              struct ocfs2_alloc_context **xattr_ac)
591 {
592         int ret = 0;
593         struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
594         int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
595                                                  si->value_len);
596
597         /*
598          * The max space of security xattr taken inline is
599          * 256(name) + 80(value) + 16(entry) = 352 bytes,
600          * So reserve one metadata block for it is ok.
601          */
602         if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
603             s_size > OCFS2_XATTR_FREE_IN_IBODY) {
604                 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
605                 if (ret) {
606                         mlog_errno(ret);
607                         return ret;
608                 }
609                 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
610         }
611
612         /* reserve clusters for xattr value which will be set in B tree*/
613         if (si->value_len > OCFS2_XATTR_INLINE_SIZE) {
614                 int new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
615                                                             si->value_len);
616
617                 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
618                                                            new_clusters);
619                 *want_clusters += new_clusters;
620         }
621         return ret;
622 }
623
624 int ocfs2_calc_xattr_init(struct inode *dir,
625                           struct buffer_head *dir_bh,
626                           int mode,
627                           struct ocfs2_security_xattr_info *si,
628                           int *want_clusters,
629                           int *xattr_credits,
630                           int *want_meta)
631 {
632         int ret = 0;
633         struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
634         int s_size = 0, a_size = 0, acl_len = 0, new_clusters;
635
636         if (si->enable)
637                 s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
638                                                      si->value_len);
639
640         if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
641                 acl_len = ocfs2_xattr_get_nolock(dir, dir_bh,
642                                         OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
643                                         "", NULL, 0);
644                 if (acl_len > 0) {
645                         a_size = ocfs2_xattr_entry_real_size(0, acl_len);
646                         if (S_ISDIR(mode))
647                                 a_size <<= 1;
648                 } else if (acl_len != 0 && acl_len != -ENODATA) {
649                         mlog_errno(ret);
650                         return ret;
651                 }
652         }
653
654         if (!(s_size + a_size))
655                 return ret;
656
657         /*
658          * The max space of security xattr taken inline is
659          * 256(name) + 80(value) + 16(entry) = 352 bytes,
660          * The max space of acl xattr taken inline is
661          * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
662          * when blocksize = 512, may reserve one more cluser for
663          * xattr bucket, otherwise reserve one metadata block
664          * for them is ok.
665          * If this is a new directory with inline data,
666          * we choose to reserve the entire inline area for
667          * directory contents and force an external xattr block.
668          */
669         if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
670             (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) ||
671             (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) {
672                 *want_meta = *want_meta + 1;
673                 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
674         }
675
676         if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
677             (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
678                 *want_clusters += 1;
679                 *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
680         }
681
682         /*
683          * reserve credits and clusters for xattrs which has large value
684          * and have to be set outside
685          */
686         if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE) {
687                 new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
688                                                         si->value_len);
689                 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
690                                                            new_clusters);
691                 *want_clusters += new_clusters;
692         }
693         if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
694             acl_len > OCFS2_XATTR_INLINE_SIZE) {
695                 /* for directory, it has DEFAULT and ACCESS two types of acls */
696                 new_clusters = (S_ISDIR(mode) ? 2 : 1) *
697                                 ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
698                 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
699                                                            new_clusters);
700                 *want_clusters += new_clusters;
701         }
702
703         return ret;
704 }
705
706 static int ocfs2_xattr_extend_allocation(struct inode *inode,
707                                          u32 clusters_to_add,
708                                          struct ocfs2_xattr_value_buf *vb,
709                                          struct ocfs2_xattr_set_ctxt *ctxt)
710 {
711         int status = 0;
712         handle_t *handle = ctxt->handle;
713         enum ocfs2_alloc_restarted why;
714         u32 prev_clusters, logical_start = le32_to_cpu(vb->vb_xv->xr_clusters);
715         struct ocfs2_extent_tree et;
716
717         mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
718
719         ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
720
721         status = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
722                               OCFS2_JOURNAL_ACCESS_WRITE);
723         if (status < 0) {
724                 mlog_errno(status);
725                 goto leave;
726         }
727
728         prev_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
729         status = ocfs2_add_clusters_in_btree(handle,
730                                              &et,
731                                              &logical_start,
732                                              clusters_to_add,
733                                              0,
734                                              ctxt->data_ac,
735                                              ctxt->meta_ac,
736                                              &why);
737         if (status < 0) {
738                 mlog_errno(status);
739                 goto leave;
740         }
741
742         ocfs2_journal_dirty(handle, vb->vb_bh);
743
744         clusters_to_add -= le32_to_cpu(vb->vb_xv->xr_clusters) - prev_clusters;
745
746         /*
747          * We should have already allocated enough space before the transaction,
748          * so no need to restart.
749          */
750         BUG_ON(why != RESTART_NONE || clusters_to_add);
751
752 leave:
753
754         return status;
755 }
756
757 static int __ocfs2_remove_xattr_range(struct inode *inode,
758                                       struct ocfs2_xattr_value_buf *vb,
759                                       u32 cpos, u32 phys_cpos, u32 len,
760                                       unsigned int ext_flags,
761                                       struct ocfs2_xattr_set_ctxt *ctxt)
762 {
763         int ret;
764         u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
765         handle_t *handle = ctxt->handle;
766         struct ocfs2_extent_tree et;
767
768         ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
769
770         ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
771                             OCFS2_JOURNAL_ACCESS_WRITE);
772         if (ret) {
773                 mlog_errno(ret);
774                 goto out;
775         }
776
777         ret = ocfs2_remove_extent(handle, &et, cpos, len, ctxt->meta_ac,
778                                   &ctxt->dealloc);
779         if (ret) {
780                 mlog_errno(ret);
781                 goto out;
782         }
783
784         le32_add_cpu(&vb->vb_xv->xr_clusters, -len);
785         ocfs2_journal_dirty(handle, vb->vb_bh);
786
787         if (ext_flags & OCFS2_EXT_REFCOUNTED)
788                 ret = ocfs2_decrease_refcount(inode, handle,
789                                         ocfs2_blocks_to_clusters(inode->i_sb,
790                                                                  phys_blkno),
791                                         len, ctxt->meta_ac, &ctxt->dealloc, 1);
792         else
793                 ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc,
794                                                   phys_blkno, len);
795         if (ret)
796                 mlog_errno(ret);
797
798 out:
799         return ret;
800 }
801
802 static int ocfs2_xattr_shrink_size(struct inode *inode,
803                                    u32 old_clusters,
804                                    u32 new_clusters,
805                                    struct ocfs2_xattr_value_buf *vb,
806                                    struct ocfs2_xattr_set_ctxt *ctxt)
807 {
808         int ret = 0;
809         unsigned int ext_flags;
810         u32 trunc_len, cpos, phys_cpos, alloc_size;
811         u64 block;
812
813         if (old_clusters <= new_clusters)
814                 return 0;
815
816         cpos = new_clusters;
817         trunc_len = old_clusters - new_clusters;
818         while (trunc_len) {
819                 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
820                                                &alloc_size,
821                                                &vb->vb_xv->xr_list, &ext_flags);
822                 if (ret) {
823                         mlog_errno(ret);
824                         goto out;
825                 }
826
827                 if (alloc_size > trunc_len)
828                         alloc_size = trunc_len;
829
830                 ret = __ocfs2_remove_xattr_range(inode, vb, cpos,
831                                                  phys_cpos, alloc_size,
832                                                  ext_flags, ctxt);
833                 if (ret) {
834                         mlog_errno(ret);
835                         goto out;
836                 }
837
838                 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
839                 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode),
840                                                        block, alloc_size);
841                 cpos += alloc_size;
842                 trunc_len -= alloc_size;
843         }
844
845 out:
846         return ret;
847 }
848
849 static int ocfs2_xattr_value_truncate(struct inode *inode,
850                                       struct ocfs2_xattr_value_buf *vb,
851                                       int len,
852                                       struct ocfs2_xattr_set_ctxt *ctxt)
853 {
854         int ret;
855         u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
856         u32 old_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
857
858         if (new_clusters == old_clusters)
859                 return 0;
860
861         if (new_clusters > old_clusters)
862                 ret = ocfs2_xattr_extend_allocation(inode,
863                                                     new_clusters - old_clusters,
864                                                     vb, ctxt);
865         else
866                 ret = ocfs2_xattr_shrink_size(inode,
867                                               old_clusters, new_clusters,
868                                               vb, ctxt);
869
870         return ret;
871 }
872
873 static int ocfs2_xattr_list_entry(char *buffer, size_t size,
874                                   size_t *result, const char *prefix,
875                                   const char *name, int name_len)
876 {
877         char *p = buffer + *result;
878         int prefix_len = strlen(prefix);
879         int total_len = prefix_len + name_len + 1;
880
881         *result += total_len;
882
883         /* we are just looking for how big our buffer needs to be */
884         if (!size)
885                 return 0;
886
887         if (*result > size)
888                 return -ERANGE;
889
890         memcpy(p, prefix, prefix_len);
891         memcpy(p + prefix_len, name, name_len);
892         p[prefix_len + name_len] = '\0';
893
894         return 0;
895 }
896
897 static int ocfs2_xattr_list_entries(struct inode *inode,
898                                     struct ocfs2_xattr_header *header,
899                                     char *buffer, size_t buffer_size)
900 {
901         size_t result = 0;
902         int i, type, ret;
903         const char *prefix, *name;
904
905         for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
906                 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
907                 type = ocfs2_xattr_get_type(entry);
908                 prefix = ocfs2_xattr_prefix(type);
909
910                 if (prefix) {
911                         name = (const char *)header +
912                                 le16_to_cpu(entry->xe_name_offset);
913
914                         ret = ocfs2_xattr_list_entry(buffer, buffer_size,
915                                                      &result, prefix, name,
916                                                      entry->xe_name_len);
917                         if (ret)
918                                 return ret;
919                 }
920         }
921
922         return result;
923 }
924
925 int ocfs2_has_inline_xattr_value_outside(struct inode *inode,
926                                          struct ocfs2_dinode *di)
927 {
928         struct ocfs2_xattr_header *xh;
929         int i;
930
931         xh = (struct ocfs2_xattr_header *)
932                  ((void *)di + inode->i_sb->s_blocksize -
933                  le16_to_cpu(di->i_xattr_inline_size));
934
935         for (i = 0; i < le16_to_cpu(xh->xh_count); i++)
936                 if (!ocfs2_xattr_is_local(&xh->xh_entries[i]))
937                         return 1;
938
939         return 0;
940 }
941
942 static int ocfs2_xattr_ibody_list(struct inode *inode,
943                                   struct ocfs2_dinode *di,
944                                   char *buffer,
945                                   size_t buffer_size)
946 {
947         struct ocfs2_xattr_header *header = NULL;
948         struct ocfs2_inode_info *oi = OCFS2_I(inode);
949         int ret = 0;
950
951         if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
952                 return ret;
953
954         header = (struct ocfs2_xattr_header *)
955                  ((void *)di + inode->i_sb->s_blocksize -
956                  le16_to_cpu(di->i_xattr_inline_size));
957
958         ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
959
960         return ret;
961 }
962
963 static int ocfs2_xattr_block_list(struct inode *inode,
964                                   struct ocfs2_dinode *di,
965                                   char *buffer,
966                                   size_t buffer_size)
967 {
968         struct buffer_head *blk_bh = NULL;
969         struct ocfs2_xattr_block *xb;
970         int ret = 0;
971
972         if (!di->i_xattr_loc)
973                 return ret;
974
975         ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
976                                      &blk_bh);
977         if (ret < 0) {
978                 mlog_errno(ret);
979                 return ret;
980         }
981
982         xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
983         if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
984                 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
985                 ret = ocfs2_xattr_list_entries(inode, header,
986                                                buffer, buffer_size);
987         } else
988                 ret = ocfs2_xattr_tree_list_index_block(inode, blk_bh,
989                                                    buffer, buffer_size);
990
991         brelse(blk_bh);
992
993         return ret;
994 }
995
996 ssize_t ocfs2_listxattr(struct dentry *dentry,
997                         char *buffer,
998                         size_t size)
999 {
1000         int ret = 0, i_ret = 0, b_ret = 0;
1001         struct buffer_head *di_bh = NULL;
1002         struct ocfs2_dinode *di = NULL;
1003         struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
1004
1005         if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
1006                 return -EOPNOTSUPP;
1007
1008         if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1009                 return ret;
1010
1011         ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
1012         if (ret < 0) {
1013                 mlog_errno(ret);
1014                 return ret;
1015         }
1016
1017         di = (struct ocfs2_dinode *)di_bh->b_data;
1018
1019         down_read(&oi->ip_xattr_sem);
1020         i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
1021         if (i_ret < 0)
1022                 b_ret = 0;
1023         else {
1024                 if (buffer) {
1025                         buffer += i_ret;
1026                         size -= i_ret;
1027                 }
1028                 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
1029                                                buffer, size);
1030                 if (b_ret < 0)
1031                         i_ret = 0;
1032         }
1033         up_read(&oi->ip_xattr_sem);
1034         ocfs2_inode_unlock(dentry->d_inode, 0);
1035
1036         brelse(di_bh);
1037
1038         return i_ret + b_ret;
1039 }
1040
1041 static int ocfs2_xattr_find_entry(int name_index,
1042                                   const char *name,
1043                                   struct ocfs2_xattr_search *xs)
1044 {
1045         struct ocfs2_xattr_entry *entry;
1046         size_t name_len;
1047         int i, cmp = 1;
1048
1049         if (name == NULL)
1050                 return -EINVAL;
1051
1052         name_len = strlen(name);
1053         entry = xs->here;
1054         for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
1055                 cmp = name_index - ocfs2_xattr_get_type(entry);
1056                 if (!cmp)
1057                         cmp = name_len - entry->xe_name_len;
1058                 if (!cmp)
1059                         cmp = memcmp(name, (xs->base +
1060                                      le16_to_cpu(entry->xe_name_offset)),
1061                                      name_len);
1062                 if (cmp == 0)
1063                         break;
1064                 entry += 1;
1065         }
1066         xs->here = entry;
1067
1068         return cmp ? -ENODATA : 0;
1069 }
1070
1071 static int ocfs2_xattr_get_value_outside(struct inode *inode,
1072                                          struct ocfs2_xattr_value_root *xv,
1073                                          void *buffer,
1074                                          size_t len)
1075 {
1076         u32 cpos, p_cluster, num_clusters, bpc, clusters;
1077         u64 blkno;
1078         int i, ret = 0;
1079         size_t cplen, blocksize;
1080         struct buffer_head *bh = NULL;
1081         struct ocfs2_extent_list *el;
1082
1083         el = &xv->xr_list;
1084         clusters = le32_to_cpu(xv->xr_clusters);
1085         bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1086         blocksize = inode->i_sb->s_blocksize;
1087
1088         cpos = 0;
1089         while (cpos < clusters) {
1090                 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1091                                                &num_clusters, el, NULL);
1092                 if (ret) {
1093                         mlog_errno(ret);
1094                         goto out;
1095                 }
1096
1097                 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1098                 /* Copy ocfs2_xattr_value */
1099                 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1100                         ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1101                                                &bh, NULL);
1102                         if (ret) {
1103                                 mlog_errno(ret);
1104                                 goto out;
1105                         }
1106
1107                         cplen = len >= blocksize ? blocksize : len;
1108                         memcpy(buffer, bh->b_data, cplen);
1109                         len -= cplen;
1110                         buffer += cplen;
1111
1112                         brelse(bh);
1113                         bh = NULL;
1114                         if (len == 0)
1115                                 break;
1116                 }
1117                 cpos += num_clusters;
1118         }
1119 out:
1120         return ret;
1121 }
1122
1123 static int ocfs2_xattr_ibody_get(struct inode *inode,
1124                                  int name_index,
1125                                  const char *name,
1126                                  void *buffer,
1127                                  size_t buffer_size,
1128                                  struct ocfs2_xattr_search *xs)
1129 {
1130         struct ocfs2_inode_info *oi = OCFS2_I(inode);
1131         struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1132         struct ocfs2_xattr_value_root *xv;
1133         size_t size;
1134         int ret = 0;
1135
1136         if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
1137                 return -ENODATA;
1138
1139         xs->end = (void *)di + inode->i_sb->s_blocksize;
1140         xs->header = (struct ocfs2_xattr_header *)
1141                         (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1142         xs->base = (void *)xs->header;
1143         xs->here = xs->header->xh_entries;
1144
1145         ret = ocfs2_xattr_find_entry(name_index, name, xs);
1146         if (ret)
1147                 return ret;
1148         size = le64_to_cpu(xs->here->xe_value_size);
1149         if (buffer) {
1150                 if (size > buffer_size)
1151                         return -ERANGE;
1152                 if (ocfs2_xattr_is_local(xs->here)) {
1153                         memcpy(buffer, (void *)xs->base +
1154                                le16_to_cpu(xs->here->xe_name_offset) +
1155                                OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
1156                 } else {
1157                         xv = (struct ocfs2_xattr_value_root *)
1158                                 (xs->base + le16_to_cpu(
1159                                  xs->here->xe_name_offset) +
1160                                 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
1161                         ret = ocfs2_xattr_get_value_outside(inode, xv,
1162                                                             buffer, size);
1163                         if (ret < 0) {
1164                                 mlog_errno(ret);
1165                                 return ret;
1166                         }
1167                 }
1168         }
1169
1170         return size;
1171 }
1172
1173 static int ocfs2_xattr_block_get(struct inode *inode,
1174                                  int name_index,
1175                                  const char *name,
1176                                  void *buffer,
1177                                  size_t buffer_size,
1178                                  struct ocfs2_xattr_search *xs)
1179 {
1180         struct ocfs2_xattr_block *xb;
1181         struct ocfs2_xattr_value_root *xv;
1182         size_t size;
1183         int ret = -ENODATA, name_offset, name_len, i;
1184         int uninitialized_var(block_off);
1185
1186         xs->bucket = ocfs2_xattr_bucket_new(inode);
1187         if (!xs->bucket) {
1188                 ret = -ENOMEM;
1189                 mlog_errno(ret);
1190                 goto cleanup;
1191         }
1192
1193         ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
1194         if (ret) {
1195                 mlog_errno(ret);
1196                 goto cleanup;
1197         }
1198
1199         if (xs->not_found) {
1200                 ret = -ENODATA;
1201                 goto cleanup;
1202         }
1203
1204         xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1205         size = le64_to_cpu(xs->here->xe_value_size);
1206         if (buffer) {
1207                 ret = -ERANGE;
1208                 if (size > buffer_size)
1209                         goto cleanup;
1210
1211                 name_offset = le16_to_cpu(xs->here->xe_name_offset);
1212                 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
1213                 i = xs->here - xs->header->xh_entries;
1214
1215                 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
1216                         ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
1217                                                                 bucket_xh(xs->bucket),
1218                                                                 i,
1219                                                                 &block_off,
1220                                                                 &name_offset);
1221                         xs->base = bucket_block(xs->bucket, block_off);
1222                 }
1223                 if (ocfs2_xattr_is_local(xs->here)) {
1224                         memcpy(buffer, (void *)xs->base +
1225                                name_offset + name_len, size);
1226                 } else {
1227                         xv = (struct ocfs2_xattr_value_root *)
1228                                 (xs->base + name_offset + name_len);
1229                         ret = ocfs2_xattr_get_value_outside(inode, xv,
1230                                                             buffer, size);
1231                         if (ret < 0) {
1232                                 mlog_errno(ret);
1233                                 goto cleanup;
1234                         }
1235                 }
1236         }
1237         ret = size;
1238 cleanup:
1239         ocfs2_xattr_bucket_free(xs->bucket);
1240
1241         brelse(xs->xattr_bh);
1242         xs->xattr_bh = NULL;
1243         return ret;
1244 }
1245
1246 int ocfs2_xattr_get_nolock(struct inode *inode,
1247                            struct buffer_head *di_bh,
1248                            int name_index,
1249                            const char *name,
1250                            void *buffer,
1251                            size_t buffer_size)
1252 {
1253         int ret;
1254         struct ocfs2_dinode *di = NULL;
1255         struct ocfs2_inode_info *oi = OCFS2_I(inode);
1256         struct ocfs2_xattr_search xis = {
1257                 .not_found = -ENODATA,
1258         };
1259         struct ocfs2_xattr_search xbs = {
1260                 .not_found = -ENODATA,
1261         };
1262
1263         if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1264                 return -EOPNOTSUPP;
1265
1266         if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1267                 ret = -ENODATA;
1268
1269         xis.inode_bh = xbs.inode_bh = di_bh;
1270         di = (struct ocfs2_dinode *)di_bh->b_data;
1271
1272         down_read(&oi->ip_xattr_sem);
1273         ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1274                                     buffer_size, &xis);
1275         if (ret == -ENODATA && di->i_xattr_loc)
1276                 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1277                                             buffer_size, &xbs);
1278         up_read(&oi->ip_xattr_sem);
1279
1280         return ret;
1281 }
1282
1283 /* ocfs2_xattr_get()
1284  *
1285  * Copy an extended attribute into the buffer provided.
1286  * Buffer is NULL to compute the size of buffer required.
1287  */
1288 static int ocfs2_xattr_get(struct inode *inode,
1289                            int name_index,
1290                            const char *name,
1291                            void *buffer,
1292                            size_t buffer_size)
1293 {
1294         int ret;
1295         struct buffer_head *di_bh = NULL;
1296
1297         ret = ocfs2_inode_lock(inode, &di_bh, 0);
1298         if (ret < 0) {
1299                 mlog_errno(ret);
1300                 return ret;
1301         }
1302         ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
1303                                      name, buffer, buffer_size);
1304
1305         ocfs2_inode_unlock(inode, 0);
1306
1307         brelse(di_bh);
1308
1309         return ret;
1310 }
1311
1312 static int __ocfs2_xattr_set_value_outside(struct inode *inode,
1313                                            handle_t *handle,
1314                                            struct ocfs2_xattr_value_buf *vb,
1315                                            const void *value,
1316                                            int value_len)
1317 {
1318         int ret = 0, i, cp_len;
1319         u16 blocksize = inode->i_sb->s_blocksize;
1320         u32 p_cluster, num_clusters;
1321         u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1322         u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1323         u64 blkno;
1324         struct buffer_head *bh = NULL;
1325         unsigned int ext_flags;
1326         struct ocfs2_xattr_value_root *xv = vb->vb_xv;
1327
1328         BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1329
1330         while (cpos < clusters) {
1331                 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1332                                                &num_clusters, &xv->xr_list,
1333                                                &ext_flags);
1334                 if (ret) {
1335                         mlog_errno(ret);
1336                         goto out;
1337                 }
1338
1339                 BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED);
1340
1341                 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1342
1343                 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1344                         ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1345                                                &bh, NULL);
1346                         if (ret) {
1347                                 mlog_errno(ret);
1348                                 goto out;
1349                         }
1350
1351                         ret = ocfs2_journal_access(handle,
1352                                                    INODE_CACHE(inode),
1353                                                    bh,
1354                                                    OCFS2_JOURNAL_ACCESS_WRITE);
1355                         if (ret < 0) {
1356                                 mlog_errno(ret);
1357                                 goto out;
1358                         }
1359
1360                         cp_len = value_len > blocksize ? blocksize : value_len;
1361                         memcpy(bh->b_data, value, cp_len);
1362                         value_len -= cp_len;
1363                         value += cp_len;
1364                         if (cp_len < blocksize)
1365                                 memset(bh->b_data + cp_len, 0,
1366                                        blocksize - cp_len);
1367
1368                         ocfs2_journal_dirty(handle, bh);
1369                         brelse(bh);
1370                         bh = NULL;
1371
1372                         /*
1373                          * XXX: do we need to empty all the following
1374                          * blocks in this cluster?
1375                          */
1376                         if (!value_len)
1377                                 break;
1378                 }
1379                 cpos += num_clusters;
1380         }
1381 out:
1382         brelse(bh);
1383
1384         return ret;
1385 }
1386
1387 static int ocfs2_xa_check_space_helper(int needed_space, int free_start,
1388                                        int num_entries)
1389 {
1390         int free_space;
1391
1392         if (!needed_space)
1393                 return 0;
1394
1395         free_space = free_start -
1396                 sizeof(struct ocfs2_xattr_header) -
1397                 (num_entries * sizeof(struct ocfs2_xattr_entry)) -
1398                 OCFS2_XATTR_HEADER_GAP;
1399         if (free_space < 0)
1400                 return -EIO;
1401         if (free_space < needed_space)
1402                 return -ENOSPC;
1403
1404         return 0;
1405 }
1406
1407 static int ocfs2_xa_journal_access(handle_t *handle, struct ocfs2_xa_loc *loc,
1408                                    int type)
1409 {
1410         return loc->xl_ops->xlo_journal_access(handle, loc, type);
1411 }
1412
1413 static void ocfs2_xa_journal_dirty(handle_t *handle, struct ocfs2_xa_loc *loc)
1414 {
1415         loc->xl_ops->xlo_journal_dirty(handle, loc);
1416 }
1417
1418 /* Give a pointer into the storage for the given offset */
1419 static void *ocfs2_xa_offset_pointer(struct ocfs2_xa_loc *loc, int offset)
1420 {
1421         BUG_ON(offset >= loc->xl_size);
1422         return loc->xl_ops->xlo_offset_pointer(loc, offset);
1423 }
1424
1425 /*
1426  * Wipe the name+value pair and allow the storage to reclaim it.  This
1427  * must be followed by either removal of the entry or a call to
1428  * ocfs2_xa_add_namevalue().
1429  */
1430 static void ocfs2_xa_wipe_namevalue(struct ocfs2_xa_loc *loc)
1431 {
1432         loc->xl_ops->xlo_wipe_namevalue(loc);
1433 }
1434
1435 /*
1436  * Find lowest offset to a name+value pair.  This is the start of our
1437  * downward-growing free space.
1438  */
1439 static int ocfs2_xa_get_free_start(struct ocfs2_xa_loc *loc)
1440 {
1441         return loc->xl_ops->xlo_get_free_start(loc);
1442 }
1443
1444 /* Can we reuse loc->xl_entry for xi? */
1445 static int ocfs2_xa_can_reuse_entry(struct ocfs2_xa_loc *loc,
1446                                     struct ocfs2_xattr_info *xi)
1447 {
1448         return loc->xl_ops->xlo_can_reuse(loc, xi);
1449 }
1450
1451 /* How much free space is needed to set the new value */
1452 static int ocfs2_xa_check_space(struct ocfs2_xa_loc *loc,
1453                                 struct ocfs2_xattr_info *xi)
1454 {
1455         return loc->xl_ops->xlo_check_space(loc, xi);
1456 }
1457
1458 static void ocfs2_xa_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1459 {
1460         loc->xl_ops->xlo_add_entry(loc, name_hash);
1461         loc->xl_entry->xe_name_hash = cpu_to_le32(name_hash);
1462         /*
1463          * We can't leave the new entry's xe_name_offset at zero or
1464          * add_namevalue() will go nuts.  We set it to the size of our
1465          * storage so that it can never be less than any other entry.
1466          */
1467         loc->xl_entry->xe_name_offset = cpu_to_le16(loc->xl_size);
1468 }
1469
1470 static void ocfs2_xa_add_namevalue(struct ocfs2_xa_loc *loc,
1471                                    struct ocfs2_xattr_info *xi)
1472 {
1473         int size = namevalue_size_xi(xi);
1474         int nameval_offset;
1475         char *nameval_buf;
1476
1477         loc->xl_ops->xlo_add_namevalue(loc, size);
1478         loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
1479         loc->xl_entry->xe_name_len = xi->xi_name_len;
1480         ocfs2_xattr_set_type(loc->xl_entry, xi->xi_name_index);
1481         ocfs2_xattr_set_local(loc->xl_entry,
1482                               xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE);
1483
1484         nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1485         nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
1486         memset(nameval_buf, 0, size);
1487         memcpy(nameval_buf, xi->xi_name, xi->xi_name_len);
1488 }
1489
1490 static void ocfs2_xa_fill_value_buf(struct ocfs2_xa_loc *loc,
1491                                     struct ocfs2_xattr_value_buf *vb)
1492 {
1493         int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1494         int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len);
1495
1496         /* Value bufs are for value trees */
1497         BUG_ON(ocfs2_xattr_is_local(loc->xl_entry));
1498         BUG_ON(namevalue_size_xe(loc->xl_entry) !=
1499                (name_size + OCFS2_XATTR_ROOT_SIZE));
1500
1501         loc->xl_ops->xlo_fill_value_buf(loc, vb);
1502         vb->vb_xv =
1503                 (struct ocfs2_xattr_value_root *)ocfs2_xa_offset_pointer(loc,
1504                                                         nameval_offset +
1505                                                         name_size);
1506 }
1507
1508 static int ocfs2_xa_block_journal_access(handle_t *handle,
1509                                          struct ocfs2_xa_loc *loc, int type)
1510 {
1511         struct buffer_head *bh = loc->xl_storage;
1512         ocfs2_journal_access_func access;
1513
1514         if (loc->xl_size == (bh->b_size -
1515                              offsetof(struct ocfs2_xattr_block,
1516                                       xb_attrs.xb_header)))
1517                 access = ocfs2_journal_access_xb;
1518         else
1519                 access = ocfs2_journal_access_di;
1520         return access(handle, INODE_CACHE(loc->xl_inode), bh, type);
1521 }
1522
1523 static void ocfs2_xa_block_journal_dirty(handle_t *handle,
1524                                          struct ocfs2_xa_loc *loc)
1525 {
1526         struct buffer_head *bh = loc->xl_storage;
1527
1528         ocfs2_journal_dirty(handle, bh);
1529 }
1530
1531 static void *ocfs2_xa_block_offset_pointer(struct ocfs2_xa_loc *loc,
1532                                            int offset)
1533 {
1534         return (char *)loc->xl_header + offset;
1535 }
1536
1537 static int ocfs2_xa_block_can_reuse(struct ocfs2_xa_loc *loc,
1538                                     struct ocfs2_xattr_info *xi)
1539 {
1540         /*
1541          * Block storage is strict.  If the sizes aren't exact, we will
1542          * remove the old one and reinsert the new.
1543          */
1544         return namevalue_size_xe(loc->xl_entry) ==
1545                 namevalue_size_xi(xi);
1546 }
1547
1548 static int ocfs2_xa_block_get_free_start(struct ocfs2_xa_loc *loc)
1549 {
1550         struct ocfs2_xattr_header *xh = loc->xl_header;
1551         int i, count = le16_to_cpu(xh->xh_count);
1552         int offset, free_start = loc->xl_size;
1553
1554         for (i = 0; i < count; i++) {
1555                 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1556                 if (offset < free_start)
1557                         free_start = offset;
1558         }
1559
1560         return free_start;
1561 }
1562
1563 static int ocfs2_xa_block_check_space(struct ocfs2_xa_loc *loc,
1564                                       struct ocfs2_xattr_info *xi)
1565 {
1566         int count = le16_to_cpu(loc->xl_header->xh_count);
1567         int free_start = ocfs2_xa_get_free_start(loc);
1568         int needed_space = ocfs2_xi_entry_usage(xi);
1569
1570         /*
1571          * Block storage will reclaim the original entry before inserting
1572          * the new value, so we only need the difference.  If the new
1573          * entry is smaller than the old one, we don't need anything.
1574          */
1575         if (loc->xl_entry) {
1576                 /* Don't need space if we're reusing! */
1577                 if (ocfs2_xa_can_reuse_entry(loc, xi))
1578                         needed_space = 0;
1579                 else
1580                         needed_space -= ocfs2_xe_entry_usage(loc->xl_entry);
1581         }
1582         if (needed_space < 0)
1583                 needed_space = 0;
1584         return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1585 }
1586
1587 /*
1588  * Block storage for xattrs keeps the name+value pairs compacted.  When
1589  * we remove one, we have to shift any that preceded it towards the end.
1590  */
1591 static void ocfs2_xa_block_wipe_namevalue(struct ocfs2_xa_loc *loc)
1592 {
1593         int i, offset;
1594         int namevalue_offset, first_namevalue_offset, namevalue_size;
1595         struct ocfs2_xattr_entry *entry = loc->xl_entry;
1596         struct ocfs2_xattr_header *xh = loc->xl_header;
1597         int count = le16_to_cpu(xh->xh_count);
1598
1599         namevalue_offset = le16_to_cpu(entry->xe_name_offset);
1600         namevalue_size = namevalue_size_xe(entry);
1601         first_namevalue_offset = ocfs2_xa_get_free_start(loc);
1602
1603         /* Shift the name+value pairs */
1604         memmove((char *)xh + first_namevalue_offset + namevalue_size,
1605                 (char *)xh + first_namevalue_offset,
1606                 namevalue_offset - first_namevalue_offset);
1607         memset((char *)xh + first_namevalue_offset, 0, namevalue_size);
1608
1609         /* Now tell xh->xh_entries about it */
1610         for (i = 0; i < count; i++) {
1611                 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1612                 if (offset <= namevalue_offset)
1613                         le16_add_cpu(&xh->xh_entries[i].xe_name_offset,
1614                                      namevalue_size);
1615         }
1616
1617         /*
1618          * Note that we don't update xh_free_start or xh_name_value_len
1619          * because they're not used in block-stored xattrs.
1620          */
1621 }
1622
1623 static void ocfs2_xa_block_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1624 {
1625         int count = le16_to_cpu(loc->xl_header->xh_count);
1626         loc->xl_entry = &(loc->xl_header->xh_entries[count]);
1627         le16_add_cpu(&loc->xl_header->xh_count, 1);
1628         memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1629 }
1630
1631 static void ocfs2_xa_block_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1632 {
1633         int free_start = ocfs2_xa_get_free_start(loc);
1634
1635         loc->xl_entry->xe_name_offset = cpu_to_le16(free_start - size);
1636 }
1637
1638 static void ocfs2_xa_block_fill_value_buf(struct ocfs2_xa_loc *loc,
1639                                           struct ocfs2_xattr_value_buf *vb)
1640 {
1641         struct buffer_head *bh = loc->xl_storage;
1642
1643         if (loc->xl_size == (bh->b_size -
1644                              offsetof(struct ocfs2_xattr_block,
1645                                       xb_attrs.xb_header)))
1646                 vb->vb_access = ocfs2_journal_access_xb;
1647         else
1648                 vb->vb_access = ocfs2_journal_access_di;
1649         vb->vb_bh = bh;
1650 }
1651
1652 /*
1653  * Operations for xattrs stored in blocks.  This includes inline inode
1654  * storage and unindexed ocfs2_xattr_blocks.
1655  */
1656 static const struct ocfs2_xa_loc_operations ocfs2_xa_block_loc_ops = {
1657         .xlo_journal_access     = ocfs2_xa_block_journal_access,
1658         .xlo_journal_dirty      = ocfs2_xa_block_journal_dirty,
1659         .xlo_offset_pointer     = ocfs2_xa_block_offset_pointer,
1660         .xlo_check_space        = ocfs2_xa_block_check_space,
1661         .xlo_can_reuse          = ocfs2_xa_block_can_reuse,
1662         .xlo_get_free_start     = ocfs2_xa_block_get_free_start,
1663         .xlo_wipe_namevalue     = ocfs2_xa_block_wipe_namevalue,
1664         .xlo_add_entry          = ocfs2_xa_block_add_entry,
1665         .xlo_add_namevalue      = ocfs2_xa_block_add_namevalue,
1666         .xlo_fill_value_buf     = ocfs2_xa_block_fill_value_buf,
1667 };
1668
1669 static int ocfs2_xa_bucket_journal_access(handle_t *handle,
1670                                           struct ocfs2_xa_loc *loc, int type)
1671 {
1672         struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1673
1674         return ocfs2_xattr_bucket_journal_access(handle, bucket, type);
1675 }
1676
1677 static void ocfs2_xa_bucket_journal_dirty(handle_t *handle,
1678                                           struct ocfs2_xa_loc *loc)
1679 {
1680         struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1681
1682         ocfs2_xattr_bucket_journal_dirty(handle, bucket);
1683 }
1684
1685 static void *ocfs2_xa_bucket_offset_pointer(struct ocfs2_xa_loc *loc,
1686                                             int offset)
1687 {
1688         struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1689         int block, block_offset;
1690
1691         /* The header is at the front of the bucket */
1692         block = offset >> loc->xl_inode->i_sb->s_blocksize_bits;
1693         block_offset = offset % loc->xl_inode->i_sb->s_blocksize;
1694
1695         return bucket_block(bucket, block) + block_offset;
1696 }
1697
1698 static int ocfs2_xa_bucket_can_reuse(struct ocfs2_xa_loc *loc,
1699                                      struct ocfs2_xattr_info *xi)
1700 {
1701         return namevalue_size_xe(loc->xl_entry) >=
1702                 namevalue_size_xi(xi);
1703 }
1704
1705 static int ocfs2_xa_bucket_get_free_start(struct ocfs2_xa_loc *loc)
1706 {
1707         struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1708         return le16_to_cpu(bucket_xh(bucket)->xh_free_start);
1709 }
1710
1711 static int ocfs2_bucket_align_free_start(struct super_block *sb,
1712                                          int free_start, int size)
1713 {
1714         /*
1715          * We need to make sure that the name+value pair fits within
1716          * one block.
1717          */
1718         if (((free_start - size) >> sb->s_blocksize_bits) !=
1719             ((free_start - 1) >> sb->s_blocksize_bits))
1720                 free_start -= free_start % sb->s_blocksize;
1721
1722         return free_start;
1723 }
1724
1725 static int ocfs2_xa_bucket_check_space(struct ocfs2_xa_loc *loc,
1726                                        struct ocfs2_xattr_info *xi)
1727 {
1728         int rc;
1729         int count = le16_to_cpu(loc->xl_header->xh_count);
1730         int free_start = ocfs2_xa_get_free_start(loc);
1731         int needed_space = ocfs2_xi_entry_usage(xi);
1732         int size = namevalue_size_xi(xi);
1733         struct super_block *sb = loc->xl_inode->i_sb;
1734
1735         /*
1736          * Bucket storage does not reclaim name+value pairs it cannot
1737          * reuse.  They live as holes until the bucket fills, and then
1738          * the bucket is defragmented.  However, the bucket can reclaim
1739          * the ocfs2_xattr_entry.
1740          */
1741         if (loc->xl_entry) {
1742                 /* Don't need space if we're reusing! */
1743                 if (ocfs2_xa_can_reuse_entry(loc, xi))
1744                         needed_space = 0;
1745                 else
1746                         needed_space -= sizeof(struct ocfs2_xattr_entry);
1747         }
1748         BUG_ON(needed_space < 0);
1749
1750         if (free_start < size) {
1751                 if (needed_space)
1752                         return -ENOSPC;
1753         } else {
1754                 /*
1755                  * First we check if it would fit in the first place.
1756                  * Below, we align the free start to a block.  This may
1757                  * slide us below the minimum gap.  By checking unaligned
1758                  * first, we avoid that error.
1759                  */
1760                 rc = ocfs2_xa_check_space_helper(needed_space, free_start,
1761                                                  count);
1762                 if (rc)
1763                         return rc;
1764                 free_start = ocfs2_bucket_align_free_start(sb, free_start,
1765                                                            size);
1766         }
1767         return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1768 }
1769
1770 static void ocfs2_xa_bucket_wipe_namevalue(struct ocfs2_xa_loc *loc)
1771 {
1772         le16_add_cpu(&loc->xl_header->xh_name_value_len,
1773                      -namevalue_size_xe(loc->xl_entry));
1774 }
1775
1776 static void ocfs2_xa_bucket_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1777 {
1778         struct ocfs2_xattr_header *xh = loc->xl_header;
1779         int count = le16_to_cpu(xh->xh_count);
1780         int low = 0, high = count - 1, tmp;
1781         struct ocfs2_xattr_entry *tmp_xe;
1782
1783         /*
1784          * We keep buckets sorted by name_hash, so we need to find
1785          * our insert place.
1786          */
1787         while (low <= high && count) {
1788                 tmp = (low + high) / 2;
1789                 tmp_xe = &xh->xh_entries[tmp];
1790
1791                 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
1792                         low = tmp + 1;
1793                 else if (name_hash < le32_to_cpu(tmp_xe->xe_name_hash))
1794                         high = tmp - 1;
1795                 else {
1796                         low = tmp;
1797                         break;
1798                 }
1799         }
1800
1801         if (low != count)
1802                 memmove(&xh->xh_entries[low + 1],
1803                         &xh->xh_entries[low],
1804                         ((count - low) * sizeof(struct ocfs2_xattr_entry)));
1805
1806         le16_add_cpu(&xh->xh_count, 1);
1807         loc->xl_entry = &xh->xh_entries[low];
1808         memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1809 }
1810
1811 static void ocfs2_xa_bucket_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1812 {
1813         int free_start = ocfs2_xa_get_free_start(loc);
1814         struct ocfs2_xattr_header *xh = loc->xl_header;
1815         struct super_block *sb = loc->xl_inode->i_sb;
1816         int nameval_offset;
1817
1818         free_start = ocfs2_bucket_align_free_start(sb, free_start, size);
1819         nameval_offset = free_start - size;
1820         loc->xl_entry->xe_name_offset = cpu_to_le16(nameval_offset);
1821         xh->xh_free_start = cpu_to_le16(nameval_offset);
1822         le16_add_cpu(&xh->xh_name_value_len, size);
1823
1824 }
1825
1826 static void ocfs2_xa_bucket_fill_value_buf(struct ocfs2_xa_loc *loc,
1827                                            struct ocfs2_xattr_value_buf *vb)
1828 {
1829         struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1830         struct super_block *sb = loc->xl_inode->i_sb;
1831         int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1832         int size = namevalue_size_xe(loc->xl_entry);
1833         int block_offset = nameval_offset >> sb->s_blocksize_bits;
1834
1835         /* Values are not allowed to straddle block boundaries */
1836         BUG_ON(block_offset !=
1837                ((nameval_offset + size - 1) >> sb->s_blocksize_bits));
1838         /* We expect the bucket to be filled in */
1839         BUG_ON(!bucket->bu_bhs[block_offset]);
1840
1841         vb->vb_access = ocfs2_journal_access;
1842         vb->vb_bh = bucket->bu_bhs[block_offset];
1843 }
1844
1845 /* Operations for xattrs stored in buckets. */
1846 static const struct ocfs2_xa_loc_operations ocfs2_xa_bucket_loc_ops = {
1847         .xlo_journal_access     = ocfs2_xa_bucket_journal_access,
1848         .xlo_journal_dirty      = ocfs2_xa_bucket_journal_dirty,
1849         .xlo_offset_pointer     = ocfs2_xa_bucket_offset_pointer,
1850         .xlo_check_space        = ocfs2_xa_bucket_check_space,
1851         .xlo_can_reuse          = ocfs2_xa_bucket_can_reuse,
1852         .xlo_get_free_start     = ocfs2_xa_bucket_get_free_start,
1853         .xlo_wipe_namevalue     = ocfs2_xa_bucket_wipe_namevalue,
1854         .xlo_add_entry          = ocfs2_xa_bucket_add_entry,
1855         .xlo_add_namevalue      = ocfs2_xa_bucket_add_namevalue,
1856         .xlo_fill_value_buf     = ocfs2_xa_bucket_fill_value_buf,
1857 };
1858
1859 static unsigned int ocfs2_xa_value_clusters(struct ocfs2_xa_loc *loc)
1860 {
1861         struct ocfs2_xattr_value_buf vb;
1862
1863         if (ocfs2_xattr_is_local(loc->xl_entry))
1864                 return 0;
1865
1866         ocfs2_xa_fill_value_buf(loc, &vb);
1867         return le32_to_cpu(vb.vb_xv->xr_clusters);
1868 }
1869
1870 static int ocfs2_xa_value_truncate(struct ocfs2_xa_loc *loc, u64 bytes,
1871                                    struct ocfs2_xattr_set_ctxt *ctxt)
1872 {
1873         int trunc_rc, access_rc;
1874         struct ocfs2_xattr_value_buf vb;
1875
1876         ocfs2_xa_fill_value_buf(loc, &vb);
1877         trunc_rc = ocfs2_xattr_value_truncate(loc->xl_inode, &vb, bytes,
1878                                               ctxt);
1879
1880         /*
1881          * The caller of ocfs2_xa_value_truncate() has already called
1882          * ocfs2_xa_journal_access on the loc.  However, The truncate code
1883          * calls ocfs2_extend_trans().  This may commit the previous
1884          * transaction and open a new one.  If this is a bucket, truncate
1885          * could leave only vb->vb_bh set up for journaling.  Meanwhile,
1886          * the caller is expecting to dirty the entire bucket.  So we must
1887          * reset the journal work.  We do this even if truncate has failed,
1888          * as it could have failed after committing the extend.
1889          */
1890         access_rc = ocfs2_xa_journal_access(ctxt->handle, loc,
1891                                             OCFS2_JOURNAL_ACCESS_WRITE);
1892
1893         /* Errors in truncate take precedence */
1894         return trunc_rc ? trunc_rc : access_rc;
1895 }
1896
1897 static void ocfs2_xa_remove_entry(struct ocfs2_xa_loc *loc)
1898 {
1899         int index, count;
1900         struct ocfs2_xattr_header *xh = loc->xl_header;
1901         struct ocfs2_xattr_entry *entry = loc->xl_entry;
1902
1903         ocfs2_xa_wipe_namevalue(loc);
1904         loc->xl_entry = NULL;
1905
1906         le16_add_cpu(&xh->xh_count, -1);
1907         count = le16_to_cpu(xh->xh_count);
1908
1909         /*
1910          * Only zero out the entry if there are more remaining.  This is
1911          * important for an empty bucket, as it keeps track of the
1912          * bucket's hash value.  It doesn't hurt empty block storage.
1913          */
1914         if (count) {
1915                 index = ((char *)entry - (char *)&xh->xh_entries) /
1916                         sizeof(struct ocfs2_xattr_entry);
1917                 memmove(&xh->xh_entries[index], &xh->xh_entries[index + 1],
1918                         (count - index) * sizeof(struct ocfs2_xattr_entry));
1919                 memset(&xh->xh_entries[count], 0,
1920                        sizeof(struct ocfs2_xattr_entry));
1921         }
1922 }
1923
1924 /*
1925  * If we have a problem adjusting the size of an external value during
1926  * ocfs2_xa_prepare_entry() or ocfs2_xa_remove(), we may have an xattr
1927  * in an intermediate state.  For example, the value may be partially
1928  * truncated.
1929  *
1930  * If the value tree hasn't changed, the extend/truncate went nowhere.
1931  * We have nothing to do.  The caller can treat it as a straight error.
1932  *
1933  * If the value tree got partially truncated, we now have a corrupted
1934  * extended attribute.  We're going to wipe its entry and leak the
1935  * clusters.  Better to leak some storage than leave a corrupt entry.
1936  *
1937  * If the value tree grew, it obviously didn't grow enough for the
1938  * new entry.  We're not going to try and reclaim those clusters either.
1939  * If there was already an external value there (orig_clusters != 0),
1940  * the new clusters are attached safely and we can just leave the old
1941  * value in place.  If there was no external value there, we remove
1942  * the entry.
1943  *
1944  * This way, the xattr block we store in the journal will be consistent.
1945  * If the size change broke because of the journal, no changes will hit
1946  * disk anyway.
1947  */
1948 static void ocfs2_xa_cleanup_value_truncate(struct ocfs2_xa_loc *loc,
1949                                             const char *what,
1950                                             unsigned int orig_clusters)
1951 {
1952         unsigned int new_clusters = ocfs2_xa_value_clusters(loc);
1953         char *nameval_buf = ocfs2_xa_offset_pointer(loc,
1954                                 le16_to_cpu(loc->xl_entry->xe_name_offset));
1955
1956         if (new_clusters < orig_clusters) {
1957                 mlog(ML_ERROR,
1958                      "Partial truncate while %s xattr %.*s.  Leaking "
1959                      "%u clusters and removing the entry\n",
1960                      what, loc->xl_entry->xe_name_len, nameval_buf,
1961                      orig_clusters - new_clusters);
1962                 ocfs2_xa_remove_entry(loc);
1963         } else if (!orig_clusters) {
1964                 mlog(ML_ERROR,
1965                      "Unable to allocate an external value for xattr "
1966                      "%.*s safely.  Leaking %u clusters and removing the "
1967                      "entry\n",
1968                      loc->xl_entry->xe_name_len, nameval_buf,
1969                      new_clusters - orig_clusters);
1970                 ocfs2_xa_remove_entry(loc);
1971         } else if (new_clusters > orig_clusters)
1972                 mlog(ML_ERROR,
1973                      "Unable to grow xattr %.*s safely.  %u new clusters "
1974                      "have been added, but the value will not be "
1975                      "modified\n",
1976                      loc->xl_entry->xe_name_len, nameval_buf,
1977                      new_clusters - orig_clusters);
1978 }
1979
1980 static int ocfs2_xa_remove(struct ocfs2_xa_loc *loc,
1981                            struct ocfs2_xattr_set_ctxt *ctxt)
1982 {
1983         int rc = 0;
1984         unsigned int orig_clusters;
1985
1986         if (!ocfs2_xattr_is_local(loc->xl_entry)) {
1987                 orig_clusters = ocfs2_xa_value_clusters(loc);
1988                 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
1989                 if (rc) {
1990                         mlog_errno(rc);
1991                         /*
1992                          * Since this is remove, we can return 0 if
1993                          * ocfs2_xa_cleanup_value_truncate() is going to
1994                          * wipe the entry anyway.  So we check the
1995                          * cluster count as well.
1996                          */
1997                         if (orig_clusters != ocfs2_xa_value_clusters(loc))
1998                                 rc = 0;
1999                         ocfs2_xa_cleanup_value_truncate(loc, "removing",
2000                                                         orig_clusters);
2001                         if (rc)
2002                                 goto out;
2003                 }
2004         }
2005
2006         ocfs2_xa_remove_entry(loc);
2007
2008 out:
2009         return rc;
2010 }
2011
2012 static void ocfs2_xa_install_value_root(struct ocfs2_xa_loc *loc)
2013 {
2014         int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len);
2015         char *nameval_buf;
2016
2017         nameval_buf = ocfs2_xa_offset_pointer(loc,
2018                                 le16_to_cpu(loc->xl_entry->xe_name_offset));
2019         memcpy(nameval_buf + name_size, &def_xv, OCFS2_XATTR_ROOT_SIZE);
2020 }
2021
2022 /*
2023  * Take an existing entry and make it ready for the new value.  This
2024  * won't allocate space, but it may free space.  It should be ready for
2025  * ocfs2_xa_prepare_entry() to finish the work.
2026  */
2027 static int ocfs2_xa_reuse_entry(struct ocfs2_xa_loc *loc,
2028                                 struct ocfs2_xattr_info *xi,
2029                                 struct ocfs2_xattr_set_ctxt *ctxt)
2030 {
2031         int rc = 0;
2032         int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
2033         unsigned int orig_clusters;
2034         char *nameval_buf;
2035         int xe_local = ocfs2_xattr_is_local(loc->xl_entry);
2036         int xi_local = xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE;
2037
2038         BUG_ON(OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len) !=
2039                name_size);
2040
2041         nameval_buf = ocfs2_xa_offset_pointer(loc,
2042                                 le16_to_cpu(loc->xl_entry->xe_name_offset));
2043         if (xe_local) {
2044                 memset(nameval_buf + name_size, 0,
2045                        namevalue_size_xe(loc->xl_entry) - name_size);
2046                 if (!xi_local)
2047                         ocfs2_xa_install_value_root(loc);
2048         } else {
2049                 orig_clusters = ocfs2_xa_value_clusters(loc);
2050                 if (xi_local) {
2051                         rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
2052                         if (rc < 0)
2053                                 mlog_errno(rc);
2054                         else
2055                                 memset(nameval_buf + name_size, 0,
2056                                        namevalue_size_xe(loc->xl_entry) -
2057                                        name_size);
2058                 } else if (le64_to_cpu(loc->xl_entry->xe_value_size) >
2059                            xi->xi_value_len) {
2060                         rc = ocfs2_xa_value_truncate(loc, xi->xi_value_len,
2061                                                      ctxt);
2062                         if (rc < 0)
2063                                 mlog_errno(rc);
2064                 }
2065
2066                 if (rc) {
2067                         ocfs2_xa_cleanup_value_truncate(loc, "reusing",
2068                                                         orig_clusters);
2069                         goto out;
2070                 }
2071         }
2072
2073         loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
2074         ocfs2_xattr_set_local(loc->xl_entry, xi_local);
2075
2076 out:
2077         return rc;
2078 }
2079
2080 /*
2081  * Prepares loc->xl_entry to receive the new xattr.  This includes
2082  * properly setting up the name+value pair region.  If loc->xl_entry
2083  * already exists, it will take care of modifying it appropriately.
2084  *
2085  * Note that this modifies the data.  You did journal_access already,
2086  * right?
2087  */
2088 static int ocfs2_xa_prepare_entry(struct ocfs2_xa_loc *loc,
2089                                   struct ocfs2_xattr_info *xi,
2090                                   u32 name_hash,
2091                                   struct ocfs2_xattr_set_ctxt *ctxt)
2092 {
2093         int rc = 0;
2094         unsigned int orig_clusters;
2095         __le64 orig_value_size = 0;
2096
2097         rc = ocfs2_xa_check_space(loc, xi);
2098         if (rc)
2099                 goto out;
2100
2101         if (loc->xl_entry) {
2102                 if (ocfs2_xa_can_reuse_entry(loc, xi)) {
2103                         orig_value_size = loc->xl_entry->xe_value_size;
2104                         rc = ocfs2_xa_reuse_entry(loc, xi, ctxt);
2105                         if (rc)
2106                                 goto out;
2107                         goto alloc_value;
2108                 }
2109
2110                 if (!ocfs2_xattr_is_local(loc->xl_entry)) {
2111                         orig_clusters = ocfs2_xa_value_clusters(loc);
2112                         rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
2113                         if (rc) {
2114                                 mlog_errno(rc);
2115                                 ocfs2_xa_cleanup_value_truncate(loc,
2116                                                                 "overwriting",
2117                                                                 orig_clusters);
2118                                 goto out;
2119                         }
2120                 }
2121                 ocfs2_xa_wipe_namevalue(loc);
2122         } else
2123                 ocfs2_xa_add_entry(loc, name_hash);
2124
2125         /*
2126          * If we get here, we have a blank entry.  Fill it.  We grow our
2127          * name+value pair back from the end.
2128          */
2129         ocfs2_xa_add_namevalue(loc, xi);
2130         if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
2131                 ocfs2_xa_install_value_root(loc);
2132
2133 alloc_value:
2134         if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2135                 orig_clusters = ocfs2_xa_value_clusters(loc);
2136                 rc = ocfs2_xa_value_truncate(loc, xi->xi_value_len, ctxt);
2137                 if (rc < 0) {
2138                         ocfs2_xa_cleanup_value_truncate(loc, "growing",
2139                                                         orig_clusters);
2140                         /*
2141                          * If we were growing an existing value,
2142                          * ocfs2_xa_cleanup_value_truncate() won't remove
2143                          * the entry. We need to restore the original value
2144                          * size.
2145                          */
2146                         if (loc->xl_entry) {
2147                                 BUG_ON(!orig_value_size);
2148                                 loc->xl_entry->xe_value_size = orig_value_size;
2149                         }
2150                         mlog_errno(rc);
2151                 }
2152         }
2153
2154 out:
2155         return rc;
2156 }
2157
2158 /*
2159  * Store the value portion of the name+value pair.  This will skip
2160  * values that are stored externally.  Their tree roots were set up
2161  * by ocfs2_xa_prepare_entry().
2162  */
2163 static int ocfs2_xa_store_value(struct ocfs2_xa_loc *loc,
2164                                 struct ocfs2_xattr_info *xi,
2165                                 struct ocfs2_xattr_set_ctxt *ctxt)
2166 {
2167         int rc = 0;
2168         int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
2169         int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
2170         char *nameval_buf;
2171         struct ocfs2_xattr_value_buf vb;
2172
2173         nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
2174         if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2175                 ocfs2_xa_fill_value_buf(loc, &vb);
2176                 rc = __ocfs2_xattr_set_value_outside(loc->xl_inode,
2177                                                      ctxt->handle, &vb,
2178                                                      xi->xi_value,
2179                                                      xi->xi_value_len);
2180         } else
2181                 memcpy(nameval_buf + name_size, xi->xi_value, xi->xi_value_len);
2182
2183         return rc;
2184 }
2185
2186 static int ocfs2_xa_set(struct ocfs2_xa_loc *loc,
2187                         struct ocfs2_xattr_info *xi,
2188                         struct ocfs2_xattr_set_ctxt *ctxt)
2189 {
2190         int ret;
2191         u32 name_hash = ocfs2_xattr_name_hash(loc->xl_inode, xi->xi_name,
2192                                               xi->xi_name_len);
2193
2194         ret = ocfs2_xa_journal_access(ctxt->handle, loc,
2195                                       OCFS2_JOURNAL_ACCESS_WRITE);
2196         if (ret) {
2197                 mlog_errno(ret);
2198                 goto out;
2199         }
2200
2201         /*
2202          * From here on out, everything is going to modify the buffer a
2203          * little.  Errors are going to leave the xattr header in a
2204          * sane state.  Thus, even with errors we dirty the sucker.
2205          */
2206
2207         /* Don't worry, we are never called with !xi_value and !xl_entry */
2208         if (!xi->xi_value) {
2209                 ret = ocfs2_xa_remove(loc, ctxt);
2210                 goto out_dirty;
2211         }
2212
2213         ret = ocfs2_xa_prepare_entry(loc, xi, name_hash, ctxt);
2214         if (ret) {
2215                 if (ret != -ENOSPC)
2216                         mlog_errno(ret);
2217                 goto out_dirty;
2218         }
2219
2220         ret = ocfs2_xa_store_value(loc, xi, ctxt);
2221         if (ret)
2222                 mlog_errno(ret);
2223
2224 out_dirty:
2225         ocfs2_xa_journal_dirty(ctxt->handle, loc);
2226
2227 out:
2228         return ret;
2229 }
2230
2231 static void ocfs2_init_dinode_xa_loc(struct ocfs2_xa_loc *loc,
2232                                      struct inode *inode,
2233                                      struct buffer_head *bh,
2234                                      struct ocfs2_xattr_entry *entry)
2235 {
2236         struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
2237
2238         BUG_ON(!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_XATTR_FL));
2239
2240         loc->xl_inode = inode;
2241         loc->xl_ops = &ocfs2_xa_block_loc_ops;
2242         loc->xl_storage = bh;
2243         loc->xl_entry = entry;
2244         loc->xl_size = le16_to_cpu(di->i_xattr_inline_size);
2245         loc->xl_header =
2246                 (struct ocfs2_xattr_header *)(bh->b_data + bh->b_size -
2247                                               loc->xl_size);
2248 }
2249
2250 static void ocfs2_init_xattr_block_xa_loc(struct ocfs2_xa_loc *loc,
2251                                           struct inode *inode,
2252                                           struct buffer_head *bh,
2253                                           struct ocfs2_xattr_entry *entry)
2254 {
2255         struct ocfs2_xattr_block *xb =
2256                 (struct ocfs2_xattr_block *)bh->b_data;
2257
2258         BUG_ON(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED);
2259
2260         loc->xl_inode = inode;
2261         loc->xl_ops = &ocfs2_xa_block_loc_ops;
2262         loc->xl_storage = bh;
2263         loc->xl_header = &(xb->xb_attrs.xb_header);
2264         loc->xl_entry = entry;
2265         loc->xl_size = bh->b_size - offsetof(struct ocfs2_xattr_block,
2266                                              xb_attrs.xb_header);
2267 }
2268
2269 static void ocfs2_init_xattr_bucket_xa_loc(struct ocfs2_xa_loc *loc,
2270                                            struct ocfs2_xattr_bucket *bucket,
2271                                            struct ocfs2_xattr_entry *entry)
2272 {
2273         loc->xl_inode = bucket->bu_inode;
2274         loc->xl_ops = &ocfs2_xa_bucket_loc_ops;
2275         loc->xl_storage = bucket;
2276         loc->xl_header = bucket_xh(bucket);
2277         loc->xl_entry = entry;
2278         loc->xl_size = OCFS2_XATTR_BUCKET_SIZE;
2279 }
2280
2281 /*
2282  * In xattr remove, if it is stored outside and refcounted, we may have
2283  * the chance to split the refcount tree. So need the allocators.
2284  */
2285 static int ocfs2_lock_xattr_remove_allocators(struct inode *inode,
2286                                         struct ocfs2_xattr_value_root *xv,
2287                                         struct ocfs2_caching_info *ref_ci,
2288                                         struct buffer_head *ref_root_bh,
2289                                         struct ocfs2_alloc_context **meta_ac,
2290                                         int *ref_credits)
2291 {
2292         int ret, meta_add = 0;
2293         u32 p_cluster, num_clusters;
2294         unsigned int ext_flags;
2295
2296         *ref_credits = 0;
2297         ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
2298                                        &num_clusters,
2299                                        &xv->xr_list,
2300                                        &ext_flags);
2301         if (ret) {
2302                 mlog_errno(ret);
2303                 goto out;
2304         }
2305
2306         if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
2307                 goto out;
2308
2309         ret = ocfs2_refcounted_xattr_delete_need(inode, ref_ci,
2310                                                  ref_root_bh, xv,
2311                                                  &meta_add, ref_credits);
2312         if (ret) {
2313                 mlog_errno(ret);
2314                 goto out;
2315         }
2316
2317         ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
2318                                                 meta_add, meta_ac);
2319         if (ret)
2320                 mlog_errno(ret);
2321
2322 out:
2323         return ret;
2324 }
2325
2326 static int ocfs2_remove_value_outside(struct inode*inode,
2327                                       struct ocfs2_xattr_value_buf *vb,
2328                                       struct ocfs2_xattr_header *header,
2329                                       struct ocfs2_caching_info *ref_ci,
2330                                       struct buffer_head *ref_root_bh)
2331 {
2332         int ret = 0, i, ref_credits;
2333         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2334         struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
2335         void *val;
2336
2337         ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
2338
2339         for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
2340                 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
2341
2342                 if (ocfs2_xattr_is_local(entry))
2343                         continue;
2344
2345                 val = (void *)header +
2346                         le16_to_cpu(entry->xe_name_offset);
2347                 vb->vb_xv = (struct ocfs2_xattr_value_root *)
2348                         (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
2349
2350                 ret = ocfs2_lock_xattr_remove_allocators(inode, vb->vb_xv,
2351                                                          ref_ci, ref_root_bh,
2352                                                          &ctxt.meta_ac,
2353                                                          &ref_credits);
2354
2355                 ctxt.handle = ocfs2_start_trans(osb, ref_credits +
2356                                         ocfs2_remove_extent_credits(osb->sb));
2357                 if (IS_ERR(ctxt.handle)) {
2358                         ret = PTR_ERR(ctxt.handle);
2359                         mlog_errno(ret);
2360                         break;
2361                 }
2362
2363                 ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt);
2364                 if (ret < 0) {
2365                         mlog_errno(ret);
2366                         break;
2367                 }
2368
2369                 ocfs2_commit_trans(osb, ctxt.handle);
2370                 if (ctxt.meta_ac) {
2371                         ocfs2_free_alloc_context(ctxt.meta_ac);
2372                         ctxt.meta_ac = NULL;
2373                 }
2374         }
2375
2376         if (ctxt.meta_ac)
2377                 ocfs2_free_alloc_context(ctxt.meta_ac);
2378         ocfs2_schedule_truncate_log_flush(osb, 1);
2379         ocfs2_run_deallocs(osb, &ctxt.dealloc);
2380         return ret;
2381 }
2382
2383 static int ocfs2_xattr_ibody_remove(struct inode *inode,
2384                                     struct buffer_head *di_bh,
2385                                     struct ocfs2_caching_info *ref_ci,
2386                                     struct buffer_head *ref_root_bh)
2387 {
2388
2389         struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2390         struct ocfs2_xattr_header *header;
2391         int ret;
2392         struct ocfs2_xattr_value_buf vb = {
2393                 .vb_bh = di_bh,
2394                 .vb_access = ocfs2_journal_access_di,
2395         };
2396
2397         header = (struct ocfs2_xattr_header *)
2398                  ((void *)di + inode->i_sb->s_blocksize -
2399                  le16_to_cpu(di->i_xattr_inline_size));
2400
2401         ret = ocfs2_remove_value_outside(inode, &vb, header,
2402                                          ref_ci, ref_root_bh);
2403
2404         return ret;
2405 }
2406
2407 struct ocfs2_rm_xattr_bucket_para {
2408         struct ocfs2_caching_info *ref_ci;
2409         struct buffer_head *ref_root_bh;
2410 };
2411
2412 static int ocfs2_xattr_block_remove(struct inode *inode,
2413                                     struct buffer_head *blk_bh,
2414                                     struct ocfs2_caching_info *ref_ci,
2415                                     struct buffer_head *ref_root_bh)
2416 {
2417         struct ocfs2_xattr_block *xb;
2418         int ret = 0;
2419         struct ocfs2_xattr_value_buf vb = {
2420                 .vb_bh = blk_bh,
2421                 .vb_access = ocfs2_journal_access_xb,
2422         };
2423         struct ocfs2_rm_xattr_bucket_para args = {
2424                 .ref_ci = ref_ci,
2425                 .ref_root_bh = ref_root_bh,
2426         };
2427
2428         xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2429         if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2430                 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
2431                 ret = ocfs2_remove_value_outside(inode, &vb, header,
2432                                                  ref_ci, ref_root_bh);
2433         } else
2434                 ret = ocfs2_iterate_xattr_index_block(inode,
2435                                                 blk_bh,
2436                                                 ocfs2_rm_xattr_cluster,
2437                                                 &args);
2438
2439         return ret;
2440 }
2441
2442 static int ocfs2_xattr_free_block(struct inode *inode,
2443                                   u64 block,
2444                                   struct ocfs2_caching_info *ref_ci,
2445                                   struct buffer_head *ref_root_bh)
2446 {
2447         struct inode *xb_alloc_inode;
2448         struct buffer_head *xb_alloc_bh = NULL;
2449         struct buffer_head *blk_bh = NULL;
2450         struct ocfs2_xattr_block *xb;
2451         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2452         handle_t *handle;
2453         int ret = 0;
2454         u64 blk, bg_blkno;
2455         u16 bit;
2456
2457         ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
2458         if (ret < 0) {
2459                 mlog_errno(ret);
2460                 goto out;
2461         }
2462
2463         ret = ocfs2_xattr_block_remove(inode, blk_bh, ref_ci, ref_root_bh);
2464         if (ret < 0) {
2465                 mlog_errno(ret);
2466                 goto out;
2467         }
2468
2469         xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2470         blk = le64_to_cpu(xb->xb_blkno);
2471         bit = le16_to_cpu(xb->xb_suballoc_bit);
2472         if (xb->xb_suballoc_loc)
2473                 bg_blkno = le64_to_cpu(xb->xb_suballoc_loc);
2474         else
2475                 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2476
2477         xb_alloc_inode = ocfs2_get_system_file_inode(osb,
2478                                 EXTENT_ALLOC_SYSTEM_INODE,
2479                                 le16_to_cpu(xb->xb_suballoc_slot));
2480         if (!xb_alloc_inode) {
2481                 ret = -ENOMEM;
2482                 mlog_errno(ret);
2483                 goto out;
2484         }
2485         mutex_lock(&xb_alloc_inode->i_mutex);
2486
2487         ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
2488         if (ret < 0) {
2489                 mlog_errno(ret);
2490                 goto out_mutex;
2491         }
2492
2493         handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
2494         if (IS_ERR(handle)) {
2495                 ret = PTR_ERR(handle);
2496                 mlog_errno(ret);
2497                 goto out_unlock;
2498         }
2499
2500         ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
2501                                        bit, bg_blkno, 1);
2502         if (ret < 0)
2503                 mlog_errno(ret);
2504
2505         ocfs2_commit_trans(osb, handle);
2506 out_unlock:
2507         ocfs2_inode_unlock(xb_alloc_inode, 1);
2508         brelse(xb_alloc_bh);
2509 out_mutex:
2510         mutex_unlock(&xb_alloc_inode->i_mutex);
2511         iput(xb_alloc_inode);
2512 out:
2513         brelse(blk_bh);
2514         return ret;
2515 }
2516
2517 /*
2518  * ocfs2_xattr_remove()
2519  *
2520  * Free extended attribute resources associated with this inode.
2521  */
2522 int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
2523 {
2524         struct ocfs2_inode_info *oi = OCFS2_I(inode);
2525         struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2526         struct ocfs2_refcount_tree *ref_tree = NULL;
2527         struct buffer_head *ref_root_bh = NULL;
2528         struct ocfs2_caching_info *ref_ci = NULL;
2529         handle_t *handle;
2530         int ret;
2531
2532         if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2533                 return 0;
2534
2535         if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
2536                 return 0;
2537
2538         if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) {
2539                 ret = ocfs2_lock_refcount_tree(OCFS2_SB(inode->i_sb),
2540                                                le64_to_cpu(di->i_refcount_loc),
2541                                                1, &ref_tree, &ref_root_bh);
2542                 if (ret) {
2543                         mlog_errno(ret);
2544                         goto out;
2545                 }
2546                 ref_ci = &ref_tree->rf_ci;
2547
2548         }
2549
2550         if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2551                 ret = ocfs2_xattr_ibody_remove(inode, di_bh,
2552                                                ref_ci, ref_root_bh);
2553                 if (ret < 0) {
2554                         mlog_errno(ret);
2555                         goto out;
2556                 }
2557         }
2558
2559         if (di->i_xattr_loc) {
2560                 ret = ocfs2_xattr_free_block(inode,
2561                                              le64_to_cpu(di->i_xattr_loc),
2562                                              ref_ci, ref_root_bh);
2563                 if (ret < 0) {
2564                         mlog_errno(ret);
2565                         goto out;
2566                 }
2567         }
2568
2569         handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
2570                                    OCFS2_INODE_UPDATE_CREDITS);
2571         if (IS_ERR(handle)) {
2572                 ret = PTR_ERR(handle);
2573                 mlog_errno(ret);
2574                 goto out;
2575         }
2576         ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
2577                                       OCFS2_JOURNAL_ACCESS_WRITE);
2578         if (ret) {
2579                 mlog_errno(ret);
2580                 goto out_commit;
2581         }
2582
2583         di->i_xattr_loc = 0;
2584
2585         spin_lock(&oi->ip_lock);
2586         oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
2587         di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2588         spin_unlock(&oi->ip_lock);
2589
2590         ocfs2_journal_dirty(handle, di_bh);
2591 out_commit:
2592         ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
2593 out:
2594         if (ref_tree)
2595                 ocfs2_unlock_refcount_tree(OCFS2_SB(inode->i_sb), ref_tree, 1);
2596         brelse(ref_root_bh);
2597         return ret;
2598 }
2599
2600 static int ocfs2_xattr_has_space_inline(struct inode *inode,
2601                                         struct ocfs2_dinode *di)
2602 {
2603         struct ocfs2_inode_info *oi = OCFS2_I(inode);
2604         unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
2605         int free;
2606
2607         if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
2608                 return 0;
2609
2610         if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2611                 struct ocfs2_inline_data *idata = &di->id2.i_data;
2612                 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
2613         } else if (ocfs2_inode_is_fast_symlink(inode)) {
2614                 free = ocfs2_fast_symlink_chars(inode->i_sb) -
2615                         le64_to_cpu(di->i_size);
2616         } else {
2617                 struct ocfs2_extent_list *el = &di->id2.i_list;
2618                 free = (le16_to_cpu(el->l_count) -
2619                         le16_to_cpu(el->l_next_free_rec)) *
2620                         sizeof(struct ocfs2_extent_rec);
2621         }
2622         if (free >= xattrsize)
2623                 return 1;
2624
2625         return 0;
2626 }
2627
2628 /*
2629  * ocfs2_xattr_ibody_find()
2630  *
2631  * Find extended attribute in inode block and
2632  * fill search info into struct ocfs2_xattr_search.
2633  */
2634 static int ocfs2_xattr_ibody_find(struct inode *inode,
2635                                   int name_index,
2636                                   const char *name,
2637                                   struct ocfs2_xattr_search *xs)
2638 {
2639         struct ocfs2_inode_info *oi = OCFS2_I(inode);
2640         struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2641         int ret;
2642         int has_space = 0;
2643
2644         if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2645                 return 0;
2646
2647         if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2648                 down_read(&oi->ip_alloc_sem);
2649                 has_space = ocfs2_xattr_has_space_inline(inode, di);
2650                 up_read(&oi->ip_alloc_sem);
2651                 if (!has_space)
2652                         return 0;
2653         }
2654
2655         xs->xattr_bh = xs->inode_bh;
2656         xs->end = (void *)di + inode->i_sb->s_blocksize;
2657         if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
2658                 xs->header = (struct ocfs2_xattr_header *)
2659                         (xs->end - le16_to_cpu(di->i_xattr_inline_size));
2660         else
2661                 xs->header = (struct ocfs2_xattr_header *)
2662                         (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
2663         xs->base = (void *)xs->header;
2664         xs->here = xs->header->xh_entries;
2665
2666         /* Find the named attribute. */
2667         if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2668                 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2669                 if (ret && ret != -ENODATA)
2670                         return ret;
2671                 xs->not_found = ret;
2672         }
2673
2674         return 0;
2675 }
2676
2677 static int ocfs2_xattr_ibody_init(struct inode *inode,
2678                                   struct buffer_head *di_bh,
2679                                   struct ocfs2_xattr_set_ctxt *ctxt)
2680 {
2681         int ret;
2682         struct ocfs2_inode_info *oi = OCFS2_I(inode);
2683         struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2684         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2685         unsigned int xattrsize = osb->s_xattr_inline_size;
2686
2687         if (!ocfs2_xattr_has_space_inline(inode, di)) {
2688                 ret = -ENOSPC;
2689                 goto out;
2690         }
2691
2692         ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode), di_bh,
2693                                       OCFS2_JOURNAL_ACCESS_WRITE);
2694         if (ret) {
2695                 mlog_errno(ret);
2696                 goto out;
2697         }
2698
2699         /*
2700          * Adjust extent record count or inline data size
2701          * to reserve space for extended attribute.
2702          */
2703         if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2704                 struct ocfs2_inline_data *idata = &di->id2.i_data;
2705                 le16_add_cpu(&idata->id_count, -xattrsize);
2706         } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
2707                 struct ocfs2_extent_list *el = &di->id2.i_list;
2708                 le16_add_cpu(&el->l_count, -(xattrsize /
2709                                              sizeof(struct ocfs2_extent_rec)));
2710         }
2711         di->i_xattr_inline_size = cpu_to_le16(xattrsize);
2712
2713         spin_lock(&oi->ip_lock);
2714         oi->ip_dyn_features |= OCFS2_INLINE_XATTR_FL|OCFS2_HAS_XATTR_FL;
2715         di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2716         spin_unlock(&oi->ip_lock);
2717
2718         ocfs2_journal_dirty(ctxt->handle, di_bh);
2719
2720 out:
2721         return ret;
2722 }
2723
2724 /*
2725  * ocfs2_xattr_ibody_set()
2726  *
2727  * Set, replace or remove an extended attribute into inode block.
2728  *
2729  */
2730 static int ocfs2_xattr_ibody_set(struct inode *inode,
2731                                  struct ocfs2_xattr_info *xi,
2732                                  struct ocfs2_xattr_search *xs,
2733                                  struct ocfs2_xattr_set_ctxt *ctxt)
2734 {
2735         int ret;
2736         struct ocfs2_inode_info *oi = OCFS2_I(inode);
2737         struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2738         struct ocfs2_xa_loc loc;
2739
2740         if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2741                 return -ENOSPC;
2742
2743         down_write(&oi->ip_alloc_sem);
2744         if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2745                 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2746                         ret = -ENOSPC;
2747                         goto out;
2748                 }
2749         }
2750
2751         if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2752                 ret = ocfs2_xattr_ibody_init(inode, xs->inode_bh, ctxt);
2753                 if (ret) {
2754                         if (ret != -ENOSPC)
2755                                 mlog_errno(ret);
2756                         goto out;
2757                 }
2758         }
2759
2760         ocfs2_init_dinode_xa_loc(&loc, inode, xs->inode_bh,
2761                                  xs->not_found ? NULL : xs->here);
2762         ret = ocfs2_xa_set(&loc, xi, ctxt);
2763         if (ret) {
2764                 if (ret != -ENOSPC)
2765                         mlog_errno(ret);
2766                 goto out;
2767         }
2768         xs->here = loc.xl_entry;
2769
2770 out:
2771         up_write(&oi->ip_alloc_sem);
2772
2773         return ret;
2774 }
2775
2776 /*
2777  * ocfs2_xattr_block_find()
2778  *
2779  * Find extended attribute in external block and
2780  * fill search info into struct ocfs2_xattr_search.
2781  */
2782 static int ocfs2_xattr_block_find(struct inode *inode,
2783                                   int name_index,
2784                                   const char *name,
2785                                   struct ocfs2_xattr_search *xs)
2786 {
2787         struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2788         struct buffer_head *blk_bh = NULL;
2789         struct ocfs2_xattr_block *xb;
2790         int ret = 0;
2791
2792         if (!di->i_xattr_loc)
2793                 return ret;
2794
2795         ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
2796                                      &blk_bh);
2797         if (ret < 0) {
2798                 mlog_errno(ret);
2799                 return ret;
2800         }
2801
2802         xs->xattr_bh = blk_bh;
2803         xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2804
2805         if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2806                 xs->header = &xb->xb_attrs.xb_header;
2807                 xs->base = (void *)xs->header;
2808                 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
2809                 xs->here = xs->header->xh_entries;
2810
2811                 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2812         } else
2813                 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
2814                                                    name_index,
2815                                                    name, xs);
2816
2817         if (ret && ret != -ENODATA) {
2818                 xs->xattr_bh = NULL;
2819                 goto cleanup;
2820         }
2821         xs->not_found = ret;
2822         return 0;
2823 cleanup:
2824         brelse(blk_bh);
2825
2826         return ret;
2827 }
2828
2829 static int ocfs2_create_xattr_block(struct inode *inode,
2830                                     struct buffer_head *inode_bh,
2831                                     struct ocfs2_xattr_set_ctxt *ctxt,
2832                                     int indexed,
2833                                     struct buffer_head **ret_bh)
2834 {
2835         int ret;
2836         u16 suballoc_bit_start;
2837         u32 num_got;
2838         u64 suballoc_loc, first_blkno;
2839         struct ocfs2_dinode *di =  (struct ocfs2_dinode *)inode_bh->b_data;
2840         struct buffer_head *new_bh = NULL;
2841         struct ocfs2_xattr_block *xblk;
2842
2843         ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
2844                                       inode_bh, OCFS2_JOURNAL_ACCESS_CREATE);
2845         if (ret < 0) {
2846                 mlog_errno(ret);
2847                 goto end;
2848         }
2849
2850         ret = ocfs2_claim_metadata(ctxt->handle, ctxt->meta_ac, 1,
2851                                    &suballoc_loc, &suballoc_bit_start,
2852                                    &num_got, &first_blkno);
2853         if (ret < 0) {
2854                 mlog_errno(ret);
2855                 goto end;
2856         }
2857
2858         new_bh = sb_getblk(inode->i_sb, first_blkno);
2859         ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
2860
2861         ret = ocfs2_journal_access_xb(ctxt->handle, INODE_CACHE(inode),
2862                                       new_bh,
2863                                       OCFS2_JOURNAL_ACCESS_CREATE);
2864         if (ret < 0) {
2865                 mlog_errno(ret);
2866                 goto end;
2867         }
2868
2869         /* Initialize ocfs2_xattr_block */
2870         xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2871         memset(xblk, 0, inode->i_sb->s_blocksize);
2872         strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
2873         xblk->xb_suballoc_slot = cpu_to_le16(ctxt->meta_ac->ac_alloc_slot);
2874         xblk->xb_suballoc_loc = cpu_to_le64(suballoc_loc);
2875         xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
2876         xblk->xb_fs_generation =
2877                 cpu_to_le32(OCFS2_SB(inode->i_sb)->fs_generation);
2878         xblk->xb_blkno = cpu_to_le64(first_blkno);
2879         if (indexed) {
2880                 struct ocfs2_xattr_tree_root *xr = &xblk->xb_attrs.xb_root;
2881                 xr->xt_clusters = cpu_to_le32(1);
2882                 xr->xt_last_eb_blk = 0;
2883                 xr->xt_list.l_tree_depth = 0;
2884                 xr->xt_list.l_count = cpu_to_le16(
2885                                         ocfs2_xattr_recs_per_xb(inode->i_sb));
2886                 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2887                 xblk->xb_flags = cpu_to_le16(OCFS2_XATTR_INDEXED);
2888         }
2889         ocfs2_journal_dirty(ctxt->handle, new_bh);
2890
2891         /* Add it to the inode */
2892         di->i_xattr_loc = cpu_to_le64(first_blkno);
2893
2894         spin_lock(&OCFS2_I(inode)->ip_lock);
2895         OCFS2_I(inode)->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
2896         di->i_dyn_features = cpu_to_le16(OCFS2_I(inode)->ip_dyn_features);
2897         spin_unlock(&OCFS2_I(inode)->ip_lock);
2898
2899         ocfs2_journal_dirty(ctxt->handle, inode_bh);
2900
2901         *ret_bh = new_bh;
2902         new_bh = NULL;
2903
2904 end:
2905         brelse(new_bh);
2906         return ret;
2907 }
2908
2909 /*
2910  * ocfs2_xattr_block_set()
2911  *
2912  * Set, replace or remove an extended attribute into external block.
2913  *
2914  */
2915 static int ocfs2_xattr_block_set(struct inode *inode,
2916                                  struct ocfs2_xattr_info *xi,
2917                                  struct ocfs2_xattr_search *xs,
2918                                  struct ocfs2_xattr_set_ctxt *ctxt)
2919 {
2920         struct buffer_head *new_bh = NULL;
2921         struct ocfs2_xattr_block *xblk = NULL;
2922         int ret;
2923         struct ocfs2_xa_loc loc;
2924
2925         if (!xs->xattr_bh) {
2926                 ret = ocfs2_create_xattr_block(inode, xs->inode_bh, ctxt,
2927                                                0, &new_bh);
2928                 if (ret) {
2929                         mlog_errno(ret);
2930                         goto end;
2931                 }
2932
2933                 xs->xattr_bh = new_bh;
2934                 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2935                 xs->header = &xblk->xb_attrs.xb_header;
2936                 xs->base = (void *)xs->header;
2937                 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2938                 xs->here = xs->header->xh_entries;
2939         } else
2940                 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2941
2942         if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
2943                 ocfs2_init_xattr_block_xa_loc(&loc, inode, xs->xattr_bh,
2944                                               xs->not_found ? NULL : xs->here);
2945
2946                 ret = ocfs2_xa_set(&loc, xi, ctxt);
2947                 if (!ret)
2948                         xs->here = loc.xl_entry;
2949                 else if (ret != -ENOSPC)
2950                         goto end;
2951                 else {
2952                         ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
2953                         if (ret)
2954                                 goto end;
2955                 }
2956         }
2957
2958         if (le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)
2959                 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
2960
2961 end:
2962         return ret;
2963 }
2964
2965 /* Check whether the new xattr can be inserted into the inode. */
2966 static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
2967                                        struct ocfs2_xattr_info *xi,
2968                                        struct ocfs2_xattr_search *xs)
2969 {
2970         struct ocfs2_xattr_entry *last;
2971         int free, i;
2972         size_t min_offs = xs->end - xs->base;
2973
2974         if (!xs->header)
2975                 return 0;
2976
2977         last = xs->header->xh_entries;
2978
2979         for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
2980                 size_t offs = le16_to_cpu(last->xe_name_offset);
2981                 if (offs < min_offs)
2982                         min_offs = offs;
2983                 last += 1;
2984         }
2985
2986         free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
2987         if (free < 0)
2988                 return 0;
2989
2990         BUG_ON(!xs->not_found);
2991
2992         if (free >= (sizeof(struct ocfs2_xattr_entry) + namevalue_size_xi(xi)))
2993                 return 1;
2994
2995         return 0;
2996 }
2997
2998 static int ocfs2_calc_xattr_set_need(struct inode *inode,
2999                                      struct ocfs2_dinode *di,
3000                                      struct ocfs2_xattr_info *xi,
3001                                      struct ocfs2_xattr_search *xis,
3002                                      struct ocfs2_xattr_search *xbs,
3003                                      int *clusters_need,
3004                                      int *meta_need,
3005                                      int *credits_need)
3006 {
3007         int ret = 0, old_in_xb = 0;
3008         int clusters_add = 0, meta_add = 0, credits = 0;
3009         struct buffer_head *bh = NULL;
3010         struct ocfs2_xattr_block *xb = NULL;
3011         struct ocfs2_xattr_entry *xe = NULL;
3012         struct ocfs2_xattr_value_root *xv = NULL;
3013         char *base = NULL;
3014         int name_offset, name_len = 0;
3015         u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
3016                                                     xi->xi_value_len);
3017         u64 value_size;
3018
3019         /*
3020          * Calculate the clusters we need to write.
3021          * No matter whether we replace an old one or add a new one,
3022          * we need this for writing.
3023          */
3024         if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
3025                 credits += new_clusters *
3026                            ocfs2_clusters_to_blocks(inode->i_sb, 1);
3027
3028         if (xis->not_found && xbs->not_found) {
3029                 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3030
3031                 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
3032                         clusters_add += new_clusters;
3033                         credits += ocfs2_calc_extend_credits(inode->i_sb,
3034                                                         &def_xv.xv.xr_list,
3035                                                         new_clusters);
3036                 }
3037
3038                 goto meta_guess;
3039         }
3040
3041         if (!xis->not_found) {
3042                 xe = xis->here;
3043                 name_offset = le16_to_cpu(xe->xe_name_offset);
3044                 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3045                 base = xis->base;
3046                 credits += OCFS2_INODE_UPDATE_CREDITS;
3047         } else {
3048                 int i, block_off = 0;
3049                 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
3050                 xe = xbs->here;
3051                 name_offset = le16_to_cpu(xe->xe_name_offset);
3052                 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3053                 i = xbs->here - xbs->header->xh_entries;
3054                 old_in_xb = 1;
3055
3056                 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
3057                         ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
3058                                                         bucket_xh(xbs->bucket),
3059                                                         i, &block_off,
3060                                                         &name_offset);
3061                         base = bucket_block(xbs->bucket, block_off);
3062                         credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3063                 } else {
3064                         base = xbs->base;
3065                         credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
3066                 }
3067         }
3068
3069         /*
3070          * delete a xattr doesn't need metadata and cluster allocation.
3071          * so just calculate the credits and return.
3072          *
3073          * The credits for removing the value tree will be extended
3074          * by ocfs2_remove_extent itself.
3075          */
3076         if (!xi->xi_value) {
3077                 if (!ocfs2_xattr_is_local(xe))
3078                         credits += ocfs2_remove_extent_credits(inode->i_sb);
3079
3080                 goto out;
3081         }
3082
3083         /* do cluster allocation guess first. */
3084         value_size = le64_to_cpu(xe->xe_value_size);
3085
3086         if (old_in_xb) {
3087                 /*
3088                  * In xattr set, we always try to set the xe in inode first,
3089                  * so if it can be inserted into inode successfully, the old
3090                  * one will be removed from the xattr block, and this xattr
3091                  * will be inserted into inode as a new xattr in inode.
3092                  */
3093                 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
3094                         clusters_add += new_clusters;
3095                         credits += ocfs2_remove_extent_credits(inode->i_sb) +
3096                                     OCFS2_INODE_UPDATE_CREDITS;
3097                         if (!ocfs2_xattr_is_local(xe))
3098                                 credits += ocfs2_calc_extend_credits(
3099                                                         inode->i_sb,
3100                                                         &def_xv.xv.xr_list,
3101                                                         new_clusters);
3102                         goto out;
3103                 }
3104         }
3105
3106         if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
3107                 /* the new values will be stored outside. */
3108                 u32 old_clusters = 0;
3109
3110                 if (!ocfs2_xattr_is_local(xe)) {
3111                         old_clusters =  ocfs2_clusters_for_bytes(inode->i_sb,
3112                                                                  value_size);
3113                         xv = (struct ocfs2_xattr_value_root *)
3114                              (base + name_offset + name_len);
3115                         value_size = OCFS2_XATTR_ROOT_SIZE;
3116                 } else
3117                         xv = &def_xv.xv;
3118
3119                 if (old_clusters >= new_clusters) {
3120                         credits += ocfs2_remove_extent_credits(inode->i_sb);
3121                         goto out;
3122                 } else {
3123                         meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
3124                         clusters_add += new_clusters - old_clusters;
3125                         credits += ocfs2_calc_extend_credits(inode->i_sb,
3126                                                              &xv->xr_list,
3127                                                              new_clusters -
3128                                                              old_clusters);
3129                         if (value_size >= OCFS2_XATTR_ROOT_SIZE)
3130                                 goto out;
3131                 }
3132         } else {
3133                 /*
3134                  * Now the new value will be stored inside. So if the new
3135                  * value is smaller than the size of value root or the old
3136                  * value, we don't need any allocation, otherwise we have
3137                  * to guess metadata allocation.
3138                  */
3139                 if ((ocfs2_xattr_is_local(xe) &&
3140                      (value_size >= xi->xi_value_len)) ||
3141                     (!ocfs2_xattr_is_local(xe) &&
3142                      OCFS2_XATTR_ROOT_SIZE >= xi->xi_value_len))
3143                         goto out;
3144         }
3145
3146 meta_guess:
3147         /* calculate metadata allocation. */
3148         if (di->i_xattr_loc) {
3149                 if (!xbs->xattr_bh) {
3150                         ret = ocfs2_read_xattr_block(inode,
3151                                                      le64_to_cpu(di->i_xattr_loc),
3152                                                      &bh);
3153                         if (ret) {
3154                                 mlog_errno(ret);
3155                                 goto out;
3156                         }
3157
3158                         xb = (struct ocfs2_xattr_block *)bh->b_data;
3159                 } else
3160                         xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
3161
3162                 /*
3163                  * If there is already an xattr tree, good, we can calculate
3164                  * like other b-trees. Otherwise we may have the chance of
3165                  * create a tree, the credit calculation is borrowed from
3166                  * ocfs2_calc_extend_credits with root_el = NULL. And the
3167                  * new tree will be cluster based, so no meta is needed.
3168                  */
3169                 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
3170                         struct ocfs2_extent_list *el =
3171                                  &xb->xb_attrs.xb_root.xt_list;
3172                         meta_add += ocfs2_extend_meta_needed(el);
3173                         credits += ocfs2_calc_extend_credits(inode->i_sb,
3174                                                              el, 1);
3175                 } else
3176                         credits += OCFS2_SUBALLOC_ALLOC + 1;
3177
3178                 /*
3179                  * This cluster will be used either for new bucket or for
3180                  * new xattr block.
3181                  * If the cluster size is the same as the bucket size, one
3182                  * more is needed since we may need to extend the bucket
3183                  * also.
3184                  */
3185                 clusters_add += 1;
3186                 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3187                 if (OCFS2_XATTR_BUCKET_SIZE ==
3188                         OCFS2_SB(inode->i_sb)->s_clustersize) {
3189                         credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3190                         clusters_add += 1;
3191                 }
3192         } else {
3193                 meta_add += 1;
3194                 credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
3195         }
3196 out:
3197         if (clusters_need)
3198                 *clusters_need = clusters_add;
3199         if (meta_need)
3200                 *meta_need = meta_add;
3201         if (credits_need)
3202                 *credits_need = credits;
3203         brelse(bh);
3204         return ret;
3205 }
3206
3207 static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
3208                                      struct ocfs2_dinode *di,
3209                                      struct ocfs2_xattr_info *xi,
3210                                      struct ocfs2_xattr_search *xis,
3211                                      struct ocfs2_xattr_search *xbs,
3212                                      struct ocfs2_xattr_set_ctxt *ctxt,
3213                                      int extra_meta,
3214                                      int *credits)
3215 {
3216         int clusters_add, meta_add, ret;
3217         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3218
3219         memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
3220
3221         ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
3222
3223         ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
3224                                         &clusters_add, &meta_add, credits);
3225         if (ret) {
3226                 mlog_errno(ret);
3227                 return ret;
3228         }
3229
3230         meta_add += extra_meta;
3231         mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
3232              "credits = %d\n", xi->xi_name, meta_add, clusters_add, *credits);
3233
3234         if (meta_add) {
3235                 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
3236                                                         &ctxt->meta_ac);
3237                 if (ret) {
3238                         mlog_errno(ret);
3239                         goto out;
3240                 }
3241         }
3242
3243         if (clusters_add) {
3244                 ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
3245                 if (ret)
3246                         mlog_errno(ret);
3247         }
3248 out:
3249         if (ret) {
3250                 if (ctxt->meta_ac) {
3251                         ocfs2_free_alloc_context(ctxt->meta_ac);
3252                         ctxt->meta_ac = NULL;
3253                 }
3254
3255                 /*
3256                  * We cannot have an error and a non null ctxt->data_ac.
3257                  */
3258         }
3259
3260         return ret;
3261 }
3262
3263 static int __ocfs2_xattr_set_handle(struct inode *inode,
3264                                     struct ocfs2_dinode *di,
3265                                     struct ocfs2_xattr_info *xi,
3266                                     struct ocfs2_xattr_search *xis,
3267                                     struct ocfs2_xattr_search *xbs,
3268                                     struct ocfs2_xattr_set_ctxt *ctxt)
3269 {
3270         int ret = 0, credits, old_found;
3271
3272         if (!xi->xi_value) {
3273                 /* Remove existing extended attribute */
3274                 if (!xis->not_found)
3275                         ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3276                 else if (!xbs->not_found)
3277                         ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3278         } else {
3279                 /* We always try to set extended attribute into inode first*/
3280                 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3281                 if (!ret && !xbs->not_found) {
3282                         /*
3283                          * If succeed and that extended attribute existing in
3284                          * external block, then we will remove it.
3285                          */
3286                         xi->xi_value = NULL;
3287                         xi->xi_value_len = 0;
3288
3289                         old_found = xis->not_found;
3290                         xis->not_found = -ENODATA;
3291                         ret = ocfs2_calc_xattr_set_need(inode,
3292                                                         di,
3293                                                         xi,
3294                                                         xis,
3295                                                         xbs,
3296                                                         NULL,
3297                                                         NULL,
3298                                                         &credits);
3299                         xis->not_found = old_found;
3300                         if (ret) {
3301                                 mlog_errno(ret);
3302                                 goto out;
3303                         }
3304
3305                         ret = ocfs2_extend_trans(ctxt->handle, credits);
3306                         if (ret) {
3307                                 mlog_errno(ret);
3308                                 goto out;
3309                         }
3310                         ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3311                 } else if (ret == -ENOSPC) {
3312                         if (di->i_xattr_loc && !xbs->xattr_bh) {
3313                                 ret = ocfs2_xattr_block_find(inode,
3314                                                              xi->xi_name_index,
3315                                                              xi->xi_name, xbs);
3316                                 if (ret)
3317                                         goto out;
3318
3319                                 old_found = xis->not_found;
3320                                 xis->not_found = -ENODATA;
3321                                 ret = ocfs2_calc_xattr_set_need(inode,
3322                                                                 di,
3323                                                                 xi,
3324                                                                 xis,
3325                                                                 xbs,
3326                                                                 NULL,
3327                                                                 NULL,
3328                                                                 &credits);
3329                                 xis->not_found = old_found;
3330                                 if (ret) {
3331                                         mlog_errno(ret);
3332                                         goto out;
3333                                 }
3334
3335                                 ret = ocfs2_extend_trans(ctxt->handle, credits);
3336                                 if (ret) {
3337                                         mlog_errno(ret);
3338                                         goto out;
3339                                 }
3340                         }
3341                         /*
3342                          * If no space in inode, we will set extended attribute
3343                          * into external block.
3344                          */
3345                         ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3346                         if (ret)
3347                                 goto out;
3348                         if (!xis->not_found) {
3349                                 /*
3350                                  * If succeed and that extended attribute
3351                                  * existing in inode, we will remove it.
3352                                  */
3353                                 xi->xi_value = NULL;
3354                                 xi->xi_value_len = 0;
3355                                 xbs->not_found = -ENODATA;
3356                                 ret = ocfs2_calc_xattr_set_need(inode,
3357                                                                 di,
3358                                                                 xi,
3359                                                                 xis,
3360                                                                 xbs,
3361                                                                 NULL,
3362                                                                 NULL,
3363                                                                 &credits);
3364                                 if (ret) {
3365                                         mlog_errno(ret);
3366                                         goto out;
3367                                 }
3368
3369                                 ret = ocfs2_extend_trans(ctxt->handle, credits);
3370                                 if (ret) {
3371                                         mlog_errno(ret);
3372                                         goto out;
3373                                 }
3374                                 ret = ocfs2_xattr_ibody_set(inode, xi,
3375                                                             xis, ctxt);
3376                         }
3377                 }
3378         }
3379
3380         if (!ret) {
3381                 /* Update inode ctime. */
3382                 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
3383                                               xis->inode_bh,
3384                                               OCFS2_JOURNAL_ACCESS_WRITE);
3385                 if (ret) {
3386                         mlog_errno(ret);
3387                         goto out;
3388                 }
3389
3390                 inode->i_ctime = CURRENT_TIME;
3391                 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
3392                 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
3393                 ocfs2_journal_dirty(ctxt->handle, xis->inode_bh);
3394         }
3395 out:
3396         return ret;
3397 }
3398
3399 /*
3400  * This function only called duing creating inode
3401  * for init security/acl xattrs of the new inode.
3402  * All transanction credits have been reserved in mknod.
3403  */
3404 int ocfs2_xattr_set_handle(handle_t *handle,
3405                            struct inode *inode,
3406                            struct buffer_head *di_bh,
3407                            int name_index,
3408                            const char *name,
3409                            const void *value,
3410                            size_t value_len,
3411                            int flags,
3412                            struct ocfs2_alloc_context *meta_ac,
3413                            struct ocfs2_alloc_context *data_ac)
3414 {
3415         struct ocfs2_dinode *di;
3416         int ret;
3417
3418         struct ocfs2_xattr_info xi = {
3419                 .xi_name_index = name_index,
3420                 .xi_name = name,
3421                 .xi_name_len = strlen(name),
3422                 .xi_value = value,
3423                 .xi_value_len = value_len,
3424         };
3425
3426         struct ocfs2_xattr_search xis = {
3427                 .not_found = -ENODATA,
3428         };
3429
3430         struct ocfs2_xattr_search xbs = {
3431                 .not_found = -ENODATA,
3432         };
3433
3434         struct ocfs2_xattr_set_ctxt ctxt = {
3435                 .handle = handle,
3436                 .meta_ac = meta_ac,
3437                 .data_ac = data_ac,
3438         };
3439
3440         if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3441                 return -EOPNOTSUPP;
3442
3443         /*
3444          * In extreme situation, may need xattr bucket when
3445          * block size is too small. And we have already reserved
3446          * the credits for bucket in mknod.
3447          */
3448         if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) {
3449                 xbs.bucket = ocfs2_xattr_bucket_new(inode);
3450                 if (!xbs.bucket) {
3451                         mlog_errno(-ENOMEM);
3452                         return -ENOMEM;
3453                 }
3454         }
3455
3456         xis.inode_bh = xbs.inode_bh = di_bh;
3457         di = (struct ocfs2_dinode *)di_bh->b_data;
3458
3459         down_write(&OCFS2_I(inode)->ip_xattr_sem);
3460
3461         ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3462         if (ret)
3463                 goto cleanup;
3464         if (xis.not_found) {
3465                 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3466                 if (ret)
3467                         goto cleanup;
3468         }
3469
3470         ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3471
3472 cleanup:
3473         up_write(&OCFS2_I(inode)->ip_xattr_sem);
3474         brelse(xbs.xattr_bh);
3475         ocfs2_xattr_bucket_free(xbs.bucket);
3476
3477         return ret;
3478 }
3479
3480 /*
3481  * ocfs2_xattr_set()
3482  *
3483  * Set, replace or remove an extended attribute for this inode.
3484  * value is NULL to remove an existing extended attribute, else either
3485  * create or replace an extended attribute.
3486  */
3487 int ocfs2_xattr_set(struct inode *inode,
3488                     int name_index,
3489                     const char *name,
3490                     const void *value,
3491                     size_t value_len,
3492                     int flags)
3493 {
3494         struct buffer_head *di_bh = NULL;
3495         struct ocfs2_dinode *di;
3496         int ret, credits, ref_meta = 0, ref_credits = 0;
3497         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3498         struct inode *tl_inode = osb->osb_tl_inode;
3499         struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
3500         struct ocfs2_refcount_tree *ref_tree = NULL;
3501
3502         struct ocfs2_xattr_info xi = {
3503                 .xi_name_index = name_index,
3504                 .xi_name = name,
3505                 .xi_name_len = strlen(name),
3506                 .xi_value = value,
3507                 .xi_value_len = value_len,
3508         };
3509
3510         struct ocfs2_xattr_search xis = {
3511                 .not_found = -ENODATA,
3512         };
3513
3514         struct ocfs2_xattr_search xbs = {
3515                 .not_found = -ENODATA,
3516         };
3517
3518         if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3519                 return -EOPNOTSUPP;
3520
3521         /*
3522          * Only xbs will be used on indexed trees.  xis doesn't need a
3523          * bucket.
3524          */
3525         xbs.bucket = ocfs2_xattr_bucket_new(inode);
3526         if (!xbs.bucket) {
3527                 mlog_errno(-ENOMEM);
3528                 return -ENOMEM;
3529         }
3530
3531         ret = ocfs2_inode_lock(inode, &di_bh, 1);
3532         if (ret < 0) {
3533                 mlog_errno(ret);
3534                 goto cleanup_nolock;
3535         }
3536         xis.inode_bh = xbs.inode_bh = di_bh;
3537         di = (struct ocfs2_dinode *)di_bh->b_data;
3538
3539         down_write(&OCFS2_I(inode)->ip_xattr_sem);
3540         /*
3541          * Scan inode and external block to find the same name
3542          * extended attribute and collect search infomation.
3543          */
3544         ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3545         if (ret)
3546                 goto cleanup;
3547         if (xis.not_found) {
3548                 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3549                 if (ret)
3550                         goto cleanup;
3551         }
3552
3553         if (xis.not_found && xbs.not_found) {
3554                 ret = -ENODATA;
3555                 if (flags & XATTR_REPLACE)
3556                         goto cleanup;
3557                 ret = 0;
3558                 if (!value)
3559                         goto cleanup;
3560         } else {
3561                 ret = -EEXIST;
3562                 if (flags & XATTR_CREATE)
3563                         goto cleanup;
3564         }
3565
3566         /* Check whether the value is refcounted and do some prepartion. */
3567         if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL &&
3568             (!xis.not_found || !xbs.not_found)) {
3569                 ret = ocfs2_prepare_refcount_xattr(inode, di, &xi,
3570                                                    &xis, &xbs, &ref_tree,
3571                                                    &ref_meta, &ref_credits);
3572                 if (ret) {
3573                         mlog_errno(ret);
3574                         goto cleanup;
3575                 }
3576         }
3577
3578         mutex_lock(&tl_inode->i_mutex);
3579
3580         if (ocfs2_truncate_log_needs_flush(osb)) {
3581                 ret = __ocfs2_flush_truncate_log(osb);
3582                 if (ret < 0) {
3583                         mutex_unlock(&tl_inode->i_mutex);
3584                         mlog_errno(ret);
3585                         goto cleanup;
3586                 }
3587         }
3588         mutex_unlock(&tl_inode->i_mutex);
3589
3590         ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
3591                                         &xbs, &ctxt, ref_meta, &credits);
3592         if (ret) {
3593                 mlog_errno(ret);
3594                 goto cleanup;
3595         }
3596
3597         /* we need to update inode's ctime field, so add credit for it. */
3598         credits += OCFS2_INODE_UPDATE_CREDITS;
3599         ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
3600         if (IS_ERR(ctxt.handle)) {
3601                 ret = PTR_ERR(ctxt.handle);
3602                 mlog_errno(ret);
3603                 goto cleanup;
3604         }
3605
3606         ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3607
3608         ocfs2_commit_trans(osb, ctxt.handle);
3609
3610         if (ctxt.data_ac)
3611                 ocfs2_free_alloc_context(ctxt.data_ac);
3612         if (ctxt.meta_ac)
3613                 ocfs2_free_alloc_context(ctxt.meta_ac);
3614         if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
3615                 ocfs2_schedule_truncate_log_flush(osb, 1);
3616         ocfs2_run_deallocs(osb, &ctxt.dealloc);
3617
3618 cleanup:
3619         if (ref_tree)
3620                 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
3621         up_write(&OCFS2_I(inode)->ip_xattr_sem);
3622         if (!value && !ret) {
3623                 ret = ocfs2_try_remove_refcount_tree(inode, di_bh);
3624                 if (ret)
3625                         mlog_errno(ret);
3626         }
3627         ocfs2_inode_unlock(inode, 1);
3628 cleanup_nolock:
3629         brelse(di_bh);
3630         brelse(xbs.xattr_bh);
3631         ocfs2_xattr_bucket_free(xbs.bucket);
3632
3633         return ret;
3634 }
3635
3636 /*
3637  * Find the xattr extent rec which may contains name_hash.
3638  * e_cpos will be the first name hash of the xattr rec.
3639  * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
3640  */
3641 static int ocfs2_xattr_get_rec(struct inode *inode,
3642                                u32 name_hash,
3643                                u64 *p_blkno,
3644                                u32 *e_cpos,
3645                                u32 *num_clusters,
3646                                struct ocfs2_extent_list *el)
3647 {
3648         int ret = 0, i;
3649         struct buffer_head *eb_bh = NULL;
3650         struct ocfs2_extent_block *eb;
3651         struct ocfs2_extent_rec *rec = NULL;
3652         u64 e_blkno = 0;
3653
3654         if (el->l_tree_depth) {
3655                 ret = ocfs2_find_leaf(INODE_CACHE(inode), el, name_hash,
3656                                       &eb_bh);
3657                 if (ret) {
3658                         mlog_errno(ret);
3659                         goto out;
3660                 }
3661
3662                 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
3663                 el = &eb->h_list;
3664
3665                 if (el->l_tree_depth) {
3666                         ocfs2_error(inode->i_sb,
3667                                     "Inode %lu has non zero tree depth in "
3668                                     "xattr tree block %llu\n", inode->i_ino,
3669                                     (unsigned long long)eb_bh->b_blocknr);
3670                         ret = -EROFS;
3671                         goto out;
3672                 }
3673         }
3674
3675         for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
3676                 rec = &el->l_recs[i];
3677
3678                 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
3679                         e_blkno = le64_to_cpu(rec->e_blkno);
3680                         break;
3681                 }
3682         }
3683
3684         if (!e_blkno) {
3685                 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
3686                             "record (%u, %u, 0) in xattr", inode->i_ino,
3687                             le32_to_cpu(rec->e_cpos),
3688                             ocfs2_rec_clusters(el, rec));
3689                 ret = -EROFS;
3690                 goto out;
3691         }
3692
3693         *p_blkno = le64_to_cpu(rec->e_blkno);
3694         *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
3695         if (e_cpos)
3696                 *e_cpos = le32_to_cpu(rec->e_cpos);
3697 out:
3698         brelse(eb_bh);
3699         return ret;
3700 }
3701
3702 typedef int (xattr_bucket_func)(struct inode *inode,
3703                                 struct ocfs2_xattr_bucket *bucket,
3704                                 void *para);
3705
3706 static int ocfs2_find_xe_in_bucket(struct inode *inode,
3707                                    struct ocfs2_xattr_bucket *bucket,
3708                                    int name_index,
3709                                    const char *name,
3710                                    u32 name_hash,
3711                                    u16 *xe_index,
3712                                    int *found)
3713 {
3714         int i, ret = 0, cmp = 1, block_off, new_offset;
3715         struct ocfs2_xattr_header *xh = bucket_xh(bucket);
3716         size_t name_len = strlen(name);
3717         struct ocfs2_xattr_entry *xe = NULL;
3718         char *xe_name;
3719
3720         /*
3721          * We don't use binary search in the bucket because there
3722          * may be multiple entries with the same name hash.
3723          */
3724         for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3725                 xe = &xh->xh_entries[i];
3726
3727                 if (name_hash > le32_to_cpu(xe->xe_name_hash))
3728                         continue;
3729                 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
3730                         break;
3731
3732                 cmp = name_index - ocfs2_xattr_get_type(xe);
3733                 if (!cmp)
3734                         cmp = name_len - xe->xe_name_len;
3735                 if (cmp)
3736                         continue;
3737
3738                 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
3739                                                         xh,
3740                                                         i,
3741                                                         &block_off,
3742                                                         &new_offset);
3743                 if (ret) {
3744                         mlog_errno(ret);
3745                         break;
3746                 }
3747
3748
3749                 xe_name = bucket_block(bucket, block_off) + new_offset;
3750                 if (!memcmp(name, xe_name, name_len)) {
3751                         *xe_index = i;
3752                         *found = 1;
3753                         ret = 0;
3754                         break;
3755                 }
3756         }
3757
3758         return ret;
3759 }
3760
3761 /*
3762  * Find the specified xattr entry in a series of buckets.
3763  * This series start from p_blkno and last for num_clusters.
3764  * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
3765  * the num of the valid buckets.
3766  *
3767  * Return the buffer_head this xattr should reside in. And if the xattr's
3768  * hash is in the gap of 2 buckets, return the lower bucket.
3769  */
3770 static int ocfs2_xattr_bucket_find(struct inode *inode,
3771                                    int name_index,
3772                                    const char *name,
3773                                    u32 name_hash,
3774                                    u64 p_blkno,
3775                                    u32 first_hash,
3776                                    u32 num_clusters,
3777                                    struct ocfs2_xattr_search *xs)
3778 {
3779         int ret, found = 0;
3780         struct ocfs2_xattr_header *xh = NULL;
3781         struct ocfs2_xattr_entry *xe = NULL;
3782         u16 index = 0;
3783         u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3784         int low_bucket = 0, bucket, high_bucket;
3785         struct ocfs2_xattr_bucket *search;
3786         u32 last_hash;
3787         u64 blkno, lower_blkno = 0;
3788
3789         search = ocfs2_xattr_bucket_new(inode);
3790         if (!search) {
3791                 ret = -ENOMEM;
3792                 mlog_errno(ret);
3793                 goto out;
3794         }
3795
3796         ret = ocfs2_read_xattr_bucket(search, p_blkno);
3797         if (ret) {
3798                 mlog_errno(ret);
3799                 goto out;
3800         }
3801
3802         xh = bucket_xh(search);
3803         high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
3804         while (low_bucket <= high_bucket) {
3805                 ocfs2_xattr_bucket_relse(search);
3806
3807                 bucket = (low_bucket + high_bucket) / 2;
3808                 blkno = p_blkno + bucket * blk_per_bucket;
3809                 ret = ocfs2_read_xattr_bucket(search, blkno);
3810                 if (ret) {
3811                         mlog_errno(ret);
3812                         goto out;
3813                 }
3814
3815                 xh = bucket_xh(search);
3816                 xe = &xh->xh_entries[0];
3817                 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
3818                         high_bucket = bucket - 1;
3819                         continue;
3820                 }
3821
3822                 /*
3823                  * Check whether the hash of the last entry in our
3824                  * bucket is larger than the search one. for an empty
3825                  * bucket, the last one is also the first one.
3826                  */
3827                 if (xh->xh_count)
3828                         xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
3829
3830                 last_hash = le32_to_cpu(xe->xe_name_hash);
3831
3832                 /* record lower_blkno which may be the insert place. */
3833                 lower_blkno = blkno;
3834
3835                 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
3836                         low_bucket = bucket + 1;
3837                         continue;
3838                 }
3839
3840                 /* the searched xattr should reside in this bucket if exists. */
3841                 ret = ocfs2_find_xe_in_bucket(inode, search,
3842                                               name_index, name, name_hash,
3843                                               &index, &found);
3844                 if (ret) {
3845                         mlog_errno(ret);
3846                         goto out;
3847                 }
3848                 break;
3849         }
3850
3851         /*
3852          * Record the bucket we have found.
3853          * When the xattr's hash value is in the gap of 2 buckets, we will
3854          * always set it to the previous bucket.
3855          */
3856         if (!lower_blkno)
3857                 lower_blkno = p_blkno;
3858
3859         /* This should be in cache - we just read it during the search */
3860         ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
3861         if (ret) {
3862                 mlog_errno(ret);
3863                 goto out;
3864         }
3865
3866         xs->header = bucket_xh(xs->bucket);
3867         xs->base = bucket_block(xs->bucket, 0);
3868         xs->end = xs->base + inode->i_sb->s_blocksize;
3869
3870         if (found) {
3871                 xs->here = &xs->header->xh_entries[index];
3872                 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
3873                      (unsigned long long)bucket_blkno(xs->bucket), index);
3874         } else
3875                 ret = -ENODATA;
3876
3877 out:
3878         ocfs2_xattr_bucket_free(search);
3879         return ret;
3880 }
3881
3882 static int ocfs2_xattr_index_block_find(struct inode *inode,
3883                                         struct buffer_head *root_bh,
3884                                         int name_index,
3885                                         const char *name,
3886                                         struct ocfs2_xattr_search *xs)
3887 {
3888         int ret;
3889         struct ocfs2_xattr_block *xb =
3890                         (struct ocfs2_xattr_block *)root_bh->b_data;
3891         struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3892         struct ocfs2_extent_list *el = &xb_root->xt_list;
3893         u64 p_blkno = 0;
3894         u32 first_hash, num_clusters = 0;
3895         u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
3896
3897         if (le16_to_cpu(el->l_next_free_rec) == 0)
3898                 return -ENODATA;
3899
3900         mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
3901              name, name_hash, name_index);
3902
3903         ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
3904                                   &num_clusters, el);
3905         if (ret) {
3906                 mlog_errno(ret);
3907                 goto out;
3908         }
3909
3910         BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
3911
3912         mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
3913              "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
3914              first_hash);
3915
3916         ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
3917                                       p_blkno, first_hash, num_clusters, xs);
3918
3919 out:
3920         return ret;
3921 }
3922
3923 static int ocfs2_iterate_xattr_buckets(struct inode *inode,
3924                                        u64 blkno,
3925                                        u32 clusters,
3926                                        xattr_bucket_func *func,
3927                                        void *para)
3928 {
3929         int i, ret = 0;
3930         u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
3931         u32 num_buckets = clusters * bpc;
3932         struct ocfs2_xattr_bucket *bucket;
3933
3934         bucket = ocfs2_xattr_bucket_new(inode);
3935         if (!bucket) {
3936                 mlog_errno(-ENOMEM);
3937                 return -ENOMEM;
3938         }
3939
3940         mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
3941              clusters, (unsigned long long)blkno);
3942
3943         for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
3944                 ret = ocfs2_read_xattr_bucket(bucket, blkno);
3945                 if (ret) {
3946                         mlog_errno(ret);
3947                         break;
3948                 }
3949
3950                 /*
3951                  * The real bucket num in this series of blocks is stored
3952                  * in the 1st bucket.
3953                  */
3954                 if (i == 0)
3955                         num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
3956
3957                 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
3958                      (unsigned long long)blkno,
3959                      le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
3960                 if (func) {
3961                         ret = func(inode, bucket, para);
3962                         if (ret && ret != -ERANGE)
3963                                 mlog_errno(ret);
3964                         /* Fall through to bucket_relse() */
3965                 }
3966
3967                 ocfs2_xattr_bucket_relse(bucket);
3968                 if (ret)
3969                         break;
3970         }
3971
3972         ocfs2_xattr_bucket_free(bucket);
3973         return ret;
3974 }
3975
3976 struct ocfs2_xattr_tree_list {
3977         char *buffer;
3978         size_t buffer_size;
3979         size_t result;
3980 };
3981
3982 static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
3983                                              struct ocfs2_xattr_header *xh,
3984                                              int index,
3985                                              int *block_off,
3986                                              int *new_offset)
3987 {
3988         u16 name_offset;
3989
3990         if (index < 0 || index >= le16_to_cpu(xh->xh_count))
3991                 return -EINVAL;
3992
3993         name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
3994
3995         *block_off = name_offset >> sb->s_blocksize_bits;
3996         *new_offset = name_offset % sb->s_blocksize;
3997
3998         return 0;
3999 }
4000
4001 static int ocfs2_list_xattr_bucket(struct inode *inode,
4002                                    struct ocfs2_xattr_bucket *bucket,
4003                                    void *para)
4004 {
4005         int ret = 0, type;
4006         struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
4007         int i, block_off, new_offset;
4008         const char *prefix, *name;
4009
4010         for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
4011                 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
4012                 type = ocfs2_xattr_get_type(entry);
4013                 prefix = ocfs2_xattr_prefix(type);
4014
4015                 if (prefix) {
4016                         ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
4017                                                                 bucket_xh(bucket),
4018                                                                 i,
4019                                                                 &block_off,
4020                                                                 &new_offset);
4021                         if (ret)
4022                                 break;
4023
4024                         name = (const char *)bucket_block(bucket, block_off) +
4025                                 new_offset;
4026                         ret = ocfs2_xattr_list_entry(xl->buffer,
4027                                                      xl->buffer_size,
4028                                                      &xl->result,
4029                                                      prefix, name,
4030                                                      entry->xe_name_len);
4031                         if (ret)
4032                                 break;
4033                 }
4034         }
4035
4036         return ret;
4037 }
4038
4039 static int ocfs2_iterate_xattr_index_block(struct inode *inode,
4040                                            struct buffer_head *blk_bh,
4041                                            xattr_tree_rec_func *rec_func,
4042                                            void *para)
4043 {
4044         struct ocfs2_xattr_block *xb =
4045                         (struct ocfs2_xattr_block *)blk_bh->b_data;
4046         struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
4047         int ret = 0;
4048         u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
4049         u64 p_blkno = 0;
4050
4051         if (!el->l_next_free_rec || !rec_func)
4052                 return 0;
4053
4054         while (name_hash > 0) {
4055                 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
4056                                           &e_cpos, &num_clusters, el);
4057                 if (ret) {
4058                         mlog_errno(ret);
4059                         break;
4060                 }
4061
4062                 ret = rec_func(inode, blk_bh, p_blkno, e_cpos,
4063                                num_clusters, para);
4064                 if (ret) {
4065                         if (ret != -ERANGE)
4066                                 mlog_errno(ret);
4067                         break;
4068                 }
4069
4070                 if (e_cpos == 0)
4071                         break;
4072
4073                 name_hash = e_cpos - 1;
4074         }
4075
4076         return ret;
4077
4078 }
4079
4080 static int ocfs2_list_xattr_tree_rec(struct inode *inode,
4081                                      struct buffer_head *root_bh,
4082                                      u64 blkno, u32 cpos, u32 len, void *para)
4083 {
4084         return ocfs2_iterate_xattr_buckets(inode, blkno, len,
4085                                            ocfs2_list_xattr_bucket, para);
4086 }
4087
4088 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
4089                                              struct buffer_head *blk_bh,
4090                                              char *buffer,
4091                                              size_t buffer_size)
4092 {
4093         int ret;
4094         struct ocfs2_xattr_tree_list xl = {
4095                 .buffer = buffer,
4096                 .buffer_size = buffer_size,
4097                 .result = 0,
4098         };
4099
4100         ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
4101                                               ocfs2_list_xattr_tree_rec, &xl);
4102         if (ret) {
4103                 mlog_errno(ret);
4104                 goto out;
4105         }
4106
4107         ret = xl.result;
4108 out:
4109         return ret;
4110 }
4111
4112 static int cmp_xe(const void *a, const void *b)
4113 {
4114         const struct ocfs2_xattr_entry *l = a, *r = b;
4115         u32 l_hash = le32_to_cpu(l->xe_name_hash);
4116         u32 r_hash = le32_to_cpu(r->xe_name_hash);
4117
4118         if (l_hash > r_hash)
4119                 return 1;
4120         if (l_hash < r_hash)
4121                 return -1;
4122         return 0;
4123 }
4124
4125 static void swap_xe(void *a, void *b, int size)
4126 {
4127         struct ocfs2_xattr_entry *l = a, *r = b, tmp;
4128
4129         tmp = *l;
4130         memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
4131         memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
4132 }
4133
4134 /*
4135  * When the ocfs2_xattr_block is filled up, new bucket will be created
4136  * and all the xattr entries will be moved to the new bucket.
4137  * The header goes at the start of the bucket, and the names+values are
4138  * filled from the end.  This is why *target starts as the last buffer.
4139  * Note: we need to sort the entries since they are not saved in order
4140  * in the ocfs2_xattr_block.
4141  */
4142 static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
4143                                            struct buffer_head *xb_bh,
4144                                            struct ocfs2_xattr_bucket *bucket)
4145 {
4146         int i, blocksize = inode->i_sb->s_blocksize;
4147         int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4148         u16 offset, size, off_change;
4149         struct ocfs2_xattr_entry *xe;
4150         struct ocfs2_xattr_block *xb =
4151                                 (struct ocfs2_xattr_block *)xb_bh->b_data;
4152         struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
4153         struct ocfs2_xattr_header *xh = bucket_xh(bucket);
4154         u16 count = le16_to_cpu(xb_xh->xh_count);
4155         char *src = xb_bh->b_data;
4156         char *target = bucket_block(bucket, blks - 1);
4157
4158         mlog(0, "cp xattr from block %llu to bucket %llu\n",
4159              (unsigned long long)xb_bh->b_blocknr,
4160              (unsigned long long)bucket_blkno(bucket));
4161
4162         for (i = 0; i < blks; i++)
4163                 memset(bucket_block(bucket, i), 0, blocksize);
4164
4165         /*
4166          * Since the xe_name_offset is based on ocfs2_xattr_header,
4167          * there is a offset change corresponding to the change of
4168          * ocfs2_xattr_header's position.
4169          */
4170         off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
4171         xe = &xb_xh->xh_entries[count - 1];
4172         offset = le16_to_cpu(xe->xe_name_offset) + off_change;
4173         size = blocksize - offset;
4174
4175         /* copy all the names and values. */
4176         memcpy(target + offset, src + offset, size);
4177
4178         /* Init new header now. */
4179         xh->xh_count = xb_xh->xh_count;
4180         xh->xh_num_buckets = cpu_to_le16(1);
4181         xh->xh_name_value_len = cpu_to_le16(size);
4182         xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
4183
4184         /* copy all the entries. */
4185         target = bucket_block(bucket, 0);
4186         offset = offsetof(struct ocfs2_xattr_header, xh_entries);
4187         size = count * sizeof(struct ocfs2_xattr_entry);
4188         memcpy(target + offset, (char *)xb_xh + offset, size);
4189
4190         /* Change the xe offset for all the xe because of the move. */
4191         off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
4192                  offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
4193         for (i = 0; i < count; i++)
4194                 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
4195
4196         mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
4197              offset, size, off_change);
4198
4199         sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
4200              cmp_xe, swap_xe);
4201 }
4202
4203 /*
4204  * After we move xattr from block to index btree, we have to
4205  * update ocfs2_xattr_search to the new xe and base.
4206  *
4207  * When the entry is in xattr block, xattr_bh indicates the storage place.
4208  * While if the entry is in index b-tree, "bucket" indicates the
4209  * real place of the xattr.
4210  */
4211 static void ocfs2_xattr_update_xattr_search(struct inode *inode,
4212                                             struct ocfs2_xattr_search *xs,
4213                                             struct buffer_head *old_bh)
4214 {
4215         char *buf = old_bh->b_data;
4216         struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
4217         struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
4218         int i;
4219
4220         xs->header = bucket_xh(xs->bucket);
4221         xs->base = bucket_block(xs->bucket, 0);
4222         xs->end = xs->base + inode->i_sb->s_blocksize;
4223
4224         if (xs->not_found)
4225                 return;
4226
4227         i = xs->here - old_xh->xh_entries;
4228         xs->here = &xs->header->xh_entries[i];
4229 }
4230
4231 static int ocfs2_xattr_create_index_block(struct inode *inode,
4232                                           struct ocfs2_xattr_search *xs,
4233                                           struct ocfs2_xattr_set_ctxt *ctxt)
4234 {
4235         int ret;
4236         u32 bit_off, len;
4237         u64 blkno;
4238         handle_t *handle = ctxt->handle;
4239         struct ocfs2_inode_info *oi = OCFS2_I(inode);
4240         struct buffer_head *xb_bh = xs->xattr_bh;
4241         struct ocfs2_xattr_block *xb =
4242                         (struct ocfs2_xattr_block *)xb_bh->b_data;
4243         struct ocfs2_xattr_tree_root *xr;
4244         u16 xb_flags = le16_to_cpu(xb->xb_flags);
4245
4246         mlog(0, "create xattr index block for %llu\n",
4247              (unsigned long long)xb_bh->b_blocknr);
4248
4249         BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
4250         BUG_ON(!xs->bucket);
4251
4252         /*
4253          * XXX:
4254          * We can use this lock for now, and maybe move to a dedicated mutex
4255          * if performance becomes a problem later.
4256          */
4257         down_write(&oi->ip_alloc_sem);
4258
4259         ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), xb_bh,
4260                                       OCFS2_JOURNAL_ACCESS_WRITE);
4261         if (ret) {
4262                 mlog_errno(ret);
4263                 goto out;
4264         }
4265
4266         ret = __ocfs2_claim_clusters(handle, ctxt->data_ac,
4267                                      1, 1, &bit_off, &len);
4268         if (ret) {
4269                 mlog_errno(ret);
4270                 goto out;
4271         }
4272
4273         /*
4274          * The bucket may spread in many blocks, and
4275          * we will only touch the 1st block and the last block
4276          * in the whole bucket(one for entry and one for data).
4277          */
4278         blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
4279
4280         mlog(0, "allocate 1 cluster from %llu to xattr block\n",
4281              (unsigned long long)blkno);
4282
4283         ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
4284         if (ret) {
4285                 mlog_errno(ret);
4286                 goto out;
4287         }
4288
4289         ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
4290                                                 OCFS2_JOURNAL_ACCESS_CREATE);
4291         if (ret) {
4292                 mlog_errno(ret);
4293                 goto out;
4294         }
4295
4296         ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
4297         ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
4298
4299         ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
4300
4301         /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
4302         memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
4303                offsetof(struct ocfs2_xattr_block, xb_attrs));
4304
4305         xr = &xb->xb_attrs.xb_root;
4306         xr->xt_clusters = cpu_to_le32(1);
4307         xr->xt_last_eb_blk = 0;
4308         xr->xt_list.l_tree_depth = 0;
4309         xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
4310         xr->xt_list.l_next_free_rec = cpu_to_le16(1);
4311
4312         xr->xt_list.l_recs[0].e_cpos = 0;
4313         xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
4314         xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
4315
4316         xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
4317
4318         ocfs2_journal_dirty(handle, xb_bh);
4319
4320 out:
4321         up_write(&oi->ip_alloc_sem);
4322
4323         return ret;
4324 }
4325
4326 static int cmp_xe_offset(const void *a, const void *b)
4327 {
4328         const struct ocfs2_xattr_entry *l = a, *r = b;
4329         u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
4330         u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
4331
4332         if (l_name_offset < r_name_offset)
4333                 return 1;
4334         if (l_name_offset > r_name_offset)
4335                 return -1;
4336         return 0;
4337 }
4338
4339 /*
4340  * defrag a xattr bucket if we find that the bucket has some
4341  * holes beteen name/value pairs.
4342  * We will move all the name/value pairs to the end of the bucket
4343  * so that we can spare some space for insertion.
4344  */
4345 static int ocfs2_defrag_xattr_bucket(struct inode *inode,
4346                                      handle_t *handle,
4347                                      struct ocfs2_xattr_bucket *bucket)
4348 {
4349         int ret, i;
4350         size_t end, offset, len;
4351         struct ocfs2_xattr_header *xh;
4352         char *entries, *buf, *bucket_buf = NULL;
4353         u64 blkno = bucket_blkno(bucket);
4354         u16 xh_free_start;
4355         size_t blocksize = inode->i_sb->s_blocksize;
4356         struct ocfs2_xattr_entry *xe;
4357
4358         /*
4359          * In order to make the operation more efficient and generic,
4360          * we copy all the blocks into a contiguous memory and do the
4361          * defragment there, so if anything is error, we will not touch
4362          * the real block.
4363          */
4364         bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
4365         if (!bucket_buf) {
4366                 ret = -EIO;
4367                 goto out;
4368         }
4369
4370         buf = bucket_buf;
4371         for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4372                 memcpy(buf, bucket_block(bucket, i), blocksize);
4373
4374         ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
4375                                                 OCFS2_JOURNAL_ACCESS_WRITE);
4376         if (ret < 0) {
4377                 mlog_errno(ret);
4378                 goto out;
4379         }
4380
4381         xh = (struct ocfs2_xattr_header *)bucket_buf;
4382         entries = (char *)xh->xh_entries;
4383         xh_free_start = le16_to_cpu(xh->xh_free_start);
4384
4385         mlog(0, "adjust xattr bucket in %llu, count = %u, "
4386              "xh_free_start = %u, xh_name_value_len = %u.\n",
4387              (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
4388              xh_free_start, le16_to_cpu(xh->xh_name_value_len));
4389
4390         /*
4391          * sort all the entries by their offset.
4392          * the largest will be the first, so that we can
4393          * move them to the end one by one.
4394          */
4395         sort(entries, le16_to_cpu(xh->xh_count),
4396              sizeof(struct ocfs2_xattr_entry),
4397              cmp_xe_offset, swap_xe);
4398
4399         /* Move all name/values to the end of the bucket. */
4400         xe = xh->xh_entries;
4401         end = OCFS2_XATTR_BUCKET_SIZE;
4402         for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
4403                 offset = le16_to_cpu(xe->xe_name_offset);
4404                 len = namevalue_size_xe(xe);
4405
4406                 /*
4407                  * We must make sure that the name/value pair
4408                  * exist in the same block. So adjust end to
4409                  * the previous block end if needed.
4410                  */
4411                 if (((end - len) / blocksize !=
4412                         (end - 1) / blocksize))
4413                         end = end - end % blocksize;
4414
4415                 if (end > offset + len) {
4416                         memmove(bucket_buf + end - len,
4417                                 bucket_buf + offset, len);
4418                         xe->xe_name_offset = cpu_to_le16(end - len);
4419                 }
4420
4421                 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
4422                                 "bucket %llu\n", (unsigned long long)blkno);
4423
4424                 end -= len;
4425         }
4426
4427         mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
4428                         "bucket %llu\n", (unsigned long long)blkno);
4429
4430         if (xh_free_start == end)
4431                 goto out;
4432
4433         memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
4434         xh->xh_free_start = cpu_to_le16(end);
4435
4436         /* sort the entries by their name_hash. */
4437         sort(entries, le16_to_cpu(xh->xh_count),
4438              sizeof(struct ocfs2_xattr_entry),
4439              cmp_xe, swap_xe);
4440
4441         buf = bucket_buf;
4442         for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4443                 memcpy(bucket_block(bucket, i), buf, blocksize);
4444         ocfs2_xattr_bucket_journal_dirty(handle, bucket);
4445
4446 out:
4447         kfree(bucket_buf);
4448         return ret;
4449 }
4450
4451 /*
4452  * prev_blkno points to the start of an existing extent.  new_blkno
4453  * points to a newly allocated extent.  Because we know each of our
4454  * clusters contains more than bucket, we can easily split one cluster
4455  * at a bucket boundary.  So we take the last cluster of the existing
4456  * extent and split it down the middle.  We move the last half of the
4457  * buckets in the last cluster of the existing extent over to the new
4458  * extent.
4459  *
4460  * first_bh is the buffer at prev_blkno so we can update the existing
4461  * extent's bucket count.  header_bh is the bucket were we were hoping
4462  * to insert our xattr.  If the bucket move places the target in the new
4463  * extent, we'll update first_bh and header_bh after modifying the old
4464  * extent.
4465  *
4466  * first_hash will be set as the 1st xe's name_hash in the new extent.
4467  */
4468 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
4469                                                handle_t *handle,
4470                                                struct ocfs2_xattr_bucket *first,
4471                                                struct ocfs2_xattr_bucket *target,
4472                                                u64 new_blkno,
4473                                                u32 num_clusters,
4474                                                u32 *first_hash)
4475 {
4476         int ret;
4477         struct super_block *sb = inode->i_sb;
4478         int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb);
4479         int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
4480         int to_move = num_buckets / 2;
4481         u64 src_blkno;
4482         u64 last_cluster_blkno = bucket_blkno(first) +
4483                 ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1));
4484
4485         BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets);
4486         BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize);
4487
4488         mlog(0, "move half of xattrs in cluster %llu to %llu\n",
4489              (unsigned long long)last_cluster_blkno, (unsigned long long)new_blkno);
4490
4491         ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first),
4492                                      last_cluster_blkno, new_blkno,
4493                                      to_move, first_hash);
4494         if (ret) {
4495                 mlog_errno(ret);
4496                 goto out;
4497         }
4498
4499         /* This is the first bucket that got moved */
4500         src_blkno = last_cluster_blkno + (to_move * blks_per_bucket);
4501
4502         /*
4503          * If the target bucket was part of the moved buckets, we need to
4504          * update first and target.
4505          */
4506         if (bucket_blkno(target) >= src_blkno) {
4507                 /* Find the block for the new target bucket */
4508                 src_blkno = new_blkno +
4509                         (bucket_blkno(target) - src_blkno);
4510
4511                 ocfs2_xattr_bucket_relse(first);
4512                 ocfs2_xattr_bucket_relse(target);
4513
4514                 /*
4515                  * These shouldn't fail - the buffers are in the
4516                  * journal from ocfs2_cp_xattr_bucket().
4517                  */
4518                 ret = ocfs2_read_xattr_bucket(first, new_blkno);
4519                 if (ret) {
4520                         mlog_errno(ret);
4521                         goto out;
4522                 }
4523                 ret = ocfs2_read_xattr_bucket(target, src_blkno);
4524                 if (ret)
4525                         mlog_errno(ret);
4526
4527         }
4528
4529 out:
4530         return ret;
4531 }
4532
4533 /*
4534  * Find the suitable pos when we divide a bucket into 2.
4535  * We have to make sure the xattrs with the same hash value exist
4536  * in the same bucket.
4537  *
4538  * If this ocfs2_xattr_header covers more than one hash value, find a
4539  * place where the hash value changes.  Try to find the most even split.
4540  * The most common case is that all entries have different hash values,
4541  * and the first check we make will find a place to split.
4542  */
4543 static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
4544 {
4545         struct ocfs2_xattr_entry *entries = xh->xh_entries;
4546         int count = le16_to_cpu(xh->xh_count);
4547         int delta, middle = count / 2;
4548
4549         /*
4550          * We start at the middle.  Each step gets farther away in both
4551          * directions.  We therefore hit the change in hash value
4552          * nearest to the middle.  Note that this loop does not execute for
4553          * count < 2.
4554          */
4555         for (delta = 0; delta < middle; delta++) {
4556                 /* Let's check delta earlier than middle */
4557                 if (cmp_xe(&entries[middle - delta - 1],
4558                            &entries[middle - delta]))
4559                         return middle - delta;
4560
4561                 /* For even counts, don't walk off the end */
4562                 if ((middle + delta + 1) == count)
4563                         continue;
4564
4565                 /* Now try delta past middle */
4566                 if (cmp_xe(&entries[middle + delta],
4567                            &entries[middle + delta + 1]))
4568                         return middle + delta + 1;
4569         }
4570
4571         /* Every entry had the same hash */
4572         return count;
4573 }
4574
4575 /*
4576  * Move some xattrs in old bucket(blk) to new bucket(new_blk).
4577  * first_hash will record the 1st hash of the new bucket.
4578  *
4579  * Normally half of the xattrs will be moved.  But we have to make
4580  * sure that the xattrs with the same hash value are stored in the
4581  * same bucket. If all the xattrs in this bucket have the same hash
4582  * value, the new bucket will be initialized as an empty one and the
4583  * first_hash will be initialized as (hash_value+1).
4584  */
4585 static int ocfs2_divide_xattr_bucket(struct inode *inode,
4586                                     handle_t *handle,
4587                                     u64 blk,
4588                                     u64 new_blk,
4589                                     u32 *first_hash,
4590                                     int new_bucket_head)
4591 {
4592         int ret, i;
4593         int count, start, len, name_value_len = 0, name_offset = 0;
4594         struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
4595         struct ocfs2_xattr_header *xh;
4596         struct ocfs2_xattr_entry *xe;
4597         int blocksize = inode->i_sb->s_blocksize;
4598
4599         mlog(0, "move some of xattrs from bucket %llu to %llu\n",
4600              (unsigned long long)blk, (unsigned long long)new_blk);
4601
4602         s_bucket = ocfs2_xattr_bucket_new(inode);
4603         t_bucket = ocfs2_xattr_bucket_new(inode);
4604         if (!s_bucket || !t_bucket) {
4605                 ret = -ENOMEM;
4606                 mlog_errno(ret);
4607                 goto out;
4608         }
4609
4610         ret = ocfs2_read_xattr_bucket(s_bucket, blk);
4611         if (ret) {
4612                 mlog_errno(ret);
4613                 goto out;
4614         }
4615
4616         ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
4617                                                 OCFS2_JOURNAL_ACCESS_WRITE);
4618         if (ret) {
4619                 mlog_errno(ret);
4620                 goto out;
4621         }
4622
4623         /*
4624          * Even if !new_bucket_head, we're overwriting t_bucket.  Thus,
4625          * there's no need to read it.
4626          */
4627         ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
4628         if (ret) {
4629                 mlog_errno(ret);
4630                 goto out;
4631         }
4632
4633         /*
4634          * Hey, if we're overwriting t_bucket, what difference does
4635          * ACCESS_CREATE vs ACCESS_WRITE make?  See the comment in the
4636          * same part of ocfs2_cp_xattr_bucket().
4637          */
4638         ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
4639                                                 new_bucket_head ?
4640                                                 OCFS2_JOURNAL_ACCESS_CREATE :
4641                                                 OCFS2_JOURNAL_ACCESS_WRITE);
4642         if (ret) {
4643                 mlog_errno(ret);
4644                 goto out;
4645         }
4646
4647         xh = bucket_xh(s_bucket);
4648         count = le16_to_cpu(xh->xh_count);
4649         start = ocfs2_xattr_find_divide_pos(xh);
4650
4651         if (start == count) {
4652                 xe = &xh->xh_entries[start-1];
4653
4654                 /*
4655                  * initialized a new empty bucket here.
4656                  * The hash value is set as one larger than
4657                  * that of the last entry in the previous bucket.
4658                  */
4659                 for (i = 0; i < t_bucket->bu_blocks; i++)
4660                         memset(bucket_block(t_bucket, i), 0, blocksize);
4661
4662                 xh = bucket_xh(t_bucket);
4663                 xh->xh_free_start = cpu_to_le16(blocksize);
4664                 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
4665                 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
4666
4667                 goto set_num_buckets;
4668         }
4669
4670         /* copy the whole bucket to the new first. */
4671         ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4672
4673         /* update the new bucket. */
4674         xh = bucket_xh(t_bucket);
4675
4676         /*
4677          * Calculate the total name/value len and xh_free_start for
4678          * the old bucket first.
4679          */
4680         name_offset = OCFS2_XATTR_BUCKET_SIZE;
4681         name_value_len = 0;
4682         for (i = 0; i < start; i++) {
4683                 xe = &xh->xh_entries[i];
4684                 name_value_len += namevalue_size_xe(xe);
4685                 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
4686                         name_offset = le16_to_cpu(xe->xe_name_offset);
4687         }
4688
4689         /*
4690          * Now begin the modification to the new bucket.
4691          *
4692          * In the new bucket, We just move the xattr entry to the beginning
4693          * and don't touch the name/value. So there will be some holes in the
4694          * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
4695          * called.
4696          */
4697         xe = &xh->xh_entries[start];
4698         len = sizeof(struct ocfs2_xattr_entry) * (count - start);
4699         mlog(0, "mv xattr entry len %d from %d to %d\n", len,
4700              (int)((char *)xe - (char *)xh),
4701              (int)((char *)xh->xh_entries - (char *)xh));
4702         memmove((char *)xh->xh_entries, (char *)xe, len);
4703         xe = &xh->xh_entries[count - start];
4704         len = sizeof(struct ocfs2_xattr_entry) * start;
4705         memset((char *)xe, 0, len);
4706
4707         le16_add_cpu(&xh->xh_count, -start);
4708         le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
4709
4710         /* Calculate xh_free_start for the new bucket. */
4711         xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4712         for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4713                 xe = &xh->xh_entries[i];
4714                 if (le16_to_cpu(xe->xe_name_offset) <
4715                     le16_to_cpu(xh->xh_free_start))
4716                         xh->xh_free_start = xe->xe_name_offset;
4717         }
4718
4719 set_num_buckets:
4720         /* set xh->xh_num_buckets for the new xh. */
4721         if (new_bucket_head)
4722                 xh->xh_num_buckets = cpu_to_le16(1);
4723         else
4724                 xh->xh_num_buckets = 0;
4725
4726         ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
4727
4728         /* store the first_hash of the new bucket. */
4729         if (first_hash)
4730                 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
4731
4732         /*
4733          * Now only update the 1st block of the old bucket.  If we
4734          * just added a new empty bucket, there is no need to modify
4735          * it.
4736          */
4737         if (start == count)
4738                 goto out;
4739
4740         xh = bucket_xh(s_bucket);
4741         memset(&xh->xh_entries[start], 0,
4742                sizeof(struct ocfs2_xattr_entry) * (count - start));
4743         xh->xh_count = cpu_to_le16(start);
4744         xh->xh_free_start = cpu_to_le16(name_offset);
4745         xh->xh_name_value_len = cpu_to_le16(name_value_len);
4746
4747         ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
4748
4749 out:
4750         ocfs2_xattr_bucket_free(s_bucket);
4751         ocfs2_xattr_bucket_free(t_bucket);
4752
4753         return ret;
4754 }
4755
4756 /*
4757  * Copy xattr from one bucket to another bucket.
4758  *
4759  * The caller must make sure that the journal transaction
4760  * has enough space for journaling.
4761  */
4762 static int ocfs2_cp_xattr_bucket(struct inode *inode,
4763                                  handle_t *handle,
4764                                  u64 s_blkno,
4765                                  u64 t_blkno,
4766                                  int t_is_new)
4767 {
4768         int ret;
4769         struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
4770
4771         BUG_ON(s_blkno == t_blkno);
4772
4773         mlog(0, "cp bucket %llu to %llu, target is %d\n",
4774              (unsigned long long)s_blkno, (unsigned long long)t_blkno,
4775              t_is_new);
4776
4777         s_bucket = ocfs2_xattr_bucket_new(inode);
4778         t_bucket = ocfs2_xattr_bucket_new(inode);
4779         if (!s_bucket || !t_bucket) {
4780                 ret = -ENOMEM;
4781                 mlog_errno(ret);
4782                 goto out;
4783         }
4784
4785         ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
4786         if (ret)
4787                 goto out;
4788
4789         /*
4790          * Even if !t_is_new, we're overwriting t_bucket.  Thus,
4791          * there's no need to read it.
4792          */
4793         ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
4794         if (ret)
4795                 goto out;
4796
4797         /*
4798          * Hey, if we're overwriting t_bucket, what difference does
4799          * ACCESS_CREATE vs ACCESS_WRITE make?  Well, if we allocated a new
4800          * cluster to fill, we came here from
4801          * ocfs2_mv_xattr_buckets(), and it is really new -
4802          * ACCESS_CREATE is required.  But we also might have moved data
4803          * out of t_bucket before extending back into it.
4804          * ocfs2_add_new_xattr_bucket() can do this - its call to
4805          * ocfs2_add_new_xattr_cluster() may have created a new extent
4806          * and copied out the end of the old extent.  Then it re-extends
4807          * the old extent back to create space for new xattrs.  That's
4808          * how we get here, and the bucket isn't really new.
4809          */
4810         ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
4811                                                 t_is_new ?
4812                                                 OCFS2_JOURNAL_ACCESS_CREATE :
4813                                                 OCFS2_JOURNAL_ACCESS_WRITE);
4814         if (ret)
4815                 goto out;
4816
4817         ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4818         ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
4819
4820 out:
4821         ocfs2_xattr_bucket_free(t_bucket);
4822         ocfs2_xattr_bucket_free(s_bucket);
4823
4824         return ret;
4825 }
4826
4827 /*
4828  * src_blk points to the start of an existing extent.  last_blk points to
4829  * last cluster in that extent.  to_blk points to a newly allocated
4830  * extent.  We copy the buckets from the cluster at last_blk to the new
4831  * extent.  If start_bucket is non-zero, we skip that many buckets before
4832  * we start copying.  The new extent's xh_num_buckets gets set to the
4833  * number of buckets we copied.  The old extent's xh_num_buckets shrinks
4834  * by the same amount.
4835  */
4836 static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
4837                                   u64 src_blk, u64 last_blk, u64 to_blk,
4838                                   unsigned int start_bucket,
4839                                   u32 *first_hash)
4840 {
4841         int i, ret, credits;
4842         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4843         int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4844         int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
4845         struct ocfs2_xattr_bucket *old_first, *new_first;
4846
4847         mlog(0, "mv xattrs from cluster %llu to %llu\n",
4848              (unsigned long long)last_blk, (unsigned long long)to_blk);
4849
4850         BUG_ON(start_bucket >= num_buckets);
4851         if (start_bucket) {
4852                 num_buckets -= start_bucket;
4853                 last_blk += (start_bucket * blks_per_bucket);
4854         }
4855
4856         /* The first bucket of the original extent */
4857         old_first = ocfs2_xattr_bucket_new(inode);
4858         /* The first bucket of the new extent */
4859         new_first = ocfs2_xattr_bucket_new(inode);
4860         if (!old_first || !new_first) {
4861                 ret = -ENOMEM;
4862                 mlog_errno(ret);
4863                 goto out;
4864         }
4865
4866         ret = ocfs2_read_xattr_bucket(old_first, src_blk);
4867         if (ret) {
4868                 mlog_errno(ret);
4869                 goto out;
4870         }
4871
4872         /*
4873          * We need to update the first bucket of the old extent and all
4874          * the buckets going to the new extent.
4875          */
4876         credits = ((num_buckets + 1) * blks_per_bucket);
4877         ret = ocfs2_extend_trans(handle, credits);
4878         if (ret) {
4879                 mlog_errno(ret);
4880                 goto out;
4881         }
4882
4883         ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
4884                                                 OCFS2_JOURNAL_ACCESS_WRITE);
4885         if (ret) {
4886                 mlog_errno(ret);
4887                 goto out;
4888         }
4889
4890         for (i = 0; i < num_buckets; i++) {
4891                 ret = ocfs2_cp_xattr_bucket(inode, handle,
4892                                             last_blk + (i * blks_per_bucket),
4893                                             to_blk + (i * blks_per_bucket),
4894                                             1);
4895                 if (ret) {
4896                         mlog_errno(ret);
4897                         goto out;
4898                 }
4899         }
4900
4901         /*
4902          * Get the new bucket ready before we dirty anything
4903          * (This actually shouldn't fail, because we already dirtied
4904          * it once in ocfs2_cp_xattr_bucket()).
4905          */
4906         ret = ocfs2_read_xattr_bucket(new_first, to_blk);
4907         if (ret) {
4908                 mlog_errno(ret);
4909                 goto out;
4910         }
4911         ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
4912                                                 OCFS2_JOURNAL_ACCESS_WRITE);
4913         if (ret) {
4914                 mlog_errno(ret);
4915                 goto out;
4916         }
4917
4918         /* Now update the headers */
4919         le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets);
4920         ocfs2_xattr_bucket_journal_dirty(handle, old_first);
4921
4922         bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets);
4923         ocfs2_xattr_bucket_journal_dirty(handle, new_first);
4924
4925         if (first_hash)
4926                 *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash);
4927
4928 out:
4929         ocfs2_xattr_bucket_free(new_first);
4930         ocfs2_xattr_bucket_free(old_first);
4931         return ret;
4932 }
4933
4934 /*
4935  * Move some xattrs in this cluster to the new cluster.
4936  * This function should only be called when bucket size == cluster size.
4937  * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
4938  */
4939 static int ocfs2_divide_xattr_cluster(struct inode *inode,
4940                                       handle_t *handle,
4941                                       u64 prev_blk,
4942                                       u64 new_blk,
4943                                       u32 *first_hash)
4944 {
4945         u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4946         int ret, credits = 2 * blk_per_bucket;
4947
4948         BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
4949
4950         ret = ocfs2_extend_trans(handle, credits);
4951         if (ret) {
4952                 mlog_errno(ret);
4953                 return ret;
4954         }
4955
4956         /* Move half of the xattr in start_blk to the next bucket. */
4957         return  ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
4958                                           new_blk, first_hash, 1);
4959 }
4960
4961 /*
4962  * Move some xattrs from the old cluster to the new one since they are not
4963  * contiguous in ocfs2 xattr tree.
4964  *
4965  * new_blk starts a new separate cluster, and we will move some xattrs from
4966  * prev_blk to it. v_start will be set as the first name hash value in this
4967  * new cluster so that it can be used as e_cpos during tree insertion and
4968  * don't collide with our original b-tree operations. first_bh and header_bh
4969  * will also be updated since they will be used in ocfs2_extend_xattr_bucket
4970  * to extend the insert bucket.
4971  *
4972  * The problem is how much xattr should we move to the new one and when should
4973  * we update first_bh and header_bh?
4974  * 1. If cluster size > bucket size, that means the previous cluster has more
4975  *    than 1 bucket, so just move half nums of bucket into the new cluster and
4976  *    update the first_bh and header_bh if the insert bucket has been moved
4977  *    to the new cluster.
4978  * 2. If cluster_size == bucket_size:
4979  *    a) If the previous extent rec has more than one cluster and the insert
4980  *       place isn't in the last cluster, copy the entire last cluster to the
4981  *       new one. This time, we don't need to upate the first_bh and header_bh
4982  *       since they will not be moved into the new cluster.
4983  *    b) Otherwise, move the bottom half of the xattrs in the last cluster into
4984  *       the new one. And we set the extend flag to zero if the insert place is
4985  *       moved into the new allocated cluster since no extend is needed.
4986  */
4987 static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
4988                                             handle_t *handle,
4989                                             struct ocfs2_xattr_bucket *first,
4990                                             struct ocfs2_xattr_bucket *target,
4991                                             u64 new_blk,
4992                                             u32 prev_clusters,
4993                                             u32 *v_start,
4994                                             int *extend)
4995 {
4996         int ret;
4997
4998         mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
4999              (unsigned long long)bucket_blkno(first), prev_clusters,
5000              (unsigned long long)new_blk);
5001
5002         if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) {
5003                 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
5004                                                           handle,
5005                                                           first, target,
5006                                                           new_blk,
5007                                                           prev_clusters,
5008                                                           v_start);
5009                 if (ret)
5010                         mlog_errno(ret);
5011         } else {
5012                 /* The start of the last cluster in the first extent */
5013                 u64 last_blk = bucket_blkno(first) +
5014                         ((prev_clusters - 1) *
5015                          ocfs2_clusters_to_blocks(inode->i_sb, 1));
5016
5017                 if (prev_clusters > 1 && bucket_blkno(target) != last_blk) {
5018                         ret = ocfs2_mv_xattr_buckets(inode, handle,
5019                                                      bucket_blkno(first),
5020                                                      last_blk, new_blk, 0,
5021                                                      v_start);
5022                         if (ret)
5023                                 mlog_errno(ret);
5024                 } else {
5025                         ret = ocfs2_divide_xattr_cluster(inode, handle,
5026                                                          last_blk, new_blk,
5027                                                          v_start);
5028                         if (ret)
5029                                 mlog_errno(ret);
5030
5031                         if ((bucket_blkno(target) == last_blk) && extend)
5032                                 *extend = 0;
5033                 }
5034         }
5035
5036         return ret;
5037 }
5038
5039 /*
5040  * Add a new cluster for xattr storage.
5041  *
5042  * If the new cluster is contiguous with the previous one, it will be
5043  * appended to the same extent record, and num_clusters will be updated.
5044  * If not, we will insert a new extent for it and move some xattrs in
5045  * the last cluster into the new allocated one.
5046  * We also need to limit the maximum size of a btree leaf, otherwise we'll
5047  * lose the benefits of hashing because we'll have to search large leaves.
5048  * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
5049  * if it's bigger).
5050  *
5051  * first_bh is the first block of the previous extent rec and header_bh
5052  * indicates the bucket we will insert the new xattrs. They will be updated
5053  * when the header_bh is moved into the new cluster.
5054  */
5055 static int ocfs2_add_new_xattr_cluster(struct inode *inode,
5056                                        struct buffer_head *root_bh,
5057                                        struct ocfs2_xattr_bucket *first,
5058                                        struct ocfs2_xattr_bucket *target,
5059                                        u32 *num_clusters,
5060                                        u32 prev_cpos,
5061                                        int *extend,
5062                                        struct ocfs2_xattr_set_ctxt *ctxt)
5063 {
5064         int ret;
5065         u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
5066         u32 prev_clusters = *num_clusters;
5067         u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
5068         u64 block;
5069         handle_t *handle = ctxt->handle;
5070         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5071         struct ocfs2_extent_tree et;
5072
5073         mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
5074              "previous xattr blkno = %llu\n",
5075              (unsigned long long)OCFS2_I(inode)->ip_blkno,
5076              prev_cpos, (unsigned long long)bucket_blkno(first));
5077
5078         ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
5079
5080         ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
5081                                       OCFS2_JOURNAL_ACCESS_WRITE);
5082         if (ret < 0) {
5083                 mlog_errno(ret);
5084                 goto leave;
5085         }
5086
5087         ret = __ocfs2_claim_clusters(handle, ctxt->data_ac, 1,
5088                                      clusters_to_add, &bit_off, &num_bits);
5089         if (ret < 0) {
5090                 if (ret != -ENOSPC)
5091                         mlog_errno(ret);
5092                 goto leave;
5093         }
5094
5095         BUG_ON(num_bits > clusters_to_add);
5096
5097         block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
5098         mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
5099              num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
5100
5101         if (bucket_blkno(first) + (prev_clusters * bpc) == block &&
5102             (prev_clusters + num_bits) << osb->s_clustersize_bits <=
5103              OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
5104                 /*
5105                  * If this cluster is contiguous with the old one and
5106                  * adding this new cluster, we don't surpass the limit of
5107                  * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
5108                  * initialized and used like other buckets in the previous
5109                  * cluster.
5110                  * So add it as a contiguous one. The caller will handle
5111                  * its init process.
5112                  */
5113                 v_start = prev_cpos + prev_clusters;
5114                 *num_clusters = prev_clusters + num_bits;
5115                 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
5116                      num_bits);
5117         } else {
5118                 ret = ocfs2_adjust_xattr_cross_cluster(inode,
5119                                                        handle,
5120                                                        first,
5121                                                        target,
5122                                                        block,
5123                                                        prev_clusters,
5124                                                        &v_start,
5125                                                        extend);
5126                 if (ret) {
5127                         mlog_errno(ret);
5128                         goto leave;
5129                 }
5130         }
5131
5132         mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
5133              num_bits, (unsigned long long)block, v_start);
5134         ret = ocfs2_insert_extent(handle, &et, v_start, block,
5135                                   num_bits, 0, ctxt->meta_ac);
5136         if (ret < 0) {
5137                 mlog_errno(ret);
5138                 goto leave;
5139         }
5140
5141         ocfs2_journal_dirty(handle, root_bh);
5142
5143 leave:
5144         return ret;
5145 }
5146
5147 /*
5148  * We are given an extent.  'first' is the bucket at the very front of
5149  * the extent.  The extent has space for an additional bucket past
5150  * bucket_xh(first)->xh_num_buckets.  'target_blkno' is the block number
5151  * of the target bucket.  We wish to shift every bucket past the target
5152  * down one, filling in that additional space.  When we get back to the
5153  * target, we split the target between itself and the now-empty bucket
5154  * at target+1 (aka, target_blkno + blks_per_bucket).
5155  */
5156 static int ocfs2_extend_xattr_bucket(struct inode *inode,
5157                                      handle_t *handle,
5158                                      struct ocfs2_xattr_bucket *first,
5159                                      u64 target_blk,
5160                                      u32 num_clusters)
5161 {
5162         int ret, credits;
5163         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5164         u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
5165         u64 end_blk;
5166         u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets);
5167
5168         mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
5169              "from %llu, len = %u\n", (unsigned long long)target_blk,
5170              (unsigned long long)bucket_blkno(first), num_clusters);
5171
5172         /* The extent must have room for an additional bucket */
5173         BUG_ON(new_bucket >=
5174                (num_clusters * ocfs2_xattr_buckets_per_cluster(osb)));
5175
5176         /* end_blk points to the last existing bucket */
5177         end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket);
5178
5179         /*
5180          * end_blk is the start of the last existing bucket.
5181          * Thus, (end_blk - target_blk) covers the target bucket and
5182          * every bucket after it up to, but not including, the last
5183          * existing bucket.  Then we add the last existing bucket, the
5184          * new bucket, and the first bucket (3 * blk_per_bucket).
5185          */
5186         credits = (end_blk - target_blk) + (3 * blk_per_bucket);
5187         ret = ocfs2_extend_trans(handle, credits);
5188         if (ret) {
5189                 mlog_errno(ret);
5190                 goto out;
5191         }
5192
5193         ret = ocfs2_xattr_bucket_journal_access(handle, first,
5194                                                 OCFS2_JOURNAL_ACCESS_WRITE);
5195         if (ret) {
5196                 mlog_errno(ret);
5197                 goto out;
5198         }
5199
5200         while (end_blk != target_blk) {
5201                 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
5202                                             end_blk + blk_per_bucket, 0);
5203                 if (ret)
5204                         goto out;
5205                 end_blk -= blk_per_bucket;
5206         }
5207
5208         /* Move half of the xattr in target_blkno to the next bucket. */
5209         ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk,
5210                                         target_blk + blk_per_bucket, NULL, 0);
5211
5212         le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1);
5213         ocfs2_xattr_bucket_journal_dirty(handle, first);
5214
5215 out:
5216         return ret;
5217 }
5218
5219 /*
5220  * Add new xattr bucket in an extent record and adjust the buckets
5221  * accordingly.  xb_bh is the ocfs2_xattr_block, and target is the
5222  * bucket we want to insert into.
5223  *
5224  * In the easy case, we will move all the buckets after target down by
5225  * one. Half of target's xattrs will be moved to the next bucket.
5226  *
5227  * If current cluster is full, we'll allocate a new one.  This may not
5228  * be contiguous.  The underlying calls will make sure that there is
5229  * space for the insert, shifting buckets around if necessary.
5230  * 'target' may be moved by those calls.
5231  */
5232 static int ocfs2_add_new_xattr_bucket(struct inode *inode,
5233                                       struct buffer_head *xb_bh,
5234                                       struct ocfs2_xattr_bucket *target,
5235                                       struct ocfs2_xattr_set_ctxt *ctxt)
5236 {
5237         struct ocfs2_xattr_block *xb =
5238                         (struct ocfs2_xattr_block *)xb_bh->b_data;
5239         struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
5240         struct ocfs2_extent_list *el = &xb_root->xt_list;
5241         u32 name_hash =
5242                 le32_to_cpu(bucket_xh(target)->xh_entries[0].xe_name_hash);
5243         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5244         int ret, num_buckets, extend = 1;
5245         u64 p_blkno;
5246         u32 e_cpos, num_clusters;
5247         /* The bucket at the front of the extent */
5248         struct ocfs2_xattr_bucket *first;
5249
5250         mlog(0, "Add new xattr bucket starting from %llu\n",
5251              (unsigned long long)bucket_blkno(target));
5252
5253         /* The first bucket of the original extent */
5254         first = ocfs2_xattr_bucket_new(inode);
5255         if (!first) {
5256                 ret = -ENOMEM;
5257                 mlog_errno(ret);
5258                 goto out;
5259         }
5260
5261         ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
5262                                   &num_clusters, el);
5263         if (ret) {
5264                 mlog_errno(ret);
5265                 goto out;
5266         }
5267
5268         ret = ocfs2_read_xattr_bucket(first, p_blkno);
5269         if (ret) {
5270                 mlog_errno(ret);
5271                 goto out;
5272         }
5273
5274         num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
5275         if (num_buckets == le16_to_cpu(bucket_xh(first)->xh_num_buckets)) {
5276                 /*
5277                  * This can move first+target if the target bucket moves
5278                  * to the new extent.
5279                  */
5280                 ret = ocfs2_add_new_xattr_cluster(inode,
5281                                                   xb_bh,
5282                                                   first,
5283                                                   target,
5284                                                   &num_clusters,
5285                                                   e_cpos,
5286                                                   &extend,
5287                                                   ctxt);
5288                 if (ret) {
5289                         mlog_errno(ret);
5290                         goto out;
5291                 }
5292         }
5293
5294         if (extend) {
5295                 ret = ocfs2_extend_xattr_bucket(inode,
5296                                                 ctxt->handle,
5297                                                 first,
5298                                                 bucket_blkno(target),
5299                                                 num_clusters);
5300                 if (ret)
5301                         mlog_errno(ret);
5302         }
5303
5304 out:
5305         ocfs2_xattr_bucket_free(first);
5306
5307         return ret;
5308 }
5309
5310 static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
5311                                         struct ocfs2_xattr_bucket *bucket,
5312                                         int offs)
5313 {
5314         int block_off = offs >> inode->i_sb->s_blocksize_bits;
5315
5316         offs = offs % inode->i_sb->s_blocksize;
5317         return bucket_block(bucket, block_off) + offs;
5318 }
5319
5320 /*
5321  * Truncate the specified xe_off entry in xattr bucket.
5322  * bucket is indicated by header_bh and len is the new length.
5323  * Both the ocfs2_xattr_value_root and the entry will be updated here.
5324  *
5325  * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
5326  */
5327 static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
5328                                              struct ocfs2_xattr_bucket *bucket,
5329                                              int xe_off,
5330                                              int len,
5331                                              struct ocfs2_xattr_set_ctxt *ctxt)
5332 {
5333         int ret, offset;
5334         u64 value_blk;
5335         struct ocfs2_xattr_entry *xe;
5336         struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5337         size_t blocksize = inode->i_sb->s_blocksize;
5338         struct ocfs2_xattr_value_buf vb = {
5339                 .vb_access = ocfs2_journal_access,
5340         };
5341
5342         xe = &xh->xh_entries[xe_off];
5343
5344         BUG_ON(!xe || ocfs2_xattr_is_local(xe));
5345
5346         offset = le16_to_cpu(xe->xe_name_offset) +
5347                  OCFS2_XATTR_SIZE(xe->xe_name_len);
5348
5349         value_blk = offset / blocksize;
5350
5351         /* We don't allow ocfs2_xattr_value to be stored in different block. */
5352         BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
5353
5354         vb.vb_bh = bucket->bu_bhs[value_blk];
5355         BUG_ON(!vb.vb_bh);
5356
5357         vb.vb_xv = (struct ocfs2_xattr_value_root *)
5358                 (vb.vb_bh->b_data + offset % blocksize);
5359
5360         /*
5361          * From here on out we have to dirty the bucket.  The generic
5362          * value calls only modify one of the bucket's bhs, but we need
5363          * to send the bucket at once.  So if they error, they *could* have
5364          * modified something.  We have to assume they did, and dirty
5365          * the whole bucket.  This leaves us in a consistent state.
5366          */
5367         mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
5368              xe_off, (unsigned long long)bucket_blkno(bucket), len);
5369         ret = ocfs2_xattr_value_truncate(inode, &vb, len, ctxt);
5370         if (ret) {
5371                 mlog_errno(ret);
5372                 goto out;
5373         }
5374
5375         ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
5376                                                 OCFS2_JOURNAL_ACCESS_WRITE);
5377         if (ret) {
5378                 mlog_errno(ret);
5379                 goto out;
5380         }
5381
5382         xe->xe_value_size = cpu_to_le64(len);
5383
5384         ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket);
5385
5386 out:
5387         return ret;
5388 }
5389
5390 static int ocfs2_rm_xattr_cluster(struct inode *inode,
5391                                   struct buffer_head *root_bh,
5392                                   u64 blkno,
5393                                   u32 cpos,
5394                                   u32 len,
5395                                   void *para)
5396 {
5397         int ret;
5398         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5399         struct inode *tl_inode = osb->osb_tl_inode;
5400         handle_t *handle;
5401         struct ocfs2_xattr_block *xb =
5402                         (struct ocfs2_xattr_block *)root_bh->b_data;
5403         struct ocfs2_alloc_context *meta_ac = NULL;
5404         struct ocfs2_cached_dealloc_ctxt dealloc;
5405         struct ocfs2_extent_tree et;
5406
5407         ret = ocfs2_iterate_xattr_buckets(inode, blkno, len,
5408                                           ocfs2_delete_xattr_in_bucket, para);
5409         if (ret) {
5410                 mlog_errno(ret);
5411                 return ret;
5412         }
5413
5414         ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
5415
5416         ocfs2_init_dealloc_ctxt(&dealloc);
5417
5418         mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
5419              cpos, len, (unsigned long long)blkno);
5420
5421         ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode), blkno,
5422                                                len);
5423
5424         ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
5425         if (ret) {
5426                 mlog_errno(ret);
5427                 return ret;
5428         }
5429
5430         mutex_lock(&tl_inode->i_mutex);
5431
5432         if (ocfs2_truncate_log_needs_flush(osb)) {
5433                 ret = __ocfs2_flush_truncate_log(osb);
5434                 if (ret < 0) {
5435                         mlog_errno(ret);
5436                         goto out;
5437                 }
5438         }
5439
5440         handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
5441         if (IS_ERR(handle)) {
5442                 ret = -ENOMEM;
5443                 mlog_errno(ret);
5444                 goto out;
5445         }
5446
5447         ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
5448                                       OCFS2_JOURNAL_ACCESS_WRITE);
5449         if (ret) {
5450                 mlog_errno(ret);
5451                 goto out_commit;
5452         }
5453
5454         ret = ocfs2_remove_extent(handle, &et, cpos, len, meta_ac,
5455                                   &dealloc);
5456         if (ret) {
5457                 mlog_errno(ret);
5458                 goto out_commit;
5459         }
5460
5461         le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
5462         ocfs2_journal_dirty(handle, root_bh);
5463
5464         ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
5465         if (ret)
5466                 mlog_errno(ret);
5467
5468 out_commit:
5469         ocfs2_commit_trans(osb, handle);
5470 out:
5471         ocfs2_schedule_truncate_log_flush(osb, 1);
5472
5473         mutex_unlock(&tl_inode->i_mutex);
5474
5475         if (meta_ac)
5476                 ocfs2_free_alloc_context(meta_ac);
5477
5478         ocfs2_run_deallocs(osb, &dealloc);
5479
5480         return ret;
5481 }
5482
5483 /*
5484  * check whether the xattr bucket is filled up with the same hash value.
5485  * If we want to insert the xattr with the same hash, return -ENOSPC.
5486  * If we want to insert a xattr with different hash value, go ahead
5487  * and ocfs2_divide_xattr_bucket will handle this.
5488  */
5489 static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
5490                                               struct ocfs2_xattr_bucket *bucket,
5491                                               const char *name)
5492 {
5493         struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5494         u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
5495
5496         if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
5497                 return 0;
5498
5499         if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
5500             xh->xh_entries[0].xe_name_hash) {
5501                 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
5502                      "hash = %u\n",
5503                      (unsigned long long)bucket_blkno(bucket),
5504                      le32_to_cpu(xh->xh_entries[0].xe_name_hash));
5505                 return -ENOSPC;
5506         }
5507
5508         return 0;
5509 }
5510
5511 /*
5512  * Try to set the entry in the current bucket.  If we fail, the caller
5513  * will handle getting us another bucket.
5514  */
5515 static int ocfs2_xattr_set_entry_bucket(struct inode *inode,
5516                                         struct ocfs2_xattr_info *xi,
5517                                         struct ocfs2_xattr_search *xs,
5518                                         struct ocfs2_xattr_set_ctxt *ctxt)
5519 {
5520         int ret;
5521         struct ocfs2_xa_loc loc;
5522
5523         mlog_entry("Set xattr %s in xattr bucket\n", xi->xi_name);
5524
5525         ocfs2_init_xattr_bucket_xa_loc(&loc, xs->bucket,
5526                                        xs->not_found ? NULL : xs->here);
5527         ret = ocfs2_xa_set(&loc, xi, ctxt);
5528         if (!ret) {
5529                 xs->here = loc.xl_entry;
5530                 goto out;
5531         }
5532         if (ret != -ENOSPC) {
5533                 mlog_errno(ret);
5534                 goto out;
5535         }
5536
5537         /* Ok, we need space.  Let's try defragmenting the bucket. */
5538         ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
5539                                         xs->bucket);
5540         if (ret) {
5541                 mlog_errno(ret);
5542                 goto out;
5543         }
5544
5545         ret = ocfs2_xa_set(&loc, xi, ctxt);
5546         if (!ret) {
5547                 xs->here = loc.xl_entry;
5548                 goto out;
5549         }
5550         if (ret != -ENOSPC)
5551                 mlog_errno(ret);
5552
5553
5554 out:
5555         mlog_exit(ret);
5556         return ret;
5557 }
5558
5559 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
5560                                              struct ocfs2_xattr_info *xi,
5561                                              struct ocfs2_xattr_search *xs,
5562                                              struct ocfs2_xattr_set_ctxt *ctxt)
5563 {
5564         int ret;
5565
5566         mlog_entry("Set xattr %s in xattr index block\n", xi->xi_name);
5567
5568         ret = ocfs2_xattr_set_entry_bucket(inode, xi, xs, ctxt);
5569         if (!ret)
5570                 goto out;
5571         if (ret != -ENOSPC) {
5572                 mlog_errno(ret);
5573                 goto out;
5574         }
5575
5576         /* Ack, need more space.  Let's try to get another bucket! */
5577
5578         /*
5579          * We do not allow for overlapping ranges between buckets. And
5580          * the maximum number of collisions we will allow for then is
5581          * one bucket's worth, so check it here whether we need to
5582          * add a new bucket for the insert.
5583          */
5584         ret = ocfs2_check_xattr_bucket_collision(inode,
5585                                                  xs->bucket,
5586                                                  xi->xi_name);
5587         if (ret) {
5588                 mlog_errno(ret);
5589                 goto out;
5590         }
5591
5592         ret = ocfs2_add_new_xattr_bucket(inode,
5593                                          xs->xattr_bh,
5594                                          xs->bucket,
5595                                          ctxt);
5596         if (ret) {
5597                 mlog_errno(ret);
5598                 goto out;
5599         }
5600
5601         /*
5602          * ocfs2_add_new_xattr_bucket() will have updated
5603          * xs->bucket if it moved, but it will not have updated
5604          * any of the other search fields.  Thus, we drop it and
5605          * re-search.  Everything should be cached, so it'll be
5606          * quick.
5607          */
5608         ocfs2_xattr_bucket_relse(xs->bucket);
5609         ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
5610                                            xi->xi_name_index,
5611                                            xi->xi_name, xs);
5612         if (ret && ret != -ENODATA)
5613                 goto out;
5614         xs->not_found = ret;
5615
5616         /* Ok, we have a new bucket, let's try again */
5617         ret = ocfs2_xattr_set_entry_bucket(inode, xi, xs, ctxt);
5618         if (ret && (ret != -ENOSPC))
5619                 mlog_errno(ret);
5620
5621 out:
5622         mlog_exit(ret);
5623         return ret;
5624 }
5625
5626 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
5627                                         struct ocfs2_xattr_bucket *bucket,
5628                                         void *para)
5629 {
5630         int ret = 0, ref_credits;
5631         struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5632         u16 i;
5633         struct ocfs2_xattr_entry *xe;
5634         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5635         struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
5636         int credits = ocfs2_remove_extent_credits(osb->sb) +
5637                 ocfs2_blocks_per_xattr_bucket(inode->i_sb);
5638         struct ocfs2_xattr_value_root *xv;
5639         struct ocfs2_rm_xattr_bucket_para *args =
5640                         (struct ocfs2_rm_xattr_bucket_para *)para;
5641
5642         ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
5643
5644         for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
5645                 xe = &xh->xh_entries[i];
5646                 if (ocfs2_xattr_is_local(xe))
5647                         continue;
5648
5649                 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket,
5650                                                       i, &xv, NULL);
5651
5652                 ret = ocfs2_lock_xattr_remove_allocators(inode, xv,
5653                                                          args->ref_ci,
5654                                                          args->ref_root_bh,
5655                                                          &ctxt.meta_ac,
5656                                                          &ref_credits);
5657
5658                 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
5659                 if (IS_ERR(ctxt.handle)) {
5660                         ret = PTR_ERR(ctxt.handle);
5661                         mlog_errno(ret);
5662                         break;
5663                 }
5664
5665                 ret = ocfs2_xattr_bucket_value_truncate(inode, bucket,
5666                                                         i, 0, &ctxt);
5667
5668                 ocfs2_commit_trans(osb, ctxt.handle);
5669                 if (ctxt.meta_ac) {
5670                         ocfs2_free_alloc_context(ctxt.meta_ac);
5671                         ctxt.meta_ac = NULL;
5672                 }
5673                 if (ret) {
5674                         mlog_errno(ret);
5675                         break;
5676                 }
5677         }
5678
5679         if (ctxt.meta_ac)
5680                 ocfs2_free_alloc_context(ctxt.meta_ac);
5681         ocfs2_schedule_truncate_log_flush(osb, 1);
5682         ocfs2_run_deallocs(osb, &ctxt.dealloc);
5683         return ret;
5684 }
5685
5686 /*
5687  * Whenever we modify a xattr value root in the bucket(e.g, CoW
5688  * or change the extent record flag), we need to recalculate
5689  * the metaecc for the whole bucket. So it is done here.
5690  *
5691  * Note:
5692  * We have to give the extra credits for the caller.
5693  */
5694 static int ocfs2_xattr_bucket_post_refcount(struct inode *inode,
5695                                             handle_t *handle,
5696                                             void *para)
5697 {
5698         int ret;
5699         struct ocfs2_xattr_bucket *bucket =
5700                         (struct ocfs2_xattr_bucket *)para;
5701
5702         ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
5703                                                 OCFS2_JOURNAL_ACCESS_WRITE);
5704         if (ret) {
5705                 mlog_errno(ret);
5706                 return ret;
5707         }
5708
5709         ocfs2_xattr_bucket_journal_dirty(handle, bucket);
5710
5711         return 0;
5712 }
5713
5714 /*
5715  * Special action we need if the xattr value is refcounted.
5716  *
5717  * 1. If the xattr is refcounted, lock the tree.
5718  * 2. CoW the xattr if we are setting the new value and the value
5719  *    will be stored outside.
5720  * 3. In other case, decrease_refcount will work for us, so just
5721  *    lock the refcount tree, calculate the meta and credits is OK.
5722  *
5723  * We have to do CoW before ocfs2_init_xattr_set_ctxt since
5724  * currently CoW is a completed transaction, while this function
5725  * will also lock the allocators and let us deadlock. So we will
5726  * CoW the whole xattr value.
5727  */
5728 static int ocfs2_prepare_refcount_xattr(struct inode *inode,
5729                                         struct ocfs2_dinode *di,
5730                                         struct ocfs2_xattr_info *xi,
5731                                         struct ocfs2_xattr_search *xis,
5732                                         struct ocfs2_xattr_search *xbs,
5733                                         struct ocfs2_refcount_tree **ref_tree,
5734                                         int *meta_add,
5735                                         int *credits)
5736 {
5737         int ret = 0;
5738         struct ocfs2_xattr_block *xb;
5739         struct ocfs2_xattr_entry *xe;
5740         char *base;
5741         u32 p_cluster, num_clusters;
5742         unsigned int ext_flags;
5743         int name_offset, name_len;
5744         struct ocfs2_xattr_value_buf vb;
5745         struct ocfs2_xattr_bucket *bucket = NULL;
5746         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5747         struct ocfs2_post_refcount refcount;
5748         struct ocfs2_post_refcount *p = NULL;
5749         struct buffer_head *ref_root_bh = NULL;
5750
5751         if (!xis->not_found) {
5752                 xe = xis->here;
5753                 name_offset = le16_to_cpu(xe->xe_name_offset);
5754                 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5755                 base = xis->base;
5756                 vb.vb_bh = xis->inode_bh;
5757                 vb.vb_access = ocfs2_journal_access_di;
5758         } else {
5759                 int i, block_off = 0;
5760                 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
5761                 xe = xbs->here;
5762                 name_offset = le16_to_cpu(xe->xe_name_offset);
5763                 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5764                 i = xbs->here - xbs->header->xh_entries;
5765
5766                 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
5767                         ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
5768                                                         bucket_xh(xbs->bucket),
5769                                                         i, &block_off,
5770                                                         &name_offset);
5771                         if (ret) {
5772                                 mlog_errno(ret);
5773                                 goto out;
5774                         }
5775                         base = bucket_block(xbs->bucket, block_off);
5776                         vb.vb_bh = xbs->bucket->bu_bhs[block_off];
5777                         vb.vb_access = ocfs2_journal_access;
5778
5779                         if (ocfs2_meta_ecc(osb)) {
5780                                 /*create parameters for ocfs2_post_refcount. */
5781                                 bucket = xbs->bucket;
5782                                 refcount.credits = bucket->bu_blocks;
5783                                 refcount.para = bucket;
5784                                 refcount.func =
5785                                         ocfs2_xattr_bucket_post_refcount;
5786                                 p = &refcount;
5787                         }
5788                 } else {
5789                         base = xbs->base;
5790                         vb.vb_bh = xbs->xattr_bh;
5791                         vb.vb_access = ocfs2_journal_access_xb;
5792                 }
5793         }
5794
5795         if (ocfs2_xattr_is_local(xe))
5796                 goto out;
5797
5798         vb.vb_xv = (struct ocfs2_xattr_value_root *)
5799                                 (base + name_offset + name_len);
5800
5801         ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
5802                                        &num_clusters, &vb.vb_xv->xr_list,
5803                                        &ext_flags);
5804         if (ret) {
5805                 mlog_errno(ret);
5806                 goto out;
5807         }
5808
5809         /*
5810          * We just need to check the 1st extent record, since we always
5811          * CoW the whole xattr. So there shouldn't be a xattr with
5812          * some REFCOUNT extent recs after the 1st one.
5813          */
5814         if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
5815                 goto out;
5816
5817         ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
5818                                        1, ref_tree, &ref_root_bh);
5819         if (ret) {
5820                 mlog_errno(ret);
5821                 goto out;
5822         }
5823
5824         /*
5825          * If we are deleting the xattr or the new size will be stored inside,
5826          * cool, leave it there, the xattr truncate process will remove them
5827          * for us(it still needs the refcount tree lock and the meta, credits).
5828          * And the worse case is that every cluster truncate will split the
5829          * refcount tree, and make the original extent become 3. So we will need
5830          * 2 * cluster more extent recs at most.
5831          */
5832         if (!xi->xi_value || xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE) {
5833
5834                 ret = ocfs2_refcounted_xattr_delete_need(inode,
5835                                                          &(*ref_tree)->rf_ci,
5836                                                          ref_root_bh, vb.vb_xv,
5837                                                          meta_add, credits);
5838                 if (ret)
5839                         mlog_errno(ret);
5840                 goto out;
5841         }
5842
5843         ret = ocfs2_refcount_cow_xattr(inode, di, &vb,
5844                                        *ref_tree, ref_root_bh, 0,
5845                                        le32_to_cpu(vb.vb_xv->xr_clusters), p);
5846         if (ret)
5847                 mlog_errno(ret);
5848
5849 out:
5850         brelse(ref_root_bh);
5851         return ret;
5852 }
5853
5854 /*
5855  * Add the REFCOUNTED flags for all the extent rec in ocfs2_xattr_value_root.
5856  * The physical clusters will be added to refcount tree.
5857  */
5858 static int ocfs2_xattr_value_attach_refcount(struct inode *inode,
5859                                 struct ocfs2_xattr_value_root *xv,
5860                                 struct ocfs2_extent_tree *value_et,
5861                                 struct ocfs2_caching_info *ref_ci,
5862                                 struct buffer_head *ref_root_bh,
5863                                 struct ocfs2_cached_dealloc_ctxt *dealloc,
5864                                 struct ocfs2_post_refcount *refcount)
5865 {
5866         int ret = 0;
5867         u32 clusters = le32_to_cpu(xv->xr_clusters);
5868         u32 cpos, p_cluster, num_clusters;
5869         struct ocfs2_extent_list *el = &xv->xr_list;
5870         unsigned int ext_flags;
5871
5872         cpos = 0;
5873         while (cpos < clusters) {
5874                 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
5875                                                &num_clusters, el, &ext_flags);
5876
5877                 cpos += num_clusters;
5878                 if ((ext_flags & OCFS2_EXT_REFCOUNTED))
5879                         continue;
5880
5881                 BUG_ON(!p_cluster);
5882
5883                 ret = ocfs2_add_refcount_flag(inode, value_et,
5884                                               ref_ci, ref_root_bh,
5885                                               cpos - num_clusters,
5886                                               p_cluster, num_clusters,
5887                                               dealloc, refcount);
5888                 if (ret) {
5889                         mlog_errno(ret);
5890                         break;
5891                 }
5892         }
5893
5894         return ret;
5895 }
5896
5897 /*
5898  * Given a normal ocfs2_xattr_header, refcount all the entries which
5899  * have value stored outside.
5900  * Used for xattrs stored in inode and ocfs2_xattr_block.
5901  */
5902 static int ocfs2_xattr_attach_refcount_normal(struct inode *inode,
5903                                 struct ocfs2_xattr_value_buf *vb,
5904                                 struct ocfs2_xattr_header *header,
5905                                 struct ocfs2_caching_info *ref_ci,
5906                                 struct buffer_head *ref_root_bh,
5907                                 struct ocfs2_cached_dealloc_ctxt *dealloc)
5908 {
5909
5910         struct ocfs2_xattr_entry *xe;
5911         struct ocfs2_xattr_value_root *xv;
5912         struct ocfs2_extent_tree et;
5913         int i, ret = 0;
5914
5915         for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
5916                 xe = &header->xh_entries[i];
5917
5918                 if (ocfs2_xattr_is_local(xe))
5919                         continue;
5920
5921                 xv = (struct ocfs2_xattr_value_root *)((void *)header +
5922                         le16_to_cpu(xe->xe_name_offset) +
5923                         OCFS2_XATTR_SIZE(xe->xe_name_len));
5924
5925                 vb->vb_xv = xv;
5926                 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
5927
5928                 ret = ocfs2_xattr_value_attach_refcount(inode, xv, &et,
5929                                                         ref_ci, ref_root_bh,
5930                                                         dealloc, NULL);
5931                 if (ret) {
5932                         mlog_errno(ret);
5933                         break;
5934                 }
5935         }
5936
5937         return ret;
5938 }
5939
5940 static int ocfs2_xattr_inline_attach_refcount(struct inode *inode,
5941                                 struct buffer_head *fe_bh,
5942                                 struct ocfs2_caching_info *ref_ci,
5943                                 struct buffer_head *ref_root_bh,
5944                                 struct ocfs2_cached_dealloc_ctxt *dealloc)
5945 {
5946         struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
5947         struct ocfs2_xattr_header *header = (struct ocfs2_xattr_header *)
5948                                 (fe_bh->b_data + inode->i_sb->s_blocksize -
5949                                 le16_to_cpu(di->i_xattr_inline_size));
5950         struct ocfs2_xattr_value_buf vb = {
5951                 .vb_bh = fe_bh,
5952                 .vb_access = ocfs2_journal_access_di,
5953         };
5954
5955         return ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
5956                                                   ref_ci, ref_root_bh, dealloc);
5957 }
5958
5959 struct ocfs2_xattr_tree_value_refcount_para {
5960         struct ocfs2_caching_info *ref_ci;
5961         struct buffer_head *ref_root_bh;
5962         struct ocfs2_cached_dealloc_ctxt *dealloc;
5963 };
5964
5965 static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
5966                                            struct ocfs2_xattr_bucket *bucket,
5967                                            int offset,
5968                                            struct ocfs2_xattr_value_root **xv,
5969                                            struct buffer_head **bh)
5970 {
5971         int ret, block_off, name_offset;
5972         struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5973         struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
5974         void *base;
5975
5976         ret = ocfs2_xattr_bucket_get_name_value(sb,
5977                                                 bucket_xh(bucket),
5978                                                 offset,
5979                                                 &block_off,
5980                                                 &name_offset);
5981         if (ret) {
5982                 mlog_errno(ret);
5983                 goto out;
5984         }
5985
5986         base = bucket_block(bucket, block_off);
5987
5988         *xv = (struct ocfs2_xattr_value_root *)(base + name_offset +
5989                          OCFS2_XATTR_SIZE(xe->xe_name_len));
5990
5991         if (bh)
5992                 *bh = bucket->bu_bhs[block_off];
5993 out:
5994         return ret;
5995 }
5996
5997 /*
5998  * For a given xattr bucket, refcount all the entries which
5999  * have value stored outside.
6000  */
6001 static int ocfs2_xattr_bucket_value_refcount(struct inode *inode,
6002                                              struct ocfs2_xattr_bucket *bucket,
6003                                              void *para)
6004 {
6005         int i, ret = 0;
6006         struct ocfs2_extent_tree et;
6007         struct ocfs2_xattr_tree_value_refcount_para *ref =
6008                         (struct ocfs2_xattr_tree_value_refcount_para *)para;
6009         struct ocfs2_xattr_header *xh =
6010                         (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6011         struct ocfs2_xattr_entry *xe;
6012         struct ocfs2_xattr_value_buf vb = {
6013                 .vb_access = ocfs2_journal_access,
6014         };
6015         struct ocfs2_post_refcount refcount = {
6016                 .credits = bucket->bu_blocks,
6017                 .para = bucket,
6018                 .func = ocfs2_xattr_bucket_post_refcount,
6019         };
6020         struct ocfs2_post_refcount *p = NULL;
6021
6022         /* We only need post_refcount if we support metaecc. */
6023         if (ocfs2_meta_ecc(OCFS2_SB(inode->i_sb)))
6024                 p = &refcount;
6025
6026         mlog(0, "refcount bucket %llu, count = %u\n",
6027              (unsigned long long)bucket_blkno(bucket),
6028              le16_to_cpu(xh->xh_count));
6029         for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6030                 xe = &xh->xh_entries[i];
6031
6032                 if (ocfs2_xattr_is_local(xe))
6033                         continue;
6034
6035                 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket, i,
6036                                                       &vb.vb_xv, &vb.vb_bh);
6037                 if (ret) {
6038                         mlog_errno(ret);
6039                         break;
6040                 }
6041
6042                 ocfs2_init_xattr_value_extent_tree(&et,
6043                                                    INODE_CACHE(inode), &vb);
6044
6045                 ret = ocfs2_xattr_value_attach_refcount(inode, vb.vb_xv,
6046                                                         &et, ref->ref_ci,
6047                                                         ref->ref_root_bh,
6048                                                         ref->dealloc, p);
6049                 if (ret) {
6050                         mlog_errno(ret);
6051                         break;
6052                 }
6053         }
6054
6055         return ret;
6056
6057 }
6058
6059 static int ocfs2_refcount_xattr_tree_rec(struct inode *inode,
6060                                      struct buffer_head *root_bh,
6061                                      u64 blkno, u32 cpos, u32 len, void *para)
6062 {
6063         return ocfs2_iterate_xattr_buckets(inode, blkno, len,
6064                                            ocfs2_xattr_bucket_value_refcount,
6065                                            para);
6066 }
6067
6068 static int ocfs2_xattr_block_attach_refcount(struct inode *inode,
6069                                 struct buffer_head *blk_bh,
6070                                 struct ocfs2_caching_info *ref_ci,
6071                                 struct buffer_head *ref_root_bh,
6072                                 struct ocfs2_cached_dealloc_ctxt *dealloc)
6073 {
6074         int ret = 0;
6075         struct ocfs2_xattr_block *xb =
6076                                 (struct ocfs2_xattr_block *)blk_bh->b_data;
6077
6078         if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
6079                 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
6080                 struct ocfs2_xattr_value_buf vb = {
6081                         .vb_bh = blk_bh,
6082                         .vb_access = ocfs2_journal_access_xb,
6083                 };
6084
6085                 ret = ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
6086                                                          ref_ci, ref_root_bh,
6087                                                          dealloc);
6088         } else {
6089                 struct ocfs2_xattr_tree_value_refcount_para para = {
6090                         .ref_ci = ref_ci,
6091                         .ref_root_bh = ref_root_bh,
6092                         .dealloc = dealloc,
6093                 };
6094
6095                 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
6096                                                 ocfs2_refcount_xattr_tree_rec,
6097                                                 &para);
6098         }
6099
6100         return ret;
6101 }
6102
6103 int ocfs2_xattr_attach_refcount_tree(struct inode *inode,
6104                                      struct buffer_head *fe_bh,
6105                                      struct ocfs2_caching_info *ref_ci,
6106                                      struct buffer_head *ref_root_bh,
6107                                      struct ocfs2_cached_dealloc_ctxt *dealloc)
6108 {
6109         int ret = 0;
6110         struct ocfs2_inode_info *oi = OCFS2_I(inode);
6111         struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
6112         struct buffer_head *blk_bh = NULL;
6113
6114         if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
6115                 ret = ocfs2_xattr_inline_attach_refcount(inode, fe_bh,
6116                                                          ref_ci, ref_root_bh,
6117                                                          dealloc);
6118                 if (ret) {
6119                         mlog_errno(ret);
6120                         goto out;
6121                 }
6122         }
6123
6124         if (!di->i_xattr_loc)
6125                 goto out;
6126
6127         ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
6128                                      &blk_bh);
6129         if (ret < 0) {
6130                 mlog_errno(ret);
6131                 goto out;
6132         }
6133
6134         ret = ocfs2_xattr_block_attach_refcount(inode, blk_bh, ref_ci,
6135                                                 ref_root_bh, dealloc);
6136         if (ret)
6137                 mlog_errno(ret);
6138
6139         brelse(blk_bh);
6140 out:
6141
6142         return ret;
6143 }
6144
6145 typedef int (should_xattr_reflinked)(struct ocfs2_xattr_entry *xe);
6146 /*
6147  * Store the information we need in xattr reflink.
6148  * old_bh and new_bh are inode bh for the old and new inode.
6149  */
6150 struct ocfs2_xattr_reflink {
6151         struct inode *old_inode;
6152         struct inode *new_inode;
6153         struct buffer_head *old_bh;
6154         struct buffer_head *new_bh;
6155         struct ocfs2_caching_info *ref_ci;
6156         struct buffer_head *ref_root_bh;
6157         struct ocfs2_cached_dealloc_ctxt *dealloc;
6158         should_xattr_reflinked *xattr_reflinked;
6159 };
6160
6161 /*
6162  * Given a xattr header and xe offset,
6163  * return the proper xv and the corresponding bh.
6164  * xattr in inode, block and xattr tree have different implementaions.
6165  */
6166 typedef int (get_xattr_value_root)(struct super_block *sb,
6167                                    struct buffer_head *bh,
6168                                    struct ocfs2_xattr_header *xh,
6169                                    int offset,
6170                                    struct ocfs2_xattr_value_root **xv,
6171                                    struct buffer_head **ret_bh,
6172                                    void *para);
6173
6174 /*
6175  * Calculate all the xattr value root metadata stored in this xattr header and
6176  * credits we need if we create them from the scratch.
6177  * We use get_xattr_value_root so that all types of xattr container can use it.
6178  */
6179 static int ocfs2_value_metas_in_xattr_header(struct super_block *sb,
6180                                              struct buffer_head *bh,
6181                                              struct ocfs2_xattr_header *xh,
6182                                              int *metas, int *credits,
6183                                              int *num_recs,
6184                                              get_xattr_value_root *func,
6185                                              void *para)
6186 {
6187         int i, ret = 0;
6188         struct ocfs2_xattr_value_root *xv;
6189         struct ocfs2_xattr_entry *xe;
6190
6191         for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6192                 xe = &xh->xh_entries[i];
6193                 if (ocfs2_xattr_is_local(xe))
6194                         continue;
6195
6196                 ret = func(sb, bh, xh, i, &xv, NULL, para);
6197                 if (ret) {
6198                         mlog_errno(ret);
6199                         break;
6200                 }
6201
6202                 *metas += le16_to_cpu(xv->xr_list.l_tree_depth) *
6203                           le16_to_cpu(xv->xr_list.l_next_free_rec);
6204
6205                 *credits += ocfs2_calc_extend_credits(sb,
6206                                                 &def_xv.xv.xr_list,
6207                                                 le32_to_cpu(xv->xr_clusters));
6208
6209                 /*
6210                  * If the value is a tree with depth > 1, We don't go deep
6211                  * to the extent block, so just calculate a maximum record num.
6212                  */
6213                 if (!xv->xr_list.l_tree_depth)
6214                         *num_recs += le16_to_cpu(xv->xr_list.l_next_free_rec);
6215                 else
6216                         *num_recs += ocfs2_clusters_for_bytes(sb,
6217                                                               XATTR_SIZE_MAX);
6218         }
6219
6220         return ret;
6221 }
6222
6223 /* Used by xattr inode and block to return the right xv and buffer_head. */
6224 static int ocfs2_get_xattr_value_root(struct super_block *sb,
6225                                       struct buffer_head *bh,
6226                                       struct ocfs2_xattr_header *xh,
6227                                       int offset,
6228                                       struct ocfs2_xattr_value_root **xv,
6229                                       struct buffer_head **ret_bh,
6230                                       void *para)
6231 {
6232         struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
6233
6234         *xv = (struct ocfs2_xattr_value_root *)((void *)xh +
6235                 le16_to_cpu(xe->xe_name_offset) +
6236                 OCFS2_XATTR_SIZE(xe->xe_name_len));
6237
6238         if (ret_bh)
6239                 *ret_bh = bh;
6240
6241         return 0;
6242 }
6243
6244 /*
6245  * Lock the meta_ac and caculate how much credits we need for reflink xattrs.
6246  * It is only used for inline xattr and xattr block.
6247  */
6248 static int ocfs2_reflink_lock_xattr_allocators(struct ocfs2_super *osb,
6249                                         struct ocfs2_xattr_header *xh,
6250                                         struct buffer_head *ref_root_bh,
6251                                         int *credits,
6252                                         struct ocfs2_alloc_context **meta_ac)
6253 {
6254         int ret, meta_add = 0, num_recs = 0;
6255         struct ocfs2_refcount_block *rb =
6256                         (struct ocfs2_refcount_block *)ref_root_bh->b_data;
6257
6258         *credits = 0;
6259
6260         ret = ocfs2_value_metas_in_xattr_header(osb->sb, NULL, xh,
6261                                                 &meta_add, credits, &num_recs,
6262                                                 ocfs2_get_xattr_value_root,
6263                                                 NULL);
6264         if (ret) {
6265                 mlog_errno(ret);
6266                 goto out;
6267         }
6268
6269         /*
6270          * We need to add/modify num_recs in refcount tree, so just calculate
6271          * an approximate number we need for refcount tree change.
6272          * Sometimes we need to split the tree, and after split,  half recs
6273          * will be moved to the new block, and a new block can only provide
6274          * half number of recs. So we multiple new blocks by 2.
6275          */
6276         num_recs = num_recs / ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6277         meta_add += num_recs;
6278         *credits += num_recs + num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6279         if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6280                 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6281                             le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6282         else
6283                 *credits += 1;
6284
6285         ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add, meta_ac);
6286         if (ret)
6287                 mlog_errno(ret);
6288
6289 out:
6290         return ret;
6291 }
6292
6293 /*
6294  * Given a xattr header, reflink all the xattrs in this container.
6295  * It can be used for inode, block and bucket.
6296  *
6297  * NOTE:
6298  * Before we call this function, the caller has memcpy the xattr in
6299  * old_xh to the new_xh.
6300  *
6301  * If args.xattr_reflinked is set, call it to decide whether the xe should
6302  * be reflinked or not. If not, remove it from the new xattr header.
6303  */
6304 static int ocfs2_reflink_xattr_header(handle_t *handle,
6305                                       struct ocfs2_xattr_reflink *args,
6306                                       struct buffer_head *old_bh,
6307                                       struct ocfs2_xattr_header *xh,
6308                                       struct buffer_head *new_bh,
6309                                       struct ocfs2_xattr_header *new_xh,
6310                                       struct ocfs2_xattr_value_buf *vb,
6311                                       struct ocfs2_alloc_context *meta_ac,
6312                                       get_xattr_value_root *func,
6313                                       void *para)
6314 {
6315         int ret = 0, i, j;
6316         struct super_block *sb = args->old_inode->i_sb;
6317         struct buffer_head *value_bh;
6318         struct ocfs2_xattr_entry *xe, *last;
6319         struct ocfs2_xattr_value_root *xv, *new_xv;
6320         struct ocfs2_extent_tree data_et;
6321         u32 clusters, cpos, p_cluster, num_clusters;
6322         unsigned int ext_flags = 0;
6323
6324         mlog(0, "reflink xattr in container %llu, count = %u\n",
6325              (unsigned long long)old_bh->b_blocknr, le16_to_cpu(xh->xh_count));
6326
6327         last = &new_xh->xh_entries[le16_to_cpu(new_xh->xh_count)];
6328         for (i = 0, j = 0; i < le16_to_cpu(xh->xh_count); i++, j++) {
6329                 xe = &xh->xh_entries[i];
6330
6331                 if (args->xattr_reflinked && !args->xattr_reflinked(xe)) {
6332                         xe = &new_xh->xh_entries[j];
6333
6334                         le16_add_cpu(&new_xh->xh_count, -1);
6335                         if (new_xh->xh_count) {
6336                                 memmove(xe, xe + 1,
6337                                         (void *)last - (void *)xe);
6338                                 memset(last, 0,
6339                                        sizeof(struct ocfs2_xattr_entry));
6340                         }
6341
6342                         /*
6343                          * We don't want j to increase in the next round since
6344                          * it is already moved ahead.
6345                          */
6346                         j--;
6347                         continue;
6348                 }
6349
6350                 if (ocfs2_xattr_is_local(xe))
6351                         continue;
6352
6353                 ret = func(sb, old_bh, xh, i, &xv, NULL, para);
6354                 if (ret) {
6355                         mlog_errno(ret);
6356                         break;
6357                 }
6358
6359                 ret = func(sb, new_bh, new_xh, j, &new_xv, &value_bh, para);
6360                 if (ret) {
6361                         mlog_errno(ret);
6362                         break;
6363                 }
6364
6365                 /*
6366                  * For the xattr which has l_tree_depth = 0, all the extent
6367                  * recs have already be copied to the new xh with the
6368                  * propriate OCFS2_EXT_REFCOUNTED flag we just need to
6369                  * increase the refount count int the refcount tree.
6370                  *
6371                  * For the xattr which has l_tree_depth > 0, we need
6372                  * to initialize it to the empty default value root,
6373                  * and then insert the extents one by one.
6374                  */
6375                 if (xv->xr_list.l_tree_depth) {
6376                         memcpy(new_xv, &def_xv, sizeof(def_xv));
6377                         vb->vb_xv = new_xv;
6378                         vb->vb_bh = value_bh;
6379                         ocfs2_init_xattr_value_extent_tree(&data_et,
6380                                         INODE_CACHE(args->new_inode), vb);
6381                 }
6382
6383                 clusters = le32_to_cpu(xv->xr_clusters);
6384                 cpos = 0;
6385                 while (cpos < clusters) {
6386                         ret = ocfs2_xattr_get_clusters(args->old_inode,
6387                                                        cpos,
6388                                                        &p_cluster,
6389                                                        &num_clusters,
6390                                                        &xv->xr_list,
6391                                                        &ext_flags);
6392                         if (ret) {
6393                                 mlog_errno(ret);
6394                                 goto out;
6395                         }
6396
6397                         BUG_ON(!p_cluster);
6398
6399                         if (xv->xr_list.l_tree_depth) {
6400                                 ret = ocfs2_insert_extent(handle,
6401                                                 &data_et, cpos,
6402                                                 ocfs2_clusters_to_blocks(
6403                                                         args->old_inode->i_sb,
6404                                                         p_cluster),
6405                                                 num_clusters, ext_flags,
6406                                                 meta_ac);
6407                                 if (ret) {
6408                                         mlog_errno(ret);
6409                                         goto out;
6410                                 }
6411                         }
6412
6413                         ret = ocfs2_increase_refcount(handle, args->ref_ci,
6414                                                       args->ref_root_bh,
6415                                                       p_cluster, num_clusters,
6416                                                       meta_ac, args->dealloc);
6417                         if (ret) {
6418                                 mlog_errno(ret);
6419                                 goto out;
6420                         }
6421
6422                         cpos += num_clusters;
6423                 }
6424         }
6425
6426 out:
6427         return ret;
6428 }
6429
6430 static int ocfs2_reflink_xattr_inline(struct ocfs2_xattr_reflink *args)
6431 {
6432         int ret = 0, credits = 0;
6433         handle_t *handle;
6434         struct ocfs2_super *osb = OCFS2_SB(args->old_inode->i_sb);
6435         struct ocfs2_dinode *di = (struct ocfs2_dinode *)args->old_bh->b_data;
6436         int inline_size = le16_to_cpu(di->i_xattr_inline_size);
6437         int header_off = osb->sb->s_blocksize - inline_size;
6438         struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)
6439                                         (args->old_bh->b_data + header_off);
6440         struct ocfs2_xattr_header *new_xh = (struct ocfs2_xattr_header *)
6441                                         (args->new_bh->b_data + header_off);
6442         struct ocfs2_alloc_context *meta_ac = NULL;
6443         struct ocfs2_inode_info *new_oi;
6444         struct ocfs2_dinode *new_di;
6445         struct ocfs2_xattr_value_buf vb = {
6446                 .vb_bh = args->new_bh,
6447                 .vb_access = ocfs2_journal_access_di,
6448         };
6449
6450         ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6451                                                   &credits, &meta_ac);
6452         if (ret) {
6453                 mlog_errno(ret);
6454                 goto out;
6455         }
6456
6457         handle = ocfs2_start_trans(osb, credits);
6458         if (IS_ERR(handle)) {
6459                 ret = PTR_ERR(handle);
6460                 mlog_errno(ret);
6461                 goto out;
6462         }
6463
6464         ret = ocfs2_journal_access_di(handle, INODE_CACHE(args->new_inode),
6465                                       args->new_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6466         if (ret) {
6467                 mlog_errno(ret);
6468                 goto out_commit;
6469         }
6470
6471         memcpy(args->new_bh->b_data + header_off,
6472                args->old_bh->b_data + header_off, inline_size);
6473
6474         new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6475         new_di->i_xattr_inline_size = cpu_to_le16(inline_size);
6476
6477         ret = ocfs2_reflink_xattr_header(handle, args, args->old_bh, xh,
6478                                          args->new_bh, new_xh, &vb, meta_ac,
6479                                          ocfs2_get_xattr_value_root, NULL);
6480         if (ret) {
6481                 mlog_errno(ret);
6482                 goto out_commit;
6483         }
6484
6485         new_oi = OCFS2_I(args->new_inode);
6486         spin_lock(&new_oi->ip_lock);
6487         new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL | OCFS2_INLINE_XATTR_FL;
6488         new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6489         spin_unlock(&new_oi->ip_lock);
6490
6491         ocfs2_journal_dirty(handle, args->new_bh);
6492
6493 out_commit:
6494         ocfs2_commit_trans(osb, handle);
6495
6496 out:
6497         if (meta_ac)
6498                 ocfs2_free_alloc_context(meta_ac);
6499         return ret;
6500 }
6501
6502 static int ocfs2_create_empty_xattr_block(struct inode *inode,
6503                                           struct buffer_head *fe_bh,
6504                                           struct buffer_head **ret_bh,
6505                                           int indexed)
6506 {
6507         int ret;
6508         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6509         struct ocfs2_xattr_set_ctxt ctxt;
6510
6511         memset(&ctxt, 0, sizeof(ctxt));
6512         ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &ctxt.meta_ac);
6513         if (ret < 0) {
6514                 mlog_errno(ret);
6515                 return ret;
6516         }
6517
6518         ctxt.handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_CREATE_CREDITS);
6519         if (IS_ERR(ctxt.handle)) {
6520                 ret = PTR_ERR(ctxt.handle);
6521                 mlog_errno(ret);
6522                 goto out;
6523         }
6524
6525         mlog(0, "create new xattr block for inode %llu, index = %d\n",
6526              (unsigned long long)fe_bh->b_blocknr, indexed);
6527         ret = ocfs2_create_xattr_block(inode, fe_bh, &ctxt, indexed,
6528                                        ret_bh);
6529         if (ret)
6530                 mlog_errno(ret);
6531
6532         ocfs2_commit_trans(osb, ctxt.handle);
6533 out:
6534         ocfs2_free_alloc_context(ctxt.meta_ac);
6535         return ret;
6536 }
6537
6538 static int ocfs2_reflink_xattr_block(struct ocfs2_xattr_reflink *args,
6539                                      struct buffer_head *blk_bh,
6540                                      struct buffer_head *new_blk_bh)
6541 {
6542         int ret = 0, credits = 0;
6543         handle_t *handle;
6544         struct ocfs2_inode_info *new_oi = OCFS2_I(args->new_inode);
6545         struct ocfs2_dinode *new_di;
6546         struct ocfs2_super *osb = OCFS2_SB(args->new_inode->i_sb);
6547         int header_off = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
6548         struct ocfs2_xattr_block *xb =
6549                         (struct ocfs2_xattr_block *)blk_bh->b_data;
6550         struct ocfs2_xattr_header *xh = &xb->xb_attrs.xb_header;
6551         struct ocfs2_xattr_block *new_xb =
6552                         (struct ocfs2_xattr_block *)new_blk_bh->b_data;
6553         struct ocfs2_xattr_header *new_xh = &new_xb->xb_attrs.xb_header;
6554         struct ocfs2_alloc_context *meta_ac;
6555         struct ocfs2_xattr_value_buf vb = {
6556                 .vb_bh = new_blk_bh,
6557                 .vb_access = ocfs2_journal_access_xb,
6558         };
6559
6560         ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6561                                                   &credits, &meta_ac);
6562         if (ret) {
6563                 mlog_errno(ret);
6564                 return ret;
6565         }
6566
6567         /* One more credits in case we need to add xattr flags in new inode. */
6568         handle = ocfs2_start_trans(osb, credits + 1);
6569         if (IS_ERR(handle)) {
6570                 ret = PTR_ERR(handle);
6571                 mlog_errno(ret);
6572                 goto out;
6573         }
6574
6575         if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6576                 ret = ocfs2_journal_access_di(handle,
6577                                               INODE_CACHE(args->new_inode),
6578                                               args->new_bh,
6579                                               OCFS2_JOURNAL_ACCESS_WRITE);
6580                 if (ret) {
6581                         mlog_errno(ret);
6582                         goto out_commit;
6583                 }
6584         }
6585
6586         ret = ocfs2_journal_access_xb(handle, INODE_CACHE(args->new_inode),
6587                                       new_blk_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6588         if (ret) {
6589                 mlog_errno(ret);
6590                 goto out_commit;
6591         }
6592
6593         memcpy(new_blk_bh->b_data + header_off, blk_bh->b_data + header_off,
6594                osb->sb->s_blocksize - header_off);
6595
6596         ret = ocfs2_reflink_xattr_header(handle, args, blk_bh, xh,
6597                                          new_blk_bh, new_xh, &vb, meta_ac,
6598                                          ocfs2_get_xattr_value_root, NULL);
6599         if (ret) {
6600                 mlog_errno(ret);
6601                 goto out_commit;
6602         }
6603
6604         ocfs2_journal_dirty(handle, new_blk_bh);
6605
6606         if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6607                 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6608                 spin_lock(&new_oi->ip_lock);
6609                 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
6610                 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6611                 spin_unlock(&new_oi->ip_lock);
6612
6613                 ocfs2_journal_dirty(handle, args->new_bh);
6614         }
6615
6616 out_commit:
6617         ocfs2_commit_trans(osb, handle);
6618
6619 out:
6620         ocfs2_free_alloc_context(meta_ac);
6621         return ret;
6622 }
6623
6624 struct ocfs2_reflink_xattr_tree_args {
6625         struct ocfs2_xattr_reflink *reflink;
6626         struct buffer_head *old_blk_bh;
6627         struct buffer_head *new_blk_bh;
6628         struct ocfs2_xattr_bucket *old_bucket;
6629         struct ocfs2_xattr_bucket *new_bucket;
6630 };
6631
6632 /*
6633  * NOTE:
6634  * We have to handle the case that both old bucket and new bucket
6635  * will call this function to get the right ret_bh.
6636  * So The caller must give us the right bh.
6637  */
6638 static int ocfs2_get_reflink_xattr_value_root(struct super_block *sb,
6639                                         struct buffer_head *bh,
6640                                         struct ocfs2_xattr_header *xh,
6641                                         int offset,
6642                                         struct ocfs2_xattr_value_root **xv,
6643                                         struct buffer_head **ret_bh,
6644                                         void *para)
6645 {
6646         struct ocfs2_reflink_xattr_tree_args *args =
6647                         (struct ocfs2_reflink_xattr_tree_args *)para;
6648         struct ocfs2_xattr_bucket *bucket;
6649
6650         if (bh == args->old_bucket->bu_bhs[0])
6651                 bucket = args->old_bucket;
6652         else
6653                 bucket = args->new_bucket;
6654
6655         return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6656                                                xv, ret_bh);
6657 }
6658
6659 struct ocfs2_value_tree_metas {
6660         int num_metas;
6661         int credits;
6662         int num_recs;
6663 };
6664
6665 static int ocfs2_value_tree_metas_in_bucket(struct super_block *sb,
6666                                         struct buffer_head *bh,
6667                                         struct ocfs2_xattr_header *xh,
6668                                         int offset,
6669                                         struct ocfs2_xattr_value_root **xv,
6670                                         struct buffer_head **ret_bh,
6671                                         void *para)
6672 {
6673         struct ocfs2_xattr_bucket *bucket =
6674                                 (struct ocfs2_xattr_bucket *)para;
6675
6676         return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6677                                                xv, ret_bh);
6678 }
6679
6680 static int ocfs2_calc_value_tree_metas(struct inode *inode,
6681                                       struct ocfs2_xattr_bucket *bucket,
6682                                       void *para)
6683 {
6684         struct ocfs2_value_tree_metas *metas =
6685                         (struct ocfs2_value_tree_metas *)para;
6686         struct ocfs2_xattr_header *xh =
6687                         (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6688
6689         /* Add the credits for this bucket first. */
6690         metas->credits += bucket->bu_blocks;
6691         return ocfs2_value_metas_in_xattr_header(inode->i_sb, bucket->bu_bhs[0],
6692                                         xh, &metas->num_metas,
6693                                         &metas->credits, &metas->num_recs,
6694                                         ocfs2_value_tree_metas_in_bucket,
6695                                         bucket);
6696 }
6697
6698 /*
6699  * Given a xattr extent rec starting from blkno and having len clusters,
6700  * iterate all the buckets calculate how much metadata we need for reflinking
6701  * all the ocfs2_xattr_value_root and lock the allocators accordingly.
6702  */
6703 static int ocfs2_lock_reflink_xattr_rec_allocators(
6704                                 struct ocfs2_reflink_xattr_tree_args *args,
6705                                 struct ocfs2_extent_tree *xt_et,
6706                                 u64 blkno, u32 len, int *credits,
6707                                 struct ocfs2_alloc_context **meta_ac,
6708                                 struct ocfs2_alloc_context **data_ac)
6709 {
6710         int ret, num_free_extents;
6711         struct ocfs2_value_tree_metas metas;
6712         struct ocfs2_super *osb = OCFS2_SB(args->reflink->old_inode->i_sb);
6713         struct ocfs2_refcount_block *rb;
6714
6715         memset(&metas, 0, sizeof(metas));
6716
6717         ret = ocfs2_iterate_xattr_buckets(args->reflink->old_inode, blkno, len,
6718                                           ocfs2_calc_value_tree_metas, &metas);
6719         if (ret) {
6720                 mlog_errno(ret);
6721                 goto out;
6722         }
6723
6724         *credits = metas.credits;
6725
6726         /*
6727          * Calculate we need for refcount tree change.
6728          *
6729          * We need to add/modify num_recs in refcount tree, so just calculate
6730          * an approximate number we need for refcount tree change.
6731          * Sometimes we need to split the tree, and after split,  half recs
6732          * will be moved to the new block, and a new block can only provide
6733          * half number of recs. So we multiple new blocks by 2.
6734          * In the end, we have to add credits for modifying the already
6735          * existed refcount block.
6736          */
6737         rb = (struct ocfs2_refcount_block *)args->reflink->ref_root_bh->b_data;
6738         metas.num_recs =
6739                 (metas.num_recs + ocfs2_refcount_recs_per_rb(osb->sb) - 1) /
6740                  ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6741         metas.num_metas += metas.num_recs;
6742         *credits += metas.num_recs +
6743                     metas.num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6744         if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6745                 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6746                             le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6747         else
6748                 *credits += 1;
6749
6750         /* count in the xattr tree change. */
6751         num_free_extents = ocfs2_num_free_extents(osb, xt_et);
6752         if (num_free_extents < 0) {
6753                 ret = num_free_extents;
6754                 mlog_errno(ret);
6755                 goto out;
6756         }
6757
6758         if (num_free_extents < len)
6759                 metas.num_metas += ocfs2_extend_meta_needed(xt_et->et_root_el);
6760
6761         *credits += ocfs2_calc_extend_credits(osb->sb,
6762                                               xt_et->et_root_el, len);
6763
6764         if (metas.num_metas) {
6765                 ret = ocfs2_reserve_new_metadata_blocks(osb, metas.num_metas,
6766                                                         meta_ac);
6767                 if (ret) {
6768                         mlog_errno(ret);
6769                         goto out;
6770                 }
6771         }
6772
6773         if (len) {
6774                 ret = ocfs2_reserve_clusters(osb, len, data_ac);
6775                 if (ret)
6776                         mlog_errno(ret);
6777         }
6778 out:
6779         if (ret) {
6780                 if (*meta_ac) {
6781                         ocfs2_free_alloc_context(*meta_ac);
6782                         meta_ac = NULL;
6783                 }
6784         }
6785
6786         return ret;
6787 }
6788
6789 static int ocfs2_reflink_xattr_buckets(handle_t *handle,
6790                                 u64 blkno, u64 new_blkno, u32 clusters,
6791                                 struct ocfs2_alloc_context *meta_ac,
6792                                 struct ocfs2_alloc_context *data_ac,
6793                                 struct ocfs2_reflink_xattr_tree_args *args)
6794 {
6795         int i, j, ret = 0;
6796         struct super_block *sb = args->reflink->old_inode->i_sb;
6797         u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
6798         u32 num_buckets = clusters * bpc;
6799         int bpb = args->old_bucket->bu_blocks;
6800         struct ocfs2_xattr_value_buf vb = {
6801                 .vb_access = ocfs2_journal_access,
6802         };
6803
6804         for (i = 0; i < num_buckets; i++, blkno += bpb, new_blkno += bpb) {
6805                 ret = ocfs2_read_xattr_bucket(args->old_bucket, blkno);
6806                 if (ret) {
6807                         mlog_errno(ret);
6808                         break;
6809                 }
6810
6811                 ret = ocfs2_init_xattr_bucket(args->new_bucket, new_blkno);
6812                 if (ret) {
6813                         mlog_errno(ret);
6814                         break;
6815                 }
6816
6817                 /*
6818                  * The real bucket num in this series of blocks is stored
6819                  * in the 1st bucket.
6820                  */
6821                 if (i == 0)
6822                         num_buckets = le16_to_cpu(
6823                                 bucket_xh(args->old_bucket)->xh_num_buckets);
6824
6825                 ret = ocfs2_xattr_bucket_journal_access(handle,
6826                                                 args->new_bucket,
6827                                                 OCFS2_JOURNAL_ACCESS_CREATE);
6828                 if (ret) {
6829                         mlog_errno(ret);
6830                         break;
6831                 }
6832
6833                 for (j = 0; j < bpb; j++)
6834                         memcpy(bucket_block(args->new_bucket, j),
6835                                bucket_block(args->old_bucket, j),
6836                                sb->s_blocksize);
6837
6838                 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
6839
6840                 ret = ocfs2_reflink_xattr_header(handle, args->reflink,
6841                                         args->old_bucket->bu_bhs[0],
6842                                         bucket_xh(args->old_bucket),
6843                                         args->new_bucket->bu_bhs[0],
6844                                         bucket_xh(args->new_bucket),
6845                                         &vb, meta_ac,
6846                                         ocfs2_get_reflink_xattr_value_root,
6847                                         args);
6848                 if (ret) {
6849                         mlog_errno(ret);
6850                         break;
6851                 }
6852
6853                 /*
6854                  * Re-access and dirty the bucket to calculate metaecc.
6855                  * Because we may extend the transaction in reflink_xattr_header
6856                  * which will let the already accessed block gone.
6857                  */
6858                 ret = ocfs2_xattr_bucket_journal_access(handle,
6859                                                 args->new_bucket,
6860                                                 OCFS2_JOURNAL_ACCESS_WRITE);
6861                 if (ret) {
6862                         mlog_errno(ret);
6863                         break;
6864                 }
6865
6866                 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
6867                 ocfs2_xattr_bucket_relse(args->old_bucket);
6868                 ocfs2_xattr_bucket_relse(args->new_bucket);
6869         }
6870
6871         ocfs2_xattr_bucket_relse(args->old_bucket);
6872         ocfs2_xattr_bucket_relse(args->new_bucket);
6873         return ret;
6874 }
6875 /*
6876  * Create the same xattr extent record in the new inode's xattr tree.
6877  */
6878 static int ocfs2_reflink_xattr_rec(struct inode *inode,
6879                                    struct buffer_head *root_bh,
6880                                    u64 blkno,
6881                                    u32 cpos,
6882                                    u32 len,
6883                                    void *para)
6884 {
6885         int ret, credits = 0;
6886         u32 p_cluster, num_clusters;
6887         u64 new_blkno;
6888         handle_t *handle;
6889         struct ocfs2_reflink_xattr_tree_args *args =
6890                         (struct ocfs2_reflink_xattr_tree_args *)para;
6891         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6892         struct ocfs2_alloc_context *meta_ac = NULL;
6893         struct ocfs2_alloc_context *data_ac = NULL;
6894         struct ocfs2_extent_tree et;
6895
6896         ocfs2_init_xattr_tree_extent_tree(&et,
6897                                           INODE_CACHE(args->reflink->new_inode),
6898                                           args->new_blk_bh);
6899
6900         ret = ocfs2_lock_reflink_xattr_rec_allocators(args, &et, blkno,
6901                                                       len, &credits,
6902                                                       &meta_ac, &data_ac);
6903         if (ret) {
6904                 mlog_errno(ret);
6905                 goto out;
6906         }
6907
6908         handle = ocfs2_start_trans(osb, credits);
6909         if (IS_ERR(handle)) {
6910                 ret = PTR_ERR(handle);
6911                 mlog_errno(ret);
6912                 goto out;
6913         }
6914
6915         ret = ocfs2_claim_clusters(handle, data_ac,
6916                                    len, &p_cluster, &num_clusters);
6917         if (ret) {
6918                 mlog_errno(ret);
6919                 goto out_commit;
6920         }
6921
6922         new_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cluster);
6923
6924         mlog(0, "reflink xattr buckets %llu to %llu, len %u\n",
6925              (unsigned long long)blkno, (unsigned long long)new_blkno, len);
6926         ret = ocfs2_reflink_xattr_buckets(handle, blkno, new_blkno, len,
6927                                           meta_ac, data_ac, args);
6928         if (ret) {
6929                 mlog_errno(ret);
6930                 goto out_commit;
6931         }
6932
6933         mlog(0, "insert new xattr extent rec start %llu len %u to %u\n",
6934              (unsigned long long)new_blkno, len, cpos);
6935         ret = ocfs2_insert_extent(handle, &et, cpos, new_blkno,
6936                                   len, 0, meta_ac);
6937         if (ret)
6938                 mlog_errno(ret);
6939
6940 out_commit:
6941         ocfs2_commit_trans(osb, handle);
6942
6943 out:
6944         if (meta_ac)
6945                 ocfs2_free_alloc_context(meta_ac);
6946         if (data_ac)
6947                 ocfs2_free_alloc_context(data_ac);
6948         return ret;
6949 }
6950
6951 /*
6952  * Create reflinked xattr buckets.
6953  * We will add bucket one by one, and refcount all the xattrs in the bucket
6954  * if they are stored outside.
6955  */
6956 static int ocfs2_reflink_xattr_tree(struct ocfs2_xattr_reflink *args,
6957                                     struct buffer_head *blk_bh,
6958                                     struct buffer_head *new_blk_bh)
6959 {
6960         int ret;
6961         struct ocfs2_reflink_xattr_tree_args para;
6962
6963         memset(&para, 0, sizeof(para));
6964         para.reflink = args;
6965         para.old_blk_bh = blk_bh;
6966         para.new_blk_bh = new_blk_bh;
6967
6968         para.old_bucket = ocfs2_xattr_bucket_new(args->old_inode);
6969         if (!para.old_bucket) {
6970                 mlog_errno(-ENOMEM);
6971                 return -ENOMEM;
6972         }
6973
6974         para.new_bucket = ocfs2_xattr_bucket_new(args->new_inode);
6975         if (!para.new_bucket) {
6976                 ret = -ENOMEM;
6977                 mlog_errno(ret);
6978                 goto out;
6979         }
6980
6981         ret = ocfs2_iterate_xattr_index_block(args->old_inode, blk_bh,
6982                                               ocfs2_reflink_xattr_rec,
6983                                               &para);
6984         if (ret)
6985                 mlog_errno(ret);
6986
6987 out:
6988         ocfs2_xattr_bucket_free(para.old_bucket);
6989         ocfs2_xattr_bucket_free(para.new_bucket);
6990         return ret;
6991 }
6992
6993 static int ocfs2_reflink_xattr_in_block(struct ocfs2_xattr_reflink *args,
6994                                         struct buffer_head *blk_bh)
6995 {
6996         int ret, indexed = 0;
6997         struct buffer_head *new_blk_bh = NULL;
6998         struct ocfs2_xattr_block *xb =
6999                         (struct ocfs2_xattr_block *)blk_bh->b_data;
7000
7001
7002         if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)
7003                 indexed = 1;
7004
7005         ret = ocfs2_create_empty_xattr_block(args->new_inode, args->new_bh,
7006                                              &new_blk_bh, indexed);
7007         if (ret) {
7008                 mlog_errno(ret);
7009                 goto out;
7010         }
7011
7012         if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED))
7013                 ret = ocfs2_reflink_xattr_block(args, blk_bh, new_blk_bh);
7014         else
7015                 ret = ocfs2_reflink_xattr_tree(args, blk_bh, new_blk_bh);
7016         if (ret)
7017                 mlog_errno(ret);
7018
7019 out:
7020         brelse(new_blk_bh);
7021         return ret;
7022 }
7023
7024 static int ocfs2_reflink_xattr_no_security(struct ocfs2_xattr_entry *xe)
7025 {
7026         int type = ocfs2_xattr_get_type(xe);
7027
7028         return type != OCFS2_XATTR_INDEX_SECURITY &&
7029                type != OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS &&
7030                type != OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
7031 }
7032
7033 int ocfs2_reflink_xattrs(struct inode *old_inode,
7034                          struct buffer_head *old_bh,
7035                          struct inode *new_inode,
7036                          struct buffer_head *new_bh,
7037                          bool preserve_security)
7038 {
7039         int ret;
7040         struct ocfs2_xattr_reflink args;
7041         struct ocfs2_inode_info *oi = OCFS2_I(old_inode);
7042         struct ocfs2_dinode *di = (struct ocfs2_dinode *)old_bh->b_data;
7043         struct buffer_head *blk_bh = NULL;
7044         struct ocfs2_cached_dealloc_ctxt dealloc;
7045         struct ocfs2_refcount_tree *ref_tree;
7046         struct buffer_head *ref_root_bh = NULL;
7047
7048         ret = ocfs2_lock_refcount_tree(OCFS2_SB(old_inode->i_sb),
7049                                        le64_to_cpu(di->i_refcount_loc),
7050                                        1, &ref_tree, &ref_root_bh);
7051         if (ret) {
7052                 mlog_errno(ret);
7053                 goto out;
7054         }
7055
7056         ocfs2_init_dealloc_ctxt(&dealloc);
7057
7058         args.old_inode = old_inode;
7059         args.new_inode = new_inode;
7060         args.old_bh = old_bh;
7061         args.new_bh = new_bh;
7062         args.ref_ci = &ref_tree->rf_ci;
7063         args.ref_root_bh = ref_root_bh;
7064         args.dealloc = &dealloc;
7065         if (preserve_security)
7066                 args.xattr_reflinked = NULL;
7067         else
7068                 args.xattr_reflinked = ocfs2_reflink_xattr_no_security;
7069
7070         if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
7071                 ret = ocfs2_reflink_xattr_inline(&args);
7072                 if (ret) {
7073                         mlog_errno(ret);
7074                         goto out_unlock;
7075                 }
7076         }
7077
7078         if (!di->i_xattr_loc)
7079                 goto out_unlock;
7080
7081         ret = ocfs2_read_xattr_block(old_inode, le64_to_cpu(di->i_xattr_loc),
7082                                      &blk_bh);
7083         if (ret < 0) {
7084                 mlog_errno(ret);
7085                 goto out_unlock;
7086         }
7087
7088         ret = ocfs2_reflink_xattr_in_block(&args, blk_bh);
7089         if (ret)
7090                 mlog_errno(ret);
7091
7092         brelse(blk_bh);
7093
7094 out_unlock:
7095         ocfs2_unlock_refcount_tree(OCFS2_SB(old_inode->i_sb),
7096                                    ref_tree, 1);
7097         brelse(ref_root_bh);
7098
7099         if (ocfs2_dealloc_has_cluster(&dealloc)) {
7100                 ocfs2_schedule_truncate_log_flush(OCFS2_SB(old_inode->i_sb), 1);
7101                 ocfs2_run_deallocs(OCFS2_SB(old_inode->i_sb), &dealloc);
7102         }
7103
7104 out:
7105         return ret;
7106 }
7107
7108 /*
7109  * Initialize security and acl for a already created inode.
7110  * Used for reflink a non-preserve-security file.
7111  *
7112  * It uses common api like ocfs2_xattr_set, so the caller
7113  * must not hold any lock expect i_mutex.
7114  */
7115 int ocfs2_init_security_and_acl(struct inode *dir,
7116                                 struct inode *inode)
7117 {
7118         int ret = 0;
7119         struct buffer_head *dir_bh = NULL;
7120         struct ocfs2_security_xattr_info si = {
7121                 .enable = 1,
7122         };
7123
7124         ret = ocfs2_init_security_get(inode, dir, &si);
7125         if (!ret) {
7126                 ret = ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY,
7127                                       si.name, si.value, si.value_len,
7128                                       XATTR_CREATE);
7129                 if (ret) {
7130                         mlog_errno(ret);
7131                         goto leave;
7132                 }
7133         } else if (ret != -EOPNOTSUPP) {
7134                 mlog_errno(ret);
7135                 goto leave;
7136         }
7137
7138         ret = ocfs2_inode_lock(dir, &dir_bh, 0);
7139         if (ret) {
7140                 mlog_errno(ret);
7141                 goto leave;
7142         }
7143
7144         ret = ocfs2_init_acl(NULL, inode, dir, NULL, dir_bh, NULL, NULL);
7145         if (ret)
7146                 mlog_errno(ret);
7147
7148         ocfs2_inode_unlock(dir, 0);
7149         brelse(dir_bh);
7150 leave:
7151         return ret;
7152 }
7153 /*
7154  * 'security' attributes support
7155  */
7156 static size_t ocfs2_xattr_security_list(struct dentry *dentry, char *list,
7157                                         size_t list_size, const char *name,
7158                                         size_t name_len, int type)
7159 {
7160         const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN;
7161         const size_t total_len = prefix_len + name_len + 1;
7162
7163         if (list && total_len <= list_size) {
7164                 memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
7165                 memcpy(list + prefix_len, name, name_len);
7166                 list[prefix_len + name_len] = '\0';
7167         }
7168         return total_len;
7169 }
7170
7171 static int ocfs2_xattr_security_get(struct dentry *dentry, const char *name,
7172                                     void *buffer, size_t size, int type)
7173 {
7174         if (strcmp(name, "") == 0)
7175                 return -EINVAL;
7176         return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
7177                                name, buffer, size);
7178 }
7179
7180 static int ocfs2_xattr_security_set(struct dentry *dentry, const char *name,
7181                 const void *value, size_t size, int flags, int type)
7182 {
7183         if (strcmp(name, "") == 0)
7184                 return -EINVAL;
7185
7186         return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
7187                                name, value, size, flags);
7188 }
7189
7190 int ocfs2_init_security_get(struct inode *inode,
7191                             struct inode *dir,
7192                             struct ocfs2_security_xattr_info *si)
7193 {
7194         /* check whether ocfs2 support feature xattr */
7195         if (!ocfs2_supports_xattr(OCFS2_SB(dir->i_sb)))
7196                 return -EOPNOTSUPP;
7197         return security_inode_init_security(inode, dir, &si->name, &si->value,
7198                                             &si->value_len);
7199 }
7200
7201 int ocfs2_init_security_set(handle_t *handle,
7202                             struct inode *inode,
7203                             struct buffer_head *di_bh,
7204                             struct ocfs2_security_xattr_info *si,
7205                             struct ocfs2_alloc_context *xattr_ac,
7206                             struct ocfs2_alloc_context *data_ac)
7207 {
7208         return ocfs2_xattr_set_handle(handle, inode, di_bh,
7209                                      OCFS2_XATTR_INDEX_SECURITY,
7210                                      si->name, si->value, si->value_len, 0,
7211                                      xattr_ac, data_ac);
7212 }
7213
7214 struct xattr_handler ocfs2_xattr_security_handler = {
7215         .prefix = XATTR_SECURITY_PREFIX,
7216         .list   = ocfs2_xattr_security_list,
7217         .get    = ocfs2_xattr_security_get,
7218         .set    = ocfs2_xattr_security_set,
7219 };
7220
7221 /*
7222  * 'trusted' attributes support
7223  */
7224 static size_t ocfs2_xattr_trusted_list(struct dentry *dentry, char *list,
7225                                        size_t list_size, const char *name,
7226                                        size_t name_len, int type)
7227 {
7228         const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
7229         const size_t total_len = prefix_len + name_len + 1;
7230
7231         if (list && total_len <= list_size) {
7232                 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
7233                 memcpy(list + prefix_len, name, name_len);
7234                 list[prefix_len + name_len] = '\0';
7235         }
7236         return total_len;
7237 }
7238
7239 static int ocfs2_xattr_trusted_get(struct dentry *dentry, const char *name,
7240                 void *buffer, size_t size, int type)
7241 {
7242         if (strcmp(name, "") == 0)
7243                 return -EINVAL;
7244         return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
7245                                name, buffer, size);
7246 }
7247
7248 static int ocfs2_xattr_trusted_set(struct dentry *dentry, const char *name,
7249                 const void *value, size_t size, int flags, int type)
7250 {
7251         if (strcmp(name, "") == 0)
7252                 return -EINVAL;
7253
7254         return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
7255                                name, value, size, flags);
7256 }
7257
7258 struct xattr_handler ocfs2_xattr_trusted_handler = {
7259         .prefix = XATTR_TRUSTED_PREFIX,
7260         .list   = ocfs2_xattr_trusted_list,
7261         .get    = ocfs2_xattr_trusted_get,
7262         .set    = ocfs2_xattr_trusted_set,
7263 };
7264
7265 /*
7266  * 'user' attributes support
7267  */
7268 static size_t ocfs2_xattr_user_list(struct dentry *dentry, char *list,
7269                                     size_t list_size, const char *name,
7270                                     size_t name_len, int type)
7271 {
7272         const size_t prefix_len = XATTR_USER_PREFIX_LEN;
7273         const size_t total_len = prefix_len + name_len + 1;
7274         struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
7275
7276         if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7277                 return 0;
7278
7279         if (list && total_len <= list_size) {
7280                 memcpy(list, XATTR_USER_PREFIX, prefix_len);
7281                 memcpy(list + prefix_len, name, name_len);
7282                 list[prefix_len + name_len] = '\0';
7283         }
7284         return total_len;
7285 }
7286
7287 static int ocfs2_xattr_user_get(struct dentry *dentry, const char *name,
7288                 void *buffer, size_t size, int type)
7289 {
7290         struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
7291
7292         if (strcmp(name, "") == 0)
7293                 return -EINVAL;
7294         if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7295                 return -EOPNOTSUPP;
7296         return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_USER, name,
7297                                buffer, size);
7298 }
7299
7300 static int ocfs2_xattr_user_set(struct dentry *dentry, const char *name,
7301                 const void *value, size_t size, int flags, int type)
7302 {
7303         struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
7304
7305         if (strcmp(name, "") == 0)
7306                 return -EINVAL;
7307         if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7308                 return -EOPNOTSUPP;
7309
7310         return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_USER,
7311                                name, value, size, flags);
7312 }
7313
7314 struct xattr_handler ocfs2_xattr_user_handler = {
7315         .prefix = XATTR_USER_PREFIX,
7316         .list   = ocfs2_xattr_user_list,
7317         .get    = ocfs2_xattr_user_get,
7318         .set    = ocfs2_xattr_user_set,
7319 };