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