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