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