1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Copyright (C) 2008 Oracle. All rights reserved.
9 * Lots of code in this file is taken from ext3.
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
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.
21 * You should have received a copy of the GNU General Public
22 * License along with this program; if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 021110-1307, USA.
27 #include <linux/capability.h>
29 #include <linux/types.h>
30 #include <linux/slab.h>
31 #include <linux/highmem.h>
32 #include <linux/pagemap.h>
33 #include <linux/uio.h>
34 #include <linux/sched.h>
35 #include <linux/splice.h>
36 #include <linux/mount.h>
37 #include <linux/writeback.h>
38 #include <linux/falloc.h>
40 #define MLOG_MASK_PREFIX ML_XATTR
41 #include <cluster/masklog.h>
54 #include "buffer_head_io.h"
59 struct ocfs2_xattr_def_value_root {
60 struct ocfs2_xattr_value_root xv;
61 struct ocfs2_extent_rec er;
64 struct ocfs2_xattr_bucket {
65 struct buffer_head *bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
66 struct ocfs2_xattr_header *xh;
69 #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
70 #define OCFS2_XATTR_INLINE_SIZE 80
72 static struct ocfs2_xattr_def_value_root def_xv = {
73 .xv.xr_list.l_count = cpu_to_le16(1),
76 struct xattr_handler *ocfs2_xattr_handlers[] = {
77 &ocfs2_xattr_user_handler,
78 &ocfs2_xattr_trusted_handler,
82 static struct xattr_handler *ocfs2_xattr_handler_map[] = {
83 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
84 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
87 struct ocfs2_xattr_info {
94 struct ocfs2_xattr_search {
95 struct buffer_head *inode_bh;
97 * xattr_bh point to the block buffer head which has extended attribute
98 * when extended attribute in inode, xattr_bh is equal to inode_bh.
100 struct buffer_head *xattr_bh;
101 struct ocfs2_xattr_header *header;
102 struct ocfs2_xattr_bucket bucket;
105 struct ocfs2_xattr_entry *here;
109 static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
110 struct ocfs2_xattr_header *xh,
115 static int ocfs2_xattr_index_block_find(struct inode *inode,
116 struct buffer_head *root_bh,
119 struct ocfs2_xattr_search *xs);
121 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
122 struct ocfs2_xattr_tree_root *xt,
126 static inline struct xattr_handler *ocfs2_xattr_handler(int name_index)
128 struct xattr_handler *handler = NULL;
130 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
131 handler = ocfs2_xattr_handler_map[name_index];
136 static inline u32 ocfs2_xattr_name_hash(struct inode *inode,
142 /* Get hash value of uuid from super block */
143 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
146 /* hash extended attribute prefix */
147 for (i = 0; i < prefix_len; i++) {
148 hash = (hash << OCFS2_HASH_SHIFT) ^
149 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
152 /* hash extended attribute name */
153 for (i = 0; i < name_len; i++) {
154 hash = (hash << OCFS2_HASH_SHIFT) ^
155 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
163 * ocfs2_xattr_hash_entry()
165 * Compute the hash of an extended attribute.
167 static void ocfs2_xattr_hash_entry(struct inode *inode,
168 struct ocfs2_xattr_header *header,
169 struct ocfs2_xattr_entry *entry)
172 struct xattr_handler *handler =
173 ocfs2_xattr_handler(ocfs2_xattr_get_type(entry));
174 char *prefix = handler->prefix;
175 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
176 int prefix_len = strlen(handler->prefix);
178 hash = ocfs2_xattr_name_hash(inode, prefix, prefix_len, name,
180 entry->xe_name_hash = cpu_to_le32(hash);
185 static int ocfs2_xattr_extend_allocation(struct inode *inode,
187 struct buffer_head *xattr_bh,
188 struct ocfs2_xattr_value_root *xv)
191 int restart_func = 0;
193 handle_t *handle = NULL;
194 struct ocfs2_alloc_context *data_ac = NULL;
195 struct ocfs2_alloc_context *meta_ac = NULL;
196 enum ocfs2_alloc_restarted why;
197 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
198 struct ocfs2_extent_list *root_el = &xv->xr_list;
199 u32 prev_clusters, logical_start = le32_to_cpu(xv->xr_clusters);
201 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
205 status = ocfs2_lock_allocators(inode, xattr_bh, root_el,
206 clusters_to_add, 0, &data_ac,
207 &meta_ac, OCFS2_XATTR_VALUE_EXTENT, xv);
213 credits = ocfs2_calc_extend_credits(osb->sb, root_el, clusters_to_add);
214 handle = ocfs2_start_trans(osb, credits);
215 if (IS_ERR(handle)) {
216 status = PTR_ERR(handle);
222 restarted_transaction:
223 status = ocfs2_journal_access(handle, inode, xattr_bh,
224 OCFS2_JOURNAL_ACCESS_WRITE);
230 prev_clusters = le32_to_cpu(xv->xr_clusters);
231 status = ocfs2_add_clusters_in_btree(osb,
242 OCFS2_XATTR_VALUE_EXTENT,
244 if ((status < 0) && (status != -EAGAIN)) {
245 if (status != -ENOSPC)
250 status = ocfs2_journal_dirty(handle, xattr_bh);
256 clusters_to_add -= le32_to_cpu(xv->xr_clusters) - prev_clusters;
258 if (why != RESTART_NONE && clusters_to_add) {
259 if (why == RESTART_META) {
260 mlog(0, "restarting function.\n");
263 BUG_ON(why != RESTART_TRANS);
265 mlog(0, "restarting transaction.\n");
266 /* TODO: This can be more intelligent. */
267 credits = ocfs2_calc_extend_credits(osb->sb,
270 status = ocfs2_extend_trans(handle, credits);
272 /* handle still has to be committed at
278 goto restarted_transaction;
284 ocfs2_commit_trans(osb, handle);
288 ocfs2_free_alloc_context(data_ac);
292 ocfs2_free_alloc_context(meta_ac);
295 if ((!status) && restart_func) {
303 static int __ocfs2_remove_xattr_range(struct inode *inode,
304 struct buffer_head *root_bh,
305 struct ocfs2_xattr_value_root *xv,
306 u32 cpos, u32 phys_cpos, u32 len,
307 struct ocfs2_cached_dealloc_ctxt *dealloc)
310 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
311 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
312 struct inode *tl_inode = osb->osb_tl_inode;
314 struct ocfs2_alloc_context *meta_ac = NULL;
316 ret = ocfs2_lock_allocators(inode, root_bh, &xv->xr_list,
317 0, 1, NULL, &meta_ac,
318 OCFS2_XATTR_VALUE_EXTENT, xv);
324 mutex_lock(&tl_inode->i_mutex);
326 if (ocfs2_truncate_log_needs_flush(osb)) {
327 ret = __ocfs2_flush_truncate_log(osb);
334 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
335 if (IS_ERR(handle)) {
336 ret = PTR_ERR(handle);
341 ret = ocfs2_journal_access(handle, inode, root_bh,
342 OCFS2_JOURNAL_ACCESS_WRITE);
348 ret = ocfs2_remove_extent(inode, root_bh, cpos, len, handle, meta_ac,
349 dealloc, OCFS2_XATTR_VALUE_EXTENT, xv);
355 le32_add_cpu(&xv->xr_clusters, -len);
357 ret = ocfs2_journal_dirty(handle, root_bh);
363 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
368 ocfs2_commit_trans(osb, handle);
370 mutex_unlock(&tl_inode->i_mutex);
373 ocfs2_free_alloc_context(meta_ac);
378 static int ocfs2_xattr_shrink_size(struct inode *inode,
381 struct buffer_head *root_bh,
382 struct ocfs2_xattr_value_root *xv)
385 u32 trunc_len, cpos, phys_cpos, alloc_size;
387 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
388 struct ocfs2_cached_dealloc_ctxt dealloc;
390 ocfs2_init_dealloc_ctxt(&dealloc);
392 if (old_clusters <= new_clusters)
396 trunc_len = old_clusters - new_clusters;
398 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
399 &alloc_size, &xv->xr_list);
405 if (alloc_size > trunc_len)
406 alloc_size = trunc_len;
408 ret = __ocfs2_remove_xattr_range(inode, root_bh, xv, cpos,
409 phys_cpos, alloc_size,
416 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
417 ocfs2_remove_xattr_clusters_from_cache(inode, block,
420 trunc_len -= alloc_size;
424 ocfs2_schedule_truncate_log_flush(osb, 1);
425 ocfs2_run_deallocs(osb, &dealloc);
430 static int ocfs2_xattr_value_truncate(struct inode *inode,
431 struct buffer_head *root_bh,
432 struct ocfs2_xattr_value_root *xv,
436 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
437 u32 old_clusters = le32_to_cpu(xv->xr_clusters);
439 if (new_clusters == old_clusters)
442 if (new_clusters > old_clusters)
443 ret = ocfs2_xattr_extend_allocation(inode,
444 new_clusters - old_clusters,
447 ret = ocfs2_xattr_shrink_size(inode,
448 old_clusters, new_clusters,
454 static int ocfs2_xattr_list_entries(struct inode *inode,
455 struct ocfs2_xattr_header *header,
456 char *buffer, size_t buffer_size)
458 size_t rest = buffer_size;
461 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
462 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
463 struct xattr_handler *handler =
464 ocfs2_xattr_handler(ocfs2_xattr_get_type(entry));
467 size_t size = handler->list(inode, buffer, rest,
469 le16_to_cpu(entry->xe_name_offset)),
480 return buffer_size - rest;
483 static int ocfs2_xattr_ibody_list(struct inode *inode,
484 struct ocfs2_dinode *di,
488 struct ocfs2_xattr_header *header = NULL;
489 struct ocfs2_inode_info *oi = OCFS2_I(inode);
492 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
495 header = (struct ocfs2_xattr_header *)
496 ((void *)di + inode->i_sb->s_blocksize -
497 le16_to_cpu(di->i_xattr_inline_size));
499 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
504 static int ocfs2_xattr_block_list(struct inode *inode,
505 struct ocfs2_dinode *di,
509 struct buffer_head *blk_bh = NULL;
510 struct ocfs2_xattr_block *xb;
513 if (!di->i_xattr_loc)
516 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
517 le64_to_cpu(di->i_xattr_loc),
518 &blk_bh, OCFS2_BH_CACHED, inode);
523 /*Verify the signature of xattr block*/
524 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
525 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
530 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
532 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
533 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
534 ret = ocfs2_xattr_list_entries(inode, header,
535 buffer, buffer_size);
537 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
538 ret = ocfs2_xattr_tree_list_index_block(inode, xt,
539 buffer, buffer_size);
547 ssize_t ocfs2_listxattr(struct dentry *dentry,
551 int ret = 0, i_ret = 0, b_ret = 0;
552 struct buffer_head *di_bh = NULL;
553 struct ocfs2_dinode *di = NULL;
554 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
556 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
559 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
565 di = (struct ocfs2_dinode *)di_bh->b_data;
567 down_read(&oi->ip_xattr_sem);
568 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
576 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
581 up_read(&oi->ip_xattr_sem);
582 ocfs2_inode_unlock(dentry->d_inode, 0);
586 return i_ret + b_ret;
589 static int ocfs2_xattr_find_entry(int name_index,
591 struct ocfs2_xattr_search *xs)
593 struct ocfs2_xattr_entry *entry;
600 name_len = strlen(name);
602 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
603 cmp = name_index - ocfs2_xattr_get_type(entry);
605 cmp = name_len - entry->xe_name_len;
607 cmp = memcmp(name, (xs->base +
608 le16_to_cpu(entry->xe_name_offset)),
616 return cmp ? -ENODATA : 0;
619 static int ocfs2_xattr_get_value_outside(struct inode *inode,
620 struct ocfs2_xattr_value_root *xv,
624 u32 cpos, p_cluster, num_clusters, bpc, clusters;
627 size_t cplen, blocksize;
628 struct buffer_head *bh = NULL;
629 struct ocfs2_extent_list *el;
632 clusters = le32_to_cpu(xv->xr_clusters);
633 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
634 blocksize = inode->i_sb->s_blocksize;
637 while (cpos < clusters) {
638 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
645 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
646 /* Copy ocfs2_xattr_value */
647 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
648 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
649 &bh, OCFS2_BH_CACHED, inode);
655 cplen = len >= blocksize ? blocksize : len;
656 memcpy(buffer, bh->b_data, cplen);
665 cpos += num_clusters;
671 static int ocfs2_xattr_ibody_get(struct inode *inode,
676 struct ocfs2_xattr_search *xs)
678 struct ocfs2_inode_info *oi = OCFS2_I(inode);
679 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
680 struct ocfs2_xattr_value_root *xv;
684 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
687 xs->end = (void *)di + inode->i_sb->s_blocksize;
688 xs->header = (struct ocfs2_xattr_header *)
689 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
690 xs->base = (void *)xs->header;
691 xs->here = xs->header->xh_entries;
693 ret = ocfs2_xattr_find_entry(name_index, name, xs);
696 size = le64_to_cpu(xs->here->xe_value_size);
698 if (size > buffer_size)
700 if (ocfs2_xattr_is_local(xs->here)) {
701 memcpy(buffer, (void *)xs->base +
702 le16_to_cpu(xs->here->xe_name_offset) +
703 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
705 xv = (struct ocfs2_xattr_value_root *)
706 (xs->base + le16_to_cpu(
707 xs->here->xe_name_offset) +
708 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
709 ret = ocfs2_xattr_get_value_outside(inode, xv,
721 static int ocfs2_xattr_block_get(struct inode *inode,
726 struct ocfs2_xattr_search *xs)
728 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
729 struct buffer_head *blk_bh = NULL;
730 struct ocfs2_xattr_block *xb;
731 struct ocfs2_xattr_value_root *xv;
733 int ret = -ENODATA, name_offset, name_len, block_off, i;
735 if (!di->i_xattr_loc)
738 memset(&xs->bucket, 0, sizeof(xs->bucket));
740 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
741 le64_to_cpu(di->i_xattr_loc),
742 &blk_bh, OCFS2_BH_CACHED, inode);
747 /*Verify the signature of xattr block*/
748 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
749 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
754 xs->xattr_bh = blk_bh;
755 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
757 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
758 xs->header = &xb->xb_attrs.xb_header;
759 xs->base = (void *)xs->header;
760 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
761 xs->here = xs->header->xh_entries;
763 ret = ocfs2_xattr_find_entry(name_index, name, xs);
765 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
771 size = le64_to_cpu(xs->here->xe_value_size);
774 if (size > buffer_size)
777 name_offset = le16_to_cpu(xs->here->xe_name_offset);
778 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
779 i = xs->here - xs->header->xh_entries;
781 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
782 ret = ocfs2_xattr_bucket_get_name_value(inode,
787 xs->base = xs->bucket.bhs[block_off]->b_data;
789 if (ocfs2_xattr_is_local(xs->here)) {
790 memcpy(buffer, (void *)xs->base +
791 name_offset + name_len, size);
793 xv = (struct ocfs2_xattr_value_root *)
794 (xs->base + name_offset + name_len);
795 ret = ocfs2_xattr_get_value_outside(inode, xv,
805 for (i = 0; i < OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET; i++)
806 brelse(xs->bucket.bhs[i]);
807 memset(&xs->bucket, 0, sizeof(xs->bucket));
815 * Copy an extended attribute into the buffer provided.
816 * Buffer is NULL to compute the size of buffer required.
818 int ocfs2_xattr_get(struct inode *inode,
825 struct ocfs2_dinode *di = NULL;
826 struct buffer_head *di_bh = NULL;
827 struct ocfs2_inode_info *oi = OCFS2_I(inode);
828 struct ocfs2_xattr_search xis = {
829 .not_found = -ENODATA,
831 struct ocfs2_xattr_search xbs = {
832 .not_found = -ENODATA,
835 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
838 ret = ocfs2_inode_lock(inode, &di_bh, 0);
843 xis.inode_bh = xbs.inode_bh = di_bh;
844 di = (struct ocfs2_dinode *)di_bh->b_data;
846 down_read(&oi->ip_xattr_sem);
847 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
850 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
852 up_read(&oi->ip_xattr_sem);
853 ocfs2_inode_unlock(inode, 0);
860 static int __ocfs2_xattr_set_value_outside(struct inode *inode,
861 struct ocfs2_xattr_value_root *xv,
865 int ret = 0, i, cp_len, credits;
866 u16 blocksize = inode->i_sb->s_blocksize;
867 u32 p_cluster, num_clusters;
868 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
869 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
871 struct buffer_head *bh = NULL;
874 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
876 credits = clusters * bpc;
877 handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb), credits);
878 if (IS_ERR(handle)) {
879 ret = PTR_ERR(handle);
884 while (cpos < clusters) {
885 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
886 &num_clusters, &xv->xr_list);
892 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
894 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
895 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
896 &bh, OCFS2_BH_CACHED, inode);
902 ret = ocfs2_journal_access(handle,
905 OCFS2_JOURNAL_ACCESS_WRITE);
911 cp_len = value_len > blocksize ? blocksize : value_len;
912 memcpy(bh->b_data, value, cp_len);
915 if (cp_len < blocksize)
916 memset(bh->b_data + cp_len, 0,
919 ret = ocfs2_journal_dirty(handle, bh);
928 * XXX: do we need to empty all the following
929 * blocks in this cluster?
934 cpos += num_clusters;
937 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
944 static int ocfs2_xattr_cleanup(struct inode *inode,
945 struct ocfs2_xattr_info *xi,
946 struct ocfs2_xattr_search *xs,
949 handle_t *handle = NULL;
951 size_t name_len = strlen(xi->name);
952 void *val = xs->base + offs;
953 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
955 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
956 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
957 if (IS_ERR(handle)) {
958 ret = PTR_ERR(handle);
962 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
963 OCFS2_JOURNAL_ACCESS_WRITE);
968 /* Decrease xattr count */
969 le16_add_cpu(&xs->header->xh_count, -1);
970 /* Remove the xattr entry and tree root which has already be set*/
971 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
972 memset(val, 0, size);
974 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
978 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
983 static int ocfs2_xattr_update_entry(struct inode *inode,
984 struct ocfs2_xattr_info *xi,
985 struct ocfs2_xattr_search *xs,
988 handle_t *handle = NULL;
991 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
992 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
993 if (IS_ERR(handle)) {
994 ret = PTR_ERR(handle);
998 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
999 OCFS2_JOURNAL_ACCESS_WRITE);
1005 xs->here->xe_name_offset = cpu_to_le16(offs);
1006 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1007 if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
1008 ocfs2_xattr_set_local(xs->here, 1);
1010 ocfs2_xattr_set_local(xs->here, 0);
1011 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1013 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1017 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1023 * ocfs2_xattr_set_value_outside()
1025 * Set large size value in B tree.
1027 static int ocfs2_xattr_set_value_outside(struct inode *inode,
1028 struct ocfs2_xattr_info *xi,
1029 struct ocfs2_xattr_search *xs,
1032 size_t name_len = strlen(xi->name);
1033 void *val = xs->base + offs;
1034 struct ocfs2_xattr_value_root *xv = NULL;
1035 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1038 memset(val, 0, size);
1039 memcpy(val, xi->name, name_len);
1040 xv = (struct ocfs2_xattr_value_root *)
1041 (val + OCFS2_XATTR_SIZE(name_len));
1042 xv->xr_clusters = 0;
1043 xv->xr_last_eb_blk = 0;
1044 xv->xr_list.l_tree_depth = 0;
1045 xv->xr_list.l_count = cpu_to_le16(1);
1046 xv->xr_list.l_next_free_rec = 0;
1048 ret = ocfs2_xattr_value_truncate(inode, xs->xattr_bh, xv,
1054 ret = __ocfs2_xattr_set_value_outside(inode, xv, xi->value,
1060 ret = ocfs2_xattr_update_entry(inode, xi, xs, offs);
1068 * ocfs2_xattr_set_entry_local()
1070 * Set, replace or remove extended attribute in local.
1072 static void ocfs2_xattr_set_entry_local(struct inode *inode,
1073 struct ocfs2_xattr_info *xi,
1074 struct ocfs2_xattr_search *xs,
1075 struct ocfs2_xattr_entry *last,
1078 size_t name_len = strlen(xi->name);
1081 if (xi->value && xs->not_found) {
1082 /* Insert the new xattr entry. */
1083 le16_add_cpu(&xs->header->xh_count, 1);
1084 ocfs2_xattr_set_type(last, xi->name_index);
1085 ocfs2_xattr_set_local(last, 1);
1086 last->xe_name_len = name_len;
1092 first_val = xs->base + min_offs;
1093 offs = le16_to_cpu(xs->here->xe_name_offset);
1094 val = xs->base + offs;
1096 if (le64_to_cpu(xs->here->xe_value_size) >
1097 OCFS2_XATTR_INLINE_SIZE)
1098 size = OCFS2_XATTR_SIZE(name_len) +
1099 OCFS2_XATTR_ROOT_SIZE;
1101 size = OCFS2_XATTR_SIZE(name_len) +
1102 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1104 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1105 OCFS2_XATTR_SIZE(xi->value_len)) {
1106 /* The old and the new value have the
1107 same size. Just replace the value. */
1108 ocfs2_xattr_set_local(xs->here, 1);
1109 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1110 /* Clear value bytes. */
1111 memset(val + OCFS2_XATTR_SIZE(name_len),
1113 OCFS2_XATTR_SIZE(xi->value_len));
1114 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1119 /* Remove the old name+value. */
1120 memmove(first_val + size, first_val, val - first_val);
1121 memset(first_val, 0, size);
1122 xs->here->xe_name_hash = 0;
1123 xs->here->xe_name_offset = 0;
1124 ocfs2_xattr_set_local(xs->here, 1);
1125 xs->here->xe_value_size = 0;
1129 /* Adjust all value offsets. */
1130 last = xs->header->xh_entries;
1131 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1132 size_t o = le16_to_cpu(last->xe_name_offset);
1135 last->xe_name_offset = cpu_to_le16(o + size);
1140 /* Remove the old entry. */
1142 memmove(xs->here, xs->here + 1,
1143 (void *)last - (void *)xs->here);
1144 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1145 le16_add_cpu(&xs->header->xh_count, -1);
1149 /* Insert the new name+value. */
1150 size_t size = OCFS2_XATTR_SIZE(name_len) +
1151 OCFS2_XATTR_SIZE(xi->value_len);
1152 void *val = xs->base + min_offs - size;
1154 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1155 memset(val, 0, size);
1156 memcpy(val, xi->name, name_len);
1157 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1160 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1161 ocfs2_xattr_set_local(xs->here, 1);
1162 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1169 * ocfs2_xattr_set_entry()
1171 * Set extended attribute entry into inode or block.
1173 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1174 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1175 * then set value in B tree with set_value_outside().
1177 static int ocfs2_xattr_set_entry(struct inode *inode,
1178 struct ocfs2_xattr_info *xi,
1179 struct ocfs2_xattr_search *xs,
1182 struct ocfs2_xattr_entry *last;
1183 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1184 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1185 size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1187 handle_t *handle = NULL;
1189 struct ocfs2_xattr_info xi_l = {
1190 .name_index = xi->name_index,
1193 .value_len = xi->value_len,
1196 /* Compute min_offs, last and free space. */
1197 last = xs->header->xh_entries;
1199 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1200 size_t offs = le16_to_cpu(last->xe_name_offset);
1201 if (offs < min_offs)
1206 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
1210 if (!xs->not_found) {
1212 if (ocfs2_xattr_is_local(xs->here))
1213 size = OCFS2_XATTR_SIZE(name_len) +
1214 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1216 size = OCFS2_XATTR_SIZE(name_len) +
1217 OCFS2_XATTR_ROOT_SIZE;
1218 free += (size + sizeof(struct ocfs2_xattr_entry));
1220 /* Check free space in inode or block */
1221 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1222 if (free < sizeof(struct ocfs2_xattr_entry) +
1223 OCFS2_XATTR_SIZE(name_len) +
1224 OCFS2_XATTR_ROOT_SIZE) {
1228 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1229 xi_l.value = (void *)&def_xv;
1230 xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
1231 } else if (xi->value) {
1232 if (free < sizeof(struct ocfs2_xattr_entry) +
1233 OCFS2_XATTR_SIZE(name_len) +
1234 OCFS2_XATTR_SIZE(xi->value_len)) {
1240 if (!xs->not_found) {
1241 /* For existing extended attribute */
1242 size_t size = OCFS2_XATTR_SIZE(name_len) +
1243 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1244 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1245 void *val = xs->base + offs;
1247 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1248 /* Replace existing local xattr with tree root */
1249 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
1254 } else if (!ocfs2_xattr_is_local(xs->here)) {
1255 /* For existing xattr which has value outside */
1256 struct ocfs2_xattr_value_root *xv = NULL;
1257 xv = (struct ocfs2_xattr_value_root *)(val +
1258 OCFS2_XATTR_SIZE(name_len));
1260 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1262 * If new value need set outside also,
1263 * first truncate old value to new value,
1264 * then set new value with set_value_outside().
1266 ret = ocfs2_xattr_value_truncate(inode,
1275 ret = __ocfs2_xattr_set_value_outside(inode,
1284 ret = ocfs2_xattr_update_entry(inode,
1293 * If new value need set in local,
1294 * just trucate old value to zero.
1296 ret = ocfs2_xattr_value_truncate(inode,
1306 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1307 OCFS2_INODE_UPDATE_CREDITS);
1308 if (IS_ERR(handle)) {
1309 ret = PTR_ERR(handle);
1314 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1315 OCFS2_JOURNAL_ACCESS_WRITE);
1321 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1322 /*set extended attribue in external blcok*/
1323 ret = ocfs2_extend_trans(handle,
1324 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1329 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1330 OCFS2_JOURNAL_ACCESS_WRITE);
1338 * Set value in local, include set tree root in local.
1339 * This is the first step for value size >INLINE_SIZE.
1341 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1343 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1344 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1351 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1352 (flag & OCFS2_INLINE_XATTR_FL)) {
1353 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1354 unsigned int xattrsize = osb->s_xattr_inline_size;
1357 * Adjust extent record count or inline data size
1358 * to reserve space for extended attribute.
1360 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1361 struct ocfs2_inline_data *idata = &di->id2.i_data;
1362 le16_add_cpu(&idata->id_count, -xattrsize);
1363 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1364 struct ocfs2_extent_list *el = &di->id2.i_list;
1365 le16_add_cpu(&el->l_count, -(xattrsize /
1366 sizeof(struct ocfs2_extent_rec)));
1368 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1370 /* Update xattr flag */
1371 spin_lock(&oi->ip_lock);
1372 oi->ip_dyn_features |= flag;
1373 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1374 spin_unlock(&oi->ip_lock);
1375 /* Update inode ctime */
1376 inode->i_ctime = CURRENT_TIME;
1377 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
1378 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
1380 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1385 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1387 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1389 * Set value outside in B tree.
1390 * This is the second step for value size > INLINE_SIZE.
1392 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1393 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, offs);
1399 * If set value outside failed, we have to clean
1400 * the junk tree root we have already set in local.
1402 ret2 = ocfs2_xattr_cleanup(inode, xi, xs, offs);
1412 static int ocfs2_xattr_free_block(handle_t *handle,
1413 struct ocfs2_super *osb,
1414 struct ocfs2_xattr_block *xb)
1416 struct inode *xb_alloc_inode;
1417 struct buffer_head *xb_alloc_bh = NULL;
1418 u64 blk = le64_to_cpu(xb->xb_blkno);
1419 u16 bit = le16_to_cpu(xb->xb_suballoc_bit);
1420 u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1423 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1424 EXTENT_ALLOC_SYSTEM_INODE,
1425 le16_to_cpu(xb->xb_suballoc_slot));
1426 if (!xb_alloc_inode) {
1431 mutex_lock(&xb_alloc_inode->i_mutex);
1433 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1438 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
1443 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1448 ocfs2_inode_unlock(xb_alloc_inode, 1);
1449 brelse(xb_alloc_bh);
1451 mutex_unlock(&xb_alloc_inode->i_mutex);
1452 iput(xb_alloc_inode);
1457 static int ocfs2_remove_value_outside(struct inode*inode,
1458 struct buffer_head *bh,
1459 struct ocfs2_xattr_header *header)
1463 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1464 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1466 if (!ocfs2_xattr_is_local(entry)) {
1467 struct ocfs2_xattr_value_root *xv;
1470 val = (void *)header +
1471 le16_to_cpu(entry->xe_name_offset);
1472 xv = (struct ocfs2_xattr_value_root *)
1473 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
1474 ret = ocfs2_xattr_value_truncate(inode, bh, xv, 0);
1485 static int ocfs2_xattr_ibody_remove(struct inode *inode,
1486 struct buffer_head *di_bh)
1489 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1490 struct ocfs2_xattr_header *header;
1493 header = (struct ocfs2_xattr_header *)
1494 ((void *)di + inode->i_sb->s_blocksize -
1495 le16_to_cpu(di->i_xattr_inline_size));
1497 ret = ocfs2_remove_value_outside(inode, di_bh, header);
1502 static int ocfs2_xattr_block_remove(struct inode *inode,
1503 struct buffer_head *blk_bh)
1505 struct ocfs2_xattr_block *xb;
1506 struct ocfs2_xattr_header *header;
1509 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1510 header = &(xb->xb_attrs.xb_header);
1512 ret = ocfs2_remove_value_outside(inode, blk_bh, header);
1518 * ocfs2_xattr_remove()
1520 * Free extended attribute resources associated with this inode.
1522 int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
1524 struct ocfs2_xattr_block *xb;
1525 struct buffer_head *blk_bh = NULL;
1526 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1527 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1528 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1532 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1535 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1536 ret = ocfs2_xattr_ibody_remove(inode, di_bh);
1542 if (di->i_xattr_loc) {
1543 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1544 le64_to_cpu(di->i_xattr_loc),
1545 &blk_bh, OCFS2_BH_CACHED, inode);
1550 /*Verify the signature of xattr block*/
1551 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
1552 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
1557 ret = ocfs2_xattr_block_remove(inode, blk_bh);
1564 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1565 OCFS2_INODE_UPDATE_CREDITS);
1566 if (IS_ERR(handle)) {
1567 ret = PTR_ERR(handle);
1571 ret = ocfs2_journal_access(handle, inode, di_bh,
1572 OCFS2_JOURNAL_ACCESS_WRITE);
1578 if (di->i_xattr_loc) {
1579 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1580 ocfs2_xattr_free_block(handle, osb, xb);
1581 di->i_xattr_loc = cpu_to_le64(0);
1584 spin_lock(&oi->ip_lock);
1585 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
1586 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1587 spin_unlock(&oi->ip_lock);
1589 ret = ocfs2_journal_dirty(handle, di_bh);
1593 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1600 static int ocfs2_xattr_has_space_inline(struct inode *inode,
1601 struct ocfs2_dinode *di)
1603 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1604 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
1607 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
1610 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1611 struct ocfs2_inline_data *idata = &di->id2.i_data;
1612 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
1613 } else if (ocfs2_inode_is_fast_symlink(inode)) {
1614 free = ocfs2_fast_symlink_chars(inode->i_sb) -
1615 le64_to_cpu(di->i_size);
1617 struct ocfs2_extent_list *el = &di->id2.i_list;
1618 free = (le16_to_cpu(el->l_count) -
1619 le16_to_cpu(el->l_next_free_rec)) *
1620 sizeof(struct ocfs2_extent_rec);
1622 if (free >= xattrsize)
1629 * ocfs2_xattr_ibody_find()
1631 * Find extended attribute in inode block and
1632 * fill search info into struct ocfs2_xattr_search.
1634 static int ocfs2_xattr_ibody_find(struct inode *inode,
1637 struct ocfs2_xattr_search *xs)
1639 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1640 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1644 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1647 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1648 down_read(&oi->ip_alloc_sem);
1649 has_space = ocfs2_xattr_has_space_inline(inode, di);
1650 up_read(&oi->ip_alloc_sem);
1655 xs->xattr_bh = xs->inode_bh;
1656 xs->end = (void *)di + inode->i_sb->s_blocksize;
1657 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
1658 xs->header = (struct ocfs2_xattr_header *)
1659 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1661 xs->header = (struct ocfs2_xattr_header *)
1662 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
1663 xs->base = (void *)xs->header;
1664 xs->here = xs->header->xh_entries;
1666 /* Find the named attribute. */
1667 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1668 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1669 if (ret && ret != -ENODATA)
1671 xs->not_found = ret;
1678 * ocfs2_xattr_ibody_set()
1680 * Set, replace or remove an extended attribute into inode block.
1683 static int ocfs2_xattr_ibody_set(struct inode *inode,
1684 struct ocfs2_xattr_info *xi,
1685 struct ocfs2_xattr_search *xs)
1687 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1688 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1691 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1694 down_write(&oi->ip_alloc_sem);
1695 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1696 if (!ocfs2_xattr_has_space_inline(inode, di)) {
1702 ret = ocfs2_xattr_set_entry(inode, xi, xs,
1703 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
1705 up_write(&oi->ip_alloc_sem);
1711 * ocfs2_xattr_block_find()
1713 * Find extended attribute in external block and
1714 * fill search info into struct ocfs2_xattr_search.
1716 static int ocfs2_xattr_block_find(struct inode *inode,
1719 struct ocfs2_xattr_search *xs)
1721 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1722 struct buffer_head *blk_bh = NULL;
1723 struct ocfs2_xattr_block *xb;
1726 if (!di->i_xattr_loc)
1729 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1730 le64_to_cpu(di->i_xattr_loc),
1731 &blk_bh, OCFS2_BH_CACHED, inode);
1736 /*Verify the signature of xattr block*/
1737 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
1738 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
1743 xs->xattr_bh = blk_bh;
1744 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1746 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1747 xs->header = &xb->xb_attrs.xb_header;
1748 xs->base = (void *)xs->header;
1749 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
1750 xs->here = xs->header->xh_entries;
1752 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1754 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
1758 if (ret && ret != -ENODATA) {
1759 xs->xattr_bh = NULL;
1762 xs->not_found = ret;
1771 * ocfs2_xattr_block_set()
1773 * Set, replace or remove an extended attribute into external block.
1776 static int ocfs2_xattr_block_set(struct inode *inode,
1777 struct ocfs2_xattr_info *xi,
1778 struct ocfs2_xattr_search *xs)
1780 struct buffer_head *new_bh = NULL;
1781 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1782 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1783 struct ocfs2_alloc_context *meta_ac = NULL;
1784 handle_t *handle = NULL;
1785 struct ocfs2_xattr_block *xblk = NULL;
1786 u16 suballoc_bit_start;
1791 if (!xs->xattr_bh) {
1793 * Alloc one external block for extended attribute
1796 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
1801 handle = ocfs2_start_trans(osb,
1802 OCFS2_XATTR_BLOCK_CREATE_CREDITS);
1803 if (IS_ERR(handle)) {
1804 ret = PTR_ERR(handle);
1808 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1809 OCFS2_JOURNAL_ACCESS_CREATE);
1815 ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
1816 &suballoc_bit_start, &num_got,
1823 new_bh = sb_getblk(inode->i_sb, first_blkno);
1824 ocfs2_set_new_buffer_uptodate(inode, new_bh);
1826 ret = ocfs2_journal_access(handle, inode, new_bh,
1827 OCFS2_JOURNAL_ACCESS_CREATE);
1833 /* Initialize ocfs2_xattr_block */
1834 xs->xattr_bh = new_bh;
1835 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
1836 memset(xblk, 0, inode->i_sb->s_blocksize);
1837 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
1838 xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
1839 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1840 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
1841 xblk->xb_blkno = cpu_to_le64(first_blkno);
1843 xs->header = &xblk->xb_attrs.xb_header;
1844 xs->base = (void *)xs->header;
1845 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
1846 xs->here = xs->header->xh_entries;
1849 ret = ocfs2_journal_dirty(handle, new_bh);
1854 di->i_xattr_loc = cpu_to_le64(first_blkno);
1855 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1859 ocfs2_commit_trans(osb, handle);
1862 ocfs2_free_alloc_context(meta_ac);
1867 /* Set extended attribute into external block */
1868 ret = ocfs2_xattr_set_entry(inode, xi, xs, OCFS2_HAS_XATTR_FL);
1876 * Set, replace or remove an extended attribute for this inode.
1877 * value is NULL to remove an existing extended attribute, else either
1878 * create or replace an extended attribute.
1880 int ocfs2_xattr_set(struct inode *inode,
1887 struct buffer_head *di_bh = NULL;
1888 struct ocfs2_dinode *di;
1891 struct ocfs2_xattr_info xi = {
1892 .name_index = name_index,
1895 .value_len = value_len,
1898 struct ocfs2_xattr_search xis = {
1899 .not_found = -ENODATA,
1902 struct ocfs2_xattr_search xbs = {
1903 .not_found = -ENODATA,
1906 ret = ocfs2_inode_lock(inode, &di_bh, 1);
1911 xis.inode_bh = xbs.inode_bh = di_bh;
1912 di = (struct ocfs2_dinode *)di_bh->b_data;
1914 down_write(&OCFS2_I(inode)->ip_xattr_sem);
1916 * Scan inode and external block to find the same name
1917 * extended attribute and collect search infomation.
1919 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
1922 if (xis.not_found) {
1923 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
1928 if (xis.not_found && xbs.not_found) {
1930 if (flags & XATTR_REPLACE)
1937 if (flags & XATTR_CREATE)
1942 /* Remove existing extended attribute */
1944 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
1945 else if (!xbs.not_found)
1946 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
1948 /* We always try to set extended attribute into inode first*/
1949 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
1950 if (!ret && !xbs.not_found) {
1952 * If succeed and that extended attribute existing in
1953 * external block, then we will remove it.
1957 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
1958 } else if (ret == -ENOSPC) {
1959 if (di->i_xattr_loc && !xbs.xattr_bh) {
1960 ret = ocfs2_xattr_block_find(inode, name_index,
1966 * If no space in inode, we will set extended attribute
1967 * into external block.
1969 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
1972 if (!xis.not_found) {
1974 * If succeed and that extended attribute
1975 * existing in inode, we will remove it.
1979 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
1984 up_write(&OCFS2_I(inode)->ip_xattr_sem);
1985 ocfs2_inode_unlock(inode, 1);
1987 brelse(xbs.xattr_bh);
1992 static inline u32 ocfs2_xattr_hash_by_name(struct inode *inode,
1994 const char *suffix_name)
1996 struct xattr_handler *handler = ocfs2_xattr_handler(name_index);
1997 char *prefix = handler->prefix;
1998 int prefix_len = strlen(handler->prefix);
2000 return ocfs2_xattr_name_hash(inode, prefix, prefix_len,
2001 (char *)suffix_name, strlen(suffix_name));
2005 * Find the xattr extent rec which may contains name_hash.
2006 * e_cpos will be the first name hash of the xattr rec.
2007 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
2009 static int ocfs2_xattr_get_rec(struct inode *inode,
2014 struct ocfs2_extent_list *el)
2017 struct buffer_head *eb_bh = NULL;
2018 struct ocfs2_extent_block *eb;
2019 struct ocfs2_extent_rec *rec = NULL;
2022 if (el->l_tree_depth) {
2023 ret = ocfs2_find_leaf(inode, el, name_hash, &eb_bh);
2029 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
2032 if (el->l_tree_depth) {
2033 ocfs2_error(inode->i_sb,
2034 "Inode %lu has non zero tree depth in "
2035 "xattr tree block %llu\n", inode->i_ino,
2036 (unsigned long long)eb_bh->b_blocknr);
2042 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
2043 rec = &el->l_recs[i];
2045 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
2046 e_blkno = le64_to_cpu(rec->e_blkno);
2052 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
2053 "record (%u, %u, 0) in xattr", inode->i_ino,
2054 le32_to_cpu(rec->e_cpos),
2055 ocfs2_rec_clusters(el, rec));
2060 *p_blkno = le64_to_cpu(rec->e_blkno);
2061 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
2063 *e_cpos = le32_to_cpu(rec->e_cpos);
2069 typedef int (xattr_bucket_func)(struct inode *inode,
2070 struct ocfs2_xattr_bucket *bucket,
2073 static int ocfs2_find_xe_in_bucket(struct inode *inode,
2074 struct buffer_head *header_bh,
2081 int i, ret = 0, cmp = 1, block_off, new_offset;
2082 struct ocfs2_xattr_header *xh =
2083 (struct ocfs2_xattr_header *)header_bh->b_data;
2084 size_t name_len = strlen(name);
2085 struct ocfs2_xattr_entry *xe = NULL;
2086 struct buffer_head *name_bh = NULL;
2090 * We don't use binary search in the bucket because there
2091 * may be multiple entries with the same name hash.
2093 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
2094 xe = &xh->xh_entries[i];
2096 if (name_hash > le32_to_cpu(xe->xe_name_hash))
2098 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
2101 cmp = name_index - ocfs2_xattr_get_type(xe);
2103 cmp = name_len - xe->xe_name_len;
2107 ret = ocfs2_xattr_bucket_get_name_value(inode,
2117 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
2118 header_bh->b_blocknr + block_off,
2119 &name_bh, OCFS2_BH_CACHED, inode);
2124 xe_name = name_bh->b_data + new_offset;
2126 cmp = memcmp(name, xe_name, name_len);
2142 * Find the specified xattr entry in a series of buckets.
2143 * This series start from p_blkno and last for num_clusters.
2144 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
2145 * the num of the valid buckets.
2147 * Return the buffer_head this xattr should reside in. And if the xattr's
2148 * hash is in the gap of 2 buckets, return the lower bucket.
2150 static int ocfs2_xattr_bucket_find(struct inode *inode,
2157 struct ocfs2_xattr_search *xs)
2160 struct buffer_head *bh = NULL;
2161 struct buffer_head *lower_bh = NULL;
2162 struct ocfs2_xattr_header *xh = NULL;
2163 struct ocfs2_xattr_entry *xe = NULL;
2165 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2166 int low_bucket = 0, bucket, high_bucket;
2170 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), p_blkno,
2171 &bh, OCFS2_BH_CACHED, inode);
2177 xh = (struct ocfs2_xattr_header *)bh->b_data;
2178 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
2180 while (low_bucket <= high_bucket) {
2183 bucket = (low_bucket + high_bucket) / 2;
2185 blkno = p_blkno + bucket * blk_per_bucket;
2187 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
2188 &bh, OCFS2_BH_CACHED, inode);
2194 xh = (struct ocfs2_xattr_header *)bh->b_data;
2195 xe = &xh->xh_entries[0];
2196 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
2197 high_bucket = bucket - 1;
2202 * Check whether the hash of the last entry in our
2203 * bucket is larger than the search one.
2205 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
2206 last_hash = le32_to_cpu(xe->xe_name_hash);
2208 /* record lower_bh which may be the insert place. */
2213 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
2214 low_bucket = bucket + 1;
2218 /* the searched xattr should reside in this bucket if exists. */
2219 ret = ocfs2_find_xe_in_bucket(inode, lower_bh,
2220 name_index, name, name_hash,
2230 * Record the bucket we have found.
2231 * When the xattr's hash value is in the gap of 2 buckets, we will
2232 * always set it to the previous bucket.
2236 * We can't find any bucket whose first name_hash is less
2237 * than the find name_hash.
2239 BUG_ON(bh->b_blocknr != p_blkno);
2243 xs->bucket.bhs[0] = lower_bh;
2244 xs->bucket.xh = (struct ocfs2_xattr_header *)
2245 xs->bucket.bhs[0]->b_data;
2248 xs->header = xs->bucket.xh;
2249 xs->base = xs->bucket.bhs[0]->b_data;
2250 xs->end = xs->base + inode->i_sb->s_blocksize;
2254 * If we have found the xattr enty, read all the blocks in
2257 ret = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
2258 xs->bucket.bhs[0]->b_blocknr + 1,
2259 blk_per_bucket - 1, &xs->bucket.bhs[1],
2260 OCFS2_BH_CACHED, inode);
2266 xs->here = &xs->header->xh_entries[index];
2267 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
2268 (unsigned long long)xs->bucket.bhs[0]->b_blocknr, index);
2278 static int ocfs2_xattr_index_block_find(struct inode *inode,
2279 struct buffer_head *root_bh,
2282 struct ocfs2_xattr_search *xs)
2285 struct ocfs2_xattr_block *xb =
2286 (struct ocfs2_xattr_block *)root_bh->b_data;
2287 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
2288 struct ocfs2_extent_list *el = &xb_root->xt_list;
2290 u32 first_hash, num_clusters = 0;
2291 u32 name_hash = ocfs2_xattr_hash_by_name(inode, name_index, name);
2293 if (le16_to_cpu(el->l_next_free_rec) == 0)
2296 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
2297 name, name_hash, name_index);
2299 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
2306 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
2308 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
2309 "in the rec is %u\n", num_clusters, p_blkno, first_hash);
2311 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
2312 p_blkno, first_hash, num_clusters, xs);
2318 static int ocfs2_iterate_xattr_buckets(struct inode *inode,
2321 xattr_bucket_func *func,
2325 int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2326 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
2327 u32 num_buckets = clusters * bpc;
2328 struct ocfs2_xattr_bucket bucket;
2330 memset(&bucket, 0, sizeof(bucket));
2332 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
2335 for (i = 0; i < num_buckets; i++, blkno += blk_per_bucket) {
2336 ret = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
2337 blkno, blk_per_bucket,
2338 bucket.bhs, OCFS2_BH_CACHED, inode);
2344 bucket.xh = (struct ocfs2_xattr_header *)bucket.bhs[0]->b_data;
2346 * The real bucket num in this series of blocks is stored
2347 * in the 1st bucket.
2350 num_buckets = le16_to_cpu(bucket.xh->xh_num_buckets);
2352 mlog(0, "iterating xattr bucket %llu\n", blkno);
2354 ret = func(inode, &bucket, para);
2361 for (j = 0; j < blk_per_bucket; j++)
2362 brelse(bucket.bhs[j]);
2363 memset(&bucket, 0, sizeof(bucket));
2367 for (j = 0; j < blk_per_bucket; j++)
2368 brelse(bucket.bhs[j]);
2373 struct ocfs2_xattr_tree_list {
2378 static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
2379 struct ocfs2_xattr_header *xh,
2386 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
2389 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
2391 *block_off = name_offset >> inode->i_sb->s_blocksize_bits;
2392 *new_offset = name_offset % inode->i_sb->s_blocksize;
2397 static int ocfs2_list_xattr_bucket(struct inode *inode,
2398 struct ocfs2_xattr_bucket *bucket,
2402 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
2404 int i, block_off, new_offset;
2406 for (i = 0 ; i < le16_to_cpu(bucket->xh->xh_count); i++) {
2407 struct ocfs2_xattr_entry *entry = &bucket->xh->xh_entries[i];
2408 struct xattr_handler *handler =
2409 ocfs2_xattr_handler(ocfs2_xattr_get_type(entry));
2412 ret = ocfs2_xattr_bucket_get_name_value(inode,
2419 size = handler->list(inode, xl->buffer, xl->buffer_size,
2420 bucket->bhs[block_off]->b_data +
2422 entry->xe_name_len);
2424 if (size > xl->buffer_size)
2428 xl->buffer_size -= size;
2435 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
2436 struct ocfs2_xattr_tree_root *xt,
2440 struct ocfs2_extent_list *el = &xt->xt_list;
2442 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
2444 struct ocfs2_xattr_tree_list xl = {
2446 .buffer_size = buffer_size,
2449 if (le16_to_cpu(el->l_next_free_rec) == 0)
2452 while (name_hash > 0) {
2453 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
2454 &e_cpos, &num_clusters, el);
2460 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
2461 ocfs2_list_xattr_bucket,
2471 name_hash = e_cpos - 1;
2474 ret = buffer_size - xl.buffer_size;