932c0773160861e905ff6269aff50de36384d76b
[sfrench/cifs-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 name_index;
120         const char *name;
121         const void *value;
122         size_t 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 static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
141                                              struct ocfs2_xattr_header *xh,
142                                              int index,
143                                              int *block_off,
144                                              int *new_offset);
145
146 static int ocfs2_xattr_block_find(struct inode *inode,
147                                   int name_index,
148                                   const char *name,
149                                   struct ocfs2_xattr_search *xs);
150 static int ocfs2_xattr_index_block_find(struct inode *inode,
151                                         struct buffer_head *root_bh,
152                                         int name_index,
153                                         const char *name,
154                                         struct ocfs2_xattr_search *xs);
155
156 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
157                                         struct buffer_head *blk_bh,
158                                         char *buffer,
159                                         size_t buffer_size);
160
161 static int ocfs2_xattr_create_index_block(struct inode *inode,
162                                           struct ocfs2_xattr_search *xs,
163                                           struct ocfs2_xattr_set_ctxt *ctxt);
164
165 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
166                                              struct ocfs2_xattr_info *xi,
167                                              struct ocfs2_xattr_search *xs,
168                                              struct ocfs2_xattr_set_ctxt *ctxt);
169
170 typedef int (xattr_tree_rec_func)(struct inode *inode,
171                                   struct buffer_head *root_bh,
172                                   u64 blkno, u32 cpos, u32 len, void *para);
173 static int ocfs2_iterate_xattr_index_block(struct inode *inode,
174                                            struct buffer_head *root_bh,
175                                            xattr_tree_rec_func *rec_func,
176                                            void *para);
177 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
178                                         struct ocfs2_xattr_bucket *bucket,
179                                         void *para);
180 static int ocfs2_rm_xattr_cluster(struct inode *inode,
181                                   struct buffer_head *root_bh,
182                                   u64 blkno,
183                                   u32 cpos,
184                                   u32 len,
185                                   void *para);
186
187 static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
188                                   u64 src_blk, u64 last_blk, u64 to_blk,
189                                   unsigned int start_bucket,
190                                   u32 *first_hash);
191 static int ocfs2_prepare_refcount_xattr(struct inode *inode,
192                                         struct ocfs2_dinode *di,
193                                         struct ocfs2_xattr_info *xi,
194                                         struct ocfs2_xattr_search *xis,
195                                         struct ocfs2_xattr_search *xbs,
196                                         struct ocfs2_refcount_tree **ref_tree,
197                                         int *meta_need,
198                                         int *credits);
199 static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
200                                            struct ocfs2_xattr_bucket *bucket,
201                                            int offset,
202                                            struct ocfs2_xattr_value_root **xv,
203                                            struct buffer_head **bh);
204 static int ocfs2_xattr_security_set(struct inode *inode, const char *name,
205                                     const void *value, size_t size, int flags);
206
207 static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
208 {
209         return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
210 }
211
212 static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
213 {
214         return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
215 }
216
217 static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb)
218 {
219         u16 len = sb->s_blocksize -
220                  offsetof(struct ocfs2_xattr_header, xh_entries);
221
222         return len / sizeof(struct ocfs2_xattr_entry);
223 }
224
225 #define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
226 #define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
227 #define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
228
229 static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
230 {
231         struct ocfs2_xattr_bucket *bucket;
232         int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
233
234         BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
235
236         bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
237         if (bucket) {
238                 bucket->bu_inode = inode;
239                 bucket->bu_blocks = blks;
240         }
241
242         return bucket;
243 }
244
245 static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
246 {
247         int i;
248
249         for (i = 0; i < bucket->bu_blocks; i++) {
250                 brelse(bucket->bu_bhs[i]);
251                 bucket->bu_bhs[i] = NULL;
252         }
253 }
254
255 static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
256 {
257         if (bucket) {
258                 ocfs2_xattr_bucket_relse(bucket);
259                 bucket->bu_inode = NULL;
260                 kfree(bucket);
261         }
262 }
263
264 /*
265  * A bucket that has never been written to disk doesn't need to be
266  * read.  We just need the buffer_heads.  Don't call this for
267  * buckets that are already on disk.  ocfs2_read_xattr_bucket() initializes
268  * them fully.
269  */
270 static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
271                                    u64 xb_blkno)
272 {
273         int i, rc = 0;
274
275         for (i = 0; i < bucket->bu_blocks; i++) {
276                 bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
277                                               xb_blkno + i);
278                 if (!bucket->bu_bhs[i]) {
279                         rc = -EIO;
280                         mlog_errno(rc);
281                         break;
282                 }
283
284                 if (!ocfs2_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
285                                            bucket->bu_bhs[i]))
286                         ocfs2_set_new_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
287                                                       bucket->bu_bhs[i]);
288         }
289
290         if (rc)
291                 ocfs2_xattr_bucket_relse(bucket);
292         return rc;
293 }
294
295 /* Read the xattr bucket at xb_blkno */
296 static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
297                                    u64 xb_blkno)
298 {
299         int rc;
300
301         rc = ocfs2_read_blocks(INODE_CACHE(bucket->bu_inode), xb_blkno,
302                                bucket->bu_blocks, bucket->bu_bhs, 0,
303                                NULL);
304         if (!rc) {
305                 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
306                 rc = ocfs2_validate_meta_ecc_bhs(bucket->bu_inode->i_sb,
307                                                  bucket->bu_bhs,
308                                                  bucket->bu_blocks,
309                                                  &bucket_xh(bucket)->xh_check);
310                 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
311                 if (rc)
312                         mlog_errno(rc);
313         }
314
315         if (rc)
316                 ocfs2_xattr_bucket_relse(bucket);
317         return rc;
318 }
319
320 static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
321                                              struct ocfs2_xattr_bucket *bucket,
322                                              int type)
323 {
324         int i, rc = 0;
325
326         for (i = 0; i < bucket->bu_blocks; i++) {
327                 rc = ocfs2_journal_access(handle,
328                                           INODE_CACHE(bucket->bu_inode),
329                                           bucket->bu_bhs[i], type);
330                 if (rc) {
331                         mlog_errno(rc);
332                         break;
333                 }
334         }
335
336         return rc;
337 }
338
339 static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
340                                              struct ocfs2_xattr_bucket *bucket)
341 {
342         int i;
343
344         spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
345         ocfs2_compute_meta_ecc_bhs(bucket->bu_inode->i_sb,
346                                    bucket->bu_bhs, bucket->bu_blocks,
347                                    &bucket_xh(bucket)->xh_check);
348         spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
349
350         for (i = 0; i < bucket->bu_blocks; i++)
351                 ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
352 }
353
354 static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
355                                          struct ocfs2_xattr_bucket *src)
356 {
357         int i;
358         int blocksize = src->bu_inode->i_sb->s_blocksize;
359
360         BUG_ON(dest->bu_blocks != src->bu_blocks);
361         BUG_ON(dest->bu_inode != src->bu_inode);
362
363         for (i = 0; i < src->bu_blocks; i++) {
364                 memcpy(bucket_block(dest, i), bucket_block(src, i),
365                        blocksize);
366         }
367 }
368
369 static int ocfs2_validate_xattr_block(struct super_block *sb,
370                                       struct buffer_head *bh)
371 {
372         int rc;
373         struct ocfs2_xattr_block *xb =
374                 (struct ocfs2_xattr_block *)bh->b_data;
375
376         mlog(0, "Validating xattr block %llu\n",
377              (unsigned long long)bh->b_blocknr);
378
379         BUG_ON(!buffer_uptodate(bh));
380
381         /*
382          * If the ecc fails, we return the error but otherwise
383          * leave the filesystem running.  We know any error is
384          * local to this block.
385          */
386         rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &xb->xb_check);
387         if (rc)
388                 return rc;
389
390         /*
391          * Errors after here are fatal
392          */
393
394         if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
395                 ocfs2_error(sb,
396                             "Extended attribute block #%llu has bad "
397                             "signature %.*s",
398                             (unsigned long long)bh->b_blocknr, 7,
399                             xb->xb_signature);
400                 return -EINVAL;
401         }
402
403         if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) {
404                 ocfs2_error(sb,
405                             "Extended attribute block #%llu has an "
406                             "invalid xb_blkno of %llu",
407                             (unsigned long long)bh->b_blocknr,
408                             (unsigned long long)le64_to_cpu(xb->xb_blkno));
409                 return -EINVAL;
410         }
411
412         if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) {
413                 ocfs2_error(sb,
414                             "Extended attribute block #%llu has an invalid "
415                             "xb_fs_generation of #%u",
416                             (unsigned long long)bh->b_blocknr,
417                             le32_to_cpu(xb->xb_fs_generation));
418                 return -EINVAL;
419         }
420
421         return 0;
422 }
423
424 static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno,
425                                   struct buffer_head **bh)
426 {
427         int rc;
428         struct buffer_head *tmp = *bh;
429
430         rc = ocfs2_read_block(INODE_CACHE(inode), xb_blkno, &tmp,
431                               ocfs2_validate_xattr_block);
432
433         /* If ocfs2_read_block() got us a new bh, pass it up. */
434         if (!rc && !*bh)
435                 *bh = tmp;
436
437         return rc;
438 }
439
440 static inline const char *ocfs2_xattr_prefix(int name_index)
441 {
442         struct xattr_handler *handler = NULL;
443
444         if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
445                 handler = ocfs2_xattr_handler_map[name_index];
446
447         return handler ? handler->prefix : NULL;
448 }
449
450 static u32 ocfs2_xattr_name_hash(struct inode *inode,
451                                  const char *name,
452                                  int name_len)
453 {
454         /* Get hash value of uuid from super block */
455         u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
456         int i;
457
458         /* hash extended attribute name */
459         for (i = 0; i < name_len; i++) {
460                 hash = (hash << OCFS2_HASH_SHIFT) ^
461                        (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
462                        *name++;
463         }
464
465         return hash;
466 }
467
468 /*
469  * ocfs2_xattr_hash_entry()
470  *
471  * Compute the hash of an extended attribute.
472  */
473 static void ocfs2_xattr_hash_entry(struct inode *inode,
474                                    struct ocfs2_xattr_header *header,
475                                    struct ocfs2_xattr_entry *entry)
476 {
477         u32 hash = 0;
478         char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
479
480         hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
481         entry->xe_name_hash = cpu_to_le32(hash);
482
483         return;
484 }
485
486 static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
487 {
488         int size = 0;
489
490         if (value_len <= OCFS2_XATTR_INLINE_SIZE)
491                 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
492         else
493                 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
494         size += sizeof(struct ocfs2_xattr_entry);
495
496         return size;
497 }
498
499 int ocfs2_calc_security_init(struct inode *dir,
500                              struct ocfs2_security_xattr_info *si,
501                              int *want_clusters,
502                              int *xattr_credits,
503                              struct ocfs2_alloc_context **xattr_ac)
504 {
505         int ret = 0;
506         struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
507         int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
508                                                  si->value_len);
509
510         /*
511          * The max space of security xattr taken inline is
512          * 256(name) + 80(value) + 16(entry) = 352 bytes,
513          * So reserve one metadata block for it is ok.
514          */
515         if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
516             s_size > OCFS2_XATTR_FREE_IN_IBODY) {
517                 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
518                 if (ret) {
519                         mlog_errno(ret);
520                         return ret;
521                 }
522                 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
523         }
524
525         /* reserve clusters for xattr value which will be set in B tree*/
526         if (si->value_len > OCFS2_XATTR_INLINE_SIZE) {
527                 int new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
528                                                             si->value_len);
529
530                 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
531                                                            new_clusters);
532                 *want_clusters += new_clusters;
533         }
534         return ret;
535 }
536
537 int ocfs2_calc_xattr_init(struct inode *dir,
538                           struct buffer_head *dir_bh,
539                           int mode,
540                           struct ocfs2_security_xattr_info *si,
541                           int *want_clusters,
542                           int *xattr_credits,
543                           int *want_meta)
544 {
545         int ret = 0;
546         struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
547         int s_size = 0, a_size = 0, acl_len = 0, new_clusters;
548
549         if (si->enable)
550                 s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
551                                                      si->value_len);
552
553         if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
554                 acl_len = ocfs2_xattr_get_nolock(dir, dir_bh,
555                                         OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
556                                         "", NULL, 0);
557                 if (acl_len > 0) {
558                         a_size = ocfs2_xattr_entry_real_size(0, acl_len);
559                         if (S_ISDIR(mode))
560                                 a_size <<= 1;
561                 } else if (acl_len != 0 && acl_len != -ENODATA) {
562                         mlog_errno(ret);
563                         return ret;
564                 }
565         }
566
567         if (!(s_size + a_size))
568                 return ret;
569
570         /*
571          * The max space of security xattr taken inline is
572          * 256(name) + 80(value) + 16(entry) = 352 bytes,
573          * The max space of acl xattr taken inline is
574          * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
575          * when blocksize = 512, may reserve one more cluser for
576          * xattr bucket, otherwise reserve one metadata block
577          * for them is ok.
578          * If this is a new directory with inline data,
579          * we choose to reserve the entire inline area for
580          * directory contents and force an external xattr block.
581          */
582         if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
583             (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) ||
584             (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) {
585                 *want_meta = *want_meta + 1;
586                 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
587         }
588
589         if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
590             (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
591                 *want_clusters += 1;
592                 *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
593         }
594
595         /*
596          * reserve credits and clusters for xattrs which has large value
597          * and have to be set outside
598          */
599         if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE) {
600                 new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
601                                                         si->value_len);
602                 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
603                                                            new_clusters);
604                 *want_clusters += new_clusters;
605         }
606         if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
607             acl_len > OCFS2_XATTR_INLINE_SIZE) {
608                 /* for directory, it has DEFAULT and ACCESS two types of acls */
609                 new_clusters = (S_ISDIR(mode) ? 2 : 1) *
610                                 ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
611                 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
612                                                            new_clusters);
613                 *want_clusters += new_clusters;
614         }
615
616         return ret;
617 }
618
619 static int ocfs2_xattr_extend_allocation(struct inode *inode,
620                                          u32 clusters_to_add,
621                                          struct ocfs2_xattr_value_buf *vb,
622                                          struct ocfs2_xattr_set_ctxt *ctxt)
623 {
624         int status = 0;
625         handle_t *handle = ctxt->handle;
626         enum ocfs2_alloc_restarted why;
627         u32 prev_clusters, logical_start = le32_to_cpu(vb->vb_xv->xr_clusters);
628         struct ocfs2_extent_tree et;
629
630         mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
631
632         ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
633
634         status = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
635                               OCFS2_JOURNAL_ACCESS_WRITE);
636         if (status < 0) {
637                 mlog_errno(status);
638                 goto leave;
639         }
640
641         prev_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
642         status = ocfs2_add_clusters_in_btree(handle,
643                                              &et,
644                                              &logical_start,
645                                              clusters_to_add,
646                                              0,
647                                              ctxt->data_ac,
648                                              ctxt->meta_ac,
649                                              &why);
650         if (status < 0) {
651                 mlog_errno(status);
652                 goto leave;
653         }
654
655         status = ocfs2_journal_dirty(handle, vb->vb_bh);
656         if (status < 0) {
657                 mlog_errno(status);
658                 goto leave;
659         }
660
661         clusters_to_add -= le32_to_cpu(vb->vb_xv->xr_clusters) - prev_clusters;
662
663         /*
664          * We should have already allocated enough space before the transaction,
665          * so no need to restart.
666          */
667         BUG_ON(why != RESTART_NONE || clusters_to_add);
668
669 leave:
670
671         return status;
672 }
673
674 static int __ocfs2_remove_xattr_range(struct inode *inode,
675                                       struct ocfs2_xattr_value_buf *vb,
676                                       u32 cpos, u32 phys_cpos, u32 len,
677                                       unsigned int ext_flags,
678                                       struct ocfs2_xattr_set_ctxt *ctxt)
679 {
680         int ret;
681         u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
682         handle_t *handle = ctxt->handle;
683         struct ocfs2_extent_tree et;
684
685         ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
686
687         ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
688                             OCFS2_JOURNAL_ACCESS_WRITE);
689         if (ret) {
690                 mlog_errno(ret);
691                 goto out;
692         }
693
694         ret = ocfs2_remove_extent(handle, &et, cpos, len, ctxt->meta_ac,
695                                   &ctxt->dealloc);
696         if (ret) {
697                 mlog_errno(ret);
698                 goto out;
699         }
700
701         le32_add_cpu(&vb->vb_xv->xr_clusters, -len);
702
703         ret = ocfs2_journal_dirty(handle, vb->vb_bh);
704         if (ret) {
705                 mlog_errno(ret);
706                 goto out;
707         }
708
709         if (ext_flags & OCFS2_EXT_REFCOUNTED)
710                 ret = ocfs2_decrease_refcount(inode, handle,
711                                         ocfs2_blocks_to_clusters(inode->i_sb,
712                                                                  phys_blkno),
713                                         len, ctxt->meta_ac, &ctxt->dealloc, 1);
714         else
715                 ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc,
716                                                   phys_blkno, len);
717         if (ret)
718                 mlog_errno(ret);
719
720 out:
721         return ret;
722 }
723
724 static int ocfs2_xattr_shrink_size(struct inode *inode,
725                                    u32 old_clusters,
726                                    u32 new_clusters,
727                                    struct ocfs2_xattr_value_buf *vb,
728                                    struct ocfs2_xattr_set_ctxt *ctxt)
729 {
730         int ret = 0;
731         unsigned int ext_flags;
732         u32 trunc_len, cpos, phys_cpos, alloc_size;
733         u64 block;
734
735         if (old_clusters <= new_clusters)
736                 return 0;
737
738         cpos = new_clusters;
739         trunc_len = old_clusters - new_clusters;
740         while (trunc_len) {
741                 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
742                                                &alloc_size,
743                                                &vb->vb_xv->xr_list, &ext_flags);
744                 if (ret) {
745                         mlog_errno(ret);
746                         goto out;
747                 }
748
749                 if (alloc_size > trunc_len)
750                         alloc_size = trunc_len;
751
752                 ret = __ocfs2_remove_xattr_range(inode, vb, cpos,
753                                                  phys_cpos, alloc_size,
754                                                  ext_flags, ctxt);
755                 if (ret) {
756                         mlog_errno(ret);
757                         goto out;
758                 }
759
760                 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
761                 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode),
762                                                        block, alloc_size);
763                 cpos += alloc_size;
764                 trunc_len -= alloc_size;
765         }
766
767 out:
768         return ret;
769 }
770
771 static int ocfs2_xattr_value_truncate(struct inode *inode,
772                                       struct ocfs2_xattr_value_buf *vb,
773                                       int len,
774                                       struct ocfs2_xattr_set_ctxt *ctxt)
775 {
776         int ret;
777         u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
778         u32 old_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
779
780         if (new_clusters == old_clusters)
781                 return 0;
782
783         if (new_clusters > old_clusters)
784                 ret = ocfs2_xattr_extend_allocation(inode,
785                                                     new_clusters - old_clusters,
786                                                     vb, ctxt);
787         else
788                 ret = ocfs2_xattr_shrink_size(inode,
789                                               old_clusters, new_clusters,
790                                               vb, ctxt);
791
792         return ret;
793 }
794
795 static int ocfs2_xattr_list_entry(char *buffer, size_t size,
796                                   size_t *result, const char *prefix,
797                                   const char *name, int name_len)
798 {
799         char *p = buffer + *result;
800         int prefix_len = strlen(prefix);
801         int total_len = prefix_len + name_len + 1;
802
803         *result += total_len;
804
805         /* we are just looking for how big our buffer needs to be */
806         if (!size)
807                 return 0;
808
809         if (*result > size)
810                 return -ERANGE;
811
812         memcpy(p, prefix, prefix_len);
813         memcpy(p + prefix_len, name, name_len);
814         p[prefix_len + name_len] = '\0';
815
816         return 0;
817 }
818
819 static int ocfs2_xattr_list_entries(struct inode *inode,
820                                     struct ocfs2_xattr_header *header,
821                                     char *buffer, size_t buffer_size)
822 {
823         size_t result = 0;
824         int i, type, ret;
825         const char *prefix, *name;
826
827         for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
828                 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
829                 type = ocfs2_xattr_get_type(entry);
830                 prefix = ocfs2_xattr_prefix(type);
831
832                 if (prefix) {
833                         name = (const char *)header +
834                                 le16_to_cpu(entry->xe_name_offset);
835
836                         ret = ocfs2_xattr_list_entry(buffer, buffer_size,
837                                                      &result, prefix, name,
838                                                      entry->xe_name_len);
839                         if (ret)
840                                 return ret;
841                 }
842         }
843
844         return result;
845 }
846
847 int ocfs2_has_inline_xattr_value_outside(struct inode *inode,
848                                          struct ocfs2_dinode *di)
849 {
850         struct ocfs2_xattr_header *xh;
851         int i;
852
853         xh = (struct ocfs2_xattr_header *)
854                  ((void *)di + inode->i_sb->s_blocksize -
855                  le16_to_cpu(di->i_xattr_inline_size));
856
857         for (i = 0; i < le16_to_cpu(xh->xh_count); i++)
858                 if (!ocfs2_xattr_is_local(&xh->xh_entries[i]))
859                         return 1;
860
861         return 0;
862 }
863
864 static int ocfs2_xattr_ibody_list(struct inode *inode,
865                                   struct ocfs2_dinode *di,
866                                   char *buffer,
867                                   size_t buffer_size)
868 {
869         struct ocfs2_xattr_header *header = NULL;
870         struct ocfs2_inode_info *oi = OCFS2_I(inode);
871         int ret = 0;
872
873         if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
874                 return ret;
875
876         header = (struct ocfs2_xattr_header *)
877                  ((void *)di + inode->i_sb->s_blocksize -
878                  le16_to_cpu(di->i_xattr_inline_size));
879
880         ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
881
882         return ret;
883 }
884
885 static int ocfs2_xattr_block_list(struct inode *inode,
886                                   struct ocfs2_dinode *di,
887                                   char *buffer,
888                                   size_t buffer_size)
889 {
890         struct buffer_head *blk_bh = NULL;
891         struct ocfs2_xattr_block *xb;
892         int ret = 0;
893
894         if (!di->i_xattr_loc)
895                 return ret;
896
897         ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
898                                      &blk_bh);
899         if (ret < 0) {
900                 mlog_errno(ret);
901                 return ret;
902         }
903
904         xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
905         if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
906                 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
907                 ret = ocfs2_xattr_list_entries(inode, header,
908                                                buffer, buffer_size);
909         } else
910                 ret = ocfs2_xattr_tree_list_index_block(inode, blk_bh,
911                                                    buffer, buffer_size);
912
913         brelse(blk_bh);
914
915         return ret;
916 }
917
918 ssize_t ocfs2_listxattr(struct dentry *dentry,
919                         char *buffer,
920                         size_t size)
921 {
922         int ret = 0, i_ret = 0, b_ret = 0;
923         struct buffer_head *di_bh = NULL;
924         struct ocfs2_dinode *di = NULL;
925         struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
926
927         if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
928                 return -EOPNOTSUPP;
929
930         if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
931                 return ret;
932
933         ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
934         if (ret < 0) {
935                 mlog_errno(ret);
936                 return ret;
937         }
938
939         di = (struct ocfs2_dinode *)di_bh->b_data;
940
941         down_read(&oi->ip_xattr_sem);
942         i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
943         if (i_ret < 0)
944                 b_ret = 0;
945         else {
946                 if (buffer) {
947                         buffer += i_ret;
948                         size -= i_ret;
949                 }
950                 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
951                                                buffer, size);
952                 if (b_ret < 0)
953                         i_ret = 0;
954         }
955         up_read(&oi->ip_xattr_sem);
956         ocfs2_inode_unlock(dentry->d_inode, 0);
957
958         brelse(di_bh);
959
960         return i_ret + b_ret;
961 }
962
963 static int ocfs2_xattr_find_entry(int name_index,
964                                   const char *name,
965                                   struct ocfs2_xattr_search *xs)
966 {
967         struct ocfs2_xattr_entry *entry;
968         size_t name_len;
969         int i, cmp = 1;
970
971         if (name == NULL)
972                 return -EINVAL;
973
974         name_len = strlen(name);
975         entry = xs->here;
976         for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
977                 cmp = name_index - ocfs2_xattr_get_type(entry);
978                 if (!cmp)
979                         cmp = name_len - entry->xe_name_len;
980                 if (!cmp)
981                         cmp = memcmp(name, (xs->base +
982                                      le16_to_cpu(entry->xe_name_offset)),
983                                      name_len);
984                 if (cmp == 0)
985                         break;
986                 entry += 1;
987         }
988         xs->here = entry;
989
990         return cmp ? -ENODATA : 0;
991 }
992
993 static int ocfs2_xattr_get_value_outside(struct inode *inode,
994                                          struct ocfs2_xattr_value_root *xv,
995                                          void *buffer,
996                                          size_t len)
997 {
998         u32 cpos, p_cluster, num_clusters, bpc, clusters;
999         u64 blkno;
1000         int i, ret = 0;
1001         size_t cplen, blocksize;
1002         struct buffer_head *bh = NULL;
1003         struct ocfs2_extent_list *el;
1004
1005         el = &xv->xr_list;
1006         clusters = le32_to_cpu(xv->xr_clusters);
1007         bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1008         blocksize = inode->i_sb->s_blocksize;
1009
1010         cpos = 0;
1011         while (cpos < clusters) {
1012                 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1013                                                &num_clusters, el, NULL);
1014                 if (ret) {
1015                         mlog_errno(ret);
1016                         goto out;
1017                 }
1018
1019                 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1020                 /* Copy ocfs2_xattr_value */
1021                 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1022                         ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1023                                                &bh, NULL);
1024                         if (ret) {
1025                                 mlog_errno(ret);
1026                                 goto out;
1027                         }
1028
1029                         cplen = len >= blocksize ? blocksize : len;
1030                         memcpy(buffer, bh->b_data, cplen);
1031                         len -= cplen;
1032                         buffer += cplen;
1033
1034                         brelse(bh);
1035                         bh = NULL;
1036                         if (len == 0)
1037                                 break;
1038                 }
1039                 cpos += num_clusters;
1040         }
1041 out:
1042         return ret;
1043 }
1044
1045 static int ocfs2_xattr_ibody_get(struct inode *inode,
1046                                  int name_index,
1047                                  const char *name,
1048                                  void *buffer,
1049                                  size_t buffer_size,
1050                                  struct ocfs2_xattr_search *xs)
1051 {
1052         struct ocfs2_inode_info *oi = OCFS2_I(inode);
1053         struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1054         struct ocfs2_xattr_value_root *xv;
1055         size_t size;
1056         int ret = 0;
1057
1058         if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
1059                 return -ENODATA;
1060
1061         xs->end = (void *)di + inode->i_sb->s_blocksize;
1062         xs->header = (struct ocfs2_xattr_header *)
1063                         (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1064         xs->base = (void *)xs->header;
1065         xs->here = xs->header->xh_entries;
1066
1067         ret = ocfs2_xattr_find_entry(name_index, name, xs);
1068         if (ret)
1069                 return ret;
1070         size = le64_to_cpu(xs->here->xe_value_size);
1071         if (buffer) {
1072                 if (size > buffer_size)
1073                         return -ERANGE;
1074                 if (ocfs2_xattr_is_local(xs->here)) {
1075                         memcpy(buffer, (void *)xs->base +
1076                                le16_to_cpu(xs->here->xe_name_offset) +
1077                                OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
1078                 } else {
1079                         xv = (struct ocfs2_xattr_value_root *)
1080                                 (xs->base + le16_to_cpu(
1081                                  xs->here->xe_name_offset) +
1082                                 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
1083                         ret = ocfs2_xattr_get_value_outside(inode, xv,
1084                                                             buffer, size);
1085                         if (ret < 0) {
1086                                 mlog_errno(ret);
1087                                 return ret;
1088                         }
1089                 }
1090         }
1091
1092         return size;
1093 }
1094
1095 static int ocfs2_xattr_block_get(struct inode *inode,
1096                                  int name_index,
1097                                  const char *name,
1098                                  void *buffer,
1099                                  size_t buffer_size,
1100                                  struct ocfs2_xattr_search *xs)
1101 {
1102         struct ocfs2_xattr_block *xb;
1103         struct ocfs2_xattr_value_root *xv;
1104         size_t size;
1105         int ret = -ENODATA, name_offset, name_len, i;
1106         int uninitialized_var(block_off);
1107
1108         xs->bucket = ocfs2_xattr_bucket_new(inode);
1109         if (!xs->bucket) {
1110                 ret = -ENOMEM;
1111                 mlog_errno(ret);
1112                 goto cleanup;
1113         }
1114
1115         ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
1116         if (ret) {
1117                 mlog_errno(ret);
1118                 goto cleanup;
1119         }
1120
1121         if (xs->not_found) {
1122                 ret = -ENODATA;
1123                 goto cleanup;
1124         }
1125
1126         xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1127         size = le64_to_cpu(xs->here->xe_value_size);
1128         if (buffer) {
1129                 ret = -ERANGE;
1130                 if (size > buffer_size)
1131                         goto cleanup;
1132
1133                 name_offset = le16_to_cpu(xs->here->xe_name_offset);
1134                 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
1135                 i = xs->here - xs->header->xh_entries;
1136
1137                 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
1138                         ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
1139                                                                 bucket_xh(xs->bucket),
1140                                                                 i,
1141                                                                 &block_off,
1142                                                                 &name_offset);
1143                         xs->base = bucket_block(xs->bucket, block_off);
1144                 }
1145                 if (ocfs2_xattr_is_local(xs->here)) {
1146                         memcpy(buffer, (void *)xs->base +
1147                                name_offset + name_len, size);
1148                 } else {
1149                         xv = (struct ocfs2_xattr_value_root *)
1150                                 (xs->base + name_offset + name_len);
1151                         ret = ocfs2_xattr_get_value_outside(inode, xv,
1152                                                             buffer, size);
1153                         if (ret < 0) {
1154                                 mlog_errno(ret);
1155                                 goto cleanup;
1156                         }
1157                 }
1158         }
1159         ret = size;
1160 cleanup:
1161         ocfs2_xattr_bucket_free(xs->bucket);
1162
1163         brelse(xs->xattr_bh);
1164         xs->xattr_bh = NULL;
1165         return ret;
1166 }
1167
1168 int ocfs2_xattr_get_nolock(struct inode *inode,
1169                            struct buffer_head *di_bh,
1170                            int name_index,
1171                            const char *name,
1172                            void *buffer,
1173                            size_t buffer_size)
1174 {
1175         int ret;
1176         struct ocfs2_dinode *di = NULL;
1177         struct ocfs2_inode_info *oi = OCFS2_I(inode);
1178         struct ocfs2_xattr_search xis = {
1179                 .not_found = -ENODATA,
1180         };
1181         struct ocfs2_xattr_search xbs = {
1182                 .not_found = -ENODATA,
1183         };
1184
1185         if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1186                 return -EOPNOTSUPP;
1187
1188         if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1189                 ret = -ENODATA;
1190
1191         xis.inode_bh = xbs.inode_bh = di_bh;
1192         di = (struct ocfs2_dinode *)di_bh->b_data;
1193
1194         down_read(&oi->ip_xattr_sem);
1195         ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1196                                     buffer_size, &xis);
1197         if (ret == -ENODATA && di->i_xattr_loc)
1198                 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1199                                             buffer_size, &xbs);
1200         up_read(&oi->ip_xattr_sem);
1201
1202         return ret;
1203 }
1204
1205 /* ocfs2_xattr_get()
1206  *
1207  * Copy an extended attribute into the buffer provided.
1208  * Buffer is NULL to compute the size of buffer required.
1209  */
1210 static int ocfs2_xattr_get(struct inode *inode,
1211                            int name_index,
1212                            const char *name,
1213                            void *buffer,
1214                            size_t buffer_size)
1215 {
1216         int ret;
1217         struct buffer_head *di_bh = NULL;
1218
1219         ret = ocfs2_inode_lock(inode, &di_bh, 0);
1220         if (ret < 0) {
1221                 mlog_errno(ret);
1222                 return ret;
1223         }
1224         ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
1225                                      name, buffer, buffer_size);
1226
1227         ocfs2_inode_unlock(inode, 0);
1228
1229         brelse(di_bh);
1230
1231         return ret;
1232 }
1233
1234 static int __ocfs2_xattr_set_value_outside(struct inode *inode,
1235                                            handle_t *handle,
1236                                            struct ocfs2_xattr_value_buf *vb,
1237                                            const void *value,
1238                                            int value_len)
1239 {
1240         int ret = 0, i, cp_len;
1241         u16 blocksize = inode->i_sb->s_blocksize;
1242         u32 p_cluster, num_clusters;
1243         u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1244         u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1245         u64 blkno;
1246         struct buffer_head *bh = NULL;
1247         unsigned int ext_flags;
1248         struct ocfs2_xattr_value_root *xv = vb->vb_xv;
1249
1250         BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1251
1252         while (cpos < clusters) {
1253                 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1254                                                &num_clusters, &xv->xr_list,
1255                                                &ext_flags);
1256                 if (ret) {
1257                         mlog_errno(ret);
1258                         goto out;
1259                 }
1260
1261                 BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED);
1262
1263                 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1264
1265                 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1266                         ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1267                                                &bh, NULL);
1268                         if (ret) {
1269                                 mlog_errno(ret);
1270                                 goto out;
1271                         }
1272
1273                         ret = ocfs2_journal_access(handle,
1274                                                    INODE_CACHE(inode),
1275                                                    bh,
1276                                                    OCFS2_JOURNAL_ACCESS_WRITE);
1277                         if (ret < 0) {
1278                                 mlog_errno(ret);
1279                                 goto out;
1280                         }
1281
1282                         cp_len = value_len > blocksize ? blocksize : value_len;
1283                         memcpy(bh->b_data, value, cp_len);
1284                         value_len -= cp_len;
1285                         value += cp_len;
1286                         if (cp_len < blocksize)
1287                                 memset(bh->b_data + cp_len, 0,
1288                                        blocksize - cp_len);
1289
1290                         ret = ocfs2_journal_dirty(handle, bh);
1291                         if (ret < 0) {
1292                                 mlog_errno(ret);
1293                                 goto out;
1294                         }
1295                         brelse(bh);
1296                         bh = NULL;
1297
1298                         /*
1299                          * XXX: do we need to empty all the following
1300                          * blocks in this cluster?
1301                          */
1302                         if (!value_len)
1303                                 break;
1304                 }
1305                 cpos += num_clusters;
1306         }
1307 out:
1308         brelse(bh);
1309
1310         return ret;
1311 }
1312
1313 static int ocfs2_xattr_cleanup(struct inode *inode,
1314                                handle_t *handle,
1315                                struct ocfs2_xattr_info *xi,
1316                                struct ocfs2_xattr_search *xs,
1317                                struct ocfs2_xattr_value_buf *vb,
1318                                size_t offs)
1319 {
1320         int ret = 0;
1321         size_t name_len = strlen(xi->name);
1322         void *val = xs->base + offs;
1323         size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1324
1325         ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
1326                             OCFS2_JOURNAL_ACCESS_WRITE);
1327         if (ret) {
1328                 mlog_errno(ret);
1329                 goto out;
1330         }
1331         /* Decrease xattr count */
1332         le16_add_cpu(&xs->header->xh_count, -1);
1333         /* Remove the xattr entry and tree root which has already be set*/
1334         memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
1335         memset(val, 0, size);
1336
1337         ret = ocfs2_journal_dirty(handle, vb->vb_bh);
1338         if (ret < 0)
1339                 mlog_errno(ret);
1340 out:
1341         return ret;
1342 }
1343
1344 static int ocfs2_xattr_update_entry(struct inode *inode,
1345                                     handle_t *handle,
1346                                     struct ocfs2_xattr_info *xi,
1347                                     struct ocfs2_xattr_search *xs,
1348                                     struct ocfs2_xattr_value_buf *vb,
1349                                     size_t offs)
1350 {
1351         int ret;
1352
1353         ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
1354                             OCFS2_JOURNAL_ACCESS_WRITE);
1355         if (ret) {
1356                 mlog_errno(ret);
1357                 goto out;
1358         }
1359
1360         xs->here->xe_name_offset = cpu_to_le16(offs);
1361         xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1362         if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
1363                 ocfs2_xattr_set_local(xs->here, 1);
1364         else
1365                 ocfs2_xattr_set_local(xs->here, 0);
1366         ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1367
1368         ret = ocfs2_journal_dirty(handle, vb->vb_bh);
1369         if (ret < 0)
1370                 mlog_errno(ret);
1371 out:
1372         return ret;
1373 }
1374
1375 /*
1376  * ocfs2_xattr_set_value_outside()
1377  *
1378  * Set large size value in B tree.
1379  */
1380 static int ocfs2_xattr_set_value_outside(struct inode *inode,
1381                                          struct ocfs2_xattr_info *xi,
1382                                          struct ocfs2_xattr_search *xs,
1383                                          struct ocfs2_xattr_set_ctxt *ctxt,
1384                                          struct ocfs2_xattr_value_buf *vb,
1385                                          size_t offs)
1386 {
1387         size_t name_len = strlen(xi->name);
1388         void *val = xs->base + offs;
1389         struct ocfs2_xattr_value_root *xv = NULL;
1390         size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1391         int ret = 0;
1392
1393         memset(val, 0, size);
1394         memcpy(val, xi->name, name_len);
1395         xv = (struct ocfs2_xattr_value_root *)
1396                 (val + OCFS2_XATTR_SIZE(name_len));
1397         xv->xr_clusters = 0;
1398         xv->xr_last_eb_blk = 0;
1399         xv->xr_list.l_tree_depth = 0;
1400         xv->xr_list.l_count = cpu_to_le16(1);
1401         xv->xr_list.l_next_free_rec = 0;
1402         vb->vb_xv = xv;
1403
1404         ret = ocfs2_xattr_value_truncate(inode, vb, xi->value_len, ctxt);
1405         if (ret < 0) {
1406                 mlog_errno(ret);
1407                 return ret;
1408         }
1409         ret = ocfs2_xattr_update_entry(inode, ctxt->handle, xi, xs, vb, offs);
1410         if (ret < 0) {
1411                 mlog_errno(ret);
1412                 return ret;
1413         }
1414         ret = __ocfs2_xattr_set_value_outside(inode, ctxt->handle, vb,
1415                                               xi->value, xi->value_len);
1416         if (ret < 0)
1417                 mlog_errno(ret);
1418
1419         return ret;
1420 }
1421
1422 /*
1423  * ocfs2_xattr_set_entry_local()
1424  *
1425  * Set, replace or remove extended attribute in local.
1426  */
1427 static void ocfs2_xattr_set_entry_local(struct inode *inode,
1428                                         struct ocfs2_xattr_info *xi,
1429                                         struct ocfs2_xattr_search *xs,
1430                                         struct ocfs2_xattr_entry *last,
1431                                         size_t min_offs)
1432 {
1433         size_t name_len = strlen(xi->name);
1434         int i;
1435
1436         if (xi->value && xs->not_found) {
1437                 /* Insert the new xattr entry. */
1438                 le16_add_cpu(&xs->header->xh_count, 1);
1439                 ocfs2_xattr_set_type(last, xi->name_index);
1440                 ocfs2_xattr_set_local(last, 1);
1441                 last->xe_name_len = name_len;
1442         } else {
1443                 void *first_val;
1444                 void *val;
1445                 size_t offs, size;
1446
1447                 first_val = xs->base + min_offs;
1448                 offs = le16_to_cpu(xs->here->xe_name_offset);
1449                 val = xs->base + offs;
1450
1451                 if (le64_to_cpu(xs->here->xe_value_size) >
1452                     OCFS2_XATTR_INLINE_SIZE)
1453                         size = OCFS2_XATTR_SIZE(name_len) +
1454                                 OCFS2_XATTR_ROOT_SIZE;
1455                 else
1456                         size = OCFS2_XATTR_SIZE(name_len) +
1457                         OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1458
1459                 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1460                                 OCFS2_XATTR_SIZE(xi->value_len)) {
1461                         /* The old and the new value have the
1462                            same size. Just replace the value. */
1463                         ocfs2_xattr_set_local(xs->here, 1);
1464                         xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1465                         /* Clear value bytes. */
1466                         memset(val + OCFS2_XATTR_SIZE(name_len),
1467                                0,
1468                                OCFS2_XATTR_SIZE(xi->value_len));
1469                         memcpy(val + OCFS2_XATTR_SIZE(name_len),
1470                                xi->value,
1471                                xi->value_len);
1472                         return;
1473                 }
1474                 /* Remove the old name+value. */
1475                 memmove(first_val + size, first_val, val - first_val);
1476                 memset(first_val, 0, size);
1477                 xs->here->xe_name_hash = 0;
1478                 xs->here->xe_name_offset = 0;
1479                 ocfs2_xattr_set_local(xs->here, 1);
1480                 xs->here->xe_value_size = 0;
1481
1482                 min_offs += size;
1483
1484                 /* Adjust all value offsets. */
1485                 last = xs->header->xh_entries;
1486                 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1487                         size_t o = le16_to_cpu(last->xe_name_offset);
1488
1489                         if (o < offs)
1490                                 last->xe_name_offset = cpu_to_le16(o + size);
1491                         last += 1;
1492                 }
1493
1494                 if (!xi->value) {
1495                         /* Remove the old entry. */
1496                         last -= 1;
1497                         memmove(xs->here, xs->here + 1,
1498                                 (void *)last - (void *)xs->here);
1499                         memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1500                         le16_add_cpu(&xs->header->xh_count, -1);
1501                 }
1502         }
1503         if (xi->value) {
1504                 /* Insert the new name+value. */
1505                 size_t size = OCFS2_XATTR_SIZE(name_len) +
1506                                 OCFS2_XATTR_SIZE(xi->value_len);
1507                 void *val = xs->base + min_offs - size;
1508
1509                 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1510                 memset(val, 0, size);
1511                 memcpy(val, xi->name, name_len);
1512                 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1513                        xi->value,
1514                        xi->value_len);
1515                 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1516                 ocfs2_xattr_set_local(xs->here, 1);
1517                 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1518         }
1519
1520         return;
1521 }
1522
1523 /*
1524  * ocfs2_xattr_set_entry()
1525  *
1526  * Set extended attribute entry into inode or block.
1527  *
1528  * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1529  * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1530  * then set value in B tree with set_value_outside().
1531  */
1532 static int ocfs2_xattr_set_entry(struct inode *inode,
1533                                  struct ocfs2_xattr_info *xi,
1534                                  struct ocfs2_xattr_search *xs,
1535                                  struct ocfs2_xattr_set_ctxt *ctxt,
1536                                  int flag)
1537 {
1538         struct ocfs2_xattr_entry *last;
1539         struct ocfs2_inode_info *oi = OCFS2_I(inode);
1540         struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1541         size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1542         size_t size_l = 0;
1543         handle_t *handle = ctxt->handle;
1544         int free, i, ret;
1545         struct ocfs2_xattr_info xi_l = {
1546                 .name_index = xi->name_index,
1547                 .name = xi->name,
1548                 .value = xi->value,
1549                 .value_len = xi->value_len,
1550         };
1551         struct ocfs2_xattr_value_buf vb = {
1552                 .vb_bh = xs->xattr_bh,
1553                 .vb_access = ocfs2_journal_access_di,
1554         };
1555
1556         if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1557                 BUG_ON(xs->xattr_bh == xs->inode_bh);
1558                 vb.vb_access = ocfs2_journal_access_xb;
1559         } else
1560                 BUG_ON(xs->xattr_bh != xs->inode_bh);
1561
1562         /* Compute min_offs, last and free space. */
1563         last = xs->header->xh_entries;
1564
1565         for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1566                 size_t offs = le16_to_cpu(last->xe_name_offset);
1567                 if (offs < min_offs)
1568                         min_offs = offs;
1569                 last += 1;
1570         }
1571
1572         free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
1573         if (free < 0)
1574                 return -EIO;
1575
1576         if (!xs->not_found) {
1577                 size_t size = 0;
1578                 if (ocfs2_xattr_is_local(xs->here))
1579                         size = OCFS2_XATTR_SIZE(name_len) +
1580                         OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1581                 else
1582                         size = OCFS2_XATTR_SIZE(name_len) +
1583                                 OCFS2_XATTR_ROOT_SIZE;
1584                 free += (size + sizeof(struct ocfs2_xattr_entry));
1585         }
1586         /* Check free space in inode or block */
1587         if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1588                 if (free < sizeof(struct ocfs2_xattr_entry) +
1589                            OCFS2_XATTR_SIZE(name_len) +
1590                            OCFS2_XATTR_ROOT_SIZE) {
1591                         ret = -ENOSPC;
1592                         goto out;
1593                 }
1594                 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1595                 xi_l.value = (void *)&def_xv;
1596                 xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
1597         } else if (xi->value) {
1598                 if (free < sizeof(struct ocfs2_xattr_entry) +
1599                            OCFS2_XATTR_SIZE(name_len) +
1600                            OCFS2_XATTR_SIZE(xi->value_len)) {
1601                         ret = -ENOSPC;
1602                         goto out;
1603                 }
1604         }
1605
1606         if (!xs->not_found) {
1607                 /* For existing extended attribute */
1608                 size_t size = OCFS2_XATTR_SIZE(name_len) +
1609                         OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1610                 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1611                 void *val = xs->base + offs;
1612
1613                 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1614                         /* Replace existing local xattr with tree root */
1615                         ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
1616                                                             ctxt, &vb, offs);
1617                         if (ret < 0)
1618                                 mlog_errno(ret);
1619                         goto out;
1620                 } else if (!ocfs2_xattr_is_local(xs->here)) {
1621                         /* For existing xattr which has value outside */
1622                         vb.vb_xv = (struct ocfs2_xattr_value_root *)
1623                                 (val + OCFS2_XATTR_SIZE(name_len));
1624
1625                         if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1626                                 /*
1627                                  * If new value need set outside also,
1628                                  * first truncate old value to new value,
1629                                  * then set new value with set_value_outside().
1630                                  */
1631                                 ret = ocfs2_xattr_value_truncate(inode,
1632                                                                  &vb,
1633                                                                  xi->value_len,
1634                                                                  ctxt);
1635                                 if (ret < 0) {
1636                                         mlog_errno(ret);
1637                                         goto out;
1638                                 }
1639
1640                                 ret = ocfs2_xattr_update_entry(inode,
1641                                                                handle,
1642                                                                xi,
1643                                                                xs,
1644                                                                &vb,
1645                                                                offs);
1646                                 if (ret < 0) {
1647                                         mlog_errno(ret);
1648                                         goto out;
1649                                 }
1650
1651                                 ret = __ocfs2_xattr_set_value_outside(inode,
1652                                                                 handle,
1653                                                                 &vb,
1654                                                                 xi->value,
1655                                                                 xi->value_len);
1656                                 if (ret < 0)
1657                                         mlog_errno(ret);
1658                                 goto out;
1659                         } else {
1660                                 /*
1661                                  * If new value need set in local,
1662                                  * just trucate old value to zero.
1663                                  */
1664                                  ret = ocfs2_xattr_value_truncate(inode,
1665                                                                   &vb,
1666                                                                   0,
1667                                                                   ctxt);
1668                                 if (ret < 0)
1669                                         mlog_errno(ret);
1670                         }
1671                 }
1672         }
1673
1674         ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), xs->inode_bh,
1675                                       OCFS2_JOURNAL_ACCESS_WRITE);
1676         if (ret) {
1677                 mlog_errno(ret);
1678                 goto out;
1679         }
1680
1681         if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1682                 ret = vb.vb_access(handle, INODE_CACHE(inode), vb.vb_bh,
1683                                    OCFS2_JOURNAL_ACCESS_WRITE);
1684                 if (ret) {
1685                         mlog_errno(ret);
1686                         goto out;
1687                 }
1688         }
1689
1690         /*
1691          * Set value in local, include set tree root in local.
1692          * This is the first step for value size >INLINE_SIZE.
1693          */
1694         ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1695
1696         if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1697                 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1698                 if (ret < 0) {
1699                         mlog_errno(ret);
1700                         goto out;
1701                 }
1702         }
1703
1704         if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1705             (flag & OCFS2_INLINE_XATTR_FL)) {
1706                 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1707                 unsigned int xattrsize = osb->s_xattr_inline_size;
1708
1709                 /*
1710                  * Adjust extent record count or inline data size
1711                  * to reserve space for extended attribute.
1712                  */
1713                 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1714                         struct ocfs2_inline_data *idata = &di->id2.i_data;
1715                         le16_add_cpu(&idata->id_count, -xattrsize);
1716                 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1717                         struct ocfs2_extent_list *el = &di->id2.i_list;
1718                         le16_add_cpu(&el->l_count, -(xattrsize /
1719                                         sizeof(struct ocfs2_extent_rec)));
1720                 }
1721                 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1722         }
1723         /* Update xattr flag */
1724         spin_lock(&oi->ip_lock);
1725         oi->ip_dyn_features |= flag;
1726         di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1727         spin_unlock(&oi->ip_lock);
1728
1729         ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1730         if (ret < 0)
1731                 mlog_errno(ret);
1732
1733         if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1734                 /*
1735                  * Set value outside in B tree.
1736                  * This is the second step for value size > INLINE_SIZE.
1737                  */
1738                 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1739                 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, ctxt,
1740                                                     &vb, offs);
1741                 if (ret < 0) {
1742                         int ret2;
1743
1744                         mlog_errno(ret);
1745                         /*
1746                          * If set value outside failed, we have to clean
1747                          * the junk tree root we have already set in local.
1748                          */
1749                         ret2 = ocfs2_xattr_cleanup(inode, ctxt->handle,
1750                                                    xi, xs, &vb, offs);
1751                         if (ret2 < 0)
1752                                 mlog_errno(ret2);
1753                 }
1754         }
1755 out:
1756         return ret;
1757 }
1758
1759 /*
1760  * In xattr remove, if it is stored outside and refcounted, we may have
1761  * the chance to split the refcount tree. So need the allocators.
1762  */
1763 static int ocfs2_lock_xattr_remove_allocators(struct inode *inode,
1764                                         struct ocfs2_xattr_value_root *xv,
1765                                         struct ocfs2_caching_info *ref_ci,
1766                                         struct buffer_head *ref_root_bh,
1767                                         struct ocfs2_alloc_context **meta_ac,
1768                                         int *ref_credits)
1769 {
1770         int ret, meta_add = 0;
1771         u32 p_cluster, num_clusters;
1772         unsigned int ext_flags;
1773
1774         *ref_credits = 0;
1775         ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
1776                                        &num_clusters,
1777                                        &xv->xr_list,
1778                                        &ext_flags);
1779         if (ret) {
1780                 mlog_errno(ret);
1781                 goto out;
1782         }
1783
1784         if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
1785                 goto out;
1786
1787         ret = ocfs2_refcounted_xattr_delete_need(inode, ref_ci,
1788                                                  ref_root_bh, xv,
1789                                                  &meta_add, ref_credits);
1790         if (ret) {
1791                 mlog_errno(ret);
1792                 goto out;
1793         }
1794
1795         ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
1796                                                 meta_add, meta_ac);
1797         if (ret)
1798                 mlog_errno(ret);
1799
1800 out:
1801         return ret;
1802 }
1803
1804 static int ocfs2_remove_value_outside(struct inode*inode,
1805                                       struct ocfs2_xattr_value_buf *vb,
1806                                       struct ocfs2_xattr_header *header,
1807                                       struct ocfs2_caching_info *ref_ci,
1808                                       struct buffer_head *ref_root_bh)
1809 {
1810         int ret = 0, i, ref_credits;
1811         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1812         struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
1813         void *val;
1814
1815         ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
1816
1817         for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1818                 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1819
1820                 if (ocfs2_xattr_is_local(entry))
1821                         continue;
1822
1823                 val = (void *)header +
1824                         le16_to_cpu(entry->xe_name_offset);
1825                 vb->vb_xv = (struct ocfs2_xattr_value_root *)
1826                         (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
1827
1828                 ret = ocfs2_lock_xattr_remove_allocators(inode, vb->vb_xv,
1829                                                          ref_ci, ref_root_bh,
1830                                                          &ctxt.meta_ac,
1831                                                          &ref_credits);
1832
1833                 ctxt.handle = ocfs2_start_trans(osb, ref_credits +
1834                                         ocfs2_remove_extent_credits(osb->sb));
1835                 if (IS_ERR(ctxt.handle)) {
1836                         ret = PTR_ERR(ctxt.handle);
1837                         mlog_errno(ret);
1838                         break;
1839                 }
1840
1841                 ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt);
1842                 if (ret < 0) {
1843                         mlog_errno(ret);
1844                         break;
1845                 }
1846
1847                 ocfs2_commit_trans(osb, ctxt.handle);
1848                 if (ctxt.meta_ac) {
1849                         ocfs2_free_alloc_context(ctxt.meta_ac);
1850                         ctxt.meta_ac = NULL;
1851                 }
1852         }
1853
1854         if (ctxt.meta_ac)
1855                 ocfs2_free_alloc_context(ctxt.meta_ac);
1856         ocfs2_schedule_truncate_log_flush(osb, 1);
1857         ocfs2_run_deallocs(osb, &ctxt.dealloc);
1858         return ret;
1859 }
1860
1861 static int ocfs2_xattr_ibody_remove(struct inode *inode,
1862                                     struct buffer_head *di_bh,
1863                                     struct ocfs2_caching_info *ref_ci,
1864                                     struct buffer_head *ref_root_bh)
1865 {
1866
1867         struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1868         struct ocfs2_xattr_header *header;
1869         int ret;
1870         struct ocfs2_xattr_value_buf vb = {
1871                 .vb_bh = di_bh,
1872                 .vb_access = ocfs2_journal_access_di,
1873         };
1874
1875         header = (struct ocfs2_xattr_header *)
1876                  ((void *)di + inode->i_sb->s_blocksize -
1877                  le16_to_cpu(di->i_xattr_inline_size));
1878
1879         ret = ocfs2_remove_value_outside(inode, &vb, header,
1880                                          ref_ci, ref_root_bh);
1881
1882         return ret;
1883 }
1884
1885 struct ocfs2_rm_xattr_bucket_para {
1886         struct ocfs2_caching_info *ref_ci;
1887         struct buffer_head *ref_root_bh;
1888 };
1889
1890 static int ocfs2_xattr_block_remove(struct inode *inode,
1891                                     struct buffer_head *blk_bh,
1892                                     struct ocfs2_caching_info *ref_ci,
1893                                     struct buffer_head *ref_root_bh)
1894 {
1895         struct ocfs2_xattr_block *xb;
1896         int ret = 0;
1897         struct ocfs2_xattr_value_buf vb = {
1898                 .vb_bh = blk_bh,
1899                 .vb_access = ocfs2_journal_access_xb,
1900         };
1901         struct ocfs2_rm_xattr_bucket_para args = {
1902                 .ref_ci = ref_ci,
1903                 .ref_root_bh = ref_root_bh,
1904         };
1905
1906         xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1907         if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1908                 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
1909                 ret = ocfs2_remove_value_outside(inode, &vb, header,
1910                                                  ref_ci, ref_root_bh);
1911         } else
1912                 ret = ocfs2_iterate_xattr_index_block(inode,
1913                                                 blk_bh,
1914                                                 ocfs2_rm_xattr_cluster,
1915                                                 &args);
1916
1917         return ret;
1918 }
1919
1920 static int ocfs2_xattr_free_block(struct inode *inode,
1921                                   u64 block,
1922                                   struct ocfs2_caching_info *ref_ci,
1923                                   struct buffer_head *ref_root_bh)
1924 {
1925         struct inode *xb_alloc_inode;
1926         struct buffer_head *xb_alloc_bh = NULL;
1927         struct buffer_head *blk_bh = NULL;
1928         struct ocfs2_xattr_block *xb;
1929         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1930         handle_t *handle;
1931         int ret = 0;
1932         u64 blk, bg_blkno;
1933         u16 bit;
1934
1935         ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
1936         if (ret < 0) {
1937                 mlog_errno(ret);
1938                 goto out;
1939         }
1940
1941         ret = ocfs2_xattr_block_remove(inode, blk_bh, ref_ci, ref_root_bh);
1942         if (ret < 0) {
1943                 mlog_errno(ret);
1944                 goto out;
1945         }
1946
1947         xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1948         blk = le64_to_cpu(xb->xb_blkno);
1949         bit = le16_to_cpu(xb->xb_suballoc_bit);
1950         bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1951
1952         xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1953                                 EXTENT_ALLOC_SYSTEM_INODE,
1954                                 le16_to_cpu(xb->xb_suballoc_slot));
1955         if (!xb_alloc_inode) {
1956                 ret = -ENOMEM;
1957                 mlog_errno(ret);
1958                 goto out;
1959         }
1960         mutex_lock(&xb_alloc_inode->i_mutex);
1961
1962         ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1963         if (ret < 0) {
1964                 mlog_errno(ret);
1965                 goto out_mutex;
1966         }
1967
1968         handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
1969         if (IS_ERR(handle)) {
1970                 ret = PTR_ERR(handle);
1971                 mlog_errno(ret);
1972                 goto out_unlock;
1973         }
1974
1975         ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1976                                        bit, bg_blkno, 1);
1977         if (ret < 0)
1978                 mlog_errno(ret);
1979
1980         ocfs2_commit_trans(osb, handle);
1981 out_unlock:
1982         ocfs2_inode_unlock(xb_alloc_inode, 1);
1983         brelse(xb_alloc_bh);
1984 out_mutex:
1985         mutex_unlock(&xb_alloc_inode->i_mutex);
1986         iput(xb_alloc_inode);
1987 out:
1988         brelse(blk_bh);
1989         return ret;
1990 }
1991
1992 /*
1993  * ocfs2_xattr_remove()
1994  *
1995  * Free extended attribute resources associated with this inode.
1996  */
1997 int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
1998 {
1999         struct ocfs2_inode_info *oi = OCFS2_I(inode);
2000         struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2001         struct ocfs2_refcount_tree *ref_tree = NULL;
2002         struct buffer_head *ref_root_bh = NULL;
2003         struct ocfs2_caching_info *ref_ci = NULL;
2004         handle_t *handle;
2005         int ret;
2006
2007         if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2008                 return 0;
2009
2010         if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
2011                 return 0;
2012
2013         if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) {
2014                 ret = ocfs2_lock_refcount_tree(OCFS2_SB(inode->i_sb),
2015                                                le64_to_cpu(di->i_refcount_loc),
2016                                                1, &ref_tree, &ref_root_bh);
2017                 if (ret) {
2018                         mlog_errno(ret);
2019                         goto out;
2020                 }
2021                 ref_ci = &ref_tree->rf_ci;
2022
2023         }
2024
2025         if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2026                 ret = ocfs2_xattr_ibody_remove(inode, di_bh,
2027                                                ref_ci, ref_root_bh);
2028                 if (ret < 0) {
2029                         mlog_errno(ret);
2030                         goto out;
2031                 }
2032         }
2033
2034         if (di->i_xattr_loc) {
2035                 ret = ocfs2_xattr_free_block(inode,
2036                                              le64_to_cpu(di->i_xattr_loc),
2037                                              ref_ci, ref_root_bh);
2038                 if (ret < 0) {
2039                         mlog_errno(ret);
2040                         goto out;
2041                 }
2042         }
2043
2044         handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
2045                                    OCFS2_INODE_UPDATE_CREDITS);
2046         if (IS_ERR(handle)) {
2047                 ret = PTR_ERR(handle);
2048                 mlog_errno(ret);
2049                 goto out;
2050         }
2051         ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
2052                                       OCFS2_JOURNAL_ACCESS_WRITE);
2053         if (ret) {
2054                 mlog_errno(ret);
2055                 goto out_commit;
2056         }
2057
2058         di->i_xattr_loc = 0;
2059
2060         spin_lock(&oi->ip_lock);
2061         oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
2062         di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2063         spin_unlock(&oi->ip_lock);
2064
2065         ret = ocfs2_journal_dirty(handle, di_bh);
2066         if (ret < 0)
2067                 mlog_errno(ret);
2068 out_commit:
2069         ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
2070 out:
2071         if (ref_tree)
2072                 ocfs2_unlock_refcount_tree(OCFS2_SB(inode->i_sb), ref_tree, 1);
2073         brelse(ref_root_bh);
2074         return ret;
2075 }
2076
2077 static int ocfs2_xattr_has_space_inline(struct inode *inode,
2078                                         struct ocfs2_dinode *di)
2079 {
2080         struct ocfs2_inode_info *oi = OCFS2_I(inode);
2081         unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
2082         int free;
2083
2084         if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
2085                 return 0;
2086
2087         if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2088                 struct ocfs2_inline_data *idata = &di->id2.i_data;
2089                 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
2090         } else if (ocfs2_inode_is_fast_symlink(inode)) {
2091                 free = ocfs2_fast_symlink_chars(inode->i_sb) -
2092                         le64_to_cpu(di->i_size);
2093         } else {
2094                 struct ocfs2_extent_list *el = &di->id2.i_list;
2095                 free = (le16_to_cpu(el->l_count) -
2096                         le16_to_cpu(el->l_next_free_rec)) *
2097                         sizeof(struct ocfs2_extent_rec);
2098         }
2099         if (free >= xattrsize)
2100                 return 1;
2101
2102         return 0;
2103 }
2104
2105 /*
2106  * ocfs2_xattr_ibody_find()
2107  *
2108  * Find extended attribute in inode block and
2109  * fill search info into struct ocfs2_xattr_search.
2110  */
2111 static int ocfs2_xattr_ibody_find(struct inode *inode,
2112                                   int name_index,
2113                                   const char *name,
2114                                   struct ocfs2_xattr_search *xs)
2115 {
2116         struct ocfs2_inode_info *oi = OCFS2_I(inode);
2117         struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2118         int ret;
2119         int has_space = 0;
2120
2121         if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2122                 return 0;
2123
2124         if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2125                 down_read(&oi->ip_alloc_sem);
2126                 has_space = ocfs2_xattr_has_space_inline(inode, di);
2127                 up_read(&oi->ip_alloc_sem);
2128                 if (!has_space)
2129                         return 0;
2130         }
2131
2132         xs->xattr_bh = xs->inode_bh;
2133         xs->end = (void *)di + inode->i_sb->s_blocksize;
2134         if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
2135                 xs->header = (struct ocfs2_xattr_header *)
2136                         (xs->end - le16_to_cpu(di->i_xattr_inline_size));
2137         else
2138                 xs->header = (struct ocfs2_xattr_header *)
2139                         (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
2140         xs->base = (void *)xs->header;
2141         xs->here = xs->header->xh_entries;
2142
2143         /* Find the named attribute. */
2144         if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2145                 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2146                 if (ret && ret != -ENODATA)
2147                         return ret;
2148                 xs->not_found = ret;
2149         }
2150
2151         return 0;
2152 }
2153
2154 /*
2155  * ocfs2_xattr_ibody_set()
2156  *
2157  * Set, replace or remove an extended attribute into inode block.
2158  *
2159  */
2160 static int ocfs2_xattr_ibody_set(struct inode *inode,
2161                                  struct ocfs2_xattr_info *xi,
2162                                  struct ocfs2_xattr_search *xs,
2163                                  struct ocfs2_xattr_set_ctxt *ctxt)
2164 {
2165         struct ocfs2_inode_info *oi = OCFS2_I(inode);
2166         struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2167         int ret;
2168
2169         if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2170                 return -ENOSPC;
2171
2172         down_write(&oi->ip_alloc_sem);
2173         if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2174                 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2175                         ret = -ENOSPC;
2176                         goto out;
2177                 }
2178         }
2179
2180         ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
2181                                 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
2182 out:
2183         up_write(&oi->ip_alloc_sem);
2184
2185         return ret;
2186 }
2187
2188 /*
2189  * ocfs2_xattr_block_find()
2190  *
2191  * Find extended attribute in external block and
2192  * fill search info into struct ocfs2_xattr_search.
2193  */
2194 static int ocfs2_xattr_block_find(struct inode *inode,
2195                                   int name_index,
2196                                   const char *name,
2197                                   struct ocfs2_xattr_search *xs)
2198 {
2199         struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2200         struct buffer_head *blk_bh = NULL;
2201         struct ocfs2_xattr_block *xb;
2202         int ret = 0;
2203
2204         if (!di->i_xattr_loc)
2205                 return ret;
2206
2207         ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
2208                                      &blk_bh);
2209         if (ret < 0) {
2210                 mlog_errno(ret);
2211                 return ret;
2212         }
2213
2214         xs->xattr_bh = blk_bh;
2215         xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2216
2217         if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2218                 xs->header = &xb->xb_attrs.xb_header;
2219                 xs->base = (void *)xs->header;
2220                 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
2221                 xs->here = xs->header->xh_entries;
2222
2223                 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2224         } else
2225                 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
2226                                                    name_index,
2227                                                    name, xs);
2228
2229         if (ret && ret != -ENODATA) {
2230                 xs->xattr_bh = NULL;
2231                 goto cleanup;
2232         }
2233         xs->not_found = ret;
2234         return 0;
2235 cleanup:
2236         brelse(blk_bh);
2237
2238         return ret;
2239 }
2240
2241 static int ocfs2_create_xattr_block(handle_t *handle,
2242                                     struct inode *inode,
2243                                     struct buffer_head *inode_bh,
2244                                     struct ocfs2_alloc_context *meta_ac,
2245                                     struct buffer_head **ret_bh,
2246                                     int indexed)
2247 {
2248         int ret;
2249         u16 suballoc_bit_start;
2250         u32 num_got;
2251         u64 first_blkno;
2252         struct ocfs2_dinode *di =  (struct ocfs2_dinode *)inode_bh->b_data;
2253         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2254         struct buffer_head *new_bh = NULL;
2255         struct ocfs2_xattr_block *xblk;
2256
2257         ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), inode_bh,
2258                                       OCFS2_JOURNAL_ACCESS_CREATE);
2259         if (ret < 0) {
2260                 mlog_errno(ret);
2261                 goto end;
2262         }
2263
2264         ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
2265                                    &suballoc_bit_start, &num_got,
2266                                    &first_blkno);
2267         if (ret < 0) {
2268                 mlog_errno(ret);
2269                 goto end;
2270         }
2271
2272         new_bh = sb_getblk(inode->i_sb, first_blkno);
2273         ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
2274
2275         ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode),
2276                                       new_bh,
2277                                       OCFS2_JOURNAL_ACCESS_CREATE);
2278         if (ret < 0) {
2279                 mlog_errno(ret);
2280                 goto end;
2281         }
2282
2283         /* Initialize ocfs2_xattr_block */
2284         xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2285         memset(xblk, 0, inode->i_sb->s_blocksize);
2286         strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
2287         xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
2288         xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
2289         xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
2290         xblk->xb_blkno = cpu_to_le64(first_blkno);
2291
2292         if (indexed) {
2293                 struct ocfs2_xattr_tree_root *xr = &xblk->xb_attrs.xb_root;
2294                 xr->xt_clusters = cpu_to_le32(1);
2295                 xr->xt_last_eb_blk = 0;
2296                 xr->xt_list.l_tree_depth = 0;
2297                 xr->xt_list.l_count = cpu_to_le16(
2298                                         ocfs2_xattr_recs_per_xb(inode->i_sb));
2299                 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2300                 xblk->xb_flags = cpu_to_le16(OCFS2_XATTR_INDEXED);
2301         }
2302
2303         ret = ocfs2_journal_dirty(handle, new_bh);
2304         if (ret < 0) {
2305                 mlog_errno(ret);
2306                 goto end;
2307         }
2308         di->i_xattr_loc = cpu_to_le64(first_blkno);
2309         ocfs2_journal_dirty(handle, inode_bh);
2310
2311         *ret_bh = new_bh;
2312         new_bh = NULL;
2313
2314 end:
2315         brelse(new_bh);
2316         return ret;
2317 }
2318
2319 /*
2320  * ocfs2_xattr_block_set()
2321  *
2322  * Set, replace or remove an extended attribute into external block.
2323  *
2324  */
2325 static int ocfs2_xattr_block_set(struct inode *inode,
2326                                  struct ocfs2_xattr_info *xi,
2327                                  struct ocfs2_xattr_search *xs,
2328                                  struct ocfs2_xattr_set_ctxt *ctxt)
2329 {
2330         struct buffer_head *new_bh = NULL;
2331         handle_t *handle = ctxt->handle;
2332         struct ocfs2_xattr_block *xblk = NULL;
2333         int ret;
2334
2335         if (!xs->xattr_bh) {
2336                 ret = ocfs2_create_xattr_block(handle, inode, xs->inode_bh,
2337                                                ctxt->meta_ac, &new_bh, 0);
2338                 if (ret) {
2339                         mlog_errno(ret);
2340                         goto end;
2341                 }
2342
2343                 xs->xattr_bh = new_bh;
2344                 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2345                 xs->header = &xblk->xb_attrs.xb_header;
2346                 xs->base = (void *)xs->header;
2347                 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2348                 xs->here = xs->header->xh_entries;
2349         } else
2350                 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2351
2352         if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
2353                 /* Set extended attribute into external block */
2354                 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
2355                                             OCFS2_HAS_XATTR_FL);
2356                 if (!ret || ret != -ENOSPC)
2357                         goto end;
2358
2359                 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
2360                 if (ret)
2361                         goto end;
2362         }
2363
2364         ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
2365
2366 end:
2367
2368         return ret;
2369 }
2370
2371 /* Check whether the new xattr can be inserted into the inode. */
2372 static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
2373                                        struct ocfs2_xattr_info *xi,
2374                                        struct ocfs2_xattr_search *xs)
2375 {
2376         u64 value_size;
2377         struct ocfs2_xattr_entry *last;
2378         int free, i;
2379         size_t min_offs = xs->end - xs->base;
2380
2381         if (!xs->header)
2382                 return 0;
2383
2384         last = xs->header->xh_entries;
2385
2386         for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
2387                 size_t offs = le16_to_cpu(last->xe_name_offset);
2388                 if (offs < min_offs)
2389                         min_offs = offs;
2390                 last += 1;
2391         }
2392
2393         free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
2394         if (free < 0)
2395                 return 0;
2396
2397         BUG_ON(!xs->not_found);
2398
2399         if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
2400                 value_size = OCFS2_XATTR_ROOT_SIZE;
2401         else
2402                 value_size = OCFS2_XATTR_SIZE(xi->value_len);
2403
2404         if (free >= sizeof(struct ocfs2_xattr_entry) +
2405                    OCFS2_XATTR_SIZE(strlen(xi->name)) + value_size)
2406                 return 1;
2407
2408         return 0;
2409 }
2410
2411 static int ocfs2_calc_xattr_set_need(struct inode *inode,
2412                                      struct ocfs2_dinode *di,
2413                                      struct ocfs2_xattr_info *xi,
2414                                      struct ocfs2_xattr_search *xis,
2415                                      struct ocfs2_xattr_search *xbs,
2416                                      int *clusters_need,
2417                                      int *meta_need,
2418                                      int *credits_need)
2419 {
2420         int ret = 0, old_in_xb = 0;
2421         int clusters_add = 0, meta_add = 0, credits = 0;
2422         struct buffer_head *bh = NULL;
2423         struct ocfs2_xattr_block *xb = NULL;
2424         struct ocfs2_xattr_entry *xe = NULL;
2425         struct ocfs2_xattr_value_root *xv = NULL;
2426         char *base = NULL;
2427         int name_offset, name_len = 0;
2428         u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2429                                                     xi->value_len);
2430         u64 value_size;
2431
2432         /*
2433          * Calculate the clusters we need to write.
2434          * No matter whether we replace an old one or add a new one,
2435          * we need this for writing.
2436          */
2437         if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
2438                 credits += new_clusters *
2439                            ocfs2_clusters_to_blocks(inode->i_sb, 1);
2440
2441         if (xis->not_found && xbs->not_found) {
2442                 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2443
2444                 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
2445                         clusters_add += new_clusters;
2446                         credits += ocfs2_calc_extend_credits(inode->i_sb,
2447                                                         &def_xv.xv.xr_list,
2448                                                         new_clusters);
2449                 }
2450
2451                 goto meta_guess;
2452         }
2453
2454         if (!xis->not_found) {
2455                 xe = xis->here;
2456                 name_offset = le16_to_cpu(xe->xe_name_offset);
2457                 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2458                 base = xis->base;
2459                 credits += OCFS2_INODE_UPDATE_CREDITS;
2460         } else {
2461                 int i, block_off = 0;
2462                 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2463                 xe = xbs->here;
2464                 name_offset = le16_to_cpu(xe->xe_name_offset);
2465                 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2466                 i = xbs->here - xbs->header->xh_entries;
2467                 old_in_xb = 1;
2468
2469                 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2470                         ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
2471                                                         bucket_xh(xbs->bucket),
2472                                                         i, &block_off,
2473                                                         &name_offset);
2474                         base = bucket_block(xbs->bucket, block_off);
2475                         credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2476                 } else {
2477                         base = xbs->base;
2478                         credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
2479                 }
2480         }
2481
2482         /*
2483          * delete a xattr doesn't need metadata and cluster allocation.
2484          * so just calculate the credits and return.
2485          *
2486          * The credits for removing the value tree will be extended
2487          * by ocfs2_remove_extent itself.
2488          */
2489         if (!xi->value) {
2490                 if (!ocfs2_xattr_is_local(xe))
2491                         credits += ocfs2_remove_extent_credits(inode->i_sb);
2492
2493                 goto out;
2494         }
2495
2496         /* do cluster allocation guess first. */
2497         value_size = le64_to_cpu(xe->xe_value_size);
2498
2499         if (old_in_xb) {
2500                 /*
2501                  * In xattr set, we always try to set the xe in inode first,
2502                  * so if it can be inserted into inode successfully, the old
2503                  * one will be removed from the xattr block, and this xattr
2504                  * will be inserted into inode as a new xattr in inode.
2505                  */
2506                 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
2507                         clusters_add += new_clusters;
2508                         credits += ocfs2_remove_extent_credits(inode->i_sb) +
2509                                     OCFS2_INODE_UPDATE_CREDITS;
2510                         if (!ocfs2_xattr_is_local(xe))
2511                                 credits += ocfs2_calc_extend_credits(
2512                                                         inode->i_sb,
2513                                                         &def_xv.xv.xr_list,
2514                                                         new_clusters);
2515                         goto out;
2516                 }
2517         }
2518
2519         if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
2520                 /* the new values will be stored outside. */
2521                 u32 old_clusters = 0;
2522
2523                 if (!ocfs2_xattr_is_local(xe)) {
2524                         old_clusters =  ocfs2_clusters_for_bytes(inode->i_sb,
2525                                                                  value_size);
2526                         xv = (struct ocfs2_xattr_value_root *)
2527                              (base + name_offset + name_len);
2528                         value_size = OCFS2_XATTR_ROOT_SIZE;
2529                 } else
2530                         xv = &def_xv.xv;
2531
2532                 if (old_clusters >= new_clusters) {
2533                         credits += ocfs2_remove_extent_credits(inode->i_sb);
2534                         goto out;
2535                 } else {
2536                         meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
2537                         clusters_add += new_clusters - old_clusters;
2538                         credits += ocfs2_calc_extend_credits(inode->i_sb,
2539                                                              &xv->xr_list,
2540                                                              new_clusters -
2541                                                              old_clusters);
2542                         if (value_size >= OCFS2_XATTR_ROOT_SIZE)
2543                                 goto out;
2544                 }
2545         } else {
2546                 /*
2547                  * Now the new value will be stored inside. So if the new
2548                  * value is smaller than the size of value root or the old
2549                  * value, we don't need any allocation, otherwise we have
2550                  * to guess metadata allocation.
2551                  */
2552                 if ((ocfs2_xattr_is_local(xe) && value_size >= xi->value_len) ||
2553                     (!ocfs2_xattr_is_local(xe) &&
2554                      OCFS2_XATTR_ROOT_SIZE >= xi->value_len))
2555                         goto out;
2556         }
2557
2558 meta_guess:
2559         /* calculate metadata allocation. */
2560         if (di->i_xattr_loc) {
2561                 if (!xbs->xattr_bh) {
2562                         ret = ocfs2_read_xattr_block(inode,
2563                                                      le64_to_cpu(di->i_xattr_loc),
2564                                                      &bh);
2565                         if (ret) {
2566                                 mlog_errno(ret);
2567                                 goto out;
2568                         }
2569
2570                         xb = (struct ocfs2_xattr_block *)bh->b_data;
2571                 } else
2572                         xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2573
2574                 /*
2575                  * If there is already an xattr tree, good, we can calculate
2576                  * like other b-trees. Otherwise we may have the chance of
2577                  * create a tree, the credit calculation is borrowed from
2578                  * ocfs2_calc_extend_credits with root_el = NULL. And the
2579                  * new tree will be cluster based, so no meta is needed.
2580                  */
2581                 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2582                         struct ocfs2_extent_list *el =
2583                                  &xb->xb_attrs.xb_root.xt_list;
2584                         meta_add += ocfs2_extend_meta_needed(el);
2585                         credits += ocfs2_calc_extend_credits(inode->i_sb,
2586                                                              el, 1);
2587                 } else
2588                         credits += OCFS2_SUBALLOC_ALLOC + 1;
2589
2590                 /*
2591                  * This cluster will be used either for new bucket or for
2592                  * new xattr block.
2593                  * If the cluster size is the same as the bucket size, one
2594                  * more is needed since we may need to extend the bucket
2595                  * also.
2596                  */
2597                 clusters_add += 1;
2598                 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2599                 if (OCFS2_XATTR_BUCKET_SIZE ==
2600                         OCFS2_SB(inode->i_sb)->s_clustersize) {
2601                         credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2602                         clusters_add += 1;
2603                 }
2604         } else {
2605                 meta_add += 1;
2606                 credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
2607         }
2608 out:
2609         if (clusters_need)
2610                 *clusters_need = clusters_add;
2611         if (meta_need)
2612                 *meta_need = meta_add;
2613         if (credits_need)
2614                 *credits_need = credits;
2615         brelse(bh);
2616         return ret;
2617 }
2618
2619 static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
2620                                      struct ocfs2_dinode *di,
2621                                      struct ocfs2_xattr_info *xi,
2622                                      struct ocfs2_xattr_search *xis,
2623                                      struct ocfs2_xattr_search *xbs,
2624                                      struct ocfs2_xattr_set_ctxt *ctxt,
2625                                      int extra_meta,
2626                                      int *credits)
2627 {
2628         int clusters_add, meta_add, ret;
2629         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2630
2631         memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
2632
2633         ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
2634
2635         ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
2636                                         &clusters_add, &meta_add, credits);
2637         if (ret) {
2638                 mlog_errno(ret);
2639                 return ret;
2640         }
2641
2642         meta_add += extra_meta;
2643         mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
2644              "credits = %d\n", xi->name, meta_add, clusters_add, *credits);
2645
2646         if (meta_add) {
2647                 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
2648                                                         &ctxt->meta_ac);
2649                 if (ret) {
2650                         mlog_errno(ret);
2651                         goto out;
2652                 }
2653         }
2654
2655         if (clusters_add) {
2656                 ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
2657                 if (ret)
2658                         mlog_errno(ret);
2659         }
2660 out:
2661         if (ret) {
2662                 if (ctxt->meta_ac) {
2663                         ocfs2_free_alloc_context(ctxt->meta_ac);
2664                         ctxt->meta_ac = NULL;
2665                 }
2666
2667                 /*
2668                  * We cannot have an error and a non null ctxt->data_ac.
2669                  */
2670         }
2671
2672         return ret;
2673 }
2674
2675 static int __ocfs2_xattr_set_handle(struct inode *inode,
2676                                     struct ocfs2_dinode *di,
2677                                     struct ocfs2_xattr_info *xi,
2678                                     struct ocfs2_xattr_search *xis,
2679                                     struct ocfs2_xattr_search *xbs,
2680                                     struct ocfs2_xattr_set_ctxt *ctxt)
2681 {
2682         int ret = 0, credits, old_found;
2683
2684         if (!xi->value) {
2685                 /* Remove existing extended attribute */
2686                 if (!xis->not_found)
2687                         ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2688                 else if (!xbs->not_found)
2689                         ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2690         } else {
2691                 /* We always try to set extended attribute into inode first*/
2692                 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2693                 if (!ret && !xbs->not_found) {
2694                         /*
2695                          * If succeed and that extended attribute existing in
2696                          * external block, then we will remove it.
2697                          */
2698                         xi->value = NULL;
2699                         xi->value_len = 0;
2700
2701                         old_found = xis->not_found;
2702                         xis->not_found = -ENODATA;
2703                         ret = ocfs2_calc_xattr_set_need(inode,
2704                                                         di,
2705                                                         xi,
2706                                                         xis,
2707                                                         xbs,
2708                                                         NULL,
2709                                                         NULL,
2710                                                         &credits);
2711                         xis->not_found = old_found;
2712                         if (ret) {
2713                                 mlog_errno(ret);
2714                                 goto out;
2715                         }
2716
2717                         ret = ocfs2_extend_trans(ctxt->handle, credits +
2718                                         ctxt->handle->h_buffer_credits);
2719                         if (ret) {
2720                                 mlog_errno(ret);
2721                                 goto out;
2722                         }
2723                         ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2724                 } else if (ret == -ENOSPC) {
2725                         if (di->i_xattr_loc && !xbs->xattr_bh) {
2726                                 ret = ocfs2_xattr_block_find(inode,
2727                                                              xi->name_index,
2728                                                              xi->name, xbs);
2729                                 if (ret)
2730                                         goto out;
2731
2732                                 old_found = xis->not_found;
2733                                 xis->not_found = -ENODATA;
2734                                 ret = ocfs2_calc_xattr_set_need(inode,
2735                                                                 di,
2736                                                                 xi,
2737                                                                 xis,
2738                                                                 xbs,
2739                                                                 NULL,
2740                                                                 NULL,
2741                                                                 &credits);
2742                                 xis->not_found = old_found;
2743                                 if (ret) {
2744                                         mlog_errno(ret);
2745                                         goto out;
2746                                 }
2747
2748                                 ret = ocfs2_extend_trans(ctxt->handle, credits +
2749                                         ctxt->handle->h_buffer_credits);
2750                                 if (ret) {
2751                                         mlog_errno(ret);
2752                                         goto out;
2753                                 }
2754                         }
2755                         /*
2756                          * If no space in inode, we will set extended attribute
2757                          * into external block.
2758                          */
2759                         ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2760                         if (ret)
2761                                 goto out;
2762                         if (!xis->not_found) {
2763                                 /*
2764                                  * If succeed and that extended attribute
2765                                  * existing in inode, we will remove it.
2766                                  */
2767                                 xi->value = NULL;
2768                                 xi->value_len = 0;
2769                                 xbs->not_found = -ENODATA;
2770                                 ret = ocfs2_calc_xattr_set_need(inode,
2771                                                                 di,
2772                                                                 xi,
2773                                                                 xis,
2774                                                                 xbs,
2775                                                                 NULL,
2776                                                                 NULL,
2777                                                                 &credits);
2778                                 if (ret) {
2779                                         mlog_errno(ret);
2780                                         goto out;
2781                                 }
2782
2783                                 ret = ocfs2_extend_trans(ctxt->handle, credits +
2784                                                 ctxt->handle->h_buffer_credits);
2785                                 if (ret) {
2786                                         mlog_errno(ret);
2787                                         goto out;
2788                                 }
2789                                 ret = ocfs2_xattr_ibody_set(inode, xi,
2790                                                             xis, ctxt);
2791                         }
2792                 }
2793         }
2794
2795         if (!ret) {
2796                 /* Update inode ctime. */
2797                 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
2798                                               xis->inode_bh,
2799                                               OCFS2_JOURNAL_ACCESS_WRITE);
2800                 if (ret) {
2801                         mlog_errno(ret);
2802                         goto out;
2803                 }
2804
2805                 inode->i_ctime = CURRENT_TIME;
2806                 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
2807                 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
2808                 ocfs2_journal_dirty(ctxt->handle, xis->inode_bh);
2809         }
2810 out:
2811         return ret;
2812 }
2813
2814 /*
2815  * This function only called duing creating inode
2816  * for init security/acl xattrs of the new inode.
2817  * All transanction credits have been reserved in mknod.
2818  */
2819 int ocfs2_xattr_set_handle(handle_t *handle,
2820                            struct inode *inode,
2821                            struct buffer_head *di_bh,
2822                            int name_index,
2823                            const char *name,
2824                            const void *value,
2825                            size_t value_len,
2826                            int flags,
2827                            struct ocfs2_alloc_context *meta_ac,
2828                            struct ocfs2_alloc_context *data_ac)
2829 {
2830         struct ocfs2_dinode *di;
2831         int ret;
2832
2833         struct ocfs2_xattr_info xi = {
2834                 .name_index = name_index,
2835                 .name = name,
2836                 .value = value,
2837                 .value_len = value_len,
2838         };
2839
2840         struct ocfs2_xattr_search xis = {
2841                 .not_found = -ENODATA,
2842         };
2843
2844         struct ocfs2_xattr_search xbs = {
2845                 .not_found = -ENODATA,
2846         };
2847
2848         struct ocfs2_xattr_set_ctxt ctxt = {
2849                 .handle = handle,
2850                 .meta_ac = meta_ac,
2851                 .data_ac = data_ac,
2852         };
2853
2854         if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2855                 return -EOPNOTSUPP;
2856
2857         /*
2858          * In extreme situation, may need xattr bucket when
2859          * block size is too small. And we have already reserved
2860          * the credits for bucket in mknod.
2861          */
2862         if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) {
2863                 xbs.bucket = ocfs2_xattr_bucket_new(inode);
2864                 if (!xbs.bucket) {
2865                         mlog_errno(-ENOMEM);
2866                         return -ENOMEM;
2867                 }
2868         }
2869
2870         xis.inode_bh = xbs.inode_bh = di_bh;
2871         di = (struct ocfs2_dinode *)di_bh->b_data;
2872
2873         down_write(&OCFS2_I(inode)->ip_xattr_sem);
2874
2875         ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2876         if (ret)
2877                 goto cleanup;
2878         if (xis.not_found) {
2879                 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2880                 if (ret)
2881                         goto cleanup;
2882         }
2883
2884         ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
2885
2886 cleanup:
2887         up_write(&OCFS2_I(inode)->ip_xattr_sem);
2888         brelse(xbs.xattr_bh);
2889         ocfs2_xattr_bucket_free(xbs.bucket);
2890
2891         return ret;
2892 }
2893
2894 /*
2895  * ocfs2_xattr_set()
2896  *
2897  * Set, replace or remove an extended attribute for this inode.
2898  * value is NULL to remove an existing extended attribute, else either
2899  * create or replace an extended attribute.
2900  */
2901 int ocfs2_xattr_set(struct inode *inode,
2902                     int name_index,
2903                     const char *name,
2904                     const void *value,
2905                     size_t value_len,
2906                     int flags)
2907 {
2908         struct buffer_head *di_bh = NULL;
2909         struct ocfs2_dinode *di;
2910         int ret, credits, ref_meta = 0, ref_credits = 0;
2911         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2912         struct inode *tl_inode = osb->osb_tl_inode;
2913         struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
2914         struct ocfs2_refcount_tree *ref_tree = NULL;
2915
2916         struct ocfs2_xattr_info xi = {
2917                 .name_index = name_index,
2918                 .name = name,
2919                 .value = value,
2920                 .value_len = value_len,
2921         };
2922
2923         struct ocfs2_xattr_search xis = {
2924                 .not_found = -ENODATA,
2925         };
2926
2927         struct ocfs2_xattr_search xbs = {
2928                 .not_found = -ENODATA,
2929         };
2930
2931         if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2932                 return -EOPNOTSUPP;
2933
2934         /*
2935          * Only xbs will be used on indexed trees.  xis doesn't need a
2936          * bucket.
2937          */
2938         xbs.bucket = ocfs2_xattr_bucket_new(inode);
2939         if (!xbs.bucket) {
2940                 mlog_errno(-ENOMEM);
2941                 return -ENOMEM;
2942         }
2943
2944         ret = ocfs2_inode_lock(inode, &di_bh, 1);
2945         if (ret < 0) {
2946                 mlog_errno(ret);
2947                 goto cleanup_nolock;
2948         }
2949         xis.inode_bh = xbs.inode_bh = di_bh;
2950         di = (struct ocfs2_dinode *)di_bh->b_data;
2951
2952         down_write(&OCFS2_I(inode)->ip_xattr_sem);
2953         /*
2954          * Scan inode and external block to find the same name
2955          * extended attribute and collect search infomation.
2956          */
2957         ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2958         if (ret)
2959                 goto cleanup;
2960         if (xis.not_found) {
2961                 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2962                 if (ret)
2963                         goto cleanup;
2964         }
2965
2966         if (xis.not_found && xbs.not_found) {
2967                 ret = -ENODATA;
2968                 if (flags & XATTR_REPLACE)
2969                         goto cleanup;
2970                 ret = 0;
2971                 if (!value)
2972                         goto cleanup;
2973         } else {
2974                 ret = -EEXIST;
2975                 if (flags & XATTR_CREATE)
2976                         goto cleanup;
2977         }
2978
2979         /* Check whether the value is refcounted and do some prepartion. */
2980         if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL &&
2981             (!xis.not_found || !xbs.not_found)) {
2982                 ret = ocfs2_prepare_refcount_xattr(inode, di, &xi,
2983                                                    &xis, &xbs, &ref_tree,
2984                                                    &ref_meta, &ref_credits);
2985                 if (ret) {
2986                         mlog_errno(ret);
2987                         goto cleanup;
2988                 }
2989         }
2990
2991         mutex_lock(&tl_inode->i_mutex);
2992
2993         if (ocfs2_truncate_log_needs_flush(osb)) {
2994                 ret = __ocfs2_flush_truncate_log(osb);
2995                 if (ret < 0) {
2996                         mutex_unlock(&tl_inode->i_mutex);
2997                         mlog_errno(ret);
2998                         goto cleanup;
2999                 }
3000         }
3001         mutex_unlock(&tl_inode->i_mutex);
3002
3003         ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
3004                                         &xbs, &ctxt, ref_meta, &credits);
3005         if (ret) {
3006                 mlog_errno(ret);
3007                 goto cleanup;
3008         }
3009
3010         /* we need to update inode's ctime field, so add credit for it. */
3011         credits += OCFS2_INODE_UPDATE_CREDITS;
3012         ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
3013         if (IS_ERR(ctxt.handle)) {
3014                 ret = PTR_ERR(ctxt.handle);
3015                 mlog_errno(ret);
3016                 goto cleanup;
3017         }
3018
3019         ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3020
3021         ocfs2_commit_trans(osb, ctxt.handle);
3022
3023         if (ctxt.data_ac)
3024                 ocfs2_free_alloc_context(ctxt.data_ac);
3025         if (ctxt.meta_ac)
3026                 ocfs2_free_alloc_context(ctxt.meta_ac);
3027         if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
3028                 ocfs2_schedule_truncate_log_flush(osb, 1);
3029         ocfs2_run_deallocs(osb, &ctxt.dealloc);
3030
3031 cleanup:
3032         if (ref_tree)
3033                 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
3034         up_write(&OCFS2_I(inode)->ip_xattr_sem);
3035         if (!value && !ret) {
3036                 ret = ocfs2_try_remove_refcount_tree(inode, di_bh);
3037                 if (ret)
3038                         mlog_errno(ret);
3039         }
3040         ocfs2_inode_unlock(inode, 1);
3041 cleanup_nolock:
3042         brelse(di_bh);
3043         brelse(xbs.xattr_bh);
3044         ocfs2_xattr_bucket_free(xbs.bucket);
3045
3046         return ret;
3047 }
3048
3049 /*
3050  * Find the xattr extent rec which may contains name_hash.
3051  * e_cpos will be the first name hash of the xattr rec.
3052  * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
3053  */
3054 static int ocfs2_xattr_get_rec(struct inode *inode,
3055                                u32 name_hash,
3056                                u64 *p_blkno,
3057                                u32 *e_cpos,
3058                                u32 *num_clusters,
3059                                struct ocfs2_extent_list *el)
3060 {
3061         int ret = 0, i;