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