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