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;
104 struct ocfs2_xattr_entry *here;
108 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
109 struct ocfs2_xattr_tree_root *xt,
113 static inline struct xattr_handler *ocfs2_xattr_handler(int name_index)
115 struct xattr_handler *handler = NULL;
117 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
118 handler = ocfs2_xattr_handler_map[name_index];
123 static inline u32 ocfs2_xattr_name_hash(struct inode *inode,
129 /* Get hash value of uuid from super block */
130 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
133 /* hash extended attribute prefix */
134 for (i = 0; i < prefix_len; i++) {
135 hash = (hash << OCFS2_HASH_SHIFT) ^
136 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
139 /* hash extended attribute name */
140 for (i = 0; i < name_len; i++) {
141 hash = (hash << OCFS2_HASH_SHIFT) ^
142 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
150 * ocfs2_xattr_hash_entry()
152 * Compute the hash of an extended attribute.
154 static void ocfs2_xattr_hash_entry(struct inode *inode,
155 struct ocfs2_xattr_header *header,
156 struct ocfs2_xattr_entry *entry)
159 struct xattr_handler *handler =
160 ocfs2_xattr_handler(ocfs2_xattr_get_type(entry));
161 char *prefix = handler->prefix;
162 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
163 int prefix_len = strlen(handler->prefix);
165 hash = ocfs2_xattr_name_hash(inode, prefix, prefix_len, name,
167 entry->xe_name_hash = cpu_to_le32(hash);
172 static int ocfs2_xattr_extend_allocation(struct inode *inode,
174 struct buffer_head *xattr_bh,
175 struct ocfs2_xattr_value_root *xv)
178 int restart_func = 0;
180 handle_t *handle = NULL;
181 struct ocfs2_alloc_context *data_ac = NULL;
182 struct ocfs2_alloc_context *meta_ac = NULL;
183 enum ocfs2_alloc_restarted why;
184 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
185 struct ocfs2_extent_list *root_el = &xv->xr_list;
186 u32 prev_clusters, logical_start = le32_to_cpu(xv->xr_clusters);
188 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
192 status = ocfs2_lock_allocators(inode, xattr_bh, root_el,
193 clusters_to_add, 0, &data_ac,
194 &meta_ac, OCFS2_XATTR_VALUE_EXTENT, xv);
200 credits = ocfs2_calc_extend_credits(osb->sb, root_el, clusters_to_add);
201 handle = ocfs2_start_trans(osb, credits);
202 if (IS_ERR(handle)) {
203 status = PTR_ERR(handle);
209 restarted_transaction:
210 status = ocfs2_journal_access(handle, inode, xattr_bh,
211 OCFS2_JOURNAL_ACCESS_WRITE);
217 prev_clusters = le32_to_cpu(xv->xr_clusters);
218 status = ocfs2_add_clusters_in_btree(osb,
229 OCFS2_XATTR_VALUE_EXTENT,
231 if ((status < 0) && (status != -EAGAIN)) {
232 if (status != -ENOSPC)
237 status = ocfs2_journal_dirty(handle, xattr_bh);
243 clusters_to_add -= le32_to_cpu(xv->xr_clusters) - prev_clusters;
245 if (why != RESTART_NONE && clusters_to_add) {
246 if (why == RESTART_META) {
247 mlog(0, "restarting function.\n");
250 BUG_ON(why != RESTART_TRANS);
252 mlog(0, "restarting transaction.\n");
253 /* TODO: This can be more intelligent. */
254 credits = ocfs2_calc_extend_credits(osb->sb,
257 status = ocfs2_extend_trans(handle, credits);
259 /* handle still has to be committed at
265 goto restarted_transaction;
271 ocfs2_commit_trans(osb, handle);
275 ocfs2_free_alloc_context(data_ac);
279 ocfs2_free_alloc_context(meta_ac);
282 if ((!status) && restart_func) {
290 static int __ocfs2_remove_xattr_range(struct inode *inode,
291 struct buffer_head *root_bh,
292 struct ocfs2_xattr_value_root *xv,
293 u32 cpos, u32 phys_cpos, u32 len,
294 struct ocfs2_cached_dealloc_ctxt *dealloc)
297 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
298 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
299 struct inode *tl_inode = osb->osb_tl_inode;
301 struct ocfs2_alloc_context *meta_ac = NULL;
303 ret = ocfs2_lock_allocators(inode, root_bh, &xv->xr_list,
304 0, 1, NULL, &meta_ac,
305 OCFS2_XATTR_VALUE_EXTENT, xv);
311 mutex_lock(&tl_inode->i_mutex);
313 if (ocfs2_truncate_log_needs_flush(osb)) {
314 ret = __ocfs2_flush_truncate_log(osb);
321 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
322 if (IS_ERR(handle)) {
323 ret = PTR_ERR(handle);
328 ret = ocfs2_journal_access(handle, inode, root_bh,
329 OCFS2_JOURNAL_ACCESS_WRITE);
335 ret = ocfs2_remove_extent(inode, root_bh, cpos, len, handle, meta_ac,
336 dealloc, OCFS2_XATTR_VALUE_EXTENT, xv);
342 le32_add_cpu(&xv->xr_clusters, -len);
344 ret = ocfs2_journal_dirty(handle, root_bh);
350 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
355 ocfs2_commit_trans(osb, handle);
357 mutex_unlock(&tl_inode->i_mutex);
360 ocfs2_free_alloc_context(meta_ac);
365 static int ocfs2_xattr_shrink_size(struct inode *inode,
368 struct buffer_head *root_bh,
369 struct ocfs2_xattr_value_root *xv)
372 u32 trunc_len, cpos, phys_cpos, alloc_size;
374 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
375 struct ocfs2_cached_dealloc_ctxt dealloc;
377 ocfs2_init_dealloc_ctxt(&dealloc);
379 if (old_clusters <= new_clusters)
383 trunc_len = old_clusters - new_clusters;
385 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
386 &alloc_size, &xv->xr_list);
392 if (alloc_size > trunc_len)
393 alloc_size = trunc_len;
395 ret = __ocfs2_remove_xattr_range(inode, root_bh, xv, cpos,
396 phys_cpos, alloc_size,
403 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
404 ocfs2_remove_xattr_clusters_from_cache(inode, block,
407 trunc_len -= alloc_size;
411 ocfs2_schedule_truncate_log_flush(osb, 1);
412 ocfs2_run_deallocs(osb, &dealloc);
417 static int ocfs2_xattr_value_truncate(struct inode *inode,
418 struct buffer_head *root_bh,
419 struct ocfs2_xattr_value_root *xv,
423 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
424 u32 old_clusters = le32_to_cpu(xv->xr_clusters);
426 if (new_clusters == old_clusters)
429 if (new_clusters > old_clusters)
430 ret = ocfs2_xattr_extend_allocation(inode,
431 new_clusters - old_clusters,
434 ret = ocfs2_xattr_shrink_size(inode,
435 old_clusters, new_clusters,
441 static int ocfs2_xattr_list_entries(struct inode *inode,
442 struct ocfs2_xattr_header *header,
443 char *buffer, size_t buffer_size)
445 size_t rest = buffer_size;
448 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
449 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
450 struct xattr_handler *handler =
451 ocfs2_xattr_handler(ocfs2_xattr_get_type(entry));
454 size_t size = handler->list(inode, buffer, rest,
456 le16_to_cpu(entry->xe_name_offset)),
467 return buffer_size - rest;
470 static int ocfs2_xattr_ibody_list(struct inode *inode,
471 struct ocfs2_dinode *di,
475 struct ocfs2_xattr_header *header = NULL;
476 struct ocfs2_inode_info *oi = OCFS2_I(inode);
479 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
482 header = (struct ocfs2_xattr_header *)
483 ((void *)di + inode->i_sb->s_blocksize -
484 le16_to_cpu(di->i_xattr_inline_size));
486 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
491 static int ocfs2_xattr_block_list(struct inode *inode,
492 struct ocfs2_dinode *di,
496 struct buffer_head *blk_bh = NULL;
497 struct ocfs2_xattr_block *xb;
500 if (!di->i_xattr_loc)
503 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
504 le64_to_cpu(di->i_xattr_loc),
505 &blk_bh, OCFS2_BH_CACHED, inode);
510 /*Verify the signature of xattr block*/
511 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
512 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
517 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
519 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
520 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
521 ret = ocfs2_xattr_list_entries(inode, header,
522 buffer, buffer_size);
524 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
525 ret = ocfs2_xattr_tree_list_index_block(inode, xt,
526 buffer, buffer_size);
534 ssize_t ocfs2_listxattr(struct dentry *dentry,
538 int ret = 0, i_ret = 0, b_ret = 0;
539 struct buffer_head *di_bh = NULL;
540 struct ocfs2_dinode *di = NULL;
541 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
543 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
546 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
552 di = (struct ocfs2_dinode *)di_bh->b_data;
554 down_read(&oi->ip_xattr_sem);
555 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
563 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
568 up_read(&oi->ip_xattr_sem);
569 ocfs2_inode_unlock(dentry->d_inode, 0);
573 return i_ret + b_ret;
576 static int ocfs2_xattr_find_entry(int name_index,
578 struct ocfs2_xattr_search *xs)
580 struct ocfs2_xattr_entry *entry;
587 name_len = strlen(name);
589 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
590 cmp = name_index - ocfs2_xattr_get_type(entry);
592 cmp = name_len - entry->xe_name_len;
594 cmp = memcmp(name, (xs->base +
595 le16_to_cpu(entry->xe_name_offset)),
603 return cmp ? -ENODATA : 0;
606 static int ocfs2_xattr_get_value_outside(struct inode *inode,
607 struct ocfs2_xattr_search *xs,
611 u32 cpos, p_cluster, num_clusters, bpc, clusters;
614 size_t cplen, blocksize;
615 struct buffer_head *bh = NULL;
616 struct ocfs2_xattr_value_root *xv;
617 struct ocfs2_extent_list *el;
619 xv = (struct ocfs2_xattr_value_root *)
620 (xs->base + le16_to_cpu(xs->here->xe_name_offset) +
621 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
623 clusters = le32_to_cpu(xv->xr_clusters);
624 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
625 blocksize = inode->i_sb->s_blocksize;
628 while (cpos < clusters) {
629 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
636 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
637 /* Copy ocfs2_xattr_value */
638 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
639 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
640 &bh, OCFS2_BH_CACHED, inode);
646 cplen = len >= blocksize ? blocksize : len;
647 memcpy(buffer, bh->b_data, cplen);
656 cpos += num_clusters;
662 static int ocfs2_xattr_ibody_get(struct inode *inode,
667 struct ocfs2_xattr_search *xs)
669 struct ocfs2_inode_info *oi = OCFS2_I(inode);
670 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
674 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
677 xs->end = (void *)di + inode->i_sb->s_blocksize;
678 xs->header = (struct ocfs2_xattr_header *)
679 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
680 xs->base = (void *)xs->header;
681 xs->here = xs->header->xh_entries;
683 ret = ocfs2_xattr_find_entry(name_index, name, xs);
686 size = le64_to_cpu(xs->here->xe_value_size);
688 if (size > buffer_size)
690 if (ocfs2_xattr_is_local(xs->here)) {
691 memcpy(buffer, (void *)xs->base +
692 le16_to_cpu(xs->here->xe_name_offset) +
693 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
695 ret = ocfs2_xattr_get_value_outside(inode, xs,
707 static int ocfs2_xattr_block_get(struct inode *inode,
712 struct ocfs2_xattr_search *xs)
714 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
715 struct buffer_head *blk_bh = NULL;
716 struct ocfs2_xattr_block *xb;
720 if (!di->i_xattr_loc)
723 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
724 le64_to_cpu(di->i_xattr_loc),
725 &blk_bh, OCFS2_BH_CACHED, inode);
730 /*Verify the signature of xattr block*/
731 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
732 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
737 xs->xattr_bh = blk_bh;
738 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
739 xs->header = &xb->xb_attrs.xb_header;
740 xs->base = (void *)xs->header;
741 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
742 xs->here = xs->header->xh_entries;
744 ret = ocfs2_xattr_find_entry(name_index, name, xs);
747 size = le64_to_cpu(xs->here->xe_value_size);
750 if (size > buffer_size)
752 if (ocfs2_xattr_is_local(xs->here)) {
753 memcpy(buffer, (void *)xs->base +
754 le16_to_cpu(xs->here->xe_name_offset) +
755 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
757 ret = ocfs2_xattr_get_value_outside(inode, xs,
774 * Copy an extended attribute into the buffer provided.
775 * Buffer is NULL to compute the size of buffer required.
777 int ocfs2_xattr_get(struct inode *inode,
784 struct ocfs2_dinode *di = NULL;
785 struct buffer_head *di_bh = NULL;
786 struct ocfs2_inode_info *oi = OCFS2_I(inode);
787 struct ocfs2_xattr_search xis = {
788 .not_found = -ENODATA,
790 struct ocfs2_xattr_search xbs = {
791 .not_found = -ENODATA,
794 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
797 ret = ocfs2_inode_lock(inode, &di_bh, 0);
802 xis.inode_bh = xbs.inode_bh = di_bh;
803 di = (struct ocfs2_dinode *)di_bh->b_data;
805 down_read(&oi->ip_xattr_sem);
806 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
809 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
811 up_read(&oi->ip_xattr_sem);
812 ocfs2_inode_unlock(inode, 0);
819 static int __ocfs2_xattr_set_value_outside(struct inode *inode,
820 struct ocfs2_xattr_value_root *xv,
824 int ret = 0, i, cp_len, credits;
825 u16 blocksize = inode->i_sb->s_blocksize;
826 u32 p_cluster, num_clusters;
827 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
828 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
830 struct buffer_head *bh = NULL;
833 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
835 credits = clusters * bpc;
836 handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb), credits);
837 if (IS_ERR(handle)) {
838 ret = PTR_ERR(handle);
843 while (cpos < clusters) {
844 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
845 &num_clusters, &xv->xr_list);
851 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
853 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
854 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
855 &bh, OCFS2_BH_CACHED, inode);
861 ret = ocfs2_journal_access(handle,
864 OCFS2_JOURNAL_ACCESS_WRITE);
870 cp_len = value_len > blocksize ? blocksize : value_len;
871 memcpy(bh->b_data, value, cp_len);
874 if (cp_len < blocksize)
875 memset(bh->b_data + cp_len, 0,
878 ret = ocfs2_journal_dirty(handle, bh);
887 * XXX: do we need to empty all the following
888 * blocks in this cluster?
893 cpos += num_clusters;
896 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
903 static int ocfs2_xattr_cleanup(struct inode *inode,
904 struct ocfs2_xattr_info *xi,
905 struct ocfs2_xattr_search *xs,
908 handle_t *handle = NULL;
910 size_t name_len = strlen(xi->name);
911 void *val = xs->base + offs;
912 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
914 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
915 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
916 if (IS_ERR(handle)) {
917 ret = PTR_ERR(handle);
921 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
922 OCFS2_JOURNAL_ACCESS_WRITE);
927 /* Decrease xattr count */
928 le16_add_cpu(&xs->header->xh_count, -1);
929 /* Remove the xattr entry and tree root which has already be set*/
930 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
931 memset(val, 0, size);
933 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
937 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
942 static int ocfs2_xattr_update_entry(struct inode *inode,
943 struct ocfs2_xattr_info *xi,
944 struct ocfs2_xattr_search *xs,
947 handle_t *handle = NULL;
950 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
951 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
952 if (IS_ERR(handle)) {
953 ret = PTR_ERR(handle);
957 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
958 OCFS2_JOURNAL_ACCESS_WRITE);
964 xs->here->xe_name_offset = cpu_to_le16(offs);
965 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
966 if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
967 ocfs2_xattr_set_local(xs->here, 1);
969 ocfs2_xattr_set_local(xs->here, 0);
970 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
972 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
976 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
982 * ocfs2_xattr_set_value_outside()
984 * Set large size value in B tree.
986 static int ocfs2_xattr_set_value_outside(struct inode *inode,
987 struct ocfs2_xattr_info *xi,
988 struct ocfs2_xattr_search *xs,
991 size_t name_len = strlen(xi->name);
992 void *val = xs->base + offs;
993 struct ocfs2_xattr_value_root *xv = NULL;
994 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
997 memset(val, 0, size);
998 memcpy(val, xi->name, name_len);
999 xv = (struct ocfs2_xattr_value_root *)
1000 (val + OCFS2_XATTR_SIZE(name_len));
1001 xv->xr_clusters = 0;
1002 xv->xr_last_eb_blk = 0;
1003 xv->xr_list.l_tree_depth = 0;
1004 xv->xr_list.l_count = cpu_to_le16(1);
1005 xv->xr_list.l_next_free_rec = 0;
1007 ret = ocfs2_xattr_value_truncate(inode, xs->xattr_bh, xv,
1013 ret = __ocfs2_xattr_set_value_outside(inode, xv, xi->value,
1019 ret = ocfs2_xattr_update_entry(inode, xi, xs, offs);
1027 * ocfs2_xattr_set_entry_local()
1029 * Set, replace or remove extended attribute in local.
1031 static void ocfs2_xattr_set_entry_local(struct inode *inode,
1032 struct ocfs2_xattr_info *xi,
1033 struct ocfs2_xattr_search *xs,
1034 struct ocfs2_xattr_entry *last,
1037 size_t name_len = strlen(xi->name);
1040 if (xi->value && xs->not_found) {
1041 /* Insert the new xattr entry. */
1042 le16_add_cpu(&xs->header->xh_count, 1);
1043 ocfs2_xattr_set_type(last, xi->name_index);
1044 ocfs2_xattr_set_local(last, 1);
1045 last->xe_name_len = name_len;
1051 first_val = xs->base + min_offs;
1052 offs = le16_to_cpu(xs->here->xe_name_offset);
1053 val = xs->base + offs;
1055 if (le64_to_cpu(xs->here->xe_value_size) >
1056 OCFS2_XATTR_INLINE_SIZE)
1057 size = OCFS2_XATTR_SIZE(name_len) +
1058 OCFS2_XATTR_ROOT_SIZE;
1060 size = OCFS2_XATTR_SIZE(name_len) +
1061 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1063 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1064 OCFS2_XATTR_SIZE(xi->value_len)) {
1065 /* The old and the new value have the
1066 same size. Just replace the value. */
1067 ocfs2_xattr_set_local(xs->here, 1);
1068 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1069 /* Clear value bytes. */
1070 memset(val + OCFS2_XATTR_SIZE(name_len),
1072 OCFS2_XATTR_SIZE(xi->value_len));
1073 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1078 /* Remove the old name+value. */
1079 memmove(first_val + size, first_val, val - first_val);
1080 memset(first_val, 0, size);
1081 xs->here->xe_name_hash = 0;
1082 xs->here->xe_name_offset = 0;
1083 ocfs2_xattr_set_local(xs->here, 1);
1084 xs->here->xe_value_size = 0;
1088 /* Adjust all value offsets. */
1089 last = xs->header->xh_entries;
1090 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1091 size_t o = le16_to_cpu(last->xe_name_offset);
1094 last->xe_name_offset = cpu_to_le16(o + size);
1099 /* Remove the old entry. */
1101 memmove(xs->here, xs->here + 1,
1102 (void *)last - (void *)xs->here);
1103 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1104 le16_add_cpu(&xs->header->xh_count, -1);
1108 /* Insert the new name+value. */
1109 size_t size = OCFS2_XATTR_SIZE(name_len) +
1110 OCFS2_XATTR_SIZE(xi->value_len);
1111 void *val = xs->base + min_offs - size;
1113 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1114 memset(val, 0, size);
1115 memcpy(val, xi->name, name_len);
1116 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1119 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1120 ocfs2_xattr_set_local(xs->here, 1);
1121 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1128 * ocfs2_xattr_set_entry()
1130 * Set extended attribute entry into inode or block.
1132 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1133 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1134 * then set value in B tree with set_value_outside().
1136 static int ocfs2_xattr_set_entry(struct inode *inode,
1137 struct ocfs2_xattr_info *xi,
1138 struct ocfs2_xattr_search *xs,
1141 struct ocfs2_xattr_entry *last;
1142 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1143 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1144 size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1146 handle_t *handle = NULL;
1148 struct ocfs2_xattr_info xi_l = {
1149 .name_index = xi->name_index,
1152 .value_len = xi->value_len,
1155 /* Compute min_offs, last and free space. */
1156 last = xs->header->xh_entries;
1158 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1159 size_t offs = le16_to_cpu(last->xe_name_offset);
1160 if (offs < min_offs)
1165 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
1169 if (!xs->not_found) {
1171 if (ocfs2_xattr_is_local(xs->here))
1172 size = OCFS2_XATTR_SIZE(name_len) +
1173 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1175 size = OCFS2_XATTR_SIZE(name_len) +
1176 OCFS2_XATTR_ROOT_SIZE;
1177 free += (size + sizeof(struct ocfs2_xattr_entry));
1179 /* Check free space in inode or block */
1180 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1181 if (free < sizeof(struct ocfs2_xattr_entry) +
1182 OCFS2_XATTR_SIZE(name_len) +
1183 OCFS2_XATTR_ROOT_SIZE) {
1187 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1188 xi_l.value = (void *)&def_xv;
1189 xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
1190 } else if (xi->value) {
1191 if (free < sizeof(struct ocfs2_xattr_entry) +
1192 OCFS2_XATTR_SIZE(name_len) +
1193 OCFS2_XATTR_SIZE(xi->value_len)) {
1199 if (!xs->not_found) {
1200 /* For existing extended attribute */
1201 size_t size = OCFS2_XATTR_SIZE(name_len) +
1202 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1203 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1204 void *val = xs->base + offs;
1206 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1207 /* Replace existing local xattr with tree root */
1208 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
1213 } else if (!ocfs2_xattr_is_local(xs->here)) {
1214 /* For existing xattr which has value outside */
1215 struct ocfs2_xattr_value_root *xv = NULL;
1216 xv = (struct ocfs2_xattr_value_root *)(val +
1217 OCFS2_XATTR_SIZE(name_len));
1219 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1221 * If new value need set outside also,
1222 * first truncate old value to new value,
1223 * then set new value with set_value_outside().
1225 ret = ocfs2_xattr_value_truncate(inode,
1234 ret = __ocfs2_xattr_set_value_outside(inode,
1243 ret = ocfs2_xattr_update_entry(inode,
1252 * If new value need set in local,
1253 * just trucate old value to zero.
1255 ret = ocfs2_xattr_value_truncate(inode,
1265 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1266 OCFS2_INODE_UPDATE_CREDITS);
1267 if (IS_ERR(handle)) {
1268 ret = PTR_ERR(handle);
1273 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1274 OCFS2_JOURNAL_ACCESS_WRITE);
1280 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1281 /*set extended attribue in external blcok*/
1282 ret = ocfs2_extend_trans(handle,
1283 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1288 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1289 OCFS2_JOURNAL_ACCESS_WRITE);
1297 * Set value in local, include set tree root in local.
1298 * This is the first step for value size >INLINE_SIZE.
1300 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1302 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1303 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1310 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1311 (flag & OCFS2_INLINE_XATTR_FL)) {
1312 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1313 unsigned int xattrsize = osb->s_xattr_inline_size;
1316 * Adjust extent record count or inline data size
1317 * to reserve space for extended attribute.
1319 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1320 struct ocfs2_inline_data *idata = &di->id2.i_data;
1321 le16_add_cpu(&idata->id_count, -xattrsize);
1322 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1323 struct ocfs2_extent_list *el = &di->id2.i_list;
1324 le16_add_cpu(&el->l_count, -(xattrsize /
1325 sizeof(struct ocfs2_extent_rec)));
1327 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1329 /* Update xattr flag */
1330 spin_lock(&oi->ip_lock);
1331 oi->ip_dyn_features |= flag;
1332 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1333 spin_unlock(&oi->ip_lock);
1334 /* Update inode ctime */
1335 inode->i_ctime = CURRENT_TIME;
1336 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
1337 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
1339 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1344 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1346 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1348 * Set value outside in B tree.
1349 * This is the second step for value size > INLINE_SIZE.
1351 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1352 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, offs);
1358 * If set value outside failed, we have to clean
1359 * the junk tree root we have already set in local.
1361 ret2 = ocfs2_xattr_cleanup(inode, xi, xs, offs);
1371 static int ocfs2_xattr_free_block(handle_t *handle,
1372 struct ocfs2_super *osb,
1373 struct ocfs2_xattr_block *xb)
1375 struct inode *xb_alloc_inode;
1376 struct buffer_head *xb_alloc_bh = NULL;
1377 u64 blk = le64_to_cpu(xb->xb_blkno);
1378 u16 bit = le16_to_cpu(xb->xb_suballoc_bit);
1379 u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1382 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1383 EXTENT_ALLOC_SYSTEM_INODE,
1384 le16_to_cpu(xb->xb_suballoc_slot));
1385 if (!xb_alloc_inode) {
1390 mutex_lock(&xb_alloc_inode->i_mutex);
1392 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1397 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
1402 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1407 ocfs2_inode_unlock(xb_alloc_inode, 1);
1408 brelse(xb_alloc_bh);
1410 mutex_unlock(&xb_alloc_inode->i_mutex);
1411 iput(xb_alloc_inode);
1416 static int ocfs2_remove_value_outside(struct inode*inode,
1417 struct buffer_head *bh,
1418 struct ocfs2_xattr_header *header)
1422 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1423 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1425 if (!ocfs2_xattr_is_local(entry)) {
1426 struct ocfs2_xattr_value_root *xv;
1429 val = (void *)header +
1430 le16_to_cpu(entry->xe_name_offset);
1431 xv = (struct ocfs2_xattr_value_root *)
1432 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
1433 ret = ocfs2_xattr_value_truncate(inode, bh, xv, 0);
1444 static int ocfs2_xattr_ibody_remove(struct inode *inode,
1445 struct buffer_head *di_bh)
1448 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1449 struct ocfs2_xattr_header *header;
1452 header = (struct ocfs2_xattr_header *)
1453 ((void *)di + inode->i_sb->s_blocksize -
1454 le16_to_cpu(di->i_xattr_inline_size));
1456 ret = ocfs2_remove_value_outside(inode, di_bh, header);
1461 static int ocfs2_xattr_block_remove(struct inode *inode,
1462 struct buffer_head *blk_bh)
1464 struct ocfs2_xattr_block *xb;
1465 struct ocfs2_xattr_header *header;
1468 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1469 header = &(xb->xb_attrs.xb_header);
1471 ret = ocfs2_remove_value_outside(inode, blk_bh, header);
1477 * ocfs2_xattr_remove()
1479 * Free extended attribute resources associated with this inode.
1481 int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
1483 struct ocfs2_xattr_block *xb;
1484 struct buffer_head *blk_bh = NULL;
1485 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1486 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1487 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1491 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1494 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1495 ret = ocfs2_xattr_ibody_remove(inode, di_bh);
1501 if (di->i_xattr_loc) {
1502 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1503 le64_to_cpu(di->i_xattr_loc),
1504 &blk_bh, OCFS2_BH_CACHED, inode);
1509 /*Verify the signature of xattr block*/
1510 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
1511 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
1516 ret = ocfs2_xattr_block_remove(inode, blk_bh);
1523 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1524 OCFS2_INODE_UPDATE_CREDITS);
1525 if (IS_ERR(handle)) {
1526 ret = PTR_ERR(handle);
1530 ret = ocfs2_journal_access(handle, inode, di_bh,
1531 OCFS2_JOURNAL_ACCESS_WRITE);
1537 if (di->i_xattr_loc) {
1538 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1539 ocfs2_xattr_free_block(handle, osb, xb);
1540 di->i_xattr_loc = cpu_to_le64(0);
1543 spin_lock(&oi->ip_lock);
1544 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
1545 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1546 spin_unlock(&oi->ip_lock);
1548 ret = ocfs2_journal_dirty(handle, di_bh);
1552 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1559 static int ocfs2_xattr_has_space_inline(struct inode *inode,
1560 struct ocfs2_dinode *di)
1562 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1563 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
1566 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
1569 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1570 struct ocfs2_inline_data *idata = &di->id2.i_data;
1571 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
1572 } else if (ocfs2_inode_is_fast_symlink(inode)) {
1573 free = ocfs2_fast_symlink_chars(inode->i_sb) -
1574 le64_to_cpu(di->i_size);
1576 struct ocfs2_extent_list *el = &di->id2.i_list;
1577 free = (le16_to_cpu(el->l_count) -
1578 le16_to_cpu(el->l_next_free_rec)) *
1579 sizeof(struct ocfs2_extent_rec);
1581 if (free >= xattrsize)
1588 * ocfs2_xattr_ibody_find()
1590 * Find extended attribute in inode block and
1591 * fill search info into struct ocfs2_xattr_search.
1593 static int ocfs2_xattr_ibody_find(struct inode *inode,
1596 struct ocfs2_xattr_search *xs)
1598 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1599 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1603 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1606 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1607 down_read(&oi->ip_alloc_sem);
1608 has_space = ocfs2_xattr_has_space_inline(inode, di);
1609 up_read(&oi->ip_alloc_sem);
1614 xs->xattr_bh = xs->inode_bh;
1615 xs->end = (void *)di + inode->i_sb->s_blocksize;
1616 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
1617 xs->header = (struct ocfs2_xattr_header *)
1618 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1620 xs->header = (struct ocfs2_xattr_header *)
1621 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
1622 xs->base = (void *)xs->header;
1623 xs->here = xs->header->xh_entries;
1625 /* Find the named attribute. */
1626 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1627 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1628 if (ret && ret != -ENODATA)
1630 xs->not_found = ret;
1637 * ocfs2_xattr_ibody_set()
1639 * Set, replace or remove an extended attribute into inode block.
1642 static int ocfs2_xattr_ibody_set(struct inode *inode,
1643 struct ocfs2_xattr_info *xi,
1644 struct ocfs2_xattr_search *xs)
1646 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1647 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1650 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1653 down_write(&oi->ip_alloc_sem);
1654 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1655 if (!ocfs2_xattr_has_space_inline(inode, di)) {
1661 ret = ocfs2_xattr_set_entry(inode, xi, xs,
1662 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
1664 up_write(&oi->ip_alloc_sem);
1670 * ocfs2_xattr_block_find()
1672 * Find extended attribute in external block and
1673 * fill search info into struct ocfs2_xattr_search.
1675 static int ocfs2_xattr_block_find(struct inode *inode,
1678 struct ocfs2_xattr_search *xs)
1680 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1681 struct buffer_head *blk_bh = NULL;
1684 if (!di->i_xattr_loc)
1687 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1688 le64_to_cpu(di->i_xattr_loc),
1689 &blk_bh, OCFS2_BH_CACHED, inode);
1694 /*Verify the signature of xattr block*/
1695 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
1696 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
1701 xs->xattr_bh = blk_bh;
1702 xs->header = &((struct ocfs2_xattr_block *)blk_bh->b_data)->
1704 xs->base = (void *)xs->header;
1705 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
1706 xs->here = xs->header->xh_entries;
1708 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1709 if (ret && ret != -ENODATA) {
1710 xs->xattr_bh = NULL;
1713 xs->not_found = ret;
1723 * ocfs2_xattr_block_set()
1725 * Set, replace or remove an extended attribute into external block.
1728 static int ocfs2_xattr_block_set(struct inode *inode,
1729 struct ocfs2_xattr_info *xi,
1730 struct ocfs2_xattr_search *xs)
1732 struct buffer_head *new_bh = NULL;
1733 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1734 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1735 struct ocfs2_alloc_context *meta_ac = NULL;
1736 handle_t *handle = NULL;
1737 struct ocfs2_xattr_block *xblk = NULL;
1738 u16 suballoc_bit_start;
1743 if (!xs->xattr_bh) {
1745 * Alloc one external block for extended attribute
1748 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
1753 handle = ocfs2_start_trans(osb,
1754 OCFS2_XATTR_BLOCK_CREATE_CREDITS);
1755 if (IS_ERR(handle)) {
1756 ret = PTR_ERR(handle);
1760 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1761 OCFS2_JOURNAL_ACCESS_CREATE);
1767 ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
1768 &suballoc_bit_start, &num_got,
1775 new_bh = sb_getblk(inode->i_sb, first_blkno);
1776 ocfs2_set_new_buffer_uptodate(inode, new_bh);
1778 ret = ocfs2_journal_access(handle, inode, new_bh,
1779 OCFS2_JOURNAL_ACCESS_CREATE);
1785 /* Initialize ocfs2_xattr_block */
1786 xs->xattr_bh = new_bh;
1787 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
1788 memset(xblk, 0, inode->i_sb->s_blocksize);
1789 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
1790 xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
1791 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1792 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
1793 xblk->xb_blkno = cpu_to_le64(first_blkno);
1795 xs->header = &xblk->xb_attrs.xb_header;
1796 xs->base = (void *)xs->header;
1797 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
1798 xs->here = xs->header->xh_entries;
1801 ret = ocfs2_journal_dirty(handle, new_bh);
1806 di->i_xattr_loc = cpu_to_le64(first_blkno);
1807 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1811 ocfs2_commit_trans(osb, handle);
1814 ocfs2_free_alloc_context(meta_ac);
1819 /* Set extended attribute into external block */
1820 ret = ocfs2_xattr_set_entry(inode, xi, xs, OCFS2_HAS_XATTR_FL);
1828 * Set, replace or remove an extended attribute for this inode.
1829 * value is NULL to remove an existing extended attribute, else either
1830 * create or replace an extended attribute.
1832 int ocfs2_xattr_set(struct inode *inode,
1839 struct buffer_head *di_bh = NULL;
1840 struct ocfs2_dinode *di;
1843 struct ocfs2_xattr_info xi = {
1844 .name_index = name_index,
1847 .value_len = value_len,
1850 struct ocfs2_xattr_search xis = {
1851 .not_found = -ENODATA,
1854 struct ocfs2_xattr_search xbs = {
1855 .not_found = -ENODATA,
1858 ret = ocfs2_inode_lock(inode, &di_bh, 1);
1863 xis.inode_bh = xbs.inode_bh = di_bh;
1864 di = (struct ocfs2_dinode *)di_bh->b_data;
1866 down_write(&OCFS2_I(inode)->ip_xattr_sem);
1868 * Scan inode and external block to find the same name
1869 * extended attribute and collect search infomation.
1871 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
1874 if (xis.not_found) {
1875 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
1880 if (xis.not_found && xbs.not_found) {
1882 if (flags & XATTR_REPLACE)
1889 if (flags & XATTR_CREATE)
1894 /* Remove existing extended attribute */
1896 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
1897 else if (!xbs.not_found)
1898 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
1900 /* We always try to set extended attribute into inode first*/
1901 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
1902 if (!ret && !xbs.not_found) {
1904 * If succeed and that extended attribute existing in
1905 * external block, then we will remove it.
1909 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
1910 } else if (ret == -ENOSPC) {
1911 if (di->i_xattr_loc && !xbs.xattr_bh) {
1912 ret = ocfs2_xattr_block_find(inode, name_index,
1918 * If no space in inode, we will set extended attribute
1919 * into external block.
1921 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
1924 if (!xis.not_found) {
1926 * If succeed and that extended attribute
1927 * existing in inode, we will remove it.
1931 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
1936 up_write(&OCFS2_I(inode)->ip_xattr_sem);
1937 ocfs2_inode_unlock(inode, 1);
1939 brelse(xbs.xattr_bh);
1945 * Find the xattr extent rec which may contains name_hash.
1946 * e_cpos will be the first name hash of the xattr rec.
1947 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
1949 static int ocfs2_xattr_get_rec(struct inode *inode,
1954 struct ocfs2_extent_list *el)
1957 struct buffer_head *eb_bh = NULL;
1958 struct ocfs2_extent_block *eb;
1959 struct ocfs2_extent_rec *rec = NULL;
1962 if (el->l_tree_depth) {
1963 ret = ocfs2_find_leaf(inode, el, name_hash, &eb_bh);
1969 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
1972 if (el->l_tree_depth) {
1973 ocfs2_error(inode->i_sb,
1974 "Inode %lu has non zero tree depth in "
1975 "xattr tree block %llu\n", inode->i_ino,
1976 (unsigned long long)eb_bh->b_blocknr);
1982 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
1983 rec = &el->l_recs[i];
1985 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
1986 e_blkno = le64_to_cpu(rec->e_blkno);
1992 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
1993 "record (%u, %u, 0) in xattr", inode->i_ino,
1994 le32_to_cpu(rec->e_cpos),
1995 ocfs2_rec_clusters(el, rec));
2000 *p_blkno = le64_to_cpu(rec->e_blkno);
2001 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
2003 *e_cpos = le32_to_cpu(rec->e_cpos);
2009 typedef int (xattr_bucket_func)(struct inode *inode,
2010 struct ocfs2_xattr_bucket *bucket,
2013 static int ocfs2_iterate_xattr_buckets(struct inode *inode,
2016 xattr_bucket_func *func,
2020 int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2021 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
2022 u32 num_buckets = clusters * bpc;
2023 struct ocfs2_xattr_bucket bucket;
2025 memset(&bucket, 0, sizeof(bucket));
2027 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
2030 for (i = 0; i < num_buckets; i++, blkno += blk_per_bucket) {
2031 ret = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
2032 blkno, blk_per_bucket,
2033 bucket.bhs, OCFS2_BH_CACHED, inode);
2039 bucket.xh = (struct ocfs2_xattr_header *)bucket.bhs[0]->b_data;
2041 * The real bucket num in this series of blocks is stored
2042 * in the 1st bucket.
2045 num_buckets = le16_to_cpu(bucket.xh->xh_num_buckets);
2047 mlog(0, "iterating xattr bucket %llu\n", blkno);
2049 ret = func(inode, &bucket, para);
2056 for (j = 0; j < blk_per_bucket; j++)
2057 brelse(bucket.bhs[j]);
2058 memset(&bucket, 0, sizeof(bucket));
2062 for (j = 0; j < blk_per_bucket; j++)
2063 brelse(bucket.bhs[j]);
2068 struct ocfs2_xattr_tree_list {
2073 static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
2074 struct ocfs2_xattr_header *xh,
2081 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
2084 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
2086 *block_off = name_offset >> inode->i_sb->s_blocksize_bits;
2087 *new_offset = name_offset % inode->i_sb->s_blocksize;
2092 static int ocfs2_list_xattr_bucket(struct inode *inode,
2093 struct ocfs2_xattr_bucket *bucket,
2097 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
2099 int i, block_off, new_offset;
2101 for (i = 0 ; i < le16_to_cpu(bucket->xh->xh_count); i++) {
2102 struct ocfs2_xattr_entry *entry = &bucket->xh->xh_entries[i];
2103 struct xattr_handler *handler =
2104 ocfs2_xattr_handler(ocfs2_xattr_get_type(entry));
2107 ret = ocfs2_xattr_bucket_get_name_value(inode,
2114 size = handler->list(inode, xl->buffer, xl->buffer_size,
2115 bucket->bhs[block_off]->b_data +
2117 entry->xe_name_len);
2119 if (size > xl->buffer_size)
2123 xl->buffer_size -= size;
2130 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
2131 struct ocfs2_xattr_tree_root *xt,
2135 struct ocfs2_extent_list *el = &xt->xt_list;
2137 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
2139 struct ocfs2_xattr_tree_list xl = {
2141 .buffer_size = buffer_size,
2144 if (le16_to_cpu(el->l_next_free_rec) == 0)
2147 while (name_hash > 0) {
2148 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
2149 &e_cpos, &num_clusters, el);
2155 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
2156 ocfs2_list_xattr_bucket,
2166 name_hash = e_cpos - 1;
2169 ret = buffer_size - xl.buffer_size;