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