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