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