1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Copyright (C) 2004, 2008 Oracle. All rights reserved.
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>
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>
45 #include "blockcheck.h"
55 #include "buffer_head_io.h"
58 #include "refcounttree.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 */
77 struct ocfs2_xattr_set_ctxt {
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,
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 {
125 struct ocfs2_xattr_search {
126 struct buffer_head *inode_bh;
128 * xattr_bh point to the block buffer head which has extended attribute
129 * when extended attribute in inode, xattr_bh is equal to inode_bh.
131 struct buffer_head *xattr_bh;
132 struct ocfs2_xattr_header *header;
133 struct ocfs2_xattr_bucket *bucket;
136 struct ocfs2_xattr_entry *here;
140 static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
141 struct ocfs2_xattr_header *xh,
146 static int ocfs2_xattr_block_find(struct inode *inode,
149 struct ocfs2_xattr_search *xs);
150 static int ocfs2_xattr_index_block_find(struct inode *inode,
151 struct buffer_head *root_bh,
154 struct ocfs2_xattr_search *xs);
156 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
157 struct buffer_head *blk_bh,
161 static int ocfs2_xattr_create_index_block(struct inode *inode,
162 struct ocfs2_xattr_search *xs,
163 struct ocfs2_xattr_set_ctxt *ctxt);
165 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
166 struct ocfs2_xattr_info *xi,
167 struct ocfs2_xattr_search *xs,
168 struct ocfs2_xattr_set_ctxt *ctxt);
170 typedef int (xattr_tree_rec_func)(struct inode *inode,
171 struct buffer_head *root_bh,
172 u64 blkno, u32 cpos, u32 len, void *para);
173 static int ocfs2_iterate_xattr_index_block(struct inode *inode,
174 struct buffer_head *root_bh,
175 xattr_tree_rec_func *rec_func,
177 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
178 struct ocfs2_xattr_bucket *bucket,
180 static int ocfs2_rm_xattr_cluster(struct inode *inode,
181 struct buffer_head *root_bh,
187 static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
188 u64 src_blk, u64 last_blk, u64 to_blk,
189 unsigned int start_bucket,
191 static int ocfs2_prepare_refcount_xattr(struct inode *inode,
192 struct ocfs2_dinode *di,
193 struct ocfs2_xattr_info *xi,
194 struct ocfs2_xattr_search *xis,
195 struct ocfs2_xattr_search *xbs,
196 struct ocfs2_refcount_tree **ref_tree,
199 static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
200 struct ocfs2_xattr_bucket *bucket,
202 struct ocfs2_xattr_value_root **xv,
203 struct buffer_head **bh);
205 static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
207 return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
210 static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
212 return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
215 static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb)
217 u16 len = sb->s_blocksize -
218 offsetof(struct ocfs2_xattr_header, xh_entries);
220 return len / sizeof(struct ocfs2_xattr_entry);
223 #define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
224 #define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
225 #define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
227 static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
229 struct ocfs2_xattr_bucket *bucket;
230 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
232 BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
234 bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
236 bucket->bu_inode = inode;
237 bucket->bu_blocks = blks;
243 static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
247 for (i = 0; i < bucket->bu_blocks; i++) {
248 brelse(bucket->bu_bhs[i]);
249 bucket->bu_bhs[i] = NULL;
253 static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
256 ocfs2_xattr_bucket_relse(bucket);
257 bucket->bu_inode = NULL;
263 * A bucket that has never been written to disk doesn't need to be
264 * read. We just need the buffer_heads. Don't call this for
265 * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
268 static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
273 for (i = 0; i < bucket->bu_blocks; i++) {
274 bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
276 if (!bucket->bu_bhs[i]) {
282 if (!ocfs2_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
284 ocfs2_set_new_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
289 ocfs2_xattr_bucket_relse(bucket);
293 /* Read the xattr bucket at xb_blkno */
294 static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
299 rc = ocfs2_read_blocks(INODE_CACHE(bucket->bu_inode), xb_blkno,
300 bucket->bu_blocks, bucket->bu_bhs, 0,
303 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
304 rc = ocfs2_validate_meta_ecc_bhs(bucket->bu_inode->i_sb,
307 &bucket_xh(bucket)->xh_check);
308 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
314 ocfs2_xattr_bucket_relse(bucket);
318 static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
319 struct ocfs2_xattr_bucket *bucket,
324 for (i = 0; i < bucket->bu_blocks; i++) {
325 rc = ocfs2_journal_access(handle,
326 INODE_CACHE(bucket->bu_inode),
327 bucket->bu_bhs[i], type);
337 static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
338 struct ocfs2_xattr_bucket *bucket)
342 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
343 ocfs2_compute_meta_ecc_bhs(bucket->bu_inode->i_sb,
344 bucket->bu_bhs, bucket->bu_blocks,
345 &bucket_xh(bucket)->xh_check);
346 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
348 for (i = 0; i < bucket->bu_blocks; i++)
349 ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
352 static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
353 struct ocfs2_xattr_bucket *src)
356 int blocksize = src->bu_inode->i_sb->s_blocksize;
358 BUG_ON(dest->bu_blocks != src->bu_blocks);
359 BUG_ON(dest->bu_inode != src->bu_inode);
361 for (i = 0; i < src->bu_blocks; i++) {
362 memcpy(bucket_block(dest, i), bucket_block(src, i),
367 static int ocfs2_validate_xattr_block(struct super_block *sb,
368 struct buffer_head *bh)
371 struct ocfs2_xattr_block *xb =
372 (struct ocfs2_xattr_block *)bh->b_data;
374 mlog(0, "Validating xattr block %llu\n",
375 (unsigned long long)bh->b_blocknr);
377 BUG_ON(!buffer_uptodate(bh));
380 * If the ecc fails, we return the error but otherwise
381 * leave the filesystem running. We know any error is
382 * local to this block.
384 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &xb->xb_check);
389 * Errors after here are fatal
392 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
394 "Extended attribute block #%llu has bad "
396 (unsigned long long)bh->b_blocknr, 7,
401 if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) {
403 "Extended attribute block #%llu has an "
404 "invalid xb_blkno of %llu",
405 (unsigned long long)bh->b_blocknr,
406 (unsigned long long)le64_to_cpu(xb->xb_blkno));
410 if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) {
412 "Extended attribute block #%llu has an invalid "
413 "xb_fs_generation of #%u",
414 (unsigned long long)bh->b_blocknr,
415 le32_to_cpu(xb->xb_fs_generation));
422 static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno,
423 struct buffer_head **bh)
426 struct buffer_head *tmp = *bh;
428 rc = ocfs2_read_block(INODE_CACHE(inode), xb_blkno, &tmp,
429 ocfs2_validate_xattr_block);
431 /* If ocfs2_read_block() got us a new bh, pass it up. */
438 static inline const char *ocfs2_xattr_prefix(int name_index)
440 struct xattr_handler *handler = NULL;
442 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
443 handler = ocfs2_xattr_handler_map[name_index];
445 return handler ? handler->prefix : NULL;
448 static u32 ocfs2_xattr_name_hash(struct inode *inode,
452 /* Get hash value of uuid from super block */
453 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
456 /* hash extended attribute name */
457 for (i = 0; i < name_len; i++) {
458 hash = (hash << OCFS2_HASH_SHIFT) ^
459 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
467 * ocfs2_xattr_hash_entry()
469 * Compute the hash of an extended attribute.
471 static void ocfs2_xattr_hash_entry(struct inode *inode,
472 struct ocfs2_xattr_header *header,
473 struct ocfs2_xattr_entry *entry)
476 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
478 hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
479 entry->xe_name_hash = cpu_to_le32(hash);
484 static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
488 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
489 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
491 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
492 size += sizeof(struct ocfs2_xattr_entry);
497 int ocfs2_calc_security_init(struct inode *dir,
498 struct ocfs2_security_xattr_info *si,
501 struct ocfs2_alloc_context **xattr_ac)
504 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
505 int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
509 * The max space of security xattr taken inline is
510 * 256(name) + 80(value) + 16(entry) = 352 bytes,
511 * So reserve one metadata block for it is ok.
513 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
514 s_size > OCFS2_XATTR_FREE_IN_IBODY) {
515 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
520 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
523 /* reserve clusters for xattr value which will be set in B tree*/
524 if (si->value_len > OCFS2_XATTR_INLINE_SIZE) {
525 int new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
528 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
530 *want_clusters += new_clusters;
535 int ocfs2_calc_xattr_init(struct inode *dir,
536 struct buffer_head *dir_bh,
538 struct ocfs2_security_xattr_info *si,
544 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
545 int s_size = 0, a_size = 0, acl_len = 0, new_clusters;
548 s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
551 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
552 acl_len = ocfs2_xattr_get_nolock(dir, dir_bh,
553 OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
556 a_size = ocfs2_xattr_entry_real_size(0, acl_len);
559 } else if (acl_len != 0 && acl_len != -ENODATA) {
565 if (!(s_size + a_size))
569 * The max space of security xattr taken inline is
570 * 256(name) + 80(value) + 16(entry) = 352 bytes,
571 * The max space of acl xattr taken inline is
572 * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
573 * when blocksize = 512, may reserve one more cluser for
574 * xattr bucket, otherwise reserve one metadata block
576 * If this is a new directory with inline data,
577 * we choose to reserve the entire inline area for
578 * directory contents and force an external xattr block.
580 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
581 (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) ||
582 (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) {
583 *want_meta = *want_meta + 1;
584 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
587 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
588 (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
590 *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
594 * reserve credits and clusters for xattrs which has large value
595 * and have to be set outside
597 if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE) {
598 new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
600 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
602 *want_clusters += new_clusters;
604 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
605 acl_len > OCFS2_XATTR_INLINE_SIZE) {
606 /* for directory, it has DEFAULT and ACCESS two types of acls */
607 new_clusters = (S_ISDIR(mode) ? 2 : 1) *
608 ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
609 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
611 *want_clusters += new_clusters;
617 static int ocfs2_xattr_extend_allocation(struct inode *inode,
619 struct ocfs2_xattr_value_buf *vb,
620 struct ocfs2_xattr_set_ctxt *ctxt)
623 handle_t *handle = ctxt->handle;
624 enum ocfs2_alloc_restarted why;
625 u32 prev_clusters, logical_start = le32_to_cpu(vb->vb_xv->xr_clusters);
626 struct ocfs2_extent_tree et;
628 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
630 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
632 status = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
633 OCFS2_JOURNAL_ACCESS_WRITE);
639 prev_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
640 status = ocfs2_add_clusters_in_btree(handle,
653 status = ocfs2_journal_dirty(handle, vb->vb_bh);
659 clusters_to_add -= le32_to_cpu(vb->vb_xv->xr_clusters) - prev_clusters;
662 * We should have already allocated enough space before the transaction,
663 * so no need to restart.
665 BUG_ON(why != RESTART_NONE || clusters_to_add);
672 static int __ocfs2_remove_xattr_range(struct inode *inode,
673 struct ocfs2_xattr_value_buf *vb,
674 u32 cpos, u32 phys_cpos, u32 len,
675 unsigned int ext_flags,
676 struct ocfs2_xattr_set_ctxt *ctxt)
679 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
680 handle_t *handle = ctxt->handle;
681 struct ocfs2_extent_tree et;
683 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
685 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
686 OCFS2_JOURNAL_ACCESS_WRITE);
692 ret = ocfs2_remove_extent(handle, &et, cpos, len, ctxt->meta_ac,
699 le32_add_cpu(&vb->vb_xv->xr_clusters, -len);
701 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
707 if (ext_flags & OCFS2_EXT_REFCOUNTED)
708 ret = ocfs2_decrease_refcount(inode, handle,
709 ocfs2_blocks_to_clusters(inode->i_sb,
711 len, ctxt->meta_ac, &ctxt->dealloc, 1);
713 ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc,
722 static int ocfs2_xattr_shrink_size(struct inode *inode,
725 struct ocfs2_xattr_value_buf *vb,
726 struct ocfs2_xattr_set_ctxt *ctxt)
729 unsigned int ext_flags;
730 u32 trunc_len, cpos, phys_cpos, alloc_size;
733 if (old_clusters <= new_clusters)
737 trunc_len = old_clusters - new_clusters;
739 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
741 &vb->vb_xv->xr_list, &ext_flags);
747 if (alloc_size > trunc_len)
748 alloc_size = trunc_len;
750 ret = __ocfs2_remove_xattr_range(inode, vb, cpos,
751 phys_cpos, alloc_size,
758 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
759 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode),
762 trunc_len -= alloc_size;
769 static int ocfs2_xattr_value_truncate(struct inode *inode,
770 struct ocfs2_xattr_value_buf *vb,
772 struct ocfs2_xattr_set_ctxt *ctxt)
775 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
776 u32 old_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
778 if (new_clusters == old_clusters)
781 if (new_clusters > old_clusters)
782 ret = ocfs2_xattr_extend_allocation(inode,
783 new_clusters - old_clusters,
786 ret = ocfs2_xattr_shrink_size(inode,
787 old_clusters, new_clusters,
793 static int ocfs2_xattr_list_entry(char *buffer, size_t size,
794 size_t *result, const char *prefix,
795 const char *name, int name_len)
797 char *p = buffer + *result;
798 int prefix_len = strlen(prefix);
799 int total_len = prefix_len + name_len + 1;
801 *result += total_len;
803 /* we are just looking for how big our buffer needs to be */
810 memcpy(p, prefix, prefix_len);
811 memcpy(p + prefix_len, name, name_len);
812 p[prefix_len + name_len] = '\0';
817 static int ocfs2_xattr_list_entries(struct inode *inode,
818 struct ocfs2_xattr_header *header,
819 char *buffer, size_t buffer_size)
823 const char *prefix, *name;
825 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
826 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
827 type = ocfs2_xattr_get_type(entry);
828 prefix = ocfs2_xattr_prefix(type);
831 name = (const char *)header +
832 le16_to_cpu(entry->xe_name_offset);
834 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
835 &result, prefix, name,
845 int ocfs2_has_inline_xattr_value_outside(struct inode *inode,
846 struct ocfs2_dinode *di)
848 struct ocfs2_xattr_header *xh;
851 xh = (struct ocfs2_xattr_header *)
852 ((void *)di + inode->i_sb->s_blocksize -
853 le16_to_cpu(di->i_xattr_inline_size));
855 for (i = 0; i < le16_to_cpu(xh->xh_count); i++)
856 if (!ocfs2_xattr_is_local(&xh->xh_entries[i]))
862 static int ocfs2_xattr_ibody_list(struct inode *inode,
863 struct ocfs2_dinode *di,
867 struct ocfs2_xattr_header *header = NULL;
868 struct ocfs2_inode_info *oi = OCFS2_I(inode);
871 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
874 header = (struct ocfs2_xattr_header *)
875 ((void *)di + inode->i_sb->s_blocksize -
876 le16_to_cpu(di->i_xattr_inline_size));
878 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
883 static int ocfs2_xattr_block_list(struct inode *inode,
884 struct ocfs2_dinode *di,
888 struct buffer_head *blk_bh = NULL;
889 struct ocfs2_xattr_block *xb;
892 if (!di->i_xattr_loc)
895 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
902 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
903 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
904 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
905 ret = ocfs2_xattr_list_entries(inode, header,
906 buffer, buffer_size);
908 ret = ocfs2_xattr_tree_list_index_block(inode, blk_bh,
909 buffer, buffer_size);
916 ssize_t ocfs2_listxattr(struct dentry *dentry,
920 int ret = 0, i_ret = 0, b_ret = 0;
921 struct buffer_head *di_bh = NULL;
922 struct ocfs2_dinode *di = NULL;
923 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
925 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
928 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
931 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
937 di = (struct ocfs2_dinode *)di_bh->b_data;
939 down_read(&oi->ip_xattr_sem);
940 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
948 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
953 up_read(&oi->ip_xattr_sem);
954 ocfs2_inode_unlock(dentry->d_inode, 0);
958 return i_ret + b_ret;
961 static int ocfs2_xattr_find_entry(int name_index,
963 struct ocfs2_xattr_search *xs)
965 struct ocfs2_xattr_entry *entry;
972 name_len = strlen(name);
974 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
975 cmp = name_index - ocfs2_xattr_get_type(entry);
977 cmp = name_len - entry->xe_name_len;
979 cmp = memcmp(name, (xs->base +
980 le16_to_cpu(entry->xe_name_offset)),
988 return cmp ? -ENODATA : 0;
991 static int ocfs2_xattr_get_value_outside(struct inode *inode,
992 struct ocfs2_xattr_value_root *xv,
996 u32 cpos, p_cluster, num_clusters, bpc, clusters;
999 size_t cplen, blocksize;
1000 struct buffer_head *bh = NULL;
1001 struct ocfs2_extent_list *el;
1004 clusters = le32_to_cpu(xv->xr_clusters);
1005 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1006 blocksize = inode->i_sb->s_blocksize;
1009 while (cpos < clusters) {
1010 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1011 &num_clusters, el, NULL);
1017 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1018 /* Copy ocfs2_xattr_value */
1019 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1020 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1027 cplen = len >= blocksize ? blocksize : len;
1028 memcpy(buffer, bh->b_data, cplen);
1037 cpos += num_clusters;
1043 static int ocfs2_xattr_ibody_get(struct inode *inode,
1048 struct ocfs2_xattr_search *xs)
1050 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1051 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1052 struct ocfs2_xattr_value_root *xv;
1056 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
1059 xs->end = (void *)di + inode->i_sb->s_blocksize;
1060 xs->header = (struct ocfs2_xattr_header *)
1061 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1062 xs->base = (void *)xs->header;
1063 xs->here = xs->header->xh_entries;
1065 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1068 size = le64_to_cpu(xs->here->xe_value_size);
1070 if (size > buffer_size)
1072 if (ocfs2_xattr_is_local(xs->here)) {
1073 memcpy(buffer, (void *)xs->base +
1074 le16_to_cpu(xs->here->xe_name_offset) +
1075 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
1077 xv = (struct ocfs2_xattr_value_root *)
1078 (xs->base + le16_to_cpu(
1079 xs->here->xe_name_offset) +
1080 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
1081 ret = ocfs2_xattr_get_value_outside(inode, xv,
1093 static int ocfs2_xattr_block_get(struct inode *inode,
1098 struct ocfs2_xattr_search *xs)
1100 struct ocfs2_xattr_block *xb;
1101 struct ocfs2_xattr_value_root *xv;
1103 int ret = -ENODATA, name_offset, name_len, i;
1104 int uninitialized_var(block_off);
1106 xs->bucket = ocfs2_xattr_bucket_new(inode);
1113 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
1119 if (xs->not_found) {
1124 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1125 size = le64_to_cpu(xs->here->xe_value_size);
1128 if (size > buffer_size)
1131 name_offset = le16_to_cpu(xs->here->xe_name_offset);
1132 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
1133 i = xs->here - xs->header->xh_entries;
1135 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
1136 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
1137 bucket_xh(xs->bucket),
1141 xs->base = bucket_block(xs->bucket, block_off);
1143 if (ocfs2_xattr_is_local(xs->here)) {
1144 memcpy(buffer, (void *)xs->base +
1145 name_offset + name_len, size);
1147 xv = (struct ocfs2_xattr_value_root *)
1148 (xs->base + name_offset + name_len);
1149 ret = ocfs2_xattr_get_value_outside(inode, xv,
1159 ocfs2_xattr_bucket_free(xs->bucket);
1161 brelse(xs->xattr_bh);
1162 xs->xattr_bh = NULL;
1166 int ocfs2_xattr_get_nolock(struct inode *inode,
1167 struct buffer_head *di_bh,
1174 struct ocfs2_dinode *di = NULL;
1175 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1176 struct ocfs2_xattr_search xis = {
1177 .not_found = -ENODATA,
1179 struct ocfs2_xattr_search xbs = {
1180 .not_found = -ENODATA,
1183 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1186 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1189 xis.inode_bh = xbs.inode_bh = di_bh;
1190 di = (struct ocfs2_dinode *)di_bh->b_data;
1192 down_read(&oi->ip_xattr_sem);
1193 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1195 if (ret == -ENODATA && di->i_xattr_loc)
1196 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1198 up_read(&oi->ip_xattr_sem);
1203 /* ocfs2_xattr_get()
1205 * Copy an extended attribute into the buffer provided.
1206 * Buffer is NULL to compute the size of buffer required.
1208 static int ocfs2_xattr_get(struct inode *inode,
1215 struct buffer_head *di_bh = NULL;
1217 ret = ocfs2_inode_lock(inode, &di_bh, 0);
1222 ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
1223 name, buffer, buffer_size);
1225 ocfs2_inode_unlock(inode, 0);
1232 static int __ocfs2_xattr_set_value_outside(struct inode *inode,
1234 struct ocfs2_xattr_value_buf *vb,
1238 int ret = 0, i, cp_len;
1239 u16 blocksize = inode->i_sb->s_blocksize;
1240 u32 p_cluster, num_clusters;
1241 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1242 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1244 struct buffer_head *bh = NULL;
1245 unsigned int ext_flags;
1246 struct ocfs2_xattr_value_root *xv = vb->vb_xv;
1248 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1250 while (cpos < clusters) {
1251 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1252 &num_clusters, &xv->xr_list,
1259 BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED);
1261 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1263 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1264 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1271 ret = ocfs2_journal_access(handle,
1274 OCFS2_JOURNAL_ACCESS_WRITE);
1280 cp_len = value_len > blocksize ? blocksize : value_len;
1281 memcpy(bh->b_data, value, cp_len);
1282 value_len -= cp_len;
1284 if (cp_len < blocksize)
1285 memset(bh->b_data + cp_len, 0,
1286 blocksize - cp_len);
1288 ret = ocfs2_journal_dirty(handle, bh);
1297 * XXX: do we need to empty all the following
1298 * blocks in this cluster?
1303 cpos += num_clusters;
1311 static int ocfs2_xattr_cleanup(struct inode *inode,
1313 struct ocfs2_xattr_info *xi,
1314 struct ocfs2_xattr_search *xs,
1315 struct ocfs2_xattr_value_buf *vb,
1319 size_t name_len = strlen(xi->name);
1320 void *val = xs->base + offs;
1321 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1323 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
1324 OCFS2_JOURNAL_ACCESS_WRITE);
1329 /* Decrease xattr count */
1330 le16_add_cpu(&xs->header->xh_count, -1);
1331 /* Remove the xattr entry and tree root which has already be set*/
1332 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
1333 memset(val, 0, size);
1335 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
1342 static int ocfs2_xattr_update_entry(struct inode *inode,
1344 struct ocfs2_xattr_info *xi,
1345 struct ocfs2_xattr_search *xs,
1346 struct ocfs2_xattr_value_buf *vb,
1351 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
1352 OCFS2_JOURNAL_ACCESS_WRITE);
1358 xs->here->xe_name_offset = cpu_to_le16(offs);
1359 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1360 if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
1361 ocfs2_xattr_set_local(xs->here, 1);
1363 ocfs2_xattr_set_local(xs->here, 0);
1364 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1366 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
1374 * ocfs2_xattr_set_value_outside()
1376 * Set large size value in B tree.
1378 static int ocfs2_xattr_set_value_outside(struct inode *inode,
1379 struct ocfs2_xattr_info *xi,
1380 struct ocfs2_xattr_search *xs,
1381 struct ocfs2_xattr_set_ctxt *ctxt,
1382 struct ocfs2_xattr_value_buf *vb,
1385 size_t name_len = strlen(xi->name);
1386 void *val = xs->base + offs;
1387 struct ocfs2_xattr_value_root *xv = NULL;
1388 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1391 memset(val, 0, size);
1392 memcpy(val, xi->name, name_len);
1393 xv = (struct ocfs2_xattr_value_root *)
1394 (val + OCFS2_XATTR_SIZE(name_len));
1395 xv->xr_clusters = 0;
1396 xv->xr_last_eb_blk = 0;
1397 xv->xr_list.l_tree_depth = 0;
1398 xv->xr_list.l_count = cpu_to_le16(1);
1399 xv->xr_list.l_next_free_rec = 0;
1402 ret = ocfs2_xattr_value_truncate(inode, vb, xi->value_len, ctxt);
1407 ret = ocfs2_xattr_update_entry(inode, ctxt->handle, xi, xs, vb, offs);
1412 ret = __ocfs2_xattr_set_value_outside(inode, ctxt->handle, vb,
1413 xi->value, xi->value_len);
1421 * ocfs2_xattr_set_entry_local()
1423 * Set, replace or remove extended attribute in local.
1425 static void ocfs2_xattr_set_entry_local(struct inode *inode,
1426 struct ocfs2_xattr_info *xi,
1427 struct ocfs2_xattr_search *xs,
1428 struct ocfs2_xattr_entry *last,
1431 size_t name_len = strlen(xi->name);
1434 if (xi->value && xs->not_found) {
1435 /* Insert the new xattr entry. */
1436 le16_add_cpu(&xs->header->xh_count, 1);
1437 ocfs2_xattr_set_type(last, xi->name_index);
1438 ocfs2_xattr_set_local(last, 1);
1439 last->xe_name_len = name_len;
1445 first_val = xs->base + min_offs;
1446 offs = le16_to_cpu(xs->here->xe_name_offset);
1447 val = xs->base + offs;
1449 if (le64_to_cpu(xs->here->xe_value_size) >
1450 OCFS2_XATTR_INLINE_SIZE)
1451 size = OCFS2_XATTR_SIZE(name_len) +
1452 OCFS2_XATTR_ROOT_SIZE;
1454 size = OCFS2_XATTR_SIZE(name_len) +
1455 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1457 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1458 OCFS2_XATTR_SIZE(xi->value_len)) {
1459 /* The old and the new value have the
1460 same size. Just replace the value. */
1461 ocfs2_xattr_set_local(xs->here, 1);
1462 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1463 /* Clear value bytes. */
1464 memset(val + OCFS2_XATTR_SIZE(name_len),
1466 OCFS2_XATTR_SIZE(xi->value_len));
1467 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1472 /* Remove the old name+value. */
1473 memmove(first_val + size, first_val, val - first_val);
1474 memset(first_val, 0, size);
1475 xs->here->xe_name_hash = 0;
1476 xs->here->xe_name_offset = 0;
1477 ocfs2_xattr_set_local(xs->here, 1);
1478 xs->here->xe_value_size = 0;
1482 /* Adjust all value offsets. */
1483 last = xs->header->xh_entries;
1484 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1485 size_t o = le16_to_cpu(last->xe_name_offset);
1488 last->xe_name_offset = cpu_to_le16(o + size);
1493 /* Remove the old entry. */
1495 memmove(xs->here, xs->here + 1,
1496 (void *)last - (void *)xs->here);
1497 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1498 le16_add_cpu(&xs->header->xh_count, -1);
1502 /* Insert the new name+value. */
1503 size_t size = OCFS2_XATTR_SIZE(name_len) +
1504 OCFS2_XATTR_SIZE(xi->value_len);
1505 void *val = xs->base + min_offs - size;
1507 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1508 memset(val, 0, size);
1509 memcpy(val, xi->name, name_len);
1510 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1513 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1514 ocfs2_xattr_set_local(xs->here, 1);
1515 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1522 * ocfs2_xattr_set_entry()
1524 * Set extended attribute entry into inode or block.
1526 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1527 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1528 * then set value in B tree with set_value_outside().
1530 static int ocfs2_xattr_set_entry(struct inode *inode,
1531 struct ocfs2_xattr_info *xi,
1532 struct ocfs2_xattr_search *xs,
1533 struct ocfs2_xattr_set_ctxt *ctxt,
1536 struct ocfs2_xattr_entry *last;
1537 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1538 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1539 size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1541 handle_t *handle = ctxt->handle;
1543 struct ocfs2_xattr_info xi_l = {
1544 .name_index = xi->name_index,
1547 .value_len = xi->value_len,
1549 struct ocfs2_xattr_value_buf vb = {
1550 .vb_bh = xs->xattr_bh,
1551 .vb_access = ocfs2_journal_access_di,
1554 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1555 BUG_ON(xs->xattr_bh == xs->inode_bh);
1556 vb.vb_access = ocfs2_journal_access_xb;
1558 BUG_ON(xs->xattr_bh != xs->inode_bh);
1560 /* Compute min_offs, last and free space. */
1561 last = xs->header->xh_entries;
1563 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1564 size_t offs = le16_to_cpu(last->xe_name_offset);
1565 if (offs < min_offs)
1570 free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
1574 if (!xs->not_found) {
1576 if (ocfs2_xattr_is_local(xs->here))
1577 size = OCFS2_XATTR_SIZE(name_len) +
1578 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1580 size = OCFS2_XATTR_SIZE(name_len) +
1581 OCFS2_XATTR_ROOT_SIZE;
1582 free += (size + sizeof(struct ocfs2_xattr_entry));
1584 /* Check free space in inode or block */
1585 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1586 if (free < sizeof(struct ocfs2_xattr_entry) +
1587 OCFS2_XATTR_SIZE(name_len) +
1588 OCFS2_XATTR_ROOT_SIZE) {
1592 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1593 xi_l.value = (void *)&def_xv;
1594 xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
1595 } else if (xi->value) {
1596 if (free < sizeof(struct ocfs2_xattr_entry) +
1597 OCFS2_XATTR_SIZE(name_len) +
1598 OCFS2_XATTR_SIZE(xi->value_len)) {
1604 if (!xs->not_found) {
1605 /* For existing extended attribute */
1606 size_t size = OCFS2_XATTR_SIZE(name_len) +
1607 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1608 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1609 void *val = xs->base + offs;
1611 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1612 /* Replace existing local xattr with tree root */
1613 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
1618 } else if (!ocfs2_xattr_is_local(xs->here)) {
1619 /* For existing xattr which has value outside */
1620 vb.vb_xv = (struct ocfs2_xattr_value_root *)
1621 (val + OCFS2_XATTR_SIZE(name_len));
1623 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1625 * If new value need set outside also,
1626 * first truncate old value to new value,
1627 * then set new value with set_value_outside().
1629 ret = ocfs2_xattr_value_truncate(inode,
1638 ret = ocfs2_xattr_update_entry(inode,
1649 ret = __ocfs2_xattr_set_value_outside(inode,
1659 * If new value need set in local,
1660 * just trucate old value to zero.
1662 ret = ocfs2_xattr_value_truncate(inode,
1672 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), xs->inode_bh,
1673 OCFS2_JOURNAL_ACCESS_WRITE);
1679 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1680 ret = vb.vb_access(handle, INODE_CACHE(inode), vb.vb_bh,
1681 OCFS2_JOURNAL_ACCESS_WRITE);
1689 * Set value in local, include set tree root in local.
1690 * This is the first step for value size >INLINE_SIZE.
1692 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1694 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1695 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1702 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1703 (flag & OCFS2_INLINE_XATTR_FL)) {
1704 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1705 unsigned int xattrsize = osb->s_xattr_inline_size;
1708 * Adjust extent record count or inline data size
1709 * to reserve space for extended attribute.
1711 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1712 struct ocfs2_inline_data *idata = &di->id2.i_data;
1713 le16_add_cpu(&idata->id_count, -xattrsize);
1714 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1715 struct ocfs2_extent_list *el = &di->id2.i_list;
1716 le16_add_cpu(&el->l_count, -(xattrsize /
1717 sizeof(struct ocfs2_extent_rec)));
1719 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1721 /* Update xattr flag */
1722 spin_lock(&oi->ip_lock);
1723 oi->ip_dyn_features |= flag;
1724 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1725 spin_unlock(&oi->ip_lock);
1727 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1731 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1733 * Set value outside in B tree.
1734 * This is the second step for value size > INLINE_SIZE.
1736 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1737 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, ctxt,
1744 * If set value outside failed, we have to clean
1745 * the junk tree root we have already set in local.
1747 ret2 = ocfs2_xattr_cleanup(inode, ctxt->handle,
1758 * In xattr remove, if it is stored outside and refcounted, we may have
1759 * the chance to split the refcount tree. So need the allocators.
1761 static int ocfs2_lock_xattr_remove_allocators(struct inode *inode,
1762 struct ocfs2_xattr_value_root *xv,
1763 struct ocfs2_caching_info *ref_ci,
1764 struct buffer_head *ref_root_bh,
1765 struct ocfs2_alloc_context **meta_ac,
1768 int ret, meta_add = 0;
1769 u32 p_cluster, num_clusters;
1770 unsigned int ext_flags;
1773 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
1782 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
1785 ret = ocfs2_refcounted_xattr_delete_need(inode, ref_ci,
1787 &meta_add, ref_credits);
1793 ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
1802 static int ocfs2_remove_value_outside(struct inode*inode,
1803 struct ocfs2_xattr_value_buf *vb,
1804 struct ocfs2_xattr_header *header,
1805 struct ocfs2_caching_info *ref_ci,
1806 struct buffer_head *ref_root_bh)
1808 int ret = 0, i, ref_credits;
1809 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1810 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
1813 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
1815 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1816 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1818 if (ocfs2_xattr_is_local(entry))
1821 val = (void *)header +
1822 le16_to_cpu(entry->xe_name_offset);
1823 vb->vb_xv = (struct ocfs2_xattr_value_root *)
1824 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
1826 ret = ocfs2_lock_xattr_remove_allocators(inode, vb->vb_xv,
1827 ref_ci, ref_root_bh,
1831 ctxt.handle = ocfs2_start_trans(osb, ref_credits +
1832 ocfs2_remove_extent_credits(osb->sb));
1833 if (IS_ERR(ctxt.handle)) {
1834 ret = PTR_ERR(ctxt.handle);
1839 ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt);
1845 ocfs2_commit_trans(osb, ctxt.handle);
1847 ocfs2_free_alloc_context(ctxt.meta_ac);
1848 ctxt.meta_ac = NULL;
1853 ocfs2_free_alloc_context(ctxt.meta_ac);
1854 ocfs2_schedule_truncate_log_flush(osb, 1);
1855 ocfs2_run_deallocs(osb, &ctxt.dealloc);
1859 static int ocfs2_xattr_ibody_remove(struct inode *inode,
1860 struct buffer_head *di_bh,
1861 struct ocfs2_caching_info *ref_ci,
1862 struct buffer_head *ref_root_bh)
1865 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1866 struct ocfs2_xattr_header *header;
1868 struct ocfs2_xattr_value_buf vb = {
1870 .vb_access = ocfs2_journal_access_di,
1873 header = (struct ocfs2_xattr_header *)
1874 ((void *)di + inode->i_sb->s_blocksize -
1875 le16_to_cpu(di->i_xattr_inline_size));
1877 ret = ocfs2_remove_value_outside(inode, &vb, header,
1878 ref_ci, ref_root_bh);
1883 struct ocfs2_rm_xattr_bucket_para {
1884 struct ocfs2_caching_info *ref_ci;
1885 struct buffer_head *ref_root_bh;
1888 static int ocfs2_xattr_block_remove(struct inode *inode,
1889 struct buffer_head *blk_bh,
1890 struct ocfs2_caching_info *ref_ci,
1891 struct buffer_head *ref_root_bh)
1893 struct ocfs2_xattr_block *xb;
1895 struct ocfs2_xattr_value_buf vb = {
1897 .vb_access = ocfs2_journal_access_xb,
1899 struct ocfs2_rm_xattr_bucket_para args = {
1901 .ref_root_bh = ref_root_bh,
1904 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1905 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1906 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
1907 ret = ocfs2_remove_value_outside(inode, &vb, header,
1908 ref_ci, ref_root_bh);
1910 ret = ocfs2_iterate_xattr_index_block(inode,
1912 ocfs2_rm_xattr_cluster,
1918 static int ocfs2_xattr_free_block(struct inode *inode,
1920 struct ocfs2_caching_info *ref_ci,
1921 struct buffer_head *ref_root_bh)
1923 struct inode *xb_alloc_inode;
1924 struct buffer_head *xb_alloc_bh = NULL;
1925 struct buffer_head *blk_bh = NULL;
1926 struct ocfs2_xattr_block *xb;
1927 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1933 ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
1939 ret = ocfs2_xattr_block_remove(inode, blk_bh, ref_ci, ref_root_bh);
1945 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1946 blk = le64_to_cpu(xb->xb_blkno);
1947 bit = le16_to_cpu(xb->xb_suballoc_bit);
1948 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1950 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1951 EXTENT_ALLOC_SYSTEM_INODE,
1952 le16_to_cpu(xb->xb_suballoc_slot));
1953 if (!xb_alloc_inode) {
1958 mutex_lock(&xb_alloc_inode->i_mutex);
1960 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1966 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
1967 if (IS_ERR(handle)) {
1968 ret = PTR_ERR(handle);
1973 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1978 ocfs2_commit_trans(osb, handle);
1980 ocfs2_inode_unlock(xb_alloc_inode, 1);
1981 brelse(xb_alloc_bh);
1983 mutex_unlock(&xb_alloc_inode->i_mutex);
1984 iput(xb_alloc_inode);
1991 * ocfs2_xattr_remove()
1993 * Free extended attribute resources associated with this inode.
1995 int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
1997 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1998 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1999 struct ocfs2_refcount_tree *ref_tree = NULL;
2000 struct buffer_head *ref_root_bh = NULL;
2001 struct ocfs2_caching_info *ref_ci = NULL;
2005 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2008 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
2011 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) {
2012 ret = ocfs2_lock_refcount_tree(OCFS2_SB(inode->i_sb),
2013 le64_to_cpu(di->i_refcount_loc),
2014 1, &ref_tree, &ref_root_bh);
2019 ref_ci = &ref_tree->rf_ci;
2023 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2024 ret = ocfs2_xattr_ibody_remove(inode, di_bh,
2025 ref_ci, ref_root_bh);
2032 if (di->i_xattr_loc) {
2033 ret = ocfs2_xattr_free_block(inode,
2034 le64_to_cpu(di->i_xattr_loc),
2035 ref_ci, ref_root_bh);
2042 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
2043 OCFS2_INODE_UPDATE_CREDITS);
2044 if (IS_ERR(handle)) {
2045 ret = PTR_ERR(handle);
2049 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
2050 OCFS2_JOURNAL_ACCESS_WRITE);
2056 di->i_xattr_loc = 0;
2058 spin_lock(&oi->ip_lock);
2059 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
2060 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2061 spin_unlock(&oi->ip_lock);
2063 ret = ocfs2_journal_dirty(handle, di_bh);
2067 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
2070 ocfs2_unlock_refcount_tree(OCFS2_SB(inode->i_sb), ref_tree, 1);
2071 brelse(ref_root_bh);
2075 static int ocfs2_xattr_has_space_inline(struct inode *inode,
2076 struct ocfs2_dinode *di)
2078 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2079 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
2082 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
2085 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2086 struct ocfs2_inline_data *idata = &di->id2.i_data;
2087 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
2088 } else if (ocfs2_inode_is_fast_symlink(inode)) {
2089 free = ocfs2_fast_symlink_chars(inode->i_sb) -
2090 le64_to_cpu(di->i_size);
2092 struct ocfs2_extent_list *el = &di->id2.i_list;
2093 free = (le16_to_cpu(el->l_count) -
2094 le16_to_cpu(el->l_next_free_rec)) *
2095 sizeof(struct ocfs2_extent_rec);
2097 if (free >= xattrsize)
2104 * ocfs2_xattr_ibody_find()
2106 * Find extended attribute in inode block and
2107 * fill search info into struct ocfs2_xattr_search.
2109 static int ocfs2_xattr_ibody_find(struct inode *inode,
2112 struct ocfs2_xattr_search *xs)
2114 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2115 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2119 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2122 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2123 down_read(&oi->ip_alloc_sem);
2124 has_space = ocfs2_xattr_has_space_inline(inode, di);
2125 up_read(&oi->ip_alloc_sem);
2130 xs->xattr_bh = xs->inode_bh;
2131 xs->end = (void *)di + inode->i_sb->s_blocksize;
2132 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
2133 xs->header = (struct ocfs2_xattr_header *)
2134 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
2136 xs->header = (struct ocfs2_xattr_header *)
2137 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
2138 xs->base = (void *)xs->header;
2139 xs->here = xs->header->xh_entries;
2141 /* Find the named attribute. */
2142 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2143 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2144 if (ret && ret != -ENODATA)
2146 xs->not_found = ret;
2153 * ocfs2_xattr_ibody_set()
2155 * Set, replace or remove an extended attribute into inode block.
2158 static int ocfs2_xattr_ibody_set(struct inode *inode,
2159 struct ocfs2_xattr_info *xi,
2160 struct ocfs2_xattr_search *xs,
2161 struct ocfs2_xattr_set_ctxt *ctxt)
2163 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2164 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2167 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2170 down_write(&oi->ip_alloc_sem);
2171 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2172 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2178 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
2179 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
2181 up_write(&oi->ip_alloc_sem);
2187 * ocfs2_xattr_block_find()
2189 * Find extended attribute in external block and
2190 * fill search info into struct ocfs2_xattr_search.
2192 static int ocfs2_xattr_block_find(struct inode *inode,
2195 struct ocfs2_xattr_search *xs)
2197 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2198 struct buffer_head *blk_bh = NULL;
2199 struct ocfs2_xattr_block *xb;
2202 if (!di->i_xattr_loc)
2205 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
2212 xs->xattr_bh = blk_bh;
2213 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2215 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2216 xs->header = &xb->xb_attrs.xb_header;
2217 xs->base = (void *)xs->header;
2218 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
2219 xs->here = xs->header->xh_entries;
2221 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2223 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
2227 if (ret && ret != -ENODATA) {
2228 xs->xattr_bh = NULL;
2231 xs->not_found = ret;
2239 static int ocfs2_create_xattr_block(handle_t *handle,
2240 struct inode *inode,
2241 struct buffer_head *inode_bh,
2242 struct ocfs2_alloc_context *meta_ac,
2243 struct buffer_head **ret_bh,
2247 u16 suballoc_bit_start;
2250 struct ocfs2_dinode *di = (struct ocfs2_dinode *)inode_bh->b_data;
2251 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2252 struct buffer_head *new_bh = NULL;
2253 struct ocfs2_xattr_block *xblk;
2255 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), inode_bh,
2256 OCFS2_JOURNAL_ACCESS_CREATE);
2262 ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
2263 &suballoc_bit_start, &num_got,
2270 new_bh = sb_getblk(inode->i_sb, first_blkno);
2271 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
2273 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode),
2275 OCFS2_JOURNAL_ACCESS_CREATE);
2281 /* Initialize ocfs2_xattr_block */
2282 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2283 memset(xblk, 0, inode->i_sb->s_blocksize);
2284 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
2285 xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
2286 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
2287 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
2288 xblk->xb_blkno = cpu_to_le64(first_blkno);
2291 struct ocfs2_xattr_tree_root *xr = &xblk->xb_attrs.xb_root;
2292 xr->xt_clusters = cpu_to_le32(1);
2293 xr->xt_last_eb_blk = 0;
2294 xr->xt_list.l_tree_depth = 0;
2295 xr->xt_list.l_count = cpu_to_le16(
2296 ocfs2_xattr_recs_per_xb(inode->i_sb));
2297 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2298 xblk->xb_flags = cpu_to_le16(OCFS2_XATTR_INDEXED);
2301 ret = ocfs2_journal_dirty(handle, new_bh);
2306 di->i_xattr_loc = cpu_to_le64(first_blkno);
2307 ocfs2_journal_dirty(handle, inode_bh);
2318 * ocfs2_xattr_block_set()
2320 * Set, replace or remove an extended attribute into external block.
2323 static int ocfs2_xattr_block_set(struct inode *inode,
2324 struct ocfs2_xattr_info *xi,
2325 struct ocfs2_xattr_search *xs,
2326 struct ocfs2_xattr_set_ctxt *ctxt)
2328 struct buffer_head *new_bh = NULL;
2329 handle_t *handle = ctxt->handle;
2330 struct ocfs2_xattr_block *xblk = NULL;
2333 if (!xs->xattr_bh) {
2334 ret = ocfs2_create_xattr_block(handle, inode, xs->inode_bh,
2335 ctxt->meta_ac, &new_bh, 0);
2341 xs->xattr_bh = new_bh;
2342 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2343 xs->header = &xblk->xb_attrs.xb_header;
2344 xs->base = (void *)xs->header;
2345 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2346 xs->here = xs->header->xh_entries;
2348 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2350 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
2351 /* Set extended attribute into external block */
2352 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
2353 OCFS2_HAS_XATTR_FL);
2354 if (!ret || ret != -ENOSPC)
2357 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
2362 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
2369 /* Check whether the new xattr can be inserted into the inode. */
2370 static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
2371 struct ocfs2_xattr_info *xi,
2372 struct ocfs2_xattr_search *xs)
2375 struct ocfs2_xattr_entry *last;
2377 size_t min_offs = xs->end - xs->base;
2382 last = xs->header->xh_entries;
2384 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
2385 size_t offs = le16_to_cpu(last->xe_name_offset);
2386 if (offs < min_offs)
2391 free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
2395 BUG_ON(!xs->not_found);
2397 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
2398 value_size = OCFS2_XATTR_ROOT_SIZE;
2400 value_size = OCFS2_XATTR_SIZE(xi->value_len);
2402 if (free >= sizeof(struct ocfs2_xattr_entry) +
2403 OCFS2_XATTR_SIZE(strlen(xi->name)) + value_size)
2409 static int ocfs2_calc_xattr_set_need(struct inode *inode,
2410 struct ocfs2_dinode *di,
2411 struct ocfs2_xattr_info *xi,
2412 struct ocfs2_xattr_search *xis,
2413 struct ocfs2_xattr_search *xbs,
2418 int ret = 0, old_in_xb = 0;
2419 int clusters_add = 0, meta_add = 0, credits = 0;
2420 struct buffer_head *bh = NULL;
2421 struct ocfs2_xattr_block *xb = NULL;
2422 struct ocfs2_xattr_entry *xe = NULL;
2423 struct ocfs2_xattr_value_root *xv = NULL;
2425 int name_offset, name_len = 0;
2426 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2431 * Calculate the clusters we need to write.
2432 * No matter whether we replace an old one or add a new one,
2433 * we need this for writing.
2435 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
2436 credits += new_clusters *
2437 ocfs2_clusters_to_blocks(inode->i_sb, 1);
2439 if (xis->not_found && xbs->not_found) {
2440 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2442 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
2443 clusters_add += new_clusters;
2444 credits += ocfs2_calc_extend_credits(inode->i_sb,
2452 if (!xis->not_found) {
2454 name_offset = le16_to_cpu(xe->xe_name_offset);
2455 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2457 credits += OCFS2_INODE_UPDATE_CREDITS;
2459 int i, block_off = 0;
2460 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2462 name_offset = le16_to_cpu(xe->xe_name_offset);
2463 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2464 i = xbs->here - xbs->header->xh_entries;
2467 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2468 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
2469 bucket_xh(xbs->bucket),
2472 base = bucket_block(xbs->bucket, block_off);
2473 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2476 credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
2481 * delete a xattr doesn't need metadata and cluster allocation.
2482 * so just calculate the credits and return.
2484 * The credits for removing the value tree will be extended
2485 * by ocfs2_remove_extent itself.
2488 if (!ocfs2_xattr_is_local(xe))
2489 credits += ocfs2_remove_extent_credits(inode->i_sb);
2494 /* do cluster allocation guess first. */
2495 value_size = le64_to_cpu(xe->xe_value_size);
2499 * In xattr set, we always try to set the xe in inode first,
2500 * so if it can be inserted into inode successfully, the old
2501 * one will be removed from the xattr block, and this xattr
2502 * will be inserted into inode as a new xattr in inode.
2504 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
2505 clusters_add += new_clusters;
2506 credits += ocfs2_remove_extent_credits(inode->i_sb) +
2507 OCFS2_INODE_UPDATE_CREDITS;
2508 if (!ocfs2_xattr_is_local(xe))
2509 credits += ocfs2_calc_extend_credits(
2517 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
2518 /* the new values will be stored outside. */
2519 u32 old_clusters = 0;
2521 if (!ocfs2_xattr_is_local(xe)) {
2522 old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2524 xv = (struct ocfs2_xattr_value_root *)
2525 (base + name_offset + name_len);
2526 value_size = OCFS2_XATTR_ROOT_SIZE;
2530 if (old_clusters >= new_clusters) {
2531 credits += ocfs2_remove_extent_credits(inode->i_sb);
2534 meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
2535 clusters_add += new_clusters - old_clusters;
2536 credits += ocfs2_calc_extend_credits(inode->i_sb,
2540 if (value_size >= OCFS2_XATTR_ROOT_SIZE)
2545 * Now the new value will be stored inside. So if the new
2546 * value is smaller than the size of value root or the old
2547 * value, we don't need any allocation, otherwise we have
2548 * to guess metadata allocation.
2550 if ((ocfs2_xattr_is_local(xe) && value_size >= xi->value_len) ||
2551 (!ocfs2_xattr_is_local(xe) &&
2552 OCFS2_XATTR_ROOT_SIZE >= xi->value_len))
2557 /* calculate metadata allocation. */
2558 if (di->i_xattr_loc) {
2559 if (!xbs->xattr_bh) {
2560 ret = ocfs2_read_xattr_block(inode,
2561 le64_to_cpu(di->i_xattr_loc),
2568 xb = (struct ocfs2_xattr_block *)bh->b_data;
2570 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2573 * If there is already an xattr tree, good, we can calculate
2574 * like other b-trees. Otherwise we may have the chance of
2575 * create a tree, the credit calculation is borrowed from
2576 * ocfs2_calc_extend_credits with root_el = NULL. And the
2577 * new tree will be cluster based, so no meta is needed.
2579 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2580 struct ocfs2_extent_list *el =
2581 &xb->xb_attrs.xb_root.xt_list;
2582 meta_add += ocfs2_extend_meta_needed(el);
2583 credits += ocfs2_calc_extend_credits(inode->i_sb,
2586 credits += OCFS2_SUBALLOC_ALLOC + 1;
2589 * This cluster will be used either for new bucket or for
2591 * If the cluster size is the same as the bucket size, one
2592 * more is needed since we may need to extend the bucket
2596 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2597 if (OCFS2_XATTR_BUCKET_SIZE ==
2598 OCFS2_SB(inode->i_sb)->s_clustersize) {
2599 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2604 credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
2608 *clusters_need = clusters_add;
2610 *meta_need = meta_add;
2612 *credits_need = credits;
2617 static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
2618 struct ocfs2_dinode *di,
2619 struct ocfs2_xattr_info *xi,
2620 struct ocfs2_xattr_search *xis,
2621 struct ocfs2_xattr_search *xbs,
2622 struct ocfs2_xattr_set_ctxt *ctxt,
2626 int clusters_add, meta_add, ret;
2627 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2629 memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
2631 ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
2633 ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
2634 &clusters_add, &meta_add, credits);
2640 meta_add += extra_meta;
2641 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
2642 "credits = %d\n", xi->name, meta_add, clusters_add, *credits);
2645 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
2654 ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
2660 if (ctxt->meta_ac) {
2661 ocfs2_free_alloc_context(ctxt->meta_ac);
2662 ctxt->meta_ac = NULL;
2666 * We cannot have an error and a non null ctxt->data_ac.
2673 static int __ocfs2_xattr_set_handle(struct inode *inode,
2674 struct ocfs2_dinode *di,
2675 struct ocfs2_xattr_info *xi,
2676 struct ocfs2_xattr_search *xis,
2677 struct ocfs2_xattr_search *xbs,
2678 struct ocfs2_xattr_set_ctxt *ctxt)
2680 int ret = 0, credits, old_found;
2683 /* Remove existing extended attribute */
2684 if (!xis->not_found)
2685 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2686 else if (!xbs->not_found)
2687 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2689 /* We always try to set extended attribute into inode first*/
2690 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2691 if (!ret && !xbs->not_found) {
2693 * If succeed and that extended attribute existing in
2694 * external block, then we will remove it.
2699 old_found = xis->not_found;
2700 xis->not_found = -ENODATA;
2701 ret = ocfs2_calc_xattr_set_need(inode,
2709 xis->not_found = old_found;
2715 ret = ocfs2_extend_trans(ctxt->handle, credits +
2716 ctxt->handle->h_buffer_credits);
2721 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2722 } else if (ret == -ENOSPC) {
2723 if (di->i_xattr_loc && !xbs->xattr_bh) {
2724 ret = ocfs2_xattr_block_find(inode,
2730 old_found = xis->not_found;
2731 xis->not_found = -ENODATA;
2732 ret = ocfs2_calc_xattr_set_need(inode,
2740 xis->not_found = old_found;
2746 ret = ocfs2_extend_trans(ctxt->handle, credits +
2747 ctxt->handle->h_buffer_credits);
2754 * If no space in inode, we will set extended attribute
2755 * into external block.
2757 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2760 if (!xis->not_found) {
2762 * If succeed and that extended attribute
2763 * existing in inode, we will remove it.
2767 xbs->not_found = -ENODATA;
2768 ret = ocfs2_calc_xattr_set_need(inode,
2781 ret = ocfs2_extend_trans(ctxt->handle, credits +
2782 ctxt->handle->h_buffer_credits);
2787 ret = ocfs2_xattr_ibody_set(inode, xi,
2794 /* Update inode ctime. */
2795 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
2797 OCFS2_JOURNAL_ACCESS_WRITE);
2803 inode->i_ctime = CURRENT_TIME;
2804 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
2805 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
2806 ocfs2_journal_dirty(ctxt->handle, xis->inode_bh);
2813 * This function only called duing creating inode
2814 * for init security/acl xattrs of the new inode.
2815 * All transanction credits have been reserved in mknod.
2817 int ocfs2_xattr_set_handle(handle_t *handle,
2818 struct inode *inode,
2819 struct buffer_head *di_bh,
2825 struct ocfs2_alloc_context *meta_ac,
2826 struct ocfs2_alloc_context *data_ac)
2828 struct ocfs2_dinode *di;
2831 struct ocfs2_xattr_info xi = {
2832 .name_index = name_index,
2835 .value_len = value_len,
2838 struct ocfs2_xattr_search xis = {
2839 .not_found = -ENODATA,
2842 struct ocfs2_xattr_search xbs = {
2843 .not_found = -ENODATA,
2846 struct ocfs2_xattr_set_ctxt ctxt = {
2852 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2856 * In extreme situation, may need xattr bucket when
2857 * block size is too small. And we have already reserved
2858 * the credits for bucket in mknod.
2860 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) {
2861 xbs.bucket = ocfs2_xattr_bucket_new(inode);
2863 mlog_errno(-ENOMEM);
2868 xis.inode_bh = xbs.inode_bh = di_bh;
2869 di = (struct ocfs2_dinode *)di_bh->b_data;
2871 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2873 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2876 if (xis.not_found) {
2877 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2882 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
2885 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2886 brelse(xbs.xattr_bh);
2887 ocfs2_xattr_bucket_free(xbs.bucket);
2895 * Set, replace or remove an extended attribute for this inode.
2896 * value is NULL to remove an existing extended attribute, else either
2897 * create or replace an extended attribute.
2899 int ocfs2_xattr_set(struct inode *inode,
2906 struct buffer_head *di_bh = NULL;
2907 struct ocfs2_dinode *di;
2908 int ret, credits, ref_meta = 0, ref_credits = 0;
2909 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2910 struct inode *tl_inode = osb->osb_tl_inode;
2911 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
2912 struct ocfs2_refcount_tree *ref_tree = NULL;
2914 struct ocfs2_xattr_info xi = {
2915 .name_index = name_index,
2918 .value_len = value_len,
2921 struct ocfs2_xattr_search xis = {
2922 .not_found = -ENODATA,
2925 struct ocfs2_xattr_search xbs = {
2926 .not_found = -ENODATA,
2929 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2933 * Only xbs will be used on indexed trees. xis doesn't need a
2936 xbs.bucket = ocfs2_xattr_bucket_new(inode);
2938 mlog_errno(-ENOMEM);
2942 ret = ocfs2_inode_lock(inode, &di_bh, 1);
2945 goto cleanup_nolock;
2947 xis.inode_bh = xbs.inode_bh = di_bh;
2948 di = (struct ocfs2_dinode *)di_bh->b_data;
2950 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2952 * Scan inode and external block to find the same name
2953 * extended attribute and collect search infomation.
2955 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2958 if (xis.not_found) {
2959 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2964 if (xis.not_found && xbs.not_found) {
2966 if (flags & XATTR_REPLACE)
2973 if (flags & XATTR_CREATE)
2977 /* Check whether the value is refcounted and do some prepartion. */
2978 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL &&
2979 (!xis.not_found || !xbs.not_found)) {
2980 ret = ocfs2_prepare_refcount_xattr(inode, di, &xi,
2981 &xis, &xbs, &ref_tree,
2982 &ref_meta, &ref_credits);
2989 mutex_lock(&tl_inode->i_mutex);
2991 if (ocfs2_truncate_log_needs_flush(osb)) {
2992 ret = __ocfs2_flush_truncate_log(osb);
2994 mutex_unlock(&tl_inode->i_mutex);
2999 mutex_unlock(&tl_inode->i_mutex);
3001 ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
3002 &xbs, &ctxt, ref_meta, &credits);
3008 /* we need to update inode's ctime field, so add credit for it. */
3009 credits += OCFS2_INODE_UPDATE_CREDITS;
3010 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
3011 if (IS_ERR(ctxt.handle)) {
3012 ret = PTR_ERR(ctxt.handle);
3017 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3019 ocfs2_commit_trans(osb, ctxt.handle);
3022 ocfs2_free_alloc_context(ctxt.data_ac);
3024 ocfs2_free_alloc_context(ctxt.meta_ac);
3025 if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
3026 ocfs2_schedule_truncate_log_flush(osb, 1);
3027 ocfs2_run_deallocs(osb, &ctxt.dealloc);
3031 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
3032 up_write(&OCFS2_I(inode)->ip_xattr_sem);
3033 if (!value && !ret) {
3034 ret = ocfs2_try_remove_refcount_tree(inode, di_bh);
3038 ocfs2_inode_unlock(inode, 1);
3041 brelse(xbs.xattr_bh);
3042 ocfs2_xattr_bucket_free(xbs.bucket);
3048 * Find the xattr extent rec which may contains name_hash.
3049 * e_cpos will be the first name hash of the xattr rec.
3050 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
3052 static int ocfs2_xattr_get_rec(struct inode *inode,
3057 struct ocfs2_extent_list *el)
3060 struct buffer_head *eb_bh = NULL;
3061 struct ocfs2_extent_block *eb;