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