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