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