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