1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Copyright (C) 2004, 2008 Oracle. All rights reserved.
9 * Lots of code in this file is copy from linux/fs/ext3/xattr.c.
10 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public
14 * License version 2 as published by the Free Software Foundation.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
22 #include <linux/capability.h>
24 #include <linux/types.h>
25 #include <linux/slab.h>
26 #include <linux/highmem.h>
27 #include <linux/pagemap.h>
28 #include <linux/uio.h>
29 #include <linux/sched.h>
30 #include <linux/splice.h>
31 #include <linux/mount.h>
32 #include <linux/writeback.h>
33 #include <linux/falloc.h>
34 #include <linux/sort.h>
35 #include <linux/init.h>
36 #include <linux/module.h>
37 #include <linux/string.h>
39 #define MLOG_MASK_PREFIX ML_XATTR
40 #include <cluster/masklog.h>
53 #include "buffer_head_io.h"
58 struct ocfs2_xattr_def_value_root {
59 struct ocfs2_xattr_value_root xv;
60 struct ocfs2_extent_rec er;
63 struct ocfs2_xattr_bucket {
64 struct buffer_head *bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
65 struct ocfs2_xattr_header *xh;
68 #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
69 #define OCFS2_XATTR_INLINE_SIZE 80
71 static struct ocfs2_xattr_def_value_root def_xv = {
72 .xv.xr_list.l_count = cpu_to_le16(1),
75 struct xattr_handler *ocfs2_xattr_handlers[] = {
76 &ocfs2_xattr_user_handler,
77 &ocfs2_xattr_trusted_handler,
81 static struct xattr_handler *ocfs2_xattr_handler_map[] = {
82 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
83 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
86 struct ocfs2_xattr_info {
93 struct ocfs2_xattr_search {
94 struct buffer_head *inode_bh;
96 * xattr_bh point to the block buffer head which has extended attribute
97 * when extended attribute in inode, xattr_bh is equal to inode_bh.
99 struct buffer_head *xattr_bh;
100 struct ocfs2_xattr_header *header;
101 struct ocfs2_xattr_bucket bucket;
104 struct ocfs2_xattr_entry *here;
108 static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
109 struct ocfs2_xattr_header *xh,
114 static int ocfs2_xattr_index_block_find(struct inode *inode,
115 struct buffer_head *root_bh,
118 struct ocfs2_xattr_search *xs);
120 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
121 struct ocfs2_xattr_tree_root *xt,
125 static int ocfs2_xattr_create_index_block(struct inode *inode,
126 struct ocfs2_xattr_search *xs);
128 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
129 struct ocfs2_xattr_info *xi,
130 struct ocfs2_xattr_search *xs);
132 static int ocfs2_delete_xattr_index_block(struct inode *inode,
133 struct buffer_head *xb_bh);
135 static inline const char *ocfs2_xattr_prefix(int name_index)
137 struct xattr_handler *handler = NULL;
139 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
140 handler = ocfs2_xattr_handler_map[name_index];
142 return handler ? handler->prefix : NULL;
145 static u32 ocfs2_xattr_name_hash(struct inode *inode,
149 /* Get hash value of uuid from super block */
150 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
153 /* hash extended attribute name */
154 for (i = 0; i < name_len; i++) {
155 hash = (hash << OCFS2_HASH_SHIFT) ^
156 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
164 * ocfs2_xattr_hash_entry()
166 * Compute the hash of an extended attribute.
168 static void ocfs2_xattr_hash_entry(struct inode *inode,
169 struct ocfs2_xattr_header *header,
170 struct ocfs2_xattr_entry *entry)
173 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
175 hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
176 entry->xe_name_hash = cpu_to_le32(hash);
181 static int ocfs2_xattr_extend_allocation(struct inode *inode,
183 struct buffer_head *xattr_bh,
184 struct ocfs2_xattr_value_root *xv)
187 int restart_func = 0;
189 handle_t *handle = NULL;
190 struct ocfs2_alloc_context *data_ac = NULL;
191 struct ocfs2_alloc_context *meta_ac = NULL;
192 enum ocfs2_alloc_restarted why;
193 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
194 u32 prev_clusters, logical_start = le32_to_cpu(xv->xr_clusters);
195 struct ocfs2_extent_tree et;
197 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
199 ocfs2_init_xattr_value_extent_tree(&et, inode, xattr_bh, xv);
203 status = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
210 credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
212 handle = ocfs2_start_trans(osb, credits);
213 if (IS_ERR(handle)) {
214 status = PTR_ERR(handle);
220 restarted_transaction:
221 status = ocfs2_journal_access(handle, inode, xattr_bh,
222 OCFS2_JOURNAL_ACCESS_WRITE);
228 prev_clusters = le32_to_cpu(xv->xr_clusters);
229 status = ocfs2_add_clusters_in_btree(osb,
239 if ((status < 0) && (status != -EAGAIN)) {
240 if (status != -ENOSPC)
245 status = ocfs2_journal_dirty(handle, xattr_bh);
251 clusters_to_add -= le32_to_cpu(xv->xr_clusters) - prev_clusters;
253 if (why != RESTART_NONE && clusters_to_add) {
254 if (why == RESTART_META) {
255 mlog(0, "restarting function.\n");
258 BUG_ON(why != RESTART_TRANS);
260 mlog(0, "restarting transaction.\n");
261 /* TODO: This can be more intelligent. */
262 credits = ocfs2_calc_extend_credits(osb->sb,
265 status = ocfs2_extend_trans(handle, credits);
267 /* handle still has to be committed at
273 goto restarted_transaction;
279 ocfs2_commit_trans(osb, handle);
283 ocfs2_free_alloc_context(data_ac);
287 ocfs2_free_alloc_context(meta_ac);
290 if ((!status) && restart_func) {
298 static int __ocfs2_remove_xattr_range(struct inode *inode,
299 struct buffer_head *root_bh,
300 struct ocfs2_xattr_value_root *xv,
301 u32 cpos, u32 phys_cpos, u32 len,
302 struct ocfs2_cached_dealloc_ctxt *dealloc)
305 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
306 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
307 struct inode *tl_inode = osb->osb_tl_inode;
309 struct ocfs2_alloc_context *meta_ac = NULL;
310 struct ocfs2_extent_tree et;
312 ocfs2_init_xattr_value_extent_tree(&et, inode, root_bh, xv);
314 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
320 mutex_lock(&tl_inode->i_mutex);
322 if (ocfs2_truncate_log_needs_flush(osb)) {
323 ret = __ocfs2_flush_truncate_log(osb);
330 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
331 if (IS_ERR(handle)) {
332 ret = PTR_ERR(handle);
337 ret = ocfs2_journal_access(handle, inode, root_bh,
338 OCFS2_JOURNAL_ACCESS_WRITE);
344 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
351 le32_add_cpu(&xv->xr_clusters, -len);
353 ret = ocfs2_journal_dirty(handle, root_bh);
359 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
364 ocfs2_commit_trans(osb, handle);
366 mutex_unlock(&tl_inode->i_mutex);
369 ocfs2_free_alloc_context(meta_ac);
374 static int ocfs2_xattr_shrink_size(struct inode *inode,
377 struct buffer_head *root_bh,
378 struct ocfs2_xattr_value_root *xv)
381 u32 trunc_len, cpos, phys_cpos, alloc_size;
383 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
384 struct ocfs2_cached_dealloc_ctxt dealloc;
386 ocfs2_init_dealloc_ctxt(&dealloc);
388 if (old_clusters <= new_clusters)
392 trunc_len = old_clusters - new_clusters;
394 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
395 &alloc_size, &xv->xr_list);
401 if (alloc_size > trunc_len)
402 alloc_size = trunc_len;
404 ret = __ocfs2_remove_xattr_range(inode, root_bh, xv, cpos,
405 phys_cpos, alloc_size,
412 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
413 ocfs2_remove_xattr_clusters_from_cache(inode, block,
416 trunc_len -= alloc_size;
420 ocfs2_schedule_truncate_log_flush(osb, 1);
421 ocfs2_run_deallocs(osb, &dealloc);
426 static int ocfs2_xattr_value_truncate(struct inode *inode,
427 struct buffer_head *root_bh,
428 struct ocfs2_xattr_value_root *xv,
432 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
433 u32 old_clusters = le32_to_cpu(xv->xr_clusters);
435 if (new_clusters == old_clusters)
438 if (new_clusters > old_clusters)
439 ret = ocfs2_xattr_extend_allocation(inode,
440 new_clusters - old_clusters,
443 ret = ocfs2_xattr_shrink_size(inode,
444 old_clusters, new_clusters,
450 static int ocfs2_xattr_list_entry(char *buffer, size_t size,
451 size_t *result, const char *prefix,
452 const char *name, int name_len)
454 char *p = buffer + *result;
455 int prefix_len = strlen(prefix);
456 int total_len = prefix_len + name_len + 1;
458 *result += total_len;
460 /* we are just looking for how big our buffer needs to be */
467 memcpy(p, prefix, prefix_len);
468 memcpy(p + prefix_len, name, name_len);
469 p[prefix_len + name_len] = '\0';
474 static int ocfs2_xattr_list_entries(struct inode *inode,
475 struct ocfs2_xattr_header *header,
476 char *buffer, size_t buffer_size)
480 const char *prefix, *name;
482 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
483 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
484 type = ocfs2_xattr_get_type(entry);
485 prefix = ocfs2_xattr_prefix(type);
488 name = (const char *)header +
489 le16_to_cpu(entry->xe_name_offset);
491 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
492 &result, prefix, name,
502 static int ocfs2_xattr_ibody_list(struct inode *inode,
503 struct ocfs2_dinode *di,
507 struct ocfs2_xattr_header *header = NULL;
508 struct ocfs2_inode_info *oi = OCFS2_I(inode);
511 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
514 header = (struct ocfs2_xattr_header *)
515 ((void *)di + inode->i_sb->s_blocksize -
516 le16_to_cpu(di->i_xattr_inline_size));
518 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
523 static int ocfs2_xattr_block_list(struct inode *inode,
524 struct ocfs2_dinode *di,
528 struct buffer_head *blk_bh = NULL;
529 struct ocfs2_xattr_block *xb;
532 if (!di->i_xattr_loc)
535 ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh);
540 /*Verify the signature of xattr block*/
541 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
542 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
547 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
549 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
550 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
551 ret = ocfs2_xattr_list_entries(inode, header,
552 buffer, buffer_size);
554 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
555 ret = ocfs2_xattr_tree_list_index_block(inode, xt,
556 buffer, buffer_size);
564 ssize_t ocfs2_listxattr(struct dentry *dentry,
568 int ret = 0, i_ret = 0, b_ret = 0;
569 struct buffer_head *di_bh = NULL;
570 struct ocfs2_dinode *di = NULL;
571 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
573 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
576 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
579 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
585 di = (struct ocfs2_dinode *)di_bh->b_data;
587 down_read(&oi->ip_xattr_sem);
588 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
596 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
601 up_read(&oi->ip_xattr_sem);
602 ocfs2_inode_unlock(dentry->d_inode, 0);
606 return i_ret + b_ret;
609 static int ocfs2_xattr_find_entry(int name_index,
611 struct ocfs2_xattr_search *xs)
613 struct ocfs2_xattr_entry *entry;
620 name_len = strlen(name);
622 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
623 cmp = name_index - ocfs2_xattr_get_type(entry);
625 cmp = name_len - entry->xe_name_len;
627 cmp = memcmp(name, (xs->base +
628 le16_to_cpu(entry->xe_name_offset)),
636 return cmp ? -ENODATA : 0;
639 static int ocfs2_xattr_get_value_outside(struct inode *inode,
640 struct ocfs2_xattr_value_root *xv,
644 u32 cpos, p_cluster, num_clusters, bpc, clusters;
647 size_t cplen, blocksize;
648 struct buffer_head *bh = NULL;
649 struct ocfs2_extent_list *el;
652 clusters = le32_to_cpu(xv->xr_clusters);
653 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
654 blocksize = inode->i_sb->s_blocksize;
657 while (cpos < clusters) {
658 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
665 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
666 /* Copy ocfs2_xattr_value */
667 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
668 ret = ocfs2_read_block(inode, blkno, &bh);
674 cplen = len >= blocksize ? blocksize : len;
675 memcpy(buffer, bh->b_data, cplen);
684 cpos += num_clusters;
690 static int ocfs2_xattr_ibody_get(struct inode *inode,
695 struct ocfs2_xattr_search *xs)
697 struct ocfs2_inode_info *oi = OCFS2_I(inode);
698 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
699 struct ocfs2_xattr_value_root *xv;
703 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
706 xs->end = (void *)di + inode->i_sb->s_blocksize;
707 xs->header = (struct ocfs2_xattr_header *)
708 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
709 xs->base = (void *)xs->header;
710 xs->here = xs->header->xh_entries;
712 ret = ocfs2_xattr_find_entry(name_index, name, xs);
715 size = le64_to_cpu(xs->here->xe_value_size);
717 if (size > buffer_size)
719 if (ocfs2_xattr_is_local(xs->here)) {
720 memcpy(buffer, (void *)xs->base +
721 le16_to_cpu(xs->here->xe_name_offset) +
722 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
724 xv = (struct ocfs2_xattr_value_root *)
725 (xs->base + le16_to_cpu(
726 xs->here->xe_name_offset) +
727 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
728 ret = ocfs2_xattr_get_value_outside(inode, xv,
740 static int ocfs2_xattr_block_get(struct inode *inode,
745 struct ocfs2_xattr_search *xs)
747 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
748 struct buffer_head *blk_bh = NULL;
749 struct ocfs2_xattr_block *xb;
750 struct ocfs2_xattr_value_root *xv;
752 int ret = -ENODATA, name_offset, name_len, block_off, i;
754 if (!di->i_xattr_loc)
757 memset(&xs->bucket, 0, sizeof(xs->bucket));
759 ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh);
764 /*Verify the signature of xattr block*/
765 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
766 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
771 xs->xattr_bh = blk_bh;
772 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
774 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
775 xs->header = &xb->xb_attrs.xb_header;
776 xs->base = (void *)xs->header;
777 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
778 xs->here = xs->header->xh_entries;
780 ret = ocfs2_xattr_find_entry(name_index, name, xs);
782 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
788 size = le64_to_cpu(xs->here->xe_value_size);
791 if (size > buffer_size)
794 name_offset = le16_to_cpu(xs->here->xe_name_offset);
795 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
796 i = xs->here - xs->header->xh_entries;
798 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
799 ret = ocfs2_xattr_bucket_get_name_value(inode,
804 xs->base = xs->bucket.bhs[block_off]->b_data;
806 if (ocfs2_xattr_is_local(xs->here)) {
807 memcpy(buffer, (void *)xs->base +
808 name_offset + name_len, size);
810 xv = (struct ocfs2_xattr_value_root *)
811 (xs->base + name_offset + name_len);
812 ret = ocfs2_xattr_get_value_outside(inode, xv,
822 for (i = 0; i < OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET; i++)
823 brelse(xs->bucket.bhs[i]);
824 memset(&xs->bucket, 0, sizeof(xs->bucket));
832 * Copy an extended attribute into the buffer provided.
833 * Buffer is NULL to compute the size of buffer required.
835 int ocfs2_xattr_get(struct inode *inode,
842 struct ocfs2_dinode *di = NULL;
843 struct buffer_head *di_bh = NULL;
844 struct ocfs2_inode_info *oi = OCFS2_I(inode);
845 struct ocfs2_xattr_search xis = {
846 .not_found = -ENODATA,
848 struct ocfs2_xattr_search xbs = {
849 .not_found = -ENODATA,
852 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
855 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
858 ret = ocfs2_inode_lock(inode, &di_bh, 0);
863 xis.inode_bh = xbs.inode_bh = di_bh;
864 di = (struct ocfs2_dinode *)di_bh->b_data;
866 down_read(&oi->ip_xattr_sem);
867 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
870 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
872 up_read(&oi->ip_xattr_sem);
873 ocfs2_inode_unlock(inode, 0);
880 static int __ocfs2_xattr_set_value_outside(struct inode *inode,
881 struct ocfs2_xattr_value_root *xv,
885 int ret = 0, i, cp_len, credits;
886 u16 blocksize = inode->i_sb->s_blocksize;
887 u32 p_cluster, num_clusters;
888 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
889 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
891 struct buffer_head *bh = NULL;
894 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
896 credits = clusters * bpc;
897 handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb), credits);
898 if (IS_ERR(handle)) {
899 ret = PTR_ERR(handle);
904 while (cpos < clusters) {
905 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
906 &num_clusters, &xv->xr_list);
912 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
914 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
915 ret = ocfs2_read_block(inode, blkno, &bh);
921 ret = ocfs2_journal_access(handle,
924 OCFS2_JOURNAL_ACCESS_WRITE);
930 cp_len = value_len > blocksize ? blocksize : value_len;
931 memcpy(bh->b_data, value, cp_len);
934 if (cp_len < blocksize)
935 memset(bh->b_data + cp_len, 0,
938 ret = ocfs2_journal_dirty(handle, bh);
947 * XXX: do we need to empty all the following
948 * blocks in this cluster?
953 cpos += num_clusters;
956 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
963 static int ocfs2_xattr_cleanup(struct inode *inode,
964 struct ocfs2_xattr_info *xi,
965 struct ocfs2_xattr_search *xs,
968 handle_t *handle = NULL;
970 size_t name_len = strlen(xi->name);
971 void *val = xs->base + offs;
972 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
974 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
975 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
976 if (IS_ERR(handle)) {
977 ret = PTR_ERR(handle);
981 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
982 OCFS2_JOURNAL_ACCESS_WRITE);
987 /* Decrease xattr count */
988 le16_add_cpu(&xs->header->xh_count, -1);
989 /* Remove the xattr entry and tree root which has already be set*/
990 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
991 memset(val, 0, size);
993 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
997 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1002 static int ocfs2_xattr_update_entry(struct inode *inode,
1003 struct ocfs2_xattr_info *xi,
1004 struct ocfs2_xattr_search *xs,
1007 handle_t *handle = NULL;
1010 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1011 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1012 if (IS_ERR(handle)) {
1013 ret = PTR_ERR(handle);
1017 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1018 OCFS2_JOURNAL_ACCESS_WRITE);
1024 xs->here->xe_name_offset = cpu_to_le16(offs);
1025 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1026 if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
1027 ocfs2_xattr_set_local(xs->here, 1);
1029 ocfs2_xattr_set_local(xs->here, 0);
1030 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1032 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1036 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1042 * ocfs2_xattr_set_value_outside()
1044 * Set large size value in B tree.
1046 static int ocfs2_xattr_set_value_outside(struct inode *inode,
1047 struct ocfs2_xattr_info *xi,
1048 struct ocfs2_xattr_search *xs,
1051 size_t name_len = strlen(xi->name);
1052 void *val = xs->base + offs;
1053 struct ocfs2_xattr_value_root *xv = NULL;
1054 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1057 memset(val, 0, size);
1058 memcpy(val, xi->name, name_len);
1059 xv = (struct ocfs2_xattr_value_root *)
1060 (val + OCFS2_XATTR_SIZE(name_len));
1061 xv->xr_clusters = 0;
1062 xv->xr_last_eb_blk = 0;
1063 xv->xr_list.l_tree_depth = 0;
1064 xv->xr_list.l_count = cpu_to_le16(1);
1065 xv->xr_list.l_next_free_rec = 0;
1067 ret = ocfs2_xattr_value_truncate(inode, xs->xattr_bh, xv,
1073 ret = __ocfs2_xattr_set_value_outside(inode, xv, xi->value,
1079 ret = ocfs2_xattr_update_entry(inode, xi, xs, offs);
1087 * ocfs2_xattr_set_entry_local()
1089 * Set, replace or remove extended attribute in local.
1091 static void ocfs2_xattr_set_entry_local(struct inode *inode,
1092 struct ocfs2_xattr_info *xi,
1093 struct ocfs2_xattr_search *xs,
1094 struct ocfs2_xattr_entry *last,
1097 size_t name_len = strlen(xi->name);
1100 if (xi->value && xs->not_found) {
1101 /* Insert the new xattr entry. */
1102 le16_add_cpu(&xs->header->xh_count, 1);
1103 ocfs2_xattr_set_type(last, xi->name_index);
1104 ocfs2_xattr_set_local(last, 1);
1105 last->xe_name_len = name_len;
1111 first_val = xs->base + min_offs;
1112 offs = le16_to_cpu(xs->here->xe_name_offset);
1113 val = xs->base + offs;
1115 if (le64_to_cpu(xs->here->xe_value_size) >
1116 OCFS2_XATTR_INLINE_SIZE)
1117 size = OCFS2_XATTR_SIZE(name_len) +
1118 OCFS2_XATTR_ROOT_SIZE;
1120 size = OCFS2_XATTR_SIZE(name_len) +
1121 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1123 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1124 OCFS2_XATTR_SIZE(xi->value_len)) {
1125 /* The old and the new value have the
1126 same size. Just replace the value. */
1127 ocfs2_xattr_set_local(xs->here, 1);
1128 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1129 /* Clear value bytes. */
1130 memset(val + OCFS2_XATTR_SIZE(name_len),
1132 OCFS2_XATTR_SIZE(xi->value_len));
1133 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1138 /* Remove the old name+value. */
1139 memmove(first_val + size, first_val, val - first_val);
1140 memset(first_val, 0, size);
1141 xs->here->xe_name_hash = 0;
1142 xs->here->xe_name_offset = 0;
1143 ocfs2_xattr_set_local(xs->here, 1);
1144 xs->here->xe_value_size = 0;
1148 /* Adjust all value offsets. */
1149 last = xs->header->xh_entries;
1150 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1151 size_t o = le16_to_cpu(last->xe_name_offset);
1154 last->xe_name_offset = cpu_to_le16(o + size);
1159 /* Remove the old entry. */
1161 memmove(xs->here, xs->here + 1,
1162 (void *)last - (void *)xs->here);
1163 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1164 le16_add_cpu(&xs->header->xh_count, -1);
1168 /* Insert the new name+value. */
1169 size_t size = OCFS2_XATTR_SIZE(name_len) +
1170 OCFS2_XATTR_SIZE(xi->value_len);
1171 void *val = xs->base + min_offs - size;
1173 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1174 memset(val, 0, size);
1175 memcpy(val, xi->name, name_len);
1176 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1179 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1180 ocfs2_xattr_set_local(xs->here, 1);
1181 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1188 * ocfs2_xattr_set_entry()
1190 * Set extended attribute entry into inode or block.
1192 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1193 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1194 * then set value in B tree with set_value_outside().
1196 static int ocfs2_xattr_set_entry(struct inode *inode,
1197 struct ocfs2_xattr_info *xi,
1198 struct ocfs2_xattr_search *xs,
1201 struct ocfs2_xattr_entry *last;
1202 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1203 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1204 size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1206 handle_t *handle = NULL;
1208 struct ocfs2_xattr_info xi_l = {
1209 .name_index = xi->name_index,
1212 .value_len = xi->value_len,
1215 /* Compute min_offs, last and free space. */
1216 last = xs->header->xh_entries;
1218 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1219 size_t offs = le16_to_cpu(last->xe_name_offset);
1220 if (offs < min_offs)
1225 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
1229 if (!xs->not_found) {
1231 if (ocfs2_xattr_is_local(xs->here))
1232 size = OCFS2_XATTR_SIZE(name_len) +
1233 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1235 size = OCFS2_XATTR_SIZE(name_len) +
1236 OCFS2_XATTR_ROOT_SIZE;
1237 free += (size + sizeof(struct ocfs2_xattr_entry));
1239 /* Check free space in inode or block */
1240 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1241 if (free < sizeof(struct ocfs2_xattr_entry) +
1242 OCFS2_XATTR_SIZE(name_len) +
1243 OCFS2_XATTR_ROOT_SIZE) {
1247 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1248 xi_l.value = (void *)&def_xv;
1249 xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
1250 } else if (xi->value) {
1251 if (free < sizeof(struct ocfs2_xattr_entry) +
1252 OCFS2_XATTR_SIZE(name_len) +
1253 OCFS2_XATTR_SIZE(xi->value_len)) {
1259 if (!xs->not_found) {
1260 /* For existing extended attribute */
1261 size_t size = OCFS2_XATTR_SIZE(name_len) +
1262 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1263 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1264 void *val = xs->base + offs;
1266 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1267 /* Replace existing local xattr with tree root */
1268 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
1273 } else if (!ocfs2_xattr_is_local(xs->here)) {
1274 /* For existing xattr which has value outside */
1275 struct ocfs2_xattr_value_root *xv = NULL;
1276 xv = (struct ocfs2_xattr_value_root *)(val +
1277 OCFS2_XATTR_SIZE(name_len));
1279 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1281 * If new value need set outside also,
1282 * first truncate old value to new value,
1283 * then set new value with set_value_outside().
1285 ret = ocfs2_xattr_value_truncate(inode,
1294 ret = __ocfs2_xattr_set_value_outside(inode,
1303 ret = ocfs2_xattr_update_entry(inode,
1312 * If new value need set in local,
1313 * just trucate old value to zero.
1315 ret = ocfs2_xattr_value_truncate(inode,
1325 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1326 OCFS2_INODE_UPDATE_CREDITS);
1327 if (IS_ERR(handle)) {
1328 ret = PTR_ERR(handle);
1333 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1334 OCFS2_JOURNAL_ACCESS_WRITE);
1340 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1341 /* set extended attribute in external block. */
1342 ret = ocfs2_extend_trans(handle,
1343 OCFS2_INODE_UPDATE_CREDITS +
1344 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1349 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1350 OCFS2_JOURNAL_ACCESS_WRITE);
1358 * Set value in local, include set tree root in local.
1359 * This is the first step for value size >INLINE_SIZE.
1361 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1363 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1364 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1371 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1372 (flag & OCFS2_INLINE_XATTR_FL)) {
1373 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1374 unsigned int xattrsize = osb->s_xattr_inline_size;
1377 * Adjust extent record count or inline data size
1378 * to reserve space for extended attribute.
1380 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1381 struct ocfs2_inline_data *idata = &di->id2.i_data;
1382 le16_add_cpu(&idata->id_count, -xattrsize);
1383 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1384 struct ocfs2_extent_list *el = &di->id2.i_list;
1385 le16_add_cpu(&el->l_count, -(xattrsize /
1386 sizeof(struct ocfs2_extent_rec)));
1388 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1390 /* Update xattr flag */
1391 spin_lock(&oi->ip_lock);
1392 oi->ip_dyn_features |= flag;
1393 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1394 spin_unlock(&oi->ip_lock);
1395 /* Update inode ctime */
1396 inode->i_ctime = CURRENT_TIME;
1397 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
1398 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
1400 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1405 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1407 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1409 * Set value outside in B tree.
1410 * This is the second step for value size > INLINE_SIZE.
1412 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1413 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, offs);
1419 * If set value outside failed, we have to clean
1420 * the junk tree root we have already set in local.
1422 ret2 = ocfs2_xattr_cleanup(inode, xi, xs, offs);
1432 static int ocfs2_remove_value_outside(struct inode*inode,
1433 struct buffer_head *bh,
1434 struct ocfs2_xattr_header *header)
1438 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1439 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1441 if (!ocfs2_xattr_is_local(entry)) {
1442 struct ocfs2_xattr_value_root *xv;
1445 val = (void *)header +
1446 le16_to_cpu(entry->xe_name_offset);
1447 xv = (struct ocfs2_xattr_value_root *)
1448 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
1449 ret = ocfs2_xattr_value_truncate(inode, bh, xv, 0);
1460 static int ocfs2_xattr_ibody_remove(struct inode *inode,
1461 struct buffer_head *di_bh)
1464 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1465 struct ocfs2_xattr_header *header;
1468 header = (struct ocfs2_xattr_header *)
1469 ((void *)di + inode->i_sb->s_blocksize -
1470 le16_to_cpu(di->i_xattr_inline_size));
1472 ret = ocfs2_remove_value_outside(inode, di_bh, header);
1477 static int ocfs2_xattr_block_remove(struct inode *inode,
1478 struct buffer_head *blk_bh)
1480 struct ocfs2_xattr_block *xb;
1483 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1484 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1485 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
1486 ret = ocfs2_remove_value_outside(inode, blk_bh, header);
1488 ret = ocfs2_delete_xattr_index_block(inode, blk_bh);
1493 static int ocfs2_xattr_free_block(struct inode *inode,
1496 struct inode *xb_alloc_inode;
1497 struct buffer_head *xb_alloc_bh = NULL;
1498 struct buffer_head *blk_bh = NULL;
1499 struct ocfs2_xattr_block *xb;
1500 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1506 ret = ocfs2_read_block(inode, block, &blk_bh);
1512 /*Verify the signature of xattr block*/
1513 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
1514 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
1519 ret = ocfs2_xattr_block_remove(inode, blk_bh);
1525 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1526 blk = le64_to_cpu(xb->xb_blkno);
1527 bit = le16_to_cpu(xb->xb_suballoc_bit);
1528 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1530 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1531 EXTENT_ALLOC_SYSTEM_INODE,
1532 le16_to_cpu(xb->xb_suballoc_slot));
1533 if (!xb_alloc_inode) {
1538 mutex_lock(&xb_alloc_inode->i_mutex);
1540 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1546 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
1547 if (IS_ERR(handle)) {
1548 ret = PTR_ERR(handle);
1553 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1558 ocfs2_commit_trans(osb, handle);
1560 ocfs2_inode_unlock(xb_alloc_inode, 1);
1561 brelse(xb_alloc_bh);
1563 mutex_unlock(&xb_alloc_inode->i_mutex);
1564 iput(xb_alloc_inode);
1571 * ocfs2_xattr_remove()
1573 * Free extended attribute resources associated with this inode.
1575 int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
1577 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1578 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1582 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1585 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1588 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1589 ret = ocfs2_xattr_ibody_remove(inode, di_bh);
1596 if (di->i_xattr_loc) {
1597 ret = ocfs2_xattr_free_block(inode,
1598 le64_to_cpu(di->i_xattr_loc));
1605 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1606 OCFS2_INODE_UPDATE_CREDITS);
1607 if (IS_ERR(handle)) {
1608 ret = PTR_ERR(handle);
1612 ret = ocfs2_journal_access(handle, inode, di_bh,
1613 OCFS2_JOURNAL_ACCESS_WRITE);
1619 di->i_xattr_loc = 0;
1621 spin_lock(&oi->ip_lock);
1622 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
1623 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1624 spin_unlock(&oi->ip_lock);
1626 ret = ocfs2_journal_dirty(handle, di_bh);
1630 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1635 static int ocfs2_xattr_has_space_inline(struct inode *inode,
1636 struct ocfs2_dinode *di)
1638 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1639 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
1642 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
1645 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1646 struct ocfs2_inline_data *idata = &di->id2.i_data;
1647 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
1648 } else if (ocfs2_inode_is_fast_symlink(inode)) {
1649 free = ocfs2_fast_symlink_chars(inode->i_sb) -
1650 le64_to_cpu(di->i_size);
1652 struct ocfs2_extent_list *el = &di->id2.i_list;
1653 free = (le16_to_cpu(el->l_count) -
1654 le16_to_cpu(el->l_next_free_rec)) *
1655 sizeof(struct ocfs2_extent_rec);
1657 if (free >= xattrsize)
1664 * ocfs2_xattr_ibody_find()
1666 * Find extended attribute in inode block and
1667 * fill search info into struct ocfs2_xattr_search.
1669 static int ocfs2_xattr_ibody_find(struct inode *inode,
1672 struct ocfs2_xattr_search *xs)
1674 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1675 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1679 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1682 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1683 down_read(&oi->ip_alloc_sem);
1684 has_space = ocfs2_xattr_has_space_inline(inode, di);
1685 up_read(&oi->ip_alloc_sem);
1690 xs->xattr_bh = xs->inode_bh;
1691 xs->end = (void *)di + inode->i_sb->s_blocksize;
1692 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
1693 xs->header = (struct ocfs2_xattr_header *)
1694 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1696 xs->header = (struct ocfs2_xattr_header *)
1697 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
1698 xs->base = (void *)xs->header;
1699 xs->here = xs->header->xh_entries;
1701 /* Find the named attribute. */
1702 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1703 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1704 if (ret && ret != -ENODATA)
1706 xs->not_found = ret;
1713 * ocfs2_xattr_ibody_set()
1715 * Set, replace or remove an extended attribute into inode block.
1718 static int ocfs2_xattr_ibody_set(struct inode *inode,
1719 struct ocfs2_xattr_info *xi,
1720 struct ocfs2_xattr_search *xs)
1722 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1723 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1726 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1729 down_write(&oi->ip_alloc_sem);
1730 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1731 if (!ocfs2_xattr_has_space_inline(inode, di)) {
1737 ret = ocfs2_xattr_set_entry(inode, xi, xs,
1738 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
1740 up_write(&oi->ip_alloc_sem);
1746 * ocfs2_xattr_block_find()
1748 * Find extended attribute in external block and
1749 * fill search info into struct ocfs2_xattr_search.
1751 static int ocfs2_xattr_block_find(struct inode *inode,
1754 struct ocfs2_xattr_search *xs)
1756 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1757 struct buffer_head *blk_bh = NULL;
1758 struct ocfs2_xattr_block *xb;
1761 if (!di->i_xattr_loc)
1764 ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh);
1769 /*Verify the signature of xattr block*/
1770 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
1771 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
1776 xs->xattr_bh = blk_bh;
1777 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1779 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1780 xs->header = &xb->xb_attrs.xb_header;
1781 xs->base = (void *)xs->header;
1782 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
1783 xs->here = xs->header->xh_entries;
1785 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1787 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
1791 if (ret && ret != -ENODATA) {
1792 xs->xattr_bh = NULL;
1795 xs->not_found = ret;
1804 * When all the xattrs are deleted from index btree, the ocfs2_xattr_tree
1805 * will be erased and ocfs2_xattr_block will have its ocfs2_xattr_header
1808 static int ocfs2_restore_xattr_block(struct inode *inode,
1809 struct ocfs2_xattr_search *xs)
1813 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1814 struct ocfs2_xattr_block *xb =
1815 (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1816 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
1817 u16 xb_flags = le16_to_cpu(xb->xb_flags);
1819 BUG_ON(!(xb_flags & OCFS2_XATTR_INDEXED) ||
1820 le16_to_cpu(el->l_next_free_rec) != 0);
1822 handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1823 if (IS_ERR(handle)) {
1824 ret = PTR_ERR(handle);
1829 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1830 OCFS2_JOURNAL_ACCESS_WRITE);
1836 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
1837 offsetof(struct ocfs2_xattr_block, xb_attrs));
1839 xb->xb_flags = cpu_to_le16(xb_flags & ~OCFS2_XATTR_INDEXED);
1841 ocfs2_journal_dirty(handle, xs->xattr_bh);
1844 ocfs2_commit_trans(osb, handle);
1850 * ocfs2_xattr_block_set()
1852 * Set, replace or remove an extended attribute into external block.
1855 static int ocfs2_xattr_block_set(struct inode *inode,
1856 struct ocfs2_xattr_info *xi,
1857 struct ocfs2_xattr_search *xs)
1859 struct buffer_head *new_bh = NULL;
1860 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1861 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1862 struct ocfs2_alloc_context *meta_ac = NULL;
1863 handle_t *handle = NULL;
1864 struct ocfs2_xattr_block *xblk = NULL;
1865 u16 suballoc_bit_start;
1870 if (!xs->xattr_bh) {
1872 * Alloc one external block for extended attribute
1875 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
1880 handle = ocfs2_start_trans(osb,
1881 OCFS2_XATTR_BLOCK_CREATE_CREDITS);
1882 if (IS_ERR(handle)) {
1883 ret = PTR_ERR(handle);
1887 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1888 OCFS2_JOURNAL_ACCESS_CREATE);
1894 ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
1895 &suballoc_bit_start, &num_got,
1902 new_bh = sb_getblk(inode->i_sb, first_blkno);
1903 ocfs2_set_new_buffer_uptodate(inode, new_bh);
1905 ret = ocfs2_journal_access(handle, inode, new_bh,
1906 OCFS2_JOURNAL_ACCESS_CREATE);
1912 /* Initialize ocfs2_xattr_block */
1913 xs->xattr_bh = new_bh;
1914 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
1915 memset(xblk, 0, inode->i_sb->s_blocksize);
1916 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
1917 xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
1918 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1919 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
1920 xblk->xb_blkno = cpu_to_le64(first_blkno);
1922 xs->header = &xblk->xb_attrs.xb_header;
1923 xs->base = (void *)xs->header;
1924 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
1925 xs->here = xs->header->xh_entries;
1928 ret = ocfs2_journal_dirty(handle, new_bh);
1933 di->i_xattr_loc = cpu_to_le64(first_blkno);
1934 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1938 ocfs2_commit_trans(osb, handle);
1941 ocfs2_free_alloc_context(meta_ac);
1945 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1947 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
1948 /* Set extended attribute into external block */
1949 ret = ocfs2_xattr_set_entry(inode, xi, xs, OCFS2_HAS_XATTR_FL);
1950 if (!ret || ret != -ENOSPC)
1953 ret = ocfs2_xattr_create_index_block(inode, xs);
1958 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs);
1959 if (!ret && xblk->xb_attrs.xb_root.xt_list.l_next_free_rec == 0)
1960 ret = ocfs2_restore_xattr_block(inode, xs);
1970 * Set, replace or remove an extended attribute for this inode.
1971 * value is NULL to remove an existing extended attribute, else either
1972 * create or replace an extended attribute.
1974 int ocfs2_xattr_set(struct inode *inode,
1981 struct buffer_head *di_bh = NULL;
1982 struct ocfs2_dinode *di;
1984 u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
1986 struct ocfs2_xattr_info xi = {
1987 .name_index = name_index,
1990 .value_len = value_len,
1993 struct ocfs2_xattr_search xis = {
1994 .not_found = -ENODATA,
1997 struct ocfs2_xattr_search xbs = {
1998 .not_found = -ENODATA,
2001 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2004 ret = ocfs2_inode_lock(inode, &di_bh, 1);
2009 xis.inode_bh = xbs.inode_bh = di_bh;
2010 di = (struct ocfs2_dinode *)di_bh->b_data;
2012 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2014 * Scan inode and external block to find the same name
2015 * extended attribute and collect search infomation.
2017 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2020 if (xis.not_found) {
2021 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2026 if (xis.not_found && xbs.not_found) {
2028 if (flags & XATTR_REPLACE)
2035 if (flags & XATTR_CREATE)
2040 /* Remove existing extended attribute */
2042 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2043 else if (!xbs.not_found)
2044 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2046 /* We always try to set extended attribute into inode first*/
2047 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2048 if (!ret && !xbs.not_found) {
2050 * If succeed and that extended attribute existing in
2051 * external block, then we will remove it.
2055 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2056 } else if (ret == -ENOSPC) {
2057 if (di->i_xattr_loc && !xbs.xattr_bh) {
2058 ret = ocfs2_xattr_block_find(inode, name_index,
2064 * If no space in inode, we will set extended attribute
2065 * into external block.
2067 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2070 if (!xis.not_found) {
2072 * If succeed and that extended attribute
2073 * existing in inode, we will remove it.
2077 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2082 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2083 ocfs2_inode_unlock(inode, 1);
2085 brelse(xbs.xattr_bh);
2086 for (i = 0; i < blk_per_bucket; i++)
2087 brelse(xbs.bucket.bhs[i]);
2093 * Find the xattr extent rec which may contains name_hash.
2094 * e_cpos will be the first name hash of the xattr rec.
2095 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
2097 static int ocfs2_xattr_get_rec(struct inode *inode,
2102 struct ocfs2_extent_list *el)
2105 struct buffer_head *eb_bh = NULL;
2106 struct ocfs2_extent_block *eb;
2107 struct ocfs2_extent_rec *rec = NULL;
2110 if (el->l_tree_depth) {
2111 ret = ocfs2_find_leaf(inode, el, name_hash, &eb_bh);
2117 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
2120 if (el->l_tree_depth) {
2121 ocfs2_error(inode->i_sb,
2122 "Inode %lu has non zero tree depth in "
2123 "xattr tree block %llu\n", inode->i_ino,
2124 (unsigned long long)eb_bh->b_blocknr);
2130 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
2131 rec = &el->l_recs[i];
2133 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
2134 e_blkno = le64_to_cpu(rec->e_blkno);
2140 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
2141 "record (%u, %u, 0) in xattr", inode->i_ino,
2142 le32_to_cpu(rec->e_cpos),
2143 ocfs2_rec_clusters(el, rec));
2148 *p_blkno = le64_to_cpu(rec->e_blkno);
2149 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
2151 *e_cpos = le32_to_cpu(rec->e_cpos);
2157 typedef int (xattr_bucket_func)(struct inode *inode,
2158 struct ocfs2_xattr_bucket *bucket,
2161 static int ocfs2_find_xe_in_bucket(struct inode *inode,
2162 struct buffer_head *header_bh,
2169 int i, ret = 0, cmp = 1, block_off, new_offset;
2170 struct ocfs2_xattr_header *xh =
2171 (struct ocfs2_xattr_header *)header_bh->b_data;
2172 size_t name_len = strlen(name);
2173 struct ocfs2_xattr_entry *xe = NULL;
2174 struct buffer_head *name_bh = NULL;
2178 * We don't use binary search in the bucket because there
2179 * may be multiple entries with the same name hash.
2181 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
2182 xe = &xh->xh_entries[i];
2184 if (name_hash > le32_to_cpu(xe->xe_name_hash))
2186 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
2189 cmp = name_index - ocfs2_xattr_get_type(xe);
2191 cmp = name_len - xe->xe_name_len;
2195 ret = ocfs2_xattr_bucket_get_name_value(inode,
2205 ret = ocfs2_read_block(inode, header_bh->b_blocknr + block_off,
2211 xe_name = name_bh->b_data + new_offset;
2213 cmp = memcmp(name, xe_name, name_len);
2229 * Find the specified xattr entry in a series of buckets.
2230 * This series start from p_blkno and last for num_clusters.
2231 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
2232 * the num of the valid buckets.
2234 * Return the buffer_head this xattr should reside in. And if the xattr's
2235 * hash is in the gap of 2 buckets, return the lower bucket.
2237 static int ocfs2_xattr_bucket_find(struct inode *inode,
2244 struct ocfs2_xattr_search *xs)
2247 struct buffer_head *bh = NULL;
2248 struct buffer_head *lower_bh = NULL;
2249 struct ocfs2_xattr_header *xh = NULL;
2250 struct ocfs2_xattr_entry *xe = NULL;
2252 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2253 int low_bucket = 0, bucket, high_bucket;
2257 ret = ocfs2_read_block(inode, p_blkno, &bh);
2263 xh = (struct ocfs2_xattr_header *)bh->b_data;
2264 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
2266 while (low_bucket <= high_bucket) {
2269 bucket = (low_bucket + high_bucket) / 2;
2271 blkno = p_blkno + bucket * blk_per_bucket;
2273 ret = ocfs2_read_block(inode, blkno, &bh);
2279 xh = (struct ocfs2_xattr_header *)bh->b_data;
2280 xe = &xh->xh_entries[0];
2281 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
2282 high_bucket = bucket - 1;
2287 * Check whether the hash of the last entry in our
2288 * bucket is larger than the search one. for an empty
2289 * bucket, the last one is also the first one.
2292 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
2294 last_hash = le32_to_cpu(xe->xe_name_hash);
2296 /* record lower_bh which may be the insert place. */
2301 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
2302 low_bucket = bucket + 1;
2306 /* the searched xattr should reside in this bucket if exists. */
2307 ret = ocfs2_find_xe_in_bucket(inode, lower_bh,
2308 name_index, name, name_hash,
2318 * Record the bucket we have found.
2319 * When the xattr's hash value is in the gap of 2 buckets, we will
2320 * always set it to the previous bucket.
2324 * We can't find any bucket whose first name_hash is less
2325 * than the find name_hash.
2327 BUG_ON(bh->b_blocknr != p_blkno);
2331 xs->bucket.bhs[0] = lower_bh;
2332 xs->bucket.xh = (struct ocfs2_xattr_header *)
2333 xs->bucket.bhs[0]->b_data;
2336 xs->header = xs->bucket.xh;
2337 xs->base = xs->bucket.bhs[0]->b_data;
2338 xs->end = xs->base + inode->i_sb->s_blocksize;
2342 * If we have found the xattr enty, read all the blocks in
2345 ret = ocfs2_read_blocks(inode, xs->bucket.bhs[0]->b_blocknr + 1,
2346 blk_per_bucket - 1, &xs->bucket.bhs[1],
2353 xs->here = &xs->header->xh_entries[index];
2354 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
2355 (unsigned long long)xs->bucket.bhs[0]->b_blocknr, index);
2365 static int ocfs2_xattr_index_block_find(struct inode *inode,
2366 struct buffer_head *root_bh,
2369 struct ocfs2_xattr_search *xs)
2372 struct ocfs2_xattr_block *xb =
2373 (struct ocfs2_xattr_block *)root_bh->b_data;
2374 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
2375 struct ocfs2_extent_list *el = &xb_root->xt_list;
2377 u32 first_hash, num_clusters = 0;
2378 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
2380 if (le16_to_cpu(el->l_next_free_rec) == 0)
2383 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
2384 name, name_hash, name_index);
2386 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
2393 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
2395 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
2396 "in the rec is %u\n", num_clusters, p_blkno, first_hash);
2398 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
2399 p_blkno, first_hash, num_clusters, xs);
2405 static int ocfs2_iterate_xattr_buckets(struct inode *inode,
2408 xattr_bucket_func *func,
2412 int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2413 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
2414 u32 num_buckets = clusters * bpc;
2415 struct ocfs2_xattr_bucket bucket;
2417 memset(&bucket, 0, sizeof(bucket));
2419 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
2422 for (i = 0; i < num_buckets; i++, blkno += blk_per_bucket) {
2423 ret = ocfs2_read_blocks(inode, blkno, blk_per_bucket,
2430 bucket.xh = (struct ocfs2_xattr_header *)bucket.bhs[0]->b_data;
2432 * The real bucket num in this series of blocks is stored
2433 * in the 1st bucket.
2436 num_buckets = le16_to_cpu(bucket.xh->xh_num_buckets);
2438 mlog(0, "iterating xattr bucket %llu, first hash %u\n", blkno,
2439 le32_to_cpu(bucket.xh->xh_entries[0].xe_name_hash));
2441 ret = func(inode, &bucket, para);
2448 for (j = 0; j < blk_per_bucket; j++)
2449 brelse(bucket.bhs[j]);
2450 memset(&bucket, 0, sizeof(bucket));
2454 for (j = 0; j < blk_per_bucket; j++)
2455 brelse(bucket.bhs[j]);
2460 struct ocfs2_xattr_tree_list {
2466 static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
2467 struct ocfs2_xattr_header *xh,
2474 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
2477 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
2479 *block_off = name_offset >> inode->i_sb->s_blocksize_bits;
2480 *new_offset = name_offset % inode->i_sb->s_blocksize;
2485 static int ocfs2_list_xattr_bucket(struct inode *inode,
2486 struct ocfs2_xattr_bucket *bucket,
2490 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
2491 int i, block_off, new_offset;
2492 const char *prefix, *name;
2494 for (i = 0 ; i < le16_to_cpu(bucket->xh->xh_count); i++) {
2495 struct ocfs2_xattr_entry *entry = &bucket->xh->xh_entries[i];
2496 type = ocfs2_xattr_get_type(entry);
2497 prefix = ocfs2_xattr_prefix(type);
2500 ret = ocfs2_xattr_bucket_get_name_value(inode,
2508 name = (const char *)bucket->bhs[block_off]->b_data +
2510 ret = ocfs2_xattr_list_entry(xl->buffer,
2514 entry->xe_name_len);
2523 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
2524 struct ocfs2_xattr_tree_root *xt,
2528 struct ocfs2_extent_list *el = &xt->xt_list;
2530 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
2532 struct ocfs2_xattr_tree_list xl = {
2534 .buffer_size = buffer_size,
2538 if (le16_to_cpu(el->l_next_free_rec) == 0)
2541 while (name_hash > 0) {
2542 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
2543 &e_cpos, &num_clusters, el);
2549 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
2550 ocfs2_list_xattr_bucket,
2560 name_hash = e_cpos - 1;
2568 static int cmp_xe(const void *a, const void *b)
2570 const struct ocfs2_xattr_entry *l = a, *r = b;
2571 u32 l_hash = le32_to_cpu(l->xe_name_hash);
2572 u32 r_hash = le32_to_cpu(r->xe_name_hash);
2574 if (l_hash > r_hash)
2576 if (l_hash < r_hash)
2581 static void swap_xe(void *a, void *b, int size)
2583 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
2586 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
2587 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
2591 * When the ocfs2_xattr_block is filled up, new bucket will be created
2592 * and all the xattr entries will be moved to the new bucket.
2593 * Note: we need to sort the entries since they are not saved in order
2594 * in the ocfs2_xattr_block.
2596 static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
2597 struct buffer_head *xb_bh,
2598 struct buffer_head *xh_bh,
2599 struct buffer_head *data_bh)
2601 int i, blocksize = inode->i_sb->s_blocksize;
2602 u16 offset, size, off_change;
2603 struct ocfs2_xattr_entry *xe;
2604 struct ocfs2_xattr_block *xb =
2605 (struct ocfs2_xattr_block *)xb_bh->b_data;
2606 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
2607 struct ocfs2_xattr_header *xh =
2608 (struct ocfs2_xattr_header *)xh_bh->b_data;
2609 u16 count = le16_to_cpu(xb_xh->xh_count);
2610 char *target = xh_bh->b_data, *src = xb_bh->b_data;
2612 mlog(0, "cp xattr from block %llu to bucket %llu\n",
2613 (unsigned long long)xb_bh->b_blocknr,
2614 (unsigned long long)xh_bh->b_blocknr);
2616 memset(xh_bh->b_data, 0, blocksize);
2618 memset(data_bh->b_data, 0, blocksize);
2620 * Since the xe_name_offset is based on ocfs2_xattr_header,
2621 * there is a offset change corresponding to the change of
2622 * ocfs2_xattr_header's position.
2624 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
2625 xe = &xb_xh->xh_entries[count - 1];
2626 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
2627 size = blocksize - offset;
2629 /* copy all the names and values. */
2631 target = data_bh->b_data;
2632 memcpy(target + offset, src + offset, size);
2634 /* Init new header now. */
2635 xh->xh_count = xb_xh->xh_count;
2636 xh->xh_num_buckets = cpu_to_le16(1);
2637 xh->xh_name_value_len = cpu_to_le16(size);
2638 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
2640 /* copy all the entries. */
2641 target = xh_bh->b_data;
2642 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
2643 size = count * sizeof(struct ocfs2_xattr_entry);
2644 memcpy(target + offset, (char *)xb_xh + offset, size);
2646 /* Change the xe offset for all the xe because of the move. */
2647 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
2648 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
2649 for (i = 0; i < count; i++)
2650 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
2652 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
2653 offset, size, off_change);
2655 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
2660 * After we move xattr from block to index btree, we have to
2661 * update ocfs2_xattr_search to the new xe and base.
2663 * When the entry is in xattr block, xattr_bh indicates the storage place.
2664 * While if the entry is in index b-tree, "bucket" indicates the
2665 * real place of the xattr.
2667 static int ocfs2_xattr_update_xattr_search(struct inode *inode,
2668 struct ocfs2_xattr_search *xs,
2669 struct buffer_head *old_bh,
2670 struct buffer_head *new_bh)
2673 char *buf = old_bh->b_data;
2674 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
2675 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
2676 int i, blocksize = inode->i_sb->s_blocksize;
2677 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2679 xs->bucket.bhs[0] = new_bh;
2681 xs->bucket.xh = (struct ocfs2_xattr_header *)xs->bucket.bhs[0]->b_data;
2682 xs->header = xs->bucket.xh;
2684 xs->base = new_bh->b_data;
2685 xs->end = xs->base + inode->i_sb->s_blocksize;
2687 if (!xs->not_found) {
2688 if (OCFS2_XATTR_BUCKET_SIZE != blocksize) {
2689 ret = ocfs2_read_blocks(inode,
2690 xs->bucket.bhs[0]->b_blocknr + 1,
2691 blk_per_bucket - 1, &xs->bucket.bhs[1],
2698 i = xs->here - old_xh->xh_entries;
2699 xs->here = &xs->header->xh_entries[i];
2706 static int ocfs2_xattr_create_index_block(struct inode *inode,
2707 struct ocfs2_xattr_search *xs)
2709 int ret, credits = OCFS2_SUBALLOC_ALLOC;
2713 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2714 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2715 struct ocfs2_alloc_context *data_ac;
2716 struct buffer_head *xh_bh = NULL, *data_bh = NULL;
2717 struct buffer_head *xb_bh = xs->xattr_bh;
2718 struct ocfs2_xattr_block *xb =
2719 (struct ocfs2_xattr_block *)xb_bh->b_data;
2720 struct ocfs2_xattr_tree_root *xr;
2721 u16 xb_flags = le16_to_cpu(xb->xb_flags);
2722 u16 bpb = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2724 mlog(0, "create xattr index block for %llu\n",
2725 (unsigned long long)xb_bh->b_blocknr);
2727 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
2729 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
2737 * We can use this lock for now, and maybe move to a dedicated mutex
2738 * if performance becomes a problem later.
2740 down_write(&oi->ip_alloc_sem);
2743 * 3 more credits, one for xattr block update, one for the 1st block
2744 * of the new xattr bucket and one for the value/data.
2747 handle = ocfs2_start_trans(osb, credits);
2748 if (IS_ERR(handle)) {
2749 ret = PTR_ERR(handle);
2754 ret = ocfs2_journal_access(handle, inode, xb_bh,
2755 OCFS2_JOURNAL_ACCESS_WRITE);
2761 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
2768 * The bucket may spread in many blocks, and
2769 * we will only touch the 1st block and the last block
2770 * in the whole bucket(one for entry and one for data).
2772 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
2774 mlog(0, "allocate 1 cluster from %llu to xattr block\n", blkno);
2776 xh_bh = sb_getblk(inode->i_sb, blkno);
2783 ocfs2_set_new_buffer_uptodate(inode, xh_bh);
2785 ret = ocfs2_journal_access(handle, inode, xh_bh,
2786 OCFS2_JOURNAL_ACCESS_CREATE);
2793 data_bh = sb_getblk(inode->i_sb, blkno + bpb - 1);
2800 ocfs2_set_new_buffer_uptodate(inode, data_bh);
2802 ret = ocfs2_journal_access(handle, inode, data_bh,
2803 OCFS2_JOURNAL_ACCESS_CREATE);
2810 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xh_bh, data_bh);
2812 ocfs2_journal_dirty(handle, xh_bh);
2814 ocfs2_journal_dirty(handle, data_bh);
2816 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh, xh_bh);
2818 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
2819 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
2820 offsetof(struct ocfs2_xattr_block, xb_attrs));
2822 xr = &xb->xb_attrs.xb_root;
2823 xr->xt_clusters = cpu_to_le32(1);
2824 xr->xt_last_eb_blk = 0;
2825 xr->xt_list.l_tree_depth = 0;
2826 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
2827 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2829 xr->xt_list.l_recs[0].e_cpos = 0;
2830 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
2831 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
2833 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
2835 ret = ocfs2_journal_dirty(handle, xb_bh);
2842 ocfs2_commit_trans(osb, handle);
2845 up_write(&oi->ip_alloc_sem);
2849 ocfs2_free_alloc_context(data_ac);
2857 static int cmp_xe_offset(const void *a, const void *b)
2859 const struct ocfs2_xattr_entry *l = a, *r = b;
2860 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
2861 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
2863 if (l_name_offset < r_name_offset)
2865 if (l_name_offset > r_name_offset)
2871 * defrag a xattr bucket if we find that the bucket has some
2872 * holes beteen name/value pairs.
2873 * We will move all the name/value pairs to the end of the bucket
2874 * so that we can spare some space for insertion.
2876 static int ocfs2_defrag_xattr_bucket(struct inode *inode,
2877 struct ocfs2_xattr_bucket *bucket)
2880 size_t end, offset, len, value_len;
2881 struct ocfs2_xattr_header *xh;
2882 char *entries, *buf, *bucket_buf = NULL;
2883 u64 blkno = bucket->bhs[0]->b_blocknr;
2884 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2886 size_t blocksize = inode->i_sb->s_blocksize;
2888 struct buffer_head **bhs;
2889 struct ocfs2_xattr_entry *xe;
2891 bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
2896 ret = ocfs2_read_blocks(inode, blkno, blk_per_bucket, bhs, 0);
2901 * In order to make the operation more efficient and generic,
2902 * we copy all the blocks into a contiguous memory and do the
2903 * defragment there, so if anything is error, we will not touch
2906 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
2913 for (i = 0; i < blk_per_bucket; i++, buf += blocksize)
2914 memcpy(buf, bhs[i]->b_data, blocksize);
2916 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), blk_per_bucket);
2917 if (IS_ERR(handle)) {
2918 ret = PTR_ERR(handle);
2924 for (i = 0; i < blk_per_bucket; i++) {
2925 ret = ocfs2_journal_access(handle, inode, bhs[i],
2926 OCFS2_JOURNAL_ACCESS_WRITE);
2933 xh = (struct ocfs2_xattr_header *)bucket_buf;
2934 entries = (char *)xh->xh_entries;
2935 xh_free_start = le16_to_cpu(xh->xh_free_start);
2937 mlog(0, "adjust xattr bucket in %llu, count = %u, "
2938 "xh_free_start = %u, xh_name_value_len = %u.\n",
2939 blkno, le16_to_cpu(xh->xh_count), xh_free_start,
2940 le16_to_cpu(xh->xh_name_value_len));
2943 * sort all the entries by their offset.
2944 * the largest will be the first, so that we can
2945 * move them to the end one by one.
2947 sort(entries, le16_to_cpu(xh->xh_count),
2948 sizeof(struct ocfs2_xattr_entry),
2949 cmp_xe_offset, swap_xe);
2951 /* Move all name/values to the end of the bucket. */
2952 xe = xh->xh_entries;
2953 end = OCFS2_XATTR_BUCKET_SIZE;
2954 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
2955 offset = le16_to_cpu(xe->xe_name_offset);
2956 if (ocfs2_xattr_is_local(xe))
2957 value_len = OCFS2_XATTR_SIZE(
2958 le64_to_cpu(xe->xe_value_size));
2960 value_len = OCFS2_XATTR_ROOT_SIZE;
2961 len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
2964 * We must make sure that the name/value pair
2965 * exist in the same block. So adjust end to
2966 * the previous block end if needed.
2968 if (((end - len) / blocksize !=
2969 (end - 1) / blocksize))
2970 end = end - end % blocksize;
2972 if (end > offset + len) {
2973 memmove(bucket_buf + end - len,
2974 bucket_buf + offset, len);
2975 xe->xe_name_offset = cpu_to_le16(end - len);
2978 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
2979 "bucket %llu\n", (unsigned long long)blkno);
2984 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
2985 "bucket %llu\n", (unsigned long long)blkno);
2987 if (xh_free_start == end)
2990 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
2991 xh->xh_free_start = cpu_to_le16(end);
2993 /* sort the entries by their name_hash. */
2994 sort(entries, le16_to_cpu(xh->xh_count),
2995 sizeof(struct ocfs2_xattr_entry),
2999 for (i = 0; i < blk_per_bucket; i++, buf += blocksize) {
3000 memcpy(bhs[i]->b_data, buf, blocksize);
3001 ocfs2_journal_dirty(handle, bhs[i]);
3005 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
3009 for (i = 0; i < blk_per_bucket; i++)
3019 * Move half nums of the xattr bucket in the previous cluster to this new
3020 * cluster. We only touch the last cluster of the previous extend record.
3022 * first_bh is the first buffer_head of a series of bucket in the same
3023 * extent rec and header_bh is the header of one bucket in this cluster.
3024 * They will be updated if we move the data header_bh contains to the new
3025 * cluster. first_hash will be set as the 1st xe's name_hash of the new cluster.
3027 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
3029 struct buffer_head **first_bh,
3030 struct buffer_head **header_bh,
3036 int i, ret, credits;
3037 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3038 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3039 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3040 int blocksize = inode->i_sb->s_blocksize;
3041 struct buffer_head *old_bh, *new_bh, *prev_bh, *new_first_bh = NULL;
3042 struct ocfs2_xattr_header *new_xh;
3043 struct ocfs2_xattr_header *xh =
3044 (struct ocfs2_xattr_header *)((*first_bh)->b_data);
3046 BUG_ON(le16_to_cpu(xh->xh_num_buckets) < num_buckets);
3047 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == osb->s_clustersize);
3049 prev_bh = *first_bh;
3051 xh = (struct ocfs2_xattr_header *)prev_bh->b_data;
3053 prev_blkno += (num_clusters - 1) * bpc + bpc / 2;
3055 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
3056 prev_blkno, new_blkno);
3059 * We need to update the 1st half of the new cluster and
3060 * 1 more for the update of the 1st bucket of the previous
3063 credits = bpc / 2 + 1;
3064 ret = ocfs2_extend_trans(handle, credits);
3070 ret = ocfs2_journal_access(handle, inode, prev_bh,
3071 OCFS2_JOURNAL_ACCESS_WRITE);
3077 for (i = 0; i < bpc / 2; i++, prev_blkno++, new_blkno++) {
3078 old_bh = new_bh = NULL;
3079 new_bh = sb_getblk(inode->i_sb, new_blkno);
3086 ocfs2_set_new_buffer_uptodate(inode, new_bh);
3088 ret = ocfs2_journal_access(handle, inode, new_bh,
3089 OCFS2_JOURNAL_ACCESS_CREATE);
3096 ret = ocfs2_read_block(inode, prev_blkno, &old_bh);
3103 memcpy(new_bh->b_data, old_bh->b_data, blocksize);
3106 new_xh = (struct ocfs2_xattr_header *)new_bh->b_data;
3107 new_xh->xh_num_buckets = cpu_to_le16(num_buckets / 2);
3110 *first_hash = le32_to_cpu(
3111 new_xh->xh_entries[0].xe_name_hash);
3112 new_first_bh = new_bh;
3113 get_bh(new_first_bh);
3116 ocfs2_journal_dirty(handle, new_bh);
3118 if (*header_bh == old_bh) {
3120 *header_bh = new_bh;
3124 *first_bh = new_first_bh;
3131 le16_add_cpu(&xh->xh_num_buckets, -(num_buckets / 2));
3133 ocfs2_journal_dirty(handle, prev_bh);
3136 brelse(new_first_bh);
3140 static int ocfs2_read_xattr_bucket(struct inode *inode,
3142 struct buffer_head **bhs,
3146 u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3149 return ocfs2_read_blocks(inode, blkno,
3150 blk_per_bucket, bhs, 0);
3152 for (i = 0; i < blk_per_bucket; i++) {
3153 bhs[i] = sb_getblk(inode->i_sb, blkno + i);
3154 if (bhs[i] == NULL) {
3159 ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
3166 * Move half num of the xattrs in old bucket(blk) to new bucket(new_blk).
3167 * first_hash will record the 1st hash of the new bucket.
3169 static int ocfs2_half_xattr_bucket(struct inode *inode,
3174 int new_bucket_head)
3177 u16 count, start, len, name_value_len, xe_len, name_offset;
3178 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3179 struct buffer_head **s_bhs, **t_bhs = NULL;
3180 struct ocfs2_xattr_header *xh;
3181 struct ocfs2_xattr_entry *xe;
3182 int blocksize = inode->i_sb->s_blocksize;
3184 mlog(0, "move half of xattrs from bucket %llu to %llu\n",
3187 s_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
3191 ret = ocfs2_read_xattr_bucket(inode, blk, s_bhs, 0);
3197 ret = ocfs2_journal_access(handle, inode, s_bhs[0],
3198 OCFS2_JOURNAL_ACCESS_WRITE);
3204 t_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
3210 ret = ocfs2_read_xattr_bucket(inode, new_blk, t_bhs, new_bucket_head);
3216 for (i = 0; i < blk_per_bucket; i++) {
3217 ret = ocfs2_journal_access(handle, inode, t_bhs[i],
3218 OCFS2_JOURNAL_ACCESS_CREATE);
3225 /* copy the whole bucket to the new first. */
3226 for (i = 0; i < blk_per_bucket; i++)
3227 memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
3229 /* update the new bucket. */
3230 xh = (struct ocfs2_xattr_header *)t_bhs[0]->b_data;
3231 count = le16_to_cpu(xh->xh_count);
3235 * Calculate the total name/value len and xh_free_start for
3236 * the old bucket first.
3238 name_offset = OCFS2_XATTR_BUCKET_SIZE;
3240 for (i = 0; i < start; i++) {
3241 xe = &xh->xh_entries[i];
3242 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3243 if (ocfs2_xattr_is_local(xe))
3245 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3247 xe_len += OCFS2_XATTR_ROOT_SIZE;
3248 name_value_len += xe_len;
3249 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
3250 name_offset = le16_to_cpu(xe->xe_name_offset);
3254 * Now begin the modification to the new bucket.
3256 * In the new bucket, We just move the xattr entry to the beginning
3257 * and don't touch the name/value. So there will be some holes in the
3258 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
3261 xe = &xh->xh_entries[start];
3262 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
3263 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
3264 (int)((char *)xe - (char *)xh),
3265 (int)((char *)xh->xh_entries - (char *)xh));
3266 memmove((char *)xh->xh_entries, (char *)xe, len);
3267 xe = &xh->xh_entries[count - start];
3268 len = sizeof(struct ocfs2_xattr_entry) * start;
3269 memset((char *)xe, 0, len);
3271 le16_add_cpu(&xh->xh_count, -start);
3272 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
3274 /* Calculate xh_free_start for the new bucket. */
3275 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3276 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3277 xe = &xh->xh_entries[i];
3278 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3279 if (ocfs2_xattr_is_local(xe))
3281 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3283 xe_len += OCFS2_XATTR_ROOT_SIZE;
3284 if (le16_to_cpu(xe->xe_name_offset) <
3285 le16_to_cpu(xh->xh_free_start))
3286 xh->xh_free_start = xe->xe_name_offset;
3289 /* set xh->xh_num_buckets for the new xh. */
3290 if (new_bucket_head)
3291 xh->xh_num_buckets = cpu_to_le16(1);
3293 xh->xh_num_buckets = 0;
3295 for (i = 0; i < blk_per_bucket; i++) {
3296 ocfs2_journal_dirty(handle, t_bhs[i]);
3301 /* store the first_hash of the new bucket. */
3303 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3306 * Now only update the 1st block of the old bucket.
3307 * Please note that the entry has been sorted already above.
3309 xh = (struct ocfs2_xattr_header *)s_bhs[0]->b_data;
3310 memset(&xh->xh_entries[start], 0,
3311 sizeof(struct ocfs2_xattr_entry) * (count - start));
3312 xh->xh_count = cpu_to_le16(start);
3313 xh->xh_free_start = cpu_to_le16(name_offset);
3314 xh->xh_name_value_len = cpu_to_le16(name_value_len);
3316 ocfs2_journal_dirty(handle, s_bhs[0]);
3322 for (i = 0; i < blk_per_bucket; i++)
3328 for (i = 0; i < blk_per_bucket; i++)
3337 * Copy xattr from one bucket to another bucket.
3339 * The caller must make sure that the journal transaction
3340 * has enough space for journaling.
3342 static int ocfs2_cp_xattr_bucket(struct inode *inode,
3349 int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3350 int blocksize = inode->i_sb->s_blocksize;
3351 struct buffer_head **s_bhs, **t_bhs = NULL;
3353 BUG_ON(s_blkno == t_blkno);
3355 mlog(0, "cp bucket %llu to %llu, target is %d\n",
3356 s_blkno, t_blkno, t_is_new);
3358 s_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
3363 ret = ocfs2_read_xattr_bucket(inode, s_blkno, s_bhs, 0);
3367 t_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
3374 ret = ocfs2_read_xattr_bucket(inode, t_blkno, t_bhs, t_is_new);
3378 for (i = 0; i < blk_per_bucket; i++) {
3379 ret = ocfs2_journal_access(handle, inode, t_bhs[i],
3380 OCFS2_JOURNAL_ACCESS_WRITE);
3385 for (i = 0; i < blk_per_bucket; i++) {
3386 memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
3387 ocfs2_journal_dirty(handle, t_bhs[i]);
3392 for (i = 0; i < blk_per_bucket; i++)
3398 for (i = 0; i < blk_per_bucket; i++)
3407 * Copy one xattr cluster from src_blk to to_blk.
3408 * The to_blk will become the first bucket header of the cluster, so its
3409 * xh_num_buckets will be initialized as the bucket num in the cluster.
3411 static int ocfs2_cp_xattr_cluster(struct inode *inode,
3413 struct buffer_head *first_bh,
3418 int i, ret, credits;
3419 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3420 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3421 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3422 struct buffer_head *bh = NULL;
3423 struct ocfs2_xattr_header *xh;
3424 u64 to_blk_start = to_blk;
3426 mlog(0, "cp xattrs from cluster %llu to %llu\n", src_blk, to_blk);
3429 * We need to update the new cluster and 1 more for the update of
3430 * the 1st bucket of the previous extent rec.
3433 ret = ocfs2_extend_trans(handle, credits);
3439 ret = ocfs2_journal_access(handle, inode, first_bh,
3440 OCFS2_JOURNAL_ACCESS_WRITE);
3446 for (i = 0; i < num_buckets; i++) {
3447 ret = ocfs2_cp_xattr_bucket(inode, handle,
3448 src_blk, to_blk, 1);
3454 src_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3455 to_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3458 /* update the old bucket header. */
3459 xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3460 le16_add_cpu(&xh->xh_num_buckets, -num_buckets);
3462 ocfs2_journal_dirty(handle, first_bh);
3464 /* update the new bucket header. */
3465 ret = ocfs2_read_block(inode, to_blk_start, &bh);
3471 ret = ocfs2_journal_access(handle, inode, bh,
3472 OCFS2_JOURNAL_ACCESS_WRITE);
3478 xh = (struct ocfs2_xattr_header *)bh->b_data;
3479 xh->xh_num_buckets = cpu_to_le16(num_buckets);
3481 ocfs2_journal_dirty(handle, bh);
3484 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3491 * Move half of the xattrs in this cluster to the new cluster.
3492 * This function should only be called when bucket size == cluster size.
3493 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
3495 static int ocfs2_half_xattr_cluster(struct inode *inode,
3501 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3502 int ret, credits = 2 * blk_per_bucket;
3504 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
3506 ret = ocfs2_extend_trans(handle, credits);
3512 /* Move half of the xattr in start_blk to the next bucket. */
3513 return ocfs2_half_xattr_bucket(inode, handle, prev_blk,
3514 new_blk, first_hash, 1);
3518 * Move some xattrs from the old cluster to the new one since they are not
3519 * contiguous in ocfs2 xattr tree.
3521 * new_blk starts a new separate cluster, and we will move some xattrs from
3522 * prev_blk to it. v_start will be set as the first name hash value in this
3523 * new cluster so that it can be used as e_cpos during tree insertion and
3524 * don't collide with our original b-tree operations. first_bh and header_bh
3525 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
3526 * to extend the insert bucket.
3528 * The problem is how much xattr should we move to the new one and when should
3529 * we update first_bh and header_bh?
3530 * 1. If cluster size > bucket size, that means the previous cluster has more
3531 * than 1 bucket, so just move half nums of bucket into the new cluster and
3532 * update the first_bh and header_bh if the insert bucket has been moved
3533 * to the new cluster.
3534 * 2. If cluster_size == bucket_size:
3535 * a) If the previous extent rec has more than one cluster and the insert
3536 * place isn't in the last cluster, copy the entire last cluster to the
3537 * new one. This time, we don't need to upate the first_bh and header_bh
3538 * since they will not be moved into the new cluster.
3539 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
3540 * the new one. And we set the extend flag to zero if the insert place is
3541 * moved into the new allocated cluster since no extend is needed.
3543 static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
3545 struct buffer_head **first_bh,
3546 struct buffer_head **header_bh,
3554 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3556 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
3557 prev_blk, prev_clusters, new_blk);
3559 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1)
3560 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
3569 u64 last_blk = prev_blk + bpc * (prev_clusters - 1);
3571 if (prev_clusters > 1 && (*header_bh)->b_blocknr != last_blk)
3572 ret = ocfs2_cp_xattr_cluster(inode, handle, *first_bh,
3576 ret = ocfs2_half_xattr_cluster(inode, handle,
3580 if ((*header_bh)->b_blocknr == last_blk && extend)
3589 * Add a new cluster for xattr storage.
3591 * If the new cluster is contiguous with the previous one, it will be
3592 * appended to the same extent record, and num_clusters will be updated.
3593 * If not, we will insert a new extent for it and move some xattrs in
3594 * the last cluster into the new allocated one.
3595 * We also need to limit the maximum size of a btree leaf, otherwise we'll
3596 * lose the benefits of hashing because we'll have to search large leaves.
3597 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
3600 * first_bh is the first block of the previous extent rec and header_bh
3601 * indicates the bucket we will insert the new xattrs. They will be updated
3602 * when the header_bh is moved into the new cluster.
3604 static int ocfs2_add_new_xattr_cluster(struct inode *inode,
3605 struct buffer_head *root_bh,
3606 struct buffer_head **first_bh,
3607 struct buffer_head **header_bh,
3614 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3615 u32 prev_clusters = *num_clusters;
3616 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
3618 handle_t *handle = NULL;
3619 struct ocfs2_alloc_context *data_ac = NULL;
3620 struct ocfs2_alloc_context *meta_ac = NULL;
3621 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3622 struct ocfs2_extent_tree et;
3624 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
3625 "previous xattr blkno = %llu\n",
3626 (unsigned long long)OCFS2_I(inode)->ip_blkno,
3627 prev_cpos, prev_blkno);
3629 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
3631 ret = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
3632 &data_ac, &meta_ac);
3638 credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
3640 handle = ocfs2_start_trans(osb, credits);
3641 if (IS_ERR(handle)) {
3642 ret = PTR_ERR(handle);
3648 ret = ocfs2_journal_access(handle, inode, root_bh,
3649 OCFS2_JOURNAL_ACCESS_WRITE);
3655 ret = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
3656 clusters_to_add, &bit_off, &num_bits);
3663 BUG_ON(num_bits > clusters_to_add);
3665 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
3666 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
3667 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
3669 if (prev_blkno + prev_clusters * bpc == block &&
3670 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
3671 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
3673 * If this cluster is contiguous with the old one and
3674 * adding this new cluster, we don't surpass the limit of
3675 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
3676 * initialized and used like other buckets in the previous
3678 * So add it as a contiguous one. The caller will handle
3681 v_start = prev_cpos + prev_clusters;
3682 *num_clusters = prev_clusters + num_bits;
3683 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
3686 ret = ocfs2_adjust_xattr_cross_cluster(inode,
3701 if (handle->h_buffer_credits < credits) {
3703 * The journal has been restarted before, and don't
3704 * have enough space for the insertion, so extend it
3707 ret = ocfs2_extend_trans(handle, credits);
3713 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
3714 num_bits, block, v_start);
3715 ret = ocfs2_insert_extent(osb, handle, inode, &et, v_start, block,
3716 num_bits, 0, meta_ac);
3722 ret = ocfs2_journal_dirty(handle, root_bh);
3730 ocfs2_commit_trans(osb, handle);
3732 ocfs2_free_alloc_context(data_ac);
3734 ocfs2_free_alloc_context(meta_ac);
3740 * Extend a new xattr bucket and move xattrs to the end one by one until
3741 * We meet with start_bh. Only move half of the xattrs to the bucket after it.
3743 static int ocfs2_extend_xattr_bucket(struct inode *inode,
3744 struct buffer_head *first_bh,
3745 struct buffer_head *start_bh,
3749 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3750 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3751 u64 start_blk = start_bh->b_blocknr, end_blk;
3752 u32 num_buckets = num_clusters * ocfs2_xattr_buckets_per_cluster(osb);
3754 struct ocfs2_xattr_header *first_xh =
3755 (struct ocfs2_xattr_header *)first_bh->b_data;
3756 u16 bucket = le16_to_cpu(first_xh->xh_num_buckets);
3758 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
3759 "from %llu, len = %u\n", start_blk,
3760 (unsigned long long)first_bh->b_blocknr, num_clusters);
3762 BUG_ON(bucket >= num_buckets);
3764 end_blk = first_bh->b_blocknr + (bucket - 1) * blk_per_bucket;
3767 * We will touch all the buckets after the start_bh(include it).
3768 * Add one more bucket and modify the first_bh.
3770 credits = end_blk - start_blk + 2 * blk_per_bucket + 1;
3771 handle = ocfs2_start_trans(osb, credits);
3772 if (IS_ERR(handle)) {
3773 ret = PTR_ERR(handle);
3779 ret = ocfs2_journal_access(handle, inode, first_bh,
3780 OCFS2_JOURNAL_ACCESS_WRITE);
3786 while (end_blk != start_blk) {
3787 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
3788 end_blk + blk_per_bucket, 0);
3791 end_blk -= blk_per_bucket;
3794 /* Move half of the xattr in start_blk to the next bucket. */
3795 ret = ocfs2_half_xattr_bucket(inode, handle, start_blk,
3796 start_blk + blk_per_bucket, NULL, 0);
3798 le16_add_cpu(&first_xh->xh_num_buckets, 1);
3799 ocfs2_journal_dirty(handle, first_bh);
3802 ocfs2_commit_trans(osb, handle);
3808 * Add new xattr bucket in an extent record and adjust the buckets accordingly.
3809 * xb_bh is the ocfs2_xattr_block.
3810 * We will move all the buckets starting from header_bh to the next place. As
3811 * for this one, half num of its xattrs will be moved to the next one.
3813 * We will allocate a new cluster if current cluster is full and adjust
3814 * header_bh and first_bh if the insert place is moved to the new cluster.
3816 static int ocfs2_add_new_xattr_bucket(struct inode *inode,
3817 struct buffer_head *xb_bh,
3818 struct buffer_head *header_bh)
3820 struct ocfs2_xattr_header *first_xh = NULL;
3821 struct buffer_head *first_bh = NULL;
3822 struct ocfs2_xattr_block *xb =
3823 (struct ocfs2_xattr_block *)xb_bh->b_data;
3824 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3825 struct ocfs2_extent_list *el = &xb_root->xt_list;
3826 struct ocfs2_xattr_header *xh =
3827 (struct ocfs2_xattr_header *)header_bh->b_data;
3828 u32 name_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3829 struct super_block *sb = inode->i_sb;
3830 struct ocfs2_super *osb = OCFS2_SB(sb);
3831 int ret, num_buckets, extend = 1;
3833 u32 e_cpos, num_clusters;
3835 mlog(0, "Add new xattr bucket starting form %llu\n",
3836 (unsigned long long)header_bh->b_blocknr);
3839 * Add refrence for header_bh here because it may be
3840 * changed in ocfs2_add_new_xattr_cluster and we need
3841 * to free it in the end.
3845 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
3852 ret = ocfs2_read_block(inode, p_blkno, &first_bh);
3858 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
3859 first_xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3861 if (num_buckets == le16_to_cpu(first_xh->xh_num_buckets)) {
3862 ret = ocfs2_add_new_xattr_cluster(inode,
3877 ret = ocfs2_extend_xattr_bucket(inode,
3889 static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
3890 struct ocfs2_xattr_bucket *bucket,
3893 int block_off = offs >> inode->i_sb->s_blocksize_bits;
3895 offs = offs % inode->i_sb->s_blocksize;
3896 return bucket->bhs[block_off]->b_data + offs;
3900 * Handle the normal xattr set, including replace, delete and new.
3902 * Note: "local" indicates the real data's locality. So we can't
3903 * just its bucket locality by its length.
3905 static void ocfs2_xattr_set_entry_normal(struct inode *inode,
3906 struct ocfs2_xattr_info *xi,
3907 struct ocfs2_xattr_search *xs,
3911 struct ocfs2_xattr_entry *last, *xe;
3912 int name_len = strlen(xi->name);
3913 struct ocfs2_xattr_header *xh = xs->header;
3914 u16 count = le16_to_cpu(xh->xh_count), start;
3915 size_t blocksize = inode->i_sb->s_blocksize;
3917 size_t offs, size, new_size;
3919 last = &xh->xh_entries[count];
3920 if (!xs->not_found) {
3922 offs = le16_to_cpu(xe->xe_name_offset);
3923 if (ocfs2_xattr_is_local(xe))
3924 size = OCFS2_XATTR_SIZE(name_len) +
3925 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3927 size = OCFS2_XATTR_SIZE(name_len) +
3928 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
3931 * If the new value will be stored outside, xi->value has been
3932 * initalized as an empty ocfs2_xattr_value_root, and the same
3933 * goes with xi->value_len, so we can set new_size safely here.
3934 * See ocfs2_xattr_set_in_bucket.
3936 new_size = OCFS2_XATTR_SIZE(name_len) +
3937 OCFS2_XATTR_SIZE(xi->value_len);
3939 le16_add_cpu(&xh->xh_name_value_len, -size);
3941 if (new_size > size)
3942 goto set_new_name_value;
3944 /* Now replace the old value with new one. */
3946 xe->xe_value_size = cpu_to_le64(xi->value_len);
3948 xe->xe_value_size = 0;
3950 val = ocfs2_xattr_bucket_get_val(inode,
3952 memset(val + OCFS2_XATTR_SIZE(name_len), 0,
3953 size - OCFS2_XATTR_SIZE(name_len));
3954 if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
3955 memcpy(val + OCFS2_XATTR_SIZE(name_len),
3956 xi->value, xi->value_len);
3958 le16_add_cpu(&xh->xh_name_value_len, new_size);
3959 ocfs2_xattr_set_local(xe, local);
3963 * Remove the old entry if there is more than one.
3964 * We don't remove the last entry so that we can
3965 * use it to indicate the hash value of the empty
3969 le16_add_cpu(&xh->xh_count, -1);
3972 (void *)last - (void *)xe);
3974 sizeof(struct ocfs2_xattr_entry));
3977 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3982 /* find a new entry for insert. */
3983 int low = 0, high = count - 1, tmp;
3984 struct ocfs2_xattr_entry *tmp_xe;
3986 while (low <= high && count) {
3987 tmp = (low + high) / 2;
3988 tmp_xe = &xh->xh_entries[tmp];
3990 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
3992 else if (name_hash <
3993 le32_to_cpu(tmp_xe->xe_name_hash))
4001 xe = &xh->xh_entries[low];
4003 memmove(xe + 1, xe, (void *)last - (void *)xe);
4005 le16_add_cpu(&xh->xh_count, 1);
4006 memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
4007 xe->xe_name_hash = cpu_to_le32(name_hash);
4008 xe->xe_name_len = name_len;
4009 ocfs2_xattr_set_type(xe, xi->name_index);
4013 /* Insert the new name+value. */
4014 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
4017 * We must make sure that the name/value pair
4018 * exists in the same block.
4020 offs = le16_to_cpu(xh->xh_free_start);
4021 start = offs - size;
4023 if (start >> inode->i_sb->s_blocksize_bits !=
4024 (offs - 1) >> inode->i_sb->s_blocksize_bits) {
4025 offs = offs - offs % blocksize;
4026 xh->xh_free_start = cpu_to_le16(offs);
4029 val = ocfs2_xattr_bucket_get_val(inode,
4030 &xs->bucket, offs - size);
4031 xe->xe_name_offset = cpu_to_le16(offs - size);
4033 memset(val, 0, size);
4034 memcpy(val, xi->name, name_len);
4035 memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
4037 xe->xe_value_size = cpu_to_le64(xi->value_len);
4038 ocfs2_xattr_set_local(xe, local);
4040 le16_add_cpu(&xh->xh_free_start, -size);
4041 le16_add_cpu(&xh->xh_name_value_len, size);
4046 static int ocfs2_xattr_bucket_handle_journal(struct inode *inode,
4048 struct ocfs2_xattr_search *xs,
4049 struct buffer_head **bhs,
4052 int ret = 0, off, block_off;
4053 struct ocfs2_xattr_entry *xe = xs->here;
4056 * First calculate all the blocks we should journal_access
4057 * and journal_dirty. The first block should always be touched.
4059 ret = ocfs2_journal_dirty(handle, bhs[0]);
4063 /* calc the data. */
4064 off = le16_to_cpu(xe->xe_name_offset);
4065 block_off = off >> inode->i_sb->s_blocksize_bits;
4066 ret = ocfs2_journal_dirty(handle, bhs[block_off]);
4074 * Set the xattr entry in the specified bucket.
4075 * The bucket is indicated by xs->bucket and it should have the enough
4076 * space for the xattr insertion.
4078 static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
4079 struct ocfs2_xattr_info *xi,
4080 struct ocfs2_xattr_search *xs,
4085 handle_t *handle = NULL;
4086 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4087 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4089 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4090 (unsigned long)xi->value_len, xi->name_index,
4091 (unsigned long long)xs->bucket.bhs[0]->b_blocknr);
4093 if (!xs->bucket.bhs[1]) {
4094 ret = ocfs2_read_blocks(inode,
4095 xs->bucket.bhs[0]->b_blocknr + 1,
4096 blk_per_bucket - 1, &xs->bucket.bhs[1],
4104 handle = ocfs2_start_trans(osb, blk_per_bucket);
4105 if (IS_ERR(handle)) {
4106 ret = PTR_ERR(handle);
4112 for (i = 0; i < blk_per_bucket; i++) {
4113 ret = ocfs2_journal_access(handle, inode, xs->bucket.bhs[i],
4114 OCFS2_JOURNAL_ACCESS_WRITE);
4121 ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
4123 /*Only dirty the blocks we have touched in set xattr. */
4124 ret = ocfs2_xattr_bucket_handle_journal(inode, handle, xs,
4125 xs->bucket.bhs, blk_per_bucket);
4129 ocfs2_commit_trans(osb, handle);
4134 static int ocfs2_xattr_value_update_size(struct inode *inode,
4135 struct buffer_head *xe_bh,
4136 struct ocfs2_xattr_entry *xe,
4140 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4141 handle_t *handle = NULL;
4143 handle = ocfs2_start_trans(osb, 1);
4144 if (handle == NULL) {
4150 ret = ocfs2_journal_access(handle, inode, xe_bh,
4151 OCFS2_JOURNAL_ACCESS_WRITE);
4157 xe->xe_value_size = cpu_to_le64(new_size);
4159 ret = ocfs2_journal_dirty(handle, xe_bh);
4164 ocfs2_commit_trans(osb, handle);
4170 * Truncate the specified xe_off entry in xattr bucket.
4171 * bucket is indicated by header_bh and len is the new length.
4172 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4174 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4176 static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
4177 struct buffer_head *header_bh,
4183 struct buffer_head *value_bh = NULL;
4184 struct ocfs2_xattr_value_root *xv;
4185 struct ocfs2_xattr_entry *xe;
4186 struct ocfs2_xattr_header *xh =
4187 (struct ocfs2_xattr_header *)header_bh->b_data;
4188 size_t blocksize = inode->i_sb->s_blocksize;
4190 xe = &xh->xh_entries[xe_off];
4192 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
4194 offset = le16_to_cpu(xe->xe_name_offset) +
4195 OCFS2_XATTR_SIZE(xe->xe_name_len);
4197 value_blk = offset / blocksize;
4199 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4200 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
4201 value_blk += header_bh->b_blocknr;
4203 ret = ocfs2_read_block(inode, value_blk, &value_bh);
4209 xv = (struct ocfs2_xattr_value_root *)
4210 (value_bh->b_data + offset % blocksize);
4212 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
4213 xe_off, (unsigned long long)header_bh->b_blocknr, len);
4214 ret = ocfs2_xattr_value_truncate(inode, value_bh, xv, len);
4220 ret = ocfs2_xattr_value_update_size(inode, header_bh, xe, len);
4231 static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
4232 struct ocfs2_xattr_search *xs,
4236 struct ocfs2_xattr_entry *xe = xs->here;
4237 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
4239 BUG_ON(!xs->bucket.bhs[0] || !xe || ocfs2_xattr_is_local(xe));
4241 offset = xe - xh->xh_entries;
4242 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket.bhs[0],
4250 static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
4251 struct ocfs2_xattr_search *xs,
4256 struct ocfs2_xattr_value_root *xv;
4257 struct ocfs2_xattr_entry *xe = xs->here;
4259 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
4261 offset = le16_to_cpu(xe->xe_name_offset) +
4262 OCFS2_XATTR_SIZE(xe->xe_name_len);
4264 xv = (struct ocfs2_xattr_value_root *)(xs->base + offset);
4266 return __ocfs2_xattr_set_value_outside(inode, xv, val, value_len);
4269 static int ocfs2_rm_xattr_cluster(struct inode *inode,
4270 struct buffer_head *root_bh,
4276 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4277 struct inode *tl_inode = osb->osb_tl_inode;
4279 struct ocfs2_xattr_block *xb =
4280 (struct ocfs2_xattr_block *)root_bh->b_data;
4281 struct ocfs2_alloc_context *meta_ac = NULL;
4282 struct ocfs2_cached_dealloc_ctxt dealloc;
4283 struct ocfs2_extent_tree et;
4285 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
4287 ocfs2_init_dealloc_ctxt(&dealloc);
4289 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
4290 cpos, len, (unsigned long long)blkno);
4292 ocfs2_remove_xattr_clusters_from_cache(inode, blkno, len);
4294 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
4300 mutex_lock(&tl_inode->i_mutex);
4302 if (ocfs2_truncate_log_needs_flush(osb)) {
4303 ret = __ocfs2_flush_truncate_log(osb);
4310 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
4311 if (handle == NULL) {
4317 ret = ocfs2_journal_access(handle, inode, root_bh,
4318 OCFS2_JOURNAL_ACCESS_WRITE);
4324 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
4331 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
4333 ret = ocfs2_journal_dirty(handle, root_bh);
4339 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
4344 ocfs2_commit_trans(osb, handle);
4346 ocfs2_schedule_truncate_log_flush(osb, 1);
4348 mutex_unlock(&tl_inode->i_mutex);
4351 ocfs2_free_alloc_context(meta_ac);
4353 ocfs2_run_deallocs(osb, &dealloc);
4358 static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
4359 struct ocfs2_xattr_search *xs)
4361 handle_t *handle = NULL;
4362 struct ocfs2_xattr_header *xh = xs->bucket.xh;
4363 struct ocfs2_xattr_entry *last = &xh->xh_entries[
4364 le16_to_cpu(xh->xh_count) - 1];
4367 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), 1);
4368 if (IS_ERR(handle)) {
4369 ret = PTR_ERR(handle);
4374 ret = ocfs2_journal_access(handle, inode, xs->bucket.bhs[0],
4375 OCFS2_JOURNAL_ACCESS_WRITE);
4381 /* Remove the old entry. */
4382 memmove(xs->here, xs->here + 1,
4383 (void *)last - (void *)xs->here);
4384 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
4385 le16_add_cpu(&xh->xh_count, -1);
4387 ret = ocfs2_journal_dirty(handle, xs->bucket.bhs[0]);
4391 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
4395 * Set the xattr name/value in the bucket specified in xs.
4397 * As the new value in xi may be stored in the bucket or in an outside cluster,
4398 * we divide the whole process into 3 steps:
4399 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
4400 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
4401 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
4402 * 4. If the clusters for the new outside value can't be allocated, we need
4403 * to free the xattr we allocated in set.
4405 static int ocfs2_xattr_set_in_bucket(struct inode *inode,
4406 struct ocfs2_xattr_info *xi,
4407 struct ocfs2_xattr_search *xs)
4411 char *val = (char *)xi->value;
4412 struct ocfs2_xattr_entry *xe = xs->here;
4413 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name,
4416 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
4418 * We need to truncate the xattr storage first.
4420 * If both the old and new value are stored to
4421 * outside block, we only need to truncate
4422 * the storage and then set the value outside.
4424 * If the new value should be stored within block,
4425 * we should free all the outside block first and
4426 * the modification to the xattr block will be done
4427 * by following steps.
4429 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4430 value_len = xi->value_len;
4434 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4440 goto set_value_outside;
4443 value_len = xi->value_len;
4444 /* So we have to handle the inside block change now. */
4445 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
4447 * If the new value will be stored outside of block,
4448 * initalize a new empty value root and insert it first.
4451 xi->value = &def_xv;
4452 xi->value_len = OCFS2_XATTR_ROOT_SIZE;
4455 ret = ocfs2_xattr_set_entry_in_bucket(inode, xi, xs, name_hash, local);
4461 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
4464 /* allocate the space now for the outside block storage. */
4465 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4470 if (xs->not_found) {
4472 * We can't allocate enough clusters for outside
4473 * storage and we have allocated xattr already,
4474 * so need to remove it.
4476 ocfs2_xattr_bucket_remove_xs(inode, xs);
4482 ret = ocfs2_xattr_bucket_set_value_outside(inode, xs, val, value_len);
4487 /* check whether the xattr bucket is filled up with the same hash value. */
4488 static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
4489 struct ocfs2_xattr_bucket *bucket)
4491 struct ocfs2_xattr_header *xh = bucket->xh;
4493 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
4494 xh->xh_entries[0].xe_name_hash) {
4495 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
4497 (unsigned long long)bucket->bhs[0]->b_blocknr,
4498 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
4505 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
4506 struct ocfs2_xattr_info *xi,
4507 struct ocfs2_xattr_search *xs)
4509 struct ocfs2_xattr_header *xh;
4510 struct ocfs2_xattr_entry *xe;
4511 u16 count, header_size, xh_free_start;
4512 int i, free, max_free, need, old;
4513 size_t value_size = 0, name_len = strlen(xi->name);
4514 size_t blocksize = inode->i_sb->s_blocksize;
4515 int ret, allocation = 0;
4516 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4518 mlog_entry("Set xattr %s in xattr index block\n", xi->name);
4522 count = le16_to_cpu(xh->xh_count);
4523 xh_free_start = le16_to_cpu(xh->xh_free_start);
4524 header_size = sizeof(struct ocfs2_xattr_header) +
4525 count * sizeof(struct ocfs2_xattr_entry);
4526 max_free = OCFS2_XATTR_BUCKET_SIZE -
4527 le16_to_cpu(xh->xh_name_value_len) - header_size;
4529 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
4530 "of %u which exceed block size\n",
4531 (unsigned long long)xs->bucket.bhs[0]->b_blocknr,
4534 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4535 value_size = OCFS2_XATTR_ROOT_SIZE;
4537 value_size = OCFS2_XATTR_SIZE(xi->value_len);
4540 need = sizeof(struct ocfs2_xattr_entry) +
4541 OCFS2_XATTR_SIZE(name_len) + value_size;
4543 need = value_size + OCFS2_XATTR_SIZE(name_len);
4546 * We only replace the old value if the new length is smaller
4547 * than the old one. Otherwise we will allocate new space in the
4548 * bucket to store it.
4551 if (ocfs2_xattr_is_local(xe))
4552 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4554 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4556 if (old >= value_size)
4560 free = xh_free_start - header_size;
4562 * We need to make sure the new name/value pair
4563 * can exist in the same block.
4565 if (xh_free_start % blocksize < need)
4566 free -= xh_free_start % blocksize;
4568 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
4569 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
4570 " %u\n", xs->not_found,
4571 (unsigned long long)xs->bucket.bhs[0]->b_blocknr,
4572 free, need, max_free, le16_to_cpu(xh->xh_free_start),
4573 le16_to_cpu(xh->xh_name_value_len));
4575 if (free < need || count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4576 if (need <= max_free &&
4577 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4579 * We can create the space by defragment. Since only the
4580 * name/value will be moved, the xe shouldn't be changed
4583 ret = ocfs2_defrag_xattr_bucket(inode, &xs->bucket);
4589 xh_free_start = le16_to_cpu(xh->xh_free_start);
4590 free = xh_free_start - header_size;
4591 if (xh_free_start % blocksize < need)
4592 free -= xh_free_start % blocksize;
4597 mlog(0, "Can't get enough space for xattr insert by "
4598 "defragment. Need %u bytes, but we have %d, so "
4599 "allocate new bucket for it.\n", need, free);
4603 * We have to add new buckets or clusters and one
4604 * allocation should leave us enough space for insert.
4609 * We do not allow for overlapping ranges between buckets. And
4610 * the maximum number of collisions we will allow for then is
4611 * one bucket's worth, so check it here whether we need to
4612 * add a new bucket for the insert.
4614 ret = ocfs2_check_xattr_bucket_collision(inode, &xs->bucket);
4620 ret = ocfs2_add_new_xattr_bucket(inode,
4628 for (i = 0; i < blk_per_bucket; i++)
4629 brelse(xs->bucket.bhs[i]);
4631 memset(&xs->bucket, 0, sizeof(xs->bucket));
4633 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
4636 if (ret && ret != -ENODATA)
4638 xs->not_found = ret;
4644 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs);
4650 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
4651 struct ocfs2_xattr_bucket *bucket,
4655 struct ocfs2_xattr_header *xh = bucket->xh;
4657 struct ocfs2_xattr_entry *xe;
4659 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4660 xe = &xh->xh_entries[i];
4661 if (ocfs2_xattr_is_local(xe))
4664 ret = ocfs2_xattr_bucket_value_truncate(inode,
4676 static int ocfs2_delete_xattr_index_block(struct inode *inode,
4677 struct buffer_head *xb_bh)
4679 struct ocfs2_xattr_block *xb =
4680 (struct ocfs2_xattr_block *)xb_bh->b_data;
4681 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
4683 u32 name_hash = UINT_MAX, e_cpos, num_clusters;
4686 if (le16_to_cpu(el->l_next_free_rec) == 0)
4689 while (name_hash > 0) {
4690 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
4691 &e_cpos, &num_clusters, el);
4697 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
4698 ocfs2_delete_xattr_in_bucket,
4705 ret = ocfs2_rm_xattr_cluster(inode, xb_bh,
4706 p_blkno, e_cpos, num_clusters);
4715 name_hash = e_cpos - 1;
4723 * 'trusted' attributes support
4726 #define XATTR_TRUSTED_PREFIX "trusted."
4728 static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list,
4729 size_t list_size, const char *name,
4732 const size_t prefix_len = sizeof(XATTR_TRUSTED_PREFIX) - 1;
4733 const size_t total_len = prefix_len + name_len + 1;
4735 if (list && total_len <= list_size) {
4736 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
4737 memcpy(list + prefix_len, name, name_len);
4738 list[prefix_len + name_len] = '\0';
4743 static int ocfs2_xattr_trusted_get(struct inode *inode, const char *name,
4744 void *buffer, size_t size)
4746 if (strcmp(name, "") == 0)
4748 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, name,
4752 static int ocfs2_xattr_trusted_set(struct inode *inode, const char *name,
4753 const void *value, size_t size, int flags)
4755 if (strcmp(name, "") == 0)
4758 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, name, value,
4762 struct xattr_handler ocfs2_xattr_trusted_handler = {
4763 .prefix = XATTR_TRUSTED_PREFIX,
4764 .list = ocfs2_xattr_trusted_list,
4765 .get = ocfs2_xattr_trusted_get,
4766 .set = ocfs2_xattr_trusted_set,
4771 * 'user' attributes support
4774 #define XATTR_USER_PREFIX "user."
4776 static size_t ocfs2_xattr_user_list(struct inode *inode, char *list,
4777 size_t list_size, const char *name,
4780 const size_t prefix_len = sizeof(XATTR_USER_PREFIX) - 1;
4781 const size_t total_len = prefix_len + name_len + 1;
4782 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4784 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4787 if (list && total_len <= list_size) {
4788 memcpy(list, XATTR_USER_PREFIX, prefix_len);
4789 memcpy(list + prefix_len, name, name_len);
4790 list[prefix_len + name_len] = '\0';
4795 static int ocfs2_xattr_user_get(struct inode *inode, const char *name,
4796 void *buffer, size_t size)
4798 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4800 if (strcmp(name, "") == 0)
4802 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4804 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name,
4808 static int ocfs2_xattr_user_set(struct inode *inode, const char *name,
4809 const void *value, size_t size, int flags)
4811 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4813 if (strcmp(name, "") == 0)
4815 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4818 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, name, value,
4822 struct xattr_handler ocfs2_xattr_user_handler = {
4823 .prefix = XATTR_USER_PREFIX,
4824 .list = ocfs2_xattr_user_list,
4825 .get = ocfs2_xattr_user_get,
4826 .set = ocfs2_xattr_user_set,