ocfs2: Don't return -EFAULT from a corrupt xattr entry.
[linux-2.6/mini2440.git] / fs / ocfs2 / xattr.c
blob41a6ca004ae37f2f7bc3815cf89b9bf48d8094ae
1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
4 * xattr.c
6 * Copyright (C) 2004, 2008 Oracle. All rights reserved.
8 * CREDITS:
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>
23 #include <linux/fs.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>
42 #include "ocfs2.h"
43 #include "alloc.h"
44 #include "dlmglue.h"
45 #include "file.h"
46 #include "symlink.h"
47 #include "sysfile.h"
48 #include "inode.h"
49 #include "journal.h"
50 #include "ocfs2_fs.h"
51 #include "suballoc.h"
52 #include "uptodate.h"
53 #include "buffer_head_io.h"
54 #include "super.h"
55 #include "xattr.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,
78 NULL
81 static struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
82 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
83 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
86 struct ocfs2_xattr_info {
87 int name_index;
88 const char *name;
89 const void *value;
90 size_t value_len;
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;
102 void *base;
103 void *end;
104 struct ocfs2_xattr_entry *here;
105 int not_found;
108 static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
109 struct ocfs2_xattr_header *xh,
110 int index,
111 int *block_off,
112 int *new_offset);
114 static int ocfs2_xattr_index_block_find(struct inode *inode,
115 struct buffer_head *root_bh,
116 int name_index,
117 const char *name,
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,
122 char *buffer,
123 size_t buffer_size);
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 u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
137 return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
140 static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
142 return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
145 static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb)
147 u16 len = sb->s_blocksize -
148 offsetof(struct ocfs2_xattr_header, xh_entries);
150 return len / sizeof(struct ocfs2_xattr_entry);
153 static inline const char *ocfs2_xattr_prefix(int name_index)
155 struct xattr_handler *handler = NULL;
157 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
158 handler = ocfs2_xattr_handler_map[name_index];
160 return handler ? handler->prefix : NULL;
163 static u32 ocfs2_xattr_name_hash(struct inode *inode,
164 const char *name,
165 int name_len)
167 /* Get hash value of uuid from super block */
168 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
169 int i;
171 /* hash extended attribute name */
172 for (i = 0; i < name_len; i++) {
173 hash = (hash << OCFS2_HASH_SHIFT) ^
174 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
175 *name++;
178 return hash;
182 * ocfs2_xattr_hash_entry()
184 * Compute the hash of an extended attribute.
186 static void ocfs2_xattr_hash_entry(struct inode *inode,
187 struct ocfs2_xattr_header *header,
188 struct ocfs2_xattr_entry *entry)
190 u32 hash = 0;
191 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
193 hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
194 entry->xe_name_hash = cpu_to_le32(hash);
196 return;
199 static int ocfs2_xattr_extend_allocation(struct inode *inode,
200 u32 clusters_to_add,
201 struct buffer_head *xattr_bh,
202 struct ocfs2_xattr_value_root *xv)
204 int status = 0;
205 int restart_func = 0;
206 int credits = 0;
207 handle_t *handle = NULL;
208 struct ocfs2_alloc_context *data_ac = NULL;
209 struct ocfs2_alloc_context *meta_ac = NULL;
210 enum ocfs2_alloc_restarted why;
211 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
212 u32 prev_clusters, logical_start = le32_to_cpu(xv->xr_clusters);
213 struct ocfs2_extent_tree et;
215 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
217 ocfs2_init_xattr_value_extent_tree(&et, inode, xattr_bh, xv);
219 restart_all:
221 status = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
222 &data_ac, &meta_ac);
223 if (status) {
224 mlog_errno(status);
225 goto leave;
228 credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
229 clusters_to_add);
230 handle = ocfs2_start_trans(osb, credits);
231 if (IS_ERR(handle)) {
232 status = PTR_ERR(handle);
233 handle = NULL;
234 mlog_errno(status);
235 goto leave;
238 restarted_transaction:
239 status = ocfs2_journal_access(handle, inode, xattr_bh,
240 OCFS2_JOURNAL_ACCESS_WRITE);
241 if (status < 0) {
242 mlog_errno(status);
243 goto leave;
246 prev_clusters = le32_to_cpu(xv->xr_clusters);
247 status = ocfs2_add_clusters_in_btree(osb,
248 inode,
249 &logical_start,
250 clusters_to_add,
252 &et,
253 handle,
254 data_ac,
255 meta_ac,
256 &why);
257 if ((status < 0) && (status != -EAGAIN)) {
258 if (status != -ENOSPC)
259 mlog_errno(status);
260 goto leave;
263 status = ocfs2_journal_dirty(handle, xattr_bh);
264 if (status < 0) {
265 mlog_errno(status);
266 goto leave;
269 clusters_to_add -= le32_to_cpu(xv->xr_clusters) - prev_clusters;
271 if (why != RESTART_NONE && clusters_to_add) {
272 if (why == RESTART_META) {
273 mlog(0, "restarting function.\n");
274 restart_func = 1;
275 } else {
276 BUG_ON(why != RESTART_TRANS);
278 mlog(0, "restarting transaction.\n");
279 /* TODO: This can be more intelligent. */
280 credits = ocfs2_calc_extend_credits(osb->sb,
281 et.et_root_el,
282 clusters_to_add);
283 status = ocfs2_extend_trans(handle, credits);
284 if (status < 0) {
285 /* handle still has to be committed at
286 * this point. */
287 status = -ENOMEM;
288 mlog_errno(status);
289 goto leave;
291 goto restarted_transaction;
295 leave:
296 if (handle) {
297 ocfs2_commit_trans(osb, handle);
298 handle = NULL;
300 if (data_ac) {
301 ocfs2_free_alloc_context(data_ac);
302 data_ac = NULL;
304 if (meta_ac) {
305 ocfs2_free_alloc_context(meta_ac);
306 meta_ac = NULL;
308 if ((!status) && restart_func) {
309 restart_func = 0;
310 goto restart_all;
313 return status;
316 static int __ocfs2_remove_xattr_range(struct inode *inode,
317 struct buffer_head *root_bh,
318 struct ocfs2_xattr_value_root *xv,
319 u32 cpos, u32 phys_cpos, u32 len,
320 struct ocfs2_cached_dealloc_ctxt *dealloc)
322 int ret;
323 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
324 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
325 struct inode *tl_inode = osb->osb_tl_inode;
326 handle_t *handle;
327 struct ocfs2_alloc_context *meta_ac = NULL;
328 struct ocfs2_extent_tree et;
330 ocfs2_init_xattr_value_extent_tree(&et, inode, root_bh, xv);
332 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
333 if (ret) {
334 mlog_errno(ret);
335 return ret;
338 mutex_lock(&tl_inode->i_mutex);
340 if (ocfs2_truncate_log_needs_flush(osb)) {
341 ret = __ocfs2_flush_truncate_log(osb);
342 if (ret < 0) {
343 mlog_errno(ret);
344 goto out;
348 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
349 if (IS_ERR(handle)) {
350 ret = PTR_ERR(handle);
351 mlog_errno(ret);
352 goto out;
355 ret = ocfs2_journal_access(handle, inode, root_bh,
356 OCFS2_JOURNAL_ACCESS_WRITE);
357 if (ret) {
358 mlog_errno(ret);
359 goto out_commit;
362 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
363 dealloc);
364 if (ret) {
365 mlog_errno(ret);
366 goto out_commit;
369 le32_add_cpu(&xv->xr_clusters, -len);
371 ret = ocfs2_journal_dirty(handle, root_bh);
372 if (ret) {
373 mlog_errno(ret);
374 goto out_commit;
377 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
378 if (ret)
379 mlog_errno(ret);
381 out_commit:
382 ocfs2_commit_trans(osb, handle);
383 out:
384 mutex_unlock(&tl_inode->i_mutex);
386 if (meta_ac)
387 ocfs2_free_alloc_context(meta_ac);
389 return ret;
392 static int ocfs2_xattr_shrink_size(struct inode *inode,
393 u32 old_clusters,
394 u32 new_clusters,
395 struct buffer_head *root_bh,
396 struct ocfs2_xattr_value_root *xv)
398 int ret = 0;
399 u32 trunc_len, cpos, phys_cpos, alloc_size;
400 u64 block;
401 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
402 struct ocfs2_cached_dealloc_ctxt dealloc;
404 ocfs2_init_dealloc_ctxt(&dealloc);
406 if (old_clusters <= new_clusters)
407 return 0;
409 cpos = new_clusters;
410 trunc_len = old_clusters - new_clusters;
411 while (trunc_len) {
412 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
413 &alloc_size, &xv->xr_list);
414 if (ret) {
415 mlog_errno(ret);
416 goto out;
419 if (alloc_size > trunc_len)
420 alloc_size = trunc_len;
422 ret = __ocfs2_remove_xattr_range(inode, root_bh, xv, cpos,
423 phys_cpos, alloc_size,
424 &dealloc);
425 if (ret) {
426 mlog_errno(ret);
427 goto out;
430 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
431 ocfs2_remove_xattr_clusters_from_cache(inode, block,
432 alloc_size);
433 cpos += alloc_size;
434 trunc_len -= alloc_size;
437 out:
438 ocfs2_schedule_truncate_log_flush(osb, 1);
439 ocfs2_run_deallocs(osb, &dealloc);
441 return ret;
444 static int ocfs2_xattr_value_truncate(struct inode *inode,
445 struct buffer_head *root_bh,
446 struct ocfs2_xattr_value_root *xv,
447 int len)
449 int ret;
450 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
451 u32 old_clusters = le32_to_cpu(xv->xr_clusters);
453 if (new_clusters == old_clusters)
454 return 0;
456 if (new_clusters > old_clusters)
457 ret = ocfs2_xattr_extend_allocation(inode,
458 new_clusters - old_clusters,
459 root_bh, xv);
460 else
461 ret = ocfs2_xattr_shrink_size(inode,
462 old_clusters, new_clusters,
463 root_bh, xv);
465 return ret;
468 static int ocfs2_xattr_list_entry(char *buffer, size_t size,
469 size_t *result, const char *prefix,
470 const char *name, int name_len)
472 char *p = buffer + *result;
473 int prefix_len = strlen(prefix);
474 int total_len = prefix_len + name_len + 1;
476 *result += total_len;
478 /* we are just looking for how big our buffer needs to be */
479 if (!size)
480 return 0;
482 if (*result > size)
483 return -ERANGE;
485 memcpy(p, prefix, prefix_len);
486 memcpy(p + prefix_len, name, name_len);
487 p[prefix_len + name_len] = '\0';
489 return 0;
492 static int ocfs2_xattr_list_entries(struct inode *inode,
493 struct ocfs2_xattr_header *header,
494 char *buffer, size_t buffer_size)
496 size_t result = 0;
497 int i, type, ret;
498 const char *prefix, *name;
500 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
501 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
502 type = ocfs2_xattr_get_type(entry);
503 prefix = ocfs2_xattr_prefix(type);
505 if (prefix) {
506 name = (const char *)header +
507 le16_to_cpu(entry->xe_name_offset);
509 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
510 &result, prefix, name,
511 entry->xe_name_len);
512 if (ret)
513 return ret;
517 return result;
520 static int ocfs2_xattr_ibody_list(struct inode *inode,
521 struct ocfs2_dinode *di,
522 char *buffer,
523 size_t buffer_size)
525 struct ocfs2_xattr_header *header = NULL;
526 struct ocfs2_inode_info *oi = OCFS2_I(inode);
527 int ret = 0;
529 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
530 return ret;
532 header = (struct ocfs2_xattr_header *)
533 ((void *)di + inode->i_sb->s_blocksize -
534 le16_to_cpu(di->i_xattr_inline_size));
536 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
538 return ret;
541 static int ocfs2_xattr_block_list(struct inode *inode,
542 struct ocfs2_dinode *di,
543 char *buffer,
544 size_t buffer_size)
546 struct buffer_head *blk_bh = NULL;
547 struct ocfs2_xattr_block *xb;
548 int ret = 0;
550 if (!di->i_xattr_loc)
551 return ret;
553 ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh);
554 if (ret < 0) {
555 mlog_errno(ret);
556 return ret;
559 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
560 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
561 ret = -EIO;
562 goto cleanup;
565 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
566 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
567 ret = ocfs2_xattr_list_entries(inode, header,
568 buffer, buffer_size);
569 } else {
570 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
571 ret = ocfs2_xattr_tree_list_index_block(inode, xt,
572 buffer, buffer_size);
574 cleanup:
575 brelse(blk_bh);
577 return ret;
580 ssize_t ocfs2_listxattr(struct dentry *dentry,
581 char *buffer,
582 size_t size)
584 int ret = 0, i_ret = 0, b_ret = 0;
585 struct buffer_head *di_bh = NULL;
586 struct ocfs2_dinode *di = NULL;
587 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
589 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
590 return -EOPNOTSUPP;
592 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
593 return ret;
595 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
596 if (ret < 0) {
597 mlog_errno(ret);
598 return ret;
601 di = (struct ocfs2_dinode *)di_bh->b_data;
603 down_read(&oi->ip_xattr_sem);
604 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
605 if (i_ret < 0)
606 b_ret = 0;
607 else {
608 if (buffer) {
609 buffer += i_ret;
610 size -= i_ret;
612 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
613 buffer, size);
614 if (b_ret < 0)
615 i_ret = 0;
617 up_read(&oi->ip_xattr_sem);
618 ocfs2_inode_unlock(dentry->d_inode, 0);
620 brelse(di_bh);
622 return i_ret + b_ret;
625 static int ocfs2_xattr_find_entry(int name_index,
626 const char *name,
627 struct ocfs2_xattr_search *xs)
629 struct ocfs2_xattr_entry *entry;
630 size_t name_len;
631 int i, cmp = 1;
633 if (name == NULL)
634 return -EINVAL;
636 name_len = strlen(name);
637 entry = xs->here;
638 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
639 cmp = name_index - ocfs2_xattr_get_type(entry);
640 if (!cmp)
641 cmp = name_len - entry->xe_name_len;
642 if (!cmp)
643 cmp = memcmp(name, (xs->base +
644 le16_to_cpu(entry->xe_name_offset)),
645 name_len);
646 if (cmp == 0)
647 break;
648 entry += 1;
650 xs->here = entry;
652 return cmp ? -ENODATA : 0;
655 static int ocfs2_xattr_get_value_outside(struct inode *inode,
656 struct ocfs2_xattr_value_root *xv,
657 void *buffer,
658 size_t len)
660 u32 cpos, p_cluster, num_clusters, bpc, clusters;
661 u64 blkno;
662 int i, ret = 0;
663 size_t cplen, blocksize;
664 struct buffer_head *bh = NULL;
665 struct ocfs2_extent_list *el;
667 el = &xv->xr_list;
668 clusters = le32_to_cpu(xv->xr_clusters);
669 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
670 blocksize = inode->i_sb->s_blocksize;
672 cpos = 0;
673 while (cpos < clusters) {
674 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
675 &num_clusters, el);
676 if (ret) {
677 mlog_errno(ret);
678 goto out;
681 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
682 /* Copy ocfs2_xattr_value */
683 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
684 ret = ocfs2_read_block(inode, blkno, &bh);
685 if (ret) {
686 mlog_errno(ret);
687 goto out;
690 cplen = len >= blocksize ? blocksize : len;
691 memcpy(buffer, bh->b_data, cplen);
692 len -= cplen;
693 buffer += cplen;
695 brelse(bh);
696 bh = NULL;
697 if (len == 0)
698 break;
700 cpos += num_clusters;
702 out:
703 return ret;
706 static int ocfs2_xattr_ibody_get(struct inode *inode,
707 int name_index,
708 const char *name,
709 void *buffer,
710 size_t buffer_size,
711 struct ocfs2_xattr_search *xs)
713 struct ocfs2_inode_info *oi = OCFS2_I(inode);
714 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
715 struct ocfs2_xattr_value_root *xv;
716 size_t size;
717 int ret = 0;
719 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
720 return -ENODATA;
722 xs->end = (void *)di + inode->i_sb->s_blocksize;
723 xs->header = (struct ocfs2_xattr_header *)
724 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
725 xs->base = (void *)xs->header;
726 xs->here = xs->header->xh_entries;
728 ret = ocfs2_xattr_find_entry(name_index, name, xs);
729 if (ret)
730 return ret;
731 size = le64_to_cpu(xs->here->xe_value_size);
732 if (buffer) {
733 if (size > buffer_size)
734 return -ERANGE;
735 if (ocfs2_xattr_is_local(xs->here)) {
736 memcpy(buffer, (void *)xs->base +
737 le16_to_cpu(xs->here->xe_name_offset) +
738 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
739 } else {
740 xv = (struct ocfs2_xattr_value_root *)
741 (xs->base + le16_to_cpu(
742 xs->here->xe_name_offset) +
743 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
744 ret = ocfs2_xattr_get_value_outside(inode, xv,
745 buffer, size);
746 if (ret < 0) {
747 mlog_errno(ret);
748 return ret;
753 return size;
756 static int ocfs2_xattr_block_get(struct inode *inode,
757 int name_index,
758 const char *name,
759 void *buffer,
760 size_t buffer_size,
761 struct ocfs2_xattr_search *xs)
763 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
764 struct buffer_head *blk_bh = NULL;
765 struct ocfs2_xattr_block *xb;
766 struct ocfs2_xattr_value_root *xv;
767 size_t size;
768 int ret = -ENODATA, name_offset, name_len, block_off, i;
770 if (!di->i_xattr_loc)
771 return ret;
773 memset(&xs->bucket, 0, sizeof(xs->bucket));
775 ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh);
776 if (ret < 0) {
777 mlog_errno(ret);
778 return ret;
781 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
782 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
783 ret = -EIO;
784 goto cleanup;
787 xs->xattr_bh = blk_bh;
789 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
790 xs->header = &xb->xb_attrs.xb_header;
791 xs->base = (void *)xs->header;
792 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
793 xs->here = xs->header->xh_entries;
795 ret = ocfs2_xattr_find_entry(name_index, name, xs);
796 } else
797 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
798 name_index,
799 name, xs);
801 if (ret)
802 goto cleanup;
803 size = le64_to_cpu(xs->here->xe_value_size);
804 if (buffer) {
805 ret = -ERANGE;
806 if (size > buffer_size)
807 goto cleanup;
809 name_offset = le16_to_cpu(xs->here->xe_name_offset);
810 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
811 i = xs->here - xs->header->xh_entries;
813 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
814 ret = ocfs2_xattr_bucket_get_name_value(inode,
815 xs->bucket.xh,
817 &block_off,
818 &name_offset);
819 xs->base = xs->bucket.bhs[block_off]->b_data;
821 if (ocfs2_xattr_is_local(xs->here)) {
822 memcpy(buffer, (void *)xs->base +
823 name_offset + name_len, size);
824 } else {
825 xv = (struct ocfs2_xattr_value_root *)
826 (xs->base + name_offset + name_len);
827 ret = ocfs2_xattr_get_value_outside(inode, xv,
828 buffer, size);
829 if (ret < 0) {
830 mlog_errno(ret);
831 goto cleanup;
835 ret = size;
836 cleanup:
837 for (i = 0; i < OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET; i++)
838 brelse(xs->bucket.bhs[i]);
839 memset(&xs->bucket, 0, sizeof(xs->bucket));
841 brelse(blk_bh);
842 return ret;
845 /* ocfs2_xattr_get()
847 * Copy an extended attribute into the buffer provided.
848 * Buffer is NULL to compute the size of buffer required.
850 static int ocfs2_xattr_get(struct inode *inode,
851 int name_index,
852 const char *name,
853 void *buffer,
854 size_t buffer_size)
856 int ret;
857 struct ocfs2_dinode *di = NULL;
858 struct buffer_head *di_bh = NULL;
859 struct ocfs2_inode_info *oi = OCFS2_I(inode);
860 struct ocfs2_xattr_search xis = {
861 .not_found = -ENODATA,
863 struct ocfs2_xattr_search xbs = {
864 .not_found = -ENODATA,
867 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
868 return -EOPNOTSUPP;
870 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
871 ret = -ENODATA;
873 ret = ocfs2_inode_lock(inode, &di_bh, 0);
874 if (ret < 0) {
875 mlog_errno(ret);
876 return ret;
878 xis.inode_bh = xbs.inode_bh = di_bh;
879 di = (struct ocfs2_dinode *)di_bh->b_data;
881 down_read(&oi->ip_xattr_sem);
882 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
883 buffer_size, &xis);
884 if (ret == -ENODATA)
885 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
886 buffer_size, &xbs);
887 up_read(&oi->ip_xattr_sem);
888 ocfs2_inode_unlock(inode, 0);
890 brelse(di_bh);
892 return ret;
895 static int __ocfs2_xattr_set_value_outside(struct inode *inode,
896 struct ocfs2_xattr_value_root *xv,
897 const void *value,
898 int value_len)
900 int ret = 0, i, cp_len, credits;
901 u16 blocksize = inode->i_sb->s_blocksize;
902 u32 p_cluster, num_clusters;
903 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
904 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
905 u64 blkno;
906 struct buffer_head *bh = NULL;
907 handle_t *handle;
909 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
911 credits = clusters * bpc;
912 handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb), credits);
913 if (IS_ERR(handle)) {
914 ret = PTR_ERR(handle);
915 mlog_errno(ret);
916 goto out;
919 while (cpos < clusters) {
920 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
921 &num_clusters, &xv->xr_list);
922 if (ret) {
923 mlog_errno(ret);
924 goto out_commit;
927 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
929 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
930 ret = ocfs2_read_block(inode, blkno, &bh);
931 if (ret) {
932 mlog_errno(ret);
933 goto out_commit;
936 ret = ocfs2_journal_access(handle,
937 inode,
939 OCFS2_JOURNAL_ACCESS_WRITE);
940 if (ret < 0) {
941 mlog_errno(ret);
942 goto out_commit;
945 cp_len = value_len > blocksize ? blocksize : value_len;
946 memcpy(bh->b_data, value, cp_len);
947 value_len -= cp_len;
948 value += cp_len;
949 if (cp_len < blocksize)
950 memset(bh->b_data + cp_len, 0,
951 blocksize - cp_len);
953 ret = ocfs2_journal_dirty(handle, bh);
954 if (ret < 0) {
955 mlog_errno(ret);
956 goto out_commit;
958 brelse(bh);
959 bh = NULL;
962 * XXX: do we need to empty all the following
963 * blocks in this cluster?
965 if (!value_len)
966 break;
968 cpos += num_clusters;
970 out_commit:
971 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
972 out:
973 brelse(bh);
975 return ret;
978 static int ocfs2_xattr_cleanup(struct inode *inode,
979 struct ocfs2_xattr_info *xi,
980 struct ocfs2_xattr_search *xs,
981 size_t offs)
983 handle_t *handle = NULL;
984 int ret = 0;
985 size_t name_len = strlen(xi->name);
986 void *val = xs->base + offs;
987 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
989 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
990 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
991 if (IS_ERR(handle)) {
992 ret = PTR_ERR(handle);
993 mlog_errno(ret);
994 goto out;
996 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
997 OCFS2_JOURNAL_ACCESS_WRITE);
998 if (ret) {
999 mlog_errno(ret);
1000 goto out_commit;
1002 /* Decrease xattr count */
1003 le16_add_cpu(&xs->header->xh_count, -1);
1004 /* Remove the xattr entry and tree root which has already be set*/
1005 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
1006 memset(val, 0, size);
1008 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1009 if (ret < 0)
1010 mlog_errno(ret);
1011 out_commit:
1012 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1013 out:
1014 return ret;
1017 static int ocfs2_xattr_update_entry(struct inode *inode,
1018 struct ocfs2_xattr_info *xi,
1019 struct ocfs2_xattr_search *xs,
1020 size_t offs)
1022 handle_t *handle = NULL;
1023 int ret = 0;
1025 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1026 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1027 if (IS_ERR(handle)) {
1028 ret = PTR_ERR(handle);
1029 mlog_errno(ret);
1030 goto out;
1032 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1033 OCFS2_JOURNAL_ACCESS_WRITE);
1034 if (ret) {
1035 mlog_errno(ret);
1036 goto out_commit;
1039 xs->here->xe_name_offset = cpu_to_le16(offs);
1040 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1041 if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
1042 ocfs2_xattr_set_local(xs->here, 1);
1043 else
1044 ocfs2_xattr_set_local(xs->here, 0);
1045 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1047 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1048 if (ret < 0)
1049 mlog_errno(ret);
1050 out_commit:
1051 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1052 out:
1053 return ret;
1057 * ocfs2_xattr_set_value_outside()
1059 * Set large size value in B tree.
1061 static int ocfs2_xattr_set_value_outside(struct inode *inode,
1062 struct ocfs2_xattr_info *xi,
1063 struct ocfs2_xattr_search *xs,
1064 size_t offs)
1066 size_t name_len = strlen(xi->name);
1067 void *val = xs->base + offs;
1068 struct ocfs2_xattr_value_root *xv = NULL;
1069 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1070 int ret = 0;
1072 memset(val, 0, size);
1073 memcpy(val, xi->name, name_len);
1074 xv = (struct ocfs2_xattr_value_root *)
1075 (val + OCFS2_XATTR_SIZE(name_len));
1076 xv->xr_clusters = 0;
1077 xv->xr_last_eb_blk = 0;
1078 xv->xr_list.l_tree_depth = 0;
1079 xv->xr_list.l_count = cpu_to_le16(1);
1080 xv->xr_list.l_next_free_rec = 0;
1082 ret = ocfs2_xattr_value_truncate(inode, xs->xattr_bh, xv,
1083 xi->value_len);
1084 if (ret < 0) {
1085 mlog_errno(ret);
1086 return ret;
1088 ret = __ocfs2_xattr_set_value_outside(inode, xv, xi->value,
1089 xi->value_len);
1090 if (ret < 0) {
1091 mlog_errno(ret);
1092 return ret;
1094 ret = ocfs2_xattr_update_entry(inode, xi, xs, offs);
1095 if (ret < 0)
1096 mlog_errno(ret);
1098 return ret;
1102 * ocfs2_xattr_set_entry_local()
1104 * Set, replace or remove extended attribute in local.
1106 static void ocfs2_xattr_set_entry_local(struct inode *inode,
1107 struct ocfs2_xattr_info *xi,
1108 struct ocfs2_xattr_search *xs,
1109 struct ocfs2_xattr_entry *last,
1110 size_t min_offs)
1112 size_t name_len = strlen(xi->name);
1113 int i;
1115 if (xi->value && xs->not_found) {
1116 /* Insert the new xattr entry. */
1117 le16_add_cpu(&xs->header->xh_count, 1);
1118 ocfs2_xattr_set_type(last, xi->name_index);
1119 ocfs2_xattr_set_local(last, 1);
1120 last->xe_name_len = name_len;
1121 } else {
1122 void *first_val;
1123 void *val;
1124 size_t offs, size;
1126 first_val = xs->base + min_offs;
1127 offs = le16_to_cpu(xs->here->xe_name_offset);
1128 val = xs->base + offs;
1130 if (le64_to_cpu(xs->here->xe_value_size) >
1131 OCFS2_XATTR_INLINE_SIZE)
1132 size = OCFS2_XATTR_SIZE(name_len) +
1133 OCFS2_XATTR_ROOT_SIZE;
1134 else
1135 size = OCFS2_XATTR_SIZE(name_len) +
1136 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1138 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1139 OCFS2_XATTR_SIZE(xi->value_len)) {
1140 /* The old and the new value have the
1141 same size. Just replace the value. */
1142 ocfs2_xattr_set_local(xs->here, 1);
1143 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1144 /* Clear value bytes. */
1145 memset(val + OCFS2_XATTR_SIZE(name_len),
1147 OCFS2_XATTR_SIZE(xi->value_len));
1148 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1149 xi->value,
1150 xi->value_len);
1151 return;
1153 /* Remove the old name+value. */
1154 memmove(first_val + size, first_val, val - first_val);
1155 memset(first_val, 0, size);
1156 xs->here->xe_name_hash = 0;
1157 xs->here->xe_name_offset = 0;
1158 ocfs2_xattr_set_local(xs->here, 1);
1159 xs->here->xe_value_size = 0;
1161 min_offs += size;
1163 /* Adjust all value offsets. */
1164 last = xs->header->xh_entries;
1165 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1166 size_t o = le16_to_cpu(last->xe_name_offset);
1168 if (o < offs)
1169 last->xe_name_offset = cpu_to_le16(o + size);
1170 last += 1;
1173 if (!xi->value) {
1174 /* Remove the old entry. */
1175 last -= 1;
1176 memmove(xs->here, xs->here + 1,
1177 (void *)last - (void *)xs->here);
1178 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1179 le16_add_cpu(&xs->header->xh_count, -1);
1182 if (xi->value) {
1183 /* Insert the new name+value. */
1184 size_t size = OCFS2_XATTR_SIZE(name_len) +
1185 OCFS2_XATTR_SIZE(xi->value_len);
1186 void *val = xs->base + min_offs - size;
1188 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1189 memset(val, 0, size);
1190 memcpy(val, xi->name, name_len);
1191 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1192 xi->value,
1193 xi->value_len);
1194 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1195 ocfs2_xattr_set_local(xs->here, 1);
1196 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1199 return;
1203 * ocfs2_xattr_set_entry()
1205 * Set extended attribute entry into inode or block.
1207 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1208 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1209 * then set value in B tree with set_value_outside().
1211 static int ocfs2_xattr_set_entry(struct inode *inode,
1212 struct ocfs2_xattr_info *xi,
1213 struct ocfs2_xattr_search *xs,
1214 int flag)
1216 struct ocfs2_xattr_entry *last;
1217 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1218 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1219 size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1220 size_t size_l = 0;
1221 handle_t *handle = NULL;
1222 int free, i, ret;
1223 struct ocfs2_xattr_info xi_l = {
1224 .name_index = xi->name_index,
1225 .name = xi->name,
1226 .value = xi->value,
1227 .value_len = xi->value_len,
1230 /* Compute min_offs, last and free space. */
1231 last = xs->header->xh_entries;
1233 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1234 size_t offs = le16_to_cpu(last->xe_name_offset);
1235 if (offs < min_offs)
1236 min_offs = offs;
1237 last += 1;
1240 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
1241 if (free < 0)
1242 return -EIO;
1244 if (!xs->not_found) {
1245 size_t size = 0;
1246 if (ocfs2_xattr_is_local(xs->here))
1247 size = OCFS2_XATTR_SIZE(name_len) +
1248 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1249 else
1250 size = OCFS2_XATTR_SIZE(name_len) +
1251 OCFS2_XATTR_ROOT_SIZE;
1252 free += (size + sizeof(struct ocfs2_xattr_entry));
1254 /* Check free space in inode or block */
1255 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1256 if (free < sizeof(struct ocfs2_xattr_entry) +
1257 OCFS2_XATTR_SIZE(name_len) +
1258 OCFS2_XATTR_ROOT_SIZE) {
1259 ret = -ENOSPC;
1260 goto out;
1262 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1263 xi_l.value = (void *)&def_xv;
1264 xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
1265 } else if (xi->value) {
1266 if (free < sizeof(struct ocfs2_xattr_entry) +
1267 OCFS2_XATTR_SIZE(name_len) +
1268 OCFS2_XATTR_SIZE(xi->value_len)) {
1269 ret = -ENOSPC;
1270 goto out;
1274 if (!xs->not_found) {
1275 /* For existing extended attribute */
1276 size_t size = OCFS2_XATTR_SIZE(name_len) +
1277 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1278 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1279 void *val = xs->base + offs;
1281 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1282 /* Replace existing local xattr with tree root */
1283 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
1284 offs);
1285 if (ret < 0)
1286 mlog_errno(ret);
1287 goto out;
1288 } else if (!ocfs2_xattr_is_local(xs->here)) {
1289 /* For existing xattr which has value outside */
1290 struct ocfs2_xattr_value_root *xv = NULL;
1291 xv = (struct ocfs2_xattr_value_root *)(val +
1292 OCFS2_XATTR_SIZE(name_len));
1294 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1296 * If new value need set outside also,
1297 * first truncate old value to new value,
1298 * then set new value with set_value_outside().
1300 ret = ocfs2_xattr_value_truncate(inode,
1301 xs->xattr_bh,
1303 xi->value_len);
1304 if (ret < 0) {
1305 mlog_errno(ret);
1306 goto out;
1309 ret = __ocfs2_xattr_set_value_outside(inode,
1311 xi->value,
1312 xi->value_len);
1313 if (ret < 0) {
1314 mlog_errno(ret);
1315 goto out;
1318 ret = ocfs2_xattr_update_entry(inode,
1321 offs);
1322 if (ret < 0)
1323 mlog_errno(ret);
1324 goto out;
1325 } else {
1327 * If new value need set in local,
1328 * just trucate old value to zero.
1330 ret = ocfs2_xattr_value_truncate(inode,
1331 xs->xattr_bh,
1334 if (ret < 0)
1335 mlog_errno(ret);
1340 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1341 OCFS2_INODE_UPDATE_CREDITS);
1342 if (IS_ERR(handle)) {
1343 ret = PTR_ERR(handle);
1344 mlog_errno(ret);
1345 goto out;
1348 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1349 OCFS2_JOURNAL_ACCESS_WRITE);
1350 if (ret) {
1351 mlog_errno(ret);
1352 goto out_commit;
1355 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1356 /* set extended attribute in external block. */
1357 ret = ocfs2_extend_trans(handle,
1358 OCFS2_INODE_UPDATE_CREDITS +
1359 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1360 if (ret) {
1361 mlog_errno(ret);
1362 goto out_commit;
1364 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1365 OCFS2_JOURNAL_ACCESS_WRITE);
1366 if (ret) {
1367 mlog_errno(ret);
1368 goto out_commit;
1373 * Set value in local, include set tree root in local.
1374 * This is the first step for value size >INLINE_SIZE.
1376 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1378 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1379 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1380 if (ret < 0) {
1381 mlog_errno(ret);
1382 goto out_commit;
1386 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1387 (flag & OCFS2_INLINE_XATTR_FL)) {
1388 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1389 unsigned int xattrsize = osb->s_xattr_inline_size;
1392 * Adjust extent record count or inline data size
1393 * to reserve space for extended attribute.
1395 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1396 struct ocfs2_inline_data *idata = &di->id2.i_data;
1397 le16_add_cpu(&idata->id_count, -xattrsize);
1398 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1399 struct ocfs2_extent_list *el = &di->id2.i_list;
1400 le16_add_cpu(&el->l_count, -(xattrsize /
1401 sizeof(struct ocfs2_extent_rec)));
1403 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1405 /* Update xattr flag */
1406 spin_lock(&oi->ip_lock);
1407 oi->ip_dyn_features |= flag;
1408 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1409 spin_unlock(&oi->ip_lock);
1410 /* Update inode ctime */
1411 inode->i_ctime = CURRENT_TIME;
1412 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
1413 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
1415 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1416 if (ret < 0)
1417 mlog_errno(ret);
1419 out_commit:
1420 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1422 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1424 * Set value outside in B tree.
1425 * This is the second step for value size > INLINE_SIZE.
1427 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1428 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, offs);
1429 if (ret < 0) {
1430 int ret2;
1432 mlog_errno(ret);
1434 * If set value outside failed, we have to clean
1435 * the junk tree root we have already set in local.
1437 ret2 = ocfs2_xattr_cleanup(inode, xi, xs, offs);
1438 if (ret2 < 0)
1439 mlog_errno(ret2);
1442 out:
1443 return ret;
1447 static int ocfs2_remove_value_outside(struct inode*inode,
1448 struct buffer_head *bh,
1449 struct ocfs2_xattr_header *header)
1451 int ret = 0, i;
1453 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1454 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1456 if (!ocfs2_xattr_is_local(entry)) {
1457 struct ocfs2_xattr_value_root *xv;
1458 void *val;
1460 val = (void *)header +
1461 le16_to_cpu(entry->xe_name_offset);
1462 xv = (struct ocfs2_xattr_value_root *)
1463 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
1464 ret = ocfs2_xattr_value_truncate(inode, bh, xv, 0);
1465 if (ret < 0) {
1466 mlog_errno(ret);
1467 return ret;
1472 return ret;
1475 static int ocfs2_xattr_ibody_remove(struct inode *inode,
1476 struct buffer_head *di_bh)
1479 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1480 struct ocfs2_xattr_header *header;
1481 int ret;
1483 header = (struct ocfs2_xattr_header *)
1484 ((void *)di + inode->i_sb->s_blocksize -
1485 le16_to_cpu(di->i_xattr_inline_size));
1487 ret = ocfs2_remove_value_outside(inode, di_bh, header);
1489 return ret;
1492 static int ocfs2_xattr_block_remove(struct inode *inode,
1493 struct buffer_head *blk_bh)
1495 struct ocfs2_xattr_block *xb;
1496 int ret = 0;
1498 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1499 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1500 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
1501 ret = ocfs2_remove_value_outside(inode, blk_bh, header);
1502 } else
1503 ret = ocfs2_delete_xattr_index_block(inode, blk_bh);
1505 return ret;
1508 static int ocfs2_xattr_free_block(struct inode *inode,
1509 u64 block)
1511 struct inode *xb_alloc_inode;
1512 struct buffer_head *xb_alloc_bh = NULL;
1513 struct buffer_head *blk_bh = NULL;
1514 struct ocfs2_xattr_block *xb;
1515 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1516 handle_t *handle;
1517 int ret = 0;
1518 u64 blk, bg_blkno;
1519 u16 bit;
1521 ret = ocfs2_read_block(inode, block, &blk_bh);
1522 if (ret < 0) {
1523 mlog_errno(ret);
1524 goto out;
1527 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1528 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
1529 ret = -EIO;
1530 goto out;
1533 ret = ocfs2_xattr_block_remove(inode, blk_bh);
1534 if (ret < 0) {
1535 mlog_errno(ret);
1536 goto out;
1539 blk = le64_to_cpu(xb->xb_blkno);
1540 bit = le16_to_cpu(xb->xb_suballoc_bit);
1541 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1543 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1544 EXTENT_ALLOC_SYSTEM_INODE,
1545 le16_to_cpu(xb->xb_suballoc_slot));
1546 if (!xb_alloc_inode) {
1547 ret = -ENOMEM;
1548 mlog_errno(ret);
1549 goto out;
1551 mutex_lock(&xb_alloc_inode->i_mutex);
1553 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1554 if (ret < 0) {
1555 mlog_errno(ret);
1556 goto out_mutex;
1559 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
1560 if (IS_ERR(handle)) {
1561 ret = PTR_ERR(handle);
1562 mlog_errno(ret);
1563 goto out_unlock;
1566 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1567 bit, bg_blkno, 1);
1568 if (ret < 0)
1569 mlog_errno(ret);
1571 ocfs2_commit_trans(osb, handle);
1572 out_unlock:
1573 ocfs2_inode_unlock(xb_alloc_inode, 1);
1574 brelse(xb_alloc_bh);
1575 out_mutex:
1576 mutex_unlock(&xb_alloc_inode->i_mutex);
1577 iput(xb_alloc_inode);
1578 out:
1579 brelse(blk_bh);
1580 return ret;
1584 * ocfs2_xattr_remove()
1586 * Free extended attribute resources associated with this inode.
1588 int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
1590 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1591 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1592 handle_t *handle;
1593 int ret;
1595 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1596 return 0;
1598 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1599 return 0;
1601 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1602 ret = ocfs2_xattr_ibody_remove(inode, di_bh);
1603 if (ret < 0) {
1604 mlog_errno(ret);
1605 goto out;
1609 if (di->i_xattr_loc) {
1610 ret = ocfs2_xattr_free_block(inode,
1611 le64_to_cpu(di->i_xattr_loc));
1612 if (ret < 0) {
1613 mlog_errno(ret);
1614 goto out;
1618 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1619 OCFS2_INODE_UPDATE_CREDITS);
1620 if (IS_ERR(handle)) {
1621 ret = PTR_ERR(handle);
1622 mlog_errno(ret);
1623 goto out;
1625 ret = ocfs2_journal_access(handle, inode, di_bh,
1626 OCFS2_JOURNAL_ACCESS_WRITE);
1627 if (ret) {
1628 mlog_errno(ret);
1629 goto out_commit;
1632 di->i_xattr_loc = 0;
1634 spin_lock(&oi->ip_lock);
1635 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
1636 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1637 spin_unlock(&oi->ip_lock);
1639 ret = ocfs2_journal_dirty(handle, di_bh);
1640 if (ret < 0)
1641 mlog_errno(ret);
1642 out_commit:
1643 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1644 out:
1645 return ret;
1648 static int ocfs2_xattr_has_space_inline(struct inode *inode,
1649 struct ocfs2_dinode *di)
1651 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1652 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
1653 int free;
1655 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
1656 return 0;
1658 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1659 struct ocfs2_inline_data *idata = &di->id2.i_data;
1660 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
1661 } else if (ocfs2_inode_is_fast_symlink(inode)) {
1662 free = ocfs2_fast_symlink_chars(inode->i_sb) -
1663 le64_to_cpu(di->i_size);
1664 } else {
1665 struct ocfs2_extent_list *el = &di->id2.i_list;
1666 free = (le16_to_cpu(el->l_count) -
1667 le16_to_cpu(el->l_next_free_rec)) *
1668 sizeof(struct ocfs2_extent_rec);
1670 if (free >= xattrsize)
1671 return 1;
1673 return 0;
1677 * ocfs2_xattr_ibody_find()
1679 * Find extended attribute in inode block and
1680 * fill search info into struct ocfs2_xattr_search.
1682 static int ocfs2_xattr_ibody_find(struct inode *inode,
1683 int name_index,
1684 const char *name,
1685 struct ocfs2_xattr_search *xs)
1687 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1688 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1689 int ret;
1690 int has_space = 0;
1692 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1693 return 0;
1695 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1696 down_read(&oi->ip_alloc_sem);
1697 has_space = ocfs2_xattr_has_space_inline(inode, di);
1698 up_read(&oi->ip_alloc_sem);
1699 if (!has_space)
1700 return 0;
1703 xs->xattr_bh = xs->inode_bh;
1704 xs->end = (void *)di + inode->i_sb->s_blocksize;
1705 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
1706 xs->header = (struct ocfs2_xattr_header *)
1707 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1708 else
1709 xs->header = (struct ocfs2_xattr_header *)
1710 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
1711 xs->base = (void *)xs->header;
1712 xs->here = xs->header->xh_entries;
1714 /* Find the named attribute. */
1715 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1716 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1717 if (ret && ret != -ENODATA)
1718 return ret;
1719 xs->not_found = ret;
1722 return 0;
1726 * ocfs2_xattr_ibody_set()
1728 * Set, replace or remove an extended attribute into inode block.
1731 static int ocfs2_xattr_ibody_set(struct inode *inode,
1732 struct ocfs2_xattr_info *xi,
1733 struct ocfs2_xattr_search *xs)
1735 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1736 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1737 int ret;
1739 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1740 return -ENOSPC;
1742 down_write(&oi->ip_alloc_sem);
1743 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1744 if (!ocfs2_xattr_has_space_inline(inode, di)) {
1745 ret = -ENOSPC;
1746 goto out;
1750 ret = ocfs2_xattr_set_entry(inode, xi, xs,
1751 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
1752 out:
1753 up_write(&oi->ip_alloc_sem);
1755 return ret;
1759 * ocfs2_xattr_block_find()
1761 * Find extended attribute in external block and
1762 * fill search info into struct ocfs2_xattr_search.
1764 static int ocfs2_xattr_block_find(struct inode *inode,
1765 int name_index,
1766 const char *name,
1767 struct ocfs2_xattr_search *xs)
1769 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1770 struct buffer_head *blk_bh = NULL;
1771 struct ocfs2_xattr_block *xb;
1772 int ret = 0;
1774 if (!di->i_xattr_loc)
1775 return ret;
1777 ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh);
1778 if (ret < 0) {
1779 mlog_errno(ret);
1780 return ret;
1783 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1784 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
1785 ret = -EIO;
1786 goto cleanup;
1789 xs->xattr_bh = blk_bh;
1791 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1792 xs->header = &xb->xb_attrs.xb_header;
1793 xs->base = (void *)xs->header;
1794 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
1795 xs->here = xs->header->xh_entries;
1797 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1798 } else
1799 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
1800 name_index,
1801 name, xs);
1803 if (ret && ret != -ENODATA) {
1804 xs->xattr_bh = NULL;
1805 goto cleanup;
1807 xs->not_found = ret;
1808 return 0;
1809 cleanup:
1810 brelse(blk_bh);
1812 return ret;
1816 * When all the xattrs are deleted from index btree, the ocfs2_xattr_tree
1817 * will be erased and ocfs2_xattr_block will have its ocfs2_xattr_header
1818 * re-initialized.
1820 static int ocfs2_restore_xattr_block(struct inode *inode,
1821 struct ocfs2_xattr_search *xs)
1823 int ret;
1824 handle_t *handle;
1825 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1826 struct ocfs2_xattr_block *xb =
1827 (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1828 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
1829 u16 xb_flags = le16_to_cpu(xb->xb_flags);
1831 BUG_ON(!(xb_flags & OCFS2_XATTR_INDEXED) ||
1832 le16_to_cpu(el->l_next_free_rec) != 0);
1834 handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1835 if (IS_ERR(handle)) {
1836 ret = PTR_ERR(handle);
1837 handle = NULL;
1838 goto out;
1841 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1842 OCFS2_JOURNAL_ACCESS_WRITE);
1843 if (ret < 0) {
1844 mlog_errno(ret);
1845 goto out_commit;
1848 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
1849 offsetof(struct ocfs2_xattr_block, xb_attrs));
1851 xb->xb_flags = cpu_to_le16(xb_flags & ~OCFS2_XATTR_INDEXED);
1853 ocfs2_journal_dirty(handle, xs->xattr_bh);
1855 out_commit:
1856 ocfs2_commit_trans(osb, handle);
1857 out:
1858 return ret;
1862 * ocfs2_xattr_block_set()
1864 * Set, replace or remove an extended attribute into external block.
1867 static int ocfs2_xattr_block_set(struct inode *inode,
1868 struct ocfs2_xattr_info *xi,
1869 struct ocfs2_xattr_search *xs)
1871 struct buffer_head *new_bh = NULL;
1872 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1873 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1874 struct ocfs2_alloc_context *meta_ac = NULL;
1875 handle_t *handle = NULL;
1876 struct ocfs2_xattr_block *xblk = NULL;
1877 u16 suballoc_bit_start;
1878 u32 num_got;
1879 u64 first_blkno;
1880 int ret;
1882 if (!xs->xattr_bh) {
1884 * Alloc one external block for extended attribute
1885 * outside of inode.
1887 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
1888 if (ret < 0) {
1889 mlog_errno(ret);
1890 goto out;
1892 handle = ocfs2_start_trans(osb,
1893 OCFS2_XATTR_BLOCK_CREATE_CREDITS);
1894 if (IS_ERR(handle)) {
1895 ret = PTR_ERR(handle);
1896 mlog_errno(ret);
1897 goto out;
1899 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1900 OCFS2_JOURNAL_ACCESS_CREATE);
1901 if (ret < 0) {
1902 mlog_errno(ret);
1903 goto out_commit;
1906 ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
1907 &suballoc_bit_start, &num_got,
1908 &first_blkno);
1909 if (ret < 0) {
1910 mlog_errno(ret);
1911 goto out_commit;
1914 new_bh = sb_getblk(inode->i_sb, first_blkno);
1915 ocfs2_set_new_buffer_uptodate(inode, new_bh);
1917 ret = ocfs2_journal_access(handle, inode, new_bh,
1918 OCFS2_JOURNAL_ACCESS_CREATE);
1919 if (ret < 0) {
1920 mlog_errno(ret);
1921 goto out_commit;
1924 /* Initialize ocfs2_xattr_block */
1925 xs->xattr_bh = new_bh;
1926 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
1927 memset(xblk, 0, inode->i_sb->s_blocksize);
1928 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
1929 xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
1930 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1931 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
1932 xblk->xb_blkno = cpu_to_le64(first_blkno);
1934 xs->header = &xblk->xb_attrs.xb_header;
1935 xs->base = (void *)xs->header;
1936 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
1937 xs->here = xs->header->xh_entries;
1940 ret = ocfs2_journal_dirty(handle, new_bh);
1941 if (ret < 0) {
1942 mlog_errno(ret);
1943 goto out_commit;
1945 di->i_xattr_loc = cpu_to_le64(first_blkno);
1946 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1947 if (ret < 0)
1948 mlog_errno(ret);
1949 out_commit:
1950 ocfs2_commit_trans(osb, handle);
1951 out:
1952 if (meta_ac)
1953 ocfs2_free_alloc_context(meta_ac);
1954 if (ret < 0)
1955 return ret;
1956 } else
1957 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1959 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
1960 /* Set extended attribute into external block */
1961 ret = ocfs2_xattr_set_entry(inode, xi, xs, OCFS2_HAS_XATTR_FL);
1962 if (!ret || ret != -ENOSPC)
1963 goto end;
1965 ret = ocfs2_xattr_create_index_block(inode, xs);
1966 if (ret)
1967 goto end;
1970 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs);
1971 if (!ret && xblk->xb_attrs.xb_root.xt_list.l_next_free_rec == 0)
1972 ret = ocfs2_restore_xattr_block(inode, xs);
1974 end:
1976 return ret;
1980 * ocfs2_xattr_set()
1982 * Set, replace or remove an extended attribute for this inode.
1983 * value is NULL to remove an existing extended attribute, else either
1984 * create or replace an extended attribute.
1986 int ocfs2_xattr_set(struct inode *inode,
1987 int name_index,
1988 const char *name,
1989 const void *value,
1990 size_t value_len,
1991 int flags)
1993 struct buffer_head *di_bh = NULL;
1994 struct ocfs2_dinode *di;
1995 int ret;
1996 u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
1998 struct ocfs2_xattr_info xi = {
1999 .name_index = name_index,
2000 .name = name,
2001 .value = value,
2002 .value_len = value_len,
2005 struct ocfs2_xattr_search xis = {
2006 .not_found = -ENODATA,
2009 struct ocfs2_xattr_search xbs = {
2010 .not_found = -ENODATA,
2013 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2014 return -EOPNOTSUPP;
2016 ret = ocfs2_inode_lock(inode, &di_bh, 1);
2017 if (ret < 0) {
2018 mlog_errno(ret);
2019 return ret;
2021 xis.inode_bh = xbs.inode_bh = di_bh;
2022 di = (struct ocfs2_dinode *)di_bh->b_data;
2024 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2026 * Scan inode and external block to find the same name
2027 * extended attribute and collect search infomation.
2029 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2030 if (ret)
2031 goto cleanup;
2032 if (xis.not_found) {
2033 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2034 if (ret)
2035 goto cleanup;
2038 if (xis.not_found && xbs.not_found) {
2039 ret = -ENODATA;
2040 if (flags & XATTR_REPLACE)
2041 goto cleanup;
2042 ret = 0;
2043 if (!value)
2044 goto cleanup;
2045 } else {
2046 ret = -EEXIST;
2047 if (flags & XATTR_CREATE)
2048 goto cleanup;
2051 if (!value) {
2052 /* Remove existing extended attribute */
2053 if (!xis.not_found)
2054 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2055 else if (!xbs.not_found)
2056 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2057 } else {
2058 /* We always try to set extended attribute into inode first*/
2059 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2060 if (!ret && !xbs.not_found) {
2062 * If succeed and that extended attribute existing in
2063 * external block, then we will remove it.
2065 xi.value = NULL;
2066 xi.value_len = 0;
2067 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2068 } else if (ret == -ENOSPC) {
2069 if (di->i_xattr_loc && !xbs.xattr_bh) {
2070 ret = ocfs2_xattr_block_find(inode, name_index,
2071 name, &xbs);
2072 if (ret)
2073 goto cleanup;
2076 * If no space in inode, we will set extended attribute
2077 * into external block.
2079 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2080 if (ret)
2081 goto cleanup;
2082 if (!xis.not_found) {
2084 * If succeed and that extended attribute
2085 * existing in inode, we will remove it.
2087 xi.value = NULL;
2088 xi.value_len = 0;
2089 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2093 cleanup:
2094 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2095 ocfs2_inode_unlock(inode, 1);
2096 brelse(di_bh);
2097 brelse(xbs.xattr_bh);
2098 for (i = 0; i < blk_per_bucket; i++)
2099 brelse(xbs.bucket.bhs[i]);
2101 return ret;
2105 * Find the xattr extent rec which may contains name_hash.
2106 * e_cpos will be the first name hash of the xattr rec.
2107 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
2109 static int ocfs2_xattr_get_rec(struct inode *inode,
2110 u32 name_hash,
2111 u64 *p_blkno,
2112 u32 *e_cpos,
2113 u32 *num_clusters,
2114 struct ocfs2_extent_list *el)
2116 int ret = 0, i;
2117 struct buffer_head *eb_bh = NULL;
2118 struct ocfs2_extent_block *eb;
2119 struct ocfs2_extent_rec *rec = NULL;
2120 u64 e_blkno = 0;
2122 if (el->l_tree_depth) {
2123 ret = ocfs2_find_leaf(inode, el, name_hash, &eb_bh);
2124 if (ret) {
2125 mlog_errno(ret);
2126 goto out;
2129 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
2130 el = &eb->h_list;
2132 if (el->l_tree_depth) {
2133 ocfs2_error(inode->i_sb,
2134 "Inode %lu has non zero tree depth in "
2135 "xattr tree block %llu\n", inode->i_ino,
2136 (unsigned long long)eb_bh->b_blocknr);
2137 ret = -EROFS;
2138 goto out;
2142 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
2143 rec = &el->l_recs[i];
2145 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
2146 e_blkno = le64_to_cpu(rec->e_blkno);
2147 break;
2151 if (!e_blkno) {
2152 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
2153 "record (%u, %u, 0) in xattr", inode->i_ino,
2154 le32_to_cpu(rec->e_cpos),
2155 ocfs2_rec_clusters(el, rec));
2156 ret = -EROFS;
2157 goto out;
2160 *p_blkno = le64_to_cpu(rec->e_blkno);
2161 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
2162 if (e_cpos)
2163 *e_cpos = le32_to_cpu(rec->e_cpos);
2164 out:
2165 brelse(eb_bh);
2166 return ret;
2169 typedef int (xattr_bucket_func)(struct inode *inode,
2170 struct ocfs2_xattr_bucket *bucket,
2171 void *para);
2173 static int ocfs2_find_xe_in_bucket(struct inode *inode,
2174 struct buffer_head *header_bh,
2175 int name_index,
2176 const char *name,
2177 u32 name_hash,
2178 u16 *xe_index,
2179 int *found)
2181 int i, ret = 0, cmp = 1, block_off, new_offset;
2182 struct ocfs2_xattr_header *xh =
2183 (struct ocfs2_xattr_header *)header_bh->b_data;
2184 size_t name_len = strlen(name);
2185 struct ocfs2_xattr_entry *xe = NULL;
2186 struct buffer_head *name_bh = NULL;
2187 char *xe_name;
2190 * We don't use binary search in the bucket because there
2191 * may be multiple entries with the same name hash.
2193 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
2194 xe = &xh->xh_entries[i];
2196 if (name_hash > le32_to_cpu(xe->xe_name_hash))
2197 continue;
2198 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
2199 break;
2201 cmp = name_index - ocfs2_xattr_get_type(xe);
2202 if (!cmp)
2203 cmp = name_len - xe->xe_name_len;
2204 if (cmp)
2205 continue;
2207 ret = ocfs2_xattr_bucket_get_name_value(inode,
2210 &block_off,
2211 &new_offset);
2212 if (ret) {
2213 mlog_errno(ret);
2214 break;
2217 ret = ocfs2_read_block(inode, header_bh->b_blocknr + block_off,
2218 &name_bh);
2219 if (ret) {
2220 mlog_errno(ret);
2221 break;
2223 xe_name = name_bh->b_data + new_offset;
2225 cmp = memcmp(name, xe_name, name_len);
2226 brelse(name_bh);
2227 name_bh = NULL;
2229 if (cmp == 0) {
2230 *xe_index = i;
2231 *found = 1;
2232 ret = 0;
2233 break;
2237 return ret;
2241 * Find the specified xattr entry in a series of buckets.
2242 * This series start from p_blkno and last for num_clusters.
2243 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
2244 * the num of the valid buckets.
2246 * Return the buffer_head this xattr should reside in. And if the xattr's
2247 * hash is in the gap of 2 buckets, return the lower bucket.
2249 static int ocfs2_xattr_bucket_find(struct inode *inode,
2250 int name_index,
2251 const char *name,
2252 u32 name_hash,
2253 u64 p_blkno,
2254 u32 first_hash,
2255 u32 num_clusters,
2256 struct ocfs2_xattr_search *xs)
2258 int ret, found = 0;
2259 struct buffer_head *bh = NULL;
2260 struct buffer_head *lower_bh = NULL;
2261 struct ocfs2_xattr_header *xh = NULL;
2262 struct ocfs2_xattr_entry *xe = NULL;
2263 u16 index = 0;
2264 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2265 int low_bucket = 0, bucket, high_bucket;
2266 u32 last_hash;
2267 u64 blkno;
2269 ret = ocfs2_read_block(inode, p_blkno, &bh);
2270 if (ret) {
2271 mlog_errno(ret);
2272 goto out;
2275 xh = (struct ocfs2_xattr_header *)bh->b_data;
2276 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
2278 while (low_bucket <= high_bucket) {
2279 brelse(bh);
2280 bh = NULL;
2281 bucket = (low_bucket + high_bucket) / 2;
2283 blkno = p_blkno + bucket * blk_per_bucket;
2285 ret = ocfs2_read_block(inode, blkno, &bh);
2286 if (ret) {
2287 mlog_errno(ret);
2288 goto out;
2291 xh = (struct ocfs2_xattr_header *)bh->b_data;
2292 xe = &xh->xh_entries[0];
2293 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
2294 high_bucket = bucket - 1;
2295 continue;
2299 * Check whether the hash of the last entry in our
2300 * bucket is larger than the search one. for an empty
2301 * bucket, the last one is also the first one.
2303 if (xh->xh_count)
2304 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
2306 last_hash = le32_to_cpu(xe->xe_name_hash);
2308 /* record lower_bh which may be the insert place. */
2309 brelse(lower_bh);
2310 lower_bh = bh;
2311 bh = NULL;
2313 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
2314 low_bucket = bucket + 1;
2315 continue;
2318 /* the searched xattr should reside in this bucket if exists. */
2319 ret = ocfs2_find_xe_in_bucket(inode, lower_bh,
2320 name_index, name, name_hash,
2321 &index, &found);
2322 if (ret) {
2323 mlog_errno(ret);
2324 goto out;
2326 break;
2330 * Record the bucket we have found.
2331 * When the xattr's hash value is in the gap of 2 buckets, we will
2332 * always set it to the previous bucket.
2334 if (!lower_bh) {
2336 * We can't find any bucket whose first name_hash is less
2337 * than the find name_hash.
2339 BUG_ON(bh->b_blocknr != p_blkno);
2340 lower_bh = bh;
2341 bh = NULL;
2343 xs->bucket.bhs[0] = lower_bh;
2344 xs->bucket.xh = (struct ocfs2_xattr_header *)
2345 xs->bucket.bhs[0]->b_data;
2346 lower_bh = NULL;
2348 xs->header = xs->bucket.xh;
2349 xs->base = xs->bucket.bhs[0]->b_data;
2350 xs->end = xs->base + inode->i_sb->s_blocksize;
2352 if (found) {
2354 * If we have found the xattr enty, read all the blocks in
2355 * this bucket.
2357 ret = ocfs2_read_blocks(inode, xs->bucket.bhs[0]->b_blocknr + 1,
2358 blk_per_bucket - 1, &xs->bucket.bhs[1],
2360 if (ret) {
2361 mlog_errno(ret);
2362 goto out;
2365 xs->here = &xs->header->xh_entries[index];
2366 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
2367 (unsigned long long)xs->bucket.bhs[0]->b_blocknr, index);
2368 } else
2369 ret = -ENODATA;
2371 out:
2372 brelse(bh);
2373 brelse(lower_bh);
2374 return ret;
2377 static int ocfs2_xattr_index_block_find(struct inode *inode,
2378 struct buffer_head *root_bh,
2379 int name_index,
2380 const char *name,
2381 struct ocfs2_xattr_search *xs)
2383 int ret;
2384 struct ocfs2_xattr_block *xb =
2385 (struct ocfs2_xattr_block *)root_bh->b_data;
2386 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
2387 struct ocfs2_extent_list *el = &xb_root->xt_list;
2388 u64 p_blkno = 0;
2389 u32 first_hash, num_clusters = 0;
2390 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
2392 if (le16_to_cpu(el->l_next_free_rec) == 0)
2393 return -ENODATA;
2395 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
2396 name, name_hash, name_index);
2398 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
2399 &num_clusters, el);
2400 if (ret) {
2401 mlog_errno(ret);
2402 goto out;
2405 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
2407 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
2408 "in the rec is %u\n", num_clusters, p_blkno, first_hash);
2410 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
2411 p_blkno, first_hash, num_clusters, xs);
2413 out:
2414 return ret;
2417 static int ocfs2_iterate_xattr_buckets(struct inode *inode,
2418 u64 blkno,
2419 u32 clusters,
2420 xattr_bucket_func *func,
2421 void *para)
2423 int i, j, ret = 0;
2424 int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2425 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
2426 u32 num_buckets = clusters * bpc;
2427 struct ocfs2_xattr_bucket bucket;
2429 memset(&bucket, 0, sizeof(bucket));
2431 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
2432 clusters, blkno);
2434 for (i = 0; i < num_buckets; i++, blkno += blk_per_bucket) {
2435 ret = ocfs2_read_blocks(inode, blkno, blk_per_bucket,
2436 bucket.bhs, 0);
2437 if (ret) {
2438 mlog_errno(ret);
2439 goto out;
2442 bucket.xh = (struct ocfs2_xattr_header *)bucket.bhs[0]->b_data;
2444 * The real bucket num in this series of blocks is stored
2445 * in the 1st bucket.
2447 if (i == 0)
2448 num_buckets = le16_to_cpu(bucket.xh->xh_num_buckets);
2450 mlog(0, "iterating xattr bucket %llu, first hash %u\n", blkno,
2451 le32_to_cpu(bucket.xh->xh_entries[0].xe_name_hash));
2452 if (func) {
2453 ret = func(inode, &bucket, para);
2454 if (ret) {
2455 mlog_errno(ret);
2456 break;
2460 for (j = 0; j < blk_per_bucket; j++)
2461 brelse(bucket.bhs[j]);
2462 memset(&bucket, 0, sizeof(bucket));
2465 out:
2466 for (j = 0; j < blk_per_bucket; j++)
2467 brelse(bucket.bhs[j]);
2469 return ret;
2472 struct ocfs2_xattr_tree_list {
2473 char *buffer;
2474 size_t buffer_size;
2475 size_t result;
2478 static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
2479 struct ocfs2_xattr_header *xh,
2480 int index,
2481 int *block_off,
2482 int *new_offset)
2484 u16 name_offset;
2486 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
2487 return -EINVAL;
2489 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
2491 *block_off = name_offset >> inode->i_sb->s_blocksize_bits;
2492 *new_offset = name_offset % inode->i_sb->s_blocksize;
2494 return 0;
2497 static int ocfs2_list_xattr_bucket(struct inode *inode,
2498 struct ocfs2_xattr_bucket *bucket,
2499 void *para)
2501 int ret = 0, type;
2502 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
2503 int i, block_off, new_offset;
2504 const char *prefix, *name;
2506 for (i = 0 ; i < le16_to_cpu(bucket->xh->xh_count); i++) {
2507 struct ocfs2_xattr_entry *entry = &bucket->xh->xh_entries[i];
2508 type = ocfs2_xattr_get_type(entry);
2509 prefix = ocfs2_xattr_prefix(type);
2511 if (prefix) {
2512 ret = ocfs2_xattr_bucket_get_name_value(inode,
2513 bucket->xh,
2515 &block_off,
2516 &new_offset);
2517 if (ret)
2518 break;
2520 name = (const char *)bucket->bhs[block_off]->b_data +
2521 new_offset;
2522 ret = ocfs2_xattr_list_entry(xl->buffer,
2523 xl->buffer_size,
2524 &xl->result,
2525 prefix, name,
2526 entry->xe_name_len);
2527 if (ret)
2528 break;
2532 return ret;
2535 static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
2536 struct ocfs2_xattr_tree_root *xt,
2537 char *buffer,
2538 size_t buffer_size)
2540 struct ocfs2_extent_list *el = &xt->xt_list;
2541 int ret = 0;
2542 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
2543 u64 p_blkno = 0;
2544 struct ocfs2_xattr_tree_list xl = {
2545 .buffer = buffer,
2546 .buffer_size = buffer_size,
2547 .result = 0,
2550 if (le16_to_cpu(el->l_next_free_rec) == 0)
2551 return 0;
2553 while (name_hash > 0) {
2554 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
2555 &e_cpos, &num_clusters, el);
2556 if (ret) {
2557 mlog_errno(ret);
2558 goto out;
2561 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
2562 ocfs2_list_xattr_bucket,
2563 &xl);
2564 if (ret) {
2565 mlog_errno(ret);
2566 goto out;
2569 if (e_cpos == 0)
2570 break;
2572 name_hash = e_cpos - 1;
2575 ret = xl.result;
2576 out:
2577 return ret;
2580 static int cmp_xe(const void *a, const void *b)
2582 const struct ocfs2_xattr_entry *l = a, *r = b;
2583 u32 l_hash = le32_to_cpu(l->xe_name_hash);
2584 u32 r_hash = le32_to_cpu(r->xe_name_hash);
2586 if (l_hash > r_hash)
2587 return 1;
2588 if (l_hash < r_hash)
2589 return -1;
2590 return 0;
2593 static void swap_xe(void *a, void *b, int size)
2595 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
2597 tmp = *l;
2598 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
2599 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
2603 * When the ocfs2_xattr_block is filled up, new bucket will be created
2604 * and all the xattr entries will be moved to the new bucket.
2605 * Note: we need to sort the entries since they are not saved in order
2606 * in the ocfs2_xattr_block.
2608 static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
2609 struct buffer_head *xb_bh,
2610 struct buffer_head *xh_bh,
2611 struct buffer_head *data_bh)
2613 int i, blocksize = inode->i_sb->s_blocksize;
2614 u16 offset, size, off_change;
2615 struct ocfs2_xattr_entry *xe;
2616 struct ocfs2_xattr_block *xb =
2617 (struct ocfs2_xattr_block *)xb_bh->b_data;
2618 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
2619 struct ocfs2_xattr_header *xh =
2620 (struct ocfs2_xattr_header *)xh_bh->b_data;
2621 u16 count = le16_to_cpu(xb_xh->xh_count);
2622 char *target = xh_bh->b_data, *src = xb_bh->b_data;
2624 mlog(0, "cp xattr from block %llu to bucket %llu\n",
2625 (unsigned long long)xb_bh->b_blocknr,
2626 (unsigned long long)xh_bh->b_blocknr);
2628 memset(xh_bh->b_data, 0, blocksize);
2629 if (data_bh)
2630 memset(data_bh->b_data, 0, blocksize);
2632 * Since the xe_name_offset is based on ocfs2_xattr_header,
2633 * there is a offset change corresponding to the change of
2634 * ocfs2_xattr_header's position.
2636 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
2637 xe = &xb_xh->xh_entries[count - 1];
2638 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
2639 size = blocksize - offset;
2641 /* copy all the names and values. */
2642 if (data_bh)
2643 target = data_bh->b_data;
2644 memcpy(target + offset, src + offset, size);
2646 /* Init new header now. */
2647 xh->xh_count = xb_xh->xh_count;
2648 xh->xh_num_buckets = cpu_to_le16(1);
2649 xh->xh_name_value_len = cpu_to_le16(size);
2650 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
2652 /* copy all the entries. */
2653 target = xh_bh->b_data;
2654 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
2655 size = count * sizeof(struct ocfs2_xattr_entry);
2656 memcpy(target + offset, (char *)xb_xh + offset, size);
2658 /* Change the xe offset for all the xe because of the move. */
2659 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
2660 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
2661 for (i = 0; i < count; i++)
2662 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
2664 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
2665 offset, size, off_change);
2667 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
2668 cmp_xe, swap_xe);
2672 * After we move xattr from block to index btree, we have to
2673 * update ocfs2_xattr_search to the new xe and base.
2675 * When the entry is in xattr block, xattr_bh indicates the storage place.
2676 * While if the entry is in index b-tree, "bucket" indicates the
2677 * real place of the xattr.
2679 static int ocfs2_xattr_update_xattr_search(struct inode *inode,
2680 struct ocfs2_xattr_search *xs,
2681 struct buffer_head *old_bh,
2682 struct buffer_head *new_bh)
2684 int ret = 0;
2685 char *buf = old_bh->b_data;
2686 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
2687 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
2688 int i, blocksize = inode->i_sb->s_blocksize;
2689 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2691 xs->bucket.bhs[0] = new_bh;
2692 get_bh(new_bh);
2693 xs->bucket.xh = (struct ocfs2_xattr_header *)xs->bucket.bhs[0]->b_data;
2694 xs->header = xs->bucket.xh;
2696 xs->base = new_bh->b_data;
2697 xs->end = xs->base + inode->i_sb->s_blocksize;
2699 if (!xs->not_found) {
2700 if (OCFS2_XATTR_BUCKET_SIZE != blocksize) {
2701 ret = ocfs2_read_blocks(inode,
2702 xs->bucket.bhs[0]->b_blocknr + 1,
2703 blk_per_bucket - 1, &xs->bucket.bhs[1],
2705 if (ret) {
2706 mlog_errno(ret);
2707 return ret;
2710 i = xs->here - old_xh->xh_entries;
2711 xs->here = &xs->header->xh_entries[i];
2715 return ret;
2718 static int ocfs2_xattr_create_index_block(struct inode *inode,
2719 struct ocfs2_xattr_search *xs)
2721 int ret, credits = OCFS2_SUBALLOC_ALLOC;
2722 u32 bit_off, len;
2723 u64 blkno;
2724 handle_t *handle;
2725 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2726 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2727 struct ocfs2_alloc_context *data_ac;
2728 struct buffer_head *xh_bh = NULL, *data_bh = NULL;
2729 struct buffer_head *xb_bh = xs->xattr_bh;
2730 struct ocfs2_xattr_block *xb =
2731 (struct ocfs2_xattr_block *)xb_bh->b_data;
2732 struct ocfs2_xattr_tree_root *xr;
2733 u16 xb_flags = le16_to_cpu(xb->xb_flags);
2734 u16 bpb = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2736 mlog(0, "create xattr index block for %llu\n",
2737 (unsigned long long)xb_bh->b_blocknr);
2739 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
2741 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
2742 if (ret) {
2743 mlog_errno(ret);
2744 goto out;
2748 * XXX:
2749 * We can use this lock for now, and maybe move to a dedicated mutex
2750 * if performance becomes a problem later.
2752 down_write(&oi->ip_alloc_sem);
2755 * 3 more credits, one for xattr block update, one for the 1st block
2756 * of the new xattr bucket and one for the value/data.
2758 credits += 3;
2759 handle = ocfs2_start_trans(osb, credits);
2760 if (IS_ERR(handle)) {
2761 ret = PTR_ERR(handle);
2762 mlog_errno(ret);
2763 goto out_sem;
2766 ret = ocfs2_journal_access(handle, inode, xb_bh,
2767 OCFS2_JOURNAL_ACCESS_WRITE);
2768 if (ret) {
2769 mlog_errno(ret);
2770 goto out_commit;
2773 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
2774 if (ret) {
2775 mlog_errno(ret);
2776 goto out_commit;
2780 * The bucket may spread in many blocks, and
2781 * we will only touch the 1st block and the last block
2782 * in the whole bucket(one for entry and one for data).
2784 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
2786 mlog(0, "allocate 1 cluster from %llu to xattr block\n", blkno);
2788 xh_bh = sb_getblk(inode->i_sb, blkno);
2789 if (!xh_bh) {
2790 ret = -EIO;
2791 mlog_errno(ret);
2792 goto out_commit;
2795 ocfs2_set_new_buffer_uptodate(inode, xh_bh);
2797 ret = ocfs2_journal_access(handle, inode, xh_bh,
2798 OCFS2_JOURNAL_ACCESS_CREATE);
2799 if (ret) {
2800 mlog_errno(ret);
2801 goto out_commit;
2804 if (bpb > 1) {
2805 data_bh = sb_getblk(inode->i_sb, blkno + bpb - 1);
2806 if (!data_bh) {
2807 ret = -EIO;
2808 mlog_errno(ret);
2809 goto out_commit;
2812 ocfs2_set_new_buffer_uptodate(inode, data_bh);
2814 ret = ocfs2_journal_access(handle, inode, data_bh,
2815 OCFS2_JOURNAL_ACCESS_CREATE);
2816 if (ret) {
2817 mlog_errno(ret);
2818 goto out_commit;
2822 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xh_bh, data_bh);
2824 ocfs2_journal_dirty(handle, xh_bh);
2825 if (data_bh)
2826 ocfs2_journal_dirty(handle, data_bh);
2828 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh, xh_bh);
2830 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
2831 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
2832 offsetof(struct ocfs2_xattr_block, xb_attrs));
2834 xr = &xb->xb_attrs.xb_root;
2835 xr->xt_clusters = cpu_to_le32(1);
2836 xr->xt_last_eb_blk = 0;
2837 xr->xt_list.l_tree_depth = 0;
2838 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
2839 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2841 xr->xt_list.l_recs[0].e_cpos = 0;
2842 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
2843 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
2845 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
2847 ret = ocfs2_journal_dirty(handle, xb_bh);
2848 if (ret) {
2849 mlog_errno(ret);
2850 goto out_commit;
2853 out_commit:
2854 ocfs2_commit_trans(osb, handle);
2856 out_sem:
2857 up_write(&oi->ip_alloc_sem);
2859 out:
2860 if (data_ac)
2861 ocfs2_free_alloc_context(data_ac);
2863 brelse(xh_bh);
2864 brelse(data_bh);
2866 return ret;
2869 static int cmp_xe_offset(const void *a, const void *b)
2871 const struct ocfs2_xattr_entry *l = a, *r = b;
2872 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
2873 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
2875 if (l_name_offset < r_name_offset)
2876 return 1;
2877 if (l_name_offset > r_name_offset)
2878 return -1;
2879 return 0;
2883 * defrag a xattr bucket if we find that the bucket has some
2884 * holes beteen name/value pairs.
2885 * We will move all the name/value pairs to the end of the bucket
2886 * so that we can spare some space for insertion.
2888 static int ocfs2_defrag_xattr_bucket(struct inode *inode,
2889 struct ocfs2_xattr_bucket *bucket)
2891 int ret, i;
2892 size_t end, offset, len, value_len;
2893 struct ocfs2_xattr_header *xh;
2894 char *entries, *buf, *bucket_buf = NULL;
2895 u64 blkno = bucket->bhs[0]->b_blocknr;
2896 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2897 u16 xh_free_start;
2898 size_t blocksize = inode->i_sb->s_blocksize;
2899 handle_t *handle;
2900 struct buffer_head **bhs;
2901 struct ocfs2_xattr_entry *xe;
2903 bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
2904 GFP_NOFS);
2905 if (!bhs)
2906 return -ENOMEM;
2908 ret = ocfs2_read_blocks(inode, blkno, blk_per_bucket, bhs, 0);
2909 if (ret)
2910 goto out;
2913 * In order to make the operation more efficient and generic,
2914 * we copy all the blocks into a contiguous memory and do the
2915 * defragment there, so if anything is error, we will not touch
2916 * the real block.
2918 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
2919 if (!bucket_buf) {
2920 ret = -EIO;
2921 goto out;
2924 buf = bucket_buf;
2925 for (i = 0; i < blk_per_bucket; i++, buf += blocksize)
2926 memcpy(buf, bhs[i]->b_data, blocksize);
2928 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), blk_per_bucket);
2929 if (IS_ERR(handle)) {
2930 ret = PTR_ERR(handle);
2931 handle = NULL;
2932 mlog_errno(ret);
2933 goto out;
2936 for (i = 0; i < blk_per_bucket; i++) {
2937 ret = ocfs2_journal_access(handle, inode, bhs[i],
2938 OCFS2_JOURNAL_ACCESS_WRITE);
2939 if (ret < 0) {
2940 mlog_errno(ret);
2941 goto commit;
2945 xh = (struct ocfs2_xattr_header *)bucket_buf;
2946 entries = (char *)xh->xh_entries;
2947 xh_free_start = le16_to_cpu(xh->xh_free_start);
2949 mlog(0, "adjust xattr bucket in %llu, count = %u, "
2950 "xh_free_start = %u, xh_name_value_len = %u.\n",
2951 blkno, le16_to_cpu(xh->xh_count), xh_free_start,
2952 le16_to_cpu(xh->xh_name_value_len));
2955 * sort all the entries by their offset.
2956 * the largest will be the first, so that we can
2957 * move them to the end one by one.
2959 sort(entries, le16_to_cpu(xh->xh_count),
2960 sizeof(struct ocfs2_xattr_entry),
2961 cmp_xe_offset, swap_xe);
2963 /* Move all name/values to the end of the bucket. */
2964 xe = xh->xh_entries;
2965 end = OCFS2_XATTR_BUCKET_SIZE;
2966 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
2967 offset = le16_to_cpu(xe->xe_name_offset);
2968 if (ocfs2_xattr_is_local(xe))
2969 value_len = OCFS2_XATTR_SIZE(
2970 le64_to_cpu(xe->xe_value_size));
2971 else
2972 value_len = OCFS2_XATTR_ROOT_SIZE;
2973 len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
2976 * We must make sure that the name/value pair
2977 * exist in the same block. So adjust end to
2978 * the previous block end if needed.
2980 if (((end - len) / blocksize !=
2981 (end - 1) / blocksize))
2982 end = end - end % blocksize;
2984 if (end > offset + len) {
2985 memmove(bucket_buf + end - len,
2986 bucket_buf + offset, len);
2987 xe->xe_name_offset = cpu_to_le16(end - len);
2990 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
2991 "bucket %llu\n", (unsigned long long)blkno);
2993 end -= len;
2996 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
2997 "bucket %llu\n", (unsigned long long)blkno);
2999 if (xh_free_start == end)
3000 goto commit;
3002 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
3003 xh->xh_free_start = cpu_to_le16(end);
3005 /* sort the entries by their name_hash. */
3006 sort(entries, le16_to_cpu(xh->xh_count),
3007 sizeof(struct ocfs2_xattr_entry),
3008 cmp_xe, swap_xe);
3010 buf = bucket_buf;
3011 for (i = 0; i < blk_per_bucket; i++, buf += blocksize) {
3012 memcpy(bhs[i]->b_data, buf, blocksize);
3013 ocfs2_journal_dirty(handle, bhs[i]);
3016 commit:
3017 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
3018 out:
3020 if (bhs) {
3021 for (i = 0; i < blk_per_bucket; i++)
3022 brelse(bhs[i]);
3024 kfree(bhs);
3026 kfree(bucket_buf);
3027 return ret;
3031 * Move half nums of the xattr bucket in the previous cluster to this new
3032 * cluster. We only touch the last cluster of the previous extend record.
3034 * first_bh is the first buffer_head of a series of bucket in the same
3035 * extent rec and header_bh is the header of one bucket in this cluster.
3036 * They will be updated if we move the data header_bh contains to the new
3037 * cluster. first_hash will be set as the 1st xe's name_hash of the new cluster.
3039 static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
3040 handle_t *handle,
3041 struct buffer_head **first_bh,
3042 struct buffer_head **header_bh,
3043 u64 new_blkno,
3044 u64 prev_blkno,
3045 u32 num_clusters,
3046 u32 *first_hash)
3048 int i, ret, credits;
3049 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3050 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3051 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3052 int blocksize = inode->i_sb->s_blocksize;
3053 struct buffer_head *old_bh, *new_bh, *prev_bh, *new_first_bh = NULL;
3054 struct ocfs2_xattr_header *new_xh;
3055 struct ocfs2_xattr_header *xh =
3056 (struct ocfs2_xattr_header *)((*first_bh)->b_data);
3058 BUG_ON(le16_to_cpu(xh->xh_num_buckets) < num_buckets);
3059 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == osb->s_clustersize);
3061 prev_bh = *first_bh;
3062 get_bh(prev_bh);
3063 xh = (struct ocfs2_xattr_header *)prev_bh->b_data;
3065 prev_blkno += (num_clusters - 1) * bpc + bpc / 2;
3067 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
3068 prev_blkno, new_blkno);
3071 * We need to update the 1st half of the new cluster and
3072 * 1 more for the update of the 1st bucket of the previous
3073 * extent record.
3075 credits = bpc / 2 + 1;
3076 ret = ocfs2_extend_trans(handle, credits);
3077 if (ret) {
3078 mlog_errno(ret);
3079 goto out;
3082 ret = ocfs2_journal_access(handle, inode, prev_bh,
3083 OCFS2_JOURNAL_ACCESS_WRITE);
3084 if (ret) {
3085 mlog_errno(ret);
3086 goto out;
3089 for (i = 0; i < bpc / 2; i++, prev_blkno++, new_blkno++) {
3090 old_bh = new_bh = NULL;
3091 new_bh = sb_getblk(inode->i_sb, new_blkno);
3092 if (!new_bh) {
3093 ret = -EIO;
3094 mlog_errno(ret);
3095 goto out;
3098 ocfs2_set_new_buffer_uptodate(inode, new_bh);
3100 ret = ocfs2_journal_access(handle, inode, new_bh,
3101 OCFS2_JOURNAL_ACCESS_CREATE);
3102 if (ret < 0) {
3103 mlog_errno(ret);
3104 brelse(new_bh);
3105 goto out;
3108 ret = ocfs2_read_block(inode, prev_blkno, &old_bh);
3109 if (ret < 0) {
3110 mlog_errno(ret);
3111 brelse(new_bh);
3112 goto out;
3115 memcpy(new_bh->b_data, old_bh->b_data, blocksize);
3117 if (i == 0) {
3118 new_xh = (struct ocfs2_xattr_header *)new_bh->b_data;
3119 new_xh->xh_num_buckets = cpu_to_le16(num_buckets / 2);
3121 if (first_hash)
3122 *first_hash = le32_to_cpu(
3123 new_xh->xh_entries[0].xe_name_hash);
3124 new_first_bh = new_bh;
3125 get_bh(new_first_bh);
3128 ocfs2_journal_dirty(handle, new_bh);
3130 if (*header_bh == old_bh) {
3131 brelse(*header_bh);
3132 *header_bh = new_bh;
3133 get_bh(*header_bh);
3135 brelse(*first_bh);
3136 *first_bh = new_first_bh;
3137 get_bh(*first_bh);
3139 brelse(new_bh);
3140 brelse(old_bh);
3143 le16_add_cpu(&xh->xh_num_buckets, -(num_buckets / 2));
3145 ocfs2_journal_dirty(handle, prev_bh);
3146 out:
3147 brelse(prev_bh);
3148 brelse(new_first_bh);
3149 return ret;
3152 static int ocfs2_read_xattr_bucket(struct inode *inode,
3153 u64 blkno,
3154 struct buffer_head **bhs,
3155 int new)
3157 int ret = 0;
3158 u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3160 if (!new)
3161 return ocfs2_read_blocks(inode, blkno,
3162 blk_per_bucket, bhs, 0);
3164 for (i = 0; i < blk_per_bucket; i++) {
3165 bhs[i] = sb_getblk(inode->i_sb, blkno + i);
3166 if (bhs[i] == NULL) {
3167 ret = -EIO;
3168 mlog_errno(ret);
3169 break;
3171 ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
3174 return ret;
3178 * Move half num of the xattrs in old bucket(blk) to new bucket(new_blk).
3179 * first_hash will record the 1st hash of the new bucket.
3181 static int ocfs2_half_xattr_bucket(struct inode *inode,
3182 handle_t *handle,
3183 u64 blk,
3184 u64 new_blk,
3185 u32 *first_hash,
3186 int new_bucket_head)
3188 int ret, i;
3189 u16 count, start, len, name_value_len, xe_len, name_offset;
3190 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3191 struct buffer_head **s_bhs, **t_bhs = NULL;
3192 struct ocfs2_xattr_header *xh;
3193 struct ocfs2_xattr_entry *xe;
3194 int blocksize = inode->i_sb->s_blocksize;
3196 mlog(0, "move half of xattrs from bucket %llu to %llu\n",
3197 blk, new_blk);
3199 s_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
3200 if (!s_bhs)
3201 return -ENOMEM;
3203 ret = ocfs2_read_xattr_bucket(inode, blk, s_bhs, 0);
3204 if (ret) {
3205 mlog_errno(ret);
3206 goto out;
3209 ret = ocfs2_journal_access(handle, inode, s_bhs[0],
3210 OCFS2_JOURNAL_ACCESS_WRITE);
3211 if (ret) {
3212 mlog_errno(ret);
3213 goto out;
3216 t_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
3217 if (!t_bhs) {
3218 ret = -ENOMEM;
3219 goto out;
3222 ret = ocfs2_read_xattr_bucket(inode, new_blk, t_bhs, new_bucket_head);
3223 if (ret) {
3224 mlog_errno(ret);
3225 goto out;
3228 for (i = 0; i < blk_per_bucket; i++) {
3229 ret = ocfs2_journal_access(handle, inode, t_bhs[i],
3230 OCFS2_JOURNAL_ACCESS_CREATE);
3231 if (ret) {
3232 mlog_errno(ret);
3233 goto out;
3237 /* copy the whole bucket to the new first. */
3238 for (i = 0; i < blk_per_bucket; i++)
3239 memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
3241 /* update the new bucket. */
3242 xh = (struct ocfs2_xattr_header *)t_bhs[0]->b_data;
3243 count = le16_to_cpu(xh->xh_count);
3244 start = count / 2;
3247 * Calculate the total name/value len and xh_free_start for
3248 * the old bucket first.
3250 name_offset = OCFS2_XATTR_BUCKET_SIZE;
3251 name_value_len = 0;
3252 for (i = 0; i < start; i++) {
3253 xe = &xh->xh_entries[i];
3254 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3255 if (ocfs2_xattr_is_local(xe))
3256 xe_len +=
3257 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3258 else
3259 xe_len += OCFS2_XATTR_ROOT_SIZE;
3260 name_value_len += xe_len;
3261 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
3262 name_offset = le16_to_cpu(xe->xe_name_offset);
3266 * Now begin the modification to the new bucket.
3268 * In the new bucket, We just move the xattr entry to the beginning
3269 * and don't touch the name/value. So there will be some holes in the
3270 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
3271 * called.
3273 xe = &xh->xh_entries[start];
3274 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
3275 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
3276 (int)((char *)xe - (char *)xh),
3277 (int)((char *)xh->xh_entries - (char *)xh));
3278 memmove((char *)xh->xh_entries, (char *)xe, len);
3279 xe = &xh->xh_entries[count - start];
3280 len = sizeof(struct ocfs2_xattr_entry) * start;
3281 memset((char *)xe, 0, len);
3283 le16_add_cpu(&xh->xh_count, -start);
3284 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
3286 /* Calculate xh_free_start for the new bucket. */
3287 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3288 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3289 xe = &xh->xh_entries[i];
3290 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3291 if (ocfs2_xattr_is_local(xe))
3292 xe_len +=
3293 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3294 else
3295 xe_len += OCFS2_XATTR_ROOT_SIZE;
3296 if (le16_to_cpu(xe->xe_name_offset) <
3297 le16_to_cpu(xh->xh_free_start))
3298 xh->xh_free_start = xe->xe_name_offset;
3301 /* set xh->xh_num_buckets for the new xh. */
3302 if (new_bucket_head)
3303 xh->xh_num_buckets = cpu_to_le16(1);
3304 else
3305 xh->xh_num_buckets = 0;
3307 for (i = 0; i < blk_per_bucket; i++) {
3308 ocfs2_journal_dirty(handle, t_bhs[i]);
3309 if (ret)
3310 mlog_errno(ret);
3313 /* store the first_hash of the new bucket. */
3314 if (first_hash)
3315 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3318 * Now only update the 1st block of the old bucket.
3319 * Please note that the entry has been sorted already above.
3321 xh = (struct ocfs2_xattr_header *)s_bhs[0]->b_data;
3322 memset(&xh->xh_entries[start], 0,
3323 sizeof(struct ocfs2_xattr_entry) * (count - start));
3324 xh->xh_count = cpu_to_le16(start);
3325 xh->xh_free_start = cpu_to_le16(name_offset);
3326 xh->xh_name_value_len = cpu_to_le16(name_value_len);
3328 ocfs2_journal_dirty(handle, s_bhs[0]);
3329 if (ret)
3330 mlog_errno(ret);
3332 out:
3333 if (s_bhs) {
3334 for (i = 0; i < blk_per_bucket; i++)
3335 brelse(s_bhs[i]);
3337 kfree(s_bhs);
3339 if (t_bhs) {
3340 for (i = 0; i < blk_per_bucket; i++)
3341 brelse(t_bhs[i]);
3343 kfree(t_bhs);
3345 return ret;
3349 * Copy xattr from one bucket to another bucket.
3351 * The caller must make sure that the journal transaction
3352 * has enough space for journaling.
3354 static int ocfs2_cp_xattr_bucket(struct inode *inode,
3355 handle_t *handle,
3356 u64 s_blkno,
3357 u64 t_blkno,
3358 int t_is_new)
3360 int ret, i;
3361 int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3362 int blocksize = inode->i_sb->s_blocksize;
3363 struct buffer_head **s_bhs, **t_bhs = NULL;
3365 BUG_ON(s_blkno == t_blkno);
3367 mlog(0, "cp bucket %llu to %llu, target is %d\n",
3368 s_blkno, t_blkno, t_is_new);
3370 s_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
3371 GFP_NOFS);
3372 if (!s_bhs)
3373 return -ENOMEM;
3375 ret = ocfs2_read_xattr_bucket(inode, s_blkno, s_bhs, 0);
3376 if (ret)
3377 goto out;
3379 t_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
3380 GFP_NOFS);
3381 if (!t_bhs) {
3382 ret = -ENOMEM;
3383 goto out;
3386 ret = ocfs2_read_xattr_bucket(inode, t_blkno, t_bhs, t_is_new);
3387 if (ret)
3388 goto out;
3390 for (i = 0; i < blk_per_bucket; i++) {
3391 ret = ocfs2_journal_access(handle, inode, t_bhs[i],
3392 OCFS2_JOURNAL_ACCESS_WRITE);
3393 if (ret)
3394 goto out;
3397 for (i = 0; i < blk_per_bucket; i++) {
3398 memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
3399 ocfs2_journal_dirty(handle, t_bhs[i]);
3402 out:
3403 if (s_bhs) {
3404 for (i = 0; i < blk_per_bucket; i++)
3405 brelse(s_bhs[i]);
3407 kfree(s_bhs);
3409 if (t_bhs) {
3410 for (i = 0; i < blk_per_bucket; i++)
3411 brelse(t_bhs[i]);
3413 kfree(t_bhs);
3415 return ret;
3419 * Copy one xattr cluster from src_blk to to_blk.
3420 * The to_blk will become the first bucket header of the cluster, so its
3421 * xh_num_buckets will be initialized as the bucket num in the cluster.
3423 static int ocfs2_cp_xattr_cluster(struct inode *inode,
3424 handle_t *handle,
3425 struct buffer_head *first_bh,
3426 u64 src_blk,
3427 u64 to_blk,
3428 u32 *first_hash)
3430 int i, ret, credits;
3431 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3432 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3433 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3434 struct buffer_head *bh = NULL;
3435 struct ocfs2_xattr_header *xh;
3436 u64 to_blk_start = to_blk;
3438 mlog(0, "cp xattrs from cluster %llu to %llu\n", src_blk, to_blk);
3441 * We need to update the new cluster and 1 more for the update of
3442 * the 1st bucket of the previous extent rec.
3444 credits = bpc + 1;
3445 ret = ocfs2_extend_trans(handle, credits);
3446 if (ret) {
3447 mlog_errno(ret);
3448 goto out;
3451 ret = ocfs2_journal_access(handle, inode, first_bh,
3452 OCFS2_JOURNAL_ACCESS_WRITE);
3453 if (ret) {
3454 mlog_errno(ret);
3455 goto out;
3458 for (i = 0; i < num_buckets; i++) {
3459 ret = ocfs2_cp_xattr_bucket(inode, handle,
3460 src_blk, to_blk, 1);
3461 if (ret) {
3462 mlog_errno(ret);
3463 goto out;
3466 src_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3467 to_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3470 /* update the old bucket header. */
3471 xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3472 le16_add_cpu(&xh->xh_num_buckets, -num_buckets);
3474 ocfs2_journal_dirty(handle, first_bh);
3476 /* update the new bucket header. */
3477 ret = ocfs2_read_block(inode, to_blk_start, &bh);
3478 if (ret < 0) {
3479 mlog_errno(ret);
3480 goto out;
3483 ret = ocfs2_journal_access(handle, inode, bh,
3484 OCFS2_JOURNAL_ACCESS_WRITE);
3485 if (ret) {
3486 mlog_errno(ret);
3487 goto out;
3490 xh = (struct ocfs2_xattr_header *)bh->b_data;
3491 xh->xh_num_buckets = cpu_to_le16(num_buckets);
3493 ocfs2_journal_dirty(handle, bh);
3495 if (first_hash)
3496 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3497 out:
3498 brelse(bh);
3499 return ret;
3503 * Move half of the xattrs in this cluster to the new cluster.
3504 * This function should only be called when bucket size == cluster size.
3505 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
3507 static int ocfs2_half_xattr_cluster(struct inode *inode,
3508 handle_t *handle,
3509 u64 prev_blk,
3510 u64 new_blk,
3511 u32 *first_hash)
3513 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3514 int ret, credits = 2 * blk_per_bucket;
3516 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
3518 ret = ocfs2_extend_trans(handle, credits);
3519 if (ret) {
3520 mlog_errno(ret);
3521 return ret;
3524 /* Move half of the xattr in start_blk to the next bucket. */
3525 return ocfs2_half_xattr_bucket(inode, handle, prev_blk,
3526 new_blk, first_hash, 1);
3530 * Move some xattrs from the old cluster to the new one since they are not
3531 * contiguous in ocfs2 xattr tree.
3533 * new_blk starts a new separate cluster, and we will move some xattrs from
3534 * prev_blk to it. v_start will be set as the first name hash value in this
3535 * new cluster so that it can be used as e_cpos during tree insertion and
3536 * don't collide with our original b-tree operations. first_bh and header_bh
3537 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
3538 * to extend the insert bucket.
3540 * The problem is how much xattr should we move to the new one and when should
3541 * we update first_bh and header_bh?
3542 * 1. If cluster size > bucket size, that means the previous cluster has more
3543 * than 1 bucket, so just move half nums of bucket into the new cluster and
3544 * update the first_bh and header_bh if the insert bucket has been moved
3545 * to the new cluster.
3546 * 2. If cluster_size == bucket_size:
3547 * a) If the previous extent rec has more than one cluster and the insert
3548 * place isn't in the last cluster, copy the entire last cluster to the
3549 * new one. This time, we don't need to upate the first_bh and header_bh
3550 * since they will not be moved into the new cluster.
3551 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
3552 * the new one. And we set the extend flag to zero if the insert place is
3553 * moved into the new allocated cluster since no extend is needed.
3555 static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
3556 handle_t *handle,
3557 struct buffer_head **first_bh,
3558 struct buffer_head **header_bh,
3559 u64 new_blk,
3560 u64 prev_blk,
3561 u32 prev_clusters,
3562 u32 *v_start,
3563 int *extend)
3565 int ret = 0;
3566 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3568 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
3569 prev_blk, prev_clusters, new_blk);
3571 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1)
3572 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
3573 handle,
3574 first_bh,
3575 header_bh,
3576 new_blk,
3577 prev_blk,
3578 prev_clusters,
3579 v_start);
3580 else {
3581 u64 last_blk = prev_blk + bpc * (prev_clusters - 1);
3583 if (prev_clusters > 1 && (*header_bh)->b_blocknr != last_blk)
3584 ret = ocfs2_cp_xattr_cluster(inode, handle, *first_bh,
3585 last_blk, new_blk,
3586 v_start);
3587 else {
3588 ret = ocfs2_half_xattr_cluster(inode, handle,
3589 last_blk, new_blk,
3590 v_start);
3592 if ((*header_bh)->b_blocknr == last_blk && extend)
3593 *extend = 0;
3597 return ret;
3601 * Add a new cluster for xattr storage.
3603 * If the new cluster is contiguous with the previous one, it will be
3604 * appended to the same extent record, and num_clusters will be updated.
3605 * If not, we will insert a new extent for it and move some xattrs in
3606 * the last cluster into the new allocated one.
3607 * We also need to limit the maximum size of a btree leaf, otherwise we'll
3608 * lose the benefits of hashing because we'll have to search large leaves.
3609 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
3610 * if it's bigger).
3612 * first_bh is the first block of the previous extent rec and header_bh
3613 * indicates the bucket we will insert the new xattrs. They will be updated
3614 * when the header_bh is moved into the new cluster.
3616 static int ocfs2_add_new_xattr_cluster(struct inode *inode,
3617 struct buffer_head *root_bh,
3618 struct buffer_head **first_bh,
3619 struct buffer_head **header_bh,
3620 u32 *num_clusters,
3621 u32 prev_cpos,
3622 u64 prev_blkno,
3623 int *extend)
3625 int ret, credits;
3626 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3627 u32 prev_clusters = *num_clusters;
3628 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
3629 u64 block;
3630 handle_t *handle = NULL;
3631 struct ocfs2_alloc_context *data_ac = NULL;
3632 struct ocfs2_alloc_context *meta_ac = NULL;
3633 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3634 struct ocfs2_extent_tree et;
3636 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
3637 "previous xattr blkno = %llu\n",
3638 (unsigned long long)OCFS2_I(inode)->ip_blkno,
3639 prev_cpos, prev_blkno);
3641 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
3643 ret = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
3644 &data_ac, &meta_ac);
3645 if (ret) {
3646 mlog_errno(ret);
3647 goto leave;
3650 credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
3651 clusters_to_add);
3652 handle = ocfs2_start_trans(osb, credits);
3653 if (IS_ERR(handle)) {
3654 ret = PTR_ERR(handle);
3655 handle = NULL;
3656 mlog_errno(ret);
3657 goto leave;
3660 ret = ocfs2_journal_access(handle, inode, root_bh,
3661 OCFS2_JOURNAL_ACCESS_WRITE);
3662 if (ret < 0) {
3663 mlog_errno(ret);
3664 goto leave;
3667 ret = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
3668 clusters_to_add, &bit_off, &num_bits);
3669 if (ret < 0) {
3670 if (ret != -ENOSPC)
3671 mlog_errno(ret);
3672 goto leave;
3675 BUG_ON(num_bits > clusters_to_add);
3677 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
3678 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
3679 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
3681 if (prev_blkno + prev_clusters * bpc == block &&
3682 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
3683 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
3685 * If this cluster is contiguous with the old one and
3686 * adding this new cluster, we don't surpass the limit of
3687 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
3688 * initialized and used like other buckets in the previous
3689 * cluster.
3690 * So add it as a contiguous one. The caller will handle
3691 * its init process.
3693 v_start = prev_cpos + prev_clusters;
3694 *num_clusters = prev_clusters + num_bits;
3695 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
3696 num_bits);
3697 } else {
3698 ret = ocfs2_adjust_xattr_cross_cluster(inode,
3699 handle,
3700 first_bh,
3701 header_bh,
3702 block,
3703 prev_blkno,
3704 prev_clusters,
3705 &v_start,
3706 extend);
3707 if (ret) {
3708 mlog_errno(ret);
3709 goto leave;
3713 if (handle->h_buffer_credits < credits) {
3715 * The journal has been restarted before, and don't
3716 * have enough space for the insertion, so extend it
3717 * here.
3719 ret = ocfs2_extend_trans(handle, credits);
3720 if (ret) {
3721 mlog_errno(ret);
3722 goto leave;
3725 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
3726 num_bits, block, v_start);
3727 ret = ocfs2_insert_extent(osb, handle, inode, &et, v_start, block,
3728 num_bits, 0, meta_ac);
3729 if (ret < 0) {
3730 mlog_errno(ret);
3731 goto leave;
3734 ret = ocfs2_journal_dirty(handle, root_bh);
3735 if (ret < 0) {
3736 mlog_errno(ret);
3737 goto leave;
3740 leave:
3741 if (handle)
3742 ocfs2_commit_trans(osb, handle);
3743 if (data_ac)
3744 ocfs2_free_alloc_context(data_ac);
3745 if (meta_ac)
3746 ocfs2_free_alloc_context(meta_ac);
3748 return ret;
3752 * Extend a new xattr bucket and move xattrs to the end one by one until
3753 * We meet with start_bh. Only move half of the xattrs to the bucket after it.
3755 static int ocfs2_extend_xattr_bucket(struct inode *inode,
3756 struct buffer_head *first_bh,
3757 struct buffer_head *start_bh,
3758 u32 num_clusters)
3760 int ret, credits;
3761 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3762 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3763 u64 start_blk = start_bh->b_blocknr, end_blk;
3764 u32 num_buckets = num_clusters * ocfs2_xattr_buckets_per_cluster(osb);
3765 handle_t *handle;
3766 struct ocfs2_xattr_header *first_xh =
3767 (struct ocfs2_xattr_header *)first_bh->b_data;
3768 u16 bucket = le16_to_cpu(first_xh->xh_num_buckets);
3770 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
3771 "from %llu, len = %u\n", start_blk,
3772 (unsigned long long)first_bh->b_blocknr, num_clusters);
3774 BUG_ON(bucket >= num_buckets);
3776 end_blk = first_bh->b_blocknr + (bucket - 1) * blk_per_bucket;
3779 * We will touch all the buckets after the start_bh(include it).
3780 * Add one more bucket and modify the first_bh.
3782 credits = end_blk - start_blk + 2 * blk_per_bucket + 1;
3783 handle = ocfs2_start_trans(osb, credits);
3784 if (IS_ERR(handle)) {
3785 ret = PTR_ERR(handle);
3786 handle = NULL;
3787 mlog_errno(ret);
3788 goto out;
3791 ret = ocfs2_journal_access(handle, inode, first_bh,
3792 OCFS2_JOURNAL_ACCESS_WRITE);
3793 if (ret) {
3794 mlog_errno(ret);
3795 goto commit;
3798 while (end_blk != start_blk) {
3799 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
3800 end_blk + blk_per_bucket, 0);
3801 if (ret)
3802 goto commit;
3803 end_blk -= blk_per_bucket;
3806 /* Move half of the xattr in start_blk to the next bucket. */
3807 ret = ocfs2_half_xattr_bucket(inode, handle, start_blk,
3808 start_blk + blk_per_bucket, NULL, 0);
3810 le16_add_cpu(&first_xh->xh_num_buckets, 1);
3811 ocfs2_journal_dirty(handle, first_bh);
3813 commit:
3814 ocfs2_commit_trans(osb, handle);
3815 out:
3816 return ret;
3820 * Add new xattr bucket in an extent record and adjust the buckets accordingly.
3821 * xb_bh is the ocfs2_xattr_block.
3822 * We will move all the buckets starting from header_bh to the next place. As
3823 * for this one, half num of its xattrs will be moved to the next one.
3825 * We will allocate a new cluster if current cluster is full and adjust
3826 * header_bh and first_bh if the insert place is moved to the new cluster.
3828 static int ocfs2_add_new_xattr_bucket(struct inode *inode,
3829 struct buffer_head *xb_bh,
3830 struct buffer_head *header_bh)
3832 struct ocfs2_xattr_header *first_xh = NULL;
3833 struct buffer_head *first_bh = NULL;
3834 struct ocfs2_xattr_block *xb =
3835 (struct ocfs2_xattr_block *)xb_bh->b_data;
3836 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3837 struct ocfs2_extent_list *el = &xb_root->xt_list;
3838 struct ocfs2_xattr_header *xh =
3839 (struct ocfs2_xattr_header *)header_bh->b_data;
3840 u32 name_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3841 struct super_block *sb = inode->i_sb;
3842 struct ocfs2_super *osb = OCFS2_SB(sb);
3843 int ret, num_buckets, extend = 1;
3844 u64 p_blkno;
3845 u32 e_cpos, num_clusters;
3847 mlog(0, "Add new xattr bucket starting form %llu\n",
3848 (unsigned long long)header_bh->b_blocknr);
3851 * Add refrence for header_bh here because it may be
3852 * changed in ocfs2_add_new_xattr_cluster and we need
3853 * to free it in the end.
3855 get_bh(header_bh);
3857 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
3858 &num_clusters, el);
3859 if (ret) {
3860 mlog_errno(ret);
3861 goto out;
3864 ret = ocfs2_read_block(inode, p_blkno, &first_bh);
3865 if (ret) {
3866 mlog_errno(ret);
3867 goto out;
3870 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
3871 first_xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3873 if (num_buckets == le16_to_cpu(first_xh->xh_num_buckets)) {
3874 ret = ocfs2_add_new_xattr_cluster(inode,
3875 xb_bh,
3876 &first_bh,
3877 &header_bh,
3878 &num_clusters,
3879 e_cpos,
3880 p_blkno,
3881 &extend);
3882 if (ret) {
3883 mlog_errno(ret);
3884 goto out;
3888 if (extend)
3889 ret = ocfs2_extend_xattr_bucket(inode,
3890 first_bh,
3891 header_bh,
3892 num_clusters);
3893 if (ret)
3894 mlog_errno(ret);
3895 out:
3896 brelse(first_bh);
3897 brelse(header_bh);
3898 return ret;
3901 static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
3902 struct ocfs2_xattr_bucket *bucket,
3903 int offs)
3905 int block_off = offs >> inode->i_sb->s_blocksize_bits;
3907 offs = offs % inode->i_sb->s_blocksize;
3908 return bucket->bhs[block_off]->b_data + offs;
3912 * Handle the normal xattr set, including replace, delete and new.
3914 * Note: "local" indicates the real data's locality. So we can't
3915 * just its bucket locality by its length.
3917 static void ocfs2_xattr_set_entry_normal(struct inode *inode,
3918 struct ocfs2_xattr_info *xi,
3919 struct ocfs2_xattr_search *xs,
3920 u32 name_hash,
3921 int local)
3923 struct ocfs2_xattr_entry *last, *xe;
3924 int name_len = strlen(xi->name);
3925 struct ocfs2_xattr_header *xh = xs->header;
3926 u16 count = le16_to_cpu(xh->xh_count), start;
3927 size_t blocksize = inode->i_sb->s_blocksize;
3928 char *val;
3929 size_t offs, size, new_size;
3931 last = &xh->xh_entries[count];
3932 if (!xs->not_found) {
3933 xe = xs->here;
3934 offs = le16_to_cpu(xe->xe_name_offset);
3935 if (ocfs2_xattr_is_local(xe))
3936 size = OCFS2_XATTR_SIZE(name_len) +
3937 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3938 else
3939 size = OCFS2_XATTR_SIZE(name_len) +
3940 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
3943 * If the new value will be stored outside, xi->value has been
3944 * initalized as an empty ocfs2_xattr_value_root, and the same
3945 * goes with xi->value_len, so we can set new_size safely here.
3946 * See ocfs2_xattr_set_in_bucket.
3948 new_size = OCFS2_XATTR_SIZE(name_len) +
3949 OCFS2_XATTR_SIZE(xi->value_len);
3951 le16_add_cpu(&xh->xh_name_value_len, -size);
3952 if (xi->value) {
3953 if (new_size > size)
3954 goto set_new_name_value;
3956 /* Now replace the old value with new one. */
3957 if (local)
3958 xe->xe_value_size = cpu_to_le64(xi->value_len);
3959 else
3960 xe->xe_value_size = 0;
3962 val = ocfs2_xattr_bucket_get_val(inode,
3963 &xs->bucket, offs);
3964 memset(val + OCFS2_XATTR_SIZE(name_len), 0,
3965 size - OCFS2_XATTR_SIZE(name_len));
3966 if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
3967 memcpy(val + OCFS2_XATTR_SIZE(name_len),
3968 xi->value, xi->value_len);
3970 le16_add_cpu(&xh->xh_name_value_len, new_size);
3971 ocfs2_xattr_set_local(xe, local);
3972 return;
3973 } else {
3975 * Remove the old entry if there is more than one.
3976 * We don't remove the last entry so that we can
3977 * use it to indicate the hash value of the empty
3978 * bucket.
3980 last -= 1;
3981 le16_add_cpu(&xh->xh_count, -1);
3982 if (xh->xh_count) {
3983 memmove(xe, xe + 1,
3984 (void *)last - (void *)xe);
3985 memset(last, 0,
3986 sizeof(struct ocfs2_xattr_entry));
3987 } else
3988 xh->xh_free_start =
3989 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3991 return;
3993 } else {
3994 /* find a new entry for insert. */
3995 int low = 0, high = count - 1, tmp;
3996 struct ocfs2_xattr_entry *tmp_xe;
3998 while (low <= high && count) {
3999 tmp = (low + high) / 2;
4000 tmp_xe = &xh->xh_entries[tmp];
4002 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
4003 low = tmp + 1;
4004 else if (name_hash <
4005 le32_to_cpu(tmp_xe->xe_name_hash))
4006 high = tmp - 1;
4007 else {
4008 low = tmp;
4009 break;
4013 xe = &xh->xh_entries[low];
4014 if (low != count)
4015 memmove(xe + 1, xe, (void *)last - (void *)xe);
4017 le16_add_cpu(&xh->xh_count, 1);
4018 memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
4019 xe->xe_name_hash = cpu_to_le32(name_hash);
4020 xe->xe_name_len = name_len;
4021 ocfs2_xattr_set_type(xe, xi->name_index);
4024 set_new_name_value:
4025 /* Insert the new name+value. */
4026 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
4029 * We must make sure that the name/value pair
4030 * exists in the same block.
4032 offs = le16_to_cpu(xh->xh_free_start);
4033 start = offs - size;
4035 if (start >> inode->i_sb->s_blocksize_bits !=
4036 (offs - 1) >> inode->i_sb->s_blocksize_bits) {
4037 offs = offs - offs % blocksize;
4038 xh->xh_free_start = cpu_to_le16(offs);
4041 val = ocfs2_xattr_bucket_get_val(inode,
4042 &xs->bucket, offs - size);
4043 xe->xe_name_offset = cpu_to_le16(offs - size);
4045 memset(val, 0, size);
4046 memcpy(val, xi->name, name_len);
4047 memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
4049 xe->xe_value_size = cpu_to_le64(xi->value_len);
4050 ocfs2_xattr_set_local(xe, local);
4051 xs->here = xe;
4052 le16_add_cpu(&xh->xh_free_start, -size);
4053 le16_add_cpu(&xh->xh_name_value_len, size);
4055 return;
4058 static int ocfs2_xattr_bucket_handle_journal(struct inode *inode,
4059 handle_t *handle,
4060 struct ocfs2_xattr_search *xs,
4061 struct buffer_head **bhs,
4062 u16 bh_num)
4064 int ret = 0, off, block_off;
4065 struct ocfs2_xattr_entry *xe = xs->here;
4068 * First calculate all the blocks we should journal_access
4069 * and journal_dirty. The first block should always be touched.
4071 ret = ocfs2_journal_dirty(handle, bhs[0]);
4072 if (ret)
4073 mlog_errno(ret);
4075 /* calc the data. */
4076 off = le16_to_cpu(xe->xe_name_offset);
4077 block_off = off >> inode->i_sb->s_blocksize_bits;
4078 ret = ocfs2_journal_dirty(handle, bhs[block_off]);
4079 if (ret)
4080 mlog_errno(ret);
4082 return ret;
4086 * Set the xattr entry in the specified bucket.
4087 * The bucket is indicated by xs->bucket and it should have the enough
4088 * space for the xattr insertion.
4090 static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
4091 struct ocfs2_xattr_info *xi,
4092 struct ocfs2_xattr_search *xs,
4093 u32 name_hash,
4094 int local)
4096 int i, ret;
4097 handle_t *handle = NULL;
4098 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4099 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4101 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4102 (unsigned long)xi->value_len, xi->name_index,
4103 (unsigned long long)xs->bucket.bhs[0]->b_blocknr);
4105 if (!xs->bucket.bhs[1]) {
4106 ret = ocfs2_read_blocks(inode,
4107 xs->bucket.bhs[0]->b_blocknr + 1,
4108 blk_per_bucket - 1, &xs->bucket.bhs[1],
4110 if (ret) {
4111 mlog_errno(ret);
4112 goto out;
4116 handle = ocfs2_start_trans(osb, blk_per_bucket);
4117 if (IS_ERR(handle)) {
4118 ret = PTR_ERR(handle);
4119 handle = NULL;
4120 mlog_errno(ret);
4121 goto out;
4124 for (i = 0; i < blk_per_bucket; i++) {
4125 ret = ocfs2_journal_access(handle, inode, xs->bucket.bhs[i],
4126 OCFS2_JOURNAL_ACCESS_WRITE);
4127 if (ret < 0) {
4128 mlog_errno(ret);
4129 goto out;
4133 ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
4135 /*Only dirty the blocks we have touched in set xattr. */
4136 ret = ocfs2_xattr_bucket_handle_journal(inode, handle, xs,
4137 xs->bucket.bhs, blk_per_bucket);
4138 if (ret)
4139 mlog_errno(ret);
4140 out:
4141 ocfs2_commit_trans(osb, handle);
4143 return ret;
4146 static int ocfs2_xattr_value_update_size(struct inode *inode,
4147 struct buffer_head *xe_bh,
4148 struct ocfs2_xattr_entry *xe,
4149 u64 new_size)
4151 int ret;
4152 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4153 handle_t *handle = NULL;
4155 handle = ocfs2_start_trans(osb, 1);
4156 if (handle == NULL) {
4157 ret = -ENOMEM;
4158 mlog_errno(ret);
4159 goto out;
4162 ret = ocfs2_journal_access(handle, inode, xe_bh,
4163 OCFS2_JOURNAL_ACCESS_WRITE);
4164 if (ret < 0) {
4165 mlog_errno(ret);
4166 goto out_commit;
4169 xe->xe_value_size = cpu_to_le64(new_size);
4171 ret = ocfs2_journal_dirty(handle, xe_bh);
4172 if (ret < 0)
4173 mlog_errno(ret);
4175 out_commit:
4176 ocfs2_commit_trans(osb, handle);
4177 out:
4178 return ret;
4182 * Truncate the specified xe_off entry in xattr bucket.
4183 * bucket is indicated by header_bh and len is the new length.
4184 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4186 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4188 static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
4189 struct buffer_head *header_bh,
4190 int xe_off,
4191 int len)
4193 int ret, offset;
4194 u64 value_blk;
4195 struct buffer_head *value_bh = NULL;
4196 struct ocfs2_xattr_value_root *xv;
4197 struct ocfs2_xattr_entry *xe;
4198 struct ocfs2_xattr_header *xh =
4199 (struct ocfs2_xattr_header *)header_bh->b_data;
4200 size_t blocksize = inode->i_sb->s_blocksize;
4202 xe = &xh->xh_entries[xe_off];
4204 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
4206 offset = le16_to_cpu(xe->xe_name_offset) +
4207 OCFS2_XATTR_SIZE(xe->xe_name_len);
4209 value_blk = offset / blocksize;
4211 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4212 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
4213 value_blk += header_bh->b_blocknr;
4215 ret = ocfs2_read_block(inode, value_blk, &value_bh);
4216 if (ret) {
4217 mlog_errno(ret);
4218 goto out;
4221 xv = (struct ocfs2_xattr_value_root *)
4222 (value_bh->b_data + offset % blocksize);
4224 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
4225 xe_off, (unsigned long long)header_bh->b_blocknr, len);
4226 ret = ocfs2_xattr_value_truncate(inode, value_bh, xv, len);
4227 if (ret) {
4228 mlog_errno(ret);
4229 goto out;
4232 ret = ocfs2_xattr_value_update_size(inode, header_bh, xe, len);
4233 if (ret) {
4234 mlog_errno(ret);
4235 goto out;
4238 out:
4239 brelse(value_bh);
4240 return ret;
4243 static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
4244 struct ocfs2_xattr_search *xs,
4245 int len)
4247 int ret, offset;
4248 struct ocfs2_xattr_entry *xe = xs->here;
4249 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
4251 BUG_ON(!xs->bucket.bhs[0] || !xe || ocfs2_xattr_is_local(xe));
4253 offset = xe - xh->xh_entries;
4254 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket.bhs[0],
4255 offset, len);
4256 if (ret)
4257 mlog_errno(ret);
4259 return ret;
4262 static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
4263 struct ocfs2_xattr_search *xs,
4264 char *val,
4265 int value_len)
4267 int offset;
4268 struct ocfs2_xattr_value_root *xv;
4269 struct ocfs2_xattr_entry *xe = xs->here;
4271 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
4273 offset = le16_to_cpu(xe->xe_name_offset) +
4274 OCFS2_XATTR_SIZE(xe->xe_name_len);
4276 xv = (struct ocfs2_xattr_value_root *)(xs->base + offset);
4278 return __ocfs2_xattr_set_value_outside(inode, xv, val, value_len);
4281 static int ocfs2_rm_xattr_cluster(struct inode *inode,
4282 struct buffer_head *root_bh,
4283 u64 blkno,
4284 u32 cpos,
4285 u32 len)
4287 int ret;
4288 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4289 struct inode *tl_inode = osb->osb_tl_inode;
4290 handle_t *handle;
4291 struct ocfs2_xattr_block *xb =
4292 (struct ocfs2_xattr_block *)root_bh->b_data;
4293 struct ocfs2_alloc_context *meta_ac = NULL;
4294 struct ocfs2_cached_dealloc_ctxt dealloc;
4295 struct ocfs2_extent_tree et;
4297 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
4299 ocfs2_init_dealloc_ctxt(&dealloc);
4301 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
4302 cpos, len, (unsigned long long)blkno);
4304 ocfs2_remove_xattr_clusters_from_cache(inode, blkno, len);
4306 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
4307 if (ret) {
4308 mlog_errno(ret);
4309 return ret;
4312 mutex_lock(&tl_inode->i_mutex);
4314 if (ocfs2_truncate_log_needs_flush(osb)) {
4315 ret = __ocfs2_flush_truncate_log(osb);
4316 if (ret < 0) {
4317 mlog_errno(ret);
4318 goto out;
4322 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
4323 if (handle == NULL) {
4324 ret = -ENOMEM;
4325 mlog_errno(ret);
4326 goto out;
4329 ret = ocfs2_journal_access(handle, inode, root_bh,
4330 OCFS2_JOURNAL_ACCESS_WRITE);
4331 if (ret) {
4332 mlog_errno(ret);
4333 goto out_commit;
4336 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
4337 &dealloc);
4338 if (ret) {
4339 mlog_errno(ret);
4340 goto out_commit;
4343 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
4345 ret = ocfs2_journal_dirty(handle, root_bh);
4346 if (ret) {
4347 mlog_errno(ret);
4348 goto out_commit;
4351 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
4352 if (ret)
4353 mlog_errno(ret);
4355 out_commit:
4356 ocfs2_commit_trans(osb, handle);
4357 out:
4358 ocfs2_schedule_truncate_log_flush(osb, 1);
4360 mutex_unlock(&tl_inode->i_mutex);
4362 if (meta_ac)
4363 ocfs2_free_alloc_context(meta_ac);
4365 ocfs2_run_deallocs(osb, &dealloc);
4367 return ret;
4370 static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
4371 struct ocfs2_xattr_search *xs)
4373 handle_t *handle = NULL;
4374 struct ocfs2_xattr_header *xh = xs->bucket.xh;
4375 struct ocfs2_xattr_entry *last = &xh->xh_entries[
4376 le16_to_cpu(xh->xh_count) - 1];
4377 int ret = 0;
4379 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), 1);
4380 if (IS_ERR(handle)) {
4381 ret = PTR_ERR(handle);
4382 mlog_errno(ret);
4383 return;
4386 ret = ocfs2_journal_access(handle, inode, xs->bucket.bhs[0],
4387 OCFS2_JOURNAL_ACCESS_WRITE);
4388 if (ret) {
4389 mlog_errno(ret);
4390 goto out_commit;
4393 /* Remove the old entry. */
4394 memmove(xs->here, xs->here + 1,
4395 (void *)last - (void *)xs->here);
4396 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
4397 le16_add_cpu(&xh->xh_count, -1);
4399 ret = ocfs2_journal_dirty(handle, xs->bucket.bhs[0]);
4400 if (ret < 0)
4401 mlog_errno(ret);
4402 out_commit:
4403 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
4407 * Set the xattr name/value in the bucket specified in xs.
4409 * As the new value in xi may be stored in the bucket or in an outside cluster,
4410 * we divide the whole process into 3 steps:
4411 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
4412 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
4413 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
4414 * 4. If the clusters for the new outside value can't be allocated, we need
4415 * to free the xattr we allocated in set.
4417 static int ocfs2_xattr_set_in_bucket(struct inode *inode,
4418 struct ocfs2_xattr_info *xi,
4419 struct ocfs2_xattr_search *xs)
4421 int ret, local = 1;
4422 size_t value_len;
4423 char *val = (char *)xi->value;
4424 struct ocfs2_xattr_entry *xe = xs->here;
4425 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name,
4426 strlen(xi->name));
4428 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
4430 * We need to truncate the xattr storage first.
4432 * If both the old and new value are stored to
4433 * outside block, we only need to truncate
4434 * the storage and then set the value outside.
4436 * If the new value should be stored within block,
4437 * we should free all the outside block first and
4438 * the modification to the xattr block will be done
4439 * by following steps.
4441 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4442 value_len = xi->value_len;
4443 else
4444 value_len = 0;
4446 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4447 value_len);
4448 if (ret)
4449 goto out;
4451 if (value_len)
4452 goto set_value_outside;
4455 value_len = xi->value_len;
4456 /* So we have to handle the inside block change now. */
4457 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
4459 * If the new value will be stored outside of block,
4460 * initalize a new empty value root and insert it first.
4462 local = 0;
4463 xi->value = &def_xv;
4464 xi->value_len = OCFS2_XATTR_ROOT_SIZE;
4467 ret = ocfs2_xattr_set_entry_in_bucket(inode, xi, xs, name_hash, local);
4468 if (ret) {
4469 mlog_errno(ret);
4470 goto out;
4473 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
4474 goto out;
4476 /* allocate the space now for the outside block storage. */
4477 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4478 value_len);
4479 if (ret) {
4480 mlog_errno(ret);
4482 if (xs->not_found) {
4484 * We can't allocate enough clusters for outside
4485 * storage and we have allocated xattr already,
4486 * so need to remove it.
4488 ocfs2_xattr_bucket_remove_xs(inode, xs);
4490 goto out;
4493 set_value_outside:
4494 ret = ocfs2_xattr_bucket_set_value_outside(inode, xs, val, value_len);
4495 out:
4496 return ret;
4499 /* check whether the xattr bucket is filled up with the same hash value. */
4500 static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
4501 struct ocfs2_xattr_bucket *bucket)
4503 struct ocfs2_xattr_header *xh = bucket->xh;
4505 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
4506 xh->xh_entries[0].xe_name_hash) {
4507 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
4508 "hash = %u\n",
4509 (unsigned long long)bucket->bhs[0]->b_blocknr,
4510 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
4511 return -ENOSPC;
4514 return 0;
4517 static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
4518 struct ocfs2_xattr_info *xi,
4519 struct ocfs2_xattr_search *xs)
4521 struct ocfs2_xattr_header *xh;
4522 struct ocfs2_xattr_entry *xe;
4523 u16 count, header_size, xh_free_start;
4524 int i, free, max_free, need, old;
4525 size_t value_size = 0, name_len = strlen(xi->name);
4526 size_t blocksize = inode->i_sb->s_blocksize;
4527 int ret, allocation = 0;
4528 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4530 mlog_entry("Set xattr %s in xattr index block\n", xi->name);
4532 try_again:
4533 xh = xs->header;
4534 count = le16_to_cpu(xh->xh_count);
4535 xh_free_start = le16_to_cpu(xh->xh_free_start);
4536 header_size = sizeof(struct ocfs2_xattr_header) +
4537 count * sizeof(struct ocfs2_xattr_entry);
4538 max_free = OCFS2_XATTR_BUCKET_SIZE -
4539 le16_to_cpu(xh->xh_name_value_len) - header_size;
4541 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
4542 "of %u which exceed block size\n",
4543 (unsigned long long)xs->bucket.bhs[0]->b_blocknr,
4544 header_size);
4546 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4547 value_size = OCFS2_XATTR_ROOT_SIZE;
4548 else if (xi->value)
4549 value_size = OCFS2_XATTR_SIZE(xi->value_len);
4551 if (xs->not_found)
4552 need = sizeof(struct ocfs2_xattr_entry) +
4553 OCFS2_XATTR_SIZE(name_len) + value_size;
4554 else {
4555 need = value_size + OCFS2_XATTR_SIZE(name_len);
4558 * We only replace the old value if the new length is smaller
4559 * than the old one. Otherwise we will allocate new space in the
4560 * bucket to store it.
4562 xe = xs->here;
4563 if (ocfs2_xattr_is_local(xe))
4564 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4565 else
4566 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4568 if (old >= value_size)
4569 need = 0;
4572 free = xh_free_start - header_size;
4574 * We need to make sure the new name/value pair
4575 * can exist in the same block.
4577 if (xh_free_start % blocksize < need)
4578 free -= xh_free_start % blocksize;
4580 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
4581 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
4582 " %u\n", xs->not_found,
4583 (unsigned long long)xs->bucket.bhs[0]->b_blocknr,
4584 free, need, max_free, le16_to_cpu(xh->xh_free_start),
4585 le16_to_cpu(xh->xh_name_value_len));
4587 if (free < need || count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4588 if (need <= max_free &&
4589 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4591 * We can create the space by defragment. Since only the
4592 * name/value will be moved, the xe shouldn't be changed
4593 * in xs.
4595 ret = ocfs2_defrag_xattr_bucket(inode, &xs->bucket);
4596 if (ret) {
4597 mlog_errno(ret);
4598 goto out;
4601 xh_free_start = le16_to_cpu(xh->xh_free_start);
4602 free = xh_free_start - header_size;
4603 if (xh_free_start % blocksize < need)
4604 free -= xh_free_start % blocksize;
4606 if (free >= need)
4607 goto xattr_set;
4609 mlog(0, "Can't get enough space for xattr insert by "
4610 "defragment. Need %u bytes, but we have %d, so "
4611 "allocate new bucket for it.\n", need, free);
4615 * We have to add new buckets or clusters and one
4616 * allocation should leave us enough space for insert.
4618 BUG_ON(allocation);
4621 * We do not allow for overlapping ranges between buckets. And
4622 * the maximum number of collisions we will allow for then is
4623 * one bucket's worth, so check it here whether we need to
4624 * add a new bucket for the insert.
4626 ret = ocfs2_check_xattr_bucket_collision(inode, &xs->bucket);
4627 if (ret) {
4628 mlog_errno(ret);
4629 goto out;
4632 ret = ocfs2_add_new_xattr_bucket(inode,
4633 xs->xattr_bh,
4634 xs->bucket.bhs[0]);
4635 if (ret) {
4636 mlog_errno(ret);
4637 goto out;
4640 for (i = 0; i < blk_per_bucket; i++)
4641 brelse(xs->bucket.bhs[i]);
4643 memset(&xs->bucket, 0, sizeof(xs->bucket));
4645 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
4646 xi->name_index,
4647 xi->name, xs);
4648 if (ret && ret != -ENODATA)
4649 goto out;
4650 xs->not_found = ret;
4651 allocation = 1;
4652 goto try_again;
4655 xattr_set:
4656 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs);
4657 out:
4658 mlog_exit(ret);
4659 return ret;
4662 static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
4663 struct ocfs2_xattr_bucket *bucket,
4664 void *para)
4666 int ret = 0;
4667 struct ocfs2_xattr_header *xh = bucket->xh;
4668 u16 i;
4669 struct ocfs2_xattr_entry *xe;
4671 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4672 xe = &xh->xh_entries[i];
4673 if (ocfs2_xattr_is_local(xe))
4674 continue;
4676 ret = ocfs2_xattr_bucket_value_truncate(inode,
4677 bucket->bhs[0],
4678 i, 0);
4679 if (ret) {
4680 mlog_errno(ret);
4681 break;
4685 return ret;
4688 static int ocfs2_delete_xattr_index_block(struct inode *inode,
4689 struct buffer_head *xb_bh)
4691 struct ocfs2_xattr_block *xb =
4692 (struct ocfs2_xattr_block *)xb_bh->b_data;
4693 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
4694 int ret = 0;
4695 u32 name_hash = UINT_MAX, e_cpos, num_clusters;
4696 u64 p_blkno;
4698 if (le16_to_cpu(el->l_next_free_rec) == 0)
4699 return 0;
4701 while (name_hash > 0) {
4702 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
4703 &e_cpos, &num_clusters, el);
4704 if (ret) {
4705 mlog_errno(ret);
4706 goto out;
4709 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
4710 ocfs2_delete_xattr_in_bucket,
4711 NULL);
4712 if (ret) {
4713 mlog_errno(ret);
4714 goto out;
4717 ret = ocfs2_rm_xattr_cluster(inode, xb_bh,
4718 p_blkno, e_cpos, num_clusters);
4719 if (ret) {
4720 mlog_errno(ret);
4721 break;
4724 if (e_cpos == 0)
4725 break;
4727 name_hash = e_cpos - 1;
4730 out:
4731 return ret;
4735 * 'trusted' attributes support
4737 static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list,
4738 size_t list_size, const char *name,
4739 size_t name_len)
4741 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
4742 const size_t total_len = prefix_len + name_len + 1;
4744 if (list && total_len <= list_size) {
4745 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
4746 memcpy(list + prefix_len, name, name_len);
4747 list[prefix_len + name_len] = '\0';
4749 return total_len;
4752 static int ocfs2_xattr_trusted_get(struct inode *inode, const char *name,
4753 void *buffer, size_t size)
4755 if (strcmp(name, "") == 0)
4756 return -EINVAL;
4757 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, name,
4758 buffer, size);
4761 static int ocfs2_xattr_trusted_set(struct inode *inode, const char *name,
4762 const void *value, size_t size, int flags)
4764 if (strcmp(name, "") == 0)
4765 return -EINVAL;
4767 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, name, value,
4768 size, flags);
4771 struct xattr_handler ocfs2_xattr_trusted_handler = {
4772 .prefix = XATTR_TRUSTED_PREFIX,
4773 .list = ocfs2_xattr_trusted_list,
4774 .get = ocfs2_xattr_trusted_get,
4775 .set = ocfs2_xattr_trusted_set,
4779 * 'user' attributes support
4781 static size_t ocfs2_xattr_user_list(struct inode *inode, char *list,
4782 size_t list_size, const char *name,
4783 size_t name_len)
4785 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
4786 const size_t total_len = prefix_len + name_len + 1;
4787 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4789 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4790 return 0;
4792 if (list && total_len <= list_size) {
4793 memcpy(list, XATTR_USER_PREFIX, prefix_len);
4794 memcpy(list + prefix_len, name, name_len);
4795 list[prefix_len + name_len] = '\0';
4797 return total_len;
4800 static int ocfs2_xattr_user_get(struct inode *inode, const char *name,
4801 void *buffer, size_t size)
4803 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4805 if (strcmp(name, "") == 0)
4806 return -EINVAL;
4807 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4808 return -EOPNOTSUPP;
4809 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name,
4810 buffer, size);
4813 static int ocfs2_xattr_user_set(struct inode *inode, const char *name,
4814 const void *value, size_t size, int flags)
4816 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4818 if (strcmp(name, "") == 0)
4819 return -EINVAL;
4820 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4821 return -EOPNOTSUPP;
4823 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, name, value,
4824 size, flags);
4827 struct xattr_handler ocfs2_xattr_user_handler = {
4828 .prefix = XATTR_USER_PREFIX,
4829 .list = ocfs2_xattr_user_list,
4830 .get = ocfs2_xattr_user_get,
4831 .set = ocfs2_xattr_user_set,