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);
204 static int ocfs2_xattr_security_set(struct inode *inode, const char *name,
205 const void *value, size_t size, int flags);
207 static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
209 return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
212 static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
214 return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
217 static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb)
219 u16 len = sb->s_blocksize -
220 offsetof(struct ocfs2_xattr_header, xh_entries);
222 return len / sizeof(struct ocfs2_xattr_entry);
225 #define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
226 #define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
227 #define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
229 static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
231 struct ocfs2_xattr_bucket *bucket;
232 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
234 BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
236 bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
238 bucket->bu_inode = inode;
239 bucket->bu_blocks = blks;
245 static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
249 for (i = 0; i < bucket->bu_blocks; i++) {
250 brelse(bucket->bu_bhs[i]);
251 bucket->bu_bhs[i] = NULL;
255 static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
258 ocfs2_xattr_bucket_relse(bucket);
259 bucket->bu_inode = NULL;
265 * A bucket that has never been written to disk doesn't need to be
266 * read. We just need the buffer_heads. Don't call this for
267 * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
270 static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
275 for (i = 0; i < bucket->bu_blocks; i++) {
276 bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
278 if (!bucket->bu_bhs[i]) {
284 if (!ocfs2_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
286 ocfs2_set_new_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
291 ocfs2_xattr_bucket_relse(bucket);
295 /* Read the xattr bucket at xb_blkno */
296 static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
301 rc = ocfs2_read_blocks(INODE_CACHE(bucket->bu_inode), xb_blkno,
302 bucket->bu_blocks, bucket->bu_bhs, 0,
305 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
306 rc = ocfs2_validate_meta_ecc_bhs(bucket->bu_inode->i_sb,
309 &bucket_xh(bucket)->xh_check);
310 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
316 ocfs2_xattr_bucket_relse(bucket);
320 static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
321 struct ocfs2_xattr_bucket *bucket,
326 for (i = 0; i < bucket->bu_blocks; i++) {
327 rc = ocfs2_journal_access(handle,
328 INODE_CACHE(bucket->bu_inode),
329 bucket->bu_bhs[i], type);
339 static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
340 struct ocfs2_xattr_bucket *bucket)
344 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
345 ocfs2_compute_meta_ecc_bhs(bucket->bu_inode->i_sb,
346 bucket->bu_bhs, bucket->bu_blocks,
347 &bucket_xh(bucket)->xh_check);
348 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
350 for (i = 0; i < bucket->bu_blocks; i++)
351 ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
354 static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
355 struct ocfs2_xattr_bucket *src)
358 int blocksize = src->bu_inode->i_sb->s_blocksize;
360 BUG_ON(dest->bu_blocks != src->bu_blocks);
361 BUG_ON(dest->bu_inode != src->bu_inode);
363 for (i = 0; i < src->bu_blocks; i++) {
364 memcpy(bucket_block(dest, i), bucket_block(src, i),
369 static int ocfs2_validate_xattr_block(struct super_block *sb,
370 struct buffer_head *bh)
373 struct ocfs2_xattr_block *xb =
374 (struct ocfs2_xattr_block *)bh->b_data;
376 mlog(0, "Validating xattr block %llu\n",
377 (unsigned long long)bh->b_blocknr);
379 BUG_ON(!buffer_uptodate(bh));
382 * If the ecc fails, we return the error but otherwise
383 * leave the filesystem running. We know any error is
384 * local to this block.
386 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &xb->xb_check);
391 * Errors after here are fatal
394 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
396 "Extended attribute block #%llu has bad "
398 (unsigned long long)bh->b_blocknr, 7,
403 if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) {
405 "Extended attribute block #%llu has an "
406 "invalid xb_blkno of %llu",
407 (unsigned long long)bh->b_blocknr,
408 (unsigned long long)le64_to_cpu(xb->xb_blkno));
412 if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) {
414 "Extended attribute block #%llu has an invalid "
415 "xb_fs_generation of #%u",
416 (unsigned long long)bh->b_blocknr,
417 le32_to_cpu(xb->xb_fs_generation));
424 static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno,
425 struct buffer_head **bh)
428 struct buffer_head *tmp = *bh;
430 rc = ocfs2_read_block(INODE_CACHE(inode), xb_blkno, &tmp,
431 ocfs2_validate_xattr_block);
433 /* If ocfs2_read_block() got us a new bh, pass it up. */
440 static inline const char *ocfs2_xattr_prefix(int name_index)
442 struct xattr_handler *handler = NULL;
444 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
445 handler = ocfs2_xattr_handler_map[name_index];
447 return handler ? handler->prefix : NULL;
450 static u32 ocfs2_xattr_name_hash(struct inode *inode,
454 /* Get hash value of uuid from super block */
455 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
458 /* hash extended attribute name */
459 for (i = 0; i < name_len; i++) {
460 hash = (hash << OCFS2_HASH_SHIFT) ^
461 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
469 * ocfs2_xattr_hash_entry()
471 * Compute the hash of an extended attribute.
473 static void ocfs2_xattr_hash_entry(struct inode *inode,
474 struct ocfs2_xattr_header *header,
475 struct ocfs2_xattr_entry *entry)
478 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
480 hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
481 entry->xe_name_hash = cpu_to_le32(hash);
486 static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
490 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
491 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
493 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
494 size += sizeof(struct ocfs2_xattr_entry);
499 int ocfs2_calc_security_init(struct inode *dir,
500 struct ocfs2_security_xattr_info *si,
503 struct ocfs2_alloc_context **xattr_ac)
506 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
507 int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
511 * The max space of security xattr taken inline is
512 * 256(name) + 80(value) + 16(entry) = 352 bytes,
513 * So reserve one metadata block for it is ok.
515 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
516 s_size > OCFS2_XATTR_FREE_IN_IBODY) {
517 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
522 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
525 /* reserve clusters for xattr value which will be set in B tree*/
526 if (si->value_len > OCFS2_XATTR_INLINE_SIZE) {
527 int new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
530 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
532 *want_clusters += new_clusters;
537 int ocfs2_calc_xattr_init(struct inode *dir,
538 struct buffer_head *dir_bh,
540 struct ocfs2_security_xattr_info *si,
546 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
547 int s_size = 0, a_size = 0, acl_len = 0, new_clusters;
550 s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
553 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
554 acl_len = ocfs2_xattr_get_nolock(dir, dir_bh,
555 OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
558 a_size = ocfs2_xattr_entry_real_size(0, acl_len);
561 } else if (acl_len != 0 && acl_len != -ENODATA) {
567 if (!(s_size + a_size))
571 * The max space of security xattr taken inline is
572 * 256(name) + 80(value) + 16(entry) = 352 bytes,
573 * The max space of acl xattr taken inline is
574 * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
575 * when blocksize = 512, may reserve one more cluser for
576 * xattr bucket, otherwise reserve one metadata block
578 * If this is a new directory with inline data,
579 * we choose to reserve the entire inline area for
580 * directory contents and force an external xattr block.
582 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
583 (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) ||
584 (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) {
585 *want_meta = *want_meta + 1;
586 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
589 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
590 (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
592 *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
596 * reserve credits and clusters for xattrs which has large value
597 * and have to be set outside
599 if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE) {
600 new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
602 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
604 *want_clusters += new_clusters;
606 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
607 acl_len > OCFS2_XATTR_INLINE_SIZE) {
608 /* for directory, it has DEFAULT and ACCESS two types of acls */
609 new_clusters = (S_ISDIR(mode) ? 2 : 1) *
610 ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
611 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
613 *want_clusters += new_clusters;
619 static int ocfs2_xattr_extend_allocation(struct inode *inode,
621 struct ocfs2_xattr_value_buf *vb,
622 struct ocfs2_xattr_set_ctxt *ctxt)
625 handle_t *handle = ctxt->handle;
626 enum ocfs2_alloc_restarted why;
627 u32 prev_clusters, logical_start = le32_to_cpu(vb->vb_xv->xr_clusters);
628 struct ocfs2_extent_tree et;
630 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
632 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
634 status = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
635 OCFS2_JOURNAL_ACCESS_WRITE);
641 prev_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
642 status = ocfs2_add_clusters_in_btree(handle,
655 status = ocfs2_journal_dirty(handle, vb->vb_bh);
661 clusters_to_add -= le32_to_cpu(vb->vb_xv->xr_clusters) - prev_clusters;
664 * We should have already allocated enough space before the transaction,
665 * so no need to restart.
667 BUG_ON(why != RESTART_NONE || clusters_to_add);
674 static int __ocfs2_remove_xattr_range(struct inode *inode,
675 struct ocfs2_xattr_value_buf *vb,
676 u32 cpos, u32 phys_cpos, u32 len,
677 unsigned int ext_flags,
678 struct ocfs2_xattr_set_ctxt *ctxt)
681 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
682 handle_t *handle = ctxt->handle;
683 struct ocfs2_extent_tree et;
685 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
687 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
688 OCFS2_JOURNAL_ACCESS_WRITE);
694 ret = ocfs2_remove_extent(handle, &et, cpos, len, ctxt->meta_ac,
701 le32_add_cpu(&vb->vb_xv->xr_clusters, -len);
703 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
709 if (ext_flags & OCFS2_EXT_REFCOUNTED)
710 ret = ocfs2_decrease_refcount(inode, handle,
711 ocfs2_blocks_to_clusters(inode->i_sb,
713 len, ctxt->meta_ac, &ctxt->dealloc, 1);
715 ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc,
724 static int ocfs2_xattr_shrink_size(struct inode *inode,
727 struct ocfs2_xattr_value_buf *vb,
728 struct ocfs2_xattr_set_ctxt *ctxt)
731 unsigned int ext_flags;
732 u32 trunc_len, cpos, phys_cpos, alloc_size;
735 if (old_clusters <= new_clusters)
739 trunc_len = old_clusters - new_clusters;
741 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
743 &vb->vb_xv->xr_list, &ext_flags);
749 if (alloc_size > trunc_len)
750 alloc_size = trunc_len;
752 ret = __ocfs2_remove_xattr_range(inode, vb, cpos,
753 phys_cpos, alloc_size,
760 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
761 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode),
764 trunc_len -= alloc_size;
771 static int ocfs2_xattr_value_truncate(struct inode *inode,
772 struct ocfs2_xattr_value_buf *vb,
774 struct ocfs2_xattr_set_ctxt *ctxt)
777 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
778 u32 old_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
780 if (new_clusters == old_clusters)
783 if (new_clusters > old_clusters)
784 ret = ocfs2_xattr_extend_allocation(inode,
785 new_clusters - old_clusters,
788 ret = ocfs2_xattr_shrink_size(inode,
789 old_clusters, new_clusters,
795 static int ocfs2_xattr_list_entry(char *buffer, size_t size,
796 size_t *result, const char *prefix,
797 const char *name, int name_len)
799 char *p = buffer + *result;
800 int prefix_len = strlen(prefix);
801 int total_len = prefix_len + name_len + 1;
803 *result += total_len;
805 /* we are just looking for how big our buffer needs to be */
812 memcpy(p, prefix, prefix_len);
813 memcpy(p + prefix_len, name, name_len);
814 p[prefix_len + name_len] = '\0';
819 static int ocfs2_xattr_list_entries(struct inode *inode,
820 struct ocfs2_xattr_header *header,
821 char *buffer, size_t buffer_size)
825 const char *prefix, *name;
827 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
828 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
829 type = ocfs2_xattr_get_type(entry);
830 prefix = ocfs2_xattr_prefix(type);
833 name = (const char *)header +
834 le16_to_cpu(entry->xe_name_offset);
836 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
837 &result, prefix, name,
847 int ocfs2_has_inline_xattr_value_outside(struct inode *inode,
848 struct ocfs2_dinode *di)
850 struct ocfs2_xattr_header *xh;
853 xh = (struct ocfs2_xattr_header *)
854 ((void *)di + inode->i_sb->s_blocksize -
855 le16_to_cpu(di->i_xattr_inline_size));
857 for (i = 0; i < le16_to_cpu(xh->xh_count); i++)
858 if (!ocfs2_xattr_is_local(&xh->xh_entries[i]))
864 static int ocfs2_xattr_ibody_list(struct inode *inode,
865 struct ocfs2_dinode *di,
869 struct ocfs2_xattr_header *header = NULL;
870 struct ocfs2_inode_info *oi = OCFS2_I(inode);
873 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
876 header = (struct ocfs2_xattr_header *)
877 ((void *)di + inode->i_sb->s_blocksize -
878 le16_to_cpu(di->i_xattr_inline_size));
880 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
885 static int ocfs2_xattr_block_list(struct inode *inode,
886 struct ocfs2_dinode *di,
890 struct buffer_head *blk_bh = NULL;
891 struct ocfs2_xattr_block *xb;
894 if (!di->i_xattr_loc)
897 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
904 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
905 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
906 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
907 ret = ocfs2_xattr_list_entries(inode, header,
908 buffer, buffer_size);
910 ret = ocfs2_xattr_tree_list_index_block(inode, blk_bh,
911 buffer, buffer_size);
918 ssize_t ocfs2_listxattr(struct dentry *dentry,
922 int ret = 0, i_ret = 0, b_ret = 0;
923 struct buffer_head *di_bh = NULL;
924 struct ocfs2_dinode *di = NULL;
925 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
927 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
930 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
933 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
939 di = (struct ocfs2_dinode *)di_bh->b_data;
941 down_read(&oi->ip_xattr_sem);
942 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
950 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
955 up_read(&oi->ip_xattr_sem);
956 ocfs2_inode_unlock(dentry->d_inode, 0);
960 return i_ret + b_ret;
963 static int ocfs2_xattr_find_entry(int name_index,
965 struct ocfs2_xattr_search *xs)
967 struct ocfs2_xattr_entry *entry;
974 name_len = strlen(name);
976 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
977 cmp = name_index - ocfs2_xattr_get_type(entry);
979 cmp = name_len - entry->xe_name_len;
981 cmp = memcmp(name, (xs->base +
982 le16_to_cpu(entry->xe_name_offset)),
990 return cmp ? -ENODATA : 0;
993 static int ocfs2_xattr_get_value_outside(struct inode *inode,
994 struct ocfs2_xattr_value_root *xv,
998 u32 cpos, p_cluster, num_clusters, bpc, clusters;
1001 size_t cplen, blocksize;
1002 struct buffer_head *bh = NULL;
1003 struct ocfs2_extent_list *el;
1006 clusters = le32_to_cpu(xv->xr_clusters);
1007 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1008 blocksize = inode->i_sb->s_blocksize;
1011 while (cpos < clusters) {
1012 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1013 &num_clusters, el, NULL);
1019 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1020 /* Copy ocfs2_xattr_value */
1021 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1022 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1029 cplen = len >= blocksize ? blocksize : len;
1030 memcpy(buffer, bh->b_data, cplen);
1039 cpos += num_clusters;
1045 static int ocfs2_xattr_ibody_get(struct inode *inode,
1050 struct ocfs2_xattr_search *xs)
1052 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1053 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1054 struct ocfs2_xattr_value_root *xv;
1058 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
1061 xs->end = (void *)di + inode->i_sb->s_blocksize;
1062 xs->header = (struct ocfs2_xattr_header *)
1063 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1064 xs->base = (void *)xs->header;
1065 xs->here = xs->header->xh_entries;
1067 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1070 size = le64_to_cpu(xs->here->xe_value_size);
1072 if (size > buffer_size)
1074 if (ocfs2_xattr_is_local(xs->here)) {
1075 memcpy(buffer, (void *)xs->base +
1076 le16_to_cpu(xs->here->xe_name_offset) +
1077 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
1079 xv = (struct ocfs2_xattr_value_root *)
1080 (xs->base + le16_to_cpu(
1081 xs->here->xe_name_offset) +
1082 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
1083 ret = ocfs2_xattr_get_value_outside(inode, xv,
1095 static int ocfs2_xattr_block_get(struct inode *inode,
1100 struct ocfs2_xattr_search *xs)
1102 struct ocfs2_xattr_block *xb;
1103 struct ocfs2_xattr_value_root *xv;
1105 int ret = -ENODATA, name_offset, name_len, i;
1106 int uninitialized_var(block_off);
1108 xs->bucket = ocfs2_xattr_bucket_new(inode);
1115 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
1121 if (xs->not_found) {
1126 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1127 size = le64_to_cpu(xs->here->xe_value_size);
1130 if (size > buffer_size)
1133 name_offset = le16_to_cpu(xs->here->xe_name_offset);
1134 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
1135 i = xs->here - xs->header->xh_entries;
1137 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
1138 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
1139 bucket_xh(xs->bucket),
1143 xs->base = bucket_block(xs->bucket, block_off);
1145 if (ocfs2_xattr_is_local(xs->here)) {
1146 memcpy(buffer, (void *)xs->base +
1147 name_offset + name_len, size);
1149 xv = (struct ocfs2_xattr_value_root *)
1150 (xs->base + name_offset + name_len);
1151 ret = ocfs2_xattr_get_value_outside(inode, xv,
1161 ocfs2_xattr_bucket_free(xs->bucket);
1163 brelse(xs->xattr_bh);
1164 xs->xattr_bh = NULL;
1168 int ocfs2_xattr_get_nolock(struct inode *inode,
1169 struct buffer_head *di_bh,
1176 struct ocfs2_dinode *di = NULL;
1177 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1178 struct ocfs2_xattr_search xis = {
1179 .not_found = -ENODATA,
1181 struct ocfs2_xattr_search xbs = {
1182 .not_found = -ENODATA,
1185 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1188 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1191 xis.inode_bh = xbs.inode_bh = di_bh;
1192 di = (struct ocfs2_dinode *)di_bh->b_data;
1194 down_read(&oi->ip_xattr_sem);
1195 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1197 if (ret == -ENODATA && di->i_xattr_loc)
1198 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1200 up_read(&oi->ip_xattr_sem);
1205 /* ocfs2_xattr_get()
1207 * Copy an extended attribute into the buffer provided.
1208 * Buffer is NULL to compute the size of buffer required.
1210 static int ocfs2_xattr_get(struct inode *inode,
1217 struct buffer_head *di_bh = NULL;
1219 ret = ocfs2_inode_lock(inode, &di_bh, 0);
1224 ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
1225 name, buffer, buffer_size);
1227 ocfs2_inode_unlock(inode, 0);
1234 static int __ocfs2_xattr_set_value_outside(struct inode *inode,
1236 struct ocfs2_xattr_value_buf *vb,
1240 int ret = 0, i, cp_len;
1241 u16 blocksize = inode->i_sb->s_blocksize;
1242 u32 p_cluster, num_clusters;
1243 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1244 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1246 struct buffer_head *bh = NULL;
1247 unsigned int ext_flags;
1248 struct ocfs2_xattr_value_root *xv = vb->vb_xv;
1250 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1252 while (cpos < clusters) {
1253 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1254 &num_clusters, &xv->xr_list,
1261 BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED);
1263 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1265 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
1266 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1273 ret = ocfs2_journal_access(handle,
1276 OCFS2_JOURNAL_ACCESS_WRITE);
1282 cp_len = value_len > blocksize ? blocksize : value_len;
1283 memcpy(bh->b_data, value, cp_len);
1284 value_len -= cp_len;
1286 if (cp_len < blocksize)
1287 memset(bh->b_data + cp_len, 0,
1288 blocksize - cp_len);
1290 ret = ocfs2_journal_dirty(handle, bh);
1299 * XXX: do we need to empty all the following
1300 * blocks in this cluster?
1305 cpos += num_clusters;
1313 static int ocfs2_xattr_cleanup(struct inode *inode,
1315 struct ocfs2_xattr_info *xi,
1316 struct ocfs2_xattr_search *xs,
1317 struct ocfs2_xattr_value_buf *vb,
1321 size_t name_len = strlen(xi->name);
1322 void *val = xs->base + offs;
1323 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1325 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
1326 OCFS2_JOURNAL_ACCESS_WRITE);
1331 /* Decrease xattr count */
1332 le16_add_cpu(&xs->header->xh_count, -1);
1333 /* Remove the xattr entry and tree root which has already be set*/
1334 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
1335 memset(val, 0, size);
1337 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
1344 static int ocfs2_xattr_update_entry(struct inode *inode,
1346 struct ocfs2_xattr_info *xi,
1347 struct ocfs2_xattr_search *xs,
1348 struct ocfs2_xattr_value_buf *vb,
1353 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
1354 OCFS2_JOURNAL_ACCESS_WRITE);
1360 xs->here->xe_name_offset = cpu_to_le16(offs);
1361 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1362 if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
1363 ocfs2_xattr_set_local(xs->here, 1);
1365 ocfs2_xattr_set_local(xs->here, 0);
1366 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1368 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
1376 * ocfs2_xattr_set_value_outside()
1378 * Set large size value in B tree.
1380 static int ocfs2_xattr_set_value_outside(struct inode *inode,
1381 struct ocfs2_xattr_info *xi,
1382 struct ocfs2_xattr_search *xs,
1383 struct ocfs2_xattr_set_ctxt *ctxt,
1384 struct ocfs2_xattr_value_buf *vb,
1387 size_t name_len = strlen(xi->name);
1388 void *val = xs->base + offs;
1389 struct ocfs2_xattr_value_root *xv = NULL;
1390 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1393 memset(val, 0, size);
1394 memcpy(val, xi->name, name_len);
1395 xv = (struct ocfs2_xattr_value_root *)
1396 (val + OCFS2_XATTR_SIZE(name_len));
1397 xv->xr_clusters = 0;
1398 xv->xr_last_eb_blk = 0;
1399 xv->xr_list.l_tree_depth = 0;
1400 xv->xr_list.l_count = cpu_to_le16(1);
1401 xv->xr_list.l_next_free_rec = 0;
1404 ret = ocfs2_xattr_value_truncate(inode, vb, xi->value_len, ctxt);
1409 ret = ocfs2_xattr_update_entry(inode, ctxt->handle, xi, xs, vb, offs);
1414 ret = __ocfs2_xattr_set_value_outside(inode, ctxt->handle, vb,
1415 xi->value, xi->value_len);
1423 * ocfs2_xattr_set_entry_local()
1425 * Set, replace or remove extended attribute in local.
1427 static void ocfs2_xattr_set_entry_local(struct inode *inode,
1428 struct ocfs2_xattr_info *xi,
1429 struct ocfs2_xattr_search *xs,
1430 struct ocfs2_xattr_entry *last,
1433 size_t name_len = strlen(xi->name);
1436 if (xi->value && xs->not_found) {
1437 /* Insert the new xattr entry. */
1438 le16_add_cpu(&xs->header->xh_count, 1);
1439 ocfs2_xattr_set_type(last, xi->name_index);
1440 ocfs2_xattr_set_local(last, 1);
1441 last->xe_name_len = name_len;
1447 first_val = xs->base + min_offs;
1448 offs = le16_to_cpu(xs->here->xe_name_offset);
1449 val = xs->base + offs;
1451 if (le64_to_cpu(xs->here->xe_value_size) >
1452 OCFS2_XATTR_INLINE_SIZE)
1453 size = OCFS2_XATTR_SIZE(name_len) +
1454 OCFS2_XATTR_ROOT_SIZE;
1456 size = OCFS2_XATTR_SIZE(name_len) +
1457 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1459 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1460 OCFS2_XATTR_SIZE(xi->value_len)) {
1461 /* The old and the new value have the
1462 same size. Just replace the value. */
1463 ocfs2_xattr_set_local(xs->here, 1);
1464 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1465 /* Clear value bytes. */
1466 memset(val + OCFS2_XATTR_SIZE(name_len),
1468 OCFS2_XATTR_SIZE(xi->value_len));
1469 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1474 /* Remove the old name+value. */
1475 memmove(first_val + size, first_val, val - first_val);
1476 memset(first_val, 0, size);
1477 xs->here->xe_name_hash = 0;
1478 xs->here->xe_name_offset = 0;
1479 ocfs2_xattr_set_local(xs->here, 1);
1480 xs->here->xe_value_size = 0;
1484 /* Adjust all value offsets. */
1485 last = xs->header->xh_entries;
1486 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1487 size_t o = le16_to_cpu(last->xe_name_offset);
1490 last->xe_name_offset = cpu_to_le16(o + size);
1495 /* Remove the old entry. */
1497 memmove(xs->here, xs->here + 1,
1498 (void *)last - (void *)xs->here);
1499 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1500 le16_add_cpu(&xs->header->xh_count, -1);
1504 /* Insert the new name+value. */
1505 size_t size = OCFS2_XATTR_SIZE(name_len) +
1506 OCFS2_XATTR_SIZE(xi->value_len);
1507 void *val = xs->base + min_offs - size;
1509 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1510 memset(val, 0, size);
1511 memcpy(val, xi->name, name_len);
1512 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1515 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1516 ocfs2_xattr_set_local(xs->here, 1);
1517 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1524 * ocfs2_xattr_set_entry()
1526 * Set extended attribute entry into inode or block.
1528 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1529 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1530 * then set value in B tree with set_value_outside().
1532 static int ocfs2_xattr_set_entry(struct inode *inode,
1533 struct ocfs2_xattr_info *xi,
1534 struct ocfs2_xattr_search *xs,
1535 struct ocfs2_xattr_set_ctxt *ctxt,
1538 struct ocfs2_xattr_entry *last;
1539 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1540 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1541 size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1543 handle_t *handle = ctxt->handle;
1545 struct ocfs2_xattr_info xi_l = {
1546 .name_index = xi->name_index,
1549 .value_len = xi->value_len,
1551 struct ocfs2_xattr_value_buf vb = {
1552 .vb_bh = xs->xattr_bh,
1553 .vb_access = ocfs2_journal_access_di,
1556 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1557 BUG_ON(xs->xattr_bh == xs->inode_bh);
1558 vb.vb_access = ocfs2_journal_access_xb;
1560 BUG_ON(xs->xattr_bh != xs->inode_bh);
1562 /* Compute min_offs, last and free space. */
1563 last = xs->header->xh_entries;
1565 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1566 size_t offs = le16_to_cpu(last->xe_name_offset);
1567 if (offs < min_offs)
1572 free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
1576 if (!xs->not_found) {
1578 if (ocfs2_xattr_is_local(xs->here))
1579 size = OCFS2_XATTR_SIZE(name_len) +
1580 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1582 size = OCFS2_XATTR_SIZE(name_len) +
1583 OCFS2_XATTR_ROOT_SIZE;
1584 free += (size + sizeof(struct ocfs2_xattr_entry));
1586 /* Check free space in inode or block */
1587 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1588 if (free < sizeof(struct ocfs2_xattr_entry) +
1589 OCFS2_XATTR_SIZE(name_len) +
1590 OCFS2_XATTR_ROOT_SIZE) {
1594 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1595 xi_l.value = (void *)&def_xv;
1596 xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
1597 } else if (xi->value) {
1598 if (free < sizeof(struct ocfs2_xattr_entry) +
1599 OCFS2_XATTR_SIZE(name_len) +
1600 OCFS2_XATTR_SIZE(xi->value_len)) {
1606 if (!xs->not_found) {
1607 /* For existing extended attribute */
1608 size_t size = OCFS2_XATTR_SIZE(name_len) +
1609 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1610 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1611 void *val = xs->base + offs;
1613 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1614 /* Replace existing local xattr with tree root */
1615 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
1620 } else if (!ocfs2_xattr_is_local(xs->here)) {
1621 /* For existing xattr which has value outside */
1622 vb.vb_xv = (struct ocfs2_xattr_value_root *)
1623 (val + OCFS2_XATTR_SIZE(name_len));
1625 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1627 * If new value need set outside also,
1628 * first truncate old value to new value,
1629 * then set new value with set_value_outside().
1631 ret = ocfs2_xattr_value_truncate(inode,
1640 ret = ocfs2_xattr_update_entry(inode,
1651 ret = __ocfs2_xattr_set_value_outside(inode,
1661 * If new value need set in local,
1662 * just trucate old value to zero.
1664 ret = ocfs2_xattr_value_truncate(inode,
1674 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), xs->inode_bh,
1675 OCFS2_JOURNAL_ACCESS_WRITE);
1681 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1682 ret = vb.vb_access(handle, INODE_CACHE(inode), vb.vb_bh,
1683 OCFS2_JOURNAL_ACCESS_WRITE);
1691 * Set value in local, include set tree root in local.
1692 * This is the first step for value size >INLINE_SIZE.
1694 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1696 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1697 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1704 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1705 (flag & OCFS2_INLINE_XATTR_FL)) {
1706 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1707 unsigned int xattrsize = osb->s_xattr_inline_size;
1710 * Adjust extent record count or inline data size
1711 * to reserve space for extended attribute.
1713 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1714 struct ocfs2_inline_data *idata = &di->id2.i_data;
1715 le16_add_cpu(&idata->id_count, -xattrsize);
1716 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1717 struct ocfs2_extent_list *el = &di->id2.i_list;
1718 le16_add_cpu(&el->l_count, -(xattrsize /
1719 sizeof(struct ocfs2_extent_rec)));
1721 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1723 /* Update xattr flag */
1724 spin_lock(&oi->ip_lock);
1725 oi->ip_dyn_features |= flag;
1726 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1727 spin_unlock(&oi->ip_lock);
1729 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1733 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1735 * Set value outside in B tree.
1736 * This is the second step for value size > INLINE_SIZE.
1738 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1739 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, ctxt,
1746 * If set value outside failed, we have to clean
1747 * the junk tree root we have already set in local.
1749 ret2 = ocfs2_xattr_cleanup(inode, ctxt->handle,
1760 * In xattr remove, if it is stored outside and refcounted, we may have
1761 * the chance to split the refcount tree. So need the allocators.
1763 static int ocfs2_lock_xattr_remove_allocators(struct inode *inode,
1764 struct ocfs2_xattr_value_root *xv,
1765 struct ocfs2_caching_info *ref_ci,
1766 struct buffer_head *ref_root_bh,
1767 struct ocfs2_alloc_context **meta_ac,
1770 int ret, meta_add = 0;
1771 u32 p_cluster, num_clusters;
1772 unsigned int ext_flags;
1775 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
1784 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
1787 ret = ocfs2_refcounted_xattr_delete_need(inode, ref_ci,
1789 &meta_add, ref_credits);
1795 ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
1804 static int ocfs2_remove_value_outside(struct inode*inode,
1805 struct ocfs2_xattr_value_buf *vb,
1806 struct ocfs2_xattr_header *header,
1807 struct ocfs2_caching_info *ref_ci,
1808 struct buffer_head *ref_root_bh)
1810 int ret = 0, i, ref_credits;
1811 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1812 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
1815 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
1817 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1818 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1820 if (ocfs2_xattr_is_local(entry))
1823 val = (void *)header +
1824 le16_to_cpu(entry->xe_name_offset);
1825 vb->vb_xv = (struct ocfs2_xattr_value_root *)
1826 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
1828 ret = ocfs2_lock_xattr_remove_allocators(inode, vb->vb_xv,
1829 ref_ci, ref_root_bh,
1833 ctxt.handle = ocfs2_start_trans(osb, ref_credits +
1834 ocfs2_remove_extent_credits(osb->sb));
1835 if (IS_ERR(ctxt.handle)) {
1836 ret = PTR_ERR(ctxt.handle);
1841 ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt);
1847 ocfs2_commit_trans(osb, ctxt.handle);
1849 ocfs2_free_alloc_context(ctxt.meta_ac);
1850 ctxt.meta_ac = NULL;
1855 ocfs2_free_alloc_context(ctxt.meta_ac);
1856 ocfs2_schedule_truncate_log_flush(osb, 1);
1857 ocfs2_run_deallocs(osb, &ctxt.dealloc);
1861 static int ocfs2_xattr_ibody_remove(struct inode *inode,
1862 struct buffer_head *di_bh,
1863 struct ocfs2_caching_info *ref_ci,
1864 struct buffer_head *ref_root_bh)
1867 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1868 struct ocfs2_xattr_header *header;
1870 struct ocfs2_xattr_value_buf vb = {
1872 .vb_access = ocfs2_journal_access_di,
1875 header = (struct ocfs2_xattr_header *)
1876 ((void *)di + inode->i_sb->s_blocksize -
1877 le16_to_cpu(di->i_xattr_inline_size));
1879 ret = ocfs2_remove_value_outside(inode, &vb, header,
1880 ref_ci, ref_root_bh);
1885 struct ocfs2_rm_xattr_bucket_para {
1886 struct ocfs2_caching_info *ref_ci;
1887 struct buffer_head *ref_root_bh;
1890 static int ocfs2_xattr_block_remove(struct inode *inode,
1891 struct buffer_head *blk_bh,
1892 struct ocfs2_caching_info *ref_ci,
1893 struct buffer_head *ref_root_bh)
1895 struct ocfs2_xattr_block *xb;
1897 struct ocfs2_xattr_value_buf vb = {
1899 .vb_access = ocfs2_journal_access_xb,
1901 struct ocfs2_rm_xattr_bucket_para args = {
1903 .ref_root_bh = ref_root_bh,
1906 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1907 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1908 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
1909 ret = ocfs2_remove_value_outside(inode, &vb, header,
1910 ref_ci, ref_root_bh);
1912 ret = ocfs2_iterate_xattr_index_block(inode,
1914 ocfs2_rm_xattr_cluster,
1920 static int ocfs2_xattr_free_block(struct inode *inode,
1922 struct ocfs2_caching_info *ref_ci,
1923 struct buffer_head *ref_root_bh)
1925 struct inode *xb_alloc_inode;
1926 struct buffer_head *xb_alloc_bh = NULL;
1927 struct buffer_head *blk_bh = NULL;
1928 struct ocfs2_xattr_block *xb;
1929 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1935 ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
1941 ret = ocfs2_xattr_block_remove(inode, blk_bh, ref_ci, ref_root_bh);
1947 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1948 blk = le64_to_cpu(xb->xb_blkno);
1949 bit = le16_to_cpu(xb->xb_suballoc_bit);
1950 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1952 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1953 EXTENT_ALLOC_SYSTEM_INODE,
1954 le16_to_cpu(xb->xb_suballoc_slot));
1955 if (!xb_alloc_inode) {
1960 mutex_lock(&xb_alloc_inode->i_mutex);
1962 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1968 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
1969 if (IS_ERR(handle)) {
1970 ret = PTR_ERR(handle);
1975 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1980 ocfs2_commit_trans(osb, handle);
1982 ocfs2_inode_unlock(xb_alloc_inode, 1);
1983 brelse(xb_alloc_bh);
1985 mutex_unlock(&xb_alloc_inode->i_mutex);
1986 iput(xb_alloc_inode);
1993 * ocfs2_xattr_remove()
1995 * Free extended attribute resources associated with this inode.
1997 int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
1999 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2000 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2001 struct ocfs2_refcount_tree *ref_tree = NULL;
2002 struct buffer_head *ref_root_bh = NULL;
2003 struct ocfs2_caching_info *ref_ci = NULL;
2007 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2010 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
2013 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) {
2014 ret = ocfs2_lock_refcount_tree(OCFS2_SB(inode->i_sb),
2015 le64_to_cpu(di->i_refcount_loc),
2016 1, &ref_tree, &ref_root_bh);
2021 ref_ci = &ref_tree->rf_ci;
2025 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2026 ret = ocfs2_xattr_ibody_remove(inode, di_bh,
2027 ref_ci, ref_root_bh);
2034 if (di->i_xattr_loc) {
2035 ret = ocfs2_xattr_free_block(inode,
2036 le64_to_cpu(di->i_xattr_loc),
2037 ref_ci, ref_root_bh);
2044 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
2045 OCFS2_INODE_UPDATE_CREDITS);
2046 if (IS_ERR(handle)) {
2047 ret = PTR_ERR(handle);
2051 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
2052 OCFS2_JOURNAL_ACCESS_WRITE);
2058 di->i_xattr_loc = 0;
2060 spin_lock(&oi->ip_lock);
2061 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
2062 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2063 spin_unlock(&oi->ip_lock);
2065 ret = ocfs2_journal_dirty(handle, di_bh);
2069 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
2072 ocfs2_unlock_refcount_tree(OCFS2_SB(inode->i_sb), ref_tree, 1);
2073 brelse(ref_root_bh);
2077 static int ocfs2_xattr_has_space_inline(struct inode *inode,
2078 struct ocfs2_dinode *di)
2080 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2081 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
2084 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
2087 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2088 struct ocfs2_inline_data *idata = &di->id2.i_data;
2089 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
2090 } else if (ocfs2_inode_is_fast_symlink(inode)) {
2091 free = ocfs2_fast_symlink_chars(inode->i_sb) -
2092 le64_to_cpu(di->i_size);
2094 struct ocfs2_extent_list *el = &di->id2.i_list;
2095 free = (le16_to_cpu(el->l_count) -
2096 le16_to_cpu(el->l_next_free_rec)) *
2097 sizeof(struct ocfs2_extent_rec);
2099 if (free >= xattrsize)
2106 * ocfs2_xattr_ibody_find()
2108 * Find extended attribute in inode block and
2109 * fill search info into struct ocfs2_xattr_search.
2111 static int ocfs2_xattr_ibody_find(struct inode *inode,
2114 struct ocfs2_xattr_search *xs)
2116 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2117 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2121 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2124 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2125 down_read(&oi->ip_alloc_sem);
2126 has_space = ocfs2_xattr_has_space_inline(inode, di);
2127 up_read(&oi->ip_alloc_sem);
2132 xs->xattr_bh = xs->inode_bh;
2133 xs->end = (void *)di + inode->i_sb->s_blocksize;
2134 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
2135 xs->header = (struct ocfs2_xattr_header *)
2136 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
2138 xs->header = (struct ocfs2_xattr_header *)
2139 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
2140 xs->base = (void *)xs->header;
2141 xs->here = xs->header->xh_entries;
2143 /* Find the named attribute. */
2144 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2145 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2146 if (ret && ret != -ENODATA)
2148 xs->not_found = ret;
2155 * ocfs2_xattr_ibody_set()
2157 * Set, replace or remove an extended attribute into inode block.
2160 static int ocfs2_xattr_ibody_set(struct inode *inode,
2161 struct ocfs2_xattr_info *xi,
2162 struct ocfs2_xattr_search *xs,
2163 struct ocfs2_xattr_set_ctxt *ctxt)
2165 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2166 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2169 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2172 down_write(&oi->ip_alloc_sem);
2173 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2174 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2180 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
2181 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
2183 up_write(&oi->ip_alloc_sem);
2189 * ocfs2_xattr_block_find()
2191 * Find extended attribute in external block and
2192 * fill search info into struct ocfs2_xattr_search.
2194 static int ocfs2_xattr_block_find(struct inode *inode,
2197 struct ocfs2_xattr_search *xs)
2199 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2200 struct buffer_head *blk_bh = NULL;
2201 struct ocfs2_xattr_block *xb;
2204 if (!di->i_xattr_loc)
2207 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
2214 xs->xattr_bh = blk_bh;
2215 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
2217 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2218 xs->header = &xb->xb_attrs.xb_header;
2219 xs->base = (void *)xs->header;
2220 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
2221 xs->here = xs->header->xh_entries;
2223 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2225 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
2229 if (ret && ret != -ENODATA) {
2230 xs->xattr_bh = NULL;
2233 xs->not_found = ret;
2241 static int ocfs2_create_xattr_block(handle_t *handle,
2242 struct inode *inode,
2243 struct buffer_head *inode_bh,
2244 struct ocfs2_alloc_context *meta_ac,
2245 struct buffer_head **ret_bh,
2249 u16 suballoc_bit_start;
2252 struct ocfs2_dinode *di = (struct ocfs2_dinode *)inode_bh->b_data;
2253 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2254 struct buffer_head *new_bh = NULL;
2255 struct ocfs2_xattr_block *xblk;
2257 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), inode_bh,
2258 OCFS2_JOURNAL_ACCESS_CREATE);
2264 ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
2265 &suballoc_bit_start, &num_got,
2272 new_bh = sb_getblk(inode->i_sb, first_blkno);
2273 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
2275 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode),
2277 OCFS2_JOURNAL_ACCESS_CREATE);
2283 /* Initialize ocfs2_xattr_block */
2284 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2285 memset(xblk, 0, inode->i_sb->s_blocksize);
2286 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
2287 xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
2288 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
2289 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
2290 xblk->xb_blkno = cpu_to_le64(first_blkno);
2293 struct ocfs2_xattr_tree_root *xr = &xblk->xb_attrs.xb_root;
2294 xr->xt_clusters = cpu_to_le32(1);
2295 xr->xt_last_eb_blk = 0;
2296 xr->xt_list.l_tree_depth = 0;
2297 xr->xt_list.l_count = cpu_to_le16(
2298 ocfs2_xattr_recs_per_xb(inode->i_sb));
2299 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2300 xblk->xb_flags = cpu_to_le16(OCFS2_XATTR_INDEXED);
2303 ret = ocfs2_journal_dirty(handle, new_bh);
2308 di->i_xattr_loc = cpu_to_le64(first_blkno);
2309 ocfs2_journal_dirty(handle, inode_bh);
2320 * ocfs2_xattr_block_set()
2322 * Set, replace or remove an extended attribute into external block.
2325 static int ocfs2_xattr_block_set(struct inode *inode,
2326 struct ocfs2_xattr_info *xi,
2327 struct ocfs2_xattr_search *xs,
2328 struct ocfs2_xattr_set_ctxt *ctxt)
2330 struct buffer_head *new_bh = NULL;
2331 handle_t *handle = ctxt->handle;
2332 struct ocfs2_xattr_block *xblk = NULL;
2335 if (!xs->xattr_bh) {
2336 ret = ocfs2_create_xattr_block(handle, inode, xs->inode_bh,
2337 ctxt->meta_ac, &new_bh, 0);
2343 xs->xattr_bh = new_bh;
2344 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2345 xs->header = &xblk->xb_attrs.xb_header;
2346 xs->base = (void *)xs->header;
2347 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2348 xs->here = xs->header->xh_entries;
2350 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2352 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
2353 /* Set extended attribute into external block */
2354 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
2355 OCFS2_HAS_XATTR_FL);
2356 if (!ret || ret != -ENOSPC)
2359 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
2364 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
2371 /* Check whether the new xattr can be inserted into the inode. */
2372 static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
2373 struct ocfs2_xattr_info *xi,
2374 struct ocfs2_xattr_search *xs)
2377 struct ocfs2_xattr_entry *last;
2379 size_t min_offs = xs->end - xs->base;
2384 last = xs->header->xh_entries;
2386 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
2387 size_t offs = le16_to_cpu(last->xe_name_offset);
2388 if (offs < min_offs)
2393 free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
2397 BUG_ON(!xs->not_found);
2399 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
2400 value_size = OCFS2_XATTR_ROOT_SIZE;
2402 value_size = OCFS2_XATTR_SIZE(xi->value_len);
2404 if (free >= sizeof(struct ocfs2_xattr_entry) +
2405 OCFS2_XATTR_SIZE(strlen(xi->name)) + value_size)
2411 static int ocfs2_calc_xattr_set_need(struct inode *inode,
2412 struct ocfs2_dinode *di,
2413 struct ocfs2_xattr_info *xi,
2414 struct ocfs2_xattr_search *xis,
2415 struct ocfs2_xattr_search *xbs,
2420 int ret = 0, old_in_xb = 0;
2421 int clusters_add = 0, meta_add = 0, credits = 0;
2422 struct buffer_head *bh = NULL;
2423 struct ocfs2_xattr_block *xb = NULL;
2424 struct ocfs2_xattr_entry *xe = NULL;
2425 struct ocfs2_xattr_value_root *xv = NULL;
2427 int name_offset, name_len = 0;
2428 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2433 * Calculate the clusters we need to write.
2434 * No matter whether we replace an old one or add a new one,
2435 * we need this for writing.
2437 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
2438 credits += new_clusters *
2439 ocfs2_clusters_to_blocks(inode->i_sb, 1);
2441 if (xis->not_found && xbs->not_found) {
2442 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2444 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
2445 clusters_add += new_clusters;
2446 credits += ocfs2_calc_extend_credits(inode->i_sb,
2454 if (!xis->not_found) {
2456 name_offset = le16_to_cpu(xe->xe_name_offset);
2457 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2459 credits += OCFS2_INODE_UPDATE_CREDITS;
2461 int i, block_off = 0;
2462 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2464 name_offset = le16_to_cpu(xe->xe_name_offset);
2465 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2466 i = xbs->here - xbs->header->xh_entries;
2469 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2470 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
2471 bucket_xh(xbs->bucket),
2474 base = bucket_block(xbs->bucket, block_off);
2475 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2478 credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
2483 * delete a xattr doesn't need metadata and cluster allocation.
2484 * so just calculate the credits and return.
2486 * The credits for removing the value tree will be extended
2487 * by ocfs2_remove_extent itself.
2490 if (!ocfs2_xattr_is_local(xe))
2491 credits += ocfs2_remove_extent_credits(inode->i_sb);
2496 /* do cluster allocation guess first. */
2497 value_size = le64_to_cpu(xe->xe_value_size);
2501 * In xattr set, we always try to set the xe in inode first,
2502 * so if it can be inserted into inode successfully, the old
2503 * one will be removed from the xattr block, and this xattr
2504 * will be inserted into inode as a new xattr in inode.
2506 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
2507 clusters_add += new_clusters;
2508 credits += ocfs2_remove_extent_credits(inode->i_sb) +
2509 OCFS2_INODE_UPDATE_CREDITS;
2510 if (!ocfs2_xattr_is_local(xe))
2511 credits += ocfs2_calc_extend_credits(
2519 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
2520 /* the new values will be stored outside. */
2521 u32 old_clusters = 0;
2523 if (!ocfs2_xattr_is_local(xe)) {
2524 old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2526 xv = (struct ocfs2_xattr_value_root *)
2527 (base + name_offset + name_len);
2528 value_size = OCFS2_XATTR_ROOT_SIZE;
2532 if (old_clusters >= new_clusters) {
2533 credits += ocfs2_remove_extent_credits(inode->i_sb);
2536 meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
2537 clusters_add += new_clusters - old_clusters;
2538 credits += ocfs2_calc_extend_credits(inode->i_sb,
2542 if (value_size >= OCFS2_XATTR_ROOT_SIZE)
2547 * Now the new value will be stored inside. So if the new
2548 * value is smaller than the size of value root or the old
2549 * value, we don't need any allocation, otherwise we have
2550 * to guess metadata allocation.
2552 if ((ocfs2_xattr_is_local(xe) && value_size >= xi->value_len) ||
2553 (!ocfs2_xattr_is_local(xe) &&
2554 OCFS2_XATTR_ROOT_SIZE >= xi->value_len))
2559 /* calculate metadata allocation. */
2560 if (di->i_xattr_loc) {
2561 if (!xbs->xattr_bh) {
2562 ret = ocfs2_read_xattr_block(inode,
2563 le64_to_cpu(di->i_xattr_loc),
2570 xb = (struct ocfs2_xattr_block *)bh->b_data;
2572 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2575 * If there is already an xattr tree, good, we can calculate
2576 * like other b-trees. Otherwise we may have the chance of
2577 * create a tree, the credit calculation is borrowed from
2578 * ocfs2_calc_extend_credits with root_el = NULL. And the
2579 * new tree will be cluster based, so no meta is needed.
2581 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2582 struct ocfs2_extent_list *el =
2583 &xb->xb_attrs.xb_root.xt_list;
2584 meta_add += ocfs2_extend_meta_needed(el);
2585 credits += ocfs2_calc_extend_credits(inode->i_sb,
2588 credits += OCFS2_SUBALLOC_ALLOC + 1;
2591 * This cluster will be used either for new bucket or for
2593 * If the cluster size is the same as the bucket size, one
2594 * more is needed since we may need to extend the bucket
2598 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2599 if (OCFS2_XATTR_BUCKET_SIZE ==
2600 OCFS2_SB(inode->i_sb)->s_clustersize) {
2601 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2606 credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
2610 *clusters_need = clusters_add;
2612 *meta_need = meta_add;
2614 *credits_need = credits;
2619 static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
2620 struct ocfs2_dinode *di,
2621 struct ocfs2_xattr_info *xi,
2622 struct ocfs2_xattr_search *xis,
2623 struct ocfs2_xattr_search *xbs,
2624 struct ocfs2_xattr_set_ctxt *ctxt,
2628 int clusters_add, meta_add, ret;
2629 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2631 memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
2633 ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
2635 ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
2636 &clusters_add, &meta_add, credits);
2642 meta_add += extra_meta;
2643 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
2644 "credits = %d\n", xi->name, meta_add, clusters_add, *credits);
2647 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
2656 ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
2662 if (ctxt->meta_ac) {
2663 ocfs2_free_alloc_context(ctxt->meta_ac);
2664 ctxt->meta_ac = NULL;
2668 * We cannot have an error and a non null ctxt->data_ac.
2675 static int __ocfs2_xattr_set_handle(struct inode *inode,
2676 struct ocfs2_dinode *di,
2677 struct ocfs2_xattr_info *xi,
2678 struct ocfs2_xattr_search *xis,
2679 struct ocfs2_xattr_search *xbs,
2680 struct ocfs2_xattr_set_ctxt *ctxt)
2682 int ret = 0, credits, old_found;
2685 /* Remove existing extended attribute */
2686 if (!xis->not_found)
2687 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2688 else if (!xbs->not_found)
2689 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2691 /* We always try to set extended attribute into inode first*/
2692 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2693 if (!ret && !xbs->not_found) {
2695 * If succeed and that extended attribute existing in
2696 * external block, then we will remove it.
2701 old_found = xis->not_found;
2702 xis->not_found = -ENODATA;
2703 ret = ocfs2_calc_xattr_set_need(inode,
2711 xis->not_found = old_found;
2717 ret = ocfs2_extend_trans(ctxt->handle, credits +
2718 ctxt->handle->h_buffer_credits);
2723 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2724 } else if (ret == -ENOSPC) {
2725 if (di->i_xattr_loc && !xbs->xattr_bh) {
2726 ret = ocfs2_xattr_block_find(inode,
2732 old_found = xis->not_found;
2733 xis->not_found = -ENODATA;
2734 ret = ocfs2_calc_xattr_set_need(inode,
2742 xis->not_found = old_found;
2748 ret = ocfs2_extend_trans(ctxt->handle, credits +
2749 ctxt->handle->h_buffer_credits);
2756 * If no space in inode, we will set extended attribute
2757 * into external block.
2759 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2762 if (!xis->not_found) {
2764 * If succeed and that extended attribute
2765 * existing in inode, we will remove it.
2769 xbs->not_found = -ENODATA;
2770 ret = ocfs2_calc_xattr_set_need(inode,
2783 ret = ocfs2_extend_trans(ctxt->handle, credits +
2784 ctxt->handle->h_buffer_credits);
2789 ret = ocfs2_xattr_ibody_set(inode, xi,
2796 /* Update inode ctime. */
2797 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
2799 OCFS2_JOURNAL_ACCESS_WRITE);
2805 inode->i_ctime = CURRENT_TIME;
2806 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
2807 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
2808 ocfs2_journal_dirty(ctxt->handle, xis->inode_bh);
2815 * This function only called duing creating inode
2816 * for init security/acl xattrs of the new inode.
2817 * All transanction credits have been reserved in mknod.
2819 int ocfs2_xattr_set_handle(handle_t *handle,
2820 struct inode *inode,
2821 struct buffer_head *di_bh,
2827 struct ocfs2_alloc_context *meta_ac,
2828 struct ocfs2_alloc_context *data_ac)
2830 struct ocfs2_dinode *di;
2833 struct ocfs2_xattr_info xi = {
2834 .name_index = name_index,
2837 .value_len = value_len,
2840 struct ocfs2_xattr_search xis = {
2841 .not_found = -ENODATA,
2844 struct ocfs2_xattr_search xbs = {
2845 .not_found = -ENODATA,
2848 struct ocfs2_xattr_set_ctxt ctxt = {
2854 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2858 * In extreme situation, may need xattr bucket when
2859 * block size is too small. And we have already reserved
2860 * the credits for bucket in mknod.
2862 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) {
2863 xbs.bucket = ocfs2_xattr_bucket_new(inode);
2865 mlog_errno(-ENOMEM);
2870 xis.inode_bh = xbs.inode_bh = di_bh;
2871 di = (struct ocfs2_dinode *)di_bh->b_data;
2873 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2875 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2878 if (xis.not_found) {
2879 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2884 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
2887 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2888 brelse(xbs.xattr_bh);
2889 ocfs2_xattr_bucket_free(xbs.bucket);
2897 * Set, replace or remove an extended attribute for this inode.
2898 * value is NULL to remove an existing extended attribute, else either
2899 * create or replace an extended attribute.
2901 int ocfs2_xattr_set(struct inode *inode,
2908 struct buffer_head *di_bh = NULL;
2909 struct ocfs2_dinode *di;
2910 int ret, credits, ref_meta = 0, ref_credits = 0;
2911 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2912 struct inode *tl_inode = osb->osb_tl_inode;
2913 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
2914 struct ocfs2_refcount_tree *ref_tree = NULL;
2916 struct ocfs2_xattr_info xi = {
2917 .name_index = name_index,
2920 .value_len = value_len,
2923 struct ocfs2_xattr_search xis = {
2924 .not_found = -ENODATA,
2927 struct ocfs2_xattr_search xbs = {
2928 .not_found = -ENODATA,
2931 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2935 * Only xbs will be used on indexed trees. xis doesn't need a
2938 xbs.bucket = ocfs2_xattr_bucket_new(inode);
2940 mlog_errno(-ENOMEM);
2944 ret = ocfs2_inode_lock(inode, &di_bh, 1);
2947 goto cleanup_nolock;
2949 xis.inode_bh = xbs.inode_bh = di_bh;
2950 di = (struct ocfs2_dinode *)di_bh->b_data;
2952 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2954 * Scan inode and external block to find the same name
2955 * extended attribute and collect search infomation.
2957 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2960 if (xis.not_found) {
2961 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2966 if (xis.not_found && xbs.not_found) {
2968 if (flags & XATTR_REPLACE)
2975 if (flags & XATTR_CREATE)
2979 /* Check whether the value is refcounted and do some prepartion. */
2980 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL &&
2981 (!xis.not_found || !xbs.not_found)) {
2982 ret = ocfs2_prepare_refcount_xattr(inode, di, &xi,
2983 &xis, &xbs, &ref_tree,
2984 &ref_meta, &ref_credits);
2991 mutex_lock(&tl_inode->i_mutex);
2993 if (ocfs2_truncate_log_needs_flush(osb)) {
2994 ret = __ocfs2_flush_truncate_log(osb);
2996 mutex_unlock(&tl_inode->i_mutex);
3001 mutex_unlock(&tl_inode->i_mutex);
3003 ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
3004 &xbs, &ctxt, ref_meta, &credits);
3010 /* we need to update inode's ctime field, so add credit for it. */
3011 credits += OCFS2_INODE_UPDATE_CREDITS;
3012 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
3013 if (IS_ERR(ctxt.handle)) {
3014 ret = PTR_ERR(ctxt.handle);
3019 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3021 ocfs2_commit_trans(osb, ctxt.handle);
3024 ocfs2_free_alloc_context(ctxt.data_ac);
3026 ocfs2_free_alloc_context(ctxt.meta_ac);
3027 if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
3028 ocfs2_schedule_truncate_log_flush(osb, 1);
3029 ocfs2_run_deallocs(osb, &ctxt.dealloc);
3033 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
3034 up_write(&OCFS2_I(inode)->ip_xattr_sem);
3035 if (!value && !ret) {
3036 ret = ocfs2_try_remove_refcount_tree(inode, di_bh);
3040 ocfs2_inode_unlock(inode, 1);
3043 brelse(xbs.xattr_bh);
3044 ocfs2_xattr_bucket_free(xbs.bucket);
3050 * Find the xattr extent rec which may contains name_hash.
3051 * e_cpos will be the first name hash of the xattr rec.
3052 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
3054 static int ocfs2_xattr_get_rec(struct inode *inode,