Linux 2.6.35-rc1
[orion.git] / fs / ocfs2 / xattr.c
blobe97b34842cfea0d188f85476a56c4a0eb0af5ce0
1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
4 * xattr.c
6 * Copyright (C) 2004, 2008 Oracle. All rights reserved.
8 * CREDITS:
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>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public
14 * License version 2 as published by the Free Software Foundation.
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.
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>
34 #include <linux/sort.h>
35 #include <linux/init.h>
36 #include <linux/module.h>
37 #include <linux/string.h>
38 #include <linux/security.h>
40 #define MLOG_MASK_PREFIX ML_XATTR
41 #include <cluster/masklog.h>
43 #include "ocfs2.h"
44 #include "alloc.h"
45 #include "blockcheck.h"
46 #include "dlmglue.h"
47 #include "file.h"
48 #include "symlink.h"
49 #include "sysfile.h"
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"
56 #include "super.h"
57 #include "xattr.h"
58 #include "refcounttree.h"
59 #include "acl.h"
61 struct ocfs2_xattr_def_value_root {
62 struct ocfs2_xattr_value_root xv;
63 struct ocfs2_extent_rec er;
66 struct ocfs2_xattr_bucket {
67 /* The inode these xattrs are associated with */
68 struct inode *bu_inode;
70 /* The actual buffers that make up the bucket */
71 struct buffer_head *bu_bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
73 /* How many blocks make up one bucket for this filesystem */
74 int bu_blocks;
77 struct ocfs2_xattr_set_ctxt {
78 handle_t *handle;
79 struct ocfs2_alloc_context *meta_ac;
80 struct ocfs2_alloc_context *data_ac;
81 struct ocfs2_cached_dealloc_ctxt dealloc;
82 int set_abort;
85 #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
86 #define OCFS2_XATTR_INLINE_SIZE 80
87 #define OCFS2_XATTR_HEADER_GAP 4
88 #define OCFS2_XATTR_FREE_IN_IBODY (OCFS2_MIN_XATTR_INLINE_SIZE \
89 - sizeof(struct ocfs2_xattr_header) \
90 - OCFS2_XATTR_HEADER_GAP)
91 #define OCFS2_XATTR_FREE_IN_BLOCK(ptr) ((ptr)->i_sb->s_blocksize \
92 - sizeof(struct ocfs2_xattr_block) \
93 - sizeof(struct ocfs2_xattr_header) \
94 - OCFS2_XATTR_HEADER_GAP)
96 static struct ocfs2_xattr_def_value_root def_xv = {
97 .xv.xr_list.l_count = cpu_to_le16(1),
100 const struct xattr_handler *ocfs2_xattr_handlers[] = {
101 &ocfs2_xattr_user_handler,
102 &ocfs2_xattr_acl_access_handler,
103 &ocfs2_xattr_acl_default_handler,
104 &ocfs2_xattr_trusted_handler,
105 &ocfs2_xattr_security_handler,
106 NULL
109 static const struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
110 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
111 [OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS]
112 = &ocfs2_xattr_acl_access_handler,
113 [OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT]
114 = &ocfs2_xattr_acl_default_handler,
115 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
116 [OCFS2_XATTR_INDEX_SECURITY] = &ocfs2_xattr_security_handler,
119 struct ocfs2_xattr_info {
120 int xi_name_index;
121 const char *xi_name;
122 int xi_name_len;
123 const void *xi_value;
124 size_t xi_value_len;
127 struct ocfs2_xattr_search {
128 struct buffer_head *inode_bh;
130 * xattr_bh point to the block buffer head which has extended attribute
131 * when extended attribute in inode, xattr_bh is equal to inode_bh.
133 struct buffer_head *xattr_bh;
134 struct ocfs2_xattr_header *header;
135 struct ocfs2_xattr_bucket *bucket;
136 void *base;
137 void *end;
138 struct ocfs2_xattr_entry *here;
139 int not_found;
142 /* Operations on struct ocfs2_xa_entry */
143 struct ocfs2_xa_loc;
144 struct ocfs2_xa_loc_operations {
146 * Journal functions
148 int (*xlo_journal_access)(handle_t *handle, struct ocfs2_xa_loc *loc,
149 int type);
150 void (*xlo_journal_dirty)(handle_t *handle, struct ocfs2_xa_loc *loc);
153 * Return a pointer to the appropriate buffer in loc->xl_storage
154 * at the given offset from loc->xl_header.
156 void *(*xlo_offset_pointer)(struct ocfs2_xa_loc *loc, int offset);
158 /* Can we reuse the existing entry for the new value? */
159 int (*xlo_can_reuse)(struct ocfs2_xa_loc *loc,
160 struct ocfs2_xattr_info *xi);
162 /* How much space is needed for the new value? */
163 int (*xlo_check_space)(struct ocfs2_xa_loc *loc,
164 struct ocfs2_xattr_info *xi);
167 * Return the offset of the first name+value pair. This is
168 * the start of our downward-filling free space.
170 int (*xlo_get_free_start)(struct ocfs2_xa_loc *loc);
173 * Remove the name+value at this location. Do whatever is
174 * appropriate with the remaining name+value pairs.
176 void (*xlo_wipe_namevalue)(struct ocfs2_xa_loc *loc);
178 /* Fill xl_entry with a new entry */
179 void (*xlo_add_entry)(struct ocfs2_xa_loc *loc, u32 name_hash);
181 /* Add name+value storage to an entry */
182 void (*xlo_add_namevalue)(struct ocfs2_xa_loc *loc, int size);
185 * Initialize the value buf's access and bh fields for this entry.
186 * ocfs2_xa_fill_value_buf() will handle the xv pointer.
188 void (*xlo_fill_value_buf)(struct ocfs2_xa_loc *loc,
189 struct ocfs2_xattr_value_buf *vb);
193 * Describes an xattr entry location. This is a memory structure
194 * tracking the on-disk structure.
196 struct ocfs2_xa_loc {
197 /* This xattr belongs to this inode */
198 struct inode *xl_inode;
200 /* The ocfs2_xattr_header inside the on-disk storage. Not NULL. */
201 struct ocfs2_xattr_header *xl_header;
203 /* Bytes from xl_header to the end of the storage */
204 int xl_size;
207 * The ocfs2_xattr_entry this location describes. If this is
208 * NULL, this location describes the on-disk structure where it
209 * would have been.
211 struct ocfs2_xattr_entry *xl_entry;
214 * Internal housekeeping
217 /* Buffer(s) containing this entry */
218 void *xl_storage;
220 /* Operations on the storage backing this location */
221 const struct ocfs2_xa_loc_operations *xl_ops;
225 * Convenience functions to calculate how much space is needed for a
226 * given name+value pair
228 static int namevalue_size(int name_len, uint64_t value_len)
230 if (value_len > OCFS2_XATTR_INLINE_SIZE)
231 return OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
232 else
233 return OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
236 static int namevalue_size_xi(struct ocfs2_xattr_info *xi)
238 return namevalue_size(xi->xi_name_len, xi->xi_value_len);
241 static int namevalue_size_xe(struct ocfs2_xattr_entry *xe)
243 u64 value_len = le64_to_cpu(xe->xe_value_size);
245 BUG_ON((value_len > OCFS2_XATTR_INLINE_SIZE) &&
246 ocfs2_xattr_is_local(xe));
247 return namevalue_size(xe->xe_name_len, value_len);
251 static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
252 struct ocfs2_xattr_header *xh,
253 int index,
254 int *block_off,
255 int *new_offset);
257 static int ocfs2_xattr_block_find(struct inode *inode,
258 int name_index,
259 const char *name,
260 struct ocfs2_xattr_search *xs);
261 static int ocfs2_xattr_index_block_find(struct inode *inode,
262 struct buffer_head *root_bh,
263 int name_index,
264 const char *name,
265 struct ocfs2_xattr_search *xs);
267 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
268 struct buffer_head *blk_bh,
269 char *buffer,
270 size_t buffer_size);
272 static int ocfs2_xattr_create_index_block(struct inode *inode,
273 struct ocfs2_xattr_search *xs,
274 struct ocfs2_xattr_set_ctxt *ctxt);
276 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
277 struct ocfs2_xattr_info *xi,
278 struct ocfs2_xattr_search *xs,
279 struct ocfs2_xattr_set_ctxt *ctxt);
281 typedef int (xattr_tree_rec_func)(struct inode *inode,
282 struct buffer_head *root_bh,
283 u64 blkno, u32 cpos, u32 len, void *para);
284 static int ocfs2_iterate_xattr_index_block(struct inode *inode,
285 struct buffer_head *root_bh,
286 xattr_tree_rec_func *rec_func,
287 void *para);
288 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
289 struct ocfs2_xattr_bucket *bucket,
290 void *para);
291 static int ocfs2_rm_xattr_cluster(struct inode *inode,
292 struct buffer_head *root_bh,
293 u64 blkno,
294 u32 cpos,
295 u32 len,
296 void *para);
298 static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
299 u64 src_blk, u64 last_blk, u64 to_blk,
300 unsigned int start_bucket,
301 u32 *first_hash);
302 static int ocfs2_prepare_refcount_xattr(struct inode *inode,
303 struct ocfs2_dinode *di,
304 struct ocfs2_xattr_info *xi,
305 struct ocfs2_xattr_search *xis,
306 struct ocfs2_xattr_search *xbs,
307 struct ocfs2_refcount_tree **ref_tree,
308 int *meta_need,
309 int *credits);
310 static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
311 struct ocfs2_xattr_bucket *bucket,
312 int offset,
313 struct ocfs2_xattr_value_root **xv,
314 struct buffer_head **bh);
316 static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
318 return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
321 static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
323 return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
326 #define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
327 #define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
328 #define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
330 static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
332 struct ocfs2_xattr_bucket *bucket;
333 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
335 BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
337 bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
338 if (bucket) {
339 bucket->bu_inode = inode;
340 bucket->bu_blocks = blks;
343 return bucket;
346 static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
348 int i;
350 for (i = 0; i < bucket->bu_blocks; i++) {
351 brelse(bucket->bu_bhs[i]);
352 bucket->bu_bhs[i] = NULL;
356 static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
358 if (bucket) {
359 ocfs2_xattr_bucket_relse(bucket);
360 bucket->bu_inode = NULL;
361 kfree(bucket);
366 * A bucket that has never been written to disk doesn't need to be
367 * read. We just need the buffer_heads. Don't call this for
368 * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
369 * them fully.
371 static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
372 u64 xb_blkno)
374 int i, rc = 0;
376 for (i = 0; i < bucket->bu_blocks; i++) {
377 bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
378 xb_blkno + i);
379 if (!bucket->bu_bhs[i]) {
380 rc = -EIO;
381 mlog_errno(rc);
382 break;
385 if (!ocfs2_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
386 bucket->bu_bhs[i]))
387 ocfs2_set_new_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
388 bucket->bu_bhs[i]);
391 if (rc)
392 ocfs2_xattr_bucket_relse(bucket);
393 return rc;
396 /* Read the xattr bucket at xb_blkno */
397 static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
398 u64 xb_blkno)
400 int rc;
402 rc = ocfs2_read_blocks(INODE_CACHE(bucket->bu_inode), xb_blkno,
403 bucket->bu_blocks, bucket->bu_bhs, 0,
404 NULL);
405 if (!rc) {
406 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
407 rc = ocfs2_validate_meta_ecc_bhs(bucket->bu_inode->i_sb,
408 bucket->bu_bhs,
409 bucket->bu_blocks,
410 &bucket_xh(bucket)->xh_check);
411 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
412 if (rc)
413 mlog_errno(rc);
416 if (rc)
417 ocfs2_xattr_bucket_relse(bucket);
418 return rc;
421 static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
422 struct ocfs2_xattr_bucket *bucket,
423 int type)
425 int i, rc = 0;
427 for (i = 0; i < bucket->bu_blocks; i++) {
428 rc = ocfs2_journal_access(handle,
429 INODE_CACHE(bucket->bu_inode),
430 bucket->bu_bhs[i], type);
431 if (rc) {
432 mlog_errno(rc);
433 break;
437 return rc;
440 static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
441 struct ocfs2_xattr_bucket *bucket)
443 int i;
445 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
446 ocfs2_compute_meta_ecc_bhs(bucket->bu_inode->i_sb,
447 bucket->bu_bhs, bucket->bu_blocks,
448 &bucket_xh(bucket)->xh_check);
449 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
451 for (i = 0; i < bucket->bu_blocks; i++)
452 ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
455 static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
456 struct ocfs2_xattr_bucket *src)
458 int i;
459 int blocksize = src->bu_inode->i_sb->s_blocksize;
461 BUG_ON(dest->bu_blocks != src->bu_blocks);
462 BUG_ON(dest->bu_inode != src->bu_inode);
464 for (i = 0; i < src->bu_blocks; i++) {
465 memcpy(bucket_block(dest, i), bucket_block(src, i),
466 blocksize);
470 static int ocfs2_validate_xattr_block(struct super_block *sb,
471 struct buffer_head *bh)
473 int rc;
474 struct ocfs2_xattr_block *xb =
475 (struct ocfs2_xattr_block *)bh->b_data;
477 mlog(0, "Validating xattr block %llu\n",
478 (unsigned long long)bh->b_blocknr);
480 BUG_ON(!buffer_uptodate(bh));
483 * If the ecc fails, we return the error but otherwise
484 * leave the filesystem running. We know any error is
485 * local to this block.
487 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &xb->xb_check);
488 if (rc)
489 return rc;
492 * Errors after here are fatal
495 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
496 ocfs2_error(sb,
497 "Extended attribute block #%llu has bad "
498 "signature %.*s",
499 (unsigned long long)bh->b_blocknr, 7,
500 xb->xb_signature);
501 return -EINVAL;
504 if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) {
505 ocfs2_error(sb,
506 "Extended attribute block #%llu has an "
507 "invalid xb_blkno of %llu",
508 (unsigned long long)bh->b_blocknr,
509 (unsigned long long)le64_to_cpu(xb->xb_blkno));
510 return -EINVAL;
513 if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) {
514 ocfs2_error(sb,
515 "Extended attribute block #%llu has an invalid "
516 "xb_fs_generation of #%u",
517 (unsigned long long)bh->b_blocknr,
518 le32_to_cpu(xb->xb_fs_generation));
519 return -EINVAL;
522 return 0;
525 static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno,
526 struct buffer_head **bh)
528 int rc;
529 struct buffer_head *tmp = *bh;
531 rc = ocfs2_read_block(INODE_CACHE(inode), xb_blkno, &tmp,
532 ocfs2_validate_xattr_block);
534 /* If ocfs2_read_block() got us a new bh, pass it up. */
535 if (!rc && !*bh)
536 *bh = tmp;
538 return rc;
541 static inline const char *ocfs2_xattr_prefix(int name_index)
543 const struct xattr_handler *handler = NULL;
545 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
546 handler = ocfs2_xattr_handler_map[name_index];
548 return handler ? handler->prefix : NULL;
551 static u32 ocfs2_xattr_name_hash(struct inode *inode,
552 const char *name,
553 int name_len)
555 /* Get hash value of uuid from super block */
556 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
557 int i;
559 /* hash extended attribute name */
560 for (i = 0; i < name_len; i++) {
561 hash = (hash << OCFS2_HASH_SHIFT) ^
562 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
563 *name++;
566 return hash;
569 static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
571 return namevalue_size(name_len, value_len) +
572 sizeof(struct ocfs2_xattr_entry);
575 static int ocfs2_xi_entry_usage(struct ocfs2_xattr_info *xi)
577 return namevalue_size_xi(xi) +
578 sizeof(struct ocfs2_xattr_entry);
581 static int ocfs2_xe_entry_usage(struct ocfs2_xattr_entry *xe)
583 return namevalue_size_xe(xe) +
584 sizeof(struct ocfs2_xattr_entry);
587 int ocfs2_calc_security_init(struct inode *dir,
588 struct ocfs2_security_xattr_info *si,
589 int *want_clusters,
590 int *xattr_credits,
591 struct ocfs2_alloc_context **xattr_ac)
593 int ret = 0;
594 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
595 int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
596 si->value_len);
599 * The max space of security xattr taken inline is
600 * 256(name) + 80(value) + 16(entry) = 352 bytes,
601 * So reserve one metadata block for it is ok.
603 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
604 s_size > OCFS2_XATTR_FREE_IN_IBODY) {
605 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
606 if (ret) {
607 mlog_errno(ret);
608 return ret;
610 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
613 /* reserve clusters for xattr value which will be set in B tree*/
614 if (si->value_len > OCFS2_XATTR_INLINE_SIZE) {
615 int new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
616 si->value_len);
618 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
619 new_clusters);
620 *want_clusters += new_clusters;
622 return ret;
625 int ocfs2_calc_xattr_init(struct inode *dir,
626 struct buffer_head *dir_bh,
627 int mode,
628 struct ocfs2_security_xattr_info *si,
629 int *want_clusters,
630 int *xattr_credits,
631 int *want_meta)
633 int ret = 0;
634 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
635 int s_size = 0, a_size = 0, acl_len = 0, new_clusters;
637 if (si->enable)
638 s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
639 si->value_len);
641 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
642 acl_len = ocfs2_xattr_get_nolock(dir, dir_bh,
643 OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
644 "", NULL, 0);
645 if (acl_len > 0) {
646 a_size = ocfs2_xattr_entry_real_size(0, acl_len);
647 if (S_ISDIR(mode))
648 a_size <<= 1;
649 } else if (acl_len != 0 && acl_len != -ENODATA) {
650 mlog_errno(ret);
651 return ret;
655 if (!(s_size + a_size))
656 return ret;
659 * The max space of security xattr taken inline is
660 * 256(name) + 80(value) + 16(entry) = 352 bytes,
661 * The max space of acl xattr taken inline is
662 * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
663 * when blocksize = 512, may reserve one more cluser for
664 * xattr bucket, otherwise reserve one metadata block
665 * for them is ok.
666 * If this is a new directory with inline data,
667 * we choose to reserve the entire inline area for
668 * directory contents and force an external xattr block.
670 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
671 (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) ||
672 (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) {
673 *want_meta = *want_meta + 1;
674 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
677 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
678 (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
679 *want_clusters += 1;
680 *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
684 * reserve credits and clusters for xattrs which has large value
685 * and have to be set outside
687 if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE) {
688 new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
689 si->value_len);
690 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
691 new_clusters);
692 *want_clusters += new_clusters;
694 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
695 acl_len > OCFS2_XATTR_INLINE_SIZE) {
696 /* for directory, it has DEFAULT and ACCESS two types of acls */
697 new_clusters = (S_ISDIR(mode) ? 2 : 1) *
698 ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
699 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
700 new_clusters);
701 *want_clusters += new_clusters;
704 return ret;
707 static int ocfs2_xattr_extend_allocation(struct inode *inode,
708 u32 clusters_to_add,
709 struct ocfs2_xattr_value_buf *vb,
710 struct ocfs2_xattr_set_ctxt *ctxt)
712 int status = 0;
713 handle_t *handle = ctxt->handle;
714 enum ocfs2_alloc_restarted why;
715 u32 prev_clusters, logical_start = le32_to_cpu(vb->vb_xv->xr_clusters);
716 struct ocfs2_extent_tree et;
718 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
720 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
722 status = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
723 OCFS2_JOURNAL_ACCESS_WRITE);
724 if (status < 0) {
725 mlog_errno(status);
726 goto leave;
729 prev_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
730 status = ocfs2_add_clusters_in_btree(handle,
731 &et,
732 &logical_start,
733 clusters_to_add,
735 ctxt->data_ac,
736 ctxt->meta_ac,
737 &why);
738 if (status < 0) {
739 mlog_errno(status);
740 goto leave;
743 ocfs2_journal_dirty(handle, vb->vb_bh);
745 clusters_to_add -= le32_to_cpu(vb->vb_xv->xr_clusters) - prev_clusters;
748 * We should have already allocated enough space before the transaction,
749 * so no need to restart.
751 BUG_ON(why != RESTART_NONE || clusters_to_add);
753 leave:
755 return status;
758 static int __ocfs2_remove_xattr_range(struct inode *inode,
759 struct ocfs2_xattr_value_buf *vb,
760 u32 cpos, u32 phys_cpos, u32 len,
761 unsigned int ext_flags,
762 struct ocfs2_xattr_set_ctxt *ctxt)
764 int ret;
765 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
766 handle_t *handle = ctxt->handle;
767 struct ocfs2_extent_tree et;
769 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
771 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
772 OCFS2_JOURNAL_ACCESS_WRITE);
773 if (ret) {
774 mlog_errno(ret);
775 goto out;
778 ret = ocfs2_remove_extent(handle, &et, cpos, len, ctxt->meta_ac,
779 &ctxt->dealloc);
780 if (ret) {
781 mlog_errno(ret);
782 goto out;
785 le32_add_cpu(&vb->vb_xv->xr_clusters, -len);
786 ocfs2_journal_dirty(handle, vb->vb_bh);
788 if (ext_flags & OCFS2_EXT_REFCOUNTED)
789 ret = ocfs2_decrease_refcount(inode, handle,
790 ocfs2_blocks_to_clusters(inode->i_sb,
791 phys_blkno),
792 len, ctxt->meta_ac, &ctxt->dealloc, 1);
793 else
794 ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc,
795 phys_blkno, len);
796 if (ret)
797 mlog_errno(ret);
799 out:
800 return ret;
803 static int ocfs2_xattr_shrink_size(struct inode *inode,
804 u32 old_clusters,
805 u32 new_clusters,
806 struct ocfs2_xattr_value_buf *vb,
807 struct ocfs2_xattr_set_ctxt *ctxt)
809 int ret = 0;
810 unsigned int ext_flags;
811 u32 trunc_len, cpos, phys_cpos, alloc_size;
812 u64 block;
814 if (old_clusters <= new_clusters)
815 return 0;
817 cpos = new_clusters;
818 trunc_len = old_clusters - new_clusters;
819 while (trunc_len) {
820 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
821 &alloc_size,
822 &vb->vb_xv->xr_list, &ext_flags);
823 if (ret) {
824 mlog_errno(ret);
825 goto out;
828 if (alloc_size > trunc_len)
829 alloc_size = trunc_len;
831 ret = __ocfs2_remove_xattr_range(inode, vb, cpos,
832 phys_cpos, alloc_size,
833 ext_flags, ctxt);
834 if (ret) {
835 mlog_errno(ret);
836 goto out;
839 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
840 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode),
841 block, alloc_size);
842 cpos += alloc_size;
843 trunc_len -= alloc_size;
846 out:
847 return ret;
850 static int ocfs2_xattr_value_truncate(struct inode *inode,
851 struct ocfs2_xattr_value_buf *vb,
852 int len,
853 struct ocfs2_xattr_set_ctxt *ctxt)
855 int ret;
856 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
857 u32 old_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
859 if (new_clusters == old_clusters)
860 return 0;
862 if (new_clusters > old_clusters)
863 ret = ocfs2_xattr_extend_allocation(inode,
864 new_clusters - old_clusters,
865 vb, ctxt);
866 else
867 ret = ocfs2_xattr_shrink_size(inode,
868 old_clusters, new_clusters,
869 vb, ctxt);
871 return ret;
874 static int ocfs2_xattr_list_entry(char *buffer, size_t size,
875 size_t *result, const char *prefix,
876 const char *name, int name_len)
878 char *p = buffer + *result;
879 int prefix_len = strlen(prefix);
880 int total_len = prefix_len + name_len + 1;
882 *result += total_len;
884 /* we are just looking for how big our buffer needs to be */
885 if (!size)
886 return 0;
888 if (*result > size)
889 return -ERANGE;
891 memcpy(p, prefix, prefix_len);
892 memcpy(p + prefix_len, name, name_len);
893 p[prefix_len + name_len] = '\0';
895 return 0;
898 static int ocfs2_xattr_list_entries(struct inode *inode,
899 struct ocfs2_xattr_header *header,
900 char *buffer, size_t buffer_size)
902 size_t result = 0;
903 int i, type, ret;
904 const char *prefix, *name;
906 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
907 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
908 type = ocfs2_xattr_get_type(entry);
909 prefix = ocfs2_xattr_prefix(type);
911 if (prefix) {
912 name = (const char *)header +
913 le16_to_cpu(entry->xe_name_offset);
915 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
916 &result, prefix, name,
917 entry->xe_name_len);
918 if (ret)
919 return ret;
923 return result;
926 int ocfs2_has_inline_xattr_value_outside(struct inode *inode,
927 struct ocfs2_dinode *di)
929 struct ocfs2_xattr_header *xh;
930 int i;
932 xh = (struct ocfs2_xattr_header *)
933 ((void *)di + inode->i_sb->s_blocksize -
934 le16_to_cpu(di->i_xattr_inline_size));
936 for (i = 0; i < le16_to_cpu(xh->xh_count); i++)
937 if (!ocfs2_xattr_is_local(&xh->xh_entries[i]))
938 return 1;
940 return 0;
943 static int ocfs2_xattr_ibody_list(struct inode *inode,
944 struct ocfs2_dinode *di,
945 char *buffer,
946 size_t buffer_size)
948 struct ocfs2_xattr_header *header = NULL;
949 struct ocfs2_inode_info *oi = OCFS2_I(inode);
950 int ret = 0;
952 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
953 return ret;
955 header = (struct ocfs2_xattr_header *)
956 ((void *)di + inode->i_sb->s_blocksize -
957 le16_to_cpu(di->i_xattr_inline_size));
959 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
961 return ret;
964 static int ocfs2_xattr_block_list(struct inode *inode,
965 struct ocfs2_dinode *di,
966 char *buffer,
967 size_t buffer_size)
969 struct buffer_head *blk_bh = NULL;
970 struct ocfs2_xattr_block *xb;
971 int ret = 0;
973 if (!di->i_xattr_loc)
974 return ret;
976 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
977 &blk_bh);
978 if (ret < 0) {
979 mlog_errno(ret);
980 return ret;
983 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
984 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
985 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
986 ret = ocfs2_xattr_list_entries(inode, header,
987 buffer, buffer_size);
988 } else
989 ret = ocfs2_xattr_tree_list_index_block(inode, blk_bh,
990 buffer, buffer_size);
992 brelse(blk_bh);
994 return ret;
997 ssize_t ocfs2_listxattr(struct dentry *dentry,
998 char *buffer,
999 size_t size)
1001 int ret = 0, i_ret = 0, b_ret = 0;
1002 struct buffer_head *di_bh = NULL;
1003 struct ocfs2_dinode *di = NULL;
1004 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
1006 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
1007 return -EOPNOTSUPP;
1009 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1010 return ret;
1012 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
1013 if (ret < 0) {
1014 mlog_errno(ret);
1015 return ret;
1018 di = (struct ocfs2_dinode *)di_bh->b_data;
1020 down_read(&oi->ip_xattr_sem);
1021 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
1022 if (i_ret < 0)
1023 b_ret = 0;
1024 else {
1025 if (buffer) {
1026 buffer += i_ret;
1027 size -= i_ret;
1029 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
1030 buffer, size);
1031 if (b_ret < 0)
1032 i_ret = 0;
1034 up_read(&oi->ip_xattr_sem);
1035 ocfs2_inode_unlock(dentry->d_inode, 0);
1037 brelse(di_bh);
1039 return i_ret + b_ret;
1042 static int ocfs2_xattr_find_entry(int name_index,
1043 const char *name,
1044 struct ocfs2_xattr_search *xs)
1046 struct ocfs2_xattr_entry *entry;
1047 size_t name_len;
1048 int i, cmp = 1;
1050 if (name == NULL)
1051 return -EINVAL;
1053 name_len = strlen(name);
1054 entry = xs->here;
1055 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
1056 cmp = name_index - ocfs2_xattr_get_type(entry);
1057 if (!cmp)
1058 cmp = name_len - entry->xe_name_len;
1059 if (!cmp)
1060 cmp = memcmp(name, (xs->base +
1061 le16_to_cpu(entry->xe_name_offset)),
1062 name_len);
1063 if (cmp == 0)
1064 break;
1065 entry += 1;
1067 xs->here = entry;
1069 return cmp ? -ENODATA : 0;
1072 static int ocfs2_xattr_get_value_outside(struct inode *inode,
1073 struct ocfs2_xattr_value_root *xv,
1074 void *buffer,
1075 size_t len)
1077 u32 cpos, p_cluster, num_clusters, bpc, clusters;
1078 u64 blkno;
1079 int i, ret = 0;
1080 size_t cplen, blocksize;
1081 struct buffer_head *bh = NULL;
1082 struct ocfs2_extent_list *el;
1084 el = &xv->xr_list;
1085 clusters = le32_to_cpu(xv->xr_clusters);
1086 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1087 blocksize = inode->i_sb->s_blocksize;
1089 cpos = 0;
1090 while (cpos < clusters) {
1091 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1092 &num_clusters, el, NULL);
1093 if (ret) {
1094 mlog_errno(ret);
1095 goto out;
1098 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1099 /* Copy ocfs2_xattr_value */
1100 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1101 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1102 &bh, NULL);
1103 if (ret) {
1104 mlog_errno(ret);
1105 goto out;
1108 cplen = len >= blocksize ? blocksize : len;
1109 memcpy(buffer, bh->b_data, cplen);
1110 len -= cplen;
1111 buffer += cplen;
1113 brelse(bh);
1114 bh = NULL;
1115 if (len == 0)
1116 break;
1118 cpos += num_clusters;
1120 out:
1121 return ret;
1124 static int ocfs2_xattr_ibody_get(struct inode *inode,
1125 int name_index,
1126 const char *name,
1127 void *buffer,
1128 size_t buffer_size,
1129 struct ocfs2_xattr_search *xs)
1131 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1132 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1133 struct ocfs2_xattr_value_root *xv;
1134 size_t size;
1135 int ret = 0;
1137 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
1138 return -ENODATA;
1140 xs->end = (void *)di + inode->i_sb->s_blocksize;
1141 xs->header = (struct ocfs2_xattr_header *)
1142 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1143 xs->base = (void *)xs->header;
1144 xs->here = xs->header->xh_entries;
1146 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1147 if (ret)
1148 return ret;
1149 size = le64_to_cpu(xs->here->xe_value_size);
1150 if (buffer) {
1151 if (size > buffer_size)
1152 return -ERANGE;
1153 if (ocfs2_xattr_is_local(xs->here)) {
1154 memcpy(buffer, (void *)xs->base +
1155 le16_to_cpu(xs->here->xe_name_offset) +
1156 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
1157 } else {
1158 xv = (struct ocfs2_xattr_value_root *)
1159 (xs->base + le16_to_cpu(
1160 xs->here->xe_name_offset) +
1161 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
1162 ret = ocfs2_xattr_get_value_outside(inode, xv,
1163 buffer, size);
1164 if (ret < 0) {
1165 mlog_errno(ret);
1166 return ret;
1171 return size;
1174 static int ocfs2_xattr_block_get(struct inode *inode,
1175 int name_index,
1176 const char *name,
1177 void *buffer,
1178 size_t buffer_size,
1179 struct ocfs2_xattr_search *xs)
1181 struct ocfs2_xattr_block *xb;
1182 struct ocfs2_xattr_value_root *xv;
1183 size_t size;
1184 int ret = -ENODATA, name_offset, name_len, i;
1185 int uninitialized_var(block_off);
1187 xs->bucket = ocfs2_xattr_bucket_new(inode);
1188 if (!xs->bucket) {
1189 ret = -ENOMEM;
1190 mlog_errno(ret);
1191 goto cleanup;
1194 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
1195 if (ret) {
1196 mlog_errno(ret);
1197 goto cleanup;
1200 if (xs->not_found) {
1201 ret = -ENODATA;
1202 goto cleanup;
1205 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1206 size = le64_to_cpu(xs->here->xe_value_size);
1207 if (buffer) {
1208 ret = -ERANGE;
1209 if (size > buffer_size)
1210 goto cleanup;
1212 name_offset = le16_to_cpu(xs->here->xe_name_offset);
1213 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
1214 i = xs->here - xs->header->xh_entries;
1216 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
1217 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
1218 bucket_xh(xs->bucket),
1220 &block_off,
1221 &name_offset);
1222 xs->base = bucket_block(xs->bucket, block_off);
1224 if (ocfs2_xattr_is_local(xs->here)) {
1225 memcpy(buffer, (void *)xs->base +
1226 name_offset + name_len, size);
1227 } else {
1228 xv = (struct ocfs2_xattr_value_root *)
1229 (xs->base + name_offset + name_len);
1230 ret = ocfs2_xattr_get_value_outside(inode, xv,
1231 buffer, size);
1232 if (ret < 0) {
1233 mlog_errno(ret);
1234 goto cleanup;
1238 ret = size;
1239 cleanup:
1240 ocfs2_xattr_bucket_free(xs->bucket);
1242 brelse(xs->xattr_bh);
1243 xs->xattr_bh = NULL;
1244 return ret;
1247 int ocfs2_xattr_get_nolock(struct inode *inode,
1248 struct buffer_head *di_bh,
1249 int name_index,
1250 const char *name,
1251 void *buffer,
1252 size_t buffer_size)
1254 int ret;
1255 struct ocfs2_dinode *di = NULL;
1256 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1257 struct ocfs2_xattr_search xis = {
1258 .not_found = -ENODATA,
1260 struct ocfs2_xattr_search xbs = {
1261 .not_found = -ENODATA,
1264 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1265 return -EOPNOTSUPP;
1267 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1268 ret = -ENODATA;
1270 xis.inode_bh = xbs.inode_bh = di_bh;
1271 di = (struct ocfs2_dinode *)di_bh->b_data;
1273 down_read(&oi->ip_xattr_sem);
1274 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1275 buffer_size, &xis);
1276 if (ret == -ENODATA && di->i_xattr_loc)
1277 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1278 buffer_size, &xbs);
1279 up_read(&oi->ip_xattr_sem);
1281 return ret;
1284 /* ocfs2_xattr_get()
1286 * Copy an extended attribute into the buffer provided.
1287 * Buffer is NULL to compute the size of buffer required.
1289 static int ocfs2_xattr_get(struct inode *inode,
1290 int name_index,
1291 const char *name,
1292 void *buffer,
1293 size_t buffer_size)
1295 int ret;
1296 struct buffer_head *di_bh = NULL;
1298 ret = ocfs2_inode_lock(inode, &di_bh, 0);
1299 if (ret < 0) {
1300 mlog_errno(ret);
1301 return ret;
1303 ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
1304 name, buffer, buffer_size);
1306 ocfs2_inode_unlock(inode, 0);
1308 brelse(di_bh);
1310 return ret;
1313 static int __ocfs2_xattr_set_value_outside(struct inode *inode,
1314 handle_t *handle,
1315 struct ocfs2_xattr_value_buf *vb,
1316 const void *value,
1317 int value_len)
1319 int ret = 0, i, cp_len;
1320 u16 blocksize = inode->i_sb->s_blocksize;
1321 u32 p_cluster, num_clusters;
1322 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1323 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1324 u64 blkno;
1325 struct buffer_head *bh = NULL;
1326 unsigned int ext_flags;
1327 struct ocfs2_xattr_value_root *xv = vb->vb_xv;
1329 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1331 while (cpos < clusters) {
1332 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1333 &num_clusters, &xv->xr_list,
1334 &ext_flags);
1335 if (ret) {
1336 mlog_errno(ret);
1337 goto out;
1340 BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED);
1342 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1344 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1345 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1346 &bh, NULL);
1347 if (ret) {
1348 mlog_errno(ret);
1349 goto out;
1352 ret = ocfs2_journal_access(handle,
1353 INODE_CACHE(inode),
1355 OCFS2_JOURNAL_ACCESS_WRITE);
1356 if (ret < 0) {
1357 mlog_errno(ret);
1358 goto out;
1361 cp_len = value_len > blocksize ? blocksize : value_len;
1362 memcpy(bh->b_data, value, cp_len);
1363 value_len -= cp_len;
1364 value += cp_len;
1365 if (cp_len < blocksize)
1366 memset(bh->b_data + cp_len, 0,
1367 blocksize - cp_len);
1369 ocfs2_journal_dirty(handle, bh);
1370 brelse(bh);
1371 bh = NULL;
1374 * XXX: do we need to empty all the following
1375 * blocks in this cluster?
1377 if (!value_len)
1378 break;
1380 cpos += num_clusters;
1382 out:
1383 brelse(bh);
1385 return ret;
1388 static int ocfs2_xa_check_space_helper(int needed_space, int free_start,
1389 int num_entries)
1391 int free_space;
1393 if (!needed_space)
1394 return 0;
1396 free_space = free_start -
1397 sizeof(struct ocfs2_xattr_header) -
1398 (num_entries * sizeof(struct ocfs2_xattr_entry)) -
1399 OCFS2_XATTR_HEADER_GAP;
1400 if (free_space < 0)
1401 return -EIO;
1402 if (free_space < needed_space)
1403 return -ENOSPC;
1405 return 0;
1408 static int ocfs2_xa_journal_access(handle_t *handle, struct ocfs2_xa_loc *loc,
1409 int type)
1411 return loc->xl_ops->xlo_journal_access(handle, loc, type);
1414 static void ocfs2_xa_journal_dirty(handle_t *handle, struct ocfs2_xa_loc *loc)
1416 loc->xl_ops->xlo_journal_dirty(handle, loc);
1419 /* Give a pointer into the storage for the given offset */
1420 static void *ocfs2_xa_offset_pointer(struct ocfs2_xa_loc *loc, int offset)
1422 BUG_ON(offset >= loc->xl_size);
1423 return loc->xl_ops->xlo_offset_pointer(loc, offset);
1427 * Wipe the name+value pair and allow the storage to reclaim it. This
1428 * must be followed by either removal of the entry or a call to
1429 * ocfs2_xa_add_namevalue().
1431 static void ocfs2_xa_wipe_namevalue(struct ocfs2_xa_loc *loc)
1433 loc->xl_ops->xlo_wipe_namevalue(loc);
1437 * Find lowest offset to a name+value pair. This is the start of our
1438 * downward-growing free space.
1440 static int ocfs2_xa_get_free_start(struct ocfs2_xa_loc *loc)
1442 return loc->xl_ops->xlo_get_free_start(loc);
1445 /* Can we reuse loc->xl_entry for xi? */
1446 static int ocfs2_xa_can_reuse_entry(struct ocfs2_xa_loc *loc,
1447 struct ocfs2_xattr_info *xi)
1449 return loc->xl_ops->xlo_can_reuse(loc, xi);
1452 /* How much free space is needed to set the new value */
1453 static int ocfs2_xa_check_space(struct ocfs2_xa_loc *loc,
1454 struct ocfs2_xattr_info *xi)
1456 return loc->xl_ops->xlo_check_space(loc, xi);
1459 static void ocfs2_xa_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1461 loc->xl_ops->xlo_add_entry(loc, name_hash);
1462 loc->xl_entry->xe_name_hash = cpu_to_le32(name_hash);
1464 * We can't leave the new entry's xe_name_offset at zero or
1465 * add_namevalue() will go nuts. We set it to the size of our
1466 * storage so that it can never be less than any other entry.
1468 loc->xl_entry->xe_name_offset = cpu_to_le16(loc->xl_size);
1471 static void ocfs2_xa_add_namevalue(struct ocfs2_xa_loc *loc,
1472 struct ocfs2_xattr_info *xi)
1474 int size = namevalue_size_xi(xi);
1475 int nameval_offset;
1476 char *nameval_buf;
1478 loc->xl_ops->xlo_add_namevalue(loc, size);
1479 loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
1480 loc->xl_entry->xe_name_len = xi->xi_name_len;
1481 ocfs2_xattr_set_type(loc->xl_entry, xi->xi_name_index);
1482 ocfs2_xattr_set_local(loc->xl_entry,
1483 xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE);
1485 nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1486 nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
1487 memset(nameval_buf, 0, size);
1488 memcpy(nameval_buf, xi->xi_name, xi->xi_name_len);
1491 static void ocfs2_xa_fill_value_buf(struct ocfs2_xa_loc *loc,
1492 struct ocfs2_xattr_value_buf *vb)
1494 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1495 int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len);
1497 /* Value bufs are for value trees */
1498 BUG_ON(ocfs2_xattr_is_local(loc->xl_entry));
1499 BUG_ON(namevalue_size_xe(loc->xl_entry) !=
1500 (name_size + OCFS2_XATTR_ROOT_SIZE));
1502 loc->xl_ops->xlo_fill_value_buf(loc, vb);
1503 vb->vb_xv =
1504 (struct ocfs2_xattr_value_root *)ocfs2_xa_offset_pointer(loc,
1505 nameval_offset +
1506 name_size);
1509 static int ocfs2_xa_block_journal_access(handle_t *handle,
1510 struct ocfs2_xa_loc *loc, int type)
1512 struct buffer_head *bh = loc->xl_storage;
1513 ocfs2_journal_access_func access;
1515 if (loc->xl_size == (bh->b_size -
1516 offsetof(struct ocfs2_xattr_block,
1517 xb_attrs.xb_header)))
1518 access = ocfs2_journal_access_xb;
1519 else
1520 access = ocfs2_journal_access_di;
1521 return access(handle, INODE_CACHE(loc->xl_inode), bh, type);
1524 static void ocfs2_xa_block_journal_dirty(handle_t *handle,
1525 struct ocfs2_xa_loc *loc)
1527 struct buffer_head *bh = loc->xl_storage;
1529 ocfs2_journal_dirty(handle, bh);
1532 static void *ocfs2_xa_block_offset_pointer(struct ocfs2_xa_loc *loc,
1533 int offset)
1535 return (char *)loc->xl_header + offset;
1538 static int ocfs2_xa_block_can_reuse(struct ocfs2_xa_loc *loc,
1539 struct ocfs2_xattr_info *xi)
1542 * Block storage is strict. If the sizes aren't exact, we will
1543 * remove the old one and reinsert the new.
1545 return namevalue_size_xe(loc->xl_entry) ==
1546 namevalue_size_xi(xi);
1549 static int ocfs2_xa_block_get_free_start(struct ocfs2_xa_loc *loc)
1551 struct ocfs2_xattr_header *xh = loc->xl_header;
1552 int i, count = le16_to_cpu(xh->xh_count);
1553 int offset, free_start = loc->xl_size;
1555 for (i = 0; i < count; i++) {
1556 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1557 if (offset < free_start)
1558 free_start = offset;
1561 return free_start;
1564 static int ocfs2_xa_block_check_space(struct ocfs2_xa_loc *loc,
1565 struct ocfs2_xattr_info *xi)
1567 int count = le16_to_cpu(loc->xl_header->xh_count);
1568 int free_start = ocfs2_xa_get_free_start(loc);
1569 int needed_space = ocfs2_xi_entry_usage(xi);
1572 * Block storage will reclaim the original entry before inserting
1573 * the new value, so we only need the difference. If the new
1574 * entry is smaller than the old one, we don't need anything.
1576 if (loc->xl_entry) {
1577 /* Don't need space if we're reusing! */
1578 if (ocfs2_xa_can_reuse_entry(loc, xi))
1579 needed_space = 0;
1580 else
1581 needed_space -= ocfs2_xe_entry_usage(loc->xl_entry);
1583 if (needed_space < 0)
1584 needed_space = 0;
1585 return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1589 * Block storage for xattrs keeps the name+value pairs compacted. When
1590 * we remove one, we have to shift any that preceded it towards the end.
1592 static void ocfs2_xa_block_wipe_namevalue(struct ocfs2_xa_loc *loc)
1594 int i, offset;
1595 int namevalue_offset, first_namevalue_offset, namevalue_size;
1596 struct ocfs2_xattr_entry *entry = loc->xl_entry;
1597 struct ocfs2_xattr_header *xh = loc->xl_header;
1598 int count = le16_to_cpu(xh->xh_count);
1600 namevalue_offset = le16_to_cpu(entry->xe_name_offset);
1601 namevalue_size = namevalue_size_xe(entry);
1602 first_namevalue_offset = ocfs2_xa_get_free_start(loc);
1604 /* Shift the name+value pairs */
1605 memmove((char *)xh + first_namevalue_offset + namevalue_size,
1606 (char *)xh + first_namevalue_offset,
1607 namevalue_offset - first_namevalue_offset);
1608 memset((char *)xh + first_namevalue_offset, 0, namevalue_size);
1610 /* Now tell xh->xh_entries about it */
1611 for (i = 0; i < count; i++) {
1612 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1613 if (offset <= namevalue_offset)
1614 le16_add_cpu(&xh->xh_entries[i].xe_name_offset,
1615 namevalue_size);
1619 * Note that we don't update xh_free_start or xh_name_value_len
1620 * because they're not used in block-stored xattrs.
1624 static void ocfs2_xa_block_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1626 int count = le16_to_cpu(loc->xl_header->xh_count);
1627 loc->xl_entry = &(loc->xl_header->xh_entries[count]);
1628 le16_add_cpu(&loc->xl_header->xh_count, 1);
1629 memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1632 static void ocfs2_xa_block_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1634 int free_start = ocfs2_xa_get_free_start(loc);
1636 loc->xl_entry->xe_name_offset = cpu_to_le16(free_start - size);
1639 static void ocfs2_xa_block_fill_value_buf(struct ocfs2_xa_loc *loc,
1640 struct ocfs2_xattr_value_buf *vb)
1642 struct buffer_head *bh = loc->xl_storage;
1644 if (loc->xl_size == (bh->b_size -
1645 offsetof(struct ocfs2_xattr_block,
1646 xb_attrs.xb_header)))
1647 vb->vb_access = ocfs2_journal_access_xb;
1648 else
1649 vb->vb_access = ocfs2_journal_access_di;
1650 vb->vb_bh = bh;
1654 * Operations for xattrs stored in blocks. This includes inline inode
1655 * storage and unindexed ocfs2_xattr_blocks.
1657 static const struct ocfs2_xa_loc_operations ocfs2_xa_block_loc_ops = {
1658 .xlo_journal_access = ocfs2_xa_block_journal_access,
1659 .xlo_journal_dirty = ocfs2_xa_block_journal_dirty,
1660 .xlo_offset_pointer = ocfs2_xa_block_offset_pointer,
1661 .xlo_check_space = ocfs2_xa_block_check_space,
1662 .xlo_can_reuse = ocfs2_xa_block_can_reuse,
1663 .xlo_get_free_start = ocfs2_xa_block_get_free_start,
1664 .xlo_wipe_namevalue = ocfs2_xa_block_wipe_namevalue,
1665 .xlo_add_entry = ocfs2_xa_block_add_entry,
1666 .xlo_add_namevalue = ocfs2_xa_block_add_namevalue,
1667 .xlo_fill_value_buf = ocfs2_xa_block_fill_value_buf,
1670 static int ocfs2_xa_bucket_journal_access(handle_t *handle,
1671 struct ocfs2_xa_loc *loc, int type)
1673 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1675 return ocfs2_xattr_bucket_journal_access(handle, bucket, type);
1678 static void ocfs2_xa_bucket_journal_dirty(handle_t *handle,
1679 struct ocfs2_xa_loc *loc)
1681 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1683 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
1686 static void *ocfs2_xa_bucket_offset_pointer(struct ocfs2_xa_loc *loc,
1687 int offset)
1689 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1690 int block, block_offset;
1692 /* The header is at the front of the bucket */
1693 block = offset >> loc->xl_inode->i_sb->s_blocksize_bits;
1694 block_offset = offset % loc->xl_inode->i_sb->s_blocksize;
1696 return bucket_block(bucket, block) + block_offset;
1699 static int ocfs2_xa_bucket_can_reuse(struct ocfs2_xa_loc *loc,
1700 struct ocfs2_xattr_info *xi)
1702 return namevalue_size_xe(loc->xl_entry) >=
1703 namevalue_size_xi(xi);
1706 static int ocfs2_xa_bucket_get_free_start(struct ocfs2_xa_loc *loc)
1708 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1709 return le16_to_cpu(bucket_xh(bucket)->xh_free_start);
1712 static int ocfs2_bucket_align_free_start(struct super_block *sb,
1713 int free_start, int size)
1716 * We need to make sure that the name+value pair fits within
1717 * one block.
1719 if (((free_start - size) >> sb->s_blocksize_bits) !=
1720 ((free_start - 1) >> sb->s_blocksize_bits))
1721 free_start -= free_start % sb->s_blocksize;
1723 return free_start;
1726 static int ocfs2_xa_bucket_check_space(struct ocfs2_xa_loc *loc,
1727 struct ocfs2_xattr_info *xi)
1729 int rc;
1730 int count = le16_to_cpu(loc->xl_header->xh_count);
1731 int free_start = ocfs2_xa_get_free_start(loc);
1732 int needed_space = ocfs2_xi_entry_usage(xi);
1733 int size = namevalue_size_xi(xi);
1734 struct super_block *sb = loc->xl_inode->i_sb;
1737 * Bucket storage does not reclaim name+value pairs it cannot
1738 * reuse. They live as holes until the bucket fills, and then
1739 * the bucket is defragmented. However, the bucket can reclaim
1740 * the ocfs2_xattr_entry.
1742 if (loc->xl_entry) {
1743 /* Don't need space if we're reusing! */
1744 if (ocfs2_xa_can_reuse_entry(loc, xi))
1745 needed_space = 0;
1746 else
1747 needed_space -= sizeof(struct ocfs2_xattr_entry);
1749 BUG_ON(needed_space < 0);
1751 if (free_start < size) {
1752 if (needed_space)
1753 return -ENOSPC;
1754 } else {
1756 * First we check if it would fit in the first place.
1757 * Below, we align the free start to a block. This may
1758 * slide us below the minimum gap. By checking unaligned
1759 * first, we avoid that error.
1761 rc = ocfs2_xa_check_space_helper(needed_space, free_start,
1762 count);
1763 if (rc)
1764 return rc;
1765 free_start = ocfs2_bucket_align_free_start(sb, free_start,
1766 size);
1768 return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1771 static void ocfs2_xa_bucket_wipe_namevalue(struct ocfs2_xa_loc *loc)
1773 le16_add_cpu(&loc->xl_header->xh_name_value_len,
1774 -namevalue_size_xe(loc->xl_entry));
1777 static void ocfs2_xa_bucket_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1779 struct ocfs2_xattr_header *xh = loc->xl_header;
1780 int count = le16_to_cpu(xh->xh_count);
1781 int low = 0, high = count - 1, tmp;
1782 struct ocfs2_xattr_entry *tmp_xe;
1785 * We keep buckets sorted by name_hash, so we need to find
1786 * our insert place.
1788 while (low <= high && count) {
1789 tmp = (low + high) / 2;
1790 tmp_xe = &xh->xh_entries[tmp];
1792 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
1793 low = tmp + 1;
1794 else if (name_hash < le32_to_cpu(tmp_xe->xe_name_hash))
1795 high = tmp - 1;
1796 else {
1797 low = tmp;
1798 break;
1802 if (low != count)
1803 memmove(&xh->xh_entries[low + 1],
1804 &xh->xh_entries[low],
1805 ((count - low) * sizeof(struct ocfs2_xattr_entry)));
1807 le16_add_cpu(&xh->xh_count, 1);
1808 loc->xl_entry = &xh->xh_entries[low];
1809 memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1812 static void ocfs2_xa_bucket_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1814 int free_start = ocfs2_xa_get_free_start(loc);
1815 struct ocfs2_xattr_header *xh = loc->xl_header;
1816 struct super_block *sb = loc->xl_inode->i_sb;
1817 int nameval_offset;
1819 free_start = ocfs2_bucket_align_free_start(sb, free_start, size);
1820 nameval_offset = free_start - size;
1821 loc->xl_entry->xe_name_offset = cpu_to_le16(nameval_offset);
1822 xh->xh_free_start = cpu_to_le16(nameval_offset);
1823 le16_add_cpu(&xh->xh_name_value_len, size);
1827 static void ocfs2_xa_bucket_fill_value_buf(struct ocfs2_xa_loc *loc,
1828 struct ocfs2_xattr_value_buf *vb)
1830 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1831 struct super_block *sb = loc->xl_inode->i_sb;
1832 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1833 int size = namevalue_size_xe(loc->xl_entry);
1834 int block_offset = nameval_offset >> sb->s_blocksize_bits;
1836 /* Values are not allowed to straddle block boundaries */
1837 BUG_ON(block_offset !=
1838 ((nameval_offset + size - 1) >> sb->s_blocksize_bits));
1839 /* We expect the bucket to be filled in */
1840 BUG_ON(!bucket->bu_bhs[block_offset]);
1842 vb->vb_access = ocfs2_journal_access;
1843 vb->vb_bh = bucket->bu_bhs[block_offset];
1846 /* Operations for xattrs stored in buckets. */
1847 static const struct ocfs2_xa_loc_operations ocfs2_xa_bucket_loc_ops = {
1848 .xlo_journal_access = ocfs2_xa_bucket_journal_access,
1849 .xlo_journal_dirty = ocfs2_xa_bucket_journal_dirty,
1850 .xlo_offset_pointer = ocfs2_xa_bucket_offset_pointer,
1851 .xlo_check_space = ocfs2_xa_bucket_check_space,
1852 .xlo_can_reuse = ocfs2_xa_bucket_can_reuse,
1853 .xlo_get_free_start = ocfs2_xa_bucket_get_free_start,
1854 .xlo_wipe_namevalue = ocfs2_xa_bucket_wipe_namevalue,
1855 .xlo_add_entry = ocfs2_xa_bucket_add_entry,
1856 .xlo_add_namevalue = ocfs2_xa_bucket_add_namevalue,
1857 .xlo_fill_value_buf = ocfs2_xa_bucket_fill_value_buf,
1860 static unsigned int ocfs2_xa_value_clusters(struct ocfs2_xa_loc *loc)
1862 struct ocfs2_xattr_value_buf vb;
1864 if (ocfs2_xattr_is_local(loc->xl_entry))
1865 return 0;
1867 ocfs2_xa_fill_value_buf(loc, &vb);
1868 return le32_to_cpu(vb.vb_xv->xr_clusters);
1871 static int ocfs2_xa_value_truncate(struct ocfs2_xa_loc *loc, u64 bytes,
1872 struct ocfs2_xattr_set_ctxt *ctxt)
1874 int trunc_rc, access_rc;
1875 struct ocfs2_xattr_value_buf vb;
1877 ocfs2_xa_fill_value_buf(loc, &vb);
1878 trunc_rc = ocfs2_xattr_value_truncate(loc->xl_inode, &vb, bytes,
1879 ctxt);
1882 * The caller of ocfs2_xa_value_truncate() has already called
1883 * ocfs2_xa_journal_access on the loc. However, The truncate code
1884 * calls ocfs2_extend_trans(). This may commit the previous
1885 * transaction and open a new one. If this is a bucket, truncate
1886 * could leave only vb->vb_bh set up for journaling. Meanwhile,
1887 * the caller is expecting to dirty the entire bucket. So we must
1888 * reset the journal work. We do this even if truncate has failed,
1889 * as it could have failed after committing the extend.
1891 access_rc = ocfs2_xa_journal_access(ctxt->handle, loc,
1892 OCFS2_JOURNAL_ACCESS_WRITE);
1894 /* Errors in truncate take precedence */
1895 return trunc_rc ? trunc_rc : access_rc;
1898 static void ocfs2_xa_remove_entry(struct ocfs2_xa_loc *loc)
1900 int index, count;
1901 struct ocfs2_xattr_header *xh = loc->xl_header;
1902 struct ocfs2_xattr_entry *entry = loc->xl_entry;
1904 ocfs2_xa_wipe_namevalue(loc);
1905 loc->xl_entry = NULL;
1907 le16_add_cpu(&xh->xh_count, -1);
1908 count = le16_to_cpu(xh->xh_count);
1911 * Only zero out the entry if there are more remaining. This is
1912 * important for an empty bucket, as it keeps track of the
1913 * bucket's hash value. It doesn't hurt empty block storage.
1915 if (count) {
1916 index = ((char *)entry - (char *)&xh->xh_entries) /
1917 sizeof(struct ocfs2_xattr_entry);
1918 memmove(&xh->xh_entries[index], &xh->xh_entries[index + 1],
1919 (count - index) * sizeof(struct ocfs2_xattr_entry));
1920 memset(&xh->xh_entries[count], 0,
1921 sizeof(struct ocfs2_xattr_entry));
1926 * If we have a problem adjusting the size of an external value during
1927 * ocfs2_xa_prepare_entry() or ocfs2_xa_remove(), we may have an xattr
1928 * in an intermediate state. For example, the value may be partially
1929 * truncated.
1931 * If the value tree hasn't changed, the extend/truncate went nowhere.
1932 * We have nothing to do. The caller can treat it as a straight error.
1934 * If the value tree got partially truncated, we now have a corrupted
1935 * extended attribute. We're going to wipe its entry and leak the
1936 * clusters. Better to leak some storage than leave a corrupt entry.
1938 * If the value tree grew, it obviously didn't grow enough for the
1939 * new entry. We're not going to try and reclaim those clusters either.
1940 * If there was already an external value there (orig_clusters != 0),
1941 * the new clusters are attached safely and we can just leave the old
1942 * value in place. If there was no external value there, we remove
1943 * the entry.
1945 * This way, the xattr block we store in the journal will be consistent.
1946 * If the size change broke because of the journal, no changes will hit
1947 * disk anyway.
1949 static void ocfs2_xa_cleanup_value_truncate(struct ocfs2_xa_loc *loc,
1950 const char *what,
1951 unsigned int orig_clusters)
1953 unsigned int new_clusters = ocfs2_xa_value_clusters(loc);
1954 char *nameval_buf = ocfs2_xa_offset_pointer(loc,
1955 le16_to_cpu(loc->xl_entry->xe_name_offset));
1957 if (new_clusters < orig_clusters) {
1958 mlog(ML_ERROR,
1959 "Partial truncate while %s xattr %.*s. Leaking "
1960 "%u clusters and removing the entry\n",
1961 what, loc->xl_entry->xe_name_len, nameval_buf,
1962 orig_clusters - new_clusters);
1963 ocfs2_xa_remove_entry(loc);
1964 } else if (!orig_clusters) {
1965 mlog(ML_ERROR,
1966 "Unable to allocate an external value for xattr "
1967 "%.*s safely. Leaking %u clusters and removing the "
1968 "entry\n",
1969 loc->xl_entry->xe_name_len, nameval_buf,
1970 new_clusters - orig_clusters);
1971 ocfs2_xa_remove_entry(loc);
1972 } else if (new_clusters > orig_clusters)
1973 mlog(ML_ERROR,
1974 "Unable to grow xattr %.*s safely. %u new clusters "
1975 "have been added, but the value will not be "
1976 "modified\n",
1977 loc->xl_entry->xe_name_len, nameval_buf,
1978 new_clusters - orig_clusters);
1981 static int ocfs2_xa_remove(struct ocfs2_xa_loc *loc,
1982 struct ocfs2_xattr_set_ctxt *ctxt)
1984 int rc = 0;
1985 unsigned int orig_clusters;
1987 if (!ocfs2_xattr_is_local(loc->xl_entry)) {
1988 orig_clusters = ocfs2_xa_value_clusters(loc);
1989 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
1990 if (rc) {
1991 mlog_errno(rc);
1993 * Since this is remove, we can return 0 if
1994 * ocfs2_xa_cleanup_value_truncate() is going to
1995 * wipe the entry anyway. So we check the
1996 * cluster count as well.
1998 if (orig_clusters != ocfs2_xa_value_clusters(loc))
1999 rc = 0;
2000 ocfs2_xa_cleanup_value_truncate(loc, "removing",
2001 orig_clusters);
2002 if (rc)
2003 goto out;
2007 ocfs2_xa_remove_entry(loc);
2009 out:
2010 return rc;
2013 static void ocfs2_xa_install_value_root(struct ocfs2_xa_loc *loc)
2015 int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len);
2016 char *nameval_buf;
2018 nameval_buf = ocfs2_xa_offset_pointer(loc,
2019 le16_to_cpu(loc->xl_entry->xe_name_offset));
2020 memcpy(nameval_buf + name_size, &def_xv, OCFS2_XATTR_ROOT_SIZE);
2024 * Take an existing entry and make it ready for the new value. This
2025 * won't allocate space, but it may free space. It should be ready for
2026 * ocfs2_xa_prepare_entry() to finish the work.
2028 static int ocfs2_xa_reuse_entry(struct ocfs2_xa_loc *loc,
2029 struct ocfs2_xattr_info *xi,
2030 struct ocfs2_xattr_set_ctxt *ctxt)
2032 int rc = 0;
2033 int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
2034 unsigned int orig_clusters;
2035 char *nameval_buf;
2036 int xe_local = ocfs2_xattr_is_local(loc->xl_entry);
2037 int xi_local = xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE;
2039 BUG_ON(OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len) !=
2040 name_size);
2042 nameval_buf = ocfs2_xa_offset_pointer(loc,
2043 le16_to_cpu(loc->xl_entry->xe_name_offset));
2044 if (xe_local) {
2045 memset(nameval_buf + name_size, 0,
2046 namevalue_size_xe(loc->xl_entry) - name_size);
2047 if (!xi_local)
2048 ocfs2_xa_install_value_root(loc);
2049 } else {
2050 orig_clusters = ocfs2_xa_value_clusters(loc);
2051 if (xi_local) {
2052 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
2053 if (rc < 0)
2054 mlog_errno(rc);
2055 else
2056 memset(nameval_buf + name_size, 0,
2057 namevalue_size_xe(loc->xl_entry) -
2058 name_size);
2059 } else if (le64_to_cpu(loc->xl_entry->xe_value_size) >
2060 xi->xi_value_len) {
2061 rc = ocfs2_xa_value_truncate(loc, xi->xi_value_len,
2062 ctxt);
2063 if (rc < 0)
2064 mlog_errno(rc);
2067 if (rc) {
2068 ocfs2_xa_cleanup_value_truncate(loc, "reusing",
2069 orig_clusters);
2070 goto out;
2074 loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
2075 ocfs2_xattr_set_local(loc->xl_entry, xi_local);
2077 out:
2078 return rc;
2082 * Prepares loc->xl_entry to receive the new xattr. This includes
2083 * properly setting up the name+value pair region. If loc->xl_entry
2084 * already exists, it will take care of modifying it appropriately.
2086 * Note that this modifies the data. You did journal_access already,
2087 * right?
2089 static int ocfs2_xa_prepare_entry(struct ocfs2_xa_loc *loc,
2090 struct ocfs2_xattr_info *xi,
2091 u32 name_hash,
2092 struct ocfs2_xattr_set_ctxt *ctxt)
2094 int rc = 0;
2095 unsigned int orig_clusters;
2096 __le64 orig_value_size = 0;
2098 rc = ocfs2_xa_check_space(loc, xi);
2099 if (rc)
2100 goto out;
2102 if (loc->xl_entry) {
2103 if (ocfs2_xa_can_reuse_entry(loc, xi)) {
2104 orig_value_size = loc->xl_entry->xe_value_size;
2105 rc = ocfs2_xa_reuse_entry(loc, xi, ctxt);
2106 if (rc)
2107 goto out;
2108 goto alloc_value;
2111 if (!ocfs2_xattr_is_local(loc->xl_entry)) {
2112 orig_clusters = ocfs2_xa_value_clusters(loc);
2113 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
2114 if (rc) {
2115 mlog_errno(rc);
2116 ocfs2_xa_cleanup_value_truncate(loc,
2117 "overwriting",
2118 orig_clusters);
2119 goto out;
2122 ocfs2_xa_wipe_namevalue(loc);
2123 } else
2124 ocfs2_xa_add_entry(loc, name_hash);
2127 * If we get here, we have a blank entry. Fill it. We grow our
2128 * name+value pair back from the end.
2130 ocfs2_xa_add_namevalue(loc, xi);
2131 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
2132 ocfs2_xa_install_value_root(loc);
2134 alloc_value:
2135 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2136 orig_clusters = ocfs2_xa_value_clusters(loc);
2137 rc = ocfs2_xa_value_truncate(loc, xi->xi_value_len, ctxt);
2138 if (rc < 0) {
2139 ctxt->set_abort = 1;
2140 ocfs2_xa_cleanup_value_truncate(loc, "growing",
2141 orig_clusters);
2143 * If we were growing an existing value,
2144 * ocfs2_xa_cleanup_value_truncate() won't remove
2145 * the entry. We need to restore the original value
2146 * size.
2148 if (loc->xl_entry) {
2149 BUG_ON(!orig_value_size);
2150 loc->xl_entry->xe_value_size = orig_value_size;
2152 mlog_errno(rc);
2156 out:
2157 return rc;
2161 * Store the value portion of the name+value pair. This will skip
2162 * values that are stored externally. Their tree roots were set up
2163 * by ocfs2_xa_prepare_entry().
2165 static int ocfs2_xa_store_value(struct ocfs2_xa_loc *loc,
2166 struct ocfs2_xattr_info *xi,
2167 struct ocfs2_xattr_set_ctxt *ctxt)
2169 int rc = 0;
2170 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
2171 int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
2172 char *nameval_buf;
2173 struct ocfs2_xattr_value_buf vb;
2175 nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
2176 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2177 ocfs2_xa_fill_value_buf(loc, &vb);
2178 rc = __ocfs2_xattr_set_value_outside(loc->xl_inode,
2179 ctxt->handle, &vb,
2180 xi->xi_value,
2181 xi->xi_value_len);
2182 } else
2183 memcpy(nameval_buf + name_size, xi->xi_value, xi->xi_value_len);
2185 return rc;
2188 static int ocfs2_xa_set(struct ocfs2_xa_loc *loc,
2189 struct ocfs2_xattr_info *xi,
2190 struct ocfs2_xattr_set_ctxt *ctxt)
2192 int ret;
2193 u32 name_hash = ocfs2_xattr_name_hash(loc->xl_inode, xi->xi_name,
2194 xi->xi_name_len);
2196 ret = ocfs2_xa_journal_access(ctxt->handle, loc,
2197 OCFS2_JOURNAL_ACCESS_WRITE);
2198 if (ret) {
2199 mlog_errno(ret);
2200 goto out;
2204 * From here on out, everything is going to modify the buffer a
2205 * little. Errors are going to leave the xattr header in a
2206 * sane state. Thus, even with errors we dirty the sucker.
2209 /* Don't worry, we are never called with !xi_value and !xl_entry */
2210 if (!xi->xi_value) {
2211 ret = ocfs2_xa_remove(loc, ctxt);
2212 goto out_dirty;
2215 ret = ocfs2_xa_prepare_entry(loc, xi, name_hash, ctxt);
2216 if (ret) {
2217 if (ret != -ENOSPC)
2218 mlog_errno(ret);
2219 goto out_dirty;
2222 ret = ocfs2_xa_store_value(loc, xi, ctxt);
2223 if (ret)
2224 mlog_errno(ret);
2226 out_dirty:
2227 ocfs2_xa_journal_dirty(ctxt->handle, loc);
2229 out:
2230 return ret;
2233 static void ocfs2_init_dinode_xa_loc(struct ocfs2_xa_loc *loc,
2234 struct inode *inode,
2235 struct buffer_head *bh,
2236 struct ocfs2_xattr_entry *entry)
2238 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
2240 BUG_ON(!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_XATTR_FL));
2242 loc->xl_inode = inode;
2243 loc->xl_ops = &ocfs2_xa_block_loc_ops;
2244 loc->xl_storage = bh;
2245 loc->xl_entry = entry;
2246 loc->xl_size = le16_to_cpu(di->i_xattr_inline_size);
2247 loc->xl_header =
2248 (struct ocfs2_xattr_header *)(bh->b_data + bh->b_size -
2249 loc->xl_size);
2252 static void ocfs2_init_xattr_block_xa_loc(struct ocfs2_xa_loc *loc,
2253 struct inode *inode,
2254 struct buffer_head *bh,
2255 struct ocfs2_xattr_entry *entry)
2257 struct ocfs2_xattr_block *xb =
2258 (struct ocfs2_xattr_block *)bh->b_data;
2260 BUG_ON(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED);
2262 loc->xl_inode = inode;
2263 loc->xl_ops = &ocfs2_xa_block_loc_ops;
2264 loc->xl_storage = bh;
2265 loc->xl_header = &(xb->xb_attrs.xb_header);
2266 loc->xl_entry = entry;
2267 loc->xl_size = bh->b_size - offsetof(struct ocfs2_xattr_block,
2268 xb_attrs.xb_header);
2271 static void ocfs2_init_xattr_bucket_xa_loc(struct ocfs2_xa_loc *loc,
2272 struct ocfs2_xattr_bucket *bucket,
2273 struct ocfs2_xattr_entry *entry)
2275 loc->xl_inode = bucket->bu_inode;
2276 loc->xl_ops = &ocfs2_xa_bucket_loc_ops;
2277 loc->xl_storage = bucket;
2278 loc->xl_header = bucket_xh(bucket);
2279 loc->xl_entry = entry;
2280 loc->xl_size = OCFS2_XATTR_BUCKET_SIZE;
2284 * In xattr remove, if it is stored outside and refcounted, we may have
2285 * the chance to split the refcount tree. So need the allocators.
2287 static int ocfs2_lock_xattr_remove_allocators(struct inode *inode,
2288 struct ocfs2_xattr_value_root *xv,
2289 struct ocfs2_caching_info *ref_ci,
2290 struct buffer_head *ref_root_bh,
2291 struct ocfs2_alloc_context **meta_ac,
2292 int *ref_credits)
2294 int ret, meta_add = 0;
2295 u32 p_cluster, num_clusters;
2296 unsigned int ext_flags;
2298 *ref_credits = 0;
2299 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
2300 &num_clusters,
2301 &xv->xr_list,
2302 &ext_flags);
2303 if (ret) {
2304 mlog_errno(ret);
2305 goto out;
2308 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
2309 goto out;
2311 ret = ocfs2_refcounted_xattr_delete_need(inode, ref_ci,
2312 ref_root_bh, xv,
2313 &meta_add, ref_credits);
2314 if (ret) {
2315 mlog_errno(ret);
2316 goto out;
2319 ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
2320 meta_add, meta_ac);
2321 if (ret)
2322 mlog_errno(ret);
2324 out:
2325 return ret;
2328 static int ocfs2_remove_value_outside(struct inode*inode,
2329 struct ocfs2_xattr_value_buf *vb,
2330 struct ocfs2_xattr_header *header,
2331 struct ocfs2_caching_info *ref_ci,
2332 struct buffer_head *ref_root_bh)
2334 int ret = 0, i, ref_credits;
2335 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2336 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
2337 void *val;
2339 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
2341 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
2342 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
2344 if (ocfs2_xattr_is_local(entry))
2345 continue;
2347 val = (void *)header +
2348 le16_to_cpu(entry->xe_name_offset);
2349 vb->vb_xv = (struct ocfs2_xattr_value_root *)
2350 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
2352 ret = ocfs2_lock_xattr_remove_allocators(inode, vb->vb_xv,
2353 ref_ci, ref_root_bh,
2354 &ctxt.meta_ac,
2355 &ref_credits);
2357 ctxt.handle = ocfs2_start_trans(osb, ref_credits +
2358 ocfs2_remove_extent_credits(osb->sb));
2359 if (IS_ERR(ctxt.handle)) {
2360 ret = PTR_ERR(ctxt.handle);
2361 mlog_errno(ret);
2362 break;
2365 ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt);
2366 if (ret < 0) {
2367 mlog_errno(ret);
2368 break;
2371 ocfs2_commit_trans(osb, ctxt.handle);
2372 if (ctxt.meta_ac) {
2373 ocfs2_free_alloc_context(ctxt.meta_ac);
2374 ctxt.meta_ac = NULL;
2378 if (ctxt.meta_ac)
2379 ocfs2_free_alloc_context(ctxt.meta_ac);
2380 ocfs2_schedule_truncate_log_flush(osb, 1);
2381 ocfs2_run_deallocs(osb, &ctxt.dealloc);
2382 return ret;
2385 static int ocfs2_xattr_ibody_remove(struct inode *inode,
2386 struct buffer_head *di_bh,
2387 struct ocfs2_caching_info *ref_ci,
2388 struct buffer_head *ref_root_bh)
2391 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2392 struct ocfs2_xattr_header *header;
2393 int ret;
2394 struct ocfs2_xattr_value_buf vb = {
2395 .vb_bh = di_bh,
2396 .vb_access = ocfs2_journal_access_di,
2399 header = (struct ocfs2_xattr_header *)
2400 ((void *)di + inode->i_sb->s_blocksize -
2401 le16_to_cpu(di->i_xattr_inline_size));
2403 ret = ocfs2_remove_value_outside(inode, &vb, header,
2404 ref_ci, ref_root_bh);
2406 return ret;
2409 struct ocfs2_rm_xattr_bucket_para {
2410 struct ocfs2_caching_info *ref_ci;
2411 struct buffer_head *ref_root_bh;
2414 static int ocfs2_xattr_block_remove(struct inode *inode,
2415 struct buffer_head *blk_bh,
2416 struct ocfs2_caching_info *ref_ci,
2417 struct buffer_head *ref_root_bh)
2419 struct ocfs2_xattr_block *xb;
2420 int ret = 0;
2421 struct ocfs2_xattr_value_buf vb = {
2422 .vb_bh = blk_bh,
2423 .vb_access = ocfs2_journal_access_xb,
2425 struct ocfs2_rm_xattr_bucket_para args = {
2426 .ref_ci = ref_ci,
2427 .ref_root_bh = ref_root_bh,
2430 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2431 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2432 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
2433 ret = ocfs2_remove_value_outside(inode, &vb, header,
2434 ref_ci, ref_root_bh);
2435 } else
2436 ret = ocfs2_iterate_xattr_index_block(inode,
2437 blk_bh,
2438 ocfs2_rm_xattr_cluster,
2439 &args);
2441 return ret;
2444 static int ocfs2_xattr_free_block(struct inode *inode,
2445 u64 block,
2446 struct ocfs2_caching_info *ref_ci,
2447 struct buffer_head *ref_root_bh)
2449 struct inode *xb_alloc_inode;
2450 struct buffer_head *xb_alloc_bh = NULL;
2451 struct buffer_head *blk_bh = NULL;
2452 struct ocfs2_xattr_block *xb;
2453 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2454 handle_t *handle;
2455 int ret = 0;
2456 u64 blk, bg_blkno;
2457 u16 bit;
2459 ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
2460 if (ret < 0) {
2461 mlog_errno(ret);
2462 goto out;
2465 ret = ocfs2_xattr_block_remove(inode, blk_bh, ref_ci, ref_root_bh);
2466 if (ret < 0) {
2467 mlog_errno(ret);
2468 goto out;
2471 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2472 blk = le64_to_cpu(xb->xb_blkno);
2473 bit = le16_to_cpu(xb->xb_suballoc_bit);
2474 if (xb->xb_suballoc_loc)
2475 bg_blkno = le64_to_cpu(xb->xb_suballoc_loc);
2476 else
2477 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2479 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
2480 EXTENT_ALLOC_SYSTEM_INODE,
2481 le16_to_cpu(xb->xb_suballoc_slot));
2482 if (!xb_alloc_inode) {
2483 ret = -ENOMEM;
2484 mlog_errno(ret);
2485 goto out;
2487 mutex_lock(&xb_alloc_inode->i_mutex);
2489 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
2490 if (ret < 0) {
2491 mlog_errno(ret);
2492 goto out_mutex;
2495 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
2496 if (IS_ERR(handle)) {
2497 ret = PTR_ERR(handle);
2498 mlog_errno(ret);
2499 goto out_unlock;
2502 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
2503 bit, bg_blkno, 1);
2504 if (ret < 0)
2505 mlog_errno(ret);
2507 ocfs2_commit_trans(osb, handle);
2508 out_unlock:
2509 ocfs2_inode_unlock(xb_alloc_inode, 1);
2510 brelse(xb_alloc_bh);
2511 out_mutex:
2512 mutex_unlock(&xb_alloc_inode->i_mutex);
2513 iput(xb_alloc_inode);
2514 out:
2515 brelse(blk_bh);
2516 return ret;
2520 * ocfs2_xattr_remove()
2522 * Free extended attribute resources associated with this inode.
2524 int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
2526 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2527 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2528 struct ocfs2_refcount_tree *ref_tree = NULL;
2529 struct buffer_head *ref_root_bh = NULL;
2530 struct ocfs2_caching_info *ref_ci = NULL;
2531 handle_t *handle;
2532 int ret;
2534 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2535 return 0;
2537 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
2538 return 0;
2540 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) {
2541 ret = ocfs2_lock_refcount_tree(OCFS2_SB(inode->i_sb),
2542 le64_to_cpu(di->i_refcount_loc),
2543 1, &ref_tree, &ref_root_bh);
2544 if (ret) {
2545 mlog_errno(ret);
2546 goto out;
2548 ref_ci = &ref_tree->rf_ci;
2552 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2553 ret = ocfs2_xattr_ibody_remove(inode, di_bh,
2554 ref_ci, ref_root_bh);
2555 if (ret < 0) {
2556 mlog_errno(ret);
2557 goto out;
2561 if (di->i_xattr_loc) {
2562 ret = ocfs2_xattr_free_block(inode,
2563 le64_to_cpu(di->i_xattr_loc),
2564 ref_ci, ref_root_bh);
2565 if (ret < 0) {
2566 mlog_errno(ret);
2567 goto out;
2571 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
2572 OCFS2_INODE_UPDATE_CREDITS);
2573 if (IS_ERR(handle)) {
2574 ret = PTR_ERR(handle);
2575 mlog_errno(ret);
2576 goto out;
2578 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
2579 OCFS2_JOURNAL_ACCESS_WRITE);
2580 if (ret) {
2581 mlog_errno(ret);
2582 goto out_commit;
2585 di->i_xattr_loc = 0;
2587 spin_lock(&oi->ip_lock);
2588 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
2589 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2590 spin_unlock(&oi->ip_lock);
2592 ocfs2_journal_dirty(handle, di_bh);
2593 out_commit:
2594 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
2595 out:
2596 if (ref_tree)
2597 ocfs2_unlock_refcount_tree(OCFS2_SB(inode->i_sb), ref_tree, 1);
2598 brelse(ref_root_bh);
2599 return ret;
2602 static int ocfs2_xattr_has_space_inline(struct inode *inode,
2603 struct ocfs2_dinode *di)
2605 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2606 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
2607 int free;
2609 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
2610 return 0;
2612 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2613 struct ocfs2_inline_data *idata = &di->id2.i_data;
2614 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
2615 } else if (ocfs2_inode_is_fast_symlink(inode)) {
2616 free = ocfs2_fast_symlink_chars(inode->i_sb) -
2617 le64_to_cpu(di->i_size);
2618 } else {
2619 struct ocfs2_extent_list *el = &di->id2.i_list;
2620 free = (le16_to_cpu(el->l_count) -
2621 le16_to_cpu(el->l_next_free_rec)) *
2622 sizeof(struct ocfs2_extent_rec);
2624 if (free >= xattrsize)
2625 return 1;
2627 return 0;
2631 * ocfs2_xattr_ibody_find()
2633 * Find extended attribute in inode block and
2634 * fill search info into struct ocfs2_xattr_search.
2636 static int ocfs2_xattr_ibody_find(struct inode *inode,
2637 int name_index,
2638 const char *name,
2639 struct ocfs2_xattr_search *xs)
2641 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2642 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2643 int ret;
2644 int has_space = 0;
2646 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2647 return 0;
2649 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2650 down_read(&oi->ip_alloc_sem);
2651 has_space = ocfs2_xattr_has_space_inline(inode, di);
2652 up_read(&oi->ip_alloc_sem);
2653 if (!has_space)
2654 return 0;
2657 xs->xattr_bh = xs->inode_bh;
2658 xs->end = (void *)di + inode->i_sb->s_blocksize;
2659 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
2660 xs->header = (struct ocfs2_xattr_header *)
2661 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
2662 else
2663 xs->header = (struct ocfs2_xattr_header *)
2664 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
2665 xs->base = (void *)xs->header;
2666 xs->here = xs->header->xh_entries;
2668 /* Find the named attribute. */
2669 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2670 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2671 if (ret && ret != -ENODATA)
2672 return ret;
2673 xs->not_found = ret;
2676 return 0;
2679 static int ocfs2_xattr_ibody_init(struct inode *inode,
2680 struct buffer_head *di_bh,
2681 struct ocfs2_xattr_set_ctxt *ctxt)
2683 int ret;
2684 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2685 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2686 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2687 unsigned int xattrsize = osb->s_xattr_inline_size;
2689 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2690 ret = -ENOSPC;
2691 goto out;
2694 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode), di_bh,
2695 OCFS2_JOURNAL_ACCESS_WRITE);
2696 if (ret) {
2697 mlog_errno(ret);
2698 goto out;
2702 * Adjust extent record count or inline data size
2703 * to reserve space for extended attribute.
2705 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2706 struct ocfs2_inline_data *idata = &di->id2.i_data;
2707 le16_add_cpu(&idata->id_count, -xattrsize);
2708 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
2709 struct ocfs2_extent_list *el = &di->id2.i_list;
2710 le16_add_cpu(&el->l_count, -(xattrsize /
2711 sizeof(struct ocfs2_extent_rec)));
2713 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
2715 spin_lock(&oi->ip_lock);
2716 oi->ip_dyn_features |= OCFS2_INLINE_XATTR_FL|OCFS2_HAS_XATTR_FL;
2717 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2718 spin_unlock(&oi->ip_lock);
2720 ocfs2_journal_dirty(ctxt->handle, di_bh);
2722 out:
2723 return ret;
2727 * ocfs2_xattr_ibody_set()
2729 * Set, replace or remove an extended attribute into inode block.
2732 static int ocfs2_xattr_ibody_set(struct inode *inode,
2733 struct ocfs2_xattr_info *xi,
2734 struct ocfs2_xattr_search *xs,
2735 struct ocfs2_xattr_set_ctxt *ctxt)
2737 int ret;
2738 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2739 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2740 struct ocfs2_xa_loc loc;
2742 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2743 return -ENOSPC;
2745 down_write(&oi->ip_alloc_sem);
2746 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2747 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2748 ret = -ENOSPC;
2749 goto out;
2753 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2754 ret = ocfs2_xattr_ibody_init(inode, xs->inode_bh, ctxt);
2755 if (ret) {
2756 if (ret != -ENOSPC)
2757 mlog_errno(ret);
2758 goto out;
2762 ocfs2_init_dinode_xa_loc(&loc, inode, xs->inode_bh,
2763 xs->not_found ? NULL : xs->here);
2764 ret = ocfs2_xa_set(&loc, xi, ctxt);
2765 if (ret) {
2766 if (ret != -ENOSPC)
2767 mlog_errno(ret);
2768 goto out;
2770 xs->here = loc.xl_entry;
2772 out:
2773 up_write(&oi->ip_alloc_sem);
2775 return ret;
2779 * ocfs2_xattr_block_find()
2781 * Find extended attribute in external block and
2782 * fill search info into struct ocfs2_xattr_search.
2784 static int ocfs2_xattr_block_find(struct inode *inode,
2785 int name_index,
2786 const char *name,
2787 struct ocfs2_xattr_search *xs)
2789 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2790 struct buffer_head *blk_bh = NULL;
2791 struct ocfs2_xattr_block *xb;
2792 int ret = 0;
2794 if (!di->i_xattr_loc)
2795 return ret;
2797 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
2798 &blk_bh);
2799 if (ret < 0) {
2800 mlog_errno(ret);
2801 return ret;
2804 xs->xattr_bh = blk_bh;
2805 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2807 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2808 xs->header = &xb->xb_attrs.xb_header;
2809 xs->base = (void *)xs->header;
2810 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
2811 xs->here = xs->header->xh_entries;
2813 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2814 } else
2815 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
2816 name_index,
2817 name, xs);
2819 if (ret && ret != -ENODATA) {
2820 xs->xattr_bh = NULL;
2821 goto cleanup;
2823 xs->not_found = ret;
2824 return 0;
2825 cleanup:
2826 brelse(blk_bh);
2828 return ret;
2831 static int ocfs2_create_xattr_block(struct inode *inode,
2832 struct buffer_head *inode_bh,
2833 struct ocfs2_xattr_set_ctxt *ctxt,
2834 int indexed,
2835 struct buffer_head **ret_bh)
2837 int ret;
2838 u16 suballoc_bit_start;
2839 u32 num_got;
2840 u64 suballoc_loc, first_blkno;
2841 struct ocfs2_dinode *di = (struct ocfs2_dinode *)inode_bh->b_data;
2842 struct buffer_head *new_bh = NULL;
2843 struct ocfs2_xattr_block *xblk;
2845 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
2846 inode_bh, OCFS2_JOURNAL_ACCESS_CREATE);
2847 if (ret < 0) {
2848 mlog_errno(ret);
2849 goto end;
2852 ret = ocfs2_claim_metadata(ctxt->handle, ctxt->meta_ac, 1,
2853 &suballoc_loc, &suballoc_bit_start,
2854 &num_got, &first_blkno);
2855 if (ret < 0) {
2856 mlog_errno(ret);
2857 goto end;
2860 new_bh = sb_getblk(inode->i_sb, first_blkno);
2861 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
2863 ret = ocfs2_journal_access_xb(ctxt->handle, INODE_CACHE(inode),
2864 new_bh,
2865 OCFS2_JOURNAL_ACCESS_CREATE);
2866 if (ret < 0) {
2867 mlog_errno(ret);
2868 goto end;
2871 /* Initialize ocfs2_xattr_block */
2872 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2873 memset(xblk, 0, inode->i_sb->s_blocksize);
2874 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
2875 xblk->xb_suballoc_slot = cpu_to_le16(ctxt->meta_ac->ac_alloc_slot);
2876 xblk->xb_suballoc_loc = cpu_to_le64(suballoc_loc);
2877 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
2878 xblk->xb_fs_generation =
2879 cpu_to_le32(OCFS2_SB(inode->i_sb)->fs_generation);
2880 xblk->xb_blkno = cpu_to_le64(first_blkno);
2881 if (indexed) {
2882 struct ocfs2_xattr_tree_root *xr = &xblk->xb_attrs.xb_root;
2883 xr->xt_clusters = cpu_to_le32(1);
2884 xr->xt_last_eb_blk = 0;
2885 xr->xt_list.l_tree_depth = 0;
2886 xr->xt_list.l_count = cpu_to_le16(
2887 ocfs2_xattr_recs_per_xb(inode->i_sb));
2888 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2889 xblk->xb_flags = cpu_to_le16(OCFS2_XATTR_INDEXED);
2891 ocfs2_journal_dirty(ctxt->handle, new_bh);
2893 /* Add it to the inode */
2894 di->i_xattr_loc = cpu_to_le64(first_blkno);
2896 spin_lock(&OCFS2_I(inode)->ip_lock);
2897 OCFS2_I(inode)->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
2898 di->i_dyn_features = cpu_to_le16(OCFS2_I(inode)->ip_dyn_features);
2899 spin_unlock(&OCFS2_I(inode)->ip_lock);
2901 ocfs2_journal_dirty(ctxt->handle, inode_bh);
2903 *ret_bh = new_bh;
2904 new_bh = NULL;
2906 end:
2907 brelse(new_bh);
2908 return ret;
2912 * ocfs2_xattr_block_set()
2914 * Set, replace or remove an extended attribute into external block.
2917 static int ocfs2_xattr_block_set(struct inode *inode,
2918 struct ocfs2_xattr_info *xi,
2919 struct ocfs2_xattr_search *xs,
2920 struct ocfs2_xattr_set_ctxt *ctxt)
2922 struct buffer_head *new_bh = NULL;
2923 struct ocfs2_xattr_block *xblk = NULL;
2924 int ret;
2925 struct ocfs2_xa_loc loc;
2927 if (!xs->xattr_bh) {
2928 ret = ocfs2_create_xattr_block(inode, xs->inode_bh, ctxt,
2929 0, &new_bh);
2930 if (ret) {
2931 mlog_errno(ret);
2932 goto end;
2935 xs->xattr_bh = new_bh;
2936 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2937 xs->header = &xblk->xb_attrs.xb_header;
2938 xs->base = (void *)xs->header;
2939 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2940 xs->here = xs->header->xh_entries;
2941 } else
2942 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2944 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
2945 ocfs2_init_xattr_block_xa_loc(&loc, inode, xs->xattr_bh,
2946 xs->not_found ? NULL : xs->here);
2948 ret = ocfs2_xa_set(&loc, xi, ctxt);
2949 if (!ret)
2950 xs->here = loc.xl_entry;
2951 else if ((ret != -ENOSPC) || ctxt->set_abort)
2952 goto end;
2953 else {
2954 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
2955 if (ret)
2956 goto end;
2960 if (le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)
2961 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
2963 end:
2964 return ret;
2967 /* Check whether the new xattr can be inserted into the inode. */
2968 static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
2969 struct ocfs2_xattr_info *xi,
2970 struct ocfs2_xattr_search *xs)
2972 struct ocfs2_xattr_entry *last;
2973 int free, i;
2974 size_t min_offs = xs->end - xs->base;
2976 if (!xs->header)
2977 return 0;
2979 last = xs->header->xh_entries;
2981 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
2982 size_t offs = le16_to_cpu(last->xe_name_offset);
2983 if (offs < min_offs)
2984 min_offs = offs;
2985 last += 1;
2988 free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
2989 if (free < 0)
2990 return 0;
2992 BUG_ON(!xs->not_found);
2994 if (free >= (sizeof(struct ocfs2_xattr_entry) + namevalue_size_xi(xi)))
2995 return 1;
2997 return 0;
3000 static int ocfs2_calc_xattr_set_need(struct inode *inode,
3001 struct ocfs2_dinode *di,
3002 struct ocfs2_xattr_info *xi,
3003 struct ocfs2_xattr_search *xis,
3004 struct ocfs2_xattr_search *xbs,
3005 int *clusters_need,
3006 int *meta_need,
3007 int *credits_need)
3009 int ret = 0, old_in_xb = 0;
3010 int clusters_add = 0, meta_add = 0, credits = 0;
3011 struct buffer_head *bh = NULL;
3012 struct ocfs2_xattr_block *xb = NULL;
3013 struct ocfs2_xattr_entry *xe = NULL;
3014 struct ocfs2_xattr_value_root *xv = NULL;
3015 char *base = NULL;
3016 int name_offset, name_len = 0;
3017 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
3018 xi->xi_value_len);
3019 u64 value_size;
3022 * Calculate the clusters we need to write.
3023 * No matter whether we replace an old one or add a new one,
3024 * we need this for writing.
3026 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
3027 credits += new_clusters *
3028 ocfs2_clusters_to_blocks(inode->i_sb, 1);
3030 if (xis->not_found && xbs->not_found) {
3031 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3033 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
3034 clusters_add += new_clusters;
3035 credits += ocfs2_calc_extend_credits(inode->i_sb,
3036 &def_xv.xv.xr_list,
3037 new_clusters);
3040 goto meta_guess;
3043 if (!xis->not_found) {
3044 xe = xis->here;
3045 name_offset = le16_to_cpu(xe->xe_name_offset);
3046 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3047 base = xis->base;
3048 credits += OCFS2_INODE_UPDATE_CREDITS;
3049 } else {
3050 int i, block_off = 0;
3051 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
3052 xe = xbs->here;
3053 name_offset = le16_to_cpu(xe->xe_name_offset);
3054 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3055 i = xbs->here - xbs->header->xh_entries;
3056 old_in_xb = 1;
3058 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
3059 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
3060 bucket_xh(xbs->bucket),
3061 i, &block_off,
3062 &name_offset);
3063 base = bucket_block(xbs->bucket, block_off);
3064 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3065 } else {
3066 base = xbs->base;
3067 credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
3072 * delete a xattr doesn't need metadata and cluster allocation.
3073 * so just calculate the credits and return.
3075 * The credits for removing the value tree will be extended
3076 * by ocfs2_remove_extent itself.
3078 if (!xi->xi_value) {
3079 if (!ocfs2_xattr_is_local(xe))
3080 credits += ocfs2_remove_extent_credits(inode->i_sb);
3082 goto out;
3085 /* do cluster allocation guess first. */
3086 value_size = le64_to_cpu(xe->xe_value_size);
3088 if (old_in_xb) {
3090 * In xattr set, we always try to set the xe in inode first,
3091 * so if it can be inserted into inode successfully, the old
3092 * one will be removed from the xattr block, and this xattr
3093 * will be inserted into inode as a new xattr in inode.
3095 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
3096 clusters_add += new_clusters;
3097 credits += ocfs2_remove_extent_credits(inode->i_sb) +
3098 OCFS2_INODE_UPDATE_CREDITS;
3099 if (!ocfs2_xattr_is_local(xe))
3100 credits += ocfs2_calc_extend_credits(
3101 inode->i_sb,
3102 &def_xv.xv.xr_list,
3103 new_clusters);
3104 goto out;
3108 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
3109 /* the new values will be stored outside. */
3110 u32 old_clusters = 0;
3112 if (!ocfs2_xattr_is_local(xe)) {
3113 old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
3114 value_size);
3115 xv = (struct ocfs2_xattr_value_root *)
3116 (base + name_offset + name_len);
3117 value_size = OCFS2_XATTR_ROOT_SIZE;
3118 } else
3119 xv = &def_xv.xv;
3121 if (old_clusters >= new_clusters) {
3122 credits += ocfs2_remove_extent_credits(inode->i_sb);
3123 goto out;
3124 } else {
3125 meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
3126 clusters_add += new_clusters - old_clusters;
3127 credits += ocfs2_calc_extend_credits(inode->i_sb,
3128 &xv->xr_list,
3129 new_clusters -
3130 old_clusters);
3131 if (value_size >= OCFS2_XATTR_ROOT_SIZE)
3132 goto out;
3134 } else {
3136 * Now the new value will be stored inside. So if the new
3137 * value is smaller than the size of value root or the old
3138 * value, we don't need any allocation, otherwise we have
3139 * to guess metadata allocation.
3141 if ((ocfs2_xattr_is_local(xe) &&
3142 (value_size >= xi->xi_value_len)) ||
3143 (!ocfs2_xattr_is_local(xe) &&
3144 OCFS2_XATTR_ROOT_SIZE >= xi->xi_value_len))
3145 goto out;
3148 meta_guess:
3149 /* calculate metadata allocation. */
3150 if (di->i_xattr_loc) {
3151 if (!xbs->xattr_bh) {
3152 ret = ocfs2_read_xattr_block(inode,
3153 le64_to_cpu(di->i_xattr_loc),
3154 &bh);
3155 if (ret) {
3156 mlog_errno(ret);
3157 goto out;
3160 xb = (struct ocfs2_xattr_block *)bh->b_data;
3161 } else
3162 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
3165 * If there is already an xattr tree, good, we can calculate
3166 * like other b-trees. Otherwise we may have the chance of
3167 * create a tree, the credit calculation is borrowed from
3168 * ocfs2_calc_extend_credits with root_el = NULL. And the
3169 * new tree will be cluster based, so no meta is needed.
3171 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
3172 struct ocfs2_extent_list *el =
3173 &xb->xb_attrs.xb_root.xt_list;
3174 meta_add += ocfs2_extend_meta_needed(el);
3175 credits += ocfs2_calc_extend_credits(inode->i_sb,
3176 el, 1);
3177 } else
3178 credits += OCFS2_SUBALLOC_ALLOC + 1;
3181 * This cluster will be used either for new bucket or for
3182 * new xattr block.
3183 * If the cluster size is the same as the bucket size, one
3184 * more is needed since we may need to extend the bucket
3185 * also.
3187 clusters_add += 1;
3188 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3189 if (OCFS2_XATTR_BUCKET_SIZE ==
3190 OCFS2_SB(inode->i_sb)->s_clustersize) {
3191 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3192 clusters_add += 1;
3194 } else {
3195 meta_add += 1;
3196 credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
3198 out:
3199 if (clusters_need)
3200 *clusters_need = clusters_add;
3201 if (meta_need)
3202 *meta_need = meta_add;
3203 if (credits_need)
3204 *credits_need = credits;
3205 brelse(bh);
3206 return ret;
3209 static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
3210 struct ocfs2_dinode *di,
3211 struct ocfs2_xattr_info *xi,
3212 struct ocfs2_xattr_search *xis,
3213 struct ocfs2_xattr_search *xbs,
3214 struct ocfs2_xattr_set_ctxt *ctxt,
3215 int extra_meta,
3216 int *credits)
3218 int clusters_add, meta_add, ret;
3219 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3221 memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
3223 ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
3225 ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
3226 &clusters_add, &meta_add, credits);
3227 if (ret) {
3228 mlog_errno(ret);
3229 return ret;
3232 meta_add += extra_meta;
3233 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
3234 "credits = %d\n", xi->xi_name, meta_add, clusters_add, *credits);
3236 if (meta_add) {
3237 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
3238 &ctxt->meta_ac);
3239 if (ret) {
3240 mlog_errno(ret);
3241 goto out;
3245 if (clusters_add) {
3246 ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
3247 if (ret)
3248 mlog_errno(ret);
3250 out:
3251 if (ret) {
3252 if (ctxt->meta_ac) {
3253 ocfs2_free_alloc_context(ctxt->meta_ac);
3254 ctxt->meta_ac = NULL;
3258 * We cannot have an error and a non null ctxt->data_ac.
3262 return ret;
3265 static int __ocfs2_xattr_set_handle(struct inode *inode,
3266 struct ocfs2_dinode *di,
3267 struct ocfs2_xattr_info *xi,
3268 struct ocfs2_xattr_search *xis,
3269 struct ocfs2_xattr_search *xbs,
3270 struct ocfs2_xattr_set_ctxt *ctxt)
3272 int ret = 0, credits, old_found;
3274 if (!xi->xi_value) {
3275 /* Remove existing extended attribute */
3276 if (!xis->not_found)
3277 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3278 else if (!xbs->not_found)
3279 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3280 } else {
3281 /* We always try to set extended attribute into inode first*/
3282 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3283 if (!ret && !xbs->not_found) {
3285 * If succeed and that extended attribute existing in
3286 * external block, then we will remove it.
3288 xi->xi_value = NULL;
3289 xi->xi_value_len = 0;
3291 old_found = xis->not_found;
3292 xis->not_found = -ENODATA;
3293 ret = ocfs2_calc_xattr_set_need(inode,
3296 xis,
3297 xbs,
3298 NULL,
3299 NULL,
3300 &credits);
3301 xis->not_found = old_found;
3302 if (ret) {
3303 mlog_errno(ret);
3304 goto out;
3307 ret = ocfs2_extend_trans(ctxt->handle, credits);
3308 if (ret) {
3309 mlog_errno(ret);
3310 goto out;
3312 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3313 } else if ((ret == -ENOSPC) && !ctxt->set_abort) {
3314 if (di->i_xattr_loc && !xbs->xattr_bh) {
3315 ret = ocfs2_xattr_block_find(inode,
3316 xi->xi_name_index,
3317 xi->xi_name, xbs);
3318 if (ret)
3319 goto out;
3321 old_found = xis->not_found;
3322 xis->not_found = -ENODATA;
3323 ret = ocfs2_calc_xattr_set_need(inode,
3326 xis,
3327 xbs,
3328 NULL,
3329 NULL,
3330 &credits);
3331 xis->not_found = old_found;
3332 if (ret) {
3333 mlog_errno(ret);
3334 goto out;
3337 ret = ocfs2_extend_trans(ctxt->handle, credits);
3338 if (ret) {
3339 mlog_errno(ret);
3340 goto out;
3344 * If no space in inode, we will set extended attribute
3345 * into external block.
3347 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3348 if (ret)
3349 goto out;
3350 if (!xis->not_found) {
3352 * If succeed and that extended attribute
3353 * existing in inode, we will remove it.
3355 xi->xi_value = NULL;
3356 xi->xi_value_len = 0;
3357 xbs->not_found = -ENODATA;
3358 ret = ocfs2_calc_xattr_set_need(inode,
3361 xis,
3362 xbs,
3363 NULL,
3364 NULL,
3365 &credits);
3366 if (ret) {
3367 mlog_errno(ret);
3368 goto out;
3371 ret = ocfs2_extend_trans(ctxt->handle, credits);
3372 if (ret) {
3373 mlog_errno(ret);
3374 goto out;
3376 ret = ocfs2_xattr_ibody_set(inode, xi,
3377 xis, ctxt);
3382 if (!ret) {
3383 /* Update inode ctime. */
3384 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
3385 xis->inode_bh,
3386 OCFS2_JOURNAL_ACCESS_WRITE);
3387 if (ret) {
3388 mlog_errno(ret);
3389 goto out;
3392 inode->i_ctime = CURRENT_TIME;
3393 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
3394 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
3395 ocfs2_journal_dirty(ctxt->handle, xis->inode_bh);
3397 out:
3398 return ret;
3402 * This function only called duing creating inode
3403 * for init security/acl xattrs of the new inode.
3404 * All transanction credits have been reserved in mknod.
3406 int ocfs2_xattr_set_handle(handle_t *handle,
3407 struct inode *inode,
3408 struct buffer_head *di_bh,
3409 int name_index,
3410 const char *name,
3411 const void *value,
3412 size_t value_len,
3413 int flags,
3414 struct ocfs2_alloc_context *meta_ac,
3415 struct ocfs2_alloc_context *data_ac)
3417 struct ocfs2_dinode *di;
3418 int ret;
3420 struct ocfs2_xattr_info xi = {
3421 .xi_name_index = name_index,
3422 .xi_name = name,
3423 .xi_name_len = strlen(name),
3424 .xi_value = value,
3425 .xi_value_len = value_len,
3428 struct ocfs2_xattr_search xis = {
3429 .not_found = -ENODATA,
3432 struct ocfs2_xattr_search xbs = {
3433 .not_found = -ENODATA,
3436 struct ocfs2_xattr_set_ctxt ctxt = {
3437 .handle = handle,
3438 .meta_ac = meta_ac,
3439 .data_ac = data_ac,
3442 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3443 return -EOPNOTSUPP;
3446 * In extreme situation, may need xattr bucket when
3447 * block size is too small. And we have already reserved
3448 * the credits for bucket in mknod.
3450 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) {
3451 xbs.bucket = ocfs2_xattr_bucket_new(inode);
3452 if (!xbs.bucket) {
3453 mlog_errno(-ENOMEM);
3454 return -ENOMEM;
3458 xis.inode_bh = xbs.inode_bh = di_bh;
3459 di = (struct ocfs2_dinode *)di_bh->b_data;
3461 down_write(&OCFS2_I(inode)->ip_xattr_sem);
3463 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3464 if (ret)
3465 goto cleanup;
3466 if (xis.not_found) {
3467 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3468 if (ret)
3469 goto cleanup;
3472 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3474 cleanup:
3475 up_write(&OCFS2_I(inode)->ip_xattr_sem);
3476 brelse(xbs.xattr_bh);
3477 ocfs2_xattr_bucket_free(xbs.bucket);
3479 return ret;
3483 * ocfs2_xattr_set()
3485 * Set, replace or remove an extended attribute for this inode.
3486 * value is NULL to remove an existing extended attribute, else either
3487 * create or replace an extended attribute.
3489 int ocfs2_xattr_set(struct inode *inode,
3490 int name_index,
3491 const char *name,
3492 const void *value,
3493 size_t value_len,
3494 int flags)
3496 struct buffer_head *di_bh = NULL;
3497 struct ocfs2_dinode *di;
3498 int ret, credits, ref_meta = 0, ref_credits = 0;
3499 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3500 struct inode *tl_inode = osb->osb_tl_inode;
3501 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
3502 struct ocfs2_refcount_tree *ref_tree = NULL;
3504 struct ocfs2_xattr_info xi = {
3505 .xi_name_index = name_index,
3506 .xi_name = name,
3507 .xi_name_len = strlen(name),
3508 .xi_value = value,
3509 .xi_value_len = value_len,
3512 struct ocfs2_xattr_search xis = {
3513 .not_found = -ENODATA,
3516 struct ocfs2_xattr_search xbs = {
3517 .not_found = -ENODATA,
3520 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3521 return -EOPNOTSUPP;
3524 * Only xbs will be used on indexed trees. xis doesn't need a
3525 * bucket.
3527 xbs.bucket = ocfs2_xattr_bucket_new(inode);
3528 if (!xbs.bucket) {
3529 mlog_errno(-ENOMEM);
3530 return -ENOMEM;
3533 ret = ocfs2_inode_lock(inode, &di_bh, 1);
3534 if (ret < 0) {
3535 mlog_errno(ret);
3536 goto cleanup_nolock;
3538 xis.inode_bh = xbs.inode_bh = di_bh;
3539 di = (struct ocfs2_dinode *)di_bh->b_data;
3541 down_write(&OCFS2_I(inode)->ip_xattr_sem);
3543 * Scan inode and external block to find the same name
3544 * extended attribute and collect search infomation.
3546 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3547 if (ret)
3548 goto cleanup;
3549 if (xis.not_found) {
3550 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3551 if (ret)
3552 goto cleanup;
3555 if (xis.not_found && xbs.not_found) {
3556 ret = -ENODATA;
3557 if (flags & XATTR_REPLACE)
3558 goto cleanup;
3559 ret = 0;
3560 if (!value)
3561 goto cleanup;
3562 } else {
3563 ret = -EEXIST;
3564 if (flags & XATTR_CREATE)
3565 goto cleanup;
3568 /* Check whether the value is refcounted and do some prepartion. */
3569 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL &&
3570 (!xis.not_found || !xbs.not_found)) {
3571 ret = ocfs2_prepare_refcount_xattr(inode, di, &xi,
3572 &xis, &xbs, &ref_tree,
3573 &ref_meta, &ref_credits);
3574 if (ret) {
3575 mlog_errno(ret);
3576 goto cleanup;
3580 mutex_lock(&tl_inode->i_mutex);
3582 if (ocfs2_truncate_log_needs_flush(osb)) {
3583 ret = __ocfs2_flush_truncate_log(osb);
3584 if (ret < 0) {
3585 mutex_unlock(&tl_inode->i_mutex);
3586 mlog_errno(ret);
3587 goto cleanup;
3590 mutex_unlock(&tl_inode->i_mutex);
3592 ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
3593 &xbs, &ctxt, ref_meta, &credits);
3594 if (ret) {
3595 mlog_errno(ret);
3596 goto cleanup;
3599 /* we need to update inode's ctime field, so add credit for it. */
3600 credits += OCFS2_INODE_UPDATE_CREDITS;
3601 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
3602 if (IS_ERR(ctxt.handle)) {
3603 ret = PTR_ERR(ctxt.handle);
3604 mlog_errno(ret);
3605 goto cleanup;
3608 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3610 ocfs2_commit_trans(osb, ctxt.handle);
3612 if (ctxt.data_ac)
3613 ocfs2_free_alloc_context(ctxt.data_ac);
3614 if (ctxt.meta_ac)
3615 ocfs2_free_alloc_context(ctxt.meta_ac);
3616 if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
3617 ocfs2_schedule_truncate_log_flush(osb, 1);
3618 ocfs2_run_deallocs(osb, &ctxt.dealloc);
3620 cleanup:
3621 if (ref_tree)
3622 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
3623 up_write(&OCFS2_I(inode)->ip_xattr_sem);
3624 if (!value && !ret) {
3625 ret = ocfs2_try_remove_refcount_tree(inode, di_bh);
3626 if (ret)
3627 mlog_errno(ret);
3629 ocfs2_inode_unlock(inode, 1);
3630 cleanup_nolock:
3631 brelse(di_bh);
3632 brelse(xbs.xattr_bh);
3633 ocfs2_xattr_bucket_free(xbs.bucket);
3635 return ret;
3639 * Find the xattr extent rec which may contains name_hash.
3640 * e_cpos will be the first name hash of the xattr rec.
3641 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
3643 static int ocfs2_xattr_get_rec(struct inode *inode,
3644 u32 name_hash,
3645 u64 *p_blkno,
3646 u32 *e_cpos,
3647 u32 *num_clusters,
3648 struct ocfs2_extent_list *el)
3650 int ret = 0, i;
3651 struct buffer_head *eb_bh = NULL;
3652 struct ocfs2_extent_block *eb;
3653 struct ocfs2_extent_rec *rec = NULL;
3654 u64 e_blkno = 0;
3656 if (el->l_tree_depth) {
3657 ret = ocfs2_find_leaf(INODE_CACHE(inode), el, name_hash,
3658 &eb_bh);
3659 if (ret) {
3660 mlog_errno(ret);
3661 goto out;
3664 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
3665 el = &eb->h_list;
3667 if (el->l_tree_depth) {
3668 ocfs2_error(inode->i_sb,
3669 "Inode %lu has non zero tree depth in "
3670 "xattr tree block %llu\n", inode->i_ino,
3671 (unsigned long long)eb_bh->b_blocknr);
3672 ret = -EROFS;
3673 goto out;
3677 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
3678 rec = &el->l_recs[i];
3680 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
3681 e_blkno = le64_to_cpu(rec->e_blkno);
3682 break;
3686 if (!e_blkno) {
3687 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
3688 "record (%u, %u, 0) in xattr", inode->i_ino,
3689 le32_to_cpu(rec->e_cpos),
3690 ocfs2_rec_clusters(el, rec));
3691 ret = -EROFS;
3692 goto out;
3695 *p_blkno = le64_to_cpu(rec->e_blkno);
3696 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
3697 if (e_cpos)
3698 *e_cpos = le32_to_cpu(rec->e_cpos);
3699 out:
3700 brelse(eb_bh);
3701 return ret;
3704 typedef int (xattr_bucket_func)(struct inode *inode,
3705 struct ocfs2_xattr_bucket *bucket,
3706 void *para);
3708 static int ocfs2_find_xe_in_bucket(struct inode *inode,
3709 struct ocfs2_xattr_bucket *bucket,
3710 int name_index,
3711 const char *name,
3712 u32 name_hash,
3713 u16 *xe_index,
3714 int *found)
3716 int i, ret = 0, cmp = 1, block_off, new_offset;
3717 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
3718 size_t name_len = strlen(name);
3719 struct ocfs2_xattr_entry *xe = NULL;
3720 char *xe_name;
3723 * We don't use binary search in the bucket because there
3724 * may be multiple entries with the same name hash.
3726 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3727 xe = &xh->xh_entries[i];
3729 if (name_hash > le32_to_cpu(xe->xe_name_hash))
3730 continue;
3731 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
3732 break;
3734 cmp = name_index - ocfs2_xattr_get_type(xe);
3735 if (!cmp)
3736 cmp = name_len - xe->xe_name_len;
3737 if (cmp)
3738 continue;
3740 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
3743 &block_off,
3744 &new_offset);
3745 if (ret) {
3746 mlog_errno(ret);
3747 break;
3751 xe_name = bucket_block(bucket, block_off) + new_offset;
3752 if (!memcmp(name, xe_name, name_len)) {
3753 *xe_index = i;
3754 *found = 1;
3755 ret = 0;
3756 break;
3760 return ret;
3764 * Find the specified xattr entry in a series of buckets.
3765 * This series start from p_blkno and last for num_clusters.
3766 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
3767 * the num of the valid buckets.
3769 * Return the buffer_head this xattr should reside in. And if the xattr's
3770 * hash is in the gap of 2 buckets, return the lower bucket.
3772 static int ocfs2_xattr_bucket_find(struct inode *inode,
3773 int name_index,
3774 const char *name,
3775 u32 name_hash,
3776 u64 p_blkno,
3777 u32 first_hash,
3778 u32 num_clusters,
3779 struct ocfs2_xattr_search *xs)
3781 int ret, found = 0;
3782 struct ocfs2_xattr_header *xh = NULL;
3783 struct ocfs2_xattr_entry *xe = NULL;
3784 u16 index = 0;
3785 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3786 int low_bucket = 0, bucket, high_bucket;
3787 struct ocfs2_xattr_bucket *search;
3788 u32 last_hash;
3789 u64 blkno, lower_blkno = 0;
3791 search = ocfs2_xattr_bucket_new(inode);
3792 if (!search) {
3793 ret = -ENOMEM;
3794 mlog_errno(ret);
3795 goto out;
3798 ret = ocfs2_read_xattr_bucket(search, p_blkno);
3799 if (ret) {
3800 mlog_errno(ret);
3801 goto out;
3804 xh = bucket_xh(search);
3805 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
3806 while (low_bucket <= high_bucket) {
3807 ocfs2_xattr_bucket_relse(search);
3809 bucket = (low_bucket + high_bucket) / 2;
3810 blkno = p_blkno + bucket * blk_per_bucket;
3811 ret = ocfs2_read_xattr_bucket(search, blkno);
3812 if (ret) {
3813 mlog_errno(ret);
3814 goto out;
3817 xh = bucket_xh(search);
3818 xe = &xh->xh_entries[0];
3819 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
3820 high_bucket = bucket - 1;
3821 continue;
3825 * Check whether the hash of the last entry in our
3826 * bucket is larger than the search one. for an empty
3827 * bucket, the last one is also the first one.
3829 if (xh->xh_count)
3830 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
3832 last_hash = le32_to_cpu(xe->xe_name_hash);
3834 /* record lower_blkno which may be the insert place. */
3835 lower_blkno = blkno;
3837 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
3838 low_bucket = bucket + 1;
3839 continue;
3842 /* the searched xattr should reside in this bucket if exists. */
3843 ret = ocfs2_find_xe_in_bucket(inode, search,
3844 name_index, name, name_hash,
3845 &index, &found);
3846 if (ret) {
3847 mlog_errno(ret);
3848 goto out;
3850 break;
3854 * Record the bucket we have found.
3855 * When the xattr's hash value is in the gap of 2 buckets, we will
3856 * always set it to the previous bucket.
3858 if (!lower_blkno)
3859 lower_blkno = p_blkno;
3861 /* This should be in cache - we just read it during the search */
3862 ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
3863 if (ret) {
3864 mlog_errno(ret);
3865 goto out;
3868 xs->header = bucket_xh(xs->bucket);
3869 xs->base = bucket_block(xs->bucket, 0);
3870 xs->end = xs->base + inode->i_sb->s_blocksize;
3872 if (found) {
3873 xs->here = &xs->header->xh_entries[index];
3874 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
3875 (unsigned long long)bucket_blkno(xs->bucket), index);
3876 } else
3877 ret = -ENODATA;
3879 out:
3880 ocfs2_xattr_bucket_free(search);
3881 return ret;
3884 static int ocfs2_xattr_index_block_find(struct inode *inode,
3885 struct buffer_head *root_bh,
3886 int name_index,
3887 const char *name,
3888 struct ocfs2_xattr_search *xs)
3890 int ret;
3891 struct ocfs2_xattr_block *xb =
3892 (struct ocfs2_xattr_block *)root_bh->b_data;
3893 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3894 struct ocfs2_extent_list *el = &xb_root->xt_list;
3895 u64 p_blkno = 0;
3896 u32 first_hash, num_clusters = 0;
3897 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
3899 if (le16_to_cpu(el->l_next_free_rec) == 0)
3900 return -ENODATA;
3902 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
3903 name, name_hash, name_index);
3905 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
3906 &num_clusters, el);
3907 if (ret) {
3908 mlog_errno(ret);
3909 goto out;
3912 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
3914 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
3915 "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
3916 first_hash);
3918 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
3919 p_blkno, first_hash, num_clusters, xs);
3921 out:
3922 return ret;
3925 static int ocfs2_iterate_xattr_buckets(struct inode *inode,
3926 u64 blkno,
3927 u32 clusters,
3928 xattr_bucket_func *func,
3929 void *para)
3931 int i, ret = 0;
3932 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
3933 u32 num_buckets = clusters * bpc;
3934 struct ocfs2_xattr_bucket *bucket;
3936 bucket = ocfs2_xattr_bucket_new(inode);
3937 if (!bucket) {
3938 mlog_errno(-ENOMEM);
3939 return -ENOMEM;
3942 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
3943 clusters, (unsigned long long)blkno);
3945 for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
3946 ret = ocfs2_read_xattr_bucket(bucket, blkno);
3947 if (ret) {
3948 mlog_errno(ret);
3949 break;
3953 * The real bucket num in this series of blocks is stored
3954 * in the 1st bucket.
3956 if (i == 0)
3957 num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
3959 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
3960 (unsigned long long)blkno,
3961 le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
3962 if (func) {
3963 ret = func(inode, bucket, para);
3964 if (ret && ret != -ERANGE)
3965 mlog_errno(ret);
3966 /* Fall through to bucket_relse() */
3969 ocfs2_xattr_bucket_relse(bucket);
3970 if (ret)
3971 break;
3974 ocfs2_xattr_bucket_free(bucket);
3975 return ret;
3978 struct ocfs2_xattr_tree_list {
3979 char *buffer;
3980 size_t buffer_size;
3981 size_t result;
3984 static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
3985 struct ocfs2_xattr_header *xh,
3986 int index,
3987 int *block_off,
3988 int *new_offset)
3990 u16 name_offset;
3992 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
3993 return -EINVAL;
3995 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
3997 *block_off = name_offset >> sb->s_blocksize_bits;
3998 *new_offset = name_offset % sb->s_blocksize;
4000 return 0;
4003 static int ocfs2_list_xattr_bucket(struct inode *inode,
4004 struct ocfs2_xattr_bucket *bucket,
4005 void *para)
4007 int ret = 0, type;
4008 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
4009 int i, block_off, new_offset;
4010 const char *prefix, *name;
4012 for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
4013 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
4014 type = ocfs2_xattr_get_type(entry);
4015 prefix = ocfs2_xattr_prefix(type);
4017 if (prefix) {
4018 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
4019 bucket_xh(bucket),
4021 &block_off,
4022 &new_offset);
4023 if (ret)
4024 break;
4026 name = (const char *)bucket_block(bucket, block_off) +
4027 new_offset;
4028 ret = ocfs2_xattr_list_entry(xl->buffer,
4029 xl->buffer_size,
4030 &xl->result,
4031 prefix, name,
4032 entry->xe_name_len);
4033 if (ret)
4034 break;
4038 return ret;
4041 static int ocfs2_iterate_xattr_index_block(struct inode *inode,
4042 struct buffer_head *blk_bh,
4043 xattr_tree_rec_func *rec_func,
4044 void *para)
4046 struct ocfs2_xattr_block *xb =
4047 (struct ocfs2_xattr_block *)blk_bh->b_data;
4048 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
4049 int ret = 0;
4050 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
4051 u64 p_blkno = 0;
4053 if (!el->l_next_free_rec || !rec_func)
4054 return 0;
4056 while (name_hash > 0) {
4057 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
4058 &e_cpos, &num_clusters, el);
4059 if (ret) {
4060 mlog_errno(ret);
4061 break;
4064 ret = rec_func(inode, blk_bh, p_blkno, e_cpos,
4065 num_clusters, para);
4066 if (ret) {
4067 if (ret != -ERANGE)
4068 mlog_errno(ret);
4069 break;
4072 if (e_cpos == 0)
4073 break;
4075 name_hash = e_cpos - 1;
4078 return ret;
4082 static int ocfs2_list_xattr_tree_rec(struct inode *inode,
4083 struct buffer_head *root_bh,
4084 u64 blkno, u32 cpos, u32 len, void *para)
4086 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
4087 ocfs2_list_xattr_bucket, para);
4090 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
4091 struct buffer_head *blk_bh,
4092 char *buffer,
4093 size_t buffer_size)
4095 int ret;
4096 struct ocfs2_xattr_tree_list xl = {
4097 .buffer = buffer,
4098 .buffer_size = buffer_size,
4099 .result = 0,
4102 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
4103 ocfs2_list_xattr_tree_rec, &xl);
4104 if (ret) {
4105 mlog_errno(ret);
4106 goto out;
4109 ret = xl.result;
4110 out:
4111 return ret;
4114 static int cmp_xe(const void *a, const void *b)
4116 const struct ocfs2_xattr_entry *l = a, *r = b;
4117 u32 l_hash = le32_to_cpu(l->xe_name_hash);
4118 u32 r_hash = le32_to_cpu(r->xe_name_hash);
4120 if (l_hash > r_hash)
4121 return 1;
4122 if (l_hash < r_hash)
4123 return -1;
4124 return 0;
4127 static void swap_xe(void *a, void *b, int size)
4129 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
4131 tmp = *l;
4132 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
4133 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
4137 * When the ocfs2_xattr_block is filled up, new bucket will be created
4138 * and all the xattr entries will be moved to the new bucket.
4139 * The header goes at the start of the bucket, and the names+values are
4140 * filled from the end. This is why *target starts as the last buffer.
4141 * Note: we need to sort the entries since they are not saved in order
4142 * in the ocfs2_xattr_block.
4144 static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
4145 struct buffer_head *xb_bh,
4146 struct ocfs2_xattr_bucket *bucket)
4148 int i, blocksize = inode->i_sb->s_blocksize;
4149 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4150 u16 offset, size, off_change;
4151 struct ocfs2_xattr_entry *xe;
4152 struct ocfs2_xattr_block *xb =
4153 (struct ocfs2_xattr_block *)xb_bh->b_data;
4154 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
4155 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
4156 u16 count = le16_to_cpu(xb_xh->xh_count);
4157 char *src = xb_bh->b_data;
4158 char *target = bucket_block(bucket, blks - 1);
4160 mlog(0, "cp xattr from block %llu to bucket %llu\n",
4161 (unsigned long long)xb_bh->b_blocknr,
4162 (unsigned long long)bucket_blkno(bucket));
4164 for (i = 0; i < blks; i++)
4165 memset(bucket_block(bucket, i), 0, blocksize);
4168 * Since the xe_name_offset is based on ocfs2_xattr_header,
4169 * there is a offset change corresponding to the change of
4170 * ocfs2_xattr_header's position.
4172 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
4173 xe = &xb_xh->xh_entries[count - 1];
4174 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
4175 size = blocksize - offset;
4177 /* copy all the names and values. */
4178 memcpy(target + offset, src + offset, size);
4180 /* Init new header now. */
4181 xh->xh_count = xb_xh->xh_count;
4182 xh->xh_num_buckets = cpu_to_le16(1);
4183 xh->xh_name_value_len = cpu_to_le16(size);
4184 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
4186 /* copy all the entries. */
4187 target = bucket_block(bucket, 0);
4188 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
4189 size = count * sizeof(struct ocfs2_xattr_entry);
4190 memcpy(target + offset, (char *)xb_xh + offset, size);
4192 /* Change the xe offset for all the xe because of the move. */
4193 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
4194 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
4195 for (i = 0; i < count; i++)
4196 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
4198 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
4199 offset, size, off_change);
4201 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
4202 cmp_xe, swap_xe);
4206 * After we move xattr from block to index btree, we have to
4207 * update ocfs2_xattr_search to the new xe and base.
4209 * When the entry is in xattr block, xattr_bh indicates the storage place.
4210 * While if the entry is in index b-tree, "bucket" indicates the
4211 * real place of the xattr.
4213 static void ocfs2_xattr_update_xattr_search(struct inode *inode,
4214 struct ocfs2_xattr_search *xs,
4215 struct buffer_head *old_bh)
4217 char *buf = old_bh->b_data;
4218 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
4219 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
4220 int i;
4222 xs->header = bucket_xh(xs->bucket);
4223 xs->base = bucket_block(xs->bucket, 0);
4224 xs->end = xs->base + inode->i_sb->s_blocksize;
4226 if (xs->not_found)
4227 return;
4229 i = xs->here - old_xh->xh_entries;
4230 xs->here = &xs->header->xh_entries[i];
4233 static int ocfs2_xattr_create_index_block(struct inode *inode,
4234 struct ocfs2_xattr_search *xs,
4235 struct ocfs2_xattr_set_ctxt *ctxt)
4237 int ret;
4238 u32 bit_off, len;
4239 u64 blkno;
4240 handle_t *handle = ctxt->handle;
4241 struct ocfs2_inode_info *oi = OCFS2_I(inode);
4242 struct buffer_head *xb_bh = xs->xattr_bh;
4243 struct ocfs2_xattr_block *xb =
4244 (struct ocfs2_xattr_block *)xb_bh->b_data;
4245 struct ocfs2_xattr_tree_root *xr;
4246 u16 xb_flags = le16_to_cpu(xb->xb_flags);
4248 mlog(0, "create xattr index block for %llu\n",
4249 (unsigned long long)xb_bh->b_blocknr);
4251 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
4252 BUG_ON(!xs->bucket);
4255 * XXX:
4256 * We can use this lock for now, and maybe move to a dedicated mutex
4257 * if performance becomes a problem later.
4259 down_write(&oi->ip_alloc_sem);
4261 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), xb_bh,
4262 OCFS2_JOURNAL_ACCESS_WRITE);
4263 if (ret) {
4264 mlog_errno(ret);
4265 goto out;
4268 ret = __ocfs2_claim_clusters(handle, ctxt->data_ac,
4269 1, 1, &bit_off, &len);
4270 if (ret) {
4271 mlog_errno(ret);
4272 goto out;
4276 * The bucket may spread in many blocks, and
4277 * we will only touch the 1st block and the last block
4278 * in the whole bucket(one for entry and one for data).
4280 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
4282 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
4283 (unsigned long long)blkno);
4285 ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
4286 if (ret) {
4287 mlog_errno(ret);
4288 goto out;
4291 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
4292 OCFS2_JOURNAL_ACCESS_CREATE);
4293 if (ret) {
4294 mlog_errno(ret);
4295 goto out;
4298 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
4299 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
4301 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
4303 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
4304 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
4305 offsetof(struct ocfs2_xattr_block, xb_attrs));
4307 xr = &xb->xb_attrs.xb_root;
4308 xr->xt_clusters = cpu_to_le32(1);
4309 xr->xt_last_eb_blk = 0;
4310 xr->xt_list.l_tree_depth = 0;
4311 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
4312 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
4314 xr->xt_list.l_recs[0].e_cpos = 0;
4315 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
4316 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
4318 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
4320 ocfs2_journal_dirty(handle, xb_bh);
4322 out:
4323 up_write(&oi->ip_alloc_sem);
4325 return ret;
4328 static int cmp_xe_offset(const void *a, const void *b)
4330 const struct ocfs2_xattr_entry *l = a, *r = b;
4331 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
4332 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
4334 if (l_name_offset < r_name_offset)
4335 return 1;
4336 if (l_name_offset > r_name_offset)
4337 return -1;
4338 return 0;
4342 * defrag a xattr bucket if we find that the bucket has some
4343 * holes beteen name/value pairs.
4344 * We will move all the name/value pairs to the end of the bucket
4345 * so that we can spare some space for insertion.
4347 static int ocfs2_defrag_xattr_bucket(struct inode *inode,
4348 handle_t *handle,
4349 struct ocfs2_xattr_bucket *bucket)
4351 int ret, i;
4352 size_t end, offset, len;
4353 struct ocfs2_xattr_header *xh;
4354 char *entries, *buf, *bucket_buf = NULL;
4355 u64 blkno = bucket_blkno(bucket);
4356 u16 xh_free_start;
4357 size_t blocksize = inode->i_sb->s_blocksize;
4358 struct ocfs2_xattr_entry *xe;
4361 * In order to make the operation more efficient and generic,
4362 * we copy all the blocks into a contiguous memory and do the
4363 * defragment there, so if anything is error, we will not touch
4364 * the real block.
4366 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
4367 if (!bucket_buf) {
4368 ret = -EIO;
4369 goto out;
4372 buf = bucket_buf;
4373 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4374 memcpy(buf, bucket_block(bucket, i), blocksize);
4376 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
4377 OCFS2_JOURNAL_ACCESS_WRITE);
4378 if (ret < 0) {
4379 mlog_errno(ret);
4380 goto out;
4383 xh = (struct ocfs2_xattr_header *)bucket_buf;
4384 entries = (char *)xh->xh_entries;
4385 xh_free_start = le16_to_cpu(xh->xh_free_start);
4387 mlog(0, "adjust xattr bucket in %llu, count = %u, "
4388 "xh_free_start = %u, xh_name_value_len = %u.\n",
4389 (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
4390 xh_free_start, le16_to_cpu(xh->xh_name_value_len));
4393 * sort all the entries by their offset.
4394 * the largest will be the first, so that we can
4395 * move them to the end one by one.
4397 sort(entries, le16_to_cpu(xh->xh_count),
4398 sizeof(struct ocfs2_xattr_entry),
4399 cmp_xe_offset, swap_xe);
4401 /* Move all name/values to the end of the bucket. */
4402 xe = xh->xh_entries;
4403 end = OCFS2_XATTR_BUCKET_SIZE;
4404 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
4405 offset = le16_to_cpu(xe->xe_name_offset);
4406 len = namevalue_size_xe(xe);
4409 * We must make sure that the name/value pair
4410 * exist in the same block. So adjust end to
4411 * the previous block end if needed.
4413 if (((end - len) / blocksize !=
4414 (end - 1) / blocksize))
4415 end = end - end % blocksize;
4417 if (end > offset + len) {
4418 memmove(bucket_buf + end - len,
4419 bucket_buf + offset, len);
4420 xe->xe_name_offset = cpu_to_le16(end - len);
4423 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
4424 "bucket %llu\n", (unsigned long long)blkno);
4426 end -= len;
4429 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
4430 "bucket %llu\n", (unsigned long long)blkno);
4432 if (xh_free_start == end)
4433 goto out;
4435 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
4436 xh->xh_free_start = cpu_to_le16(end);
4438 /* sort the entries by their name_hash. */
4439 sort(entries, le16_to_cpu(xh->xh_count),
4440 sizeof(struct ocfs2_xattr_entry),
4441 cmp_xe, swap_xe);
4443 buf = bucket_buf;
4444 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4445 memcpy(bucket_block(bucket, i), buf, blocksize);
4446 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
4448 out:
4449 kfree(bucket_buf);
4450 return ret;
4454 * prev_blkno points to the start of an existing extent. new_blkno
4455 * points to a newly allocated extent. Because we know each of our
4456 * clusters contains more than bucket, we can easily split one cluster
4457 * at a bucket boundary. So we take the last cluster of the existing
4458 * extent and split it down the middle. We move the last half of the
4459 * buckets in the last cluster of the existing extent over to the new
4460 * extent.
4462 * first_bh is the buffer at prev_blkno so we can update the existing
4463 * extent's bucket count. header_bh is the bucket were we were hoping
4464 * to insert our xattr. If the bucket move places the target in the new
4465 * extent, we'll update first_bh and header_bh after modifying the old
4466 * extent.
4468 * first_hash will be set as the 1st xe's name_hash in the new extent.
4470 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
4471 handle_t *handle,
4472 struct ocfs2_xattr_bucket *first,
4473 struct ocfs2_xattr_bucket *target,
4474 u64 new_blkno,
4475 u32 num_clusters,
4476 u32 *first_hash)
4478 int ret;
4479 struct super_block *sb = inode->i_sb;
4480 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb);
4481 int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
4482 int to_move = num_buckets / 2;
4483 u64 src_blkno;
4484 u64 last_cluster_blkno = bucket_blkno(first) +
4485 ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1));
4487 BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets);
4488 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize);
4490 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
4491 (unsigned long long)last_cluster_blkno, (unsigned long long)new_blkno);
4493 ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first),
4494 last_cluster_blkno, new_blkno,
4495 to_move, first_hash);
4496 if (ret) {
4497 mlog_errno(ret);
4498 goto out;
4501 /* This is the first bucket that got moved */
4502 src_blkno = last_cluster_blkno + (to_move * blks_per_bucket);
4505 * If the target bucket was part of the moved buckets, we need to
4506 * update first and target.
4508 if (bucket_blkno(target) >= src_blkno) {
4509 /* Find the block for the new target bucket */
4510 src_blkno = new_blkno +
4511 (bucket_blkno(target) - src_blkno);
4513 ocfs2_xattr_bucket_relse(first);
4514 ocfs2_xattr_bucket_relse(target);
4517 * These shouldn't fail - the buffers are in the
4518 * journal from ocfs2_cp_xattr_bucket().
4520 ret = ocfs2_read_xattr_bucket(first, new_blkno);
4521 if (ret) {
4522 mlog_errno(ret);
4523 goto out;
4525 ret = ocfs2_read_xattr_bucket(target, src_blkno);
4526 if (ret)
4527 mlog_errno(ret);
4531 out:
4532 return ret;
4536 * Find the suitable pos when we divide a bucket into 2.
4537 * We have to make sure the xattrs with the same hash value exist
4538 * in the same bucket.
4540 * If this ocfs2_xattr_header covers more than one hash value, find a
4541 * place where the hash value changes. Try to find the most even split.
4542 * The most common case is that all entries have different hash values,
4543 * and the first check we make will find a place to split.
4545 static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
4547 struct ocfs2_xattr_entry *entries = xh->xh_entries;
4548 int count = le16_to_cpu(xh->xh_count);
4549 int delta, middle = count / 2;
4552 * We start at the middle. Each step gets farther away in both
4553 * directions. We therefore hit the change in hash value
4554 * nearest to the middle. Note that this loop does not execute for
4555 * count < 2.
4557 for (delta = 0; delta < middle; delta++) {
4558 /* Let's check delta earlier than middle */
4559 if (cmp_xe(&entries[middle - delta - 1],
4560 &entries[middle - delta]))
4561 return middle - delta;
4563 /* For even counts, don't walk off the end */
4564 if ((middle + delta + 1) == count)
4565 continue;
4567 /* Now try delta past middle */
4568 if (cmp_xe(&entries[middle + delta],
4569 &entries[middle + delta + 1]))
4570 return middle + delta + 1;
4573 /* Every entry had the same hash */
4574 return count;
4578 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
4579 * first_hash will record the 1st hash of the new bucket.
4581 * Normally half of the xattrs will be moved. But we have to make
4582 * sure that the xattrs with the same hash value are stored in the
4583 * same bucket. If all the xattrs in this bucket have the same hash
4584 * value, the new bucket will be initialized as an empty one and the
4585 * first_hash will be initialized as (hash_value+1).
4587 static int ocfs2_divide_xattr_bucket(struct inode *inode,
4588 handle_t *handle,
4589 u64 blk,
4590 u64 new_blk,
4591 u32 *first_hash,
4592 int new_bucket_head)
4594 int ret, i;
4595 int count, start, len, name_value_len = 0, name_offset = 0;
4596 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
4597 struct ocfs2_xattr_header *xh;
4598 struct ocfs2_xattr_entry *xe;
4599 int blocksize = inode->i_sb->s_blocksize;
4601 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
4602 (unsigned long long)blk, (unsigned long long)new_blk);
4604 s_bucket = ocfs2_xattr_bucket_new(inode);
4605 t_bucket = ocfs2_xattr_bucket_new(inode);
4606 if (!s_bucket || !t_bucket) {
4607 ret = -ENOMEM;
4608 mlog_errno(ret);
4609 goto out;
4612 ret = ocfs2_read_xattr_bucket(s_bucket, blk);
4613 if (ret) {
4614 mlog_errno(ret);
4615 goto out;
4618 ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
4619 OCFS2_JOURNAL_ACCESS_WRITE);
4620 if (ret) {
4621 mlog_errno(ret);
4622 goto out;
4626 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
4627 * there's no need to read it.
4629 ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
4630 if (ret) {
4631 mlog_errno(ret);
4632 goto out;
4636 * Hey, if we're overwriting t_bucket, what difference does
4637 * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the
4638 * same part of ocfs2_cp_xattr_bucket().
4640 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
4641 new_bucket_head ?
4642 OCFS2_JOURNAL_ACCESS_CREATE :
4643 OCFS2_JOURNAL_ACCESS_WRITE);
4644 if (ret) {
4645 mlog_errno(ret);
4646 goto out;
4649 xh = bucket_xh(s_bucket);
4650 count = le16_to_cpu(xh->xh_count);
4651 start = ocfs2_xattr_find_divide_pos(xh);
4653 if (start == count) {
4654 xe = &xh->xh_entries[start-1];
4657 * initialized a new empty bucket here.
4658 * The hash value is set as one larger than
4659 * that of the last entry in the previous bucket.
4661 for (i = 0; i < t_bucket->bu_blocks; i++)
4662 memset(bucket_block(t_bucket, i), 0, blocksize);
4664 xh = bucket_xh(t_bucket);
4665 xh->xh_free_start = cpu_to_le16(blocksize);
4666 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
4667 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
4669 goto set_num_buckets;
4672 /* copy the whole bucket to the new first. */
4673 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4675 /* update the new bucket. */
4676 xh = bucket_xh(t_bucket);
4679 * Calculate the total name/value len and xh_free_start for
4680 * the old bucket first.
4682 name_offset = OCFS2_XATTR_BUCKET_SIZE;
4683 name_value_len = 0;
4684 for (i = 0; i < start; i++) {
4685 xe = &xh->xh_entries[i];
4686 name_value_len += namevalue_size_xe(xe);
4687 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
4688 name_offset = le16_to_cpu(xe->xe_name_offset);
4692 * Now begin the modification to the new bucket.
4694 * In the new bucket, We just move the xattr entry to the beginning
4695 * and don't touch the name/value. So there will be some holes in the
4696 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
4697 * called.
4699 xe = &xh->xh_entries[start];
4700 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
4701 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
4702 (int)((char *)xe - (char *)xh),
4703 (int)((char *)xh->xh_entries - (char *)xh));
4704 memmove((char *)xh->xh_entries, (char *)xe, len);
4705 xe = &xh->xh_entries[count - start];
4706 len = sizeof(struct ocfs2_xattr_entry) * start;
4707 memset((char *)xe, 0, len);
4709 le16_add_cpu(&xh->xh_count, -start);
4710 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
4712 /* Calculate xh_free_start for the new bucket. */
4713 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4714 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4715 xe = &xh->xh_entries[i];
4716 if (le16_to_cpu(xe->xe_name_offset) <
4717 le16_to_cpu(xh->xh_free_start))
4718 xh->xh_free_start = xe->xe_name_offset;
4721 set_num_buckets:
4722 /* set xh->xh_num_buckets for the new xh. */
4723 if (new_bucket_head)
4724 xh->xh_num_buckets = cpu_to_le16(1);
4725 else
4726 xh->xh_num_buckets = 0;
4728 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
4730 /* store the first_hash of the new bucket. */
4731 if (first_hash)
4732 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
4735 * Now only update the 1st block of the old bucket. If we
4736 * just added a new empty bucket, there is no need to modify
4737 * it.
4739 if (start == count)
4740 goto out;
4742 xh = bucket_xh(s_bucket);
4743 memset(&xh->xh_entries[start], 0,
4744 sizeof(struct ocfs2_xattr_entry) * (count - start));
4745 xh->xh_count = cpu_to_le16(start);
4746 xh->xh_free_start = cpu_to_le16(name_offset);
4747 xh->xh_name_value_len = cpu_to_le16(name_value_len);
4749 ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
4751 out:
4752 ocfs2_xattr_bucket_free(s_bucket);
4753 ocfs2_xattr_bucket_free(t_bucket);
4755 return ret;
4759 * Copy xattr from one bucket to another bucket.
4761 * The caller must make sure that the journal transaction
4762 * has enough space for journaling.
4764 static int ocfs2_cp_xattr_bucket(struct inode *inode,
4765 handle_t *handle,
4766 u64 s_blkno,
4767 u64 t_blkno,
4768 int t_is_new)
4770 int ret;
4771 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
4773 BUG_ON(s_blkno == t_blkno);
4775 mlog(0, "cp bucket %llu to %llu, target is %d\n",
4776 (unsigned long long)s_blkno, (unsigned long long)t_blkno,
4777 t_is_new);
4779 s_bucket = ocfs2_xattr_bucket_new(inode);
4780 t_bucket = ocfs2_xattr_bucket_new(inode);
4781 if (!s_bucket || !t_bucket) {
4782 ret = -ENOMEM;
4783 mlog_errno(ret);
4784 goto out;
4787 ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
4788 if (ret)
4789 goto out;
4792 * Even if !t_is_new, we're overwriting t_bucket. Thus,
4793 * there's no need to read it.
4795 ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
4796 if (ret)
4797 goto out;
4800 * Hey, if we're overwriting t_bucket, what difference does
4801 * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new
4802 * cluster to fill, we came here from
4803 * ocfs2_mv_xattr_buckets(), and it is really new -
4804 * ACCESS_CREATE is required. But we also might have moved data
4805 * out of t_bucket before extending back into it.
4806 * ocfs2_add_new_xattr_bucket() can do this - its call to
4807 * ocfs2_add_new_xattr_cluster() may have created a new extent
4808 * and copied out the end of the old extent. Then it re-extends
4809 * the old extent back to create space for new xattrs. That's
4810 * how we get here, and the bucket isn't really new.
4812 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
4813 t_is_new ?
4814 OCFS2_JOURNAL_ACCESS_CREATE :
4815 OCFS2_JOURNAL_ACCESS_WRITE);
4816 if (ret)
4817 goto out;
4819 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4820 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
4822 out:
4823 ocfs2_xattr_bucket_free(t_bucket);
4824 ocfs2_xattr_bucket_free(s_bucket);
4826 return ret;
4830 * src_blk points to the start of an existing extent. last_blk points to
4831 * last cluster in that extent. to_blk points to a newly allocated
4832 * extent. We copy the buckets from the cluster at last_blk to the new
4833 * extent. If start_bucket is non-zero, we skip that many buckets before
4834 * we start copying. The new extent's xh_num_buckets gets set to the
4835 * number of buckets we copied. The old extent's xh_num_buckets shrinks
4836 * by the same amount.
4838 static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
4839 u64 src_blk, u64 last_blk, u64 to_blk,
4840 unsigned int start_bucket,
4841 u32 *first_hash)
4843 int i, ret, credits;
4844 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4845 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4846 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
4847 struct ocfs2_xattr_bucket *old_first, *new_first;
4849 mlog(0, "mv xattrs from cluster %llu to %llu\n",
4850 (unsigned long long)last_blk, (unsigned long long)to_blk);
4852 BUG_ON(start_bucket >= num_buckets);
4853 if (start_bucket) {
4854 num_buckets -= start_bucket;
4855 last_blk += (start_bucket * blks_per_bucket);
4858 /* The first bucket of the original extent */
4859 old_first = ocfs2_xattr_bucket_new(inode);
4860 /* The first bucket of the new extent */
4861 new_first = ocfs2_xattr_bucket_new(inode);
4862 if (!old_first || !new_first) {
4863 ret = -ENOMEM;
4864 mlog_errno(ret);
4865 goto out;
4868 ret = ocfs2_read_xattr_bucket(old_first, src_blk);
4869 if (ret) {
4870 mlog_errno(ret);
4871 goto out;
4875 * We need to update the first bucket of the old extent and all
4876 * the buckets going to the new extent.
4878 credits = ((num_buckets + 1) * blks_per_bucket);
4879 ret = ocfs2_extend_trans(handle, credits);
4880 if (ret) {
4881 mlog_errno(ret);
4882 goto out;
4885 ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
4886 OCFS2_JOURNAL_ACCESS_WRITE);
4887 if (ret) {
4888 mlog_errno(ret);
4889 goto out;
4892 for (i = 0; i < num_buckets; i++) {
4893 ret = ocfs2_cp_xattr_bucket(inode, handle,
4894 last_blk + (i * blks_per_bucket),
4895 to_blk + (i * blks_per_bucket),
4897 if (ret) {
4898 mlog_errno(ret);
4899 goto out;
4904 * Get the new bucket ready before we dirty anything
4905 * (This actually shouldn't fail, because we already dirtied
4906 * it once in ocfs2_cp_xattr_bucket()).
4908 ret = ocfs2_read_xattr_bucket(new_first, to_blk);
4909 if (ret) {
4910 mlog_errno(ret);
4911 goto out;
4913 ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
4914 OCFS2_JOURNAL_ACCESS_WRITE);
4915 if (ret) {
4916 mlog_errno(ret);
4917 goto out;
4920 /* Now update the headers */
4921 le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets);
4922 ocfs2_xattr_bucket_journal_dirty(handle, old_first);
4924 bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets);
4925 ocfs2_xattr_bucket_journal_dirty(handle, new_first);
4927 if (first_hash)
4928 *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash);
4930 out:
4931 ocfs2_xattr_bucket_free(new_first);
4932 ocfs2_xattr_bucket_free(old_first);
4933 return ret;
4937 * Move some xattrs in this cluster to the new cluster.
4938 * This function should only be called when bucket size == cluster size.
4939 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
4941 static int ocfs2_divide_xattr_cluster(struct inode *inode,
4942 handle_t *handle,
4943 u64 prev_blk,
4944 u64 new_blk,
4945 u32 *first_hash)
4947 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4948 int ret, credits = 2 * blk_per_bucket;
4950 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
4952 ret = ocfs2_extend_trans(handle, credits);
4953 if (ret) {
4954 mlog_errno(ret);
4955 return ret;
4958 /* Move half of the xattr in start_blk to the next bucket. */
4959 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
4960 new_blk, first_hash, 1);
4964 * Move some xattrs from the old cluster to the new one since they are not
4965 * contiguous in ocfs2 xattr tree.
4967 * new_blk starts a new separate cluster, and we will move some xattrs from
4968 * prev_blk to it. v_start will be set as the first name hash value in this
4969 * new cluster so that it can be used as e_cpos during tree insertion and
4970 * don't collide with our original b-tree operations. first_bh and header_bh
4971 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
4972 * to extend the insert bucket.
4974 * The problem is how much xattr should we move to the new one and when should
4975 * we update first_bh and header_bh?
4976 * 1. If cluster size > bucket size, that means the previous cluster has more
4977 * than 1 bucket, so just move half nums of bucket into the new cluster and
4978 * update the first_bh and header_bh if the insert bucket has been moved
4979 * to the new cluster.
4980 * 2. If cluster_size == bucket_size:
4981 * a) If the previous extent rec has more than one cluster and the insert
4982 * place isn't in the last cluster, copy the entire last cluster to the
4983 * new one. This time, we don't need to upate the first_bh and header_bh
4984 * since they will not be moved into the new cluster.
4985 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
4986 * the new one. And we set the extend flag to zero if the insert place is
4987 * moved into the new allocated cluster since no extend is needed.
4989 static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
4990 handle_t *handle,
4991 struct ocfs2_xattr_bucket *first,
4992 struct ocfs2_xattr_bucket *target,
4993 u64 new_blk,
4994 u32 prev_clusters,
4995 u32 *v_start,
4996 int *extend)
4998 int ret;
5000 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
5001 (unsigned long long)bucket_blkno(first), prev_clusters,
5002 (unsigned long long)new_blk);
5004 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) {
5005 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
5006 handle,
5007 first, target,
5008 new_blk,
5009 prev_clusters,
5010 v_start);
5011 if (ret)
5012 mlog_errno(ret);
5013 } else {
5014 /* The start of the last cluster in the first extent */
5015 u64 last_blk = bucket_blkno(first) +
5016 ((prev_clusters - 1) *
5017 ocfs2_clusters_to_blocks(inode->i_sb, 1));
5019 if (prev_clusters > 1 && bucket_blkno(target) != last_blk) {
5020 ret = ocfs2_mv_xattr_buckets(inode, handle,
5021 bucket_blkno(first),
5022 last_blk, new_blk, 0,
5023 v_start);
5024 if (ret)
5025 mlog_errno(ret);
5026 } else {
5027 ret = ocfs2_divide_xattr_cluster(inode, handle,
5028 last_blk, new_blk,
5029 v_start);
5030 if (ret)
5031 mlog_errno(ret);
5033 if ((bucket_blkno(target) == last_blk) && extend)
5034 *extend = 0;
5038 return ret;
5042 * Add a new cluster for xattr storage.
5044 * If the new cluster is contiguous with the previous one, it will be
5045 * appended to the same extent record, and num_clusters will be updated.
5046 * If not, we will insert a new extent for it and move some xattrs in
5047 * the last cluster into the new allocated one.
5048 * We also need to limit the maximum size of a btree leaf, otherwise we'll
5049 * lose the benefits of hashing because we'll have to search large leaves.
5050 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
5051 * if it's bigger).
5053 * first_bh is the first block of the previous extent rec and header_bh
5054 * indicates the bucket we will insert the new xattrs. They will be updated
5055 * when the header_bh is moved into the new cluster.
5057 static int ocfs2_add_new_xattr_cluster(struct inode *inode,
5058 struct buffer_head *root_bh,
5059 struct ocfs2_xattr_bucket *first,
5060 struct ocfs2_xattr_bucket *target,
5061 u32 *num_clusters,
5062 u32 prev_cpos,
5063 int *extend,
5064 struct ocfs2_xattr_set_ctxt *ctxt)
5066 int ret;
5067 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
5068 u32 prev_clusters = *num_clusters;
5069 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
5070 u64 block;
5071 handle_t *handle = ctxt->handle;
5072 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5073 struct ocfs2_extent_tree et;
5075 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
5076 "previous xattr blkno = %llu\n",
5077 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5078 prev_cpos, (unsigned long long)bucket_blkno(first));
5080 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
5082 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
5083 OCFS2_JOURNAL_ACCESS_WRITE);
5084 if (ret < 0) {
5085 mlog_errno(ret);
5086 goto leave;
5089 ret = __ocfs2_claim_clusters(handle, ctxt->data_ac, 1,
5090 clusters_to_add, &bit_off, &num_bits);
5091 if (ret < 0) {
5092 if (ret != -ENOSPC)
5093 mlog_errno(ret);
5094 goto leave;
5097 BUG_ON(num_bits > clusters_to_add);
5099 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
5100 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
5101 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
5103 if (bucket_blkno(first) + (prev_clusters * bpc) == block &&
5104 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
5105 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
5107 * If this cluster is contiguous with the old one and
5108 * adding this new cluster, we don't surpass the limit of
5109 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
5110 * initialized and used like other buckets in the previous
5111 * cluster.
5112 * So add it as a contiguous one. The caller will handle
5113 * its init process.
5115 v_start = prev_cpos + prev_clusters;
5116 *num_clusters = prev_clusters + num_bits;
5117 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
5118 num_bits);
5119 } else {
5120 ret = ocfs2_adjust_xattr_cross_cluster(inode,
5121 handle,
5122 first,
5123 target,
5124 block,
5125 prev_clusters,
5126 &v_start,
5127 extend);
5128 if (ret) {
5129 mlog_errno(ret);
5130 goto leave;
5134 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
5135 num_bits, (unsigned long long)block, v_start);
5136 ret = ocfs2_insert_extent(handle, &et, v_start, block,
5137 num_bits, 0, ctxt->meta_ac);
5138 if (ret < 0) {
5139 mlog_errno(ret);
5140 goto leave;
5143 ocfs2_journal_dirty(handle, root_bh);
5145 leave:
5146 return ret;
5150 * We are given an extent. 'first' is the bucket at the very front of
5151 * the extent. The extent has space for an additional bucket past
5152 * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
5153 * of the target bucket. We wish to shift every bucket past the target
5154 * down one, filling in that additional space. When we get back to the
5155 * target, we split the target between itself and the now-empty bucket
5156 * at target+1 (aka, target_blkno + blks_per_bucket).
5158 static int ocfs2_extend_xattr_bucket(struct inode *inode,
5159 handle_t *handle,
5160 struct ocfs2_xattr_bucket *first,
5161 u64 target_blk,
5162 u32 num_clusters)
5164 int ret, credits;
5165 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5166 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
5167 u64 end_blk;
5168 u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets);
5170 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
5171 "from %llu, len = %u\n", (unsigned long long)target_blk,
5172 (unsigned long long)bucket_blkno(first), num_clusters);
5174 /* The extent must have room for an additional bucket */
5175 BUG_ON(new_bucket >=
5176 (num_clusters * ocfs2_xattr_buckets_per_cluster(osb)));
5178 /* end_blk points to the last existing bucket */
5179 end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket);
5182 * end_blk is the start of the last existing bucket.
5183 * Thus, (end_blk - target_blk) covers the target bucket and
5184 * every bucket after it up to, but not including, the last
5185 * existing bucket. Then we add the last existing bucket, the
5186 * new bucket, and the first bucket (3 * blk_per_bucket).
5188 credits = (end_blk - target_blk) + (3 * blk_per_bucket);
5189 ret = ocfs2_extend_trans(handle, credits);
5190 if (ret) {
5191 mlog_errno(ret);
5192 goto out;
5195 ret = ocfs2_xattr_bucket_journal_access(handle, first,
5196 OCFS2_JOURNAL_ACCESS_WRITE);
5197 if (ret) {
5198 mlog_errno(ret);
5199 goto out;
5202 while (end_blk != target_blk) {
5203 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
5204 end_blk + blk_per_bucket, 0);
5205 if (ret)
5206 goto out;
5207 end_blk -= blk_per_bucket;
5210 /* Move half of the xattr in target_blkno to the next bucket. */
5211 ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk,
5212 target_blk + blk_per_bucket, NULL, 0);
5214 le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1);
5215 ocfs2_xattr_bucket_journal_dirty(handle, first);
5217 out:
5218 return ret;
5222 * Add new xattr bucket in an extent record and adjust the buckets
5223 * accordingly. xb_bh is the ocfs2_xattr_block, and target is the
5224 * bucket we want to insert into.
5226 * In the easy case, we will move all the buckets after target down by
5227 * one. Half of target's xattrs will be moved to the next bucket.
5229 * If current cluster is full, we'll allocate a new one. This may not
5230 * be contiguous. The underlying calls will make sure that there is
5231 * space for the insert, shifting buckets around if necessary.
5232 * 'target' may be moved by those calls.
5234 static int ocfs2_add_new_xattr_bucket(struct inode *inode,
5235 struct buffer_head *xb_bh,
5236 struct ocfs2_xattr_bucket *target,
5237 struct ocfs2_xattr_set_ctxt *ctxt)
5239 struct ocfs2_xattr_block *xb =
5240 (struct ocfs2_xattr_block *)xb_bh->b_data;
5241 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
5242 struct ocfs2_extent_list *el = &xb_root->xt_list;
5243 u32 name_hash =
5244 le32_to_cpu(bucket_xh(target)->xh_entries[0].xe_name_hash);
5245 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5246 int ret, num_buckets, extend = 1;
5247 u64 p_blkno;
5248 u32 e_cpos, num_clusters;
5249 /* The bucket at the front of the extent */
5250 struct ocfs2_xattr_bucket *first;
5252 mlog(0, "Add new xattr bucket starting from %llu\n",
5253 (unsigned long long)bucket_blkno(target));
5255 /* The first bucket of the original extent */
5256 first = ocfs2_xattr_bucket_new(inode);
5257 if (!first) {
5258 ret = -ENOMEM;
5259 mlog_errno(ret);
5260 goto out;
5263 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
5264 &num_clusters, el);
5265 if (ret) {
5266 mlog_errno(ret);
5267 goto out;
5270 ret = ocfs2_read_xattr_bucket(first, p_blkno);
5271 if (ret) {
5272 mlog_errno(ret);
5273 goto out;
5276 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
5277 if (num_buckets == le16_to_cpu(bucket_xh(first)->xh_num_buckets)) {
5279 * This can move first+target if the target bucket moves
5280 * to the new extent.
5282 ret = ocfs2_add_new_xattr_cluster(inode,
5283 xb_bh,
5284 first,
5285 target,
5286 &num_clusters,
5287 e_cpos,
5288 &extend,
5289 ctxt);
5290 if (ret) {
5291 mlog_errno(ret);
5292 goto out;
5296 if (extend) {
5297 ret = ocfs2_extend_xattr_bucket(inode,
5298 ctxt->handle,
5299 first,
5300 bucket_blkno(target),
5301 num_clusters);
5302 if (ret)
5303 mlog_errno(ret);
5306 out:
5307 ocfs2_xattr_bucket_free(first);
5309 return ret;
5312 static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
5313 struct ocfs2_xattr_bucket *bucket,
5314 int offs)
5316 int block_off = offs >> inode->i_sb->s_blocksize_bits;
5318 offs = offs % inode->i_sb->s_blocksize;
5319 return bucket_block(bucket, block_off) + offs;
5323 * Truncate the specified xe_off entry in xattr bucket.
5324 * bucket is indicated by header_bh and len is the new length.
5325 * Both the ocfs2_xattr_value_root and the entry will be updated here.
5327 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
5329 static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
5330 struct ocfs2_xattr_bucket *bucket,
5331 int xe_off,
5332 int len,
5333 struct ocfs2_xattr_set_ctxt *ctxt)
5335 int ret, offset;
5336 u64 value_blk;
5337 struct ocfs2_xattr_entry *xe;
5338 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5339 size_t blocksize = inode->i_sb->s_blocksize;
5340 struct ocfs2_xattr_value_buf vb = {
5341 .vb_access = ocfs2_journal_access,
5344 xe = &xh->xh_entries[xe_off];
5346 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
5348 offset = le16_to_cpu(xe->xe_name_offset) +
5349 OCFS2_XATTR_SIZE(xe->xe_name_len);
5351 value_blk = offset / blocksize;
5353 /* We don't allow ocfs2_xattr_value to be stored in different block. */
5354 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
5356 vb.vb_bh = bucket->bu_bhs[value_blk];
5357 BUG_ON(!vb.vb_bh);
5359 vb.vb_xv = (struct ocfs2_xattr_value_root *)
5360 (vb.vb_bh->b_data + offset % blocksize);
5363 * From here on out we have to dirty the bucket. The generic
5364 * value calls only modify one of the bucket's bhs, but we need
5365 * to send the bucket at once. So if they error, they *could* have
5366 * modified something. We have to assume they did, and dirty
5367 * the whole bucket. This leaves us in a consistent state.
5369 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
5370 xe_off, (unsigned long long)bucket_blkno(bucket), len);
5371 ret = ocfs2_xattr_value_truncate(inode, &vb, len, ctxt);
5372 if (ret) {
5373 mlog_errno(ret);
5374 goto out;
5377 ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
5378 OCFS2_JOURNAL_ACCESS_WRITE);
5379 if (ret) {
5380 mlog_errno(ret);
5381 goto out;
5384 xe->xe_value_size = cpu_to_le64(len);
5386 ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket);
5388 out:
5389 return ret;
5392 static int ocfs2_rm_xattr_cluster(struct inode *inode,
5393 struct buffer_head *root_bh,
5394 u64 blkno,
5395 u32 cpos,
5396 u32 len,
5397 void *para)
5399 int ret;
5400 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5401 struct inode *tl_inode = osb->osb_tl_inode;
5402 handle_t *handle;
5403 struct ocfs2_xattr_block *xb =
5404 (struct ocfs2_xattr_block *)root_bh->b_data;
5405 struct ocfs2_alloc_context *meta_ac = NULL;
5406 struct ocfs2_cached_dealloc_ctxt dealloc;
5407 struct ocfs2_extent_tree et;
5409 ret = ocfs2_iterate_xattr_buckets(inode, blkno, len,
5410 ocfs2_delete_xattr_in_bucket, para);
5411 if (ret) {
5412 mlog_errno(ret);
5413 return ret;
5416 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
5418 ocfs2_init_dealloc_ctxt(&dealloc);
5420 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
5421 cpos, len, (unsigned long long)blkno);
5423 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode), blkno,
5424 len);
5426 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
5427 if (ret) {
5428 mlog_errno(ret);
5429 return ret;
5432 mutex_lock(&tl_inode->i_mutex);
5434 if (ocfs2_truncate_log_needs_flush(osb)) {
5435 ret = __ocfs2_flush_truncate_log(osb);
5436 if (ret < 0) {
5437 mlog_errno(ret);
5438 goto out;
5442 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
5443 if (IS_ERR(handle)) {
5444 ret = -ENOMEM;
5445 mlog_errno(ret);
5446 goto out;
5449 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
5450 OCFS2_JOURNAL_ACCESS_WRITE);
5451 if (ret) {
5452 mlog_errno(ret);
5453 goto out_commit;
5456 ret = ocfs2_remove_extent(handle, &et, cpos, len, meta_ac,
5457 &dealloc);
5458 if (ret) {
5459 mlog_errno(ret);
5460 goto out_commit;
5463 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
5464 ocfs2_journal_dirty(handle, root_bh);
5466 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
5467 if (ret)
5468 mlog_errno(ret);
5470 out_commit:
5471 ocfs2_commit_trans(osb, handle);
5472 out:
5473 ocfs2_schedule_truncate_log_flush(osb, 1);
5475 mutex_unlock(&tl_inode->i_mutex);
5477 if (meta_ac)
5478 ocfs2_free_alloc_context(meta_ac);
5480 ocfs2_run_deallocs(osb, &dealloc);
5482 return ret;
5486 * check whether the xattr bucket is filled up with the same hash value.
5487 * If we want to insert the xattr with the same hash, return -ENOSPC.
5488 * If we want to insert a xattr with different hash value, go ahead
5489 * and ocfs2_divide_xattr_bucket will handle this.
5491 static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
5492 struct ocfs2_xattr_bucket *bucket,
5493 const char *name)
5495 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5496 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
5498 if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
5499 return 0;
5501 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
5502 xh->xh_entries[0].xe_name_hash) {
5503 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
5504 "hash = %u\n",
5505 (unsigned long long)bucket_blkno(bucket),
5506 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
5507 return -ENOSPC;
5510 return 0;
5514 * Try to set the entry in the current bucket. If we fail, the caller
5515 * will handle getting us another bucket.
5517 static int ocfs2_xattr_set_entry_bucket(struct inode *inode,
5518 struct ocfs2_xattr_info *xi,
5519 struct ocfs2_xattr_search *xs,
5520 struct ocfs2_xattr_set_ctxt *ctxt)
5522 int ret;
5523 struct ocfs2_xa_loc loc;
5525 mlog_entry("Set xattr %s in xattr bucket\n", xi->xi_name);
5527 ocfs2_init_xattr_bucket_xa_loc(&loc, xs->bucket,
5528 xs->not_found ? NULL : xs->here);
5529 ret = ocfs2_xa_set(&loc, xi, ctxt);
5530 if (!ret) {
5531 xs->here = loc.xl_entry;
5532 goto out;
5534 if (ret != -ENOSPC) {
5535 mlog_errno(ret);
5536 goto out;
5539 /* Ok, we need space. Let's try defragmenting the bucket. */
5540 ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
5541 xs->bucket);
5542 if (ret) {
5543 mlog_errno(ret);
5544 goto out;
5547 ret = ocfs2_xa_set(&loc, xi, ctxt);
5548 if (!ret) {
5549 xs->here = loc.xl_entry;
5550 goto out;
5552 if (ret != -ENOSPC)
5553 mlog_errno(ret);
5556 out:
5557 mlog_exit(ret);
5558 return ret;
5561 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
5562 struct ocfs2_xattr_info *xi,
5563 struct ocfs2_xattr_search *xs,
5564 struct ocfs2_xattr_set_ctxt *ctxt)
5566 int ret;
5568 mlog_entry("Set xattr %s in xattr index block\n", xi->xi_name);
5570 ret = ocfs2_xattr_set_entry_bucket(inode, xi, xs, ctxt);
5571 if (!ret)
5572 goto out;
5573 if (ret != -ENOSPC) {
5574 mlog_errno(ret);
5575 goto out;
5578 /* Ack, need more space. Let's try to get another bucket! */
5581 * We do not allow for overlapping ranges between buckets. And
5582 * the maximum number of collisions we will allow for then is
5583 * one bucket's worth, so check it here whether we need to
5584 * add a new bucket for the insert.
5586 ret = ocfs2_check_xattr_bucket_collision(inode,
5587 xs->bucket,
5588 xi->xi_name);
5589 if (ret) {
5590 mlog_errno(ret);
5591 goto out;
5594 ret = ocfs2_add_new_xattr_bucket(inode,
5595 xs->xattr_bh,
5596 xs->bucket,
5597 ctxt);
5598 if (ret) {
5599 mlog_errno(ret);
5600 goto out;
5604 * ocfs2_add_new_xattr_bucket() will have updated
5605 * xs->bucket if it moved, but it will not have updated
5606 * any of the other search fields. Thus, we drop it and
5607 * re-search. Everything should be cached, so it'll be
5608 * quick.
5610 ocfs2_xattr_bucket_relse(xs->bucket);
5611 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
5612 xi->xi_name_index,
5613 xi->xi_name, xs);
5614 if (ret && ret != -ENODATA)
5615 goto out;
5616 xs->not_found = ret;
5618 /* Ok, we have a new bucket, let's try again */
5619 ret = ocfs2_xattr_set_entry_bucket(inode, xi, xs, ctxt);
5620 if (ret && (ret != -ENOSPC))
5621 mlog_errno(ret);
5623 out:
5624 mlog_exit(ret);
5625 return ret;
5628 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
5629 struct ocfs2_xattr_bucket *bucket,
5630 void *para)
5632 int ret = 0, ref_credits;
5633 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5634 u16 i;
5635 struct ocfs2_xattr_entry *xe;
5636 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5637 struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
5638 int credits = ocfs2_remove_extent_credits(osb->sb) +
5639 ocfs2_blocks_per_xattr_bucket(inode->i_sb);
5640 struct ocfs2_xattr_value_root *xv;
5641 struct ocfs2_rm_xattr_bucket_para *args =
5642 (struct ocfs2_rm_xattr_bucket_para *)para;
5644 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
5646 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
5647 xe = &xh->xh_entries[i];
5648 if (ocfs2_xattr_is_local(xe))
5649 continue;
5651 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket,
5652 i, &xv, NULL);
5654 ret = ocfs2_lock_xattr_remove_allocators(inode, xv,
5655 args->ref_ci,
5656 args->ref_root_bh,
5657 &ctxt.meta_ac,
5658 &ref_credits);
5660 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
5661 if (IS_ERR(ctxt.handle)) {
5662 ret = PTR_ERR(ctxt.handle);
5663 mlog_errno(ret);
5664 break;
5667 ret = ocfs2_xattr_bucket_value_truncate(inode, bucket,
5668 i, 0, &ctxt);
5670 ocfs2_commit_trans(osb, ctxt.handle);
5671 if (ctxt.meta_ac) {
5672 ocfs2_free_alloc_context(ctxt.meta_ac);
5673 ctxt.meta_ac = NULL;
5675 if (ret) {
5676 mlog_errno(ret);
5677 break;
5681 if (ctxt.meta_ac)
5682 ocfs2_free_alloc_context(ctxt.meta_ac);
5683 ocfs2_schedule_truncate_log_flush(osb, 1);
5684 ocfs2_run_deallocs(osb, &ctxt.dealloc);
5685 return ret;
5689 * Whenever we modify a xattr value root in the bucket(e.g, CoW
5690 * or change the extent record flag), we need to recalculate
5691 * the metaecc for the whole bucket. So it is done here.
5693 * Note:
5694 * We have to give the extra credits for the caller.
5696 static int ocfs2_xattr_bucket_post_refcount(struct inode *inode,
5697 handle_t *handle,
5698 void *para)
5700 int ret;
5701 struct ocfs2_xattr_bucket *bucket =
5702 (struct ocfs2_xattr_bucket *)para;
5704 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
5705 OCFS2_JOURNAL_ACCESS_WRITE);
5706 if (ret) {
5707 mlog_errno(ret);
5708 return ret;
5711 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
5713 return 0;
5717 * Special action we need if the xattr value is refcounted.
5719 * 1. If the xattr is refcounted, lock the tree.
5720 * 2. CoW the xattr if we are setting the new value and the value
5721 * will be stored outside.
5722 * 3. In other case, decrease_refcount will work for us, so just
5723 * lock the refcount tree, calculate the meta and credits is OK.
5725 * We have to do CoW before ocfs2_init_xattr_set_ctxt since
5726 * currently CoW is a completed transaction, while this function
5727 * will also lock the allocators and let us deadlock. So we will
5728 * CoW the whole xattr value.
5730 static int ocfs2_prepare_refcount_xattr(struct inode *inode,
5731 struct ocfs2_dinode *di,
5732 struct ocfs2_xattr_info *xi,
5733 struct ocfs2_xattr_search *xis,
5734 struct ocfs2_xattr_search *xbs,
5735 struct ocfs2_refcount_tree **ref_tree,
5736 int *meta_add,
5737 int *credits)
5739 int ret = 0;
5740 struct ocfs2_xattr_block *xb;
5741 struct ocfs2_xattr_entry *xe;
5742 char *base;
5743 u32 p_cluster, num_clusters;
5744 unsigned int ext_flags;
5745 int name_offset, name_len;
5746 struct ocfs2_xattr_value_buf vb;
5747 struct ocfs2_xattr_bucket *bucket = NULL;
5748 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5749 struct ocfs2_post_refcount refcount;
5750 struct ocfs2_post_refcount *p = NULL;
5751 struct buffer_head *ref_root_bh = NULL;
5753 if (!xis->not_found) {
5754 xe = xis->here;
5755 name_offset = le16_to_cpu(xe->xe_name_offset);
5756 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5757 base = xis->base;
5758 vb.vb_bh = xis->inode_bh;
5759 vb.vb_access = ocfs2_journal_access_di;
5760 } else {
5761 int i, block_off = 0;
5762 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
5763 xe = xbs->here;
5764 name_offset = le16_to_cpu(xe->xe_name_offset);
5765 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5766 i = xbs->here - xbs->header->xh_entries;
5768 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
5769 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
5770 bucket_xh(xbs->bucket),
5771 i, &block_off,
5772 &name_offset);
5773 if (ret) {
5774 mlog_errno(ret);
5775 goto out;
5777 base = bucket_block(xbs->bucket, block_off);
5778 vb.vb_bh = xbs->bucket->bu_bhs[block_off];
5779 vb.vb_access = ocfs2_journal_access;
5781 if (ocfs2_meta_ecc(osb)) {
5782 /*create parameters for ocfs2_post_refcount. */
5783 bucket = xbs->bucket;
5784 refcount.credits = bucket->bu_blocks;
5785 refcount.para = bucket;
5786 refcount.func =
5787 ocfs2_xattr_bucket_post_refcount;
5788 p = &refcount;
5790 } else {
5791 base = xbs->base;
5792 vb.vb_bh = xbs->xattr_bh;
5793 vb.vb_access = ocfs2_journal_access_xb;
5797 if (ocfs2_xattr_is_local(xe))
5798 goto out;
5800 vb.vb_xv = (struct ocfs2_xattr_value_root *)
5801 (base + name_offset + name_len);
5803 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
5804 &num_clusters, &vb.vb_xv->xr_list,
5805 &ext_flags);
5806 if (ret) {
5807 mlog_errno(ret);
5808 goto out;
5812 * We just need to check the 1st extent record, since we always
5813 * CoW the whole xattr. So there shouldn't be a xattr with
5814 * some REFCOUNT extent recs after the 1st one.
5816 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
5817 goto out;
5819 ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
5820 1, ref_tree, &ref_root_bh);
5821 if (ret) {
5822 mlog_errno(ret);
5823 goto out;
5827 * If we are deleting the xattr or the new size will be stored inside,
5828 * cool, leave it there, the xattr truncate process will remove them
5829 * for us(it still needs the refcount tree lock and the meta, credits).
5830 * And the worse case is that every cluster truncate will split the
5831 * refcount tree, and make the original extent become 3. So we will need
5832 * 2 * cluster more extent recs at most.
5834 if (!xi->xi_value || xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE) {
5836 ret = ocfs2_refcounted_xattr_delete_need(inode,
5837 &(*ref_tree)->rf_ci,
5838 ref_root_bh, vb.vb_xv,
5839 meta_add, credits);
5840 if (ret)
5841 mlog_errno(ret);
5842 goto out;
5845 ret = ocfs2_refcount_cow_xattr(inode, di, &vb,
5846 *ref_tree, ref_root_bh, 0,
5847 le32_to_cpu(vb.vb_xv->xr_clusters), p);
5848 if (ret)
5849 mlog_errno(ret);
5851 out:
5852 brelse(ref_root_bh);
5853 return ret;
5857 * Add the REFCOUNTED flags for all the extent rec in ocfs2_xattr_value_root.
5858 * The physical clusters will be added to refcount tree.
5860 static int ocfs2_xattr_value_attach_refcount(struct inode *inode,
5861 struct ocfs2_xattr_value_root *xv,
5862 struct ocfs2_extent_tree *value_et,
5863 struct ocfs2_caching_info *ref_ci,
5864 struct buffer_head *ref_root_bh,
5865 struct ocfs2_cached_dealloc_ctxt *dealloc,
5866 struct ocfs2_post_refcount *refcount)
5868 int ret = 0;
5869 u32 clusters = le32_to_cpu(xv->xr_clusters);
5870 u32 cpos, p_cluster, num_clusters;
5871 struct ocfs2_extent_list *el = &xv->xr_list;
5872 unsigned int ext_flags;
5874 cpos = 0;
5875 while (cpos < clusters) {
5876 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
5877 &num_clusters, el, &ext_flags);
5879 cpos += num_clusters;
5880 if ((ext_flags & OCFS2_EXT_REFCOUNTED))
5881 continue;
5883 BUG_ON(!p_cluster);
5885 ret = ocfs2_add_refcount_flag(inode, value_et,
5886 ref_ci, ref_root_bh,
5887 cpos - num_clusters,
5888 p_cluster, num_clusters,
5889 dealloc, refcount);
5890 if (ret) {
5891 mlog_errno(ret);
5892 break;
5896 return ret;
5900 * Given a normal ocfs2_xattr_header, refcount all the entries which
5901 * have value stored outside.
5902 * Used for xattrs stored in inode and ocfs2_xattr_block.
5904 static int ocfs2_xattr_attach_refcount_normal(struct inode *inode,
5905 struct ocfs2_xattr_value_buf *vb,
5906 struct ocfs2_xattr_header *header,
5907 struct ocfs2_caching_info *ref_ci,
5908 struct buffer_head *ref_root_bh,
5909 struct ocfs2_cached_dealloc_ctxt *dealloc)
5912 struct ocfs2_xattr_entry *xe;
5913 struct ocfs2_xattr_value_root *xv;
5914 struct ocfs2_extent_tree et;
5915 int i, ret = 0;
5917 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
5918 xe = &header->xh_entries[i];
5920 if (ocfs2_xattr_is_local(xe))
5921 continue;
5923 xv = (struct ocfs2_xattr_value_root *)((void *)header +
5924 le16_to_cpu(xe->xe_name_offset) +
5925 OCFS2_XATTR_SIZE(xe->xe_name_len));
5927 vb->vb_xv = xv;
5928 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
5930 ret = ocfs2_xattr_value_attach_refcount(inode, xv, &et,
5931 ref_ci, ref_root_bh,
5932 dealloc, NULL);
5933 if (ret) {
5934 mlog_errno(ret);
5935 break;
5939 return ret;
5942 static int ocfs2_xattr_inline_attach_refcount(struct inode *inode,
5943 struct buffer_head *fe_bh,
5944 struct ocfs2_caching_info *ref_ci,
5945 struct buffer_head *ref_root_bh,
5946 struct ocfs2_cached_dealloc_ctxt *dealloc)
5948 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
5949 struct ocfs2_xattr_header *header = (struct ocfs2_xattr_header *)
5950 (fe_bh->b_data + inode->i_sb->s_blocksize -
5951 le16_to_cpu(di->i_xattr_inline_size));
5952 struct ocfs2_xattr_value_buf vb = {
5953 .vb_bh = fe_bh,
5954 .vb_access = ocfs2_journal_access_di,
5957 return ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
5958 ref_ci, ref_root_bh, dealloc);
5961 struct ocfs2_xattr_tree_value_refcount_para {
5962 struct ocfs2_caching_info *ref_ci;
5963 struct buffer_head *ref_root_bh;
5964 struct ocfs2_cached_dealloc_ctxt *dealloc;
5967 static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
5968 struct ocfs2_xattr_bucket *bucket,
5969 int offset,
5970 struct ocfs2_xattr_value_root **xv,
5971 struct buffer_head **bh)
5973 int ret, block_off, name_offset;
5974 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5975 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
5976 void *base;
5978 ret = ocfs2_xattr_bucket_get_name_value(sb,
5979 bucket_xh(bucket),
5980 offset,
5981 &block_off,
5982 &name_offset);
5983 if (ret) {
5984 mlog_errno(ret);
5985 goto out;
5988 base = bucket_block(bucket, block_off);
5990 *xv = (struct ocfs2_xattr_value_root *)(base + name_offset +
5991 OCFS2_XATTR_SIZE(xe->xe_name_len));
5993 if (bh)
5994 *bh = bucket->bu_bhs[block_off];
5995 out:
5996 return ret;
6000 * For a given xattr bucket, refcount all the entries which
6001 * have value stored outside.
6003 static int ocfs2_xattr_bucket_value_refcount(struct inode *inode,
6004 struct ocfs2_xattr_bucket *bucket,
6005 void *para)
6007 int i, ret = 0;
6008 struct ocfs2_extent_tree et;
6009 struct ocfs2_xattr_tree_value_refcount_para *ref =
6010 (struct ocfs2_xattr_tree_value_refcount_para *)para;
6011 struct ocfs2_xattr_header *xh =
6012 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6013 struct ocfs2_xattr_entry *xe;
6014 struct ocfs2_xattr_value_buf vb = {
6015 .vb_access = ocfs2_journal_access,
6017 struct ocfs2_post_refcount refcount = {
6018 .credits = bucket->bu_blocks,
6019 .para = bucket,
6020 .func = ocfs2_xattr_bucket_post_refcount,
6022 struct ocfs2_post_refcount *p = NULL;
6024 /* We only need post_refcount if we support metaecc. */
6025 if (ocfs2_meta_ecc(OCFS2_SB(inode->i_sb)))
6026 p = &refcount;
6028 mlog(0, "refcount bucket %llu, count = %u\n",
6029 (unsigned long long)bucket_blkno(bucket),
6030 le16_to_cpu(xh->xh_count));
6031 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6032 xe = &xh->xh_entries[i];
6034 if (ocfs2_xattr_is_local(xe))
6035 continue;
6037 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket, i,
6038 &vb.vb_xv, &vb.vb_bh);
6039 if (ret) {
6040 mlog_errno(ret);
6041 break;
6044 ocfs2_init_xattr_value_extent_tree(&et,
6045 INODE_CACHE(inode), &vb);
6047 ret = ocfs2_xattr_value_attach_refcount(inode, vb.vb_xv,
6048 &et, ref->ref_ci,
6049 ref->ref_root_bh,
6050 ref->dealloc, p);
6051 if (ret) {
6052 mlog_errno(ret);
6053 break;
6057 return ret;
6061 static int ocfs2_refcount_xattr_tree_rec(struct inode *inode,
6062 struct buffer_head *root_bh,
6063 u64 blkno, u32 cpos, u32 len, void *para)
6065 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
6066 ocfs2_xattr_bucket_value_refcount,
6067 para);
6070 static int ocfs2_xattr_block_attach_refcount(struct inode *inode,
6071 struct buffer_head *blk_bh,
6072 struct ocfs2_caching_info *ref_ci,
6073 struct buffer_head *ref_root_bh,
6074 struct ocfs2_cached_dealloc_ctxt *dealloc)
6076 int ret = 0;
6077 struct ocfs2_xattr_block *xb =
6078 (struct ocfs2_xattr_block *)blk_bh->b_data;
6080 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
6081 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
6082 struct ocfs2_xattr_value_buf vb = {
6083 .vb_bh = blk_bh,
6084 .vb_access = ocfs2_journal_access_xb,
6087 ret = ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
6088 ref_ci, ref_root_bh,
6089 dealloc);
6090 } else {
6091 struct ocfs2_xattr_tree_value_refcount_para para = {
6092 .ref_ci = ref_ci,
6093 .ref_root_bh = ref_root_bh,
6094 .dealloc = dealloc,
6097 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
6098 ocfs2_refcount_xattr_tree_rec,
6099 &para);
6102 return ret;
6105 int ocfs2_xattr_attach_refcount_tree(struct inode *inode,
6106 struct buffer_head *fe_bh,
6107 struct ocfs2_caching_info *ref_ci,
6108 struct buffer_head *ref_root_bh,
6109 struct ocfs2_cached_dealloc_ctxt *dealloc)
6111 int ret = 0;
6112 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6113 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
6114 struct buffer_head *blk_bh = NULL;
6116 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
6117 ret = ocfs2_xattr_inline_attach_refcount(inode, fe_bh,
6118 ref_ci, ref_root_bh,
6119 dealloc);
6120 if (ret) {
6121 mlog_errno(ret);
6122 goto out;
6126 if (!di->i_xattr_loc)
6127 goto out;
6129 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
6130 &blk_bh);
6131 if (ret < 0) {
6132 mlog_errno(ret);
6133 goto out;
6136 ret = ocfs2_xattr_block_attach_refcount(inode, blk_bh, ref_ci,
6137 ref_root_bh, dealloc);
6138 if (ret)
6139 mlog_errno(ret);
6141 brelse(blk_bh);
6142 out:
6144 return ret;
6147 typedef int (should_xattr_reflinked)(struct ocfs2_xattr_entry *xe);
6149 * Store the information we need in xattr reflink.
6150 * old_bh and new_bh are inode bh for the old and new inode.
6152 struct ocfs2_xattr_reflink {
6153 struct inode *old_inode;
6154 struct inode *new_inode;
6155 struct buffer_head *old_bh;
6156 struct buffer_head *new_bh;
6157 struct ocfs2_caching_info *ref_ci;
6158 struct buffer_head *ref_root_bh;
6159 struct ocfs2_cached_dealloc_ctxt *dealloc;
6160 should_xattr_reflinked *xattr_reflinked;
6164 * Given a xattr header and xe offset,
6165 * return the proper xv and the corresponding bh.
6166 * xattr in inode, block and xattr tree have different implementaions.
6168 typedef int (get_xattr_value_root)(struct super_block *sb,
6169 struct buffer_head *bh,
6170 struct ocfs2_xattr_header *xh,
6171 int offset,
6172 struct ocfs2_xattr_value_root **xv,
6173 struct buffer_head **ret_bh,
6174 void *para);
6177 * Calculate all the xattr value root metadata stored in this xattr header and
6178 * credits we need if we create them from the scratch.
6179 * We use get_xattr_value_root so that all types of xattr container can use it.
6181 static int ocfs2_value_metas_in_xattr_header(struct super_block *sb,
6182 struct buffer_head *bh,
6183 struct ocfs2_xattr_header *xh,
6184 int *metas, int *credits,
6185 int *num_recs,
6186 get_xattr_value_root *func,
6187 void *para)
6189 int i, ret = 0;
6190 struct ocfs2_xattr_value_root *xv;
6191 struct ocfs2_xattr_entry *xe;
6193 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6194 xe = &xh->xh_entries[i];
6195 if (ocfs2_xattr_is_local(xe))
6196 continue;
6198 ret = func(sb, bh, xh, i, &xv, NULL, para);
6199 if (ret) {
6200 mlog_errno(ret);
6201 break;
6204 *metas += le16_to_cpu(xv->xr_list.l_tree_depth) *
6205 le16_to_cpu(xv->xr_list.l_next_free_rec);
6207 *credits += ocfs2_calc_extend_credits(sb,
6208 &def_xv.xv.xr_list,
6209 le32_to_cpu(xv->xr_clusters));
6212 * If the value is a tree with depth > 1, We don't go deep
6213 * to the extent block, so just calculate a maximum record num.
6215 if (!xv->xr_list.l_tree_depth)
6216 *num_recs += le16_to_cpu(xv->xr_list.l_next_free_rec);
6217 else
6218 *num_recs += ocfs2_clusters_for_bytes(sb,
6219 XATTR_SIZE_MAX);
6222 return ret;
6225 /* Used by xattr inode and block to return the right xv and buffer_head. */
6226 static int ocfs2_get_xattr_value_root(struct super_block *sb,
6227 struct buffer_head *bh,
6228 struct ocfs2_xattr_header *xh,
6229 int offset,
6230 struct ocfs2_xattr_value_root **xv,
6231 struct buffer_head **ret_bh,
6232 void *para)
6234 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
6236 *xv = (struct ocfs2_xattr_value_root *)((void *)xh +
6237 le16_to_cpu(xe->xe_name_offset) +
6238 OCFS2_XATTR_SIZE(xe->xe_name_len));
6240 if (ret_bh)
6241 *ret_bh = bh;
6243 return 0;
6247 * Lock the meta_ac and caculate how much credits we need for reflink xattrs.
6248 * It is only used for inline xattr and xattr block.
6250 static int ocfs2_reflink_lock_xattr_allocators(struct ocfs2_super *osb,
6251 struct ocfs2_xattr_header *xh,
6252 struct buffer_head *ref_root_bh,
6253 int *credits,
6254 struct ocfs2_alloc_context **meta_ac)
6256 int ret, meta_add = 0, num_recs = 0;
6257 struct ocfs2_refcount_block *rb =
6258 (struct ocfs2_refcount_block *)ref_root_bh->b_data;
6260 *credits = 0;
6262 ret = ocfs2_value_metas_in_xattr_header(osb->sb, NULL, xh,
6263 &meta_add, credits, &num_recs,
6264 ocfs2_get_xattr_value_root,
6265 NULL);
6266 if (ret) {
6267 mlog_errno(ret);
6268 goto out;
6272 * We need to add/modify num_recs in refcount tree, so just calculate
6273 * an approximate number we need for refcount tree change.
6274 * Sometimes we need to split the tree, and after split, half recs
6275 * will be moved to the new block, and a new block can only provide
6276 * half number of recs. So we multiple new blocks by 2.
6278 num_recs = num_recs / ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6279 meta_add += num_recs;
6280 *credits += num_recs + num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6281 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6282 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6283 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6284 else
6285 *credits += 1;
6287 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add, meta_ac);
6288 if (ret)
6289 mlog_errno(ret);
6291 out:
6292 return ret;
6296 * Given a xattr header, reflink all the xattrs in this container.
6297 * It can be used for inode, block and bucket.
6299 * NOTE:
6300 * Before we call this function, the caller has memcpy the xattr in
6301 * old_xh to the new_xh.
6303 * If args.xattr_reflinked is set, call it to decide whether the xe should
6304 * be reflinked or not. If not, remove it from the new xattr header.
6306 static int ocfs2_reflink_xattr_header(handle_t *handle,
6307 struct ocfs2_xattr_reflink *args,
6308 struct buffer_head *old_bh,
6309 struct ocfs2_xattr_header *xh,
6310 struct buffer_head *new_bh,
6311 struct ocfs2_xattr_header *new_xh,
6312 struct ocfs2_xattr_value_buf *vb,
6313 struct ocfs2_alloc_context *meta_ac,
6314 get_xattr_value_root *func,
6315 void *para)
6317 int ret = 0, i, j;
6318 struct super_block *sb = args->old_inode->i_sb;
6319 struct buffer_head *value_bh;
6320 struct ocfs2_xattr_entry *xe, *last;
6321 struct ocfs2_xattr_value_root *xv, *new_xv;
6322 struct ocfs2_extent_tree data_et;
6323 u32 clusters, cpos, p_cluster, num_clusters;
6324 unsigned int ext_flags = 0;
6326 mlog(0, "reflink xattr in container %llu, count = %u\n",
6327 (unsigned long long)old_bh->b_blocknr, le16_to_cpu(xh->xh_count));
6329 last = &new_xh->xh_entries[le16_to_cpu(new_xh->xh_count)];
6330 for (i = 0, j = 0; i < le16_to_cpu(xh->xh_count); i++, j++) {
6331 xe = &xh->xh_entries[i];
6333 if (args->xattr_reflinked && !args->xattr_reflinked(xe)) {
6334 xe = &new_xh->xh_entries[j];
6336 le16_add_cpu(&new_xh->xh_count, -1);
6337 if (new_xh->xh_count) {
6338 memmove(xe, xe + 1,
6339 (void *)last - (void *)xe);
6340 memset(last, 0,
6341 sizeof(struct ocfs2_xattr_entry));
6345 * We don't want j to increase in the next round since
6346 * it is already moved ahead.
6348 j--;
6349 continue;
6352 if (ocfs2_xattr_is_local(xe))
6353 continue;
6355 ret = func(sb, old_bh, xh, i, &xv, NULL, para);
6356 if (ret) {
6357 mlog_errno(ret);
6358 break;
6361 ret = func(sb, new_bh, new_xh, j, &new_xv, &value_bh, para);
6362 if (ret) {
6363 mlog_errno(ret);
6364 break;
6368 * For the xattr which has l_tree_depth = 0, all the extent
6369 * recs have already be copied to the new xh with the
6370 * propriate OCFS2_EXT_REFCOUNTED flag we just need to
6371 * increase the refount count int the refcount tree.
6373 * For the xattr which has l_tree_depth > 0, we need
6374 * to initialize it to the empty default value root,
6375 * and then insert the extents one by one.
6377 if (xv->xr_list.l_tree_depth) {
6378 memcpy(new_xv, &def_xv, sizeof(def_xv));
6379 vb->vb_xv = new_xv;
6380 vb->vb_bh = value_bh;
6381 ocfs2_init_xattr_value_extent_tree(&data_et,
6382 INODE_CACHE(args->new_inode), vb);
6385 clusters = le32_to_cpu(xv->xr_clusters);
6386 cpos = 0;
6387 while (cpos < clusters) {
6388 ret = ocfs2_xattr_get_clusters(args->old_inode,
6389 cpos,
6390 &p_cluster,
6391 &num_clusters,
6392 &xv->xr_list,
6393 &ext_flags);
6394 if (ret) {
6395 mlog_errno(ret);
6396 goto out;
6399 BUG_ON(!p_cluster);
6401 if (xv->xr_list.l_tree_depth) {
6402 ret = ocfs2_insert_extent(handle,
6403 &data_et, cpos,
6404 ocfs2_clusters_to_blocks(
6405 args->old_inode->i_sb,
6406 p_cluster),
6407 num_clusters, ext_flags,
6408 meta_ac);
6409 if (ret) {
6410 mlog_errno(ret);
6411 goto out;
6415 ret = ocfs2_increase_refcount(handle, args->ref_ci,
6416 args->ref_root_bh,
6417 p_cluster, num_clusters,
6418 meta_ac, args->dealloc);
6419 if (ret) {
6420 mlog_errno(ret);
6421 goto out;
6424 cpos += num_clusters;
6428 out:
6429 return ret;
6432 static int ocfs2_reflink_xattr_inline(struct ocfs2_xattr_reflink *args)
6434 int ret = 0, credits = 0;
6435 handle_t *handle;
6436 struct ocfs2_super *osb = OCFS2_SB(args->old_inode->i_sb);
6437 struct ocfs2_dinode *di = (struct ocfs2_dinode *)args->old_bh->b_data;
6438 int inline_size = le16_to_cpu(di->i_xattr_inline_size);
6439 int header_off = osb->sb->s_blocksize - inline_size;
6440 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)
6441 (args->old_bh->b_data + header_off);
6442 struct ocfs2_xattr_header *new_xh = (struct ocfs2_xattr_header *)
6443 (args->new_bh->b_data + header_off);
6444 struct ocfs2_alloc_context *meta_ac = NULL;
6445 struct ocfs2_inode_info *new_oi;
6446 struct ocfs2_dinode *new_di;
6447 struct ocfs2_xattr_value_buf vb = {
6448 .vb_bh = args->new_bh,
6449 .vb_access = ocfs2_journal_access_di,
6452 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6453 &credits, &meta_ac);
6454 if (ret) {
6455 mlog_errno(ret);
6456 goto out;
6459 handle = ocfs2_start_trans(osb, credits);
6460 if (IS_ERR(handle)) {
6461 ret = PTR_ERR(handle);
6462 mlog_errno(ret);
6463 goto out;
6466 ret = ocfs2_journal_access_di(handle, INODE_CACHE(args->new_inode),
6467 args->new_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6468 if (ret) {
6469 mlog_errno(ret);
6470 goto out_commit;
6473 memcpy(args->new_bh->b_data + header_off,
6474 args->old_bh->b_data + header_off, inline_size);
6476 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6477 new_di->i_xattr_inline_size = cpu_to_le16(inline_size);
6479 ret = ocfs2_reflink_xattr_header(handle, args, args->old_bh, xh,
6480 args->new_bh, new_xh, &vb, meta_ac,
6481 ocfs2_get_xattr_value_root, NULL);
6482 if (ret) {
6483 mlog_errno(ret);
6484 goto out_commit;
6487 new_oi = OCFS2_I(args->new_inode);
6488 spin_lock(&new_oi->ip_lock);
6489 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL | OCFS2_INLINE_XATTR_FL;
6490 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6491 spin_unlock(&new_oi->ip_lock);
6493 ocfs2_journal_dirty(handle, args->new_bh);
6495 out_commit:
6496 ocfs2_commit_trans(osb, handle);
6498 out:
6499 if (meta_ac)
6500 ocfs2_free_alloc_context(meta_ac);
6501 return ret;
6504 static int ocfs2_create_empty_xattr_block(struct inode *inode,
6505 struct buffer_head *fe_bh,
6506 struct buffer_head **ret_bh,
6507 int indexed)
6509 int ret;
6510 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6511 struct ocfs2_xattr_set_ctxt ctxt;
6513 memset(&ctxt, 0, sizeof(ctxt));
6514 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &ctxt.meta_ac);
6515 if (ret < 0) {
6516 mlog_errno(ret);
6517 return ret;
6520 ctxt.handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_CREATE_CREDITS);
6521 if (IS_ERR(ctxt.handle)) {
6522 ret = PTR_ERR(ctxt.handle);
6523 mlog_errno(ret);
6524 goto out;
6527 mlog(0, "create new xattr block for inode %llu, index = %d\n",
6528 (unsigned long long)fe_bh->b_blocknr, indexed);
6529 ret = ocfs2_create_xattr_block(inode, fe_bh, &ctxt, indexed,
6530 ret_bh);
6531 if (ret)
6532 mlog_errno(ret);
6534 ocfs2_commit_trans(osb, ctxt.handle);
6535 out:
6536 ocfs2_free_alloc_context(ctxt.meta_ac);
6537 return ret;
6540 static int ocfs2_reflink_xattr_block(struct ocfs2_xattr_reflink *args,
6541 struct buffer_head *blk_bh,
6542 struct buffer_head *new_blk_bh)
6544 int ret = 0, credits = 0;
6545 handle_t *handle;
6546 struct ocfs2_inode_info *new_oi = OCFS2_I(args->new_inode);
6547 struct ocfs2_dinode *new_di;
6548 struct ocfs2_super *osb = OCFS2_SB(args->new_inode->i_sb);
6549 int header_off = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
6550 struct ocfs2_xattr_block *xb =
6551 (struct ocfs2_xattr_block *)blk_bh->b_data;
6552 struct ocfs2_xattr_header *xh = &xb->xb_attrs.xb_header;
6553 struct ocfs2_xattr_block *new_xb =
6554 (struct ocfs2_xattr_block *)new_blk_bh->b_data;
6555 struct ocfs2_xattr_header *new_xh = &new_xb->xb_attrs.xb_header;
6556 struct ocfs2_alloc_context *meta_ac;
6557 struct ocfs2_xattr_value_buf vb = {
6558 .vb_bh = new_blk_bh,
6559 .vb_access = ocfs2_journal_access_xb,
6562 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6563 &credits, &meta_ac);
6564 if (ret) {
6565 mlog_errno(ret);
6566 return ret;
6569 /* One more credits in case we need to add xattr flags in new inode. */
6570 handle = ocfs2_start_trans(osb, credits + 1);
6571 if (IS_ERR(handle)) {
6572 ret = PTR_ERR(handle);
6573 mlog_errno(ret);
6574 goto out;
6577 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6578 ret = ocfs2_journal_access_di(handle,
6579 INODE_CACHE(args->new_inode),
6580 args->new_bh,
6581 OCFS2_JOURNAL_ACCESS_WRITE);
6582 if (ret) {
6583 mlog_errno(ret);
6584 goto out_commit;
6588 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(args->new_inode),
6589 new_blk_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6590 if (ret) {
6591 mlog_errno(ret);
6592 goto out_commit;
6595 memcpy(new_blk_bh->b_data + header_off, blk_bh->b_data + header_off,
6596 osb->sb->s_blocksize - header_off);
6598 ret = ocfs2_reflink_xattr_header(handle, args, blk_bh, xh,
6599 new_blk_bh, new_xh, &vb, meta_ac,
6600 ocfs2_get_xattr_value_root, NULL);
6601 if (ret) {
6602 mlog_errno(ret);
6603 goto out_commit;
6606 ocfs2_journal_dirty(handle, new_blk_bh);
6608 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6609 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6610 spin_lock(&new_oi->ip_lock);
6611 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
6612 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6613 spin_unlock(&new_oi->ip_lock);
6615 ocfs2_journal_dirty(handle, args->new_bh);
6618 out_commit:
6619 ocfs2_commit_trans(osb, handle);
6621 out:
6622 ocfs2_free_alloc_context(meta_ac);
6623 return ret;
6626 struct ocfs2_reflink_xattr_tree_args {
6627 struct ocfs2_xattr_reflink *reflink;
6628 struct buffer_head *old_blk_bh;
6629 struct buffer_head *new_blk_bh;
6630 struct ocfs2_xattr_bucket *old_bucket;
6631 struct ocfs2_xattr_bucket *new_bucket;
6635 * NOTE:
6636 * We have to handle the case that both old bucket and new bucket
6637 * will call this function to get the right ret_bh.
6638 * So The caller must give us the right bh.
6640 static int ocfs2_get_reflink_xattr_value_root(struct super_block *sb,
6641 struct buffer_head *bh,
6642 struct ocfs2_xattr_header *xh,
6643 int offset,
6644 struct ocfs2_xattr_value_root **xv,
6645 struct buffer_head **ret_bh,
6646 void *para)
6648 struct ocfs2_reflink_xattr_tree_args *args =
6649 (struct ocfs2_reflink_xattr_tree_args *)para;
6650 struct ocfs2_xattr_bucket *bucket;
6652 if (bh == args->old_bucket->bu_bhs[0])
6653 bucket = args->old_bucket;
6654 else
6655 bucket = args->new_bucket;
6657 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6658 xv, ret_bh);
6661 struct ocfs2_value_tree_metas {
6662 int num_metas;
6663 int credits;
6664 int num_recs;
6667 static int ocfs2_value_tree_metas_in_bucket(struct super_block *sb,
6668 struct buffer_head *bh,
6669 struct ocfs2_xattr_header *xh,
6670 int offset,
6671 struct ocfs2_xattr_value_root **xv,
6672 struct buffer_head **ret_bh,
6673 void *para)
6675 struct ocfs2_xattr_bucket *bucket =
6676 (struct ocfs2_xattr_bucket *)para;
6678 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6679 xv, ret_bh);
6682 static int ocfs2_calc_value_tree_metas(struct inode *inode,
6683 struct ocfs2_xattr_bucket *bucket,
6684 void *para)
6686 struct ocfs2_value_tree_metas *metas =
6687 (struct ocfs2_value_tree_metas *)para;
6688 struct ocfs2_xattr_header *xh =
6689 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6691 /* Add the credits for this bucket first. */
6692 metas->credits += bucket->bu_blocks;
6693 return ocfs2_value_metas_in_xattr_header(inode->i_sb, bucket->bu_bhs[0],
6694 xh, &metas->num_metas,
6695 &metas->credits, &metas->num_recs,
6696 ocfs2_value_tree_metas_in_bucket,
6697 bucket);
6701 * Given a xattr extent rec starting from blkno and having len clusters,
6702 * iterate all the buckets calculate how much metadata we need for reflinking
6703 * all the ocfs2_xattr_value_root and lock the allocators accordingly.
6705 static int ocfs2_lock_reflink_xattr_rec_allocators(
6706 struct ocfs2_reflink_xattr_tree_args *args,
6707 struct ocfs2_extent_tree *xt_et,
6708 u64 blkno, u32 len, int *credits,
6709 struct ocfs2_alloc_context **meta_ac,
6710 struct ocfs2_alloc_context **data_ac)
6712 int ret, num_free_extents;
6713 struct ocfs2_value_tree_metas metas;
6714 struct ocfs2_super *osb = OCFS2_SB(args->reflink->old_inode->i_sb);
6715 struct ocfs2_refcount_block *rb;
6717 memset(&metas, 0, sizeof(metas));
6719 ret = ocfs2_iterate_xattr_buckets(args->reflink->old_inode, blkno, len,
6720 ocfs2_calc_value_tree_metas, &metas);
6721 if (ret) {
6722 mlog_errno(ret);
6723 goto out;
6726 *credits = metas.credits;
6729 * Calculate we need for refcount tree change.
6731 * We need to add/modify num_recs in refcount tree, so just calculate
6732 * an approximate number we need for refcount tree change.
6733 * Sometimes we need to split the tree, and after split, half recs
6734 * will be moved to the new block, and a new block can only provide
6735 * half number of recs. So we multiple new blocks by 2.
6736 * In the end, we have to add credits for modifying the already
6737 * existed refcount block.
6739 rb = (struct ocfs2_refcount_block *)args->reflink->ref_root_bh->b_data;
6740 metas.num_recs =
6741 (metas.num_recs + ocfs2_refcount_recs_per_rb(osb->sb) - 1) /
6742 ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6743 metas.num_metas += metas.num_recs;
6744 *credits += metas.num_recs +
6745 metas.num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6746 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6747 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6748 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6749 else
6750 *credits += 1;
6752 /* count in the xattr tree change. */
6753 num_free_extents = ocfs2_num_free_extents(osb, xt_et);
6754 if (num_free_extents < 0) {
6755 ret = num_free_extents;
6756 mlog_errno(ret);
6757 goto out;
6760 if (num_free_extents < len)
6761 metas.num_metas += ocfs2_extend_meta_needed(xt_et->et_root_el);
6763 *credits += ocfs2_calc_extend_credits(osb->sb,
6764 xt_et->et_root_el, len);
6766 if (metas.num_metas) {
6767 ret = ocfs2_reserve_new_metadata_blocks(osb, metas.num_metas,
6768 meta_ac);
6769 if (ret) {
6770 mlog_errno(ret);
6771 goto out;
6775 if (len) {
6776 ret = ocfs2_reserve_clusters(osb, len, data_ac);
6777 if (ret)
6778 mlog_errno(ret);
6780 out:
6781 if (ret) {
6782 if (*meta_ac) {
6783 ocfs2_free_alloc_context(*meta_ac);
6784 meta_ac = NULL;
6788 return ret;
6791 static int ocfs2_reflink_xattr_buckets(handle_t *handle,
6792 u64 blkno, u64 new_blkno, u32 clusters,
6793 struct ocfs2_alloc_context *meta_ac,
6794 struct ocfs2_alloc_context *data_ac,
6795 struct ocfs2_reflink_xattr_tree_args *args)
6797 int i, j, ret = 0;
6798 struct super_block *sb = args->reflink->old_inode->i_sb;
6799 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
6800 u32 num_buckets = clusters * bpc;
6801 int bpb = args->old_bucket->bu_blocks;
6802 struct ocfs2_xattr_value_buf vb = {
6803 .vb_access = ocfs2_journal_access,
6806 for (i = 0; i < num_buckets; i++, blkno += bpb, new_blkno += bpb) {
6807 ret = ocfs2_read_xattr_bucket(args->old_bucket, blkno);
6808 if (ret) {
6809 mlog_errno(ret);
6810 break;
6813 ret = ocfs2_init_xattr_bucket(args->new_bucket, new_blkno);
6814 if (ret) {
6815 mlog_errno(ret);
6816 break;
6820 * The real bucket num in this series of blocks is stored
6821 * in the 1st bucket.
6823 if (i == 0)
6824 num_buckets = le16_to_cpu(
6825 bucket_xh(args->old_bucket)->xh_num_buckets);
6827 ret = ocfs2_xattr_bucket_journal_access(handle,
6828 args->new_bucket,
6829 OCFS2_JOURNAL_ACCESS_CREATE);
6830 if (ret) {
6831 mlog_errno(ret);
6832 break;
6835 for (j = 0; j < bpb; j++)
6836 memcpy(bucket_block(args->new_bucket, j),
6837 bucket_block(args->old_bucket, j),
6838 sb->s_blocksize);
6840 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
6842 ret = ocfs2_reflink_xattr_header(handle, args->reflink,
6843 args->old_bucket->bu_bhs[0],
6844 bucket_xh(args->old_bucket),
6845 args->new_bucket->bu_bhs[0],
6846 bucket_xh(args->new_bucket),
6847 &vb, meta_ac,
6848 ocfs2_get_reflink_xattr_value_root,
6849 args);
6850 if (ret) {
6851 mlog_errno(ret);
6852 break;
6856 * Re-access and dirty the bucket to calculate metaecc.
6857 * Because we may extend the transaction in reflink_xattr_header
6858 * which will let the already accessed block gone.
6860 ret = ocfs2_xattr_bucket_journal_access(handle,
6861 args->new_bucket,
6862 OCFS2_JOURNAL_ACCESS_WRITE);
6863 if (ret) {
6864 mlog_errno(ret);
6865 break;
6868 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
6869 ocfs2_xattr_bucket_relse(args->old_bucket);
6870 ocfs2_xattr_bucket_relse(args->new_bucket);
6873 ocfs2_xattr_bucket_relse(args->old_bucket);
6874 ocfs2_xattr_bucket_relse(args->new_bucket);
6875 return ret;
6878 * Create the same xattr extent record in the new inode's xattr tree.
6880 static int ocfs2_reflink_xattr_rec(struct inode *inode,
6881 struct buffer_head *root_bh,
6882 u64 blkno,
6883 u32 cpos,
6884 u32 len,
6885 void *para)
6887 int ret, credits = 0;
6888 u32 p_cluster, num_clusters;
6889 u64 new_blkno;
6890 handle_t *handle;
6891 struct ocfs2_reflink_xattr_tree_args *args =
6892 (struct ocfs2_reflink_xattr_tree_args *)para;
6893 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6894 struct ocfs2_alloc_context *meta_ac = NULL;
6895 struct ocfs2_alloc_context *data_ac = NULL;
6896 struct ocfs2_extent_tree et;
6898 ocfs2_init_xattr_tree_extent_tree(&et,
6899 INODE_CACHE(args->reflink->new_inode),
6900 args->new_blk_bh);
6902 ret = ocfs2_lock_reflink_xattr_rec_allocators(args, &et, blkno,
6903 len, &credits,
6904 &meta_ac, &data_ac);
6905 if (ret) {
6906 mlog_errno(ret);
6907 goto out;
6910 handle = ocfs2_start_trans(osb, credits);
6911 if (IS_ERR(handle)) {
6912 ret = PTR_ERR(handle);
6913 mlog_errno(ret);
6914 goto out;
6917 ret = ocfs2_claim_clusters(handle, data_ac,
6918 len, &p_cluster, &num_clusters);
6919 if (ret) {
6920 mlog_errno(ret);
6921 goto out_commit;
6924 new_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cluster);
6926 mlog(0, "reflink xattr buckets %llu to %llu, len %u\n",
6927 (unsigned long long)blkno, (unsigned long long)new_blkno, len);
6928 ret = ocfs2_reflink_xattr_buckets(handle, blkno, new_blkno, len,
6929 meta_ac, data_ac, args);
6930 if (ret) {
6931 mlog_errno(ret);
6932 goto out_commit;
6935 mlog(0, "insert new xattr extent rec start %llu len %u to %u\n",
6936 (unsigned long long)new_blkno, len, cpos);
6937 ret = ocfs2_insert_extent(handle, &et, cpos, new_blkno,
6938 len, 0, meta_ac);
6939 if (ret)
6940 mlog_errno(ret);
6942 out_commit:
6943 ocfs2_commit_trans(osb, handle);
6945 out:
6946 if (meta_ac)
6947 ocfs2_free_alloc_context(meta_ac);
6948 if (data_ac)
6949 ocfs2_free_alloc_context(data_ac);
6950 return ret;
6954 * Create reflinked xattr buckets.
6955 * We will add bucket one by one, and refcount all the xattrs in the bucket
6956 * if they are stored outside.
6958 static int ocfs2_reflink_xattr_tree(struct ocfs2_xattr_reflink *args,
6959 struct buffer_head *blk_bh,
6960 struct buffer_head *new_blk_bh)
6962 int ret;
6963 struct ocfs2_reflink_xattr_tree_args para;
6965 memset(&para, 0, sizeof(para));
6966 para.reflink = args;
6967 para.old_blk_bh = blk_bh;
6968 para.new_blk_bh = new_blk_bh;
6970 para.old_bucket = ocfs2_xattr_bucket_new(args->old_inode);
6971 if (!para.old_bucket) {
6972 mlog_errno(-ENOMEM);
6973 return -ENOMEM;
6976 para.new_bucket = ocfs2_xattr_bucket_new(args->new_inode);
6977 if (!para.new_bucket) {
6978 ret = -ENOMEM;
6979 mlog_errno(ret);
6980 goto out;
6983 ret = ocfs2_iterate_xattr_index_block(args->old_inode, blk_bh,
6984 ocfs2_reflink_xattr_rec,
6985 &para);
6986 if (ret)
6987 mlog_errno(ret);
6989 out:
6990 ocfs2_xattr_bucket_free(para.old_bucket);
6991 ocfs2_xattr_bucket_free(para.new_bucket);
6992 return ret;
6995 static int ocfs2_reflink_xattr_in_block(struct ocfs2_xattr_reflink *args,
6996 struct buffer_head *blk_bh)
6998 int ret, indexed = 0;
6999 struct buffer_head *new_blk_bh = NULL;
7000 struct ocfs2_xattr_block *xb =
7001 (struct ocfs2_xattr_block *)blk_bh->b_data;
7004 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)
7005 indexed = 1;
7007 ret = ocfs2_create_empty_xattr_block(args->new_inode, args->new_bh,
7008 &new_blk_bh, indexed);
7009 if (ret) {
7010 mlog_errno(ret);
7011 goto out;
7014 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED))
7015 ret = ocfs2_reflink_xattr_block(args, blk_bh, new_blk_bh);
7016 else
7017 ret = ocfs2_reflink_xattr_tree(args, blk_bh, new_blk_bh);
7018 if (ret)
7019 mlog_errno(ret);
7021 out:
7022 brelse(new_blk_bh);
7023 return ret;
7026 static int ocfs2_reflink_xattr_no_security(struct ocfs2_xattr_entry *xe)
7028 int type = ocfs2_xattr_get_type(xe);
7030 return type != OCFS2_XATTR_INDEX_SECURITY &&
7031 type != OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS &&
7032 type != OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
7035 int ocfs2_reflink_xattrs(struct inode *old_inode,
7036 struct buffer_head *old_bh,
7037 struct inode *new_inode,
7038 struct buffer_head *new_bh,
7039 bool preserve_security)
7041 int ret;
7042 struct ocfs2_xattr_reflink args;
7043 struct ocfs2_inode_info *oi = OCFS2_I(old_inode);
7044 struct ocfs2_dinode *di = (struct ocfs2_dinode *)old_bh->b_data;
7045 struct buffer_head *blk_bh = NULL;
7046 struct ocfs2_cached_dealloc_ctxt dealloc;
7047 struct ocfs2_refcount_tree *ref_tree;
7048 struct buffer_head *ref_root_bh = NULL;
7050 ret = ocfs2_lock_refcount_tree(OCFS2_SB(old_inode->i_sb),
7051 le64_to_cpu(di->i_refcount_loc),
7052 1, &ref_tree, &ref_root_bh);
7053 if (ret) {
7054 mlog_errno(ret);
7055 goto out;
7058 ocfs2_init_dealloc_ctxt(&dealloc);
7060 args.old_inode = old_inode;
7061 args.new_inode = new_inode;
7062 args.old_bh = old_bh;
7063 args.new_bh = new_bh;
7064 args.ref_ci = &ref_tree->rf_ci;
7065 args.ref_root_bh = ref_root_bh;
7066 args.dealloc = &dealloc;
7067 if (preserve_security)
7068 args.xattr_reflinked = NULL;
7069 else
7070 args.xattr_reflinked = ocfs2_reflink_xattr_no_security;
7072 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
7073 ret = ocfs2_reflink_xattr_inline(&args);
7074 if (ret) {
7075 mlog_errno(ret);
7076 goto out_unlock;
7080 if (!di->i_xattr_loc)
7081 goto out_unlock;
7083 ret = ocfs2_read_xattr_block(old_inode, le64_to_cpu(di->i_xattr_loc),
7084 &blk_bh);
7085 if (ret < 0) {
7086 mlog_errno(ret);
7087 goto out_unlock;
7090 ret = ocfs2_reflink_xattr_in_block(&args, blk_bh);
7091 if (ret)
7092 mlog_errno(ret);
7094 brelse(blk_bh);
7096 out_unlock:
7097 ocfs2_unlock_refcount_tree(OCFS2_SB(old_inode->i_sb),
7098 ref_tree, 1);
7099 brelse(ref_root_bh);
7101 if (ocfs2_dealloc_has_cluster(&dealloc)) {
7102 ocfs2_schedule_truncate_log_flush(OCFS2_SB(old_inode->i_sb), 1);
7103 ocfs2_run_deallocs(OCFS2_SB(old_inode->i_sb), &dealloc);
7106 out:
7107 return ret;
7111 * Initialize security and acl for a already created inode.
7112 * Used for reflink a non-preserve-security file.
7114 * It uses common api like ocfs2_xattr_set, so the caller
7115 * must not hold any lock expect i_mutex.
7117 int ocfs2_init_security_and_acl(struct inode *dir,
7118 struct inode *inode)
7120 int ret = 0;
7121 struct buffer_head *dir_bh = NULL;
7122 struct ocfs2_security_xattr_info si = {
7123 .enable = 1,
7126 ret = ocfs2_init_security_get(inode, dir, &si);
7127 if (!ret) {
7128 ret = ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY,
7129 si.name, si.value, si.value_len,
7130 XATTR_CREATE);
7131 if (ret) {
7132 mlog_errno(ret);
7133 goto leave;
7135 } else if (ret != -EOPNOTSUPP) {
7136 mlog_errno(ret);
7137 goto leave;
7140 ret = ocfs2_inode_lock(dir, &dir_bh, 0);
7141 if (ret) {
7142 mlog_errno(ret);
7143 goto leave;
7146 ret = ocfs2_init_acl(NULL, inode, dir, NULL, dir_bh, NULL, NULL);
7147 if (ret)
7148 mlog_errno(ret);
7150 ocfs2_inode_unlock(dir, 0);
7151 brelse(dir_bh);
7152 leave:
7153 return ret;
7156 * 'security' attributes support
7158 static size_t ocfs2_xattr_security_list(struct dentry *dentry, char *list,
7159 size_t list_size, const char *name,
7160 size_t name_len, int type)
7162 const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN;
7163 const size_t total_len = prefix_len + name_len + 1;
7165 if (list && total_len <= list_size) {
7166 memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
7167 memcpy(list + prefix_len, name, name_len);
7168 list[prefix_len + name_len] = '\0';
7170 return total_len;
7173 static int ocfs2_xattr_security_get(struct dentry *dentry, const char *name,
7174 void *buffer, size_t size, int type)
7176 if (strcmp(name, "") == 0)
7177 return -EINVAL;
7178 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
7179 name, buffer, size);
7182 static int ocfs2_xattr_security_set(struct dentry *dentry, const char *name,
7183 const void *value, size_t size, int flags, int type)
7185 if (strcmp(name, "") == 0)
7186 return -EINVAL;
7188 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
7189 name, value, size, flags);
7192 int ocfs2_init_security_get(struct inode *inode,
7193 struct inode *dir,
7194 struct ocfs2_security_xattr_info *si)
7196 /* check whether ocfs2 support feature xattr */
7197 if (!ocfs2_supports_xattr(OCFS2_SB(dir->i_sb)))
7198 return -EOPNOTSUPP;
7199 return security_inode_init_security(inode, dir, &si->name, &si->value,
7200 &si->value_len);
7203 int ocfs2_init_security_set(handle_t *handle,
7204 struct inode *inode,
7205 struct buffer_head *di_bh,
7206 struct ocfs2_security_xattr_info *si,
7207 struct ocfs2_alloc_context *xattr_ac,
7208 struct ocfs2_alloc_context *data_ac)
7210 return ocfs2_xattr_set_handle(handle, inode, di_bh,
7211 OCFS2_XATTR_INDEX_SECURITY,
7212 si->name, si->value, si->value_len, 0,
7213 xattr_ac, data_ac);
7216 const struct xattr_handler ocfs2_xattr_security_handler = {
7217 .prefix = XATTR_SECURITY_PREFIX,
7218 .list = ocfs2_xattr_security_list,
7219 .get = ocfs2_xattr_security_get,
7220 .set = ocfs2_xattr_security_set,
7224 * 'trusted' attributes support
7226 static size_t ocfs2_xattr_trusted_list(struct dentry *dentry, char *list,
7227 size_t list_size, const char *name,
7228 size_t name_len, int type)
7230 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
7231 const size_t total_len = prefix_len + name_len + 1;
7233 if (list && total_len <= list_size) {
7234 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
7235 memcpy(list + prefix_len, name, name_len);
7236 list[prefix_len + name_len] = '\0';
7238 return total_len;
7241 static int ocfs2_xattr_trusted_get(struct dentry *dentry, const char *name,
7242 void *buffer, size_t size, int type)
7244 if (strcmp(name, "") == 0)
7245 return -EINVAL;
7246 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
7247 name, buffer, size);
7250 static int ocfs2_xattr_trusted_set(struct dentry *dentry, const char *name,
7251 const void *value, size_t size, int flags, int type)
7253 if (strcmp(name, "") == 0)
7254 return -EINVAL;
7256 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
7257 name, value, size, flags);
7260 const struct xattr_handler ocfs2_xattr_trusted_handler = {
7261 .prefix = XATTR_TRUSTED_PREFIX,
7262 .list = ocfs2_xattr_trusted_list,
7263 .get = ocfs2_xattr_trusted_get,
7264 .set = ocfs2_xattr_trusted_set,
7268 * 'user' attributes support
7270 static size_t ocfs2_xattr_user_list(struct dentry *dentry, char *list,
7271 size_t list_size, const char *name,
7272 size_t name_len, int type)
7274 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
7275 const size_t total_len = prefix_len + name_len + 1;
7276 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
7278 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7279 return 0;
7281 if (list && total_len <= list_size) {
7282 memcpy(list, XATTR_USER_PREFIX, prefix_len);
7283 memcpy(list + prefix_len, name, name_len);
7284 list[prefix_len + name_len] = '\0';
7286 return total_len;
7289 static int ocfs2_xattr_user_get(struct dentry *dentry, const char *name,
7290 void *buffer, size_t size, int type)
7292 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
7294 if (strcmp(name, "") == 0)
7295 return -EINVAL;
7296 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7297 return -EOPNOTSUPP;
7298 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_USER, name,
7299 buffer, size);
7302 static int ocfs2_xattr_user_set(struct dentry *dentry, const char *name,
7303 const void *value, size_t size, int flags, int type)
7305 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
7307 if (strcmp(name, "") == 0)
7308 return -EINVAL;
7309 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7310 return -EOPNOTSUPP;
7312 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_USER,
7313 name, value, size, flags);
7316 const struct xattr_handler ocfs2_xattr_user_handler = {
7317 .prefix = XATTR_USER_PREFIX,
7318 .list = ocfs2_xattr_user_list,
7319 .get = ocfs2_xattr_user_get,
7320 .set = ocfs2_xattr_user_set,