]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - fs/ocfs2/xattr.c
ocfs2: Allocation in ocfs2_xa_prepare_entry(), values in ocfs2_xa_store_value()
[mirror_ubuntu-zesty-kernel.git] / fs / ocfs2 / xattr.c
CommitLineData
f56654c4
TM
1/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * xattr.c
5 *
c3cb6827 6 * Copyright (C) 2004, 2008 Oracle. All rights reserved.
f56654c4 7 *
cf1d6c76 8 * CREDITS:
c3cb6827
TY
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>
cf1d6c76 11 *
f56654c4
TM
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public
c3cb6827 14 * License version 2 as published by the Free Software Foundation.
f56654c4
TM
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.
f56654c4
TM
20 */
21
cf1d6c76
TY
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>
01225596 34#include <linux/sort.h>
99219aea
MF
35#include <linux/init.h>
36#include <linux/module.h>
37#include <linux/string.h>
923f7f31 38#include <linux/security.h>
cf1d6c76 39
f56654c4
TM
40#define MLOG_MASK_PREFIX ML_XATTR
41#include <cluster/masklog.h>
42
43#include "ocfs2.h"
44#include "alloc.h"
d6b32bbb 45#include "blockcheck.h"
f56654c4
TM
46#include "dlmglue.h"
47#include "file.h"
cf1d6c76
TY
48#include "symlink.h"
49#include "sysfile.h"
f56654c4
TM
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"
0c044f0b 56#include "super.h"
cf1d6c76 57#include "xattr.h"
492a8a33 58#include "refcounttree.h"
0fe9b66c 59#include "acl.h"
cf1d6c76
TY
60
61struct ocfs2_xattr_def_value_root {
62 struct ocfs2_xattr_value_root xv;
63 struct ocfs2_extent_rec er;
64};
65
0c044f0b 66struct ocfs2_xattr_bucket {
ba937127
JB
67 /* The inode these xattrs are associated with */
68 struct inode *bu_inode;
69
70 /* The actual buffers that make up the bucket */
4ac6032d 71 struct buffer_head *bu_bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
ba937127
JB
72
73 /* How many blocks make up one bucket for this filesystem */
74 int bu_blocks;
0c044f0b
TM
75};
76
78f30c31 77struct ocfs2_xattr_set_ctxt {
85db90e7 78 handle_t *handle;
78f30c31
TM
79 struct ocfs2_alloc_context *meta_ac;
80 struct ocfs2_alloc_context *data_ac;
81 struct ocfs2_cached_dealloc_ctxt dealloc;
82};
83
cf1d6c76
TY
84#define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
85#define OCFS2_XATTR_INLINE_SIZE 80
4442f518 86#define OCFS2_XATTR_HEADER_GAP 4
534eaddd
TY
87#define OCFS2_XATTR_FREE_IN_IBODY (OCFS2_MIN_XATTR_INLINE_SIZE \
88 - sizeof(struct ocfs2_xattr_header) \
4442f518 89 - OCFS2_XATTR_HEADER_GAP)
89c38bd0
TY
90#define OCFS2_XATTR_FREE_IN_BLOCK(ptr) ((ptr)->i_sb->s_blocksize \
91 - sizeof(struct ocfs2_xattr_block) \
92 - sizeof(struct ocfs2_xattr_header) \
4442f518 93 - OCFS2_XATTR_HEADER_GAP)
cf1d6c76
TY
94
95static struct ocfs2_xattr_def_value_root def_xv = {
96 .xv.xr_list.l_count = cpu_to_le16(1),
97};
98
99struct xattr_handler *ocfs2_xattr_handlers[] = {
100 &ocfs2_xattr_user_handler,
929fb014
TY
101 &ocfs2_xattr_acl_access_handler,
102 &ocfs2_xattr_acl_default_handler,
cf1d6c76 103 &ocfs2_xattr_trusted_handler,
923f7f31 104 &ocfs2_xattr_security_handler,
cf1d6c76
TY
105 NULL
106};
107
c988fd04 108static struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
cf1d6c76 109 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
929fb014
TY
110 [OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS]
111 = &ocfs2_xattr_acl_access_handler,
112 [OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT]
113 = &ocfs2_xattr_acl_default_handler,
cf1d6c76 114 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
923f7f31 115 [OCFS2_XATTR_INDEX_SECURITY] = &ocfs2_xattr_security_handler,
cf1d6c76
TY
116};
117
118struct ocfs2_xattr_info {
6b240ff6
JB
119 int xi_name_index;
120 const char *xi_name;
18853b95 121 int xi_name_len;
6b240ff6
JB
122 const void *xi_value;
123 size_t xi_value_len;
cf1d6c76
TY
124};
125
126struct ocfs2_xattr_search {
127 struct buffer_head *inode_bh;
128 /*
129 * xattr_bh point to the block buffer head which has extended attribute
130 * when extended attribute in inode, xattr_bh is equal to inode_bh.
131 */
132 struct buffer_head *xattr_bh;
133 struct ocfs2_xattr_header *header;
ba937127 134 struct ocfs2_xattr_bucket *bucket;
cf1d6c76
TY
135 void *base;
136 void *end;
137 struct ocfs2_xattr_entry *here;
138 int not_found;
139};
140
11179f2c
JB
141/* Operations on struct ocfs2_xa_entry */
142struct ocfs2_xa_loc;
143struct ocfs2_xa_loc_operations {
cf2bc809
JB
144 /*
145 * Journal functions
146 */
147 int (*xlo_journal_access)(handle_t *handle, struct ocfs2_xa_loc *loc,
148 int type);
149 void (*xlo_journal_dirty)(handle_t *handle, struct ocfs2_xa_loc *loc);
150
11179f2c
JB
151 /*
152 * Return a pointer to the appropriate buffer in loc->xl_storage
153 * at the given offset from loc->xl_header.
154 */
155 void *(*xlo_offset_pointer)(struct ocfs2_xa_loc *loc, int offset);
156
69a3e539
JB
157 /* Can we reuse the existing entry for the new value? */
158 int (*xlo_can_reuse)(struct ocfs2_xa_loc *loc,
159 struct ocfs2_xattr_info *xi);
160
161 /* How much space is needed for the new value? */
162 int (*xlo_check_space)(struct ocfs2_xa_loc *loc,
163 struct ocfs2_xattr_info *xi);
164
165 /*
166 * Return the offset of the first name+value pair. This is
167 * the start of our downward-filling free space.
168 */
169 int (*xlo_get_free_start)(struct ocfs2_xa_loc *loc);
170
11179f2c
JB
171 /*
172 * Remove the name+value at this location. Do whatever is
173 * appropriate with the remaining name+value pairs.
174 */
175 void (*xlo_wipe_namevalue)(struct ocfs2_xa_loc *loc);
69a3e539
JB
176
177 /* Fill xl_entry with a new entry */
178 void (*xlo_add_entry)(struct ocfs2_xa_loc *loc, u32 name_hash);
179
180 /* Add name+value storage to an entry */
181 void (*xlo_add_namevalue)(struct ocfs2_xa_loc *loc, int size);
3fc12afa
JB
182
183 /*
184 * Initialize the value buf's access and bh fields for this entry.
185 * ocfs2_xa_fill_value_buf() will handle the xv pointer.
186 */
187 void (*xlo_fill_value_buf)(struct ocfs2_xa_loc *loc,
188 struct ocfs2_xattr_value_buf *vb);
11179f2c
JB
189};
190
191/*
192 * Describes an xattr entry location. This is a memory structure
193 * tracking the on-disk structure.
194 */
195struct ocfs2_xa_loc {
cf2bc809
JB
196 /* This xattr belongs to this inode */
197 struct inode *xl_inode;
198
11179f2c
JB
199 /* The ocfs2_xattr_header inside the on-disk storage. Not NULL. */
200 struct ocfs2_xattr_header *xl_header;
201
202 /* Bytes from xl_header to the end of the storage */
203 int xl_size;
204
205 /*
206 * The ocfs2_xattr_entry this location describes. If this is
207 * NULL, this location describes the on-disk structure where it
208 * would have been.
209 */
210 struct ocfs2_xattr_entry *xl_entry;
211
212 /*
213 * Internal housekeeping
214 */
215
216 /* Buffer(s) containing this entry */
217 void *xl_storage;
218
219 /* Operations on the storage backing this location */
220 const struct ocfs2_xa_loc_operations *xl_ops;
221};
222
199799a3
JB
223/*
224 * Convenience functions to calculate how much space is needed for a
225 * given name+value pair
226 */
227static int namevalue_size(int name_len, uint64_t value_len)
228{
229 if (value_len > OCFS2_XATTR_INLINE_SIZE)
230 return OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
231 else
232 return OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
233}
234
235static int namevalue_size_xi(struct ocfs2_xattr_info *xi)
236{
237 return namevalue_size(xi->xi_name_len, xi->xi_value_len);
238}
239
240static int namevalue_size_xe(struct ocfs2_xattr_entry *xe)
241{
242 u64 value_len = le64_to_cpu(xe->xe_value_size);
243
244 BUG_ON((value_len > OCFS2_XATTR_INLINE_SIZE) &&
245 ocfs2_xattr_is_local(xe));
246 return namevalue_size(xe->xe_name_len, value_len);
247}
248
249
fd68a894 250static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
589dc260
TM
251 struct ocfs2_xattr_header *xh,
252 int index,
253 int *block_off,
254 int *new_offset);
255
54f443f4
JB
256static int ocfs2_xattr_block_find(struct inode *inode,
257 int name_index,
258 const char *name,
259 struct ocfs2_xattr_search *xs);
589dc260
TM
260static int ocfs2_xattr_index_block_find(struct inode *inode,
261 struct buffer_head *root_bh,
262 int name_index,
263 const char *name,
264 struct ocfs2_xattr_search *xs);
265
0c044f0b 266static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
47bca495 267 struct buffer_head *blk_bh,
0c044f0b
TM
268 char *buffer,
269 size_t buffer_size);
270
01225596 271static int ocfs2_xattr_create_index_block(struct inode *inode,
78f30c31
TM
272 struct ocfs2_xattr_search *xs,
273 struct ocfs2_xattr_set_ctxt *ctxt);
01225596
TM
274
275static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
276 struct ocfs2_xattr_info *xi,
78f30c31
TM
277 struct ocfs2_xattr_search *xs,
278 struct ocfs2_xattr_set_ctxt *ctxt);
01225596 279
47bca495
TM
280typedef int (xattr_tree_rec_func)(struct inode *inode,
281 struct buffer_head *root_bh,
282 u64 blkno, u32 cpos, u32 len, void *para);
283static int ocfs2_iterate_xattr_index_block(struct inode *inode,
284 struct buffer_head *root_bh,
285 xattr_tree_rec_func *rec_func,
286 void *para);
287static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
288 struct ocfs2_xattr_bucket *bucket,
289 void *para);
290static int ocfs2_rm_xattr_cluster(struct inode *inode,
291 struct buffer_head *root_bh,
292 u64 blkno,
293 u32 cpos,
294 u32 len,
295 void *para);
296
c58b6032
JB
297static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
298 u64 src_blk, u64 last_blk, u64 to_blk,
299 unsigned int start_bucket,
300 u32 *first_hash);
492a8a33
TM
301static int ocfs2_prepare_refcount_xattr(struct inode *inode,
302 struct ocfs2_dinode *di,
303 struct ocfs2_xattr_info *xi,
304 struct ocfs2_xattr_search *xis,
305 struct ocfs2_xattr_search *xbs,
306 struct ocfs2_refcount_tree **ref_tree,
307 int *meta_need,
308 int *credits);
ce9c5a54
TM
309static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
310 struct ocfs2_xattr_bucket *bucket,
311 int offset,
312 struct ocfs2_xattr_value_root **xv,
313 struct buffer_head **bh);
a3944256 314
0030e001
TY
315static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
316{
317 return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
318}
319
320static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
321{
322 return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
323}
324
325static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb)
326{
327 u16 len = sb->s_blocksize -
328 offsetof(struct ocfs2_xattr_header, xh_entries);
329
330 return len / sizeof(struct ocfs2_xattr_entry);
331}
332
9c7759aa 333#define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
51def39f 334#define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
3e632946 335#define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
9c7759aa 336
ba937127 337static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
6dde41d9 338{
ba937127
JB
339 struct ocfs2_xattr_bucket *bucket;
340 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
6dde41d9 341
ba937127
JB
342 BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
343
344 bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
345 if (bucket) {
346 bucket->bu_inode = inode;
347 bucket->bu_blocks = blks;
348 }
349
350 return bucket;
351}
352
353static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
354{
355 int i;
356
357 for (i = 0; i < bucket->bu_blocks; i++) {
6dde41d9
JB
358 brelse(bucket->bu_bhs[i]);
359 bucket->bu_bhs[i] = NULL;
360 }
361}
362
ba937127
JB
363static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
364{
365 if (bucket) {
366 ocfs2_xattr_bucket_relse(bucket);
367 bucket->bu_inode = NULL;
368 kfree(bucket);
369 }
370}
371
784b816a
JB
372/*
373 * A bucket that has never been written to disk doesn't need to be
374 * read. We just need the buffer_heads. Don't call this for
375 * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
376 * them fully.
377 */
ba937127 378static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
784b816a
JB
379 u64 xb_blkno)
380{
381 int i, rc = 0;
784b816a 382
ba937127
JB
383 for (i = 0; i < bucket->bu_blocks; i++) {
384 bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
385 xb_blkno + i);
784b816a
JB
386 if (!bucket->bu_bhs[i]) {
387 rc = -EIO;
388 mlog_errno(rc);
389 break;
390 }
391
8cb471e8 392 if (!ocfs2_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
757055ad 393 bucket->bu_bhs[i]))
8cb471e8 394 ocfs2_set_new_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
757055ad 395 bucket->bu_bhs[i]);
784b816a
JB
396 }
397
398 if (rc)
ba937127 399 ocfs2_xattr_bucket_relse(bucket);
784b816a
JB
400 return rc;
401}
402
403/* Read the xattr bucket at xb_blkno */
ba937127 404static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
784b816a
JB
405 u64 xb_blkno)
406{
ba937127 407 int rc;
784b816a 408
8cb471e8 409 rc = ocfs2_read_blocks(INODE_CACHE(bucket->bu_inode), xb_blkno,
970e4936
JB
410 bucket->bu_blocks, bucket->bu_bhs, 0,
411 NULL);
4d0e214e 412 if (!rc) {
c8b9cf9a 413 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
4d0e214e
JB
414 rc = ocfs2_validate_meta_ecc_bhs(bucket->bu_inode->i_sb,
415 bucket->bu_bhs,
416 bucket->bu_blocks,
417 &bucket_xh(bucket)->xh_check);
c8b9cf9a 418 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
4d0e214e
JB
419 if (rc)
420 mlog_errno(rc);
421 }
422
784b816a 423 if (rc)
ba937127 424 ocfs2_xattr_bucket_relse(bucket);
784b816a
JB
425 return rc;
426}
427
1224be02 428static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
1224be02
JB
429 struct ocfs2_xattr_bucket *bucket,
430 int type)
431{
432 int i, rc = 0;
1224be02 433
ba937127 434 for (i = 0; i < bucket->bu_blocks; i++) {
0cf2f763
JB
435 rc = ocfs2_journal_access(handle,
436 INODE_CACHE(bucket->bu_inode),
1224be02
JB
437 bucket->bu_bhs[i], type);
438 if (rc) {
439 mlog_errno(rc);
440 break;
441 }
442 }
443
444 return rc;
445}
446
447static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
1224be02
JB
448 struct ocfs2_xattr_bucket *bucket)
449{
ba937127 450 int i;
1224be02 451
c8b9cf9a 452 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
4d0e214e
JB
453 ocfs2_compute_meta_ecc_bhs(bucket->bu_inode->i_sb,
454 bucket->bu_bhs, bucket->bu_blocks,
455 &bucket_xh(bucket)->xh_check);
c8b9cf9a 456 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
4d0e214e 457
ba937127 458 for (i = 0; i < bucket->bu_blocks; i++)
1224be02
JB
459 ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
460}
461
ba937127 462static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
4980c6da
JB
463 struct ocfs2_xattr_bucket *src)
464{
465 int i;
ba937127
JB
466 int blocksize = src->bu_inode->i_sb->s_blocksize;
467
468 BUG_ON(dest->bu_blocks != src->bu_blocks);
469 BUG_ON(dest->bu_inode != src->bu_inode);
4980c6da 470
ba937127 471 for (i = 0; i < src->bu_blocks; i++) {
4980c6da
JB
472 memcpy(bucket_block(dest, i), bucket_block(src, i),
473 blocksize);
474 }
475}
1224be02 476
4ae1d69b
JB
477static int ocfs2_validate_xattr_block(struct super_block *sb,
478 struct buffer_head *bh)
479{
d6b32bbb 480 int rc;
4ae1d69b
JB
481 struct ocfs2_xattr_block *xb =
482 (struct ocfs2_xattr_block *)bh->b_data;
483
484 mlog(0, "Validating xattr block %llu\n",
485 (unsigned long long)bh->b_blocknr);
486
d6b32bbb
JB
487 BUG_ON(!buffer_uptodate(bh));
488
489 /*
490 * If the ecc fails, we return the error but otherwise
491 * leave the filesystem running. We know any error is
492 * local to this block.
493 */
494 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &xb->xb_check);
495 if (rc)
496 return rc;
497
498 /*
499 * Errors after here are fatal
500 */
501
4ae1d69b
JB
502 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
503 ocfs2_error(sb,
504 "Extended attribute block #%llu has bad "
505 "signature %.*s",
506 (unsigned long long)bh->b_blocknr, 7,
507 xb->xb_signature);
508 return -EINVAL;
509 }
510
511 if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) {
512 ocfs2_error(sb,
513 "Extended attribute block #%llu has an "
514 "invalid xb_blkno of %llu",
515 (unsigned long long)bh->b_blocknr,
516 (unsigned long long)le64_to_cpu(xb->xb_blkno));
517 return -EINVAL;
518 }
519
520 if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) {
521 ocfs2_error(sb,
522 "Extended attribute block #%llu has an invalid "
523 "xb_fs_generation of #%u",
524 (unsigned long long)bh->b_blocknr,
525 le32_to_cpu(xb->xb_fs_generation));
526 return -EINVAL;
527 }
528
529 return 0;
530}
531
532static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno,
533 struct buffer_head **bh)
534{
535 int rc;
536 struct buffer_head *tmp = *bh;
537
8cb471e8 538 rc = ocfs2_read_block(INODE_CACHE(inode), xb_blkno, &tmp,
970e4936 539 ocfs2_validate_xattr_block);
4ae1d69b
JB
540
541 /* If ocfs2_read_block() got us a new bh, pass it up. */
542 if (!rc && !*bh)
543 *bh = tmp;
544
545 return rc;
546}
547
936b8834 548static inline const char *ocfs2_xattr_prefix(int name_index)
cf1d6c76
TY
549{
550 struct xattr_handler *handler = NULL;
551
552 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
553 handler = ocfs2_xattr_handler_map[name_index];
554
936b8834 555 return handler ? handler->prefix : NULL;
cf1d6c76
TY
556}
557
40daa16a 558static u32 ocfs2_xattr_name_hash(struct inode *inode,
2057e5c6 559 const char *name,
40daa16a 560 int name_len)
cf1d6c76
TY
561{
562 /* Get hash value of uuid from super block */
563 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
564 int i;
565
cf1d6c76
TY
566 /* hash extended attribute name */
567 for (i = 0; i < name_len; i++) {
568 hash = (hash << OCFS2_HASH_SHIFT) ^
569 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
570 *name++;
571 }
572
573 return hash;
574}
575
534eaddd
TY
576static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
577{
199799a3
JB
578 return namevalue_size(name_len, value_len) +
579 sizeof(struct ocfs2_xattr_entry);
580}
534eaddd 581
199799a3
JB
582static int ocfs2_xi_entry_usage(struct ocfs2_xattr_info *xi)
583{
584 return namevalue_size_xi(xi) +
585 sizeof(struct ocfs2_xattr_entry);
586}
534eaddd 587
199799a3
JB
588static int ocfs2_xe_entry_usage(struct ocfs2_xattr_entry *xe)
589{
590 return namevalue_size_xe(xe) +
591 sizeof(struct ocfs2_xattr_entry);
534eaddd
TY
592}
593
594int ocfs2_calc_security_init(struct inode *dir,
595 struct ocfs2_security_xattr_info *si,
596 int *want_clusters,
597 int *xattr_credits,
598 struct ocfs2_alloc_context **xattr_ac)
599{
600 int ret = 0;
601 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
602 int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
603 si->value_len);
604
605 /*
606 * The max space of security xattr taken inline is
607 * 256(name) + 80(value) + 16(entry) = 352 bytes,
608 * So reserve one metadata block for it is ok.
609 */
610 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
611 s_size > OCFS2_XATTR_FREE_IN_IBODY) {
612 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
613 if (ret) {
614 mlog_errno(ret);
615 return ret;
616 }
617 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
618 }
619
620 /* reserve clusters for xattr value which will be set in B tree*/
0e445b6f
TY
621 if (si->value_len > OCFS2_XATTR_INLINE_SIZE) {
622 int new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
623 si->value_len);
624
625 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
626 new_clusters);
627 *want_clusters += new_clusters;
628 }
534eaddd
TY
629 return ret;
630}
631
89c38bd0
TY
632int ocfs2_calc_xattr_init(struct inode *dir,
633 struct buffer_head *dir_bh,
634 int mode,
635 struct ocfs2_security_xattr_info *si,
636 int *want_clusters,
637 int *xattr_credits,
9b7895ef 638 int *want_meta)
89c38bd0
TY
639{
640 int ret = 0;
641 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
0e445b6f 642 int s_size = 0, a_size = 0, acl_len = 0, new_clusters;
89c38bd0
TY
643
644 if (si->enable)
645 s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
646 si->value_len);
647
648 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
649 acl_len = ocfs2_xattr_get_nolock(dir, dir_bh,
650 OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
651 "", NULL, 0);
652 if (acl_len > 0) {
653 a_size = ocfs2_xattr_entry_real_size(0, acl_len);
654 if (S_ISDIR(mode))
655 a_size <<= 1;
656 } else if (acl_len != 0 && acl_len != -ENODATA) {
657 mlog_errno(ret);
658 return ret;
659 }
660 }
661
662 if (!(s_size + a_size))
663 return ret;
664
665 /*
666 * The max space of security xattr taken inline is
667 * 256(name) + 80(value) + 16(entry) = 352 bytes,
668 * The max space of acl xattr taken inline is
669 * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
670 * when blocksize = 512, may reserve one more cluser for
671 * xattr bucket, otherwise reserve one metadata block
672 * for them is ok.
6c9fd1dc
TY
673 * If this is a new directory with inline data,
674 * we choose to reserve the entire inline area for
675 * directory contents and force an external xattr block.
89c38bd0
TY
676 */
677 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
6c9fd1dc 678 (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) ||
89c38bd0 679 (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) {
9b7895ef 680 *want_meta = *want_meta + 1;
89c38bd0
TY
681 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
682 }
683
684 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
685 (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
686 *want_clusters += 1;
687 *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
688 }
689
0e445b6f
TY
690 /*
691 * reserve credits and clusters for xattrs which has large value
692 * and have to be set outside
693 */
694 if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE) {
695 new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
696 si->value_len);
697 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
698 new_clusters);
699 *want_clusters += new_clusters;
700 }
89c38bd0
TY
701 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
702 acl_len > OCFS2_XATTR_INLINE_SIZE) {
0e445b6f
TY
703 /* for directory, it has DEFAULT and ACCESS two types of acls */
704 new_clusters = (S_ISDIR(mode) ? 2 : 1) *
705 ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
706 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
707 new_clusters);
708 *want_clusters += new_clusters;
89c38bd0
TY
709 }
710
711 return ret;
712}
713
f56654c4
TM
714static int ocfs2_xattr_extend_allocation(struct inode *inode,
715 u32 clusters_to_add,
19b801f4 716 struct ocfs2_xattr_value_buf *vb,
78f30c31 717 struct ocfs2_xattr_set_ctxt *ctxt)
f56654c4
TM
718{
719 int status = 0;
85db90e7 720 handle_t *handle = ctxt->handle;
f56654c4 721 enum ocfs2_alloc_restarted why;
19b801f4 722 u32 prev_clusters, logical_start = le32_to_cpu(vb->vb_xv->xr_clusters);
f99b9b7c 723 struct ocfs2_extent_tree et;
f56654c4
TM
724
725 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
726
5e404e9e 727 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
f99b9b7c 728
0cf2f763 729 status = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
2a50a743 730 OCFS2_JOURNAL_ACCESS_WRITE);
f56654c4
TM
731 if (status < 0) {
732 mlog_errno(status);
733 goto leave;
734 }
735
19b801f4 736 prev_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
cbee7e1a
JB
737 status = ocfs2_add_clusters_in_btree(handle,
738 &et,
f56654c4
TM
739 &logical_start,
740 clusters_to_add,
741 0,
78f30c31
TM
742 ctxt->data_ac,
743 ctxt->meta_ac,
f99b9b7c 744 &why);
85db90e7
TM
745 if (status < 0) {
746 mlog_errno(status);
f56654c4
TM
747 goto leave;
748 }
749
19b801f4 750 status = ocfs2_journal_dirty(handle, vb->vb_bh);
f56654c4
TM
751 if (status < 0) {
752 mlog_errno(status);
753 goto leave;
754 }
755
19b801f4 756 clusters_to_add -= le32_to_cpu(vb->vb_xv->xr_clusters) - prev_clusters;
f56654c4 757
85db90e7
TM
758 /*
759 * We should have already allocated enough space before the transaction,
760 * so no need to restart.
761 */
762 BUG_ON(why != RESTART_NONE || clusters_to_add);
f56654c4
TM
763
764leave:
f56654c4
TM
765
766 return status;
767}
768
769static int __ocfs2_remove_xattr_range(struct inode *inode,
d72cc72d 770 struct ocfs2_xattr_value_buf *vb,
f56654c4 771 u32 cpos, u32 phys_cpos, u32 len,
492a8a33 772 unsigned int ext_flags,
78f30c31 773 struct ocfs2_xattr_set_ctxt *ctxt)
f56654c4
TM
774{
775 int ret;
776 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
85db90e7 777 handle_t *handle = ctxt->handle;
f99b9b7c
JB
778 struct ocfs2_extent_tree et;
779
5e404e9e 780 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
f56654c4 781
0cf2f763 782 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
d72cc72d 783 OCFS2_JOURNAL_ACCESS_WRITE);
f56654c4
TM
784 if (ret) {
785 mlog_errno(ret);
85db90e7 786 goto out;
f56654c4
TM
787 }
788
dbdcf6a4 789 ret = ocfs2_remove_extent(handle, &et, cpos, len, ctxt->meta_ac,
78f30c31 790 &ctxt->dealloc);
f56654c4
TM
791 if (ret) {
792 mlog_errno(ret);
85db90e7 793 goto out;
f56654c4
TM
794 }
795
d72cc72d 796 le32_add_cpu(&vb->vb_xv->xr_clusters, -len);
f56654c4 797
d72cc72d 798 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
f56654c4
TM
799 if (ret) {
800 mlog_errno(ret);
85db90e7 801 goto out;
f56654c4
TM
802 }
803
492a8a33
TM
804 if (ext_flags & OCFS2_EXT_REFCOUNTED)
805 ret = ocfs2_decrease_refcount(inode, handle,
806 ocfs2_blocks_to_clusters(inode->i_sb,
807 phys_blkno),
808 len, ctxt->meta_ac, &ctxt->dealloc, 1);
809 else
810 ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc,
811 phys_blkno, len);
f56654c4
TM
812 if (ret)
813 mlog_errno(ret);
814
f56654c4 815out:
f56654c4
TM
816 return ret;
817}
818
819static int ocfs2_xattr_shrink_size(struct inode *inode,
820 u32 old_clusters,
821 u32 new_clusters,
19b801f4 822 struct ocfs2_xattr_value_buf *vb,
78f30c31 823 struct ocfs2_xattr_set_ctxt *ctxt)
f56654c4
TM
824{
825 int ret = 0;
492a8a33 826 unsigned int ext_flags;
f56654c4
TM
827 u32 trunc_len, cpos, phys_cpos, alloc_size;
828 u64 block;
f56654c4
TM
829
830 if (old_clusters <= new_clusters)
831 return 0;
832
833 cpos = new_clusters;
834 trunc_len = old_clusters - new_clusters;
835 while (trunc_len) {
836 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
d72cc72d 837 &alloc_size,
492a8a33 838 &vb->vb_xv->xr_list, &ext_flags);
f56654c4
TM
839 if (ret) {
840 mlog_errno(ret);
841 goto out;
842 }
843
844 if (alloc_size > trunc_len)
845 alloc_size = trunc_len;
846
19b801f4 847 ret = __ocfs2_remove_xattr_range(inode, vb, cpos,
f56654c4 848 phys_cpos, alloc_size,
492a8a33 849 ext_flags, ctxt);
f56654c4
TM
850 if (ret) {
851 mlog_errno(ret);
852 goto out;
853 }
854
855 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
8cb471e8
JB
856 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode),
857 block, alloc_size);
f56654c4
TM
858 cpos += alloc_size;
859 trunc_len -= alloc_size;
860 }
861
862out:
f56654c4
TM
863 return ret;
864}
865
866static int ocfs2_xattr_value_truncate(struct inode *inode,
b3e5d379 867 struct ocfs2_xattr_value_buf *vb,
78f30c31
TM
868 int len,
869 struct ocfs2_xattr_set_ctxt *ctxt)
f56654c4
TM
870{
871 int ret;
872 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
b3e5d379 873 u32 old_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
f56654c4
TM
874
875 if (new_clusters == old_clusters)
876 return 0;
877
878 if (new_clusters > old_clusters)
879 ret = ocfs2_xattr_extend_allocation(inode,
880 new_clusters - old_clusters,
b3e5d379 881 vb, ctxt);
f56654c4
TM
882 else
883 ret = ocfs2_xattr_shrink_size(inode,
884 old_clusters, new_clusters,
b3e5d379 885 vb, ctxt);
f56654c4
TM
886
887 return ret;
888}
cf1d6c76 889
936b8834
TM
890static int ocfs2_xattr_list_entry(char *buffer, size_t size,
891 size_t *result, const char *prefix,
892 const char *name, int name_len)
893{
894 char *p = buffer + *result;
895 int prefix_len = strlen(prefix);
896 int total_len = prefix_len + name_len + 1;
897
898 *result += total_len;
899
900 /* we are just looking for how big our buffer needs to be */
901 if (!size)
902 return 0;
903
904 if (*result > size)
905 return -ERANGE;
906
907 memcpy(p, prefix, prefix_len);
908 memcpy(p + prefix_len, name, name_len);
909 p[prefix_len + name_len] = '\0';
910
911 return 0;
912}
913
cf1d6c76
TY
914static int ocfs2_xattr_list_entries(struct inode *inode,
915 struct ocfs2_xattr_header *header,
916 char *buffer, size_t buffer_size)
917{
936b8834
TM
918 size_t result = 0;
919 int i, type, ret;
920 const char *prefix, *name;
cf1d6c76
TY
921
922 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
923 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
936b8834
TM
924 type = ocfs2_xattr_get_type(entry);
925 prefix = ocfs2_xattr_prefix(type);
926
927 if (prefix) {
928 name = (const char *)header +
929 le16_to_cpu(entry->xe_name_offset);
930
931 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
932 &result, prefix, name,
933 entry->xe_name_len);
934 if (ret)
935 return ret;
cf1d6c76
TY
936 }
937 }
938
936b8834 939 return result;
cf1d6c76
TY
940}
941
8b2c0dba
TM
942int ocfs2_has_inline_xattr_value_outside(struct inode *inode,
943 struct ocfs2_dinode *di)
944{
945 struct ocfs2_xattr_header *xh;
946 int i;
947
948 xh = (struct ocfs2_xattr_header *)
949 ((void *)di + inode->i_sb->s_blocksize -
950 le16_to_cpu(di->i_xattr_inline_size));
951
952 for (i = 0; i < le16_to_cpu(xh->xh_count); i++)
953 if (!ocfs2_xattr_is_local(&xh->xh_entries[i]))
954 return 1;
955
956 return 0;
957}
958
cf1d6c76
TY
959static int ocfs2_xattr_ibody_list(struct inode *inode,
960 struct ocfs2_dinode *di,
961 char *buffer,
962 size_t buffer_size)
963{
964 struct ocfs2_xattr_header *header = NULL;
965 struct ocfs2_inode_info *oi = OCFS2_I(inode);
966 int ret = 0;
967
968 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
969 return ret;
970
971 header = (struct ocfs2_xattr_header *)
972 ((void *)di + inode->i_sb->s_blocksize -
973 le16_to_cpu(di->i_xattr_inline_size));
974
975 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
976
977 return ret;
978}
979
980static int ocfs2_xattr_block_list(struct inode *inode,
981 struct ocfs2_dinode *di,
982 char *buffer,
983 size_t buffer_size)
984{
985 struct buffer_head *blk_bh = NULL;
0c044f0b 986 struct ocfs2_xattr_block *xb;
cf1d6c76
TY
987 int ret = 0;
988
989 if (!di->i_xattr_loc)
990 return ret;
991
4ae1d69b
JB
992 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
993 &blk_bh);
cf1d6c76
TY
994 if (ret < 0) {
995 mlog_errno(ret);
996 return ret;
997 }
cf1d6c76 998
0c044f0b 999 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
0c044f0b
TM
1000 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1001 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
1002 ret = ocfs2_xattr_list_entries(inode, header,
1003 buffer, buffer_size);
47bca495
TM
1004 } else
1005 ret = ocfs2_xattr_tree_list_index_block(inode, blk_bh,
0c044f0b 1006 buffer, buffer_size);
4ae1d69b 1007
cf1d6c76
TY
1008 brelse(blk_bh);
1009
1010 return ret;
1011}
1012
1013ssize_t ocfs2_listxattr(struct dentry *dentry,
1014 char *buffer,
1015 size_t size)
1016{
1017 int ret = 0, i_ret = 0, b_ret = 0;
1018 struct buffer_head *di_bh = NULL;
1019 struct ocfs2_dinode *di = NULL;
1020 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
1021
8154da3d
TY
1022 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
1023 return -EOPNOTSUPP;
1024
cf1d6c76
TY
1025 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1026 return ret;
1027
1028 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
1029 if (ret < 0) {
1030 mlog_errno(ret);
1031 return ret;
1032 }
1033
1034 di = (struct ocfs2_dinode *)di_bh->b_data;
1035
1036 down_read(&oi->ip_xattr_sem);
1037 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
1038 if (i_ret < 0)
1039 b_ret = 0;
1040 else {
1041 if (buffer) {
1042 buffer += i_ret;
1043 size -= i_ret;
1044 }
1045 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
1046 buffer, size);
1047 if (b_ret < 0)
1048 i_ret = 0;
1049 }
1050 up_read(&oi->ip_xattr_sem);
1051 ocfs2_inode_unlock(dentry->d_inode, 0);
1052
1053 brelse(di_bh);
1054
1055 return i_ret + b_ret;
1056}
1057
1058static int ocfs2_xattr_find_entry(int name_index,
1059 const char *name,
1060 struct ocfs2_xattr_search *xs)
1061{
1062 struct ocfs2_xattr_entry *entry;
1063 size_t name_len;
1064 int i, cmp = 1;
1065
1066 if (name == NULL)
1067 return -EINVAL;
1068
1069 name_len = strlen(name);
1070 entry = xs->here;
1071 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
1072 cmp = name_index - ocfs2_xattr_get_type(entry);
1073 if (!cmp)
1074 cmp = name_len - entry->xe_name_len;
1075 if (!cmp)
1076 cmp = memcmp(name, (xs->base +
1077 le16_to_cpu(entry->xe_name_offset)),
1078 name_len);
1079 if (cmp == 0)
1080 break;
1081 entry += 1;
1082 }
1083 xs->here = entry;
1084
1085 return cmp ? -ENODATA : 0;
1086}
1087
1088static int ocfs2_xattr_get_value_outside(struct inode *inode,
589dc260 1089 struct ocfs2_xattr_value_root *xv,
cf1d6c76
TY
1090 void *buffer,
1091 size_t len)
1092{
1093 u32 cpos, p_cluster, num_clusters, bpc, clusters;
1094 u64 blkno;
1095 int i, ret = 0;
1096 size_t cplen, blocksize;
1097 struct buffer_head *bh = NULL;
cf1d6c76
TY
1098 struct ocfs2_extent_list *el;
1099
cf1d6c76
TY
1100 el = &xv->xr_list;
1101 clusters = le32_to_cpu(xv->xr_clusters);
1102 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1103 blocksize = inode->i_sb->s_blocksize;
1104
1105 cpos = 0;
1106 while (cpos < clusters) {
1107 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1061f9c1 1108 &num_clusters, el, NULL);
cf1d6c76
TY
1109 if (ret) {
1110 mlog_errno(ret);
1111 goto out;
1112 }
1113
1114 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1115 /* Copy ocfs2_xattr_value */
1116 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
8cb471e8
JB
1117 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1118 &bh, NULL);
cf1d6c76
TY
1119 if (ret) {
1120 mlog_errno(ret);
1121 goto out;
1122 }
1123
1124 cplen = len >= blocksize ? blocksize : len;
1125 memcpy(buffer, bh->b_data, cplen);
1126 len -= cplen;
1127 buffer += cplen;
1128
1129 brelse(bh);
1130 bh = NULL;
1131 if (len == 0)
1132 break;
1133 }
1134 cpos += num_clusters;
1135 }
1136out:
1137 return ret;
1138}
1139
1140static int ocfs2_xattr_ibody_get(struct inode *inode,
1141 int name_index,
1142 const char *name,
1143 void *buffer,
1144 size_t buffer_size,
1145 struct ocfs2_xattr_search *xs)
1146{
1147 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1148 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
589dc260 1149 struct ocfs2_xattr_value_root *xv;
cf1d6c76
TY
1150 size_t size;
1151 int ret = 0;
1152
1153 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
1154 return -ENODATA;
1155
1156 xs->end = (void *)di + inode->i_sb->s_blocksize;
1157 xs->header = (struct ocfs2_xattr_header *)
1158 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1159 xs->base = (void *)xs->header;
1160 xs->here = xs->header->xh_entries;
1161
1162 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1163 if (ret)
1164 return ret;
1165 size = le64_to_cpu(xs->here->xe_value_size);
1166 if (buffer) {
1167 if (size > buffer_size)
1168 return -ERANGE;
1169 if (ocfs2_xattr_is_local(xs->here)) {
1170 memcpy(buffer, (void *)xs->base +
1171 le16_to_cpu(xs->here->xe_name_offset) +
1172 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
1173 } else {
589dc260
TM
1174 xv = (struct ocfs2_xattr_value_root *)
1175 (xs->base + le16_to_cpu(
1176 xs->here->xe_name_offset) +
1177 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
1178 ret = ocfs2_xattr_get_value_outside(inode, xv,
cf1d6c76
TY
1179 buffer, size);
1180 if (ret < 0) {
1181 mlog_errno(ret);
1182 return ret;
1183 }
1184 }
1185 }
1186
1187 return size;
1188}
1189
1190static int ocfs2_xattr_block_get(struct inode *inode,
1191 int name_index,
1192 const char *name,
1193 void *buffer,
1194 size_t buffer_size,
1195 struct ocfs2_xattr_search *xs)
1196{
cf1d6c76 1197 struct ocfs2_xattr_block *xb;
589dc260 1198 struct ocfs2_xattr_value_root *xv;
cf1d6c76 1199 size_t size;
44d8e4e1
SM
1200 int ret = -ENODATA, name_offset, name_len, i;
1201 int uninitialized_var(block_off);
cf1d6c76 1202
ba937127
JB
1203 xs->bucket = ocfs2_xattr_bucket_new(inode);
1204 if (!xs->bucket) {
1205 ret = -ENOMEM;
1206 mlog_errno(ret);
1207 goto cleanup;
1208 }
589dc260 1209
54f443f4
JB
1210 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
1211 if (ret) {
cf1d6c76 1212 mlog_errno(ret);
cf1d6c76
TY
1213 goto cleanup;
1214 }
1215
6c1e183e
TY
1216 if (xs->not_found) {
1217 ret = -ENODATA;
1218 goto cleanup;
1219 }
1220
54f443f4 1221 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
cf1d6c76
TY
1222 size = le64_to_cpu(xs->here->xe_value_size);
1223 if (buffer) {
1224 ret = -ERANGE;
1225 if (size > buffer_size)
1226 goto cleanup;
589dc260
TM
1227
1228 name_offset = le16_to_cpu(xs->here->xe_name_offset);
1229 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
1230 i = xs->here - xs->header->xh_entries;
1231
1232 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
fd68a894 1233 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
ba937127 1234 bucket_xh(xs->bucket),
589dc260
TM
1235 i,
1236 &block_off,
1237 &name_offset);
ba937127 1238 xs->base = bucket_block(xs->bucket, block_off);
589dc260 1239 }
cf1d6c76
TY
1240 if (ocfs2_xattr_is_local(xs->here)) {
1241 memcpy(buffer, (void *)xs->base +
589dc260 1242 name_offset + name_len, size);
cf1d6c76 1243 } else {
589dc260
TM
1244 xv = (struct ocfs2_xattr_value_root *)
1245 (xs->base + name_offset + name_len);
1246 ret = ocfs2_xattr_get_value_outside(inode, xv,
cf1d6c76
TY
1247 buffer, size);
1248 if (ret < 0) {
1249 mlog_errno(ret);
1250 goto cleanup;
1251 }
1252 }
1253 }
1254 ret = size;
1255cleanup:
ba937127 1256 ocfs2_xattr_bucket_free(xs->bucket);
cf1d6c76 1257
54f443f4
JB
1258 brelse(xs->xattr_bh);
1259 xs->xattr_bh = NULL;
cf1d6c76
TY
1260 return ret;
1261}
1262
4e3e9d02
TY
1263int ocfs2_xattr_get_nolock(struct inode *inode,
1264 struct buffer_head *di_bh,
0030e001
TY
1265 int name_index,
1266 const char *name,
1267 void *buffer,
1268 size_t buffer_size)
cf1d6c76
TY
1269{
1270 int ret;
1271 struct ocfs2_dinode *di = NULL;
cf1d6c76
TY
1272 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1273 struct ocfs2_xattr_search xis = {
1274 .not_found = -ENODATA,
1275 };
1276 struct ocfs2_xattr_search xbs = {
1277 .not_found = -ENODATA,
1278 };
1279
8154da3d
TY
1280 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1281 return -EOPNOTSUPP;
1282
cf1d6c76
TY
1283 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1284 ret = -ENODATA;
1285
cf1d6c76
TY
1286 xis.inode_bh = xbs.inode_bh = di_bh;
1287 di = (struct ocfs2_dinode *)di_bh->b_data;
1288
1289 down_read(&oi->ip_xattr_sem);
1290 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1291 buffer_size, &xis);
6c1e183e 1292 if (ret == -ENODATA && di->i_xattr_loc)
cf1d6c76
TY
1293 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1294 buffer_size, &xbs);
1295 up_read(&oi->ip_xattr_sem);
4e3e9d02
TY
1296
1297 return ret;
1298}
1299
1300/* ocfs2_xattr_get()
1301 *
1302 * Copy an extended attribute into the buffer provided.
1303 * Buffer is NULL to compute the size of buffer required.
1304 */
1305static int ocfs2_xattr_get(struct inode *inode,
1306 int name_index,
1307 const char *name,
1308 void *buffer,
1309 size_t buffer_size)
1310{
1311 int ret;
1312 struct buffer_head *di_bh = NULL;
1313
1314 ret = ocfs2_inode_lock(inode, &di_bh, 0);
1315 if (ret < 0) {
1316 mlog_errno(ret);
1317 return ret;
1318 }
1319 ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
1320 name, buffer, buffer_size);
1321
cf1d6c76
TY
1322 ocfs2_inode_unlock(inode, 0);
1323
1324 brelse(di_bh);
1325
1326 return ret;
1327}
1328
1329static int __ocfs2_xattr_set_value_outside(struct inode *inode,
85db90e7 1330 handle_t *handle,
492a8a33 1331 struct ocfs2_xattr_value_buf *vb,
cf1d6c76
TY
1332 const void *value,
1333 int value_len)
1334{
71d548a6 1335 int ret = 0, i, cp_len;
cf1d6c76
TY
1336 u16 blocksize = inode->i_sb->s_blocksize;
1337 u32 p_cluster, num_clusters;
1338 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1339 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1340 u64 blkno;
1341 struct buffer_head *bh = NULL;
492a8a33
TM
1342 unsigned int ext_flags;
1343 struct ocfs2_xattr_value_root *xv = vb->vb_xv;
cf1d6c76
TY
1344
1345 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1346
cf1d6c76
TY
1347 while (cpos < clusters) {
1348 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1061f9c1 1349 &num_clusters, &xv->xr_list,
492a8a33 1350 &ext_flags);
cf1d6c76
TY
1351 if (ret) {
1352 mlog_errno(ret);
85db90e7 1353 goto out;
cf1d6c76
TY
1354 }
1355
492a8a33
TM
1356 BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED);
1357
cf1d6c76
TY
1358 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1359
1360 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
8cb471e8
JB
1361 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1362 &bh, NULL);
cf1d6c76
TY
1363 if (ret) {
1364 mlog_errno(ret);
85db90e7 1365 goto out;
cf1d6c76
TY
1366 }
1367
1368 ret = ocfs2_journal_access(handle,
0cf2f763 1369 INODE_CACHE(inode),
cf1d6c76
TY
1370 bh,
1371 OCFS2_JOURNAL_ACCESS_WRITE);
1372 if (ret < 0) {
1373 mlog_errno(ret);
85db90e7 1374 goto out;
cf1d6c76
TY
1375 }
1376
1377 cp_len = value_len > blocksize ? blocksize : value_len;
1378 memcpy(bh->b_data, value, cp_len);
1379 value_len -= cp_len;
1380 value += cp_len;
1381 if (cp_len < blocksize)
1382 memset(bh->b_data + cp_len, 0,
1383 blocksize - cp_len);
1384
1385 ret = ocfs2_journal_dirty(handle, bh);
1386 if (ret < 0) {
1387 mlog_errno(ret);
85db90e7 1388 goto out;
cf1d6c76
TY
1389 }
1390 brelse(bh);
1391 bh = NULL;
1392
1393 /*
1394 * XXX: do we need to empty all the following
1395 * blocks in this cluster?
1396 */
1397 if (!value_len)
1398 break;
1399 }
1400 cpos += num_clusters;
1401 }
cf1d6c76
TY
1402out:
1403 brelse(bh);
1404
1405 return ret;
1406}
1407
69a3e539
JB
1408static int ocfs2_xa_check_space_helper(int needed_space, int free_start,
1409 int num_entries)
1410{
1411 int free_space;
1412
1413 if (!needed_space)
1414 return 0;
1415
1416 free_space = free_start -
1417 sizeof(struct ocfs2_xattr_header) -
1418 (num_entries * sizeof(struct ocfs2_xattr_entry)) -
1419 OCFS2_XATTR_HEADER_GAP;
1420 if (free_space < 0)
1421 return -EIO;
1422 if (free_space < needed_space)
1423 return -ENOSPC;
1424
1425 return 0;
1426}
1427
cf2bc809
JB
1428static int ocfs2_xa_journal_access(handle_t *handle, struct ocfs2_xa_loc *loc,
1429 int type)
1430{
1431 return loc->xl_ops->xlo_journal_access(handle, loc, type);
1432}
1433
1434static void ocfs2_xa_journal_dirty(handle_t *handle, struct ocfs2_xa_loc *loc)
1435{
1436 loc->xl_ops->xlo_journal_dirty(handle, loc);
1437}
1438
69a3e539
JB
1439/* Give a pointer into the storage for the given offset */
1440static void *ocfs2_xa_offset_pointer(struct ocfs2_xa_loc *loc, int offset)
1441{
1442 BUG_ON(offset >= loc->xl_size);
1443 return loc->xl_ops->xlo_offset_pointer(loc, offset);
1444}
1445
11179f2c
JB
1446/*
1447 * Wipe the name+value pair and allow the storage to reclaim it. This
1448 * must be followed by either removal of the entry or a call to
1449 * ocfs2_xa_add_namevalue().
1450 */
1451static void ocfs2_xa_wipe_namevalue(struct ocfs2_xa_loc *loc)
1452{
1453 loc->xl_ops->xlo_wipe_namevalue(loc);
1454}
1455
69a3e539
JB
1456/*
1457 * Find lowest offset to a name+value pair. This is the start of our
1458 * downward-growing free space.
1459 */
1460static int ocfs2_xa_get_free_start(struct ocfs2_xa_loc *loc)
1461{
1462 return loc->xl_ops->xlo_get_free_start(loc);
1463}
1464
1465/* Can we reuse loc->xl_entry for xi? */
1466static int ocfs2_xa_can_reuse_entry(struct ocfs2_xa_loc *loc,
1467 struct ocfs2_xattr_info *xi)
1468{
1469 return loc->xl_ops->xlo_can_reuse(loc, xi);
1470}
1471
1472/* How much free space is needed to set the new value */
1473static int ocfs2_xa_check_space(struct ocfs2_xa_loc *loc,
1474 struct ocfs2_xattr_info *xi)
1475{
1476 return loc->xl_ops->xlo_check_space(loc, xi);
1477}
1478
1479static void ocfs2_xa_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1480{
1481 loc->xl_ops->xlo_add_entry(loc, name_hash);
1482 loc->xl_entry->xe_name_hash = cpu_to_le32(name_hash);
1483 /*
1484 * We can't leave the new entry's xe_name_offset at zero or
1485 * add_namevalue() will go nuts. We set it to the size of our
1486 * storage so that it can never be less than any other entry.
1487 */
1488 loc->xl_entry->xe_name_offset = cpu_to_le16(loc->xl_size);
1489}
1490
1491static void ocfs2_xa_add_namevalue(struct ocfs2_xa_loc *loc,
1492 struct ocfs2_xattr_info *xi)
1493{
1494 int size = namevalue_size_xi(xi);
1495 int nameval_offset;
1496 char *nameval_buf;
1497
1498 loc->xl_ops->xlo_add_namevalue(loc, size);
1499 loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
1500 loc->xl_entry->xe_name_len = xi->xi_name_len;
1501 ocfs2_xattr_set_type(loc->xl_entry, xi->xi_name_index);
1502 ocfs2_xattr_set_local(loc->xl_entry,
1503 xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE);
1504
1505 nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1506 nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
1507 memset(nameval_buf, 0, size);
1508 memcpy(nameval_buf, xi->xi_name, xi->xi_name_len);
1509}
1510
3fc12afa
JB
1511static void ocfs2_xa_fill_value_buf(struct ocfs2_xa_loc *loc,
1512 struct ocfs2_xattr_value_buf *vb)
1513{
1514 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1515 int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len);
1516
1517 /* Value bufs are for value trees */
73857ee0 1518 BUG_ON(ocfs2_xattr_is_local(loc->xl_entry));
3fc12afa
JB
1519 BUG_ON(namevalue_size_xe(loc->xl_entry) !=
1520 (name_size + OCFS2_XATTR_ROOT_SIZE));
1521
1522 loc->xl_ops->xlo_fill_value_buf(loc, vb);
1523 vb->vb_xv =
1524 (struct ocfs2_xattr_value_root *)ocfs2_xa_offset_pointer(loc,
1525 nameval_offset +
1526 name_size);
1527}
1528
cf2bc809
JB
1529static int ocfs2_xa_block_journal_access(handle_t *handle,
1530 struct ocfs2_xa_loc *loc, int type)
1531{
1532 struct buffer_head *bh = loc->xl_storage;
1533 ocfs2_journal_access_func access;
1534
1535 if (loc->xl_size == (bh->b_size -
1536 offsetof(struct ocfs2_xattr_block,
1537 xb_attrs.xb_header)))
1538 access = ocfs2_journal_access_xb;
1539 else
1540 access = ocfs2_journal_access_di;
1541 return access(handle, INODE_CACHE(loc->xl_inode), bh, type);
1542}
1543
1544static void ocfs2_xa_block_journal_dirty(handle_t *handle,
1545 struct ocfs2_xa_loc *loc)
1546{
1547 struct buffer_head *bh = loc->xl_storage;
1548
1549 ocfs2_journal_dirty(handle, bh);
1550}
1551
11179f2c
JB
1552static void *ocfs2_xa_block_offset_pointer(struct ocfs2_xa_loc *loc,
1553 int offset)
1554{
11179f2c
JB
1555 return (char *)loc->xl_header + offset;
1556}
1557
69a3e539
JB
1558static int ocfs2_xa_block_can_reuse(struct ocfs2_xa_loc *loc,
1559 struct ocfs2_xattr_info *xi)
1560{
1561 /*
1562 * Block storage is strict. If the sizes aren't exact, we will
1563 * remove the old one and reinsert the new.
1564 */
1565 return namevalue_size_xe(loc->xl_entry) ==
1566 namevalue_size_xi(xi);
1567}
1568
1569static int ocfs2_xa_block_get_free_start(struct ocfs2_xa_loc *loc)
1570{
1571 struct ocfs2_xattr_header *xh = loc->xl_header;
1572 int i, count = le16_to_cpu(xh->xh_count);
1573 int offset, free_start = loc->xl_size;
1574
1575 for (i = 0; i < count; i++) {
1576 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1577 if (offset < free_start)
1578 free_start = offset;
1579 }
1580
1581 return free_start;
1582}
1583
1584static int ocfs2_xa_block_check_space(struct ocfs2_xa_loc *loc,
1585 struct ocfs2_xattr_info *xi)
1586{
1587 int count = le16_to_cpu(loc->xl_header->xh_count);
1588 int free_start = ocfs2_xa_get_free_start(loc);
1589 int needed_space = ocfs2_xi_entry_usage(xi);
1590
1591 /*
1592 * Block storage will reclaim the original entry before inserting
1593 * the new value, so we only need the difference. If the new
1594 * entry is smaller than the old one, we don't need anything.
1595 */
1596 if (loc->xl_entry) {
1597 /* Don't need space if we're reusing! */
1598 if (ocfs2_xa_can_reuse_entry(loc, xi))
1599 needed_space = 0;
1600 else
1601 needed_space -= ocfs2_xe_entry_usage(loc->xl_entry);
1602 }
1603 if (needed_space < 0)
1604 needed_space = 0;
1605 return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1606}
1607
11179f2c
JB
1608/*
1609 * Block storage for xattrs keeps the name+value pairs compacted. When
1610 * we remove one, we have to shift any that preceded it towards the end.
1611 */
1612static void ocfs2_xa_block_wipe_namevalue(struct ocfs2_xa_loc *loc)
1613{
1614 int i, offset;
1615 int namevalue_offset, first_namevalue_offset, namevalue_size;
1616 struct ocfs2_xattr_entry *entry = loc->xl_entry;
1617 struct ocfs2_xattr_header *xh = loc->xl_header;
11179f2c
JB
1618 int count = le16_to_cpu(xh->xh_count);
1619
1620 namevalue_offset = le16_to_cpu(entry->xe_name_offset);
199799a3 1621 namevalue_size = namevalue_size_xe(entry);
69a3e539 1622 first_namevalue_offset = ocfs2_xa_get_free_start(loc);
11179f2c
JB
1623
1624 /* Shift the name+value pairs */
1625 memmove((char *)xh + first_namevalue_offset + namevalue_size,
1626 (char *)xh + first_namevalue_offset,
1627 namevalue_offset - first_namevalue_offset);
1628 memset((char *)xh + first_namevalue_offset, 0, namevalue_size);
1629
1630 /* Now tell xh->xh_entries about it */
1631 for (i = 0; i < count; i++) {
1632 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1633 if (offset < namevalue_offset)
1634 le16_add_cpu(&xh->xh_entries[i].xe_name_offset,
1635 namevalue_size);
1636 }
1637
1638 /*
1639 * Note that we don't update xh_free_start or xh_name_value_len
1640 * because they're not used in block-stored xattrs.
1641 */
1642}
1643
69a3e539
JB
1644static void ocfs2_xa_block_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1645{
1646 int count = le16_to_cpu(loc->xl_header->xh_count);
1647 loc->xl_entry = &(loc->xl_header->xh_entries[count]);
1648 le16_add_cpu(&loc->xl_header->xh_count, 1);
1649 memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1650}
1651
1652static void ocfs2_xa_block_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1653{
1654 int free_start = ocfs2_xa_get_free_start(loc);
1655
1656 loc->xl_entry->xe_name_offset = cpu_to_le16(free_start - size);
1657}
1658
3fc12afa
JB
1659static void ocfs2_xa_block_fill_value_buf(struct ocfs2_xa_loc *loc,
1660 struct ocfs2_xattr_value_buf *vb)
1661{
1662 struct buffer_head *bh = loc->xl_storage;
1663
1664 if (loc->xl_size == (bh->b_size -
1665 offsetof(struct ocfs2_xattr_block,
1666 xb_attrs.xb_header)))
1667 vb->vb_access = ocfs2_journal_access_xb;
1668 else
1669 vb->vb_access = ocfs2_journal_access_di;
1670 vb->vb_bh = bh;
1671}
1672
11179f2c
JB
1673/*
1674 * Operations for xattrs stored in blocks. This includes inline inode
1675 * storage and unindexed ocfs2_xattr_blocks.
1676 */
1677static const struct ocfs2_xa_loc_operations ocfs2_xa_block_loc_ops = {
cf2bc809
JB
1678 .xlo_journal_access = ocfs2_xa_block_journal_access,
1679 .xlo_journal_dirty = ocfs2_xa_block_journal_dirty,
11179f2c 1680 .xlo_offset_pointer = ocfs2_xa_block_offset_pointer,
69a3e539
JB
1681 .xlo_check_space = ocfs2_xa_block_check_space,
1682 .xlo_can_reuse = ocfs2_xa_block_can_reuse,
1683 .xlo_get_free_start = ocfs2_xa_block_get_free_start,
11179f2c 1684 .xlo_wipe_namevalue = ocfs2_xa_block_wipe_namevalue,
69a3e539
JB
1685 .xlo_add_entry = ocfs2_xa_block_add_entry,
1686 .xlo_add_namevalue = ocfs2_xa_block_add_namevalue,
3fc12afa 1687 .xlo_fill_value_buf = ocfs2_xa_block_fill_value_buf,
11179f2c
JB
1688};
1689
cf2bc809
JB
1690static int ocfs2_xa_bucket_journal_access(handle_t *handle,
1691 struct ocfs2_xa_loc *loc, int type)
1692{
1693 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1694
1695 return ocfs2_xattr_bucket_journal_access(handle, bucket, type);
1696}
1697
1698static void ocfs2_xa_bucket_journal_dirty(handle_t *handle,
1699 struct ocfs2_xa_loc *loc)
1700{
1701 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1702
1703 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
1704}
1705
11179f2c
JB
1706static void *ocfs2_xa_bucket_offset_pointer(struct ocfs2_xa_loc *loc,
1707 int offset)
1708{
1709 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1710 int block, block_offset;
1711
11179f2c 1712 /* The header is at the front of the bucket */
cf2bc809
JB
1713 block = offset >> loc->xl_inode->i_sb->s_blocksize_bits;
1714 block_offset = offset % loc->xl_inode->i_sb->s_blocksize;
11179f2c
JB
1715
1716 return bucket_block(bucket, block) + block_offset;
1717}
1718
69a3e539
JB
1719static int ocfs2_xa_bucket_can_reuse(struct ocfs2_xa_loc *loc,
1720 struct ocfs2_xattr_info *xi)
1721{
1722 return namevalue_size_xe(loc->xl_entry) >=
1723 namevalue_size_xi(xi);
1724}
1725
1726static int ocfs2_xa_bucket_get_free_start(struct ocfs2_xa_loc *loc)
1727{
1728 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1729 return le16_to_cpu(bucket_xh(bucket)->xh_free_start);
1730}
1731
1732static int ocfs2_bucket_align_free_start(struct super_block *sb,
1733 int free_start, int size)
1734{
1735 /*
1736 * We need to make sure that the name+value pair fits within
1737 * one block.
1738 */
1739 if (((free_start - size) >> sb->s_blocksize_bits) !=
1740 ((free_start - 1) >> sb->s_blocksize_bits))
1741 free_start -= free_start % sb->s_blocksize;
1742
1743 return free_start;
1744}
1745
1746static int ocfs2_xa_bucket_check_space(struct ocfs2_xa_loc *loc,
1747 struct ocfs2_xattr_info *xi)
1748{
1749 int rc;
1750 int count = le16_to_cpu(loc->xl_header->xh_count);
1751 int free_start = ocfs2_xa_get_free_start(loc);
1752 int needed_space = ocfs2_xi_entry_usage(xi);
1753 int size = namevalue_size_xi(xi);
cf2bc809 1754 struct super_block *sb = loc->xl_inode->i_sb;
69a3e539
JB
1755
1756 /*
1757 * Bucket storage does not reclaim name+value pairs it cannot
1758 * reuse. They live as holes until the bucket fills, and then
1759 * the bucket is defragmented. However, the bucket can reclaim
1760 * the ocfs2_xattr_entry.
1761 */
1762 if (loc->xl_entry) {
1763 /* Don't need space if we're reusing! */
1764 if (ocfs2_xa_can_reuse_entry(loc, xi))
1765 needed_space = 0;
1766 else
1767 needed_space -= sizeof(struct ocfs2_xattr_entry);
1768 }
1769 BUG_ON(needed_space < 0);
1770
1771 if (free_start < size) {
1772 if (needed_space)
1773 return -ENOSPC;
1774 } else {
1775 /*
1776 * First we check if it would fit in the first place.
1777 * Below, we align the free start to a block. This may
1778 * slide us below the minimum gap. By checking unaligned
1779 * first, we avoid that error.
1780 */
1781 rc = ocfs2_xa_check_space_helper(needed_space, free_start,
1782 count);
1783 if (rc)
1784 return rc;
1785 free_start = ocfs2_bucket_align_free_start(sb, free_start,
1786 size);
1787 }
1788 return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1789}
1790
11179f2c
JB
1791static void ocfs2_xa_bucket_wipe_namevalue(struct ocfs2_xa_loc *loc)
1792{
199799a3
JB
1793 le16_add_cpu(&loc->xl_header->xh_name_value_len,
1794 -namevalue_size_xe(loc->xl_entry));
11179f2c
JB
1795}
1796
69a3e539
JB
1797static void ocfs2_xa_bucket_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1798{
1799 struct ocfs2_xattr_header *xh = loc->xl_header;
1800 int count = le16_to_cpu(xh->xh_count);
1801 int low = 0, high = count - 1, tmp;
1802 struct ocfs2_xattr_entry *tmp_xe;
1803
1804 /*
1805 * We keep buckets sorted by name_hash, so we need to find
1806 * our insert place.
1807 */
1808 while (low <= high && count) {
1809 tmp = (low + high) / 2;
1810 tmp_xe = &xh->xh_entries[tmp];
1811
1812 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
1813 low = tmp + 1;
1814 else if (name_hash < le32_to_cpu(tmp_xe->xe_name_hash))
1815 high = tmp - 1;
1816 else {
1817 low = tmp;
1818 break;
1819 }
1820 }
1821
1822 if (low != count)
1823 memmove(&xh->xh_entries[low + 1],
1824 &xh->xh_entries[low],
1825 ((count - low) * sizeof(struct ocfs2_xattr_entry)));
1826
1827 le16_add_cpu(&xh->xh_count, 1);
1828 loc->xl_entry = &xh->xh_entries[low];
1829 memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1830}
1831
1832static void ocfs2_xa_bucket_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1833{
1834 int free_start = ocfs2_xa_get_free_start(loc);
1835 struct ocfs2_xattr_header *xh = loc->xl_header;
cf2bc809 1836 struct super_block *sb = loc->xl_inode->i_sb;
69a3e539
JB
1837 int nameval_offset;
1838
1839 free_start = ocfs2_bucket_align_free_start(sb, free_start, size);
1840 nameval_offset = free_start - size;
1841 loc->xl_entry->xe_name_offset = cpu_to_le16(nameval_offset);
1842 xh->xh_free_start = cpu_to_le16(nameval_offset);
1843 le16_add_cpu(&xh->xh_name_value_len, size);
1844
1845}
1846
3fc12afa
JB
1847static void ocfs2_xa_bucket_fill_value_buf(struct ocfs2_xa_loc *loc,
1848 struct ocfs2_xattr_value_buf *vb)
1849{
1850 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
cf2bc809 1851 struct super_block *sb = loc->xl_inode->i_sb;
3fc12afa
JB
1852 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1853 int size = namevalue_size_xe(loc->xl_entry);
1854 int block_offset = nameval_offset >> sb->s_blocksize_bits;
1855
1856 /* Values are not allowed to straddle block boundaries */
1857 BUG_ON(block_offset !=
1858 ((nameval_offset + size - 1) >> sb->s_blocksize_bits));
1859 /* We expect the bucket to be filled in */
1860 BUG_ON(!bucket->bu_bhs[block_offset]);
1861
1862 vb->vb_access = ocfs2_journal_access;
1863 vb->vb_bh = bucket->bu_bhs[block_offset];
1864}
1865
11179f2c
JB
1866/* Operations for xattrs stored in buckets. */
1867static const struct ocfs2_xa_loc_operations ocfs2_xa_bucket_loc_ops = {
cf2bc809
JB
1868 .xlo_journal_access = ocfs2_xa_bucket_journal_access,
1869 .xlo_journal_dirty = ocfs2_xa_bucket_journal_dirty,
11179f2c 1870 .xlo_offset_pointer = ocfs2_xa_bucket_offset_pointer,
69a3e539
JB
1871 .xlo_check_space = ocfs2_xa_bucket_check_space,
1872 .xlo_can_reuse = ocfs2_xa_bucket_can_reuse,
1873 .xlo_get_free_start = ocfs2_xa_bucket_get_free_start,
11179f2c 1874 .xlo_wipe_namevalue = ocfs2_xa_bucket_wipe_namevalue,
69a3e539
JB
1875 .xlo_add_entry = ocfs2_xa_bucket_add_entry,
1876 .xlo_add_namevalue = ocfs2_xa_bucket_add_namevalue,
3fc12afa 1877 .xlo_fill_value_buf = ocfs2_xa_bucket_fill_value_buf,
11179f2c
JB
1878};
1879
73857ee0
JB
1880static int ocfs2_xa_value_truncate(struct ocfs2_xa_loc *loc, u64 bytes,
1881 struct ocfs2_xattr_set_ctxt *ctxt)
1882{
1883 int trunc_rc, access_rc;
1884 struct ocfs2_xattr_value_buf vb;
1885
1886 ocfs2_xa_fill_value_buf(loc, &vb);
1887 trunc_rc = ocfs2_xattr_value_truncate(loc->xl_inode, &vb, bytes,
1888 ctxt);
1889
1890 /*
1891 * The caller of ocfs2_xa_value_truncate() has already called
1892 * ocfs2_xa_journal_access on the loc. However, The truncate code
1893 * calls ocfs2_extend_trans(). This may commit the previous
1894 * transaction and open a new one. If this is a bucket, truncate
1895 * could leave only vb->vb_bh set up for journaling. Meanwhile,
1896 * the caller is expecting to dirty the entire bucket. So we must
1897 * reset the journal work. We do this even if truncate has failed,
1898 * as it could have failed after committing the extend.
1899 */
1900 access_rc = ocfs2_xa_journal_access(ctxt->handle, loc,
1901 OCFS2_JOURNAL_ACCESS_WRITE);
1902
1903 /* Errors in truncate take precedence */
1904 return trunc_rc ? trunc_rc : access_rc;
1905}
1906
11179f2c
JB
1907static void ocfs2_xa_remove_entry(struct ocfs2_xa_loc *loc)
1908{
bde1e540
JB
1909 int index, count;
1910 struct ocfs2_xattr_header *xh = loc->xl_header;
1911 struct ocfs2_xattr_entry *entry = loc->xl_entry;
1912
11179f2c 1913 ocfs2_xa_wipe_namevalue(loc);
bde1e540
JB
1914 loc->xl_entry = NULL;
1915
1916 le16_add_cpu(&xh->xh_count, -1);
1917 count = le16_to_cpu(xh->xh_count);
1918
1919 /*
1920 * Only zero out the entry if there are more remaining. This is
1921 * important for an empty bucket, as it keeps track of the
1922 * bucket's hash value. It doesn't hurt empty block storage.
1923 */
1924 if (count) {
1925 index = ((char *)entry - (char *)&xh->xh_entries) /
1926 sizeof(struct ocfs2_xattr_entry);
1927 memmove(&xh->xh_entries[index], &xh->xh_entries[index + 1],
1928 (count - index) * sizeof(struct ocfs2_xattr_entry));
1929 memset(&xh->xh_entries[count], 0,
1930 sizeof(struct ocfs2_xattr_entry));
1931 }
11179f2c
JB
1932}
1933
73857ee0
JB
1934static int ocfs2_xa_remove(struct ocfs2_xa_loc *loc,
1935 struct ocfs2_xattr_set_ctxt *ctxt)
1936{
1937 int rc = 0;
1938
1939 if (!ocfs2_xattr_is_local(loc->xl_entry)) {
1940 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
1941 if (rc) {
1942 mlog_errno(rc);
1943 goto out;
1944 }
1945 }
1946
1947 ocfs2_xa_remove_entry(loc);
1948
1949out:
1950 return rc;
1951}
1952
1953static void ocfs2_xa_install_value_root(struct ocfs2_xa_loc *loc)
1954{
1955 int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len);
1956 char *nameval_buf;
1957
1958 nameval_buf = ocfs2_xa_offset_pointer(loc,
1959 le16_to_cpu(loc->xl_entry->xe_name_offset));
1960 memcpy(nameval_buf + name_size, &def_xv, OCFS2_XATTR_ROOT_SIZE);
1961}
1962
1963/*
1964 * Take an existing entry and make it ready for the new value. This
1965 * won't allocate space, but it may free space. It should be ready for
1966 * ocfs2_xa_prepare_entry() to finish the work.
1967 */
1968static int ocfs2_xa_reuse_entry(struct ocfs2_xa_loc *loc,
1969 struct ocfs2_xattr_info *xi,
1970 struct ocfs2_xattr_set_ctxt *ctxt)
1971{
1972 int rc = 0;
1973 int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
1974 char *nameval_buf;
1975 int xe_local = ocfs2_xattr_is_local(loc->xl_entry);
1976 int xi_local = xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE;
1977
1978 BUG_ON(OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len) !=
1979 name_size);
1980
1981 nameval_buf = ocfs2_xa_offset_pointer(loc,
1982 le16_to_cpu(loc->xl_entry->xe_name_offset));
1983 if (xe_local) {
1984 memset(nameval_buf + name_size, 0,
1985 namevalue_size_xe(loc->xl_entry) - name_size);
1986 if (!xi_local)
1987 ocfs2_xa_install_value_root(loc);
1988 } else {
1989 if (xi_local) {
1990 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
1991 if (rc < 0) {
1992 mlog_errno(rc);
1993 goto out;
1994 }
1995 memset(nameval_buf + name_size, 0,
1996 namevalue_size_xe(loc->xl_entry) -
1997 name_size);
1998 } else if (le64_to_cpu(loc->xl_entry->xe_value_size) >
1999 xi->xi_value_len) {
2000 rc = ocfs2_xa_value_truncate(loc, xi->xi_value_len,
2001 ctxt);
2002 if (rc < 0) {
2003 mlog_errno(rc);
2004 goto out;
2005 }
2006 }
2007 }
2008
2009 loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
2010 ocfs2_xattr_set_local(loc->xl_entry, xi_local);
2011
2012out:
2013 return rc;
2014}
2015
69a3e539
JB
2016/*
2017 * Prepares loc->xl_entry to receive the new xattr. This includes
2018 * properly setting up the name+value pair region. If loc->xl_entry
2019 * already exists, it will take care of modifying it appropriately.
2020 * This also includes deleting entries, but don't call this to remove
2021 * a non-existant entry. That's just a bug.
2022 *
2023 * Note that this modifies the data. You did journal_access already,
2024 * right?
2025 */
2026static int ocfs2_xa_prepare_entry(struct ocfs2_xa_loc *loc,
2027 struct ocfs2_xattr_info *xi,
73857ee0
JB
2028 u32 name_hash,
2029 struct ocfs2_xattr_set_ctxt *ctxt)
69a3e539
JB
2030{
2031 int rc = 0;
69a3e539
JB
2032
2033 if (!xi->xi_value) {
73857ee0 2034 rc = ocfs2_xa_remove(loc, ctxt);
69a3e539
JB
2035 goto out;
2036 }
2037
2038 rc = ocfs2_xa_check_space(loc, xi);
2039 if (rc)
2040 goto out;
2041
2042 if (loc->xl_entry) {
2043 if (ocfs2_xa_can_reuse_entry(loc, xi)) {
73857ee0
JB
2044 rc = ocfs2_xa_reuse_entry(loc, xi, ctxt);
2045 if (rc)
2046 goto out;
2047 goto alloc_value;
69a3e539
JB
2048 }
2049
73857ee0
JB
2050 if (!ocfs2_xattr_is_local(loc->xl_entry)) {
2051 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
2052 if (rc) {
2053 mlog_errno(rc);
2054 goto out;
2055 }
2056 }
69a3e539
JB
2057 ocfs2_xa_wipe_namevalue(loc);
2058 } else
2059 ocfs2_xa_add_entry(loc, name_hash);
2060
2061 /*
2062 * If we get here, we have a blank entry. Fill it. We grow our
2063 * name+value pair back from the end.
2064 */
2065 ocfs2_xa_add_namevalue(loc, xi);
73857ee0
JB
2066 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
2067 ocfs2_xa_install_value_root(loc);
2068
2069alloc_value:
2070 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2071 rc = ocfs2_xa_value_truncate(loc, xi->xi_value_len, ctxt);
2072 if (rc < 0)
2073 mlog_errno(rc);
2074 }
69a3e539
JB
2075
2076out:
2077 return rc;
2078}
2079
2080/*
73857ee0
JB
2081 * Store the value portion of the name+value pair. This will skip
2082 * values that are stored externally. Their tree roots were set up
2083 * by ocfs2_xa_prepare_entry().
69a3e539 2084 */
73857ee0
JB
2085static int ocfs2_xa_store_value(struct ocfs2_xa_loc *loc,
2086 struct ocfs2_xattr_info *xi,
2087 struct ocfs2_xattr_set_ctxt *ctxt)
69a3e539 2088{
73857ee0 2089 int rc = 0;
69a3e539
JB
2090 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
2091 int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
69a3e539 2092 char *nameval_buf;
73857ee0 2093 struct ocfs2_xattr_value_buf vb;
69a3e539
JB
2094
2095 if (!xi->xi_value)
73857ee0 2096 goto out;
69a3e539
JB
2097
2098 nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
73857ee0
JB
2099 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2100 ocfs2_xa_fill_value_buf(loc, &vb);
2101 rc = __ocfs2_xattr_set_value_outside(loc->xl_inode,
2102 ctxt->handle, &vb,
2103 xi->xi_value,
2104 xi->xi_value_len);
2105 } else
2106 memcpy(nameval_buf + name_size, xi->xi_value, xi->xi_value_len);
2107
2108out:
2109 return rc;
69a3e539
JB
2110}
2111
11179f2c
JB
2112static void ocfs2_init_dinode_xa_loc(struct ocfs2_xa_loc *loc,
2113 struct inode *inode,
2114 struct buffer_head *bh,
2115 struct ocfs2_xattr_entry *entry)
2116{
2117 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
2118
cf2bc809 2119 loc->xl_inode = inode;
11179f2c
JB
2120 loc->xl_ops = &ocfs2_xa_block_loc_ops;
2121 loc->xl_storage = bh;
2122 loc->xl_entry = entry;
2123
2124 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
2125 loc->xl_size = le16_to_cpu(di->i_xattr_inline_size);
2126 else {
2127 BUG_ON(entry);
2128 loc->xl_size = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
2129 }
2130 loc->xl_header =
2131 (struct ocfs2_xattr_header *)(bh->b_data + bh->b_size -
2132 loc->xl_size);
2133}
2134
2135static void ocfs2_init_xattr_block_xa_loc(struct ocfs2_xa_loc *loc,
cf2bc809 2136 struct inode *inode,
11179f2c
JB
2137 struct buffer_head *bh,
2138 struct ocfs2_xattr_entry *entry)
2139{
2140 struct ocfs2_xattr_block *xb =
2141 (struct ocfs2_xattr_block *)bh->b_data;
2142
2143 BUG_ON(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED);
2144
cf2bc809 2145 loc->xl_inode = inode;
11179f2c
JB
2146 loc->xl_ops = &ocfs2_xa_block_loc_ops;
2147 loc->xl_storage = bh;
2148 loc->xl_header = &(xb->xb_attrs.xb_header);
2149 loc->xl_entry = entry;
2150 loc->xl_size = bh->b_size - offsetof(struct ocfs2_xattr_block,
2151 xb_attrs.xb_header);
2152}
2153
2154static void ocfs2_init_xattr_bucket_xa_loc(struct ocfs2_xa_loc *loc,
2155 struct ocfs2_xattr_bucket *bucket,
2156 struct ocfs2_xattr_entry *entry)
2157{
cf2bc809 2158 loc->xl_inode = bucket->bu_inode;
11179f2c
JB
2159 loc->xl_ops = &ocfs2_xa_bucket_loc_ops;
2160 loc->xl_storage = bucket;
2161 loc->xl_header = bucket_xh(bucket);
2162 loc->xl_entry = entry;
2163 loc->xl_size = OCFS2_XATTR_BUCKET_SIZE;
2164}
2165
cf1d6c76
TY
2166
2167/*
2168 * ocfs2_xattr_set_entry()
2169 *
2170 * Set extended attribute entry into inode or block.
2171 *
2172 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
69a3e539 2173 * We first insert tree root(ocfs2_xattr_value_root) like a normal value,
cf1d6c76
TY
2174 * then set value in B tree with set_value_outside().
2175 */
2176static int ocfs2_xattr_set_entry(struct inode *inode,
2177 struct ocfs2_xattr_info *xi,
2178 struct ocfs2_xattr_search *xs,
78f30c31 2179 struct ocfs2_xattr_set_ctxt *ctxt,
cf1d6c76
TY
2180 int flag)
2181{
cf1d6c76
TY
2182 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2183 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
85db90e7 2184 handle_t *handle = ctxt->handle;
73857ee0 2185 int ret;
69a3e539
JB
2186 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->xi_name,
2187 xi->xi_name_len);
2188 struct ocfs2_xa_loc loc;
512620f4 2189
73857ee0 2190 if (!(flag & OCFS2_INLINE_XATTR_FL))
512620f4 2191 BUG_ON(xs->xattr_bh == xs->inode_bh);
73857ee0 2192 else
512620f4 2193 BUG_ON(xs->xattr_bh != xs->inode_bh);
cf1d6c76 2194
0cf2f763 2195 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), xs->inode_bh,
512620f4 2196 OCFS2_JOURNAL_ACCESS_WRITE);
cf1d6c76
TY
2197 if (ret) {
2198 mlog_errno(ret);
85db90e7 2199 goto out;
cf1d6c76
TY
2200 }
2201
69a3e539
JB
2202 if (xs->xattr_bh == xs->inode_bh)
2203 ocfs2_init_dinode_xa_loc(&loc, inode, xs->inode_bh,
2204 xs->not_found ? NULL : xs->here);
2205 else
cf2bc809 2206 ocfs2_init_xattr_block_xa_loc(&loc, inode, xs->xattr_bh,
69a3e539 2207 xs->not_found ? NULL : xs->here);
cf2bc809
JB
2208 ret = ocfs2_xa_journal_access(handle, &loc,
2209 OCFS2_JOURNAL_ACCESS_WRITE);
2210 if (ret) {
2211 mlog_errno(ret);
2212 goto out;
2213 }
69a3e539 2214
73857ee0 2215 ret = ocfs2_xa_prepare_entry(&loc, xi, name_hash, ctxt);
69a3e539
JB
2216 if (ret) {
2217 if (ret != -ENOSPC)
2218 mlog_errno(ret);
2219 goto out;
2220 }
69a3e539 2221 xs->here = loc.xl_entry;
cf1d6c76 2222
73857ee0
JB
2223 ret = ocfs2_xa_store_value(&loc, xi, ctxt);
2224 if (ret) {
2225 mlog_errno(ret);
2226 goto out;
2227 }
2228
cf2bc809 2229 ocfs2_xa_journal_dirty(handle, &loc);
cf1d6c76
TY
2230
2231 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
2232 (flag & OCFS2_INLINE_XATTR_FL)) {
2233 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2234 unsigned int xattrsize = osb->s_xattr_inline_size;
2235
2236 /*
2237 * Adjust extent record count or inline data size
2238 * to reserve space for extended attribute.
2239 */
2240 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2241 struct ocfs2_inline_data *idata = &di->id2.i_data;
2242 le16_add_cpu(&idata->id_count, -xattrsize);
2243 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
2244 struct ocfs2_extent_list *el = &di->id2.i_list;
2245 le16_add_cpu(&el->l_count, -(xattrsize /
2246 sizeof(struct ocfs2_extent_rec)));
2247 }
2248 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
2249 }
2250 /* Update xattr flag */
2251 spin_lock(&oi->ip_lock);
2252 oi->ip_dyn_features |= flag;
2253 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2254 spin_unlock(&oi->ip_lock);
cf1d6c76
TY
2255
2256 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
2257 if (ret < 0)
2258 mlog_errno(ret);
2259
cf1d6c76
TY
2260out:
2261 return ret;
cf1d6c76
TY
2262}
2263
ce9c5a54
TM
2264/*
2265 * In xattr remove, if it is stored outside and refcounted, we may have
2266 * the chance to split the refcount tree. So need the allocators.
2267 */
2268static int ocfs2_lock_xattr_remove_allocators(struct inode *inode,
2269 struct ocfs2_xattr_value_root *xv,
2270 struct ocfs2_caching_info *ref_ci,
2271 struct buffer_head *ref_root_bh,
2272 struct ocfs2_alloc_context **meta_ac,
2273 int *ref_credits)
2274{
2275 int ret, meta_add = 0;
2276 u32 p_cluster, num_clusters;
2277 unsigned int ext_flags;
2278
2279 *ref_credits = 0;
2280 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
2281 &num_clusters,
2282 &xv->xr_list,
2283 &ext_flags);
2284 if (ret) {
2285 mlog_errno(ret);
2286 goto out;
2287 }
2288
2289 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
2290 goto out;
2291
2292 ret = ocfs2_refcounted_xattr_delete_need(inode, ref_ci,
2293 ref_root_bh, xv,
2294 &meta_add, ref_credits);
2295 if (ret) {
2296 mlog_errno(ret);
2297 goto out;
2298 }
2299
2300 ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
2301 meta_add, meta_ac);
2302 if (ret)
2303 mlog_errno(ret);
2304
2305out:
2306 return ret;
2307}
2308
cf1d6c76 2309static int ocfs2_remove_value_outside(struct inode*inode,
4311901d 2310 struct ocfs2_xattr_value_buf *vb,
ce9c5a54
TM
2311 struct ocfs2_xattr_header *header,
2312 struct ocfs2_caching_info *ref_ci,
2313 struct buffer_head *ref_root_bh)
cf1d6c76 2314{
ce9c5a54 2315 int ret = 0, i, ref_credits;
78f30c31
TM
2316 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2317 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
ce9c5a54 2318 void *val;
78f30c31
TM
2319
2320 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
cf1d6c76
TY
2321
2322 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
2323 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
2324
ce9c5a54
TM
2325 if (ocfs2_xattr_is_local(entry))
2326 continue;
cf1d6c76 2327
ce9c5a54
TM
2328 val = (void *)header +
2329 le16_to_cpu(entry->xe_name_offset);
2330 vb->vb_xv = (struct ocfs2_xattr_value_root *)
2331 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
2332
2333 ret = ocfs2_lock_xattr_remove_allocators(inode, vb->vb_xv,
2334 ref_ci, ref_root_bh,
2335 &ctxt.meta_ac,
2336 &ref_credits);
2337
2338 ctxt.handle = ocfs2_start_trans(osb, ref_credits +
2339 ocfs2_remove_extent_credits(osb->sb));
2340 if (IS_ERR(ctxt.handle)) {
2341 ret = PTR_ERR(ctxt.handle);
2342 mlog_errno(ret);
2343 break;
2344 }
2345
2346 ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt);
2347 if (ret < 0) {
2348 mlog_errno(ret);
2349 break;
2350 }
2351
2352 ocfs2_commit_trans(osb, ctxt.handle);
2353 if (ctxt.meta_ac) {
2354 ocfs2_free_alloc_context(ctxt.meta_ac);
2355 ctxt.meta_ac = NULL;
cf1d6c76
TY
2356 }
2357 }
2358
ce9c5a54
TM
2359 if (ctxt.meta_ac)
2360 ocfs2_free_alloc_context(ctxt.meta_ac);
78f30c31
TM
2361 ocfs2_schedule_truncate_log_flush(osb, 1);
2362 ocfs2_run_deallocs(osb, &ctxt.dealloc);
cf1d6c76
TY
2363 return ret;
2364}
2365
2366static int ocfs2_xattr_ibody_remove(struct inode *inode,
ce9c5a54
TM
2367 struct buffer_head *di_bh,
2368 struct ocfs2_caching_info *ref_ci,
2369 struct buffer_head *ref_root_bh)
cf1d6c76
TY
2370{
2371
2372 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2373 struct ocfs2_xattr_header *header;
2374 int ret;
4311901d
JB
2375 struct ocfs2_xattr_value_buf vb = {
2376 .vb_bh = di_bh,
2377 .vb_access = ocfs2_journal_access_di,
2378 };
cf1d6c76
TY
2379
2380 header = (struct ocfs2_xattr_header *)
2381 ((void *)di + inode->i_sb->s_blocksize -
2382 le16_to_cpu(di->i_xattr_inline_size));
2383
ce9c5a54
TM
2384 ret = ocfs2_remove_value_outside(inode, &vb, header,
2385 ref_ci, ref_root_bh);
cf1d6c76
TY
2386
2387 return ret;
2388}
2389
ce9c5a54
TM
2390struct ocfs2_rm_xattr_bucket_para {
2391 struct ocfs2_caching_info *ref_ci;
2392 struct buffer_head *ref_root_bh;
2393};
2394
cf1d6c76 2395static int ocfs2_xattr_block_remove(struct inode *inode,
ce9c5a54
TM
2396 struct buffer_head *blk_bh,
2397 struct ocfs2_caching_info *ref_ci,
2398 struct buffer_head *ref_root_bh)
cf1d6c76
TY
2399{
2400 struct ocfs2_xattr_block *xb;
cf1d6c76 2401 int ret = 0;
4311901d
JB
2402 struct ocfs2_xattr_value_buf vb = {
2403 .vb_bh = blk_bh,
2404 .vb_access = ocfs2_journal_access_xb,
2405 };
ce9c5a54
TM
2406 struct ocfs2_rm_xattr_bucket_para args = {
2407 .ref_ci = ref_ci,
2408 .ref_root_bh = ref_root_bh,
2409 };
cf1d6c76
TY
2410
2411 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
a3944256
TM
2412 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2413 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
ce9c5a54
TM
2414 ret = ocfs2_remove_value_outside(inode, &vb, header,
2415 ref_ci, ref_root_bh);
a3944256 2416 } else
47bca495
TM
2417 ret = ocfs2_iterate_xattr_index_block(inode,
2418 blk_bh,
2419 ocfs2_rm_xattr_cluster,
ce9c5a54 2420 &args);
cf1d6c76
TY
2421
2422 return ret;
2423}
2424
08413899 2425static int ocfs2_xattr_free_block(struct inode *inode,
ce9c5a54
TM
2426 u64 block,
2427 struct ocfs2_caching_info *ref_ci,
2428 struct buffer_head *ref_root_bh)
08413899
TM
2429{
2430 struct inode *xb_alloc_inode;
2431 struct buffer_head *xb_alloc_bh = NULL;
2432 struct buffer_head *blk_bh = NULL;
2433 struct ocfs2_xattr_block *xb;
2434 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2435 handle_t *handle;
2436 int ret = 0;
2437 u64 blk, bg_blkno;
2438 u16 bit;
2439
4ae1d69b 2440 ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
08413899
TM
2441 if (ret < 0) {
2442 mlog_errno(ret);
2443 goto out;
2444 }
2445
ce9c5a54 2446 ret = ocfs2_xattr_block_remove(inode, blk_bh, ref_ci, ref_root_bh);
08413899
TM
2447 if (ret < 0) {
2448 mlog_errno(ret);
2449 goto out;
2450 }
2451
4ae1d69b 2452 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
08413899
TM
2453 blk = le64_to_cpu(xb->xb_blkno);
2454 bit = le16_to_cpu(xb->xb_suballoc_bit);
2455 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2456
2457 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
2458 EXTENT_ALLOC_SYSTEM_INODE,
2459 le16_to_cpu(xb->xb_suballoc_slot));
2460 if (!xb_alloc_inode) {
2461 ret = -ENOMEM;
2462 mlog_errno(ret);
2463 goto out;
2464 }
2465 mutex_lock(&xb_alloc_inode->i_mutex);
2466
2467 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
2468 if (ret < 0) {
2469 mlog_errno(ret);
2470 goto out_mutex;
2471 }
2472
2473 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
2474 if (IS_ERR(handle)) {
2475 ret = PTR_ERR(handle);
2476 mlog_errno(ret);
2477 goto out_unlock;
2478 }
2479
2480 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
2481 bit, bg_blkno, 1);
2482 if (ret < 0)
2483 mlog_errno(ret);
2484
2485 ocfs2_commit_trans(osb, handle);
2486out_unlock:
2487 ocfs2_inode_unlock(xb_alloc_inode, 1);
2488 brelse(xb_alloc_bh);
2489out_mutex:
2490 mutex_unlock(&xb_alloc_inode->i_mutex);
2491 iput(xb_alloc_inode);
2492out:
2493 brelse(blk_bh);
2494 return ret;
2495}
2496
cf1d6c76
TY
2497/*
2498 * ocfs2_xattr_remove()
2499 *
2500 * Free extended attribute resources associated with this inode.
2501 */
2502int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
2503{
cf1d6c76
TY
2504 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2505 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
ce9c5a54
TM
2506 struct ocfs2_refcount_tree *ref_tree = NULL;
2507 struct buffer_head *ref_root_bh = NULL;
2508 struct ocfs2_caching_info *ref_ci = NULL;
cf1d6c76
TY
2509 handle_t *handle;
2510 int ret;
2511
8154da3d
TY
2512 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2513 return 0;
2514
cf1d6c76
TY
2515 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
2516 return 0;
2517
ce9c5a54
TM
2518 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) {
2519 ret = ocfs2_lock_refcount_tree(OCFS2_SB(inode->i_sb),
2520 le64_to_cpu(di->i_refcount_loc),
2521 1, &ref_tree, &ref_root_bh);
2522 if (ret) {
2523 mlog_errno(ret);
2524 goto out;
2525 }
2526 ref_ci = &ref_tree->rf_ci;
2527
2528 }
2529
cf1d6c76 2530 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
ce9c5a54
TM
2531 ret = ocfs2_xattr_ibody_remove(inode, di_bh,
2532 ref_ci, ref_root_bh);
cf1d6c76
TY
2533 if (ret < 0) {
2534 mlog_errno(ret);
2535 goto out;
2536 }
2537 }
cf1d6c76 2538
08413899
TM
2539 if (di->i_xattr_loc) {
2540 ret = ocfs2_xattr_free_block(inode,
ce9c5a54
TM
2541 le64_to_cpu(di->i_xattr_loc),
2542 ref_ci, ref_root_bh);
cf1d6c76
TY
2543 if (ret < 0) {
2544 mlog_errno(ret);
2545 goto out;
2546 }
2547 }
2548
2549 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
2550 OCFS2_INODE_UPDATE_CREDITS);
2551 if (IS_ERR(handle)) {
2552 ret = PTR_ERR(handle);
2553 mlog_errno(ret);
2554 goto out;
2555 }
0cf2f763 2556 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
84008972 2557 OCFS2_JOURNAL_ACCESS_WRITE);
cf1d6c76
TY
2558 if (ret) {
2559 mlog_errno(ret);
2560 goto out_commit;
2561 }
2562
08413899 2563 di->i_xattr_loc = 0;
cf1d6c76
TY
2564
2565 spin_lock(&oi->ip_lock);
2566 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
2567 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2568 spin_unlock(&oi->ip_lock);
2569
2570 ret = ocfs2_journal_dirty(handle, di_bh);
2571 if (ret < 0)
2572 mlog_errno(ret);
2573out_commit:
2574 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
2575out:
ce9c5a54
TM
2576 if (ref_tree)
2577 ocfs2_unlock_refcount_tree(OCFS2_SB(inode->i_sb), ref_tree, 1);
2578 brelse(ref_root_bh);
cf1d6c76
TY
2579 return ret;
2580}
2581
2582static int ocfs2_xattr_has_space_inline(struct inode *inode,
2583 struct ocfs2_dinode *di)
2584{
2585 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2586 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
2587 int free;
2588
2589 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
2590 return 0;
2591
2592 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2593 struct ocfs2_inline_data *idata = &di->id2.i_data;
2594 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
2595 } else if (ocfs2_inode_is_fast_symlink(inode)) {
2596 free = ocfs2_fast_symlink_chars(inode->i_sb) -
2597 le64_to_cpu(di->i_size);
2598 } else {
2599 struct ocfs2_extent_list *el = &di->id2.i_list;
2600 free = (le16_to_cpu(el->l_count) -
2601 le16_to_cpu(el->l_next_free_rec)) *
2602 sizeof(struct ocfs2_extent_rec);
2603 }
2604 if (free >= xattrsize)
2605 return 1;
2606
2607 return 0;
2608}
2609
2610/*
2611 * ocfs2_xattr_ibody_find()
2612 *
2613 * Find extended attribute in inode block and
2614 * fill search info into struct ocfs2_xattr_search.
2615 */
2616static int ocfs2_xattr_ibody_find(struct inode *inode,
2617 int name_index,
2618 const char *name,
2619 struct ocfs2_xattr_search *xs)
2620{
2621 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2622 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2623 int ret;
2624 int has_space = 0;
2625
2626 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2627 return 0;
2628
2629 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2630 down_read(&oi->ip_alloc_sem);
2631 has_space = ocfs2_xattr_has_space_inline(inode, di);
2632 up_read(&oi->ip_alloc_sem);
2633 if (!has_space)
2634 return 0;
2635 }
2636
2637 xs->xattr_bh = xs->inode_bh;
2638 xs->end = (void *)di + inode->i_sb->s_blocksize;
2639 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
2640 xs->header = (struct ocfs2_xattr_header *)
2641 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
2642 else
2643 xs->header = (struct ocfs2_xattr_header *)
2644 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
2645 xs->base = (void *)xs->header;
2646 xs->here = xs->header->xh_entries;
2647
2648 /* Find the named attribute. */
2649 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2650 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2651 if (ret && ret != -ENODATA)
2652 return ret;
2653 xs->not_found = ret;
2654 }
2655
2656 return 0;
2657}
2658
2659/*
2660 * ocfs2_xattr_ibody_set()
2661 *
2662 * Set, replace or remove an extended attribute into inode block.
2663 *
2664 */
2665static int ocfs2_xattr_ibody_set(struct inode *inode,
2666 struct ocfs2_xattr_info *xi,
78f30c31
TM
2667 struct ocfs2_xattr_search *xs,
2668 struct ocfs2_xattr_set_ctxt *ctxt)
cf1d6c76
TY
2669{
2670 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2671 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2672 int ret;
2673
2674 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2675 return -ENOSPC;
2676
2677 down_write(&oi->ip_alloc_sem);
2678 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2679 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2680 ret = -ENOSPC;
2681 goto out;
2682 }
2683 }
2684
78f30c31 2685 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
cf1d6c76
TY
2686 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
2687out:
2688 up_write(&oi->ip_alloc_sem);
2689
2690 return ret;
2691}
2692
2693/*
2694 * ocfs2_xattr_block_find()
2695 *
2696 * Find extended attribute in external block and
2697 * fill search info into struct ocfs2_xattr_search.
2698 */
2699static int ocfs2_xattr_block_find(struct inode *inode,
2700 int name_index,
2701 const char *name,
2702 struct ocfs2_xattr_search *xs)
2703{
2704 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2705 struct buffer_head *blk_bh = NULL;
589dc260 2706 struct ocfs2_xattr_block *xb;
cf1d6c76
TY
2707 int ret = 0;
2708
2709 if (!di->i_xattr_loc)
2710 return ret;
2711
4ae1d69b
JB
2712 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
2713 &blk_bh);
cf1d6c76
TY
2714 if (ret < 0) {
2715 mlog_errno(ret);
2716 return ret;
2717 }
f6087fb7 2718
cf1d6c76 2719 xs->xattr_bh = blk_bh;
4ae1d69b 2720 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
589dc260
TM
2721
2722 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2723 xs->header = &xb->xb_attrs.xb_header;
2724 xs->base = (void *)xs->header;
2725 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
2726 xs->here = xs->header->xh_entries;
2727
2728 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2729 } else
2730 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
2731 name_index,
2732 name, xs);
cf1d6c76 2733
cf1d6c76
TY
2734 if (ret && ret != -ENODATA) {
2735 xs->xattr_bh = NULL;
2736 goto cleanup;
2737 }
2738 xs->not_found = ret;
2739 return 0;
cf1d6c76
TY
2740cleanup:
2741 brelse(blk_bh);
2742
2743 return ret;
2744}
2745
5aea1f0e
TM
2746static int ocfs2_create_xattr_block(handle_t *handle,
2747 struct inode *inode,
2748 struct buffer_head *inode_bh,
2749 struct ocfs2_alloc_context *meta_ac,
a7fe7a3a
TM
2750 struct buffer_head **ret_bh,
2751 int indexed)
5aea1f0e
TM
2752{
2753 int ret;
2754 u16 suballoc_bit_start;
2755 u32 num_got;
2756 u64 first_blkno;
2757 struct ocfs2_dinode *di = (struct ocfs2_dinode *)inode_bh->b_data;
2758 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2759 struct buffer_head *new_bh = NULL;
2760 struct ocfs2_xattr_block *xblk;
2761
2762 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), inode_bh,
2763 OCFS2_JOURNAL_ACCESS_CREATE);
2764 if (ret < 0) {
2765 mlog_errno(ret);
2766 goto end;
2767 }
2768
2769 ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
2770 &suballoc_bit_start, &num_got,
2771 &first_blkno);
2772 if (ret < 0) {
2773 mlog_errno(ret);
2774 goto end;
2775 }
2776
2777 new_bh = sb_getblk(inode->i_sb, first_blkno);
2778 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
2779
2780 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode),
2781 new_bh,
2782 OCFS2_JOURNAL_ACCESS_CREATE);
2783 if (ret < 0) {
2784 mlog_errno(ret);
2785 goto end;
2786 }
2787
2788 /* Initialize ocfs2_xattr_block */
2789 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2790 memset(xblk, 0, inode->i_sb->s_blocksize);
2791 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
b89c5428 2792 xblk->xb_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
5aea1f0e
TM
2793 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
2794 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
2795 xblk->xb_blkno = cpu_to_le64(first_blkno);
2796
a7fe7a3a
TM
2797 if (indexed) {
2798 struct ocfs2_xattr_tree_root *xr = &xblk->xb_attrs.xb_root;
2799 xr->xt_clusters = cpu_to_le32(1);
2800 xr->xt_last_eb_blk = 0;
2801 xr->xt_list.l_tree_depth = 0;
2802 xr->xt_list.l_count = cpu_to_le16(
2803 ocfs2_xattr_recs_per_xb(inode->i_sb));
2804 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2805 xblk->xb_flags = cpu_to_le16(OCFS2_XATTR_INDEXED);
2806 }
2807
5aea1f0e
TM
2808 ret = ocfs2_journal_dirty(handle, new_bh);
2809 if (ret < 0) {
2810 mlog_errno(ret);
2811 goto end;
2812 }
2813 di->i_xattr_loc = cpu_to_le64(first_blkno);
2814 ocfs2_journal_dirty(handle, inode_bh);
2815
2816 *ret_bh = new_bh;
2817 new_bh = NULL;
2818
2819end:
2820 brelse(new_bh);
2821 return ret;
2822}
2823
cf1d6c76
TY
2824/*
2825 * ocfs2_xattr_block_set()
2826 *
2827 * Set, replace or remove an extended attribute into external block.
2828 *
2829 */
2830static int ocfs2_xattr_block_set(struct inode *inode,
2831 struct ocfs2_xattr_info *xi,
78f30c31
TM
2832 struct ocfs2_xattr_search *xs,
2833 struct ocfs2_xattr_set_ctxt *ctxt)
cf1d6c76
TY
2834{
2835 struct buffer_head *new_bh = NULL;
85db90e7 2836 handle_t *handle = ctxt->handle;
cf1d6c76 2837 struct ocfs2_xattr_block *xblk = NULL;
cf1d6c76
TY
2838 int ret;
2839
2840 if (!xs->xattr_bh) {
5aea1f0e 2841 ret = ocfs2_create_xattr_block(handle, inode, xs->inode_bh,
a7fe7a3a 2842 ctxt->meta_ac, &new_bh, 0);
5aea1f0e 2843 if (ret) {
cf1d6c76 2844 mlog_errno(ret);
85db90e7 2845 goto end;
cf1d6c76
TY
2846 }
2847
cf1d6c76 2848 xs->xattr_bh = new_bh;
5aea1f0e 2849 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
cf1d6c76
TY
2850 xs->header = &xblk->xb_attrs.xb_header;
2851 xs->base = (void *)xs->header;
2852 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2853 xs->here = xs->header->xh_entries;
01225596
TM
2854 } else
2855 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2856
2857 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
2858 /* Set extended attribute into external block */
78f30c31
TM
2859 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
2860 OCFS2_HAS_XATTR_FL);
01225596
TM
2861 if (!ret || ret != -ENOSPC)
2862 goto end;
2863
78f30c31 2864 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
01225596
TM
2865 if (ret)
2866 goto end;
cf1d6c76
TY
2867 }
2868
78f30c31 2869 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
01225596
TM
2870
2871end:
cf1d6c76
TY
2872
2873 return ret;
2874}
2875
78f30c31
TM
2876/* Check whether the new xattr can be inserted into the inode. */
2877static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
2878 struct ocfs2_xattr_info *xi,
2879 struct ocfs2_xattr_search *xs)
2880{
78f30c31
TM
2881 struct ocfs2_xattr_entry *last;
2882 int free, i;
2883 size_t min_offs = xs->end - xs->base;
2884
2885 if (!xs->header)
2886 return 0;
2887
2888 last = xs->header->xh_entries;
2889
2890 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
2891 size_t offs = le16_to_cpu(last->xe_name_offset);
2892 if (offs < min_offs)
2893 min_offs = offs;
2894 last += 1;
2895 }
2896
4442f518 2897 free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
78f30c31
TM
2898 if (free < 0)
2899 return 0;
2900
2901 BUG_ON(!xs->not_found);
2902
199799a3 2903 if (free >= (sizeof(struct ocfs2_xattr_entry) + namevalue_size_xi(xi)))
78f30c31
TM
2904 return 1;
2905
2906 return 0;
2907}
2908
2909static int ocfs2_calc_xattr_set_need(struct inode *inode,
2910 struct ocfs2_dinode *di,
2911 struct ocfs2_xattr_info *xi,
2912 struct ocfs2_xattr_search *xis,
2913 struct ocfs2_xattr_search *xbs,
2914 int *clusters_need,
85db90e7
TM
2915 int *meta_need,
2916 int *credits_need)
78f30c31
TM
2917{
2918 int ret = 0, old_in_xb = 0;
85db90e7 2919 int clusters_add = 0, meta_add = 0, credits = 0;
78f30c31
TM
2920 struct buffer_head *bh = NULL;
2921 struct ocfs2_xattr_block *xb = NULL;
2922 struct ocfs2_xattr_entry *xe = NULL;
2923 struct ocfs2_xattr_value_root *xv = NULL;
2924 char *base = NULL;
2925 int name_offset, name_len = 0;
2926 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
6b240ff6 2927 xi->xi_value_len);
78f30c31
TM
2928 u64 value_size;
2929
71d548a6
TM
2930 /*
2931 * Calculate the clusters we need to write.
2932 * No matter whether we replace an old one or add a new one,
2933 * we need this for writing.
2934 */
6b240ff6 2935 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
71d548a6
TM
2936 credits += new_clusters *
2937 ocfs2_clusters_to_blocks(inode->i_sb, 1);
2938
78f30c31 2939 if (xis->not_found && xbs->not_found) {
85db90e7
TM
2940 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2941
6b240ff6 2942 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
78f30c31 2943 clusters_add += new_clusters;
85db90e7
TM
2944 credits += ocfs2_calc_extend_credits(inode->i_sb,
2945 &def_xv.xv.xr_list,
2946 new_clusters);
2947 }
78f30c31
TM
2948
2949 goto meta_guess;
2950 }
2951
2952 if (!xis->not_found) {
2953 xe = xis->here;
2954 name_offset = le16_to_cpu(xe->xe_name_offset);
2955 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2956 base = xis->base;
85db90e7 2957 credits += OCFS2_INODE_UPDATE_CREDITS;
78f30c31 2958 } else {
970e4936 2959 int i, block_off = 0;
78f30c31
TM
2960 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2961 xe = xbs->here;
2962 name_offset = le16_to_cpu(xe->xe_name_offset);
2963 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2964 i = xbs->here - xbs->header->xh_entries;
2965 old_in_xb = 1;
2966
2967 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
fd68a894 2968 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
78f30c31
TM
2969 bucket_xh(xbs->bucket),
2970 i, &block_off,
2971 &name_offset);
2972 base = bucket_block(xbs->bucket, block_off);
85db90e7
TM
2973 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2974 } else {
78f30c31 2975 base = xbs->base;
85db90e7
TM
2976 credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
2977 }
2978 }
2979
2980 /*
2981 * delete a xattr doesn't need metadata and cluster allocation.
2982 * so just calculate the credits and return.
2983 *
2984 * The credits for removing the value tree will be extended
2985 * by ocfs2_remove_extent itself.
2986 */
6b240ff6 2987 if (!xi->xi_value) {
85db90e7 2988 if (!ocfs2_xattr_is_local(xe))
a90714c1 2989 credits += ocfs2_remove_extent_credits(inode->i_sb);
85db90e7
TM
2990
2991 goto out;
78f30c31
TM
2992 }
2993
2994 /* do cluster allocation guess first. */
2995 value_size = le64_to_cpu(xe->xe_value_size);
2996
2997 if (old_in_xb) {
2998 /*
2999 * In xattr set, we always try to set the xe in inode first,
3000 * so if it can be inserted into inode successfully, the old
3001 * one will be removed from the xattr block, and this xattr
3002 * will be inserted into inode as a new xattr in inode.
3003 */
3004 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
3005 clusters_add += new_clusters;
a90714c1 3006 credits += ocfs2_remove_extent_credits(inode->i_sb) +
85db90e7
TM
3007 OCFS2_INODE_UPDATE_CREDITS;
3008 if (!ocfs2_xattr_is_local(xe))
3009 credits += ocfs2_calc_extend_credits(
3010 inode->i_sb,
3011 &def_xv.xv.xr_list,
3012 new_clusters);
78f30c31
TM
3013 goto out;
3014 }
3015 }
3016
6b240ff6 3017 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
78f30c31
TM
3018 /* the new values will be stored outside. */
3019 u32 old_clusters = 0;
3020
3021 if (!ocfs2_xattr_is_local(xe)) {
3022 old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
3023 value_size);
3024 xv = (struct ocfs2_xattr_value_root *)
3025 (base + name_offset + name_len);
97aff52a 3026 value_size = OCFS2_XATTR_ROOT_SIZE;
78f30c31
TM
3027 } else
3028 xv = &def_xv.xv;
3029
85db90e7 3030 if (old_clusters >= new_clusters) {
a90714c1 3031 credits += ocfs2_remove_extent_credits(inode->i_sb);
78f30c31 3032 goto out;
85db90e7 3033 } else {
78f30c31
TM
3034 meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
3035 clusters_add += new_clusters - old_clusters;
85db90e7
TM
3036 credits += ocfs2_calc_extend_credits(inode->i_sb,
3037 &xv->xr_list,
3038 new_clusters -
3039 old_clusters);
97aff52a
TM
3040 if (value_size >= OCFS2_XATTR_ROOT_SIZE)
3041 goto out;
78f30c31
TM
3042 }
3043 } else {
3044 /*
3045 * Now the new value will be stored inside. So if the new
3046 * value is smaller than the size of value root or the old
3047 * value, we don't need any allocation, otherwise we have
3048 * to guess metadata allocation.
3049 */
6b240ff6
JB
3050 if ((ocfs2_xattr_is_local(xe) &&
3051 (value_size >= xi->xi_value_len)) ||
78f30c31 3052 (!ocfs2_xattr_is_local(xe) &&
6b240ff6 3053 OCFS2_XATTR_ROOT_SIZE >= xi->xi_value_len))
78f30c31
TM
3054 goto out;
3055 }
3056
3057meta_guess:
3058 /* calculate metadata allocation. */
3059 if (di->i_xattr_loc) {
3060 if (!xbs->xattr_bh) {
4ae1d69b
JB
3061 ret = ocfs2_read_xattr_block(inode,
3062 le64_to_cpu(di->i_xattr_loc),
3063 &bh);
78f30c31
TM
3064 if (ret) {
3065 mlog_errno(ret);
3066 goto out;
3067 }
3068
3069 xb = (struct ocfs2_xattr_block *)bh->b_data;
3070 } else
3071 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
3072
90cb546c
TM
3073 /*
3074 * If there is already an xattr tree, good, we can calculate
3075 * like other b-trees. Otherwise we may have the chance of
3076 * create a tree, the credit calculation is borrowed from
3077 * ocfs2_calc_extend_credits with root_el = NULL. And the
3078 * new tree will be cluster based, so no meta is needed.
3079 */
78f30c31
TM
3080 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
3081 struct ocfs2_extent_list *el =
3082 &xb->xb_attrs.xb_root.xt_list;
3083 meta_add += ocfs2_extend_meta_needed(el);
85db90e7
TM
3084 credits += ocfs2_calc_extend_credits(inode->i_sb,
3085 el, 1);
90cb546c
TM
3086 } else
3087 credits += OCFS2_SUBALLOC_ALLOC + 1;
78f30c31
TM
3088
3089 /*
3090 * This cluster will be used either for new bucket or for
3091 * new xattr block.
3092 * If the cluster size is the same as the bucket size, one
3093 * more is needed since we may need to extend the bucket
3094 * also.
3095 */
3096 clusters_add += 1;
85db90e7 3097 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
78f30c31 3098 if (OCFS2_XATTR_BUCKET_SIZE ==
85db90e7
TM
3099 OCFS2_SB(inode->i_sb)->s_clustersize) {
3100 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
78f30c31 3101 clusters_add += 1;
85db90e7
TM
3102 }
3103 } else {
78f30c31 3104 meta_add += 1;
85db90e7
TM
3105 credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
3106 }
78f30c31
TM
3107out:
3108 if (clusters_need)
3109 *clusters_need = clusters_add;
3110 if (meta_need)
3111 *meta_need = meta_add;
85db90e7
TM
3112 if (credits_need)
3113 *credits_need = credits;
78f30c31
TM
3114 brelse(bh);
3115 return ret;
3116}
3117
3118static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
3119 struct ocfs2_dinode *di,
3120 struct ocfs2_xattr_info *xi,
3121 struct ocfs2_xattr_search *xis,
3122 struct ocfs2_xattr_search *xbs,
85db90e7 3123 struct ocfs2_xattr_set_ctxt *ctxt,
492a8a33 3124 int extra_meta,
85db90e7 3125 int *credits)
78f30c31
TM
3126{
3127 int clusters_add, meta_add, ret;
3128 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3129
3130 memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
3131
3132 ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
3133
3134 ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
85db90e7 3135 &clusters_add, &meta_add, credits);
78f30c31
TM
3136 if (ret) {
3137 mlog_errno(ret);
3138 return ret;
3139 }
3140
492a8a33 3141 meta_add += extra_meta;
85db90e7 3142 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
6b240ff6 3143 "credits = %d\n", xi->xi_name, meta_add, clusters_add, *credits);
78f30c31
TM
3144
3145 if (meta_add) {
3146 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
3147 &ctxt->meta_ac);
3148 if (ret) {
3149 mlog_errno(ret);
3150 goto out;
3151 }
3152 }
3153
3154 if (clusters_add) {
3155 ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
3156 if (ret)
3157 mlog_errno(ret);
3158 }
3159out:
3160 if (ret) {
3161 if (ctxt->meta_ac) {
3162 ocfs2_free_alloc_context(ctxt->meta_ac);
3163 ctxt->meta_ac = NULL;
3164 }
3165
3166 /*
3167 * We cannot have an error and a non null ctxt->data_ac.
3168 */
3169 }
3170
3171 return ret;
3172}
3173
85db90e7
TM
3174static int __ocfs2_xattr_set_handle(struct inode *inode,
3175 struct ocfs2_dinode *di,
3176 struct ocfs2_xattr_info *xi,
3177 struct ocfs2_xattr_search *xis,
3178 struct ocfs2_xattr_search *xbs,
3179 struct ocfs2_xattr_set_ctxt *ctxt)
3180{
9f868f16 3181 int ret = 0, credits, old_found;
85db90e7 3182
6b240ff6 3183 if (!xi->xi_value) {
85db90e7
TM
3184 /* Remove existing extended attribute */
3185 if (!xis->not_found)
3186 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3187 else if (!xbs->not_found)
3188 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3189 } else {
3190 /* We always try to set extended attribute into inode first*/
3191 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3192 if (!ret && !xbs->not_found) {
3193 /*
3194 * If succeed and that extended attribute existing in
3195 * external block, then we will remove it.
3196 */
6b240ff6
JB
3197 xi->xi_value = NULL;
3198 xi->xi_value_len = 0;
85db90e7 3199
9f868f16 3200 old_found = xis->not_found;
85db90e7
TM
3201 xis->not_found = -ENODATA;
3202 ret = ocfs2_calc_xattr_set_need(inode,
3203 di,
3204 xi,
3205 xis,
3206 xbs,
3207 NULL,
3208 NULL,
3209 &credits);
9f868f16 3210 xis->not_found = old_found;
85db90e7
TM
3211 if (ret) {
3212 mlog_errno(ret);
3213 goto out;
3214 }
3215
3216 ret = ocfs2_extend_trans(ctxt->handle, credits +
3217 ctxt->handle->h_buffer_credits);
3218 if (ret) {
3219 mlog_errno(ret);
3220 goto out;
3221 }
3222 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3223 } else if (ret == -ENOSPC) {
3224 if (di->i_xattr_loc && !xbs->xattr_bh) {
3225 ret = ocfs2_xattr_block_find(inode,
6b240ff6
JB
3226 xi->xi_name_index,
3227 xi->xi_name, xbs);
85db90e7
TM
3228 if (ret)
3229 goto out;
3230
9f868f16 3231 old_found = xis->not_found;
85db90e7
TM
3232 xis->not_found = -ENODATA;
3233 ret = ocfs2_calc_xattr_set_need(inode,
3234 di,
3235 xi,
3236 xis,
3237 xbs,
3238 NULL,
3239 NULL,
3240 &credits);
9f868f16 3241 xis->not_found = old_found;
85db90e7
TM
3242 if (ret) {
3243 mlog_errno(ret);
3244 goto out;
3245 }
3246
3247 ret = ocfs2_extend_trans(ctxt->handle, credits +
3248 ctxt->handle->h_buffer_credits);
3249 if (ret) {
3250 mlog_errno(ret);
3251 goto out;
3252 }
3253 }
3254 /*
3255 * If no space in inode, we will set extended attribute
3256 * into external block.
3257 */
3258 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3259 if (ret)
3260 goto out;
3261 if (!xis->not_found) {
3262 /*
3263 * If succeed and that extended attribute
3264 * existing in inode, we will remove it.
3265 */
6b240ff6
JB
3266 xi->xi_value = NULL;
3267 xi->xi_value_len = 0;
85db90e7
TM
3268 xbs->not_found = -ENODATA;
3269 ret = ocfs2_calc_xattr_set_need(inode,
3270 di,
3271 xi,
3272 xis,
3273 xbs,
3274 NULL,
3275 NULL,
3276 &credits);
3277 if (ret) {
3278 mlog_errno(ret);
3279 goto out;
3280 }
3281
3282 ret = ocfs2_extend_trans(ctxt->handle, credits +
3283 ctxt->handle->h_buffer_credits);
3284 if (ret) {
3285 mlog_errno(ret);
3286 goto out;
3287 }
3288 ret = ocfs2_xattr_ibody_set(inode, xi,
3289 xis, ctxt);
3290 }
3291 }
3292 }
3293
4b3f6209
TM
3294 if (!ret) {
3295 /* Update inode ctime. */
0cf2f763 3296 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
89a907af
TM
3297 xis->inode_bh,
3298 OCFS2_JOURNAL_ACCESS_WRITE);
4b3f6209
TM
3299 if (ret) {
3300 mlog_errno(ret);
3301 goto out;
3302 }
3303
3304 inode->i_ctime = CURRENT_TIME;
3305 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
3306 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
3307 ocfs2_journal_dirty(ctxt->handle, xis->inode_bh);
3308 }
85db90e7
TM
3309out:
3310 return ret;
3311}
3312
6c3faba4
TY
3313/*
3314 * This function only called duing creating inode
3315 * for init security/acl xattrs of the new inode.
008aafaf 3316 * All transanction credits have been reserved in mknod.
6c3faba4
TY
3317 */
3318int ocfs2_xattr_set_handle(handle_t *handle,
3319 struct inode *inode,
3320 struct buffer_head *di_bh,
3321 int name_index,
3322 const char *name,
3323 const void *value,
3324 size_t value_len,
3325 int flags,
3326 struct ocfs2_alloc_context *meta_ac,
3327 struct ocfs2_alloc_context *data_ac)
3328{
3329 struct ocfs2_dinode *di;
3330 int ret;
3331
3332 struct ocfs2_xattr_info xi = {
6b240ff6
JB
3333 .xi_name_index = name_index,
3334 .xi_name = name,
18853b95 3335 .xi_name_len = strlen(name),
6b240ff6
JB
3336 .xi_value = value,
3337 .xi_value_len = value_len,
6c3faba4
TY
3338 };
3339
3340 struct ocfs2_xattr_search xis = {
3341 .not_found = -ENODATA,
3342 };
3343
3344 struct ocfs2_xattr_search xbs = {
3345 .not_found = -ENODATA,
3346 };
3347
3348 struct ocfs2_xattr_set_ctxt ctxt = {
3349 .handle = handle,
3350 .meta_ac = meta_ac,
3351 .data_ac = data_ac,
3352 };
3353
3354 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3355 return -EOPNOTSUPP;
3356
008aafaf
TY
3357 /*
3358 * In extreme situation, may need xattr bucket when
3359 * block size is too small. And we have already reserved
3360 * the credits for bucket in mknod.
3361 */
3362 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) {
3363 xbs.bucket = ocfs2_xattr_bucket_new(inode);
3364 if (!xbs.bucket) {
3365 mlog_errno(-ENOMEM);
3366 return -ENOMEM;
3367 }
3368 }
3369
6c3faba4
TY
3370 xis.inode_bh = xbs.inode_bh = di_bh;
3371 di = (struct ocfs2_dinode *)di_bh->b_data;
3372
3373 down_write(&OCFS2_I(inode)->ip_xattr_sem);
3374
3375 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3376 if (ret)
3377 goto cleanup;
3378 if (xis.not_found) {
3379 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3380 if (ret)
3381 goto cleanup;
3382 }
3383
3384 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3385
3386cleanup:
3387 up_write(&OCFS2_I(inode)->ip_xattr_sem);
3388 brelse(xbs.xattr_bh);
008aafaf 3389 ocfs2_xattr_bucket_free(xbs.bucket);
6c3faba4
TY
3390
3391 return ret;
3392}
3393
cf1d6c76
TY
3394/*
3395 * ocfs2_xattr_set()
3396 *
3397 * Set, replace or remove an extended attribute for this inode.
3398 * value is NULL to remove an existing extended attribute, else either
3399 * create or replace an extended attribute.
3400 */
3401int ocfs2_xattr_set(struct inode *inode,
3402 int name_index,
3403 const char *name,
3404 const void *value,
3405 size_t value_len,
3406 int flags)
3407{
3408 struct buffer_head *di_bh = NULL;
3409 struct ocfs2_dinode *di;
492a8a33 3410 int ret, credits, ref_meta = 0, ref_credits = 0;
78f30c31 3411 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
85db90e7 3412 struct inode *tl_inode = osb->osb_tl_inode;
78f30c31 3413 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
492a8a33 3414 struct ocfs2_refcount_tree *ref_tree = NULL;
cf1d6c76
TY
3415
3416 struct ocfs2_xattr_info xi = {
6b240ff6
JB
3417 .xi_name_index = name_index,
3418 .xi_name = name,
18853b95 3419 .xi_name_len = strlen(name),
6b240ff6
JB
3420 .xi_value = value,
3421 .xi_value_len = value_len,
cf1d6c76
TY
3422 };
3423
3424 struct ocfs2_xattr_search xis = {
3425 .not_found = -ENODATA,
3426 };
3427
3428 struct ocfs2_xattr_search xbs = {
3429 .not_found = -ENODATA,
3430 };
3431
8154da3d
TY
3432 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3433 return -EOPNOTSUPP;
3434
ba937127
JB
3435 /*
3436 * Only xbs will be used on indexed trees. xis doesn't need a
3437 * bucket.
3438 */
3439 xbs.bucket = ocfs2_xattr_bucket_new(inode);
3440 if (!xbs.bucket) {
3441 mlog_errno(-ENOMEM);
3442 return -ENOMEM;
3443 }
3444
cf1d6c76
TY
3445 ret = ocfs2_inode_lock(inode, &di_bh, 1);
3446 if (ret < 0) {
3447 mlog_errno(ret);
ba937127 3448 goto cleanup_nolock;
cf1d6c76
TY
3449 }
3450 xis.inode_bh = xbs.inode_bh = di_bh;
3451 di = (struct ocfs2_dinode *)di_bh->b_data;
3452
3453 down_write(&OCFS2_I(inode)->ip_xattr_sem);
3454 /*
3455 * Scan inode and external block to find the same name
3456 * extended attribute and collect search infomation.
3457 */
3458 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3459 if (ret)
3460 goto cleanup;
3461 if (xis.not_found) {
3462 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3463 if (ret)
3464 goto cleanup;
3465 }
3466
3467 if (xis.not_found && xbs.not_found) {
3468 ret = -ENODATA;
3469 if (flags & XATTR_REPLACE)
3470 goto cleanup;
3471 ret = 0;
3472 if (!value)
3473 goto cleanup;
3474 } else {
3475 ret = -EEXIST;
3476 if (flags & XATTR_CREATE)
3477 goto cleanup;
3478 }
3479
492a8a33
TM
3480 /* Check whether the value is refcounted and do some prepartion. */
3481 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL &&
3482 (!xis.not_found || !xbs.not_found)) {
3483 ret = ocfs2_prepare_refcount_xattr(inode, di, &xi,
3484 &xis, &xbs, &ref_tree,
3485 &ref_meta, &ref_credits);
3486 if (ret) {
3487 mlog_errno(ret);
3488 goto cleanup;
3489 }
3490 }
85db90e7
TM
3491
3492 mutex_lock(&tl_inode->i_mutex);
3493
3494 if (ocfs2_truncate_log_needs_flush(osb)) {
3495 ret = __ocfs2_flush_truncate_log(osb);
3496 if (ret < 0) {
3497 mutex_unlock(&tl_inode->i_mutex);
3498 mlog_errno(ret);
3499 goto cleanup;
3500 }
3501 }
3502 mutex_unlock(&tl_inode->i_mutex);
3503
3504 ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
492a8a33 3505 &xbs, &ctxt, ref_meta, &credits);
78f30c31
TM
3506 if (ret) {
3507 mlog_errno(ret);
3508 goto cleanup;
3509 }
3510
4b3f6209
TM
3511 /* we need to update inode's ctime field, so add credit for it. */
3512 credits += OCFS2_INODE_UPDATE_CREDITS;
492a8a33 3513 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
85db90e7
TM
3514 if (IS_ERR(ctxt.handle)) {
3515 ret = PTR_ERR(ctxt.handle);
3516 mlog_errno(ret);
3517 goto cleanup;
cf1d6c76 3518 }
85db90e7
TM
3519
3520 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3521
3522 ocfs2_commit_trans(osb, ctxt.handle);
3523
78f30c31
TM
3524 if (ctxt.data_ac)
3525 ocfs2_free_alloc_context(ctxt.data_ac);
3526 if (ctxt.meta_ac)
3527 ocfs2_free_alloc_context(ctxt.meta_ac);
3528 if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
3529 ocfs2_schedule_truncate_log_flush(osb, 1);
3530 ocfs2_run_deallocs(osb, &ctxt.dealloc);
8b2c0dba 3531
cf1d6c76 3532cleanup:
492a8a33
TM
3533 if (ref_tree)
3534 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
cf1d6c76 3535 up_write(&OCFS2_I(inode)->ip_xattr_sem);
8b2c0dba
TM
3536 if (!value && !ret) {
3537 ret = ocfs2_try_remove_refcount_tree(inode, di_bh);
3538 if (ret)
3539 mlog_errno(ret);
3540 }
cf1d6c76 3541 ocfs2_inode_unlock(inode, 1);
ba937127 3542cleanup_nolock:
cf1d6c76
TY
3543 brelse(di_bh);
3544 brelse(xbs.xattr_bh);
ba937127 3545 ocfs2_xattr_bucket_free(xbs.bucket);
cf1d6c76
TY
3546
3547 return ret;
3548}
3549
0c044f0b
TM
3550/*
3551 * Find the xattr extent rec which may contains name_hash.
3552 * e_cpos will be the first name hash of the xattr rec.
3553 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
3554 */
3555static int ocfs2_xattr_get_rec(struct inode *inode,
3556 u32 name_hash,
3557 u64 *p_blkno,
3558 u32 *e_cpos,
3559 u32 *num_clusters,
3560 struct ocfs2_extent_list *el)
3561{
3562 int ret = 0, i;
3563 struct buffer_head *eb_bh = NULL;
3564 struct ocfs2_extent_block *eb;
3565 struct ocfs2_extent_rec *rec = NULL;
3566 u64 e_blkno = 0;
3567
3568 if (el->l_tree_depth) {
facdb77f
JB
3569 ret = ocfs2_find_leaf(INODE_CACHE(inode), el, name_hash,
3570 &eb_bh);
0c044f0b
TM
3571 if (ret) {
3572 mlog_errno(ret);
3573 goto out;
3574 }
3575
3576 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
3577 el = &eb->h_list;
3578
3579 if (el->l_tree_depth) {
3580 ocfs2_error(inode->i_sb,
3581 "Inode %lu has non zero tree depth in "
3582 "xattr tree block %llu\n", inode->i_ino,
3583 (unsigned long long)eb_bh->b_blocknr);
3584 ret = -EROFS;
3585 goto out;
3586 }
3587 }
3588
3589 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
3590 rec = &el->l_recs[i];
3591
3592 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
3593 e_blkno = le64_to_cpu(rec->e_blkno);
3594 break;
3595 }
3596 }
3597
3598 if (!e_blkno) {
3599 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
3600 "record (%u, %u, 0) in xattr", inode->i_ino,
3601 le32_to_cpu(rec->e_cpos),
3602 ocfs2_rec_clusters(el, rec));
3603 ret = -EROFS;
3604 goto out;
3605 }
3606
3607 *p_blkno = le64_to_cpu(rec->e_blkno);
3608 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
3609 if (e_cpos)
3610 *e_cpos = le32_to_cpu(rec->e_cpos);
3611out:
3612 brelse(eb_bh);
3613 return ret;
3614}
3615
3616typedef int (xattr_bucket_func)(struct inode *inode,
3617 struct ocfs2_xattr_bucket *bucket,
3618 void *para);
3619
589dc260 3620static int ocfs2_find_xe_in_bucket(struct inode *inode,
e2356a3f 3621 struct ocfs2_xattr_bucket *bucket,
589dc260
TM
3622 int name_index,
3623 const char *name,
3624 u32 name_hash,
3625 u16 *xe_index,
3626 int *found)
3627{
3628 int i, ret = 0, cmp = 1, block_off, new_offset;
e2356a3f 3629 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
589dc260
TM
3630 size_t name_len = strlen(name);
3631 struct ocfs2_xattr_entry *xe = NULL;
589dc260
TM
3632 char *xe_name;
3633
3634 /*
3635 * We don't use binary search in the bucket because there
3636 * may be multiple entries with the same name hash.
3637 */
3638 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3639 xe = &xh->xh_entries[i];
3640
3641 if (name_hash > le32_to_cpu(xe->xe_name_hash))
3642 continue;
3643 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
3644 break;
3645
3646 cmp = name_index - ocfs2_xattr_get_type(xe);
3647 if (!cmp)
3648 cmp = name_len - xe->xe_name_len;
3649 if (cmp)
3650 continue;
3651
fd68a894 3652 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
589dc260
TM
3653 xh,
3654 i,
3655 &block_off,
3656 &new_offset);
3657 if (ret) {
3658 mlog_errno(ret);
3659 break;
3660 }
3661
970e4936 3662
e2356a3f
JB
3663 xe_name = bucket_block(bucket, block_off) + new_offset;
3664 if (!memcmp(name, xe_name, name_len)) {
589dc260
TM
3665 *xe_index = i;
3666 *found = 1;
3667 ret = 0;
3668 break;
3669 }
3670 }
3671
3672 return ret;
3673}
3674
3675/*
3676 * Find the specified xattr entry in a series of buckets.
3677 * This series start from p_blkno and last for num_clusters.
3678 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
3679 * the num of the valid buckets.
3680 *
3681 * Return the buffer_head this xattr should reside in. And if the xattr's
3682 * hash is in the gap of 2 buckets, return the lower bucket.
3683 */
3684static int ocfs2_xattr_bucket_find(struct inode *inode,
3685 int name_index,
3686 const char *name,
3687 u32 name_hash,
3688 u64 p_blkno,
3689 u32 first_hash,
3690 u32 num_clusters,
3691 struct ocfs2_xattr_search *xs)
3692{
3693 int ret, found = 0;
589dc260
TM
3694 struct ocfs2_xattr_header *xh = NULL;
3695 struct ocfs2_xattr_entry *xe = NULL;
3696 u16 index = 0;
3697 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3698 int low_bucket = 0, bucket, high_bucket;
e2356a3f 3699 struct ocfs2_xattr_bucket *search;
589dc260 3700 u32 last_hash;
e2356a3f 3701 u64 blkno, lower_blkno = 0;
589dc260 3702
e2356a3f
JB
3703 search = ocfs2_xattr_bucket_new(inode);
3704 if (!search) {
3705 ret = -ENOMEM;
3706 mlog_errno(ret);
3707 goto out;
3708 }
3709
3710 ret = ocfs2_read_xattr_bucket(search, p_blkno);
589dc260
TM
3711 if (ret) {
3712 mlog_errno(ret);
3713 goto out;
3714 }
3715
e2356a3f 3716 xh = bucket_xh(search);
589dc260 3717 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
589dc260 3718 while (low_bucket <= high_bucket) {
e2356a3f 3719 ocfs2_xattr_bucket_relse(search);
589dc260 3720
e2356a3f 3721 bucket = (low_bucket + high_bucket) / 2;
589dc260 3722 blkno = p_blkno + bucket * blk_per_bucket;
e2356a3f 3723 ret = ocfs2_read_xattr_bucket(search, blkno);
589dc260
TM
3724 if (ret) {
3725 mlog_errno(ret);
3726 goto out;
3727 }
3728
e2356a3f 3729 xh = bucket_xh(search);
589dc260
TM
3730 xe = &xh->xh_entries[0];
3731 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
3732 high_bucket = bucket - 1;
3733 continue;
3734 }
3735
3736 /*
3737 * Check whether the hash of the last entry in our
5a095611
TM
3738 * bucket is larger than the search one. for an empty
3739 * bucket, the last one is also the first one.
589dc260 3740 */
5a095611
TM
3741 if (xh->xh_count)
3742 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
3743
589dc260
TM
3744 last_hash = le32_to_cpu(xe->xe_name_hash);
3745
e2356a3f
JB
3746 /* record lower_blkno which may be the insert place. */
3747 lower_blkno = blkno;
589dc260
TM
3748
3749 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
3750 low_bucket = bucket + 1;
3751 continue;
3752 }
3753
3754 /* the searched xattr should reside in this bucket if exists. */
e2356a3f 3755 ret = ocfs2_find_xe_in_bucket(inode, search,
589dc260
TM
3756 name_index, name, name_hash,
3757 &index, &found);
3758 if (ret) {
3759 mlog_errno(ret);
3760 goto out;
3761 }
3762 break;
3763 }
3764
3765 /*
3766 * Record the bucket we have found.
3767 * When the xattr's hash value is in the gap of 2 buckets, we will
3768 * always set it to the previous bucket.
3769 */
e2356a3f
JB
3770 if (!lower_blkno)
3771 lower_blkno = p_blkno;
3772
3773 /* This should be in cache - we just read it during the search */
3774 ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
3775 if (ret) {
3776 mlog_errno(ret);
3777 goto out;
589dc260 3778 }
589dc260 3779
ba937127
JB
3780 xs->header = bucket_xh(xs->bucket);
3781 xs->base = bucket_block(xs->bucket, 0);
589dc260
TM
3782 xs->end = xs->base + inode->i_sb->s_blocksize;
3783
3784 if (found) {
589dc260
TM
3785 xs->here = &xs->header->xh_entries[index];
3786 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
ba937127 3787 (unsigned long long)bucket_blkno(xs->bucket), index);
589dc260
TM
3788 } else
3789 ret = -ENODATA;
3790
3791out:
e2356a3f 3792 ocfs2_xattr_bucket_free(search);
589dc260
TM
3793 return ret;
3794}
3795
3796static int ocfs2_xattr_index_block_find(struct inode *inode,
3797 struct buffer_head *root_bh,
3798 int name_index,
3799 const char *name,
3800 struct ocfs2_xattr_search *xs)
3801{
3802 int ret;
3803 struct ocfs2_xattr_block *xb =
3804 (struct ocfs2_xattr_block *)root_bh->b_data;
3805 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3806 struct ocfs2_extent_list *el = &xb_root->xt_list;
3807 u64 p_blkno = 0;
3808 u32 first_hash, num_clusters = 0;
2057e5c6 3809 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
589dc260
TM
3810
3811 if (le16_to_cpu(el->l_next_free_rec) == 0)
3812 return -ENODATA;
3813
3814 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
3815 name, name_hash, name_index);
3816
3817 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
3818 &num_clusters, el);
3819 if (ret) {
3820 mlog_errno(ret);
3821 goto out;
3822 }
3823
3824 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
3825
3826 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
de29c085
MF
3827 "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
3828 first_hash);
589dc260
TM
3829
3830 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
3831 p_blkno, first_hash, num_clusters, xs);
3832
3833out:
3834 return ret;
3835}
3836
0c044f0b
TM
3837static int ocfs2_iterate_xattr_buckets(struct inode *inode,
3838 u64 blkno,
3839 u32 clusters,
3840 xattr_bucket_func *func,
3841 void *para)
3842{
6dde41d9 3843 int i, ret = 0;
0c044f0b
TM
3844 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
3845 u32 num_buckets = clusters * bpc;
ba937127 3846 struct ocfs2_xattr_bucket *bucket;
0c044f0b 3847
ba937127
JB
3848 bucket = ocfs2_xattr_bucket_new(inode);
3849 if (!bucket) {
3850 mlog_errno(-ENOMEM);
3851 return -ENOMEM;
3852 }
0c044f0b
TM
3853
3854 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
de29c085 3855 clusters, (unsigned long long)blkno);
0c044f0b 3856
ba937127
JB
3857 for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
3858 ret = ocfs2_read_xattr_bucket(bucket, blkno);
0c044f0b
TM
3859 if (ret) {
3860 mlog_errno(ret);
ba937127 3861 break;
0c044f0b
TM
3862 }
3863
0c044f0b
TM
3864 /*
3865 * The real bucket num in this series of blocks is stored
3866 * in the 1st bucket.
3867 */
3868 if (i == 0)
ba937127 3869 num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
0c044f0b 3870
de29c085
MF
3871 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
3872 (unsigned long long)blkno,
ba937127 3873 le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
0c044f0b 3874 if (func) {
ba937127 3875 ret = func(inode, bucket, para);
a46fa684 3876 if (ret && ret != -ERANGE)
0c044f0b 3877 mlog_errno(ret);
ba937127 3878 /* Fall through to bucket_relse() */
0c044f0b
TM
3879 }
3880
ba937127
JB
3881 ocfs2_xattr_bucket_relse(bucket);
3882 if (ret)
3883 break;
0c044f0b
TM
3884 }
3885
ba937127 3886 ocfs2_xattr_bucket_free(bucket);
0c044f0b
TM
3887 return ret;
3888}
3889
3890struct ocfs2_xattr_tree_list {
3891 char *buffer;
3892 size_t buffer_size;
936b8834 3893 size_t result;
0c044f0b
TM
3894};
3895
fd68a894 3896static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
0c044f0b
TM
3897 struct ocfs2_xattr_header *xh,
3898 int index,
3899 int *block_off,
3900 int *new_offset)
3901{
3902 u16 name_offset;
3903
3904 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
3905 return -EINVAL;
3906
3907 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
3908
fd68a894
TM
3909 *block_off = name_offset >> sb->s_blocksize_bits;
3910 *new_offset = name_offset % sb->s_blocksize;
0c044f0b
TM
3911
3912 return 0;
3913}
3914
3915static int ocfs2_list_xattr_bucket(struct inode *inode,
3916 struct ocfs2_xattr_bucket *bucket,
3917 void *para)
3918{
936b8834 3919 int ret = 0, type;
0c044f0b 3920 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
0c044f0b 3921 int i, block_off, new_offset;
936b8834 3922 const char *prefix, *name;
0c044f0b 3923
3e632946
JB
3924 for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
3925 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
936b8834
TM
3926 type = ocfs2_xattr_get_type(entry);
3927 prefix = ocfs2_xattr_prefix(type);
0c044f0b 3928
936b8834 3929 if (prefix) {
fd68a894 3930 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
3e632946 3931 bucket_xh(bucket),
0c044f0b
TM
3932 i,
3933 &block_off,
3934 &new_offset);
3935 if (ret)
3936 break;
936b8834 3937
51def39f 3938 name = (const char *)bucket_block(bucket, block_off) +
936b8834
TM
3939 new_offset;
3940 ret = ocfs2_xattr_list_entry(xl->buffer,
3941 xl->buffer_size,
3942 &xl->result,
3943 prefix, name,
3944 entry->xe_name_len);
3945 if (ret)
3946 break;
0c044f0b
TM
3947 }
3948 }
3949
3950 return ret;
3951}
3952
47bca495
TM
3953static int ocfs2_iterate_xattr_index_block(struct inode *inode,
3954 struct buffer_head *blk_bh,
3955 xattr_tree_rec_func *rec_func,
3956 void *para)
0c044f0b 3957{
47bca495
TM
3958 struct ocfs2_xattr_block *xb =
3959 (struct ocfs2_xattr_block *)blk_bh->b_data;
3960 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
0c044f0b
TM
3961 int ret = 0;
3962 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
3963 u64 p_blkno = 0;
0c044f0b 3964
47bca495 3965 if (!el->l_next_free_rec || !rec_func)
0c044f0b
TM
3966 return 0;
3967
3968 while (name_hash > 0) {
3969 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
3970 &e_cpos, &num_clusters, el);
3971 if (ret) {
3972 mlog_errno(ret);
47bca495 3973 break;
0c044f0b
TM
3974 }
3975
47bca495
TM
3976 ret = rec_func(inode, blk_bh, p_blkno, e_cpos,
3977 num_clusters, para);
0c044f0b 3978 if (ret) {
a46fa684
TM
3979 if (ret != -ERANGE)
3980 mlog_errno(ret);
47bca495 3981 break;
0c044f0b
TM
3982 }
3983
3984 if (e_cpos == 0)
3985 break;
3986
3987 name_hash = e_cpos - 1;
3988 }
3989
47bca495
TM
3990 return ret;
3991
3992}
3993
3994static int ocfs2_list_xattr_tree_rec(struct inode *inode,
3995 struct buffer_head *root_bh,
3996 u64 blkno, u32 cpos, u32 len, void *para)
3997{
3998 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
3999 ocfs2_list_xattr_bucket, para);
4000}
4001
4002static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
4003 struct buffer_head *blk_bh,
4004 char *buffer,
4005 size_t buffer_size)
4006{
4007 int ret;
4008 struct ocfs2_xattr_tree_list xl = {
4009 .buffer = buffer,
4010 .buffer_size = buffer_size,
4011 .result = 0,
4012 };
4013
4014 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
4015 ocfs2_list_xattr_tree_rec, &xl);
4016 if (ret) {
4017 mlog_errno(ret);
4018 goto out;
4019 }
4020
936b8834 4021 ret = xl.result;
0c044f0b
TM
4022out:
4023 return ret;
4024}
01225596
TM
4025
4026static int cmp_xe(const void *a, const void *b)
4027{
4028 const struct ocfs2_xattr_entry *l = a, *r = b;
4029 u32 l_hash = le32_to_cpu(l->xe_name_hash);
4030 u32 r_hash = le32_to_cpu(r->xe_name_hash);
4031
4032 if (l_hash > r_hash)
4033 return 1;
4034 if (l_hash < r_hash)
4035 return -1;
4036 return 0;
4037}
4038
4039static void swap_xe(void *a, void *b, int size)
4040{
4041 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
4042
4043 tmp = *l;
4044 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
4045 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
4046}
4047
4048/*
4049 * When the ocfs2_xattr_block is filled up, new bucket will be created
4050 * and all the xattr entries will be moved to the new bucket.
178eeac3
JB
4051 * The header goes at the start of the bucket, and the names+values are
4052 * filled from the end. This is why *target starts as the last buffer.
01225596
TM
4053 * Note: we need to sort the entries since they are not saved in order
4054 * in the ocfs2_xattr_block.
4055 */
4056static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
4057 struct buffer_head *xb_bh,
178eeac3 4058 struct ocfs2_xattr_bucket *bucket)
01225596
TM
4059{
4060 int i, blocksize = inode->i_sb->s_blocksize;
178eeac3 4061 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
01225596
TM
4062 u16 offset, size, off_change;
4063 struct ocfs2_xattr_entry *xe;
4064 struct ocfs2_xattr_block *xb =
4065 (struct ocfs2_xattr_block *)xb_bh->b_data;
4066 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
178eeac3 4067 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
01225596 4068 u16 count = le16_to_cpu(xb_xh->xh_count);
178eeac3
JB
4069 char *src = xb_bh->b_data;
4070 char *target = bucket_block(bucket, blks - 1);
01225596
TM
4071
4072 mlog(0, "cp xattr from block %llu to bucket %llu\n",
4073 (unsigned long long)xb_bh->b_blocknr,
178eeac3
JB
4074 (unsigned long long)bucket_blkno(bucket));
4075
4076 for (i = 0; i < blks; i++)
4077 memset(bucket_block(bucket, i), 0, blocksize);
01225596 4078
01225596
TM
4079 /*
4080 * Since the xe_name_offset is based on ocfs2_xattr_header,
4081 * there is a offset change corresponding to the change of
4082 * ocfs2_xattr_header's position.
4083 */
4084 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
4085 xe = &xb_xh->xh_entries[count - 1];
4086 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
4087 size = blocksize - offset;
4088
4089 /* copy all the names and values. */
01225596
TM
4090 memcpy(target + offset, src + offset, size);
4091
4092 /* Init new header now. */
4093 xh->xh_count = xb_xh->xh_count;
4094 xh->xh_num_buckets = cpu_to_le16(1);
4095 xh->xh_name_value_len = cpu_to_le16(size);
4096 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
4097
4098 /* copy all the entries. */
178eeac3 4099 target = bucket_block(bucket, 0);
01225596
TM
4100 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
4101 size = count * sizeof(struct ocfs2_xattr_entry);
4102 memcpy(target + offset, (char *)xb_xh + offset, size);
4103
4104 /* Change the xe offset for all the xe because of the move. */
4105 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
4106 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
4107 for (i = 0; i < count; i++)
4108 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
4109
4110 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
4111 offset, size, off_change);
4112
4113 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
4114 cmp_xe, swap_xe);
4115}
4116
4117/*
4118 * After we move xattr from block to index btree, we have to
4119 * update ocfs2_xattr_search to the new xe and base.
4120 *
4121 * When the entry is in xattr block, xattr_bh indicates the storage place.
4122 * While if the entry is in index b-tree, "bucket" indicates the
4123 * real place of the xattr.
4124 */
178eeac3
JB
4125static void ocfs2_xattr_update_xattr_search(struct inode *inode,
4126 struct ocfs2_xattr_search *xs,
4127 struct buffer_head *old_bh)
01225596 4128{
01225596
TM
4129 char *buf = old_bh->b_data;
4130 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
4131 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
178eeac3 4132 int i;
01225596 4133
ba937127 4134 xs->header = bucket_xh(xs->bucket);
178eeac3 4135 xs->base = bucket_block(xs->bucket, 0);
01225596
TM
4136 xs->end = xs->base + inode->i_sb->s_blocksize;
4137
178eeac3
JB
4138 if (xs->not_found)
4139 return;
01225596 4140
178eeac3
JB
4141 i = xs->here - old_xh->xh_entries;
4142 xs->here = &xs->header->xh_entries[i];
01225596
TM
4143}
4144
4145static int ocfs2_xattr_create_index_block(struct inode *inode,
78f30c31
TM
4146 struct ocfs2_xattr_search *xs,
4147 struct ocfs2_xattr_set_ctxt *ctxt)
01225596 4148{
85db90e7 4149 int ret;
01225596
TM
4150 u32 bit_off, len;
4151 u64 blkno;
85db90e7 4152 handle_t *handle = ctxt->handle;
01225596
TM
4153 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4154 struct ocfs2_inode_info *oi = OCFS2_I(inode);
01225596
TM
4155 struct buffer_head *xb_bh = xs->xattr_bh;
4156 struct ocfs2_xattr_block *xb =
4157 (struct ocfs2_xattr_block *)xb_bh->b_data;
4158 struct ocfs2_xattr_tree_root *xr;
4159 u16 xb_flags = le16_to_cpu(xb->xb_flags);
01225596
TM
4160
4161 mlog(0, "create xattr index block for %llu\n",
4162 (unsigned long long)xb_bh->b_blocknr);
4163
4164 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
178eeac3 4165 BUG_ON(!xs->bucket);
01225596 4166
01225596
TM
4167 /*
4168 * XXX:
4169 * We can use this lock for now, and maybe move to a dedicated mutex
4170 * if performance becomes a problem later.
4171 */
4172 down_write(&oi->ip_alloc_sem);
4173
0cf2f763 4174 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), xb_bh,
84008972 4175 OCFS2_JOURNAL_ACCESS_WRITE);
01225596
TM
4176 if (ret) {
4177 mlog_errno(ret);
85db90e7 4178 goto out;
01225596
TM
4179 }
4180
78f30c31
TM
4181 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac,
4182 1, 1, &bit_off, &len);
01225596
TM
4183 if (ret) {
4184 mlog_errno(ret);
85db90e7 4185 goto out;
01225596
TM
4186 }
4187
4188 /*
4189 * The bucket may spread in many blocks, and
4190 * we will only touch the 1st block and the last block
4191 * in the whole bucket(one for entry and one for data).
4192 */
4193 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
4194
de29c085
MF
4195 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
4196 (unsigned long long)blkno);
01225596 4197
178eeac3
JB
4198 ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
4199 if (ret) {
01225596 4200 mlog_errno(ret);
85db90e7 4201 goto out;
01225596
TM
4202 }
4203
178eeac3
JB
4204 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
4205 OCFS2_JOURNAL_ACCESS_CREATE);
01225596
TM
4206 if (ret) {
4207 mlog_errno(ret);
85db90e7 4208 goto out;
01225596
TM
4209 }
4210
178eeac3
JB
4211 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
4212 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
01225596 4213
178eeac3 4214 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
01225596
TM
4215
4216 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
4217 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
4218 offsetof(struct ocfs2_xattr_block, xb_attrs));
4219
4220 xr = &xb->xb_attrs.xb_root;
4221 xr->xt_clusters = cpu_to_le32(1);
4222 xr->xt_last_eb_blk = 0;
4223 xr->xt_list.l_tree_depth = 0;
4224 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
4225 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
4226
4227 xr->xt_list.l_recs[0].e_cpos = 0;
4228 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
4229 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
4230
4231 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
4232
85db90e7 4233 ocfs2_journal_dirty(handle, xb_bh);
01225596 4234
85db90e7 4235out:
01225596
TM
4236 up_write(&oi->ip_alloc_sem);
4237
01225596
TM
4238 return ret;
4239}
4240
4241static int cmp_xe_offset(const void *a, const void *b)
4242{
4243 const struct ocfs2_xattr_entry *l = a, *r = b;
4244 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
4245 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
4246
4247 if (l_name_offset < r_name_offset)
4248 return 1;
4249 if (l_name_offset > r_name_offset)
4250 return -1;
4251 return 0;
4252}
4253
4254/*
4255 * defrag a xattr bucket if we find that the bucket has some
4256 * holes beteen name/value pairs.
4257 * We will move all the name/value pairs to the end of the bucket
4258 * so that we can spare some space for insertion.
4259 */
4260static int ocfs2_defrag_xattr_bucket(struct inode *inode,
85db90e7 4261 handle_t *handle,
01225596
TM
4262 struct ocfs2_xattr_bucket *bucket)
4263{
4264 int ret, i;
199799a3 4265 size_t end, offset, len;
01225596
TM
4266 struct ocfs2_xattr_header *xh;
4267 char *entries, *buf, *bucket_buf = NULL;
9c7759aa 4268 u64 blkno = bucket_blkno(bucket);
01225596 4269 u16 xh_free_start;
01225596 4270 size_t blocksize = inode->i_sb->s_blocksize;
01225596 4271 struct ocfs2_xattr_entry *xe;
01225596
TM
4272
4273 /*
4274 * In order to make the operation more efficient and generic,
4275 * we copy all the blocks into a contiguous memory and do the
4276 * defragment there, so if anything is error, we will not touch
4277 * the real block.
4278 */
4279 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
4280 if (!bucket_buf) {
4281 ret = -EIO;
4282 goto out;
4283 }
4284
4285 buf = bucket_buf;
1c32a2fd
TM
4286 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4287 memcpy(buf, bucket_block(bucket, i), blocksize);
01225596 4288
1c32a2fd 4289 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
161d6f30
JB
4290 OCFS2_JOURNAL_ACCESS_WRITE);
4291 if (ret < 0) {
4292 mlog_errno(ret);
85db90e7 4293 goto out;
01225596
TM
4294 }
4295
4296 xh = (struct ocfs2_xattr_header *)bucket_buf;
4297 entries = (char *)xh->xh_entries;
4298 xh_free_start = le16_to_cpu(xh->xh_free_start);
4299
4300 mlog(0, "adjust xattr bucket in %llu, count = %u, "
4301 "xh_free_start = %u, xh_name_value_len = %u.\n",
de29c085
MF
4302 (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
4303 xh_free_start, le16_to_cpu(xh->xh_name_value_len));
01225596
TM
4304
4305 /*
4306 * sort all the entries by their offset.
4307 * the largest will be the first, so that we can
4308 * move them to the end one by one.
4309 */
4310 sort(entries, le16_to_cpu(xh->xh_count),
4311 sizeof(struct ocfs2_xattr_entry),
4312 cmp_xe_offset, swap_xe);
4313
4314 /* Move all name/values to the end of the bucket. */
4315 xe = xh->xh_entries;
4316 end = OCFS2_XATTR_BUCKET_SIZE;
4317 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
4318 offset = le16_to_cpu(xe->xe_name_offset);
199799a3 4319 len = namevalue_size_xe(xe);
01225596
TM
4320
4321 /*
4322 * We must make sure that the name/value pair
4323 * exist in the same block. So adjust end to
4324 * the previous block end if needed.
4325 */
4326 if (((end - len) / blocksize !=
4327 (end - 1) / blocksize))
4328 end = end - end % blocksize;
4329
4330 if (end > offset + len) {
4331 memmove(bucket_buf + end - len,
4332 bucket_buf + offset, len);
4333 xe->xe_name_offset = cpu_to_le16(end - len);
4334 }
4335
4336 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
4337 "bucket %llu\n", (unsigned long long)blkno);
4338
4339 end -= len;
4340 }
4341
4342 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
4343 "bucket %llu\n", (unsigned long long)blkno);
4344
4345 if (xh_free_start == end)
85db90e7 4346 goto out;
01225596
TM
4347
4348 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
4349 xh->xh_free_start = cpu_to_le16(end);
4350
4351 /* sort the entries by their name_hash. */
4352 sort(entries, le16_to_cpu(xh->xh_count),
4353 sizeof(struct ocfs2_xattr_entry),
4354 cmp_xe, swap_xe);
4355
4356 buf = bucket_buf;
1c32a2fd
TM
4357 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4358 memcpy(bucket_block(bucket, i), buf, blocksize);
4359 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
01225596 4360
01225596 4361out:
01225596
TM
4362 kfree(bucket_buf);
4363 return ret;
4364}
4365
4366/*
b5c03e74
JB
4367 * prev_blkno points to the start of an existing extent. new_blkno
4368 * points to a newly allocated extent. Because we know each of our
4369 * clusters contains more than bucket, we can easily split one cluster
4370 * at a bucket boundary. So we take the last cluster of the existing
4371 * extent and split it down the middle. We move the last half of the
4372 * buckets in the last cluster of the existing extent over to the new
4373 * extent.
01225596 4374 *
b5c03e74
JB
4375 * first_bh is the buffer at prev_blkno so we can update the existing
4376 * extent's bucket count. header_bh is the bucket were we were hoping
4377 * to insert our xattr. If the bucket move places the target in the new
4378 * extent, we'll update first_bh and header_bh after modifying the old
4379 * extent.
4380 *
4381 * first_hash will be set as the 1st xe's name_hash in the new extent.
01225596
TM
4382 */
4383static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
4384 handle_t *handle,
41cb8148
JB
4385 struct ocfs2_xattr_bucket *first,
4386 struct ocfs2_xattr_bucket *target,
01225596 4387 u64 new_blkno,
01225596
TM
4388 u32 num_clusters,
4389 u32 *first_hash)
4390{
c58b6032 4391 int ret;
41cb8148
JB
4392 struct super_block *sb = inode->i_sb;
4393 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb);
4394 int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
b5c03e74 4395 int to_move = num_buckets / 2;
c58b6032 4396 u64 src_blkno;
41cb8148
JB
4397 u64 last_cluster_blkno = bucket_blkno(first) +
4398 ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1));
01225596 4399
41cb8148
JB
4400 BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets);
4401 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize);
01225596 4402
01225596 4403 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
c58b6032 4404 (unsigned long long)last_cluster_blkno, (unsigned long long)new_blkno);
01225596 4405
41cb8148 4406 ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first),
c58b6032
JB
4407 last_cluster_blkno, new_blkno,
4408 to_move, first_hash);
01225596
TM
4409 if (ret) {
4410 mlog_errno(ret);
4411 goto out;
4412 }
4413
c58b6032
JB
4414 /* This is the first bucket that got moved */
4415 src_blkno = last_cluster_blkno + (to_move * blks_per_bucket);
01225596 4416
b5c03e74 4417 /*
c58b6032 4418 * If the target bucket was part of the moved buckets, we need to
41cb8148 4419 * update first and target.
b5c03e74 4420 */
41cb8148 4421 if (bucket_blkno(target) >= src_blkno) {
b5c03e74
JB
4422 /* Find the block for the new target bucket */
4423 src_blkno = new_blkno +
41cb8148
JB
4424 (bucket_blkno(target) - src_blkno);
4425
4426 ocfs2_xattr_bucket_relse(first);
4427 ocfs2_xattr_bucket_relse(target);
01225596 4428
b5c03e74 4429 /*
c58b6032 4430 * These shouldn't fail - the buffers are in the
b5c03e74
JB
4431 * journal from ocfs2_cp_xattr_bucket().
4432 */
41cb8148 4433 ret = ocfs2_read_xattr_bucket(first, new_blkno);
c58b6032
JB
4434 if (ret) {
4435 mlog_errno(ret);
4436 goto out;
4437 }
41cb8148
JB
4438 ret = ocfs2_read_xattr_bucket(target, src_blkno);
4439 if (ret)
b5c03e74 4440 mlog_errno(ret);
01225596 4441
01225596
TM
4442 }
4443
01225596 4444out:
01225596
TM
4445 return ret;
4446}
4447
01225596 4448/*
80bcaf34
TM
4449 * Find the suitable pos when we divide a bucket into 2.
4450 * We have to make sure the xattrs with the same hash value exist
4451 * in the same bucket.
4452 *
4453 * If this ocfs2_xattr_header covers more than one hash value, find a
4454 * place where the hash value changes. Try to find the most even split.
4455 * The most common case is that all entries have different hash values,
4456 * and the first check we make will find a place to split.
4457 */
4458static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
4459{
4460 struct ocfs2_xattr_entry *entries = xh->xh_entries;
4461 int count = le16_to_cpu(xh->xh_count);
4462 int delta, middle = count / 2;
4463
4464 /*
4465 * We start at the middle. Each step gets farther away in both
4466 * directions. We therefore hit the change in hash value
4467 * nearest to the middle. Note that this loop does not execute for
4468 * count < 2.
4469 */
4470 for (delta = 0; delta < middle; delta++) {
4471 /* Let's check delta earlier than middle */
4472 if (cmp_xe(&entries[middle - delta - 1],
4473 &entries[middle - delta]))
4474 return middle - delta;
4475
4476 /* For even counts, don't walk off the end */
4477 if ((middle + delta + 1) == count)
4478 continue;
4479
4480 /* Now try delta past middle */
4481 if (cmp_xe(&entries[middle + delta],
4482 &entries[middle + delta + 1]))
4483 return middle + delta + 1;
4484 }
4485
4486 /* Every entry had the same hash */
4487 return count;
4488}
4489
4490/*
4491 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
01225596 4492 * first_hash will record the 1st hash of the new bucket.
80bcaf34
TM
4493 *
4494 * Normally half of the xattrs will be moved. But we have to make
4495 * sure that the xattrs with the same hash value are stored in the
4496 * same bucket. If all the xattrs in this bucket have the same hash
4497 * value, the new bucket will be initialized as an empty one and the
4498 * first_hash will be initialized as (hash_value+1).
01225596 4499 */
80bcaf34
TM
4500static int ocfs2_divide_xattr_bucket(struct inode *inode,
4501 handle_t *handle,
4502 u64 blk,
4503 u64 new_blk,
4504 u32 *first_hash,
4505 int new_bucket_head)
01225596
TM
4506{
4507 int ret, i;
199799a3 4508 int count, start, len, name_value_len = 0, name_offset = 0;
ba937127 4509 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
01225596
TM
4510 struct ocfs2_xattr_header *xh;
4511 struct ocfs2_xattr_entry *xe;
4512 int blocksize = inode->i_sb->s_blocksize;
4513
80bcaf34 4514 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
de29c085 4515 (unsigned long long)blk, (unsigned long long)new_blk);
01225596 4516
ba937127
JB
4517 s_bucket = ocfs2_xattr_bucket_new(inode);
4518 t_bucket = ocfs2_xattr_bucket_new(inode);
4519 if (!s_bucket || !t_bucket) {
4520 ret = -ENOMEM;
4521 mlog_errno(ret);
4522 goto out;
4523 }
01225596 4524
ba937127 4525 ret = ocfs2_read_xattr_bucket(s_bucket, blk);
01225596
TM
4526 if (ret) {
4527 mlog_errno(ret);
4528 goto out;
4529 }
4530
ba937127 4531 ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
1224be02 4532 OCFS2_JOURNAL_ACCESS_WRITE);
01225596
TM
4533 if (ret) {
4534 mlog_errno(ret);
4535 goto out;
4536 }
4537
784b816a
JB
4538 /*
4539 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
4540 * there's no need to read it.
4541 */
ba937127 4542 ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
01225596
TM
4543 if (ret) {
4544 mlog_errno(ret);
4545 goto out;
4546 }
4547
2b656c1d
JB
4548 /*
4549 * Hey, if we're overwriting t_bucket, what difference does
4550 * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the
4551 * same part of ocfs2_cp_xattr_bucket().
4552 */
ba937127 4553 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
1224be02
JB
4554 new_bucket_head ?
4555 OCFS2_JOURNAL_ACCESS_CREATE :
4556 OCFS2_JOURNAL_ACCESS_WRITE);
4557 if (ret) {
4558 mlog_errno(ret);
4559 goto out;
01225596
TM
4560 }
4561
ba937127 4562 xh = bucket_xh(s_bucket);
80bcaf34
TM
4563 count = le16_to_cpu(xh->xh_count);
4564 start = ocfs2_xattr_find_divide_pos(xh);
4565
4566 if (start == count) {
4567 xe = &xh->xh_entries[start-1];
4568
4569 /*
4570 * initialized a new empty bucket here.
4571 * The hash value is set as one larger than
4572 * that of the last entry in the previous bucket.
4573 */
ba937127
JB
4574 for (i = 0; i < t_bucket->bu_blocks; i++)
4575 memset(bucket_block(t_bucket, i), 0, blocksize);
80bcaf34 4576
ba937127 4577 xh = bucket_xh(t_bucket);
80bcaf34
TM
4578 xh->xh_free_start = cpu_to_le16(blocksize);
4579 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
4580 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
4581
4582 goto set_num_buckets;
4583 }
4584
01225596 4585 /* copy the whole bucket to the new first. */
ba937127 4586 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
01225596
TM
4587
4588 /* update the new bucket. */
ba937127 4589 xh = bucket_xh(t_bucket);
01225596
TM
4590
4591 /*
4592 * Calculate the total name/value len and xh_free_start for
4593 * the old bucket first.
4594 */
4595 name_offset = OCFS2_XATTR_BUCKET_SIZE;
4596 name_value_len = 0;
4597 for (i = 0; i < start; i++) {
4598 xe = &xh->xh_entries[i];
199799a3 4599 name_value_len += namevalue_size_xe(xe);
01225596
TM
4600 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
4601 name_offset = le16_to_cpu(xe->xe_name_offset);
4602 }
4603
4604 /*
4605 * Now begin the modification to the new bucket.
4606 *
4607 * In the new bucket, We just move the xattr entry to the beginning
4608 * and don't touch the name/value. So there will be some holes in the
4609 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
4610 * called.
4611 */
4612 xe = &xh->xh_entries[start];
4613 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
4614 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
ff1ec20e
MF
4615 (int)((char *)xe - (char *)xh),
4616 (int)((char *)xh->xh_entries - (char *)xh));
01225596
TM
4617 memmove((char *)xh->xh_entries, (char *)xe, len);
4618 xe = &xh->xh_entries[count - start];
4619 len = sizeof(struct ocfs2_xattr_entry) * start;
4620 memset((char *)xe, 0, len);
4621
4622 le16_add_cpu(&xh->xh_count, -start);
4623 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
4624
4625 /* Calculate xh_free_start for the new bucket. */
4626 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4627 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4628 xe = &xh->xh_entries[i];
01225596
TM
4629 if (le16_to_cpu(xe->xe_name_offset) <
4630 le16_to_cpu(xh->xh_free_start))
4631 xh->xh_free_start = xe->xe_name_offset;
4632 }
4633
80bcaf34 4634set_num_buckets:
01225596
TM
4635 /* set xh->xh_num_buckets for the new xh. */
4636 if (new_bucket_head)
4637 xh->xh_num_buckets = cpu_to_le16(1);
4638 else
4639 xh->xh_num_buckets = 0;
4640
ba937127 4641 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
01225596
TM
4642
4643 /* store the first_hash of the new bucket. */
4644 if (first_hash)
4645 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
4646
4647 /*
80bcaf34
TM
4648 * Now only update the 1st block of the old bucket. If we
4649 * just added a new empty bucket, there is no need to modify
4650 * it.
01225596 4651 */
80bcaf34
TM
4652 if (start == count)
4653 goto out;
4654
ba937127 4655 xh = bucket_xh(s_bucket);
01225596
TM
4656 memset(&xh->xh_entries[start], 0,
4657 sizeof(struct ocfs2_xattr_entry) * (count - start));
4658 xh->xh_count = cpu_to_le16(start);
4659 xh->xh_free_start = cpu_to_le16(name_offset);
4660 xh->xh_name_value_len = cpu_to_le16(name_value_len);
4661
ba937127 4662 ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
01225596
TM
4663
4664out:
ba937127
JB
4665 ocfs2_xattr_bucket_free(s_bucket);
4666 ocfs2_xattr_bucket_free(t_bucket);
01225596
TM
4667
4668 return ret;
4669}
4670
4671/*
4672 * Copy xattr from one bucket to another bucket.
4673 *
4674 * The caller must make sure that the journal transaction
4675 * has enough space for journaling.
4676 */
4677static int ocfs2_cp_xattr_bucket(struct inode *inode,
4678 handle_t *handle,
4679 u64 s_blkno,
4680 u64 t_blkno,
4681 int t_is_new)
4682{
4980c6da 4683 int ret;
ba937127 4684 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
01225596
TM
4685
4686 BUG_ON(s_blkno == t_blkno);
4687
4688 mlog(0, "cp bucket %llu to %llu, target is %d\n",
de29c085
MF
4689 (unsigned long long)s_blkno, (unsigned long long)t_blkno,
4690 t_is_new);
01225596 4691
ba937127
JB
4692 s_bucket = ocfs2_xattr_bucket_new(inode);
4693 t_bucket = ocfs2_xattr_bucket_new(inode);
4694 if (!s_bucket || !t_bucket) {
4695 ret = -ENOMEM;
4696 mlog_errno(ret);
4697 goto out;
4698 }
92de109a 4699
ba937127 4700 ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
01225596
TM
4701 if (ret)
4702 goto out;
4703
784b816a
JB
4704 /*
4705 * Even if !t_is_new, we're overwriting t_bucket. Thus,
4706 * there's no need to read it.
4707 */
ba937127 4708 ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
01225596
TM
4709 if (ret)
4710 goto out;
4711
2b656c1d
JB
4712 /*
4713 * Hey, if we're overwriting t_bucket, what difference does
4714 * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new
874d65af
JB
4715 * cluster to fill, we came here from
4716 * ocfs2_mv_xattr_buckets(), and it is really new -
4717 * ACCESS_CREATE is required. But we also might have moved data
4718 * out of t_bucket before extending back into it.
4719 * ocfs2_add_new_xattr_bucket() can do this - its call to
4720 * ocfs2_add_new_xattr_cluster() may have created a new extent
2b656c1d
JB
4721 * and copied out the end of the old extent. Then it re-extends
4722 * the old extent back to create space for new xattrs. That's
4723 * how we get here, and the bucket isn't really new.
4724 */
ba937127 4725 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
1224be02
JB
4726 t_is_new ?
4727 OCFS2_JOURNAL_ACCESS_CREATE :
4728 OCFS2_JOURNAL_ACCESS_WRITE);
4729 if (ret)
4730 goto out;
01225596 4731
ba937127
JB
4732 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4733 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
01225596
TM
4734
4735out:
ba937127
JB
4736 ocfs2_xattr_bucket_free(t_bucket);
4737 ocfs2_xattr_bucket_free(s_bucket);
01225596
TM
4738
4739 return ret;
4740}
4741
4742/*
874d65af
JB
4743 * src_blk points to the start of an existing extent. last_blk points to
4744 * last cluster in that extent. to_blk points to a newly allocated
54ecb6b6
JB
4745 * extent. We copy the buckets from the cluster at last_blk to the new
4746 * extent. If start_bucket is non-zero, we skip that many buckets before
4747 * we start copying. The new extent's xh_num_buckets gets set to the
4748 * number of buckets we copied. The old extent's xh_num_buckets shrinks
4749 * by the same amount.
01225596 4750 */
54ecb6b6
JB
4751static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
4752 u64 src_blk, u64 last_blk, u64 to_blk,
4753 unsigned int start_bucket,
4754 u32 *first_hash)
01225596
TM
4755{
4756 int i, ret, credits;
4757 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
15d60929 4758 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
01225596 4759 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
15d60929 4760 struct ocfs2_xattr_bucket *old_first, *new_first;
01225596 4761
874d65af
JB
4762 mlog(0, "mv xattrs from cluster %llu to %llu\n",
4763 (unsigned long long)last_blk, (unsigned long long)to_blk);
01225596 4764
54ecb6b6
JB
4765 BUG_ON(start_bucket >= num_buckets);
4766 if (start_bucket) {
4767 num_buckets -= start_bucket;
4768 last_blk += (start_bucket * blks_per_bucket);
4769 }
4770
15d60929
JB
4771 /* The first bucket of the original extent */
4772 old_first = ocfs2_xattr_bucket_new(inode);
4773 /* The first bucket of the new extent */
4774 new_first = ocfs2_xattr_bucket_new(inode);
4775 if (!old_first || !new_first) {
4776 ret = -ENOMEM;
4777 mlog_errno(ret);
4778 goto out;
4779 }
4780
874d65af 4781 ret = ocfs2_read_xattr_bucket(old_first, src_blk);
15d60929
JB
4782 if (ret) {
4783 mlog_errno(ret);
4784 goto out;
4785 }
4786
01225596 4787 /*
54ecb6b6
JB
4788 * We need to update the first bucket of the old extent and all
4789 * the buckets going to the new extent.
01225596 4790 */
54ecb6b6
JB
4791 credits = ((num_buckets + 1) * blks_per_bucket) +
4792 handle->h_buffer_credits;
01225596
TM
4793 ret = ocfs2_extend_trans(handle, credits);
4794 if (ret) {
4795 mlog_errno(ret);
4796 goto out;
4797 }
4798
15d60929
JB
4799 ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
4800 OCFS2_JOURNAL_ACCESS_WRITE);
01225596
TM
4801 if (ret) {
4802 mlog_errno(ret);
4803 goto out;
4804 }
4805
4806 for (i = 0; i < num_buckets; i++) {
4807 ret = ocfs2_cp_xattr_bucket(inode, handle,
874d65af 4808 last_blk + (i * blks_per_bucket),
15d60929
JB
4809 to_blk + (i * blks_per_bucket),
4810 1);
01225596
TM
4811 if (ret) {
4812 mlog_errno(ret);
4813 goto out;
4814 }
01225596
TM
4815 }
4816
15d60929
JB
4817 /*
4818 * Get the new bucket ready before we dirty anything
4819 * (This actually shouldn't fail, because we already dirtied
4820 * it once in ocfs2_cp_xattr_bucket()).
4821 */
4822 ret = ocfs2_read_xattr_bucket(new_first, to_blk);
4823 if (ret) {
01225596
TM
4824 mlog_errno(ret);
4825 goto out;
4826 }
15d60929
JB
4827 ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
4828 OCFS2_JOURNAL_ACCESS_WRITE);
01225596
TM
4829 if (ret) {
4830 mlog_errno(ret);
4831 goto out;
4832 }
4833
15d60929
JB
4834 /* Now update the headers */
4835 le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets);
4836 ocfs2_xattr_bucket_journal_dirty(handle, old_first);
01225596 4837
15d60929
JB
4838 bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets);
4839 ocfs2_xattr_bucket_journal_dirty(handle, new_first);
01225596
TM
4840
4841 if (first_hash)
15d60929
JB
4842 *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash);
4843
01225596 4844out:
15d60929
JB
4845 ocfs2_xattr_bucket_free(new_first);
4846 ocfs2_xattr_bucket_free(old_first);
01225596
TM
4847 return ret;
4848}
4849
4850/*
80bcaf34 4851 * Move some xattrs in this cluster to the new cluster.
01225596
TM
4852 * This function should only be called when bucket size == cluster size.
4853 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
4854 */
80bcaf34
TM
4855static int ocfs2_divide_xattr_cluster(struct inode *inode,
4856 handle_t *handle,
4857 u64 prev_blk,
4858 u64 new_blk,
4859 u32 *first_hash)
01225596
TM
4860{
4861 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
85db90e7 4862 int ret, credits = 2 * blk_per_bucket + handle->h_buffer_credits;
01225596
TM
4863
4864 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
4865
4866 ret = ocfs2_extend_trans(handle, credits);
4867 if (ret) {
4868 mlog_errno(ret);
4869 return ret;
4870 }
4871
4872 /* Move half of the xattr in start_blk to the next bucket. */
80bcaf34
TM
4873 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
4874 new_blk, first_hash, 1);
01225596
TM
4875}
4876
4877/*
4878 * Move some xattrs from the old cluster to the new one since they are not
4879 * contiguous in ocfs2 xattr tree.
4880 *
4881 * new_blk starts a new separate cluster, and we will move some xattrs from
4882 * prev_blk to it. v_start will be set as the first name hash value in this
4883 * new cluster so that it can be used as e_cpos during tree insertion and
4884 * don't collide with our original b-tree operations. first_bh and header_bh
4885 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
4886 * to extend the insert bucket.
4887 *
4888 * The problem is how much xattr should we move to the new one and when should
4889 * we update first_bh and header_bh?
4890 * 1. If cluster size > bucket size, that means the previous cluster has more
4891 * than 1 bucket, so just move half nums of bucket into the new cluster and
4892 * update the first_bh and header_bh if the insert bucket has been moved
4893 * to the new cluster.
4894 * 2. If cluster_size == bucket_size:
4895 * a) If the previous extent rec has more than one cluster and the insert
4896 * place isn't in the last cluster, copy the entire last cluster to the
4897 * new one. This time, we don't need to upate the first_bh and header_bh
4898 * since they will not be moved into the new cluster.
4899 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
4900 * the new one. And we set the extend flag to zero if the insert place is
4901 * moved into the new allocated cluster since no extend is needed.
4902 */
4903static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
4904 handle_t *handle,
012ee910
JB
4905 struct ocfs2_xattr_bucket *first,
4906 struct ocfs2_xattr_bucket *target,
01225596 4907 u64 new_blk,
01225596
TM
4908 u32 prev_clusters,
4909 u32 *v_start,
4910 int *extend)
4911{
92cf3adf 4912 int ret;
01225596
TM
4913
4914 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
012ee910 4915 (unsigned long long)bucket_blkno(first), prev_clusters,
de29c085 4916 (unsigned long long)new_blk);
01225596 4917
41cb8148 4918 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) {
01225596
TM
4919 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
4920 handle,
41cb8148 4921 first, target,
01225596 4922 new_blk,
01225596
TM
4923 prev_clusters,
4924 v_start);
012ee910 4925 if (ret)
41cb8148 4926 mlog_errno(ret);
41cb8148 4927 } else {
92cf3adf
JB
4928 /* The start of the last cluster in the first extent */
4929 u64 last_blk = bucket_blkno(first) +
4930 ((prev_clusters - 1) *
4931 ocfs2_clusters_to_blocks(inode->i_sb, 1));
01225596 4932
012ee910 4933 if (prev_clusters > 1 && bucket_blkno(target) != last_blk) {
874d65af 4934 ret = ocfs2_mv_xattr_buckets(inode, handle,
92cf3adf 4935 bucket_blkno(first),
54ecb6b6 4936 last_blk, new_blk, 0,
01225596 4937 v_start);
012ee910
JB
4938 if (ret)
4939 mlog_errno(ret);
4940 } else {
80bcaf34
TM
4941 ret = ocfs2_divide_xattr_cluster(inode, handle,
4942 last_blk, new_blk,
4943 v_start);
012ee910
JB
4944 if (ret)
4945 mlog_errno(ret);
01225596 4946
92cf3adf 4947 if ((bucket_blkno(target) == last_blk) && extend)
01225596
TM
4948 *extend = 0;
4949 }
4950 }
4951
4952 return ret;
4953}
4954
4955/*
4956 * Add a new cluster for xattr storage.
4957 *
4958 * If the new cluster is contiguous with the previous one, it will be
4959 * appended to the same extent record, and num_clusters will be updated.
4960 * If not, we will insert a new extent for it and move some xattrs in
4961 * the last cluster into the new allocated one.
4962 * We also need to limit the maximum size of a btree leaf, otherwise we'll
4963 * lose the benefits of hashing because we'll have to search large leaves.
4964 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
4965 * if it's bigger).
4966 *
4967 * first_bh is the first block of the previous extent rec and header_bh
4968 * indicates the bucket we will insert the new xattrs. They will be updated
4969 * when the header_bh is moved into the new cluster.
4970 */
4971static int ocfs2_add_new_xattr_cluster(struct inode *inode,
4972 struct buffer_head *root_bh,
ed29c0ca
JB
4973 struct ocfs2_xattr_bucket *first,
4974 struct ocfs2_xattr_bucket *target,
01225596
TM
4975 u32 *num_clusters,
4976 u32 prev_cpos,
78f30c31
TM
4977 int *extend,
4978 struct ocfs2_xattr_set_ctxt *ctxt)
01225596 4979{
85db90e7 4980 int ret;
01225596
TM
4981 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
4982 u32 prev_clusters = *num_clusters;
4983 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
4984 u64 block;
85db90e7 4985 handle_t *handle = ctxt->handle;
01225596 4986 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
f99b9b7c 4987 struct ocfs2_extent_tree et;
01225596
TM
4988
4989 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
4990 "previous xattr blkno = %llu\n",
4991 (unsigned long long)OCFS2_I(inode)->ip_blkno,
ed29c0ca 4992 prev_cpos, (unsigned long long)bucket_blkno(first));
01225596 4993
5e404e9e 4994 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
f99b9b7c 4995
0cf2f763 4996 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
84008972 4997 OCFS2_JOURNAL_ACCESS_WRITE);
01225596
TM
4998 if (ret < 0) {
4999 mlog_errno(ret);
5000 goto leave;
5001 }
5002
78f30c31 5003 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, 1,
01225596
TM
5004 clusters_to_add, &bit_off, &num_bits);
5005 if (ret < 0) {
5006 if (ret != -ENOSPC)
5007 mlog_errno(ret);
5008 goto leave;
5009 }
5010
5011 BUG_ON(num_bits > clusters_to_add);
5012
5013 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
5014 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
5015 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
5016
ed29c0ca 5017 if (bucket_blkno(first) + (prev_clusters * bpc) == block &&
01225596
TM
5018 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
5019 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
5020 /*
5021 * If this cluster is contiguous with the old one and
5022 * adding this new cluster, we don't surpass the limit of
5023 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
5024 * initialized and used like other buckets in the previous
5025 * cluster.
5026 * So add it as a contiguous one. The caller will handle
5027 * its init process.
5028 */
5029 v_start = prev_cpos + prev_clusters;
5030 *num_clusters = prev_clusters + num_bits;
5031 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
5032 num_bits);
5033 } else {
5034 ret = ocfs2_adjust_xattr_cross_cluster(inode,
5035 handle,
012ee910
JB
5036 first,
5037 target,
01225596 5038 block,
01225596
TM
5039 prev_clusters,
5040 &v_start,
5041 extend);
5042 if (ret) {
5043 mlog_errno(ret);
5044 goto leave;
5045 }
5046 }
5047
5048 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
de29c085 5049 num_bits, (unsigned long long)block, v_start);
cc79d8c1 5050 ret = ocfs2_insert_extent(handle, &et, v_start, block,
78f30c31 5051 num_bits, 0, ctxt->meta_ac);
01225596
TM
5052 if (ret < 0) {
5053 mlog_errno(ret);
5054 goto leave;
5055 }
5056
5057 ret = ocfs2_journal_dirty(handle, root_bh);
85db90e7 5058 if (ret < 0)
01225596 5059 mlog_errno(ret);
01225596
TM
5060
5061leave:
01225596
TM
5062 return ret;
5063}
5064
5065/*
92de109a
JB
5066 * We are given an extent. 'first' is the bucket at the very front of
5067 * the extent. The extent has space for an additional bucket past
5068 * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
5069 * of the target bucket. We wish to shift every bucket past the target
5070 * down one, filling in that additional space. When we get back to the
5071 * target, we split the target between itself and the now-empty bucket
5072 * at target+1 (aka, target_blkno + blks_per_bucket).
01225596
TM
5073 */
5074static int ocfs2_extend_xattr_bucket(struct inode *inode,
85db90e7 5075 handle_t *handle,
92de109a
JB
5076 struct ocfs2_xattr_bucket *first,
5077 u64 target_blk,
01225596
TM
5078 u32 num_clusters)
5079{
5080 int ret, credits;
5081 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5082 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
92de109a
JB
5083 u64 end_blk;
5084 u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets);
01225596
TM
5085
5086 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
92de109a
JB
5087 "from %llu, len = %u\n", (unsigned long long)target_blk,
5088 (unsigned long long)bucket_blkno(first), num_clusters);
01225596 5089
92de109a
JB
5090 /* The extent must have room for an additional bucket */
5091 BUG_ON(new_bucket >=
5092 (num_clusters * ocfs2_xattr_buckets_per_cluster(osb)));
01225596 5093
92de109a
JB
5094 /* end_blk points to the last existing bucket */
5095 end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket);
01225596
TM
5096
5097 /*
92de109a
JB
5098 * end_blk is the start of the last existing bucket.
5099 * Thus, (end_blk - target_blk) covers the target bucket and
5100 * every bucket after it up to, but not including, the last
5101 * existing bucket. Then we add the last existing bucket, the
5102 * new bucket, and the first bucket (3 * blk_per_bucket).
01225596 5103 */
92de109a 5104 credits = (end_blk - target_blk) + (3 * blk_per_bucket) +
85db90e7
TM
5105 handle->h_buffer_credits;
5106 ret = ocfs2_extend_trans(handle, credits);
5107 if (ret) {
01225596
TM
5108 mlog_errno(ret);
5109 goto out;
5110 }
5111
92de109a
JB
5112 ret = ocfs2_xattr_bucket_journal_access(handle, first,
5113 OCFS2_JOURNAL_ACCESS_WRITE);
01225596
TM
5114 if (ret) {
5115 mlog_errno(ret);
85db90e7 5116 goto out;
01225596
TM
5117 }
5118
92de109a 5119 while (end_blk != target_blk) {
01225596
TM
5120 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
5121 end_blk + blk_per_bucket, 0);
5122 if (ret)
85db90e7 5123 goto out;
01225596
TM
5124 end_blk -= blk_per_bucket;
5125 }
5126
92de109a
JB
5127 /* Move half of the xattr in target_blkno to the next bucket. */
5128 ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk,
5129 target_blk + blk_per_bucket, NULL, 0);
01225596 5130
92de109a
JB
5131 le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1);
5132 ocfs2_xattr_bucket_journal_dirty(handle, first);
01225596 5133
01225596
TM
5134out:
5135 return ret;
5136}
5137
5138/*
91f2033f
JB
5139 * Add new xattr bucket in an extent record and adjust the buckets
5140 * accordingly. xb_bh is the ocfs2_xattr_block, and target is the
5141 * bucket we want to insert into.
01225596 5142 *
91f2033f
JB
5143 * In the easy case, we will move all the buckets after target down by
5144 * one. Half of target's xattrs will be moved to the next bucket.
5145 *
5146 * If current cluster is full, we'll allocate a new one. This may not
5147 * be contiguous. The underlying calls will make sure that there is
5148 * space for the insert, shifting buckets around if necessary.
5149 * 'target' may be moved by those calls.
01225596
TM
5150 */
5151static int ocfs2_add_new_xattr_bucket(struct inode *inode,
5152 struct buffer_head *xb_bh,
91f2033f 5153 struct ocfs2_xattr_bucket *target,
78f30c31 5154 struct ocfs2_xattr_set_ctxt *ctxt)
01225596 5155{
01225596
TM
5156 struct ocfs2_xattr_block *xb =
5157 (struct ocfs2_xattr_block *)xb_bh->b_data;
5158 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
5159 struct ocfs2_extent_list *el = &xb_root->xt_list;
91f2033f
JB
5160 u32 name_hash =
5161 le32_to_cpu(bucket_xh(target)->xh_entries[0].xe_name_hash);
ed29c0ca 5162 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
01225596
TM
5163 int ret, num_buckets, extend = 1;
5164 u64 p_blkno;
5165 u32 e_cpos, num_clusters;
92de109a 5166 /* The bucket at the front of the extent */
91f2033f 5167 struct ocfs2_xattr_bucket *first;
01225596 5168
91f2033f
JB
5169 mlog(0, "Add new xattr bucket starting from %llu\n",
5170 (unsigned long long)bucket_blkno(target));
01225596 5171
ed29c0ca 5172 /* The first bucket of the original extent */
92de109a 5173 first = ocfs2_xattr_bucket_new(inode);
91f2033f 5174 if (!first) {
92de109a
JB
5175 ret = -ENOMEM;
5176 mlog_errno(ret);
5177 goto out;
5178 }
5179
01225596
TM
5180 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
5181 &num_clusters, el);
5182 if (ret) {
5183 mlog_errno(ret);
5184 goto out;
5185 }
5186
ed29c0ca 5187 ret = ocfs2_read_xattr_bucket(first, p_blkno);
01225596
TM
5188 if (ret) {
5189 mlog_errno(ret);
5190 goto out;
5191 }
5192
ed29c0ca
JB
5193 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
5194 if (num_buckets == le16_to_cpu(bucket_xh(first)->xh_num_buckets)) {
5195 /*
5196 * This can move first+target if the target bucket moves
5197 * to the new extent.
5198 */
01225596
TM
5199 ret = ocfs2_add_new_xattr_cluster(inode,
5200 xb_bh,
ed29c0ca
JB
5201 first,
5202 target,
01225596
TM
5203 &num_clusters,
5204 e_cpos,
78f30c31
TM
5205 &extend,
5206 ctxt);
01225596
TM
5207 if (ret) {
5208 mlog_errno(ret);
5209 goto out;
5210 }
5211 }
5212
92de109a 5213 if (extend) {
01225596 5214 ret = ocfs2_extend_xattr_bucket(inode,
85db90e7 5215 ctxt->handle,
ed29c0ca
JB
5216 first,
5217 bucket_blkno(target),
01225596 5218 num_clusters);
92de109a
JB
5219 if (ret)
5220 mlog_errno(ret);
5221 }
5222
01225596 5223out:
92de109a 5224 ocfs2_xattr_bucket_free(first);
ed29c0ca 5225
01225596
TM
5226 return ret;
5227}
5228
5229static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
5230 struct ocfs2_xattr_bucket *bucket,
5231 int offs)
5232{
5233 int block_off = offs >> inode->i_sb->s_blocksize_bits;
5234
5235 offs = offs % inode->i_sb->s_blocksize;
51def39f 5236 return bucket_block(bucket, block_off) + offs;
01225596
TM
5237}
5238
01225596
TM
5239/*
5240 * Truncate the specified xe_off entry in xattr bucket.
5241 * bucket is indicated by header_bh and len is the new length.
5242 * Both the ocfs2_xattr_value_root and the entry will be updated here.
5243 *
5244 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
5245 */
5246static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
548b0f22 5247 struct ocfs2_xattr_bucket *bucket,
01225596 5248 int xe_off,
78f30c31
TM
5249 int len,
5250 struct ocfs2_xattr_set_ctxt *ctxt)
01225596
TM
5251{
5252 int ret, offset;
5253 u64 value_blk;
01225596 5254 struct ocfs2_xattr_entry *xe;
548b0f22 5255 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
01225596 5256 size_t blocksize = inode->i_sb->s_blocksize;
b3e5d379
JB
5257 struct ocfs2_xattr_value_buf vb = {
5258 .vb_access = ocfs2_journal_access,
5259 };
01225596
TM
5260
5261 xe = &xh->xh_entries[xe_off];
5262
5263 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
5264
5265 offset = le16_to_cpu(xe->xe_name_offset) +
5266 OCFS2_XATTR_SIZE(xe->xe_name_len);
5267
5268 value_blk = offset / blocksize;
5269
5270 /* We don't allow ocfs2_xattr_value to be stored in different block. */
5271 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
01225596 5272
b3e5d379
JB
5273 vb.vb_bh = bucket->bu_bhs[value_blk];
5274 BUG_ON(!vb.vb_bh);
01225596 5275
b3e5d379
JB
5276 vb.vb_xv = (struct ocfs2_xattr_value_root *)
5277 (vb.vb_bh->b_data + offset % blocksize);
01225596 5278
548b0f22
JB
5279 /*
5280 * From here on out we have to dirty the bucket. The generic
5281 * value calls only modify one of the bucket's bhs, but we need
5282 * to send the bucket at once. So if they error, they *could* have
5283 * modified something. We have to assume they did, and dirty
5284 * the whole bucket. This leaves us in a consistent state.
5285 */
5286 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
5287 xe_off, (unsigned long long)bucket_blkno(bucket), len);
b3e5d379 5288 ret = ocfs2_xattr_value_truncate(inode, &vb, len, ctxt);
01225596
TM
5289 if (ret) {
5290 mlog_errno(ret);
554e7f9e
TM
5291 goto out;
5292 }
5293
5294 ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
5295 OCFS2_JOURNAL_ACCESS_WRITE);
5296 if (ret) {
5297 mlog_errno(ret);
5298 goto out;
01225596
TM
5299 }
5300
548b0f22
JB
5301 xe->xe_value_size = cpu_to_le64(len);
5302
548b0f22
JB
5303 ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket);
5304
01225596 5305out:
01225596
TM
5306 return ret;
5307}
5308
01225596
TM
5309static int ocfs2_rm_xattr_cluster(struct inode *inode,
5310 struct buffer_head *root_bh,
5311 u64 blkno,
5312 u32 cpos,
47bca495
TM
5313 u32 len,
5314 void *para)
01225596
TM
5315{
5316 int ret;
5317 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5318 struct inode *tl_inode = osb->osb_tl_inode;
5319 handle_t *handle;
5320 struct ocfs2_xattr_block *xb =
5321 (struct ocfs2_xattr_block *)root_bh->b_data;
01225596
TM
5322 struct ocfs2_alloc_context *meta_ac = NULL;
5323 struct ocfs2_cached_dealloc_ctxt dealloc;
f99b9b7c
JB
5324 struct ocfs2_extent_tree et;
5325
47bca495 5326 ret = ocfs2_iterate_xattr_buckets(inode, blkno, len,
ce9c5a54 5327 ocfs2_delete_xattr_in_bucket, para);
47bca495
TM
5328 if (ret) {
5329 mlog_errno(ret);
5330 return ret;
5331 }
5332
5e404e9e 5333 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
01225596
TM
5334
5335 ocfs2_init_dealloc_ctxt(&dealloc);
5336
5337 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
5338 cpos, len, (unsigned long long)blkno);
5339
8cb471e8
JB
5340 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode), blkno,
5341 len);
01225596 5342
f99b9b7c 5343 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
01225596
TM
5344 if (ret) {
5345 mlog_errno(ret);
5346 return ret;
5347 }
5348
5349 mutex_lock(&tl_inode->i_mutex);
5350
5351 if (ocfs2_truncate_log_needs_flush(osb)) {
5352 ret = __ocfs2_flush_truncate_log(osb);
5353 if (ret < 0) {
5354 mlog_errno(ret);
5355 goto out;
5356 }
5357 }
5358
a90714c1 5359 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
d3264799 5360 if (IS_ERR(handle)) {
01225596
TM
5361 ret = -ENOMEM;
5362 mlog_errno(ret);
5363 goto out;
5364 }
5365
0cf2f763 5366 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
84008972 5367 OCFS2_JOURNAL_ACCESS_WRITE);
01225596
TM
5368 if (ret) {
5369 mlog_errno(ret);
5370 goto out_commit;
5371 }
5372
dbdcf6a4 5373 ret = ocfs2_remove_extent(handle, &et, cpos, len, meta_ac,
f99b9b7c 5374 &dealloc);
01225596
TM
5375 if (ret) {
5376 mlog_errno(ret);
5377 goto out_commit;
5378 }
5379
5380 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
5381
5382 ret = ocfs2_journal_dirty(handle, root_bh);
5383 if (ret) {
5384 mlog_errno(ret);
5385 goto out_commit;
5386 }
5387
5388 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
5389 if (ret)
5390 mlog_errno(ret);
5391
5392out_commit:
5393 ocfs2_commit_trans(osb, handle);
5394out:
5395 ocfs2_schedule_truncate_log_flush(osb, 1);
5396
5397 mutex_unlock(&tl_inode->i_mutex);
5398
5399 if (meta_ac)
5400 ocfs2_free_alloc_context(meta_ac);
5401
5402 ocfs2_run_deallocs(osb, &dealloc);
5403
5404 return ret;
5405}
5406
01225596
TM
5407/*
5408 * Set the xattr name/value in the bucket specified in xs.
01225596
TM
5409 */
5410static int ocfs2_xattr_set_in_bucket(struct inode *inode,
5411 struct ocfs2_xattr_info *xi,
78f30c31
TM
5412 struct ocfs2_xattr_search *xs,
5413 struct ocfs2_xattr_set_ctxt *ctxt)
01225596 5414{
9dc47400 5415 int ret;
73857ee0
JB
5416 u64 blkno;
5417 struct ocfs2_xa_loc loc;
6b240ff6 5418 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->xi_name,
18853b95 5419 xi->xi_name_len);
01225596 5420
73857ee0
JB
5421 if (!xs->bucket->bu_bhs[1]) {
5422 blkno = bucket_blkno(xs->bucket);
5423 ocfs2_xattr_bucket_relse(xs->bucket);
5424 ret = ocfs2_read_xattr_bucket(xs->bucket, blkno);
5425 if (ret) {
5426 mlog_errno(ret);
01225596 5427 goto out;
73857ee0 5428 }
01225596
TM
5429 }
5430
73857ee0
JB
5431 ocfs2_init_xattr_bucket_xa_loc(&loc, xs->bucket,
5432 xs->not_found ? NULL : xs->here);
5433 ret = ocfs2_xa_journal_access(ctxt->handle, &loc,
5434 OCFS2_JOURNAL_ACCESS_WRITE);
5435 if (ret < 0) {
01225596
TM
5436 mlog_errno(ret);
5437 goto out;
5438 }
5439
73857ee0
JB
5440 ret = ocfs2_xa_prepare_entry(&loc, xi, name_hash, ctxt);
5441 if (ret) {
5442 if (ret != -ENOSPC)
5443 mlog_errno(ret);
5a095611 5444 goto out;
73857ee0
JB
5445 }
5446 xs->here = loc.xl_entry;
01225596 5447
73857ee0 5448 ret = ocfs2_xa_store_value(&loc, xi, ctxt);
5a095611
TM
5449 if (ret) {
5450 mlog_errno(ret);
01225596
TM
5451 goto out;
5452 }
5453
73857ee0
JB
5454 ocfs2_xa_journal_dirty(ctxt->handle, &loc);
5455
01225596
TM
5456out:
5457 return ret;
5458}
5459
80bcaf34
TM
5460/*
5461 * check whether the xattr bucket is filled up with the same hash value.
5462 * If we want to insert the xattr with the same hash, return -ENOSPC.
5463 * If we want to insert a xattr with different hash value, go ahead
5464 * and ocfs2_divide_xattr_bucket will handle this.
5465 */
01225596 5466static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
80bcaf34
TM
5467 struct ocfs2_xattr_bucket *bucket,
5468 const char *name)
01225596 5469{
3e632946 5470 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
80bcaf34
TM
5471 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
5472
5473 if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
5474 return 0;
01225596
TM
5475
5476 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
5477 xh->xh_entries[0].xe_name_hash) {
5478 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
5479 "hash = %u\n",
9c7759aa 5480 (unsigned long long)bucket_blkno(bucket),
01225596
TM
5481 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
5482 return -ENOSPC;
5483 }
5484
5485 return 0;
5486}
5487
5488static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
5489 struct ocfs2_xattr_info *xi,
78f30c31
TM
5490 struct ocfs2_xattr_search *xs,
5491 struct ocfs2_xattr_set_ctxt *ctxt)
01225596
TM
5492{
5493 struct ocfs2_xattr_header *xh;
5494 struct ocfs2_xattr_entry *xe;
5495 u16 count, header_size, xh_free_start;
6dde41d9 5496 int free, max_free, need, old;
18853b95 5497 size_t value_size = 0;
01225596
TM
5498 size_t blocksize = inode->i_sb->s_blocksize;
5499 int ret, allocation = 0;
01225596 5500
6b240ff6 5501 mlog_entry("Set xattr %s in xattr index block\n", xi->xi_name);
01225596
TM
5502
5503try_again:
5504 xh = xs->header;
5505 count = le16_to_cpu(xh->xh_count);
5506 xh_free_start = le16_to_cpu(xh->xh_free_start);
5507 header_size = sizeof(struct ocfs2_xattr_header) +
5508 count * sizeof(struct ocfs2_xattr_entry);
4442f518
TY
5509 max_free = OCFS2_XATTR_BUCKET_SIZE - header_size -
5510 le16_to_cpu(xh->xh_name_value_len) - OCFS2_XATTR_HEADER_GAP;
01225596
TM
5511
5512 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
5513 "of %u which exceed block size\n",
ba937127 5514 (unsigned long long)bucket_blkno(xs->bucket),
01225596
TM
5515 header_size);
5516
6b240ff6 5517 if (xi->xi_value && xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
01225596 5518 value_size = OCFS2_XATTR_ROOT_SIZE;
6b240ff6
JB
5519 else if (xi->xi_value)
5520 value_size = OCFS2_XATTR_SIZE(xi->xi_value_len);
01225596
TM
5521
5522 if (xs->not_found)
5523 need = sizeof(struct ocfs2_xattr_entry) +
18853b95 5524 OCFS2_XATTR_SIZE(xi->xi_name_len) + value_size;
01225596 5525 else {
18853b95 5526 need = value_size + OCFS2_XATTR_SIZE(xi->xi_name_len);
01225596
TM
5527
5528 /*
5529 * We only replace the old value if the new length is smaller
5530 * than the old one. Otherwise we will allocate new space in the
5531 * bucket to store it.
5532 */
5533 xe = xs->here;
5534 if (ocfs2_xattr_is_local(xe))
5535 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
5536 else
5537 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
5538
5539 if (old >= value_size)
5540 need = 0;
5541 }
5542
4442f518 5543 free = xh_free_start - header_size - OCFS2_XATTR_HEADER_GAP;
01225596
TM
5544 /*
5545 * We need to make sure the new name/value pair
5546 * can exist in the same block.
5547 */
5548 if (xh_free_start % blocksize < need)
5549 free -= xh_free_start % blocksize;
5550
5551 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
5552 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
5553 " %u\n", xs->not_found,
ba937127 5554 (unsigned long long)bucket_blkno(xs->bucket),
01225596
TM
5555 free, need, max_free, le16_to_cpu(xh->xh_free_start),
5556 le16_to_cpu(xh->xh_name_value_len));
5557
976331d8
TM
5558 if (free < need ||
5559 (xs->not_found &&
5560 count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb))) {
01225596
TM
5561 if (need <= max_free &&
5562 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
5563 /*
5564 * We can create the space by defragment. Since only the
5565 * name/value will be moved, the xe shouldn't be changed
5566 * in xs.
5567 */
85db90e7
TM
5568 ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
5569 xs->bucket);
01225596
TM
5570 if (ret) {
5571 mlog_errno(ret);
5572 goto out;
5573 }
5574
5575 xh_free_start = le16_to_cpu(xh->xh_free_start);
4442f518
TY
5576 free = xh_free_start - header_size
5577 - OCFS2_XATTR_HEADER_GAP;
01225596
TM
5578 if (xh_free_start % blocksize < need)
5579 free -= xh_free_start % blocksize;
5580
5581 if (free >= need)
5582 goto xattr_set;
5583
5584 mlog(0, "Can't get enough space for xattr insert by "
5585 "defragment. Need %u bytes, but we have %d, so "
5586 "allocate new bucket for it.\n", need, free);
5587 }
5588
5589 /*
5590 * We have to add new buckets or clusters and one
5591 * allocation should leave us enough space for insert.
5592 */
5593 BUG_ON(allocation);
5594
5595 /*
5596 * We do not allow for overlapping ranges between buckets. And
5597 * the maximum number of collisions we will allow for then is
5598 * one bucket's worth, so check it here whether we need to
5599 * add a new bucket for the insert.
5600 */
80bcaf34 5601 ret = ocfs2_check_xattr_bucket_collision(inode,
ba937127 5602 xs->bucket,
6b240ff6 5603 xi->xi_name);
01225596
TM
5604 if (ret) {
5605 mlog_errno(ret);
5606 goto out;
5607 }
5608
5609 ret = ocfs2_add_new_xattr_bucket(inode,
5610 xs->xattr_bh,
91f2033f 5611 xs->bucket,
78f30c31 5612 ctxt);
01225596
TM
5613 if (ret) {
5614 mlog_errno(ret);
5615 goto out;
5616 }
5617
91f2033f
JB
5618 /*
5619 * ocfs2_add_new_xattr_bucket() will have updated
5620 * xs->bucket if it moved, but it will not have updated
5621 * any of the other search fields. Thus, we drop it and
5622 * re-search. Everything should be cached, so it'll be
5623 * quick.
5624 */
ba937127 5625 ocfs2_xattr_bucket_relse(xs->bucket);
01225596 5626 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
6b240ff6
JB
5627 xi->xi_name_index,
5628 xi->xi_name, xs);
01225596
TM
5629 if (ret && ret != -ENODATA)
5630 goto out;
5631 xs->not_found = ret;
5632 allocation = 1;
5633 goto try_again;
5634 }
5635
5636xattr_set:
78f30c31 5637 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs, ctxt);
01225596
TM
5638out:
5639 mlog_exit(ret);
5640 return ret;
5641}
a3944256
TM
5642
5643static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
5644 struct ocfs2_xattr_bucket *bucket,
5645 void *para)
5646{
ce9c5a54 5647 int ret = 0, ref_credits;
3e632946 5648 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
a3944256
TM
5649 u16 i;
5650 struct ocfs2_xattr_entry *xe;
78f30c31
TM
5651 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5652 struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
548b0f22
JB
5653 int credits = ocfs2_remove_extent_credits(osb->sb) +
5654 ocfs2_blocks_per_xattr_bucket(inode->i_sb);
ce9c5a54
TM
5655 struct ocfs2_xattr_value_root *xv;
5656 struct ocfs2_rm_xattr_bucket_para *args =
5657 (struct ocfs2_rm_xattr_bucket_para *)para;
78f30c31
TM
5658
5659 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
a3944256
TM
5660
5661 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
5662 xe = &xh->xh_entries[i];
5663 if (ocfs2_xattr_is_local(xe))
5664 continue;
5665
ce9c5a54
TM
5666 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket,
5667 i, &xv, NULL);
5668
5669 ret = ocfs2_lock_xattr_remove_allocators(inode, xv,
5670 args->ref_ci,
5671 args->ref_root_bh,
5672 &ctxt.meta_ac,
5673 &ref_credits);
5674
5675 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
88c3b062
TM
5676 if (IS_ERR(ctxt.handle)) {
5677 ret = PTR_ERR(ctxt.handle);
5678 mlog_errno(ret);
5679 break;
5680 }
5681
548b0f22 5682 ret = ocfs2_xattr_bucket_value_truncate(inode, bucket,
78f30c31 5683 i, 0, &ctxt);
88c3b062
TM
5684
5685 ocfs2_commit_trans(osb, ctxt.handle);
ce9c5a54
TM
5686 if (ctxt.meta_ac) {
5687 ocfs2_free_alloc_context(ctxt.meta_ac);
5688 ctxt.meta_ac = NULL;
5689 }
a3944256
TM
5690 if (ret) {
5691 mlog_errno(ret);
5692 break;
5693 }
5694 }
5695
ce9c5a54
TM
5696 if (ctxt.meta_ac)
5697 ocfs2_free_alloc_context(ctxt.meta_ac);
78f30c31
TM
5698 ocfs2_schedule_truncate_log_flush(osb, 1);
5699 ocfs2_run_deallocs(osb, &ctxt.dealloc);
a3944256
TM
5700 return ret;
5701}
5702
492a8a33
TM
5703/*
5704 * Whenever we modify a xattr value root in the bucket(e.g, CoW
5705 * or change the extent record flag), we need to recalculate
5706 * the metaecc for the whole bucket. So it is done here.
5707 *
5708 * Note:
5709 * We have to give the extra credits for the caller.
5710 */
5711static int ocfs2_xattr_bucket_post_refcount(struct inode *inode,
5712 handle_t *handle,
5713 void *para)
5714{
5715 int ret;
5716 struct ocfs2_xattr_bucket *bucket =
5717 (struct ocfs2_xattr_bucket *)para;
5718
5719 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
5720 OCFS2_JOURNAL_ACCESS_WRITE);
5721 if (ret) {
5722 mlog_errno(ret);
5723 return ret;
5724 }
5725
5726 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
5727
5728 return 0;
5729}
5730
5731/*
5732 * Special action we need if the xattr value is refcounted.
5733 *
5734 * 1. If the xattr is refcounted, lock the tree.
5735 * 2. CoW the xattr if we are setting the new value and the value
5736 * will be stored outside.
5737 * 3. In other case, decrease_refcount will work for us, so just
5738 * lock the refcount tree, calculate the meta and credits is OK.
5739 *
5740 * We have to do CoW before ocfs2_init_xattr_set_ctxt since
5741 * currently CoW is a completed transaction, while this function
5742 * will also lock the allocators and let us deadlock. So we will
5743 * CoW the whole xattr value.
5744 */
5745static int ocfs2_prepare_refcount_xattr(struct inode *inode,
5746 struct ocfs2_dinode *di,
5747 struct ocfs2_xattr_info *xi,
5748 struct ocfs2_xattr_search *xis,
5749 struct ocfs2_xattr_search *xbs,
5750 struct ocfs2_refcount_tree **ref_tree,
5751 int *meta_add,
5752 int *credits)
5753{
5754 int ret = 0;
5755 struct ocfs2_xattr_block *xb;
5756 struct ocfs2_xattr_entry *xe;
5757 char *base;
5758 u32 p_cluster, num_clusters;
5759 unsigned int ext_flags;
5760 int name_offset, name_len;
5761 struct ocfs2_xattr_value_buf vb;
5762 struct ocfs2_xattr_bucket *bucket = NULL;
5763 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5764 struct ocfs2_post_refcount refcount;
5765 struct ocfs2_post_refcount *p = NULL;
5766 struct buffer_head *ref_root_bh = NULL;
5767
5768 if (!xis->not_found) {
5769 xe = xis->here;
5770 name_offset = le16_to_cpu(xe->xe_name_offset);
5771 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5772 base = xis->base;
5773 vb.vb_bh = xis->inode_bh;
5774 vb.vb_access = ocfs2_journal_access_di;
5775 } else {
5776 int i, block_off = 0;
5777 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
5778 xe = xbs->here;
5779 name_offset = le16_to_cpu(xe->xe_name_offset);
5780 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5781 i = xbs->here - xbs->header->xh_entries;
5782
5783 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
fd68a894 5784 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
492a8a33
TM
5785 bucket_xh(xbs->bucket),
5786 i, &block_off,
5787 &name_offset);
5788 if (ret) {
5789 mlog_errno(ret);
5790 goto out;
5791 }
5792 base = bucket_block(xbs->bucket, block_off);
5793 vb.vb_bh = xbs->bucket->bu_bhs[block_off];
5794 vb.vb_access = ocfs2_journal_access;
5795
5796 if (ocfs2_meta_ecc(osb)) {
5797 /*create parameters for ocfs2_post_refcount. */
5798 bucket = xbs->bucket;
5799 refcount.credits = bucket->bu_blocks;
5800 refcount.para = bucket;
5801 refcount.func =
5802 ocfs2_xattr_bucket_post_refcount;
5803 p = &refcount;
5804 }
5805 } else {
5806 base = xbs->base;
5807 vb.vb_bh = xbs->xattr_bh;
5808 vb.vb_access = ocfs2_journal_access_xb;
5809 }
5810 }
5811
5812 if (ocfs2_xattr_is_local(xe))
5813 goto out;
5814
5815 vb.vb_xv = (struct ocfs2_xattr_value_root *)
5816 (base + name_offset + name_len);
5817
5818 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
5819 &num_clusters, &vb.vb_xv->xr_list,
5820 &ext_flags);
5821 if (ret) {
5822 mlog_errno(ret);
5823 goto out;
5824 }
5825
5826 /*
5827 * We just need to check the 1st extent record, since we always
5828 * CoW the whole xattr. So there shouldn't be a xattr with
5829 * some REFCOUNT extent recs after the 1st one.
5830 */
5831 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
5832 goto out;
5833
5834 ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
5835 1, ref_tree, &ref_root_bh);
5836 if (ret) {
5837 mlog_errno(ret);
5838 goto out;
5839 }
5840
5841 /*
5842 * If we are deleting the xattr or the new size will be stored inside,
5843 * cool, leave it there, the xattr truncate process will remove them
5844 * for us(it still needs the refcount tree lock and the meta, credits).
5845 * And the worse case is that every cluster truncate will split the
5846 * refcount tree, and make the original extent become 3. So we will need
5847 * 2 * cluster more extent recs at most.
5848 */
6b240ff6 5849 if (!xi->xi_value || xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE) {
492a8a33
TM
5850
5851 ret = ocfs2_refcounted_xattr_delete_need(inode,
5852 &(*ref_tree)->rf_ci,
5853 ref_root_bh, vb.vb_xv,
5854 meta_add, credits);
5855 if (ret)
5856 mlog_errno(ret);
5857 goto out;
5858 }
5859
5860 ret = ocfs2_refcount_cow_xattr(inode, di, &vb,
5861 *ref_tree, ref_root_bh, 0,
5862 le32_to_cpu(vb.vb_xv->xr_clusters), p);
5863 if (ret)
5864 mlog_errno(ret);
5865
5866out:
5867 brelse(ref_root_bh);
5868 return ret;
5869}
5870
0129241e
TM
5871/*
5872 * Add the REFCOUNTED flags for all the extent rec in ocfs2_xattr_value_root.
5873 * The physical clusters will be added to refcount tree.
5874 */
5875static int ocfs2_xattr_value_attach_refcount(struct inode *inode,
5876 struct ocfs2_xattr_value_root *xv,
5877 struct ocfs2_extent_tree *value_et,
5878 struct ocfs2_caching_info *ref_ci,
5879 struct buffer_head *ref_root_bh,
5880 struct ocfs2_cached_dealloc_ctxt *dealloc,
5881 struct ocfs2_post_refcount *refcount)
5882{
5883 int ret = 0;
5884 u32 clusters = le32_to_cpu(xv->xr_clusters);
5885 u32 cpos, p_cluster, num_clusters;
5886 struct ocfs2_extent_list *el = &xv->xr_list;
5887 unsigned int ext_flags;
5888
5889 cpos = 0;
5890 while (cpos < clusters) {
5891 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
5892 &num_clusters, el, &ext_flags);
5893
5894 cpos += num_clusters;
5895 if ((ext_flags & OCFS2_EXT_REFCOUNTED))
5896 continue;
5897
5898 BUG_ON(!p_cluster);
5899
5900 ret = ocfs2_add_refcount_flag(inode, value_et,
5901 ref_ci, ref_root_bh,
5902 cpos - num_clusters,
5903 p_cluster, num_clusters,
5904 dealloc, refcount);
5905 if (ret) {
5906 mlog_errno(ret);
5907 break;
5908 }
5909 }
5910
5911 return ret;
5912}
5913
5914/*
5915 * Given a normal ocfs2_xattr_header, refcount all the entries which
5916 * have value stored outside.
5917 * Used for xattrs stored in inode and ocfs2_xattr_block.
5918 */
5919static int ocfs2_xattr_attach_refcount_normal(struct inode *inode,
5920 struct ocfs2_xattr_value_buf *vb,
5921 struct ocfs2_xattr_header *header,
5922 struct ocfs2_caching_info *ref_ci,
5923 struct buffer_head *ref_root_bh,
5924 struct ocfs2_cached_dealloc_ctxt *dealloc)
5925{
5926
5927 struct ocfs2_xattr_entry *xe;
5928 struct ocfs2_xattr_value_root *xv;
5929 struct ocfs2_extent_tree et;
5930 int i, ret = 0;
5931
5932 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
5933 xe = &header->xh_entries[i];
5934
5935 if (ocfs2_xattr_is_local(xe))
5936 continue;
5937
5938 xv = (struct ocfs2_xattr_value_root *)((void *)header +
5939 le16_to_cpu(xe->xe_name_offset) +
5940 OCFS2_XATTR_SIZE(xe->xe_name_len));
5941
5942 vb->vb_xv = xv;
5943 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
5944
5945 ret = ocfs2_xattr_value_attach_refcount(inode, xv, &et,
5946 ref_ci, ref_root_bh,
5947 dealloc, NULL);
5948 if (ret) {
5949 mlog_errno(ret);
5950 break;
5951 }
5952 }
5953
5954 return ret;
5955}
5956
5957static int ocfs2_xattr_inline_attach_refcount(struct inode *inode,
5958 struct buffer_head *fe_bh,
5959 struct ocfs2_caching_info *ref_ci,
5960 struct buffer_head *ref_root_bh,
5961 struct ocfs2_cached_dealloc_ctxt *dealloc)
5962{
5963 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
5964 struct ocfs2_xattr_header *header = (struct ocfs2_xattr_header *)
5965 (fe_bh->b_data + inode->i_sb->s_blocksize -
5966 le16_to_cpu(di->i_xattr_inline_size));
5967 struct ocfs2_xattr_value_buf vb = {
5968 .vb_bh = fe_bh,
5969 .vb_access = ocfs2_journal_access_di,
5970 };
5971
5972 return ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
5973 ref_ci, ref_root_bh, dealloc);
5974}
5975
5976struct ocfs2_xattr_tree_value_refcount_para {
5977 struct ocfs2_caching_info *ref_ci;
5978 struct buffer_head *ref_root_bh;
5979 struct ocfs2_cached_dealloc_ctxt *dealloc;
5980};
5981
5982static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
5983 struct ocfs2_xattr_bucket *bucket,
5984 int offset,
5985 struct ocfs2_xattr_value_root **xv,
5986 struct buffer_head **bh)
5987{
5988 int ret, block_off, name_offset;
5989 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5990 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
5991 void *base;
5992
5993 ret = ocfs2_xattr_bucket_get_name_value(sb,
5994 bucket_xh(bucket),
5995 offset,
5996 &block_off,
5997 &name_offset);
5998 if (ret) {
5999 mlog_errno(ret);
6000 goto out;
6001 }
6002
6003 base = bucket_block(bucket, block_off);
6004
6005 *xv = (struct ocfs2_xattr_value_root *)(base + name_offset +
6006 OCFS2_XATTR_SIZE(xe->xe_name_len));
6007
6008 if (bh)
6009 *bh = bucket->bu_bhs[block_off];
6010out:
6011 return ret;
6012}
6013
6014/*
6015 * For a given xattr bucket, refcount all the entries which
6016 * have value stored outside.
6017 */
6018static int ocfs2_xattr_bucket_value_refcount(struct inode *inode,
6019 struct ocfs2_xattr_bucket *bucket,
6020 void *para)
6021{
6022 int i, ret = 0;
6023 struct ocfs2_extent_tree et;
6024 struct ocfs2_xattr_tree_value_refcount_para *ref =
6025 (struct ocfs2_xattr_tree_value_refcount_para *)para;
6026 struct ocfs2_xattr_header *xh =
6027 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6028 struct ocfs2_xattr_entry *xe;
6029 struct ocfs2_xattr_value_buf vb = {
6030 .vb_access = ocfs2_journal_access,
6031 };
6032 struct ocfs2_post_refcount refcount = {
6033 .credits = bucket->bu_blocks,
6034 .para = bucket,
6035 .func = ocfs2_xattr_bucket_post_refcount,
6036 };
6037 struct ocfs2_post_refcount *p = NULL;
6038
6039 /* We only need post_refcount if we support metaecc. */
6040 if (ocfs2_meta_ecc(OCFS2_SB(inode->i_sb)))
6041 p = &refcount;
6042
6043 mlog(0, "refcount bucket %llu, count = %u\n",
6044 (unsigned long long)bucket_blkno(bucket),
6045 le16_to_cpu(xh->xh_count));
6046 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6047 xe = &xh->xh_entries[i];
6048
6049 if (ocfs2_xattr_is_local(xe))
6050 continue;
6051
6052 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket, i,
6053 &vb.vb_xv, &vb.vb_bh);
6054 if (ret) {
6055 mlog_errno(ret);
6056 break;
6057 }
6058
6059 ocfs2_init_xattr_value_extent_tree(&et,
6060 INODE_CACHE(inode), &vb);
6061
6062 ret = ocfs2_xattr_value_attach_refcount(inode, vb.vb_xv,
6063 &et, ref->ref_ci,
6064 ref->ref_root_bh,
6065 ref->dealloc, p);
6066 if (ret) {
6067 mlog_errno(ret);
6068 break;
6069 }
6070 }
6071
6072 return ret;
6073
6074}
6075
6076static int ocfs2_refcount_xattr_tree_rec(struct inode *inode,
6077 struct buffer_head *root_bh,
6078 u64 blkno, u32 cpos, u32 len, void *para)
6079{
6080 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
6081 ocfs2_xattr_bucket_value_refcount,
6082 para);
6083}
6084
6085static int ocfs2_xattr_block_attach_refcount(struct inode *inode,
6086 struct buffer_head *blk_bh,
6087 struct ocfs2_caching_info *ref_ci,
6088 struct buffer_head *ref_root_bh,
6089 struct ocfs2_cached_dealloc_ctxt *dealloc)
6090{
6091 int ret = 0;
6092 struct ocfs2_xattr_block *xb =
6093 (struct ocfs2_xattr_block *)blk_bh->b_data;
6094
6095 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
6096 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
6097 struct ocfs2_xattr_value_buf vb = {
6098 .vb_bh = blk_bh,
6099 .vb_access = ocfs2_journal_access_xb,
6100 };
6101
6102 ret = ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
6103 ref_ci, ref_root_bh,
6104 dealloc);
6105 } else {
6106 struct ocfs2_xattr_tree_value_refcount_para para = {
6107 .ref_ci = ref_ci,
6108 .ref_root_bh = ref_root_bh,
6109 .dealloc = dealloc,
6110 };
6111
6112 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
6113 ocfs2_refcount_xattr_tree_rec,
6114 &para);
6115 }
6116
6117 return ret;
6118}
6119
6120int ocfs2_xattr_attach_refcount_tree(struct inode *inode,
6121 struct buffer_head *fe_bh,
6122 struct ocfs2_caching_info *ref_ci,
6123 struct buffer_head *ref_root_bh,
6124 struct ocfs2_cached_dealloc_ctxt *dealloc)
6125{
6126 int ret = 0;
6127 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6128 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
6129 struct buffer_head *blk_bh = NULL;
6130
6131 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
6132 ret = ocfs2_xattr_inline_attach_refcount(inode, fe_bh,
6133 ref_ci, ref_root_bh,
6134 dealloc);
6135 if (ret) {
6136 mlog_errno(ret);
6137 goto out;
6138 }
6139 }
6140
6141 if (!di->i_xattr_loc)
6142 goto out;
6143
6144 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
6145 &blk_bh);
6146 if (ret < 0) {
6147 mlog_errno(ret);
6148 goto out;
6149 }
6150
6151 ret = ocfs2_xattr_block_attach_refcount(inode, blk_bh, ref_ci,
6152 ref_root_bh, dealloc);
6153 if (ret)
6154 mlog_errno(ret);
6155
6156 brelse(blk_bh);
6157out:
6158
6159 return ret;
6160}
6161
0fe9b66c 6162typedef int (should_xattr_reflinked)(struct ocfs2_xattr_entry *xe);
2999d12f
TM
6163/*
6164 * Store the information we need in xattr reflink.
6165 * old_bh and new_bh are inode bh for the old and new inode.
6166 */
6167struct ocfs2_xattr_reflink {
6168 struct inode *old_inode;
6169 struct inode *new_inode;
6170 struct buffer_head *old_bh;
6171 struct buffer_head *new_bh;
6172 struct ocfs2_caching_info *ref_ci;
6173 struct buffer_head *ref_root_bh;
6174 struct ocfs2_cached_dealloc_ctxt *dealloc;
0fe9b66c 6175 should_xattr_reflinked *xattr_reflinked;
2999d12f
TM
6176};
6177
6178/*
6179 * Given a xattr header and xe offset,
6180 * return the proper xv and the corresponding bh.
6181 * xattr in inode, block and xattr tree have different implementaions.
6182 */
6183typedef int (get_xattr_value_root)(struct super_block *sb,
6184 struct buffer_head *bh,
6185 struct ocfs2_xattr_header *xh,
6186 int offset,
6187 struct ocfs2_xattr_value_root **xv,
6188 struct buffer_head **ret_bh,
6189 void *para);
6190
6191/*
6192 * Calculate all the xattr value root metadata stored in this xattr header and
6193 * credits we need if we create them from the scratch.
6194 * We use get_xattr_value_root so that all types of xattr container can use it.
6195 */
6196static int ocfs2_value_metas_in_xattr_header(struct super_block *sb,
6197 struct buffer_head *bh,
6198 struct ocfs2_xattr_header *xh,
6199 int *metas, int *credits,
6200 int *num_recs,
6201 get_xattr_value_root *func,
6202 void *para)
6203{
6204 int i, ret = 0;
6205 struct ocfs2_xattr_value_root *xv;
6206 struct ocfs2_xattr_entry *xe;
6207
6208 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6209 xe = &xh->xh_entries[i];
6210 if (ocfs2_xattr_is_local(xe))
6211 continue;
6212
6213 ret = func(sb, bh, xh, i, &xv, NULL, para);
6214 if (ret) {
6215 mlog_errno(ret);
6216 break;
6217 }
6218
6219 *metas += le16_to_cpu(xv->xr_list.l_tree_depth) *
6220 le16_to_cpu(xv->xr_list.l_next_free_rec);
6221
6222 *credits += ocfs2_calc_extend_credits(sb,
6223 &def_xv.xv.xr_list,
6224 le32_to_cpu(xv->xr_clusters));
6225
6226 /*
6227 * If the value is a tree with depth > 1, We don't go deep
6228 * to the extent block, so just calculate a maximum record num.
6229 */
6230 if (!xv->xr_list.l_tree_depth)
8ff6af88 6231 *num_recs += le16_to_cpu(xv->xr_list.l_next_free_rec);
2999d12f
TM
6232 else
6233 *num_recs += ocfs2_clusters_for_bytes(sb,
6234 XATTR_SIZE_MAX);
6235 }
6236
6237 return ret;
6238}
6239
6240/* Used by xattr inode and block to return the right xv and buffer_head. */
6241static int ocfs2_get_xattr_value_root(struct super_block *sb,
6242 struct buffer_head *bh,
6243 struct ocfs2_xattr_header *xh,
6244 int offset,
6245 struct ocfs2_xattr_value_root **xv,
6246 struct buffer_head **ret_bh,
6247 void *para)
6248{
6249 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
6250
6251 *xv = (struct ocfs2_xattr_value_root *)((void *)xh +
6252 le16_to_cpu(xe->xe_name_offset) +
6253 OCFS2_XATTR_SIZE(xe->xe_name_len));
6254
6255 if (ret_bh)
6256 *ret_bh = bh;
6257
6258 return 0;
6259}
6260
6261/*
6262 * Lock the meta_ac and caculate how much credits we need for reflink xattrs.
6263 * It is only used for inline xattr and xattr block.
6264 */
6265static int ocfs2_reflink_lock_xattr_allocators(struct ocfs2_super *osb,
6266 struct ocfs2_xattr_header *xh,
6267 struct buffer_head *ref_root_bh,
6268 int *credits,
6269 struct ocfs2_alloc_context **meta_ac)
6270{
6271 int ret, meta_add = 0, num_recs = 0;
6272 struct ocfs2_refcount_block *rb =
6273 (struct ocfs2_refcount_block *)ref_root_bh->b_data;
6274
6275 *credits = 0;
6276
6277 ret = ocfs2_value_metas_in_xattr_header(osb->sb, NULL, xh,
6278 &meta_add, credits, &num_recs,
6279 ocfs2_get_xattr_value_root,
6280 NULL);
6281 if (ret) {
6282 mlog_errno(ret);
6283 goto out;
6284 }
6285
6286 /*
6287 * We need to add/modify num_recs in refcount tree, so just calculate
6288 * an approximate number we need for refcount tree change.
6289 * Sometimes we need to split the tree, and after split, half recs
6290 * will be moved to the new block, and a new block can only provide
6291 * half number of recs. So we multiple new blocks by 2.
6292 */
6293 num_recs = num_recs / ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6294 meta_add += num_recs;
6295 *credits += num_recs + num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6296 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6297 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6298 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6299 else
6300 *credits += 1;
6301
6302 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add, meta_ac);
6303 if (ret)
6304 mlog_errno(ret);
6305
6306out:
6307 return ret;
6308}
6309
6310/*
6311 * Given a xattr header, reflink all the xattrs in this container.
6312 * It can be used for inode, block and bucket.
6313 *
6314 * NOTE:
6315 * Before we call this function, the caller has memcpy the xattr in
6316 * old_xh to the new_xh.
0fe9b66c
TM
6317 *
6318 * If args.xattr_reflinked is set, call it to decide whether the xe should
6319 * be reflinked or not. If not, remove it from the new xattr header.
2999d12f
TM
6320 */
6321static int ocfs2_reflink_xattr_header(handle_t *handle,
6322 struct ocfs2_xattr_reflink *args,
6323 struct buffer_head *old_bh,
6324 struct ocfs2_xattr_header *xh,
6325 struct buffer_head *new_bh,
6326 struct ocfs2_xattr_header *new_xh,
6327 struct ocfs2_xattr_value_buf *vb,
6328 struct ocfs2_alloc_context *meta_ac,
6329 get_xattr_value_root *func,
6330 void *para)
6331{
0fe9b66c 6332 int ret = 0, i, j;
2999d12f
TM
6333 struct super_block *sb = args->old_inode->i_sb;
6334 struct buffer_head *value_bh;
0fe9b66c 6335 struct ocfs2_xattr_entry *xe, *last;
2999d12f
TM
6336 struct ocfs2_xattr_value_root *xv, *new_xv;
6337 struct ocfs2_extent_tree data_et;
6338 u32 clusters, cpos, p_cluster, num_clusters;
6339 unsigned int ext_flags = 0;
6340
6341 mlog(0, "reflink xattr in container %llu, count = %u\n",
6342 (unsigned long long)old_bh->b_blocknr, le16_to_cpu(xh->xh_count));
0fe9b66c
TM
6343
6344 last = &new_xh->xh_entries[le16_to_cpu(new_xh->xh_count)];
6345 for (i = 0, j = 0; i < le16_to_cpu(xh->xh_count); i++, j++) {
2999d12f
TM
6346 xe = &xh->xh_entries[i];
6347
0fe9b66c
TM
6348 if (args->xattr_reflinked && !args->xattr_reflinked(xe)) {
6349 xe = &new_xh->xh_entries[j];
6350
6351 le16_add_cpu(&new_xh->xh_count, -1);
6352 if (new_xh->xh_count) {
6353 memmove(xe, xe + 1,
6354 (void *)last - (void *)xe);
6355 memset(last, 0,
6356 sizeof(struct ocfs2_xattr_entry));
6357 }
6358
6359 /*
6360 * We don't want j to increase in the next round since
6361 * it is already moved ahead.
6362 */
6363 j--;
6364 continue;
6365 }
6366
2999d12f
TM
6367 if (ocfs2_xattr_is_local(xe))
6368 continue;
6369
6370 ret = func(sb, old_bh, xh, i, &xv, NULL, para);
6371 if (ret) {
6372 mlog_errno(ret);
6373 break;
6374 }
6375
0fe9b66c 6376 ret = func(sb, new_bh, new_xh, j, &new_xv, &value_bh, para);
2999d12f
TM
6377 if (ret) {
6378 mlog_errno(ret);
6379 break;
6380 }
6381
6382 /*
6383 * For the xattr which has l_tree_depth = 0, all the extent
6384 * recs have already be copied to the new xh with the
6385 * propriate OCFS2_EXT_REFCOUNTED flag we just need to
6386 * increase the refount count int the refcount tree.
6387 *
6388 * For the xattr which has l_tree_depth > 0, we need
6389 * to initialize it to the empty default value root,
6390 * and then insert the extents one by one.
6391 */
6392 if (xv->xr_list.l_tree_depth) {
6393 memcpy(new_xv, &def_xv, sizeof(def_xv));
6394 vb->vb_xv = new_xv;
6395 vb->vb_bh = value_bh;
6396 ocfs2_init_xattr_value_extent_tree(&data_et,
6397 INODE_CACHE(args->new_inode), vb);
6398 }
6399
6400 clusters = le32_to_cpu(xv->xr_clusters);
6401 cpos = 0;
6402 while (cpos < clusters) {
6403 ret = ocfs2_xattr_get_clusters(args->old_inode,
6404 cpos,
6405 &p_cluster,
6406 &num_clusters,
6407 &xv->xr_list,
6408 &ext_flags);
6409 if (ret) {
6410 mlog_errno(ret);
6411 goto out;
6412 }
6413
6414 BUG_ON(!p_cluster);
6415
6416 if (xv->xr_list.l_tree_depth) {
6417 ret = ocfs2_insert_extent(handle,
6418 &data_et, cpos,
6419 ocfs2_clusters_to_blocks(
6420 args->old_inode->i_sb,
6421 p_cluster),
6422 num_clusters, ext_flags,
6423 meta_ac);
6424 if (ret) {
6425 mlog_errno(ret);
6426 goto out;
6427 }
6428 }
6429
6430 ret = ocfs2_increase_refcount(handle, args->ref_ci,
6431 args->ref_root_bh,
6432 p_cluster, num_clusters,
6433 meta_ac, args->dealloc);
6434 if (ret) {
6435 mlog_errno(ret);
6436 goto out;
6437 }
6438
6439 cpos += num_clusters;
6440 }
6441 }
6442
6443out:
6444 return ret;
6445}
6446
6447static int ocfs2_reflink_xattr_inline(struct ocfs2_xattr_reflink *args)
6448{
6449 int ret = 0, credits = 0;
6450 handle_t *handle;
6451 struct ocfs2_super *osb = OCFS2_SB(args->old_inode->i_sb);
6452 struct ocfs2_dinode *di = (struct ocfs2_dinode *)args->old_bh->b_data;
6453 int inline_size = le16_to_cpu(di->i_xattr_inline_size);
6454 int header_off = osb->sb->s_blocksize - inline_size;
6455 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)
6456 (args->old_bh->b_data + header_off);
6457 struct ocfs2_xattr_header *new_xh = (struct ocfs2_xattr_header *)
6458 (args->new_bh->b_data + header_off);
6459 struct ocfs2_alloc_context *meta_ac = NULL;
6460 struct ocfs2_inode_info *new_oi;
6461 struct ocfs2_dinode *new_di;
6462 struct ocfs2_xattr_value_buf vb = {
6463 .vb_bh = args->new_bh,
6464 .vb_access = ocfs2_journal_access_di,
6465 };
6466
6467 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6468 &credits, &meta_ac);
6469 if (ret) {
6470 mlog_errno(ret);
6471 goto out;
6472 }
6473
6474 handle = ocfs2_start_trans(osb, credits);
6475 if (IS_ERR(handle)) {
6476 ret = PTR_ERR(handle);
6477 mlog_errno(ret);
6478 goto out;
6479 }
6480
6481 ret = ocfs2_journal_access_di(handle, INODE_CACHE(args->new_inode),
6482 args->new_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6483 if (ret) {
6484 mlog_errno(ret);
6485 goto out_commit;
6486 }
6487
6488 memcpy(args->new_bh->b_data + header_off,
6489 args->old_bh->b_data + header_off, inline_size);
6490
6491 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6492 new_di->i_xattr_inline_size = cpu_to_le16(inline_size);
6493
6494 ret = ocfs2_reflink_xattr_header(handle, args, args->old_bh, xh,
6495 args->new_bh, new_xh, &vb, meta_ac,
6496 ocfs2_get_xattr_value_root, NULL);
6497 if (ret) {
6498 mlog_errno(ret);
6499 goto out_commit;
6500 }
6501
6502 new_oi = OCFS2_I(args->new_inode);
6503 spin_lock(&new_oi->ip_lock);
6504 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL | OCFS2_INLINE_XATTR_FL;
6505 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6506 spin_unlock(&new_oi->ip_lock);
6507
6508 ocfs2_journal_dirty(handle, args->new_bh);
6509
6510out_commit:
6511 ocfs2_commit_trans(osb, handle);
6512
6513out:
6514 if (meta_ac)
6515 ocfs2_free_alloc_context(meta_ac);
6516 return ret;
6517}
6518
6519static int ocfs2_create_empty_xattr_block(struct inode *inode,
6520 struct buffer_head *fe_bh,
6521 struct buffer_head **ret_bh,
6522 int indexed)
6523{
6524 int ret;
6525 handle_t *handle;
6526 struct ocfs2_alloc_context *meta_ac;
6527 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6528
6529 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
6530 if (ret < 0) {
6531 mlog_errno(ret);
6532 return ret;
6533 }
6534
6535 handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_CREATE_CREDITS);
6536 if (IS_ERR(handle)) {
6537 ret = PTR_ERR(handle);
6538 mlog_errno(ret);
6539 goto out;
6540 }
6541
6542 mlog(0, "create new xattr block for inode %llu, index = %d\n",
6543 (unsigned long long)fe_bh->b_blocknr, indexed);
6544 ret = ocfs2_create_xattr_block(handle, inode, fe_bh,
6545 meta_ac, ret_bh, indexed);
6546 if (ret)
6547 mlog_errno(ret);
6548
6549 ocfs2_commit_trans(osb, handle);
6550out:
6551 ocfs2_free_alloc_context(meta_ac);
6552 return ret;
6553}
6554
6555static int ocfs2_reflink_xattr_block(struct ocfs2_xattr_reflink *args,
6556 struct buffer_head *blk_bh,
6557 struct buffer_head *new_blk_bh)
6558{
6559 int ret = 0, credits = 0;
6560 handle_t *handle;
6561 struct ocfs2_inode_info *new_oi = OCFS2_I(args->new_inode);
6562 struct ocfs2_dinode *new_di;
6563 struct ocfs2_super *osb = OCFS2_SB(args->new_inode->i_sb);
6564 int header_off = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
6565 struct ocfs2_xattr_block *xb =
6566 (struct ocfs2_xattr_block *)blk_bh->b_data;
6567 struct ocfs2_xattr_header *xh = &xb->xb_attrs.xb_header;
6568 struct ocfs2_xattr_block *new_xb =
6569 (struct ocfs2_xattr_block *)new_blk_bh->b_data;
6570 struct ocfs2_xattr_header *new_xh = &new_xb->xb_attrs.xb_header;
6571 struct ocfs2_alloc_context *meta_ac;
6572 struct ocfs2_xattr_value_buf vb = {
6573 .vb_bh = new_blk_bh,
6574 .vb_access = ocfs2_journal_access_xb,
6575 };
6576
6577 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6578 &credits, &meta_ac);
6579 if (ret) {
6580 mlog_errno(ret);
6581 return ret;
6582 }
6583
6584 /* One more credits in case we need to add xattr flags in new inode. */
6585 handle = ocfs2_start_trans(osb, credits + 1);
6586 if (IS_ERR(handle)) {
6587 ret = PTR_ERR(handle);
6588 mlog_errno(ret);
6589 goto out;
6590 }
6591
6592 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6593 ret = ocfs2_journal_access_di(handle,
6594 INODE_CACHE(args->new_inode),
6595 args->new_bh,
6596 OCFS2_JOURNAL_ACCESS_WRITE);
6597 if (ret) {
6598 mlog_errno(ret);
6599 goto out_commit;
6600 }
6601 }
6602
6603 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(args->new_inode),
6604 new_blk_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6605 if (ret) {
6606 mlog_errno(ret);
6607 goto out_commit;
6608 }
6609
6610 memcpy(new_blk_bh->b_data + header_off, blk_bh->b_data + header_off,
6611 osb->sb->s_blocksize - header_off);
6612
6613 ret = ocfs2_reflink_xattr_header(handle, args, blk_bh, xh,
6614 new_blk_bh, new_xh, &vb, meta_ac,
6615 ocfs2_get_xattr_value_root, NULL);
6616 if (ret) {
6617 mlog_errno(ret);
6618 goto out_commit;
6619 }
6620
6621 ocfs2_journal_dirty(handle, new_blk_bh);
6622
6623 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6624 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6625 spin_lock(&new_oi->ip_lock);
6626 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
6627 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6628 spin_unlock(&new_oi->ip_lock);
6629
6630 ocfs2_journal_dirty(handle, args->new_bh);
6631 }
6632
6633out_commit:
6634 ocfs2_commit_trans(osb, handle);
6635
6636out:
6637 ocfs2_free_alloc_context(meta_ac);
6638 return ret;
6639}
6640
6641struct ocfs2_reflink_xattr_tree_args {
6642 struct ocfs2_xattr_reflink *reflink;
6643 struct buffer_head *old_blk_bh;
6644 struct buffer_head *new_blk_bh;
6645 struct ocfs2_xattr_bucket *old_bucket;
6646 struct ocfs2_xattr_bucket *new_bucket;
6647};
6648
6649/*
6650 * NOTE:
6651 * We have to handle the case that both old bucket and new bucket
6652 * will call this function to get the right ret_bh.
6653 * So The caller must give us the right bh.
6654 */
6655static int ocfs2_get_reflink_xattr_value_root(struct super_block *sb,
6656 struct buffer_head *bh,
6657 struct ocfs2_xattr_header *xh,
6658 int offset,
6659 struct ocfs2_xattr_value_root **xv,
6660 struct buffer_head **ret_bh,
6661 void *para)
6662{
6663 struct ocfs2_reflink_xattr_tree_args *args =
6664 (struct ocfs2_reflink_xattr_tree_args *)para;
6665 struct ocfs2_xattr_bucket *bucket;
6666
6667 if (bh == args->old_bucket->bu_bhs[0])
6668 bucket = args->old_bucket;
6669 else
6670 bucket = args->new_bucket;
6671
6672 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6673 xv, ret_bh);
6674}
6675
6676struct ocfs2_value_tree_metas {
6677 int num_metas;
6678 int credits;
6679 int num_recs;
6680};
6681
6682static int ocfs2_value_tree_metas_in_bucket(struct super_block *sb,
6683 struct buffer_head *bh,
6684 struct ocfs2_xattr_header *xh,
6685 int offset,
6686 struct ocfs2_xattr_value_root **xv,
6687 struct buffer_head **ret_bh,
6688 void *para)
6689{
6690 struct ocfs2_xattr_bucket *bucket =
6691 (struct ocfs2_xattr_bucket *)para;
6692
6693 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6694 xv, ret_bh);
6695}
6696
6697static int ocfs2_calc_value_tree_metas(struct inode *inode,
6698 struct ocfs2_xattr_bucket *bucket,
6699 void *para)
6700{
6701 struct ocfs2_value_tree_metas *metas =
6702 (struct ocfs2_value_tree_metas *)para;
6703 struct ocfs2_xattr_header *xh =
6704 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6705
6706 /* Add the credits for this bucket first. */
6707 metas->credits += bucket->bu_blocks;
6708 return ocfs2_value_metas_in_xattr_header(inode->i_sb, bucket->bu_bhs[0],
6709 xh, &metas->num_metas,
6710 &metas->credits, &metas->num_recs,
6711 ocfs2_value_tree_metas_in_bucket,
6712 bucket);
6713}
6714
6715/*
6716 * Given a xattr extent rec starting from blkno and having len clusters,
6717 * iterate all the buckets calculate how much metadata we need for reflinking
6718 * all the ocfs2_xattr_value_root and lock the allocators accordingly.
6719 */
6720static int ocfs2_lock_reflink_xattr_rec_allocators(
6721 struct ocfs2_reflink_xattr_tree_args *args,
6722 struct ocfs2_extent_tree *xt_et,
6723 u64 blkno, u32 len, int *credits,
6724 struct ocfs2_alloc_context **meta_ac,
6725 struct ocfs2_alloc_context **data_ac)
6726{
6727 int ret, num_free_extents;
6728 struct ocfs2_value_tree_metas metas;
6729 struct ocfs2_super *osb = OCFS2_SB(args->reflink->old_inode->i_sb);
6730 struct ocfs2_refcount_block *rb;
6731
6732 memset(&metas, 0, sizeof(metas));
6733
6734 ret = ocfs2_iterate_xattr_buckets(args->reflink->old_inode, blkno, len,
6735 ocfs2_calc_value_tree_metas, &metas);
6736 if (ret) {
6737 mlog_errno(ret);
6738 goto out;
6739 }
6740
6741 *credits = metas.credits;
6742
6743 /*
6744 * Calculate we need for refcount tree change.
6745 *
6746 * We need to add/modify num_recs in refcount tree, so just calculate
6747 * an approximate number we need for refcount tree change.
6748 * Sometimes we need to split the tree, and after split, half recs
6749 * will be moved to the new block, and a new block can only provide
6750 * half number of recs. So we multiple new blocks by 2.
6751 * In the end, we have to add credits for modifying the already
6752 * existed refcount block.
6753 */
6754 rb = (struct ocfs2_refcount_block *)args->reflink->ref_root_bh->b_data;
6755 metas.num_recs =
6756 (metas.num_recs + ocfs2_refcount_recs_per_rb(osb->sb) - 1) /
6757 ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6758 metas.num_metas += metas.num_recs;
6759 *credits += metas.num_recs +
6760 metas.num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6761 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6762 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6763 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6764 else
6765 *credits += 1;
6766
6767 /* count in the xattr tree change. */
6768 num_free_extents = ocfs2_num_free_extents(osb, xt_et);
6769 if (num_free_extents < 0) {
6770 ret = num_free_extents;
6771 mlog_errno(ret);
6772 goto out;
6773 }
6774
6775 if (num_free_extents < len)
6776 metas.num_metas += ocfs2_extend_meta_needed(xt_et->et_root_el);
6777
6778 *credits += ocfs2_calc_extend_credits(osb->sb,
6779 xt_et->et_root_el, len);
6780
6781 if (metas.num_metas) {
6782 ret = ocfs2_reserve_new_metadata_blocks(osb, metas.num_metas,
6783 meta_ac);
6784 if (ret) {
6785 mlog_errno(ret);
6786 goto out;
6787 }
6788 }
6789
6790 if (len) {
6791 ret = ocfs2_reserve_clusters(osb, len, data_ac);
6792 if (ret)
6793 mlog_errno(ret);
6794 }
6795out:
6796 if (ret) {
6797 if (*meta_ac) {
6798 ocfs2_free_alloc_context(*meta_ac);
6799 meta_ac = NULL;
6800 }
6801 }
6802
6803 return ret;
6804}
6805
6806static int ocfs2_reflink_xattr_buckets(handle_t *handle,
6807 u64 blkno, u64 new_blkno, u32 clusters,
6808 struct ocfs2_alloc_context *meta_ac,
6809 struct ocfs2_alloc_context *data_ac,
6810 struct ocfs2_reflink_xattr_tree_args *args)
6811{
6812 int i, j, ret = 0;
6813 struct super_block *sb = args->reflink->old_inode->i_sb;
6814 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
6815 u32 num_buckets = clusters * bpc;
6816 int bpb = args->old_bucket->bu_blocks;
6817 struct ocfs2_xattr_value_buf vb = {
6818 .vb_access = ocfs2_journal_access,
6819 };
6820
6821 for (i = 0; i < num_buckets; i++, blkno += bpb, new_blkno += bpb) {
6822 ret = ocfs2_read_xattr_bucket(args->old_bucket, blkno);
6823 if (ret) {
6824 mlog_errno(ret);
6825 break;
6826 }
6827
6828 ret = ocfs2_init_xattr_bucket(args->new_bucket, new_blkno);
6829 if (ret) {
6830 mlog_errno(ret);
6831 break;
6832 }
6833
6834 /*
6835 * The real bucket num in this series of blocks is stored
6836 * in the 1st bucket.
6837 */
6838 if (i == 0)
6839 num_buckets = le16_to_cpu(
6840 bucket_xh(args->old_bucket)->xh_num_buckets);
6841
6842 ret = ocfs2_xattr_bucket_journal_access(handle,
6843 args->new_bucket,
6844 OCFS2_JOURNAL_ACCESS_CREATE);
6845 if (ret) {
6846 mlog_errno(ret);
6847 break;
6848 }
6849
6850 for (j = 0; j < bpb; j++)
6851 memcpy(bucket_block(args->new_bucket, j),
6852 bucket_block(args->old_bucket, j),
6853 sb->s_blocksize);
6854
6855 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
6856
6857 ret = ocfs2_reflink_xattr_header(handle, args->reflink,
6858 args->old_bucket->bu_bhs[0],
6859 bucket_xh(args->old_bucket),
6860 args->new_bucket->bu_bhs[0],
6861 bucket_xh(args->new_bucket),
6862 &vb, meta_ac,
6863 ocfs2_get_reflink_xattr_value_root,
6864 args);
6865 if (ret) {
6866 mlog_errno(ret);
6867 break;
6868 }
6869
6870 /*
6871 * Re-access and dirty the bucket to calculate metaecc.
6872 * Because we may extend the transaction in reflink_xattr_header
6873 * which will let the already accessed block gone.
6874 */
6875 ret = ocfs2_xattr_bucket_journal_access(handle,
6876 args->new_bucket,
6877 OCFS2_JOURNAL_ACCESS_WRITE);
6878 if (ret) {
6879 mlog_errno(ret);
6880 break;
6881 }
6882
6883 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
6884 ocfs2_xattr_bucket_relse(args->old_bucket);
6885 ocfs2_xattr_bucket_relse(args->new_bucket);
6886 }
6887
6888 ocfs2_xattr_bucket_relse(args->old_bucket);
6889 ocfs2_xattr_bucket_relse(args->new_bucket);
6890 return ret;
6891}
6892/*
6893 * Create the same xattr extent record in the new inode's xattr tree.
6894 */
6895static int ocfs2_reflink_xattr_rec(struct inode *inode,
6896 struct buffer_head *root_bh,
6897 u64 blkno,
6898 u32 cpos,
6899 u32 len,
6900 void *para)
6901{
6902 int ret, credits = 0;
6903 u32 p_cluster, num_clusters;
6904 u64 new_blkno;
6905 handle_t *handle;
6906 struct ocfs2_reflink_xattr_tree_args *args =
6907 (struct ocfs2_reflink_xattr_tree_args *)para;
6908 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6909 struct ocfs2_alloc_context *meta_ac = NULL;
6910 struct ocfs2_alloc_context *data_ac = NULL;
6911 struct ocfs2_extent_tree et;
6912
6913 ocfs2_init_xattr_tree_extent_tree(&et,
6914 INODE_CACHE(args->reflink->new_inode),
6915 args->new_blk_bh);
6916
6917 ret = ocfs2_lock_reflink_xattr_rec_allocators(args, &et, blkno,
6918 len, &credits,
6919 &meta_ac, &data_ac);
6920 if (ret) {
6921 mlog_errno(ret);
6922 goto out;
6923 }
6924
6925 handle = ocfs2_start_trans(osb, credits);
6926 if (IS_ERR(handle)) {
6927 ret = PTR_ERR(handle);
6928 mlog_errno(ret);
6929 goto out;
6930 }
6931
6932 ret = ocfs2_claim_clusters(osb, handle, data_ac,
6933 len, &p_cluster, &num_clusters);
6934 if (ret) {
6935 mlog_errno(ret);
6936 goto out_commit;
6937 }
6938
6939 new_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cluster);
6940
6941 mlog(0, "reflink xattr buckets %llu to %llu, len %u\n",
6942 (unsigned long long)blkno, (unsigned long long)new_blkno, len);
6943 ret = ocfs2_reflink_xattr_buckets(handle, blkno, new_blkno, len,
6944 meta_ac, data_ac, args);
6945 if (ret) {
6946 mlog_errno(ret);
6947 goto out_commit;
6948 }
6949
6950 mlog(0, "insert new xattr extent rec start %llu len %u to %u\n",
6951 (unsigned long long)new_blkno, len, cpos);
6952 ret = ocfs2_insert_extent(handle, &et, cpos, new_blkno,
6953 len, 0, meta_ac);
6954 if (ret)
6955 mlog_errno(ret);
6956
6957out_commit:
6958 ocfs2_commit_trans(osb, handle);
6959
6960out:
6961 if (meta_ac)
6962 ocfs2_free_alloc_context(meta_ac);
6963 if (data_ac)
6964 ocfs2_free_alloc_context(data_ac);
6965 return ret;
6966}
6967
6968/*
6969 * Create reflinked xattr buckets.
6970 * We will add bucket one by one, and refcount all the xattrs in the bucket
6971 * if they are stored outside.
6972 */
6973static int ocfs2_reflink_xattr_tree(struct ocfs2_xattr_reflink *args,
6974 struct buffer_head *blk_bh,
6975 struct buffer_head *new_blk_bh)
6976{
6977 int ret;
6978 struct ocfs2_reflink_xattr_tree_args para;
6979
6980 memset(&para, 0, sizeof(para));
6981 para.reflink = args;
6982 para.old_blk_bh = blk_bh;
6983 para.new_blk_bh = new_blk_bh;
6984
6985 para.old_bucket = ocfs2_xattr_bucket_new(args->old_inode);
6986 if (!para.old_bucket) {
6987 mlog_errno(-ENOMEM);
6988 return -ENOMEM;
6989 }
6990
6991 para.new_bucket = ocfs2_xattr_bucket_new(args->new_inode);
6992 if (!para.new_bucket) {
6993 ret = -ENOMEM;
6994 mlog_errno(ret);
6995 goto out;
6996 }
6997
6998 ret = ocfs2_iterate_xattr_index_block(args->old_inode, blk_bh,
6999 ocfs2_reflink_xattr_rec,
7000 &para);
7001 if (ret)
7002 mlog_errno(ret);
7003
7004out:
7005 ocfs2_xattr_bucket_free(para.old_bucket);
7006 ocfs2_xattr_bucket_free(para.new_bucket);
7007 return ret;
7008}
7009
7010static int ocfs2_reflink_xattr_in_block(struct ocfs2_xattr_reflink *args,
7011 struct buffer_head *blk_bh)
7012{
7013 int ret, indexed = 0;
7014 struct buffer_head *new_blk_bh = NULL;
7015 struct ocfs2_xattr_block *xb =
7016 (struct ocfs2_xattr_block *)blk_bh->b_data;
7017
7018
7019 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)
7020 indexed = 1;
7021
7022 ret = ocfs2_create_empty_xattr_block(args->new_inode, args->new_bh,
7023 &new_blk_bh, indexed);
7024 if (ret) {
7025 mlog_errno(ret);
7026 goto out;
7027 }
7028
7029 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED))
7030 ret = ocfs2_reflink_xattr_block(args, blk_bh, new_blk_bh);
7031 else
7032 ret = ocfs2_reflink_xattr_tree(args, blk_bh, new_blk_bh);
7033 if (ret)
7034 mlog_errno(ret);
7035
7036out:
7037 brelse(new_blk_bh);
7038 return ret;
7039}
7040
0fe9b66c
TM
7041static int ocfs2_reflink_xattr_no_security(struct ocfs2_xattr_entry *xe)
7042{
7043 int type = ocfs2_xattr_get_type(xe);
7044
7045 return type != OCFS2_XATTR_INDEX_SECURITY &&
7046 type != OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS &&
7047 type != OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
7048}
7049
2999d12f
TM
7050int ocfs2_reflink_xattrs(struct inode *old_inode,
7051 struct buffer_head *old_bh,
7052 struct inode *new_inode,
0fe9b66c
TM
7053 struct buffer_head *new_bh,
7054 bool preserve_security)
2999d12f
TM
7055{
7056 int ret;
7057 struct ocfs2_xattr_reflink args;
7058 struct ocfs2_inode_info *oi = OCFS2_I(old_inode);
7059 struct ocfs2_dinode *di = (struct ocfs2_dinode *)old_bh->b_data;
7060 struct buffer_head *blk_bh = NULL;
7061 struct ocfs2_cached_dealloc_ctxt dealloc;
7062 struct ocfs2_refcount_tree *ref_tree;
7063 struct buffer_head *ref_root_bh = NULL;
7064
7065 ret = ocfs2_lock_refcount_tree(OCFS2_SB(old_inode->i_sb),
7066 le64_to_cpu(di->i_refcount_loc),
7067 1, &ref_tree, &ref_root_bh);
7068 if (ret) {
7069 mlog_errno(ret);
7070 goto out;
7071 }
7072
7073 ocfs2_init_dealloc_ctxt(&dealloc);
7074
7075 args.old_inode = old_inode;
7076 args.new_inode = new_inode;
7077 args.old_bh = old_bh;
7078 args.new_bh = new_bh;
7079 args.ref_ci = &ref_tree->rf_ci;
7080 args.ref_root_bh = ref_root_bh;
7081 args.dealloc = &dealloc;
0fe9b66c
TM
7082 if (preserve_security)
7083 args.xattr_reflinked = NULL;
7084 else
7085 args.xattr_reflinked = ocfs2_reflink_xattr_no_security;
2999d12f
TM
7086
7087 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
7088 ret = ocfs2_reflink_xattr_inline(&args);
7089 if (ret) {
7090 mlog_errno(ret);
7091 goto out_unlock;
7092 }
7093 }
7094
7095 if (!di->i_xattr_loc)
7096 goto out_unlock;
7097
7098 ret = ocfs2_read_xattr_block(old_inode, le64_to_cpu(di->i_xattr_loc),
7099 &blk_bh);
7100 if (ret < 0) {
7101 mlog_errno(ret);
7102 goto out_unlock;
7103 }
7104
7105 ret = ocfs2_reflink_xattr_in_block(&args, blk_bh);
7106 if (ret)
7107 mlog_errno(ret);
7108
7109 brelse(blk_bh);
7110
7111out_unlock:
7112 ocfs2_unlock_refcount_tree(OCFS2_SB(old_inode->i_sb),
7113 ref_tree, 1);
7114 brelse(ref_root_bh);
7115
7116 if (ocfs2_dealloc_has_cluster(&dealloc)) {
7117 ocfs2_schedule_truncate_log_flush(OCFS2_SB(old_inode->i_sb), 1);
7118 ocfs2_run_deallocs(OCFS2_SB(old_inode->i_sb), &dealloc);
7119 }
7120
7121out:
7122 return ret;
7123}
7124
0fe9b66c
TM
7125/*
7126 * Initialize security and acl for a already created inode.
7127 * Used for reflink a non-preserve-security file.
7128 *
7129 * It uses common api like ocfs2_xattr_set, so the caller
7130 * must not hold any lock expect i_mutex.
7131 */
7132int ocfs2_init_security_and_acl(struct inode *dir,
7133 struct inode *inode)
7134{
7135 int ret = 0;
7136 struct buffer_head *dir_bh = NULL;
7137 struct ocfs2_security_xattr_info si = {
7138 .enable = 1,
7139 };
7140
7141 ret = ocfs2_init_security_get(inode, dir, &si);
7142 if (!ret) {
431547b3
CH
7143 ret = ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY,
7144 si.name, si.value, si.value_len,
7145 XATTR_CREATE);
0fe9b66c
TM
7146 if (ret) {
7147 mlog_errno(ret);
7148 goto leave;
7149 }
7150 } else if (ret != -EOPNOTSUPP) {
7151 mlog_errno(ret);
7152 goto leave;
7153 }
7154
7155 ret = ocfs2_inode_lock(dir, &dir_bh, 0);
7156 if (ret) {
7157 mlog_errno(ret);
7158 goto leave;
7159 }
7160
7161 ret = ocfs2_init_acl(NULL, inode, dir, NULL, dir_bh, NULL, NULL);
7162 if (ret)
7163 mlog_errno(ret);
7164
7165 ocfs2_inode_unlock(dir, 0);
7166 brelse(dir_bh);
7167leave:
7168 return ret;
7169}
923f7f31
TY
7170/*
7171 * 'security' attributes support
7172 */
431547b3 7173static size_t ocfs2_xattr_security_list(struct dentry *dentry, char *list,
923f7f31 7174 size_t list_size, const char *name,
431547b3 7175 size_t name_len, int type)
923f7f31
TY
7176{
7177 const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN;
7178 const size_t total_len = prefix_len + name_len + 1;
7179
7180 if (list && total_len <= list_size) {
7181 memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
7182 memcpy(list + prefix_len, name, name_len);
7183 list[prefix_len + name_len] = '\0';
7184 }
7185 return total_len;
7186}
7187
431547b3
CH
7188static int ocfs2_xattr_security_get(struct dentry *dentry, const char *name,
7189 void *buffer, size_t size, int type)
923f7f31
TY
7190{
7191 if (strcmp(name, "") == 0)
7192 return -EINVAL;
431547b3
CH
7193 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
7194 name, buffer, size);
923f7f31
TY
7195}
7196
431547b3
CH
7197static int ocfs2_xattr_security_set(struct dentry *dentry, const char *name,
7198 const void *value, size_t size, int flags, int type)
923f7f31
TY
7199{
7200 if (strcmp(name, "") == 0)
7201 return -EINVAL;
7202
431547b3
CH
7203 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
7204 name, value, size, flags);
923f7f31
TY
7205}
7206
534eaddd
TY
7207int ocfs2_init_security_get(struct inode *inode,
7208 struct inode *dir,
7209 struct ocfs2_security_xattr_info *si)
7210{
38d59ef6
TY
7211 /* check whether ocfs2 support feature xattr */
7212 if (!ocfs2_supports_xattr(OCFS2_SB(dir->i_sb)))
7213 return -EOPNOTSUPP;
534eaddd
TY
7214 return security_inode_init_security(inode, dir, &si->name, &si->value,
7215 &si->value_len);
7216}
7217
7218int ocfs2_init_security_set(handle_t *handle,
7219 struct inode *inode,
7220 struct buffer_head *di_bh,
7221 struct ocfs2_security_xattr_info *si,
7222 struct ocfs2_alloc_context *xattr_ac,
7223 struct ocfs2_alloc_context *data_ac)
7224{
7225 return ocfs2_xattr_set_handle(handle, inode, di_bh,
7226 OCFS2_XATTR_INDEX_SECURITY,
7227 si->name, si->value, si->value_len, 0,
7228 xattr_ac, data_ac);
7229}
7230
923f7f31
TY
7231struct xattr_handler ocfs2_xattr_security_handler = {
7232 .prefix = XATTR_SECURITY_PREFIX,
7233 .list = ocfs2_xattr_security_list,
7234 .get = ocfs2_xattr_security_get,
7235 .set = ocfs2_xattr_security_set,
7236};
7237
99219aea
MF
7238/*
7239 * 'trusted' attributes support
7240 */
431547b3 7241static size_t ocfs2_xattr_trusted_list(struct dentry *dentry, char *list,
99219aea 7242 size_t list_size, const char *name,
431547b3 7243 size_t name_len, int type)
99219aea 7244{
ceb1eba3 7245 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
99219aea
MF
7246 const size_t total_len = prefix_len + name_len + 1;
7247
7248 if (list && total_len <= list_size) {
7249 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
7250 memcpy(list + prefix_len, name, name_len);
7251 list[prefix_len + name_len] = '\0';
7252 }
7253 return total_len;
7254}
7255
431547b3
CH
7256static int ocfs2_xattr_trusted_get(struct dentry *dentry, const char *name,
7257 void *buffer, size_t size, int type)
99219aea
MF
7258{
7259 if (strcmp(name, "") == 0)
7260 return -EINVAL;
431547b3
CH
7261 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
7262 name, buffer, size);
99219aea
MF
7263}
7264
431547b3
CH
7265static int ocfs2_xattr_trusted_set(struct dentry *dentry, const char *name,
7266 const void *value, size_t size, int flags, int type)
99219aea
MF
7267{
7268 if (strcmp(name, "") == 0)
7269 return -EINVAL;
7270
431547b3
CH
7271 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
7272 name, value, size, flags);
99219aea
MF
7273}
7274
7275struct xattr_handler ocfs2_xattr_trusted_handler = {
7276 .prefix = XATTR_TRUSTED_PREFIX,
7277 .list = ocfs2_xattr_trusted_list,
7278 .get = ocfs2_xattr_trusted_get,
7279 .set = ocfs2_xattr_trusted_set,
7280};
7281
99219aea
MF
7282/*
7283 * 'user' attributes support
7284 */
431547b3 7285static size_t ocfs2_xattr_user_list(struct dentry *dentry, char *list,
99219aea 7286 size_t list_size, const char *name,
431547b3 7287 size_t name_len, int type)
99219aea 7288{
ceb1eba3 7289 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
99219aea 7290 const size_t total_len = prefix_len + name_len + 1;
431547b3 7291 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
99219aea
MF
7292
7293 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7294 return 0;
7295
7296 if (list && total_len <= list_size) {
7297 memcpy(list, XATTR_USER_PREFIX, prefix_len);
7298 memcpy(list + prefix_len, name, name_len);
7299 list[prefix_len + name_len] = '\0';
7300 }
7301 return total_len;
7302}
7303
431547b3
CH
7304static int ocfs2_xattr_user_get(struct dentry *dentry, const char *name,
7305 void *buffer, size_t size, int type)
99219aea 7306{
431547b3 7307 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
99219aea
MF
7308
7309 if (strcmp(name, "") == 0)
7310 return -EINVAL;
7311 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7312 return -EOPNOTSUPP;
431547b3 7313 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_USER, name,
99219aea
MF
7314 buffer, size);
7315}
7316
431547b3
CH
7317static int ocfs2_xattr_user_set(struct dentry *dentry, const char *name,
7318 const void *value, size_t size, int flags, int type)
99219aea 7319{
431547b3 7320 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
99219aea
MF
7321
7322 if (strcmp(name, "") == 0)
7323 return -EINVAL;
7324 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7325 return -EOPNOTSUPP;
7326
431547b3
CH
7327 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_USER,
7328 name, value, size, flags);
99219aea
MF
7329}
7330
7331struct xattr_handler ocfs2_xattr_user_handler = {
7332 .prefix = XATTR_USER_PREFIX,
7333 .list = ocfs2_xattr_user_list,
7334 .get = ocfs2_xattr_user_get,
7335 .set = ocfs2_xattr_user_set,
7336};