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