USB: io_ti: check firmware version before updating
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / ocfs2 / xattr.c
bloba1c365012ea0e3d297048085699b9bb4c023bde0
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;
84 #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
85 #define OCFS2_XATTR_INLINE_SIZE 80
86 #define OCFS2_XATTR_HEADER_GAP 4
87 #define OCFS2_XATTR_FREE_IN_IBODY (OCFS2_MIN_XATTR_INLINE_SIZE \
88 - sizeof(struct ocfs2_xattr_header) \
89 - OCFS2_XATTR_HEADER_GAP)
90 #define OCFS2_XATTR_FREE_IN_BLOCK(ptr) ((ptr)->i_sb->s_blocksize \
91 - sizeof(struct ocfs2_xattr_block) \
92 - sizeof(struct ocfs2_xattr_header) \
93 - OCFS2_XATTR_HEADER_GAP)
95 static struct ocfs2_xattr_def_value_root def_xv = {
96 .xv.xr_list.l_count = cpu_to_le16(1),
99 struct xattr_handler *ocfs2_xattr_handlers[] = {
100 &ocfs2_xattr_user_handler,
101 &ocfs2_xattr_acl_access_handler,
102 &ocfs2_xattr_acl_default_handler,
103 &ocfs2_xattr_trusted_handler,
104 &ocfs2_xattr_security_handler,
105 NULL
108 static struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
109 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
110 [OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS]
111 = &ocfs2_xattr_acl_access_handler,
112 [OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT]
113 = &ocfs2_xattr_acl_default_handler,
114 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
115 [OCFS2_XATTR_INDEX_SECURITY] = &ocfs2_xattr_security_handler,
118 struct ocfs2_xattr_info {
119 int xi_name_index;
120 const char *xi_name;
121 int xi_name_len;
122 const void *xi_value;
123 size_t xi_value_len;
126 struct ocfs2_xattr_search {
127 struct buffer_head *inode_bh;
129 * xattr_bh point to the block buffer head which has extended attribute
130 * when extended attribute in inode, xattr_bh is equal to inode_bh.
132 struct buffer_head *xattr_bh;
133 struct ocfs2_xattr_header *header;
134 struct ocfs2_xattr_bucket *bucket;
135 void *base;
136 void *end;
137 struct ocfs2_xattr_entry *here;
138 int not_found;
141 /* Operations on struct ocfs2_xa_entry */
142 struct ocfs2_xa_loc;
143 struct ocfs2_xa_loc_operations {
145 * Journal functions
147 int (*xlo_journal_access)(handle_t *handle, struct ocfs2_xa_loc *loc,
148 int type);
149 void (*xlo_journal_dirty)(handle_t *handle, struct ocfs2_xa_loc *loc);
152 * Return a pointer to the appropriate buffer in loc->xl_storage
153 * at the given offset from loc->xl_header.
155 void *(*xlo_offset_pointer)(struct ocfs2_xa_loc *loc, int offset);
157 /* Can we reuse the existing entry for the new value? */
158 int (*xlo_can_reuse)(struct ocfs2_xa_loc *loc,
159 struct ocfs2_xattr_info *xi);
161 /* How much space is needed for the new value? */
162 int (*xlo_check_space)(struct ocfs2_xa_loc *loc,
163 struct ocfs2_xattr_info *xi);
166 * Return the offset of the first name+value pair. This is
167 * the start of our downward-filling free space.
169 int (*xlo_get_free_start)(struct ocfs2_xa_loc *loc);
172 * Remove the name+value at this location. Do whatever is
173 * appropriate with the remaining name+value pairs.
175 void (*xlo_wipe_namevalue)(struct ocfs2_xa_loc *loc);
177 /* Fill xl_entry with a new entry */
178 void (*xlo_add_entry)(struct ocfs2_xa_loc *loc, u32 name_hash);
180 /* Add name+value storage to an entry */
181 void (*xlo_add_namevalue)(struct ocfs2_xa_loc *loc, int size);
184 * Initialize the value buf's access and bh fields for this entry.
185 * ocfs2_xa_fill_value_buf() will handle the xv pointer.
187 void (*xlo_fill_value_buf)(struct ocfs2_xa_loc *loc,
188 struct ocfs2_xattr_value_buf *vb);
192 * Describes an xattr entry location. This is a memory structure
193 * tracking the on-disk structure.
195 struct ocfs2_xa_loc {
196 /* This xattr belongs to this inode */
197 struct inode *xl_inode;
199 /* The ocfs2_xattr_header inside the on-disk storage. Not NULL. */
200 struct ocfs2_xattr_header *xl_header;
202 /* Bytes from xl_header to the end of the storage */
203 int xl_size;
206 * The ocfs2_xattr_entry this location describes. If this is
207 * NULL, this location describes the on-disk structure where it
208 * would have been.
210 struct ocfs2_xattr_entry *xl_entry;
213 * Internal housekeeping
216 /* Buffer(s) containing this entry */
217 void *xl_storage;
219 /* Operations on the storage backing this location */
220 const struct ocfs2_xa_loc_operations *xl_ops;
224 * Convenience functions to calculate how much space is needed for a
225 * given name+value pair
227 static int namevalue_size(int name_len, uint64_t value_len)
229 if (value_len > OCFS2_XATTR_INLINE_SIZE)
230 return OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
231 else
232 return OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
235 static int namevalue_size_xi(struct ocfs2_xattr_info *xi)
237 return namevalue_size(xi->xi_name_len, xi->xi_value_len);
240 static int namevalue_size_xe(struct ocfs2_xattr_entry *xe)
242 u64 value_len = le64_to_cpu(xe->xe_value_size);
244 BUG_ON((value_len > OCFS2_XATTR_INLINE_SIZE) &&
245 ocfs2_xattr_is_local(xe));
246 return namevalue_size(xe->xe_name_len, value_len);
250 static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
251 struct ocfs2_xattr_header *xh,
252 int index,
253 int *block_off,
254 int *new_offset);
256 static int ocfs2_xattr_block_find(struct inode *inode,
257 int name_index,
258 const char *name,
259 struct ocfs2_xattr_search *xs);
260 static int ocfs2_xattr_index_block_find(struct inode *inode,
261 struct buffer_head *root_bh,
262 int name_index,
263 const char *name,
264 struct ocfs2_xattr_search *xs);
266 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
267 struct buffer_head *blk_bh,
268 char *buffer,
269 size_t buffer_size);
271 static int ocfs2_xattr_create_index_block(struct inode *inode,
272 struct ocfs2_xattr_search *xs,
273 struct ocfs2_xattr_set_ctxt *ctxt);
275 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
276 struct ocfs2_xattr_info *xi,
277 struct ocfs2_xattr_search *xs,
278 struct ocfs2_xattr_set_ctxt *ctxt);
280 typedef int (xattr_tree_rec_func)(struct inode *inode,
281 struct buffer_head *root_bh,
282 u64 blkno, u32 cpos, u32 len, void *para);
283 static int ocfs2_iterate_xattr_index_block(struct inode *inode,
284 struct buffer_head *root_bh,
285 xattr_tree_rec_func *rec_func,
286 void *para);
287 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
288 struct ocfs2_xattr_bucket *bucket,
289 void *para);
290 static int ocfs2_rm_xattr_cluster(struct inode *inode,
291 struct buffer_head *root_bh,
292 u64 blkno,
293 u32 cpos,
294 u32 len,
295 void *para);
297 static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
298 u64 src_blk, u64 last_blk, u64 to_blk,
299 unsigned int start_bucket,
300 u32 *first_hash);
301 static int ocfs2_prepare_refcount_xattr(struct inode *inode,
302 struct ocfs2_dinode *di,
303 struct ocfs2_xattr_info *xi,
304 struct ocfs2_xattr_search *xis,
305 struct ocfs2_xattr_search *xbs,
306 struct ocfs2_refcount_tree **ref_tree,
307 int *meta_need,
308 int *credits);
309 static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
310 struct ocfs2_xattr_bucket *bucket,
311 int offset,
312 struct ocfs2_xattr_value_root **xv,
313 struct buffer_head **bh);
315 static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
317 return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
320 static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
322 return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
325 #define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
326 #define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
327 #define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
329 static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
331 struct ocfs2_xattr_bucket *bucket;
332 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
334 BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
336 bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
337 if (bucket) {
338 bucket->bu_inode = inode;
339 bucket->bu_blocks = blks;
342 return bucket;
345 static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
347 int i;
349 for (i = 0; i < bucket->bu_blocks; i++) {
350 brelse(bucket->bu_bhs[i]);
351 bucket->bu_bhs[i] = NULL;
355 static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
357 if (bucket) {
358 ocfs2_xattr_bucket_relse(bucket);
359 bucket->bu_inode = NULL;
360 kfree(bucket);
365 * A bucket that has never been written to disk doesn't need to be
366 * read. We just need the buffer_heads. Don't call this for
367 * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
368 * them fully.
370 static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
371 u64 xb_blkno)
373 int i, rc = 0;
375 for (i = 0; i < bucket->bu_blocks; i++) {
376 bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
377 xb_blkno + i);
378 if (!bucket->bu_bhs[i]) {
379 rc = -EIO;
380 mlog_errno(rc);
381 break;
384 if (!ocfs2_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
385 bucket->bu_bhs[i]))
386 ocfs2_set_new_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
387 bucket->bu_bhs[i]);
390 if (rc)
391 ocfs2_xattr_bucket_relse(bucket);
392 return rc;
395 /* Read the xattr bucket at xb_blkno */
396 static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
397 u64 xb_blkno)
399 int rc;
401 rc = ocfs2_read_blocks(INODE_CACHE(bucket->bu_inode), xb_blkno,
402 bucket->bu_blocks, bucket->bu_bhs, 0,
403 NULL);
404 if (!rc) {
405 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
406 rc = ocfs2_validate_meta_ecc_bhs(bucket->bu_inode->i_sb,
407 bucket->bu_bhs,
408 bucket->bu_blocks,
409 &bucket_xh(bucket)->xh_check);
410 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
411 if (rc)
412 mlog_errno(rc);
415 if (rc)
416 ocfs2_xattr_bucket_relse(bucket);
417 return rc;
420 static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
421 struct ocfs2_xattr_bucket *bucket,
422 int type)
424 int i, rc = 0;
426 for (i = 0; i < bucket->bu_blocks; i++) {
427 rc = ocfs2_journal_access(handle,
428 INODE_CACHE(bucket->bu_inode),
429 bucket->bu_bhs[i], type);
430 if (rc) {
431 mlog_errno(rc);
432 break;
436 return rc;
439 static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
440 struct ocfs2_xattr_bucket *bucket)
442 int i;
444 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
445 ocfs2_compute_meta_ecc_bhs(bucket->bu_inode->i_sb,
446 bucket->bu_bhs, bucket->bu_blocks,
447 &bucket_xh(bucket)->xh_check);
448 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
450 for (i = 0; i < bucket->bu_blocks; i++)
451 ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
454 static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
455 struct ocfs2_xattr_bucket *src)
457 int i;
458 int blocksize = src->bu_inode->i_sb->s_blocksize;
460 BUG_ON(dest->bu_blocks != src->bu_blocks);
461 BUG_ON(dest->bu_inode != src->bu_inode);
463 for (i = 0; i < src->bu_blocks; i++) {
464 memcpy(bucket_block(dest, i), bucket_block(src, i),
465 blocksize);
469 static int ocfs2_validate_xattr_block(struct super_block *sb,
470 struct buffer_head *bh)
472 int rc;
473 struct ocfs2_xattr_block *xb =
474 (struct ocfs2_xattr_block *)bh->b_data;
476 mlog(0, "Validating xattr block %llu\n",
477 (unsigned long long)bh->b_blocknr);
479 BUG_ON(!buffer_uptodate(bh));
482 * If the ecc fails, we return the error but otherwise
483 * leave the filesystem running. We know any error is
484 * local to this block.
486 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &xb->xb_check);
487 if (rc)
488 return rc;
491 * Errors after here are fatal
494 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
495 ocfs2_error(sb,
496 "Extended attribute block #%llu has bad "
497 "signature %.*s",
498 (unsigned long long)bh->b_blocknr, 7,
499 xb->xb_signature);
500 return -EINVAL;
503 if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) {
504 ocfs2_error(sb,
505 "Extended attribute block #%llu has an "
506 "invalid xb_blkno of %llu",
507 (unsigned long long)bh->b_blocknr,
508 (unsigned long long)le64_to_cpu(xb->xb_blkno));
509 return -EINVAL;
512 if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) {
513 ocfs2_error(sb,
514 "Extended attribute block #%llu has an invalid "
515 "xb_fs_generation of #%u",
516 (unsigned long long)bh->b_blocknr,
517 le32_to_cpu(xb->xb_fs_generation));
518 return -EINVAL;
521 return 0;
524 static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno,
525 struct buffer_head **bh)
527 int rc;
528 struct buffer_head *tmp = *bh;
530 rc = ocfs2_read_block(INODE_CACHE(inode), xb_blkno, &tmp,
531 ocfs2_validate_xattr_block);
533 /* If ocfs2_read_block() got us a new bh, pass it up. */
534 if (!rc && !*bh)
535 *bh = tmp;
537 return rc;
540 static inline const char *ocfs2_xattr_prefix(int name_index)
542 struct xattr_handler *handler = NULL;
544 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
545 handler = ocfs2_xattr_handler_map[name_index];
547 return handler ? handler->prefix : NULL;
550 static u32 ocfs2_xattr_name_hash(struct inode *inode,
551 const char *name,
552 int name_len)
554 /* Get hash value of uuid from super block */
555 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
556 int i;
558 /* hash extended attribute name */
559 for (i = 0; i < name_len; i++) {
560 hash = (hash << OCFS2_HASH_SHIFT) ^
561 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
562 *name++;
565 return hash;
568 static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
570 return namevalue_size(name_len, value_len) +
571 sizeof(struct ocfs2_xattr_entry);
574 static int ocfs2_xi_entry_usage(struct ocfs2_xattr_info *xi)
576 return namevalue_size_xi(xi) +
577 sizeof(struct ocfs2_xattr_entry);
580 static int ocfs2_xe_entry_usage(struct ocfs2_xattr_entry *xe)
582 return namevalue_size_xe(xe) +
583 sizeof(struct ocfs2_xattr_entry);
586 int ocfs2_calc_security_init(struct inode *dir,
587 struct ocfs2_security_xattr_info *si,
588 int *want_clusters,
589 int *xattr_credits,
590 struct ocfs2_alloc_context **xattr_ac)
592 int ret = 0;
593 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
594 int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
595 si->value_len);
598 * The max space of security xattr taken inline is
599 * 256(name) + 80(value) + 16(entry) = 352 bytes,
600 * So reserve one metadata block for it is ok.
602 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
603 s_size > OCFS2_XATTR_FREE_IN_IBODY) {
604 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
605 if (ret) {
606 mlog_errno(ret);
607 return ret;
609 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
612 /* reserve clusters for xattr value which will be set in B tree*/
613 if (si->value_len > OCFS2_XATTR_INLINE_SIZE) {
614 int new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
615 si->value_len);
617 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
618 new_clusters);
619 *want_clusters += new_clusters;
621 return ret;
624 int ocfs2_calc_xattr_init(struct inode *dir,
625 struct buffer_head *dir_bh,
626 int mode,
627 struct ocfs2_security_xattr_info *si,
628 int *want_clusters,
629 int *xattr_credits,
630 int *want_meta)
632 int ret = 0;
633 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
634 int s_size = 0, a_size = 0, acl_len = 0, new_clusters;
636 if (si->enable)
637 s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
638 si->value_len);
640 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
641 acl_len = ocfs2_xattr_get_nolock(dir, dir_bh,
642 OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
643 "", NULL, 0);
644 if (acl_len > 0) {
645 a_size = ocfs2_xattr_entry_real_size(0, acl_len);
646 if (S_ISDIR(mode))
647 a_size <<= 1;
648 } else if (acl_len != 0 && acl_len != -ENODATA) {
649 mlog_errno(ret);
650 return ret;
654 if (!(s_size + a_size))
655 return ret;
658 * The max space of security xattr taken inline is
659 * 256(name) + 80(value) + 16(entry) = 352 bytes,
660 * The max space of acl xattr taken inline is
661 * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
662 * when blocksize = 512, may reserve one more cluser for
663 * xattr bucket, otherwise reserve one metadata block
664 * for them is ok.
665 * If this is a new directory with inline data,
666 * we choose to reserve the entire inline area for
667 * directory contents and force an external xattr block.
669 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
670 (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) ||
671 (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) {
672 *want_meta = *want_meta + 1;
673 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
676 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
677 (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
678 *want_clusters += 1;
679 *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
683 * reserve credits and clusters for xattrs which has large value
684 * and have to be set outside
686 if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE) {
687 new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
688 si->value_len);
689 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
690 new_clusters);
691 *want_clusters += new_clusters;
693 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
694 acl_len > OCFS2_XATTR_INLINE_SIZE) {
695 /* for directory, it has DEFAULT and ACCESS two types of acls */
696 new_clusters = (S_ISDIR(mode) ? 2 : 1) *
697 ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
698 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
699 new_clusters);
700 *want_clusters += new_clusters;
703 return ret;
706 static int ocfs2_xattr_extend_allocation(struct inode *inode,
707 u32 clusters_to_add,
708 struct ocfs2_xattr_value_buf *vb,
709 struct ocfs2_xattr_set_ctxt *ctxt)
711 int status = 0, credits;
712 handle_t *handle = ctxt->handle;
713 enum ocfs2_alloc_restarted why;
714 u32 prev_clusters, logical_start = le32_to_cpu(vb->vb_xv->xr_clusters);
715 struct ocfs2_extent_tree et;
717 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
719 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
721 while (clusters_to_add) {
722 status = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
723 OCFS2_JOURNAL_ACCESS_WRITE);
724 if (status < 0) {
725 mlog_errno(status);
726 break;
729 prev_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
730 status = ocfs2_add_clusters_in_btree(handle,
731 &et,
732 &logical_start,
733 clusters_to_add,
735 ctxt->data_ac,
736 ctxt->meta_ac,
737 &why);
738 if ((status < 0) && (status != -EAGAIN)) {
739 if (status != -ENOSPC)
740 mlog_errno(status);
741 break;
744 status = ocfs2_journal_dirty(handle, vb->vb_bh);
745 if (status < 0) {
746 mlog_errno(status);
747 break;
750 clusters_to_add -= le32_to_cpu(vb->vb_xv->xr_clusters) -
751 prev_clusters;
753 if (why != RESTART_NONE && clusters_to_add) {
755 * We can only fail in case the alloc file doesn't give
756 * up enough clusters.
758 BUG_ON(why == RESTART_META);
760 mlog(0, "restarting xattr value extension for %u"
761 " clusters,.\n", clusters_to_add);
762 credits = ocfs2_calc_extend_credits(inode->i_sb,
763 &vb->vb_xv->xr_list,
764 clusters_to_add);
765 status = ocfs2_extend_trans(handle, credits);
766 if (status < 0) {
767 status = -ENOMEM;
768 mlog_errno(status);
769 break;
774 return status;
777 static int __ocfs2_remove_xattr_range(struct inode *inode,
778 struct ocfs2_xattr_value_buf *vb,
779 u32 cpos, u32 phys_cpos, u32 len,
780 unsigned int ext_flags,
781 struct ocfs2_xattr_set_ctxt *ctxt)
783 int ret;
784 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
785 handle_t *handle = ctxt->handle;
786 struct ocfs2_extent_tree et;
788 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
790 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
791 OCFS2_JOURNAL_ACCESS_WRITE);
792 if (ret) {
793 mlog_errno(ret);
794 goto out;
797 ret = ocfs2_remove_extent(handle, &et, cpos, len, ctxt->meta_ac,
798 &ctxt->dealloc);
799 if (ret) {
800 mlog_errno(ret);
801 goto out;
804 le32_add_cpu(&vb->vb_xv->xr_clusters, -len);
806 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
807 if (ret) {
808 mlog_errno(ret);
809 goto out;
812 if (ext_flags & OCFS2_EXT_REFCOUNTED)
813 ret = ocfs2_decrease_refcount(inode, handle,
814 ocfs2_blocks_to_clusters(inode->i_sb,
815 phys_blkno),
816 len, ctxt->meta_ac, &ctxt->dealloc, 1);
817 else
818 ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc,
819 phys_blkno, len);
820 if (ret)
821 mlog_errno(ret);
823 out:
824 return ret;
827 static int ocfs2_xattr_shrink_size(struct inode *inode,
828 u32 old_clusters,
829 u32 new_clusters,
830 struct ocfs2_xattr_value_buf *vb,
831 struct ocfs2_xattr_set_ctxt *ctxt)
833 int ret = 0;
834 unsigned int ext_flags;
835 u32 trunc_len, cpos, phys_cpos, alloc_size;
836 u64 block;
838 if (old_clusters <= new_clusters)
839 return 0;
841 cpos = new_clusters;
842 trunc_len = old_clusters - new_clusters;
843 while (trunc_len) {
844 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
845 &alloc_size,
846 &vb->vb_xv->xr_list, &ext_flags);
847 if (ret) {
848 mlog_errno(ret);
849 goto out;
852 if (alloc_size > trunc_len)
853 alloc_size = trunc_len;
855 ret = __ocfs2_remove_xattr_range(inode, vb, cpos,
856 phys_cpos, alloc_size,
857 ext_flags, ctxt);
858 if (ret) {
859 mlog_errno(ret);
860 goto out;
863 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
864 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode),
865 block, alloc_size);
866 cpos += alloc_size;
867 trunc_len -= alloc_size;
870 out:
871 return ret;
874 static int ocfs2_xattr_value_truncate(struct inode *inode,
875 struct ocfs2_xattr_value_buf *vb,
876 int len,
877 struct ocfs2_xattr_set_ctxt *ctxt)
879 int ret;
880 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
881 u32 old_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
883 if (new_clusters == old_clusters)
884 return 0;
886 if (new_clusters > old_clusters)
887 ret = ocfs2_xattr_extend_allocation(inode,
888 new_clusters - old_clusters,
889 vb, ctxt);
890 else
891 ret = ocfs2_xattr_shrink_size(inode,
892 old_clusters, new_clusters,
893 vb, ctxt);
895 return ret;
898 static int ocfs2_xattr_list_entry(char *buffer, size_t size,
899 size_t *result, const char *prefix,
900 const char *name, int name_len)
902 char *p = buffer + *result;
903 int prefix_len = strlen(prefix);
904 int total_len = prefix_len + name_len + 1;
906 *result += total_len;
908 /* we are just looking for how big our buffer needs to be */
909 if (!size)
910 return 0;
912 if (*result > size)
913 return -ERANGE;
915 memcpy(p, prefix, prefix_len);
916 memcpy(p + prefix_len, name, name_len);
917 p[prefix_len + name_len] = '\0';
919 return 0;
922 static int ocfs2_xattr_list_entries(struct inode *inode,
923 struct ocfs2_xattr_header *header,
924 char *buffer, size_t buffer_size)
926 size_t result = 0;
927 int i, type, ret;
928 const char *prefix, *name;
930 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
931 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
932 type = ocfs2_xattr_get_type(entry);
933 prefix = ocfs2_xattr_prefix(type);
935 if (prefix) {
936 name = (const char *)header +
937 le16_to_cpu(entry->xe_name_offset);
939 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
940 &result, prefix, name,
941 entry->xe_name_len);
942 if (ret)
943 return ret;
947 return result;
950 int ocfs2_has_inline_xattr_value_outside(struct inode *inode,
951 struct ocfs2_dinode *di)
953 struct ocfs2_xattr_header *xh;
954 int i;
956 xh = (struct ocfs2_xattr_header *)
957 ((void *)di + inode->i_sb->s_blocksize -
958 le16_to_cpu(di->i_xattr_inline_size));
960 for (i = 0; i < le16_to_cpu(xh->xh_count); i++)
961 if (!ocfs2_xattr_is_local(&xh->xh_entries[i]))
962 return 1;
964 return 0;
967 static int ocfs2_xattr_ibody_list(struct inode *inode,
968 struct ocfs2_dinode *di,
969 char *buffer,
970 size_t buffer_size)
972 struct ocfs2_xattr_header *header = NULL;
973 struct ocfs2_inode_info *oi = OCFS2_I(inode);
974 int ret = 0;
976 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
977 return ret;
979 header = (struct ocfs2_xattr_header *)
980 ((void *)di + inode->i_sb->s_blocksize -
981 le16_to_cpu(di->i_xattr_inline_size));
983 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
985 return ret;
988 static int ocfs2_xattr_block_list(struct inode *inode,
989 struct ocfs2_dinode *di,
990 char *buffer,
991 size_t buffer_size)
993 struct buffer_head *blk_bh = NULL;
994 struct ocfs2_xattr_block *xb;
995 int ret = 0;
997 if (!di->i_xattr_loc)
998 return ret;
1000 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
1001 &blk_bh);
1002 if (ret < 0) {
1003 mlog_errno(ret);
1004 return ret;
1007 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1008 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1009 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
1010 ret = ocfs2_xattr_list_entries(inode, header,
1011 buffer, buffer_size);
1012 } else
1013 ret = ocfs2_xattr_tree_list_index_block(inode, blk_bh,
1014 buffer, buffer_size);
1016 brelse(blk_bh);
1018 return ret;
1021 ssize_t ocfs2_listxattr(struct dentry *dentry,
1022 char *buffer,
1023 size_t size)
1025 int ret = 0, i_ret = 0, b_ret = 0;
1026 struct buffer_head *di_bh = NULL;
1027 struct ocfs2_dinode *di = NULL;
1028 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
1030 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
1031 return -EOPNOTSUPP;
1033 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1034 return ret;
1036 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
1037 if (ret < 0) {
1038 mlog_errno(ret);
1039 return ret;
1042 di = (struct ocfs2_dinode *)di_bh->b_data;
1044 down_read(&oi->ip_xattr_sem);
1045 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
1046 if (i_ret < 0)
1047 b_ret = 0;
1048 else {
1049 if (buffer) {
1050 buffer += i_ret;
1051 size -= i_ret;
1053 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
1054 buffer, size);
1055 if (b_ret < 0)
1056 i_ret = 0;
1058 up_read(&oi->ip_xattr_sem);
1059 ocfs2_inode_unlock(dentry->d_inode, 0);
1061 brelse(di_bh);
1063 return i_ret + b_ret;
1066 static int ocfs2_xattr_find_entry(int name_index,
1067 const char *name,
1068 struct ocfs2_xattr_search *xs)
1070 struct ocfs2_xattr_entry *entry;
1071 size_t name_len;
1072 int i, cmp = 1;
1074 if (name == NULL)
1075 return -EINVAL;
1077 name_len = strlen(name);
1078 entry = xs->here;
1079 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
1080 cmp = name_index - ocfs2_xattr_get_type(entry);
1081 if (!cmp)
1082 cmp = name_len - entry->xe_name_len;
1083 if (!cmp)
1084 cmp = memcmp(name, (xs->base +
1085 le16_to_cpu(entry->xe_name_offset)),
1086 name_len);
1087 if (cmp == 0)
1088 break;
1089 entry += 1;
1091 xs->here = entry;
1093 return cmp ? -ENODATA : 0;
1096 static int ocfs2_xattr_get_value_outside(struct inode *inode,
1097 struct ocfs2_xattr_value_root *xv,
1098 void *buffer,
1099 size_t len)
1101 u32 cpos, p_cluster, num_clusters, bpc, clusters;
1102 u64 blkno;
1103 int i, ret = 0;
1104 size_t cplen, blocksize;
1105 struct buffer_head *bh = NULL;
1106 struct ocfs2_extent_list *el;
1108 el = &xv->xr_list;
1109 clusters = le32_to_cpu(xv->xr_clusters);
1110 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1111 blocksize = inode->i_sb->s_blocksize;
1113 cpos = 0;
1114 while (cpos < clusters) {
1115 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1116 &num_clusters, el, NULL);
1117 if (ret) {
1118 mlog_errno(ret);
1119 goto out;
1122 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1123 /* Copy ocfs2_xattr_value */
1124 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1125 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1126 &bh, NULL);
1127 if (ret) {
1128 mlog_errno(ret);
1129 goto out;
1132 cplen = len >= blocksize ? blocksize : len;
1133 memcpy(buffer, bh->b_data, cplen);
1134 len -= cplen;
1135 buffer += cplen;
1137 brelse(bh);
1138 bh = NULL;
1139 if (len == 0)
1140 break;
1142 cpos += num_clusters;
1144 out:
1145 return ret;
1148 static int ocfs2_xattr_ibody_get(struct inode *inode,
1149 int name_index,
1150 const char *name,
1151 void *buffer,
1152 size_t buffer_size,
1153 struct ocfs2_xattr_search *xs)
1155 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1156 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1157 struct ocfs2_xattr_value_root *xv;
1158 size_t size;
1159 int ret = 0;
1161 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
1162 return -ENODATA;
1164 xs->end = (void *)di + inode->i_sb->s_blocksize;
1165 xs->header = (struct ocfs2_xattr_header *)
1166 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1167 xs->base = (void *)xs->header;
1168 xs->here = xs->header->xh_entries;
1170 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1171 if (ret)
1172 return ret;
1173 size = le64_to_cpu(xs->here->xe_value_size);
1174 if (buffer) {
1175 if (size > buffer_size)
1176 return -ERANGE;
1177 if (ocfs2_xattr_is_local(xs->here)) {
1178 memcpy(buffer, (void *)xs->base +
1179 le16_to_cpu(xs->here->xe_name_offset) +
1180 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
1181 } else {
1182 xv = (struct ocfs2_xattr_value_root *)
1183 (xs->base + le16_to_cpu(
1184 xs->here->xe_name_offset) +
1185 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
1186 ret = ocfs2_xattr_get_value_outside(inode, xv,
1187 buffer, size);
1188 if (ret < 0) {
1189 mlog_errno(ret);
1190 return ret;
1195 return size;
1198 static int ocfs2_xattr_block_get(struct inode *inode,
1199 int name_index,
1200 const char *name,
1201 void *buffer,
1202 size_t buffer_size,
1203 struct ocfs2_xattr_search *xs)
1205 struct ocfs2_xattr_block *xb;
1206 struct ocfs2_xattr_value_root *xv;
1207 size_t size;
1208 int ret = -ENODATA, name_offset, name_len, i;
1209 int uninitialized_var(block_off);
1211 xs->bucket = ocfs2_xattr_bucket_new(inode);
1212 if (!xs->bucket) {
1213 ret = -ENOMEM;
1214 mlog_errno(ret);
1215 goto cleanup;
1218 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
1219 if (ret) {
1220 mlog_errno(ret);
1221 goto cleanup;
1224 if (xs->not_found) {
1225 ret = -ENODATA;
1226 goto cleanup;
1229 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1230 size = le64_to_cpu(xs->here->xe_value_size);
1231 if (buffer) {
1232 ret = -ERANGE;
1233 if (size > buffer_size)
1234 goto cleanup;
1236 name_offset = le16_to_cpu(xs->here->xe_name_offset);
1237 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
1238 i = xs->here - xs->header->xh_entries;
1240 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
1241 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
1242 bucket_xh(xs->bucket),
1244 &block_off,
1245 &name_offset);
1246 xs->base = bucket_block(xs->bucket, block_off);
1248 if (ocfs2_xattr_is_local(xs->here)) {
1249 memcpy(buffer, (void *)xs->base +
1250 name_offset + name_len, size);
1251 } else {
1252 xv = (struct ocfs2_xattr_value_root *)
1253 (xs->base + name_offset + name_len);
1254 ret = ocfs2_xattr_get_value_outside(inode, xv,
1255 buffer, size);
1256 if (ret < 0) {
1257 mlog_errno(ret);
1258 goto cleanup;
1262 ret = size;
1263 cleanup:
1264 ocfs2_xattr_bucket_free(xs->bucket);
1266 brelse(xs->xattr_bh);
1267 xs->xattr_bh = NULL;
1268 return ret;
1271 int ocfs2_xattr_get_nolock(struct inode *inode,
1272 struct buffer_head *di_bh,
1273 int name_index,
1274 const char *name,
1275 void *buffer,
1276 size_t buffer_size)
1278 int ret;
1279 struct ocfs2_dinode *di = NULL;
1280 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1281 struct ocfs2_xattr_search xis = {
1282 .not_found = -ENODATA,
1284 struct ocfs2_xattr_search xbs = {
1285 .not_found = -ENODATA,
1288 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1289 return -EOPNOTSUPP;
1291 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1292 ret = -ENODATA;
1294 xis.inode_bh = xbs.inode_bh = di_bh;
1295 di = (struct ocfs2_dinode *)di_bh->b_data;
1297 down_read(&oi->ip_xattr_sem);
1298 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1299 buffer_size, &xis);
1300 if (ret == -ENODATA && di->i_xattr_loc)
1301 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1302 buffer_size, &xbs);
1303 up_read(&oi->ip_xattr_sem);
1305 return ret;
1308 /* ocfs2_xattr_get()
1310 * Copy an extended attribute into the buffer provided.
1311 * Buffer is NULL to compute the size of buffer required.
1313 static int ocfs2_xattr_get(struct inode *inode,
1314 int name_index,
1315 const char *name,
1316 void *buffer,
1317 size_t buffer_size)
1319 int ret;
1320 struct buffer_head *di_bh = NULL;
1322 ret = ocfs2_inode_lock(inode, &di_bh, 0);
1323 if (ret < 0) {
1324 mlog_errno(ret);
1325 return ret;
1327 ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
1328 name, buffer, buffer_size);
1330 ocfs2_inode_unlock(inode, 0);
1332 brelse(di_bh);
1334 return ret;
1337 static int __ocfs2_xattr_set_value_outside(struct inode *inode,
1338 handle_t *handle,
1339 struct ocfs2_xattr_value_buf *vb,
1340 const void *value,
1341 int value_len)
1343 int ret = 0, i, cp_len;
1344 u16 blocksize = inode->i_sb->s_blocksize;
1345 u32 p_cluster, num_clusters;
1346 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1347 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1348 u64 blkno;
1349 struct buffer_head *bh = NULL;
1350 unsigned int ext_flags;
1351 struct ocfs2_xattr_value_root *xv = vb->vb_xv;
1353 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1355 while (cpos < clusters) {
1356 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1357 &num_clusters, &xv->xr_list,
1358 &ext_flags);
1359 if (ret) {
1360 mlog_errno(ret);
1361 goto out;
1364 BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED);
1366 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1368 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1369 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1370 &bh, NULL);
1371 if (ret) {
1372 mlog_errno(ret);
1373 goto out;
1376 ret = ocfs2_journal_access(handle,
1377 INODE_CACHE(inode),
1379 OCFS2_JOURNAL_ACCESS_WRITE);
1380 if (ret < 0) {
1381 mlog_errno(ret);
1382 goto out;
1385 cp_len = value_len > blocksize ? blocksize : value_len;
1386 memcpy(bh->b_data, value, cp_len);
1387 value_len -= cp_len;
1388 value += cp_len;
1389 if (cp_len < blocksize)
1390 memset(bh->b_data + cp_len, 0,
1391 blocksize - cp_len);
1393 ret = ocfs2_journal_dirty(handle, bh);
1394 if (ret < 0) {
1395 mlog_errno(ret);
1396 goto out;
1398 brelse(bh);
1399 bh = NULL;
1402 * XXX: do we need to empty all the following
1403 * blocks in this cluster?
1405 if (!value_len)
1406 break;
1408 cpos += num_clusters;
1410 out:
1411 brelse(bh);
1413 return ret;
1416 static int ocfs2_xa_check_space_helper(int needed_space, int free_start,
1417 int num_entries)
1419 int free_space;
1421 if (!needed_space)
1422 return 0;
1424 free_space = free_start -
1425 sizeof(struct ocfs2_xattr_header) -
1426 (num_entries * sizeof(struct ocfs2_xattr_entry)) -
1427 OCFS2_XATTR_HEADER_GAP;
1428 if (free_space < 0)
1429 return -EIO;
1430 if (free_space < needed_space)
1431 return -ENOSPC;
1433 return 0;
1436 static int ocfs2_xa_journal_access(handle_t *handle, struct ocfs2_xa_loc *loc,
1437 int type)
1439 return loc->xl_ops->xlo_journal_access(handle, loc, type);
1442 static void ocfs2_xa_journal_dirty(handle_t *handle, struct ocfs2_xa_loc *loc)
1444 loc->xl_ops->xlo_journal_dirty(handle, loc);
1447 /* Give a pointer into the storage for the given offset */
1448 static void *ocfs2_xa_offset_pointer(struct ocfs2_xa_loc *loc, int offset)
1450 BUG_ON(offset >= loc->xl_size);
1451 return loc->xl_ops->xlo_offset_pointer(loc, offset);
1455 * Wipe the name+value pair and allow the storage to reclaim it. This
1456 * must be followed by either removal of the entry or a call to
1457 * ocfs2_xa_add_namevalue().
1459 static void ocfs2_xa_wipe_namevalue(struct ocfs2_xa_loc *loc)
1461 loc->xl_ops->xlo_wipe_namevalue(loc);
1465 * Find lowest offset to a name+value pair. This is the start of our
1466 * downward-growing free space.
1468 static int ocfs2_xa_get_free_start(struct ocfs2_xa_loc *loc)
1470 return loc->xl_ops->xlo_get_free_start(loc);
1473 /* Can we reuse loc->xl_entry for xi? */
1474 static int ocfs2_xa_can_reuse_entry(struct ocfs2_xa_loc *loc,
1475 struct ocfs2_xattr_info *xi)
1477 return loc->xl_ops->xlo_can_reuse(loc, xi);
1480 /* How much free space is needed to set the new value */
1481 static int ocfs2_xa_check_space(struct ocfs2_xa_loc *loc,
1482 struct ocfs2_xattr_info *xi)
1484 return loc->xl_ops->xlo_check_space(loc, xi);
1487 static void ocfs2_xa_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1489 loc->xl_ops->xlo_add_entry(loc, name_hash);
1490 loc->xl_entry->xe_name_hash = cpu_to_le32(name_hash);
1492 * We can't leave the new entry's xe_name_offset at zero or
1493 * add_namevalue() will go nuts. We set it to the size of our
1494 * storage so that it can never be less than any other entry.
1496 loc->xl_entry->xe_name_offset = cpu_to_le16(loc->xl_size);
1499 static void ocfs2_xa_add_namevalue(struct ocfs2_xa_loc *loc,
1500 struct ocfs2_xattr_info *xi)
1502 int size = namevalue_size_xi(xi);
1503 int nameval_offset;
1504 char *nameval_buf;
1506 loc->xl_ops->xlo_add_namevalue(loc, size);
1507 loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
1508 loc->xl_entry->xe_name_len = xi->xi_name_len;
1509 ocfs2_xattr_set_type(loc->xl_entry, xi->xi_name_index);
1510 ocfs2_xattr_set_local(loc->xl_entry,
1511 xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE);
1513 nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1514 nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
1515 memset(nameval_buf, 0, size);
1516 memcpy(nameval_buf, xi->xi_name, xi->xi_name_len);
1519 static void ocfs2_xa_fill_value_buf(struct ocfs2_xa_loc *loc,
1520 struct ocfs2_xattr_value_buf *vb)
1522 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1523 int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len);
1525 /* Value bufs are for value trees */
1526 BUG_ON(ocfs2_xattr_is_local(loc->xl_entry));
1527 BUG_ON(namevalue_size_xe(loc->xl_entry) !=
1528 (name_size + OCFS2_XATTR_ROOT_SIZE));
1530 loc->xl_ops->xlo_fill_value_buf(loc, vb);
1531 vb->vb_xv =
1532 (struct ocfs2_xattr_value_root *)ocfs2_xa_offset_pointer(loc,
1533 nameval_offset +
1534 name_size);
1537 static int ocfs2_xa_block_journal_access(handle_t *handle,
1538 struct ocfs2_xa_loc *loc, int type)
1540 struct buffer_head *bh = loc->xl_storage;
1541 ocfs2_journal_access_func access;
1543 if (loc->xl_size == (bh->b_size -
1544 offsetof(struct ocfs2_xattr_block,
1545 xb_attrs.xb_header)))
1546 access = ocfs2_journal_access_xb;
1547 else
1548 access = ocfs2_journal_access_di;
1549 return access(handle, INODE_CACHE(loc->xl_inode), bh, type);
1552 static void ocfs2_xa_block_journal_dirty(handle_t *handle,
1553 struct ocfs2_xa_loc *loc)
1555 struct buffer_head *bh = loc->xl_storage;
1557 ocfs2_journal_dirty(handle, bh);
1560 static void *ocfs2_xa_block_offset_pointer(struct ocfs2_xa_loc *loc,
1561 int offset)
1563 return (char *)loc->xl_header + offset;
1566 static int ocfs2_xa_block_can_reuse(struct ocfs2_xa_loc *loc,
1567 struct ocfs2_xattr_info *xi)
1570 * Block storage is strict. If the sizes aren't exact, we will
1571 * remove the old one and reinsert the new.
1573 return namevalue_size_xe(loc->xl_entry) ==
1574 namevalue_size_xi(xi);
1577 static int ocfs2_xa_block_get_free_start(struct ocfs2_xa_loc *loc)
1579 struct ocfs2_xattr_header *xh = loc->xl_header;
1580 int i, count = le16_to_cpu(xh->xh_count);
1581 int offset, free_start = loc->xl_size;
1583 for (i = 0; i < count; i++) {
1584 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1585 if (offset < free_start)
1586 free_start = offset;
1589 return free_start;
1592 static int ocfs2_xa_block_check_space(struct ocfs2_xa_loc *loc,
1593 struct ocfs2_xattr_info *xi)
1595 int count = le16_to_cpu(loc->xl_header->xh_count);
1596 int free_start = ocfs2_xa_get_free_start(loc);
1597 int needed_space = ocfs2_xi_entry_usage(xi);
1600 * Block storage will reclaim the original entry before inserting
1601 * the new value, so we only need the difference. If the new
1602 * entry is smaller than the old one, we don't need anything.
1604 if (loc->xl_entry) {
1605 /* Don't need space if we're reusing! */
1606 if (ocfs2_xa_can_reuse_entry(loc, xi))
1607 needed_space = 0;
1608 else
1609 needed_space -= ocfs2_xe_entry_usage(loc->xl_entry);
1611 if (needed_space < 0)
1612 needed_space = 0;
1613 return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1617 * Block storage for xattrs keeps the name+value pairs compacted. When
1618 * we remove one, we have to shift any that preceded it towards the end.
1620 static void ocfs2_xa_block_wipe_namevalue(struct ocfs2_xa_loc *loc)
1622 int i, offset;
1623 int namevalue_offset, first_namevalue_offset, namevalue_size;
1624 struct ocfs2_xattr_entry *entry = loc->xl_entry;
1625 struct ocfs2_xattr_header *xh = loc->xl_header;
1626 int count = le16_to_cpu(xh->xh_count);
1628 namevalue_offset = le16_to_cpu(entry->xe_name_offset);
1629 namevalue_size = namevalue_size_xe(entry);
1630 first_namevalue_offset = ocfs2_xa_get_free_start(loc);
1632 /* Shift the name+value pairs */
1633 memmove((char *)xh + first_namevalue_offset + namevalue_size,
1634 (char *)xh + first_namevalue_offset,
1635 namevalue_offset - first_namevalue_offset);
1636 memset((char *)xh + first_namevalue_offset, 0, namevalue_size);
1638 /* Now tell xh->xh_entries about it */
1639 for (i = 0; i < count; i++) {
1640 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1641 if (offset <= namevalue_offset)
1642 le16_add_cpu(&xh->xh_entries[i].xe_name_offset,
1643 namevalue_size);
1647 * Note that we don't update xh_free_start or xh_name_value_len
1648 * because they're not used in block-stored xattrs.
1652 static void ocfs2_xa_block_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1654 int count = le16_to_cpu(loc->xl_header->xh_count);
1655 loc->xl_entry = &(loc->xl_header->xh_entries[count]);
1656 le16_add_cpu(&loc->xl_header->xh_count, 1);
1657 memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1660 static void ocfs2_xa_block_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1662 int free_start = ocfs2_xa_get_free_start(loc);
1664 loc->xl_entry->xe_name_offset = cpu_to_le16(free_start - size);
1667 static void ocfs2_xa_block_fill_value_buf(struct ocfs2_xa_loc *loc,
1668 struct ocfs2_xattr_value_buf *vb)
1670 struct buffer_head *bh = loc->xl_storage;
1672 if (loc->xl_size == (bh->b_size -
1673 offsetof(struct ocfs2_xattr_block,
1674 xb_attrs.xb_header)))
1675 vb->vb_access = ocfs2_journal_access_xb;
1676 else
1677 vb->vb_access = ocfs2_journal_access_di;
1678 vb->vb_bh = bh;
1682 * Operations for xattrs stored in blocks. This includes inline inode
1683 * storage and unindexed ocfs2_xattr_blocks.
1685 static const struct ocfs2_xa_loc_operations ocfs2_xa_block_loc_ops = {
1686 .xlo_journal_access = ocfs2_xa_block_journal_access,
1687 .xlo_journal_dirty = ocfs2_xa_block_journal_dirty,
1688 .xlo_offset_pointer = ocfs2_xa_block_offset_pointer,
1689 .xlo_check_space = ocfs2_xa_block_check_space,
1690 .xlo_can_reuse = ocfs2_xa_block_can_reuse,
1691 .xlo_get_free_start = ocfs2_xa_block_get_free_start,
1692 .xlo_wipe_namevalue = ocfs2_xa_block_wipe_namevalue,
1693 .xlo_add_entry = ocfs2_xa_block_add_entry,
1694 .xlo_add_namevalue = ocfs2_xa_block_add_namevalue,
1695 .xlo_fill_value_buf = ocfs2_xa_block_fill_value_buf,
1698 static int ocfs2_xa_bucket_journal_access(handle_t *handle,
1699 struct ocfs2_xa_loc *loc, int type)
1701 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1703 return ocfs2_xattr_bucket_journal_access(handle, bucket, type);
1706 static void ocfs2_xa_bucket_journal_dirty(handle_t *handle,
1707 struct ocfs2_xa_loc *loc)
1709 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1711 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
1714 static void *ocfs2_xa_bucket_offset_pointer(struct ocfs2_xa_loc *loc,
1715 int offset)
1717 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1718 int block, block_offset;
1720 /* The header is at the front of the bucket */
1721 block = offset >> loc->xl_inode->i_sb->s_blocksize_bits;
1722 block_offset = offset % loc->xl_inode->i_sb->s_blocksize;
1724 return bucket_block(bucket, block) + block_offset;
1727 static int ocfs2_xa_bucket_can_reuse(struct ocfs2_xa_loc *loc,
1728 struct ocfs2_xattr_info *xi)
1730 return namevalue_size_xe(loc->xl_entry) >=
1731 namevalue_size_xi(xi);
1734 static int ocfs2_xa_bucket_get_free_start(struct ocfs2_xa_loc *loc)
1736 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1737 return le16_to_cpu(bucket_xh(bucket)->xh_free_start);
1740 static int ocfs2_bucket_align_free_start(struct super_block *sb,
1741 int free_start, int size)
1744 * We need to make sure that the name+value pair fits within
1745 * one block.
1747 if (((free_start - size) >> sb->s_blocksize_bits) !=
1748 ((free_start - 1) >> sb->s_blocksize_bits))
1749 free_start -= free_start % sb->s_blocksize;
1751 return free_start;
1754 static int ocfs2_xa_bucket_check_space(struct ocfs2_xa_loc *loc,
1755 struct ocfs2_xattr_info *xi)
1757 int rc;
1758 int count = le16_to_cpu(loc->xl_header->xh_count);
1759 int free_start = ocfs2_xa_get_free_start(loc);
1760 int needed_space = ocfs2_xi_entry_usage(xi);
1761 int size = namevalue_size_xi(xi);
1762 struct super_block *sb = loc->xl_inode->i_sb;
1765 * Bucket storage does not reclaim name+value pairs it cannot
1766 * reuse. They live as holes until the bucket fills, and then
1767 * the bucket is defragmented. However, the bucket can reclaim
1768 * the ocfs2_xattr_entry.
1770 if (loc->xl_entry) {
1771 /* Don't need space if we're reusing! */
1772 if (ocfs2_xa_can_reuse_entry(loc, xi))
1773 needed_space = 0;
1774 else
1775 needed_space -= sizeof(struct ocfs2_xattr_entry);
1777 BUG_ON(needed_space < 0);
1779 if (free_start < size) {
1780 if (needed_space)
1781 return -ENOSPC;
1782 } else {
1784 * First we check if it would fit in the first place.
1785 * Below, we align the free start to a block. This may
1786 * slide us below the minimum gap. By checking unaligned
1787 * first, we avoid that error.
1789 rc = ocfs2_xa_check_space_helper(needed_space, free_start,
1790 count);
1791 if (rc)
1792 return rc;
1793 free_start = ocfs2_bucket_align_free_start(sb, free_start,
1794 size);
1796 return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1799 static void ocfs2_xa_bucket_wipe_namevalue(struct ocfs2_xa_loc *loc)
1801 le16_add_cpu(&loc->xl_header->xh_name_value_len,
1802 -namevalue_size_xe(loc->xl_entry));
1805 static void ocfs2_xa_bucket_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1807 struct ocfs2_xattr_header *xh = loc->xl_header;
1808 int count = le16_to_cpu(xh->xh_count);
1809 int low = 0, high = count - 1, tmp;
1810 struct ocfs2_xattr_entry *tmp_xe;
1813 * We keep buckets sorted by name_hash, so we need to find
1814 * our insert place.
1816 while (low <= high && count) {
1817 tmp = (low + high) / 2;
1818 tmp_xe = &xh->xh_entries[tmp];
1820 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
1821 low = tmp + 1;
1822 else if (name_hash < le32_to_cpu(tmp_xe->xe_name_hash))
1823 high = tmp - 1;
1824 else {
1825 low = tmp;
1826 break;
1830 if (low != count)
1831 memmove(&xh->xh_entries[low + 1],
1832 &xh->xh_entries[low],
1833 ((count - low) * sizeof(struct ocfs2_xattr_entry)));
1835 le16_add_cpu(&xh->xh_count, 1);
1836 loc->xl_entry = &xh->xh_entries[low];
1837 memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1840 static void ocfs2_xa_bucket_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1842 int free_start = ocfs2_xa_get_free_start(loc);
1843 struct ocfs2_xattr_header *xh = loc->xl_header;
1844 struct super_block *sb = loc->xl_inode->i_sb;
1845 int nameval_offset;
1847 free_start = ocfs2_bucket_align_free_start(sb, free_start, size);
1848 nameval_offset = free_start - size;
1849 loc->xl_entry->xe_name_offset = cpu_to_le16(nameval_offset);
1850 xh->xh_free_start = cpu_to_le16(nameval_offset);
1851 le16_add_cpu(&xh->xh_name_value_len, size);
1855 static void ocfs2_xa_bucket_fill_value_buf(struct ocfs2_xa_loc *loc,
1856 struct ocfs2_xattr_value_buf *vb)
1858 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1859 struct super_block *sb = loc->xl_inode->i_sb;
1860 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1861 int size = namevalue_size_xe(loc->xl_entry);
1862 int block_offset = nameval_offset >> sb->s_blocksize_bits;
1864 /* Values are not allowed to straddle block boundaries */
1865 BUG_ON(block_offset !=
1866 ((nameval_offset + size - 1) >> sb->s_blocksize_bits));
1867 /* We expect the bucket to be filled in */
1868 BUG_ON(!bucket->bu_bhs[block_offset]);
1870 vb->vb_access = ocfs2_journal_access;
1871 vb->vb_bh = bucket->bu_bhs[block_offset];
1874 /* Operations for xattrs stored in buckets. */
1875 static const struct ocfs2_xa_loc_operations ocfs2_xa_bucket_loc_ops = {
1876 .xlo_journal_access = ocfs2_xa_bucket_journal_access,
1877 .xlo_journal_dirty = ocfs2_xa_bucket_journal_dirty,
1878 .xlo_offset_pointer = ocfs2_xa_bucket_offset_pointer,
1879 .xlo_check_space = ocfs2_xa_bucket_check_space,
1880 .xlo_can_reuse = ocfs2_xa_bucket_can_reuse,
1881 .xlo_get_free_start = ocfs2_xa_bucket_get_free_start,
1882 .xlo_wipe_namevalue = ocfs2_xa_bucket_wipe_namevalue,
1883 .xlo_add_entry = ocfs2_xa_bucket_add_entry,
1884 .xlo_add_namevalue = ocfs2_xa_bucket_add_namevalue,
1885 .xlo_fill_value_buf = ocfs2_xa_bucket_fill_value_buf,
1888 static unsigned int ocfs2_xa_value_clusters(struct ocfs2_xa_loc *loc)
1890 struct ocfs2_xattr_value_buf vb;
1892 if (ocfs2_xattr_is_local(loc->xl_entry))
1893 return 0;
1895 ocfs2_xa_fill_value_buf(loc, &vb);
1896 return le32_to_cpu(vb.vb_xv->xr_clusters);
1899 static int ocfs2_xa_value_truncate(struct ocfs2_xa_loc *loc, u64 bytes,
1900 struct ocfs2_xattr_set_ctxt *ctxt)
1902 int trunc_rc, access_rc;
1903 struct ocfs2_xattr_value_buf vb;
1905 ocfs2_xa_fill_value_buf(loc, &vb);
1906 trunc_rc = ocfs2_xattr_value_truncate(loc->xl_inode, &vb, bytes,
1907 ctxt);
1910 * The caller of ocfs2_xa_value_truncate() has already called
1911 * ocfs2_xa_journal_access on the loc. However, The truncate code
1912 * calls ocfs2_extend_trans(). This may commit the previous
1913 * transaction and open a new one. If this is a bucket, truncate
1914 * could leave only vb->vb_bh set up for journaling. Meanwhile,
1915 * the caller is expecting to dirty the entire bucket. So we must
1916 * reset the journal work. We do this even if truncate has failed,
1917 * as it could have failed after committing the extend.
1919 access_rc = ocfs2_xa_journal_access(ctxt->handle, loc,
1920 OCFS2_JOURNAL_ACCESS_WRITE);
1922 /* Errors in truncate take precedence */
1923 return trunc_rc ? trunc_rc : access_rc;
1926 static void ocfs2_xa_remove_entry(struct ocfs2_xa_loc *loc)
1928 int index, count;
1929 struct ocfs2_xattr_header *xh = loc->xl_header;
1930 struct ocfs2_xattr_entry *entry = loc->xl_entry;
1932 ocfs2_xa_wipe_namevalue(loc);
1933 loc->xl_entry = NULL;
1935 le16_add_cpu(&xh->xh_count, -1);
1936 count = le16_to_cpu(xh->xh_count);
1939 * Only zero out the entry if there are more remaining. This is
1940 * important for an empty bucket, as it keeps track of the
1941 * bucket's hash value. It doesn't hurt empty block storage.
1943 if (count) {
1944 index = ((char *)entry - (char *)&xh->xh_entries) /
1945 sizeof(struct ocfs2_xattr_entry);
1946 memmove(&xh->xh_entries[index], &xh->xh_entries[index + 1],
1947 (count - index) * sizeof(struct ocfs2_xattr_entry));
1948 memset(&xh->xh_entries[count], 0,
1949 sizeof(struct ocfs2_xattr_entry));
1954 * If we have a problem adjusting the size of an external value during
1955 * ocfs2_xa_prepare_entry() or ocfs2_xa_remove(), we may have an xattr
1956 * in an intermediate state. For example, the value may be partially
1957 * truncated.
1959 * If the value tree hasn't changed, the extend/truncate went nowhere.
1960 * We have nothing to do. The caller can treat it as a straight error.
1962 * If the value tree got partially truncated, we now have a corrupted
1963 * extended attribute. We're going to wipe its entry and leak the
1964 * clusters. Better to leak some storage than leave a corrupt entry.
1966 * If the value tree grew, it obviously didn't grow enough for the
1967 * new entry. We're not going to try and reclaim those clusters either.
1968 * If there was already an external value there (orig_clusters != 0),
1969 * the new clusters are attached safely and we can just leave the old
1970 * value in place. If there was no external value there, we remove
1971 * the entry.
1973 * This way, the xattr block we store in the journal will be consistent.
1974 * If the size change broke because of the journal, no changes will hit
1975 * disk anyway.
1977 static void ocfs2_xa_cleanup_value_truncate(struct ocfs2_xa_loc *loc,
1978 const char *what,
1979 unsigned int orig_clusters)
1981 unsigned int new_clusters = ocfs2_xa_value_clusters(loc);
1982 char *nameval_buf = ocfs2_xa_offset_pointer(loc,
1983 le16_to_cpu(loc->xl_entry->xe_name_offset));
1985 if (new_clusters < orig_clusters) {
1986 mlog(ML_ERROR,
1987 "Partial truncate while %s xattr %.*s. Leaking "
1988 "%u clusters and removing the entry\n",
1989 what, loc->xl_entry->xe_name_len, nameval_buf,
1990 orig_clusters - new_clusters);
1991 ocfs2_xa_remove_entry(loc);
1992 } else if (!orig_clusters) {
1993 mlog(ML_ERROR,
1994 "Unable to allocate an external value for xattr "
1995 "%.*s safely. Leaking %u clusters and removing the "
1996 "entry\n",
1997 loc->xl_entry->xe_name_len, nameval_buf,
1998 new_clusters - orig_clusters);
1999 ocfs2_xa_remove_entry(loc);
2000 } else if (new_clusters > orig_clusters)
2001 mlog(ML_ERROR,
2002 "Unable to grow xattr %.*s safely. %u new clusters "
2003 "have been added, but the value will not be "
2004 "modified\n",
2005 loc->xl_entry->xe_name_len, nameval_buf,
2006 new_clusters - orig_clusters);
2009 static int ocfs2_xa_remove(struct ocfs2_xa_loc *loc,
2010 struct ocfs2_xattr_set_ctxt *ctxt)
2012 int rc = 0;
2013 unsigned int orig_clusters;
2015 if (!ocfs2_xattr_is_local(loc->xl_entry)) {
2016 orig_clusters = ocfs2_xa_value_clusters(loc);
2017 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
2018 if (rc) {
2019 mlog_errno(rc);
2021 * Since this is remove, we can return 0 if
2022 * ocfs2_xa_cleanup_value_truncate() is going to
2023 * wipe the entry anyway. So we check the
2024 * cluster count as well.
2026 if (orig_clusters != ocfs2_xa_value_clusters(loc))
2027 rc = 0;
2028 ocfs2_xa_cleanup_value_truncate(loc, "removing",
2029 orig_clusters);
2030 if (rc)
2031 goto out;
2035 ocfs2_xa_remove_entry(loc);
2037 out:
2038 return rc;
2041 static void ocfs2_xa_install_value_root(struct ocfs2_xa_loc *loc)
2043 int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len);
2044 char *nameval_buf;
2046 nameval_buf = ocfs2_xa_offset_pointer(loc,
2047 le16_to_cpu(loc->xl_entry->xe_name_offset));
2048 memcpy(nameval_buf + name_size, &def_xv, OCFS2_XATTR_ROOT_SIZE);
2052 * Take an existing entry and make it ready for the new value. This
2053 * won't allocate space, but it may free space. It should be ready for
2054 * ocfs2_xa_prepare_entry() to finish the work.
2056 static int ocfs2_xa_reuse_entry(struct ocfs2_xa_loc *loc,
2057 struct ocfs2_xattr_info *xi,
2058 struct ocfs2_xattr_set_ctxt *ctxt)
2060 int rc = 0;
2061 int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
2062 unsigned int orig_clusters;
2063 char *nameval_buf;
2064 int xe_local = ocfs2_xattr_is_local(loc->xl_entry);
2065 int xi_local = xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE;
2067 BUG_ON(OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len) !=
2068 name_size);
2070 nameval_buf = ocfs2_xa_offset_pointer(loc,
2071 le16_to_cpu(loc->xl_entry->xe_name_offset));
2072 if (xe_local) {
2073 memset(nameval_buf + name_size, 0,
2074 namevalue_size_xe(loc->xl_entry) - name_size);
2075 if (!xi_local)
2076 ocfs2_xa_install_value_root(loc);
2077 } else {
2078 orig_clusters = ocfs2_xa_value_clusters(loc);
2079 if (xi_local) {
2080 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
2081 if (rc < 0)
2082 mlog_errno(rc);
2083 else
2084 memset(nameval_buf + name_size, 0,
2085 namevalue_size_xe(loc->xl_entry) -
2086 name_size);
2087 } else if (le64_to_cpu(loc->xl_entry->xe_value_size) >
2088 xi->xi_value_len) {
2089 rc = ocfs2_xa_value_truncate(loc, xi->xi_value_len,
2090 ctxt);
2091 if (rc < 0)
2092 mlog_errno(rc);
2095 if (rc) {
2096 ocfs2_xa_cleanup_value_truncate(loc, "reusing",
2097 orig_clusters);
2098 goto out;
2102 loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
2103 ocfs2_xattr_set_local(loc->xl_entry, xi_local);
2105 out:
2106 return rc;
2110 * Prepares loc->xl_entry to receive the new xattr. This includes
2111 * properly setting up the name+value pair region. If loc->xl_entry
2112 * already exists, it will take care of modifying it appropriately.
2114 * Note that this modifies the data. You did journal_access already,
2115 * right?
2117 static int ocfs2_xa_prepare_entry(struct ocfs2_xa_loc *loc,
2118 struct ocfs2_xattr_info *xi,
2119 u32 name_hash,
2120 struct ocfs2_xattr_set_ctxt *ctxt)
2122 int rc = 0;
2123 unsigned int orig_clusters;
2124 __le64 orig_value_size = 0;
2126 rc = ocfs2_xa_check_space(loc, xi);
2127 if (rc)
2128 goto out;
2130 if (loc->xl_entry) {
2131 if (ocfs2_xa_can_reuse_entry(loc, xi)) {
2132 orig_value_size = loc->xl_entry->xe_value_size;
2133 rc = ocfs2_xa_reuse_entry(loc, xi, ctxt);
2134 if (rc)
2135 goto out;
2136 goto alloc_value;
2139 if (!ocfs2_xattr_is_local(loc->xl_entry)) {
2140 orig_clusters = ocfs2_xa_value_clusters(loc);
2141 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
2142 if (rc) {
2143 mlog_errno(rc);
2144 ocfs2_xa_cleanup_value_truncate(loc,
2145 "overwriting",
2146 orig_clusters);
2147 goto out;
2150 ocfs2_xa_wipe_namevalue(loc);
2151 } else
2152 ocfs2_xa_add_entry(loc, name_hash);
2155 * If we get here, we have a blank entry. Fill it. We grow our
2156 * name+value pair back from the end.
2158 ocfs2_xa_add_namevalue(loc, xi);
2159 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
2160 ocfs2_xa_install_value_root(loc);
2162 alloc_value:
2163 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2164 orig_clusters = ocfs2_xa_value_clusters(loc);
2165 rc = ocfs2_xa_value_truncate(loc, xi->xi_value_len, ctxt);
2166 if (rc < 0) {
2168 * If we tried to grow an existing external value,
2169 * ocfs2_xa_cleanuP-value_truncate() is going to
2170 * let it stand. We have to restore its original
2171 * value size.
2173 loc->xl_entry->xe_value_size = orig_value_size;
2174 ocfs2_xa_cleanup_value_truncate(loc, "growing",
2175 orig_clusters);
2176 mlog_errno(rc);
2180 out:
2181 return rc;
2185 * Store the value portion of the name+value pair. This will skip
2186 * values that are stored externally. Their tree roots were set up
2187 * by ocfs2_xa_prepare_entry().
2189 static int ocfs2_xa_store_value(struct ocfs2_xa_loc *loc,
2190 struct ocfs2_xattr_info *xi,
2191 struct ocfs2_xattr_set_ctxt *ctxt)
2193 int rc = 0;
2194 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
2195 int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
2196 char *nameval_buf;
2197 struct ocfs2_xattr_value_buf vb;
2199 nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
2200 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2201 ocfs2_xa_fill_value_buf(loc, &vb);
2202 rc = __ocfs2_xattr_set_value_outside(loc->xl_inode,
2203 ctxt->handle, &vb,
2204 xi->xi_value,
2205 xi->xi_value_len);
2206 } else
2207 memcpy(nameval_buf + name_size, xi->xi_value, xi->xi_value_len);
2209 return rc;
2212 static int ocfs2_xa_set(struct ocfs2_xa_loc *loc,
2213 struct ocfs2_xattr_info *xi,
2214 struct ocfs2_xattr_set_ctxt *ctxt)
2216 int ret;
2217 u32 name_hash = ocfs2_xattr_name_hash(loc->xl_inode, xi->xi_name,
2218 xi->xi_name_len);
2220 ret = ocfs2_xa_journal_access(ctxt->handle, loc,
2221 OCFS2_JOURNAL_ACCESS_WRITE);
2222 if (ret) {
2223 mlog_errno(ret);
2224 goto out;
2228 * From here on out, everything is going to modify the buffer a
2229 * little. Errors are going to leave the xattr header in a
2230 * sane state. Thus, even with errors we dirty the sucker.
2233 /* Don't worry, we are never called with !xi_value and !xl_entry */
2234 if (!xi->xi_value) {
2235 ret = ocfs2_xa_remove(loc, ctxt);
2236 goto out_dirty;
2239 ret = ocfs2_xa_prepare_entry(loc, xi, name_hash, ctxt);
2240 if (ret) {
2241 if (ret != -ENOSPC)
2242 mlog_errno(ret);
2243 goto out_dirty;
2246 ret = ocfs2_xa_store_value(loc, xi, ctxt);
2247 if (ret)
2248 mlog_errno(ret);
2250 out_dirty:
2251 ocfs2_xa_journal_dirty(ctxt->handle, loc);
2253 out:
2254 return ret;
2257 static void ocfs2_init_dinode_xa_loc(struct ocfs2_xa_loc *loc,
2258 struct inode *inode,
2259 struct buffer_head *bh,
2260 struct ocfs2_xattr_entry *entry)
2262 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
2264 BUG_ON(!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_XATTR_FL));
2266 loc->xl_inode = inode;
2267 loc->xl_ops = &ocfs2_xa_block_loc_ops;
2268 loc->xl_storage = bh;
2269 loc->xl_entry = entry;
2270 loc->xl_size = le16_to_cpu(di->i_xattr_inline_size);
2271 loc->xl_header =
2272 (struct ocfs2_xattr_header *)(bh->b_data + bh->b_size -
2273 loc->xl_size);
2276 static void ocfs2_init_xattr_block_xa_loc(struct ocfs2_xa_loc *loc,
2277 struct inode *inode,
2278 struct buffer_head *bh,
2279 struct ocfs2_xattr_entry *entry)
2281 struct ocfs2_xattr_block *xb =
2282 (struct ocfs2_xattr_block *)bh->b_data;
2284 BUG_ON(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED);
2286 loc->xl_inode = inode;
2287 loc->xl_ops = &ocfs2_xa_block_loc_ops;
2288 loc->xl_storage = bh;
2289 loc->xl_header = &(xb->xb_attrs.xb_header);
2290 loc->xl_entry = entry;
2291 loc->xl_size = bh->b_size - offsetof(struct ocfs2_xattr_block,
2292 xb_attrs.xb_header);
2295 static void ocfs2_init_xattr_bucket_xa_loc(struct ocfs2_xa_loc *loc,
2296 struct ocfs2_xattr_bucket *bucket,
2297 struct ocfs2_xattr_entry *entry)
2299 loc->xl_inode = bucket->bu_inode;
2300 loc->xl_ops = &ocfs2_xa_bucket_loc_ops;
2301 loc->xl_storage = bucket;
2302 loc->xl_header = bucket_xh(bucket);
2303 loc->xl_entry = entry;
2304 loc->xl_size = OCFS2_XATTR_BUCKET_SIZE;
2308 * In xattr remove, if it is stored outside and refcounted, we may have
2309 * the chance to split the refcount tree. So need the allocators.
2311 static int ocfs2_lock_xattr_remove_allocators(struct inode *inode,
2312 struct ocfs2_xattr_value_root *xv,
2313 struct ocfs2_caching_info *ref_ci,
2314 struct buffer_head *ref_root_bh,
2315 struct ocfs2_alloc_context **meta_ac,
2316 int *ref_credits)
2318 int ret, meta_add = 0;
2319 u32 p_cluster, num_clusters;
2320 unsigned int ext_flags;
2322 *ref_credits = 0;
2323 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
2324 &num_clusters,
2325 &xv->xr_list,
2326 &ext_flags);
2327 if (ret) {
2328 mlog_errno(ret);
2329 goto out;
2332 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
2333 goto out;
2335 ret = ocfs2_refcounted_xattr_delete_need(inode, ref_ci,
2336 ref_root_bh, xv,
2337 &meta_add, ref_credits);
2338 if (ret) {
2339 mlog_errno(ret);
2340 goto out;
2343 ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
2344 meta_add, meta_ac);
2345 if (ret)
2346 mlog_errno(ret);
2348 out:
2349 return ret;
2352 static int ocfs2_remove_value_outside(struct inode*inode,
2353 struct ocfs2_xattr_value_buf *vb,
2354 struct ocfs2_xattr_header *header,
2355 struct ocfs2_caching_info *ref_ci,
2356 struct buffer_head *ref_root_bh)
2358 int ret = 0, i, ref_credits;
2359 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2360 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
2361 void *val;
2363 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
2365 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
2366 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
2368 if (ocfs2_xattr_is_local(entry))
2369 continue;
2371 val = (void *)header +
2372 le16_to_cpu(entry->xe_name_offset);
2373 vb->vb_xv = (struct ocfs2_xattr_value_root *)
2374 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
2376 ret = ocfs2_lock_xattr_remove_allocators(inode, vb->vb_xv,
2377 ref_ci, ref_root_bh,
2378 &ctxt.meta_ac,
2379 &ref_credits);
2381 ctxt.handle = ocfs2_start_trans(osb, ref_credits +
2382 ocfs2_remove_extent_credits(osb->sb));
2383 if (IS_ERR(ctxt.handle)) {
2384 ret = PTR_ERR(ctxt.handle);
2385 mlog_errno(ret);
2386 break;
2389 ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt);
2390 if (ret < 0) {
2391 mlog_errno(ret);
2392 break;
2395 ocfs2_commit_trans(osb, ctxt.handle);
2396 if (ctxt.meta_ac) {
2397 ocfs2_free_alloc_context(ctxt.meta_ac);
2398 ctxt.meta_ac = NULL;
2402 if (ctxt.meta_ac)
2403 ocfs2_free_alloc_context(ctxt.meta_ac);
2404 ocfs2_schedule_truncate_log_flush(osb, 1);
2405 ocfs2_run_deallocs(osb, &ctxt.dealloc);
2406 return ret;
2409 static int ocfs2_xattr_ibody_remove(struct inode *inode,
2410 struct buffer_head *di_bh,
2411 struct ocfs2_caching_info *ref_ci,
2412 struct buffer_head *ref_root_bh)
2415 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2416 struct ocfs2_xattr_header *header;
2417 int ret;
2418 struct ocfs2_xattr_value_buf vb = {
2419 .vb_bh = di_bh,
2420 .vb_access = ocfs2_journal_access_di,
2423 header = (struct ocfs2_xattr_header *)
2424 ((void *)di + inode->i_sb->s_blocksize -
2425 le16_to_cpu(di->i_xattr_inline_size));
2427 ret = ocfs2_remove_value_outside(inode, &vb, header,
2428 ref_ci, ref_root_bh);
2430 return ret;
2433 struct ocfs2_rm_xattr_bucket_para {
2434 struct ocfs2_caching_info *ref_ci;
2435 struct buffer_head *ref_root_bh;
2438 static int ocfs2_xattr_block_remove(struct inode *inode,
2439 struct buffer_head *blk_bh,
2440 struct ocfs2_caching_info *ref_ci,
2441 struct buffer_head *ref_root_bh)
2443 struct ocfs2_xattr_block *xb;
2444 int ret = 0;
2445 struct ocfs2_xattr_value_buf vb = {
2446 .vb_bh = blk_bh,
2447 .vb_access = ocfs2_journal_access_xb,
2449 struct ocfs2_rm_xattr_bucket_para args = {
2450 .ref_ci = ref_ci,
2451 .ref_root_bh = ref_root_bh,
2454 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2455 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2456 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
2457 ret = ocfs2_remove_value_outside(inode, &vb, header,
2458 ref_ci, ref_root_bh);
2459 } else
2460 ret = ocfs2_iterate_xattr_index_block(inode,
2461 blk_bh,
2462 ocfs2_rm_xattr_cluster,
2463 &args);
2465 return ret;
2468 static int ocfs2_xattr_free_block(struct inode *inode,
2469 u64 block,
2470 struct ocfs2_caching_info *ref_ci,
2471 struct buffer_head *ref_root_bh)
2473 struct inode *xb_alloc_inode;
2474 struct buffer_head *xb_alloc_bh = NULL;
2475 struct buffer_head *blk_bh = NULL;
2476 struct ocfs2_xattr_block *xb;
2477 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2478 handle_t *handle;
2479 int ret = 0;
2480 u64 blk, bg_blkno;
2481 u16 bit;
2483 ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
2484 if (ret < 0) {
2485 mlog_errno(ret);
2486 goto out;
2489 ret = ocfs2_xattr_block_remove(inode, blk_bh, ref_ci, ref_root_bh);
2490 if (ret < 0) {
2491 mlog_errno(ret);
2492 goto out;
2495 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2496 blk = le64_to_cpu(xb->xb_blkno);
2497 bit = le16_to_cpu(xb->xb_suballoc_bit);
2498 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2500 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
2501 EXTENT_ALLOC_SYSTEM_INODE,
2502 le16_to_cpu(xb->xb_suballoc_slot));
2503 if (!xb_alloc_inode) {
2504 ret = -ENOMEM;
2505 mlog_errno(ret);
2506 goto out;
2508 mutex_lock(&xb_alloc_inode->i_mutex);
2510 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
2511 if (ret < 0) {
2512 mlog_errno(ret);
2513 goto out_mutex;
2516 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
2517 if (IS_ERR(handle)) {
2518 ret = PTR_ERR(handle);
2519 mlog_errno(ret);
2520 goto out_unlock;
2523 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
2524 bit, bg_blkno, 1);
2525 if (ret < 0)
2526 mlog_errno(ret);
2528 ocfs2_commit_trans(osb, handle);
2529 out_unlock:
2530 ocfs2_inode_unlock(xb_alloc_inode, 1);
2531 brelse(xb_alloc_bh);
2532 out_mutex:
2533 mutex_unlock(&xb_alloc_inode->i_mutex);
2534 iput(xb_alloc_inode);
2535 out:
2536 brelse(blk_bh);
2537 return ret;
2541 * ocfs2_xattr_remove()
2543 * Free extended attribute resources associated with this inode.
2545 int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
2547 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2548 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2549 struct ocfs2_refcount_tree *ref_tree = NULL;
2550 struct buffer_head *ref_root_bh = NULL;
2551 struct ocfs2_caching_info *ref_ci = NULL;
2552 handle_t *handle;
2553 int ret;
2555 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2556 return 0;
2558 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
2559 return 0;
2561 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) {
2562 ret = ocfs2_lock_refcount_tree(OCFS2_SB(inode->i_sb),
2563 le64_to_cpu(di->i_refcount_loc),
2564 1, &ref_tree, &ref_root_bh);
2565 if (ret) {
2566 mlog_errno(ret);
2567 goto out;
2569 ref_ci = &ref_tree->rf_ci;
2573 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2574 ret = ocfs2_xattr_ibody_remove(inode, di_bh,
2575 ref_ci, ref_root_bh);
2576 if (ret < 0) {
2577 mlog_errno(ret);
2578 goto out;
2582 if (di->i_xattr_loc) {
2583 ret = ocfs2_xattr_free_block(inode,
2584 le64_to_cpu(di->i_xattr_loc),
2585 ref_ci, ref_root_bh);
2586 if (ret < 0) {
2587 mlog_errno(ret);
2588 goto out;
2592 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
2593 OCFS2_INODE_UPDATE_CREDITS);
2594 if (IS_ERR(handle)) {
2595 ret = PTR_ERR(handle);
2596 mlog_errno(ret);
2597 goto out;
2599 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
2600 OCFS2_JOURNAL_ACCESS_WRITE);
2601 if (ret) {
2602 mlog_errno(ret);
2603 goto out_commit;
2606 di->i_xattr_loc = 0;
2608 spin_lock(&oi->ip_lock);
2609 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
2610 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2611 spin_unlock(&oi->ip_lock);
2613 ret = ocfs2_journal_dirty(handle, di_bh);
2614 if (ret < 0)
2615 mlog_errno(ret);
2616 out_commit:
2617 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
2618 out:
2619 if (ref_tree)
2620 ocfs2_unlock_refcount_tree(OCFS2_SB(inode->i_sb), ref_tree, 1);
2621 brelse(ref_root_bh);
2622 return ret;
2625 static int ocfs2_xattr_has_space_inline(struct inode *inode,
2626 struct ocfs2_dinode *di)
2628 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2629 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
2630 int free;
2632 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
2633 return 0;
2635 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2636 struct ocfs2_inline_data *idata = &di->id2.i_data;
2637 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
2638 } else if (ocfs2_inode_is_fast_symlink(inode)) {
2639 free = ocfs2_fast_symlink_chars(inode->i_sb) -
2640 le64_to_cpu(di->i_size);
2641 } else {
2642 struct ocfs2_extent_list *el = &di->id2.i_list;
2643 free = (le16_to_cpu(el->l_count) -
2644 le16_to_cpu(el->l_next_free_rec)) *
2645 sizeof(struct ocfs2_extent_rec);
2647 if (free >= xattrsize)
2648 return 1;
2650 return 0;
2654 * ocfs2_xattr_ibody_find()
2656 * Find extended attribute in inode block and
2657 * fill search info into struct ocfs2_xattr_search.
2659 static int ocfs2_xattr_ibody_find(struct inode *inode,
2660 int name_index,
2661 const char *name,
2662 struct ocfs2_xattr_search *xs)
2664 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2665 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2666 int ret;
2667 int has_space = 0;
2669 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2670 return 0;
2672 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2673 down_read(&oi->ip_alloc_sem);
2674 has_space = ocfs2_xattr_has_space_inline(inode, di);
2675 up_read(&oi->ip_alloc_sem);
2676 if (!has_space)
2677 return 0;
2680 xs->xattr_bh = xs->inode_bh;
2681 xs->end = (void *)di + inode->i_sb->s_blocksize;
2682 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
2683 xs->header = (struct ocfs2_xattr_header *)
2684 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
2685 else
2686 xs->header = (struct ocfs2_xattr_header *)
2687 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
2688 xs->base = (void *)xs->header;
2689 xs->here = xs->header->xh_entries;
2691 /* Find the named attribute. */
2692 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2693 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2694 if (ret && ret != -ENODATA)
2695 return ret;
2696 xs->not_found = ret;
2699 return 0;
2702 static int ocfs2_xattr_ibody_init(struct inode *inode,
2703 struct buffer_head *di_bh,
2704 struct ocfs2_xattr_set_ctxt *ctxt)
2706 int ret;
2707 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2708 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2709 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2710 unsigned int xattrsize = osb->s_xattr_inline_size;
2712 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2713 ret = -ENOSPC;
2714 goto out;
2717 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode), di_bh,
2718 OCFS2_JOURNAL_ACCESS_WRITE);
2719 if (ret) {
2720 mlog_errno(ret);
2721 goto out;
2725 * Adjust extent record count or inline data size
2726 * to reserve space for extended attribute.
2728 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2729 struct ocfs2_inline_data *idata = &di->id2.i_data;
2730 le16_add_cpu(&idata->id_count, -xattrsize);
2731 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
2732 struct ocfs2_extent_list *el = &di->id2.i_list;
2733 le16_add_cpu(&el->l_count, -(xattrsize /
2734 sizeof(struct ocfs2_extent_rec)));
2736 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
2738 spin_lock(&oi->ip_lock);
2739 oi->ip_dyn_features |= OCFS2_INLINE_XATTR_FL|OCFS2_HAS_XATTR_FL;
2740 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2741 spin_unlock(&oi->ip_lock);
2743 ret = ocfs2_journal_dirty(ctxt->handle, di_bh);
2744 if (ret < 0)
2745 mlog_errno(ret);
2747 out:
2748 return ret;
2752 * ocfs2_xattr_ibody_set()
2754 * Set, replace or remove an extended attribute into inode block.
2757 static int ocfs2_xattr_ibody_set(struct inode *inode,
2758 struct ocfs2_xattr_info *xi,
2759 struct ocfs2_xattr_search *xs,
2760 struct ocfs2_xattr_set_ctxt *ctxt)
2762 int ret;
2763 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2764 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2765 struct ocfs2_xa_loc loc;
2767 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2768 return -ENOSPC;
2770 down_write(&oi->ip_alloc_sem);
2771 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2772 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2773 ret = -ENOSPC;
2774 goto out;
2778 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2779 ret = ocfs2_xattr_ibody_init(inode, xs->inode_bh, ctxt);
2780 if (ret) {
2781 if (ret != -ENOSPC)
2782 mlog_errno(ret);
2783 goto out;
2787 ocfs2_init_dinode_xa_loc(&loc, inode, xs->inode_bh,
2788 xs->not_found ? NULL : xs->here);
2789 ret = ocfs2_xa_set(&loc, xi, ctxt);
2790 if (ret) {
2791 if (ret != -ENOSPC)
2792 mlog_errno(ret);
2793 goto out;
2795 xs->here = loc.xl_entry;
2797 out:
2798 up_write(&oi->ip_alloc_sem);
2800 return ret;
2804 * ocfs2_xattr_block_find()
2806 * Find extended attribute in external block and
2807 * fill search info into struct ocfs2_xattr_search.
2809 static int ocfs2_xattr_block_find(struct inode *inode,
2810 int name_index,
2811 const char *name,
2812 struct ocfs2_xattr_search *xs)
2814 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2815 struct buffer_head *blk_bh = NULL;
2816 struct ocfs2_xattr_block *xb;
2817 int ret = 0;
2819 if (!di->i_xattr_loc)
2820 return ret;
2822 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
2823 &blk_bh);
2824 if (ret < 0) {
2825 mlog_errno(ret);
2826 return ret;
2829 xs->xattr_bh = blk_bh;
2830 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2832 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2833 xs->header = &xb->xb_attrs.xb_header;
2834 xs->base = (void *)xs->header;
2835 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
2836 xs->here = xs->header->xh_entries;
2838 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2839 } else
2840 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
2841 name_index,
2842 name, xs);
2844 if (ret && ret != -ENODATA) {
2845 xs->xattr_bh = NULL;
2846 goto cleanup;
2848 xs->not_found = ret;
2849 return 0;
2850 cleanup:
2851 brelse(blk_bh);
2853 return ret;
2856 static int ocfs2_create_xattr_block(struct inode *inode,
2857 struct buffer_head *inode_bh,
2858 struct ocfs2_xattr_set_ctxt *ctxt,
2859 int indexed,
2860 struct buffer_head **ret_bh)
2862 int ret;
2863 u16 suballoc_bit_start;
2864 u32 num_got;
2865 u64 first_blkno;
2866 struct ocfs2_dinode *di = (struct ocfs2_dinode *)inode_bh->b_data;
2867 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2868 struct buffer_head *new_bh = NULL;
2869 struct ocfs2_xattr_block *xblk;
2871 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
2872 inode_bh, OCFS2_JOURNAL_ACCESS_CREATE);
2873 if (ret < 0) {
2874 mlog_errno(ret);
2875 goto end;
2878 ret = ocfs2_claim_metadata(osb, ctxt->handle, ctxt->meta_ac, 1,
2879 &suballoc_bit_start, &num_got,
2880 &first_blkno);
2881 if (ret < 0) {
2882 mlog_errno(ret);
2883 goto end;
2886 new_bh = sb_getblk(inode->i_sb, first_blkno);
2887 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
2889 ret = ocfs2_journal_access_xb(ctxt->handle, INODE_CACHE(inode),
2890 new_bh,
2891 OCFS2_JOURNAL_ACCESS_CREATE);
2892 if (ret < 0) {
2893 mlog_errno(ret);
2894 goto end;
2897 /* Initialize ocfs2_xattr_block */
2898 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2899 memset(xblk, 0, inode->i_sb->s_blocksize);
2900 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
2901 xblk->xb_suballoc_slot = cpu_to_le16(ctxt->meta_ac->ac_alloc_slot);
2902 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
2903 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
2904 xblk->xb_blkno = cpu_to_le64(first_blkno);
2905 if (indexed) {
2906 struct ocfs2_xattr_tree_root *xr = &xblk->xb_attrs.xb_root;
2907 xr->xt_clusters = cpu_to_le32(1);
2908 xr->xt_last_eb_blk = 0;
2909 xr->xt_list.l_tree_depth = 0;
2910 xr->xt_list.l_count = cpu_to_le16(
2911 ocfs2_xattr_recs_per_xb(inode->i_sb));
2912 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2913 xblk->xb_flags = cpu_to_le16(OCFS2_XATTR_INDEXED);
2915 ocfs2_journal_dirty(ctxt->handle, new_bh);
2917 /* Add it to the inode */
2918 di->i_xattr_loc = cpu_to_le64(first_blkno);
2920 spin_lock(&OCFS2_I(inode)->ip_lock);
2921 OCFS2_I(inode)->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
2922 di->i_dyn_features = cpu_to_le16(OCFS2_I(inode)->ip_dyn_features);
2923 spin_unlock(&OCFS2_I(inode)->ip_lock);
2925 ocfs2_journal_dirty(ctxt->handle, inode_bh);
2927 *ret_bh = new_bh;
2928 new_bh = NULL;
2930 end:
2931 brelse(new_bh);
2932 return ret;
2936 * ocfs2_xattr_block_set()
2938 * Set, replace or remove an extended attribute into external block.
2941 static int ocfs2_xattr_block_set(struct inode *inode,
2942 struct ocfs2_xattr_info *xi,
2943 struct ocfs2_xattr_search *xs,
2944 struct ocfs2_xattr_set_ctxt *ctxt)
2946 struct buffer_head *new_bh = NULL;
2947 struct ocfs2_xattr_block *xblk = NULL;
2948 int ret;
2949 struct ocfs2_xa_loc loc;
2951 if (!xs->xattr_bh) {
2952 ret = ocfs2_create_xattr_block(inode, xs->inode_bh, ctxt,
2953 0, &new_bh);
2954 if (ret) {
2955 mlog_errno(ret);
2956 goto end;
2959 xs->xattr_bh = new_bh;
2960 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2961 xs->header = &xblk->xb_attrs.xb_header;
2962 xs->base = (void *)xs->header;
2963 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2964 xs->here = xs->header->xh_entries;
2965 } else
2966 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2968 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
2969 ocfs2_init_xattr_block_xa_loc(&loc, inode, xs->xattr_bh,
2970 xs->not_found ? NULL : xs->here);
2972 ret = ocfs2_xa_set(&loc, xi, ctxt);
2973 if (!ret)
2974 xs->here = loc.xl_entry;
2975 else if (ret != -ENOSPC)
2976 goto end;
2977 else {
2978 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
2979 if (ret)
2980 goto end;
2984 if (le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)
2985 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
2987 end:
2988 return ret;
2991 /* Check whether the new xattr can be inserted into the inode. */
2992 static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
2993 struct ocfs2_xattr_info *xi,
2994 struct ocfs2_xattr_search *xs)
2996 struct ocfs2_xattr_entry *last;
2997 int free, i;
2998 size_t min_offs = xs->end - xs->base;
3000 if (!xs->header)
3001 return 0;
3003 last = xs->header->xh_entries;
3005 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
3006 size_t offs = le16_to_cpu(last->xe_name_offset);
3007 if (offs < min_offs)
3008 min_offs = offs;
3009 last += 1;
3012 free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
3013 if (free < 0)
3014 return 0;
3016 BUG_ON(!xs->not_found);
3018 if (free >= (sizeof(struct ocfs2_xattr_entry) + namevalue_size_xi(xi)))
3019 return 1;
3021 return 0;
3024 static int ocfs2_calc_xattr_set_need(struct inode *inode,
3025 struct ocfs2_dinode *di,
3026 struct ocfs2_xattr_info *xi,
3027 struct ocfs2_xattr_search *xis,
3028 struct ocfs2_xattr_search *xbs,
3029 int *clusters_need,
3030 int *meta_need,
3031 int *credits_need)
3033 int ret = 0, old_in_xb = 0;
3034 int clusters_add = 0, meta_add = 0, credits = 0;
3035 struct buffer_head *bh = NULL;
3036 struct ocfs2_xattr_block *xb = NULL;
3037 struct ocfs2_xattr_entry *xe = NULL;
3038 struct ocfs2_xattr_value_root *xv = NULL;
3039 char *base = NULL;
3040 int name_offset, name_len = 0;
3041 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
3042 xi->xi_value_len);
3043 u64 value_size;
3046 * Calculate the clusters we need to write.
3047 * No matter whether we replace an old one or add a new one,
3048 * we need this for writing.
3050 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
3051 credits += new_clusters *
3052 ocfs2_clusters_to_blocks(inode->i_sb, 1);
3054 if (xis->not_found && xbs->not_found) {
3055 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3057 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
3058 clusters_add += new_clusters;
3059 credits += ocfs2_calc_extend_credits(inode->i_sb,
3060 &def_xv.xv.xr_list,
3061 new_clusters);
3064 goto meta_guess;
3067 if (!xis->not_found) {
3068 xe = xis->here;
3069 name_offset = le16_to_cpu(xe->xe_name_offset);
3070 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3071 base = xis->base;
3072 credits += OCFS2_INODE_UPDATE_CREDITS;
3073 } else {
3074 int i, block_off = 0;
3075 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
3076 xe = xbs->here;
3077 name_offset = le16_to_cpu(xe->xe_name_offset);
3078 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3079 i = xbs->here - xbs->header->xh_entries;
3080 old_in_xb = 1;
3082 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
3083 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
3084 bucket_xh(xbs->bucket),
3085 i, &block_off,
3086 &name_offset);
3087 base = bucket_block(xbs->bucket, block_off);
3088 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3089 } else {
3090 base = xbs->base;
3091 credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
3096 * delete a xattr doesn't need metadata and cluster allocation.
3097 * so just calculate the credits and return.
3099 * The credits for removing the value tree will be extended
3100 * by ocfs2_remove_extent itself.
3102 if (!xi->xi_value) {
3103 if (!ocfs2_xattr_is_local(xe))
3104 credits += ocfs2_remove_extent_credits(inode->i_sb);
3106 goto out;
3109 /* do cluster allocation guess first. */
3110 value_size = le64_to_cpu(xe->xe_value_size);
3112 if (old_in_xb) {
3114 * In xattr set, we always try to set the xe in inode first,
3115 * so if it can be inserted into inode successfully, the old
3116 * one will be removed from the xattr block, and this xattr
3117 * will be inserted into inode as a new xattr in inode.
3119 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
3120 clusters_add += new_clusters;
3121 credits += ocfs2_remove_extent_credits(inode->i_sb) +
3122 OCFS2_INODE_UPDATE_CREDITS;
3123 if (!ocfs2_xattr_is_local(xe))
3124 credits += ocfs2_calc_extend_credits(
3125 inode->i_sb,
3126 &def_xv.xv.xr_list,
3127 new_clusters);
3128 goto out;
3132 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
3133 /* the new values will be stored outside. */
3134 u32 old_clusters = 0;
3136 if (!ocfs2_xattr_is_local(xe)) {
3137 old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
3138 value_size);
3139 xv = (struct ocfs2_xattr_value_root *)
3140 (base + name_offset + name_len);
3141 value_size = OCFS2_XATTR_ROOT_SIZE;
3142 } else
3143 xv = &def_xv.xv;
3145 if (old_clusters >= new_clusters) {
3146 credits += ocfs2_remove_extent_credits(inode->i_sb);
3147 goto out;
3148 } else {
3149 meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
3150 clusters_add += new_clusters - old_clusters;
3151 credits += ocfs2_calc_extend_credits(inode->i_sb,
3152 &xv->xr_list,
3153 new_clusters -
3154 old_clusters);
3155 if (value_size >= OCFS2_XATTR_ROOT_SIZE)
3156 goto out;
3158 } else {
3160 * Now the new value will be stored inside. So if the new
3161 * value is smaller than the size of value root or the old
3162 * value, we don't need any allocation, otherwise we have
3163 * to guess metadata allocation.
3165 if ((ocfs2_xattr_is_local(xe) &&
3166 (value_size >= xi->xi_value_len)) ||
3167 (!ocfs2_xattr_is_local(xe) &&
3168 OCFS2_XATTR_ROOT_SIZE >= xi->xi_value_len))
3169 goto out;
3172 meta_guess:
3173 /* calculate metadata allocation. */
3174 if (di->i_xattr_loc) {
3175 if (!xbs->xattr_bh) {
3176 ret = ocfs2_read_xattr_block(inode,
3177 le64_to_cpu(di->i_xattr_loc),
3178 &bh);
3179 if (ret) {
3180 mlog_errno(ret);
3181 goto out;
3184 xb = (struct ocfs2_xattr_block *)bh->b_data;
3185 } else
3186 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
3189 * If there is already an xattr tree, good, we can calculate
3190 * like other b-trees. Otherwise we may have the chance of
3191 * create a tree, the credit calculation is borrowed from
3192 * ocfs2_calc_extend_credits with root_el = NULL. And the
3193 * new tree will be cluster based, so no meta is needed.
3195 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
3196 struct ocfs2_extent_list *el =
3197 &xb->xb_attrs.xb_root.xt_list;
3198 meta_add += ocfs2_extend_meta_needed(el);
3199 credits += ocfs2_calc_extend_credits(inode->i_sb,
3200 el, 1);
3201 } else
3202 credits += OCFS2_SUBALLOC_ALLOC + 1;
3205 * This cluster will be used either for new bucket or for
3206 * new xattr block.
3207 * If the cluster size is the same as the bucket size, one
3208 * more is needed since we may need to extend the bucket
3209 * also.
3211 clusters_add += 1;
3212 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3213 if (OCFS2_XATTR_BUCKET_SIZE ==
3214 OCFS2_SB(inode->i_sb)->s_clustersize) {
3215 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3216 clusters_add += 1;
3218 } else {
3219 meta_add += 1;
3220 credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
3222 out:
3223 if (clusters_need)
3224 *clusters_need = clusters_add;
3225 if (meta_need)
3226 *meta_need = meta_add;
3227 if (credits_need)
3228 *credits_need = credits;
3229 brelse(bh);
3230 return ret;
3233 static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
3234 struct ocfs2_dinode *di,
3235 struct ocfs2_xattr_info *xi,
3236 struct ocfs2_xattr_search *xis,
3237 struct ocfs2_xattr_search *xbs,
3238 struct ocfs2_xattr_set_ctxt *ctxt,
3239 int extra_meta,
3240 int *credits)
3242 int clusters_add, meta_add, ret;
3243 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3245 memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
3247 ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
3249 ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
3250 &clusters_add, &meta_add, credits);
3251 if (ret) {
3252 mlog_errno(ret);
3253 return ret;
3256 meta_add += extra_meta;
3257 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
3258 "credits = %d\n", xi->xi_name, meta_add, clusters_add, *credits);
3260 if (meta_add) {
3261 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
3262 &ctxt->meta_ac);
3263 if (ret) {
3264 mlog_errno(ret);
3265 goto out;
3269 if (clusters_add) {
3270 ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
3271 if (ret)
3272 mlog_errno(ret);
3274 out:
3275 if (ret) {
3276 if (ctxt->meta_ac) {
3277 ocfs2_free_alloc_context(ctxt->meta_ac);
3278 ctxt->meta_ac = NULL;
3282 * We cannot have an error and a non null ctxt->data_ac.
3286 return ret;
3289 static int __ocfs2_xattr_set_handle(struct inode *inode,
3290 struct ocfs2_dinode *di,
3291 struct ocfs2_xattr_info *xi,
3292 struct ocfs2_xattr_search *xis,
3293 struct ocfs2_xattr_search *xbs,
3294 struct ocfs2_xattr_set_ctxt *ctxt)
3296 int ret = 0, credits, old_found;
3298 if (!xi->xi_value) {
3299 /* Remove existing extended attribute */
3300 if (!xis->not_found)
3301 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3302 else if (!xbs->not_found)
3303 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3304 } else {
3305 /* We always try to set extended attribute into inode first*/
3306 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3307 if (!ret && !xbs->not_found) {
3309 * If succeed and that extended attribute existing in
3310 * external block, then we will remove it.
3312 xi->xi_value = NULL;
3313 xi->xi_value_len = 0;
3315 old_found = xis->not_found;
3316 xis->not_found = -ENODATA;
3317 ret = ocfs2_calc_xattr_set_need(inode,
3320 xis,
3321 xbs,
3322 NULL,
3323 NULL,
3324 &credits);
3325 xis->not_found = old_found;
3326 if (ret) {
3327 mlog_errno(ret);
3328 goto out;
3331 ret = ocfs2_extend_trans(ctxt->handle, credits +
3332 ctxt->handle->h_buffer_credits);
3333 if (ret) {
3334 mlog_errno(ret);
3335 goto out;
3337 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3338 } else if (ret == -ENOSPC) {
3339 if (di->i_xattr_loc && !xbs->xattr_bh) {
3340 ret = ocfs2_xattr_block_find(inode,
3341 xi->xi_name_index,
3342 xi->xi_name, xbs);
3343 if (ret)
3344 goto out;
3346 old_found = xis->not_found;
3347 xis->not_found = -ENODATA;
3348 ret = ocfs2_calc_xattr_set_need(inode,
3351 xis,
3352 xbs,
3353 NULL,
3354 NULL,
3355 &credits);
3356 xis->not_found = old_found;
3357 if (ret) {
3358 mlog_errno(ret);
3359 goto out;
3362 ret = ocfs2_extend_trans(ctxt->handle, credits +
3363 ctxt->handle->h_buffer_credits);
3364 if (ret) {
3365 mlog_errno(ret);
3366 goto out;
3370 * If no space in inode, we will set extended attribute
3371 * into external block.
3373 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3374 if (ret)
3375 goto out;
3376 if (!xis->not_found) {
3378 * If succeed and that extended attribute
3379 * existing in inode, we will remove it.
3381 xi->xi_value = NULL;
3382 xi->xi_value_len = 0;
3383 xbs->not_found = -ENODATA;
3384 ret = ocfs2_calc_xattr_set_need(inode,
3387 xis,
3388 xbs,
3389 NULL,
3390 NULL,
3391 &credits);
3392 if (ret) {
3393 mlog_errno(ret);
3394 goto out;
3397 ret = ocfs2_extend_trans(ctxt->handle, credits +
3398 ctxt->handle->h_buffer_credits);
3399 if (ret) {
3400 mlog_errno(ret);
3401 goto out;
3403 ret = ocfs2_xattr_ibody_set(inode, xi,
3404 xis, ctxt);
3409 if (!ret) {
3410 /* Update inode ctime. */
3411 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
3412 xis->inode_bh,
3413 OCFS2_JOURNAL_ACCESS_WRITE);
3414 if (ret) {
3415 mlog_errno(ret);
3416 goto out;
3419 inode->i_ctime = CURRENT_TIME;
3420 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
3421 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
3422 ocfs2_journal_dirty(ctxt->handle, xis->inode_bh);
3424 out:
3425 return ret;
3429 * This function only called duing creating inode
3430 * for init security/acl xattrs of the new inode.
3431 * All transanction credits have been reserved in mknod.
3433 int ocfs2_xattr_set_handle(handle_t *handle,
3434 struct inode *inode,
3435 struct buffer_head *di_bh,
3436 int name_index,
3437 const char *name,
3438 const void *value,
3439 size_t value_len,
3440 int flags,
3441 struct ocfs2_alloc_context *meta_ac,
3442 struct ocfs2_alloc_context *data_ac)
3444 struct ocfs2_dinode *di;
3445 int ret;
3447 struct ocfs2_xattr_info xi = {
3448 .xi_name_index = name_index,
3449 .xi_name = name,
3450 .xi_name_len = strlen(name),
3451 .xi_value = value,
3452 .xi_value_len = value_len,
3455 struct ocfs2_xattr_search xis = {
3456 .not_found = -ENODATA,
3459 struct ocfs2_xattr_search xbs = {
3460 .not_found = -ENODATA,
3463 struct ocfs2_xattr_set_ctxt ctxt = {
3464 .handle = handle,
3465 .meta_ac = meta_ac,
3466 .data_ac = data_ac,
3469 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3470 return -EOPNOTSUPP;
3473 * In extreme situation, may need xattr bucket when
3474 * block size is too small. And we have already reserved
3475 * the credits for bucket in mknod.
3477 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) {
3478 xbs.bucket = ocfs2_xattr_bucket_new(inode);
3479 if (!xbs.bucket) {
3480 mlog_errno(-ENOMEM);
3481 return -ENOMEM;
3485 xis.inode_bh = xbs.inode_bh = di_bh;
3486 di = (struct ocfs2_dinode *)di_bh->b_data;
3488 down_write(&OCFS2_I(inode)->ip_xattr_sem);
3490 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3491 if (ret)
3492 goto cleanup;
3493 if (xis.not_found) {
3494 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3495 if (ret)
3496 goto cleanup;
3499 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3501 cleanup:
3502 up_write(&OCFS2_I(inode)->ip_xattr_sem);
3503 brelse(xbs.xattr_bh);
3504 ocfs2_xattr_bucket_free(xbs.bucket);
3506 return ret;
3510 * ocfs2_xattr_set()
3512 * Set, replace or remove an extended attribute for this inode.
3513 * value is NULL to remove an existing extended attribute, else either
3514 * create or replace an extended attribute.
3516 int ocfs2_xattr_set(struct inode *inode,
3517 int name_index,
3518 const char *name,
3519 const void *value,
3520 size_t value_len,
3521 int flags)
3523 struct buffer_head *di_bh = NULL;
3524 struct ocfs2_dinode *di;
3525 int ret, credits, ref_meta = 0, ref_credits = 0;
3526 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3527 struct inode *tl_inode = osb->osb_tl_inode;
3528 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
3529 struct ocfs2_refcount_tree *ref_tree = NULL;
3531 struct ocfs2_xattr_info xi = {
3532 .xi_name_index = name_index,
3533 .xi_name = name,
3534 .xi_name_len = strlen(name),
3535 .xi_value = value,
3536 .xi_value_len = value_len,
3539 struct ocfs2_xattr_search xis = {
3540 .not_found = -ENODATA,
3543 struct ocfs2_xattr_search xbs = {
3544 .not_found = -ENODATA,
3547 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3548 return -EOPNOTSUPP;
3551 * Only xbs will be used on indexed trees. xis doesn't need a
3552 * bucket.
3554 xbs.bucket = ocfs2_xattr_bucket_new(inode);
3555 if (!xbs.bucket) {
3556 mlog_errno(-ENOMEM);
3557 return -ENOMEM;
3560 ret = ocfs2_inode_lock(inode, &di_bh, 1);
3561 if (ret < 0) {
3562 mlog_errno(ret);
3563 goto cleanup_nolock;
3565 xis.inode_bh = xbs.inode_bh = di_bh;
3566 di = (struct ocfs2_dinode *)di_bh->b_data;
3568 down_write(&OCFS2_I(inode)->ip_xattr_sem);
3570 * Scan inode and external block to find the same name
3571 * extended attribute and collect search infomation.
3573 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3574 if (ret)
3575 goto cleanup;
3576 if (xis.not_found) {
3577 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3578 if (ret)
3579 goto cleanup;
3582 if (xis.not_found && xbs.not_found) {
3583 ret = -ENODATA;
3584 if (flags & XATTR_REPLACE)
3585 goto cleanup;
3586 ret = 0;
3587 if (!value)
3588 goto cleanup;
3589 } else {
3590 ret = -EEXIST;
3591 if (flags & XATTR_CREATE)
3592 goto cleanup;
3595 /* Check whether the value is refcounted and do some prepartion. */
3596 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL &&
3597 (!xis.not_found || !xbs.not_found)) {
3598 ret = ocfs2_prepare_refcount_xattr(inode, di, &xi,
3599 &xis, &xbs, &ref_tree,
3600 &ref_meta, &ref_credits);
3601 if (ret) {
3602 mlog_errno(ret);
3603 goto cleanup;
3607 mutex_lock(&tl_inode->i_mutex);
3609 if (ocfs2_truncate_log_needs_flush(osb)) {
3610 ret = __ocfs2_flush_truncate_log(osb);
3611 if (ret < 0) {
3612 mutex_unlock(&tl_inode->i_mutex);
3613 mlog_errno(ret);
3614 goto cleanup;
3617 mutex_unlock(&tl_inode->i_mutex);
3619 ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
3620 &xbs, &ctxt, ref_meta, &credits);
3621 if (ret) {
3622 mlog_errno(ret);
3623 goto cleanup;
3626 /* we need to update inode's ctime field, so add credit for it. */
3627 credits += OCFS2_INODE_UPDATE_CREDITS;
3628 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
3629 if (IS_ERR(ctxt.handle)) {
3630 ret = PTR_ERR(ctxt.handle);
3631 mlog_errno(ret);
3632 goto cleanup;
3635 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3637 ocfs2_commit_trans(osb, ctxt.handle);
3639 if (ctxt.data_ac)
3640 ocfs2_free_alloc_context(ctxt.data_ac);
3641 if (ctxt.meta_ac)
3642 ocfs2_free_alloc_context(ctxt.meta_ac);
3643 if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
3644 ocfs2_schedule_truncate_log_flush(osb, 1);
3645 ocfs2_run_deallocs(osb, &ctxt.dealloc);
3647 cleanup:
3648 if (ref_tree)
3649 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
3650 up_write(&OCFS2_I(inode)->ip_xattr_sem);
3651 if (!value && !ret) {
3652 ret = ocfs2_try_remove_refcount_tree(inode, di_bh);
3653 if (ret)
3654 mlog_errno(ret);
3656 ocfs2_inode_unlock(inode, 1);
3657 cleanup_nolock:
3658 brelse(di_bh);
3659 brelse(xbs.xattr_bh);
3660 ocfs2_xattr_bucket_free(xbs.bucket);
3662 return ret;
3666 * Find the xattr extent rec which may contains name_hash.
3667 * e_cpos will be the first name hash of the xattr rec.
3668 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
3670 static int ocfs2_xattr_get_rec(struct inode *inode,
3671 u32 name_hash,
3672 u64 *p_blkno,
3673 u32 *e_cpos,
3674 u32 *num_clusters,
3675 struct ocfs2_extent_list *el)
3677 int ret = 0, i;
3678 struct buffer_head *eb_bh = NULL;
3679 struct ocfs2_extent_block *eb;
3680 struct ocfs2_extent_rec *rec = NULL;
3681 u64 e_blkno = 0;
3683 if (el->l_tree_depth) {
3684 ret = ocfs2_find_leaf(INODE_CACHE(inode), el, name_hash,
3685 &eb_bh);
3686 if (ret) {
3687 mlog_errno(ret);
3688 goto out;
3691 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
3692 el = &eb->h_list;
3694 if (el->l_tree_depth) {
3695 ocfs2_error(inode->i_sb,
3696 "Inode %lu has non zero tree depth in "
3697 "xattr tree block %llu\n", inode->i_ino,
3698 (unsigned long long)eb_bh->b_blocknr);
3699 ret = -EROFS;
3700 goto out;
3704 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
3705 rec = &el->l_recs[i];
3707 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
3708 e_blkno = le64_to_cpu(rec->e_blkno);
3709 break;
3713 if (!e_blkno) {
3714 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
3715 "record (%u, %u, 0) in xattr", inode->i_ino,
3716 le32_to_cpu(rec->e_cpos),
3717 ocfs2_rec_clusters(el, rec));
3718 ret = -EROFS;
3719 goto out;
3722 *p_blkno = le64_to_cpu(rec->e_blkno);
3723 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
3724 if (e_cpos)
3725 *e_cpos = le32_to_cpu(rec->e_cpos);
3726 out:
3727 brelse(eb_bh);
3728 return ret;
3731 typedef int (xattr_bucket_func)(struct inode *inode,
3732 struct ocfs2_xattr_bucket *bucket,
3733 void *para);
3735 static int ocfs2_find_xe_in_bucket(struct inode *inode,
3736 struct ocfs2_xattr_bucket *bucket,
3737 int name_index,
3738 const char *name,
3739 u32 name_hash,
3740 u16 *xe_index,
3741 int *found)
3743 int i, ret = 0, cmp = 1, block_off, new_offset;
3744 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
3745 size_t name_len = strlen(name);
3746 struct ocfs2_xattr_entry *xe = NULL;
3747 char *xe_name;
3750 * We don't use binary search in the bucket because there
3751 * may be multiple entries with the same name hash.
3753 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3754 xe = &xh->xh_entries[i];
3756 if (name_hash > le32_to_cpu(xe->xe_name_hash))
3757 continue;
3758 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
3759 break;
3761 cmp = name_index - ocfs2_xattr_get_type(xe);
3762 if (!cmp)
3763 cmp = name_len - xe->xe_name_len;
3764 if (cmp)
3765 continue;
3767 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
3770 &block_off,
3771 &new_offset);
3772 if (ret) {
3773 mlog_errno(ret);
3774 break;
3778 xe_name = bucket_block(bucket, block_off) + new_offset;
3779 if (!memcmp(name, xe_name, name_len)) {
3780 *xe_index = i;
3781 *found = 1;
3782 ret = 0;
3783 break;
3787 return ret;
3791 * Find the specified xattr entry in a series of buckets.
3792 * This series start from p_blkno and last for num_clusters.
3793 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
3794 * the num of the valid buckets.
3796 * Return the buffer_head this xattr should reside in. And if the xattr's
3797 * hash is in the gap of 2 buckets, return the lower bucket.
3799 static int ocfs2_xattr_bucket_find(struct inode *inode,
3800 int name_index,
3801 const char *name,
3802 u32 name_hash,
3803 u64 p_blkno,
3804 u32 first_hash,
3805 u32 num_clusters,
3806 struct ocfs2_xattr_search *xs)
3808 int ret, found = 0;
3809 struct ocfs2_xattr_header *xh = NULL;
3810 struct ocfs2_xattr_entry *xe = NULL;
3811 u16 index = 0;
3812 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3813 int low_bucket = 0, bucket, high_bucket;
3814 struct ocfs2_xattr_bucket *search;
3815 u32 last_hash;
3816 u64 blkno, lower_blkno = 0;
3818 search = ocfs2_xattr_bucket_new(inode);
3819 if (!search) {
3820 ret = -ENOMEM;
3821 mlog_errno(ret);
3822 goto out;
3825 ret = ocfs2_read_xattr_bucket(search, p_blkno);
3826 if (ret) {
3827 mlog_errno(ret);
3828 goto out;
3831 xh = bucket_xh(search);
3832 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
3833 while (low_bucket <= high_bucket) {
3834 ocfs2_xattr_bucket_relse(search);
3836 bucket = (low_bucket + high_bucket) / 2;
3837 blkno = p_blkno + bucket * blk_per_bucket;
3838 ret = ocfs2_read_xattr_bucket(search, blkno);
3839 if (ret) {
3840 mlog_errno(ret);
3841 goto out;
3844 xh = bucket_xh(search);
3845 xe = &xh->xh_entries[0];
3846 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
3847 high_bucket = bucket - 1;
3848 continue;
3852 * Check whether the hash of the last entry in our
3853 * bucket is larger than the search one. for an empty
3854 * bucket, the last one is also the first one.
3856 if (xh->xh_count)
3857 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
3859 last_hash = le32_to_cpu(xe->xe_name_hash);
3861 /* record lower_blkno which may be the insert place. */
3862 lower_blkno = blkno;
3864 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
3865 low_bucket = bucket + 1;
3866 continue;
3869 /* the searched xattr should reside in this bucket if exists. */
3870 ret = ocfs2_find_xe_in_bucket(inode, search,
3871 name_index, name, name_hash,
3872 &index, &found);
3873 if (ret) {
3874 mlog_errno(ret);
3875 goto out;
3877 break;
3881 * Record the bucket we have found.
3882 * When the xattr's hash value is in the gap of 2 buckets, we will
3883 * always set it to the previous bucket.
3885 if (!lower_blkno)
3886 lower_blkno = p_blkno;
3888 /* This should be in cache - we just read it during the search */
3889 ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
3890 if (ret) {
3891 mlog_errno(ret);
3892 goto out;
3895 xs->header = bucket_xh(xs->bucket);
3896 xs->base = bucket_block(xs->bucket, 0);
3897 xs->end = xs->base + inode->i_sb->s_blocksize;
3899 if (found) {
3900 xs->here = &xs->header->xh_entries[index];
3901 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
3902 (unsigned long long)bucket_blkno(xs->bucket), index);
3903 } else
3904 ret = -ENODATA;
3906 out:
3907 ocfs2_xattr_bucket_free(search);
3908 return ret;
3911 static int ocfs2_xattr_index_block_find(struct inode *inode,
3912 struct buffer_head *root_bh,
3913 int name_index,
3914 const char *name,
3915 struct ocfs2_xattr_search *xs)
3917 int ret;
3918 struct ocfs2_xattr_block *xb =
3919 (struct ocfs2_xattr_block *)root_bh->b_data;
3920 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3921 struct ocfs2_extent_list *el = &xb_root->xt_list;
3922 u64 p_blkno = 0;
3923 u32 first_hash, num_clusters = 0;
3924 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
3926 if (le16_to_cpu(el->l_next_free_rec) == 0)
3927 return -ENODATA;
3929 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
3930 name, name_hash, name_index);
3932 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
3933 &num_clusters, el);
3934 if (ret) {
3935 mlog_errno(ret);
3936 goto out;
3939 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
3941 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
3942 "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
3943 first_hash);
3945 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
3946 p_blkno, first_hash, num_clusters, xs);
3948 out:
3949 return ret;
3952 static int ocfs2_iterate_xattr_buckets(struct inode *inode,
3953 u64 blkno,
3954 u32 clusters,
3955 xattr_bucket_func *func,
3956 void *para)
3958 int i, ret = 0;
3959 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
3960 u32 num_buckets = clusters * bpc;
3961 struct ocfs2_xattr_bucket *bucket;
3963 bucket = ocfs2_xattr_bucket_new(inode);
3964 if (!bucket) {
3965 mlog_errno(-ENOMEM);
3966 return -ENOMEM;
3969 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
3970 clusters, (unsigned long long)blkno);
3972 for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
3973 ret = ocfs2_read_xattr_bucket(bucket, blkno);
3974 if (ret) {
3975 mlog_errno(ret);
3976 break;
3980 * The real bucket num in this series of blocks is stored
3981 * in the 1st bucket.
3983 if (i == 0)
3984 num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
3986 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
3987 (unsigned long long)blkno,
3988 le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
3989 if (func) {
3990 ret = func(inode, bucket, para);
3991 if (ret && ret != -ERANGE)
3992 mlog_errno(ret);
3993 /* Fall through to bucket_relse() */
3996 ocfs2_xattr_bucket_relse(bucket);
3997 if (ret)
3998 break;
4001 ocfs2_xattr_bucket_free(bucket);
4002 return ret;
4005 struct ocfs2_xattr_tree_list {
4006 char *buffer;
4007 size_t buffer_size;
4008 size_t result;
4011 static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
4012 struct ocfs2_xattr_header *xh,
4013 int index,
4014 int *block_off,
4015 int *new_offset)
4017 u16 name_offset;
4019 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
4020 return -EINVAL;
4022 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
4024 *block_off = name_offset >> sb->s_blocksize_bits;
4025 *new_offset = name_offset % sb->s_blocksize;
4027 return 0;
4030 static int ocfs2_list_xattr_bucket(struct inode *inode,
4031 struct ocfs2_xattr_bucket *bucket,
4032 void *para)
4034 int ret = 0, type;
4035 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
4036 int i, block_off, new_offset;
4037 const char *prefix, *name;
4039 for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
4040 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
4041 type = ocfs2_xattr_get_type(entry);
4042 prefix = ocfs2_xattr_prefix(type);
4044 if (prefix) {
4045 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
4046 bucket_xh(bucket),
4048 &block_off,
4049 &new_offset);
4050 if (ret)
4051 break;
4053 name = (const char *)bucket_block(bucket, block_off) +
4054 new_offset;
4055 ret = ocfs2_xattr_list_entry(xl->buffer,
4056 xl->buffer_size,
4057 &xl->result,
4058 prefix, name,
4059 entry->xe_name_len);
4060 if (ret)
4061 break;
4065 return ret;
4068 static int ocfs2_iterate_xattr_index_block(struct inode *inode,
4069 struct buffer_head *blk_bh,
4070 xattr_tree_rec_func *rec_func,
4071 void *para)
4073 struct ocfs2_xattr_block *xb =
4074 (struct ocfs2_xattr_block *)blk_bh->b_data;
4075 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
4076 int ret = 0;
4077 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
4078 u64 p_blkno = 0;
4080 if (!el->l_next_free_rec || !rec_func)
4081 return 0;
4083 while (name_hash > 0) {
4084 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
4085 &e_cpos, &num_clusters, el);
4086 if (ret) {
4087 mlog_errno(ret);
4088 break;
4091 ret = rec_func(inode, blk_bh, p_blkno, e_cpos,
4092 num_clusters, para);
4093 if (ret) {
4094 if (ret != -ERANGE)
4095 mlog_errno(ret);
4096 break;
4099 if (e_cpos == 0)
4100 break;
4102 name_hash = e_cpos - 1;
4105 return ret;
4109 static int ocfs2_list_xattr_tree_rec(struct inode *inode,
4110 struct buffer_head *root_bh,
4111 u64 blkno, u32 cpos, u32 len, void *para)
4113 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
4114 ocfs2_list_xattr_bucket, para);
4117 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
4118 struct buffer_head *blk_bh,
4119 char *buffer,
4120 size_t buffer_size)
4122 int ret;
4123 struct ocfs2_xattr_tree_list xl = {
4124 .buffer = buffer,
4125 .buffer_size = buffer_size,
4126 .result = 0,
4129 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
4130 ocfs2_list_xattr_tree_rec, &xl);
4131 if (ret) {
4132 mlog_errno(ret);
4133 goto out;
4136 ret = xl.result;
4137 out:
4138 return ret;
4141 static int cmp_xe(const void *a, const void *b)
4143 const struct ocfs2_xattr_entry *l = a, *r = b;
4144 u32 l_hash = le32_to_cpu(l->xe_name_hash);
4145 u32 r_hash = le32_to_cpu(r->xe_name_hash);
4147 if (l_hash > r_hash)
4148 return 1;
4149 if (l_hash < r_hash)
4150 return -1;
4151 return 0;
4154 static void swap_xe(void *a, void *b, int size)
4156 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
4158 tmp = *l;
4159 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
4160 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
4164 * When the ocfs2_xattr_block is filled up, new bucket will be created
4165 * and all the xattr entries will be moved to the new bucket.
4166 * The header goes at the start of the bucket, and the names+values are
4167 * filled from the end. This is why *target starts as the last buffer.
4168 * Note: we need to sort the entries since they are not saved in order
4169 * in the ocfs2_xattr_block.
4171 static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
4172 struct buffer_head *xb_bh,
4173 struct ocfs2_xattr_bucket *bucket)
4175 int i, blocksize = inode->i_sb->s_blocksize;
4176 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4177 u16 offset, size, off_change;
4178 struct ocfs2_xattr_entry *xe;
4179 struct ocfs2_xattr_block *xb =
4180 (struct ocfs2_xattr_block *)xb_bh->b_data;
4181 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
4182 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
4183 u16 count = le16_to_cpu(xb_xh->xh_count);
4184 char *src = xb_bh->b_data;
4185 char *target = bucket_block(bucket, blks - 1);
4187 mlog(0, "cp xattr from block %llu to bucket %llu\n",
4188 (unsigned long long)xb_bh->b_blocknr,
4189 (unsigned long long)bucket_blkno(bucket));
4191 for (i = 0; i < blks; i++)
4192 memset(bucket_block(bucket, i), 0, blocksize);
4195 * Since the xe_name_offset is based on ocfs2_xattr_header,
4196 * there is a offset change corresponding to the change of
4197 * ocfs2_xattr_header's position.
4199 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
4200 xe = &xb_xh->xh_entries[count - 1];
4201 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
4202 size = blocksize - offset;
4204 /* copy all the names and values. */
4205 memcpy(target + offset, src + offset, size);
4207 /* Init new header now. */
4208 xh->xh_count = xb_xh->xh_count;
4209 xh->xh_num_buckets = cpu_to_le16(1);
4210 xh->xh_name_value_len = cpu_to_le16(size);
4211 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
4213 /* copy all the entries. */
4214 target = bucket_block(bucket, 0);
4215 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
4216 size = count * sizeof(struct ocfs2_xattr_entry);
4217 memcpy(target + offset, (char *)xb_xh + offset, size);
4219 /* Change the xe offset for all the xe because of the move. */
4220 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
4221 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
4222 for (i = 0; i < count; i++)
4223 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
4225 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
4226 offset, size, off_change);
4228 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
4229 cmp_xe, swap_xe);
4233 * After we move xattr from block to index btree, we have to
4234 * update ocfs2_xattr_search to the new xe and base.
4236 * When the entry is in xattr block, xattr_bh indicates the storage place.
4237 * While if the entry is in index b-tree, "bucket" indicates the
4238 * real place of the xattr.
4240 static void ocfs2_xattr_update_xattr_search(struct inode *inode,
4241 struct ocfs2_xattr_search *xs,
4242 struct buffer_head *old_bh)
4244 char *buf = old_bh->b_data;
4245 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
4246 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
4247 int i;
4249 xs->header = bucket_xh(xs->bucket);
4250 xs->base = bucket_block(xs->bucket, 0);
4251 xs->end = xs->base + inode->i_sb->s_blocksize;
4253 if (xs->not_found)
4254 return;
4256 i = xs->here - old_xh->xh_entries;
4257 xs->here = &xs->header->xh_entries[i];
4260 static int ocfs2_xattr_create_index_block(struct inode *inode,
4261 struct ocfs2_xattr_search *xs,
4262 struct ocfs2_xattr_set_ctxt *ctxt)
4264 int ret;
4265 u32 bit_off, len;
4266 u64 blkno;
4267 handle_t *handle = ctxt->handle;
4268 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4269 struct ocfs2_inode_info *oi = OCFS2_I(inode);
4270 struct buffer_head *xb_bh = xs->xattr_bh;
4271 struct ocfs2_xattr_block *xb =
4272 (struct ocfs2_xattr_block *)xb_bh->b_data;
4273 struct ocfs2_xattr_tree_root *xr;
4274 u16 xb_flags = le16_to_cpu(xb->xb_flags);
4276 mlog(0, "create xattr index block for %llu\n",
4277 (unsigned long long)xb_bh->b_blocknr);
4279 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
4280 BUG_ON(!xs->bucket);
4283 * XXX:
4284 * We can use this lock for now, and maybe move to a dedicated mutex
4285 * if performance becomes a problem later.
4287 down_write(&oi->ip_alloc_sem);
4289 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), xb_bh,
4290 OCFS2_JOURNAL_ACCESS_WRITE);
4291 if (ret) {
4292 mlog_errno(ret);
4293 goto out;
4296 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac,
4297 1, 1, &bit_off, &len);
4298 if (ret) {
4299 mlog_errno(ret);
4300 goto out;
4304 * The bucket may spread in many blocks, and
4305 * we will only touch the 1st block and the last block
4306 * in the whole bucket(one for entry and one for data).
4308 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
4310 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
4311 (unsigned long long)blkno);
4313 ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
4314 if (ret) {
4315 mlog_errno(ret);
4316 goto out;
4319 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
4320 OCFS2_JOURNAL_ACCESS_CREATE);
4321 if (ret) {
4322 mlog_errno(ret);
4323 goto out;
4326 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
4327 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
4329 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
4331 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
4332 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
4333 offsetof(struct ocfs2_xattr_block, xb_attrs));
4335 xr = &xb->xb_attrs.xb_root;
4336 xr->xt_clusters = cpu_to_le32(1);
4337 xr->xt_last_eb_blk = 0;
4338 xr->xt_list.l_tree_depth = 0;
4339 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
4340 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
4342 xr->xt_list.l_recs[0].e_cpos = 0;
4343 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
4344 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
4346 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
4348 ocfs2_journal_dirty(handle, xb_bh);
4350 out:
4351 up_write(&oi->ip_alloc_sem);
4353 return ret;
4356 static int cmp_xe_offset(const void *a, const void *b)
4358 const struct ocfs2_xattr_entry *l = a, *r = b;
4359 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
4360 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
4362 if (l_name_offset < r_name_offset)
4363 return 1;
4364 if (l_name_offset > r_name_offset)
4365 return -1;
4366 return 0;
4370 * defrag a xattr bucket if we find that the bucket has some
4371 * holes beteen name/value pairs.
4372 * We will move all the name/value pairs to the end of the bucket
4373 * so that we can spare some space for insertion.
4375 static int ocfs2_defrag_xattr_bucket(struct inode *inode,
4376 handle_t *handle,
4377 struct ocfs2_xattr_bucket *bucket)
4379 int ret, i;
4380 size_t end, offset, len;
4381 struct ocfs2_xattr_header *xh;
4382 char *entries, *buf, *bucket_buf = NULL;
4383 u64 blkno = bucket_blkno(bucket);
4384 u16 xh_free_start;
4385 size_t blocksize = inode->i_sb->s_blocksize;
4386 struct ocfs2_xattr_entry *xe;
4389 * In order to make the operation more efficient and generic,
4390 * we copy all the blocks into a contiguous memory and do the
4391 * defragment there, so if anything is error, we will not touch
4392 * the real block.
4394 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
4395 if (!bucket_buf) {
4396 ret = -EIO;
4397 goto out;
4400 buf = bucket_buf;
4401 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4402 memcpy(buf, bucket_block(bucket, i), blocksize);
4404 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
4405 OCFS2_JOURNAL_ACCESS_WRITE);
4406 if (ret < 0) {
4407 mlog_errno(ret);
4408 goto out;
4411 xh = (struct ocfs2_xattr_header *)bucket_buf;
4412 entries = (char *)xh->xh_entries;
4413 xh_free_start = le16_to_cpu(xh->xh_free_start);
4415 mlog(0, "adjust xattr bucket in %llu, count = %u, "
4416 "xh_free_start = %u, xh_name_value_len = %u.\n",
4417 (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
4418 xh_free_start, le16_to_cpu(xh->xh_name_value_len));
4421 * sort all the entries by their offset.
4422 * the largest will be the first, so that we can
4423 * move them to the end one by one.
4425 sort(entries, le16_to_cpu(xh->xh_count),
4426 sizeof(struct ocfs2_xattr_entry),
4427 cmp_xe_offset, swap_xe);
4429 /* Move all name/values to the end of the bucket. */
4430 xe = xh->xh_entries;
4431 end = OCFS2_XATTR_BUCKET_SIZE;
4432 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
4433 offset = le16_to_cpu(xe->xe_name_offset);
4434 len = namevalue_size_xe(xe);
4437 * We must make sure that the name/value pair
4438 * exist in the same block. So adjust end to
4439 * the previous block end if needed.
4441 if (((end - len) / blocksize !=
4442 (end - 1) / blocksize))
4443 end = end - end % blocksize;
4445 if (end > offset + len) {
4446 memmove(bucket_buf + end - len,
4447 bucket_buf + offset, len);
4448 xe->xe_name_offset = cpu_to_le16(end - len);
4451 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
4452 "bucket %llu\n", (unsigned long long)blkno);
4454 end -= len;
4457 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
4458 "bucket %llu\n", (unsigned long long)blkno);
4460 if (xh_free_start == end)
4461 goto out;
4463 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
4464 xh->xh_free_start = cpu_to_le16(end);
4466 /* sort the entries by their name_hash. */
4467 sort(entries, le16_to_cpu(xh->xh_count),
4468 sizeof(struct ocfs2_xattr_entry),
4469 cmp_xe, swap_xe);
4471 buf = bucket_buf;
4472 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4473 memcpy(bucket_block(bucket, i), buf, blocksize);
4474 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
4476 out:
4477 kfree(bucket_buf);
4478 return ret;
4482 * prev_blkno points to the start of an existing extent. new_blkno
4483 * points to a newly allocated extent. Because we know each of our
4484 * clusters contains more than bucket, we can easily split one cluster
4485 * at a bucket boundary. So we take the last cluster of the existing
4486 * extent and split it down the middle. We move the last half of the
4487 * buckets in the last cluster of the existing extent over to the new
4488 * extent.
4490 * first_bh is the buffer at prev_blkno so we can update the existing
4491 * extent's bucket count. header_bh is the bucket were we were hoping
4492 * to insert our xattr. If the bucket move places the target in the new
4493 * extent, we'll update first_bh and header_bh after modifying the old
4494 * extent.
4496 * first_hash will be set as the 1st xe's name_hash in the new extent.
4498 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
4499 handle_t *handle,
4500 struct ocfs2_xattr_bucket *first,
4501 struct ocfs2_xattr_bucket *target,
4502 u64 new_blkno,
4503 u32 num_clusters,
4504 u32 *first_hash)
4506 int ret;
4507 struct super_block *sb = inode->i_sb;
4508 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb);
4509 int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
4510 int to_move = num_buckets / 2;
4511 u64 src_blkno;
4512 u64 last_cluster_blkno = bucket_blkno(first) +
4513 ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1));
4515 BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets);
4516 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize);
4518 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
4519 (unsigned long long)last_cluster_blkno, (unsigned long long)new_blkno);
4521 ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first),
4522 last_cluster_blkno, new_blkno,
4523 to_move, first_hash);
4524 if (ret) {
4525 mlog_errno(ret);
4526 goto out;
4529 /* This is the first bucket that got moved */
4530 src_blkno = last_cluster_blkno + (to_move * blks_per_bucket);
4533 * If the target bucket was part of the moved buckets, we need to
4534 * update first and target.
4536 if (bucket_blkno(target) >= src_blkno) {
4537 /* Find the block for the new target bucket */
4538 src_blkno = new_blkno +
4539 (bucket_blkno(target) - src_blkno);
4541 ocfs2_xattr_bucket_relse(first);
4542 ocfs2_xattr_bucket_relse(target);
4545 * These shouldn't fail - the buffers are in the
4546 * journal from ocfs2_cp_xattr_bucket().
4548 ret = ocfs2_read_xattr_bucket(first, new_blkno);
4549 if (ret) {
4550 mlog_errno(ret);
4551 goto out;
4553 ret = ocfs2_read_xattr_bucket(target, src_blkno);
4554 if (ret)
4555 mlog_errno(ret);
4559 out:
4560 return ret;
4564 * Find the suitable pos when we divide a bucket into 2.
4565 * We have to make sure the xattrs with the same hash value exist
4566 * in the same bucket.
4568 * If this ocfs2_xattr_header covers more than one hash value, find a
4569 * place where the hash value changes. Try to find the most even split.
4570 * The most common case is that all entries have different hash values,
4571 * and the first check we make will find a place to split.
4573 static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
4575 struct ocfs2_xattr_entry *entries = xh->xh_entries;
4576 int count = le16_to_cpu(xh->xh_count);
4577 int delta, middle = count / 2;
4580 * We start at the middle. Each step gets farther away in both
4581 * directions. We therefore hit the change in hash value
4582 * nearest to the middle. Note that this loop does not execute for
4583 * count < 2.
4585 for (delta = 0; delta < middle; delta++) {
4586 /* Let's check delta earlier than middle */
4587 if (cmp_xe(&entries[middle - delta - 1],
4588 &entries[middle - delta]))
4589 return middle - delta;
4591 /* For even counts, don't walk off the end */
4592 if ((middle + delta + 1) == count)
4593 continue;
4595 /* Now try delta past middle */
4596 if (cmp_xe(&entries[middle + delta],
4597 &entries[middle + delta + 1]))
4598 return middle + delta + 1;
4601 /* Every entry had the same hash */
4602 return count;
4606 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
4607 * first_hash will record the 1st hash of the new bucket.
4609 * Normally half of the xattrs will be moved. But we have to make
4610 * sure that the xattrs with the same hash value are stored in the
4611 * same bucket. If all the xattrs in this bucket have the same hash
4612 * value, the new bucket will be initialized as an empty one and the
4613 * first_hash will be initialized as (hash_value+1).
4615 static int ocfs2_divide_xattr_bucket(struct inode *inode,
4616 handle_t *handle,
4617 u64 blk,
4618 u64 new_blk,
4619 u32 *first_hash,
4620 int new_bucket_head)
4622 int ret, i;
4623 int count, start, len, name_value_len = 0, name_offset = 0;
4624 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
4625 struct ocfs2_xattr_header *xh;
4626 struct ocfs2_xattr_entry *xe;
4627 int blocksize = inode->i_sb->s_blocksize;
4629 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
4630 (unsigned long long)blk, (unsigned long long)new_blk);
4632 s_bucket = ocfs2_xattr_bucket_new(inode);
4633 t_bucket = ocfs2_xattr_bucket_new(inode);
4634 if (!s_bucket || !t_bucket) {
4635 ret = -ENOMEM;
4636 mlog_errno(ret);
4637 goto out;
4640 ret = ocfs2_read_xattr_bucket(s_bucket, blk);
4641 if (ret) {
4642 mlog_errno(ret);
4643 goto out;
4646 ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
4647 OCFS2_JOURNAL_ACCESS_WRITE);
4648 if (ret) {
4649 mlog_errno(ret);
4650 goto out;
4654 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
4655 * there's no need to read it.
4657 ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
4658 if (ret) {
4659 mlog_errno(ret);
4660 goto out;
4664 * Hey, if we're overwriting t_bucket, what difference does
4665 * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the
4666 * same part of ocfs2_cp_xattr_bucket().
4668 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
4669 new_bucket_head ?
4670 OCFS2_JOURNAL_ACCESS_CREATE :
4671 OCFS2_JOURNAL_ACCESS_WRITE);
4672 if (ret) {
4673 mlog_errno(ret);
4674 goto out;
4677 xh = bucket_xh(s_bucket);
4678 count = le16_to_cpu(xh->xh_count);
4679 start = ocfs2_xattr_find_divide_pos(xh);
4681 if (start == count) {
4682 xe = &xh->xh_entries[start-1];
4685 * initialized a new empty bucket here.
4686 * The hash value is set as one larger than
4687 * that of the last entry in the previous bucket.
4689 for (i = 0; i < t_bucket->bu_blocks; i++)
4690 memset(bucket_block(t_bucket, i), 0, blocksize);
4692 xh = bucket_xh(t_bucket);
4693 xh->xh_free_start = cpu_to_le16(blocksize);
4694 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
4695 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
4697 goto set_num_buckets;
4700 /* copy the whole bucket to the new first. */
4701 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4703 /* update the new bucket. */
4704 xh = bucket_xh(t_bucket);
4707 * Calculate the total name/value len and xh_free_start for
4708 * the old bucket first.
4710 name_offset = OCFS2_XATTR_BUCKET_SIZE;
4711 name_value_len = 0;
4712 for (i = 0; i < start; i++) {
4713 xe = &xh->xh_entries[i];
4714 name_value_len += namevalue_size_xe(xe);
4715 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
4716 name_offset = le16_to_cpu(xe->xe_name_offset);
4720 * Now begin the modification to the new bucket.
4722 * In the new bucket, We just move the xattr entry to the beginning
4723 * and don't touch the name/value. So there will be some holes in the
4724 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
4725 * called.
4727 xe = &xh->xh_entries[start];
4728 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
4729 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
4730 (int)((char *)xe - (char *)xh),
4731 (int)((char *)xh->xh_entries - (char *)xh));
4732 memmove((char *)xh->xh_entries, (char *)xe, len);
4733 xe = &xh->xh_entries[count - start];
4734 len = sizeof(struct ocfs2_xattr_entry) * start;
4735 memset((char *)xe, 0, len);
4737 le16_add_cpu(&xh->xh_count, -start);
4738 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
4740 /* Calculate xh_free_start for the new bucket. */
4741 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4742 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4743 xe = &xh->xh_entries[i];
4744 if (le16_to_cpu(xe->xe_name_offset) <
4745 le16_to_cpu(xh->xh_free_start))
4746 xh->xh_free_start = xe->xe_name_offset;
4749 set_num_buckets:
4750 /* set xh->xh_num_buckets for the new xh. */
4751 if (new_bucket_head)
4752 xh->xh_num_buckets = cpu_to_le16(1);
4753 else
4754 xh->xh_num_buckets = 0;
4756 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
4758 /* store the first_hash of the new bucket. */
4759 if (first_hash)
4760 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
4763 * Now only update the 1st block of the old bucket. If we
4764 * just added a new empty bucket, there is no need to modify
4765 * it.
4767 if (start == count)
4768 goto out;
4770 xh = bucket_xh(s_bucket);
4771 memset(&xh->xh_entries[start], 0,
4772 sizeof(struct ocfs2_xattr_entry) * (count - start));
4773 xh->xh_count = cpu_to_le16(start);
4774 xh->xh_free_start = cpu_to_le16(name_offset);
4775 xh->xh_name_value_len = cpu_to_le16(name_value_len);
4777 ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
4779 out:
4780 ocfs2_xattr_bucket_free(s_bucket);
4781 ocfs2_xattr_bucket_free(t_bucket);
4783 return ret;
4787 * Copy xattr from one bucket to another bucket.
4789 * The caller must make sure that the journal transaction
4790 * has enough space for journaling.
4792 static int ocfs2_cp_xattr_bucket(struct inode *inode,
4793 handle_t *handle,
4794 u64 s_blkno,
4795 u64 t_blkno,
4796 int t_is_new)
4798 int ret;
4799 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
4801 BUG_ON(s_blkno == t_blkno);
4803 mlog(0, "cp bucket %llu to %llu, target is %d\n",
4804 (unsigned long long)s_blkno, (unsigned long long)t_blkno,
4805 t_is_new);
4807 s_bucket = ocfs2_xattr_bucket_new(inode);
4808 t_bucket = ocfs2_xattr_bucket_new(inode);
4809 if (!s_bucket || !t_bucket) {
4810 ret = -ENOMEM;
4811 mlog_errno(ret);
4812 goto out;
4815 ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
4816 if (ret)
4817 goto out;
4820 * Even if !t_is_new, we're overwriting t_bucket. Thus,
4821 * there's no need to read it.
4823 ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
4824 if (ret)
4825 goto out;
4828 * Hey, if we're overwriting t_bucket, what difference does
4829 * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new
4830 * cluster to fill, we came here from
4831 * ocfs2_mv_xattr_buckets(), and it is really new -
4832 * ACCESS_CREATE is required. But we also might have moved data
4833 * out of t_bucket before extending back into it.
4834 * ocfs2_add_new_xattr_bucket() can do this - its call to
4835 * ocfs2_add_new_xattr_cluster() may have created a new extent
4836 * and copied out the end of the old extent. Then it re-extends
4837 * the old extent back to create space for new xattrs. That's
4838 * how we get here, and the bucket isn't really new.
4840 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
4841 t_is_new ?
4842 OCFS2_JOURNAL_ACCESS_CREATE :
4843 OCFS2_JOURNAL_ACCESS_WRITE);
4844 if (ret)
4845 goto out;
4847 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4848 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
4850 out:
4851 ocfs2_xattr_bucket_free(t_bucket);
4852 ocfs2_xattr_bucket_free(s_bucket);
4854 return ret;
4858 * src_blk points to the start of an existing extent. last_blk points to
4859 * last cluster in that extent. to_blk points to a newly allocated
4860 * extent. We copy the buckets from the cluster at last_blk to the new
4861 * extent. If start_bucket is non-zero, we skip that many buckets before
4862 * we start copying. The new extent's xh_num_buckets gets set to the
4863 * number of buckets we copied. The old extent's xh_num_buckets shrinks
4864 * by the same amount.
4866 static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
4867 u64 src_blk, u64 last_blk, u64 to_blk,
4868 unsigned int start_bucket,
4869 u32 *first_hash)
4871 int i, ret, credits;
4872 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4873 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4874 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
4875 struct ocfs2_xattr_bucket *old_first, *new_first;
4877 mlog(0, "mv xattrs from cluster %llu to %llu\n",
4878 (unsigned long long)last_blk, (unsigned long long)to_blk);
4880 BUG_ON(start_bucket >= num_buckets);
4881 if (start_bucket) {
4882 num_buckets -= start_bucket;
4883 last_blk += (start_bucket * blks_per_bucket);
4886 /* The first bucket of the original extent */
4887 old_first = ocfs2_xattr_bucket_new(inode);
4888 /* The first bucket of the new extent */
4889 new_first = ocfs2_xattr_bucket_new(inode);
4890 if (!old_first || !new_first) {
4891 ret = -ENOMEM;
4892 mlog_errno(ret);
4893 goto out;
4896 ret = ocfs2_read_xattr_bucket(old_first, src_blk);
4897 if (ret) {
4898 mlog_errno(ret);
4899 goto out;
4903 * We need to update the first bucket of the old extent and all
4904 * the buckets going to the new extent.
4906 credits = ((num_buckets + 1) * blks_per_bucket) +
4907 handle->h_buffer_credits;
4908 ret = ocfs2_extend_trans(handle, credits);
4909 if (ret) {
4910 mlog_errno(ret);
4911 goto out;
4914 ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
4915 OCFS2_JOURNAL_ACCESS_WRITE);
4916 if (ret) {
4917 mlog_errno(ret);
4918 goto out;
4921 for (i = 0; i < num_buckets; i++) {
4922 ret = ocfs2_cp_xattr_bucket(inode, handle,
4923 last_blk + (i * blks_per_bucket),
4924 to_blk + (i * blks_per_bucket),
4926 if (ret) {
4927 mlog_errno(ret);
4928 goto out;
4933 * Get the new bucket ready before we dirty anything
4934 * (This actually shouldn't fail, because we already dirtied
4935 * it once in ocfs2_cp_xattr_bucket()).
4937 ret = ocfs2_read_xattr_bucket(new_first, to_blk);
4938 if (ret) {
4939 mlog_errno(ret);
4940 goto out;
4942 ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
4943 OCFS2_JOURNAL_ACCESS_WRITE);
4944 if (ret) {
4945 mlog_errno(ret);
4946 goto out;
4949 /* Now update the headers */
4950 le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets);
4951 ocfs2_xattr_bucket_journal_dirty(handle, old_first);
4953 bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets);
4954 ocfs2_xattr_bucket_journal_dirty(handle, new_first);
4956 if (first_hash)
4957 *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash);
4959 out:
4960 ocfs2_xattr_bucket_free(new_first);
4961 ocfs2_xattr_bucket_free(old_first);
4962 return ret;
4966 * Move some xattrs in this cluster to the new cluster.
4967 * This function should only be called when bucket size == cluster size.
4968 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
4970 static int ocfs2_divide_xattr_cluster(struct inode *inode,
4971 handle_t *handle,
4972 u64 prev_blk,
4973 u64 new_blk,
4974 u32 *first_hash)
4976 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4977 int ret, credits = 2 * blk_per_bucket + handle->h_buffer_credits;
4979 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
4981 ret = ocfs2_extend_trans(handle, credits);
4982 if (ret) {
4983 mlog_errno(ret);
4984 return ret;
4987 /* Move half of the xattr in start_blk to the next bucket. */
4988 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
4989 new_blk, first_hash, 1);
4993 * Move some xattrs from the old cluster to the new one since they are not
4994 * contiguous in ocfs2 xattr tree.
4996 * new_blk starts a new separate cluster, and we will move some xattrs from
4997 * prev_blk to it. v_start will be set as the first name hash value in this
4998 * new cluster so that it can be used as e_cpos during tree insertion and
4999 * don't collide with our original b-tree operations. first_bh and header_bh
5000 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
5001 * to extend the insert bucket.
5003 * The problem is how much xattr should we move to the new one and when should
5004 * we update first_bh and header_bh?
5005 * 1. If cluster size > bucket size, that means the previous cluster has more
5006 * than 1 bucket, so just move half nums of bucket into the new cluster and
5007 * update the first_bh and header_bh if the insert bucket has been moved
5008 * to the new cluster.
5009 * 2. If cluster_size == bucket_size:
5010 * a) If the previous extent rec has more than one cluster and the insert
5011 * place isn't in the last cluster, copy the entire last cluster to the
5012 * new one. This time, we don't need to upate the first_bh and header_bh
5013 * since they will not be moved into the new cluster.
5014 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
5015 * the new one. And we set the extend flag to zero if the insert place is
5016 * moved into the new allocated cluster since no extend is needed.
5018 static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
5019 handle_t *handle,
5020 struct ocfs2_xattr_bucket *first,
5021 struct ocfs2_xattr_bucket *target,
5022 u64 new_blk,
5023 u32 prev_clusters,
5024 u32 *v_start,
5025 int *extend)
5027 int ret;
5029 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
5030 (unsigned long long)bucket_blkno(first), prev_clusters,
5031 (unsigned long long)new_blk);
5033 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) {
5034 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
5035 handle,
5036 first, target,
5037 new_blk,
5038 prev_clusters,
5039 v_start);
5040 if (ret)
5041 mlog_errno(ret);
5042 } else {
5043 /* The start of the last cluster in the first extent */
5044 u64 last_blk = bucket_blkno(first) +
5045 ((prev_clusters - 1) *
5046 ocfs2_clusters_to_blocks(inode->i_sb, 1));
5048 if (prev_clusters > 1 && bucket_blkno(target) != last_blk) {
5049 ret = ocfs2_mv_xattr_buckets(inode, handle,
5050 bucket_blkno(first),
5051 last_blk, new_blk, 0,
5052 v_start);
5053 if (ret)
5054 mlog_errno(ret);
5055 } else {
5056 ret = ocfs2_divide_xattr_cluster(inode, handle,
5057 last_blk, new_blk,
5058 v_start);
5059 if (ret)
5060 mlog_errno(ret);
5062 if ((bucket_blkno(target) == last_blk) && extend)
5063 *extend = 0;
5067 return ret;
5071 * Add a new cluster for xattr storage.
5073 * If the new cluster is contiguous with the previous one, it will be
5074 * appended to the same extent record, and num_clusters will be updated.
5075 * If not, we will insert a new extent for it and move some xattrs in
5076 * the last cluster into the new allocated one.
5077 * We also need to limit the maximum size of a btree leaf, otherwise we'll
5078 * lose the benefits of hashing because we'll have to search large leaves.
5079 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
5080 * if it's bigger).
5082 * first_bh is the first block of the previous extent rec and header_bh
5083 * indicates the bucket we will insert the new xattrs. They will be updated
5084 * when the header_bh is moved into the new cluster.
5086 static int ocfs2_add_new_xattr_cluster(struct inode *inode,
5087 struct buffer_head *root_bh,
5088 struct ocfs2_xattr_bucket *first,
5089 struct ocfs2_xattr_bucket *target,
5090 u32 *num_clusters,
5091 u32 prev_cpos,
5092 int *extend,
5093 struct ocfs2_xattr_set_ctxt *ctxt)
5095 int ret;
5096 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
5097 u32 prev_clusters = *num_clusters;
5098 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
5099 u64 block;
5100 handle_t *handle = ctxt->handle;
5101 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5102 struct ocfs2_extent_tree et;
5104 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
5105 "previous xattr blkno = %llu\n",
5106 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5107 prev_cpos, (unsigned long long)bucket_blkno(first));
5109 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
5111 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
5112 OCFS2_JOURNAL_ACCESS_WRITE);
5113 if (ret < 0) {
5114 mlog_errno(ret);
5115 goto leave;
5118 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, 1,
5119 clusters_to_add, &bit_off, &num_bits);
5120 if (ret < 0) {
5121 if (ret != -ENOSPC)
5122 mlog_errno(ret);
5123 goto leave;
5126 BUG_ON(num_bits > clusters_to_add);
5128 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
5129 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
5130 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
5132 if (bucket_blkno(first) + (prev_clusters * bpc) == block &&
5133 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
5134 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
5136 * If this cluster is contiguous with the old one and
5137 * adding this new cluster, we don't surpass the limit of
5138 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
5139 * initialized and used like other buckets in the previous
5140 * cluster.
5141 * So add it as a contiguous one. The caller will handle
5142 * its init process.
5144 v_start = prev_cpos + prev_clusters;
5145 *num_clusters = prev_clusters + num_bits;
5146 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
5147 num_bits);
5148 } else {
5149 ret = ocfs2_adjust_xattr_cross_cluster(inode,
5150 handle,
5151 first,
5152 target,
5153 block,
5154 prev_clusters,
5155 &v_start,
5156 extend);
5157 if (ret) {
5158 mlog_errno(ret);
5159 goto leave;
5163 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
5164 num_bits, (unsigned long long)block, v_start);
5165 ret = ocfs2_insert_extent(handle, &et, v_start, block,
5166 num_bits, 0, ctxt->meta_ac);
5167 if (ret < 0) {
5168 mlog_errno(ret);
5169 goto leave;
5172 ret = ocfs2_journal_dirty(handle, root_bh);
5173 if (ret < 0)
5174 mlog_errno(ret);
5176 leave:
5177 return ret;
5181 * We are given an extent. 'first' is the bucket at the very front of
5182 * the extent. The extent has space for an additional bucket past
5183 * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
5184 * of the target bucket. We wish to shift every bucket past the target
5185 * down one, filling in that additional space. When we get back to the
5186 * target, we split the target between itself and the now-empty bucket
5187 * at target+1 (aka, target_blkno + blks_per_bucket).
5189 static int ocfs2_extend_xattr_bucket(struct inode *inode,
5190 handle_t *handle,
5191 struct ocfs2_xattr_bucket *first,
5192 u64 target_blk,
5193 u32 num_clusters)
5195 int ret, credits;
5196 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5197 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
5198 u64 end_blk;
5199 u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets);
5201 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
5202 "from %llu, len = %u\n", (unsigned long long)target_blk,
5203 (unsigned long long)bucket_blkno(first), num_clusters);
5205 /* The extent must have room for an additional bucket */
5206 BUG_ON(new_bucket >=
5207 (num_clusters * ocfs2_xattr_buckets_per_cluster(osb)));
5209 /* end_blk points to the last existing bucket */
5210 end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket);
5213 * end_blk is the start of the last existing bucket.
5214 * Thus, (end_blk - target_blk) covers the target bucket and
5215 * every bucket after it up to, but not including, the last
5216 * existing bucket. Then we add the last existing bucket, the
5217 * new bucket, and the first bucket (3 * blk_per_bucket).
5219 credits = (end_blk - target_blk) + (3 * blk_per_bucket) +
5220 handle->h_buffer_credits;
5221 ret = ocfs2_extend_trans(handle, credits);
5222 if (ret) {
5223 mlog_errno(ret);
5224 goto out;
5227 ret = ocfs2_xattr_bucket_journal_access(handle, first,
5228 OCFS2_JOURNAL_ACCESS_WRITE);
5229 if (ret) {
5230 mlog_errno(ret);
5231 goto out;
5234 while (end_blk != target_blk) {
5235 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
5236 end_blk + blk_per_bucket, 0);
5237 if (ret)
5238 goto out;
5239 end_blk -= blk_per_bucket;
5242 /* Move half of the xattr in target_blkno to the next bucket. */
5243 ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk,
5244 target_blk + blk_per_bucket, NULL, 0);
5246 le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1);
5247 ocfs2_xattr_bucket_journal_dirty(handle, first);
5249 out:
5250 return ret;
5254 * Add new xattr bucket in an extent record and adjust the buckets
5255 * accordingly. xb_bh is the ocfs2_xattr_block, and target is the
5256 * bucket we want to insert into.
5258 * In the easy case, we will move all the buckets after target down by
5259 * one. Half of target's xattrs will be moved to the next bucket.
5261 * If current cluster is full, we'll allocate a new one. This may not
5262 * be contiguous. The underlying calls will make sure that there is
5263 * space for the insert, shifting buckets around if necessary.
5264 * 'target' may be moved by those calls.
5266 static int ocfs2_add_new_xattr_bucket(struct inode *inode,
5267 struct buffer_head *xb_bh,
5268 struct ocfs2_xattr_bucket *target,
5269 struct ocfs2_xattr_set_ctxt *ctxt)
5271 struct ocfs2_xattr_block *xb =
5272 (struct ocfs2_xattr_block *)xb_bh->b_data;
5273 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
5274 struct ocfs2_extent_list *el = &xb_root->xt_list;
5275 u32 name_hash =
5276 le32_to_cpu(bucket_xh(target)->xh_entries[0].xe_name_hash);
5277 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5278 int ret, num_buckets, extend = 1;
5279 u64 p_blkno;
5280 u32 e_cpos, num_clusters;
5281 /* The bucket at the front of the extent */
5282 struct ocfs2_xattr_bucket *first;
5284 mlog(0, "Add new xattr bucket starting from %llu\n",
5285 (unsigned long long)bucket_blkno(target));
5287 /* The first bucket of the original extent */
5288 first = ocfs2_xattr_bucket_new(inode);
5289 if (!first) {
5290 ret = -ENOMEM;
5291 mlog_errno(ret);
5292 goto out;
5295 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
5296 &num_clusters, el);
5297 if (ret) {
5298 mlog_errno(ret);
5299 goto out;
5302 ret = ocfs2_read_xattr_bucket(first, p_blkno);
5303 if (ret) {
5304 mlog_errno(ret);
5305 goto out;
5308 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
5309 if (num_buckets == le16_to_cpu(bucket_xh(first)->xh_num_buckets)) {
5311 * This can move first+target if the target bucket moves
5312 * to the new extent.
5314 ret = ocfs2_add_new_xattr_cluster(inode,
5315 xb_bh,
5316 first,
5317 target,
5318 &num_clusters,
5319 e_cpos,
5320 &extend,
5321 ctxt);
5322 if (ret) {
5323 mlog_errno(ret);
5324 goto out;
5328 if (extend) {
5329 ret = ocfs2_extend_xattr_bucket(inode,
5330 ctxt->handle,
5331 first,
5332 bucket_blkno(target),
5333 num_clusters);
5334 if (ret)
5335 mlog_errno(ret);
5338 out:
5339 ocfs2_xattr_bucket_free(first);
5341 return ret;
5344 static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
5345 struct ocfs2_xattr_bucket *bucket,
5346 int offs)
5348 int block_off = offs >> inode->i_sb->s_blocksize_bits;
5350 offs = offs % inode->i_sb->s_blocksize;
5351 return bucket_block(bucket, block_off) + offs;
5355 * Truncate the specified xe_off entry in xattr bucket.
5356 * bucket is indicated by header_bh and len is the new length.
5357 * Both the ocfs2_xattr_value_root and the entry will be updated here.
5359 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
5361 static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
5362 struct ocfs2_xattr_bucket *bucket,
5363 int xe_off,
5364 int len,
5365 struct ocfs2_xattr_set_ctxt *ctxt)
5367 int ret, offset;
5368 u64 value_blk;
5369 struct ocfs2_xattr_entry *xe;
5370 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5371 size_t blocksize = inode->i_sb->s_blocksize;
5372 struct ocfs2_xattr_value_buf vb = {
5373 .vb_access = ocfs2_journal_access,
5376 xe = &xh->xh_entries[xe_off];
5378 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
5380 offset = le16_to_cpu(xe->xe_name_offset) +
5381 OCFS2_XATTR_SIZE(xe->xe_name_len);
5383 value_blk = offset / blocksize;
5385 /* We don't allow ocfs2_xattr_value to be stored in different block. */
5386 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
5388 vb.vb_bh = bucket->bu_bhs[value_blk];
5389 BUG_ON(!vb.vb_bh);
5391 vb.vb_xv = (struct ocfs2_xattr_value_root *)
5392 (vb.vb_bh->b_data + offset % blocksize);
5395 * From here on out we have to dirty the bucket. The generic
5396 * value calls only modify one of the bucket's bhs, but we need
5397 * to send the bucket at once. So if they error, they *could* have
5398 * modified something. We have to assume they did, and dirty
5399 * the whole bucket. This leaves us in a consistent state.
5401 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
5402 xe_off, (unsigned long long)bucket_blkno(bucket), len);
5403 ret = ocfs2_xattr_value_truncate(inode, &vb, len, ctxt);
5404 if (ret) {
5405 mlog_errno(ret);
5406 goto out;
5409 ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
5410 OCFS2_JOURNAL_ACCESS_WRITE);
5411 if (ret) {
5412 mlog_errno(ret);
5413 goto out;
5416 xe->xe_value_size = cpu_to_le64(len);
5418 ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket);
5420 out:
5421 return ret;
5424 static int ocfs2_rm_xattr_cluster(struct inode *inode,
5425 struct buffer_head *root_bh,
5426 u64 blkno,
5427 u32 cpos,
5428 u32 len,
5429 void *para)
5431 int ret;
5432 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5433 struct inode *tl_inode = osb->osb_tl_inode;
5434 handle_t *handle;
5435 struct ocfs2_xattr_block *xb =
5436 (struct ocfs2_xattr_block *)root_bh->b_data;
5437 struct ocfs2_alloc_context *meta_ac = NULL;
5438 struct ocfs2_cached_dealloc_ctxt dealloc;
5439 struct ocfs2_extent_tree et;
5441 ret = ocfs2_iterate_xattr_buckets(inode, blkno, len,
5442 ocfs2_delete_xattr_in_bucket, para);
5443 if (ret) {
5444 mlog_errno(ret);
5445 return ret;
5448 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
5450 ocfs2_init_dealloc_ctxt(&dealloc);
5452 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
5453 cpos, len, (unsigned long long)blkno);
5455 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode), blkno,
5456 len);
5458 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
5459 if (ret) {
5460 mlog_errno(ret);
5461 return ret;
5464 mutex_lock(&tl_inode->i_mutex);
5466 if (ocfs2_truncate_log_needs_flush(osb)) {
5467 ret = __ocfs2_flush_truncate_log(osb);
5468 if (ret < 0) {
5469 mlog_errno(ret);
5470 goto out;
5474 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
5475 if (IS_ERR(handle)) {
5476 ret = -ENOMEM;
5477 mlog_errno(ret);
5478 goto out;
5481 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
5482 OCFS2_JOURNAL_ACCESS_WRITE);
5483 if (ret) {
5484 mlog_errno(ret);
5485 goto out_commit;
5488 ret = ocfs2_remove_extent(handle, &et, cpos, len, meta_ac,
5489 &dealloc);
5490 if (ret) {
5491 mlog_errno(ret);
5492 goto out_commit;
5495 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
5497 ret = ocfs2_journal_dirty(handle, root_bh);
5498 if (ret) {
5499 mlog_errno(ret);
5500 goto out_commit;
5503 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
5504 if (ret)
5505 mlog_errno(ret);
5507 out_commit:
5508 ocfs2_commit_trans(osb, handle);
5509 out:
5510 ocfs2_schedule_truncate_log_flush(osb, 1);
5512 mutex_unlock(&tl_inode->i_mutex);
5514 if (meta_ac)
5515 ocfs2_free_alloc_context(meta_ac);
5517 ocfs2_run_deallocs(osb, &dealloc);
5519 return ret;
5523 * check whether the xattr bucket is filled up with the same hash value.
5524 * If we want to insert the xattr with the same hash, return -ENOSPC.
5525 * If we want to insert a xattr with different hash value, go ahead
5526 * and ocfs2_divide_xattr_bucket will handle this.
5528 static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
5529 struct ocfs2_xattr_bucket *bucket,
5530 const char *name)
5532 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5533 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
5535 if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
5536 return 0;
5538 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
5539 xh->xh_entries[0].xe_name_hash) {
5540 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
5541 "hash = %u\n",
5542 (unsigned long long)bucket_blkno(bucket),
5543 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
5544 return -ENOSPC;
5547 return 0;
5551 * Try to set the entry in the current bucket. If we fail, the caller
5552 * will handle getting us another bucket.
5554 static int ocfs2_xattr_set_entry_bucket(struct inode *inode,
5555 struct ocfs2_xattr_info *xi,
5556 struct ocfs2_xattr_search *xs,
5557 struct ocfs2_xattr_set_ctxt *ctxt)
5559 int ret;
5560 struct ocfs2_xa_loc loc;
5562 mlog_entry("Set xattr %s in xattr bucket\n", xi->xi_name);
5564 ocfs2_init_xattr_bucket_xa_loc(&loc, xs->bucket,
5565 xs->not_found ? NULL : xs->here);
5566 ret = ocfs2_xa_set(&loc, xi, ctxt);
5567 if (!ret) {
5568 xs->here = loc.xl_entry;
5569 goto out;
5571 if (ret != -ENOSPC) {
5572 mlog_errno(ret);
5573 goto out;
5576 /* Ok, we need space. Let's try defragmenting the bucket. */
5577 ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
5578 xs->bucket);
5579 if (ret) {
5580 mlog_errno(ret);
5581 goto out;
5584 ret = ocfs2_xa_set(&loc, xi, ctxt);
5585 if (!ret) {
5586 xs->here = loc.xl_entry;
5587 goto out;
5589 if (ret != -ENOSPC)
5590 mlog_errno(ret);
5593 out:
5594 mlog_exit(ret);
5595 return ret;
5598 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
5599 struct ocfs2_xattr_info *xi,
5600 struct ocfs2_xattr_search *xs,
5601 struct ocfs2_xattr_set_ctxt *ctxt)
5603 int ret;
5605 mlog_entry("Set xattr %s in xattr index block\n", xi->xi_name);
5607 ret = ocfs2_xattr_set_entry_bucket(inode, xi, xs, ctxt);
5608 if (!ret)
5609 goto out;
5610 if (ret != -ENOSPC) {
5611 mlog_errno(ret);
5612 goto out;
5615 /* Ack, need more space. Let's try to get another bucket! */
5618 * We do not allow for overlapping ranges between buckets. And
5619 * the maximum number of collisions we will allow for then is
5620 * one bucket's worth, so check it here whether we need to
5621 * add a new bucket for the insert.
5623 ret = ocfs2_check_xattr_bucket_collision(inode,
5624 xs->bucket,
5625 xi->xi_name);
5626 if (ret) {
5627 mlog_errno(ret);
5628 goto out;
5631 ret = ocfs2_add_new_xattr_bucket(inode,
5632 xs->xattr_bh,
5633 xs->bucket,
5634 ctxt);
5635 if (ret) {
5636 mlog_errno(ret);
5637 goto out;
5641 * ocfs2_add_new_xattr_bucket() will have updated
5642 * xs->bucket if it moved, but it will not have updated
5643 * any of the other search fields. Thus, we drop it and
5644 * re-search. Everything should be cached, so it'll be
5645 * quick.
5647 ocfs2_xattr_bucket_relse(xs->bucket);
5648 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
5649 xi->xi_name_index,
5650 xi->xi_name, xs);
5651 if (ret && ret != -ENODATA)
5652 goto out;
5653 xs->not_found = ret;
5655 /* Ok, we have a new bucket, let's try again */
5656 ret = ocfs2_xattr_set_entry_bucket(inode, xi, xs, ctxt);
5657 if (ret && (ret != -ENOSPC))
5658 mlog_errno(ret);
5660 out:
5661 mlog_exit(ret);
5662 return ret;
5665 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
5666 struct ocfs2_xattr_bucket *bucket,
5667 void *para)
5669 int ret = 0, ref_credits;
5670 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5671 u16 i;
5672 struct ocfs2_xattr_entry *xe;
5673 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5674 struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
5675 int credits = ocfs2_remove_extent_credits(osb->sb) +
5676 ocfs2_blocks_per_xattr_bucket(inode->i_sb);
5677 struct ocfs2_xattr_value_root *xv;
5678 struct ocfs2_rm_xattr_bucket_para *args =
5679 (struct ocfs2_rm_xattr_bucket_para *)para;
5681 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
5683 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
5684 xe = &xh->xh_entries[i];
5685 if (ocfs2_xattr_is_local(xe))
5686 continue;
5688 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket,
5689 i, &xv, NULL);
5691 ret = ocfs2_lock_xattr_remove_allocators(inode, xv,
5692 args->ref_ci,
5693 args->ref_root_bh,
5694 &ctxt.meta_ac,
5695 &ref_credits);
5697 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
5698 if (IS_ERR(ctxt.handle)) {
5699 ret = PTR_ERR(ctxt.handle);
5700 mlog_errno(ret);
5701 break;
5704 ret = ocfs2_xattr_bucket_value_truncate(inode, bucket,
5705 i, 0, &ctxt);
5707 ocfs2_commit_trans(osb, ctxt.handle);
5708 if (ctxt.meta_ac) {
5709 ocfs2_free_alloc_context(ctxt.meta_ac);
5710 ctxt.meta_ac = NULL;
5712 if (ret) {
5713 mlog_errno(ret);
5714 break;
5718 if (ctxt.meta_ac)
5719 ocfs2_free_alloc_context(ctxt.meta_ac);
5720 ocfs2_schedule_truncate_log_flush(osb, 1);
5721 ocfs2_run_deallocs(osb, &ctxt.dealloc);
5722 return ret;
5726 * Whenever we modify a xattr value root in the bucket(e.g, CoW
5727 * or change the extent record flag), we need to recalculate
5728 * the metaecc for the whole bucket. So it is done here.
5730 * Note:
5731 * We have to give the extra credits for the caller.
5733 static int ocfs2_xattr_bucket_post_refcount(struct inode *inode,
5734 handle_t *handle,
5735 void *para)
5737 int ret;
5738 struct ocfs2_xattr_bucket *bucket =
5739 (struct ocfs2_xattr_bucket *)para;
5741 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
5742 OCFS2_JOURNAL_ACCESS_WRITE);
5743 if (ret) {
5744 mlog_errno(ret);
5745 return ret;
5748 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
5750 return 0;
5754 * Special action we need if the xattr value is refcounted.
5756 * 1. If the xattr is refcounted, lock the tree.
5757 * 2. CoW the xattr if we are setting the new value and the value
5758 * will be stored outside.
5759 * 3. In other case, decrease_refcount will work for us, so just
5760 * lock the refcount tree, calculate the meta and credits is OK.
5762 * We have to do CoW before ocfs2_init_xattr_set_ctxt since
5763 * currently CoW is a completed transaction, while this function
5764 * will also lock the allocators and let us deadlock. So we will
5765 * CoW the whole xattr value.
5767 static int ocfs2_prepare_refcount_xattr(struct inode *inode,
5768 struct ocfs2_dinode *di,
5769 struct ocfs2_xattr_info *xi,
5770 struct ocfs2_xattr_search *xis,
5771 struct ocfs2_xattr_search *xbs,
5772 struct ocfs2_refcount_tree **ref_tree,
5773 int *meta_add,
5774 int *credits)
5776 int ret = 0;
5777 struct ocfs2_xattr_block *xb;
5778 struct ocfs2_xattr_entry *xe;
5779 char *base;
5780 u32 p_cluster, num_clusters;
5781 unsigned int ext_flags;
5782 int name_offset, name_len;
5783 struct ocfs2_xattr_value_buf vb;
5784 struct ocfs2_xattr_bucket *bucket = NULL;
5785 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5786 struct ocfs2_post_refcount refcount;
5787 struct ocfs2_post_refcount *p = NULL;
5788 struct buffer_head *ref_root_bh = NULL;
5790 if (!xis->not_found) {
5791 xe = xis->here;
5792 name_offset = le16_to_cpu(xe->xe_name_offset);
5793 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5794 base = xis->base;
5795 vb.vb_bh = xis->inode_bh;
5796 vb.vb_access = ocfs2_journal_access_di;
5797 } else {
5798 int i, block_off = 0;
5799 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
5800 xe = xbs->here;
5801 name_offset = le16_to_cpu(xe->xe_name_offset);
5802 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5803 i = xbs->here - xbs->header->xh_entries;
5805 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
5806 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
5807 bucket_xh(xbs->bucket),
5808 i, &block_off,
5809 &name_offset);
5810 if (ret) {
5811 mlog_errno(ret);
5812 goto out;
5814 base = bucket_block(xbs->bucket, block_off);
5815 vb.vb_bh = xbs->bucket->bu_bhs[block_off];
5816 vb.vb_access = ocfs2_journal_access;
5818 if (ocfs2_meta_ecc(osb)) {
5819 /*create parameters for ocfs2_post_refcount. */
5820 bucket = xbs->bucket;
5821 refcount.credits = bucket->bu_blocks;
5822 refcount.para = bucket;
5823 refcount.func =
5824 ocfs2_xattr_bucket_post_refcount;
5825 p = &refcount;
5827 } else {
5828 base = xbs->base;
5829 vb.vb_bh = xbs->xattr_bh;
5830 vb.vb_access = ocfs2_journal_access_xb;
5834 if (ocfs2_xattr_is_local(xe))
5835 goto out;
5837 vb.vb_xv = (struct ocfs2_xattr_value_root *)
5838 (base + name_offset + name_len);
5840 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
5841 &num_clusters, &vb.vb_xv->xr_list,
5842 &ext_flags);
5843 if (ret) {
5844 mlog_errno(ret);
5845 goto out;
5849 * We just need to check the 1st extent record, since we always
5850 * CoW the whole xattr. So there shouldn't be a xattr with
5851 * some REFCOUNT extent recs after the 1st one.
5853 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
5854 goto out;
5856 ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
5857 1, ref_tree, &ref_root_bh);
5858 if (ret) {
5859 mlog_errno(ret);
5860 goto out;
5864 * If we are deleting the xattr or the new size will be stored inside,
5865 * cool, leave it there, the xattr truncate process will remove them
5866 * for us(it still needs the refcount tree lock and the meta, credits).
5867 * And the worse case is that every cluster truncate will split the
5868 * refcount tree, and make the original extent become 3. So we will need
5869 * 2 * cluster more extent recs at most.
5871 if (!xi->xi_value || xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE) {
5873 ret = ocfs2_refcounted_xattr_delete_need(inode,
5874 &(*ref_tree)->rf_ci,
5875 ref_root_bh, vb.vb_xv,
5876 meta_add, credits);
5877 if (ret)
5878 mlog_errno(ret);
5879 goto out;
5882 ret = ocfs2_refcount_cow_xattr(inode, di, &vb,
5883 *ref_tree, ref_root_bh, 0,
5884 le32_to_cpu(vb.vb_xv->xr_clusters), p);
5885 if (ret)
5886 mlog_errno(ret);
5888 out:
5889 brelse(ref_root_bh);
5890 return ret;
5894 * Add the REFCOUNTED flags for all the extent rec in ocfs2_xattr_value_root.
5895 * The physical clusters will be added to refcount tree.
5897 static int ocfs2_xattr_value_attach_refcount(struct inode *inode,
5898 struct ocfs2_xattr_value_root *xv,
5899 struct ocfs2_extent_tree *value_et,
5900 struct ocfs2_caching_info *ref_ci,
5901 struct buffer_head *ref_root_bh,
5902 struct ocfs2_cached_dealloc_ctxt *dealloc,
5903 struct ocfs2_post_refcount *refcount)
5905 int ret = 0;
5906 u32 clusters = le32_to_cpu(xv->xr_clusters);
5907 u32 cpos, p_cluster, num_clusters;
5908 struct ocfs2_extent_list *el = &xv->xr_list;
5909 unsigned int ext_flags;
5911 cpos = 0;
5912 while (cpos < clusters) {
5913 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
5914 &num_clusters, el, &ext_flags);
5916 cpos += num_clusters;
5917 if ((ext_flags & OCFS2_EXT_REFCOUNTED))
5918 continue;
5920 BUG_ON(!p_cluster);
5922 ret = ocfs2_add_refcount_flag(inode, value_et,
5923 ref_ci, ref_root_bh,
5924 cpos - num_clusters,
5925 p_cluster, num_clusters,
5926 dealloc, refcount);
5927 if (ret) {
5928 mlog_errno(ret);
5929 break;
5933 return ret;
5937 * Given a normal ocfs2_xattr_header, refcount all the entries which
5938 * have value stored outside.
5939 * Used for xattrs stored in inode and ocfs2_xattr_block.
5941 static int ocfs2_xattr_attach_refcount_normal(struct inode *inode,
5942 struct ocfs2_xattr_value_buf *vb,
5943 struct ocfs2_xattr_header *header,
5944 struct ocfs2_caching_info *ref_ci,
5945 struct buffer_head *ref_root_bh,
5946 struct ocfs2_cached_dealloc_ctxt *dealloc)
5949 struct ocfs2_xattr_entry *xe;
5950 struct ocfs2_xattr_value_root *xv;
5951 struct ocfs2_extent_tree et;
5952 int i, ret = 0;
5954 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
5955 xe = &header->xh_entries[i];
5957 if (ocfs2_xattr_is_local(xe))
5958 continue;
5960 xv = (struct ocfs2_xattr_value_root *)((void *)header +
5961 le16_to_cpu(xe->xe_name_offset) +
5962 OCFS2_XATTR_SIZE(xe->xe_name_len));
5964 vb->vb_xv = xv;
5965 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
5967 ret = ocfs2_xattr_value_attach_refcount(inode, xv, &et,
5968 ref_ci, ref_root_bh,
5969 dealloc, NULL);
5970 if (ret) {
5971 mlog_errno(ret);
5972 break;
5976 return ret;
5979 static int ocfs2_xattr_inline_attach_refcount(struct inode *inode,
5980 struct buffer_head *fe_bh,
5981 struct ocfs2_caching_info *ref_ci,
5982 struct buffer_head *ref_root_bh,
5983 struct ocfs2_cached_dealloc_ctxt *dealloc)
5985 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
5986 struct ocfs2_xattr_header *header = (struct ocfs2_xattr_header *)
5987 (fe_bh->b_data + inode->i_sb->s_blocksize -
5988 le16_to_cpu(di->i_xattr_inline_size));
5989 struct ocfs2_xattr_value_buf vb = {
5990 .vb_bh = fe_bh,
5991 .vb_access = ocfs2_journal_access_di,
5994 return ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
5995 ref_ci, ref_root_bh, dealloc);
5998 struct ocfs2_xattr_tree_value_refcount_para {
5999 struct ocfs2_caching_info *ref_ci;
6000 struct buffer_head *ref_root_bh;
6001 struct ocfs2_cached_dealloc_ctxt *dealloc;
6004 static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
6005 struct ocfs2_xattr_bucket *bucket,
6006 int offset,
6007 struct ocfs2_xattr_value_root **xv,
6008 struct buffer_head **bh)
6010 int ret, block_off, name_offset;
6011 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
6012 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
6013 void *base;
6015 ret = ocfs2_xattr_bucket_get_name_value(sb,
6016 bucket_xh(bucket),
6017 offset,
6018 &block_off,
6019 &name_offset);
6020 if (ret) {
6021 mlog_errno(ret);
6022 goto out;
6025 base = bucket_block(bucket, block_off);
6027 *xv = (struct ocfs2_xattr_value_root *)(base + name_offset +
6028 OCFS2_XATTR_SIZE(xe->xe_name_len));
6030 if (bh)
6031 *bh = bucket->bu_bhs[block_off];
6032 out:
6033 return ret;
6037 * For a given xattr bucket, refcount all the entries which
6038 * have value stored outside.
6040 static int ocfs2_xattr_bucket_value_refcount(struct inode *inode,
6041 struct ocfs2_xattr_bucket *bucket,
6042 void *para)
6044 int i, ret = 0;
6045 struct ocfs2_extent_tree et;
6046 struct ocfs2_xattr_tree_value_refcount_para *ref =
6047 (struct ocfs2_xattr_tree_value_refcount_para *)para;
6048 struct ocfs2_xattr_header *xh =
6049 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6050 struct ocfs2_xattr_entry *xe;
6051 struct ocfs2_xattr_value_buf vb = {
6052 .vb_access = ocfs2_journal_access,
6054 struct ocfs2_post_refcount refcount = {
6055 .credits = bucket->bu_blocks,
6056 .para = bucket,
6057 .func = ocfs2_xattr_bucket_post_refcount,
6059 struct ocfs2_post_refcount *p = NULL;
6061 /* We only need post_refcount if we support metaecc. */
6062 if (ocfs2_meta_ecc(OCFS2_SB(inode->i_sb)))
6063 p = &refcount;
6065 mlog(0, "refcount bucket %llu, count = %u\n",
6066 (unsigned long long)bucket_blkno(bucket),
6067 le16_to_cpu(xh->xh_count));
6068 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6069 xe = &xh->xh_entries[i];
6071 if (ocfs2_xattr_is_local(xe))
6072 continue;
6074 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket, i,
6075 &vb.vb_xv, &vb.vb_bh);
6076 if (ret) {
6077 mlog_errno(ret);
6078 break;
6081 ocfs2_init_xattr_value_extent_tree(&et,
6082 INODE_CACHE(inode), &vb);
6084 ret = ocfs2_xattr_value_attach_refcount(inode, vb.vb_xv,
6085 &et, ref->ref_ci,
6086 ref->ref_root_bh,
6087 ref->dealloc, p);
6088 if (ret) {
6089 mlog_errno(ret);
6090 break;
6094 return ret;
6098 static int ocfs2_refcount_xattr_tree_rec(struct inode *inode,
6099 struct buffer_head *root_bh,
6100 u64 blkno, u32 cpos, u32 len, void *para)
6102 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
6103 ocfs2_xattr_bucket_value_refcount,
6104 para);
6107 static int ocfs2_xattr_block_attach_refcount(struct inode *inode,
6108 struct buffer_head *blk_bh,
6109 struct ocfs2_caching_info *ref_ci,
6110 struct buffer_head *ref_root_bh,
6111 struct ocfs2_cached_dealloc_ctxt *dealloc)
6113 int ret = 0;
6114 struct ocfs2_xattr_block *xb =
6115 (struct ocfs2_xattr_block *)blk_bh->b_data;
6117 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
6118 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
6119 struct ocfs2_xattr_value_buf vb = {
6120 .vb_bh = blk_bh,
6121 .vb_access = ocfs2_journal_access_xb,
6124 ret = ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
6125 ref_ci, ref_root_bh,
6126 dealloc);
6127 } else {
6128 struct ocfs2_xattr_tree_value_refcount_para para = {
6129 .ref_ci = ref_ci,
6130 .ref_root_bh = ref_root_bh,
6131 .dealloc = dealloc,
6134 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
6135 ocfs2_refcount_xattr_tree_rec,
6136 &para);
6139 return ret;
6142 int ocfs2_xattr_attach_refcount_tree(struct inode *inode,
6143 struct buffer_head *fe_bh,
6144 struct ocfs2_caching_info *ref_ci,
6145 struct buffer_head *ref_root_bh,
6146 struct ocfs2_cached_dealloc_ctxt *dealloc)
6148 int ret = 0;
6149 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6150 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
6151 struct buffer_head *blk_bh = NULL;
6153 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
6154 ret = ocfs2_xattr_inline_attach_refcount(inode, fe_bh,
6155 ref_ci, ref_root_bh,
6156 dealloc);
6157 if (ret) {
6158 mlog_errno(ret);
6159 goto out;
6163 if (!di->i_xattr_loc)
6164 goto out;
6166 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
6167 &blk_bh);
6168 if (ret < 0) {
6169 mlog_errno(ret);
6170 goto out;
6173 ret = ocfs2_xattr_block_attach_refcount(inode, blk_bh, ref_ci,
6174 ref_root_bh, dealloc);
6175 if (ret)
6176 mlog_errno(ret);
6178 brelse(blk_bh);
6179 out:
6181 return ret;
6184 typedef int (should_xattr_reflinked)(struct ocfs2_xattr_entry *xe);
6186 * Store the information we need in xattr reflink.
6187 * old_bh and new_bh are inode bh for the old and new inode.
6189 struct ocfs2_xattr_reflink {
6190 struct inode *old_inode;
6191 struct inode *new_inode;
6192 struct buffer_head *old_bh;
6193 struct buffer_head *new_bh;
6194 struct ocfs2_caching_info *ref_ci;
6195 struct buffer_head *ref_root_bh;
6196 struct ocfs2_cached_dealloc_ctxt *dealloc;
6197 should_xattr_reflinked *xattr_reflinked;
6201 * Given a xattr header and xe offset,
6202 * return the proper xv and the corresponding bh.
6203 * xattr in inode, block and xattr tree have different implementaions.
6205 typedef int (get_xattr_value_root)(struct super_block *sb,
6206 struct buffer_head *bh,
6207 struct ocfs2_xattr_header *xh,
6208 int offset,
6209 struct ocfs2_xattr_value_root **xv,
6210 struct buffer_head **ret_bh,
6211 void *para);
6214 * Calculate all the xattr value root metadata stored in this xattr header and
6215 * credits we need if we create them from the scratch.
6216 * We use get_xattr_value_root so that all types of xattr container can use it.
6218 static int ocfs2_value_metas_in_xattr_header(struct super_block *sb,
6219 struct buffer_head *bh,
6220 struct ocfs2_xattr_header *xh,
6221 int *metas, int *credits,
6222 int *num_recs,
6223 get_xattr_value_root *func,
6224 void *para)
6226 int i, ret = 0;
6227 struct ocfs2_xattr_value_root *xv;
6228 struct ocfs2_xattr_entry *xe;
6230 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6231 xe = &xh->xh_entries[i];
6232 if (ocfs2_xattr_is_local(xe))
6233 continue;
6235 ret = func(sb, bh, xh, i, &xv, NULL, para);
6236 if (ret) {
6237 mlog_errno(ret);
6238 break;
6241 *metas += le16_to_cpu(xv->xr_list.l_tree_depth) *
6242 le16_to_cpu(xv->xr_list.l_next_free_rec);
6244 *credits += ocfs2_calc_extend_credits(sb,
6245 &def_xv.xv.xr_list,
6246 le32_to_cpu(xv->xr_clusters));
6249 * If the value is a tree with depth > 1, We don't go deep
6250 * to the extent block, so just calculate a maximum record num.
6252 if (!xv->xr_list.l_tree_depth)
6253 *num_recs += le16_to_cpu(xv->xr_list.l_next_free_rec);
6254 else
6255 *num_recs += ocfs2_clusters_for_bytes(sb,
6256 XATTR_SIZE_MAX);
6259 return ret;
6262 /* Used by xattr inode and block to return the right xv and buffer_head. */
6263 static int ocfs2_get_xattr_value_root(struct super_block *sb,
6264 struct buffer_head *bh,
6265 struct ocfs2_xattr_header *xh,
6266 int offset,
6267 struct ocfs2_xattr_value_root **xv,
6268 struct buffer_head **ret_bh,
6269 void *para)
6271 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
6273 *xv = (struct ocfs2_xattr_value_root *)((void *)xh +
6274 le16_to_cpu(xe->xe_name_offset) +
6275 OCFS2_XATTR_SIZE(xe->xe_name_len));
6277 if (ret_bh)
6278 *ret_bh = bh;
6280 return 0;
6284 * Lock the meta_ac and caculate how much credits we need for reflink xattrs.
6285 * It is only used for inline xattr and xattr block.
6287 static int ocfs2_reflink_lock_xattr_allocators(struct ocfs2_super *osb,
6288 struct ocfs2_xattr_header *xh,
6289 struct buffer_head *ref_root_bh,
6290 int *credits,
6291 struct ocfs2_alloc_context **meta_ac)
6293 int ret, meta_add = 0, num_recs = 0;
6294 struct ocfs2_refcount_block *rb =
6295 (struct ocfs2_refcount_block *)ref_root_bh->b_data;
6297 *credits = 0;
6299 ret = ocfs2_value_metas_in_xattr_header(osb->sb, NULL, xh,
6300 &meta_add, credits, &num_recs,
6301 ocfs2_get_xattr_value_root,
6302 NULL);
6303 if (ret) {
6304 mlog_errno(ret);
6305 goto out;
6309 * We need to add/modify num_recs in refcount tree, so just calculate
6310 * an approximate number we need for refcount tree change.
6311 * Sometimes we need to split the tree, and after split, half recs
6312 * will be moved to the new block, and a new block can only provide
6313 * half number of recs. So we multiple new blocks by 2.
6315 num_recs = num_recs / ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6316 meta_add += num_recs;
6317 *credits += num_recs + num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6318 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6319 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6320 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6321 else
6322 *credits += 1;
6324 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add, meta_ac);
6325 if (ret)
6326 mlog_errno(ret);
6328 out:
6329 return ret;
6333 * Given a xattr header, reflink all the xattrs in this container.
6334 * It can be used for inode, block and bucket.
6336 * NOTE:
6337 * Before we call this function, the caller has memcpy the xattr in
6338 * old_xh to the new_xh.
6340 * If args.xattr_reflinked is set, call it to decide whether the xe should
6341 * be reflinked or not. If not, remove it from the new xattr header.
6343 static int ocfs2_reflink_xattr_header(handle_t *handle,
6344 struct ocfs2_xattr_reflink *args,
6345 struct buffer_head *old_bh,
6346 struct ocfs2_xattr_header *xh,
6347 struct buffer_head *new_bh,
6348 struct ocfs2_xattr_header *new_xh,
6349 struct ocfs2_xattr_value_buf *vb,
6350 struct ocfs2_alloc_context *meta_ac,
6351 get_xattr_value_root *func,
6352 void *para)
6354 int ret = 0, i, j;
6355 struct super_block *sb = args->old_inode->i_sb;
6356 struct buffer_head *value_bh;
6357 struct ocfs2_xattr_entry *xe, *last;
6358 struct ocfs2_xattr_value_root *xv, *new_xv;
6359 struct ocfs2_extent_tree data_et;
6360 u32 clusters, cpos, p_cluster, num_clusters;
6361 unsigned int ext_flags = 0;
6363 mlog(0, "reflink xattr in container %llu, count = %u\n",
6364 (unsigned long long)old_bh->b_blocknr, le16_to_cpu(xh->xh_count));
6366 last = &new_xh->xh_entries[le16_to_cpu(new_xh->xh_count)];
6367 for (i = 0, j = 0; i < le16_to_cpu(xh->xh_count); i++, j++) {
6368 xe = &xh->xh_entries[i];
6370 if (args->xattr_reflinked && !args->xattr_reflinked(xe)) {
6371 xe = &new_xh->xh_entries[j];
6373 le16_add_cpu(&new_xh->xh_count, -1);
6374 if (new_xh->xh_count) {
6375 memmove(xe, xe + 1,
6376 (void *)last - (void *)xe);
6377 memset(last, 0,
6378 sizeof(struct ocfs2_xattr_entry));
6382 * We don't want j to increase in the next round since
6383 * it is already moved ahead.
6385 j--;
6386 continue;
6389 if (ocfs2_xattr_is_local(xe))
6390 continue;
6392 ret = func(sb, old_bh, xh, i, &xv, NULL, para);
6393 if (ret) {
6394 mlog_errno(ret);
6395 break;
6398 ret = func(sb, new_bh, new_xh, j, &new_xv, &value_bh, para);
6399 if (ret) {
6400 mlog_errno(ret);
6401 break;
6405 * For the xattr which has l_tree_depth = 0, all the extent
6406 * recs have already be copied to the new xh with the
6407 * propriate OCFS2_EXT_REFCOUNTED flag we just need to
6408 * increase the refount count int the refcount tree.
6410 * For the xattr which has l_tree_depth > 0, we need
6411 * to initialize it to the empty default value root,
6412 * and then insert the extents one by one.
6414 if (xv->xr_list.l_tree_depth) {
6415 memcpy(new_xv, &def_xv, sizeof(def_xv));
6416 vb->vb_xv = new_xv;
6417 vb->vb_bh = value_bh;
6418 ocfs2_init_xattr_value_extent_tree(&data_et,
6419 INODE_CACHE(args->new_inode), vb);
6422 clusters = le32_to_cpu(xv->xr_clusters);
6423 cpos = 0;
6424 while (cpos < clusters) {
6425 ret = ocfs2_xattr_get_clusters(args->old_inode,
6426 cpos,
6427 &p_cluster,
6428 &num_clusters,
6429 &xv->xr_list,
6430 &ext_flags);
6431 if (ret) {
6432 mlog_errno(ret);
6433 goto out;
6436 BUG_ON(!p_cluster);
6438 if (xv->xr_list.l_tree_depth) {
6439 ret = ocfs2_insert_extent(handle,
6440 &data_et, cpos,
6441 ocfs2_clusters_to_blocks(
6442 args->old_inode->i_sb,
6443 p_cluster),
6444 num_clusters, ext_flags,
6445 meta_ac);
6446 if (ret) {
6447 mlog_errno(ret);
6448 goto out;
6452 ret = ocfs2_increase_refcount(handle, args->ref_ci,
6453 args->ref_root_bh,
6454 p_cluster, num_clusters,
6455 meta_ac, args->dealloc);
6456 if (ret) {
6457 mlog_errno(ret);
6458 goto out;
6461 cpos += num_clusters;
6465 out:
6466 return ret;
6469 static int ocfs2_reflink_xattr_inline(struct ocfs2_xattr_reflink *args)
6471 int ret = 0, credits = 0;
6472 handle_t *handle;
6473 struct ocfs2_super *osb = OCFS2_SB(args->old_inode->i_sb);
6474 struct ocfs2_dinode *di = (struct ocfs2_dinode *)args->old_bh->b_data;
6475 int inline_size = le16_to_cpu(di->i_xattr_inline_size);
6476 int header_off = osb->sb->s_blocksize - inline_size;
6477 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)
6478 (args->old_bh->b_data + header_off);
6479 struct ocfs2_xattr_header *new_xh = (struct ocfs2_xattr_header *)
6480 (args->new_bh->b_data + header_off);
6481 struct ocfs2_alloc_context *meta_ac = NULL;
6482 struct ocfs2_inode_info *new_oi;
6483 struct ocfs2_dinode *new_di;
6484 struct ocfs2_xattr_value_buf vb = {
6485 .vb_bh = args->new_bh,
6486 .vb_access = ocfs2_journal_access_di,
6489 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6490 &credits, &meta_ac);
6491 if (ret) {
6492 mlog_errno(ret);
6493 goto out;
6496 handle = ocfs2_start_trans(osb, credits);
6497 if (IS_ERR(handle)) {
6498 ret = PTR_ERR(handle);
6499 mlog_errno(ret);
6500 goto out;
6503 ret = ocfs2_journal_access_di(handle, INODE_CACHE(args->new_inode),
6504 args->new_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6505 if (ret) {
6506 mlog_errno(ret);
6507 goto out_commit;
6510 memcpy(args->new_bh->b_data + header_off,
6511 args->old_bh->b_data + header_off, inline_size);
6513 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6514 new_di->i_xattr_inline_size = cpu_to_le16(inline_size);
6516 ret = ocfs2_reflink_xattr_header(handle, args, args->old_bh, xh,
6517 args->new_bh, new_xh, &vb, meta_ac,
6518 ocfs2_get_xattr_value_root, NULL);
6519 if (ret) {
6520 mlog_errno(ret);
6521 goto out_commit;
6524 new_oi = OCFS2_I(args->new_inode);
6525 spin_lock(&new_oi->ip_lock);
6526 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL | OCFS2_INLINE_XATTR_FL;
6527 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6528 spin_unlock(&new_oi->ip_lock);
6530 ocfs2_journal_dirty(handle, args->new_bh);
6532 out_commit:
6533 ocfs2_commit_trans(osb, handle);
6535 out:
6536 if (meta_ac)
6537 ocfs2_free_alloc_context(meta_ac);
6538 return ret;
6541 static int ocfs2_create_empty_xattr_block(struct inode *inode,
6542 struct buffer_head *fe_bh,
6543 struct buffer_head **ret_bh,
6544 int indexed)
6546 int ret;
6547 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6548 struct ocfs2_xattr_set_ctxt ctxt;
6550 memset(&ctxt, 0, sizeof(ctxt));
6551 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &ctxt.meta_ac);
6552 if (ret < 0) {
6553 mlog_errno(ret);
6554 return ret;
6557 ctxt.handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_CREATE_CREDITS);
6558 if (IS_ERR(ctxt.handle)) {
6559 ret = PTR_ERR(ctxt.handle);
6560 mlog_errno(ret);
6561 goto out;
6564 mlog(0, "create new xattr block for inode %llu, index = %d\n",
6565 (unsigned long long)fe_bh->b_blocknr, indexed);
6566 ret = ocfs2_create_xattr_block(inode, fe_bh, &ctxt, indexed,
6567 ret_bh);
6568 if (ret)
6569 mlog_errno(ret);
6571 ocfs2_commit_trans(osb, ctxt.handle);
6572 out:
6573 ocfs2_free_alloc_context(ctxt.meta_ac);
6574 return ret;
6577 static int ocfs2_reflink_xattr_block(struct ocfs2_xattr_reflink *args,
6578 struct buffer_head *blk_bh,
6579 struct buffer_head *new_blk_bh)
6581 int ret = 0, credits = 0;
6582 handle_t *handle;
6583 struct ocfs2_inode_info *new_oi = OCFS2_I(args->new_inode);
6584 struct ocfs2_dinode *new_di;
6585 struct ocfs2_super *osb = OCFS2_SB(args->new_inode->i_sb);
6586 int header_off = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
6587 struct ocfs2_xattr_block *xb =
6588 (struct ocfs2_xattr_block *)blk_bh->b_data;
6589 struct ocfs2_xattr_header *xh = &xb->xb_attrs.xb_header;
6590 struct ocfs2_xattr_block *new_xb =
6591 (struct ocfs2_xattr_block *)new_blk_bh->b_data;
6592 struct ocfs2_xattr_header *new_xh = &new_xb->xb_attrs.xb_header;
6593 struct ocfs2_alloc_context *meta_ac;
6594 struct ocfs2_xattr_value_buf vb = {
6595 .vb_bh = new_blk_bh,
6596 .vb_access = ocfs2_journal_access_xb,
6599 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6600 &credits, &meta_ac);
6601 if (ret) {
6602 mlog_errno(ret);
6603 return ret;
6606 /* One more credits in case we need to add xattr flags in new inode. */
6607 handle = ocfs2_start_trans(osb, credits + 1);
6608 if (IS_ERR(handle)) {
6609 ret = PTR_ERR(handle);
6610 mlog_errno(ret);
6611 goto out;
6614 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6615 ret = ocfs2_journal_access_di(handle,
6616 INODE_CACHE(args->new_inode),
6617 args->new_bh,
6618 OCFS2_JOURNAL_ACCESS_WRITE);
6619 if (ret) {
6620 mlog_errno(ret);
6621 goto out_commit;
6625 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(args->new_inode),
6626 new_blk_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6627 if (ret) {
6628 mlog_errno(ret);
6629 goto out_commit;
6632 memcpy(new_blk_bh->b_data + header_off, blk_bh->b_data + header_off,
6633 osb->sb->s_blocksize - header_off);
6635 ret = ocfs2_reflink_xattr_header(handle, args, blk_bh, xh,
6636 new_blk_bh, new_xh, &vb, meta_ac,
6637 ocfs2_get_xattr_value_root, NULL);
6638 if (ret) {
6639 mlog_errno(ret);
6640 goto out_commit;
6643 ocfs2_journal_dirty(handle, new_blk_bh);
6645 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6646 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6647 spin_lock(&new_oi->ip_lock);
6648 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
6649 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6650 spin_unlock(&new_oi->ip_lock);
6652 ocfs2_journal_dirty(handle, args->new_bh);
6655 out_commit:
6656 ocfs2_commit_trans(osb, handle);
6658 out:
6659 ocfs2_free_alloc_context(meta_ac);
6660 return ret;
6663 struct ocfs2_reflink_xattr_tree_args {
6664 struct ocfs2_xattr_reflink *reflink;
6665 struct buffer_head *old_blk_bh;
6666 struct buffer_head *new_blk_bh;
6667 struct ocfs2_xattr_bucket *old_bucket;
6668 struct ocfs2_xattr_bucket *new_bucket;
6672 * NOTE:
6673 * We have to handle the case that both old bucket and new bucket
6674 * will call this function to get the right ret_bh.
6675 * So The caller must give us the right bh.
6677 static int ocfs2_get_reflink_xattr_value_root(struct super_block *sb,
6678 struct buffer_head *bh,
6679 struct ocfs2_xattr_header *xh,
6680 int offset,
6681 struct ocfs2_xattr_value_root **xv,
6682 struct buffer_head **ret_bh,
6683 void *para)
6685 struct ocfs2_reflink_xattr_tree_args *args =
6686 (struct ocfs2_reflink_xattr_tree_args *)para;
6687 struct ocfs2_xattr_bucket *bucket;
6689 if (bh == args->old_bucket->bu_bhs[0])
6690 bucket = args->old_bucket;
6691 else
6692 bucket = args->new_bucket;
6694 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6695 xv, ret_bh);
6698 struct ocfs2_value_tree_metas {
6699 int num_metas;
6700 int credits;
6701 int num_recs;
6704 static int ocfs2_value_tree_metas_in_bucket(struct super_block *sb,
6705 struct buffer_head *bh,
6706 struct ocfs2_xattr_header *xh,
6707 int offset,
6708 struct ocfs2_xattr_value_root **xv,
6709 struct buffer_head **ret_bh,
6710 void *para)
6712 struct ocfs2_xattr_bucket *bucket =
6713 (struct ocfs2_xattr_bucket *)para;
6715 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6716 xv, ret_bh);
6719 static int ocfs2_calc_value_tree_metas(struct inode *inode,
6720 struct ocfs2_xattr_bucket *bucket,
6721 void *para)
6723 struct ocfs2_value_tree_metas *metas =
6724 (struct ocfs2_value_tree_metas *)para;
6725 struct ocfs2_xattr_header *xh =
6726 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6728 /* Add the credits for this bucket first. */
6729 metas->credits += bucket->bu_blocks;
6730 return ocfs2_value_metas_in_xattr_header(inode->i_sb, bucket->bu_bhs[0],
6731 xh, &metas->num_metas,
6732 &metas->credits, &metas->num_recs,
6733 ocfs2_value_tree_metas_in_bucket,
6734 bucket);
6738 * Given a xattr extent rec starting from blkno and having len clusters,
6739 * iterate all the buckets calculate how much metadata we need for reflinking
6740 * all the ocfs2_xattr_value_root and lock the allocators accordingly.
6742 static int ocfs2_lock_reflink_xattr_rec_allocators(
6743 struct ocfs2_reflink_xattr_tree_args *args,
6744 struct ocfs2_extent_tree *xt_et,
6745 u64 blkno, u32 len, int *credits,
6746 struct ocfs2_alloc_context **meta_ac,
6747 struct ocfs2_alloc_context **data_ac)
6749 int ret, num_free_extents;
6750 struct ocfs2_value_tree_metas metas;
6751 struct ocfs2_super *osb = OCFS2_SB(args->reflink->old_inode->i_sb);
6752 struct ocfs2_refcount_block *rb;
6754 memset(&metas, 0, sizeof(metas));
6756 ret = ocfs2_iterate_xattr_buckets(args->reflink->old_inode, blkno, len,
6757 ocfs2_calc_value_tree_metas, &metas);
6758 if (ret) {
6759 mlog_errno(ret);
6760 goto out;
6763 *credits = metas.credits;
6766 * Calculate we need for refcount tree change.
6768 * We need to add/modify num_recs in refcount tree, so just calculate
6769 * an approximate number we need for refcount tree change.
6770 * Sometimes we need to split the tree, and after split, half recs
6771 * will be moved to the new block, and a new block can only provide
6772 * half number of recs. So we multiple new blocks by 2.
6773 * In the end, we have to add credits for modifying the already
6774 * existed refcount block.
6776 rb = (struct ocfs2_refcount_block *)args->reflink->ref_root_bh->b_data;
6777 metas.num_recs =
6778 (metas.num_recs + ocfs2_refcount_recs_per_rb(osb->sb) - 1) /
6779 ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6780 metas.num_metas += metas.num_recs;
6781 *credits += metas.num_recs +
6782 metas.num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6783 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6784 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6785 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6786 else
6787 *credits += 1;
6789 /* count in the xattr tree change. */
6790 num_free_extents = ocfs2_num_free_extents(osb, xt_et);
6791 if (num_free_extents < 0) {
6792 ret = num_free_extents;
6793 mlog_errno(ret);
6794 goto out;
6797 if (num_free_extents < len)
6798 metas.num_metas += ocfs2_extend_meta_needed(xt_et->et_root_el);
6800 *credits += ocfs2_calc_extend_credits(osb->sb,
6801 xt_et->et_root_el, len);
6803 if (metas.num_metas) {
6804 ret = ocfs2_reserve_new_metadata_blocks(osb, metas.num_metas,
6805 meta_ac);
6806 if (ret) {
6807 mlog_errno(ret);
6808 goto out;
6812 if (len) {
6813 ret = ocfs2_reserve_clusters(osb, len, data_ac);
6814 if (ret)
6815 mlog_errno(ret);
6817 out:
6818 if (ret) {
6819 if (*meta_ac) {
6820 ocfs2_free_alloc_context(*meta_ac);
6821 meta_ac = NULL;
6825 return ret;
6828 static int ocfs2_reflink_xattr_buckets(handle_t *handle,
6829 u64 blkno, u64 new_blkno, u32 clusters,
6830 struct ocfs2_alloc_context *meta_ac,
6831 struct ocfs2_alloc_context *data_ac,
6832 struct ocfs2_reflink_xattr_tree_args *args)
6834 int i, j, ret = 0;
6835 struct super_block *sb = args->reflink->old_inode->i_sb;
6836 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
6837 u32 num_buckets = clusters * bpc;
6838 int bpb = args->old_bucket->bu_blocks;
6839 struct ocfs2_xattr_value_buf vb = {
6840 .vb_access = ocfs2_journal_access,
6843 for (i = 0; i < num_buckets; i++, blkno += bpb, new_blkno += bpb) {
6844 ret = ocfs2_read_xattr_bucket(args->old_bucket, blkno);
6845 if (ret) {
6846 mlog_errno(ret);
6847 break;
6850 ret = ocfs2_init_xattr_bucket(args->new_bucket, new_blkno);
6851 if (ret) {
6852 mlog_errno(ret);
6853 break;
6857 * The real bucket num in this series of blocks is stored
6858 * in the 1st bucket.
6860 if (i == 0)
6861 num_buckets = le16_to_cpu(
6862 bucket_xh(args->old_bucket)->xh_num_buckets);
6864 ret = ocfs2_xattr_bucket_journal_access(handle,
6865 args->new_bucket,
6866 OCFS2_JOURNAL_ACCESS_CREATE);
6867 if (ret) {
6868 mlog_errno(ret);
6869 break;
6872 for (j = 0; j < bpb; j++)
6873 memcpy(bucket_block(args->new_bucket, j),
6874 bucket_block(args->old_bucket, j),
6875 sb->s_blocksize);
6877 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
6879 ret = ocfs2_reflink_xattr_header(handle, args->reflink,
6880 args->old_bucket->bu_bhs[0],
6881 bucket_xh(args->old_bucket),
6882 args->new_bucket->bu_bhs[0],
6883 bucket_xh(args->new_bucket),
6884 &vb, meta_ac,
6885 ocfs2_get_reflink_xattr_value_root,
6886 args);
6887 if (ret) {
6888 mlog_errno(ret);
6889 break;
6893 * Re-access and dirty the bucket to calculate metaecc.
6894 * Because we may extend the transaction in reflink_xattr_header
6895 * which will let the already accessed block gone.
6897 ret = ocfs2_xattr_bucket_journal_access(handle,
6898 args->new_bucket,
6899 OCFS2_JOURNAL_ACCESS_WRITE);
6900 if (ret) {
6901 mlog_errno(ret);
6902 break;
6905 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
6906 ocfs2_xattr_bucket_relse(args->old_bucket);
6907 ocfs2_xattr_bucket_relse(args->new_bucket);
6910 ocfs2_xattr_bucket_relse(args->old_bucket);
6911 ocfs2_xattr_bucket_relse(args->new_bucket);
6912 return ret;
6915 * Create the same xattr extent record in the new inode's xattr tree.
6917 static int ocfs2_reflink_xattr_rec(struct inode *inode,
6918 struct buffer_head *root_bh,
6919 u64 blkno,
6920 u32 cpos,
6921 u32 len,
6922 void *para)
6924 int ret, credits = 0;
6925 u32 p_cluster, num_clusters;
6926 u64 new_blkno;
6927 handle_t *handle;
6928 struct ocfs2_reflink_xattr_tree_args *args =
6929 (struct ocfs2_reflink_xattr_tree_args *)para;
6930 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6931 struct ocfs2_alloc_context *meta_ac = NULL;
6932 struct ocfs2_alloc_context *data_ac = NULL;
6933 struct ocfs2_extent_tree et;
6935 ocfs2_init_xattr_tree_extent_tree(&et,
6936 INODE_CACHE(args->reflink->new_inode),
6937 args->new_blk_bh);
6939 ret = ocfs2_lock_reflink_xattr_rec_allocators(args, &et, blkno,
6940 len, &credits,
6941 &meta_ac, &data_ac);
6942 if (ret) {
6943 mlog_errno(ret);
6944 goto out;
6947 handle = ocfs2_start_trans(osb, credits);
6948 if (IS_ERR(handle)) {
6949 ret = PTR_ERR(handle);
6950 mlog_errno(ret);
6951 goto out;
6954 ret = ocfs2_claim_clusters(osb, handle, data_ac,
6955 len, &p_cluster, &num_clusters);
6956 if (ret) {
6957 mlog_errno(ret);
6958 goto out_commit;
6961 new_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cluster);
6963 mlog(0, "reflink xattr buckets %llu to %llu, len %u\n",
6964 (unsigned long long)blkno, (unsigned long long)new_blkno, len);
6965 ret = ocfs2_reflink_xattr_buckets(handle, blkno, new_blkno, len,
6966 meta_ac, data_ac, args);
6967 if (ret) {
6968 mlog_errno(ret);
6969 goto out_commit;
6972 mlog(0, "insert new xattr extent rec start %llu len %u to %u\n",
6973 (unsigned long long)new_blkno, len, cpos);
6974 ret = ocfs2_insert_extent(handle, &et, cpos, new_blkno,
6975 len, 0, meta_ac);
6976 if (ret)
6977 mlog_errno(ret);
6979 out_commit:
6980 ocfs2_commit_trans(osb, handle);
6982 out:
6983 if (meta_ac)
6984 ocfs2_free_alloc_context(meta_ac);
6985 if (data_ac)
6986 ocfs2_free_alloc_context(data_ac);
6987 return ret;
6991 * Create reflinked xattr buckets.
6992 * We will add bucket one by one, and refcount all the xattrs in the bucket
6993 * if they are stored outside.
6995 static int ocfs2_reflink_xattr_tree(struct ocfs2_xattr_reflink *args,
6996 struct buffer_head *blk_bh,
6997 struct buffer_head *new_blk_bh)
6999 int ret;
7000 struct ocfs2_reflink_xattr_tree_args para;
7002 memset(&para, 0, sizeof(para));
7003 para.reflink = args;
7004 para.old_blk_bh = blk_bh;
7005 para.new_blk_bh = new_blk_bh;
7007 para.old_bucket = ocfs2_xattr_bucket_new(args->old_inode);
7008 if (!para.old_bucket) {
7009 mlog_errno(-ENOMEM);
7010 return -ENOMEM;
7013 para.new_bucket = ocfs2_xattr_bucket_new(args->new_inode);
7014 if (!para.new_bucket) {
7015 ret = -ENOMEM;
7016 mlog_errno(ret);
7017 goto out;
7020 ret = ocfs2_iterate_xattr_index_block(args->old_inode, blk_bh,
7021 ocfs2_reflink_xattr_rec,
7022 &para);
7023 if (ret)
7024 mlog_errno(ret);
7026 out:
7027 ocfs2_xattr_bucket_free(para.old_bucket);
7028 ocfs2_xattr_bucket_free(para.new_bucket);
7029 return ret;
7032 static int ocfs2_reflink_xattr_in_block(struct ocfs2_xattr_reflink *args,
7033 struct buffer_head *blk_bh)
7035 int ret, indexed = 0;
7036 struct buffer_head *new_blk_bh = NULL;
7037 struct ocfs2_xattr_block *xb =
7038 (struct ocfs2_xattr_block *)blk_bh->b_data;
7041 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)
7042 indexed = 1;
7044 ret = ocfs2_create_empty_xattr_block(args->new_inode, args->new_bh,
7045 &new_blk_bh, indexed);
7046 if (ret) {
7047 mlog_errno(ret);
7048 goto out;
7051 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED))
7052 ret = ocfs2_reflink_xattr_block(args, blk_bh, new_blk_bh);
7053 else
7054 ret = ocfs2_reflink_xattr_tree(args, blk_bh, new_blk_bh);
7055 if (ret)
7056 mlog_errno(ret);
7058 out:
7059 brelse(new_blk_bh);
7060 return ret;
7063 static int ocfs2_reflink_xattr_no_security(struct ocfs2_xattr_entry *xe)
7065 int type = ocfs2_xattr_get_type(xe);
7067 return type != OCFS2_XATTR_INDEX_SECURITY &&
7068 type != OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS &&
7069 type != OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
7072 int ocfs2_reflink_xattrs(struct inode *old_inode,
7073 struct buffer_head *old_bh,
7074 struct inode *new_inode,
7075 struct buffer_head *new_bh,
7076 bool preserve_security)
7078 int ret;
7079 struct ocfs2_xattr_reflink args;
7080 struct ocfs2_inode_info *oi = OCFS2_I(old_inode);
7081 struct ocfs2_dinode *di = (struct ocfs2_dinode *)old_bh->b_data;
7082 struct buffer_head *blk_bh = NULL;
7083 struct ocfs2_cached_dealloc_ctxt dealloc;
7084 struct ocfs2_refcount_tree *ref_tree;
7085 struct buffer_head *ref_root_bh = NULL;
7087 ret = ocfs2_lock_refcount_tree(OCFS2_SB(old_inode->i_sb),
7088 le64_to_cpu(di->i_refcount_loc),
7089 1, &ref_tree, &ref_root_bh);
7090 if (ret) {
7091 mlog_errno(ret);
7092 goto out;
7095 ocfs2_init_dealloc_ctxt(&dealloc);
7097 args.old_inode = old_inode;
7098 args.new_inode = new_inode;
7099 args.old_bh = old_bh;
7100 args.new_bh = new_bh;
7101 args.ref_ci = &ref_tree->rf_ci;
7102 args.ref_root_bh = ref_root_bh;
7103 args.dealloc = &dealloc;
7104 if (preserve_security)
7105 args.xattr_reflinked = NULL;
7106 else
7107 args.xattr_reflinked = ocfs2_reflink_xattr_no_security;
7109 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
7110 ret = ocfs2_reflink_xattr_inline(&args);
7111 if (ret) {
7112 mlog_errno(ret);
7113 goto out_unlock;
7117 if (!di->i_xattr_loc)
7118 goto out_unlock;
7120 ret = ocfs2_read_xattr_block(old_inode, le64_to_cpu(di->i_xattr_loc),
7121 &blk_bh);
7122 if (ret < 0) {
7123 mlog_errno(ret);
7124 goto out_unlock;
7127 ret = ocfs2_reflink_xattr_in_block(&args, blk_bh);
7128 if (ret)
7129 mlog_errno(ret);
7131 brelse(blk_bh);
7133 out_unlock:
7134 ocfs2_unlock_refcount_tree(OCFS2_SB(old_inode->i_sb),
7135 ref_tree, 1);
7136 brelse(ref_root_bh);
7138 if (ocfs2_dealloc_has_cluster(&dealloc)) {
7139 ocfs2_schedule_truncate_log_flush(OCFS2_SB(old_inode->i_sb), 1);
7140 ocfs2_run_deallocs(OCFS2_SB(old_inode->i_sb), &dealloc);
7143 out:
7144 return ret;
7148 * Initialize security and acl for a already created inode.
7149 * Used for reflink a non-preserve-security file.
7151 * It uses common api like ocfs2_xattr_set, so the caller
7152 * must not hold any lock expect i_mutex.
7154 int ocfs2_init_security_and_acl(struct inode *dir,
7155 struct inode *inode)
7157 int ret = 0;
7158 struct buffer_head *dir_bh = NULL;
7159 struct ocfs2_security_xattr_info si = {
7160 .enable = 1,
7163 ret = ocfs2_init_security_get(inode, dir, &si);
7164 if (!ret) {
7165 ret = ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY,
7166 si.name, si.value, si.value_len,
7167 XATTR_CREATE);
7168 if (ret) {
7169 mlog_errno(ret);
7170 goto leave;
7172 } else if (ret != -EOPNOTSUPP) {
7173 mlog_errno(ret);
7174 goto leave;
7177 ret = ocfs2_inode_lock(dir, &dir_bh, 0);
7178 if (ret) {
7179 mlog_errno(ret);
7180 goto leave;
7183 ret = ocfs2_init_acl(NULL, inode, dir, NULL, dir_bh, NULL, NULL);
7184 if (ret)
7185 mlog_errno(ret);
7187 ocfs2_inode_unlock(dir, 0);
7188 brelse(dir_bh);
7189 leave:
7190 return ret;
7193 * 'security' attributes support
7195 static size_t ocfs2_xattr_security_list(struct dentry *dentry, char *list,
7196 size_t list_size, const char *name,
7197 size_t name_len, int type)
7199 const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN;
7200 const size_t total_len = prefix_len + name_len + 1;
7202 if (list && total_len <= list_size) {
7203 memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
7204 memcpy(list + prefix_len, name, name_len);
7205 list[prefix_len + name_len] = '\0';
7207 return total_len;
7210 static int ocfs2_xattr_security_get(struct dentry *dentry, const char *name,
7211 void *buffer, size_t size, int type)
7213 if (strcmp(name, "") == 0)
7214 return -EINVAL;
7215 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
7216 name, buffer, size);
7219 static int ocfs2_xattr_security_set(struct dentry *dentry, const char *name,
7220 const void *value, size_t size, int flags, int type)
7222 if (strcmp(name, "") == 0)
7223 return -EINVAL;
7225 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
7226 name, value, size, flags);
7229 int ocfs2_init_security_get(struct inode *inode,
7230 struct inode *dir,
7231 struct ocfs2_security_xattr_info *si)
7233 /* check whether ocfs2 support feature xattr */
7234 if (!ocfs2_supports_xattr(OCFS2_SB(dir->i_sb)))
7235 return -EOPNOTSUPP;
7236 return security_inode_init_security(inode, dir, &si->name, &si->value,
7237 &si->value_len);
7240 int ocfs2_init_security_set(handle_t *handle,
7241 struct inode *inode,
7242 struct buffer_head *di_bh,
7243 struct ocfs2_security_xattr_info *si,
7244 struct ocfs2_alloc_context *xattr_ac,
7245 struct ocfs2_alloc_context *data_ac)
7247 return ocfs2_xattr_set_handle(handle, inode, di_bh,
7248 OCFS2_XATTR_INDEX_SECURITY,
7249 si->name, si->value, si->value_len, 0,
7250 xattr_ac, data_ac);
7253 struct xattr_handler ocfs2_xattr_security_handler = {
7254 .prefix = XATTR_SECURITY_PREFIX,
7255 .list = ocfs2_xattr_security_list,
7256 .get = ocfs2_xattr_security_get,
7257 .set = ocfs2_xattr_security_set,
7261 * 'trusted' attributes support
7263 static size_t ocfs2_xattr_trusted_list(struct dentry *dentry, char *list,
7264 size_t list_size, const char *name,
7265 size_t name_len, int type)
7267 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
7268 const size_t total_len = prefix_len + name_len + 1;
7270 if (list && total_len <= list_size) {
7271 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
7272 memcpy(list + prefix_len, name, name_len);
7273 list[prefix_len + name_len] = '\0';
7275 return total_len;
7278 static int ocfs2_xattr_trusted_get(struct dentry *dentry, const char *name,
7279 void *buffer, size_t size, int type)
7281 if (strcmp(name, "") == 0)
7282 return -EINVAL;
7283 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
7284 name, buffer, size);
7287 static int ocfs2_xattr_trusted_set(struct dentry *dentry, const char *name,
7288 const void *value, size_t size, int flags, int type)
7290 if (strcmp(name, "") == 0)
7291 return -EINVAL;
7293 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
7294 name, value, size, flags);
7297 struct xattr_handler ocfs2_xattr_trusted_handler = {
7298 .prefix = XATTR_TRUSTED_PREFIX,
7299 .list = ocfs2_xattr_trusted_list,
7300 .get = ocfs2_xattr_trusted_get,
7301 .set = ocfs2_xattr_trusted_set,
7305 * 'user' attributes support
7307 static size_t ocfs2_xattr_user_list(struct dentry *dentry, char *list,
7308 size_t list_size, const char *name,
7309 size_t name_len, int type)
7311 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
7312 const size_t total_len = prefix_len + name_len + 1;
7313 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
7315 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7316 return 0;
7318 if (list && total_len <= list_size) {
7319 memcpy(list, XATTR_USER_PREFIX, prefix_len);
7320 memcpy(list + prefix_len, name, name_len);
7321 list[prefix_len + name_len] = '\0';
7323 return total_len;
7326 static int ocfs2_xattr_user_get(struct dentry *dentry, const char *name,
7327 void *buffer, size_t size, int type)
7329 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
7331 if (strcmp(name, "") == 0)
7332 return -EINVAL;
7333 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7334 return -EOPNOTSUPP;
7335 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_USER, name,
7336 buffer, size);
7339 static int ocfs2_xattr_user_set(struct dentry *dentry, const char *name,
7340 const void *value, size_t size, int flags, int type)
7342 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
7344 if (strcmp(name, "") == 0)
7345 return -EINVAL;
7346 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7347 return -EOPNOTSUPP;
7349 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_USER,
7350 name, value, size, flags);
7353 struct xattr_handler ocfs2_xattr_user_handler = {
7354 .prefix = XATTR_USER_PREFIX,
7355 .list = ocfs2_xattr_user_list,
7356 .get = ocfs2_xattr_user_get,
7357 .set = ocfs2_xattr_user_set,