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