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