ocfs2: Prefix the member fields of struct ocfs2_xattr_info.
[linux-2.6.git] / 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         const void      *xi_value;
122         size_t          xi_value_len;
123 };
124
125 struct ocfs2_xattr_search {
126         struct buffer_head *inode_bh;
127         /*
128          * xattr_bh point to the block buffer head which has extended attribute
129          * when extended attribute in inode, xattr_bh is equal to inode_bh.
130          */
131         struct buffer_head *xattr_bh;
132         struct ocfs2_xattr_header *header;
133         struct ocfs2_xattr_bucket *bucket;
134         void *base;
135         void *end;
136         struct ocfs2_xattr_entry *here;
137         int not_found;
138 };
139
140 /* Operations on struct ocfs2_xa_entry */
141 struct ocfs2_xa_loc;
142 struct ocfs2_xa_loc_operations {
143         /*
144          * Return a pointer to the appropriate buffer in loc->xl_storage
145          * at the given offset from loc->xl_header.
146          */
147         void *(*xlo_offset_pointer)(struct ocfs2_xa_loc *loc, int offset);
148
149         /*
150          * Remove the name+value at this location.  Do whatever is
151          * appropriate with the remaining name+value pairs.
152          */
153         void (*xlo_wipe_namevalue)(struct ocfs2_xa_loc *loc);
154 };
155
156 /*
157  * Describes an xattr entry location.  This is a memory structure
158  * tracking the on-disk structure.
159  */
160 struct ocfs2_xa_loc {
161         /* The ocfs2_xattr_header inside the on-disk storage. Not NULL. */
162         struct ocfs2_xattr_header *xl_header;
163
164         /* Bytes from xl_header to the end of the storage */
165         int xl_size;
166
167         /*
168          * The ocfs2_xattr_entry this location describes.  If this is
169          * NULL, this location describes the on-disk structure where it
170          * would have been.
171          */
172         struct ocfs2_xattr_entry *xl_entry;
173
174         /*
175          * Internal housekeeping
176          */
177
178         /* Buffer(s) containing this entry */
179         void *xl_storage;
180
181         /* Operations on the storage backing this location */
182         const struct ocfs2_xa_loc_operations *xl_ops;
183 };
184
185 static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
186                                              struct ocfs2_xattr_header *xh,
187                                              int index,
188                                              int *block_off,
189                                              int *new_offset);
190
191 static int ocfs2_xattr_block_find(struct inode *inode,
192                                   int name_index,
193                                   const char *name,
194                                   struct ocfs2_xattr_search *xs);
195 static int ocfs2_xattr_index_block_find(struct inode *inode,
196                                         struct buffer_head *root_bh,
197                                         int name_index,
198                                         const char *name,
199                                         struct ocfs2_xattr_search *xs);
200
201 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
202                                         struct buffer_head *blk_bh,
203                                         char *buffer,
204                                         size_t buffer_size);
205
206 static int ocfs2_xattr_create_index_block(struct inode *inode,
207                                           struct ocfs2_xattr_search *xs,
208                                           struct ocfs2_xattr_set_ctxt *ctxt);
209
210 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
211                                              struct ocfs2_xattr_info *xi,
212                                              struct ocfs2_xattr_search *xs,
213                                              struct ocfs2_xattr_set_ctxt *ctxt);
214
215 typedef int (xattr_tree_rec_func)(struct inode *inode,
216                                   struct buffer_head *root_bh,
217                                   u64 blkno, u32 cpos, u32 len, void *para);
218 static int ocfs2_iterate_xattr_index_block(struct inode *inode,
219                                            struct buffer_head *root_bh,
220                                            xattr_tree_rec_func *rec_func,
221                                            void *para);
222 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
223                                         struct ocfs2_xattr_bucket *bucket,
224                                         void *para);
225 static int ocfs2_rm_xattr_cluster(struct inode *inode,
226                                   struct buffer_head *root_bh,
227                                   u64 blkno,
228                                   u32 cpos,
229                                   u32 len,
230                                   void *para);
231
232 static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
233                                   u64 src_blk, u64 last_blk, u64 to_blk,
234                                   unsigned int start_bucket,
235                                   u32 *first_hash);
236 static int ocfs2_prepare_refcount_xattr(struct inode *inode,
237                                         struct ocfs2_dinode *di,
238                                         struct ocfs2_xattr_info *xi,
239                                         struct ocfs2_xattr_search *xis,
240                                         struct ocfs2_xattr_search *xbs,
241                                         struct ocfs2_refcount_tree **ref_tree,
242                                         int *meta_need,
243                                         int *credits);
244 static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
245                                            struct ocfs2_xattr_bucket *bucket,
246                                            int offset,
247                                            struct ocfs2_xattr_value_root **xv,
248                                            struct buffer_head **bh);
249
250 static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
251 {
252         return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
253 }
254
255 static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
256 {
257         return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
258 }
259
260 static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb)
261 {
262         u16 len = sb->s_blocksize -
263                  offsetof(struct ocfs2_xattr_header, xh_entries);
264
265         return len / sizeof(struct ocfs2_xattr_entry);
266 }
267
268 #define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
269 #define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
270 #define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
271
272 static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
273 {
274         struct ocfs2_xattr_bucket *bucket;
275         int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
276
277         BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
278
279         bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
280         if (bucket) {
281                 bucket->bu_inode = inode;
282                 bucket->bu_blocks = blks;
283         }
284
285         return bucket;
286 }
287
288 static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
289 {
290         int i;
291
292         for (i = 0; i < bucket->bu_blocks; i++) {
293                 brelse(bucket->bu_bhs[i]);
294                 bucket->bu_bhs[i] = NULL;
295         }
296 }
297
298 static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
299 {
300         if (bucket) {
301                 ocfs2_xattr_bucket_relse(bucket);
302                 bucket->bu_inode = NULL;
303                 kfree(bucket);
304         }
305 }
306
307 /*
308  * A bucket that has never been written to disk doesn't need to be
309  * read.  We just need the buffer_heads.  Don't call this for
310  * buckets that are already on disk.  ocfs2_read_xattr_bucket() initializes
311  * them fully.
312  */
313 static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
314                                    u64 xb_blkno)
315 {
316         int i, rc = 0;
317
318         for (i = 0; i < bucket->bu_blocks; i++) {
319                 bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
320                                               xb_blkno + i);
321                 if (!bucket->bu_bhs[i]) {
322                         rc = -EIO;
323                         mlog_errno(rc);
324                         break;
325                 }
326
327                 if (!ocfs2_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
328                                            bucket->bu_bhs[i]))
329                         ocfs2_set_new_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
330                                                       bucket->bu_bhs[i]);
331         }
332
333         if (rc)
334                 ocfs2_xattr_bucket_relse(bucket);
335         return rc;
336 }
337
338 /* Read the xattr bucket at xb_blkno */
339 static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
340                                    u64 xb_blkno)
341 {
342         int rc;
343
344         rc = ocfs2_read_blocks(INODE_CACHE(bucket->bu_inode), xb_blkno,
345                                bucket->bu_blocks, bucket->bu_bhs, 0,
346                                NULL);
347         if (!rc) {
348                 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
349                 rc = ocfs2_validate_meta_ecc_bhs(bucket->bu_inode->i_sb,
350                                                  bucket->bu_bhs,
351                                                  bucket->bu_blocks,
352                                                  &bucket_xh(bucket)->xh_check);
353                 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
354                 if (rc)
355                         mlog_errno(rc);
356         }
357
358         if (rc)
359                 ocfs2_xattr_bucket_relse(bucket);
360         return rc;
361 }
362
363 static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
364                                              struct ocfs2_xattr_bucket *bucket,
365                                              int type)
366 {
367         int i, rc = 0;
368
369         for (i = 0; i < bucket->bu_blocks; i++) {
370                 rc = ocfs2_journal_access(handle,
371                                           INODE_CACHE(bucket->bu_inode),
372                                           bucket->bu_bhs[i], type);
373                 if (rc) {
374                         mlog_errno(rc);
375                         break;
376                 }
377         }
378
379         return rc;
380 }
381
382 static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
383                                              struct ocfs2_xattr_bucket *bucket)
384 {
385         int i;
386
387         spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
388         ocfs2_compute_meta_ecc_bhs(bucket->bu_inode->i_sb,
389                                    bucket->bu_bhs, bucket->bu_blocks,
390                                    &bucket_xh(bucket)->xh_check);
391         spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
392
393         for (i = 0; i < bucket->bu_blocks; i++)
394                 ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
395 }
396
397 static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
398                                          struct ocfs2_xattr_bucket *src)
399 {
400         int i;
401         int blocksize = src->bu_inode->i_sb->s_blocksize;
402
403         BUG_ON(dest->bu_blocks != src->bu_blocks);
404         BUG_ON(dest->bu_inode != src->bu_inode);
405
406         for (i = 0; i < src->bu_blocks; i++) {
407                 memcpy(bucket_block(dest, i), bucket_block(src, i),
408                        blocksize);
409         }
410 }
411
412 static int ocfs2_validate_xattr_block(struct super_block *sb,
413                                       struct buffer_head *bh)
414 {
415         int rc;
416         struct ocfs2_xattr_block *xb =
417                 (struct ocfs2_xattr_block *)bh->b_data;
418
419         mlog(0, "Validating xattr block %llu\n",
420              (unsigned long long)bh->b_blocknr);
421
422         BUG_ON(!buffer_uptodate(bh));
423
424         /*
425          * If the ecc fails, we return the error but otherwise
426          * leave the filesystem running.  We know any error is
427          * local to this block.
428          */
429         rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &xb->xb_check);
430         if (rc)
431                 return rc;
432
433         /*
434          * Errors after here are fatal
435          */
436
437         if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
438                 ocfs2_error(sb,
439                             "Extended attribute block #%llu has bad "
440                             "signature %.*s",
441                             (unsigned long long)bh->b_blocknr, 7,
442                             xb->xb_signature);
443                 return -EINVAL;
444         }
445
446         if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) {
447                 ocfs2_error(sb,
448                             "Extended attribute block #%llu has an "
449                             "invalid xb_blkno of %llu",
450                             (unsigned long long)bh->b_blocknr,
451                             (unsigned long long)le64_to_cpu(xb->xb_blkno));
452                 return -EINVAL;
453         }
454
455         if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) {
456                 ocfs2_error(sb,
457                             "Extended attribute block #%llu has an invalid "
458                             "xb_fs_generation of #%u",
459                             (unsigned long long)bh->b_blocknr,
460                             le32_to_cpu(xb->xb_fs_generation));
461                 return -EINVAL;
462         }
463
464         return 0;
465 }
466
467 static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno,
468                                   struct buffer_head **bh)
469 {
470         int rc;
471         struct buffer_head *tmp = *bh;
472
473         rc = ocfs2_read_block(INODE_CACHE(inode), xb_blkno, &tmp,
474                               ocfs2_validate_xattr_block);
475
476         /* If ocfs2_read_block() got us a new bh, pass it up. */
477         if (!rc && !*bh)
478                 *bh = tmp;
479
480         return rc;
481 }
482
483 static inline const char *ocfs2_xattr_prefix(int name_index)
484 {
485         struct xattr_handler *handler = NULL;
486
487         if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
488                 handler = ocfs2_xattr_handler_map[name_index];
489
490         return handler ? handler->prefix : NULL;
491 }
492
493 static u32 ocfs2_xattr_name_hash(struct inode *inode,
494                                  const char *name,
495                                  int name_len)
496 {
497         /* Get hash value of uuid from super block */
498         u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
499         int i;
500
501         /* hash extended attribute name */
502         for (i = 0; i < name_len; i++) {
503                 hash = (hash << OCFS2_HASH_SHIFT) ^
504                        (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
505                        *name++;
506         }
507
508         return hash;
509 }
510
511 /*
512  * ocfs2_xattr_hash_entry()
513  *
514  * Compute the hash of an extended attribute.
515  */
516 static void ocfs2_xattr_hash_entry(struct inode *inode,
517                                    struct ocfs2_xattr_header *header,
518                                    struct ocfs2_xattr_entry *entry)
519 {
520         u32 hash = 0;
521         char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
522
523         hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
524         entry->xe_name_hash = cpu_to_le32(hash);
525
526         return;
527 }
528
529 static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
530 {
531         int size = 0;
532
533         if (value_len <= OCFS2_XATTR_INLINE_SIZE)
534                 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
535         else
536                 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
537         size += sizeof(struct ocfs2_xattr_entry);
538
539         return size;
540 }
541
542 int ocfs2_calc_security_init(struct inode *dir,
543                              struct ocfs2_security_xattr_info *si,
544                              int *want_clusters,
545                              int *xattr_credits,
546                              struct ocfs2_alloc_context **xattr_ac)
547 {
548         int ret = 0;
549         struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
550         int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
551                                                  si->value_len);
552
553         /*
554          * The max space of security xattr taken inline is
555          * 256(name) + 80(value) + 16(entry) = 352 bytes,
556          * So reserve one metadata block for it is ok.
557          */
558         if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
559             s_size > OCFS2_XATTR_FREE_IN_IBODY) {
560                 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
561                 if (ret) {
562                         mlog_errno(ret);
563                         return ret;
564                 }
565                 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
566         }
567
568         /* reserve clusters for xattr value which will be set in B tree*/
569         if (si->value_len > OCFS2_XATTR_INLINE_SIZE) {
570                 int new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
571                                                             si->value_len);
572
573                 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
574                                                            new_clusters);
575                 *want_clusters += new_clusters;
576         }
577         return ret;
578 }
579
580 int ocfs2_calc_xattr_init(struct inode *dir,
581                           struct buffer_head *dir_bh,
582                           int mode,
583                           struct ocfs2_security_xattr_info *si,
584                           int *want_clusters,
585                           int *xattr_credits,
586                           int *want_meta)
587 {
588         int ret = 0;
589         struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
590         int s_size = 0, a_size = 0, acl_len = 0, new_clusters;
591
592         if (si->enable)
593                 s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
594                                                      si->value_len);
595
596         if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
597                 acl_len = ocfs2_xattr_get_nolock(dir, dir_bh,
598                                         OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
599                                         "", NULL, 0);
600                 if (acl_len > 0) {
601                         a_size = ocfs2_xattr_entry_real_size(0, acl_len);
602                         if (S_ISDIR(mode))
603                                 a_size <<= 1;
604                 } else if (acl_len != 0 && acl_len != -ENODATA) {
605                         mlog_errno(ret);
606                         return ret;
607                 }
608         }
609
610         if (!(s_size + a_size))
611                 return ret;
612
613         /*
614          * The max space of security xattr taken inline is
615          * 256(name) + 80(value) + 16(entry) = 352 bytes,
616          * The max space of acl xattr taken inline is
617          * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
618          * when blocksize = 512, may reserve one more cluser for
619          * xattr bucket, otherwise reserve one metadata block
620          * for them is ok.
621          * If this is a new directory with inline data,
622          * we choose to reserve the entire inline area for
623          * directory contents and force an external xattr block.
624          */
625         if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
626             (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) ||
627             (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) {
628                 *want_meta = *want_meta + 1;
629                 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
630         }
631
632         if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
633             (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
634                 *want_clusters += 1;
635                 *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
636         }
637
638         /*
639          * reserve credits and clusters for xattrs which has large value
640          * and have to be set outside
641          */
642         if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE) {
643                 new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
644                                                         si->value_len);
645                 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
646                                                            new_clusters);
647                 *want_clusters += new_clusters;
648         }
649         if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
650             acl_len > OCFS2_XATTR_INLINE_SIZE) {
651                 /* for directory, it has DEFAULT and ACCESS two types of acls */
652                 new_clusters = (S_ISDIR(mode) ? 2 : 1) *
653                                 ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
654                 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
655                                                            new_clusters);
656                 *want_clusters += new_clusters;
657         }
658
659         return ret;
660 }
661
662 static int ocfs2_xattr_extend_allocation(struct inode *inode,
663                                          u32 clusters_to_add,
664                                          struct ocfs2_xattr_value_buf *vb,
665                                          struct ocfs2_xattr_set_ctxt *ctxt)
666 {
667         int status = 0;
668         handle_t *handle = ctxt->handle;
669         enum ocfs2_alloc_restarted why;
670         u32 prev_clusters, logical_start = le32_to_cpu(vb->vb_xv->xr_clusters);
671         struct ocfs2_extent_tree et;
672
673         mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
674
675         ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
676
677         status = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
678                               OCFS2_JOURNAL_ACCESS_WRITE);
679         if (status < 0) {
680                 mlog_errno(status);
681                 goto leave;
682         }
683
684         prev_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
685         status = ocfs2_add_clusters_in_btree(handle,
686                                              &et,
687                                              &logical_start,
688                                              clusters_to_add,
689                                              0,
690                                              ctxt->data_ac,
691                                              ctxt->meta_ac,
692                                              &why);
693         if (status < 0) {
694                 mlog_errno(status);
695                 goto leave;
696         }
697
698         status = ocfs2_journal_dirty(handle, vb->vb_bh);
699         if (status < 0) {
700                 mlog_errno(status);
701                 goto leave;
702         }
703
704         clusters_to_add -= le32_to_cpu(vb->vb_xv->xr_clusters) - prev_clusters;
705
706         /*
707          * We should have already allocated enough space before the transaction,
708          * so no need to restart.
709          */
710         BUG_ON(why != RESTART_NONE || clusters_to_add);
711
712 leave:
713
714         return status;
715 }
716
717 static int __ocfs2_remove_xattr_range(struct inode *inode,
718                                       struct ocfs2_xattr_value_buf *vb,
719                                       u32 cpos, u32 phys_cpos, u32 len,
720                                       unsigned int ext_flags,
721                                       struct ocfs2_xattr_set_ctxt *ctxt)
722 {
723         int ret;
724         u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
725         handle_t *handle = ctxt->handle;
726         struct ocfs2_extent_tree et;
727
728         ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
729
730         ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
731                             OCFS2_JOURNAL_ACCESS_WRITE);
732         if (ret) {
733                 mlog_errno(ret);
734                 goto out;
735         }
736
737         ret = ocfs2_remove_extent(handle, &et, cpos, len, ctxt->meta_ac,
738                                   &ctxt->dealloc);
739         if (ret) {
740                 mlog_errno(ret);
741                 goto out;
742         }
743
744         le32_add_cpu(&vb->vb_xv->xr_clusters, -len);
745
746         ret = ocfs2_journal_dirty(handle, vb->vb_bh);
747         if (ret) {
748                 mlog_errno(ret);
749                 goto out;
750         }
751
752         if (ext_flags & OCFS2_EXT_REFCOUNTED)
753                 ret = ocfs2_decrease_refcount(inode, handle,
754                                         ocfs2_blocks_to_clusters(inode->i_sb,
755                                                                  phys_blkno),
756                                         len, ctxt->meta_ac, &ctxt->dealloc, 1);
757         else
758                 ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc,
759                                                   phys_blkno, len);
760         if (ret)
761                 mlog_errno(ret);
762
763 out:
764         return ret;
765 }
766
767 static int ocfs2_xattr_shrink_size(struct inode *inode,
768                                    u32 old_clusters,
769                                    u32 new_clusters,
770                                    struct ocfs2_xattr_value_buf *vb,
771                                    struct ocfs2_xattr_set_ctxt *ctxt)
772 {
773         int ret = 0;
774         unsigned int ext_flags;
775         u32 trunc_len, cpos, phys_cpos, alloc_size;
776         u64 block;
777
778         if (old_clusters <= new_clusters)
779                 return 0;
780
781         cpos = new_clusters;
782         trunc_len = old_clusters - new_clusters;
783         while (trunc_len) {
784                 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
785                                                &alloc_size,
786                                                &vb->vb_xv->xr_list, &ext_flags);
787                 if (ret) {
788                         mlog_errno(ret);
789                         goto out;
790                 }
791
792                 if (alloc_size > trunc_len)
793                         alloc_size = trunc_len;
794
795                 ret = __ocfs2_remove_xattr_range(inode, vb, cpos,
796                                                  phys_cpos, alloc_size,
797                                                  ext_flags, ctxt);
798                 if (ret) {
799                         mlog_errno(ret);
800                         goto out;
801                 }
802
803                 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
804                 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode),
805                                                        block, alloc_size);
806                 cpos += alloc_size;
807                 trunc_len -= alloc_size;
808         }
809
810 out:
811         return ret;
812 }
813
814 static int ocfs2_xattr_value_truncate(struct inode *inode,
815                                       struct ocfs2_xattr_value_buf *vb,
816                                       int len,
817                                       struct ocfs2_xattr_set_ctxt *ctxt)
818 {
819         int ret;
820         u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
821         u32 old_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
822
823         if (new_clusters == old_clusters)
824                 return 0;
825
826         if (new_clusters > old_clusters)
827                 ret = ocfs2_xattr_extend_allocation(inode,
828                                                     new_clusters - old_clusters,
829                                                     vb, ctxt);
830         else
831                 ret = ocfs2_xattr_shrink_size(inode,
832                                               old_clusters, new_clusters,
833                                               vb, ctxt);
834
835         return ret;
836 }
837
838 static int ocfs2_xattr_list_entry(char *buffer, size_t size,
839                                   size_t *result, const char *prefix,
840                                   const char *name, int name_len)
841 {
842         char *p = buffer + *result;
843         int prefix_len = strlen(prefix);
844         int total_len = prefix_len + name_len + 1;
845
846         *result += total_len;
847
848         /* we are just looking for how big our buffer needs to be */
849         if (!size)
850                 return 0;
851
852         if (*result > size)
853                 return -ERANGE;
854
855         memcpy(p, prefix, prefix_len);
856         memcpy(p + prefix_len, name, name_len);
857         p[prefix_len + name_len] = '\0';
858
859         return 0;
860 }
861
862 static int ocfs2_xattr_list_entries(struct inode *inode,
863                                     struct ocfs2_xattr_header *header,
864                                     char *buffer, size_t buffer_size)
865 {
866         size_t result = 0;
867         int i, type, ret;
868         const char *prefix, *name;
869
870         for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
871                 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
872                 type = ocfs2_xattr_get_type(entry);
873                 prefix = ocfs2_xattr_prefix(type);
874
875                 if (prefix) {
876                         name = (const char *)header +
877                                 le16_to_cpu(entry->xe_name_offset);
878
879                         ret = ocfs2_xattr_list_entry(buffer, buffer_size,
880                                                      &result, prefix, name,
881                                                      entry->xe_name_len);
882                         if (ret)
883                                 return ret;
884                 }
885         }
886
887         return result;
888 }
889
890 int ocfs2_has_inline_xattr_value_outside(struct inode *inode,
891                                          struct ocfs2_dinode *di)
892 {
893         struct ocfs2_xattr_header *xh;
894         int i;
895
896         xh = (struct ocfs2_xattr_header *)
897                  ((void *)di + inode->i_sb->s_blocksize -
898                  le16_to_cpu(di->i_xattr_inline_size));
899
900         for (i = 0; i < le16_to_cpu(xh->xh_count); i++)
901                 if (!ocfs2_xattr_is_local(&xh->xh_entries[i]))
902                         return 1;
903
904         return 0;
905 }
906
907 static int ocfs2_xattr_ibody_list(struct inode *inode,
908                                   struct ocfs2_dinode *di,
909                                   char *buffer,
910                                   size_t buffer_size)
911 {
912         struct ocfs2_xattr_header *header = NULL;
913         struct ocfs2_inode_info *oi = OCFS2_I(inode);
914         int ret = 0;
915
916         if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
917                 return ret;
918
919         header = (struct ocfs2_xattr_header *)
920                  ((void *)di + inode->i_sb->s_blocksize -
921                  le16_to_cpu(di->i_xattr_inline_size));
922
923         ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
924
925         return ret;
926 }
927
928 static int ocfs2_xattr_block_list(struct inode *inode,
929                                   struct ocfs2_dinode *di,
930                                   char *buffer,
931                                   size_t buffer_size)
932 {
933         struct buffer_head *blk_bh = NULL;
934         struct ocfs2_xattr_block *xb;
935         int ret = 0;
936
937         if (!di->i_xattr_loc)
938                 return ret;
939
940         ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
941                                      &blk_bh);
942         if (ret < 0) {
943                 mlog_errno(ret);
944                 return ret;
945         }
946
947         xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
948         if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
949                 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
950                 ret = ocfs2_xattr_list_entries(inode, header,
951                                                buffer, buffer_size);
952         } else
953                 ret = ocfs2_xattr_tree_list_index_block(inode, blk_bh,
954                                                    buffer, buffer_size);
955
956         brelse(blk_bh);
957
958         return ret;
959 }
960
961 ssize_t ocfs2_listxattr(struct dentry *dentry,
962                         char *buffer,
963                         size_t size)
964 {
965         int ret = 0, i_ret = 0, b_ret = 0;
966         struct buffer_head *di_bh = NULL;
967         struct ocfs2_dinode *di = NULL;
968         struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
969
970         if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
971                 return -EOPNOTSUPP;
972
973         if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
974                 return ret;
975
976         ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
977         if (ret < 0) {
978                 mlog_errno(ret);
979                 return ret;
980         }
981
982         di = (struct ocfs2_dinode *)di_bh->b_data;
983
984         down_read(&oi->ip_xattr_sem);
985         i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
986         if (i_ret < 0)
987                 b_ret = 0;
988         else {
989                 if (buffer) {
990                         buffer += i_ret;
991                         size -= i_ret;
992                 }
993                 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
994                                                buffer, size);
995                 if (b_ret < 0)
996                         i_ret = 0;
997         }
998         up_read(&oi->ip_xattr_sem);
999         ocfs2_inode_unlock(dentry->d_inode, 0);
1000
1001         brelse(di_bh);
1002
1003         return i_ret + b_ret;
1004 }
1005
1006 static int ocfs2_xattr_find_entry(int name_index,
1007                                   const char *name,
1008                                   struct ocfs2_xattr_search *xs)
1009 {
1010         struct ocfs2_xattr_entry *entry;
1011         size_t name_len;
1012         int i, cmp = 1;
1013
1014         if (name == NULL)
1015                 return -EINVAL;
1016
1017         name_len = strlen(name);
1018         entry = xs->here;
1019         for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
1020                 cmp = name_index - ocfs2_xattr_get_type(entry);
1021                 if (!cmp)
1022                         cmp = name_len - entry->xe_name_len;
1023                 if (!cmp)
1024                         cmp = memcmp(name, (xs->base +
1025                                      le16_to_cpu(entry->xe_name_offset)),
1026                                      name_len);
1027                 if (cmp == 0)
1028                         break;
1029                 entry += 1;
1030         }
1031         xs->here = entry;
1032
1033         return cmp ? -ENODATA : 0;
1034 }
1035
1036 static int ocfs2_xattr_get_value_outside(struct inode *inode,
1037                                          struct ocfs2_xattr_value_root *xv,
1038                                          void *buffer,
1039                                          size_t len)
1040 {
1041         u32 cpos, p_cluster, num_clusters, bpc, clusters;
1042         u64 blkno;
1043         int i, ret = 0;
1044         size_t cplen, blocksize;
1045         struct buffer_head *bh = NULL;
1046         struct ocfs2_extent_list *el;
1047
1048         el = &xv->xr_list;
1049         clusters = le32_to_cpu(xv->xr_clusters);
1050         bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1051         blocksize = inode->i_sb->s_blocksize;
1052
1053         cpos = 0;
1054         while (cpos < clusters) {
1055                 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1056                                                &num_clusters, el, NULL);
1057                 if (ret) {
1058                         mlog_errno(ret);
1059                         goto out;
1060                 }
1061
1062                 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1063                 /* Copy ocfs2_xattr_value */
1064                 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1065                         ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1066                                                &bh, NULL);
1067                         if (ret) {
1068                                 mlog_errno(ret);
1069                                 goto out;
1070                         }
1071
1072                         cplen = len >= blocksize ? blocksize : len;
1073                         memcpy(buffer, bh->b_data, cplen);
1074                         len -= cplen;
1075                         buffer += cplen;
1076
1077                         brelse(bh);
1078                         bh = NULL;
1079                         if (len == 0)
1080                                 break;
1081                 }
1082                 cpos += num_clusters;
1083         }
1084 out:
1085         return ret;
1086 }
1087
1088 static int ocfs2_xattr_ibody_get(struct inode *inode,
1089                                  int name_index,
1090                                  const char *name,
1091                                  void *buffer,
1092                                  size_t buffer_size,
1093                                  struct ocfs2_xattr_search *xs)
1094 {
1095         struct ocfs2_inode_info *oi = OCFS2_I(inode);
1096         struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1097         struct ocfs2_xattr_value_root *xv;
1098         size_t size;
1099         int ret = 0;
1100
1101         if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
1102                 return -ENODATA;
1103
1104         xs->end = (void *)di + inode->i_sb->s_blocksize;
1105         xs->header = (struct ocfs2_xattr_header *)
1106                         (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1107         xs->base = (void *)xs->header;
1108         xs->here = xs->header->xh_entries;
1109
1110         ret = ocfs2_xattr_find_entry(name_index, name, xs);
1111         if (ret)
1112                 return ret;
1113         size = le64_to_cpu(xs->here->xe_value_size);
1114         if (buffer) {
1115                 if (size > buffer_size)
1116                         return -ERANGE;
1117                 if (ocfs2_xattr_is_local(xs->here)) {
1118                         memcpy(buffer, (void *)xs->base +
1119                                le16_to_cpu(xs->here->xe_name_offset) +
1120                                OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
1121                 } else {
1122                         xv = (struct ocfs2_xattr_value_root *)
1123                                 (xs->base + le16_to_cpu(
1124                                  xs->here->xe_name_offset) +
1125                                 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
1126                         ret = ocfs2_xattr_get_value_outside(inode, xv,
1127                                                             buffer, size);
1128                         if (ret < 0) {
1129                                 mlog_errno(ret);
1130                                 return ret;
1131                         }
1132                 }
1133         }
1134
1135         return size;
1136 }
1137
1138 static int ocfs2_xattr_block_get(struct inode *inode,
1139                                  int name_index,
1140                                  const char *name,
1141                                  void *buffer,
1142                                  size_t buffer_size,
1143                                  struct ocfs2_xattr_search *xs)
1144 {
1145         struct ocfs2_xattr_block *xb;
1146         struct ocfs2_xattr_value_root *xv;
1147         size_t size;
1148         int ret = -ENODATA, name_offset, name_len, i;
1149         int uninitialized_var(block_off);
1150
1151         xs->bucket = ocfs2_xattr_bucket_new(inode);
1152         if (!xs->bucket) {
1153                 ret = -ENOMEM;
1154                 mlog_errno(ret);
1155                 goto cleanup;
1156         }
1157
1158         ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
1159         if (ret) {
1160                 mlog_errno(ret);
1161                 goto cleanup;
1162         }
1163
1164         if (xs->not_found) {
1165                 ret = -ENODATA;
1166                 goto cleanup;
1167         }
1168
1169         xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1170         size = le64_to_cpu(xs->here->xe_value_size);
1171         if (buffer) {
1172                 ret = -ERANGE;
1173                 if (size > buffer_size)
1174                         goto cleanup;
1175
1176                 name_offset = le16_to_cpu(xs->here->xe_name_offset);
1177                 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
1178                 i = xs->here - xs->header->xh_entries;
1179
1180                 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
1181                         ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
1182                                                                 bucket_xh(xs->bucket),
1183                                                                 i,
1184                                                                 &block_off,
1185                                                                 &name_offset);
1186                         xs->base = bucket_block(xs->bucket, block_off);
1187                 }
1188                 if (ocfs2_xattr_is_local(xs->here)) {
1189                         memcpy(buffer, (void *)xs->base +
1190                                name_offset + name_len, size);
1191                 } else {
1192                         xv = (struct ocfs2_xattr_value_root *)
1193                                 (xs->base + name_offset + name_len);
1194                         ret = ocfs2_xattr_get_value_outside(inode, xv,
1195                                                             buffer, size);
1196                         if (ret < 0) {
1197                                 mlog_errno(ret);
1198                                 goto cleanup;
1199                         }
1200                 }
1201         }
1202         ret = size;
1203 cleanup:
1204         ocfs2_xattr_bucket_free(xs->bucket);
1205
1206         brelse(xs->xattr_bh);
1207         xs->xattr_bh = NULL;
1208         return ret;
1209 }
1210
1211 int ocfs2_xattr_get_nolock(struct inode *inode,
1212                            struct buffer_head *di_bh,
1213                            int name_index,
1214                            const char *name,
1215                            void *buffer,
1216                            size_t buffer_size)
1217 {
1218         int ret;
1219         struct ocfs2_dinode *di = NULL;
1220         struct ocfs2_inode_info *oi = OCFS2_I(inode);
1221         struct ocfs2_xattr_search xis = {
1222                 .not_found = -ENODATA,
1223         };
1224         struct ocfs2_xattr_search xbs = {
1225                 .not_found = -ENODATA,
1226         };
1227
1228         if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1229                 return -EOPNOTSUPP;
1230
1231         if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1232                 ret = -ENODATA;
1233
1234         xis.inode_bh = xbs.inode_bh = di_bh;
1235         di = (struct ocfs2_dinode *)di_bh->b_data;
1236
1237         down_read(&oi->ip_xattr_sem);
1238         ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1239                                     buffer_size, &xis);
1240         if (ret == -ENODATA && di->i_xattr_loc)
1241                 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1242                                             buffer_size, &xbs);
1243         up_read(&oi->ip_xattr_sem);
1244
1245         return ret;
1246 }
1247
1248 /* ocfs2_xattr_get()
1249  *
1250  * Copy an extended attribute into the buffer provided.
1251  * Buffer is NULL to compute the size of buffer required.
1252  */
1253 static int ocfs2_xattr_get(struct inode *inode,
1254                            int name_index,
1255                            const char *name,
1256                            void *buffer,
1257                            size_t buffer_size)
1258 {
1259         int ret;
1260         struct buffer_head *di_bh = NULL;
1261
1262         ret = ocfs2_inode_lock(inode, &di_bh, 0);
1263         if (ret < 0) {
1264                 mlog_errno(ret);
1265                 return ret;
1266         }
1267         ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
1268                                      name, buffer, buffer_size);
1269
1270         ocfs2_inode_unlock(inode, 0);
1271
1272         brelse(di_bh);
1273
1274         return ret;
1275 }
1276
1277 static int __ocfs2_xattr_set_value_outside(struct inode *inode,
1278                                            handle_t *handle,
1279                                            struct ocfs2_xattr_value_buf *vb,
1280                                            const void *value,
1281                                            int value_len)
1282 {
1283         int ret = 0, i, cp_len;
1284         u16 blocksize = inode->i_sb->s_blocksize;
1285         u32 p_cluster, num_clusters;
1286         u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1287         u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1288         u64 blkno;
1289         struct buffer_head *bh = NULL;
1290         unsigned int ext_flags;
1291         struct ocfs2_xattr_value_root *xv = vb->vb_xv;
1292
1293         BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1294
1295         while (cpos < clusters) {
1296                 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1297                                                &num_clusters, &xv->xr_list,
1298                                                &ext_flags);
1299                 if (ret) {
1300                         mlog_errno(ret);
1301                         goto out;
1302                 }
1303
1304                 BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED);
1305
1306                 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1307
1308                 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1309                         ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1310                                                &bh, NULL);
1311                         if (ret) {
1312                                 mlog_errno(ret);
1313                                 goto out;
1314                         }
1315
1316                         ret = ocfs2_journal_access(handle,
1317                                                    INODE_CACHE(inode),
1318                                                    bh,
1319                                                    OCFS2_JOURNAL_ACCESS_WRITE);
1320                         if (ret < 0) {
1321                                 mlog_errno(ret);
1322                                 goto out;
1323                         }
1324
1325                         cp_len = value_len > blocksize ? blocksize : value_len;
1326                         memcpy(bh->b_data, value, cp_len);
1327                         value_len -= cp_len;
1328                         value += cp_len;
1329                         if (cp_len < blocksize)
1330                                 memset(bh->b_data + cp_len, 0,
1331                                        blocksize - cp_len);
1332
1333                         ret = ocfs2_journal_dirty(handle, bh);
1334                         if (ret < 0) {
1335                                 mlog_errno(ret);
1336                                 goto out;
1337                         }
1338                         brelse(bh);
1339                         bh = NULL;
1340
1341                         /*
1342                          * XXX: do we need to empty all the following
1343                          * blocks in this cluster?
1344                          */
1345                         if (!value_len)
1346                                 break;
1347                 }
1348                 cpos += num_clusters;
1349         }
1350 out:
1351         brelse(bh);
1352
1353         return ret;
1354 }
1355
1356 static int ocfs2_xattr_cleanup(struct inode *inode,
1357                                handle_t *handle,
1358                                struct ocfs2_xattr_info *xi,
1359                                struct ocfs2_xattr_search *xs,
1360                                struct ocfs2_xattr_value_buf *vb,
1361                                size_t offs)
1362 {
1363         int ret = 0;
1364         size_t name_len = strlen(xi->xi_name);
1365         void *val = xs->base + offs;
1366         size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1367
1368         ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
1369                             OCFS2_JOURNAL_ACCESS_WRITE);
1370         if (ret) {
1371                 mlog_errno(ret);
1372                 goto out;
1373         }
1374         /* Decrease xattr count */
1375         le16_add_cpu(&xs->header->xh_count, -1);
1376         /* Remove the xattr entry and tree root which has already be set*/
1377         memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
1378         memset(val, 0, size);
1379
1380         ret = ocfs2_journal_dirty(handle, vb->vb_bh);
1381         if (ret < 0)
1382                 mlog_errno(ret);
1383 out:
1384         return ret;
1385 }
1386
1387 static int ocfs2_xattr_update_entry(struct inode *inode,
1388                                     handle_t *handle,
1389                                     struct ocfs2_xattr_info *xi,
1390                                     struct ocfs2_xattr_search *xs,
1391                                     struct ocfs2_xattr_value_buf *vb,
1392                                     size_t offs)
1393 {
1394         int ret;
1395
1396         ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
1397                             OCFS2_JOURNAL_ACCESS_WRITE);
1398         if (ret) {
1399                 mlog_errno(ret);
1400                 goto out;
1401         }
1402
1403         xs->here->xe_name_offset = cpu_to_le16(offs);
1404         xs->here->xe_value_size = cpu_to_le64(xi->xi_value_len);
1405         if (xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE)
1406                 ocfs2_xattr_set_local(xs->here, 1);
1407         else
1408                 ocfs2_xattr_set_local(xs->here, 0);
1409         ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1410
1411         ret = ocfs2_journal_dirty(handle, vb->vb_bh);
1412         if (ret < 0)
1413                 mlog_errno(ret);
1414 out:
1415         return ret;
1416 }
1417
1418 /*
1419  * ocfs2_xattr_set_value_outside()
1420  *
1421  * Set large size value in B tree.
1422  */
1423 static int ocfs2_xattr_set_value_outside(struct inode *inode,
1424                                          struct ocfs2_xattr_info *xi,
1425                                          struct ocfs2_xattr_search *xs,
1426                                          struct ocfs2_xattr_set_ctxt *ctxt,
1427                                          struct ocfs2_xattr_value_buf *vb,
1428                                          size_t offs)
1429 {
1430         size_t name_len = strlen(xi->xi_name);
1431         void *val = xs->base + offs;
1432         struct ocfs2_xattr_value_root *xv = NULL;
1433         size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1434         int ret = 0;
1435
1436         memset(val, 0, size);
1437         memcpy(val, xi->xi_name, name_len);
1438         xv = (struct ocfs2_xattr_value_root *)
1439                 (val + OCFS2_XATTR_SIZE(name_len));
1440         xv->xr_clusters = 0;
1441         xv->xr_last_eb_blk = 0;
1442         xv->xr_list.l_tree_depth = 0;
1443         xv->xr_list.l_count = cpu_to_le16(1);
1444         xv->xr_list.l_next_free_rec = 0;
1445         vb->vb_xv = xv;
1446
1447         ret = ocfs2_xattr_value_truncate(inode, vb, xi->xi_value_len, ctxt);
1448         if (ret < 0) {
1449                 mlog_errno(ret);
1450                 return ret;
1451         }
1452         ret = ocfs2_xattr_update_entry(inode, ctxt->handle, xi, xs, vb, offs);
1453         if (ret < 0) {
1454                 mlog_errno(ret);
1455                 return ret;
1456         }
1457         ret = __ocfs2_xattr_set_value_outside(inode, ctxt->handle, vb,
1458                                               xi->xi_value, xi->xi_value_len);
1459         if (ret < 0)
1460                 mlog_errno(ret);
1461
1462         return ret;
1463 }
1464
1465 /*
1466  * Wipe the name+value pair and allow the storage to reclaim it.  This
1467  * must be followed by either removal of the entry or a call to
1468  * ocfs2_xa_add_namevalue().
1469  */
1470 static void ocfs2_xa_wipe_namevalue(struct ocfs2_xa_loc *loc)
1471 {
1472         loc->xl_ops->xlo_wipe_namevalue(loc);
1473 }
1474
1475 static void *ocfs2_xa_block_offset_pointer(struct ocfs2_xa_loc *loc,
1476                                            int offset)
1477 {
1478         BUG_ON(offset >= loc->xl_size);
1479         return (char *)loc->xl_header + offset;
1480 }
1481
1482 /*
1483  * Block storage for xattrs keeps the name+value pairs compacted.  When
1484  * we remove one, we have to shift any that preceded it towards the end.
1485  */
1486 static void ocfs2_xa_block_wipe_namevalue(struct ocfs2_xa_loc *loc)
1487 {
1488         int i, offset;
1489         int namevalue_offset, first_namevalue_offset, namevalue_size;
1490         struct ocfs2_xattr_entry *entry = loc->xl_entry;
1491         struct ocfs2_xattr_header *xh = loc->xl_header;
1492         u64 value_size = le64_to_cpu(entry->xe_value_size);
1493         int count = le16_to_cpu(xh->xh_count);
1494
1495         namevalue_offset = le16_to_cpu(entry->xe_name_offset);
1496         namevalue_size = OCFS2_XATTR_SIZE(entry->xe_name_len);
1497         if (value_size > OCFS2_XATTR_INLINE_SIZE)
1498                 namevalue_size += OCFS2_XATTR_ROOT_SIZE;
1499         else
1500                 namevalue_size += OCFS2_XATTR_SIZE(value_size);
1501
1502         for (i = 0, first_namevalue_offset = loc->xl_size;
1503              i < count; i++) {
1504                 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1505                 if (offset < first_namevalue_offset)
1506                         first_namevalue_offset = offset;
1507         }
1508
1509         /* Shift the name+value pairs */
1510         memmove((char *)xh + first_namevalue_offset + namevalue_size,
1511                 (char *)xh + first_namevalue_offset,
1512                 namevalue_offset - first_namevalue_offset);
1513         memset((char *)xh + first_namevalue_offset, 0, namevalue_size);
1514
1515         /* Now tell xh->xh_entries about it */
1516         for (i = 0; i < count; i++) {
1517                 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1518                 if (offset < namevalue_offset)
1519                         le16_add_cpu(&xh->xh_entries[i].xe_name_offset,
1520                                      namevalue_size);
1521         }
1522
1523         /*
1524          * Note that we don't update xh_free_start or xh_name_value_len
1525          * because they're not used in block-stored xattrs.
1526          */
1527 }
1528
1529 /*
1530  * Operations for xattrs stored in blocks.  This includes inline inode
1531  * storage and unindexed ocfs2_xattr_blocks.
1532  */
1533 static const struct ocfs2_xa_loc_operations ocfs2_xa_block_loc_ops = {
1534         .xlo_offset_pointer     = ocfs2_xa_block_offset_pointer,
1535         .xlo_wipe_namevalue     = ocfs2_xa_block_wipe_namevalue,
1536 };
1537
1538 static void *ocfs2_xa_bucket_offset_pointer(struct ocfs2_xa_loc *loc,
1539                                             int offset)
1540 {
1541         struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1542         int block, block_offset;
1543
1544         BUG_ON(offset >= OCFS2_XATTR_BUCKET_SIZE);
1545
1546         /* The header is at the front of the bucket */
1547         block = offset >> bucket->bu_inode->i_sb->s_blocksize_bits;
1548         block_offset = offset % bucket->bu_inode->i_sb->s_blocksize;
1549
1550         return bucket_block(bucket, block) + block_offset;
1551 }
1552
1553 static void ocfs2_xa_bucket_wipe_namevalue(struct ocfs2_xa_loc *loc)
1554 {
1555         int namevalue_size;
1556         struct ocfs2_xattr_entry *entry = loc->xl_entry;
1557         u64 value_size = le64_to_cpu(entry->xe_value_size);
1558
1559         namevalue_size = OCFS2_XATTR_SIZE(entry->xe_name_len);
1560         if (value_size > OCFS2_XATTR_INLINE_SIZE)
1561                 namevalue_size += OCFS2_XATTR_ROOT_SIZE;
1562         else
1563                 namevalue_size += OCFS2_XATTR_SIZE(value_size);
1564
1565         le16_add_cpu(&loc->xl_header->xh_name_value_len, -namevalue_size);
1566 }
1567
1568 /* Operations for xattrs stored in buckets. */
1569 static const struct ocfs2_xa_loc_operations ocfs2_xa_bucket_loc_ops = {
1570         .xlo_offset_pointer     = ocfs2_xa_bucket_offset_pointer,
1571         .xlo_wipe_namevalue     = ocfs2_xa_bucket_wipe_namevalue,
1572 };
1573
1574 static void ocfs2_xa_remove_entry(struct ocfs2_xa_loc *loc)
1575 {
1576         int index, count;
1577         struct ocfs2_xattr_header *xh = loc->xl_header;
1578         struct ocfs2_xattr_entry *entry = loc->xl_entry;
1579
1580         ocfs2_xa_wipe_namevalue(loc);
1581         loc->xl_entry = NULL;
1582
1583         le16_add_cpu(&xh->xh_count, -1);
1584         count = le16_to_cpu(xh->xh_count);
1585
1586         /*
1587          * Only zero out the entry if there are more remaining.  This is
1588          * important for an empty bucket, as it keeps track of the
1589          * bucket's hash value.  It doesn't hurt empty block storage.
1590          */
1591         if (count) {
1592                 index = ((char *)entry - (char *)&xh->xh_entries) /
1593                         sizeof(struct ocfs2_xattr_entry);
1594                 memmove(&xh->xh_entries[index], &xh->xh_entries[index + 1],
1595                         (count - index) * sizeof(struct ocfs2_xattr_entry));
1596                 memset(&xh->xh_entries[count], 0,
1597                        sizeof(struct ocfs2_xattr_entry));
1598         }
1599 }
1600
1601 static void ocfs2_init_dinode_xa_loc(struct ocfs2_xa_loc *loc,
1602                                      struct inode *inode,
1603                                      struct buffer_head *bh,
1604                                      struct ocfs2_xattr_entry *entry)
1605 {
1606         struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
1607
1608         loc->xl_ops = &ocfs2_xa_block_loc_ops;
1609         loc->xl_storage = bh;
1610         loc->xl_entry = entry;
1611
1612         if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
1613                 loc->xl_size = le16_to_cpu(di->i_xattr_inline_size);
1614         else {
1615                 BUG_ON(entry);
1616                 loc->xl_size = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
1617         }
1618         loc->xl_header =
1619                 (struct ocfs2_xattr_header *)(bh->b_data + bh->b_size -
1620                                               loc->xl_size);
1621 }
1622
1623 static void ocfs2_init_xattr_block_xa_loc(struct ocfs2_xa_loc *loc,
1624                                           struct buffer_head *bh,
1625                                           struct ocfs2_xattr_entry *entry)
1626 {
1627         struct ocfs2_xattr_block *xb =
1628                 (struct ocfs2_xattr_block *)bh->b_data;
1629
1630         BUG_ON(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED);
1631
1632         loc->xl_ops = &ocfs2_xa_block_loc_ops;
1633         loc->xl_storage = bh;
1634         loc->xl_header = &(xb->xb_attrs.xb_header);
1635         loc->xl_entry = entry;
1636         loc->xl_size = bh->b_size - offsetof(struct ocfs2_xattr_block,
1637                                              xb_attrs.xb_header);
1638 }
1639
1640 static void ocfs2_init_xattr_bucket_xa_loc(struct ocfs2_xa_loc *loc,
1641                                            struct ocfs2_xattr_bucket *bucket,
1642                                            struct ocfs2_xattr_entry *entry)
1643 {
1644         loc->xl_ops = &ocfs2_xa_bucket_loc_ops;
1645         loc->xl_storage = bucket;
1646         loc->xl_header = bucket_xh(bucket);
1647         loc->xl_entry = entry;
1648         loc->xl_size = OCFS2_XATTR_BUCKET_SIZE;
1649 }
1650
1651 /*
1652  * ocfs2_xattr_set_entry_local()
1653  *
1654  * Set, replace or remove extended attribute in local.
1655  */
1656 static void ocfs2_xattr_set_entry_local(struct inode *inode,
1657                                         struct ocfs2_xattr_info *xi,
1658                                         struct ocfs2_xattr_search *xs,
1659                                         struct ocfs2_xattr_entry *last,
1660                                         size_t min_offs)
1661 {
1662         size_t name_len = strlen(xi->xi_name);
1663         struct ocfs2_xa_loc loc;
1664
1665         if (xs->xattr_bh == xs->inode_bh)
1666                 ocfs2_init_dinode_xa_loc(&loc, inode, xs->inode_bh,
1667                                          xs->not_found ? NULL : xs->here);
1668         else
1669                 ocfs2_init_xattr_block_xa_loc(&loc, xs->xattr_bh,
1670                                               xs->not_found ? NULL : xs->here);
1671         if (xi->xi_value && xs->not_found) {
1672                 /* Insert the new xattr entry. */
1673                 le16_add_cpu(&xs->header->xh_count, 1);
1674                 ocfs2_xattr_set_type(last, xi->xi_name_index);
1675                 ocfs2_xattr_set_local(last, 1);
1676                 last->xe_name_len = name_len;
1677         } else {
1678                 void *first_val;
1679                 void *val;
1680                 size_t offs, size;
1681
1682                 first_val = xs->base + min_offs;
1683                 offs = le16_to_cpu(xs->here->xe_name_offset);
1684                 val = xs->base + offs;
1685
1686                 if (le64_to_cpu(xs->here->xe_value_size) >
1687                     OCFS2_XATTR_INLINE_SIZE)
1688                         size = OCFS2_XATTR_SIZE(name_len) +
1689                                 OCFS2_XATTR_ROOT_SIZE;
1690                 else
1691                         size = OCFS2_XATTR_SIZE(name_len) +
1692                         OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1693
1694                 if (xi->xi_value && size == OCFS2_XATTR_SIZE(name_len) +
1695                                 OCFS2_XATTR_SIZE(xi->xi_value_len)) {
1696                         /* The old and the new value have the
1697                            same size. Just replace the value. */
1698                         ocfs2_xattr_set_local(xs->here, 1);
1699                         xs->here->xe_value_size = cpu_to_le64(xi->xi_value_len);
1700                         /* Clear value bytes. */
1701                         memset(val + OCFS2_XATTR_SIZE(name_len),
1702                                0,
1703                                OCFS2_XATTR_SIZE(xi->xi_value_len));
1704                         memcpy(val + OCFS2_XATTR_SIZE(name_len),
1705                                xi->xi_value,
1706                                xi->xi_value_len);
1707                         return;
1708                 }
1709
1710                 if (!xi->xi_value)
1711                         ocfs2_xa_remove_entry(&loc);
1712                 else
1713                         ocfs2_xa_wipe_namevalue(&loc);
1714
1715                 min_offs += size;
1716         }
1717         if (xi->xi_value) {
1718                 /* Insert the new name+value. */
1719                 size_t size = OCFS2_XATTR_SIZE(name_len) +
1720                                 OCFS2_XATTR_SIZE(xi->xi_value_len);
1721                 void *val = xs->base + min_offs - size;
1722
1723                 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1724                 memset(val, 0, size);
1725                 memcpy(val, xi->xi_name, name_len);
1726                 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1727                        xi->xi_value,
1728                        xi->xi_value_len);
1729                 xs->here->xe_value_size = cpu_to_le64(xi->xi_value_len);
1730                 ocfs2_xattr_set_local(xs->here, 1);
1731                 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1732         }
1733
1734         return;
1735 }
1736
1737 /*
1738  * ocfs2_xattr_set_entry()
1739  *
1740  * Set extended attribute entry into inode or block.
1741  *
1742  * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1743  * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1744  * then set value in B tree with set_value_outside().
1745  */
1746 static int ocfs2_xattr_set_entry(struct inode *inode,
1747                                  struct ocfs2_xattr_info *xi,
1748                                  struct ocfs2_xattr_search *xs,
1749                                  struct ocfs2_xattr_set_ctxt *ctxt,
1750                                  int flag)
1751 {
1752         struct ocfs2_xattr_entry *last;
1753         struct ocfs2_inode_info *oi = OCFS2_I(inode);
1754         struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1755         size_t min_offs = xs->end - xs->base, name_len = strlen(xi->xi_name);
1756         size_t size_l = 0;
1757         handle_t *handle = ctxt->handle;
1758         int free, i, ret;
1759         struct ocfs2_xattr_info xi_l = {
1760                 .xi_name_index = xi->xi_name_index,
1761                 .xi_name = xi->xi_name,
1762                 .xi_value = xi->xi_value,
1763                 .xi_value_len = xi->xi_value_len,
1764         };
1765         struct ocfs2_xattr_value_buf vb = {
1766                 .vb_bh = xs->xattr_bh,
1767                 .vb_access = ocfs2_journal_access_di,
1768         };
1769
1770         if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1771                 BUG_ON(xs->xattr_bh == xs->inode_bh);
1772                 vb.vb_access = ocfs2_journal_access_xb;
1773         } else
1774                 BUG_ON(xs->xattr_bh != xs->inode_bh);
1775
1776         /* Compute min_offs, last and free space. */
1777         last = xs->header->xh_entries;
1778
1779         for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1780                 size_t offs = le16_to_cpu(last->xe_name_offset);
1781                 if (offs < min_offs)
1782                         min_offs = offs;
1783                 last += 1;
1784         }
1785
1786         free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
1787         if (free < 0)
1788                 return -EIO;
1789
1790         if (!xs->not_found) {
1791                 size_t size = 0;
1792                 if (ocfs2_xattr_is_local(xs->here))
1793                         size = OCFS2_XATTR_SIZE(name_len) +
1794                         OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1795                 else
1796                         size = OCFS2_XATTR_SIZE(name_len) +
1797                                 OCFS2_XATTR_ROOT_SIZE;
1798                 free += (size + sizeof(struct ocfs2_xattr_entry));
1799         }
1800         /* Check free space in inode or block */
1801         if (xi->xi_value && xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
1802                 if (free < sizeof(struct ocfs2_xattr_entry) +
1803                            OCFS2_XATTR_SIZE(name_len) +
1804                            OCFS2_XATTR_ROOT_SIZE) {
1805                         ret = -ENOSPC;
1806                         goto out;
1807                 }
1808                 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1809                 xi_l.xi_value = (void *)&def_xv;
1810                 xi_l.xi_value_len = OCFS2_XATTR_ROOT_SIZE;
1811         } else if (xi->xi_value) {
1812                 if (free < sizeof(struct ocfs2_xattr_entry) +
1813                            OCFS2_XATTR_SIZE(name_len) +
1814                            OCFS2_XATTR_SIZE(xi->xi_value_len)) {
1815                         ret = -ENOSPC;
1816                         goto out;
1817                 }
1818         }
1819
1820         if (!xs->not_found) {
1821                 /* For existing extended attribute */
1822                 size_t size = OCFS2_XATTR_SIZE(name_len) +
1823                         OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1824                 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1825                 void *val = xs->base + offs;
1826
1827                 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1828                         /* Replace existing local xattr with tree root */
1829                         ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
1830                                                             ctxt, &vb, offs);
1831                         if (ret < 0)
1832                                 mlog_errno(ret);
1833                         goto out;
1834                 } else if (!ocfs2_xattr_is_local(xs->here)) {
1835                         /* For existing xattr which has value outside */
1836                         vb.vb_xv = (struct ocfs2_xattr_value_root *)
1837                                 (val + OCFS2_XATTR_SIZE(name_len));
1838
1839                         if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
1840                                 /*
1841                                  * If new value need set outside also,
1842                                  * first truncate old value to new value,
1843                                  * then set new value with set_value_outside().
1844                                  */
1845                                 ret = ocfs2_xattr_value_truncate(inode,
1846                                                         &vb,
1847                                                         xi->xi_value_len,
1848                                                         ctxt);
1849                                 if (ret < 0) {
1850                                         mlog_errno(ret);
1851                                         goto out;
1852                                 }
1853
1854                                 ret = ocfs2_xattr_update_entry(inode,
1855                                                                handle,
1856                                                                xi,
1857                                                                xs,
1858                                                                &vb,
1859                                                                offs);
1860                                 if (ret < 0) {
1861                                         mlog_errno(ret);
1862                                         goto out;
1863                                 }
1864
1865                                 ret = __ocfs2_xattr_set_value_outside(inode,
1866                                                         handle,
1867                                                         &vb,
1868                                                         xi->xi_value,
1869                                                         xi->xi_value_len);
1870                                 if (ret < 0)
1871                                         mlog_errno(ret);
1872                                 goto out;
1873                         } else {
1874                                 /*
1875                                  * If new value need set in local,
1876                                  * just trucate old value to zero.
1877                                  */
1878                                  ret = ocfs2_xattr_value_truncate(inode,
1879                                                                   &vb,
1880                                                                   0,
1881                                                                   ctxt);
1882                                 if (ret < 0)
1883                                         mlog_errno(ret);
1884                         }
1885                 }
1886         }
1887
1888         ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), xs->inode_bh,
1889                                       OCFS2_JOURNAL_ACCESS_WRITE);
1890         if (ret) {
1891                 mlog_errno(ret);
1892                 goto out;
1893         }
1894
1895         if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1896                 ret = vb.vb_access(handle, INODE_CACHE(inode), vb.vb_bh,
1897                                    OCFS2_JOURNAL_ACCESS_WRITE);
1898                 if (ret) {
1899                         mlog_errno(ret);
1900                         goto out;
1901                 }
1902         }
1903
1904         /*
1905          * Set value in local, include set tree root in local.
1906          * This is the first step for value size >INLINE_SIZE.
1907          */
1908         ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1909
1910         if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1911                 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1912                 if (ret < 0) {
1913                         mlog_errno(ret);
1914                         goto out;
1915                 }
1916         }
1917
1918         if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1919             (flag & OCFS2_INLINE_XATTR_FL)) {
1920                 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1921                 unsigned int xattrsize = osb->s_xattr_inline_size;
1922
1923                 /*
1924                  * Adjust extent record count or inline data size
1925                  * to reserve space for extended attribute.
1926                  */
1927                 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1928                         struct ocfs2_inline_data *idata = &di->id2.i_data;
1929                         le16_add_cpu(&idata->id_count, -xattrsize);
1930                 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1931                         struct ocfs2_extent_list *el = &di->id2.i_list;
1932                         le16_add_cpu(&el->l_count, -(xattrsize /
1933                                         sizeof(struct ocfs2_extent_rec)));
1934                 }
1935                 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1936         }
1937         /* Update xattr flag */
1938         spin_lock(&oi->ip_lock);
1939         oi->ip_dyn_features |= flag;
1940         di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1941         spin_unlock(&oi->ip_lock);
1942
1943         ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1944         if (ret < 0)
1945                 mlog_errno(ret);
1946
1947         if (!ret && xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
1948                 /*
1949                  * Set value outside in B tree.
1950                  * This is the second step for value size > INLINE_SIZE.
1951                  */
1952                 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1953                 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, ctxt,
1954                                                     &vb, offs);
1955                 if (ret < 0) {
1956                         int ret2;
1957
1958                         mlog_errno(ret);
1959                         /*
1960                          * If set value outside failed, we have to clean
1961                          * the junk tree root we have already set in local.
1962                          */
1963                         ret2 = ocfs2_xattr_cleanup(inode, ctxt->handle,
1964                                                    xi, xs, &vb, offs);
1965                         if (ret2 < 0)
1966                                 mlog_errno(ret2);
1967                 }
1968         }
1969 out:
1970         return ret;
1971 }
1972
1973 /*
1974  * In xattr remove, if it is stored outside and refcounted, we may have
1975  * the chance to split the refcount tree. So need the allocators.
1976  */
1977 static int ocfs2_lock_xattr_remove_allocators(struct inode *inode,
1978                                         struct ocfs2_xattr_value_root *xv,
1979                                         struct ocfs2_caching_info *ref_ci,
1980                                         struct buffer_head *ref_root_bh,
1981                                         struct ocfs2_alloc_context **meta_ac,
1982                                         int *ref_credits)
1983 {
1984         int ret, meta_add = 0;
1985         u32 p_cluster, num_clusters;
1986         unsigned int ext_flags;
1987
1988         *ref_credits = 0;
1989         ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
1990                                        &num_clusters,
1991                                        &xv->xr_list,
1992                                        &ext_flags);
1993         if (ret) {
1994                 mlog_errno(ret);
1995                 goto out;
1996         }
1997
1998         if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
1999                 goto out;
2000
2001         ret = ocfs2_refcounted_xattr_delete_need(inode, ref_ci,
2002                                                  ref_root_bh, xv,
2003                                                  &meta_add, ref_credits);
2004         if (ret) {
2005                 mlog_errno(ret);
2006                 goto out;
2007         }
2008
2009         ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
2010                                                 meta_add, meta_ac);
2011         if (ret)
2012                 mlog_errno(ret);
2013
2014 out:
2015         return ret;
2016 }
2017
2018 static int ocfs2_remove_value_outside(struct inode*inode,
2019                                       struct ocfs2_xattr_value_buf *vb,
2020                                       struct ocfs2_xattr_header *header,
2021                                       struct ocfs2_caching_info *ref_ci,
2022                                       struct buffer_head *ref_root_bh)
2023 {
2024         int ret = 0, i, ref_credits;
2025         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2026         struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
2027         void *val;
2028
2029         ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
2030
2031         for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
2032                 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
2033
2034                 if (ocfs2_xattr_is_local(entry))
2035                         continue;
2036
2037                 val = (void *)header +
2038                         le16_to_cpu(entry->xe_name_offset);
2039                 vb->vb_xv = (struct ocfs2_xattr_value_root *)
2040                         (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
2041
2042                 ret = ocfs2_lock_xattr_remove_allocators(inode, vb->vb_xv,
2043                                                          ref_ci, ref_root_bh,
2044                                                          &ctxt.meta_ac,
2045                                                          &ref_credits);
2046
2047                 ctxt.handle = ocfs2_start_trans(osb, ref_credits +
2048                                         ocfs2_remove_extent_credits(osb->sb));
2049                 if (IS_ERR(ctxt.handle)) {
2050                         ret = PTR_ERR(ctxt.handle);
2051                         mlog_errno(ret);
2052                         break;
2053                 }
2054
2055                 ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt);
2056                 if (ret < 0) {
2057                         mlog_errno(ret);
2058                         break;
2059                 }
2060
2061                 ocfs2_commit_trans(osb, ctxt.handle);
2062                 if (ctxt.meta_ac) {
2063                         ocfs2_free_alloc_context(ctxt.meta_ac);
2064                         ctxt.meta_ac = NULL;
2065                 }
2066         }
2067
2068         if (ctxt.meta_ac)
2069                 ocfs2_free_alloc_context(ctxt.meta_ac);
2070         ocfs2_schedule_truncate_log_flush(osb, 1);
2071         ocfs2_run_deallocs(osb, &ctxt.dealloc);
2072         return ret;
2073 }
2074
2075 static int ocfs2_xattr_ibody_remove(struct inode *inode,
2076                                     struct buffer_head *di_bh,
2077                                     struct ocfs2_caching_info *ref_ci,
2078                                     struct buffer_head *ref_root_bh)
2079 {
2080
2081         struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2082         struct ocfs2_xattr_header *header;
2083         int ret;
2084         struct ocfs2_xattr_value_buf vb = {
2085                 .vb_bh = di_bh,
2086                 .vb_access = ocfs2_journal_access_di,
2087         };
2088
2089         header = (struct ocfs2_xattr_header *)
2090                  ((void *)di + inode->i_sb->s_blocksize -
2091                  le16_to_cpu(di->i_xattr_inline_size));
2092
2093         ret = ocfs2_remove_value_outside(inode, &vb, header,
2094                                          ref_ci, ref_root_bh);
2095
2096         return ret;
2097 }
2098
2099 struct ocfs2_rm_xattr_bucket_para {
2100         struct ocfs2_caching_info *ref_ci;
2101         struct buffer_head *ref_root_bh;
2102 };
2103
2104 static int ocfs2_xattr_block_remove(struct inode *inode,
2105                                     struct buffer_head *blk_bh,
2106                                     struct ocfs2_caching_info *ref_ci,
2107                                     struct buffer_head *ref_root_bh)
2108 {
2109         struct ocfs2_xattr_block *xb;
2110         int ret = 0;
2111         struct ocfs2_xattr_value_buf vb = {
2112                 .vb_bh = blk_bh,
2113                 .vb_access = ocfs2_journal_access_xb,
2114         };
2115         struct ocfs2_rm_xattr_bucket_para args = {
2116                 .ref_ci = ref_ci,
2117                 .ref_root_bh = ref_root_bh,
2118         };
2119
2120         xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2121         if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2122                 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
2123                 ret = ocfs2_remove_value_outside(inode, &vb, header,
2124                                                  ref_ci, ref_root_bh);
2125         } else
2126                 ret = ocfs2_iterate_xattr_index_block(inode,
2127                                                 blk_bh,
2128                                                 ocfs2_rm_xattr_cluster,
2129                                                 &args);
2130
2131         return ret;
2132 }
2133
2134 static int ocfs2_xattr_free_block(struct inode *inode,
2135                                   u64 block,
2136                                   struct ocfs2_caching_info *ref_ci,
2137                                   struct buffer_head *ref_root_bh)
2138 {
2139         struct inode *xb_alloc_inode;
2140         struct buffer_head *xb_alloc_bh = NULL;
2141         struct buffer_head *blk_bh = NULL;
2142         struct ocfs2_xattr_block *xb;
2143         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2144         handle_t *handle;
2145         int ret = 0;
2146         u64 blk, bg_blkno;
2147         u16 bit;
2148
2149         ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
2150         if (ret < 0) {
2151                 mlog_errno(ret);
2152                 goto out;
2153         }
2154
2155         ret = ocfs2_xattr_block_remove(inode, blk_bh, ref_ci, ref_root_bh);
2156         if (ret < 0) {
2157                 mlog_errno(ret);
2158                 goto out;
2159         }
2160
2161         xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2162         blk = le64_to_cpu(xb->xb_blkno);
2163         bit = le16_to_cpu(xb->xb_suballoc_bit);
2164         bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2165
2166         xb_alloc_inode = ocfs2_get_system_file_inode(osb,
2167                                 EXTENT_ALLOC_SYSTEM_INODE,
2168                                 le16_to_cpu(xb->xb_suballoc_slot));
2169         if (!xb_alloc_inode) {
2170                 ret = -ENOMEM;
2171                 mlog_errno(ret);
2172                 goto out;
2173         }
2174         mutex_lock(&xb_alloc_inode->i_mutex);
2175
2176         ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
2177         if (ret < 0) {
2178                 mlog_errno(ret);
2179                 goto out_mutex;
2180         }
2181
2182         handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
2183         if (IS_ERR(handle)) {
2184                 ret = PTR_ERR(handle);
2185                 mlog_errno(ret);
2186                 goto out_unlock;
2187         }
2188
2189         ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
2190                                        bit, bg_blkno, 1);
2191         if (ret < 0)
2192                 mlog_errno(ret);
2193
2194         ocfs2_commit_trans(osb, handle);
2195 out_unlock:
2196         ocfs2_inode_unlock(xb_alloc_inode, 1);
2197         brelse(xb_alloc_bh);
2198 out_mutex:
2199         mutex_unlock(&xb_alloc_inode->i_mutex);
2200         iput(xb_alloc_inode);
2201 out:
2202         brelse(blk_bh);
2203         return ret;
2204 }
2205
2206 /*
2207  * ocfs2_xattr_remove()
2208  *
2209  * Free extended attribute resources associated with this inode.
2210  */
2211 int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
2212 {
2213         struct ocfs2_inode_info *oi = OCFS2_I(inode);
2214         struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2215         struct ocfs2_refcount_tree *ref_tree = NULL;
2216         struct buffer_head *ref_root_bh = NULL;
2217         struct ocfs2_caching_info *ref_ci = NULL;
2218         handle_t *handle;
2219         int ret;
2220
2221         if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2222                 return 0;
2223
2224         if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
2225                 return 0;
2226
2227         if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) {
2228                 ret = ocfs2_lock_refcount_tree(OCFS2_SB(inode->i_sb),
2229                                                le64_to_cpu(di->i_refcount_loc),
2230                                                1, &ref_tree, &ref_root_bh);
2231                 if (ret) {
2232                         mlog_errno(ret);
2233                         goto out;
2234                 }
2235                 ref_ci = &ref_tree->rf_ci;
2236
2237         }
2238
2239         if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2240                 ret = ocfs2_xattr_ibody_remove(inode, di_bh,
2241                                                ref_ci, ref_root_bh);
2242                 if (ret < 0) {
2243                         mlog_errno(ret);
2244                         goto out;
2245                 }
2246         }
2247
2248         if (di->i_xattr_loc) {
2249                 ret = ocfs2_xattr_free_block(inode,
2250                                              le64_to_cpu(di->i_xattr_loc),
2251                                              ref_ci, ref_root_bh);
2252                 if (ret < 0) {
2253                         mlog_errno(ret);
2254                         goto out;
2255                 }
2256         }
2257
2258         handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
2259                                    OCFS2_INODE_UPDATE_CREDITS);
2260         if (IS_ERR(handle)) {
2261                 ret = PTR_ERR(handle);
2262                 mlog_errno(ret);
2263                 goto out;
2264         }
2265         ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
2266                                       OCFS2_JOURNAL_ACCESS_WRITE);
2267         if (ret) {
2268                 mlog_errno(ret);
2269                 goto out_commit;
2270         }
2271
2272         di->i_xattr_loc = 0;
2273
2274         spin_lock(&oi->ip_lock);
2275         oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
2276         di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2277         spin_unlock(&oi->ip_lock);
2278
2279         ret = ocfs2_journal_dirty(handle, di_bh);
2280         if (ret < 0)
2281                 mlog_errno(ret);
2282 out_commit:
2283         ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
2284 out:
2285         if (ref_tree)
2286                 ocfs2_unlock_refcount_tree(OCFS2_SB(inode->i_sb), ref_tree, 1);
2287         brelse(ref_root_bh);
2288         return ret;
2289 }
2290
2291 static int ocfs2_xattr_has_space_inline(struct inode *inode,
2292                                         struct ocfs2_dinode *di)
2293 {
2294         struct ocfs2_inode_info *oi = OCFS2_I(inode);
2295         unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
2296         int free;
2297
2298         if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
2299                 return 0;
2300
2301         if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2302                 struct ocfs2_inline_data *idata = &di->id2.i_data;
2303                 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
2304         } else if (ocfs2_inode_is_fast_symlink(inode)) {
2305                 free = ocfs2_fast_symlink_chars(inode->i_sb) -
2306                         le64_to_cpu(di->i_size);
2307         } else {
2308                 struct ocfs2_extent_list *el = &di->id2.i_list;
2309                 free = (le16_to_cpu(el->l_count) -
2310                         le16_to_cpu(el->l_next_free_rec)) *
2311                         sizeof(struct ocfs2_extent_rec);
2312         }
2313         if (free >= xattrsize)
2314                 return 1;
2315
2316         return 0;
2317 }
2318
2319 /*
2320  * ocfs2_xattr_ibody_find()
2321  *
2322  * Find extended attribute in inode block and
2323  * fill search info into struct ocfs2_xattr_search.
2324  */
2325 static int ocfs2_xattr_ibody_find(struct inode *inode,
2326                                   int name_index,
2327                                   const char *name,
2328                                   struct ocfs2_xattr_search *xs)
2329 {
2330         struct ocfs2_inode_info *oi = OCFS2_I(inode);
2331         struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2332         int ret;
2333         int has_space = 0;
2334
2335         if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2336                 return 0;
2337
2338         if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2339                 down_read(&oi->ip_alloc_sem);
2340                 has_space = ocfs2_xattr_has_space_inline(inode, di);
2341                 up_read(&oi->ip_alloc_sem);
2342                 if (!has_space)
2343                         return 0;
2344         }
2345
2346         xs->xattr_bh = xs->inode_bh;
2347         xs->end = (void *)di + inode->i_sb->s_blocksize;
2348         if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
2349                 xs->header = (struct ocfs2_xattr_header *)
2350                         (xs->end - le16_to_cpu(di->i_xattr_inline_size));
2351         else
2352                 xs->header = (struct ocfs2_xattr_header *)
2353                         (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
2354         xs->base = (void *)xs->header;
2355         xs->here = xs->header->xh_entries;
2356
2357         /* Find the named attribute. */
2358         if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2359                 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2360                 if (ret && ret != -ENODATA)
2361                         return ret;
2362                 xs->not_found = ret;
2363         }
2364
2365         return 0;
2366 }
2367
2368 /*
2369  * ocfs2_xattr_ibody_set()
2370  *
2371  * Set, replace or remove an extended attribute into inode block.
2372  *
2373  */
2374 static int ocfs2_xattr_ibody_set(struct inode *inode,
2375                                  struct ocfs2_xattr_info *xi,
2376                                  struct ocfs2_xattr_search *xs,
2377                                  struct ocfs2_xattr_set_ctxt *ctxt)
2378 {
2379         struct ocfs2_inode_info *oi = OCFS2_I(inode);
2380         struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2381         int ret;
2382
2383         if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2384                 return -ENOSPC;
2385
2386         down_write(&oi->ip_alloc_sem);
2387         if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2388                 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2389                         ret = -ENOSPC;
2390                         goto out;
2391                 }
2392         }
2393
2394         ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
2395                                 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
2396 out:
2397         up_write(&oi->ip_alloc_sem);
2398
2399         return ret;
2400 }
2401
2402 /*
2403  * ocfs2_xattr_block_find()
2404  *
2405  * Find extended attribute in external block and
2406  * fill search info into struct ocfs2_xattr_search.
2407  */
2408 static int ocfs2_xattr_block_find(struct inode *inode,
2409                                   int name_index,
2410                                   const char *name,
2411                                   struct ocfs2_xattr_search *xs)
2412 {
2413         struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2414         struct buffer_head *blk_bh = NULL;
2415         struct ocfs2_xattr_block *xb;
2416         int ret = 0;
2417
2418         if (!di->i_xattr_loc)
2419                 return ret;
2420
2421         ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
2422                                      &blk_bh);
2423         if (ret < 0) {
2424                 mlog_errno(ret);
2425                 return ret;
2426         }
2427
2428         xs->xattr_bh = blk_bh;
2429         xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2430
2431         if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2432                 xs->header = &xb->xb_attrs.xb_header;
2433                 xs->base = (void *)xs->header;
2434                 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
2435                 xs->here = xs->header->xh_entries;
2436
2437                 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2438         } else
2439                 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
2440                                                    name_index,
2441                                                    name, xs);
2442
2443         if (ret && ret != -ENODATA) {
2444                 xs->xattr_bh = NULL;
2445                 goto cleanup;
2446         }
2447         xs->not_found = ret;
2448         return 0;
2449 cleanup:
2450         brelse(blk_bh);
2451
2452         return ret;
2453 }
2454
2455 static int ocfs2_create_xattr_block(handle_t *handle,
2456                                     struct inode *inode,
2457                                     struct buffer_head *inode_bh,
2458                                     struct ocfs2_alloc_context *meta_ac,
2459                                     struct buffer_head **ret_bh,
2460                                     int indexed)
2461 {
2462         int ret;
2463         u16 suballoc_bit_start;
2464         u32 num_got;
2465         u64 first_blkno;
2466         struct ocfs2_dinode *di =  (struct ocfs2_dinode *)inode_bh->b_data;
2467         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2468         struct buffer_head *new_bh = NULL;
2469         struct ocfs2_xattr_block *xblk;
2470
2471         ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), inode_bh,
2472                                       OCFS2_JOURNAL_ACCESS_CREATE);
2473         if (ret < 0) {
2474                 mlog_errno(ret);
2475                 goto end;
2476         }
2477
2478         ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
2479                                    &suballoc_bit_start, &num_got,
2480                                    &first_blkno);
2481         if (ret < 0) {
2482                 mlog_errno(ret);
2483                 goto end;
2484         }
2485
2486         new_bh = sb_getblk(inode->i_sb, first_blkno);
2487         ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
2488
2489         ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode),
2490                                       new_bh,
2491                                       OCFS2_JOURNAL_ACCESS_CREATE);
2492         if (ret < 0) {
2493                 mlog_errno(ret);
2494                 goto end;
2495         }
2496
2497         /* Initialize ocfs2_xattr_block */
2498         xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2499         memset(xblk, 0, inode->i_sb->s_blocksize);
2500         strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
2501         xblk->xb_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
2502         xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
2503         xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
2504         xblk->xb_blkno = cpu_to_le64(first_blkno);
2505
2506         if (indexed) {
2507                 struct ocfs2_xattr_tree_root *xr = &xblk->xb_attrs.xb_root;
2508                 xr->xt_clusters = cpu_to_le32(1);
2509                 xr->xt_last_eb_blk = 0;
2510                 xr->xt_list.l_tree_depth = 0;
2511                 xr->xt_list.l_count = cpu_to_le16(
2512                                         ocfs2_xattr_recs_per_xb(inode->i_sb));
2513                 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2514                 xblk->xb_flags = cpu_to_le16(OCFS2_XATTR_INDEXED);
2515         }
2516
2517         ret = ocfs2_journal_dirty(handle, new_bh);
2518         if (ret < 0) {
2519                 mlog_errno(ret);
2520                 goto end;
2521         }
2522         di->i_xattr_loc = cpu_to_le64(first_blkno);
2523         ocfs2_journal_dirty(handle, inode_bh);
2524
2525         *ret_bh = new_bh;
2526         new_bh = NULL;
2527
2528 end:
2529         brelse(new_bh);
2530         return ret;
2531 }
2532
2533 /*
2534  * ocfs2_xattr_block_set()
2535  *
2536  * Set, replace or remove an extended attribute into external block.
2537  *
2538  */
2539 static int ocfs2_xattr_block_set(struct inode *inode,
2540                                  struct ocfs2_xattr_info *xi,
2541                                  struct ocfs2_xattr_search *xs,
2542                                  struct ocfs2_xattr_set_ctxt *ctxt)
2543 {
2544         struct buffer_head *new_bh = NULL;
2545         handle_t *handle = ctxt->handle;
2546         struct ocfs2_xattr_block *xblk = NULL;
2547         int ret;
2548
2549         if (!xs->xattr_bh) {
2550                 ret = ocfs2_create_xattr_block(handle, inode, xs->inode_bh,
2551                                                ctxt->meta_ac, &new_bh, 0);
2552                 if (ret) {
2553                         mlog_errno(ret);
2554                         goto end;
2555                 }
2556
2557                 xs->xattr_bh = new_bh;
2558                 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2559                 xs->header = &xblk->xb_attrs.xb_header;
2560                 xs->base = (void *)xs->header;
2561                 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2562                 xs->here = xs->header->xh_entries;
2563         } else
2564                 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2565
2566         if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
2567                 /* Set extended attribute into external block */
2568                 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
2569                                             OCFS2_HAS_XATTR_FL);
2570                 if (!ret || ret != -ENOSPC)
2571                         goto end;
2572
2573                 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
2574                 if (ret)
2575                         goto end;
2576         }
2577
2578         ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
2579
2580 end:
2581
2582         return ret;
2583 }
2584
2585 /* Check whether the new xattr can be inserted into the inode. */
2586 static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
2587                                        struct ocfs2_xattr_info *xi,
2588                                        struct ocfs2_xattr_search *xs)
2589 {
2590         u64 value_size;
2591         struct ocfs2_xattr_entry *last;
2592         int free, i;
2593         size_t min_offs = xs->end - xs->base;
2594
2595         if (!xs->header)
2596                 return 0;
2597
2598         last = xs->header->xh_entries;
2599
2600         for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
2601                 size_t offs = le16_to_cpu(last->xe_name_offset);
2602                 if (offs < min_offs)
2603                         min_offs = offs;
2604                 last += 1;
2605         }
2606
2607         free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
2608         if (free < 0)
2609                 return 0;
2610
2611         BUG_ON(!xs->not_found);
2612
2613         if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
2614                 value_size = OCFS2_XATTR_ROOT_SIZE;
2615         else
2616                 value_size = OCFS2_XATTR_SIZE(xi->xi_value_len);
2617
2618         if (free >= sizeof(struct ocfs2_xattr_entry) +
2619                    OCFS2_XATTR_SIZE(strlen(xi->xi_name)) + value_size)
2620                 return 1;
2621
2622         return 0;
2623 }
2624
2625 static int ocfs2_calc_xattr_set_need(struct inode *inode,
2626                                      struct ocfs2_dinode *di,
2627                                      struct ocfs2_xattr_info *xi,
2628                                      struct ocfs2_xattr_search *xis,
2629                                      struct ocfs2_xattr_search *xbs,
2630                                      int *clusters_need,
2631                                      int *meta_need,
2632                                      int *credits_need)
2633 {
2634         int ret = 0, old_in_xb = 0;
2635         int clusters_add = 0, meta_add = 0, credits = 0;
2636         struct buffer_head *bh = NULL;
2637         struct ocfs2_xattr_block *xb = NULL;
2638         struct ocfs2_xattr_entry *xe = NULL;
2639         struct ocfs2_xattr_value_root *xv = NULL;
2640         char *base = NULL;
2641         int name_offset, name_len = 0;
2642         u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2643                                                     xi->xi_value_len);
2644         u64 value_size;
2645
2646         /*
2647          * Calculate the clusters we need to write.
2648          * No matter whether we replace an old one or add a new one,
2649          * we need this for writing.
2650          */
2651         if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
2652                 credits += new_clusters *
2653                            ocfs2_clusters_to_blocks(inode->i_sb, 1);
2654
2655         if (xis->not_found && xbs->not_found) {
2656                 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2657
2658                 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2659                         clusters_add += new_clusters;
2660                         credits += ocfs2_calc_extend_credits(inode->i_sb,
2661                                                         &def_xv.xv.xr_list,
2662                                                         new_clusters);
2663                 }
2664
2665                 goto meta_guess;
2666         }
2667
2668         if (!xis->not_found) {
2669                 xe = xis->here;
2670                 name_offset = le16_to_cpu(xe->xe_name_offset);
2671                 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2672                 base = xis->base;
2673                 credits += OCFS2_INODE_UPDATE_CREDITS;
2674         } else {
2675                 int i, block_off = 0;
2676                 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2677                 xe = xbs->here;
2678                 name_offset = le16_to_cpu(xe->xe_name_offset);
2679                 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2680                 i = xbs->here - xbs->header->xh_entries;
2681                 old_in_xb = 1;
2682
2683                 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2684                         ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
2685                                                         bucket_xh(xbs->bucket),
2686                                                         i, &block_off,
2687                                                         &name_offset);
2688                         base = bucket_block(xbs->bucket, block_off);
2689                         credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2690                 } else {
2691                         base = xbs->base;
2692                         credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
2693                 }
2694         }
2695
2696         /*
2697          * delete a xattr doesn't need metadata and cluster allocation.
2698          * so just calculate the credits and return.
2699          *
2700          * The credits for removing the value tree will be extended
2701          * by ocfs2_remove_extent itself.
2702          */
2703         if (!xi->xi_value) {
2704                 if (!ocfs2_xattr_is_local(xe))
2705                         credits += ocfs2_remove_extent_credits(inode->i_sb);
2706
2707                 goto out;
2708         }
2709
2710         /* do cluster allocation guess first. */
2711         value_size = le64_to_cpu(xe->xe_value_size);
2712
2713         if (old_in_xb) {
2714                 /*
2715                  * In xattr set, we always try to set the xe in inode first,
2716                  * so if it can be inserted into inode successfully, the old
2717                  * one will be removed from the xattr block, and this xattr
2718                  * will be inserted into inode as a new xattr in inode.
2719                  */
2720                 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
2721                         clusters_add += new_clusters;
2722                         credits += ocfs2_remove_extent_credits(inode->i_sb) +
2723                                     OCFS2_INODE_UPDATE_CREDITS;
2724                         if (!ocfs2_xattr_is_local(xe))
2725                                 credits += ocfs2_calc_extend_credits(
2726                                                         inode->i_sb,
2727                                                         &def_xv.xv.xr_list,
2728                                                         new_clusters);
2729                         goto out;
2730                 }
2731         }
2732
2733         if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2734                 /* the new values will be stored outside. */
2735                 u32 old_clusters = 0;
2736
2737                 if (!ocfs2_xattr_is_local(xe)) {
2738                         old_clusters =  ocfs2_clusters_for_bytes(inode->i_sb,
2739                                                                  value_size);
2740                         xv = (struct ocfs2_xattr_value_root *)
2741                              (base + name_offset + name_len);
2742                         value_size = OCFS2_XATTR_ROOT_SIZE;
2743                 } else
2744                         xv = &def_xv.xv;
2745
2746                 if (old_clusters >= new_clusters) {
2747                         credits += ocfs2_remove_extent_credits(inode->i_sb);
2748                         goto out;
2749                 } else {
2750                         meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
2751                         clusters_add += new_clusters - old_clusters;
2752                         credits += ocfs2_calc_extend_credits(inode->i_sb,
2753                                                              &xv->xr_list,
2754                                                              new_clusters -
2755                                                              old_clusters);
2756                         if (value_size >= OCFS2_XATTR_ROOT_SIZE)
2757                                 goto out;
2758                 }
2759         } else {
2760                 /*
2761                  * Now the new value will be stored inside. So if the new
2762                  * value is smaller than the size of value root or the old
2763                  * value, we don't need any allocation, otherwise we have
2764                  * to guess metadata allocation.
2765                  */
2766                 if ((ocfs2_xattr_is_local(xe) &&
2767                      (value_size >= xi->xi_value_len)) ||
2768                     (!ocfs2_xattr_is_local(xe) &&
2769                      OCFS2_XATTR_ROOT_SIZE >= xi->xi_value_len))
2770                         goto out;
2771         }
2772
2773 meta_guess:
2774         /* calculate metadata allocation. */
2775         if (di->i_xattr_loc) {
2776                 if (!xbs->xattr_bh) {
2777                         ret = ocfs2_read_xattr_block(inode,
2778                                                      le64_to_cpu(di->i_xattr_loc),
2779                                                      &bh);
2780                         if (ret) {
2781                                 mlog_errno(ret);
2782                                 goto out;
2783                         }
2784
2785                         xb = (struct ocfs2_xattr_block *)bh->b_data;
2786                 } else
2787                         xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2788
2789                 /*
2790                  * If there is already an xattr tree, good, we can calculate
2791                  * like other b-trees. Otherwise we may have the chance of
2792                  * create a tree, the credit calculation is borrowed from
2793                  * ocfs2_calc_extend_credits with root_el = NULL. And the
2794                  * new tree will be cluster based, so no meta is needed.
2795                  */
2796                 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2797                         struct ocfs2_extent_list *el =
2798                                  &xb->xb_attrs.xb_root.xt_list;
2799                         meta_add += ocfs2_extend_meta_needed(el);
2800                         credits += ocfs2_calc_extend_credits(inode->i_sb,
2801                                                              el, 1);
2802                 } else
2803                         credits += OCFS2_SUBALLOC_ALLOC + 1;
2804
2805                 /*
2806                  * This cluster will be used either for new bucket or for
2807                  * new xattr block.
2808                  * If the cluster size is the same as the bucket size, one
2809                  * more is needed since we may need to extend the bucket
2810                  * also.
2811                  */
2812                 clusters_add += 1;
2813                 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2814                 if (OCFS2_XATTR_BUCKET_SIZE ==
2815                         OCFS2_SB(inode->i_sb)->s_clustersize) {
2816                         credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2817                         clusters_add += 1;
2818                 }
2819         } else {
2820                 meta_add += 1;
2821                 credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
2822         }
2823 out:
2824         if (clusters_need)
2825                 *clusters_need = clusters_add;
2826         if (meta_need)
2827                 *meta_need = meta_add;
2828         if (credits_need)
2829                 *credits_need = credits;
2830         brelse(bh);
2831         return ret;
2832 }
2833
2834 static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
2835                                      struct ocfs2_dinode *di,
2836                                      struct ocfs2_xattr_info *xi,
2837                                      struct ocfs2_xattr_search *xis,
2838                                      struct ocfs2_xattr_search *xbs,
2839                                      struct ocfs2_xattr_set_ctxt *ctxt,
2840                                      int extra_meta,
2841                                      int *credits)
2842 {
2843         int clusters_add, meta_add, ret;
2844         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2845
2846         memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
2847
2848         ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
2849
2850         ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
2851                                         &clusters_add, &meta_add, credits);
2852         if (ret) {
2853                 mlog_errno(ret);
2854                 return ret;
2855         }
2856
2857         meta_add += extra_meta;
2858         mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
2859              "credits = %d\n", xi->xi_name, meta_add, clusters_add, *credits);
2860
2861         if (meta_add) {
2862                 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
2863                                                         &ctxt->meta_ac);
2864                 if (ret) {
2865                         mlog_errno(ret);
2866                         goto out;
2867                 }
2868         }
2869
2870         if (clusters_add) {
2871                 ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
2872                 if (ret)
2873                         mlog_errno(ret);
2874         }
2875 out:
2876         if (ret) {
2877                 if (ctxt->meta_ac) {
2878                         ocfs2_free_alloc_context(ctxt->meta_ac);
2879                         ctxt->meta_ac = NULL;
2880                 }
2881
2882                 /*
2883                  * We cannot have an error and a non null ctxt->data_ac.
2884                  */
2885         }
2886
2887         return ret;
2888 }
2889
2890 static int __ocfs2_xattr_set_handle(struct inode *inode,
2891                                     struct ocfs2_dinode *di,
2892                                     struct ocfs2_xattr_info *xi,
2893                                     struct ocfs2_xattr_search *xis,
2894                                     struct ocfs2_xattr_search *xbs,
2895                                     struct ocfs2_xattr_set_ctxt *ctxt)
2896 {
2897         int ret = 0, credits, old_found;
2898
2899         if (!xi->xi_value) {
2900                 /* Remove existing extended attribute */
2901                 if (!xis->not_found)
2902                         ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2903                 else if (!xbs->not_found)
2904                         ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2905         } else {
2906                 /* We always try to set extended attribute into inode first*/
2907                 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2908                 if (!ret && !xbs->not_found) {
2909                         /*
2910                          * If succeed and that extended attribute existing in
2911                          * external block, then we will remove it.
2912                          */
2913                         xi->xi_value = NULL;
2914                         xi->xi_value_len = 0;
2915
2916                         old_found = xis->not_found;
2917                         xis->not_found = -ENODATA;
2918                         ret = ocfs2_calc_xattr_set_need(inode,
2919                                                         di,
2920                                                         xi,
2921                                                         xis,
2922                                                         xbs,
2923                                                         NULL,
2924                                                         NULL,
2925                                                         &credits);
2926                         xis->not_found = old_found;
2927                         if (ret) {
2928                                 mlog_errno(ret);
2929                                 goto out;
2930                         }
2931
2932                         ret = ocfs2_extend_trans(ctxt->handle, credits +
2933                                         ctxt->handle->h_buffer_credits);
2934                         if (ret) {
2935                                 mlog_errno(ret);
2936                                 goto out;
2937                         }
2938                         ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2939                 } else if (ret == -ENOSPC) {
2940                         if (di->i_xattr_loc && !xbs->xattr_bh) {
2941                                 ret = ocfs2_xattr_block_find(inode,
2942                                                              xi->xi_name_index,
2943                                                              xi->xi_name, xbs);
2944                                 if (ret)
2945                                         goto out;
2946
2947                                 old_found = xis->not_found;
2948                                 xis->not_found = -ENODATA;
2949                                 ret = ocfs2_calc_xattr_set_need(inode,
2950                                                                 di,
2951                                                                 xi,
2952                                                                 xis,
2953                                                                 xbs,
2954                                                                 NULL,
2955                                                                 NULL,
2956                                                                 &credits);
2957                                 xis->not_found = old_found;
2958                                 if (ret) {
2959                                         mlog_errno(ret);
2960                                         goto out;
2961                                 }
2962
2963                                 ret = ocfs2_extend_trans(ctxt->handle, credits +
2964                                         ctxt->handle->h_buffer_credits);
2965                                 if (ret) {
2966                                         mlog_errno(ret);
2967                                         goto out;
2968                                 }
2969                         }
2970                         /*
2971                          * If no space in inode, we will set extended attribute
2972                          * into external block.
2973                          */
2974                         ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2975                         if (ret)
2976                                 goto out;
2977                         if (!xis->not_found) {
2978                                 /*
2979                                  * If succeed and that extended attribute
2980                                  * existing in inode, we will remove it.
2981                                  */
2982                                 xi->xi_value = NULL;
2983                                 xi->xi_value_len = 0;
2984                                 xbs->not_found = -ENODATA;
2985                                 ret = ocfs2_calc_xattr_set_need(inode,
2986                                                                 di,
2987                                                                 xi,
2988                                                                 xis,
2989                                                                 xbs,
2990                                                                 NULL,
2991                                                                 NULL,
2992                                                                 &credits);
2993                                 if (ret) {
2994                                         mlog_errno(ret);
2995                                         goto out;
2996                                 }
2997
2998                                 ret = ocfs2_extend_trans(ctxt->handle, credits +
2999                                                 ctxt->handle->h_buffer_credits);
3000                                 if (ret) {
3001                                         mlog_errno(ret);
3002                                         goto out;
3003                                 }
3004                                 ret = ocfs2_xattr_ibody_set(inode, xi,
3005                                                             xis, ctxt);
3006                         }
3007                 }
3008         }
3009
3010         if (!ret) {
3011                 /* Update inode ctime. */
3012                 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
3013                                               xis->inode_bh,
3014                                               OCFS2_JOURNAL_ACCESS_WRITE);
3015                 if (ret) {
3016                         mlog_errno(ret);
3017                         goto out;
3018                 }
3019
3020                 inode->i_ctime = CURRENT_TIME;
3021                 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
3022                 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
3023                 ocfs2_journal_dirty(ctxt->handle, xis->inode_bh);
3024         }
3025 out:
3026         return ret;
3027 }
3028
3029 /*
3030  * This function only called duing creating inode
3031  * for init security/acl xattrs of the new inode.
3032  * All transanction credits have been reserved in mknod.
3033  */
3034 int ocfs2_xattr_set_handle(handle_t *handle,
3035                            struct inode *inode,
3036                            struct buffer_head *di_bh,
3037                            int name_index,
3038                            const char *name,
3039                            const void *value,
3040                            size_t value_len,
3041                            int flags,
3042                            struct ocfs2_alloc_context *meta_ac,
3043                            struct ocfs2_alloc_context *data_ac)
3044 {
3045         struct ocfs2_dinode *di;
3046         int ret;
3047
3048         struct ocfs2_xattr_info xi = {
3049                 .xi_name_index = name_index,
3050                 .xi_name = name,
3051                 .xi_value = value,
3052                 .xi_value_len = value_len,
3053         };
3054
3055         struct ocfs2_xattr_search xis = {
3056                 .not_found = -ENODATA,
3057         };
3058
3059         struct ocfs2_xattr_search xbs = {
3060                 .not_found = -ENODATA,
3061         };
3062
3063         struct ocfs2_xattr_set_ctxt ctxt = {
3064                 .handle = handle,
3065                 .meta_ac = meta_ac,
3066                 .data_ac = data_ac,
3067         };
3068
3069         if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3070                 return -EOPNOTSUPP;
3071
3072         /*
3073          * In extreme situation, may need xattr bucket when
3074          * block size is too small. And we have already reserved
3075          * the credits for bucket in mknod.
3076          */
3077         if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) {
3078                 xbs.bucket = ocfs2_xattr_bucket_new(inode);
3079                 if (!xbs.bucket) {
3080                         mlog_errno(-ENOMEM);
3081                         return -ENOMEM;
3082                 }
3083         }
3084
3085         xis.inode_bh = xbs.inode_bh = di_bh;
3086         di = (struct ocfs2_dinode *)di_bh->b_data;
3087
3088         down_write(&OCFS2_I(inode)->ip_xattr_sem);
3089
3090         ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3091         if (ret)
3092                 goto cleanup;
3093         if (xis.not_found) {
3094                 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3095                 if (ret)
3096                         goto cleanup;
3097         }
3098
3099         ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3100
3101 cleanup:
3102         up_write(&OCFS2_I(inode)->ip_xattr_sem);
3103         brelse(xbs.xattr_bh);
3104         ocfs2_xattr_bucket_free(xbs.bucket);
3105
3106         return ret;
3107 }
3108
3109 /*
3110  * ocfs2_xattr_set()
3111  *
3112  * Set, replace or remove an extended attribute for this inode.
3113  * value is NULL to remove an existing extended attribute, else either
3114  * create or replace an extended attribute.
3115  */
3116 int ocfs2_xattr_set(struct inode *inode,
3117                     int name_index,
3118                     const char *name,
3119                     const void *value,
3120                     size_t value_len,
3121                     int flags)
3122 {
3123         struct buffer_head *di_bh = NULL;
3124         struct ocfs2_dinode *di;
3125         int ret, credits, ref_meta = 0, ref_credits = 0;
3126         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3127         struct inode *tl_inode = osb->osb_tl_inode;
3128         struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
3129         struct ocfs2_refcount_tree *ref_tree = NULL;
3130
3131         struct ocfs2_xattr_info xi = {
3132                 .xi_name_index = name_index,
3133                 .xi_name = name,
3134                 .xi_value = value,
3135                 .xi_value_len = value_len,
3136         };
3137
3138         struct ocfs2_xattr_search xis = {
3139                 .not_found = -ENODATA,
3140         };
3141
3142         struct ocfs2_xattr_search xbs = {
3143                 .not_found = -ENODATA,
3144         };
3145
3146         if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3147                 return -EOPNOTSUPP;
3148
3149         /*
3150          * Only xbs will be used on indexed trees.  xis doesn't need a
3151          * bucket.
3152          */
3153         xbs.bucket = ocfs2_xattr_bucket_new(inode);
3154         if (!xbs.bucket) {
3155                 mlog_errno(-ENOMEM);
3156                 return -ENOMEM;
3157         }
3158
3159         ret = ocfs2_inode_lock(inode, &di_bh, 1);
3160         if (ret < 0) {
3161                 mlog_errno(ret);
3162                 goto cleanup_nolock;
3163         }
3164         xis.inode_bh = xbs.inode_bh = di_bh;
3165         di = (struct ocfs2_dinode *)di_bh->b_data;
3166
3167         down_write(&OCFS2_I(inode)->ip_xattr_sem);
3168         /*
3169          * Scan inode and external block to find the same name
3170          * extended attribute and collect search infomation.
3171          */
3172         ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3173         if (ret)
3174                 goto cleanup;
3175         if (xis.not_found) {
3176                 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3177                 if (ret)
3178                         goto cleanup;
3179         }
3180
3181         if (xis.not_found && xbs.not_found) {
3182                 ret = -ENODATA;
3183                 if (flags & XATTR_REPLACE)
3184                         goto cleanup;
3185                 ret = 0;
3186                 if (!value)
3187                         goto cleanup;
3188         } else {
3189                 ret = -EEXIST;
3190                 if (flags & XATTR_CREATE)
3191                         goto cleanup;
3192         }
3193
3194         /* Check whether the value is refcounted and do some prepartion. */
3195         if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL &&
3196             (!xis.not_found || !xbs.not_found)) {
3197                 ret = ocfs2_prepare_refcount_xattr(inode, di, &xi,
3198                                                    &xis, &xbs, &ref_tree,
3199                                                    &ref_meta, &ref_credits);
3200                 if (ret) {
3201                         mlog_errno(ret);
3202                         goto cleanup;
3203                 }
3204         }
3205
3206         mutex_lock(&tl_inode->i_mutex);
3207
3208         if (ocfs2_truncate_log_needs_flush(osb)) {
3209                 ret = __ocfs2_flush_truncate_log(osb);
3210                 if (ret < 0) {
3211                         mutex_unlock(&tl_inode->i_mutex);
3212                         mlog_errno(ret);
3213                         goto cleanup;
3214                 }
3215         }
3216         mutex_unlock(&tl_inode->i_mutex);
3217
3218         ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
3219                                         &xbs, &ctxt, ref_meta, &credits);
3220         if (ret) {
3221                 mlog_errno(ret);
3222                 goto cleanup;
3223         }
3224
3225         /* we need to update inode's ctime field, so add credit for it. */
3226         credits += OCFS2_INODE_UPDATE_CREDITS;
3227         ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
3228         if (IS_ERR(ctxt.handle)) {
3229                 ret = PTR_ERR(ctxt.handle);
3230                 mlog_errno(ret);
3231                 goto cleanup;
3232         }
3233
3234         ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3235
3236         ocfs2_commit_trans(osb, ctxt.handle);
3237
3238         if (ctxt.data_ac)
3239                 ocfs2_free_alloc_context(ctxt.data_ac);
3240         if (ctxt.meta_ac)
3241                 ocfs2_free_alloc_context(ctxt.meta_ac);
3242         if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
3243                 ocfs2_schedule_truncate_log_flush(osb, 1);
3244         ocfs2_run_deallocs(osb, &ctxt.dealloc);
3245
3246 cleanup:
3247         if (ref_tree)
3248                 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
3249         up_write(&OCFS2_I(inode)->ip_xattr_sem);
3250         if (!value && !ret) {
3251                 ret = ocfs2_try_remove_refcount_tree(inode, di_bh);
3252                 if (ret)
3253                         mlog_errno(ret);
3254         }
3255         ocfs2_inode_unlock(inode, 1);
3256 cleanup_nolock:
3257         brelse(di_bh);
3258         brelse(xbs.xattr_bh);
3259         ocfs2_xattr_bucket_free(xbs.bucket);
3260
3261         return ret;
3262 }
3263
3264 /*
3265  * Find the xattr extent rec which may contains name_hash.
3266  * e_cpos will be the first name hash of the xattr rec.
3267  * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
3268  */
3269 static int ocfs2_xattr_get_rec(struct inode *inode,
3270                                u32 name_hash,
3271                                u64 *p_blkno,
3272                                u32 *e_cpos,
3273                                u32 *num_clusters,
3274                                struct ocfs2_extent_list *el)
3275 {
3276         int ret = 0, i;
3277         struct buffer_head *eb_bh = NULL;
3278         struct ocfs2_extent_block *eb;
3279         struct ocfs2_extent_rec *rec = NULL;
3280         u64 e_blkno = 0;
3281
3282         if (el->l_tree_depth) {
3283                 ret = ocfs2_find_leaf(INODE_CACHE(inode), el, name_hash,
3284                                       &eb_bh);
3285                 if (ret) {
3286                         mlog_errno(ret);
3287                         goto out;
3288                 }
3289
3290                 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
3291                 el = &eb->h_list;
3292
3293                 if (el->l_tree_depth) {
3294                         ocfs2_error(inode->i_sb,
3295                                     "Inode %lu has non zero tree depth in "
3296                                     "xattr tree block %llu\n", inode->i_ino,
3297                                     (unsigned long long)eb_bh->b_blocknr);
3298                         ret = -EROFS;
3299                         goto out;
3300                 }
3301         }
3302
3303         for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
3304                 rec = &el->l_recs[i];
3305
3306                 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
3307                         e_blkno = le64_to_cpu(rec->e_blkno);
3308                         break;
3309                 }
3310         }
3311
3312         if (!e_blkno) {
3313                 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
3314                             "record (%u, %u, 0) in xattr", inode->i_ino,
3315                             le32_to_cpu(rec->e_cpos),
3316                             ocfs2_rec_clusters(el, rec));
3317                 ret = -EROFS;
3318                 goto out;
3319         }
3320
3321         *p_blkno = le64_to_cpu(rec->e_blkno);
3322         *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
3323         if (e_cpos)
3324                 *e_cpos = le32_to_cpu(rec->e_cpos);
3325 out:
3326         brelse(eb_bh);
3327         return ret;
3328 }
3329
3330 typedef int (xattr_bucket_func)(struct inode *inode,
3331                                 struct ocfs2_xattr_bucket *bucket,
3332                                 void *para);
3333
3334 static int ocfs2_find_xe_in_bucket(struct inode *inode,
3335                                    struct ocfs2_xattr_bucket *bucket,
3336                                    int name_index,
3337                                    const char *name,
3338                                    u32 name_hash,
3339                                    u16 *xe_index,
3340                                    int *found)
3341 {
3342         int i, ret = 0, cmp = 1, block_off, new_offset;
3343         struct ocfs2_xattr_header *xh = bucket_xh(bucket);
3344         size_t name_len = strlen(name);
3345         struct ocfs2_xattr_entry *xe = NULL;
3346         char *xe_name;
3347
3348         /*
3349          * We don't use binary search in the bucket because there
3350          * may be multiple entries with the same name hash.
3351          */
3352         for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3353                 xe = &xh->xh_entries[i];
3354
3355                 if (name_hash > le32_to_cpu(xe->xe_name_hash))
3356                         continue;
3357                 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
3358                         break;
3359
3360                 cmp = name_index - ocfs2_xattr_get_type(xe);
3361                 if (!cmp)
3362                         cmp = name_len - xe->xe_name_len;
3363                 if (cmp)
3364                         continue;
3365
3366                 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
3367                                                         xh,
3368                                                         i,
3369                                                         &block_off,
3370                                                         &new_offset);
3371                 if (ret) {
3372                         mlog_errno(ret);
3373                         break;
3374                 }
3375
3376
3377                 xe_name = bucket_block(bucket, block_off) + new_offset;
3378                 if (!memcmp(name, xe_name, name_len)) {
3379                         *xe_index = i;
3380                         *found = 1;
3381                         ret = 0;
3382                         break;
3383                 }
3384         }
3385
3386         return ret;
3387 }
3388
3389 /*
3390  * Find the specified xattr entry in a series of buckets.
3391  * This series start from p_blkno and last for num_clusters.
3392  * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
3393  * the num of the valid buckets.
3394  *
3395  * Return the buffer_head this xattr should reside in. And if the xattr's
3396  * hash is in the gap of 2 buckets, return the lower bucket.
3397  */
3398 static int ocfs2_xattr_bucket_find(struct inode *inode,
3399                                    int name_index,
3400                                    const char *name,
3401                                    u32 name_hash,
3402                                    u64 p_blkno,
3403                                    u32 first_hash,
3404                                    u32 num_clusters,
3405                                    struct ocfs2_xattr_search *xs)
3406 {
3407         int ret, found = 0;
3408         struct ocfs2_xattr_header *xh = NULL;
3409         struct ocfs2_xattr_entry *xe = NULL;
3410         u16 index = 0;
3411         u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3412         int low_bucket = 0, bucket, high_bucket;
3413         struct ocfs2_xattr_bucket *search;
3414         u32 last_hash;
3415         u64 blkno, lower_blkno = 0;
3416
3417         search = ocfs2_xattr_bucket_new(inode);
3418         if (!search) {
3419                 ret = -ENOMEM;
3420                 mlog_errno(ret);
3421                 goto out;
3422         }
3423
3424         ret = ocfs2_read_xattr_bucket(search, p_blkno);
3425         if (ret) {
3426                 mlog_errno(ret);
3427                 goto out;
3428         }
3429
3430         xh = bucket_xh(search);
3431         high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
3432         while (low_bucket <= high_bucket) {
3433                 ocfs2_xattr_bucket_relse(search);
3434
3435                 bucket = (low_bucket + high_bucket) / 2;
3436                 blkno = p_blkno + bucket * blk_per_bucket;
3437                 ret = ocfs2_read_xattr_bucket(search, blkno);
3438                 if (ret) {
3439                         mlog_errno(ret);
3440                         goto out;
3441                 }
3442
3443                 xh = bucket_xh(search);
3444                 xe = &xh->xh_entries[0];
3445                 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
3446                         high_bucket = bucket - 1;
3447                         continue;
3448                 }
3449
3450                 /*
3451                  * Check whether the hash of the last entry in our
3452                  * bucket is larger than the search one. for an empty
3453                  * bucket, the last one is also the first one.
3454                  */
3455                 if (xh->xh_count)
3456                         xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
3457
3458                 last_hash = le32_to_cpu(xe->xe_name_hash);
3459
3460                 /* record lower_blkno which may be the insert place. */
3461                 lower_blkno = blkno;
3462
3463                 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
3464                         low_bucket = bucket + 1;
3465                         continue;
3466                 }
3467
3468                 /* the searched xattr should reside in this bucket if exists. */
3469                 ret = ocfs2_find_xe_in_bucket(inode, search,
3470                                               name_index, name, name_hash,
3471                                               &index, &found);
3472                 if (ret) {
3473                         mlog_errno(ret);
3474                         goto out;
3475                 }
3476                 break;
3477         }
3478
3479         /*
3480          * Record the bucket we have found.
3481          * When the xattr's hash value is in the gap of 2 buckets, we will
3482          * always set it to the previous bucket.
3483          */
3484         if (!lower_blkno)
3485                 lower_blkno = p_blkno;
3486
3487         /* This should be in cache - we just read it during the search */
3488         ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
3489         if (ret) {
3490                 mlog_errno(ret);
3491                 goto out;
3492         }
3493
3494         xs->header = bucket_xh(xs->bucket);
3495         xs->base = bucket_block(xs->bucket, 0);
3496         xs->end = xs->base + inode->i_sb->s_blocksize;
3497
3498         if (found) {
3499                 xs->here = &xs->header->xh_entries[index];
3500                 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
3501                      (unsigned long long)bucket_blkno(xs->bucket), index);
3502         } else
3503                 ret = -ENODATA;
3504
3505 out:
3506         ocfs2_xattr_bucket_free(search);
3507         return ret;
3508 }
3509
3510 static int ocfs2_xattr_index_block_find(struct inode *inode,
3511                                         struct buffer_head *root_bh,
3512                                         int name_index,
3513                                         const char *name,
3514                                         struct ocfs2_xattr_search *xs)
3515 {
3516         int ret;
3517         struct ocfs2_xattr_block *xb =
3518                         (struct ocfs2_xattr_block *)root_bh->b_data;
3519         struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3520         struct ocfs2_extent_list *el = &xb_root->xt_list;
3521         u64 p_blkno = 0;
3522         u32 first_hash, num_clusters = 0;
3523         u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
3524
3525         if (le16_to_cpu(el->l_next_free_rec) == 0)
3526                 return -ENODATA;
3527
3528         mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
3529              name, name_hash, name_index);
3530
3531         ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
3532                                   &num_clusters, el);
3533         if (ret) {
3534                 mlog_errno(ret);
3535                 goto out;
3536         }
3537
3538         BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
3539
3540         mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
3541              "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
3542              first_hash);
3543
3544         ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
3545                                       p_blkno, first_hash, num_clusters, xs);
3546
3547 out:
3548         return ret;
3549 }
3550
3551 static int ocfs2_iterate_xattr_buckets(struct inode *inode,
3552                                        u64 blkno,
3553                                        u32 clusters,
3554                                        xattr_bucket_func *func,
3555                                        void *para)
3556 {
3557         int i, ret = 0;
3558         u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
3559         u32 num_buckets = clusters * bpc;
3560         struct ocfs2_xattr_bucket *bucket;
3561
3562         bucket = ocfs2_xattr_bucket_new(inode);
3563         if (!bucket) {
3564                 mlog_errno(-ENOMEM);
3565                 return -ENOMEM;
3566         }
3567
3568         mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
3569              clusters, (unsigned long long)blkno);
3570
3571         for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
3572                 ret = ocfs2_read_xattr_bucket(bucket, blkno);
3573                 if (ret) {
3574                         mlog_errno(ret);
3575                         break;
3576                 }
3577
3578                 /*
3579                  * The real bucket num in this series of blocks is stored
3580                  * in the 1st bucket.
3581                  */
3582                 if (i == 0)
3583                         num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
3584
3585                 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
3586                      (unsigned long long)blkno,
3587                      le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
3588                 if (func) {
3589                         ret = func(inode, bucket, para);
3590                         if (ret && ret != -ERANGE)
3591                                 mlog_errno(ret);
3592                         /* Fall through to bucket_relse() */
3593                 }
3594
3595                 ocfs2_xattr_bucket_relse(bucket);
3596                 if (ret)
3597                         break;
3598         }
3599
3600         ocfs2_xattr_bucket_free(bucket);
3601         return ret;
3602 }
3603
3604 struct ocfs2_xattr_tree_list {
3605         char *buffer;
3606         size_t buffer_size;
3607         size_t result;
3608 };
3609
3610 static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
3611                                              struct ocfs2_xattr_header *xh,
3612                                              int index,
3613                                              int *block_off,
3614                                              int *new_offset)
3615 {
3616         u16 name_offset;
3617
3618         if (index < 0 || index >= le16_to_cpu(xh->xh_count))
3619                 return -EINVAL;
3620
3621         name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
3622
3623         *block_off = name_offset >> sb->s_blocksize_bits;
3624         *new_offset = name_offset % sb->s_blocksize;
3625
3626         return 0;
3627 }
3628
3629 static int ocfs2_list_xattr_bucket(struct inode *inode,
3630                                    struct ocfs2_xattr_bucket *bucket,
3631                                    void *para)
3632 {
3633         int ret = 0, type;
3634         struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
3635         int i, block_off, new_offset;
3636         const char *prefix, *name;
3637
3638         for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
3639                 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
3640                 type = ocfs2_xattr_get_type(entry);
3641                 prefix = ocfs2_xattr_prefix(type);
3642
3643                 if (prefix) {
3644                         ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
3645                                                                 bucket_xh(bucket),
3646                                                                 i,
3647                                                                 &block_off,
3648                                                                 &new_offset);
3649                         if (ret)
3650                                 break;
3651
3652                         name = (const char *)bucket_block(bucket, block_off) +
3653                                 new_offset;
3654                         ret = ocfs2_xattr_list_entry(xl->buffer,
3655                                                      xl->buffer_size,
3656                                                      &xl->result,
3657                                                      prefix, name,
3658                                                      entry->xe_name_len);
3659                         if (ret)
3660                                 break;
3661                 }
3662         }
3663
3664         return ret;
3665 }
3666
3667 static int ocfs2_iterate_xattr_index_block(struct inode *inode,
3668                                            struct buffer_head *blk_bh,
3669                                            xattr_tree_rec_func *rec_func,
3670                                            void *para)
3671 {
3672         struct ocfs2_xattr_block *xb =
3673                         (struct ocfs2_xattr_block *)blk_bh->b_data;
3674         struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
3675         int ret = 0;
3676         u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
3677         u64 p_blkno = 0;
3678
3679         if (!el->l_next_free_rec || !rec_func)
3680                 return 0;
3681
3682         while (name_hash > 0) {
3683                 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
3684                                           &e_cpos, &num_clusters, el);
3685                 if (ret) {
3686                         mlog_errno(ret);
3687                         break;
3688                 }
3689
3690                 ret = rec_func(inode, blk_bh, p_blkno, e_cpos,
3691                                num_clusters, para);
3692                 if (ret) {
3693                         if (ret != -ERANGE)
3694                                 mlog_errno(ret);
3695                         break;
3696                 }
3697
3698                 if (e_cpos == 0)
3699                         break;
3700
3701                 name_hash = e_cpos - 1;
3702         }
3703
3704         return ret;
3705
3706 }
3707
3708 static int ocfs2_list_xattr_tree_rec(struct inode *inode,
3709                                      struct buffer_head *root_bh,
3710                                      u64 blkno, u32 cpos, u32 len, void *para)
3711 {
3712         return ocfs2_iterate_xattr_buckets(inode, blkno, len,
3713                                            ocfs2_list_xattr_bucket, para);
3714 }
3715
3716 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
3717                                              struct buffer_head *blk_bh,
3718                                              char *buffer,
3719                                              size_t buffer_size)
3720 {
3721         int ret;
3722         struct ocfs2_xattr_tree_list xl = {
3723                 .buffer = buffer,
3724                 .buffer_size = buffer_size,
3725                 .result = 0,
3726         };
3727
3728         ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
3729                                               ocfs2_list_xattr_tree_rec, &xl);
3730         if (ret) {
3731                 mlog_errno(ret);
3732                 goto out;
3733         }
3734
3735         ret = xl.result;
3736 out:
3737         return ret;
3738 }
3739
3740 static int cmp_xe(const void *a, const void *b)
3741 {
3742         const struct ocfs2_xattr_entry *l = a, *r = b;
3743         u32 l_hash = le32_to_cpu(l->xe_name_hash);
3744         u32 r_hash = le32_to_cpu(r->xe_name_hash);
3745
3746         if (l_hash > r_hash)
3747                 return 1;
3748         if (l_hash < r_hash)
3749                 return -1;
3750         return 0;
3751 }
3752
3753 static void swap_xe(void *a, void *b, int size)
3754 {
3755         struct ocfs2_xattr_entry *l = a, *r = b, tmp;
3756
3757         tmp = *l;
3758         memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
3759         memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
3760 }
3761
3762 /*
3763  * When the ocfs2_xattr_block is filled up, new bucket will be created
3764  * and all the xattr entries will be moved to the new bucket.
3765  * The header goes at the start of the bucket, and the names+values are
3766  * filled from the end.  This is why *target starts as the last buffer.
3767  * Note: we need to sort the entries since they are not saved in order
3768  * in the ocfs2_xattr_block.
3769  */
3770 static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
3771                                            struct buffer_head *xb_bh,
3772                                            struct ocfs2_xattr_bucket *bucket)
3773 {
3774         int i, blocksize = inode->i_sb->s_blocksize;
3775         int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3776         u16 offset, size, off_change;
3777         struct ocfs2_xattr_entry *xe;
3778         struct ocfs2_xattr_block *xb =
3779                                 (struct ocfs2_xattr_block *)xb_bh->b_data;
3780         struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
3781         struct ocfs2_xattr_header *xh = bucket_xh(bucket);
3782         u16 count = le16_to_cpu(xb_xh->xh_count);
3783         char *src = xb_bh->b_data;
3784         char *target = bucket_block(bucket, blks - 1);
3785
3786         mlog(0, "cp xattr from block %llu to bucket %llu\n",
3787              (unsigned long long)xb_bh->b_blocknr,
3788              (unsigned long long)bucket_blkno(bucket));
3789
3790         for (i = 0; i < blks; i++)
3791                 memset(bucket_block(bucket, i), 0, blocksize);
3792
3793         /*
3794          * Since the xe_name_offset is based on ocfs2_xattr_header,
3795          * there is a offset change corresponding to the change of
3796          * ocfs2_xattr_header's position.
3797          */
3798         off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
3799         xe = &xb_xh->xh_entries[count - 1];
3800         offset = le16_to_cpu(xe->xe_name_offset) + off_change;
3801         size = blocksize - offset;
3802
3803         /* copy all the names and values. */
3804         memcpy(target + offset, src + offset, size);
3805
3806         /* Init new header now. */
3807         xh->xh_count = xb_xh->xh_count;
3808         xh->xh_num_buckets = cpu_to_le16(1);
3809         xh->xh_name_value_len = cpu_to_le16(size);
3810         xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
3811
3812         /* copy all the entries. */
3813         target = bucket_block(bucket, 0);
3814         offset = offsetof(struct ocfs2_xattr_header, xh_entries);
3815         size = count * sizeof(struct ocfs2_xattr_entry);
3816         memcpy(target + offset, (char *)xb_xh + offset, size);
3817
3818         /* Change the xe offset for all the xe because of the move. */
3819         off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
3820                  offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
3821         for (i = 0; i < count; i++)
3822                 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
3823
3824         mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
3825              offset, size, off_change);
3826
3827         sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
3828              cmp_xe, swap_xe);
3829 }
3830
3831 /*
3832  * After we move xattr from block to index btree, we have to
3833  * update ocfs2_xattr_search to the new xe and base.
3834  *
3835  * When the entry is in xattr block, xattr_bh indicates the storage place.
3836  * While if the entry is in index b-tree, "bucket" indicates the
3837  * real place of the xattr.
3838  */
3839 static void ocfs2_xattr_update_xattr_search(struct inode *inode,
3840                                             struct ocfs2_xattr_search *xs,
3841                                             struct buffer_head *old_bh)
3842 {
3843         char *buf = old_bh->b_data;
3844         struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
3845         struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
3846         int i;
3847
3848         xs->header = bucket_xh(xs->bucket);
3849         xs->base = bucket_block(xs->bucket, 0);
3850         xs->end = xs->base + inode->i_sb->s_blocksize;
3851
3852         if (xs->not_found)
3853                 return;
3854
3855         i = xs->here - old_xh->xh_entries;
3856         xs->here = &xs->header->xh_entries[i];
3857 }
3858
3859 static int ocfs2_xattr_create_index_block(struct inode *inode,
3860                                           struct ocfs2_xattr_search *xs,
3861                                           struct ocfs2_xattr_set_ctxt *ctxt)
3862 {
3863         int ret;
3864         u32 bit_off, len;
3865         u64 blkno;
3866         handle_t *handle = ctxt->handle;
3867         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3868         struct ocfs2_inode_info *oi = OCFS2_I(inode);
3869         struct buffer_head *xb_bh = xs->xattr_bh;
3870         struct ocfs2_xattr_block *xb =
3871                         (struct ocfs2_xattr_block *)xb_bh->b_data;
3872         struct ocfs2_xattr_tree_root *xr;
3873         u16 xb_flags = le16_to_cpu(xb->xb_flags);
3874
3875         mlog(0, "create xattr index block for %llu\n",
3876              (unsigned long long)xb_bh->b_blocknr);
3877
3878         BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
3879         BUG_ON(!xs->bucket);
3880
3881         /*
3882          * XXX:
3883          * We can use this lock for now, and maybe move to a dedicated mutex
3884          * if performance becomes a problem later.
3885          */
3886         down_write(&oi->ip_alloc_sem);
3887
3888         ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), xb_bh,
3889                                       OCFS2_JOURNAL_ACCESS_WRITE);
3890         if (ret) {
3891                 mlog_errno(ret);
3892                 goto out;
3893         }
3894
3895         ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac,
3896                                      1, 1, &bit_off, &len);
3897         if (ret) {
3898                 mlog_errno(ret);
3899                 goto out;
3900         }
3901
3902         /*
3903          * The bucket may spread in many blocks, and
3904          * we will only touch the 1st block and the last block
3905          * in the whole bucket(one for entry and one for data).
3906          */
3907         blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
3908
3909         mlog(0, "allocate 1 cluster from %llu to xattr block\n",
3910              (unsigned long long)blkno);
3911
3912         ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
3913         if (ret) {
3914                 mlog_errno(ret);
3915                 goto out;
3916         }
3917
3918         ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
3919                                                 OCFS2_JOURNAL_ACCESS_CREATE);
3920         if (ret) {
3921                 mlog_errno(ret);
3922                 goto out;
3923         }
3924
3925         ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
3926         ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
3927
3928         ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
3929
3930         /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
3931         memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
3932                offsetof(struct ocfs2_xattr_block, xb_attrs));
3933
3934         xr = &xb->xb_attrs.xb_root;
3935         xr->xt_clusters = cpu_to_le32(1);
3936         xr->xt_last_eb_blk = 0;
3937         xr->xt_list.l_tree_depth = 0;
3938         xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
3939         xr->xt_list.l_next_free_rec = cpu_to_le16(1);
3940
3941         xr->xt_list.l_recs[0].e_cpos = 0;
3942         xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
3943         xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
3944
3945         xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
3946
3947         ocfs2_journal_dirty(handle, xb_bh);
3948
3949 out:
3950         up_write(&oi->ip_alloc_sem);
3951
3952         return ret;
3953 }
3954
3955 static int cmp_xe_offset(const void *a, const void *b)
3956 {
3957         const struct ocfs2_xattr_entry *l = a, *r = b;
3958         u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
3959         u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
3960
3961         if (l_name_offset < r_name_offset)
3962                 return 1;
3963         if (l_name_offset > r_name_offset)
3964                 return -1;
3965         return 0;
3966 }
3967
3968 /*
3969  * defrag a xattr bucket if we find that the bucket has some
3970  * holes beteen name/value pairs.
3971  * We will move all the name/value pairs to the end of the bucket
3972  * so that we can spare some space for insertion.
3973  */
3974 static int ocfs2_defrag_xattr_bucket(struct inode *inode,
3975                                      handle_t *handle,
3976                                      struct ocfs2_xattr_bucket *bucket)
3977 {
3978         int ret, i;
3979         size_t end, offset, len, value_len;
3980         struct ocfs2_xattr_header *xh;
3981         char *entries, *buf, *bucket_buf = NULL;
3982         u64 blkno = bucket_blkno(bucket);
3983         u16 xh_free_start;
3984         size_t blocksize = inode->i_sb->s_blocksize;
3985         struct ocfs2_xattr_entry *xe;
3986
3987         /*
3988          * In order to make the operation more efficient and generic,
3989          * we copy all the blocks into a contiguous memory and do the
3990          * defragment there, so if anything is error, we will not touch
3991          * the real block.
3992          */
3993         bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
3994         if (!bucket_buf) {
3995                 ret = -EIO;
3996                 goto out;
3997         }
3998
3999         buf = bucket_buf;
4000         for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4001                 memcpy(buf, bucket_block(bucket, i), blocksize);
4002
4003         ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
4004                                                 OCFS2_JOURNAL_ACCESS_WRITE);
4005         if (ret < 0) {
4006                 mlog_errno(ret);
4007                 goto out;
4008         }
4009
4010         xh = (struct ocfs2_xattr_header *)bucket_buf;
4011         entries = (char *)xh->xh_entries;
4012         xh_free_start = le16_to_cpu(xh->xh_free_start);
4013
4014         mlog(0, "adjust xattr bucket in %llu, count = %u, "
4015              "xh_free_start = %u, xh_name_value_len = %u.\n",
4016              (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
4017              xh_free_start, le16_to_cpu(xh->xh_name_value_len));
4018
4019         /*
4020          * sort all the entries by their offset.
4021          * the largest will be the first, so that we can
4022          * move them to the end one by one.
4023          */
4024         sort(entries, le16_to_cpu(xh->xh_count),
4025              sizeof(struct ocfs2_xattr_entry),
4026              cmp_xe_offset, swap_xe);
4027
4028         /* Move all name/values to the end of the bucket. */
4029         xe = xh->xh_entries;
4030         end = OCFS2_XATTR_BUCKET_SIZE;
4031         for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
4032                 offset = le16_to_cpu(xe->xe_name_offset);
4033                 if (ocfs2_xattr_is_local(xe))
4034                         value_len = OCFS2_XATTR_SIZE(
4035                                         le64_to_cpu(xe->xe_value_size));
4036                 else
4037                         value_len = OCFS2_XATTR_ROOT_SIZE;
4038                 len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
4039
4040                 /*
4041                  * We must make sure that the name/value pair
4042                  * exist in the same block. So adjust end to
4043                  * the previous block end if needed.
4044                  */
4045                 if (((end - len) / blocksize !=
4046                         (end - 1) / blocksize))
4047                         end = end - end % blocksize;
4048
4049                 if (end > offset + len) {
4050                         memmove(bucket_buf + end - len,
4051                                 bucket_buf + offset, len);
4052                         xe->xe_name_offset = cpu_to_le16(end - len);
4053                 }
4054
4055                 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
4056                                 "bucket %llu\n", (unsigned long long)blkno);
4057
4058                 end -= len;
4059         }
4060
4061         mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
4062                         "bucket %llu\n", (unsigned long long)blkno);
4063
4064         if (xh_free_start == end)
4065                 goto out;
4066
4067         memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
4068         xh->xh_free_start = cpu_to_le16(end);
4069
4070         /* sort the entries by their name_hash. */
4071         sort(entries, le16_to_cpu(xh->xh_count),
4072              sizeof(struct ocfs2_xattr_entry),
4073              cmp_xe, swap_xe);
4074
4075         buf = bucket_buf;
4076         for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4077                 memcpy(bucket_block(bucket, i), buf, blocksize);
4078         ocfs2_xattr_bucket_journal_dirty(handle, bucket);
4079
4080 out:
4081         kfree(bucket_buf);
4082         return ret;
4083 }
4084
4085 /*
4086  * prev_blkno points to the start of an existing extent.  new_blkno
4087  * points to a newly allocated extent.  Because we know each of our
4088  * clusters contains more than bucket, we can easily split one cluster
4089  * at a bucket boundary.  So we take the last cluster of the existing
4090  * extent and split it down the middle.  We move the last half of the
4091  * buckets in the last cluster of the existing extent over to the new
4092  * extent.
4093  *
4094  * first_bh is the buffer at prev_blkno so we can update the existing
4095  * extent's bucket count.  header_bh is the bucket were we were hoping
4096  * to insert our xattr.  If the bucket move places the target in the new
4097  * extent, we'll update first_bh and header_bh after modifying the old
4098  * extent.
4099  *
4100  * first_hash will be set as the 1st xe's name_hash in the new extent.
4101  */
4102 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
4103                                                handle_t *handle,
4104                                                struct ocfs2_xattr_bucket *first,
4105                                                struct ocfs2_xattr_bucket *target,
4106                                                u64 new_blkno,
4107                                                u32 num_clusters,
4108                                                u32 *first_hash)
4109 {
4110         int ret;
4111         struct super_block *sb = inode->i_sb;
4112         int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb);
4113         int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
4114         int to_move = num_buckets / 2;
4115         u64 src_blkno;
4116         u64 last_cluster_blkno = bucket_blkno(first) +
4117                 ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1));
4118
4119         BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets);
4120         BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize);
4121
4122         mlog(0, "move half of xattrs in cluster %llu to %llu\n",
4123              (unsigned long long)last_cluster_blkno, (unsigned long long)new_blkno);
4124
4125         ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first),
4126                                      last_cluster_blkno, new_blkno,
4127                                      to_move, first_hash);
4128         if (ret) {
4129                 mlog_errno(ret);
4130                 goto out;
4131         }
4132
4133         /* This is the first bucket that got moved */
4134         src_blkno = last_cluster_blkno + (to_move * blks_per_bucket);
4135
4136         /*
4137          * If the target bucket was part of the moved buckets, we need to
4138          * update first and target.
4139          */
4140         if (bucket_blkno(target) >= src_blkno) {
4141                 /* Find the block for the new target bucket */
4142                 src_blkno = new_blkno +
4143                         (bucket_blkno(target) - src_blkno);
4144
4145                 ocfs2_xattr_bucket_relse(first);
4146                 ocfs2_xattr_bucket_relse(target);
4147
4148                 /*
4149                  * These shouldn't fail - the buffers are in the
4150                  * journal from ocfs2_cp_xattr_bucket().
4151                  */
4152                 ret = ocfs2_read_xattr_bucket(first, new_blkno);
4153                 if (ret) {
4154                         mlog_errno(ret);
4155                         goto out;
4156                 }
4157                 ret = ocfs2_read_xattr_bucket(target, src_blkno);
4158                 if (ret)
4159                         mlog_errno(ret);
4160
4161         }
4162
4163 out:
4164         return ret;
4165 }
4166
4167 /*
4168  * Find the suitable pos when we divide a bucket into 2.
4169  * We have to make sure the xattrs with the same hash value exist
4170  * in the same bucket.
4171  *
4172  * If this ocfs2_xattr_header covers more than one hash value, find a
4173  * place where the hash value changes.  Try to find the most even split.
4174  * The most common case is that all entries have different hash values,
4175  * and the first check we make will find a place to split.
4176  */
4177 static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
4178 {
4179         struct ocfs2_xattr_entry *entries = xh->xh_entries;
4180         int count = le16_to_cpu(xh->xh_count);
4181         int delta, middle = count / 2;
4182
4183         /*
4184          * We start at the middle.  Each step gets farther away in both
4185          * directions.  We therefore hit the change in hash value
4186          * nearest to the middle.  Note that this loop does not execute for
4187          * count < 2.
4188          */
4189         for (delta = 0; delta < middle; delta++) {
4190                 /* Let's check delta earlier than middle */
4191                 if (cmp_xe(&entries[middle - delta - 1],
4192                            &entries[middle - delta]))
4193                         return middle - delta;
4194
4195                 /* For even counts, don't walk off the end */
4196                 if ((middle + delta + 1) == count)
4197                         continue;
4198
4199                 /* Now try delta past middle */
4200                 if (cmp_xe(&entries[middle + delta],
4201                            &entries[middle + delta + 1]))
4202                         return middle + delta + 1;
4203         }
4204
4205         /* Every entry had the same hash */
4206         return count;
4207 }
4208
4209 /*
4210  * Move some xattrs in old bucket(blk) to new bucket(new_blk).
4211  * first_hash will record the 1st hash of the new bucket.
4212  *
4213  * Normally half of the xattrs will be moved.  But we have to make
4214  * sure that the xattrs with the same hash value are stored in the
4215  * same bucket. If all the xattrs in this bucket have the same hash
4216  * value, the new bucket will be initialized as an empty one and the
4217  * first_hash will be initialized as (hash_value+1).
4218  */
4219 static int ocfs2_divide_xattr_bucket(struct inode *inode,
4220                                     handle_t *handle,
4221                                     u64 blk,
4222                                     u64 new_blk,
4223                                     u32 *first_hash,
4224                                     int new_bucket_head)
4225 {
4226         int ret, i;
4227         int count, start, len, name_value_len = 0, xe_len, name_offset = 0;
4228         struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
4229         struct ocfs2_xattr_header *xh;
4230         struct ocfs2_xattr_entry *xe;
4231         int blocksize = inode->i_sb->s_blocksize;
4232
4233         mlog(0, "move some of xattrs from bucket %llu to %llu\n",
4234              (unsigned long long)blk, (unsigned long long)new_blk);
4235
4236         s_bucket = ocfs2_xattr_bucket_new(inode);
4237         t_bucket = ocfs2_xattr_bucket_new(inode);
4238         if (!s_bucket || !t_bucket) {
4239                 ret = -ENOMEM;
4240                 mlog_errno(ret);
4241                 goto out;
4242         }
4243
4244         ret = ocfs2_read_xattr_bucket(s_bucket, blk);
4245         if (ret) {
4246                 mlog_errno(ret);
4247                 goto out;
4248         }
4249
4250         ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
4251                                                 OCFS2_JOURNAL_ACCESS_WRITE);
4252         if (ret) {
4253                 mlog_errno(ret);
4254                 goto out;
4255         }
4256
4257         /*
4258          * Even if !new_bucket_head, we're overwriting t_bucket.  Thus,
4259          * there's no need to read it.
4260          */
4261         ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
4262         if (ret) {
4263                 mlog_errno(ret);
4264                 goto out;
4265         }
4266
4267         /*
4268          * Hey, if we're overwriting t_bucket, what difference does
4269          * ACCESS_CREATE vs ACCESS_WRITE make?  See the comment in the
4270          * same part of ocfs2_cp_xattr_bucket().
4271          */
4272         ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
4273                                                 new_bucket_head ?
4274                                                 OCFS2_JOURNAL_ACCESS_CREATE :
4275                                                 OCFS2_JOURNAL_ACCESS_WRITE);
4276         if (ret) {
4277                 mlog_errno(ret);
4278                 goto out;
4279         }
4280
4281         xh = bucket_xh(s_bucket);
4282         count = le16_to_cpu(xh->xh_count);
4283         start = ocfs2_xattr_find_divide_pos(xh);
4284
4285         if (start == count) {
4286                 xe = &xh->xh_entries[start-1];
4287
4288                 /*
4289                  * initialized a new empty bucket here.
4290                  * The hash value is set as one larger than
4291                  * that of the last entry in the previous bucket.
4292                  */
4293                 for (i = 0; i < t_bucket->bu_blocks; i++)
4294                         memset(bucket_block(t_bucket, i), 0, blocksize);
4295
4296                 xh = bucket_xh(t_bucket);
4297                 xh->xh_free_start = cpu_to_le16(blocksize);
4298                 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
4299                 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
4300
4301                 goto set_num_buckets;
4302         }
4303
4304         /* copy the whole bucket to the new first. */
4305         ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4306
4307         /* update the new bucket. */
4308         xh = bucket_xh(t_bucket);
4309
4310         /*
4311          * Calculate the total name/value len and xh_free_start for
4312          * the old bucket first.
4313          */
4314         name_offset = OCFS2_XATTR_BUCKET_SIZE;
4315         name_value_len = 0;
4316         for (i = 0; i < start; i++) {
4317                 xe = &xh->xh_entries[i];
4318                 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
4319                 if (ocfs2_xattr_is_local(xe))
4320                         xe_len +=
4321                            OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4322                 else
4323                         xe_len += OCFS2_XATTR_ROOT_SIZE;
4324                 name_value_len += xe_len;
4325                 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
4326                         name_offset = le16_to_cpu(xe->xe_name_offset);
4327         }
4328
4329         /*
4330          * Now begin the modification to the new bucket.
4331          *
4332          * In the new bucket, We just move the xattr entry to the beginning
4333          * and don't touch the name/value. So there will be some holes in the
4334          * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
4335          * called.
4336          */
4337         xe = &xh->xh_entries[start];
4338         len = sizeof(struct ocfs2_xattr_entry) * (count - start);
4339         mlog(0, "mv xattr entry len %d from %d to %d\n", len,
4340              (int)((char *)xe - (char *)xh),
4341              (int)((char *)xh->xh_entries - (char *)xh));
4342         memmove((char *)xh->xh_entries, (char *)xe, len);
4343         xe = &xh->xh_entries[count - start];
4344         len = sizeof(struct ocfs2_xattr_entry) * start;
4345         memset((char *)xe, 0, len);
4346
4347         le16_add_cpu(&xh->xh_count, -start);
4348         le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
4349
4350         /* Calculate xh_free_start for the new bucket. */
4351         xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4352         for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4353                 xe = &xh->xh_entries[i];
4354                 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
4355                 if (ocfs2_xattr_is_local(xe))
4356                         xe_len +=
4357                            OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4358                 else
4359                         xe_len += OCFS2_XATTR_ROOT_SIZE;
4360                 if (le16_to_cpu(xe->xe_name_offset) <
4361                     le16_to_cpu(xh->xh_free_start))
4362                         xh->xh_free_start = xe->xe_name_offset;
4363         }
4364
4365 set_num_buckets:
4366         /* set xh->xh_num_buckets for the new xh. */
4367         if (new_bucket_head)
4368                 xh->xh_num_buckets = cpu_to_le16(1);
4369         else
4370                 xh->xh_num_buckets = 0;
4371
4372         ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
4373
4374         /* store the first_hash of the new bucket. */
4375         if (first_hash)
4376                 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
4377
4378         /*
4379          * Now only update the 1st block of the old bucket.  If we
4380          * just added a new empty bucket, there is no need to modify
4381          * it.
4382          */
4383         if (start == count)
4384                 goto out;
4385
4386         xh = bucket_xh(s_bucket);
4387         memset(&xh->xh_entries[start], 0,
4388                sizeof(struct ocfs2_xattr_entry) * (count - start));
4389         xh->xh_count = cpu_to_le16(start);
4390         xh->xh_free_start = cpu_to_le16(name_offset);
4391         xh->xh_name_value_len = cpu_to_le16(name_value_len);
4392
4393         ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
4394
4395 out:
4396         ocfs2_xattr_bucket_free(s_bucket);
4397         ocfs2_xattr_bucket_free(t_bucket);
4398
4399         return ret;
4400 }
4401
4402 /*
4403  * Copy xattr from one bucket to another bucket.
4404  *
4405  * The caller must make sure that the journal transaction
4406  * has enough space for journaling.
4407  */
4408 static int ocfs2_cp_xattr_bucket(struct inode *inode,
4409                                  handle_t *handle,
4410                                  u64 s_blkno,
4411                                  u64 t_blkno,
4412                                  int t_is_new)
4413 {
4414         int ret;
4415         struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
4416
4417         BUG_ON(s_blkno == t_blkno);
4418
4419         mlog(0, "cp bucket %llu to %llu, target is %d\n",
4420              (unsigned long long)s_blkno, (unsigned long long)t_blkno,
4421              t_is_new);
4422
4423         s_bucket = ocfs2_xattr_bucket_new(inode);
4424         t_bucket = ocfs2_xattr_bucket_new(inode);
4425         if (!s_bucket || !t_bucket) {
4426                 ret = -ENOMEM;
4427                 mlog_errno(ret);
4428                 goto out;
4429         }
4430
4431         ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
4432         if (ret)
4433                 goto out;
4434
4435         /*
4436          * Even if !t_is_new, we're overwriting t_bucket.  Thus,
4437          * there's no need to read it.
4438          */
4439         ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
4440         if (ret)
4441                 goto out;
4442
4443         /*
4444          * Hey, if we're overwriting t_bucket, what difference does
4445          * ACCESS_CREATE vs ACCESS_WRITE make?  Well, if we allocated a new
4446          * cluster to fill, we came here from
4447          * ocfs2_mv_xattr_buckets(), and it is really new -
4448          * ACCESS_CREATE is required.  But we also might have moved data
4449          * out of t_bucket before extending back into it.
4450          * ocfs2_add_new_xattr_bucket() can do this - its call to
4451          * ocfs2_add_new_xattr_cluster() may have created a new extent
4452          * and copied out the end of the old extent.  Then it re-extends
4453          * the old extent back to create space for new xattrs.  That's
4454          * how we get here, and the bucket isn't really new.
4455          */
4456         ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
4457                                                 t_is_new ?
4458                                                 OCFS2_JOURNAL_ACCESS_CREATE :
4459                                                 OCFS2_JOURNAL_ACCESS_WRITE);
4460         if (ret)
4461                 goto out;
4462
4463         ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4464         ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
4465
4466 out:
4467         ocfs2_xattr_bucket_free(t_bucket);
4468         ocfs2_xattr_bucket_free(s_bucket);
4469
4470         return ret;
4471 }
4472
4473 /*
4474  * src_blk points to the start of an existing extent.  last_blk points to
4475  * last cluster in that extent.  to_blk points to a newly allocated
4476  * extent.  We copy the buckets from the cluster at last_blk to the new
4477  * extent.  If start_bucket is non-zero, we skip that many buckets before
4478  * we start copying.  The new extent's xh_num_buckets gets set to the
4479  * number of buckets we copied.  The old extent's xh_num_buckets shrinks
4480  * by the same amount.
4481  */
4482 static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
4483                                   u64 src_blk, u64 last_blk, u64 to_blk,
4484                                   unsigned int start_bucket,
4485                                   u32 *first_hash)
4486 {
4487         int i, ret, credits;
4488         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4489         int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4490         int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
4491         struct ocfs2_xattr_bucket *old_first, *new_first;
4492
4493         mlog(0, "mv xattrs from cluster %llu to %llu\n",
4494              (unsigned long long)last_blk, (unsigned long long)to_blk);
4495
4496         BUG_ON(start_bucket >= num_buckets);
4497         if (start_bucket) {
4498                 num_buckets -= start_bucket;
4499                 last_blk += (start_bucket * blks_per_bucket);
4500         }
4501
4502         /* The first bucket of the original extent */
4503         old_first = ocfs2_xattr_bucket_new(inode);
4504         /* The first bucket of the new extent */
4505         new_first = ocfs2_xattr_bucket_new(inode);
4506         if (!old_first || !new_first) {
4507                 ret = -ENOMEM;
4508                 mlog_errno(ret);
4509                 goto out;
4510         }
4511
4512         ret = ocfs2_read_xattr_bucket(old_first, src_blk);
4513         if (ret) {
4514                 mlog_errno(ret);
4515                 goto out;
4516         }
4517
4518         /*
4519          * We need to update the first bucket of the old extent and all
4520          * the buckets going to the new extent.
4521          */
4522         credits = ((num_buckets + 1) * blks_per_bucket) +
4523                 handle->h_buffer_credits;
4524         ret = ocfs2_extend_trans(handle, credits);
4525         if (ret) {
4526                 mlog_errno(ret);
4527                 goto out;
4528         }
4529
4530         ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
4531                                                 OCFS2_JOURNAL_ACCESS_WRITE);
4532         if (ret) {
4533                 mlog_errno(ret);
4534                 goto out;
4535         }
4536
4537         for (i = 0; i < num_buckets; i++) {
4538                 ret = ocfs2_cp_xattr_bucket(inode, handle,
4539                                             last_blk + (i * blks_per_bucket),
4540                                             to_blk + (i * blks_per_bucket),
4541                                             1);
4542                 if (ret) {
4543                         mlog_errno(ret);
4544                         goto out;
4545                 }
4546         }
4547
4548         /*
4549          * Get the new bucket ready before we dirty anything
4550          * (This actually shouldn't fail, because we already dirtied
4551          * it once in ocfs2_cp_xattr_bucket()).
4552          */
4553         ret = ocfs2_read_xattr_bucket(new_first, to_blk);
4554         if (ret) {
4555                 mlog_errno(ret);
4556                 goto out;
4557         }
4558         ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
4559                                                 OCFS2_JOURNAL_ACCESS_WRITE);
4560         if (ret) {
4561                 mlog_errno(ret);
4562                 goto out;
4563         }
4564
4565         /* Now update the headers */
4566         le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets);
4567         ocfs2_xattr_bucket_journal_dirty(handle, old_first);
4568
4569         bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets);
4570         ocfs2_xattr_bucket_journal_dirty(handle, new_first);
4571
4572         if (first_hash)
4573                 *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash);
4574
4575 out:
4576         ocfs2_xattr_bucket_free(new_first);
4577         ocfs2_xattr_bucket_free(old_first);
4578         return ret;
4579 }
4580
4581 /*
4582  * Move some xattrs in this cluster to the new cluster.
4583  * This function should only be called when bucket size == cluster size.
4584  * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
4585  */
4586 static int ocfs2_divide_xattr_cluster(struct inode *inode,
4587                                       handle_t *handle,
4588                                       u64 prev_blk,
4589                                       u64 new_blk,
4590                                       u32 *first_hash)
4591 {
4592         u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4593         int ret, credits = 2 * blk_per_bucket + handle->h_buffer_credits;
4594
4595         BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
4596
4597         ret = ocfs2_extend_trans(handle, credits);
4598         if (ret) {
4599                 mlog_errno(ret);
4600                 return ret;
4601         }
4602
4603         /* Move half of the xattr in start_blk to the next bucket. */
4604         return  ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
4605                                           new_blk, first_hash, 1);
4606 }
4607
4608 /*
4609  * Move some xattrs from the old cluster to the new one since they are not
4610  * contiguous in ocfs2 xattr tree.
4611  *
4612  * new_blk starts a new separate cluster, and we will move some xattrs from
4613  * prev_blk to it. v_start will be set as the first name hash value in this
4614  * new cluster so that it can be used as e_cpos during tree insertion and
4615  * don't collide with our original b-tree operations. first_bh and header_bh
4616  * will also be updated since they will be used in ocfs2_extend_xattr_bucket
4617  * to extend the insert bucket.
4618  *
4619  * The problem is how much xattr should we move to the new one and when should
4620  * we update first_bh and header_bh?
4621  * 1. If cluster size > bucket size, that means the previous cluster has more
4622  *    than 1 bucket, so just move half nums of bucket into the new cluster and
4623  *    update the first_bh and header_bh if the insert bucket has been moved
4624  *    to the new cluster.
4625  * 2. If cluster_size == bucket_size:
4626  *    a) If the previous extent rec has more than one cluster and the insert
4627  *       place isn't in the last cluster, copy the entire last cluster to the
4628  *       new one. This time, we don't need to upate the first_bh and header_bh
4629  *       since they will not be moved into the new cluster.
4630  *    b) Otherwise, move the bottom half of the xattrs in the last cluster into
4631  *       the new one. And we set the extend flag to zero if the insert place is
4632  *       moved into the new allocated cluster since no extend is needed.
4633  */
4634 static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
4635                                             handle_t *handle,
4636                                             struct ocfs2_xattr_bucket *first,
4637                                             struct ocfs2_xattr_bucket *target,
4638                                             u64 new_blk,
4639                                             u32 prev_clusters,
4640                                             u32 *v_start,
4641                                             int *extend)
4642 {
4643         int ret;
4644
4645         mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
4646              (unsigned long long)bucket_blkno(first), prev_clusters,
4647              (unsigned long long)new_blk);
4648
4649         if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) {
4650                 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
4651                                                           handle,
4652                                                           first, target,
4653                                                           new_blk,
4654                                                           prev_clusters,
4655                                                           v_start);
4656                 if (ret)
4657                         mlog_errno(ret);
4658         } else {
4659                 /* The start of the last cluster in the first extent */
4660                 u64 last_blk = bucket_blkno(first) +
4661                         ((prev_clusters - 1) *
4662                          ocfs2_clusters_to_blocks(inode->i_sb, 1));
4663
4664                 if (prev_clusters > 1 && bucket_blkno(target) != last_blk) {
4665                         ret = ocfs2_mv_xattr_buckets(inode, handle,
4666                                                      bucket_blkno(first),
4667                                                      last_blk, new_blk, 0,
4668                                                      v_start);
4669                         if (ret)
4670                                 mlog_errno(ret);
4671                 } else {
4672                         ret = ocfs2_divide_xattr_cluster(inode, handle,
4673                                                          last_blk, new_blk,
4674                                                          v_start);
4675                         if (ret)
4676                                 mlog_errno(ret);
4677
4678                         if ((bucket_blkno(target) == last_blk) && extend)
4679                                 *extend = 0;
4680                 }
4681         }
4682
4683         return ret;
4684 }
4685
4686 /*
4687  * Add a new cluster for xattr storage.
4688  *
4689  * If the new cluster is contiguous with the previous one, it will be
4690  * appended to the same extent record, and num_clusters will be updated.
4691  * If not, we will insert a new extent for it and move some xattrs in
4692  * the last cluster into the new allocated one.
4693  * We also need to limit the maximum size of a btree leaf, otherwise we'll
4694  * lose the benefits of hashing because we'll have to search large leaves.
4695  * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
4696  * if it's bigger).
4697  *
4698  * first_bh is the first block of the previous extent rec and header_bh
4699  * indicates the bucket we will insert the new xattrs. They will be updated
4700  * when the header_bh is moved into the new cluster.
4701  */
4702 static int ocfs2_add_new_xattr_cluster(struct inode *inode,
4703                                        struct buffer_head *root_bh,
4704                                        struct ocfs2_xattr_bucket *first,
4705                                        struct ocfs2_xattr_bucket *target,
4706                                        u32 *num_clusters,
4707                                        u32 prev_cpos,
4708                                        int *extend,
4709                                        struct ocfs2_xattr_set_ctxt *ctxt)
4710 {
4711         int ret;
4712         u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
4713         u32 prev_clusters = *num_clusters;
4714         u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
4715         u64 block;
4716         handle_t *handle = ctxt->handle;
4717         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4718         struct ocfs2_extent_tree et;
4719
4720         mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
4721              "previous xattr blkno = %llu\n",
4722              (unsigned long long)OCFS2_I(inode)->ip_blkno,
4723              prev_cpos, (unsigned long long)bucket_blkno(first));
4724
4725         ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
4726
4727         ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
4728                                       OCFS2_JOURNAL_ACCESS_WRITE);
4729         if (ret < 0) {
4730                 mlog_errno(ret);
4731                 goto leave;
4732         }
4733
4734         ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, 1,
4735                                      clusters_to_add, &bit_off, &num_bits);
4736         if (ret < 0) {
4737                 if (ret != -ENOSPC)
4738                         mlog_errno(ret);
4739                 goto leave;
4740         }
4741
4742         BUG_ON(num_bits > clusters_to_add);
4743
4744         block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4745         mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
4746              num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4747
4748         if (bucket_blkno(first) + (prev_clusters * bpc) == block &&
4749             (prev_clusters + num_bits) << osb->s_clustersize_bits <=
4750              OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
4751                 /*
4752                  * If this cluster is contiguous with the old one and
4753                  * adding this new cluster, we don't surpass the limit of
4754                  * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
4755                  * initialized and used like other buckets in the previous
4756                  * cluster.
4757                  * So add it as a contiguous one. The caller will handle
4758                  * its init process.
4759                  */
4760                 v_start = prev_cpos + prev_clusters;
4761                 *num_clusters = prev_clusters + num_bits;
4762                 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
4763                      num_bits);
4764         } else {
4765                 ret = ocfs2_adjust_xattr_cross_cluster(inode,
4766                                                        handle,
4767                                                        first,
4768                                                        target,
4769                                                        block,
4770                                                        prev_clusters,
4771                                                        &v_start,
4772                                                        extend);
4773                 if (ret) {
4774                         mlog_errno(ret);
4775                         goto leave;
4776                 }
4777         }
4778
4779         mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
4780              num_bits, (unsigned long long)block, v_start);
4781         ret = ocfs2_insert_extent(handle, &et, v_start, block,
4782                                   num_bits, 0, ctxt->meta_ac);
4783         if (ret < 0) {
4784                 mlog_errno(ret);
4785                 goto leave;
4786         }
4787
4788         ret = ocfs2_journal_dirty(handle, root_bh);
4789         if (ret < 0)
4790                 mlog_errno(ret);
4791
4792 leave:
4793         return ret;
4794 }
4795
4796 /*
4797  * We are given an extent.  'first' is the bucket at the very front of
4798  * the extent.  The extent has space for an additional bucket past
4799  * bucket_xh(first)->xh_num_buckets.  'target_blkno' is the block number
4800  * of the target bucket.  We wish to shift every bucket past the target
4801  * down one, filling in that additional space.  When we get back to the
4802  * target, we split the target between itself and the now-empty bucket
4803  * at target+1 (aka, target_blkno + blks_per_bucket).
4804  */
4805 static int ocfs2_extend_xattr_bucket(struct inode *inode,
4806                                      handle_t *handle,
4807                                      struct ocfs2_xattr_bucket *first,
4808                                      u64 target_blk,
4809                                      u32 num_clusters)
4810 {
4811         int ret, credits;
4812         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4813         u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4814         u64 end_blk;
4815         u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets);
4816
4817         mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
4818              "from %llu, len = %u\n", (unsigned long long)target_blk,
4819              (unsigned long long)bucket_blkno(first), num_clusters);
4820
4821         /* The extent must have room for an additional bucket */
4822         BUG_ON(new_bucket >=
4823                (num_clusters * ocfs2_xattr_buckets_per_cluster(osb)));
4824
4825         /* end_blk points to the last existing bucket */
4826         end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket);
4827
4828         /*
4829          * end_blk is the start of the last existing bucket.
4830          * Thus, (end_blk - target_blk) covers the target bucket and
4831          * every bucket after it up to, but not including, the last
4832          * existing bucket.  Then we add the last existing bucket, the
4833          * new bucket, and the first bucket (3 * blk_per_bucket).
4834          */
4835         credits = (end_blk - target_blk) + (3 * blk_per_bucket) +
4836                   handle->h_buffer_credits;
4837         ret = ocfs2_extend_trans(handle, credits);
4838         if (ret) {
4839                 mlog_errno(ret);
4840                 goto out;
4841         }
4842
4843         ret = ocfs2_xattr_bucket_journal_access(handle, first,
4844                                                 OCFS2_JOURNAL_ACCESS_WRITE);
4845         if (ret) {
4846                 mlog_errno(ret);
4847                 goto out;
4848         }
4849
4850         while (end_blk != target_blk) {
4851                 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
4852                                             end_blk + blk_per_bucket, 0);
4853                 if (ret)
4854                         goto out;
4855                 end_blk -= blk_per_bucket;
4856         }
4857
4858         /* Move half of the xattr in target_blkno to the next bucket. */
4859         ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk,
4860                                         target_blk + blk_per_bucket, NULL, 0);
4861
4862         le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1);
4863         ocfs2_xattr_bucket_journal_dirty(handle, first);
4864
4865 out:
4866         return ret;
4867 }
4868
4869 /*
4870  * Add new xattr bucket in an extent record and adjust the buckets
4871  * accordingly.  xb_bh is the ocfs2_xattr_block, and target is the
4872  * bucket we want to insert into.
4873  *
4874  * In the easy case, we will move all the buckets after target down by
4875  * one. Half of target's xattrs will be moved to the next bucket.
4876  *
4877  * If current cluster is full, we'll allocate a new one.  This may not
4878  * be contiguous.  The underlying calls will make sure that there is
4879  * space for the insert, shifting buckets around if necessary.
4880  * 'target' may be moved by those calls.
4881  */
4882 static int ocfs2_add_new_xattr_bucket(struct inode *inode,
4883                                       struct buffer_head *xb_bh,
4884                                       struct ocfs2_xattr_bucket *target,
4885                                       struct ocfs2_xattr_set_ctxt *ctxt)
4886 {
4887         struct ocfs2_xattr_block *xb =
4888                         (struct ocfs2_xattr_block *)xb_bh->b_data;
4889         struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
4890         struct ocfs2_extent_list *el = &xb_root->xt_list;
4891         u32 name_hash =
4892                 le32_to_cpu(bucket_xh(target)->xh_entries[0].xe_name_hash);
4893         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4894         int ret, num_buckets, extend = 1;
4895         u64 p_blkno;
4896         u32 e_cpos, num_clusters;
4897         /* The bucket at the front of the extent */
4898         struct ocfs2_xattr_bucket *first;
4899
4900         mlog(0, "Add new xattr bucket starting from %llu\n",
4901              (unsigned long long)bucket_blkno(target));
4902
4903         /* The first bucket of the original extent */
4904         first = ocfs2_xattr_bucket_new(inode);
4905         if (!first) {
4906                 ret = -ENOMEM;
4907                 mlog_errno(ret);
4908                 goto out;
4909         }
4910
4911         ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
4912                                   &num_clusters, el);
4913         if (ret) {
4914                 mlog_errno(ret);
4915                 goto out;
4916         }
4917
4918         ret = ocfs2_read_xattr_bucket(first, p_blkno);
4919         if (ret) {
4920                 mlog_errno(ret);
4921                 goto out;
4922         }
4923
4924         num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
4925         if (num_buckets == le16_to_cpu(bucket_xh(first)->xh_num_buckets)) {
4926                 /*
4927                  * This can move first+target if the target bucket moves
4928                  * to the new extent.
4929                  */
4930                 ret = ocfs2_add_new_xattr_cluster(inode,
4931                                                   xb_bh,
4932                                                   first,
4933                                                   target,
4934                                                   &num_clusters,
4935                                                   e_cpos,
4936                                                   &extend,
4937                                                   ctxt);
4938                 if (ret) {
4939                         mlog_errno(ret);
4940                         goto out;
4941                 }
4942         }
4943
4944         if (extend) {
4945                 ret = ocfs2_extend_xattr_bucket(inode,
4946                                                 ctxt->handle,
4947                                                 first,
4948                                                 bucket_blkno(target),
4949                                                 num_clusters);
4950                 if (ret)
4951                         mlog_errno(ret);
4952         }
4953
4954 out:
4955         ocfs2_xattr_bucket_free(first);
4956
4957         return ret;
4958 }
4959
4960 static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
4961                                         struct ocfs2_xattr_bucket *bucket,
4962                                         int offs)
4963 {
4964         int block_off = offs >> inode->i_sb->s_blocksize_bits;
4965
4966         offs = offs % inode->i_sb->s_blocksize;
4967         return bucket_block(bucket, block_off) + offs;
4968 }
4969
4970 /*
4971  * Handle the normal xattr set, including replace, delete and new.
4972  *
4973  * Note: "local" indicates the real data's locality. So we can't
4974  * just its bucket locality by its length.
4975  */
4976 static void ocfs2_xattr_set_entry_normal(struct inode *inode,
4977                                          struct ocfs2_xattr_info *xi,
4978                                          struct ocfs2_xattr_search *xs,
4979                                          u32 name_hash,
4980                                          int local)
4981 {
4982         struct ocfs2_xattr_entry *last, *xe;
4983         int name_len = strlen(xi->xi_name);
4984         struct ocfs2_xattr_header *xh = xs->header;
4985         u16 count = le16_to_cpu(xh->xh_count), start;
4986         size_t blocksize = inode->i_sb->s_blocksize;
4987         char *val;
4988         size_t offs, size, new_size;
4989         struct ocfs2_xa_loc loc;
4990
4991         ocfs2_init_xattr_bucket_xa_loc(&loc, xs->bucket,
4992                                        xs->not_found ? NULL : xs->here);
4993         last = &xh->xh_entries[count];
4994         if (!xs->not_found) {
4995                 xe = xs->here;
4996                 offs = le16_to_cpu(xe->xe_name_offset);
4997                 if (ocfs2_xattr_is_local(xe))
4998                         size = OCFS2_XATTR_SIZE(name_len) +
4999                         OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
5000                 else
5001                         size = OCFS2_XATTR_SIZE(name_len) +
5002                         OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
5003
5004                 /*
5005                  * If the new value will be stored outside, xi->xi_value has
5006                  * been initalized as an empty ocfs2_xattr_value_root, and
5007                  * the same goes with xi->xi_value_len, so we can set
5008                  * new_size safely here.
5009                  * See ocfs2_xattr_set_in_bucket.
5010                  */
5011                 new_size = OCFS2_XATTR_SIZE(name_len) +
5012                            OCFS2_XATTR_SIZE(xi->xi_value_len);
5013
5014                 if (xi->xi_value) {
5015                         ocfs2_xa_wipe_namevalue(&loc);
5016                         if (new_size > size)
5017                                 goto set_new_name_value;
5018
5019                         /* Now replace the old value with new one. */
5020                         if (local)
5021                                 xe->xe_value_size =
5022                                         cpu_to_le64(xi->xi_value_len);
5023                         else
5024                                 xe->xe_value_size = 0;
5025
5026                         val = ocfs2_xattr_bucket_get_val(inode,
5027                                                          xs->bucket, offs);
5028                         memset(val + OCFS2_XATTR_SIZE(name_len), 0,
5029                                size - OCFS2_XATTR_SIZE(name_len));
5030                         if (OCFS2_XATTR_SIZE(xi->xi_value_len) > 0)
5031                                 memcpy(val + OCFS2_XATTR_SIZE(name_len),
5032                                        xi->xi_value, xi->xi_value_len);
5033
5034                         le16_add_cpu(&xh->xh_name_value_len, new_size);
5035                         ocfs2_xattr_set_local(xe, local);
5036                         return;
5037                 } else {
5038                         ocfs2_xa_remove_entry(&loc);
5039                         if (!xh->xh_count)
5040                                 xh->xh_free_start =
5041                                         cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
5042
5043                         return;
5044                 }
5045         } else {
5046                 /* find a new entry for insert. */
5047                 int low = 0, high = count - 1, tmp;
5048                 struct ocfs2_xattr_entry *tmp_xe;
5049
5050                 while (low <= high && count) {
5051                         tmp = (low + high) / 2;
5052                         tmp_xe = &xh->xh_entries[tmp];
5053
5054                         if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
5055                                 low = tmp + 1;
5056                         else if (name_hash <
5057                                  le32_to_cpu(tmp_xe->xe_name_hash))
5058                                 high = tmp - 1;
5059                         else {
5060                                 low = tmp;
5061                                 break;
5062                         }
5063                 }
5064
5065                 xe = &xh->xh_entries[low];
5066                 if (low != count)
5067                         memmove(xe + 1, xe, (void *)last - (void *)xe);
5068
5069                 le16_add_cpu(&xh->xh_count, 1);
5070                 memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
5071                 xe->xe_name_hash = cpu_to_le32(name_hash);
5072                 xe->xe_name_len = name_len;
5073                 ocfs2_xattr_set_type(xe, xi->xi_name_index);
5074         }
5075
5076 set_new_name_value:
5077         /* Insert the new name+value. */
5078         size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->xi_value_len);
5079
5080         /*
5081          * We must make sure that the name/value pair
5082          * exists in the same block.
5083          */
5084         offs = le16_to_cpu(xh->xh_free_start);
5085         start = offs - size;
5086
5087         if (start >> inode->i_sb->s_blocksize_bits !=
5088             (offs - 1) >> inode->i_sb->s_blocksize_bits) {
5089                 offs = offs - offs % blocksize;
5090                 xh->xh_free_start = cpu_to_le16(offs);
5091         }
5092
5093         val = ocfs2_xattr_bucket_get_val(inode, xs->bucket, offs - size);
5094         xe->xe_name_offset = cpu_to_le16(offs - size);
5095
5096         memset(val, 0, size);
5097         memcpy(val, xi->xi_name, name_len);
5098         memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->xi_value,
5099                xi->xi_value_len);
5100
5101         xe->xe_value_size = cpu_to_le64(xi->xi_value_len);
5102         ocfs2_xattr_set_local(xe, local);
5103         xs->here = xe;
5104         le16_add_cpu(&xh->xh_free_start, -size);
5105         le16_add_cpu(&xh->xh_name_value_len, size);
5106
5107         return;
5108 }
5109
5110 /*
5111  * Set the xattr entry in the specified bucket.
5112  * The bucket is indicated by xs->bucket and it should have the enough
5113  * space for the xattr insertion.
5114  */
5115 static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
5116                                            handle_t *handle,
5117                                            struct ocfs2_xattr_info *xi,
5118                                            struct ocfs2_xattr_search *xs,
5119                                            u32 name_hash,
5120                                            int local)
5121 {
5122         int ret;
5123         u64 blkno;
5124
5125         mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
5126              (unsigned long)xi->xi_value_len, xi->xi_name_index,
5127              (unsigned long long)bucket_blkno(xs->bucket));
5128
5129         if (!xs->bucket->bu_bhs[1]) {
5130                 blkno = bucket_blkno(xs->bucket);
5131                 ocfs2_xattr_bucket_relse(xs->bucket);
5132                 ret = ocfs2_read_xattr_bucket(xs->bucket, blkno);
5133                 if (ret) {
5134                         mlog_errno(ret);
5135                         goto out;
5136                 }
5137         }
5138
5139         ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
5140                                                 OCFS2_JOURNAL_ACCESS_WRITE);
5141         if (ret < 0) {
5142                 mlog_errno(ret);
5143                 goto out;
5144         }
5145
5146         ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
5147         ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
5148
5149 out:
5150         return ret;
5151 }
5152
5153 /*
5154  * Truncate the specified xe_off entry in xattr bucket.
5155  * bucket is indicated by header_bh and len is the new length.
5156  * Both the ocfs2_xattr_value_root and the entry will be updated here.
5157  *
5158  * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
5159  */
5160 static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
5161                                              struct ocfs2_xattr_bucket *bucket,
5162                                              int xe_off,
5163                                              int len,
5164                                              struct ocfs2_xattr_set_ctxt *ctxt)
5165 {
5166         int ret, offset;
5167         u64 value_blk;
5168         struct ocfs2_xattr_entry *xe;
5169         struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5170         size_t blocksize = inode->i_sb->s_blocksize;
5171         struct ocfs2_xattr_value_buf vb = {
5172                 .vb_access = ocfs2_journal_access,
5173         };
5174
5175         xe = &xh->xh_entries[xe_off];
5176
5177         BUG_ON(!xe || ocfs2_xattr_is_local(xe));
5178
5179         offset = le16_to_cpu(xe->xe_name_offset) +
5180                  OCFS2_XATTR_SIZE(xe->xe_name_len);
5181
5182         value_blk = offset / blocksize;
5183
5184         /* We don't allow ocfs2_xattr_value to be stored in different block. */
5185         BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
5186
5187         vb.vb_bh = bucket->bu_bhs[value_blk];
5188         BUG_ON(!vb.vb_bh);
5189
5190         vb.vb_xv = (struct ocfs2_xattr_value_root *)
5191                 (vb.vb_bh->b_data + offset % blocksize);
5192
5193         /*
5194          * From here on out we have to dirty the bucket.  The generic
5195          * value calls only modify one of the bucket's bhs, but we need
5196          * to send the bucket at once.  So if they error, they *could* have
5197          * modified something.  We have to assume they did, and dirty
5198          * the whole bucket.  This leaves us in a consistent state.
5199          */
5200         mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
5201              xe_off, (unsigned long long)bucket_blkno(bucket), len);
5202         ret = ocfs2_xattr_value_truncate(inode, &vb, len, ctxt);
5203         if (ret) {
5204                 mlog_errno(ret);
5205                 goto out;
5206         }
5207
5208         ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
5209                                                 OCFS2_JOURNAL_ACCESS_WRITE);
5210         if (ret) {
5211                 mlog_errno(ret);
5212                 goto out;
5213         }
5214
5215         xe->xe_value_size = cpu_to_le64(len);
5216
5217         ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket);
5218
5219 out:
5220         return ret;
5221 }
5222
5223 static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
5224                                         struct ocfs2_xattr_search *xs,
5225                                         int len,
5226                                         struct ocfs2_xattr_set_ctxt *ctxt)
5227 {
5228         int ret, offset;
5229         struct ocfs2_xattr_entry *xe = xs->here;
5230         struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
5231
5232         BUG_ON(!xs->bucket->bu_bhs[0] || !xe || ocfs2_xattr_is_local(xe));
5233
5234         offset = xe - xh->xh_entries;
5235         ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket,
5236                                                 offset, len, ctxt);
5237         if (ret)
5238                 mlog_errno(ret);
5239
5240         return ret;
5241 }
5242
5243 static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
5244                                                 handle_t *handle,
5245                                                 struct ocfs2_xattr_search *xs,
5246                                                 char *val,
5247                                                 int value_len)
5248 {
5249         int ret, offset, block_off;
5250         struct ocfs2_xattr_value_root *xv;
5251         struct ocfs2_xattr_entry *xe = xs->here;
5252         struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
5253         void *base;
5254         struct ocfs2_xattr_value_buf vb = {
5255                 .vb_access = ocfs2_journal_access,
5256         };
5257
5258         BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
5259
5260         ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb, xh,
5261                                                 xe - xh->xh_entries,
5262                                                 &block_off,
5263                                                 &offset);
5264         if (ret) {
5265                 mlog_errno(ret);
5266                 goto out;
5267         }
5268
5269         base = bucket_block(xs->bucket, block_off);
5270         xv = (struct ocfs2_xattr_value_root *)(base + offset +
5271                  OCFS2_XATTR_SIZE(xe->xe_name_len));
5272
5273         vb.vb_xv = xv;
5274         vb.vb_bh = xs->bucket->bu_bhs[block_off];
5275         ret = __ocfs2_xattr_set_value_outside(inode, handle,
5276                                               &vb, val, value_len);
5277         if (ret)
5278                 mlog_errno(ret);
5279 out:
5280         return ret;
5281 }
5282
5283 static int ocfs2_rm_xattr_cluster(struct inode *inode,
5284                                   struct buffer_head *root_bh,
5285                                   u64 blkno,
5286                                   u32 cpos,
5287                                   u32 len,
5288                                   void *para)
5289 {
5290         int ret;
5291         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5292         struct inode *tl_inode = osb->osb_tl_inode;
5293         handle_t *handle;
5294         struct ocfs2_xattr_block *xb =
5295                         (struct ocfs2_xattr_block *)root_bh->b_data;
5296         struct ocfs2_alloc_context *meta_ac = NULL;
5297         struct ocfs2_cached_dealloc_ctxt dealloc;
5298         struct ocfs2_extent_tree et;
5299
5300         ret = ocfs2_iterate_xattr_buckets(inode, blkno, len,
5301                                           ocfs2_delete_xattr_in_bucket, para);
5302         if (ret) {
5303                 mlog_errno(ret);
5304                 return ret;
5305         }
5306
5307         ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
5308
5309         ocfs2_init_dealloc_ctxt(&dealloc);
5310
5311         mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
5312              cpos, len, (unsigned long long)blkno);
5313
5314         ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode), blkno,
5315                                                len);
5316
5317         ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
5318         if (ret) {
5319                 mlog_errno(ret);
5320                 return ret;
5321         }
5322
5323         mutex_lock(&tl_inode->i_mutex);
5324
5325         if (ocfs2_truncate_log_needs_flush(osb)) {
5326                 ret = __ocfs2_flush_truncate_log(osb);
5327                 if (ret < 0) {
5328                         mlog_errno(ret);
5329                         goto out;
5330                 }
5331         }
5332
5333         handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
5334         if (IS_ERR(handle)) {
5335                 ret = -ENOMEM;
5336                 mlog_errno(ret);
5337                 goto out;
5338         }
5339
5340         ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
5341                                       OCFS2_JOURNAL_ACCESS_WRITE);
5342         if (ret) {
5343                 mlog_errno(ret);
5344                 goto out_commit;
5345         }
5346
5347         ret = ocfs2_remove_extent(handle, &et, cpos, len, meta_ac,
5348                                   &dealloc);
5349         if (ret) {
5350                 mlog_errno(ret);
5351                 goto out_commit;
5352         }
5353
5354         le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
5355
5356         ret = ocfs2_journal_dirty(handle, root_bh);
5357         if (ret) {
5358                 mlog_errno(ret);
5359                 goto out_commit;
5360         }
5361
5362         ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
5363         if (ret)
5364                 mlog_errno(ret);
5365
5366 out_commit:
5367         ocfs2_commit_trans(osb, handle);
5368 out:
5369         ocfs2_schedule_truncate_log_flush(osb, 1);
5370
5371         mutex_unlock(&tl_inode->i_mutex);
5372
5373         if (meta_ac)
5374                 ocfs2_free_alloc_context(meta_ac);
5375
5376         ocfs2_run_deallocs(osb, &dealloc);
5377
5378         return ret;
5379 }
5380
5381 static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
5382                                          handle_t *handle,
5383                                          struct ocfs2_xattr_search *xs)
5384 {
5385         struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
5386         struct ocfs2_xattr_entry *last = &xh->xh_entries[
5387                                                 le16_to_cpu(xh->xh_count) - 1];
5388         int ret = 0;
5389
5390         ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
5391                                                 OCFS2_JOURNAL_ACCESS_WRITE);
5392         if (ret) {
5393                 mlog_errno(ret);
5394                 return;
5395         }
5396
5397         /* Remove the old entry. */
5398         memmove(xs->here, xs->here + 1,
5399                 (void *)last - (void *)xs->here);
5400         memset(last, 0, sizeof(struct ocfs2_xattr_entry));
5401         le16_add_cpu(&xh->xh_count, -1);
5402
5403         ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
5404 }
5405
5406 /*
5407  * Set the xattr name/value in the bucket specified in xs.
5408  *
5409  * As the new value in xi may be stored in the bucket or in an outside cluster,
5410  * we divide the whole process into 3 steps:
5411  * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
5412  * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
5413  * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
5414  * 4. If the clusters for the new outside value can't be allocated, we need
5415  *    to free the xattr we allocated in set.
5416  */
5417 static int ocfs2_xattr_set_in_bucket(struct inode *inode,
5418                                      struct ocfs2_xattr_info *xi,
5419                                      struct ocfs2_xattr_search *xs,
5420                                      struct ocfs2_xattr_set_ctxt *ctxt)
5421 {
5422         int ret, local = 1;
5423         size_t value_len;
5424         char *val = (char *)xi->xi_value;
5425         struct ocfs2_xattr_entry *xe = xs->here;
5426         u32 name_hash = ocfs2_xattr_name_hash(inode, xi->xi_name,
5427                                               strlen(xi->xi_name));
5428
5429         if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
5430                 /*
5431                  * We need to truncate the xattr storage first.
5432                  *
5433                  * If both the old and new value are stored to
5434                  * outside block, we only need to truncate
5435                  * the storage and then set the value outside.
5436                  *
5437                  * If the new value should be stored within block,
5438                  * we should free all the outside block first and
5439                  * the modification to the xattr block will be done
5440                  * by following steps.
5441                  */
5442                 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
5443                         value_len = xi->xi_value_len;
5444                 else
5445                         value_len = 0;
5446
5447                 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
5448                                                            value_len,
5449                                                            ctxt);
5450                 if (ret)
5451                         goto out;
5452
5453                 if (value_len)
5454                         goto set_value_outside;
5455         }
5456
5457         value_len = xi->xi_value_len;
5458         /* So we have to handle the inside block change now. */
5459         if (value_len > OCFS2_XATTR_INLINE_SIZE) {
5460                 /*
5461                  * If the new value will be stored outside of block,
5462                  * initalize a new empty value root and insert it first.
5463                  */
5464                 local = 0;
5465                 xi->xi_value = &def_xv;
5466                 xi->xi_value_len = OCFS2_XATTR_ROOT_SIZE;
5467         }
5468
5469         ret = ocfs2_xattr_set_entry_in_bucket(inode, ctxt->handle, xi, xs,
5470                                               name_hash, local);
5471         if (ret) {
5472                 mlog_errno(ret);
5473                 goto out;
5474         }
5475
5476         if (value_len <= OCFS2_XATTR_INLINE_SIZE)
5477                 goto out;
5478
5479         /* allocate the space now for the outside block storage. */
5480         ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
5481                                                    value_len, ctxt);
5482         if (ret) {
5483                 mlog_errno(ret);
5484
5485                 if (xs->not_found) {
5486                         /*
5487                          * We can't allocate enough clusters for outside
5488                          * storage and we have allocated xattr already,
5489                        &