1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Copyright (C) 2008 Oracle. All rights reserved.
9 * Lots of code in this file is taken from ext3.
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public
22 * License along with this program; if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 021110-1307, USA.
27 #include <linux/capability.h>
29 #include <linux/types.h>
30 #include <linux/slab.h>
31 #include <linux/highmem.h>
32 #include <linux/pagemap.h>
33 #include <linux/uio.h>
34 #include <linux/sched.h>
35 #include <linux/splice.h>
36 #include <linux/mount.h>
37 #include <linux/writeback.h>
38 #include <linux/falloc.h>
40 #define MLOG_MASK_PREFIX ML_XATTR
41 #include <cluster/masklog.h>
54 #include "buffer_head_io.h"
58 struct ocfs2_xattr_def_value_root {
59 struct ocfs2_xattr_value_root xv;
60 struct ocfs2_extent_rec er;
63 #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
64 #define OCFS2_XATTR_INLINE_SIZE 80
66 static struct ocfs2_xattr_def_value_root def_xv = {
67 .xv.xr_list.l_count = cpu_to_le16(1),
70 struct xattr_handler *ocfs2_xattr_handlers[] = {
71 &ocfs2_xattr_user_handler,
72 &ocfs2_xattr_trusted_handler,
76 static struct xattr_handler *ocfs2_xattr_handler_map[] = {
77 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
78 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
81 struct ocfs2_xattr_info {
88 struct ocfs2_xattr_search {
89 struct buffer_head *inode_bh;
91 * xattr_bh point to the block buffer head which has extended attribute
92 * when extended attribute in inode, xattr_bh is equal to inode_bh.
94 struct buffer_head *xattr_bh;
95 struct ocfs2_xattr_header *header;
98 struct ocfs2_xattr_entry *here;
102 static inline struct xattr_handler *ocfs2_xattr_handler(int name_index)
104 struct xattr_handler *handler = NULL;
106 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
107 handler = ocfs2_xattr_handler_map[name_index];
112 static inline u32 ocfs2_xattr_name_hash(struct inode *inode,
118 /* Get hash value of uuid from super block */
119 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
122 /* hash extended attribute prefix */
123 for (i = 0; i < prefix_len; i++) {
124 hash = (hash << OCFS2_HASH_SHIFT) ^
125 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
128 /* hash extended attribute name */
129 for (i = 0; i < name_len; i++) {
130 hash = (hash << OCFS2_HASH_SHIFT) ^
131 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
139 * ocfs2_xattr_hash_entry()
141 * Compute the hash of an extended attribute.
143 static void ocfs2_xattr_hash_entry(struct inode *inode,
144 struct ocfs2_xattr_header *header,
145 struct ocfs2_xattr_entry *entry)
148 struct xattr_handler *handler =
149 ocfs2_xattr_handler(ocfs2_xattr_get_type(entry));
150 char *prefix = handler->prefix;
151 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
152 int prefix_len = strlen(handler->prefix);
154 hash = ocfs2_xattr_name_hash(inode, prefix, prefix_len, name,
156 entry->xe_name_hash = cpu_to_le32(hash);
161 static int ocfs2_xattr_extend_allocation(struct inode *inode,
163 struct buffer_head *xattr_bh,
164 struct ocfs2_xattr_value_root *xv)
167 int restart_func = 0;
169 handle_t *handle = NULL;
170 struct ocfs2_alloc_context *data_ac = NULL;
171 struct ocfs2_alloc_context *meta_ac = NULL;
172 enum ocfs2_alloc_restarted why;
173 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
174 struct ocfs2_extent_list *root_el = &xv->xr_list;
175 u32 prev_clusters, logical_start = le32_to_cpu(xv->xr_clusters);
177 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
181 status = ocfs2_lock_allocators(inode, xattr_bh, root_el,
182 clusters_to_add, 0, &data_ac,
183 &meta_ac, OCFS2_XATTR_VALUE_EXTENT, xv);
189 credits = ocfs2_calc_extend_credits(osb->sb, root_el, clusters_to_add);
190 handle = ocfs2_start_trans(osb, credits);
191 if (IS_ERR(handle)) {
192 status = PTR_ERR(handle);
198 restarted_transaction:
199 status = ocfs2_journal_access(handle, inode, xattr_bh,
200 OCFS2_JOURNAL_ACCESS_WRITE);
206 prev_clusters = le32_to_cpu(xv->xr_clusters);
207 status = ocfs2_add_clusters_in_btree(osb,
218 OCFS2_XATTR_VALUE_EXTENT,
220 if ((status < 0) && (status != -EAGAIN)) {
221 if (status != -ENOSPC)
226 status = ocfs2_journal_dirty(handle, xattr_bh);
232 clusters_to_add -= le32_to_cpu(xv->xr_clusters) - prev_clusters;
234 if (why != RESTART_NONE && clusters_to_add) {
235 if (why == RESTART_META) {
236 mlog(0, "restarting function.\n");
239 BUG_ON(why != RESTART_TRANS);
241 mlog(0, "restarting transaction.\n");
242 /* TODO: This can be more intelligent. */
243 credits = ocfs2_calc_extend_credits(osb->sb,
246 status = ocfs2_extend_trans(handle, credits);
248 /* handle still has to be committed at
254 goto restarted_transaction;
260 ocfs2_commit_trans(osb, handle);
264 ocfs2_free_alloc_context(data_ac);
268 ocfs2_free_alloc_context(meta_ac);
271 if ((!status) && restart_func) {
279 static int __ocfs2_remove_xattr_range(struct inode *inode,
280 struct buffer_head *root_bh,
281 struct ocfs2_xattr_value_root *xv,
282 u32 cpos, u32 phys_cpos, u32 len,
283 struct ocfs2_cached_dealloc_ctxt *dealloc)
286 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
287 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
288 struct inode *tl_inode = osb->osb_tl_inode;
290 struct ocfs2_alloc_context *meta_ac = NULL;
292 ret = ocfs2_lock_allocators(inode, root_bh, &xv->xr_list,
293 0, 1, NULL, &meta_ac,
294 OCFS2_XATTR_VALUE_EXTENT, xv);
300 mutex_lock(&tl_inode->i_mutex);
302 if (ocfs2_truncate_log_needs_flush(osb)) {
303 ret = __ocfs2_flush_truncate_log(osb);
310 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
311 if (IS_ERR(handle)) {
312 ret = PTR_ERR(handle);
317 ret = ocfs2_journal_access(handle, inode, root_bh,
318 OCFS2_JOURNAL_ACCESS_WRITE);
324 ret = ocfs2_remove_extent(inode, root_bh, cpos, len, handle, meta_ac,
325 dealloc, OCFS2_XATTR_VALUE_EXTENT, xv);
331 le32_add_cpu(&xv->xr_clusters, -len);
333 ret = ocfs2_journal_dirty(handle, root_bh);
339 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
344 ocfs2_commit_trans(osb, handle);
346 mutex_unlock(&tl_inode->i_mutex);
349 ocfs2_free_alloc_context(meta_ac);
354 static int ocfs2_xattr_shrink_size(struct inode *inode,
357 struct buffer_head *root_bh,
358 struct ocfs2_xattr_value_root *xv)
361 u32 trunc_len, cpos, phys_cpos, alloc_size;
363 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
364 struct ocfs2_cached_dealloc_ctxt dealloc;
366 ocfs2_init_dealloc_ctxt(&dealloc);
368 if (old_clusters <= new_clusters)
372 trunc_len = old_clusters - new_clusters;
374 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
375 &alloc_size, &xv->xr_list);
381 if (alloc_size > trunc_len)
382 alloc_size = trunc_len;
384 ret = __ocfs2_remove_xattr_range(inode, root_bh, xv, cpos,
385 phys_cpos, alloc_size,
392 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
393 ocfs2_remove_xattr_clusters_from_cache(inode, block,
396 trunc_len -= alloc_size;
400 ocfs2_schedule_truncate_log_flush(osb, 1);
401 ocfs2_run_deallocs(osb, &dealloc);
406 static int ocfs2_xattr_value_truncate(struct inode *inode,
407 struct buffer_head *root_bh,
408 struct ocfs2_xattr_value_root *xv,
412 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
413 u32 old_clusters = le32_to_cpu(xv->xr_clusters);
415 if (new_clusters == old_clusters)
418 if (new_clusters > old_clusters)
419 ret = ocfs2_xattr_extend_allocation(inode,
420 new_clusters - old_clusters,
423 ret = ocfs2_xattr_shrink_size(inode,
424 old_clusters, new_clusters,
430 static int ocfs2_xattr_list_entries(struct inode *inode,
431 struct ocfs2_xattr_header *header,
432 char *buffer, size_t buffer_size)
434 size_t rest = buffer_size;
437 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
438 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
439 struct xattr_handler *handler =
440 ocfs2_xattr_handler(ocfs2_xattr_get_type(entry));
443 size_t size = handler->list(inode, buffer, rest,
445 le16_to_cpu(entry->xe_name_offset)),
456 return buffer_size - rest;
459 static int ocfs2_xattr_ibody_list(struct inode *inode,
460 struct ocfs2_dinode *di,
464 struct ocfs2_xattr_header *header = NULL;
465 struct ocfs2_inode_info *oi = OCFS2_I(inode);
468 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
471 header = (struct ocfs2_xattr_header *)
472 ((void *)di + inode->i_sb->s_blocksize -
473 le16_to_cpu(di->i_xattr_inline_size));
475 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
480 static int ocfs2_xattr_block_list(struct inode *inode,
481 struct ocfs2_dinode *di,
485 struct buffer_head *blk_bh = NULL;
486 struct ocfs2_xattr_header *header = NULL;
489 if (!di->i_xattr_loc)
492 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
493 le64_to_cpu(di->i_xattr_loc),
494 &blk_bh, OCFS2_BH_CACHED, inode);
499 /*Verify the signature of xattr block*/
500 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
501 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
506 header = &((struct ocfs2_xattr_block *)blk_bh->b_data)->
509 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
516 ssize_t ocfs2_listxattr(struct dentry *dentry,
520 int ret = 0, i_ret = 0, b_ret = 0;
521 struct buffer_head *di_bh = NULL;
522 struct ocfs2_dinode *di = NULL;
523 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
525 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
528 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
534 di = (struct ocfs2_dinode *)di_bh->b_data;
536 down_read(&oi->ip_xattr_sem);
537 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
545 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
550 up_read(&oi->ip_xattr_sem);
551 ocfs2_inode_unlock(dentry->d_inode, 0);
555 return i_ret + b_ret;
558 static int ocfs2_xattr_find_entry(int name_index,
560 struct ocfs2_xattr_search *xs)
562 struct ocfs2_xattr_entry *entry;
569 name_len = strlen(name);
571 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
572 cmp = name_index - ocfs2_xattr_get_type(entry);
574 cmp = name_len - entry->xe_name_len;
576 cmp = memcmp(name, (xs->base +
577 le16_to_cpu(entry->xe_name_offset)),
585 return cmp ? -ENODATA : 0;
588 static int ocfs2_xattr_get_value_outside(struct inode *inode,
589 struct ocfs2_xattr_search *xs,
593 u32 cpos, p_cluster, num_clusters, bpc, clusters;
596 size_t cplen, blocksize;
597 struct buffer_head *bh = NULL;
598 struct ocfs2_xattr_value_root *xv;
599 struct ocfs2_extent_list *el;
601 xv = (struct ocfs2_xattr_value_root *)
602 (xs->base + le16_to_cpu(xs->here->xe_name_offset) +
603 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
605 clusters = le32_to_cpu(xv->xr_clusters);
606 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
607 blocksize = inode->i_sb->s_blocksize;
610 while (cpos < clusters) {
611 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
618 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
619 /* Copy ocfs2_xattr_value */
620 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
621 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
622 &bh, OCFS2_BH_CACHED, inode);
628 cplen = len >= blocksize ? blocksize : len;
629 memcpy(buffer, bh->b_data, cplen);
638 cpos += num_clusters;
644 static int ocfs2_xattr_ibody_get(struct inode *inode,
649 struct ocfs2_xattr_search *xs)
651 struct ocfs2_inode_info *oi = OCFS2_I(inode);
652 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
656 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
659 xs->end = (void *)di + inode->i_sb->s_blocksize;
660 xs->header = (struct ocfs2_xattr_header *)
661 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
662 xs->base = (void *)xs->header;
663 xs->here = xs->header->xh_entries;
665 ret = ocfs2_xattr_find_entry(name_index, name, xs);
668 size = le64_to_cpu(xs->here->xe_value_size);
670 if (size > buffer_size)
672 if (ocfs2_xattr_is_local(xs->here)) {
673 memcpy(buffer, (void *)xs->base +
674 le16_to_cpu(xs->here->xe_name_offset) +
675 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
677 ret = ocfs2_xattr_get_value_outside(inode, xs,
689 static int ocfs2_xattr_block_get(struct inode *inode,
694 struct ocfs2_xattr_search *xs)
696 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
697 struct buffer_head *blk_bh = NULL;
698 struct ocfs2_xattr_block *xb;
702 if (!di->i_xattr_loc)
705 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
706 le64_to_cpu(di->i_xattr_loc),
707 &blk_bh, OCFS2_BH_CACHED, inode);
712 /*Verify the signature of xattr block*/
713 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
714 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
719 xs->xattr_bh = blk_bh;
720 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
721 xs->header = &xb->xb_attrs.xb_header;
722 xs->base = (void *)xs->header;
723 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
724 xs->here = xs->header->xh_entries;
726 ret = ocfs2_xattr_find_entry(name_index, name, xs);
729 size = le64_to_cpu(xs->here->xe_value_size);
732 if (size > buffer_size)
734 if (ocfs2_xattr_is_local(xs->here)) {
735 memcpy(buffer, (void *)xs->base +
736 le16_to_cpu(xs->here->xe_name_offset) +
737 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
739 ret = ocfs2_xattr_get_value_outside(inode, xs,
756 * Copy an extended attribute into the buffer provided.
757 * Buffer is NULL to compute the size of buffer required.
759 int ocfs2_xattr_get(struct inode *inode,
766 struct ocfs2_dinode *di = NULL;
767 struct buffer_head *di_bh = NULL;
768 struct ocfs2_inode_info *oi = OCFS2_I(inode);
769 struct ocfs2_xattr_search xis = {
770 .not_found = -ENODATA,
772 struct ocfs2_xattr_search xbs = {
773 .not_found = -ENODATA,
776 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
779 ret = ocfs2_inode_lock(inode, &di_bh, 0);
784 xis.inode_bh = xbs.inode_bh = di_bh;
785 di = (struct ocfs2_dinode *)di_bh->b_data;
787 down_read(&oi->ip_xattr_sem);
788 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
791 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
793 up_read(&oi->ip_xattr_sem);
794 ocfs2_inode_unlock(inode, 0);
801 static int __ocfs2_xattr_set_value_outside(struct inode *inode,
802 struct ocfs2_xattr_value_root *xv,
806 int ret = 0, i, cp_len, credits;
807 u16 blocksize = inode->i_sb->s_blocksize;
808 u32 p_cluster, num_clusters;
809 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
810 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
812 struct buffer_head *bh = NULL;
815 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
817 credits = clusters * bpc;
818 handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb), credits);
819 if (IS_ERR(handle)) {
820 ret = PTR_ERR(handle);
825 while (cpos < clusters) {
826 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
827 &num_clusters, &xv->xr_list);
833 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
835 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
836 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
837 &bh, OCFS2_BH_CACHED, inode);
843 ret = ocfs2_journal_access(handle,
846 OCFS2_JOURNAL_ACCESS_WRITE);
852 cp_len = value_len > blocksize ? blocksize : value_len;
853 memcpy(bh->b_data, value, cp_len);
856 if (cp_len < blocksize)
857 memset(bh->b_data + cp_len, 0,
860 ret = ocfs2_journal_dirty(handle, bh);
869 * XXX: do we need to empty all the following
870 * blocks in this cluster?
875 cpos += num_clusters;
878 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
885 static int ocfs2_xattr_cleanup(struct inode *inode,
886 struct ocfs2_xattr_info *xi,
887 struct ocfs2_xattr_search *xs,
890 handle_t *handle = NULL;
892 size_t name_len = strlen(xi->name);
893 void *val = xs->base + offs;
894 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
896 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
897 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
898 if (IS_ERR(handle)) {
899 ret = PTR_ERR(handle);
903 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
904 OCFS2_JOURNAL_ACCESS_WRITE);
909 /* Decrease xattr count */
910 le16_add_cpu(&xs->header->xh_count, -1);
911 /* Remove the xattr entry and tree root which has already be set*/
912 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
913 memset(val, 0, size);
915 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
919 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
924 static int ocfs2_xattr_update_entry(struct inode *inode,
925 struct ocfs2_xattr_info *xi,
926 struct ocfs2_xattr_search *xs,
929 handle_t *handle = NULL;
932 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
933 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
934 if (IS_ERR(handle)) {
935 ret = PTR_ERR(handle);
939 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
940 OCFS2_JOURNAL_ACCESS_WRITE);
946 xs->here->xe_name_offset = cpu_to_le16(offs);
947 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
948 if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
949 ocfs2_xattr_set_local(xs->here, 1);
951 ocfs2_xattr_set_local(xs->here, 0);
952 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
954 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
958 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
964 * ocfs2_xattr_set_value_outside()
966 * Set large size value in B tree.
968 static int ocfs2_xattr_set_value_outside(struct inode *inode,
969 struct ocfs2_xattr_info *xi,
970 struct ocfs2_xattr_search *xs,
973 size_t name_len = strlen(xi->name);
974 void *val = xs->base + offs;
975 struct ocfs2_xattr_value_root *xv = NULL;
976 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
979 memset(val, 0, size);
980 memcpy(val, xi->name, name_len);
981 xv = (struct ocfs2_xattr_value_root *)
982 (val + OCFS2_XATTR_SIZE(name_len));
984 xv->xr_last_eb_blk = 0;
985 xv->xr_list.l_tree_depth = 0;
986 xv->xr_list.l_count = cpu_to_le16(1);
987 xv->xr_list.l_next_free_rec = 0;
989 ret = ocfs2_xattr_value_truncate(inode, xs->xattr_bh, xv,
995 ret = __ocfs2_xattr_set_value_outside(inode, xv, xi->value,
1001 ret = ocfs2_xattr_update_entry(inode, xi, xs, offs);
1009 * ocfs2_xattr_set_entry_local()
1011 * Set, replace or remove extended attribute in local.
1013 static void ocfs2_xattr_set_entry_local(struct inode *inode,
1014 struct ocfs2_xattr_info *xi,
1015 struct ocfs2_xattr_search *xs,
1016 struct ocfs2_xattr_entry *last,
1019 size_t name_len = strlen(xi->name);
1022 if (xi->value && xs->not_found) {
1023 /* Insert the new xattr entry. */
1024 le16_add_cpu(&xs->header->xh_count, 1);
1025 ocfs2_xattr_set_type(last, xi->name_index);
1026 ocfs2_xattr_set_local(last, 1);
1027 last->xe_name_len = name_len;
1033 first_val = xs->base + min_offs;
1034 offs = le16_to_cpu(xs->here->xe_name_offset);
1035 val = xs->base + offs;
1037 if (le64_to_cpu(xs->here->xe_value_size) >
1038 OCFS2_XATTR_INLINE_SIZE)
1039 size = OCFS2_XATTR_SIZE(name_len) +
1040 OCFS2_XATTR_ROOT_SIZE;
1042 size = OCFS2_XATTR_SIZE(name_len) +
1043 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1045 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1046 OCFS2_XATTR_SIZE(xi->value_len)) {
1047 /* The old and the new value have the
1048 same size. Just replace the value. */
1049 ocfs2_xattr_set_local(xs->here, 1);
1050 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1051 /* Clear value bytes. */
1052 memset(val + OCFS2_XATTR_SIZE(name_len),
1054 OCFS2_XATTR_SIZE(xi->value_len));
1055 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1060 /* Remove the old name+value. */
1061 memmove(first_val + size, first_val, val - first_val);
1062 memset(first_val, 0, size);
1063 xs->here->xe_name_hash = 0;
1064 xs->here->xe_name_offset = 0;
1065 ocfs2_xattr_set_local(xs->here, 1);
1066 xs->here->xe_value_size = 0;
1070 /* Adjust all value offsets. */
1071 last = xs->header->xh_entries;
1072 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1073 size_t o = le16_to_cpu(last->xe_name_offset);
1076 last->xe_name_offset = cpu_to_le16(o + size);
1081 /* Remove the old entry. */
1083 memmove(xs->here, xs->here + 1,
1084 (void *)last - (void *)xs->here);
1085 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1086 le16_add_cpu(&xs->header->xh_count, -1);
1090 /* Insert the new name+value. */
1091 size_t size = OCFS2_XATTR_SIZE(name_len) +
1092 OCFS2_XATTR_SIZE(xi->value_len);
1093 void *val = xs->base + min_offs - size;
1095 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1096 memset(val, 0, size);
1097 memcpy(val, xi->name, name_len);
1098 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1101 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1102 ocfs2_xattr_set_local(xs->here, 1);
1103 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1110 * ocfs2_xattr_set_entry()
1112 * Set extended attribute entry into inode or block.
1114 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1115 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1116 * then set value in B tree with set_value_outside().
1118 static int ocfs2_xattr_set_entry(struct inode *inode,
1119 struct ocfs2_xattr_info *xi,
1120 struct ocfs2_xattr_search *xs,
1123 struct ocfs2_xattr_entry *last;
1124 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1125 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1126 size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1128 handle_t *handle = NULL;
1130 struct ocfs2_xattr_info xi_l = {
1131 .name_index = xi->name_index,
1134 .value_len = xi->value_len,
1137 /* Compute min_offs, last and free space. */
1138 last = xs->header->xh_entries;
1140 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1141 size_t offs = le16_to_cpu(last->xe_name_offset);
1142 if (offs < min_offs)
1147 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
1151 if (!xs->not_found) {
1153 if (ocfs2_xattr_is_local(xs->here))
1154 size = OCFS2_XATTR_SIZE(name_len) +
1155 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1157 size = OCFS2_XATTR_SIZE(name_len) +
1158 OCFS2_XATTR_ROOT_SIZE;
1159 free += (size + sizeof(struct ocfs2_xattr_entry));
1161 /* Check free space in inode or block */
1162 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1163 if (free < sizeof(struct ocfs2_xattr_entry) +
1164 OCFS2_XATTR_SIZE(name_len) +
1165 OCFS2_XATTR_ROOT_SIZE) {
1169 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1170 xi_l.value = (void *)&def_xv;
1171 xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
1172 } else if (xi->value) {
1173 if (free < sizeof(struct ocfs2_xattr_entry) +
1174 OCFS2_XATTR_SIZE(name_len) +
1175 OCFS2_XATTR_SIZE(xi->value_len)) {
1181 if (!xs->not_found) {
1182 /* For existing extended attribute */
1183 size_t size = OCFS2_XATTR_SIZE(name_len) +
1184 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1185 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1186 void *val = xs->base + offs;
1188 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1189 /* Replace existing local xattr with tree root */
1190 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
1195 } else if (!ocfs2_xattr_is_local(xs->here)) {
1196 /* For existing xattr which has value outside */
1197 struct ocfs2_xattr_value_root *xv = NULL;
1198 xv = (struct ocfs2_xattr_value_root *)(val +
1199 OCFS2_XATTR_SIZE(name_len));
1201 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1203 * If new value need set outside also,
1204 * first truncate old value to new value,
1205 * then set new value with set_value_outside().
1207 ret = ocfs2_xattr_value_truncate(inode,
1216 ret = __ocfs2_xattr_set_value_outside(inode,
1225 ret = ocfs2_xattr_update_entry(inode,
1234 * If new value need set in local,
1235 * just trucate old value to zero.
1237 ret = ocfs2_xattr_value_truncate(inode,
1247 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1248 OCFS2_INODE_UPDATE_CREDITS);
1249 if (IS_ERR(handle)) {
1250 ret = PTR_ERR(handle);
1255 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1256 OCFS2_JOURNAL_ACCESS_WRITE);
1262 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1263 /*set extended attribue in external blcok*/
1264 ret = ocfs2_extend_trans(handle,
1265 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1270 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1271 OCFS2_JOURNAL_ACCESS_WRITE);
1279 * Set value in local, include set tree root in local.
1280 * This is the first step for value size >INLINE_SIZE.
1282 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1284 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1285 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1292 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1293 (flag & OCFS2_INLINE_XATTR_FL)) {
1294 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1295 unsigned int xattrsize = osb->s_xattr_inline_size;
1298 * Adjust extent record count or inline data size
1299 * to reserve space for extended attribute.
1301 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1302 struct ocfs2_inline_data *idata = &di->id2.i_data;
1303 le16_add_cpu(&idata->id_count, -xattrsize);
1304 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1305 struct ocfs2_extent_list *el = &di->id2.i_list;
1306 le16_add_cpu(&el->l_count, -(xattrsize /
1307 sizeof(struct ocfs2_extent_rec)));
1309 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1311 /* Update xattr flag */
1312 spin_lock(&oi->ip_lock);
1313 oi->ip_dyn_features |= flag;
1314 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1315 spin_unlock(&oi->ip_lock);
1316 /* Update inode ctime */
1317 inode->i_ctime = CURRENT_TIME;
1318 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
1319 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
1321 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1326 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1328 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1330 * Set value outside in B tree.
1331 * This is the second step for value size > INLINE_SIZE.
1333 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1334 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, offs);
1340 * If set value outside failed, we have to clean
1341 * the junk tree root we have already set in local.
1343 ret2 = ocfs2_xattr_cleanup(inode, xi, xs, offs);
1353 static int ocfs2_xattr_free_block(handle_t *handle,
1354 struct ocfs2_super *osb,
1355 struct ocfs2_xattr_block *xb)
1357 struct inode *xb_alloc_inode;
1358 struct buffer_head *xb_alloc_bh = NULL;
1359 u64 blk = le64_to_cpu(xb->xb_blkno);
1360 u16 bit = le16_to_cpu(xb->xb_suballoc_bit);
1361 u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1364 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1365 EXTENT_ALLOC_SYSTEM_INODE,
1366 le16_to_cpu(xb->xb_suballoc_slot));
1367 if (!xb_alloc_inode) {
1372 mutex_lock(&xb_alloc_inode->i_mutex);
1374 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1379 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
1384 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1389 ocfs2_inode_unlock(xb_alloc_inode, 1);
1390 brelse(xb_alloc_bh);
1392 mutex_unlock(&xb_alloc_inode->i_mutex);
1393 iput(xb_alloc_inode);
1398 static int ocfs2_remove_value_outside(struct inode*inode,
1399 struct buffer_head *bh,
1400 struct ocfs2_xattr_header *header)
1404 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1405 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1407 if (!ocfs2_xattr_is_local(entry)) {
1408 struct ocfs2_xattr_value_root *xv;
1411 val = (void *)header +
1412 le16_to_cpu(entry->xe_name_offset);
1413 xv = (struct ocfs2_xattr_value_root *)
1414 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
1415 ret = ocfs2_xattr_value_truncate(inode, bh, xv, 0);
1426 static int ocfs2_xattr_ibody_remove(struct inode *inode,
1427 struct buffer_head *di_bh)
1430 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1431 struct ocfs2_xattr_header *header;
1434 header = (struct ocfs2_xattr_header *)
1435 ((void *)di + inode->i_sb->s_blocksize -
1436 le16_to_cpu(di->i_xattr_inline_size));
1438 ret = ocfs2_remove_value_outside(inode, di_bh, header);
1443 static int ocfs2_xattr_block_remove(struct inode *inode,
1444 struct buffer_head *blk_bh)
1446 struct ocfs2_xattr_block *xb;
1447 struct ocfs2_xattr_header *header;
1450 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1451 header = &(xb->xb_attrs.xb_header);
1453 ret = ocfs2_remove_value_outside(inode, blk_bh, header);
1459 * ocfs2_xattr_remove()
1461 * Free extended attribute resources associated with this inode.
1463 int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
1465 struct ocfs2_xattr_block *xb;
1466 struct buffer_head *blk_bh = NULL;
1467 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1468 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1469 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1473 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1476 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1477 ret = ocfs2_xattr_ibody_remove(inode, di_bh);
1483 if (di->i_xattr_loc) {
1484 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1485 le64_to_cpu(di->i_xattr_loc),
1486 &blk_bh, OCFS2_BH_CACHED, inode);
1491 /*Verify the signature of xattr block*/
1492 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
1493 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
1498 ret = ocfs2_xattr_block_remove(inode, blk_bh);
1505 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1506 OCFS2_INODE_UPDATE_CREDITS);
1507 if (IS_ERR(handle)) {
1508 ret = PTR_ERR(handle);
1512 ret = ocfs2_journal_access(handle, inode, di_bh,
1513 OCFS2_JOURNAL_ACCESS_WRITE);
1519 if (di->i_xattr_loc) {
1520 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1521 ocfs2_xattr_free_block(handle, osb, xb);
1522 di->i_xattr_loc = cpu_to_le64(0);
1525 spin_lock(&oi->ip_lock);
1526 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
1527 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1528 spin_unlock(&oi->ip_lock);
1530 ret = ocfs2_journal_dirty(handle, di_bh);
1534 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1541 static int ocfs2_xattr_has_space_inline(struct inode *inode,
1542 struct ocfs2_dinode *di)
1544 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1545 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
1548 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
1551 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1552 struct ocfs2_inline_data *idata = &di->id2.i_data;
1553 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
1554 } else if (ocfs2_inode_is_fast_symlink(inode)) {
1555 free = ocfs2_fast_symlink_chars(inode->i_sb) -
1556 le64_to_cpu(di->i_size);
1558 struct ocfs2_extent_list *el = &di->id2.i_list;
1559 free = (le16_to_cpu(el->l_count) -
1560 le16_to_cpu(el->l_next_free_rec)) *
1561 sizeof(struct ocfs2_extent_rec);
1563 if (free >= xattrsize)
1570 * ocfs2_xattr_ibody_find()
1572 * Find extended attribute in inode block and
1573 * fill search info into struct ocfs2_xattr_search.
1575 static int ocfs2_xattr_ibody_find(struct inode *inode,
1578 struct ocfs2_xattr_search *xs)
1580 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1581 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1585 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1588 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1589 down_read(&oi->ip_alloc_sem);
1590 has_space = ocfs2_xattr_has_space_inline(inode, di);
1591 up_read(&oi->ip_alloc_sem);
1596 xs->xattr_bh = xs->inode_bh;
1597 xs->end = (void *)di + inode->i_sb->s_blocksize;
1598 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
1599 xs->header = (struct ocfs2_xattr_header *)
1600 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1602 xs->header = (struct ocfs2_xattr_header *)
1603 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
1604 xs->base = (void *)xs->header;
1605 xs->here = xs->header->xh_entries;
1607 /* Find the named attribute. */
1608 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1609 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1610 if (ret && ret != -ENODATA)
1612 xs->not_found = ret;
1619 * ocfs2_xattr_ibody_set()
1621 * Set, replace or remove an extended attribute into inode block.
1624 static int ocfs2_xattr_ibody_set(struct inode *inode,
1625 struct ocfs2_xattr_info *xi,
1626 struct ocfs2_xattr_search *xs)
1628 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1629 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1632 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1635 down_write(&oi->ip_alloc_sem);
1636 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1637 if (!ocfs2_xattr_has_space_inline(inode, di)) {
1643 ret = ocfs2_xattr_set_entry(inode, xi, xs,
1644 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
1646 up_write(&oi->ip_alloc_sem);
1652 * ocfs2_xattr_block_find()
1654 * Find extended attribute in external block and
1655 * fill search info into struct ocfs2_xattr_search.
1657 static int ocfs2_xattr_block_find(struct inode *inode,
1660 struct ocfs2_xattr_search *xs)
1662 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1663 struct buffer_head *blk_bh = NULL;
1666 if (!di->i_xattr_loc)
1669 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1670 le64_to_cpu(di->i_xattr_loc),
1671 &blk_bh, OCFS2_BH_CACHED, inode);
1676 /*Verify the signature of xattr block*/
1677 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
1678 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
1683 xs->xattr_bh = blk_bh;
1684 xs->header = &((struct ocfs2_xattr_block *)blk_bh->b_data)->
1686 xs->base = (void *)xs->header;
1687 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
1688 xs->here = xs->header->xh_entries;
1690 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1691 if (ret && ret != -ENODATA) {
1692 xs->xattr_bh = NULL;
1695 xs->not_found = ret;
1705 * ocfs2_xattr_block_set()
1707 * Set, replace or remove an extended attribute into external block.
1710 static int ocfs2_xattr_block_set(struct inode *inode,
1711 struct ocfs2_xattr_info *xi,
1712 struct ocfs2_xattr_search *xs)
1714 struct buffer_head *new_bh = NULL;
1715 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1716 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1717 struct ocfs2_alloc_context *meta_ac = NULL;
1718 handle_t *handle = NULL;
1719 struct ocfs2_xattr_block *xblk = NULL;
1720 u16 suballoc_bit_start;
1725 if (!xs->xattr_bh) {
1727 * Alloc one external block for extended attribute
1730 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
1735 handle = ocfs2_start_trans(osb,
1736 OCFS2_XATTR_BLOCK_CREATE_CREDITS);
1737 if (IS_ERR(handle)) {
1738 ret = PTR_ERR(handle);
1742 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1743 OCFS2_JOURNAL_ACCESS_CREATE);
1749 ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
1750 &suballoc_bit_start, &num_got,
1757 new_bh = sb_getblk(inode->i_sb, first_blkno);
1758 ocfs2_set_new_buffer_uptodate(inode, new_bh);
1760 ret = ocfs2_journal_access(handle, inode, new_bh,
1761 OCFS2_JOURNAL_ACCESS_CREATE);
1767 /* Initialize ocfs2_xattr_block */
1768 xs->xattr_bh = new_bh;
1769 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
1770 memset(xblk, 0, inode->i_sb->s_blocksize);
1771 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
1772 xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
1773 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1774 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
1775 xblk->xb_blkno = cpu_to_le64(first_blkno);
1777 xs->header = &xblk->xb_attrs.xb_header;
1778 xs->base = (void *)xs->header;
1779 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
1780 xs->here = xs->header->xh_entries;
1783 ret = ocfs2_journal_dirty(handle, new_bh);
1788 di->i_xattr_loc = cpu_to_le64(first_blkno);
1789 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1793 ocfs2_commit_trans(osb, handle);
1796 ocfs2_free_alloc_context(meta_ac);
1801 /* Set extended attribute into external block */
1802 ret = ocfs2_xattr_set_entry(inode, xi, xs, OCFS2_HAS_XATTR_FL);
1810 * Set, replace or remove an extended attribute for this inode.
1811 * value is NULL to remove an existing extended attribute, else either
1812 * create or replace an extended attribute.
1814 int ocfs2_xattr_set(struct inode *inode,
1821 struct buffer_head *di_bh = NULL;
1822 struct ocfs2_dinode *di;
1825 struct ocfs2_xattr_info xi = {
1826 .name_index = name_index,
1829 .value_len = value_len,
1832 struct ocfs2_xattr_search xis = {
1833 .not_found = -ENODATA,
1836 struct ocfs2_xattr_search xbs = {
1837 .not_found = -ENODATA,
1840 ret = ocfs2_inode_lock(inode, &di_bh, 1);
1845 xis.inode_bh = xbs.inode_bh = di_bh;
1846 di = (struct ocfs2_dinode *)di_bh->b_data;
1848 down_write(&OCFS2_I(inode)->ip_xattr_sem);
1850 * Scan inode and external block to find the same name
1851 * extended attribute and collect search infomation.
1853 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
1856 if (xis.not_found) {
1857 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
1862 if (xis.not_found && xbs.not_found) {
1864 if (flags & XATTR_REPLACE)
1871 if (flags & XATTR_CREATE)
1876 /* Remove existing extended attribute */
1878 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
1879 else if (!xbs.not_found)
1880 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
1882 /* We always try to set extended attribute into inode first*/
1883 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
1884 if (!ret && !xbs.not_found) {
1886 * If succeed and that extended attribute existing in
1887 * external block, then we will remove it.
1891 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
1892 } else if (ret == -ENOSPC) {
1893 if (di->i_xattr_loc && !xbs.xattr_bh) {
1894 ret = ocfs2_xattr_block_find(inode, name_index,
1900 * If no space in inode, we will set extended attribute
1901 * into external block.
1903 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
1906 if (!xis.not_found) {
1908 * If succeed and that extended attribute
1909 * existing in inode, we will remove it.
1913 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
1918 up_write(&OCFS2_I(inode)->ip_xattr_sem);
1919 ocfs2_inode_unlock(inode, 1);
1921 brelse(xbs.xattr_bh);