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