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