GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / fs / ocfs2 / xattr.c
blob5aacfd0f6efcca82ed935fb0f52cfd9ddbe8158a
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, credits;
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 while (clusters_to_add) {
723 status = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
724 OCFS2_JOURNAL_ACCESS_WRITE);
725 if (status < 0) {
726 mlog_errno(status);
727 break;
730 prev_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
731 status = ocfs2_add_clusters_in_btree(handle,
732 &et,
733 &logical_start,
734 clusters_to_add,
736 ctxt->data_ac,
737 ctxt->meta_ac,
738 &why);
739 if ((status < 0) && (status != -EAGAIN)) {
740 if (status != -ENOSPC)
741 mlog_errno(status);
742 break;
745 ocfs2_journal_dirty(handle, vb->vb_bh);
747 clusters_to_add -= le32_to_cpu(vb->vb_xv->xr_clusters) -
748 prev_clusters;
750 if (why != RESTART_NONE && clusters_to_add) {
752 * We can only fail in case the alloc file doesn't give
753 * up enough clusters.
755 BUG_ON(why == RESTART_META);
757 mlog(0, "restarting xattr value extension for %u"
758 " clusters,.\n", clusters_to_add);
759 credits = ocfs2_calc_extend_credits(inode->i_sb,
760 &vb->vb_xv->xr_list,
761 clusters_to_add);
762 status = ocfs2_extend_trans(handle, credits);
763 if (status < 0) {
764 status = -ENOMEM;
765 mlog_errno(status);
766 break;
771 return status;
774 static int __ocfs2_remove_xattr_range(struct inode *inode,
775 struct ocfs2_xattr_value_buf *vb,
776 u32 cpos, u32 phys_cpos, u32 len,
777 unsigned int ext_flags,
778 struct ocfs2_xattr_set_ctxt *ctxt)
780 int ret;
781 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
782 handle_t *handle = ctxt->handle;
783 struct ocfs2_extent_tree et;
785 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
787 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
788 OCFS2_JOURNAL_ACCESS_WRITE);
789 if (ret) {
790 mlog_errno(ret);
791 goto out;
794 ret = ocfs2_remove_extent(handle, &et, cpos, len, ctxt->meta_ac,
795 &ctxt->dealloc);
796 if (ret) {
797 mlog_errno(ret);
798 goto out;
801 le32_add_cpu(&vb->vb_xv->xr_clusters, -len);
802 ocfs2_journal_dirty(handle, vb->vb_bh);
804 if (ext_flags & OCFS2_EXT_REFCOUNTED)
805 ret = ocfs2_decrease_refcount(inode, handle,
806 ocfs2_blocks_to_clusters(inode->i_sb,
807 phys_blkno),
808 len, ctxt->meta_ac, &ctxt->dealloc, 1);
809 else
810 ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc,
811 phys_blkno, len);
812 if (ret)
813 mlog_errno(ret);
815 out:
816 return ret;
819 static int ocfs2_xattr_shrink_size(struct inode *inode,
820 u32 old_clusters,
821 u32 new_clusters,
822 struct ocfs2_xattr_value_buf *vb,
823 struct ocfs2_xattr_set_ctxt *ctxt)
825 int ret = 0;
826 unsigned int ext_flags;
827 u32 trunc_len, cpos, phys_cpos, alloc_size;
828 u64 block;
830 if (old_clusters <= new_clusters)
831 return 0;
833 cpos = new_clusters;
834 trunc_len = old_clusters - new_clusters;
835 while (trunc_len) {
836 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
837 &alloc_size,
838 &vb->vb_xv->xr_list, &ext_flags);
839 if (ret) {
840 mlog_errno(ret);
841 goto out;
844 if (alloc_size > trunc_len)
845 alloc_size = trunc_len;
847 ret = __ocfs2_remove_xattr_range(inode, vb, cpos,
848 phys_cpos, alloc_size,
849 ext_flags, ctxt);
850 if (ret) {
851 mlog_errno(ret);
852 goto out;
855 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
856 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode),
857 block, alloc_size);
858 cpos += alloc_size;
859 trunc_len -= alloc_size;
862 out:
863 return ret;
866 static int ocfs2_xattr_value_truncate(struct inode *inode,
867 struct ocfs2_xattr_value_buf *vb,
868 int len,
869 struct ocfs2_xattr_set_ctxt *ctxt)
871 int ret;
872 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
873 u32 old_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
875 if (new_clusters == old_clusters)
876 return 0;
878 if (new_clusters > old_clusters)
879 ret = ocfs2_xattr_extend_allocation(inode,
880 new_clusters - old_clusters,
881 vb, ctxt);
882 else
883 ret = ocfs2_xattr_shrink_size(inode,
884 old_clusters, new_clusters,
885 vb, ctxt);
887 return ret;
890 static int ocfs2_xattr_list_entry(char *buffer, size_t size,
891 size_t *result, const char *prefix,
892 const char *name, int name_len)
894 char *p = buffer + *result;
895 int prefix_len = strlen(prefix);
896 int total_len = prefix_len + name_len + 1;
898 *result += total_len;
900 /* we are just looking for how big our buffer needs to be */
901 if (!size)
902 return 0;
904 if (*result > size)
905 return -ERANGE;
907 memcpy(p, prefix, prefix_len);
908 memcpy(p + prefix_len, name, name_len);
909 p[prefix_len + name_len] = '\0';
911 return 0;
914 static int ocfs2_xattr_list_entries(struct inode *inode,
915 struct ocfs2_xattr_header *header,
916 char *buffer, size_t buffer_size)
918 size_t result = 0;
919 int i, type, ret;
920 const char *prefix, *name;
922 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
923 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
924 type = ocfs2_xattr_get_type(entry);
925 prefix = ocfs2_xattr_prefix(type);
927 if (prefix) {
928 name = (const char *)header +
929 le16_to_cpu(entry->xe_name_offset);
931 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
932 &result, prefix, name,
933 entry->xe_name_len);
934 if (ret)
935 return ret;
939 return result;
942 int ocfs2_has_inline_xattr_value_outside(struct inode *inode,
943 struct ocfs2_dinode *di)
945 struct ocfs2_xattr_header *xh;
946 int i;
948 xh = (struct ocfs2_xattr_header *)
949 ((void *)di + inode->i_sb->s_blocksize -
950 le16_to_cpu(di->i_xattr_inline_size));
952 for (i = 0; i < le16_to_cpu(xh->xh_count); i++)
953 if (!ocfs2_xattr_is_local(&xh->xh_entries[i]))
954 return 1;
956 return 0;
959 static int ocfs2_xattr_ibody_list(struct inode *inode,
960 struct ocfs2_dinode *di,
961 char *buffer,
962 size_t buffer_size)
964 struct ocfs2_xattr_header *header = NULL;
965 struct ocfs2_inode_info *oi = OCFS2_I(inode);
966 int ret = 0;
968 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
969 return ret;
971 header = (struct ocfs2_xattr_header *)
972 ((void *)di + inode->i_sb->s_blocksize -
973 le16_to_cpu(di->i_xattr_inline_size));
975 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
977 return ret;
980 static int ocfs2_xattr_block_list(struct inode *inode,
981 struct ocfs2_dinode *di,
982 char *buffer,
983 size_t buffer_size)
985 struct buffer_head *blk_bh = NULL;
986 struct ocfs2_xattr_block *xb;
987 int ret = 0;
989 if (!di->i_xattr_loc)
990 return ret;
992 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
993 &blk_bh);
994 if (ret < 0) {
995 mlog_errno(ret);
996 return ret;
999 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1000 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1001 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
1002 ret = ocfs2_xattr_list_entries(inode, header,
1003 buffer, buffer_size);
1004 } else
1005 ret = ocfs2_xattr_tree_list_index_block(inode, blk_bh,
1006 buffer, buffer_size);
1008 brelse(blk_bh);
1010 return ret;
1013 ssize_t ocfs2_listxattr(struct dentry *dentry,
1014 char *buffer,
1015 size_t size)
1017 int ret = 0, i_ret = 0, b_ret = 0;
1018 struct buffer_head *di_bh = NULL;
1019 struct ocfs2_dinode *di = NULL;
1020 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
1022 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
1023 return -EOPNOTSUPP;
1025 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1026 return ret;
1028 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
1029 if (ret < 0) {
1030 mlog_errno(ret);
1031 return ret;
1034 di = (struct ocfs2_dinode *)di_bh->b_data;
1036 down_read(&oi->ip_xattr_sem);
1037 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
1038 if (i_ret < 0)
1039 b_ret = 0;
1040 else {
1041 if (buffer) {
1042 buffer += i_ret;
1043 size -= i_ret;
1045 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
1046 buffer, size);
1047 if (b_ret < 0)
1048 i_ret = 0;
1050 up_read(&oi->ip_xattr_sem);
1051 ocfs2_inode_unlock(dentry->d_inode, 0);
1053 brelse(di_bh);
1055 return i_ret + b_ret;
1058 static int ocfs2_xattr_find_entry(int name_index,
1059 const char *name,
1060 struct ocfs2_xattr_search *xs)
1062 struct ocfs2_xattr_entry *entry;
1063 size_t name_len;
1064 int i, cmp = 1;
1066 if (name == NULL)
1067 return -EINVAL;
1069 name_len = strlen(name);
1070 entry = xs->here;
1071 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
1072 cmp = name_index - ocfs2_xattr_get_type(entry);
1073 if (!cmp)
1074 cmp = name_len - entry->xe_name_len;
1075 if (!cmp)
1076 cmp = memcmp(name, (xs->base +
1077 le16_to_cpu(entry->xe_name_offset)),
1078 name_len);
1079 if (cmp == 0)
1080 break;
1081 entry += 1;
1083 xs->here = entry;
1085 return cmp ? -ENODATA : 0;
1088 static int ocfs2_xattr_get_value_outside(struct inode *inode,
1089 struct ocfs2_xattr_value_root *xv,
1090 void *buffer,
1091 size_t len)
1093 u32 cpos, p_cluster, num_clusters, bpc, clusters;
1094 u64 blkno;
1095 int i, ret = 0;
1096 size_t cplen, blocksize;
1097 struct buffer_head *bh = NULL;
1098 struct ocfs2_extent_list *el;
1100 el = &xv->xr_list;
1101 clusters = le32_to_cpu(xv->xr_clusters);
1102 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1103 blocksize = inode->i_sb->s_blocksize;
1105 cpos = 0;
1106 while (cpos < clusters) {
1107 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1108 &num_clusters, el, NULL);
1109 if (ret) {
1110 mlog_errno(ret);
1111 goto out;
1114 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1115 /* Copy ocfs2_xattr_value */
1116 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1117 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1118 &bh, NULL);
1119 if (ret) {
1120 mlog_errno(ret);
1121 goto out;
1124 cplen = len >= blocksize ? blocksize : len;
1125 memcpy(buffer, bh->b_data, cplen);
1126 len -= cplen;
1127 buffer += cplen;
1129 brelse(bh);
1130 bh = NULL;
1131 if (len == 0)
1132 break;
1134 cpos += num_clusters;
1136 out:
1137 return ret;
1140 static int ocfs2_xattr_ibody_get(struct inode *inode,
1141 int name_index,
1142 const char *name,
1143 void *buffer,
1144 size_t buffer_size,
1145 struct ocfs2_xattr_search *xs)
1147 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1148 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1149 struct ocfs2_xattr_value_root *xv;
1150 size_t size;
1151 int ret = 0;
1153 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
1154 return -ENODATA;
1156 xs->end = (void *)di + inode->i_sb->s_blocksize;
1157 xs->header = (struct ocfs2_xattr_header *)
1158 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1159 xs->base = (void *)xs->header;
1160 xs->here = xs->header->xh_entries;
1162 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1163 if (ret)
1164 return ret;
1165 size = le64_to_cpu(xs->here->xe_value_size);
1166 if (buffer) {
1167 if (size > buffer_size)
1168 return -ERANGE;
1169 if (ocfs2_xattr_is_local(xs->here)) {
1170 memcpy(buffer, (void *)xs->base +
1171 le16_to_cpu(xs->here->xe_name_offset) +
1172 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
1173 } else {
1174 xv = (struct ocfs2_xattr_value_root *)
1175 (xs->base + le16_to_cpu(
1176 xs->here->xe_name_offset) +
1177 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
1178 ret = ocfs2_xattr_get_value_outside(inode, xv,
1179 buffer, size);
1180 if (ret < 0) {
1181 mlog_errno(ret);
1182 return ret;
1187 return size;
1190 static int ocfs2_xattr_block_get(struct inode *inode,
1191 int name_index,
1192 const char *name,
1193 void *buffer,
1194 size_t buffer_size,
1195 struct ocfs2_xattr_search *xs)
1197 struct ocfs2_xattr_block *xb;
1198 struct ocfs2_xattr_value_root *xv;
1199 size_t size;
1200 int ret = -ENODATA, name_offset, name_len, i;
1201 int uninitialized_var(block_off);
1203 xs->bucket = ocfs2_xattr_bucket_new(inode);
1204 if (!xs->bucket) {
1205 ret = -ENOMEM;
1206 mlog_errno(ret);
1207 goto cleanup;
1210 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
1211 if (ret) {
1212 mlog_errno(ret);
1213 goto cleanup;
1216 if (xs->not_found) {
1217 ret = -ENODATA;
1218 goto cleanup;
1221 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1222 size = le64_to_cpu(xs->here->xe_value_size);
1223 if (buffer) {
1224 ret = -ERANGE;
1225 if (size > buffer_size)
1226 goto cleanup;
1228 name_offset = le16_to_cpu(xs->here->xe_name_offset);
1229 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
1230 i = xs->here - xs->header->xh_entries;
1232 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
1233 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
1234 bucket_xh(xs->bucket),
1236 &block_off,
1237 &name_offset);
1238 xs->base = bucket_block(xs->bucket, block_off);
1240 if (ocfs2_xattr_is_local(xs->here)) {
1241 memcpy(buffer, (void *)xs->base +
1242 name_offset + name_len, size);
1243 } else {
1244 xv = (struct ocfs2_xattr_value_root *)
1245 (xs->base + name_offset + name_len);
1246 ret = ocfs2_xattr_get_value_outside(inode, xv,
1247 buffer, size);
1248 if (ret < 0) {
1249 mlog_errno(ret);
1250 goto cleanup;
1254 ret = size;
1255 cleanup:
1256 ocfs2_xattr_bucket_free(xs->bucket);
1258 brelse(xs->xattr_bh);
1259 xs->xattr_bh = NULL;
1260 return ret;
1263 int ocfs2_xattr_get_nolock(struct inode *inode,
1264 struct buffer_head *di_bh,
1265 int name_index,
1266 const char *name,
1267 void *buffer,
1268 size_t buffer_size)
1270 int ret;
1271 struct ocfs2_dinode *di = NULL;
1272 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1273 struct ocfs2_xattr_search xis = {
1274 .not_found = -ENODATA,
1276 struct ocfs2_xattr_search xbs = {
1277 .not_found = -ENODATA,
1280 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1281 return -EOPNOTSUPP;
1283 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1284 ret = -ENODATA;
1286 xis.inode_bh = xbs.inode_bh = di_bh;
1287 di = (struct ocfs2_dinode *)di_bh->b_data;
1289 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1290 buffer_size, &xis);
1291 if (ret == -ENODATA && di->i_xattr_loc)
1292 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1293 buffer_size, &xbs);
1295 return ret;
1298 /* ocfs2_xattr_get()
1300 * Copy an extended attribute into the buffer provided.
1301 * Buffer is NULL to compute the size of buffer required.
1303 static int ocfs2_xattr_get(struct inode *inode,
1304 int name_index,
1305 const char *name,
1306 void *buffer,
1307 size_t buffer_size)
1309 int ret;
1310 struct buffer_head *di_bh = NULL;
1312 ret = ocfs2_inode_lock(inode, &di_bh, 0);
1313 if (ret < 0) {
1314 mlog_errno(ret);
1315 return ret;
1317 down_read(&OCFS2_I(inode)->ip_xattr_sem);
1318 ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
1319 name, buffer, buffer_size);
1320 up_read(&OCFS2_I(inode)->ip_xattr_sem);
1322 ocfs2_inode_unlock(inode, 0);
1324 brelse(di_bh);
1326 return ret;
1329 static int __ocfs2_xattr_set_value_outside(struct inode *inode,
1330 handle_t *handle,
1331 struct ocfs2_xattr_value_buf *vb,
1332 const void *value,
1333 int value_len)
1335 int ret = 0, i, cp_len;
1336 u16 blocksize = inode->i_sb->s_blocksize;
1337 u32 p_cluster, num_clusters;
1338 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1339 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1340 u64 blkno;
1341 struct buffer_head *bh = NULL;
1342 unsigned int ext_flags;
1343 struct ocfs2_xattr_value_root *xv = vb->vb_xv;
1345 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1347 while (cpos < clusters) {
1348 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1349 &num_clusters, &xv->xr_list,
1350 &ext_flags);
1351 if (ret) {
1352 mlog_errno(ret);
1353 goto out;
1356 BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED);
1358 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1360 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1361 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1362 &bh, NULL);
1363 if (ret) {
1364 mlog_errno(ret);
1365 goto out;
1368 ret = ocfs2_journal_access(handle,
1369 INODE_CACHE(inode),
1371 OCFS2_JOURNAL_ACCESS_WRITE);
1372 if (ret < 0) {
1373 mlog_errno(ret);
1374 goto out;
1377 cp_len = value_len > blocksize ? blocksize : value_len;
1378 memcpy(bh->b_data, value, cp_len);
1379 value_len -= cp_len;
1380 value += cp_len;
1381 if (cp_len < blocksize)
1382 memset(bh->b_data + cp_len, 0,
1383 blocksize - cp_len);
1385 ocfs2_journal_dirty(handle, bh);
1386 brelse(bh);
1387 bh = NULL;
1389 if (!value_len)
1390 break;
1392 cpos += num_clusters;
1394 out:
1395 brelse(bh);
1397 return ret;
1400 static int ocfs2_xa_check_space_helper(int needed_space, int free_start,
1401 int num_entries)
1403 int free_space;
1405 if (!needed_space)
1406 return 0;
1408 free_space = free_start -
1409 sizeof(struct ocfs2_xattr_header) -
1410 (num_entries * sizeof(struct ocfs2_xattr_entry)) -
1411 OCFS2_XATTR_HEADER_GAP;
1412 if (free_space < 0)
1413 return -EIO;
1414 if (free_space < needed_space)
1415 return -ENOSPC;
1417 return 0;
1420 static int ocfs2_xa_journal_access(handle_t *handle, struct ocfs2_xa_loc *loc,
1421 int type)
1423 return loc->xl_ops->xlo_journal_access(handle, loc, type);
1426 static void ocfs2_xa_journal_dirty(handle_t *handle, struct ocfs2_xa_loc *loc)
1428 loc->xl_ops->xlo_journal_dirty(handle, loc);
1431 /* Give a pointer into the storage for the given offset */
1432 static void *ocfs2_xa_offset_pointer(struct ocfs2_xa_loc *loc, int offset)
1434 BUG_ON(offset >= loc->xl_size);
1435 return loc->xl_ops->xlo_offset_pointer(loc, offset);
1439 * Wipe the name+value pair and allow the storage to reclaim it. This
1440 * must be followed by either removal of the entry or a call to
1441 * ocfs2_xa_add_namevalue().
1443 static void ocfs2_xa_wipe_namevalue(struct ocfs2_xa_loc *loc)
1445 loc->xl_ops->xlo_wipe_namevalue(loc);
1449 * Find lowest offset to a name+value pair. This is the start of our
1450 * downward-growing free space.
1452 static int ocfs2_xa_get_free_start(struct ocfs2_xa_loc *loc)
1454 return loc->xl_ops->xlo_get_free_start(loc);
1457 /* Can we reuse loc->xl_entry for xi? */
1458 static int ocfs2_xa_can_reuse_entry(struct ocfs2_xa_loc *loc,
1459 struct ocfs2_xattr_info *xi)
1461 return loc->xl_ops->xlo_can_reuse(loc, xi);
1464 /* How much free space is needed to set the new value */
1465 static int ocfs2_xa_check_space(struct ocfs2_xa_loc *loc,
1466 struct ocfs2_xattr_info *xi)
1468 return loc->xl_ops->xlo_check_space(loc, xi);
1471 static void ocfs2_xa_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1473 loc->xl_ops->xlo_add_entry(loc, name_hash);
1474 loc->xl_entry->xe_name_hash = cpu_to_le32(name_hash);
1476 * We can't leave the new entry's xe_name_offset at zero or
1477 * add_namevalue() will go nuts. We set it to the size of our
1478 * storage so that it can never be less than any other entry.
1480 loc->xl_entry->xe_name_offset = cpu_to_le16(loc->xl_size);
1483 static void ocfs2_xa_add_namevalue(struct ocfs2_xa_loc *loc,
1484 struct ocfs2_xattr_info *xi)
1486 int size = namevalue_size_xi(xi);
1487 int nameval_offset;
1488 char *nameval_buf;
1490 loc->xl_ops->xlo_add_namevalue(loc, size);
1491 loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
1492 loc->xl_entry->xe_name_len = xi->xi_name_len;
1493 ocfs2_xattr_set_type(loc->xl_entry, xi->xi_name_index);
1494 ocfs2_xattr_set_local(loc->xl_entry,
1495 xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE);
1497 nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1498 nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
1499 memset(nameval_buf, 0, size);
1500 memcpy(nameval_buf, xi->xi_name, xi->xi_name_len);
1503 static void ocfs2_xa_fill_value_buf(struct ocfs2_xa_loc *loc,
1504 struct ocfs2_xattr_value_buf *vb)
1506 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1507 int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len);
1509 /* Value bufs are for value trees */
1510 BUG_ON(ocfs2_xattr_is_local(loc->xl_entry));
1511 BUG_ON(namevalue_size_xe(loc->xl_entry) !=
1512 (name_size + OCFS2_XATTR_ROOT_SIZE));
1514 loc->xl_ops->xlo_fill_value_buf(loc, vb);
1515 vb->vb_xv =
1516 (struct ocfs2_xattr_value_root *)ocfs2_xa_offset_pointer(loc,
1517 nameval_offset +
1518 name_size);
1521 static int ocfs2_xa_block_journal_access(handle_t *handle,
1522 struct ocfs2_xa_loc *loc, int type)
1524 struct buffer_head *bh = loc->xl_storage;
1525 ocfs2_journal_access_func access;
1527 if (loc->xl_size == (bh->b_size -
1528 offsetof(struct ocfs2_xattr_block,
1529 xb_attrs.xb_header)))
1530 access = ocfs2_journal_access_xb;
1531 else
1532 access = ocfs2_journal_access_di;
1533 return access(handle, INODE_CACHE(loc->xl_inode), bh, type);
1536 static void ocfs2_xa_block_journal_dirty(handle_t *handle,
1537 struct ocfs2_xa_loc *loc)
1539 struct buffer_head *bh = loc->xl_storage;
1541 ocfs2_journal_dirty(handle, bh);
1544 static void *ocfs2_xa_block_offset_pointer(struct ocfs2_xa_loc *loc,
1545 int offset)
1547 return (char *)loc->xl_header + offset;
1550 static int ocfs2_xa_block_can_reuse(struct ocfs2_xa_loc *loc,
1551 struct ocfs2_xattr_info *xi)
1554 * Block storage is strict. If the sizes aren't exact, we will
1555 * remove the old one and reinsert the new.
1557 return namevalue_size_xe(loc->xl_entry) ==
1558 namevalue_size_xi(xi);
1561 static int ocfs2_xa_block_get_free_start(struct ocfs2_xa_loc *loc)
1563 struct ocfs2_xattr_header *xh = loc->xl_header;
1564 int i, count = le16_to_cpu(xh->xh_count);
1565 int offset, free_start = loc->xl_size;
1567 for (i = 0; i < count; i++) {
1568 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1569 if (offset < free_start)
1570 free_start = offset;
1573 return free_start;
1576 static int ocfs2_xa_block_check_space(struct ocfs2_xa_loc *loc,
1577 struct ocfs2_xattr_info *xi)
1579 int count = le16_to_cpu(loc->xl_header->xh_count);
1580 int free_start = ocfs2_xa_get_free_start(loc);
1581 int needed_space = ocfs2_xi_entry_usage(xi);
1584 * Block storage will reclaim the original entry before inserting
1585 * the new value, so we only need the difference. If the new
1586 * entry is smaller than the old one, we don't need anything.
1588 if (loc->xl_entry) {
1589 /* Don't need space if we're reusing! */
1590 if (ocfs2_xa_can_reuse_entry(loc, xi))
1591 needed_space = 0;
1592 else
1593 needed_space -= ocfs2_xe_entry_usage(loc->xl_entry);
1595 if (needed_space < 0)
1596 needed_space = 0;
1597 return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1601 * Block storage for xattrs keeps the name+value pairs compacted. When
1602 * we remove one, we have to shift any that preceded it towards the end.
1604 static void ocfs2_xa_block_wipe_namevalue(struct ocfs2_xa_loc *loc)
1606 int i, offset;
1607 int namevalue_offset, first_namevalue_offset, namevalue_size;
1608 struct ocfs2_xattr_entry *entry = loc->xl_entry;
1609 struct ocfs2_xattr_header *xh = loc->xl_header;
1610 int count = le16_to_cpu(xh->xh_count);
1612 namevalue_offset = le16_to_cpu(entry->xe_name_offset);
1613 namevalue_size = namevalue_size_xe(entry);
1614 first_namevalue_offset = ocfs2_xa_get_free_start(loc);
1616 /* Shift the name+value pairs */
1617 memmove((char *)xh + first_namevalue_offset + namevalue_size,
1618 (char *)xh + first_namevalue_offset,
1619 namevalue_offset - first_namevalue_offset);
1620 memset((char *)xh + first_namevalue_offset, 0, namevalue_size);
1622 /* Now tell xh->xh_entries about it */
1623 for (i = 0; i < count; i++) {
1624 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1625 if (offset <= namevalue_offset)
1626 le16_add_cpu(&xh->xh_entries[i].xe_name_offset,
1627 namevalue_size);
1631 * Note that we don't update xh_free_start or xh_name_value_len
1632 * because they're not used in block-stored xattrs.
1636 static void ocfs2_xa_block_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1638 int count = le16_to_cpu(loc->xl_header->xh_count);
1639 loc->xl_entry = &(loc->xl_header->xh_entries[count]);
1640 le16_add_cpu(&loc->xl_header->xh_count, 1);
1641 memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1644 static void ocfs2_xa_block_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1646 int free_start = ocfs2_xa_get_free_start(loc);
1648 loc->xl_entry->xe_name_offset = cpu_to_le16(free_start - size);
1651 static void ocfs2_xa_block_fill_value_buf(struct ocfs2_xa_loc *loc,
1652 struct ocfs2_xattr_value_buf *vb)
1654 struct buffer_head *bh = loc->xl_storage;
1656 if (loc->xl_size == (bh->b_size -
1657 offsetof(struct ocfs2_xattr_block,
1658 xb_attrs.xb_header)))
1659 vb->vb_access = ocfs2_journal_access_xb;
1660 else
1661 vb->vb_access = ocfs2_journal_access_di;
1662 vb->vb_bh = bh;
1666 * Operations for xattrs stored in blocks. This includes inline inode
1667 * storage and unindexed ocfs2_xattr_blocks.
1669 static const struct ocfs2_xa_loc_operations ocfs2_xa_block_loc_ops = {
1670 .xlo_journal_access = ocfs2_xa_block_journal_access,
1671 .xlo_journal_dirty = ocfs2_xa_block_journal_dirty,
1672 .xlo_offset_pointer = ocfs2_xa_block_offset_pointer,
1673 .xlo_check_space = ocfs2_xa_block_check_space,
1674 .xlo_can_reuse = ocfs2_xa_block_can_reuse,
1675 .xlo_get_free_start = ocfs2_xa_block_get_free_start,
1676 .xlo_wipe_namevalue = ocfs2_xa_block_wipe_namevalue,
1677 .xlo_add_entry = ocfs2_xa_block_add_entry,
1678 .xlo_add_namevalue = ocfs2_xa_block_add_namevalue,
1679 .xlo_fill_value_buf = ocfs2_xa_block_fill_value_buf,
1682 static int ocfs2_xa_bucket_journal_access(handle_t *handle,
1683 struct ocfs2_xa_loc *loc, int type)
1685 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1687 return ocfs2_xattr_bucket_journal_access(handle, bucket, type);
1690 static void ocfs2_xa_bucket_journal_dirty(handle_t *handle,
1691 struct ocfs2_xa_loc *loc)
1693 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1695 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
1698 static void *ocfs2_xa_bucket_offset_pointer(struct ocfs2_xa_loc *loc,
1699 int offset)
1701 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1702 int block, block_offset;
1704 /* The header is at the front of the bucket */
1705 block = offset >> loc->xl_inode->i_sb->s_blocksize_bits;
1706 block_offset = offset % loc->xl_inode->i_sb->s_blocksize;
1708 return bucket_block(bucket, block) + block_offset;
1711 static int ocfs2_xa_bucket_can_reuse(struct ocfs2_xa_loc *loc,
1712 struct ocfs2_xattr_info *xi)
1714 return namevalue_size_xe(loc->xl_entry) >=
1715 namevalue_size_xi(xi);
1718 static int ocfs2_xa_bucket_get_free_start(struct ocfs2_xa_loc *loc)
1720 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1721 return le16_to_cpu(bucket_xh(bucket)->xh_free_start);
1724 static int ocfs2_bucket_align_free_start(struct super_block *sb,
1725 int free_start, int size)
1728 * We need to make sure that the name+value pair fits within
1729 * one block.
1731 if (((free_start - size) >> sb->s_blocksize_bits) !=
1732 ((free_start - 1) >> sb->s_blocksize_bits))
1733 free_start -= free_start % sb->s_blocksize;
1735 return free_start;
1738 static int ocfs2_xa_bucket_check_space(struct ocfs2_xa_loc *loc,
1739 struct ocfs2_xattr_info *xi)
1741 int rc;
1742 int count = le16_to_cpu(loc->xl_header->xh_count);
1743 int free_start = ocfs2_xa_get_free_start(loc);
1744 int needed_space = ocfs2_xi_entry_usage(xi);
1745 int size = namevalue_size_xi(xi);
1746 struct super_block *sb = loc->xl_inode->i_sb;
1749 * Bucket storage does not reclaim name+value pairs it cannot
1750 * reuse. They live as holes until the bucket fills, and then
1751 * the bucket is defragmented. However, the bucket can reclaim
1752 * the ocfs2_xattr_entry.
1754 if (loc->xl_entry) {
1755 /* Don't need space if we're reusing! */
1756 if (ocfs2_xa_can_reuse_entry(loc, xi))
1757 needed_space = 0;
1758 else
1759 needed_space -= sizeof(struct ocfs2_xattr_entry);
1761 BUG_ON(needed_space < 0);
1763 if (free_start < size) {
1764 if (needed_space)
1765 return -ENOSPC;
1766 } else {
1768 * First we check if it would fit in the first place.
1769 * Below, we align the free start to a block. This may
1770 * slide us below the minimum gap. By checking unaligned
1771 * first, we avoid that error.
1773 rc = ocfs2_xa_check_space_helper(needed_space, free_start,
1774 count);
1775 if (rc)
1776 return rc;
1777 free_start = ocfs2_bucket_align_free_start(sb, free_start,
1778 size);
1780 return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1783 static void ocfs2_xa_bucket_wipe_namevalue(struct ocfs2_xa_loc *loc)
1785 le16_add_cpu(&loc->xl_header->xh_name_value_len,
1786 -namevalue_size_xe(loc->xl_entry));
1789 static void ocfs2_xa_bucket_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1791 struct ocfs2_xattr_header *xh = loc->xl_header;
1792 int count = le16_to_cpu(xh->xh_count);
1793 int low = 0, high = count - 1, tmp;
1794 struct ocfs2_xattr_entry *tmp_xe;
1797 * We keep buckets sorted by name_hash, so we need to find
1798 * our insert place.
1800 while (low <= high && count) {
1801 tmp = (low + high) / 2;
1802 tmp_xe = &xh->xh_entries[tmp];
1804 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
1805 low = tmp + 1;
1806 else if (name_hash < le32_to_cpu(tmp_xe->xe_name_hash))
1807 high = tmp - 1;
1808 else {
1809 low = tmp;
1810 break;
1814 if (low != count)
1815 memmove(&xh->xh_entries[low + 1],
1816 &xh->xh_entries[low],
1817 ((count - low) * sizeof(struct ocfs2_xattr_entry)));
1819 le16_add_cpu(&xh->xh_count, 1);
1820 loc->xl_entry = &xh->xh_entries[low];
1821 memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1824 static void ocfs2_xa_bucket_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1826 int free_start = ocfs2_xa_get_free_start(loc);
1827 struct ocfs2_xattr_header *xh = loc->xl_header;
1828 struct super_block *sb = loc->xl_inode->i_sb;
1829 int nameval_offset;
1831 free_start = ocfs2_bucket_align_free_start(sb, free_start, size);
1832 nameval_offset = free_start - size;
1833 loc->xl_entry->xe_name_offset = cpu_to_le16(nameval_offset);
1834 xh->xh_free_start = cpu_to_le16(nameval_offset);
1835 le16_add_cpu(&xh->xh_name_value_len, size);
1839 static void ocfs2_xa_bucket_fill_value_buf(struct ocfs2_xa_loc *loc,
1840 struct ocfs2_xattr_value_buf *vb)
1842 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1843 struct super_block *sb = loc->xl_inode->i_sb;
1844 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1845 int size = namevalue_size_xe(loc->xl_entry);
1846 int block_offset = nameval_offset >> sb->s_blocksize_bits;
1848 /* Values are not allowed to straddle block boundaries */
1849 BUG_ON(block_offset !=
1850 ((nameval_offset + size - 1) >> sb->s_blocksize_bits));
1851 /* We expect the bucket to be filled in */
1852 BUG_ON(!bucket->bu_bhs[block_offset]);
1854 vb->vb_access = ocfs2_journal_access;
1855 vb->vb_bh = bucket->bu_bhs[block_offset];
1858 /* Operations for xattrs stored in buckets. */
1859 static const struct ocfs2_xa_loc_operations ocfs2_xa_bucket_loc_ops = {
1860 .xlo_journal_access = ocfs2_xa_bucket_journal_access,
1861 .xlo_journal_dirty = ocfs2_xa_bucket_journal_dirty,
1862 .xlo_offset_pointer = ocfs2_xa_bucket_offset_pointer,
1863 .xlo_check_space = ocfs2_xa_bucket_check_space,
1864 .xlo_can_reuse = ocfs2_xa_bucket_can_reuse,
1865 .xlo_get_free_start = ocfs2_xa_bucket_get_free_start,
1866 .xlo_wipe_namevalue = ocfs2_xa_bucket_wipe_namevalue,
1867 .xlo_add_entry = ocfs2_xa_bucket_add_entry,
1868 .xlo_add_namevalue = ocfs2_xa_bucket_add_namevalue,
1869 .xlo_fill_value_buf = ocfs2_xa_bucket_fill_value_buf,
1872 static unsigned int ocfs2_xa_value_clusters(struct ocfs2_xa_loc *loc)
1874 struct ocfs2_xattr_value_buf vb;
1876 if (ocfs2_xattr_is_local(loc->xl_entry))
1877 return 0;
1879 ocfs2_xa_fill_value_buf(loc, &vb);
1880 return le32_to_cpu(vb.vb_xv->xr_clusters);
1883 static int ocfs2_xa_value_truncate(struct ocfs2_xa_loc *loc, u64 bytes,
1884 struct ocfs2_xattr_set_ctxt *ctxt)
1886 int trunc_rc, access_rc;
1887 struct ocfs2_xattr_value_buf vb;
1889 ocfs2_xa_fill_value_buf(loc, &vb);
1890 trunc_rc = ocfs2_xattr_value_truncate(loc->xl_inode, &vb, bytes,
1891 ctxt);
1894 * The caller of ocfs2_xa_value_truncate() has already called
1895 * ocfs2_xa_journal_access on the loc. However, The truncate code
1896 * calls ocfs2_extend_trans(). This may commit the previous
1897 * transaction and open a new one. If this is a bucket, truncate
1898 * could leave only vb->vb_bh set up for journaling. Meanwhile,
1899 * the caller is expecting to dirty the entire bucket. So we must
1900 * reset the journal work. We do this even if truncate has failed,
1901 * as it could have failed after committing the extend.
1903 access_rc = ocfs2_xa_journal_access(ctxt->handle, loc,
1904 OCFS2_JOURNAL_ACCESS_WRITE);
1906 /* Errors in truncate take precedence */
1907 return trunc_rc ? trunc_rc : access_rc;
1910 static void ocfs2_xa_remove_entry(struct ocfs2_xa_loc *loc)
1912 int index, count;
1913 struct ocfs2_xattr_header *xh = loc->xl_header;
1914 struct ocfs2_xattr_entry *entry = loc->xl_entry;
1916 ocfs2_xa_wipe_namevalue(loc);
1917 loc->xl_entry = NULL;
1919 le16_add_cpu(&xh->xh_count, -1);
1920 count = le16_to_cpu(xh->xh_count);
1923 * Only zero out the entry if there are more remaining. This is
1924 * important for an empty bucket, as it keeps track of the
1925 * bucket's hash value. It doesn't hurt empty block storage.
1927 if (count) {
1928 index = ((char *)entry - (char *)&xh->xh_entries) /
1929 sizeof(struct ocfs2_xattr_entry);
1930 memmove(&xh->xh_entries[index], &xh->xh_entries[index + 1],
1931 (count - index) * sizeof(struct ocfs2_xattr_entry));
1932 memset(&xh->xh_entries[count], 0,
1933 sizeof(struct ocfs2_xattr_entry));
1938 * If we have a problem adjusting the size of an external value during
1939 * ocfs2_xa_prepare_entry() or ocfs2_xa_remove(), we may have an xattr
1940 * in an intermediate state. For example, the value may be partially
1941 * truncated.
1943 * If the value tree hasn't changed, the extend/truncate went nowhere.
1944 * We have nothing to do. The caller can treat it as a straight error.
1946 * If the value tree got partially truncated, we now have a corrupted
1947 * extended attribute. We're going to wipe its entry and leak the
1948 * clusters. Better to leak some storage than leave a corrupt entry.
1950 * If the value tree grew, it obviously didn't grow enough for the
1951 * new entry. We're not going to try and reclaim those clusters either.
1952 * If there was already an external value there (orig_clusters != 0),
1953 * the new clusters are attached safely and we can just leave the old
1954 * value in place. If there was no external value there, we remove
1955 * the entry.
1957 * This way, the xattr block we store in the journal will be consistent.
1958 * If the size change broke because of the journal, no changes will hit
1959 * disk anyway.
1961 static void ocfs2_xa_cleanup_value_truncate(struct ocfs2_xa_loc *loc,
1962 const char *what,
1963 unsigned int orig_clusters)
1965 unsigned int new_clusters = ocfs2_xa_value_clusters(loc);
1966 char *nameval_buf = ocfs2_xa_offset_pointer(loc,
1967 le16_to_cpu(loc->xl_entry->xe_name_offset));
1969 if (new_clusters < orig_clusters) {
1970 mlog(ML_ERROR,
1971 "Partial truncate while %s xattr %.*s. Leaking "
1972 "%u clusters and removing the entry\n",
1973 what, loc->xl_entry->xe_name_len, nameval_buf,
1974 orig_clusters - new_clusters);
1975 ocfs2_xa_remove_entry(loc);
1976 } else if (!orig_clusters) {
1977 mlog(ML_ERROR,
1978 "Unable to allocate an external value for xattr "
1979 "%.*s safely. Leaking %u clusters and removing the "
1980 "entry\n",
1981 loc->xl_entry->xe_name_len, nameval_buf,
1982 new_clusters - orig_clusters);
1983 ocfs2_xa_remove_entry(loc);
1984 } else if (new_clusters > orig_clusters)
1985 mlog(ML_ERROR,
1986 "Unable to grow xattr %.*s safely. %u new clusters "
1987 "have been added, but the value will not be "
1988 "modified\n",
1989 loc->xl_entry->xe_name_len, nameval_buf,
1990 new_clusters - orig_clusters);
1993 static int ocfs2_xa_remove(struct ocfs2_xa_loc *loc,
1994 struct ocfs2_xattr_set_ctxt *ctxt)
1996 int rc = 0;
1997 unsigned int orig_clusters;
1999 if (!ocfs2_xattr_is_local(loc->xl_entry)) {
2000 orig_clusters = ocfs2_xa_value_clusters(loc);
2001 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
2002 if (rc) {
2003 mlog_errno(rc);
2005 * Since this is remove, we can return 0 if
2006 * ocfs2_xa_cleanup_value_truncate() is going to
2007 * wipe the entry anyway. So we check the
2008 * cluster count as well.
2010 if (orig_clusters != ocfs2_xa_value_clusters(loc))
2011 rc = 0;
2012 ocfs2_xa_cleanup_value_truncate(loc, "removing",
2013 orig_clusters);
2014 if (rc)
2015 goto out;
2019 ocfs2_xa_remove_entry(loc);
2021 out:
2022 return rc;
2025 static void ocfs2_xa_install_value_root(struct ocfs2_xa_loc *loc)
2027 int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len);
2028 char *nameval_buf;
2030 nameval_buf = ocfs2_xa_offset_pointer(loc,
2031 le16_to_cpu(loc->xl_entry->xe_name_offset));
2032 memcpy(nameval_buf + name_size, &def_xv, OCFS2_XATTR_ROOT_SIZE);
2036 * Take an existing entry and make it ready for the new value. This
2037 * won't allocate space, but it may free space. It should be ready for
2038 * ocfs2_xa_prepare_entry() to finish the work.
2040 static int ocfs2_xa_reuse_entry(struct ocfs2_xa_loc *loc,
2041 struct ocfs2_xattr_info *xi,
2042 struct ocfs2_xattr_set_ctxt *ctxt)
2044 int rc = 0;
2045 int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
2046 unsigned int orig_clusters;
2047 char *nameval_buf;
2048 int xe_local = ocfs2_xattr_is_local(loc->xl_entry);
2049 int xi_local = xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE;
2051 BUG_ON(OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len) !=
2052 name_size);
2054 nameval_buf = ocfs2_xa_offset_pointer(loc,
2055 le16_to_cpu(loc->xl_entry->xe_name_offset));
2056 if (xe_local) {
2057 memset(nameval_buf + name_size, 0,
2058 namevalue_size_xe(loc->xl_entry) - name_size);
2059 if (!xi_local)
2060 ocfs2_xa_install_value_root(loc);
2061 } else {
2062 orig_clusters = ocfs2_xa_value_clusters(loc);
2063 if (xi_local) {
2064 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
2065 if (rc < 0)
2066 mlog_errno(rc);
2067 else
2068 memset(nameval_buf + name_size, 0,
2069 namevalue_size_xe(loc->xl_entry) -
2070 name_size);
2071 } else if (le64_to_cpu(loc->xl_entry->xe_value_size) >
2072 xi->xi_value_len) {
2073 rc = ocfs2_xa_value_truncate(loc, xi->xi_value_len,
2074 ctxt);
2075 if (rc < 0)
2076 mlog_errno(rc);
2079 if (rc) {
2080 ocfs2_xa_cleanup_value_truncate(loc, "reusing",
2081 orig_clusters);
2082 goto out;
2086 loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
2087 ocfs2_xattr_set_local(loc->xl_entry, xi_local);
2089 out:
2090 return rc;
2094 * Prepares loc->xl_entry to receive the new xattr. This includes
2095 * properly setting up the name+value pair region. If loc->xl_entry
2096 * already exists, it will take care of modifying it appropriately.
2098 * Note that this modifies the data. You did journal_access already,
2099 * right?
2101 static int ocfs2_xa_prepare_entry(struct ocfs2_xa_loc *loc,
2102 struct ocfs2_xattr_info *xi,
2103 u32 name_hash,
2104 struct ocfs2_xattr_set_ctxt *ctxt)
2106 int rc = 0;
2107 unsigned int orig_clusters;
2108 __le64 orig_value_size = 0;
2110 rc = ocfs2_xa_check_space(loc, xi);
2111 if (rc)
2112 goto out;
2114 if (loc->xl_entry) {
2115 if (ocfs2_xa_can_reuse_entry(loc, xi)) {
2116 orig_value_size = loc->xl_entry->xe_value_size;
2117 rc = ocfs2_xa_reuse_entry(loc, xi, ctxt);
2118 if (rc)
2119 goto out;
2120 goto alloc_value;
2123 if (!ocfs2_xattr_is_local(loc->xl_entry)) {
2124 orig_clusters = ocfs2_xa_value_clusters(loc);
2125 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
2126 if (rc) {
2127 mlog_errno(rc);
2128 ocfs2_xa_cleanup_value_truncate(loc,
2129 "overwriting",
2130 orig_clusters);
2131 goto out;
2134 ocfs2_xa_wipe_namevalue(loc);
2135 } else
2136 ocfs2_xa_add_entry(loc, name_hash);
2139 * If we get here, we have a blank entry. Fill it. We grow our
2140 * name+value pair back from the end.
2142 ocfs2_xa_add_namevalue(loc, xi);
2143 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
2144 ocfs2_xa_install_value_root(loc);
2146 alloc_value:
2147 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2148 orig_clusters = ocfs2_xa_value_clusters(loc);
2149 rc = ocfs2_xa_value_truncate(loc, xi->xi_value_len, ctxt);
2150 if (rc < 0) {
2151 ctxt->set_abort = 1;
2152 ocfs2_xa_cleanup_value_truncate(loc, "growing",
2153 orig_clusters);
2155 * If we were growing an existing value,
2156 * ocfs2_xa_cleanup_value_truncate() won't remove
2157 * the entry. We need to restore the original value
2158 * size.
2160 if (loc->xl_entry) {
2161 BUG_ON(!orig_value_size);
2162 loc->xl_entry->xe_value_size = orig_value_size;
2164 mlog_errno(rc);
2168 out:
2169 return rc;
2173 * Store the value portion of the name+value pair. This will skip
2174 * values that are stored externally. Their tree roots were set up
2175 * by ocfs2_xa_prepare_entry().
2177 static int ocfs2_xa_store_value(struct ocfs2_xa_loc *loc,
2178 struct ocfs2_xattr_info *xi,
2179 struct ocfs2_xattr_set_ctxt *ctxt)
2181 int rc = 0;
2182 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
2183 int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
2184 char *nameval_buf;
2185 struct ocfs2_xattr_value_buf vb;
2187 nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
2188 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2189 ocfs2_xa_fill_value_buf(loc, &vb);
2190 rc = __ocfs2_xattr_set_value_outside(loc->xl_inode,
2191 ctxt->handle, &vb,
2192 xi->xi_value,
2193 xi->xi_value_len);
2194 } else
2195 memcpy(nameval_buf + name_size, xi->xi_value, xi->xi_value_len);
2197 return rc;
2200 static int ocfs2_xa_set(struct ocfs2_xa_loc *loc,
2201 struct ocfs2_xattr_info *xi,
2202 struct ocfs2_xattr_set_ctxt *ctxt)
2204 int ret;
2205 u32 name_hash = ocfs2_xattr_name_hash(loc->xl_inode, xi->xi_name,
2206 xi->xi_name_len);
2208 ret = ocfs2_xa_journal_access(ctxt->handle, loc,
2209 OCFS2_JOURNAL_ACCESS_WRITE);
2210 if (ret) {
2211 mlog_errno(ret);
2212 goto out;
2216 * From here on out, everything is going to modify the buffer a
2217 * little. Errors are going to leave the xattr header in a
2218 * sane state. Thus, even with errors we dirty the sucker.
2221 /* Don't worry, we are never called with !xi_value and !xl_entry */
2222 if (!xi->xi_value) {
2223 ret = ocfs2_xa_remove(loc, ctxt);
2224 goto out_dirty;
2227 ret = ocfs2_xa_prepare_entry(loc, xi, name_hash, ctxt);
2228 if (ret) {
2229 if (ret != -ENOSPC)
2230 mlog_errno(ret);
2231 goto out_dirty;
2234 ret = ocfs2_xa_store_value(loc, xi, ctxt);
2235 if (ret)
2236 mlog_errno(ret);
2238 out_dirty:
2239 ocfs2_xa_journal_dirty(ctxt->handle, loc);
2241 out:
2242 return ret;
2245 static void ocfs2_init_dinode_xa_loc(struct ocfs2_xa_loc *loc,
2246 struct inode *inode,
2247 struct buffer_head *bh,
2248 struct ocfs2_xattr_entry *entry)
2250 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
2252 BUG_ON(!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_XATTR_FL));
2254 loc->xl_inode = inode;
2255 loc->xl_ops = &ocfs2_xa_block_loc_ops;
2256 loc->xl_storage = bh;
2257 loc->xl_entry = entry;
2258 loc->xl_size = le16_to_cpu(di->i_xattr_inline_size);
2259 loc->xl_header =
2260 (struct ocfs2_xattr_header *)(bh->b_data + bh->b_size -
2261 loc->xl_size);
2264 static void ocfs2_init_xattr_block_xa_loc(struct ocfs2_xa_loc *loc,
2265 struct inode *inode,
2266 struct buffer_head *bh,
2267 struct ocfs2_xattr_entry *entry)
2269 struct ocfs2_xattr_block *xb =
2270 (struct ocfs2_xattr_block *)bh->b_data;
2272 BUG_ON(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED);
2274 loc->xl_inode = inode;
2275 loc->xl_ops = &ocfs2_xa_block_loc_ops;
2276 loc->xl_storage = bh;
2277 loc->xl_header = &(xb->xb_attrs.xb_header);
2278 loc->xl_entry = entry;
2279 loc->xl_size = bh->b_size - offsetof(struct ocfs2_xattr_block,
2280 xb_attrs.xb_header);
2283 static void ocfs2_init_xattr_bucket_xa_loc(struct ocfs2_xa_loc *loc,
2284 struct ocfs2_xattr_bucket *bucket,
2285 struct ocfs2_xattr_entry *entry)
2287 loc->xl_inode = bucket->bu_inode;
2288 loc->xl_ops = &ocfs2_xa_bucket_loc_ops;
2289 loc->xl_storage = bucket;
2290 loc->xl_header = bucket_xh(bucket);
2291 loc->xl_entry = entry;
2292 loc->xl_size = OCFS2_XATTR_BUCKET_SIZE;
2296 * In xattr remove, if it is stored outside and refcounted, we may have
2297 * the chance to split the refcount tree. So need the allocators.
2299 static int ocfs2_lock_xattr_remove_allocators(struct inode *inode,
2300 struct ocfs2_xattr_value_root *xv,
2301 struct ocfs2_caching_info *ref_ci,
2302 struct buffer_head *ref_root_bh,
2303 struct ocfs2_alloc_context **meta_ac,
2304 int *ref_credits)
2306 int ret, meta_add = 0;
2307 u32 p_cluster, num_clusters;
2308 unsigned int ext_flags;
2310 *ref_credits = 0;
2311 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
2312 &num_clusters,
2313 &xv->xr_list,
2314 &ext_flags);
2315 if (ret) {
2316 mlog_errno(ret);
2317 goto out;
2320 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
2321 goto out;
2323 ret = ocfs2_refcounted_xattr_delete_need(inode, ref_ci,
2324 ref_root_bh, xv,
2325 &meta_add, ref_credits);
2326 if (ret) {
2327 mlog_errno(ret);
2328 goto out;
2331 ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
2332 meta_add, meta_ac);
2333 if (ret)
2334 mlog_errno(ret);
2336 out:
2337 return ret;
2340 static int ocfs2_remove_value_outside(struct inode*inode,
2341 struct ocfs2_xattr_value_buf *vb,
2342 struct ocfs2_xattr_header *header,
2343 struct ocfs2_caching_info *ref_ci,
2344 struct buffer_head *ref_root_bh)
2346 int ret = 0, i, ref_credits;
2347 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2348 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
2349 void *val;
2351 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
2353 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
2354 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
2356 if (ocfs2_xattr_is_local(entry))
2357 continue;
2359 val = (void *)header +
2360 le16_to_cpu(entry->xe_name_offset);
2361 vb->vb_xv = (struct ocfs2_xattr_value_root *)
2362 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
2364 ret = ocfs2_lock_xattr_remove_allocators(inode, vb->vb_xv,
2365 ref_ci, ref_root_bh,
2366 &ctxt.meta_ac,
2367 &ref_credits);
2369 ctxt.handle = ocfs2_start_trans(osb, ref_credits +
2370 ocfs2_remove_extent_credits(osb->sb));
2371 if (IS_ERR(ctxt.handle)) {
2372 ret = PTR_ERR(ctxt.handle);
2373 mlog_errno(ret);
2374 break;
2377 ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt);
2378 if (ret < 0) {
2379 mlog_errno(ret);
2380 break;
2383 ocfs2_commit_trans(osb, ctxt.handle);
2384 if (ctxt.meta_ac) {
2385 ocfs2_free_alloc_context(ctxt.meta_ac);
2386 ctxt.meta_ac = NULL;
2390 if (ctxt.meta_ac)
2391 ocfs2_free_alloc_context(ctxt.meta_ac);
2392 ocfs2_schedule_truncate_log_flush(osb, 1);
2393 ocfs2_run_deallocs(osb, &ctxt.dealloc);
2394 return ret;
2397 static int ocfs2_xattr_ibody_remove(struct inode *inode,
2398 struct buffer_head *di_bh,
2399 struct ocfs2_caching_info *ref_ci,
2400 struct buffer_head *ref_root_bh)
2403 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2404 struct ocfs2_xattr_header *header;
2405 int ret;
2406 struct ocfs2_xattr_value_buf vb = {
2407 .vb_bh = di_bh,
2408 .vb_access = ocfs2_journal_access_di,
2411 header = (struct ocfs2_xattr_header *)
2412 ((void *)di + inode->i_sb->s_blocksize -
2413 le16_to_cpu(di->i_xattr_inline_size));
2415 ret = ocfs2_remove_value_outside(inode, &vb, header,
2416 ref_ci, ref_root_bh);
2418 return ret;
2421 struct ocfs2_rm_xattr_bucket_para {
2422 struct ocfs2_caching_info *ref_ci;
2423 struct buffer_head *ref_root_bh;
2426 static int ocfs2_xattr_block_remove(struct inode *inode,
2427 struct buffer_head *blk_bh,
2428 struct ocfs2_caching_info *ref_ci,
2429 struct buffer_head *ref_root_bh)
2431 struct ocfs2_xattr_block *xb;
2432 int ret = 0;
2433 struct ocfs2_xattr_value_buf vb = {
2434 .vb_bh = blk_bh,
2435 .vb_access = ocfs2_journal_access_xb,
2437 struct ocfs2_rm_xattr_bucket_para args = {
2438 .ref_ci = ref_ci,
2439 .ref_root_bh = ref_root_bh,
2442 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2443 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2444 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
2445 ret = ocfs2_remove_value_outside(inode, &vb, header,
2446 ref_ci, ref_root_bh);
2447 } else
2448 ret = ocfs2_iterate_xattr_index_block(inode,
2449 blk_bh,
2450 ocfs2_rm_xattr_cluster,
2451 &args);
2453 return ret;
2456 static int ocfs2_xattr_free_block(struct inode *inode,
2457 u64 block,
2458 struct ocfs2_caching_info *ref_ci,
2459 struct buffer_head *ref_root_bh)
2461 struct inode *xb_alloc_inode;
2462 struct buffer_head *xb_alloc_bh = NULL;
2463 struct buffer_head *blk_bh = NULL;
2464 struct ocfs2_xattr_block *xb;
2465 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2466 handle_t *handle;
2467 int ret = 0;
2468 u64 blk, bg_blkno;
2469 u16 bit;
2471 ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
2472 if (ret < 0) {
2473 mlog_errno(ret);
2474 goto out;
2477 ret = ocfs2_xattr_block_remove(inode, blk_bh, ref_ci, ref_root_bh);
2478 if (ret < 0) {
2479 mlog_errno(ret);
2480 goto out;
2483 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2484 blk = le64_to_cpu(xb->xb_blkno);
2485 bit = le16_to_cpu(xb->xb_suballoc_bit);
2486 if (xb->xb_suballoc_loc)
2487 bg_blkno = le64_to_cpu(xb->xb_suballoc_loc);
2488 else
2489 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2491 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
2492 EXTENT_ALLOC_SYSTEM_INODE,
2493 le16_to_cpu(xb->xb_suballoc_slot));
2494 if (!xb_alloc_inode) {
2495 ret = -ENOMEM;
2496 mlog_errno(ret);
2497 goto out;
2499 mutex_lock(&xb_alloc_inode->i_mutex);
2501 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
2502 if (ret < 0) {
2503 mlog_errno(ret);
2504 goto out_mutex;
2507 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
2508 if (IS_ERR(handle)) {
2509 ret = PTR_ERR(handle);
2510 mlog_errno(ret);
2511 goto out_unlock;
2514 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
2515 bit, bg_blkno, 1);
2516 if (ret < 0)
2517 mlog_errno(ret);
2519 ocfs2_commit_trans(osb, handle);
2520 out_unlock:
2521 ocfs2_inode_unlock(xb_alloc_inode, 1);
2522 brelse(xb_alloc_bh);
2523 out_mutex:
2524 mutex_unlock(&xb_alloc_inode->i_mutex);
2525 iput(xb_alloc_inode);
2526 out:
2527 brelse(blk_bh);
2528 return ret;
2532 * ocfs2_xattr_remove()
2534 * Free extended attribute resources associated with this inode.
2536 int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
2538 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2539 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2540 struct ocfs2_refcount_tree *ref_tree = NULL;
2541 struct buffer_head *ref_root_bh = NULL;
2542 struct ocfs2_caching_info *ref_ci = NULL;
2543 handle_t *handle;
2544 int ret;
2546 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2547 return 0;
2549 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
2550 return 0;
2552 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) {
2553 ret = ocfs2_lock_refcount_tree(OCFS2_SB(inode->i_sb),
2554 le64_to_cpu(di->i_refcount_loc),
2555 1, &ref_tree, &ref_root_bh);
2556 if (ret) {
2557 mlog_errno(ret);
2558 goto out;
2560 ref_ci = &ref_tree->rf_ci;
2564 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2565 ret = ocfs2_xattr_ibody_remove(inode, di_bh,
2566 ref_ci, ref_root_bh);
2567 if (ret < 0) {
2568 mlog_errno(ret);
2569 goto out;
2573 if (di->i_xattr_loc) {
2574 ret = ocfs2_xattr_free_block(inode,
2575 le64_to_cpu(di->i_xattr_loc),
2576 ref_ci, ref_root_bh);
2577 if (ret < 0) {
2578 mlog_errno(ret);
2579 goto out;
2583 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
2584 OCFS2_INODE_UPDATE_CREDITS);
2585 if (IS_ERR(handle)) {
2586 ret = PTR_ERR(handle);
2587 mlog_errno(ret);
2588 goto out;
2590 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
2591 OCFS2_JOURNAL_ACCESS_WRITE);
2592 if (ret) {
2593 mlog_errno(ret);
2594 goto out_commit;
2597 di->i_xattr_loc = 0;
2599 spin_lock(&oi->ip_lock);
2600 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
2601 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2602 spin_unlock(&oi->ip_lock);
2604 ocfs2_journal_dirty(handle, di_bh);
2605 out_commit:
2606 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
2607 out:
2608 if (ref_tree)
2609 ocfs2_unlock_refcount_tree(OCFS2_SB(inode->i_sb), ref_tree, 1);
2610 brelse(ref_root_bh);
2611 return ret;
2614 static int ocfs2_xattr_has_space_inline(struct inode *inode,
2615 struct ocfs2_dinode *di)
2617 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2618 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
2619 int free;
2621 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
2622 return 0;
2624 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2625 struct ocfs2_inline_data *idata = &di->id2.i_data;
2626 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
2627 } else if (ocfs2_inode_is_fast_symlink(inode)) {
2628 free = ocfs2_fast_symlink_chars(inode->i_sb) -
2629 le64_to_cpu(di->i_size);
2630 } else {
2631 struct ocfs2_extent_list *el = &di->id2.i_list;
2632 free = (le16_to_cpu(el->l_count) -
2633 le16_to_cpu(el->l_next_free_rec)) *
2634 sizeof(struct ocfs2_extent_rec);
2636 if (free >= xattrsize)
2637 return 1;
2639 return 0;
2643 * ocfs2_xattr_ibody_find()
2645 * Find extended attribute in inode block and
2646 * fill search info into struct ocfs2_xattr_search.
2648 static int ocfs2_xattr_ibody_find(struct inode *inode,
2649 int name_index,
2650 const char *name,
2651 struct ocfs2_xattr_search *xs)
2653 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2654 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2655 int ret;
2656 int has_space = 0;
2658 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2659 return 0;
2661 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2662 down_read(&oi->ip_alloc_sem);
2663 has_space = ocfs2_xattr_has_space_inline(inode, di);
2664 up_read(&oi->ip_alloc_sem);
2665 if (!has_space)
2666 return 0;
2669 xs->xattr_bh = xs->inode_bh;
2670 xs->end = (void *)di + inode->i_sb->s_blocksize;
2671 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
2672 xs->header = (struct ocfs2_xattr_header *)
2673 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
2674 else
2675 xs->header = (struct ocfs2_xattr_header *)
2676 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
2677 xs->base = (void *)xs->header;
2678 xs->here = xs->header->xh_entries;
2680 /* Find the named attribute. */
2681 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2682 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2683 if (ret && ret != -ENODATA)
2684 return ret;
2685 xs->not_found = ret;
2688 return 0;
2691 static int ocfs2_xattr_ibody_init(struct inode *inode,
2692 struct buffer_head *di_bh,
2693 struct ocfs2_xattr_set_ctxt *ctxt)
2695 int ret;
2696 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2697 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2698 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2699 unsigned int xattrsize = osb->s_xattr_inline_size;
2701 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2702 ret = -ENOSPC;
2703 goto out;
2706 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode), di_bh,
2707 OCFS2_JOURNAL_ACCESS_WRITE);
2708 if (ret) {
2709 mlog_errno(ret);
2710 goto out;
2714 * Adjust extent record count or inline data size
2715 * to reserve space for extended attribute.
2717 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2718 struct ocfs2_inline_data *idata = &di->id2.i_data;
2719 le16_add_cpu(&idata->id_count, -xattrsize);
2720 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
2721 struct ocfs2_extent_list *el = &di->id2.i_list;
2722 le16_add_cpu(&el->l_count, -(xattrsize /
2723 sizeof(struct ocfs2_extent_rec)));
2725 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
2727 spin_lock(&oi->ip_lock);
2728 oi->ip_dyn_features |= OCFS2_INLINE_XATTR_FL|OCFS2_HAS_XATTR_FL;
2729 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2730 spin_unlock(&oi->ip_lock);
2732 ocfs2_journal_dirty(ctxt->handle, di_bh);
2734 out:
2735 return ret;
2739 * ocfs2_xattr_ibody_set()
2741 * Set, replace or remove an extended attribute into inode block.
2744 static int ocfs2_xattr_ibody_set(struct inode *inode,
2745 struct ocfs2_xattr_info *xi,
2746 struct ocfs2_xattr_search *xs,
2747 struct ocfs2_xattr_set_ctxt *ctxt)
2749 int ret;
2750 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2751 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2752 struct ocfs2_xa_loc loc;
2754 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2755 return -ENOSPC;
2757 down_write(&oi->ip_alloc_sem);
2758 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2759 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2760 ret = -ENOSPC;
2761 goto out;
2765 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2766 ret = ocfs2_xattr_ibody_init(inode, xs->inode_bh, ctxt);
2767 if (ret) {
2768 if (ret != -ENOSPC)
2769 mlog_errno(ret);
2770 goto out;
2774 ocfs2_init_dinode_xa_loc(&loc, inode, xs->inode_bh,
2775 xs->not_found ? NULL : xs->here);
2776 ret = ocfs2_xa_set(&loc, xi, ctxt);
2777 if (ret) {
2778 if (ret != -ENOSPC)
2779 mlog_errno(ret);
2780 goto out;
2782 xs->here = loc.xl_entry;
2784 out:
2785 up_write(&oi->ip_alloc_sem);
2787 return ret;
2791 * ocfs2_xattr_block_find()
2793 * Find extended attribute in external block and
2794 * fill search info into struct ocfs2_xattr_search.
2796 static int ocfs2_xattr_block_find(struct inode *inode,
2797 int name_index,
2798 const char *name,
2799 struct ocfs2_xattr_search *xs)
2801 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2802 struct buffer_head *blk_bh = NULL;
2803 struct ocfs2_xattr_block *xb;
2804 int ret = 0;
2806 if (!di->i_xattr_loc)
2807 return ret;
2809 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
2810 &blk_bh);
2811 if (ret < 0) {
2812 mlog_errno(ret);
2813 return ret;
2816 xs->xattr_bh = blk_bh;
2817 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2819 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2820 xs->header = &xb->xb_attrs.xb_header;
2821 xs->base = (void *)xs->header;
2822 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
2823 xs->here = xs->header->xh_entries;
2825 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2826 } else
2827 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
2828 name_index,
2829 name, xs);
2831 if (ret && ret != -ENODATA) {
2832 xs->xattr_bh = NULL;
2833 goto cleanup;
2835 xs->not_found = ret;
2836 return 0;
2837 cleanup:
2838 brelse(blk_bh);
2840 return ret;
2843 static int ocfs2_create_xattr_block(struct inode *inode,
2844 struct buffer_head *inode_bh,
2845 struct ocfs2_xattr_set_ctxt *ctxt,
2846 int indexed,
2847 struct buffer_head **ret_bh)
2849 int ret;
2850 u16 suballoc_bit_start;
2851 u32 num_got;
2852 u64 suballoc_loc, first_blkno;
2853 struct ocfs2_dinode *di = (struct ocfs2_dinode *)inode_bh->b_data;
2854 struct buffer_head *new_bh = NULL;
2855 struct ocfs2_xattr_block *xblk;
2857 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
2858 inode_bh, OCFS2_JOURNAL_ACCESS_CREATE);
2859 if (ret < 0) {
2860 mlog_errno(ret);
2861 goto end;
2864 ret = ocfs2_claim_metadata(ctxt->handle, ctxt->meta_ac, 1,
2865 &suballoc_loc, &suballoc_bit_start,
2866 &num_got, &first_blkno);
2867 if (ret < 0) {
2868 mlog_errno(ret);
2869 goto end;
2872 new_bh = sb_getblk(inode->i_sb, first_blkno);
2873 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
2875 ret = ocfs2_journal_access_xb(ctxt->handle, INODE_CACHE(inode),
2876 new_bh,
2877 OCFS2_JOURNAL_ACCESS_CREATE);
2878 if (ret < 0) {
2879 mlog_errno(ret);
2880 goto end;
2883 /* Initialize ocfs2_xattr_block */
2884 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2885 memset(xblk, 0, inode->i_sb->s_blocksize);
2886 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
2887 xblk->xb_suballoc_slot = cpu_to_le16(ctxt->meta_ac->ac_alloc_slot);
2888 xblk->xb_suballoc_loc = cpu_to_le64(suballoc_loc);
2889 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
2890 xblk->xb_fs_generation =
2891 cpu_to_le32(OCFS2_SB(inode->i_sb)->fs_generation);
2892 xblk->xb_blkno = cpu_to_le64(first_blkno);
2893 if (indexed) {
2894 struct ocfs2_xattr_tree_root *xr = &xblk->xb_attrs.xb_root;
2895 xr->xt_clusters = cpu_to_le32(1);
2896 xr->xt_last_eb_blk = 0;
2897 xr->xt_list.l_tree_depth = 0;
2898 xr->xt_list.l_count = cpu_to_le16(
2899 ocfs2_xattr_recs_per_xb(inode->i_sb));
2900 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2901 xblk->xb_flags = cpu_to_le16(OCFS2_XATTR_INDEXED);
2903 ocfs2_journal_dirty(ctxt->handle, new_bh);
2905 /* Add it to the inode */
2906 di->i_xattr_loc = cpu_to_le64(first_blkno);
2908 spin_lock(&OCFS2_I(inode)->ip_lock);
2909 OCFS2_I(inode)->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
2910 di->i_dyn_features = cpu_to_le16(OCFS2_I(inode)->ip_dyn_features);
2911 spin_unlock(&OCFS2_I(inode)->ip_lock);
2913 ocfs2_journal_dirty(ctxt->handle, inode_bh);
2915 *ret_bh = new_bh;
2916 new_bh = NULL;
2918 end:
2919 brelse(new_bh);
2920 return ret;
2924 * ocfs2_xattr_block_set()
2926 * Set, replace or remove an extended attribute into external block.
2929 static int ocfs2_xattr_block_set(struct inode *inode,
2930 struct ocfs2_xattr_info *xi,
2931 struct ocfs2_xattr_search *xs,
2932 struct ocfs2_xattr_set_ctxt *ctxt)
2934 struct buffer_head *new_bh = NULL;
2935 struct ocfs2_xattr_block *xblk = NULL;
2936 int ret;
2937 struct ocfs2_xa_loc loc;
2939 if (!xs->xattr_bh) {
2940 ret = ocfs2_create_xattr_block(inode, xs->inode_bh, ctxt,
2941 0, &new_bh);
2942 if (ret) {
2943 mlog_errno(ret);
2944 goto end;
2947 xs->xattr_bh = new_bh;
2948 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2949 xs->header = &xblk->xb_attrs.xb_header;
2950 xs->base = (void *)xs->header;
2951 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2952 xs->here = xs->header->xh_entries;
2953 } else
2954 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2956 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
2957 ocfs2_init_xattr_block_xa_loc(&loc, inode, xs->xattr_bh,
2958 xs->not_found ? NULL : xs->here);
2960 ret = ocfs2_xa_set(&loc, xi, ctxt);
2961 if (!ret)
2962 xs->here = loc.xl_entry;
2963 else if ((ret != -ENOSPC) || ctxt->set_abort)
2964 goto end;
2965 else {
2966 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
2967 if (ret)
2968 goto end;
2972 if (le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)
2973 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
2975 end:
2976 return ret;
2979 /* Check whether the new xattr can be inserted into the inode. */
2980 static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
2981 struct ocfs2_xattr_info *xi,
2982 struct ocfs2_xattr_search *xs)
2984 struct ocfs2_xattr_entry *last;
2985 int free, i;
2986 size_t min_offs = xs->end - xs->base;
2988 if (!xs->header)
2989 return 0;
2991 last = xs->header->xh_entries;
2993 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
2994 size_t offs = le16_to_cpu(last->xe_name_offset);
2995 if (offs < min_offs)
2996 min_offs = offs;
2997 last += 1;
3000 free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
3001 if (free < 0)
3002 return 0;
3004 BUG_ON(!xs->not_found);
3006 if (free >= (sizeof(struct ocfs2_xattr_entry) + namevalue_size_xi(xi)))
3007 return 1;
3009 return 0;
3012 static int ocfs2_calc_xattr_set_need(struct inode *inode,
3013 struct ocfs2_dinode *di,
3014 struct ocfs2_xattr_info *xi,
3015 struct ocfs2_xattr_search *xis,
3016 struct ocfs2_xattr_search *xbs,
3017 int *clusters_need,
3018 int *meta_need,
3019 int *credits_need)
3021 int ret = 0, old_in_xb = 0;
3022 int clusters_add = 0, meta_add = 0, credits = 0;
3023 struct buffer_head *bh = NULL;
3024 struct ocfs2_xattr_block *xb = NULL;
3025 struct ocfs2_xattr_entry *xe = NULL;
3026 struct ocfs2_xattr_value_root *xv = NULL;
3027 char *base = NULL;
3028 int name_offset, name_len = 0;
3029 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
3030 xi->xi_value_len);
3031 u64 value_size;
3034 * Calculate the clusters we need to write.
3035 * No matter whether we replace an old one or add a new one,
3036 * we need this for writing.
3038 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
3039 credits += new_clusters *
3040 ocfs2_clusters_to_blocks(inode->i_sb, 1);
3042 if (xis->not_found && xbs->not_found) {
3043 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3045 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
3046 clusters_add += new_clusters;
3047 credits += ocfs2_calc_extend_credits(inode->i_sb,
3048 &def_xv.xv.xr_list,
3049 new_clusters);
3052 goto meta_guess;
3055 if (!xis->not_found) {
3056 xe = xis->here;
3057 name_offset = le16_to_cpu(xe->xe_name_offset);
3058 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3059 base = xis->base;
3060 credits += OCFS2_INODE_UPDATE_CREDITS;
3061 } else {
3062 int i, block_off = 0;
3063 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
3064 xe = xbs->here;
3065 name_offset = le16_to_cpu(xe->xe_name_offset);
3066 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3067 i = xbs->here - xbs->header->xh_entries;
3068 old_in_xb = 1;
3070 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
3071 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
3072 bucket_xh(xbs->bucket),
3073 i, &block_off,
3074 &name_offset);
3075 base = bucket_block(xbs->bucket, block_off);
3076 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3077 } else {
3078 base = xbs->base;
3079 credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
3084 * delete a xattr doesn't need metadata and cluster allocation.
3085 * so just calculate the credits and return.
3087 * The credits for removing the value tree will be extended
3088 * by ocfs2_remove_extent itself.
3090 if (!xi->xi_value) {
3091 if (!ocfs2_xattr_is_local(xe))
3092 credits += ocfs2_remove_extent_credits(inode->i_sb);
3094 goto out;
3097 /* do cluster allocation guess first. */
3098 value_size = le64_to_cpu(xe->xe_value_size);
3100 if (old_in_xb) {
3102 * In xattr set, we always try to set the xe in inode first,
3103 * so if it can be inserted into inode successfully, the old
3104 * one will be removed from the xattr block, and this xattr
3105 * will be inserted into inode as a new xattr in inode.
3107 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
3108 clusters_add += new_clusters;
3109 credits += ocfs2_remove_extent_credits(inode->i_sb) +
3110 OCFS2_INODE_UPDATE_CREDITS;
3111 if (!ocfs2_xattr_is_local(xe))
3112 credits += ocfs2_calc_extend_credits(
3113 inode->i_sb,
3114 &def_xv.xv.xr_list,
3115 new_clusters);
3116 goto out;
3120 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
3121 /* the new values will be stored outside. */
3122 u32 old_clusters = 0;
3124 if (!ocfs2_xattr_is_local(xe)) {
3125 old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
3126 value_size);
3127 xv = (struct ocfs2_xattr_value_root *)
3128 (base + name_offset + name_len);
3129 value_size = OCFS2_XATTR_ROOT_SIZE;
3130 } else
3131 xv = &def_xv.xv;
3133 if (old_clusters >= new_clusters) {
3134 credits += ocfs2_remove_extent_credits(inode->i_sb);
3135 goto out;
3136 } else {
3137 meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
3138 clusters_add += new_clusters - old_clusters;
3139 credits += ocfs2_calc_extend_credits(inode->i_sb,
3140 &xv->xr_list,
3141 new_clusters -
3142 old_clusters);
3143 if (value_size >= OCFS2_XATTR_ROOT_SIZE)
3144 goto out;
3146 } else {
3148 * Now the new value will be stored inside. So if the new
3149 * value is smaller than the size of value root or the old
3150 * value, we don't need any allocation, otherwise we have
3151 * to guess metadata allocation.
3153 if ((ocfs2_xattr_is_local(xe) &&
3154 (value_size >= xi->xi_value_len)) ||
3155 (!ocfs2_xattr_is_local(xe) &&
3156 OCFS2_XATTR_ROOT_SIZE >= xi->xi_value_len))
3157 goto out;
3160 meta_guess:
3161 /* calculate metadata allocation. */
3162 if (di->i_xattr_loc) {
3163 if (!xbs->xattr_bh) {
3164 ret = ocfs2_read_xattr_block(inode,
3165 le64_to_cpu(di->i_xattr_loc),
3166 &bh);
3167 if (ret) {
3168 mlog_errno(ret);
3169 goto out;
3172 xb = (struct ocfs2_xattr_block *)bh->b_data;
3173 } else
3174 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
3177 * If there is already an xattr tree, good, we can calculate
3178 * like other b-trees. Otherwise we may have the chance of
3179 * create a tree, the credit calculation is borrowed from
3180 * ocfs2_calc_extend_credits with root_el = NULL. And the
3181 * new tree will be cluster based, so no meta is needed.
3183 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
3184 struct ocfs2_extent_list *el =
3185 &xb->xb_attrs.xb_root.xt_list;
3186 meta_add += ocfs2_extend_meta_needed(el);
3187 credits += ocfs2_calc_extend_credits(inode->i_sb,
3188 el, 1);
3189 } else
3190 credits += OCFS2_SUBALLOC_ALLOC + 1;
3193 * This cluster will be used either for new bucket or for
3194 * new xattr block.
3195 * If the cluster size is the same as the bucket size, one
3196 * more is needed since we may need to extend the bucket
3197 * also.
3199 clusters_add += 1;
3200 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3201 if (OCFS2_XATTR_BUCKET_SIZE ==
3202 OCFS2_SB(inode->i_sb)->s_clustersize) {
3203 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3204 clusters_add += 1;
3206 } else {
3207 meta_add += 1;
3208 credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
3210 out:
3211 if (clusters_need)
3212 *clusters_need = clusters_add;
3213 if (meta_need)
3214 *meta_need = meta_add;
3215 if (credits_need)
3216 *credits_need = credits;
3217 brelse(bh);
3218 return ret;
3221 static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
3222 struct ocfs2_dinode *di,
3223 struct ocfs2_xattr_info *xi,
3224 struct ocfs2_xattr_search *xis,
3225 struct ocfs2_xattr_search *xbs,
3226 struct ocfs2_xattr_set_ctxt *ctxt,
3227 int extra_meta,
3228 int *credits)
3230 int clusters_add, meta_add, ret;
3231 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3233 memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
3235 ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
3237 ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
3238 &clusters_add, &meta_add, credits);
3239 if (ret) {
3240 mlog_errno(ret);
3241 return ret;
3244 meta_add += extra_meta;
3245 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
3246 "credits = %d\n", xi->xi_name, meta_add, clusters_add, *credits);
3248 if (meta_add) {
3249 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
3250 &ctxt->meta_ac);
3251 if (ret) {
3252 mlog_errno(ret);
3253 goto out;
3257 if (clusters_add) {
3258 ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
3259 if (ret)
3260 mlog_errno(ret);
3262 out:
3263 if (ret) {
3264 if (ctxt->meta_ac) {
3265 ocfs2_free_alloc_context(ctxt->meta_ac);
3266 ctxt->meta_ac = NULL;
3270 * We cannot have an error and a non null ctxt->data_ac.
3274 return ret;
3277 static int __ocfs2_xattr_set_handle(struct inode *inode,
3278 struct ocfs2_dinode *di,
3279 struct ocfs2_xattr_info *xi,
3280 struct ocfs2_xattr_search *xis,
3281 struct ocfs2_xattr_search *xbs,
3282 struct ocfs2_xattr_set_ctxt *ctxt)
3284 int ret = 0, credits, old_found;
3286 if (!xi->xi_value) {
3287 /* Remove existing extended attribute */
3288 if (!xis->not_found)
3289 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3290 else if (!xbs->not_found)
3291 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3292 } else {
3293 /* We always try to set extended attribute into inode first*/
3294 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3295 if (!ret && !xbs->not_found) {
3297 * If succeed and that extended attribute existing in
3298 * external block, then we will remove it.
3300 xi->xi_value = NULL;
3301 xi->xi_value_len = 0;
3303 old_found = xis->not_found;
3304 xis->not_found = -ENODATA;
3305 ret = ocfs2_calc_xattr_set_need(inode,
3308 xis,
3309 xbs,
3310 NULL,
3311 NULL,
3312 &credits);
3313 xis->not_found = old_found;
3314 if (ret) {
3315 mlog_errno(ret);
3316 goto out;
3319 ret = ocfs2_extend_trans(ctxt->handle, credits);
3320 if (ret) {
3321 mlog_errno(ret);
3322 goto out;
3324 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3325 } else if ((ret == -ENOSPC) && !ctxt->set_abort) {
3326 if (di->i_xattr_loc && !xbs->xattr_bh) {
3327 ret = ocfs2_xattr_block_find(inode,
3328 xi->xi_name_index,
3329 xi->xi_name, xbs);
3330 if (ret)
3331 goto out;
3333 old_found = xis->not_found;
3334 xis->not_found = -ENODATA;
3335 ret = ocfs2_calc_xattr_set_need(inode,
3338 xis,
3339 xbs,
3340 NULL,
3341 NULL,
3342 &credits);
3343 xis->not_found = old_found;
3344 if (ret) {
3345 mlog_errno(ret);
3346 goto out;
3349 ret = ocfs2_extend_trans(ctxt->handle, credits);
3350 if (ret) {
3351 mlog_errno(ret);
3352 goto out;
3356 * If no space in inode, we will set extended attribute
3357 * into external block.
3359 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3360 if (ret)
3361 goto out;
3362 if (!xis->not_found) {
3364 * If succeed and that extended attribute
3365 * existing in inode, we will remove it.
3367 xi->xi_value = NULL;
3368 xi->xi_value_len = 0;
3369 xbs->not_found = -ENODATA;
3370 ret = ocfs2_calc_xattr_set_need(inode,
3373 xis,
3374 xbs,
3375 NULL,
3376 NULL,
3377 &credits);
3378 if (ret) {
3379 mlog_errno(ret);
3380 goto out;
3383 ret = ocfs2_extend_trans(ctxt->handle, credits);
3384 if (ret) {
3385 mlog_errno(ret);
3386 goto out;
3388 ret = ocfs2_xattr_ibody_set(inode, xi,
3389 xis, ctxt);
3394 if (!ret) {
3395 /* Update inode ctime. */
3396 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
3397 xis->inode_bh,
3398 OCFS2_JOURNAL_ACCESS_WRITE);
3399 if (ret) {
3400 mlog_errno(ret);
3401 goto out;
3404 inode->i_ctime = CURRENT_TIME;
3405 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
3406 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
3407 ocfs2_journal_dirty(ctxt->handle, xis->inode_bh);
3409 out:
3410 return ret;
3414 * This function only called duing creating inode
3415 * for init security/acl xattrs of the new inode.
3416 * All transanction credits have been reserved in mknod.
3418 int ocfs2_xattr_set_handle(handle_t *handle,
3419 struct inode *inode,
3420 struct buffer_head *di_bh,
3421 int name_index,
3422 const char *name,
3423 const void *value,
3424 size_t value_len,
3425 int flags,
3426 struct ocfs2_alloc_context *meta_ac,
3427 struct ocfs2_alloc_context *data_ac)
3429 struct ocfs2_dinode *di;
3430 int ret;
3432 struct ocfs2_xattr_info xi = {
3433 .xi_name_index = name_index,
3434 .xi_name = name,
3435 .xi_name_len = strlen(name),
3436 .xi_value = value,
3437 .xi_value_len = value_len,
3440 struct ocfs2_xattr_search xis = {
3441 .not_found = -ENODATA,
3444 struct ocfs2_xattr_search xbs = {
3445 .not_found = -ENODATA,
3448 struct ocfs2_xattr_set_ctxt ctxt = {
3449 .handle = handle,
3450 .meta_ac = meta_ac,
3451 .data_ac = data_ac,
3454 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3455 return -EOPNOTSUPP;
3458 * In extreme situation, may need xattr bucket when
3459 * block size is too small. And we have already reserved
3460 * the credits for bucket in mknod.
3462 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) {
3463 xbs.bucket = ocfs2_xattr_bucket_new(inode);
3464 if (!xbs.bucket) {
3465 mlog_errno(-ENOMEM);
3466 return -ENOMEM;
3470 xis.inode_bh = xbs.inode_bh = di_bh;
3471 di = (struct ocfs2_dinode *)di_bh->b_data;
3473 down_write(&OCFS2_I(inode)->ip_xattr_sem);
3475 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3476 if (ret)
3477 goto cleanup;
3478 if (xis.not_found) {
3479 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3480 if (ret)
3481 goto cleanup;
3484 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3486 cleanup:
3487 up_write(&OCFS2_I(inode)->ip_xattr_sem);
3488 brelse(xbs.xattr_bh);
3489 ocfs2_xattr_bucket_free(xbs.bucket);
3491 return ret;
3495 * ocfs2_xattr_set()
3497 * Set, replace or remove an extended attribute for this inode.
3498 * value is NULL to remove an existing extended attribute, else either
3499 * create or replace an extended attribute.
3501 int ocfs2_xattr_set(struct inode *inode,
3502 int name_index,
3503 const char *name,
3504 const void *value,
3505 size_t value_len,
3506 int flags)
3508 struct buffer_head *di_bh = NULL;
3509 struct ocfs2_dinode *di;
3510 int ret, credits, ref_meta = 0, ref_credits = 0;
3511 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3512 struct inode *tl_inode = osb->osb_tl_inode;
3513 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
3514 struct ocfs2_refcount_tree *ref_tree = NULL;
3516 struct ocfs2_xattr_info xi = {
3517 .xi_name_index = name_index,
3518 .xi_name = name,
3519 .xi_name_len = strlen(name),
3520 .xi_value = value,
3521 .xi_value_len = value_len,
3524 struct ocfs2_xattr_search xis = {
3525 .not_found = -ENODATA,
3528 struct ocfs2_xattr_search xbs = {
3529 .not_found = -ENODATA,
3532 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3533 return -EOPNOTSUPP;
3536 * Only xbs will be used on indexed trees. xis doesn't need a
3537 * bucket.
3539 xbs.bucket = ocfs2_xattr_bucket_new(inode);
3540 if (!xbs.bucket) {
3541 mlog_errno(-ENOMEM);
3542 return -ENOMEM;
3545 ret = ocfs2_inode_lock(inode, &di_bh, 1);
3546 if (ret < 0) {
3547 mlog_errno(ret);
3548 goto cleanup_nolock;
3550 xis.inode_bh = xbs.inode_bh = di_bh;
3551 di = (struct ocfs2_dinode *)di_bh->b_data;
3553 down_write(&OCFS2_I(inode)->ip_xattr_sem);
3555 * Scan inode and external block to find the same name
3556 * extended attribute and collect search infomation.
3558 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3559 if (ret)
3560 goto cleanup;
3561 if (xis.not_found) {
3562 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3563 if (ret)
3564 goto cleanup;
3567 if (xis.not_found && xbs.not_found) {
3568 ret = -ENODATA;
3569 if (flags & XATTR_REPLACE)
3570 goto cleanup;
3571 ret = 0;
3572 if (!value)
3573 goto cleanup;
3574 } else {
3575 ret = -EEXIST;
3576 if (flags & XATTR_CREATE)
3577 goto cleanup;
3580 /* Check whether the value is refcounted and do some prepartion. */
3581 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL &&
3582 (!xis.not_found || !xbs.not_found)) {
3583 ret = ocfs2_prepare_refcount_xattr(inode, di, &xi,
3584 &xis, &xbs, &ref_tree,
3585 &ref_meta, &ref_credits);
3586 if (ret) {
3587 mlog_errno(ret);
3588 goto cleanup;
3592 mutex_lock(&tl_inode->i_mutex);
3594 if (ocfs2_truncate_log_needs_flush(osb)) {
3595 ret = __ocfs2_flush_truncate_log(osb);
3596 if (ret < 0) {
3597 mutex_unlock(&tl_inode->i_mutex);
3598 mlog_errno(ret);
3599 goto cleanup;
3602 mutex_unlock(&tl_inode->i_mutex);
3604 ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
3605 &xbs, &ctxt, ref_meta, &credits);
3606 if (ret) {
3607 mlog_errno(ret);
3608 goto cleanup;
3611 /* we need to update inode's ctime field, so add credit for it. */
3612 credits += OCFS2_INODE_UPDATE_CREDITS;
3613 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
3614 if (IS_ERR(ctxt.handle)) {
3615 ret = PTR_ERR(ctxt.handle);
3616 mlog_errno(ret);
3617 goto cleanup;
3620 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3622 ocfs2_commit_trans(osb, ctxt.handle);
3624 if (ctxt.data_ac)
3625 ocfs2_free_alloc_context(ctxt.data_ac);
3626 if (ctxt.meta_ac)
3627 ocfs2_free_alloc_context(ctxt.meta_ac);
3628 if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
3629 ocfs2_schedule_truncate_log_flush(osb, 1);
3630 ocfs2_run_deallocs(osb, &ctxt.dealloc);
3632 cleanup:
3633 if (ref_tree)
3634 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
3635 up_write(&OCFS2_I(inode)->ip_xattr_sem);
3636 if (!value && !ret) {
3637 ret = ocfs2_try_remove_refcount_tree(inode, di_bh);
3638 if (ret)
3639 mlog_errno(ret);
3641 ocfs2_inode_unlock(inode, 1);
3642 cleanup_nolock:
3643 brelse(di_bh);
3644 brelse(xbs.xattr_bh);
3645 ocfs2_xattr_bucket_free(xbs.bucket);
3647 return ret;
3651 * Find the xattr extent rec which may contains name_hash.
3652 * e_cpos will be the first name hash of the xattr rec.
3653 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
3655 static int ocfs2_xattr_get_rec(struct inode *inode,
3656 u32 name_hash,
3657 u64 *p_blkno,
3658 u32 *e_cpos,
3659 u32 *num_clusters,
3660 struct ocfs2_extent_list *el)
3662 int ret = 0, i;
3663 struct buffer_head *eb_bh = NULL;
3664 struct ocfs2_extent_block *eb;
3665 struct ocfs2_extent_rec *rec = NULL;
3666 u64 e_blkno = 0;
3668 if (el->l_tree_depth) {
3669 ret = ocfs2_find_leaf(INODE_CACHE(inode), el, name_hash,
3670 &eb_bh);
3671 if (ret) {
3672 mlog_errno(ret);
3673 goto out;
3676 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
3677 el = &eb->h_list;
3679 if (el->l_tree_depth) {
3680 ocfs2_error(inode->i_sb,
3681 "Inode %lu has non zero tree depth in "
3682 "xattr tree block %llu\n", inode->i_ino,
3683 (unsigned long long)eb_bh->b_blocknr);
3684 ret = -EROFS;
3685 goto out;
3689 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
3690 rec = &el->l_recs[i];
3692 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
3693 e_blkno = le64_to_cpu(rec->e_blkno);
3694 break;
3698 if (!e_blkno) {
3699 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
3700 "record (%u, %u, 0) in xattr", inode->i_ino,
3701 le32_to_cpu(rec->e_cpos),
3702 ocfs2_rec_clusters(el, rec));
3703 ret = -EROFS;
3704 goto out;
3707 *p_blkno = le64_to_cpu(rec->e_blkno);
3708 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
3709 if (e_cpos)
3710 *e_cpos = le32_to_cpu(rec->e_cpos);
3711 out:
3712 brelse(eb_bh);
3713 return ret;
3716 typedef int (xattr_bucket_func)(struct inode *inode,
3717 struct ocfs2_xattr_bucket *bucket,
3718 void *para);
3720 static int ocfs2_find_xe_in_bucket(struct inode *inode,
3721 struct ocfs2_xattr_bucket *bucket,
3722 int name_index,
3723 const char *name,
3724 u32 name_hash,
3725 u16 *xe_index,
3726 int *found)
3728 int i, ret = 0, cmp = 1, block_off, new_offset;
3729 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
3730 size_t name_len = strlen(name);
3731 struct ocfs2_xattr_entry *xe = NULL;
3732 char *xe_name;
3735 * We don't use binary search in the bucket because there
3736 * may be multiple entries with the same name hash.
3738 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3739 xe = &xh->xh_entries[i];
3741 if (name_hash > le32_to_cpu(xe->xe_name_hash))
3742 continue;
3743 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
3744 break;
3746 cmp = name_index - ocfs2_xattr_get_type(xe);
3747 if (!cmp)
3748 cmp = name_len - xe->xe_name_len;
3749 if (cmp)
3750 continue;
3752 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
3755 &block_off,
3756 &new_offset);
3757 if (ret) {
3758 mlog_errno(ret);
3759 break;
3763 xe_name = bucket_block(bucket, block_off) + new_offset;
3764 if (!memcmp(name, xe_name, name_len)) {
3765 *xe_index = i;
3766 *found = 1;
3767 ret = 0;
3768 break;
3772 return ret;
3776 * Find the specified xattr entry in a series of buckets.
3777 * This series start from p_blkno and last for num_clusters.
3778 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
3779 * the num of the valid buckets.
3781 * Return the buffer_head this xattr should reside in. And if the xattr's
3782 * hash is in the gap of 2 buckets, return the lower bucket.
3784 static int ocfs2_xattr_bucket_find(struct inode *inode,
3785 int name_index,
3786 const char *name,
3787 u32 name_hash,
3788 u64 p_blkno,
3789 u32 first_hash,
3790 u32 num_clusters,
3791 struct ocfs2_xattr_search *xs)
3793 int ret, found = 0;
3794 struct ocfs2_xattr_header *xh = NULL;
3795 struct ocfs2_xattr_entry *xe = NULL;
3796 u16 index = 0;
3797 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3798 int low_bucket = 0, bucket, high_bucket;
3799 struct ocfs2_xattr_bucket *search;
3800 u32 last_hash;
3801 u64 blkno, lower_blkno = 0;
3803 search = ocfs2_xattr_bucket_new(inode);
3804 if (!search) {
3805 ret = -ENOMEM;
3806 mlog_errno(ret);
3807 goto out;
3810 ret = ocfs2_read_xattr_bucket(search, p_blkno);
3811 if (ret) {
3812 mlog_errno(ret);
3813 goto out;
3816 xh = bucket_xh(search);
3817 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
3818 while (low_bucket <= high_bucket) {
3819 ocfs2_xattr_bucket_relse(search);
3821 bucket = (low_bucket + high_bucket) / 2;
3822 blkno = p_blkno + bucket * blk_per_bucket;
3823 ret = ocfs2_read_xattr_bucket(search, blkno);
3824 if (ret) {
3825 mlog_errno(ret);
3826 goto out;
3829 xh = bucket_xh(search);
3830 xe = &xh->xh_entries[0];
3831 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
3832 high_bucket = bucket - 1;
3833 continue;
3837 * Check whether the hash of the last entry in our
3838 * bucket is larger than the search one. for an empty
3839 * bucket, the last one is also the first one.
3841 if (xh->xh_count)
3842 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
3844 last_hash = le32_to_cpu(xe->xe_name_hash);
3846 /* record lower_blkno which may be the insert place. */
3847 lower_blkno = blkno;
3849 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
3850 low_bucket = bucket + 1;
3851 continue;
3854 /* the searched xattr should reside in this bucket if exists. */
3855 ret = ocfs2_find_xe_in_bucket(inode, search,
3856 name_index, name, name_hash,
3857 &index, &found);
3858 if (ret) {
3859 mlog_errno(ret);
3860 goto out;
3862 break;
3866 * Record the bucket we have found.
3867 * When the xattr's hash value is in the gap of 2 buckets, we will
3868 * always set it to the previous bucket.
3870 if (!lower_blkno)
3871 lower_blkno = p_blkno;
3873 /* This should be in cache - we just read it during the search */
3874 ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
3875 if (ret) {
3876 mlog_errno(ret);
3877 goto out;
3880 xs->header = bucket_xh(xs->bucket);
3881 xs->base = bucket_block(xs->bucket, 0);
3882 xs->end = xs->base + inode->i_sb->s_blocksize;
3884 if (found) {
3885 xs->here = &xs->header->xh_entries[index];
3886 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
3887 (unsigned long long)bucket_blkno(xs->bucket), index);
3888 } else
3889 ret = -ENODATA;
3891 out:
3892 ocfs2_xattr_bucket_free(search);
3893 return ret;
3896 static int ocfs2_xattr_index_block_find(struct inode *inode,
3897 struct buffer_head *root_bh,
3898 int name_index,
3899 const char *name,
3900 struct ocfs2_xattr_search *xs)
3902 int ret;
3903 struct ocfs2_xattr_block *xb =
3904 (struct ocfs2_xattr_block *)root_bh->b_data;
3905 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3906 struct ocfs2_extent_list *el = &xb_root->xt_list;
3907 u64 p_blkno = 0;
3908 u32 first_hash, num_clusters = 0;
3909 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
3911 if (le16_to_cpu(el->l_next_free_rec) == 0)
3912 return -ENODATA;
3914 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
3915 name, name_hash, name_index);
3917 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
3918 &num_clusters, el);
3919 if (ret) {
3920 mlog_errno(ret);
3921 goto out;
3924 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
3926 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
3927 "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
3928 first_hash);
3930 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
3931 p_blkno, first_hash, num_clusters, xs);
3933 out:
3934 return ret;
3937 static int ocfs2_iterate_xattr_buckets(struct inode *inode,
3938 u64 blkno,
3939 u32 clusters,
3940 xattr_bucket_func *func,
3941 void *para)
3943 int i, ret = 0;
3944 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
3945 u32 num_buckets = clusters * bpc;
3946 struct ocfs2_xattr_bucket *bucket;
3948 bucket = ocfs2_xattr_bucket_new(inode);
3949 if (!bucket) {
3950 mlog_errno(-ENOMEM);
3951 return -ENOMEM;
3954 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
3955 clusters, (unsigned long long)blkno);
3957 for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
3958 ret = ocfs2_read_xattr_bucket(bucket, blkno);
3959 if (ret) {
3960 mlog_errno(ret);
3961 break;
3965 * The real bucket num in this series of blocks is stored
3966 * in the 1st bucket.
3968 if (i == 0)
3969 num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
3971 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
3972 (unsigned long long)blkno,
3973 le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
3974 if (func) {
3975 ret = func(inode, bucket, para);
3976 if (ret && ret != -ERANGE)
3977 mlog_errno(ret);
3978 /* Fall through to bucket_relse() */
3981 ocfs2_xattr_bucket_relse(bucket);
3982 if (ret)
3983 break;
3986 ocfs2_xattr_bucket_free(bucket);
3987 return ret;
3990 struct ocfs2_xattr_tree_list {
3991 char *buffer;
3992 size_t buffer_size;
3993 size_t result;
3996 static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
3997 struct ocfs2_xattr_header *xh,
3998 int index,
3999 int *block_off,
4000 int *new_offset)
4002 u16 name_offset;
4004 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
4005 return -EINVAL;
4007 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
4009 *block_off = name_offset >> sb->s_blocksize_bits;
4010 *new_offset = name_offset % sb->s_blocksize;
4012 return 0;
4015 static int ocfs2_list_xattr_bucket(struct inode *inode,
4016 struct ocfs2_xattr_bucket *bucket,
4017 void *para)
4019 int ret = 0, type;
4020 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
4021 int i, block_off, new_offset;
4022 const char *prefix, *name;
4024 for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
4025 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
4026 type = ocfs2_xattr_get_type(entry);
4027 prefix = ocfs2_xattr_prefix(type);
4029 if (prefix) {
4030 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
4031 bucket_xh(bucket),
4033 &block_off,
4034 &new_offset);
4035 if (ret)
4036 break;
4038 name = (const char *)bucket_block(bucket, block_off) +
4039 new_offset;
4040 ret = ocfs2_xattr_list_entry(xl->buffer,
4041 xl->buffer_size,
4042 &xl->result,
4043 prefix, name,
4044 entry->xe_name_len);
4045 if (ret)
4046 break;
4050 return ret;
4053 static int ocfs2_iterate_xattr_index_block(struct inode *inode,
4054 struct buffer_head *blk_bh,
4055 xattr_tree_rec_func *rec_func,
4056 void *para)
4058 struct ocfs2_xattr_block *xb =
4059 (struct ocfs2_xattr_block *)blk_bh->b_data;
4060 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
4061 int ret = 0;
4062 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
4063 u64 p_blkno = 0;
4065 if (!el->l_next_free_rec || !rec_func)
4066 return 0;
4068 while (name_hash > 0) {
4069 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
4070 &e_cpos, &num_clusters, el);
4071 if (ret) {
4072 mlog_errno(ret);
4073 break;
4076 ret = rec_func(inode, blk_bh, p_blkno, e_cpos,
4077 num_clusters, para);
4078 if (ret) {
4079 if (ret != -ERANGE)
4080 mlog_errno(ret);
4081 break;
4084 if (e_cpos == 0)
4085 break;
4087 name_hash = e_cpos - 1;
4090 return ret;
4094 static int ocfs2_list_xattr_tree_rec(struct inode *inode,
4095 struct buffer_head *root_bh,
4096 u64 blkno, u32 cpos, u32 len, void *para)
4098 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
4099 ocfs2_list_xattr_bucket, para);
4102 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
4103 struct buffer_head *blk_bh,
4104 char *buffer,
4105 size_t buffer_size)
4107 int ret;
4108 struct ocfs2_xattr_tree_list xl = {
4109 .buffer = buffer,
4110 .buffer_size = buffer_size,
4111 .result = 0,
4114 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
4115 ocfs2_list_xattr_tree_rec, &xl);
4116 if (ret) {
4117 mlog_errno(ret);
4118 goto out;
4121 ret = xl.result;
4122 out:
4123 return ret;
4126 static int cmp_xe(const void *a, const void *b)
4128 const struct ocfs2_xattr_entry *l = a, *r = b;
4129 u32 l_hash = le32_to_cpu(l->xe_name_hash);
4130 u32 r_hash = le32_to_cpu(r->xe_name_hash);
4132 if (l_hash > r_hash)
4133 return 1;
4134 if (l_hash < r_hash)
4135 return -1;
4136 return 0;
4139 static void swap_xe(void *a, void *b, int size)
4141 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
4143 tmp = *l;
4144 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
4145 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
4149 * When the ocfs2_xattr_block is filled up, new bucket will be created
4150 * and all the xattr entries will be moved to the new bucket.
4151 * The header goes at the start of the bucket, and the names+values are
4152 * filled from the end. This is why *target starts as the last buffer.
4153 * Note: we need to sort the entries since they are not saved in order
4154 * in the ocfs2_xattr_block.
4156 static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
4157 struct buffer_head *xb_bh,
4158 struct ocfs2_xattr_bucket *bucket)
4160 int i, blocksize = inode->i_sb->s_blocksize;
4161 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4162 u16 offset, size, off_change;
4163 struct ocfs2_xattr_entry *xe;
4164 struct ocfs2_xattr_block *xb =
4165 (struct ocfs2_xattr_block *)xb_bh->b_data;
4166 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
4167 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
4168 u16 count = le16_to_cpu(xb_xh->xh_count);
4169 char *src = xb_bh->b_data;
4170 char *target = bucket_block(bucket, blks - 1);
4172 mlog(0, "cp xattr from block %llu to bucket %llu\n",
4173 (unsigned long long)xb_bh->b_blocknr,
4174 (unsigned long long)bucket_blkno(bucket));
4176 for (i = 0; i < blks; i++)
4177 memset(bucket_block(bucket, i), 0, blocksize);
4180 * Since the xe_name_offset is based on ocfs2_xattr_header,
4181 * there is a offset change corresponding to the change of
4182 * ocfs2_xattr_header's position.
4184 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
4185 xe = &xb_xh->xh_entries[count - 1];
4186 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
4187 size = blocksize - offset;
4189 /* copy all the names and values. */
4190 memcpy(target + offset, src + offset, size);
4192 /* Init new header now. */
4193 xh->xh_count = xb_xh->xh_count;
4194 xh->xh_num_buckets = cpu_to_le16(1);
4195 xh->xh_name_value_len = cpu_to_le16(size);
4196 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
4198 /* copy all the entries. */
4199 target = bucket_block(bucket, 0);
4200 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
4201 size = count * sizeof(struct ocfs2_xattr_entry);
4202 memcpy(target + offset, (char *)xb_xh + offset, size);
4204 /* Change the xe offset for all the xe because of the move. */
4205 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
4206 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
4207 for (i = 0; i < count; i++)
4208 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
4210 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
4211 offset, size, off_change);
4213 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
4214 cmp_xe, swap_xe);
4218 * After we move xattr from block to index btree, we have to
4219 * update ocfs2_xattr_search to the new xe and base.
4221 * When the entry is in xattr block, xattr_bh indicates the storage place.
4222 * While if the entry is in index b-tree, "bucket" indicates the
4223 * real place of the xattr.
4225 static void ocfs2_xattr_update_xattr_search(struct inode *inode,
4226 struct ocfs2_xattr_search *xs,
4227 struct buffer_head *old_bh)
4229 char *buf = old_bh->b_data;
4230 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
4231 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
4232 int i;
4234 xs->header = bucket_xh(xs->bucket);
4235 xs->base = bucket_block(xs->bucket, 0);
4236 xs->end = xs->base + inode->i_sb->s_blocksize;
4238 if (xs->not_found)
4239 return;
4241 i = xs->here - old_xh->xh_entries;
4242 xs->here = &xs->header->xh_entries[i];
4245 static int ocfs2_xattr_create_index_block(struct inode *inode,
4246 struct ocfs2_xattr_search *xs,
4247 struct ocfs2_xattr_set_ctxt *ctxt)
4249 int ret;
4250 u32 bit_off, len;
4251 u64 blkno;
4252 handle_t *handle = ctxt->handle;
4253 struct ocfs2_inode_info *oi = OCFS2_I(inode);
4254 struct buffer_head *xb_bh = xs->xattr_bh;
4255 struct ocfs2_xattr_block *xb =
4256 (struct ocfs2_xattr_block *)xb_bh->b_data;
4257 struct ocfs2_xattr_tree_root *xr;
4258 u16 xb_flags = le16_to_cpu(xb->xb_flags);
4260 mlog(0, "create xattr index block for %llu\n",
4261 (unsigned long long)xb_bh->b_blocknr);
4263 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
4264 BUG_ON(!xs->bucket);
4266 down_write(&oi->ip_alloc_sem);
4268 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), xb_bh,
4269 OCFS2_JOURNAL_ACCESS_WRITE);
4270 if (ret) {
4271 mlog_errno(ret);
4272 goto out;
4275 ret = __ocfs2_claim_clusters(handle, ctxt->data_ac,
4276 1, 1, &bit_off, &len);
4277 if (ret) {
4278 mlog_errno(ret);
4279 goto out;
4283 * The bucket may spread in many blocks, and
4284 * we will only touch the 1st block and the last block
4285 * in the whole bucket(one for entry and one for data).
4287 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
4289 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
4290 (unsigned long long)blkno);
4292 ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
4293 if (ret) {
4294 mlog_errno(ret);
4295 goto out;
4298 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
4299 OCFS2_JOURNAL_ACCESS_CREATE);
4300 if (ret) {
4301 mlog_errno(ret);
4302 goto out;
4305 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
4306 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
4308 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
4310 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
4311 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
4312 offsetof(struct ocfs2_xattr_block, xb_attrs));
4314 xr = &xb->xb_attrs.xb_root;
4315 xr->xt_clusters = cpu_to_le32(1);
4316 xr->xt_last_eb_blk = 0;
4317 xr->xt_list.l_tree_depth = 0;
4318 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
4319 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
4321 xr->xt_list.l_recs[0].e_cpos = 0;
4322 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
4323 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
4325 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
4327 ocfs2_journal_dirty(handle, xb_bh);
4329 out:
4330 up_write(&oi->ip_alloc_sem);
4332 return ret;
4335 static int cmp_xe_offset(const void *a, const void *b)
4337 const struct ocfs2_xattr_entry *l = a, *r = b;
4338 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
4339 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
4341 if (l_name_offset < r_name_offset)
4342 return 1;
4343 if (l_name_offset > r_name_offset)
4344 return -1;
4345 return 0;
4349 * defrag a xattr bucket if we find that the bucket has some
4350 * holes beteen name/value pairs.
4351 * We will move all the name/value pairs to the end of the bucket
4352 * so that we can spare some space for insertion.
4354 static int ocfs2_defrag_xattr_bucket(struct inode *inode,
4355 handle_t *handle,
4356 struct ocfs2_xattr_bucket *bucket)
4358 int ret, i;
4359 size_t end, offset, len;
4360 struct ocfs2_xattr_header *xh;
4361 char *entries, *buf, *bucket_buf = NULL;
4362 u64 blkno = bucket_blkno(bucket);
4363 u16 xh_free_start;
4364 size_t blocksize = inode->i_sb->s_blocksize;
4365 struct ocfs2_xattr_entry *xe;
4368 * In order to make the operation more efficient and generic,
4369 * we copy all the blocks into a contiguous memory and do the
4370 * defragment there, so if anything is error, we will not touch
4371 * the real block.
4373 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
4374 if (!bucket_buf) {
4375 ret = -EIO;
4376 goto out;
4379 buf = bucket_buf;
4380 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4381 memcpy(buf, bucket_block(bucket, i), blocksize);
4383 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
4384 OCFS2_JOURNAL_ACCESS_WRITE);
4385 if (ret < 0) {
4386 mlog_errno(ret);
4387 goto out;
4390 xh = (struct ocfs2_xattr_header *)bucket_buf;
4391 entries = (char *)xh->xh_entries;
4392 xh_free_start = le16_to_cpu(xh->xh_free_start);
4394 mlog(0, "adjust xattr bucket in %llu, count = %u, "
4395 "xh_free_start = %u, xh_name_value_len = %u.\n",
4396 (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
4397 xh_free_start, le16_to_cpu(xh->xh_name_value_len));
4400 * sort all the entries by their offset.
4401 * the largest will be the first, so that we can
4402 * move them to the end one by one.
4404 sort(entries, le16_to_cpu(xh->xh_count),
4405 sizeof(struct ocfs2_xattr_entry),
4406 cmp_xe_offset, swap_xe);
4408 /* Move all name/values to the end of the bucket. */
4409 xe = xh->xh_entries;
4410 end = OCFS2_XATTR_BUCKET_SIZE;
4411 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
4412 offset = le16_to_cpu(xe->xe_name_offset);
4413 len = namevalue_size_xe(xe);
4416 * We must make sure that the name/value pair
4417 * exist in the same block. So adjust end to
4418 * the previous block end if needed.
4420 if (((end - len) / blocksize !=
4421 (end - 1) / blocksize))
4422 end = end - end % blocksize;
4424 if (end > offset + len) {
4425 memmove(bucket_buf + end - len,
4426 bucket_buf + offset, len);
4427 xe->xe_name_offset = cpu_to_le16(end - len);
4430 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
4431 "bucket %llu\n", (unsigned long long)blkno);
4433 end -= len;
4436 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
4437 "bucket %llu\n", (unsigned long long)blkno);
4439 if (xh_free_start == end)
4440 goto out;
4442 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
4443 xh->xh_free_start = cpu_to_le16(end);
4445 /* sort the entries by their name_hash. */
4446 sort(entries, le16_to_cpu(xh->xh_count),
4447 sizeof(struct ocfs2_xattr_entry),
4448 cmp_xe, swap_xe);
4450 buf = bucket_buf;
4451 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4452 memcpy(bucket_block(bucket, i), buf, blocksize);
4453 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
4455 out:
4456 kfree(bucket_buf);
4457 return ret;
4461 * prev_blkno points to the start of an existing extent. new_blkno
4462 * points to a newly allocated extent. Because we know each of our
4463 * clusters contains more than bucket, we can easily split one cluster
4464 * at a bucket boundary. So we take the last cluster of the existing
4465 * extent and split it down the middle. We move the last half of the
4466 * buckets in the last cluster of the existing extent over to the new
4467 * extent.
4469 * first_bh is the buffer at prev_blkno so we can update the existing
4470 * extent's bucket count. header_bh is the bucket were we were hoping
4471 * to insert our xattr. If the bucket move places the target in the new
4472 * extent, we'll update first_bh and header_bh after modifying the old
4473 * extent.
4475 * first_hash will be set as the 1st xe's name_hash in the new extent.
4477 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
4478 handle_t *handle,
4479 struct ocfs2_xattr_bucket *first,
4480 struct ocfs2_xattr_bucket *target,
4481 u64 new_blkno,
4482 u32 num_clusters,
4483 u32 *first_hash)
4485 int ret;
4486 struct super_block *sb = inode->i_sb;
4487 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb);
4488 int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
4489 int to_move = num_buckets / 2;
4490 u64 src_blkno;
4491 u64 last_cluster_blkno = bucket_blkno(first) +
4492 ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1));
4494 BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets);
4495 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize);
4497 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
4498 (unsigned long long)last_cluster_blkno, (unsigned long long)new_blkno);
4500 ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first),
4501 last_cluster_blkno, new_blkno,
4502 to_move, first_hash);
4503 if (ret) {
4504 mlog_errno(ret);
4505 goto out;
4508 /* This is the first bucket that got moved */
4509 src_blkno = last_cluster_blkno + (to_move * blks_per_bucket);
4512 * If the target bucket was part of the moved buckets, we need to
4513 * update first and target.
4515 if (bucket_blkno(target) >= src_blkno) {
4516 /* Find the block for the new target bucket */
4517 src_blkno = new_blkno +
4518 (bucket_blkno(target) - src_blkno);
4520 ocfs2_xattr_bucket_relse(first);
4521 ocfs2_xattr_bucket_relse(target);
4524 * These shouldn't fail - the buffers are in the
4525 * journal from ocfs2_cp_xattr_bucket().
4527 ret = ocfs2_read_xattr_bucket(first, new_blkno);
4528 if (ret) {
4529 mlog_errno(ret);
4530 goto out;
4532 ret = ocfs2_read_xattr_bucket(target, src_blkno);
4533 if (ret)
4534 mlog_errno(ret);
4538 out:
4539 return ret;
4543 * Find the suitable pos when we divide a bucket into 2.
4544 * We have to make sure the xattrs with the same hash value exist
4545 * in the same bucket.
4547 * If this ocfs2_xattr_header covers more than one hash value, find a
4548 * place where the hash value changes. Try to find the most even split.
4549 * The most common case is that all entries have different hash values,
4550 * and the first check we make will find a place to split.
4552 static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
4554 struct ocfs2_xattr_entry *entries = xh->xh_entries;
4555 int count = le16_to_cpu(xh->xh_count);
4556 int delta, middle = count / 2;
4559 * We start at the middle. Each step gets farther away in both
4560 * directions. We therefore hit the change in hash value
4561 * nearest to the middle. Note that this loop does not execute for
4562 * count < 2.
4564 for (delta = 0; delta < middle; delta++) {
4565 /* Let's check delta earlier than middle */
4566 if (cmp_xe(&entries[middle - delta - 1],
4567 &entries[middle - delta]))
4568 return middle - delta;
4570 /* For even counts, don't walk off the end */
4571 if ((middle + delta + 1) == count)
4572 continue;
4574 /* Now try delta past middle */
4575 if (cmp_xe(&entries[middle + delta],
4576 &entries[middle + delta + 1]))
4577 return middle + delta + 1;
4580 /* Every entry had the same hash */
4581 return count;
4585 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
4586 * first_hash will record the 1st hash of the new bucket.
4588 * Normally half of the xattrs will be moved. But we have to make
4589 * sure that the xattrs with the same hash value are stored in the
4590 * same bucket. If all the xattrs in this bucket have the same hash
4591 * value, the new bucket will be initialized as an empty one and the
4592 * first_hash will be initialized as (hash_value+1).
4594 static int ocfs2_divide_xattr_bucket(struct inode *inode,
4595 handle_t *handle,
4596 u64 blk,
4597 u64 new_blk,
4598 u32 *first_hash,
4599 int new_bucket_head)
4601 int ret, i;
4602 int count, start, len, name_value_len = 0, name_offset = 0;
4603 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
4604 struct ocfs2_xattr_header *xh;
4605 struct ocfs2_xattr_entry *xe;
4606 int blocksize = inode->i_sb->s_blocksize;
4608 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
4609 (unsigned long long)blk, (unsigned long long)new_blk);
4611 s_bucket = ocfs2_xattr_bucket_new(inode);
4612 t_bucket = ocfs2_xattr_bucket_new(inode);
4613 if (!s_bucket || !t_bucket) {
4614 ret = -ENOMEM;
4615 mlog_errno(ret);
4616 goto out;
4619 ret = ocfs2_read_xattr_bucket(s_bucket, blk);
4620 if (ret) {
4621 mlog_errno(ret);
4622 goto out;
4625 ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
4626 OCFS2_JOURNAL_ACCESS_WRITE);
4627 if (ret) {
4628 mlog_errno(ret);
4629 goto out;
4633 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
4634 * there's no need to read it.
4636 ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
4637 if (ret) {
4638 mlog_errno(ret);
4639 goto out;
4643 * Hey, if we're overwriting t_bucket, what difference does
4644 * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the
4645 * same part of ocfs2_cp_xattr_bucket().
4647 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
4648 new_bucket_head ?
4649 OCFS2_JOURNAL_ACCESS_CREATE :
4650 OCFS2_JOURNAL_ACCESS_WRITE);
4651 if (ret) {
4652 mlog_errno(ret);
4653 goto out;
4656 xh = bucket_xh(s_bucket);
4657 count = le16_to_cpu(xh->xh_count);
4658 start = ocfs2_xattr_find_divide_pos(xh);
4660 if (start == count) {
4661 xe = &xh->xh_entries[start-1];
4664 * initialized a new empty bucket here.
4665 * The hash value is set as one larger than
4666 * that of the last entry in the previous bucket.
4668 for (i = 0; i < t_bucket->bu_blocks; i++)
4669 memset(bucket_block(t_bucket, i), 0, blocksize);
4671 xh = bucket_xh(t_bucket);
4672 xh->xh_free_start = cpu_to_le16(blocksize);
4673 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
4674 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
4676 goto set_num_buckets;
4679 /* copy the whole bucket to the new first. */
4680 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4682 /* update the new bucket. */
4683 xh = bucket_xh(t_bucket);
4686 * Calculate the total name/value len and xh_free_start for
4687 * the old bucket first.
4689 name_offset = OCFS2_XATTR_BUCKET_SIZE;
4690 name_value_len = 0;
4691 for (i = 0; i < start; i++) {
4692 xe = &xh->xh_entries[i];
4693 name_value_len += namevalue_size_xe(xe);
4694 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
4695 name_offset = le16_to_cpu(xe->xe_name_offset);
4699 * Now begin the modification to the new bucket.
4701 * In the new bucket, We just move the xattr entry to the beginning
4702 * and don't touch the name/value. So there will be some holes in the
4703 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
4704 * called.
4706 xe = &xh->xh_entries[start];
4707 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
4708 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
4709 (int)((char *)xe - (char *)xh),
4710 (int)((char *)xh->xh_entries - (char *)xh));
4711 memmove((char *)xh->xh_entries, (char *)xe, len);
4712 xe = &xh->xh_entries[count - start];
4713 len = sizeof(struct ocfs2_xattr_entry) * start;
4714 memset((char *)xe, 0, len);
4716 le16_add_cpu(&xh->xh_count, -start);
4717 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
4719 /* Calculate xh_free_start for the new bucket. */
4720 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4721 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4722 xe = &xh->xh_entries[i];
4723 if (le16_to_cpu(xe->xe_name_offset) <
4724 le16_to_cpu(xh->xh_free_start))
4725 xh->xh_free_start = xe->xe_name_offset;
4728 set_num_buckets:
4729 /* set xh->xh_num_buckets for the new xh. */
4730 if (new_bucket_head)
4731 xh->xh_num_buckets = cpu_to_le16(1);
4732 else
4733 xh->xh_num_buckets = 0;
4735 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
4737 /* store the first_hash of the new bucket. */
4738 if (first_hash)
4739 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
4742 * Now only update the 1st block of the old bucket. If we
4743 * just added a new empty bucket, there is no need to modify
4744 * it.
4746 if (start == count)
4747 goto out;
4749 xh = bucket_xh(s_bucket);
4750 memset(&xh->xh_entries[start], 0,
4751 sizeof(struct ocfs2_xattr_entry) * (count - start));
4752 xh->xh_count = cpu_to_le16(start);
4753 xh->xh_free_start = cpu_to_le16(name_offset);
4754 xh->xh_name_value_len = cpu_to_le16(name_value_len);
4756 ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
4758 out:
4759 ocfs2_xattr_bucket_free(s_bucket);
4760 ocfs2_xattr_bucket_free(t_bucket);
4762 return ret;
4766 * Copy xattr from one bucket to another bucket.
4768 * The caller must make sure that the journal transaction
4769 * has enough space for journaling.
4771 static int ocfs2_cp_xattr_bucket(struct inode *inode,
4772 handle_t *handle,
4773 u64 s_blkno,
4774 u64 t_blkno,
4775 int t_is_new)
4777 int ret;
4778 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
4780 BUG_ON(s_blkno == t_blkno);
4782 mlog(0, "cp bucket %llu to %llu, target is %d\n",
4783 (unsigned long long)s_blkno, (unsigned long long)t_blkno,
4784 t_is_new);
4786 s_bucket = ocfs2_xattr_bucket_new(inode);
4787 t_bucket = ocfs2_xattr_bucket_new(inode);
4788 if (!s_bucket || !t_bucket) {
4789 ret = -ENOMEM;
4790 mlog_errno(ret);
4791 goto out;
4794 ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
4795 if (ret)
4796 goto out;
4799 * Even if !t_is_new, we're overwriting t_bucket. Thus,
4800 * there's no need to read it.
4802 ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
4803 if (ret)
4804 goto out;
4807 * Hey, if we're overwriting t_bucket, what difference does
4808 * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new
4809 * cluster to fill, we came here from
4810 * ocfs2_mv_xattr_buckets(), and it is really new -
4811 * ACCESS_CREATE is required. But we also might have moved data
4812 * out of t_bucket before extending back into it.
4813 * ocfs2_add_new_xattr_bucket() can do this - its call to
4814 * ocfs2_add_new_xattr_cluster() may have created a new extent
4815 * and copied out the end of the old extent. Then it re-extends
4816 * the old extent back to create space for new xattrs. That's
4817 * how we get here, and the bucket isn't really new.
4819 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
4820 t_is_new ?
4821 OCFS2_JOURNAL_ACCESS_CREATE :
4822 OCFS2_JOURNAL_ACCESS_WRITE);
4823 if (ret)
4824 goto out;
4826 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4827 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
4829 out:
4830 ocfs2_xattr_bucket_free(t_bucket);
4831 ocfs2_xattr_bucket_free(s_bucket);
4833 return ret;
4837 * src_blk points to the start of an existing extent. last_blk points to
4838 * last cluster in that extent. to_blk points to a newly allocated
4839 * extent. We copy the buckets from the cluster at last_blk to the new
4840 * extent. If start_bucket is non-zero, we skip that many buckets before
4841 * we start copying. The new extent's xh_num_buckets gets set to the
4842 * number of buckets we copied. The old extent's xh_num_buckets shrinks
4843 * by the same amount.
4845 static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
4846 u64 src_blk, u64 last_blk, u64 to_blk,
4847 unsigned int start_bucket,
4848 u32 *first_hash)
4850 int i, ret, credits;
4851 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4852 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4853 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
4854 struct ocfs2_xattr_bucket *old_first, *new_first;
4856 mlog(0, "mv xattrs from cluster %llu to %llu\n",
4857 (unsigned long long)last_blk, (unsigned long long)to_blk);
4859 BUG_ON(start_bucket >= num_buckets);
4860 if (start_bucket) {
4861 num_buckets -= start_bucket;
4862 last_blk += (start_bucket * blks_per_bucket);
4865 /* The first bucket of the original extent */
4866 old_first = ocfs2_xattr_bucket_new(inode);
4867 /* The first bucket of the new extent */
4868 new_first = ocfs2_xattr_bucket_new(inode);
4869 if (!old_first || !new_first) {
4870 ret = -ENOMEM;
4871 mlog_errno(ret);
4872 goto out;
4875 ret = ocfs2_read_xattr_bucket(old_first, src_blk);
4876 if (ret) {
4877 mlog_errno(ret);
4878 goto out;
4882 * We need to update the first bucket of the old extent and all
4883 * the buckets going to the new extent.
4885 credits = ((num_buckets + 1) * blks_per_bucket);
4886 ret = ocfs2_extend_trans(handle, credits);
4887 if (ret) {
4888 mlog_errno(ret);
4889 goto out;
4892 ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
4893 OCFS2_JOURNAL_ACCESS_WRITE);
4894 if (ret) {
4895 mlog_errno(ret);
4896 goto out;
4899 for (i = 0; i < num_buckets; i++) {
4900 ret = ocfs2_cp_xattr_bucket(inode, handle,
4901 last_blk + (i * blks_per_bucket),
4902 to_blk + (i * blks_per_bucket),
4904 if (ret) {
4905 mlog_errno(ret);
4906 goto out;
4911 * Get the new bucket ready before we dirty anything
4912 * (This actually shouldn't fail, because we already dirtied
4913 * it once in ocfs2_cp_xattr_bucket()).
4915 ret = ocfs2_read_xattr_bucket(new_first, to_blk);
4916 if (ret) {
4917 mlog_errno(ret);
4918 goto out;
4920 ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
4921 OCFS2_JOURNAL_ACCESS_WRITE);
4922 if (ret) {
4923 mlog_errno(ret);
4924 goto out;
4927 /* Now update the headers */
4928 le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets);
4929 ocfs2_xattr_bucket_journal_dirty(handle, old_first);
4931 bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets);
4932 ocfs2_xattr_bucket_journal_dirty(handle, new_first);
4934 if (first_hash)
4935 *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash);
4937 out:
4938 ocfs2_xattr_bucket_free(new_first);
4939 ocfs2_xattr_bucket_free(old_first);
4940 return ret;
4944 * Move some xattrs in this cluster to the new cluster.
4945 * This function should only be called when bucket size == cluster size.
4946 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
4948 static int ocfs2_divide_xattr_cluster(struct inode *inode,
4949 handle_t *handle,
4950 u64 prev_blk,
4951 u64 new_blk,
4952 u32 *first_hash)
4954 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4955 int ret, credits = 2 * blk_per_bucket;
4957 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
4959 ret = ocfs2_extend_trans(handle, credits);
4960 if (ret) {
4961 mlog_errno(ret);
4962 return ret;
4965 /* Move half of the xattr in start_blk to the next bucket. */
4966 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
4967 new_blk, first_hash, 1);
4971 * Move some xattrs from the old cluster to the new one since they are not
4972 * contiguous in ocfs2 xattr tree.
4974 * new_blk starts a new separate cluster, and we will move some xattrs from
4975 * prev_blk to it. v_start will be set as the first name hash value in this
4976 * new cluster so that it can be used as e_cpos during tree insertion and
4977 * don't collide with our original b-tree operations. first_bh and header_bh
4978 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
4979 * to extend the insert bucket.
4981 * The problem is how much xattr should we move to the new one and when should
4982 * we update first_bh and header_bh?
4983 * 1. If cluster size > bucket size, that means the previous cluster has more
4984 * than 1 bucket, so just move half nums of bucket into the new cluster and
4985 * update the first_bh and header_bh if the insert bucket has been moved
4986 * to the new cluster.
4987 * 2. If cluster_size == bucket_size:
4988 * a) If the previous extent rec has more than one cluster and the insert
4989 * place isn't in the last cluster, copy the entire last cluster to the
4990 * new one. This time, we don't need to upate the first_bh and header_bh
4991 * since they will not be moved into the new cluster.
4992 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
4993 * the new one. And we set the extend flag to zero if the insert place is
4994 * moved into the new allocated cluster since no extend is needed.
4996 static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
4997 handle_t *handle,
4998 struct ocfs2_xattr_bucket *first,
4999 struct ocfs2_xattr_bucket *target,
5000 u64 new_blk,
5001 u32 prev_clusters,
5002 u32 *v_start,
5003 int *extend)
5005 int ret;
5007 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
5008 (unsigned long long)bucket_blkno(first), prev_clusters,
5009 (unsigned long long)new_blk);
5011 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) {
5012 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
5013 handle,
5014 first, target,
5015 new_blk,
5016 prev_clusters,
5017 v_start);
5018 if (ret)
5019 mlog_errno(ret);
5020 } else {
5021 /* The start of the last cluster in the first extent */
5022 u64 last_blk = bucket_blkno(first) +
5023 ((prev_clusters - 1) *
5024 ocfs2_clusters_to_blocks(inode->i_sb, 1));
5026 if (prev_clusters > 1 && bucket_blkno(target) != last_blk) {
5027 ret = ocfs2_mv_xattr_buckets(inode, handle,
5028 bucket_blkno(first),
5029 last_blk, new_blk, 0,
5030 v_start);
5031 if (ret)
5032 mlog_errno(ret);
5033 } else {
5034 ret = ocfs2_divide_xattr_cluster(inode, handle,
5035 last_blk, new_blk,
5036 v_start);
5037 if (ret)
5038 mlog_errno(ret);
5040 if ((bucket_blkno(target) == last_blk) && extend)
5041 *extend = 0;
5045 return ret;
5049 * Add a new cluster for xattr storage.
5051 * If the new cluster is contiguous with the previous one, it will be
5052 * appended to the same extent record, and num_clusters will be updated.
5053 * If not, we will insert a new extent for it and move some xattrs in
5054 * the last cluster into the new allocated one.
5055 * We also need to limit the maximum size of a btree leaf, otherwise we'll
5056 * lose the benefits of hashing because we'll have to search large leaves.
5057 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
5058 * if it's bigger).
5060 * first_bh is the first block of the previous extent rec and header_bh
5061 * indicates the bucket we will insert the new xattrs. They will be updated
5062 * when the header_bh is moved into the new cluster.
5064 static int ocfs2_add_new_xattr_cluster(struct inode *inode,
5065 struct buffer_head *root_bh,
5066 struct ocfs2_xattr_bucket *first,
5067 struct ocfs2_xattr_bucket *target,
5068 u32 *num_clusters,
5069 u32 prev_cpos,
5070 int *extend,
5071 struct ocfs2_xattr_set_ctxt *ctxt)
5073 int ret;
5074 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
5075 u32 prev_clusters = *num_clusters;
5076 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
5077 u64 block;
5078 handle_t *handle = ctxt->handle;
5079 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5080 struct ocfs2_extent_tree et;
5082 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
5083 "previous xattr blkno = %llu\n",
5084 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5085 prev_cpos, (unsigned long long)bucket_blkno(first));
5087 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
5089 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
5090 OCFS2_JOURNAL_ACCESS_WRITE);
5091 if (ret < 0) {
5092 mlog_errno(ret);
5093 goto leave;
5096 ret = __ocfs2_claim_clusters(handle, ctxt->data_ac, 1,
5097 clusters_to_add, &bit_off, &num_bits);
5098 if (ret < 0) {
5099 if (ret != -ENOSPC)
5100 mlog_errno(ret);
5101 goto leave;
5104 BUG_ON(num_bits > clusters_to_add);
5106 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
5107 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
5108 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
5110 if (bucket_blkno(first) + (prev_clusters * bpc) == block &&
5111 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
5112 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
5114 * If this cluster is contiguous with the old one and
5115 * adding this new cluster, we don't surpass the limit of
5116 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
5117 * initialized and used like other buckets in the previous
5118 * cluster.
5119 * So add it as a contiguous one. The caller will handle
5120 * its init process.
5122 v_start = prev_cpos + prev_clusters;
5123 *num_clusters = prev_clusters + num_bits;
5124 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
5125 num_bits);
5126 } else {
5127 ret = ocfs2_adjust_xattr_cross_cluster(inode,
5128 handle,
5129 first,
5130 target,
5131 block,
5132 prev_clusters,
5133 &v_start,
5134 extend);
5135 if (ret) {
5136 mlog_errno(ret);
5137 goto leave;
5141 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
5142 num_bits, (unsigned long long)block, v_start);
5143 ret = ocfs2_insert_extent(handle, &et, v_start, block,
5144 num_bits, 0, ctxt->meta_ac);
5145 if (ret < 0) {
5146 mlog_errno(ret);
5147 goto leave;
5150 ocfs2_journal_dirty(handle, root_bh);
5152 leave:
5153 return ret;
5157 * We are given an extent. 'first' is the bucket at the very front of
5158 * the extent. The extent has space for an additional bucket past
5159 * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
5160 * of the target bucket. We wish to shift every bucket past the target
5161 * down one, filling in that additional space. When we get back to the
5162 * target, we split the target between itself and the now-empty bucket
5163 * at target+1 (aka, target_blkno + blks_per_bucket).
5165 static int ocfs2_extend_xattr_bucket(struct inode *inode,
5166 handle_t *handle,
5167 struct ocfs2_xattr_bucket *first,
5168 u64 target_blk,
5169 u32 num_clusters)
5171 int ret, credits;
5172 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5173 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
5174 u64 end_blk;
5175 u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets);
5177 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
5178 "from %llu, len = %u\n", (unsigned long long)target_blk,
5179 (unsigned long long)bucket_blkno(first), num_clusters);
5181 /* The extent must have room for an additional bucket */
5182 BUG_ON(new_bucket >=
5183 (num_clusters * ocfs2_xattr_buckets_per_cluster(osb)));
5185 /* end_blk points to the last existing bucket */
5186 end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket);
5189 * end_blk is the start of the last existing bucket.
5190 * Thus, (end_blk - target_blk) covers the target bucket and
5191 * every bucket after it up to, but not including, the last
5192 * existing bucket. Then we add the last existing bucket, the
5193 * new bucket, and the first bucket (3 * blk_per_bucket).
5195 credits = (end_blk - target_blk) + (3 * blk_per_bucket);
5196 ret = ocfs2_extend_trans(handle, credits);
5197 if (ret) {
5198 mlog_errno(ret);
5199 goto out;
5202 ret = ocfs2_xattr_bucket_journal_access(handle, first,
5203 OCFS2_JOURNAL_ACCESS_WRITE);
5204 if (ret) {
5205 mlog_errno(ret);
5206 goto out;
5209 while (end_blk != target_blk) {
5210 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
5211 end_blk + blk_per_bucket, 0);
5212 if (ret)
5213 goto out;
5214 end_blk -= blk_per_bucket;
5217 /* Move half of the xattr in target_blkno to the next bucket. */
5218 ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk,
5219 target_blk + blk_per_bucket, NULL, 0);
5221 le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1);
5222 ocfs2_xattr_bucket_journal_dirty(handle, first);
5224 out:
5225 return ret;
5229 * Add new xattr bucket in an extent record and adjust the buckets
5230 * accordingly. xb_bh is the ocfs2_xattr_block, and target is the
5231 * bucket we want to insert into.
5233 * In the easy case, we will move all the buckets after target down by
5234 * one. Half of target's xattrs will be moved to the next bucket.
5236 * If current cluster is full, we'll allocate a new one. This may not
5237 * be contiguous. The underlying calls will make sure that there is
5238 * space for the insert, shifting buckets around if necessary.
5239 * 'target' may be moved by those calls.
5241 static int ocfs2_add_new_xattr_bucket(struct inode *inode,
5242 struct buffer_head *xb_bh,
5243 struct ocfs2_xattr_bucket *target,
5244 struct ocfs2_xattr_set_ctxt *ctxt)
5246 struct ocfs2_xattr_block *xb =
5247 (struct ocfs2_xattr_block *)xb_bh->b_data;
5248 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
5249 struct ocfs2_extent_list *el = &xb_root->xt_list;
5250 u32 name_hash =
5251 le32_to_cpu(bucket_xh(target)->xh_entries[0].xe_name_hash);
5252 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5253 int ret, num_buckets, extend = 1;
5254 u64 p_blkno;
5255 u32 e_cpos, num_clusters;
5256 /* The bucket at the front of the extent */
5257 struct ocfs2_xattr_bucket *first;
5259 mlog(0, "Add new xattr bucket starting from %llu\n",
5260 (unsigned long long)bucket_blkno(target));
5262 /* The first bucket of the original extent */
5263 first = ocfs2_xattr_bucket_new(inode);
5264 if (!first) {
5265 ret = -ENOMEM;
5266 mlog_errno(ret);
5267 goto out;
5270 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
5271 &num_clusters, el);
5272 if (ret) {
5273 mlog_errno(ret);
5274 goto out;
5277 ret = ocfs2_read_xattr_bucket(first, p_blkno);
5278 if (ret) {
5279 mlog_errno(ret);
5280 goto out;
5283 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
5284 if (num_buckets == le16_to_cpu(bucket_xh(first)->xh_num_buckets)) {
5286 * This can move first+target if the target bucket moves
5287 * to the new extent.
5289 ret = ocfs2_add_new_xattr_cluster(inode,
5290 xb_bh,
5291 first,
5292 target,
5293 &num_clusters,
5294 e_cpos,
5295 &extend,
5296 ctxt);
5297 if (ret) {
5298 mlog_errno(ret);
5299 goto out;
5303 if (extend) {
5304 ret = ocfs2_extend_xattr_bucket(inode,
5305 ctxt->handle,
5306 first,
5307 bucket_blkno(target),
5308 num_clusters);
5309 if (ret)
5310 mlog_errno(ret);
5313 out:
5314 ocfs2_xattr_bucket_free(first);
5316 return ret;
5319 static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
5320 struct ocfs2_xattr_bucket *bucket,
5321 int offs)
5323 int block_off = offs >> inode->i_sb->s_blocksize_bits;
5325 offs = offs % inode->i_sb->s_blocksize;
5326 return bucket_block(bucket, block_off) + offs;
5330 * Truncate the specified xe_off entry in xattr bucket.
5331 * bucket is indicated by header_bh and len is the new length.
5332 * Both the ocfs2_xattr_value_root and the entry will be updated here.
5334 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
5336 static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
5337 struct ocfs2_xattr_bucket *bucket,
5338 int xe_off,
5339 int len,
5340 struct ocfs2_xattr_set_ctxt *ctxt)
5342 int ret, offset;
5343 u64 value_blk;
5344 struct ocfs2_xattr_entry *xe;
5345 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5346 size_t blocksize = inode->i_sb->s_blocksize;
5347 struct ocfs2_xattr_value_buf vb = {
5348 .vb_access = ocfs2_journal_access,
5351 xe = &xh->xh_entries[xe_off];
5353 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
5355 offset = le16_to_cpu(xe->xe_name_offset) +
5356 OCFS2_XATTR_SIZE(xe->xe_name_len);
5358 value_blk = offset / blocksize;
5360 /* We don't allow ocfs2_xattr_value to be stored in different block. */
5361 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
5363 vb.vb_bh = bucket->bu_bhs[value_blk];
5364 BUG_ON(!vb.vb_bh);
5366 vb.vb_xv = (struct ocfs2_xattr_value_root *)
5367 (vb.vb_bh->b_data + offset % blocksize);
5370 * From here on out we have to dirty the bucket. The generic
5371 * value calls only modify one of the bucket's bhs, but we need
5372 * to send the bucket at once. So if they error, they *could* have
5373 * modified something. We have to assume they did, and dirty
5374 * the whole bucket. This leaves us in a consistent state.
5376 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
5377 xe_off, (unsigned long long)bucket_blkno(bucket), len);
5378 ret = ocfs2_xattr_value_truncate(inode, &vb, len, ctxt);
5379 if (ret) {
5380 mlog_errno(ret);
5381 goto out;
5384 ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
5385 OCFS2_JOURNAL_ACCESS_WRITE);
5386 if (ret) {
5387 mlog_errno(ret);
5388 goto out;
5391 xe->xe_value_size = cpu_to_le64(len);
5393 ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket);
5395 out:
5396 return ret;
5399 static int ocfs2_rm_xattr_cluster(struct inode *inode,
5400 struct buffer_head *root_bh,
5401 u64 blkno,
5402 u32 cpos,
5403 u32 len,
5404 void *para)
5406 int ret;
5407 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5408 struct inode *tl_inode = osb->osb_tl_inode;
5409 handle_t *handle;
5410 struct ocfs2_xattr_block *xb =
5411 (struct ocfs2_xattr_block *)root_bh->b_data;
5412 struct ocfs2_alloc_context *meta_ac = NULL;
5413 struct ocfs2_cached_dealloc_ctxt dealloc;
5414 struct ocfs2_extent_tree et;
5416 ret = ocfs2_iterate_xattr_buckets(inode, blkno, len,
5417 ocfs2_delete_xattr_in_bucket, para);
5418 if (ret) {
5419 mlog_errno(ret);
5420 return ret;
5423 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
5425 ocfs2_init_dealloc_ctxt(&dealloc);
5427 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
5428 cpos, len, (unsigned long long)blkno);
5430 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode), blkno,
5431 len);
5433 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
5434 if (ret) {
5435 mlog_errno(ret);
5436 return ret;
5439 mutex_lock(&tl_inode->i_mutex);
5441 if (ocfs2_truncate_log_needs_flush(osb)) {
5442 ret = __ocfs2_flush_truncate_log(osb);
5443 if (ret < 0) {
5444 mlog_errno(ret);
5445 goto out;
5449 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
5450 if (IS_ERR(handle)) {
5451 ret = -ENOMEM;
5452 mlog_errno(ret);
5453 goto out;
5456 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
5457 OCFS2_JOURNAL_ACCESS_WRITE);
5458 if (ret) {
5459 mlog_errno(ret);
5460 goto out_commit;
5463 ret = ocfs2_remove_extent(handle, &et, cpos, len, meta_ac,
5464 &dealloc);
5465 if (ret) {
5466 mlog_errno(ret);
5467 goto out_commit;
5470 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
5471 ocfs2_journal_dirty(handle, root_bh);
5473 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
5474 if (ret)
5475 mlog_errno(ret);
5477 out_commit:
5478 ocfs2_commit_trans(osb, handle);
5479 out:
5480 ocfs2_schedule_truncate_log_flush(osb, 1);
5482 mutex_unlock(&tl_inode->i_mutex);
5484 if (meta_ac)
5485 ocfs2_free_alloc_context(meta_ac);
5487 ocfs2_run_deallocs(osb, &dealloc);
5489 return ret;
5493 * check whether the xattr bucket is filled up with the same hash value.
5494 * If we want to insert the xattr with the same hash, return -ENOSPC.
5495 * If we want to insert a xattr with different hash value, go ahead
5496 * and ocfs2_divide_xattr_bucket will handle this.
5498 static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
5499 struct ocfs2_xattr_bucket *bucket,
5500 const char *name)
5502 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5503 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
5505 if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
5506 return 0;
5508 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
5509 xh->xh_entries[0].xe_name_hash) {
5510 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
5511 "hash = %u\n",
5512 (unsigned long long)bucket_blkno(bucket),
5513 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
5514 return -ENOSPC;
5517 return 0;
5521 * Try to set the entry in the current bucket. If we fail, the caller
5522 * will handle getting us another bucket.
5524 static int ocfs2_xattr_set_entry_bucket(struct inode *inode,
5525 struct ocfs2_xattr_info *xi,
5526 struct ocfs2_xattr_search *xs,
5527 struct ocfs2_xattr_set_ctxt *ctxt)
5529 int ret;
5530 struct ocfs2_xa_loc loc;
5532 mlog_entry("Set xattr %s in xattr bucket\n", xi->xi_name);
5534 ocfs2_init_xattr_bucket_xa_loc(&loc, xs->bucket,
5535 xs->not_found ? NULL : xs->here);
5536 ret = ocfs2_xa_set(&loc, xi, ctxt);
5537 if (!ret) {
5538 xs->here = loc.xl_entry;
5539 goto out;
5541 if (ret != -ENOSPC) {
5542 mlog_errno(ret);
5543 goto out;
5546 /* Ok, we need space. Let's try defragmenting the bucket. */
5547 ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
5548 xs->bucket);
5549 if (ret) {
5550 mlog_errno(ret);
5551 goto out;
5554 ret = ocfs2_xa_set(&loc, xi, ctxt);
5555 if (!ret) {
5556 xs->here = loc.xl_entry;
5557 goto out;
5559 if (ret != -ENOSPC)
5560 mlog_errno(ret);
5563 out:
5564 mlog_exit(ret);
5565 return ret;
5568 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
5569 struct ocfs2_xattr_info *xi,
5570 struct ocfs2_xattr_search *xs,
5571 struct ocfs2_xattr_set_ctxt *ctxt)
5573 int ret;
5575 mlog_entry("Set xattr %s in xattr index block\n", xi->xi_name);
5577 ret = ocfs2_xattr_set_entry_bucket(inode, xi, xs, ctxt);
5578 if (!ret)
5579 goto out;
5580 if (ret != -ENOSPC) {
5581 mlog_errno(ret);
5582 goto out;
5585 /* Ack, need more space. Let's try to get another bucket! */
5588 * We do not allow for overlapping ranges between buckets. And
5589 * the maximum number of collisions we will allow for then is
5590 * one bucket's worth, so check it here whether we need to
5591 * add a new bucket for the insert.
5593 ret = ocfs2_check_xattr_bucket_collision(inode,
5594 xs->bucket,
5595 xi->xi_name);
5596 if (ret) {
5597 mlog_errno(ret);
5598 goto out;
5601 ret = ocfs2_add_new_xattr_bucket(inode,
5602 xs->xattr_bh,
5603 xs->bucket,
5604 ctxt);
5605 if (ret) {
5606 mlog_errno(ret);
5607 goto out;
5611 * ocfs2_add_new_xattr_bucket() will have updated
5612 * xs->bucket if it moved, but it will not have updated
5613 * any of the other search fields. Thus, we drop it and
5614 * re-search. Everything should be cached, so it'll be
5615 * quick.
5617 ocfs2_xattr_bucket_relse(xs->bucket);
5618 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
5619 xi->xi_name_index,
5620 xi->xi_name, xs);
5621 if (ret && ret != -ENODATA)
5622 goto out;
5623 xs->not_found = ret;
5625 /* Ok, we have a new bucket, let's try again */
5626 ret = ocfs2_xattr_set_entry_bucket(inode, xi, xs, ctxt);
5627 if (ret && (ret != -ENOSPC))
5628 mlog_errno(ret);
5630 out:
5631 mlog_exit(ret);
5632 return ret;
5635 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
5636 struct ocfs2_xattr_bucket *bucket,
5637 void *para)
5639 int ret = 0, ref_credits;
5640 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5641 u16 i;
5642 struct ocfs2_xattr_entry *xe;
5643 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5644 struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
5645 int credits = ocfs2_remove_extent_credits(osb->sb) +
5646 ocfs2_blocks_per_xattr_bucket(inode->i_sb);
5647 struct ocfs2_xattr_value_root *xv;
5648 struct ocfs2_rm_xattr_bucket_para *args =
5649 (struct ocfs2_rm_xattr_bucket_para *)para;
5651 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
5653 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
5654 xe = &xh->xh_entries[i];
5655 if (ocfs2_xattr_is_local(xe))
5656 continue;
5658 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket,
5659 i, &xv, NULL);
5661 ret = ocfs2_lock_xattr_remove_allocators(inode, xv,
5662 args->ref_ci,
5663 args->ref_root_bh,
5664 &ctxt.meta_ac,
5665 &ref_credits);
5667 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
5668 if (IS_ERR(ctxt.handle)) {
5669 ret = PTR_ERR(ctxt.handle);
5670 mlog_errno(ret);
5671 break;
5674 ret = ocfs2_xattr_bucket_value_truncate(inode, bucket,
5675 i, 0, &ctxt);
5677 ocfs2_commit_trans(osb, ctxt.handle);
5678 if (ctxt.meta_ac) {
5679 ocfs2_free_alloc_context(ctxt.meta_ac);
5680 ctxt.meta_ac = NULL;
5682 if (ret) {
5683 mlog_errno(ret);
5684 break;
5688 if (ctxt.meta_ac)
5689 ocfs2_free_alloc_context(ctxt.meta_ac);
5690 ocfs2_schedule_truncate_log_flush(osb, 1);
5691 ocfs2_run_deallocs(osb, &ctxt.dealloc);
5692 return ret;
5696 * Whenever we modify a xattr value root in the bucket(e.g, CoW
5697 * or change the extent record flag), we need to recalculate
5698 * the metaecc for the whole bucket. So it is done here.
5700 * Note:
5701 * We have to give the extra credits for the caller.
5703 static int ocfs2_xattr_bucket_post_refcount(struct inode *inode,
5704 handle_t *handle,
5705 void *para)
5707 int ret;
5708 struct ocfs2_xattr_bucket *bucket =
5709 (struct ocfs2_xattr_bucket *)para;
5711 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
5712 OCFS2_JOURNAL_ACCESS_WRITE);
5713 if (ret) {
5714 mlog_errno(ret);
5715 return ret;
5718 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
5720 return 0;
5724 * Special action we need if the xattr value is refcounted.
5726 * 1. If the xattr is refcounted, lock the tree.
5727 * 2. CoW the xattr if we are setting the new value and the value
5728 * will be stored outside.
5729 * 3. In other case, decrease_refcount will work for us, so just
5730 * lock the refcount tree, calculate the meta and credits is OK.
5732 * We have to do CoW before ocfs2_init_xattr_set_ctxt since
5733 * currently CoW is a completed transaction, while this function
5734 * will also lock the allocators and let us deadlock. So we will
5735 * CoW the whole xattr value.
5737 static int ocfs2_prepare_refcount_xattr(struct inode *inode,
5738 struct ocfs2_dinode *di,
5739 struct ocfs2_xattr_info *xi,
5740 struct ocfs2_xattr_search *xis,
5741 struct ocfs2_xattr_search *xbs,
5742 struct ocfs2_refcount_tree **ref_tree,
5743 int *meta_add,
5744 int *credits)
5746 int ret = 0;
5747 struct ocfs2_xattr_block *xb;
5748 struct ocfs2_xattr_entry *xe;
5749 char *base;
5750 u32 p_cluster, num_clusters;
5751 unsigned int ext_flags;
5752 int name_offset, name_len;
5753 struct ocfs2_xattr_value_buf vb;
5754 struct ocfs2_xattr_bucket *bucket = NULL;
5755 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5756 struct ocfs2_post_refcount refcount;
5757 struct ocfs2_post_refcount *p = NULL;
5758 struct buffer_head *ref_root_bh = NULL;
5760 if (!xis->not_found) {
5761 xe = xis->here;
5762 name_offset = le16_to_cpu(xe->xe_name_offset);
5763 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5764 base = xis->base;
5765 vb.vb_bh = xis->inode_bh;
5766 vb.vb_access = ocfs2_journal_access_di;
5767 } else {
5768 int i, block_off = 0;
5769 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
5770 xe = xbs->here;
5771 name_offset = le16_to_cpu(xe->xe_name_offset);
5772 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5773 i = xbs->here - xbs->header->xh_entries;
5775 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
5776 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
5777 bucket_xh(xbs->bucket),
5778 i, &block_off,
5779 &name_offset);
5780 if (ret) {
5781 mlog_errno(ret);
5782 goto out;
5784 base = bucket_block(xbs->bucket, block_off);
5785 vb.vb_bh = xbs->bucket->bu_bhs[block_off];
5786 vb.vb_access = ocfs2_journal_access;
5788 if (ocfs2_meta_ecc(osb)) {
5789 /*create parameters for ocfs2_post_refcount. */
5790 bucket = xbs->bucket;
5791 refcount.credits = bucket->bu_blocks;
5792 refcount.para = bucket;
5793 refcount.func =
5794 ocfs2_xattr_bucket_post_refcount;
5795 p = &refcount;
5797 } else {
5798 base = xbs->base;
5799 vb.vb_bh = xbs->xattr_bh;
5800 vb.vb_access = ocfs2_journal_access_xb;
5804 if (ocfs2_xattr_is_local(xe))
5805 goto out;
5807 vb.vb_xv = (struct ocfs2_xattr_value_root *)
5808 (base + name_offset + name_len);
5810 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
5811 &num_clusters, &vb.vb_xv->xr_list,
5812 &ext_flags);
5813 if (ret) {
5814 mlog_errno(ret);
5815 goto out;
5819 * We just need to check the 1st extent record, since we always
5820 * CoW the whole xattr. So there shouldn't be a xattr with
5821 * some REFCOUNT extent recs after the 1st one.
5823 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
5824 goto out;
5826 ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
5827 1, ref_tree, &ref_root_bh);
5828 if (ret) {
5829 mlog_errno(ret);
5830 goto out;
5834 * If we are deleting the xattr or the new size will be stored inside,
5835 * cool, leave it there, the xattr truncate process will remove them
5836 * for us(it still needs the refcount tree lock and the meta, credits).
5837 * And the worse case is that every cluster truncate will split the
5838 * refcount tree, and make the original extent become 3. So we will need
5839 * 2 * cluster more extent recs at most.
5841 if (!xi->xi_value || xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE) {
5843 ret = ocfs2_refcounted_xattr_delete_need(inode,
5844 &(*ref_tree)->rf_ci,
5845 ref_root_bh, vb.vb_xv,
5846 meta_add, credits);
5847 if (ret)
5848 mlog_errno(ret);
5849 goto out;
5852 ret = ocfs2_refcount_cow_xattr(inode, di, &vb,
5853 *ref_tree, ref_root_bh, 0,
5854 le32_to_cpu(vb.vb_xv->xr_clusters), p);
5855 if (ret)
5856 mlog_errno(ret);
5858 out:
5859 brelse(ref_root_bh);
5860 return ret;
5864 * Add the REFCOUNTED flags for all the extent rec in ocfs2_xattr_value_root.
5865 * The physical clusters will be added to refcount tree.
5867 static int ocfs2_xattr_value_attach_refcount(struct inode *inode,
5868 struct ocfs2_xattr_value_root *xv,
5869 struct ocfs2_extent_tree *value_et,
5870 struct ocfs2_caching_info *ref_ci,
5871 struct buffer_head *ref_root_bh,
5872 struct ocfs2_cached_dealloc_ctxt *dealloc,
5873 struct ocfs2_post_refcount *refcount)
5875 int ret = 0;
5876 u32 clusters = le32_to_cpu(xv->xr_clusters);
5877 u32 cpos, p_cluster, num_clusters;
5878 struct ocfs2_extent_list *el = &xv->xr_list;
5879 unsigned int ext_flags;
5881 cpos = 0;
5882 while (cpos < clusters) {
5883 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
5884 &num_clusters, el, &ext_flags);
5886 cpos += num_clusters;
5887 if ((ext_flags & OCFS2_EXT_REFCOUNTED))
5888 continue;
5890 BUG_ON(!p_cluster);
5892 ret = ocfs2_add_refcount_flag(inode, value_et,
5893 ref_ci, ref_root_bh,
5894 cpos - num_clusters,
5895 p_cluster, num_clusters,
5896 dealloc, refcount);
5897 if (ret) {
5898 mlog_errno(ret);
5899 break;
5903 return ret;
5907 * Given a normal ocfs2_xattr_header, refcount all the entries which
5908 * have value stored outside.
5909 * Used for xattrs stored in inode and ocfs2_xattr_block.
5911 static int ocfs2_xattr_attach_refcount_normal(struct inode *inode,
5912 struct ocfs2_xattr_value_buf *vb,
5913 struct ocfs2_xattr_header *header,
5914 struct ocfs2_caching_info *ref_ci,
5915 struct buffer_head *ref_root_bh,
5916 struct ocfs2_cached_dealloc_ctxt *dealloc)
5919 struct ocfs2_xattr_entry *xe;
5920 struct ocfs2_xattr_value_root *xv;
5921 struct ocfs2_extent_tree et;
5922 int i, ret = 0;
5924 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
5925 xe = &header->xh_entries[i];
5927 if (ocfs2_xattr_is_local(xe))
5928 continue;
5930 xv = (struct ocfs2_xattr_value_root *)((void *)header +
5931 le16_to_cpu(xe->xe_name_offset) +
5932 OCFS2_XATTR_SIZE(xe->xe_name_len));
5934 vb->vb_xv = xv;
5935 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
5937 ret = ocfs2_xattr_value_attach_refcount(inode, xv, &et,
5938 ref_ci, ref_root_bh,
5939 dealloc, NULL);
5940 if (ret) {
5941 mlog_errno(ret);
5942 break;
5946 return ret;
5949 static int ocfs2_xattr_inline_attach_refcount(struct inode *inode,
5950 struct buffer_head *fe_bh,
5951 struct ocfs2_caching_info *ref_ci,
5952 struct buffer_head *ref_root_bh,
5953 struct ocfs2_cached_dealloc_ctxt *dealloc)
5955 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
5956 struct ocfs2_xattr_header *header = (struct ocfs2_xattr_header *)
5957 (fe_bh->b_data + inode->i_sb->s_blocksize -
5958 le16_to_cpu(di->i_xattr_inline_size));
5959 struct ocfs2_xattr_value_buf vb = {
5960 .vb_bh = fe_bh,
5961 .vb_access = ocfs2_journal_access_di,
5964 return ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
5965 ref_ci, ref_root_bh, dealloc);
5968 struct ocfs2_xattr_tree_value_refcount_para {
5969 struct ocfs2_caching_info *ref_ci;
5970 struct buffer_head *ref_root_bh;
5971 struct ocfs2_cached_dealloc_ctxt *dealloc;
5974 static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
5975 struct ocfs2_xattr_bucket *bucket,
5976 int offset,
5977 struct ocfs2_xattr_value_root **xv,
5978 struct buffer_head **bh)
5980 int ret, block_off, name_offset;
5981 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5982 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
5983 void *base;
5985 ret = ocfs2_xattr_bucket_get_name_value(sb,
5986 bucket_xh(bucket),
5987 offset,
5988 &block_off,
5989 &name_offset);
5990 if (ret) {
5991 mlog_errno(ret);
5992 goto out;
5995 base = bucket_block(bucket, block_off);
5997 *xv = (struct ocfs2_xattr_value_root *)(base + name_offset +
5998 OCFS2_XATTR_SIZE(xe->xe_name_len));
6000 if (bh)
6001 *bh = bucket->bu_bhs[block_off];
6002 out:
6003 return ret;
6007 * For a given xattr bucket, refcount all the entries which
6008 * have value stored outside.
6010 static int ocfs2_xattr_bucket_value_refcount(struct inode *inode,
6011 struct ocfs2_xattr_bucket *bucket,
6012 void *para)
6014 int i, ret = 0;
6015 struct ocfs2_extent_tree et;
6016 struct ocfs2_xattr_tree_value_refcount_para *ref =
6017 (struct ocfs2_xattr_tree_value_refcount_para *)para;
6018 struct ocfs2_xattr_header *xh =
6019 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6020 struct ocfs2_xattr_entry *xe;
6021 struct ocfs2_xattr_value_buf vb = {
6022 .vb_access = ocfs2_journal_access,
6024 struct ocfs2_post_refcount refcount = {
6025 .credits = bucket->bu_blocks,
6026 .para = bucket,
6027 .func = ocfs2_xattr_bucket_post_refcount,
6029 struct ocfs2_post_refcount *p = NULL;
6031 /* We only need post_refcount if we support metaecc. */
6032 if (ocfs2_meta_ecc(OCFS2_SB(inode->i_sb)))
6033 p = &refcount;
6035 mlog(0, "refcount bucket %llu, count = %u\n",
6036 (unsigned long long)bucket_blkno(bucket),
6037 le16_to_cpu(xh->xh_count));
6038 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6039 xe = &xh->xh_entries[i];
6041 if (ocfs2_xattr_is_local(xe))
6042 continue;
6044 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket, i,
6045 &vb.vb_xv, &vb.vb_bh);
6046 if (ret) {
6047 mlog_errno(ret);
6048 break;
6051 ocfs2_init_xattr_value_extent_tree(&et,
6052 INODE_CACHE(inode), &vb);
6054 ret = ocfs2_xattr_value_attach_refcount(inode, vb.vb_xv,
6055 &et, ref->ref_ci,
6056 ref->ref_root_bh,
6057 ref->dealloc, p);
6058 if (ret) {
6059 mlog_errno(ret);
6060 break;
6064 return ret;
6068 static int ocfs2_refcount_xattr_tree_rec(struct inode *inode,
6069 struct buffer_head *root_bh,
6070 u64 blkno, u32 cpos, u32 len, void *para)
6072 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
6073 ocfs2_xattr_bucket_value_refcount,
6074 para);
6077 static int ocfs2_xattr_block_attach_refcount(struct inode *inode,
6078 struct buffer_head *blk_bh,
6079 struct ocfs2_caching_info *ref_ci,
6080 struct buffer_head *ref_root_bh,
6081 struct ocfs2_cached_dealloc_ctxt *dealloc)
6083 int ret = 0;
6084 struct ocfs2_xattr_block *xb =
6085 (struct ocfs2_xattr_block *)blk_bh->b_data;
6087 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
6088 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
6089 struct ocfs2_xattr_value_buf vb = {
6090 .vb_bh = blk_bh,
6091 .vb_access = ocfs2_journal_access_xb,
6094 ret = ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
6095 ref_ci, ref_root_bh,
6096 dealloc);
6097 } else {
6098 struct ocfs2_xattr_tree_value_refcount_para para = {
6099 .ref_ci = ref_ci,
6100 .ref_root_bh = ref_root_bh,
6101 .dealloc = dealloc,
6104 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
6105 ocfs2_refcount_xattr_tree_rec,
6106 &para);
6109 return ret;
6112 int ocfs2_xattr_attach_refcount_tree(struct inode *inode,
6113 struct buffer_head *fe_bh,
6114 struct ocfs2_caching_info *ref_ci,
6115 struct buffer_head *ref_root_bh,
6116 struct ocfs2_cached_dealloc_ctxt *dealloc)
6118 int ret = 0;
6119 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6120 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
6121 struct buffer_head *blk_bh = NULL;
6123 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
6124 ret = ocfs2_xattr_inline_attach_refcount(inode, fe_bh,
6125 ref_ci, ref_root_bh,
6126 dealloc);
6127 if (ret) {
6128 mlog_errno(ret);
6129 goto out;
6133 if (!di->i_xattr_loc)
6134 goto out;
6136 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
6137 &blk_bh);
6138 if (ret < 0) {
6139 mlog_errno(ret);
6140 goto out;
6143 ret = ocfs2_xattr_block_attach_refcount(inode, blk_bh, ref_ci,
6144 ref_root_bh, dealloc);
6145 if (ret)
6146 mlog_errno(ret);
6148 brelse(blk_bh);
6149 out:
6151 return ret;
6154 typedef int (should_xattr_reflinked)(struct ocfs2_xattr_entry *xe);
6156 * Store the information we need in xattr reflink.
6157 * old_bh and new_bh are inode bh for the old and new inode.
6159 struct ocfs2_xattr_reflink {
6160 struct inode *old_inode;
6161 struct inode *new_inode;
6162 struct buffer_head *old_bh;
6163 struct buffer_head *new_bh;
6164 struct ocfs2_caching_info *ref_ci;
6165 struct buffer_head *ref_root_bh;
6166 struct ocfs2_cached_dealloc_ctxt *dealloc;
6167 should_xattr_reflinked *xattr_reflinked;
6171 * Given a xattr header and xe offset,
6172 * return the proper xv and the corresponding bh.
6173 * xattr in inode, block and xattr tree have different implementaions.
6175 typedef int (get_xattr_value_root)(struct super_block *sb,
6176 struct buffer_head *bh,
6177 struct ocfs2_xattr_header *xh,
6178 int offset,
6179 struct ocfs2_xattr_value_root **xv,
6180 struct buffer_head **ret_bh,
6181 void *para);
6184 * Calculate all the xattr value root metadata stored in this xattr header and
6185 * credits we need if we create them from the scratch.
6186 * We use get_xattr_value_root so that all types of xattr container can use it.
6188 static int ocfs2_value_metas_in_xattr_header(struct super_block *sb,
6189 struct buffer_head *bh,
6190 struct ocfs2_xattr_header *xh,
6191 int *metas, int *credits,
6192 int *num_recs,
6193 get_xattr_value_root *func,
6194 void *para)
6196 int i, ret = 0;
6197 struct ocfs2_xattr_value_root *xv;
6198 struct ocfs2_xattr_entry *xe;
6200 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6201 xe = &xh->xh_entries[i];
6202 if (ocfs2_xattr_is_local(xe))
6203 continue;
6205 ret = func(sb, bh, xh, i, &xv, NULL, para);
6206 if (ret) {
6207 mlog_errno(ret);
6208 break;
6211 *metas += le16_to_cpu(xv->xr_list.l_tree_depth) *
6212 le16_to_cpu(xv->xr_list.l_next_free_rec);
6214 *credits += ocfs2_calc_extend_credits(sb,
6215 &def_xv.xv.xr_list,
6216 le32_to_cpu(xv->xr_clusters));
6219 * If the value is a tree with depth > 1, We don't go deep
6220 * to the extent block, so just calculate a maximum record num.
6222 if (!xv->xr_list.l_tree_depth)
6223 *num_recs += le16_to_cpu(xv->xr_list.l_next_free_rec);
6224 else
6225 *num_recs += ocfs2_clusters_for_bytes(sb,
6226 XATTR_SIZE_MAX);
6229 return ret;
6232 /* Used by xattr inode and block to return the right xv and buffer_head. */
6233 static int ocfs2_get_xattr_value_root(struct super_block *sb,
6234 struct buffer_head *bh,
6235 struct ocfs2_xattr_header *xh,
6236 int offset,
6237 struct ocfs2_xattr_value_root **xv,
6238 struct buffer_head **ret_bh,
6239 void *para)
6241 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
6243 *xv = (struct ocfs2_xattr_value_root *)((void *)xh +
6244 le16_to_cpu(xe->xe_name_offset) +
6245 OCFS2_XATTR_SIZE(xe->xe_name_len));
6247 if (ret_bh)
6248 *ret_bh = bh;
6250 return 0;
6254 * Lock the meta_ac and caculate how much credits we need for reflink xattrs.
6255 * It is only used for inline xattr and xattr block.
6257 static int ocfs2_reflink_lock_xattr_allocators(struct ocfs2_super *osb,
6258 struct ocfs2_xattr_header *xh,
6259 struct buffer_head *ref_root_bh,
6260 int *credits,
6261 struct ocfs2_alloc_context **meta_ac)
6263 int ret, meta_add = 0, num_recs = 0;
6264 struct ocfs2_refcount_block *rb =
6265 (struct ocfs2_refcount_block *)ref_root_bh->b_data;
6267 *credits = 0;
6269 ret = ocfs2_value_metas_in_xattr_header(osb->sb, NULL, xh,
6270 &meta_add, credits, &num_recs,
6271 ocfs2_get_xattr_value_root,
6272 NULL);
6273 if (ret) {
6274 mlog_errno(ret);
6275 goto out;
6279 * We need to add/modify num_recs in refcount tree, so just calculate
6280 * an approximate number we need for refcount tree change.
6281 * Sometimes we need to split the tree, and after split, half recs
6282 * will be moved to the new block, and a new block can only provide
6283 * half number of recs. So we multiple new blocks by 2.
6285 num_recs = num_recs / ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6286 meta_add += num_recs;
6287 *credits += num_recs + num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6288 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6289 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6290 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6291 else
6292 *credits += 1;
6294 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add, meta_ac);
6295 if (ret)
6296 mlog_errno(ret);
6298 out:
6299 return ret;
6303 * Given a xattr header, reflink all the xattrs in this container.
6304 * It can be used for inode, block and bucket.
6306 * NOTE:
6307 * Before we call this function, the caller has memcpy the xattr in
6308 * old_xh to the new_xh.
6310 * If args.xattr_reflinked is set, call it to decide whether the xe should
6311 * be reflinked or not. If not, remove it from the new xattr header.
6313 static int ocfs2_reflink_xattr_header(handle_t *handle,
6314 struct ocfs2_xattr_reflink *args,
6315 struct buffer_head *old_bh,
6316 struct ocfs2_xattr_header *xh,
6317 struct buffer_head *new_bh,
6318 struct ocfs2_xattr_header *new_xh,
6319 struct ocfs2_xattr_value_buf *vb,
6320 struct ocfs2_alloc_context *meta_ac,
6321 get_xattr_value_root *func,
6322 void *para)
6324 int ret = 0, i, j;
6325 struct super_block *sb = args->old_inode->i_sb;
6326 struct buffer_head *value_bh;
6327 struct ocfs2_xattr_entry *xe, *last;
6328 struct ocfs2_xattr_value_root *xv, *new_xv;
6329 struct ocfs2_extent_tree data_et;
6330 u32 clusters, cpos, p_cluster, num_clusters;
6331 unsigned int ext_flags = 0;
6333 mlog(0, "reflink xattr in container %llu, count = %u\n",
6334 (unsigned long long)old_bh->b_blocknr, le16_to_cpu(xh->xh_count));
6336 last = &new_xh->xh_entries[le16_to_cpu(new_xh->xh_count)];
6337 for (i = 0, j = 0; i < le16_to_cpu(xh->xh_count); i++, j++) {
6338 xe = &xh->xh_entries[i];
6340 if (args->xattr_reflinked && !args->xattr_reflinked(xe)) {
6341 xe = &new_xh->xh_entries[j];
6343 le16_add_cpu(&new_xh->xh_count, -1);
6344 if (new_xh->xh_count) {
6345 memmove(xe, xe + 1,
6346 (void *)last - (void *)xe);
6347 memset(last, 0,
6348 sizeof(struct ocfs2_xattr_entry));
6352 * We don't want j to increase in the next round since
6353 * it is already moved ahead.
6355 j--;
6356 continue;
6359 if (ocfs2_xattr_is_local(xe))
6360 continue;
6362 ret = func(sb, old_bh, xh, i, &xv, NULL, para);
6363 if (ret) {
6364 mlog_errno(ret);
6365 break;
6368 ret = func(sb, new_bh, new_xh, j, &new_xv, &value_bh, para);
6369 if (ret) {
6370 mlog_errno(ret);
6371 break;
6375 * For the xattr which has l_tree_depth = 0, all the extent
6376 * recs have already be copied to the new xh with the
6377 * propriate OCFS2_EXT_REFCOUNTED flag we just need to
6378 * increase the refount count int the refcount tree.
6380 * For the xattr which has l_tree_depth > 0, we need
6381 * to initialize it to the empty default value root,
6382 * and then insert the extents one by one.
6384 if (xv->xr_list.l_tree_depth) {
6385 memcpy(new_xv, &def_xv, sizeof(def_xv));
6386 vb->vb_xv = new_xv;
6387 vb->vb_bh = value_bh;
6388 ocfs2_init_xattr_value_extent_tree(&data_et,
6389 INODE_CACHE(args->new_inode), vb);
6392 clusters = le32_to_cpu(xv->xr_clusters);
6393 cpos = 0;
6394 while (cpos < clusters) {
6395 ret = ocfs2_xattr_get_clusters(args->old_inode,
6396 cpos,
6397 &p_cluster,
6398 &num_clusters,
6399 &xv->xr_list,
6400 &ext_flags);
6401 if (ret) {
6402 mlog_errno(ret);
6403 goto out;
6406 BUG_ON(!p_cluster);
6408 if (xv->xr_list.l_tree_depth) {
6409 ret = ocfs2_insert_extent(handle,
6410 &data_et, cpos,
6411 ocfs2_clusters_to_blocks(
6412 args->old_inode->i_sb,
6413 p_cluster),
6414 num_clusters, ext_flags,
6415 meta_ac);
6416 if (ret) {
6417 mlog_errno(ret);
6418 goto out;
6422 ret = ocfs2_increase_refcount(handle, args->ref_ci,
6423 args->ref_root_bh,
6424 p_cluster, num_clusters,
6425 meta_ac, args->dealloc);
6426 if (ret) {
6427 mlog_errno(ret);
6428 goto out;
6431 cpos += num_clusters;
6435 out:
6436 return ret;
6439 static int ocfs2_reflink_xattr_inline(struct ocfs2_xattr_reflink *args)
6441 int ret = 0, credits = 0;
6442 handle_t *handle;
6443 struct ocfs2_super *osb = OCFS2_SB(args->old_inode->i_sb);
6444 struct ocfs2_dinode *di = (struct ocfs2_dinode *)args->old_bh->b_data;
6445 int inline_size = le16_to_cpu(di->i_xattr_inline_size);
6446 int header_off = osb->sb->s_blocksize - inline_size;
6447 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)
6448 (args->old_bh->b_data + header_off);
6449 struct ocfs2_xattr_header *new_xh = (struct ocfs2_xattr_header *)
6450 (args->new_bh->b_data + header_off);
6451 struct ocfs2_alloc_context *meta_ac = NULL;
6452 struct ocfs2_inode_info *new_oi;
6453 struct ocfs2_dinode *new_di;
6454 struct ocfs2_xattr_value_buf vb = {
6455 .vb_bh = args->new_bh,
6456 .vb_access = ocfs2_journal_access_di,
6459 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6460 &credits, &meta_ac);
6461 if (ret) {
6462 mlog_errno(ret);
6463 goto out;
6466 handle = ocfs2_start_trans(osb, credits);
6467 if (IS_ERR(handle)) {
6468 ret = PTR_ERR(handle);
6469 mlog_errno(ret);
6470 goto out;
6473 ret = ocfs2_journal_access_di(handle, INODE_CACHE(args->new_inode),
6474 args->new_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6475 if (ret) {
6476 mlog_errno(ret);
6477 goto out_commit;
6480 memcpy(args->new_bh->b_data + header_off,
6481 args->old_bh->b_data + header_off, inline_size);
6483 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6484 new_di->i_xattr_inline_size = cpu_to_le16(inline_size);
6486 ret = ocfs2_reflink_xattr_header(handle, args, args->old_bh, xh,
6487 args->new_bh, new_xh, &vb, meta_ac,
6488 ocfs2_get_xattr_value_root, NULL);
6489 if (ret) {
6490 mlog_errno(ret);
6491 goto out_commit;
6494 new_oi = OCFS2_I(args->new_inode);
6495 spin_lock(&new_oi->ip_lock);
6496 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL | OCFS2_INLINE_XATTR_FL;
6497 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6498 spin_unlock(&new_oi->ip_lock);
6500 ocfs2_journal_dirty(handle, args->new_bh);
6502 out_commit:
6503 ocfs2_commit_trans(osb, handle);
6505 out:
6506 if (meta_ac)
6507 ocfs2_free_alloc_context(meta_ac);
6508 return ret;
6511 static int ocfs2_create_empty_xattr_block(struct inode *inode,
6512 struct buffer_head *fe_bh,
6513 struct buffer_head **ret_bh,
6514 int indexed)
6516 int ret;
6517 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6518 struct ocfs2_xattr_set_ctxt ctxt;
6520 memset(&ctxt, 0, sizeof(ctxt));
6521 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &ctxt.meta_ac);
6522 if (ret < 0) {
6523 mlog_errno(ret);
6524 return ret;
6527 ctxt.handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_CREATE_CREDITS);
6528 if (IS_ERR(ctxt.handle)) {
6529 ret = PTR_ERR(ctxt.handle);
6530 mlog_errno(ret);
6531 goto out;
6534 mlog(0, "create new xattr block for inode %llu, index = %d\n",
6535 (unsigned long long)fe_bh->b_blocknr, indexed);
6536 ret = ocfs2_create_xattr_block(inode, fe_bh, &ctxt, indexed,
6537 ret_bh);
6538 if (ret)
6539 mlog_errno(ret);
6541 ocfs2_commit_trans(osb, ctxt.handle);
6542 out:
6543 ocfs2_free_alloc_context(ctxt.meta_ac);
6544 return ret;
6547 static int ocfs2_reflink_xattr_block(struct ocfs2_xattr_reflink *args,
6548 struct buffer_head *blk_bh,
6549 struct buffer_head *new_blk_bh)
6551 int ret = 0, credits = 0;
6552 handle_t *handle;
6553 struct ocfs2_inode_info *new_oi = OCFS2_I(args->new_inode);
6554 struct ocfs2_dinode *new_di;
6555 struct ocfs2_super *osb = OCFS2_SB(args->new_inode->i_sb);
6556 int header_off = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
6557 struct ocfs2_xattr_block *xb =
6558 (struct ocfs2_xattr_block *)blk_bh->b_data;
6559 struct ocfs2_xattr_header *xh = &xb->xb_attrs.xb_header;
6560 struct ocfs2_xattr_block *new_xb =
6561 (struct ocfs2_xattr_block *)new_blk_bh->b_data;
6562 struct ocfs2_xattr_header *new_xh = &new_xb->xb_attrs.xb_header;
6563 struct ocfs2_alloc_context *meta_ac;
6564 struct ocfs2_xattr_value_buf vb = {
6565 .vb_bh = new_blk_bh,
6566 .vb_access = ocfs2_journal_access_xb,
6569 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6570 &credits, &meta_ac);
6571 if (ret) {
6572 mlog_errno(ret);
6573 return ret;
6576 /* One more credits in case we need to add xattr flags in new inode. */
6577 handle = ocfs2_start_trans(osb, credits + 1);
6578 if (IS_ERR(handle)) {
6579 ret = PTR_ERR(handle);
6580 mlog_errno(ret);
6581 goto out;
6584 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6585 ret = ocfs2_journal_access_di(handle,
6586 INODE_CACHE(args->new_inode),
6587 args->new_bh,
6588 OCFS2_JOURNAL_ACCESS_WRITE);
6589 if (ret) {
6590 mlog_errno(ret);
6591 goto out_commit;
6595 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(args->new_inode),
6596 new_blk_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6597 if (ret) {
6598 mlog_errno(ret);
6599 goto out_commit;
6602 memcpy(new_blk_bh->b_data + header_off, blk_bh->b_data + header_off,
6603 osb->sb->s_blocksize - header_off);
6605 ret = ocfs2_reflink_xattr_header(handle, args, blk_bh, xh,
6606 new_blk_bh, new_xh, &vb, meta_ac,
6607 ocfs2_get_xattr_value_root, NULL);
6608 if (ret) {
6609 mlog_errno(ret);
6610 goto out_commit;
6613 ocfs2_journal_dirty(handle, new_blk_bh);
6615 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6616 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6617 spin_lock(&new_oi->ip_lock);
6618 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
6619 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6620 spin_unlock(&new_oi->ip_lock);
6622 ocfs2_journal_dirty(handle, args->new_bh);
6625 out_commit:
6626 ocfs2_commit_trans(osb, handle);
6628 out:
6629 ocfs2_free_alloc_context(meta_ac);
6630 return ret;
6633 struct ocfs2_reflink_xattr_tree_args {
6634 struct ocfs2_xattr_reflink *reflink;
6635 struct buffer_head *old_blk_bh;
6636 struct buffer_head *new_blk_bh;
6637 struct ocfs2_xattr_bucket *old_bucket;
6638 struct ocfs2_xattr_bucket *new_bucket;
6642 * NOTE:
6643 * We have to handle the case that both old bucket and new bucket
6644 * will call this function to get the right ret_bh.
6645 * So The caller must give us the right bh.
6647 static int ocfs2_get_reflink_xattr_value_root(struct super_block *sb,
6648 struct buffer_head *bh,
6649 struct ocfs2_xattr_header *xh,
6650 int offset,
6651 struct ocfs2_xattr_value_root **xv,
6652 struct buffer_head **ret_bh,
6653 void *para)
6655 struct ocfs2_reflink_xattr_tree_args *args =
6656 (struct ocfs2_reflink_xattr_tree_args *)para;
6657 struct ocfs2_xattr_bucket *bucket;
6659 if (bh == args->old_bucket->bu_bhs[0])
6660 bucket = args->old_bucket;
6661 else
6662 bucket = args->new_bucket;
6664 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6665 xv, ret_bh);
6668 struct ocfs2_value_tree_metas {
6669 int num_metas;
6670 int credits;
6671 int num_recs;
6674 static int ocfs2_value_tree_metas_in_bucket(struct super_block *sb,
6675 struct buffer_head *bh,
6676 struct ocfs2_xattr_header *xh,
6677 int offset,
6678 struct ocfs2_xattr_value_root **xv,
6679 struct buffer_head **ret_bh,
6680 void *para)
6682 struct ocfs2_xattr_bucket *bucket =
6683 (struct ocfs2_xattr_bucket *)para;
6685 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6686 xv, ret_bh);
6689 static int ocfs2_calc_value_tree_metas(struct inode *inode,
6690 struct ocfs2_xattr_bucket *bucket,
6691 void *para)
6693 struct ocfs2_value_tree_metas *metas =
6694 (struct ocfs2_value_tree_metas *)para;
6695 struct ocfs2_xattr_header *xh =
6696 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6698 /* Add the credits for this bucket first. */
6699 metas->credits += bucket->bu_blocks;
6700 return ocfs2_value_metas_in_xattr_header(inode->i_sb, bucket->bu_bhs[0],
6701 xh, &metas->num_metas,
6702 &metas->credits, &metas->num_recs,
6703 ocfs2_value_tree_metas_in_bucket,
6704 bucket);
6708 * Given a xattr extent rec starting from blkno and having len clusters,
6709 * iterate all the buckets calculate how much metadata we need for reflinking
6710 * all the ocfs2_xattr_value_root and lock the allocators accordingly.
6712 static int ocfs2_lock_reflink_xattr_rec_allocators(
6713 struct ocfs2_reflink_xattr_tree_args *args,
6714 struct ocfs2_extent_tree *xt_et,
6715 u64 blkno, u32 len, int *credits,
6716 struct ocfs2_alloc_context **meta_ac,
6717 struct ocfs2_alloc_context **data_ac)
6719 int ret, num_free_extents;
6720 struct ocfs2_value_tree_metas metas;
6721 struct ocfs2_super *osb = OCFS2_SB(args->reflink->old_inode->i_sb);
6722 struct ocfs2_refcount_block *rb;
6724 memset(&metas, 0, sizeof(metas));
6726 ret = ocfs2_iterate_xattr_buckets(args->reflink->old_inode, blkno, len,
6727 ocfs2_calc_value_tree_metas, &metas);
6728 if (ret) {
6729 mlog_errno(ret);
6730 goto out;
6733 *credits = metas.credits;
6736 * Calculate we need for refcount tree change.
6738 * We need to add/modify num_recs in refcount tree, so just calculate
6739 * an approximate number we need for refcount tree change.
6740 * Sometimes we need to split the tree, and after split, half recs
6741 * will be moved to the new block, and a new block can only provide
6742 * half number of recs. So we multiple new blocks by 2.
6743 * In the end, we have to add credits for modifying the already
6744 * existed refcount block.
6746 rb = (struct ocfs2_refcount_block *)args->reflink->ref_root_bh->b_data;
6747 metas.num_recs =
6748 (metas.num_recs + ocfs2_refcount_recs_per_rb(osb->sb) - 1) /
6749 ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6750 metas.num_metas += metas.num_recs;
6751 *credits += metas.num_recs +
6752 metas.num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6753 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6754 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6755 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6756 else
6757 *credits += 1;
6759 /* count in the xattr tree change. */
6760 num_free_extents = ocfs2_num_free_extents(osb, xt_et);
6761 if (num_free_extents < 0) {
6762 ret = num_free_extents;
6763 mlog_errno(ret);
6764 goto out;
6767 if (num_free_extents < len)
6768 metas.num_metas += ocfs2_extend_meta_needed(xt_et->et_root_el);
6770 *credits += ocfs2_calc_extend_credits(osb->sb,
6771 xt_et->et_root_el, len);
6773 if (metas.num_metas) {
6774 ret = ocfs2_reserve_new_metadata_blocks(osb, metas.num_metas,
6775 meta_ac);
6776 if (ret) {
6777 mlog_errno(ret);
6778 goto out;
6782 if (len) {
6783 ret = ocfs2_reserve_clusters(osb, len, data_ac);
6784 if (ret)
6785 mlog_errno(ret);
6787 out:
6788 if (ret) {
6789 if (*meta_ac) {
6790 ocfs2_free_alloc_context(*meta_ac);
6791 meta_ac = NULL;
6795 return ret;
6798 static int ocfs2_reflink_xattr_bucket(handle_t *handle,
6799 u64 blkno, u64 new_blkno, u32 clusters,
6800 u32 *cpos, int num_buckets,
6801 struct ocfs2_alloc_context *meta_ac,
6802 struct ocfs2_alloc_context *data_ac,
6803 struct ocfs2_reflink_xattr_tree_args *args)
6805 int i, j, ret = 0;
6806 struct super_block *sb = args->reflink->old_inode->i_sb;
6807 int bpb = args->old_bucket->bu_blocks;
6808 struct ocfs2_xattr_value_buf vb = {
6809 .vb_access = ocfs2_journal_access,
6812 for (i = 0; i < num_buckets; i++, blkno += bpb, new_blkno += bpb) {
6813 ret = ocfs2_read_xattr_bucket(args->old_bucket, blkno);
6814 if (ret) {
6815 mlog_errno(ret);
6816 break;
6819 ret = ocfs2_init_xattr_bucket(args->new_bucket, new_blkno);
6820 if (ret) {
6821 mlog_errno(ret);
6822 break;
6825 ret = ocfs2_xattr_bucket_journal_access(handle,
6826 args->new_bucket,
6827 OCFS2_JOURNAL_ACCESS_CREATE);
6828 if (ret) {
6829 mlog_errno(ret);
6830 break;
6833 for (j = 0; j < bpb; j++)
6834 memcpy(bucket_block(args->new_bucket, j),
6835 bucket_block(args->old_bucket, j),
6836 sb->s_blocksize);
6839 * Record the start cpos so that we can use it to initialize
6840 * our xattr tree we also set the xh_num_bucket for the new
6841 * bucket.
6843 if (i == 0) {
6844 *cpos = le32_to_cpu(bucket_xh(args->new_bucket)->
6845 xh_entries[0].xe_name_hash);
6846 bucket_xh(args->new_bucket)->xh_num_buckets =
6847 cpu_to_le16(num_buckets);
6850 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
6852 ret = ocfs2_reflink_xattr_header(handle, args->reflink,
6853 args->old_bucket->bu_bhs[0],
6854 bucket_xh(args->old_bucket),
6855 args->new_bucket->bu_bhs[0],
6856 bucket_xh(args->new_bucket),
6857 &vb, meta_ac,
6858 ocfs2_get_reflink_xattr_value_root,
6859 args);
6860 if (ret) {
6861 mlog_errno(ret);
6862 break;
6866 * Re-access and dirty the bucket to calculate metaecc.
6867 * Because we may extend the transaction in reflink_xattr_header
6868 * which will let the already accessed block gone.
6870 ret = ocfs2_xattr_bucket_journal_access(handle,
6871 args->new_bucket,
6872 OCFS2_JOURNAL_ACCESS_WRITE);
6873 if (ret) {
6874 mlog_errno(ret);
6875 break;
6878 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
6880 ocfs2_xattr_bucket_relse(args->old_bucket);
6881 ocfs2_xattr_bucket_relse(args->new_bucket);
6884 ocfs2_xattr_bucket_relse(args->old_bucket);
6885 ocfs2_xattr_bucket_relse(args->new_bucket);
6886 return ret;
6889 static int ocfs2_reflink_xattr_buckets(handle_t *handle,
6890 struct inode *inode,
6891 struct ocfs2_reflink_xattr_tree_args *args,
6892 struct ocfs2_extent_tree *et,
6893 struct ocfs2_alloc_context *meta_ac,
6894 struct ocfs2_alloc_context *data_ac,
6895 u64 blkno, u32 cpos, u32 len)
6897 int ret, first_inserted = 0;
6898 u32 p_cluster, num_clusters, reflink_cpos = 0;
6899 u64 new_blkno;
6900 unsigned int num_buckets, reflink_buckets;
6901 unsigned int bpc =
6902 ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
6904 ret = ocfs2_read_xattr_bucket(args->old_bucket, blkno);
6905 if (ret) {
6906 mlog_errno(ret);
6907 goto out;
6909 num_buckets = le16_to_cpu(bucket_xh(args->old_bucket)->xh_num_buckets);
6910 ocfs2_xattr_bucket_relse(args->old_bucket);
6912 while (len && num_buckets) {
6913 ret = ocfs2_claim_clusters(handle, data_ac,
6914 1, &p_cluster, &num_clusters);
6915 if (ret) {
6916 mlog_errno(ret);
6917 goto out;
6920 new_blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
6921 reflink_buckets = min(num_buckets, bpc * num_clusters);
6923 ret = ocfs2_reflink_xattr_bucket(handle, blkno,
6924 new_blkno, num_clusters,
6925 &reflink_cpos, reflink_buckets,
6926 meta_ac, data_ac, args);
6927 if (ret) {
6928 mlog_errno(ret);
6929 goto out;
6933 * For the 1st allocated cluster, we make it use the same cpos
6934 * so that the xattr tree looks the same as the original one
6935 * in the most case.
6937 if (!first_inserted) {
6938 reflink_cpos = cpos;
6939 first_inserted = 1;
6941 ret = ocfs2_insert_extent(handle, et, reflink_cpos, new_blkno,
6942 num_clusters, 0, meta_ac);
6943 if (ret)
6944 mlog_errno(ret);
6946 mlog(0, "insert new xattr extent rec start %llu len %u to %u\n",
6947 (unsigned long long)new_blkno, num_clusters, reflink_cpos);
6949 len -= num_clusters;
6950 blkno += ocfs2_clusters_to_blocks(inode->i_sb, num_clusters);
6951 num_buckets -= reflink_buckets;
6953 out:
6954 return ret;
6958 * Create the same xattr extent record in the new inode's xattr tree.
6960 static int ocfs2_reflink_xattr_rec(struct inode *inode,
6961 struct buffer_head *root_bh,
6962 u64 blkno,
6963 u32 cpos,
6964 u32 len,
6965 void *para)
6967 int ret, credits = 0;
6968 handle_t *handle;
6969 struct ocfs2_reflink_xattr_tree_args *args =
6970 (struct ocfs2_reflink_xattr_tree_args *)para;
6971 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6972 struct ocfs2_alloc_context *meta_ac = NULL;
6973 struct ocfs2_alloc_context *data_ac = NULL;
6974 struct ocfs2_extent_tree et;
6976 mlog(0, "reflink xattr buckets %llu len %u\n",
6977 (unsigned long long)blkno, len);
6979 ocfs2_init_xattr_tree_extent_tree(&et,
6980 INODE_CACHE(args->reflink->new_inode),
6981 args->new_blk_bh);
6983 ret = ocfs2_lock_reflink_xattr_rec_allocators(args, &et, blkno,
6984 len, &credits,
6985 &meta_ac, &data_ac);
6986 if (ret) {
6987 mlog_errno(ret);
6988 goto out;
6991 handle = ocfs2_start_trans(osb, credits);
6992 if (IS_ERR(handle)) {
6993 ret = PTR_ERR(handle);
6994 mlog_errno(ret);
6995 goto out;
6998 ret = ocfs2_reflink_xattr_buckets(handle, inode, args, &et,
6999 meta_ac, data_ac,
7000 blkno, cpos, len);
7001 if (ret)
7002 mlog_errno(ret);
7004 ocfs2_commit_trans(osb, handle);
7006 out:
7007 if (meta_ac)
7008 ocfs2_free_alloc_context(meta_ac);
7009 if (data_ac)
7010 ocfs2_free_alloc_context(data_ac);
7011 return ret;
7015 * Create reflinked xattr buckets.
7016 * We will add bucket one by one, and refcount all the xattrs in the bucket
7017 * if they are stored outside.
7019 static int ocfs2_reflink_xattr_tree(struct ocfs2_xattr_reflink *args,
7020 struct buffer_head *blk_bh,
7021 struct buffer_head *new_blk_bh)
7023 int ret;
7024 struct ocfs2_reflink_xattr_tree_args para;
7026 memset(&para, 0, sizeof(para));
7027 para.reflink = args;
7028 para.old_blk_bh = blk_bh;
7029 para.new_blk_bh = new_blk_bh;
7031 para.old_bucket = ocfs2_xattr_bucket_new(args->old_inode);
7032 if (!para.old_bucket) {
7033 mlog_errno(-ENOMEM);
7034 return -ENOMEM;
7037 para.new_bucket = ocfs2_xattr_bucket_new(args->new_inode);
7038 if (!para.new_bucket) {
7039 ret = -ENOMEM;
7040 mlog_errno(ret);
7041 goto out;
7044 ret = ocfs2_iterate_xattr_index_block(args->old_inode, blk_bh,
7045 ocfs2_reflink_xattr_rec,
7046 &para);
7047 if (ret)
7048 mlog_errno(ret);
7050 out:
7051 ocfs2_xattr_bucket_free(para.old_bucket);
7052 ocfs2_xattr_bucket_free(para.new_bucket);
7053 return ret;
7056 static int ocfs2_reflink_xattr_in_block(struct ocfs2_xattr_reflink *args,
7057 struct buffer_head *blk_bh)
7059 int ret, indexed = 0;
7060 struct buffer_head *new_blk_bh = NULL;
7061 struct ocfs2_xattr_block *xb =
7062 (struct ocfs2_xattr_block *)blk_bh->b_data;
7065 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)
7066 indexed = 1;
7068 ret = ocfs2_create_empty_xattr_block(args->new_inode, args->new_bh,
7069 &new_blk_bh, indexed);
7070 if (ret) {
7071 mlog_errno(ret);
7072 goto out;
7075 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED))
7076 ret = ocfs2_reflink_xattr_block(args, blk_bh, new_blk_bh);
7077 else
7078 ret = ocfs2_reflink_xattr_tree(args, blk_bh, new_blk_bh);
7079 if (ret)
7080 mlog_errno(ret);
7082 out:
7083 brelse(new_blk_bh);
7084 return ret;
7087 static int ocfs2_reflink_xattr_no_security(struct ocfs2_xattr_entry *xe)
7089 int type = ocfs2_xattr_get_type(xe);
7091 return type != OCFS2_XATTR_INDEX_SECURITY &&
7092 type != OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS &&
7093 type != OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
7096 int ocfs2_reflink_xattrs(struct inode *old_inode,
7097 struct buffer_head *old_bh,
7098 struct inode *new_inode,
7099 struct buffer_head *new_bh,
7100 bool preserve_security)
7102 int ret;
7103 struct ocfs2_xattr_reflink args;
7104 struct ocfs2_inode_info *oi = OCFS2_I(old_inode);
7105 struct ocfs2_dinode *di = (struct ocfs2_dinode *)old_bh->b_data;
7106 struct buffer_head *blk_bh = NULL;
7107 struct ocfs2_cached_dealloc_ctxt dealloc;
7108 struct ocfs2_refcount_tree *ref_tree;
7109 struct buffer_head *ref_root_bh = NULL;
7111 ret = ocfs2_lock_refcount_tree(OCFS2_SB(old_inode->i_sb),
7112 le64_to_cpu(di->i_refcount_loc),
7113 1, &ref_tree, &ref_root_bh);
7114 if (ret) {
7115 mlog_errno(ret);
7116 goto out;
7119 ocfs2_init_dealloc_ctxt(&dealloc);
7121 args.old_inode = old_inode;
7122 args.new_inode = new_inode;
7123 args.old_bh = old_bh;
7124 args.new_bh = new_bh;
7125 args.ref_ci = &ref_tree->rf_ci;
7126 args.ref_root_bh = ref_root_bh;
7127 args.dealloc = &dealloc;
7128 if (preserve_security)
7129 args.xattr_reflinked = NULL;
7130 else
7131 args.xattr_reflinked = ocfs2_reflink_xattr_no_security;
7133 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
7134 ret = ocfs2_reflink_xattr_inline(&args);
7135 if (ret) {
7136 mlog_errno(ret);
7137 goto out_unlock;
7141 if (!di->i_xattr_loc)
7142 goto out_unlock;
7144 ret = ocfs2_read_xattr_block(old_inode, le64_to_cpu(di->i_xattr_loc),
7145 &blk_bh);
7146 if (ret < 0) {
7147 mlog_errno(ret);
7148 goto out_unlock;
7151 ret = ocfs2_reflink_xattr_in_block(&args, blk_bh);
7152 if (ret)
7153 mlog_errno(ret);
7155 brelse(blk_bh);
7157 out_unlock:
7158 ocfs2_unlock_refcount_tree(OCFS2_SB(old_inode->i_sb),
7159 ref_tree, 1);
7160 brelse(ref_root_bh);
7162 if (ocfs2_dealloc_has_cluster(&dealloc)) {
7163 ocfs2_schedule_truncate_log_flush(OCFS2_SB(old_inode->i_sb), 1);
7164 ocfs2_run_deallocs(OCFS2_SB(old_inode->i_sb), &dealloc);
7167 out:
7168 return ret;
7172 * Initialize security and acl for a already created inode.
7173 * Used for reflink a non-preserve-security file.
7175 * It uses common api like ocfs2_xattr_set, so the caller
7176 * must not hold any lock expect i_mutex.
7178 int ocfs2_init_security_and_acl(struct inode *dir,
7179 struct inode *inode)
7181 int ret = 0;
7182 struct buffer_head *dir_bh = NULL;
7183 struct ocfs2_security_xattr_info si = {
7184 .enable = 1,
7187 ret = ocfs2_init_security_get(inode, dir, &si);
7188 if (!ret) {
7189 ret = ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY,
7190 si.name, si.value, si.value_len,
7191 XATTR_CREATE);
7192 if (ret) {
7193 mlog_errno(ret);
7194 goto leave;
7196 } else if (ret != -EOPNOTSUPP) {
7197 mlog_errno(ret);
7198 goto leave;
7201 ret = ocfs2_inode_lock(dir, &dir_bh, 0);
7202 if (ret) {
7203 mlog_errno(ret);
7204 goto leave;
7207 ret = ocfs2_init_acl(NULL, inode, dir, NULL, dir_bh, NULL, NULL);
7208 if (ret)
7209 mlog_errno(ret);
7211 ocfs2_inode_unlock(dir, 0);
7212 brelse(dir_bh);
7213 leave:
7214 return ret;
7217 * 'security' attributes support
7219 static size_t ocfs2_xattr_security_list(struct dentry *dentry, char *list,
7220 size_t list_size, const char *name,
7221 size_t name_len, int type)
7223 const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN;
7224 const size_t total_len = prefix_len + name_len + 1;
7226 if (list && total_len <= list_size) {
7227 memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
7228 memcpy(list + prefix_len, name, name_len);
7229 list[prefix_len + name_len] = '\0';
7231 return total_len;
7234 static int ocfs2_xattr_security_get(struct dentry *dentry, const char *name,
7235 void *buffer, size_t size, int type)
7237 if (strcmp(name, "") == 0)
7238 return -EINVAL;
7239 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
7240 name, buffer, size);
7243 static int ocfs2_xattr_security_set(struct dentry *dentry, const char *name,
7244 const void *value, size_t size, int flags, int type)
7246 if (strcmp(name, "") == 0)
7247 return -EINVAL;
7249 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
7250 name, value, size, flags);
7253 int ocfs2_init_security_get(struct inode *inode,
7254 struct inode *dir,
7255 struct ocfs2_security_xattr_info *si)
7257 /* check whether ocfs2 support feature xattr */
7258 if (!ocfs2_supports_xattr(OCFS2_SB(dir->i_sb)))
7259 return -EOPNOTSUPP;
7260 return security_inode_init_security(inode, dir, &si->name, &si->value,
7261 &si->value_len);
7264 int ocfs2_init_security_set(handle_t *handle,
7265 struct inode *inode,
7266 struct buffer_head *di_bh,
7267 struct ocfs2_security_xattr_info *si,
7268 struct ocfs2_alloc_context *xattr_ac,
7269 struct ocfs2_alloc_context *data_ac)
7271 return ocfs2_xattr_set_handle(handle, inode, di_bh,
7272 OCFS2_XATTR_INDEX_SECURITY,
7273 si->name, si->value, si->value_len, 0,
7274 xattr_ac, data_ac);
7277 const struct xattr_handler ocfs2_xattr_security_handler = {
7278 .prefix = XATTR_SECURITY_PREFIX,
7279 .list = ocfs2_xattr_security_list,
7280 .get = ocfs2_xattr_security_get,
7281 .set = ocfs2_xattr_security_set,
7285 * 'trusted' attributes support
7287 static size_t ocfs2_xattr_trusted_list(struct dentry *dentry, char *list,
7288 size_t list_size, const char *name,
7289 size_t name_len, int type)
7291 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
7292 const size_t total_len = prefix_len + name_len + 1;
7294 if (list && total_len <= list_size) {
7295 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
7296 memcpy(list + prefix_len, name, name_len);
7297 list[prefix_len + name_len] = '\0';
7299 return total_len;
7302 static int ocfs2_xattr_trusted_get(struct dentry *dentry, const char *name,
7303 void *buffer, size_t size, int type)
7305 if (strcmp(name, "") == 0)
7306 return -EINVAL;
7307 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
7308 name, buffer, size);
7311 static int ocfs2_xattr_trusted_set(struct dentry *dentry, const char *name,
7312 const void *value, size_t size, int flags, int type)
7314 if (strcmp(name, "") == 0)
7315 return -EINVAL;
7317 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
7318 name, value, size, flags);
7321 const struct xattr_handler ocfs2_xattr_trusted_handler = {
7322 .prefix = XATTR_TRUSTED_PREFIX,
7323 .list = ocfs2_xattr_trusted_list,
7324 .get = ocfs2_xattr_trusted_get,
7325 .set = ocfs2_xattr_trusted_set,
7329 * 'user' attributes support
7331 static size_t ocfs2_xattr_user_list(struct dentry *dentry, char *list,
7332 size_t list_size, const char *name,
7333 size_t name_len, int type)
7335 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
7336 const size_t total_len = prefix_len + name_len + 1;
7337 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
7339 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7340 return 0;
7342 if (list && total_len <= list_size) {
7343 memcpy(list, XATTR_USER_PREFIX, prefix_len);
7344 memcpy(list + prefix_len, name, name_len);
7345 list[prefix_len + name_len] = '\0';
7347 return total_len;
7350 static int ocfs2_xattr_user_get(struct dentry *dentry, const char *name,
7351 void *buffer, size_t size, int type)
7353 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
7355 if (strcmp(name, "") == 0)
7356 return -EINVAL;
7357 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7358 return -EOPNOTSUPP;
7359 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_USER, name,
7360 buffer, size);
7363 static int ocfs2_xattr_user_set(struct dentry *dentry, const char *name,
7364 const void *value, size_t size, int flags, int type)
7366 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
7368 if (strcmp(name, "") == 0)
7369 return -EINVAL;
7370 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7371 return -EOPNOTSUPP;
7373 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_USER,
7374 name, value, size, flags);
7377 const struct xattr_handler ocfs2_xattr_user_handler = {
7378 .prefix = XATTR_USER_PREFIX,
7379 .list = ocfs2_xattr_user_list,
7380 .get = ocfs2_xattr_user_get,
7381 .set = ocfs2_xattr_user_set,