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