]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - fs/ocfs2/xattr.c
ocfs2: Specify appropriate journal access for new xattr buckets.
[mirror_ubuntu-zesty-kernel.git] / fs / ocfs2 / xattr.c
CommitLineData
f56654c4
TM
1/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * xattr.c
5 *
c3cb6827 6 * Copyright (C) 2004, 2008 Oracle. All rights reserved.
f56654c4 7 *
cf1d6c76 8 * CREDITS:
c3cb6827
TY
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>
cf1d6c76 11 *
f56654c4
TM
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public
c3cb6827 14 * License version 2 as published by the Free Software Foundation.
f56654c4
TM
15 *
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.
f56654c4
TM
20 */
21
cf1d6c76
TY
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>
01225596 34#include <linux/sort.h>
99219aea
MF
35#include <linux/init.h>
36#include <linux/module.h>
37#include <linux/string.h>
cf1d6c76 38
f56654c4
TM
39#define MLOG_MASK_PREFIX ML_XATTR
40#include <cluster/masklog.h>
41
42#include "ocfs2.h"
43#include "alloc.h"
44#include "dlmglue.h"
45#include "file.h"
cf1d6c76
TY
46#include "symlink.h"
47#include "sysfile.h"
f56654c4
TM
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"
0c044f0b 54#include "super.h"
cf1d6c76
TY
55#include "xattr.h"
56
57
58struct ocfs2_xattr_def_value_root {
59 struct ocfs2_xattr_value_root xv;
60 struct ocfs2_extent_rec er;
61};
62
0c044f0b
TM
63struct ocfs2_xattr_bucket {
64 struct buffer_head *bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
65 struct ocfs2_xattr_header *xh;
66};
67
cf1d6c76
TY
68#define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
69#define OCFS2_XATTR_INLINE_SIZE 80
70
71static struct ocfs2_xattr_def_value_root def_xv = {
72 .xv.xr_list.l_count = cpu_to_le16(1),
73};
74
75struct xattr_handler *ocfs2_xattr_handlers[] = {
76 &ocfs2_xattr_user_handler,
77 &ocfs2_xattr_trusted_handler,
78 NULL
79};
80
c988fd04 81static struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
cf1d6c76
TY
82 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
83 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
84};
85
86struct ocfs2_xattr_info {
87 int name_index;
88 const char *name;
89 const void *value;
90 size_t value_len;
91};
92
93struct ocfs2_xattr_search {
94 struct buffer_head *inode_bh;
95 /*
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.
98 */
99 struct buffer_head *xattr_bh;
100 struct ocfs2_xattr_header *header;
589dc260 101 struct ocfs2_xattr_bucket bucket;
cf1d6c76
TY
102 void *base;
103 void *end;
104 struct ocfs2_xattr_entry *here;
105 int not_found;
106};
107
589dc260
TM
108static 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);
113
114static 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);
119
0c044f0b
TM
120static 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);
124
01225596
TM
125static int ocfs2_xattr_create_index_block(struct inode *inode,
126 struct ocfs2_xattr_search *xs);
127
128static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
129 struct ocfs2_xattr_info *xi,
130 struct ocfs2_xattr_search *xs);
131
a3944256
TM
132static int ocfs2_delete_xattr_index_block(struct inode *inode,
133 struct buffer_head *xb_bh);
134
0030e001
TY
135static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
136{
137 return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
138}
139
140static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
141{
142 return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
143}
144
145static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb)
146{
147 u16 len = sb->s_blocksize -
148 offsetof(struct ocfs2_xattr_header, xh_entries);
149
150 return len / sizeof(struct ocfs2_xattr_entry);
151}
152
936b8834 153static inline const char *ocfs2_xattr_prefix(int name_index)
cf1d6c76
TY
154{
155 struct xattr_handler *handler = NULL;
156
157 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
158 handler = ocfs2_xattr_handler_map[name_index];
159
936b8834 160 return handler ? handler->prefix : NULL;
cf1d6c76
TY
161}
162
40daa16a 163static u32 ocfs2_xattr_name_hash(struct inode *inode,
2057e5c6 164 const char *name,
40daa16a 165 int name_len)
cf1d6c76
TY
166{
167 /* Get hash value of uuid from super block */
168 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
169 int i;
170
cf1d6c76
TY
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++;
176 }
177
178 return hash;
179}
180
181/*
182 * ocfs2_xattr_hash_entry()
183 *
184 * Compute the hash of an extended attribute.
185 */
186static void ocfs2_xattr_hash_entry(struct inode *inode,
187 struct ocfs2_xattr_header *header,
188 struct ocfs2_xattr_entry *entry)
189{
190 u32 hash = 0;
cf1d6c76 191 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
cf1d6c76 192
2057e5c6 193 hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
cf1d6c76
TY
194 entry->xe_name_hash = cpu_to_le32(hash);
195
196 return;
197}
f56654c4
TM
198
199static 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)
203{
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);
f56654c4 212 u32 prev_clusters, logical_start = le32_to_cpu(xv->xr_clusters);
f99b9b7c 213 struct ocfs2_extent_tree et;
f56654c4
TM
214
215 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
216
8d6220d6 217 ocfs2_init_xattr_value_extent_tree(&et, inode, xattr_bh, xv);
f99b9b7c 218
f56654c4
TM
219restart_all:
220
f99b9b7c
JB
221 status = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
222 &data_ac, &meta_ac);
f56654c4
TM
223 if (status) {
224 mlog_errno(status);
225 goto leave;
226 }
227
f99b9b7c
JB
228 credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
229 clusters_to_add);
f56654c4
TM
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;
236 }
237
238restarted_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;
244 }
245
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,
251 0,
f99b9b7c 252 &et,
f56654c4
TM
253 handle,
254 data_ac,
255 meta_ac,
f99b9b7c 256 &why);
f56654c4
TM
257 if ((status < 0) && (status != -EAGAIN)) {
258 if (status != -ENOSPC)
259 mlog_errno(status);
260 goto leave;
261 }
262
263 status = ocfs2_journal_dirty(handle, xattr_bh);
264 if (status < 0) {
265 mlog_errno(status);
266 goto leave;
267 }
268
269 clusters_to_add -= le32_to_cpu(xv->xr_clusters) - prev_clusters;
270
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);
277
278 mlog(0, "restarting transaction.\n");
279 /* TODO: This can be more intelligent. */
280 credits = ocfs2_calc_extend_credits(osb->sb,
f99b9b7c 281 et.et_root_el,
f56654c4
TM
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;
290 }
291 goto restarted_transaction;
292 }
293 }
294
295leave:
296 if (handle) {
297 ocfs2_commit_trans(osb, handle);
298 handle = NULL;
299 }
300 if (data_ac) {
301 ocfs2_free_alloc_context(data_ac);
302 data_ac = NULL;
303 }
304 if (meta_ac) {
305 ocfs2_free_alloc_context(meta_ac);
306 meta_ac = NULL;
307 }
308 if ((!status) && restart_func) {
309 restart_func = 0;
310 goto restart_all;
311 }
312
313 return status;
314}
315
316static 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)
321{
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;
f99b9b7c
JB
328 struct ocfs2_extent_tree et;
329
8d6220d6 330 ocfs2_init_xattr_value_extent_tree(&et, inode, root_bh, xv);
f56654c4 331
f99b9b7c 332 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
f56654c4
TM
333 if (ret) {
334 mlog_errno(ret);
335 return ret;
336 }
337
338 mutex_lock(&tl_inode->i_mutex);
339
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;
345 }
346 }
347
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;
353 }
354
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;
360 }
361
f99b9b7c
JB
362 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
363 dealloc);
f56654c4
TM
364 if (ret) {
365 mlog_errno(ret);
366 goto out_commit;
367 }
368
369 le32_add_cpu(&xv->xr_clusters, -len);
370
371 ret = ocfs2_journal_dirty(handle, root_bh);
372 if (ret) {
373 mlog_errno(ret);
374 goto out_commit;
375 }
376
377 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
378 if (ret)
379 mlog_errno(ret);
380
381out_commit:
382 ocfs2_commit_trans(osb, handle);
383out:
384 mutex_unlock(&tl_inode->i_mutex);
385
386 if (meta_ac)
387 ocfs2_free_alloc_context(meta_ac);
388
389 return ret;
390}
391
392static 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)
397{
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;
403
404 ocfs2_init_dealloc_ctxt(&dealloc);
405
406 if (old_clusters <= new_clusters)
407 return 0;
408
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;
417 }
418
419 if (alloc_size > trunc_len)
420 alloc_size = trunc_len;
421
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;
428 }
429
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;
435 }
436
437out:
438 ocfs2_schedule_truncate_log_flush(osb, 1);
439 ocfs2_run_deallocs(osb, &dealloc);
440
441 return ret;
442}
443
444static int ocfs2_xattr_value_truncate(struct inode *inode,
445 struct buffer_head *root_bh,
446 struct ocfs2_xattr_value_root *xv,
447 int len)
448{
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);
452
453 if (new_clusters == old_clusters)
454 return 0;
455
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);
464
465 return ret;
466}
cf1d6c76 467
936b8834
TM
468static int ocfs2_xattr_list_entry(char *buffer, size_t size,
469 size_t *result, const char *prefix,
470 const char *name, int name_len)
471{
472 char *p = buffer + *result;
473 int prefix_len = strlen(prefix);
474 int total_len = prefix_len + name_len + 1;
475
476 *result += total_len;
477
478 /* we are just looking for how big our buffer needs to be */
479 if (!size)
480 return 0;
481
482 if (*result > size)
483 return -ERANGE;
484
485 memcpy(p, prefix, prefix_len);
486 memcpy(p + prefix_len, name, name_len);
487 p[prefix_len + name_len] = '\0';
488
489 return 0;
490}
491
cf1d6c76
TY
492static int ocfs2_xattr_list_entries(struct inode *inode,
493 struct ocfs2_xattr_header *header,
494 char *buffer, size_t buffer_size)
495{
936b8834
TM
496 size_t result = 0;
497 int i, type, ret;
498 const char *prefix, *name;
cf1d6c76
TY
499
500 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
501 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
936b8834
TM
502 type = ocfs2_xattr_get_type(entry);
503 prefix = ocfs2_xattr_prefix(type);
504
505 if (prefix) {
506 name = (const char *)header +
507 le16_to_cpu(entry->xe_name_offset);
508
509 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
510 &result, prefix, name,
511 entry->xe_name_len);
512 if (ret)
513 return ret;
cf1d6c76
TY
514 }
515 }
516
936b8834 517 return result;
cf1d6c76
TY
518}
519
520static int ocfs2_xattr_ibody_list(struct inode *inode,
521 struct ocfs2_dinode *di,
522 char *buffer,
523 size_t buffer_size)
524{
525 struct ocfs2_xattr_header *header = NULL;
526 struct ocfs2_inode_info *oi = OCFS2_I(inode);
527 int ret = 0;
528
529 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
530 return ret;
531
532 header = (struct ocfs2_xattr_header *)
533 ((void *)di + inode->i_sb->s_blocksize -
534 le16_to_cpu(di->i_xattr_inline_size));
535
536 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
537
538 return ret;
539}
540
541static int ocfs2_xattr_block_list(struct inode *inode,
542 struct ocfs2_dinode *di,
543 char *buffer,
544 size_t buffer_size)
545{
546 struct buffer_head *blk_bh = NULL;
0c044f0b 547 struct ocfs2_xattr_block *xb;
cf1d6c76
TY
548 int ret = 0;
549
550 if (!di->i_xattr_loc)
551 return ret;
552
0fcaa56a 553 ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh);
cf1d6c76
TY
554 if (ret < 0) {
555 mlog_errno(ret);
556 return ret;
557 }
cf1d6c76 558
0c044f0b 559 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
f6087fb7
JB
560 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
561 ret = -EIO;
562 goto cleanup;
563 }
cf1d6c76 564
0c044f0b
TM
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);
573 }
cf1d6c76
TY
574cleanup:
575 brelse(blk_bh);
576
577 return ret;
578}
579
580ssize_t ocfs2_listxattr(struct dentry *dentry,
581 char *buffer,
582 size_t size)
583{
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);
588
8154da3d
TY
589 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
590 return -EOPNOTSUPP;
591
cf1d6c76
TY
592 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
593 return ret;
594
595 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
596 if (ret < 0) {
597 mlog_errno(ret);
598 return ret;
599 }
600
601 di = (struct ocfs2_dinode *)di_bh->b_data;
602
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;
611 }
612 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
613 buffer, size);
614 if (b_ret < 0)
615 i_ret = 0;
616 }
617 up_read(&oi->ip_xattr_sem);
618 ocfs2_inode_unlock(dentry->d_inode, 0);
619
620 brelse(di_bh);
621
622 return i_ret + b_ret;
623}
624
625static int ocfs2_xattr_find_entry(int name_index,
626 const char *name,
627 struct ocfs2_xattr_search *xs)
628{
629 struct ocfs2_xattr_entry *entry;
630 size_t name_len;
631 int i, cmp = 1;
632
633 if (name == NULL)
634 return -EINVAL;
635
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;
649 }
650 xs->here = entry;
651
652 return cmp ? -ENODATA : 0;
653}
654
655static int ocfs2_xattr_get_value_outside(struct inode *inode,
589dc260 656 struct ocfs2_xattr_value_root *xv,
cf1d6c76
TY
657 void *buffer,
658 size_t len)
659{
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;
cf1d6c76
TY
665 struct ocfs2_extent_list *el;
666
cf1d6c76
TY
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;
671
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;
679 }
680
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++) {
0fcaa56a 684 ret = ocfs2_read_block(inode, blkno, &bh);
cf1d6c76
TY
685 if (ret) {
686 mlog_errno(ret);
687 goto out;
688 }
689
690 cplen = len >= blocksize ? blocksize : len;
691 memcpy(buffer, bh->b_data, cplen);
692 len -= cplen;
693 buffer += cplen;
694
695 brelse(bh);
696 bh = NULL;
697 if (len == 0)
698 break;
699 }
700 cpos += num_clusters;
701 }
702out:
703 return ret;
704}
705
706static 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)
712{
713 struct ocfs2_inode_info *oi = OCFS2_I(inode);
714 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
589dc260 715 struct ocfs2_xattr_value_root *xv;
cf1d6c76
TY
716 size_t size;
717 int ret = 0;
718
719 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
720 return -ENODATA;
721
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;
727
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 {
589dc260
TM
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,
cf1d6c76
TY
745 buffer, size);
746 if (ret < 0) {
747 mlog_errno(ret);
748 return ret;
749 }
750 }
751 }
752
753 return size;
754}
755
756static 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)
762{
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;
589dc260 766 struct ocfs2_xattr_value_root *xv;
cf1d6c76 767 size_t size;
589dc260 768 int ret = -ENODATA, name_offset, name_len, block_off, i;
cf1d6c76
TY
769
770 if (!di->i_xattr_loc)
771 return ret;
772
589dc260
TM
773 memset(&xs->bucket, 0, sizeof(xs->bucket));
774
0fcaa56a 775 ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh);
cf1d6c76
TY
776 if (ret < 0) {
777 mlog_errno(ret);
778 return ret;
779 }
f6087fb7
JB
780
781 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
782 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
783 ret = -EIO;
cf1d6c76
TY
784 goto cleanup;
785 }
786
787 xs->xattr_bh = blk_bh;
cf1d6c76 788
589dc260
TM
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;
794
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);
800
cf1d6c76
TY
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;
589dc260
TM
808
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;
812
813 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
814 ret = ocfs2_xattr_bucket_get_name_value(inode,
815 xs->bucket.xh,
816 i,
817 &block_off,
818 &name_offset);
819 xs->base = xs->bucket.bhs[block_off]->b_data;
820 }
cf1d6c76
TY
821 if (ocfs2_xattr_is_local(xs->here)) {
822 memcpy(buffer, (void *)xs->base +
589dc260 823 name_offset + name_len, size);
cf1d6c76 824 } else {
589dc260
TM
825 xv = (struct ocfs2_xattr_value_root *)
826 (xs->base + name_offset + name_len);
827 ret = ocfs2_xattr_get_value_outside(inode, xv,
cf1d6c76
TY
828 buffer, size);
829 if (ret < 0) {
830 mlog_errno(ret);
831 goto cleanup;
832 }
833 }
834 }
835 ret = size;
836cleanup:
589dc260
TM
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));
cf1d6c76 840
589dc260 841 brelse(blk_bh);
cf1d6c76
TY
842 return ret;
843}
844
845/* ocfs2_xattr_get()
846 *
847 * Copy an extended attribute into the buffer provided.
848 * Buffer is NULL to compute the size of buffer required.
849 */
0030e001
TY
850static int ocfs2_xattr_get(struct inode *inode,
851 int name_index,
852 const char *name,
853 void *buffer,
854 size_t buffer_size)
cf1d6c76
TY
855{
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,
862 };
863 struct ocfs2_xattr_search xbs = {
864 .not_found = -ENODATA,
865 };
866
8154da3d
TY
867 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
868 return -EOPNOTSUPP;
869
cf1d6c76
TY
870 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
871 ret = -ENODATA;
872
873 ret = ocfs2_inode_lock(inode, &di_bh, 0);
874 if (ret < 0) {
875 mlog_errno(ret);
876 return ret;
877 }
878 xis.inode_bh = xbs.inode_bh = di_bh;
879 di = (struct ocfs2_dinode *)di_bh->b_data;
880
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);
889
890 brelse(di_bh);
891
892 return ret;
893}
894
895static int __ocfs2_xattr_set_value_outside(struct inode *inode,
896 struct ocfs2_xattr_value_root *xv,
897 const void *value,
898 int value_len)
899{
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;
908
909 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
910
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;
917 }
918
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;
925 }
926
927 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
928
929 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
0fcaa56a 930 ret = ocfs2_read_block(inode, blkno, &bh);
cf1d6c76
TY
931 if (ret) {
932 mlog_errno(ret);
933 goto out_commit;
934 }
935
936 ret = ocfs2_journal_access(handle,
937 inode,
938 bh,
939 OCFS2_JOURNAL_ACCESS_WRITE);
940 if (ret < 0) {
941 mlog_errno(ret);
942 goto out_commit;
943 }
944
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);
952
953 ret = ocfs2_journal_dirty(handle, bh);
954 if (ret < 0) {
955 mlog_errno(ret);
956 goto out_commit;
957 }
958 brelse(bh);
959 bh = NULL;
960
961 /*
962 * XXX: do we need to empty all the following
963 * blocks in this cluster?
964 */
965 if (!value_len)
966 break;
967 }
968 cpos += num_clusters;
969 }
970out_commit:
971 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
972out:
973 brelse(bh);
974
975 return ret;
976}
977
978static int ocfs2_xattr_cleanup(struct inode *inode,
979 struct ocfs2_xattr_info *xi,
980 struct ocfs2_xattr_search *xs,
981 size_t offs)
982{
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;
988
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;
995 }
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;
1001 }
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);
1007
1008 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1009 if (ret < 0)
1010 mlog_errno(ret);
1011out_commit:
1012 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1013out:
1014 return ret;
1015}
1016
1017static int ocfs2_xattr_update_entry(struct inode *inode,
1018 struct ocfs2_xattr_info *xi,
1019 struct ocfs2_xattr_search *xs,
1020 size_t offs)
1021{
1022 handle_t *handle = NULL;
1023 int ret = 0;
1024
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;
1031 }
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;
1037 }
1038
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);
1046
1047 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1048 if (ret < 0)
1049 mlog_errno(ret);
1050out_commit:
1051 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1052out:
1053 return ret;
1054}
1055
1056/*
1057 * ocfs2_xattr_set_value_outside()
1058 *
1059 * Set large size value in B tree.
1060 */
1061static 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)
1065{
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;
1071
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;
1081
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;
1087 }
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;
1093 }
1094 ret = ocfs2_xattr_update_entry(inode, xi, xs, offs);
1095 if (ret < 0)
1096 mlog_errno(ret);
1097
1098 return ret;
1099}
1100
1101/*
1102 * ocfs2_xattr_set_entry_local()
1103 *
1104 * Set, replace or remove extended attribute in local.
1105 */
1106static 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)
1111{
1112 size_t name_len = strlen(xi->name);
1113 int i;
1114
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;
1125
1126 first_val = xs->base + min_offs;
1127 offs = le16_to_cpu(xs->here->xe_name_offset);
1128 val = xs->base + offs;
1129
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));
1137
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),
1146 0,
1147 OCFS2_XATTR_SIZE(xi->value_len));
1148 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1149 xi->value,
1150 xi->value_len);
1151 return;
1152 }
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;
1160
1161 min_offs += size;
1162
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);
1167
1168 if (o < offs)
1169 last->xe_name_offset = cpu_to_le16(o + size);
1170 last += 1;
1171 }
1172
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);
1180 }
1181 }
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;
1187
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);
1197 }
1198
1199 return;
1200}
1201
1202/*
1203 * ocfs2_xattr_set_entry()
1204 *
1205 * Set extended attribute entry into inode or block.
1206 *
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().
1210 */
1211static int ocfs2_xattr_set_entry(struct inode *inode,
1212 struct ocfs2_xattr_info *xi,
1213 struct ocfs2_xattr_search *xs,
1214 int flag)
1215{
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,
1228 };
1229
1230 /* Compute min_offs, last and free space. */
1231 last = xs->header->xh_entries;
1232
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;
1238 }
1239
1240 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
1241 if (free < 0)
b37c4d84 1242 return -EIO;
cf1d6c76
TY
1243
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));
1253 }
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;
1261 }
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;
1271 }
1272 }
1273
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;
1280
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));
1293
1294 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1295 /*
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().
1299 */
1300 ret = ocfs2_xattr_value_truncate(inode,
1301 xs->xattr_bh,
1302 xv,
1303 xi->value_len);
1304 if (ret < 0) {
1305 mlog_errno(ret);
1306 goto out;
1307 }
1308
1309 ret = __ocfs2_xattr_set_value_outside(inode,
1310 xv,
1311 xi->value,
1312 xi->value_len);
1313 if (ret < 0) {
1314 mlog_errno(ret);
1315 goto out;
1316 }
1317
1318 ret = ocfs2_xattr_update_entry(inode,
1319 xi,
1320 xs,
1321 offs);
1322 if (ret < 0)
1323 mlog_errno(ret);
1324 goto out;
1325 } else {
1326 /*
1327 * If new value need set in local,
1328 * just trucate old value to zero.
1329 */
1330 ret = ocfs2_xattr_value_truncate(inode,
1331 xs->xattr_bh,
1332 xv,
1333 0);
1334 if (ret < 0)
1335 mlog_errno(ret);
1336 }
1337 }
1338 }
1339
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;
1346 }
1347
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;
1353 }
1354
1355 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
28b8ca0b 1356 /* set extended attribute in external block. */
cf1d6c76 1357 ret = ocfs2_extend_trans(handle,
28b8ca0b 1358 OCFS2_INODE_UPDATE_CREDITS +
cf1d6c76
TY
1359 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1360 if (ret) {
1361 mlog_errno(ret);
1362 goto out_commit;
1363 }
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;
1369 }
1370 }
1371
1372 /*
1373 * Set value in local, include set tree root in local.
1374 * This is the first step for value size >INLINE_SIZE.
1375 */
1376 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1377
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;
1383 }
1384 }
1385
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;
1390
1391 /*
1392 * Adjust extent record count or inline data size
1393 * to reserve space for extended attribute.
1394 */
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)));
1402 }
1403 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1404 }
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);
1414
1415 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1416 if (ret < 0)
1417 mlog_errno(ret);
1418
1419out_commit:
1420 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1421
1422 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1423 /*
1424 * Set value outside in B tree.
1425 * This is the second step for value size > INLINE_SIZE.
1426 */
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;
1431
1432 mlog_errno(ret);
1433 /*
1434 * If set value outside failed, we have to clean
1435 * the junk tree root we have already set in local.
1436 */
1437 ret2 = ocfs2_xattr_cleanup(inode, xi, xs, offs);
1438 if (ret2 < 0)
1439 mlog_errno(ret2);
1440 }
1441 }
1442out:
1443 return ret;
1444
1445}
1446
cf1d6c76
TY
1447static int ocfs2_remove_value_outside(struct inode*inode,
1448 struct buffer_head *bh,
1449 struct ocfs2_xattr_header *header)
1450{
1451 int ret = 0, i;
1452
1453 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1454 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1455
1456 if (!ocfs2_xattr_is_local(entry)) {
1457 struct ocfs2_xattr_value_root *xv;
1458 void *val;
1459
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;
1468 }
1469 }
1470 }
1471
1472 return ret;
1473}
1474
1475static int ocfs2_xattr_ibody_remove(struct inode *inode,
1476 struct buffer_head *di_bh)
1477{
1478
1479 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1480 struct ocfs2_xattr_header *header;
1481 int ret;
1482
1483 header = (struct ocfs2_xattr_header *)
1484 ((void *)di + inode->i_sb->s_blocksize -
1485 le16_to_cpu(di->i_xattr_inline_size));
1486
1487 ret = ocfs2_remove_value_outside(inode, di_bh, header);
1488
1489 return ret;
1490}
1491
1492static int ocfs2_xattr_block_remove(struct inode *inode,
1493 struct buffer_head *blk_bh)
1494{
1495 struct ocfs2_xattr_block *xb;
cf1d6c76
TY
1496 int ret = 0;
1497
1498 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
a3944256
TM
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);
cf1d6c76
TY
1504
1505 return ret;
1506}
1507
08413899
TM
1508static int ocfs2_xattr_free_block(struct inode *inode,
1509 u64 block)
1510{
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;
1520
0fcaa56a 1521 ret = ocfs2_read_block(inode, block, &blk_bh);
08413899
TM
1522 if (ret < 0) {
1523 mlog_errno(ret);
1524 goto out;
1525 }
1526
f6087fb7
JB
1527 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1528 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
1529 ret = -EIO;
08413899
TM
1530 goto out;
1531 }
1532
1533 ret = ocfs2_xattr_block_remove(inode, blk_bh);
1534 if (ret < 0) {
1535 mlog_errno(ret);
1536 goto out;
1537 }
1538
08413899
TM
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);
1542
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;
1550 }
1551 mutex_lock(&xb_alloc_inode->i_mutex);
1552
1553 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1554 if (ret < 0) {
1555 mlog_errno(ret);
1556 goto out_mutex;
1557 }
1558
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;
1564 }
1565
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);
1570
1571 ocfs2_commit_trans(osb, handle);
1572out_unlock:
1573 ocfs2_inode_unlock(xb_alloc_inode, 1);
1574 brelse(xb_alloc_bh);
1575out_mutex:
1576 mutex_unlock(&xb_alloc_inode->i_mutex);
1577 iput(xb_alloc_inode);
1578out:
1579 brelse(blk_bh);
1580 return ret;
1581}
1582
cf1d6c76
TY
1583/*
1584 * ocfs2_xattr_remove()
1585 *
1586 * Free extended attribute resources associated with this inode.
1587 */
1588int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
1589{
cf1d6c76
TY
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;
1594
8154da3d
TY
1595 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1596 return 0;
1597
cf1d6c76
TY
1598 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1599 return 0;
1600
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;
1606 }
1607 }
cf1d6c76 1608
08413899
TM
1609 if (di->i_xattr_loc) {
1610 ret = ocfs2_xattr_free_block(inode,
1611 le64_to_cpu(di->i_xattr_loc));
cf1d6c76
TY
1612 if (ret < 0) {
1613 mlog_errno(ret);
1614 goto out;
1615 }
1616 }
1617
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;
1624 }
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;
1630 }
1631
08413899 1632 di->i_xattr_loc = 0;
cf1d6c76
TY
1633
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);
1638
1639 ret = ocfs2_journal_dirty(handle, di_bh);
1640 if (ret < 0)
1641 mlog_errno(ret);
1642out_commit:
1643 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1644out:
cf1d6c76
TY
1645 return ret;
1646}
1647
1648static int ocfs2_xattr_has_space_inline(struct inode *inode,
1649 struct ocfs2_dinode *di)
1650{
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;
1654
1655 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
1656 return 0;
1657
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);
1669 }
1670 if (free >= xattrsize)
1671 return 1;
1672
1673 return 0;
1674}
1675
1676/*
1677 * ocfs2_xattr_ibody_find()
1678 *
1679 * Find extended attribute in inode block and
1680 * fill search info into struct ocfs2_xattr_search.
1681 */
1682static int ocfs2_xattr_ibody_find(struct inode *inode,
1683 int name_index,
1684 const char *name,
1685 struct ocfs2_xattr_search *xs)
1686{
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;
1691
1692 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1693 return 0;
1694
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;
1701 }
1702
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;
1713
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;
1720 }
1721
1722 return 0;
1723}
1724
1725/*
1726 * ocfs2_xattr_ibody_set()
1727 *
1728 * Set, replace or remove an extended attribute into inode block.
1729 *
1730 */
1731static int ocfs2_xattr_ibody_set(struct inode *inode,
1732 struct ocfs2_xattr_info *xi,
1733 struct ocfs2_xattr_search *xs)
1734{
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;
1738
1739 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1740 return -ENOSPC;
1741
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;
1747 }
1748 }
1749
1750 ret = ocfs2_xattr_set_entry(inode, xi, xs,
1751 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
1752out:
1753 up_write(&oi->ip_alloc_sem);
1754
1755 return ret;
1756}
1757
1758/*
1759 * ocfs2_xattr_block_find()
1760 *
1761 * Find extended attribute in external block and
1762 * fill search info into struct ocfs2_xattr_search.
1763 */
1764static int ocfs2_xattr_block_find(struct inode *inode,
1765 int name_index,
1766 const char *name,
1767 struct ocfs2_xattr_search *xs)
1768{
1769 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1770 struct buffer_head *blk_bh = NULL;
589dc260 1771 struct ocfs2_xattr_block *xb;
cf1d6c76
TY
1772 int ret = 0;
1773
1774 if (!di->i_xattr_loc)
1775 return ret;
1776
0fcaa56a 1777 ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh);
cf1d6c76
TY
1778 if (ret < 0) {
1779 mlog_errno(ret);
1780 return ret;
1781 }
f6087fb7
JB
1782
1783 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1784 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
1785 ret = -EIO;
1786 goto cleanup;
cf1d6c76
TY
1787 }
1788
1789 xs->xattr_bh = blk_bh;
589dc260
TM
1790
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;
1796
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);
cf1d6c76 1802
cf1d6c76
TY
1803 if (ret && ret != -ENODATA) {
1804 xs->xattr_bh = NULL;
1805 goto cleanup;
1806 }
1807 xs->not_found = ret;
1808 return 0;
cf1d6c76
TY
1809cleanup:
1810 brelse(blk_bh);
1811
1812 return ret;
1813}
1814
01225596
TM
1815/*
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.
1819 */
1820static int ocfs2_restore_xattr_block(struct inode *inode,
1821 struct ocfs2_xattr_search *xs)
1822{
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);
1830
1831 BUG_ON(!(xb_flags & OCFS2_XATTR_INDEXED) ||
1832 le16_to_cpu(el->l_next_free_rec) != 0);
1833
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;
1839 }
1840
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;
1846 }
1847
1848 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
1849 offsetof(struct ocfs2_xattr_block, xb_attrs));
1850
1851 xb->xb_flags = cpu_to_le16(xb_flags & ~OCFS2_XATTR_INDEXED);
1852
1853 ocfs2_journal_dirty(handle, xs->xattr_bh);
1854
1855out_commit:
1856 ocfs2_commit_trans(osb, handle);
1857out:
1858 return ret;
1859}
1860
cf1d6c76
TY
1861/*
1862 * ocfs2_xattr_block_set()
1863 *
1864 * Set, replace or remove an extended attribute into external block.
1865 *
1866 */
1867static int ocfs2_xattr_block_set(struct inode *inode,
1868 struct ocfs2_xattr_info *xi,
1869 struct ocfs2_xattr_search *xs)
1870{
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;
1881
1882 if (!xs->xattr_bh) {
1883 /*
1884 * Alloc one external block for extended attribute
1885 * outside of inode.
1886 */
1887 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
1888 if (ret < 0) {
1889 mlog_errno(ret);
1890 goto out;
1891 }
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;
1898 }
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;
1904 }
1905
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;
1912 }
1913
1914 new_bh = sb_getblk(inode->i_sb, first_blkno);
1915 ocfs2_set_new_buffer_uptodate(inode, new_bh);
1916
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;
1922 }
1923
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);
1933
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;
1938
1939
1940 ret = ocfs2_journal_dirty(handle, new_bh);
1941 if (ret < 0) {
1942 mlog_errno(ret);
1943 goto out_commit;
1944 }
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);
1949out_commit:
1950 ocfs2_commit_trans(osb, handle);
1951out:
1952 if (meta_ac)
1953 ocfs2_free_alloc_context(meta_ac);
1954 if (ret < 0)
1955 return ret;
01225596
TM
1956 } else
1957 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1958
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;
1964
1965 ret = ocfs2_xattr_create_index_block(inode, xs);
1966 if (ret)
1967 goto end;
cf1d6c76
TY
1968 }
1969
01225596
TM
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);
1973
1974end:
cf1d6c76
TY
1975
1976 return ret;
1977}
1978
1979/*
1980 * ocfs2_xattr_set()
1981 *
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.
1985 */
1986int 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)
1992{
1993 struct buffer_head *di_bh = NULL;
1994 struct ocfs2_dinode *di;
1995 int ret;
01225596 1996 u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
cf1d6c76
TY
1997
1998 struct ocfs2_xattr_info xi = {
1999 .name_index = name_index,
2000 .name = name,
2001 .value = value,
2002 .value_len = value_len,
2003 };
2004
2005 struct ocfs2_xattr_search xis = {
2006 .not_found = -ENODATA,
2007 };
2008
2009 struct ocfs2_xattr_search xbs = {
2010 .not_found = -ENODATA,
2011 };
2012
8154da3d
TY
2013 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2014 return -EOPNOTSUPP;
2015
cf1d6c76
TY
2016 ret = ocfs2_inode_lock(inode, &di_bh, 1);
2017 if (ret < 0) {
2018 mlog_errno(ret);
2019 return ret;
2020 }
2021 xis.inode_bh = xbs.inode_bh = di_bh;
2022 di = (struct ocfs2_dinode *)di_bh->b_data;
2023
2024 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2025 /*
2026 * Scan inode and external block to find the same name
2027 * extended attribute and collect search infomation.
2028 */
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;
2036 }
2037
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;
2049 }
2050
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) {
2061 /*
2062 * If succeed and that extended attribute existing in
2063 * external block, then we will remove it.
2064 */
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;
2074 }
2075 /*
2076 * If no space in inode, we will set extended attribute
2077 * into external block.
2078 */
2079 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2080 if (ret)
2081 goto cleanup;
2082 if (!xis.not_found) {
2083 /*
2084 * If succeed and that extended attribute
2085 * existing in inode, we will remove it.
2086 */
2087 xi.value = NULL;
2088 xi.value_len = 0;
2089 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2090 }
2091 }
2092 }
2093cleanup:
2094 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2095 ocfs2_inode_unlock(inode, 1);
2096 brelse(di_bh);
2097 brelse(xbs.xattr_bh);
01225596
TM
2098 for (i = 0; i < blk_per_bucket; i++)
2099 brelse(xbs.bucket.bhs[i]);
cf1d6c76
TY
2100
2101 return ret;
2102}
2103
0c044f0b
TM
2104/*
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.
2108 */
2109static 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)
2115{
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;
2121
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;
2127 }
2128
2129 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
2130 el = &eb->h_list;
2131
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;
2139 }
2140 }
2141
2142 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
2143 rec = &el->l_recs[i];
2144
2145 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
2146 e_blkno = le64_to_cpu(rec->e_blkno);
2147 break;
2148 }
2149 }
2150
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;
2158 }
2159
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);
2164out:
2165 brelse(eb_bh);
2166 return ret;
2167}
2168
2169typedef int (xattr_bucket_func)(struct inode *inode,
2170 struct ocfs2_xattr_bucket *bucket,
2171 void *para);
2172
589dc260
TM
2173static 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)
2180{
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;
2188
2189 /*
2190 * We don't use binary search in the bucket because there
2191 * may be multiple entries with the same name hash.
2192 */
2193 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
2194 xe = &xh->xh_entries[i];
2195
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;
2200
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;
2206
2207 ret = ocfs2_xattr_bucket_get_name_value(inode,
2208 xh,
2209 i,
2210 &block_off,
2211 &new_offset);
2212 if (ret) {
2213 mlog_errno(ret);
2214 break;
2215 }
2216
0fcaa56a
JB
2217 ret = ocfs2_read_block(inode, header_bh->b_blocknr + block_off,
2218 &name_bh);
589dc260
TM
2219 if (ret) {
2220 mlog_errno(ret);
2221 break;
2222 }
2223 xe_name = name_bh->b_data + new_offset;
2224
2225 cmp = memcmp(name, xe_name, name_len);
2226 brelse(name_bh);
2227 name_bh = NULL;
2228
2229 if (cmp == 0) {
2230 *xe_index = i;
2231 *found = 1;
2232 ret = 0;
2233 break;
2234 }
2235 }
2236
2237 return ret;
2238}
2239
2240/*
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.
2245 *
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.
2248 */
2249static 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)
2257{
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;
2268
0fcaa56a 2269 ret = ocfs2_read_block(inode, p_blkno, &bh);
589dc260
TM
2270 if (ret) {
2271 mlog_errno(ret);
2272 goto out;
2273 }
2274
2275 xh = (struct ocfs2_xattr_header *)bh->b_data;
2276 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
2277
2278 while (low_bucket <= high_bucket) {
2279 brelse(bh);
2280 bh = NULL;
2281 bucket = (low_bucket + high_bucket) / 2;
2282
2283 blkno = p_blkno + bucket * blk_per_bucket;
2284
0fcaa56a 2285 ret = ocfs2_read_block(inode, blkno, &bh);
589dc260
TM
2286 if (ret) {
2287 mlog_errno(ret);
2288 goto out;
2289 }
2290
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;
2296 }
2297
2298 /*
2299 * Check whether the hash of the last entry in our
5a095611
TM
2300 * bucket is larger than the search one. for an empty
2301 * bucket, the last one is also the first one.
589dc260 2302 */
5a095611
TM
2303 if (xh->xh_count)
2304 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
2305
589dc260
TM
2306 last_hash = le32_to_cpu(xe->xe_name_hash);
2307
2308 /* record lower_bh which may be the insert place. */
2309 brelse(lower_bh);
2310 lower_bh = bh;
2311 bh = NULL;
2312
2313 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
2314 low_bucket = bucket + 1;
2315 continue;
2316 }
2317
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;
2325 }
2326 break;
2327 }
2328
2329 /*
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.
2333 */
2334 if (!lower_bh) {
2335 /*
2336 * We can't find any bucket whose first name_hash is less
2337 * than the find name_hash.
2338 */
2339 BUG_ON(bh->b_blocknr != p_blkno);
2340 lower_bh = bh;
2341 bh = NULL;
2342 }
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;
2347
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;
2351
2352 if (found) {
2353 /*
2354 * If we have found the xattr enty, read all the blocks in
2355 * this bucket.
2356 */
31d33073 2357 ret = ocfs2_read_blocks(inode, xs->bucket.bhs[0]->b_blocknr + 1,
589dc260 2358 blk_per_bucket - 1, &xs->bucket.bhs[1],
1efd47f8 2359 0);
589dc260
TM
2360 if (ret) {
2361 mlog_errno(ret);
2362 goto out;
2363 }
2364
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;
2370
2371out:
2372 brelse(bh);
2373 brelse(lower_bh);
2374 return ret;
2375}
2376
2377static 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)
2382{
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;
2057e5c6 2390 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
589dc260
TM
2391
2392 if (le16_to_cpu(el->l_next_free_rec) == 0)
2393 return -ENODATA;
2394
2395 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
2396 name, name_hash, name_index);
2397
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;
2403 }
2404
2405 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
2406
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);
2409
2410 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
2411 p_blkno, first_hash, num_clusters, xs);
2412
2413out:
2414 return ret;
2415}
2416
0c044f0b
TM
2417static int ocfs2_iterate_xattr_buckets(struct inode *inode,
2418 u64 blkno,
2419 u32 clusters,
2420 xattr_bucket_func *func,
2421 void *para)
2422{
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;
2428
2429 memset(&bucket, 0, sizeof(bucket));
2430
2431 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
2432 clusters, blkno);
2433
2434 for (i = 0; i < num_buckets; i++, blkno += blk_per_bucket) {
31d33073 2435 ret = ocfs2_read_blocks(inode, blkno, blk_per_bucket,
1efd47f8 2436 bucket.bhs, 0);
0c044f0b
TM
2437 if (ret) {
2438 mlog_errno(ret);
2439 goto out;
2440 }
2441
2442 bucket.xh = (struct ocfs2_xattr_header *)bucket.bhs[0]->b_data;
2443 /*
2444 * The real bucket num in this series of blocks is stored
2445 * in the 1st bucket.
2446 */
2447 if (i == 0)
2448 num_buckets = le16_to_cpu(bucket.xh->xh_num_buckets);
2449
5a095611
TM
2450 mlog(0, "iterating xattr bucket %llu, first hash %u\n", blkno,
2451 le32_to_cpu(bucket.xh->xh_entries[0].xe_name_hash));
0c044f0b
TM
2452 if (func) {
2453 ret = func(inode, &bucket, para);
2454 if (ret) {
2455 mlog_errno(ret);
2456 break;
2457 }
2458 }
2459
2460 for (j = 0; j < blk_per_bucket; j++)
2461 brelse(bucket.bhs[j]);
2462 memset(&bucket, 0, sizeof(bucket));
2463 }
2464
2465out:
2466 for (j = 0; j < blk_per_bucket; j++)
2467 brelse(bucket.bhs[j]);
2468
2469 return ret;
2470}
2471
2472struct ocfs2_xattr_tree_list {
2473 char *buffer;
2474 size_t buffer_size;
936b8834 2475 size_t result;
0c044f0b
TM
2476};
2477
2478static 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)
2483{
2484 u16 name_offset;
2485
2486 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
2487 return -EINVAL;
2488
2489 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
2490
2491 *block_off = name_offset >> inode->i_sb->s_blocksize_bits;
2492 *new_offset = name_offset % inode->i_sb->s_blocksize;
2493
2494 return 0;
2495}
2496
2497static int ocfs2_list_xattr_bucket(struct inode *inode,
2498 struct ocfs2_xattr_bucket *bucket,
2499 void *para)
2500{
936b8834 2501 int ret = 0, type;
0c044f0b 2502 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
0c044f0b 2503 int i, block_off, new_offset;
936b8834 2504 const char *prefix, *name;
0c044f0b
TM
2505
2506 for (i = 0 ; i < le16_to_cpu(bucket->xh->xh_count); i++) {
2507 struct ocfs2_xattr_entry *entry = &bucket->xh->xh_entries[i];
936b8834
TM
2508 type = ocfs2_xattr_get_type(entry);
2509 prefix = ocfs2_xattr_prefix(type);
0c044f0b 2510
936b8834 2511 if (prefix) {
0c044f0b
TM
2512 ret = ocfs2_xattr_bucket_get_name_value(inode,
2513 bucket->xh,
2514 i,
2515 &block_off,
2516 &new_offset);
2517 if (ret)
2518 break;
936b8834
TM
2519
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;
0c044f0b
TM
2529 }
2530 }
2531
2532 return ret;
2533}
2534
2535static 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)
2539{
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,
936b8834 2547 .result = 0,
0c044f0b
TM
2548 };
2549
2550 if (le16_to_cpu(el->l_next_free_rec) == 0)
2551 return 0;
2552
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;
2559 }
2560
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;
2567 }
2568
2569 if (e_cpos == 0)
2570 break;
2571
2572 name_hash = e_cpos - 1;
2573 }
2574
936b8834 2575 ret = xl.result;
0c044f0b
TM
2576out:
2577 return ret;
2578}
01225596
TM
2579
2580static int cmp_xe(const void *a, const void *b)
2581{
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);
2585
2586 if (l_hash > r_hash)
2587 return 1;
2588 if (l_hash < r_hash)
2589 return -1;
2590 return 0;
2591}
2592
2593static void swap_xe(void *a, void *b, int size)
2594{
2595 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
2596
2597 tmp = *l;
2598 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
2599 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
2600}
2601
2602/*
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.
2607 */
2608static 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)
2612{
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;
2623
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);
2627
2628 memset(xh_bh->b_data, 0, blocksize);
2629 if (data_bh)
2630 memset(data_bh->b_data, 0, blocksize);
2631 /*
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.
2635 */
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;
2640
2641 /* copy all the names and values. */
2642 if (data_bh)
2643 target = data_bh->b_data;
2644 memcpy(target + offset, src + offset, size);
2645
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);
2651
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);
2657
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);
2663
2664 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
2665 offset, size, off_change);
2666
2667 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
2668 cmp_xe, swap_xe);
2669}
2670
2671/*
2672 * After we move xattr from block to index btree, we have to
2673 * update ocfs2_xattr_search to the new xe and base.
2674 *
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.
2678 */
2679static 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)
2683{
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);
2690
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;
2695
2696 xs->base = new_bh->b_data;
2697 xs->end = xs->base + inode->i_sb->s_blocksize;
2698
2699 if (!xs->not_found) {
2700 if (OCFS2_XATTR_BUCKET_SIZE != blocksize) {
31d33073 2701 ret = ocfs2_read_blocks(inode,
01225596
TM
2702 xs->bucket.bhs[0]->b_blocknr + 1,
2703 blk_per_bucket - 1, &xs->bucket.bhs[1],
1efd47f8 2704 0);
01225596
TM
2705 if (ret) {
2706 mlog_errno(ret);
2707 return ret;
2708 }
2709
2710 i = xs->here - old_xh->xh_entries;
2711 xs->here = &xs->header->xh_entries[i];
2712 }
2713 }
2714
2715 return ret;
2716}
2717
2718static int ocfs2_xattr_create_index_block(struct inode *inode,
2719 struct ocfs2_xattr_search *xs)
2720{
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);
2735
2736 mlog(0, "create xattr index block for %llu\n",
2737 (unsigned long long)xb_bh->b_blocknr);
2738
2739 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
2740
2741 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
2742 if (ret) {
2743 mlog_errno(ret);
2744 goto out;
2745 }
2746
2747 /*
2748 * XXX:
2749 * We can use this lock for now, and maybe move to a dedicated mutex
2750 * if performance becomes a problem later.
2751 */
2752 down_write(&oi->ip_alloc_sem);
2753
2754 /*
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.
2757 */
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;
2764 }
2765
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;
2771 }
2772
2773 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
2774 if (ret) {
2775 mlog_errno(ret);
2776 goto out_commit;
2777 }
2778
2779 /*
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).
2783 */
2784 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
2785
2786 mlog(0, "allocate 1 cluster from %llu to xattr block\n", blkno);
2787
2788 xh_bh = sb_getblk(inode->i_sb, blkno);
2789 if (!xh_bh) {
2790 ret = -EIO;
2791 mlog_errno(ret);
2792 goto out_commit;
2793 }
2794
2795 ocfs2_set_new_buffer_uptodate(inode, xh_bh);
2796
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;
2802 }
2803
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;
2810 }
2811
2812 ocfs2_set_new_buffer_uptodate(inode, data_bh);
2813
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;
2819 }
2820 }
2821
2822 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xh_bh, data_bh);
2823
2824 ocfs2_journal_dirty(handle, xh_bh);
2825 if (data_bh)
2826 ocfs2_journal_dirty(handle, data_bh);
2827
bd60bd37
JB
2828 ret = ocfs2_xattr_update_xattr_search(inode, xs, xb_bh, xh_bh);
2829 if (ret) {
2830 mlog_errno(ret);
2831 goto out_commit;
2832 }
01225596
TM
2833
2834 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
2835 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
2836 offsetof(struct ocfs2_xattr_block, xb_attrs));
2837
2838 xr = &xb->xb_attrs.xb_root;
2839 xr->xt_clusters = cpu_to_le32(1);
2840 xr->xt_last_eb_blk = 0;
2841 xr->xt_list.l_tree_depth = 0;
2842 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
2843 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2844
2845 xr->xt_list.l_recs[0].e_cpos = 0;
2846 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
2847 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
2848
2849 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
2850
2851 ret = ocfs2_journal_dirty(handle, xb_bh);
2852 if (ret) {
2853 mlog_errno(ret);
2854 goto out_commit;
2855 }
2856
2857out_commit:
2858 ocfs2_commit_trans(osb, handle);
2859
2860out_sem:
2861 up_write(&oi->ip_alloc_sem);
2862
2863out:
2864 if (data_ac)
2865 ocfs2_free_alloc_context(data_ac);
2866
2867 brelse(xh_bh);
2868 brelse(data_bh);
2869
2870 return ret;
2871}
2872
2873static int cmp_xe_offset(const void *a, const void *b)
2874{
2875 const struct ocfs2_xattr_entry *l = a, *r = b;
2876 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
2877 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
2878
2879 if (l_name_offset < r_name_offset)
2880 return 1;
2881 if (l_name_offset > r_name_offset)
2882 return -1;
2883 return 0;
2884}
2885
2886/*
2887 * defrag a xattr bucket if we find that the bucket has some
2888 * holes beteen name/value pairs.
2889 * We will move all the name/value pairs to the end of the bucket
2890 * so that we can spare some space for insertion.
2891 */
2892static int ocfs2_defrag_xattr_bucket(struct inode *inode,
2893 struct ocfs2_xattr_bucket *bucket)
2894{
2895 int ret, i;
2896 size_t end, offset, len, value_len;
2897 struct ocfs2_xattr_header *xh;
2898 char *entries, *buf, *bucket_buf = NULL;
2899 u64 blkno = bucket->bhs[0]->b_blocknr;
2900 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2901 u16 xh_free_start;
01225596
TM
2902 size_t blocksize = inode->i_sb->s_blocksize;
2903 handle_t *handle;
2904 struct buffer_head **bhs;
2905 struct ocfs2_xattr_entry *xe;
2906
2907 bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
2908 GFP_NOFS);
2909 if (!bhs)
2910 return -ENOMEM;
2911
1efd47f8 2912 ret = ocfs2_read_blocks(inode, blkno, blk_per_bucket, bhs, 0);
01225596
TM
2913 if (ret)
2914 goto out;
2915
2916 /*
2917 * In order to make the operation more efficient and generic,
2918 * we copy all the blocks into a contiguous memory and do the
2919 * defragment there, so if anything is error, we will not touch
2920 * the real block.
2921 */
2922 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
2923 if (!bucket_buf) {
2924 ret = -EIO;
2925 goto out;
2926 }
2927
2928 buf = bucket_buf;
2929 for (i = 0; i < blk_per_bucket; i++, buf += blocksize)
2930 memcpy(buf, bhs[i]->b_data, blocksize);
2931
2932 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), blk_per_bucket);
2933 if (IS_ERR(handle)) {
2934 ret = PTR_ERR(handle);
2935 handle = NULL;
2936 mlog_errno(ret);
2937 goto out;
2938 }
2939
2940 for (i = 0; i < blk_per_bucket; i++) {
2941 ret = ocfs2_journal_access(handle, inode, bhs[i],
2942 OCFS2_JOURNAL_ACCESS_WRITE);
2943 if (ret < 0) {
2944 mlog_errno(ret);
2945 goto commit;
2946 }
2947 }
2948
2949 xh = (struct ocfs2_xattr_header *)bucket_buf;
2950 entries = (char *)xh->xh_entries;
2951 xh_free_start = le16_to_cpu(xh->xh_free_start);
2952
2953 mlog(0, "adjust xattr bucket in %llu, count = %u, "
2954 "xh_free_start = %u, xh_name_value_len = %u.\n",
2955 blkno, le16_to_cpu(xh->xh_count), xh_free_start,
2956 le16_to_cpu(xh->xh_name_value_len));
2957
2958 /*
2959 * sort all the entries by their offset.
2960 * the largest will be the first, so that we can
2961 * move them to the end one by one.
2962 */
2963 sort(entries, le16_to_cpu(xh->xh_count),
2964 sizeof(struct ocfs2_xattr_entry),
2965 cmp_xe_offset, swap_xe);
2966
2967 /* Move all name/values to the end of the bucket. */
2968 xe = xh->xh_entries;
2969 end = OCFS2_XATTR_BUCKET_SIZE;
2970 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
2971 offset = le16_to_cpu(xe->xe_name_offset);
2972 if (ocfs2_xattr_is_local(xe))
2973 value_len = OCFS2_XATTR_SIZE(
2974 le64_to_cpu(xe->xe_value_size));
2975 else
2976 value_len = OCFS2_XATTR_ROOT_SIZE;
2977 len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
2978
2979 /*
2980 * We must make sure that the name/value pair
2981 * exist in the same block. So adjust end to
2982 * the previous block end if needed.
2983 */
2984 if (((end - len) / blocksize !=
2985 (end - 1) / blocksize))
2986 end = end - end % blocksize;
2987
2988 if (end > offset + len) {
2989 memmove(bucket_buf + end - len,
2990 bucket_buf + offset, len);
2991 xe->xe_name_offset = cpu_to_le16(end - len);
2992 }
2993
2994 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
2995 "bucket %llu\n", (unsigned long long)blkno);
2996
2997 end -= len;
2998 }
2999
3000 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
3001 "bucket %llu\n", (unsigned long long)blkno);
3002
3003 if (xh_free_start == end)
3004 goto commit;
3005
3006 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
3007 xh->xh_free_start = cpu_to_le16(end);
3008
3009 /* sort the entries by their name_hash. */
3010 sort(entries, le16_to_cpu(xh->xh_count),
3011 sizeof(struct ocfs2_xattr_entry),
3012 cmp_xe, swap_xe);
3013
3014 buf = bucket_buf;
3015 for (i = 0; i < blk_per_bucket; i++, buf += blocksize) {
3016 memcpy(bhs[i]->b_data, buf, blocksize);
3017 ocfs2_journal_dirty(handle, bhs[i]);
3018 }
3019
3020commit:
3021 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
3022out:
3023
3024 if (bhs) {
3025 for (i = 0; i < blk_per_bucket; i++)
3026 brelse(bhs[i]);
3027 }
3028 kfree(bhs);
3029
3030 kfree(bucket_buf);
3031 return ret;
3032}
3033
3034/*
3035 * Move half nums of the xattr bucket in the previous cluster to this new
3036 * cluster. We only touch the last cluster of the previous extend record.
3037 *
3038 * first_bh is the first buffer_head of a series of bucket in the same
3039 * extent rec and header_bh is the header of one bucket in this cluster.
3040 * They will be updated if we move the data header_bh contains to the new
3041 * cluster. first_hash will be set as the 1st xe's name_hash of the new cluster.
3042 */
3043static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
3044 handle_t *handle,
3045 struct buffer_head **first_bh,
3046 struct buffer_head **header_bh,
3047 u64 new_blkno,
3048 u64 prev_blkno,
3049 u32 num_clusters,
3050 u32 *first_hash)
3051{
3052 int i, ret, credits;
3053 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3054 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3055 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3056 int blocksize = inode->i_sb->s_blocksize;
3057 struct buffer_head *old_bh, *new_bh, *prev_bh, *new_first_bh = NULL;
3058 struct ocfs2_xattr_header *new_xh;
3059 struct ocfs2_xattr_header *xh =
3060 (struct ocfs2_xattr_header *)((*first_bh)->b_data);
3061
3062 BUG_ON(le16_to_cpu(xh->xh_num_buckets) < num_buckets);
3063 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == osb->s_clustersize);
3064
3065 prev_bh = *first_bh;
3066 get_bh(prev_bh);
3067 xh = (struct ocfs2_xattr_header *)prev_bh->b_data;
3068
3069 prev_blkno += (num_clusters - 1) * bpc + bpc / 2;
3070
3071 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
3072 prev_blkno, new_blkno);
3073
3074 /*
3075 * We need to update the 1st half of the new cluster and
3076 * 1 more for the update of the 1st bucket of the previous
3077 * extent record.
3078 */
3079 credits = bpc / 2 + 1;
3080 ret = ocfs2_extend_trans(handle, credits);
3081 if (ret) {
3082 mlog_errno(ret);
3083 goto out;
3084 }
3085
3086 ret = ocfs2_journal_access(handle, inode, prev_bh,
3087 OCFS2_JOURNAL_ACCESS_WRITE);
3088 if (ret) {
3089 mlog_errno(ret);
3090 goto out;
3091 }
3092
3093 for (i = 0; i < bpc / 2; i++, prev_blkno++, new_blkno++) {
3094 old_bh = new_bh = NULL;
3095 new_bh = sb_getblk(inode->i_sb, new_blkno);
3096 if (!new_bh) {
3097 ret = -EIO;
3098 mlog_errno(ret);
3099 goto out;
3100 }
3101
3102 ocfs2_set_new_buffer_uptodate(inode, new_bh);
3103
3104 ret = ocfs2_journal_access(handle, inode, new_bh,
3105 OCFS2_JOURNAL_ACCESS_CREATE);
3106 if (ret < 0) {
3107 mlog_errno(ret);
3108 brelse(new_bh);
3109 goto out;
3110 }
3111
0fcaa56a 3112 ret = ocfs2_read_block(inode, prev_blkno, &old_bh);
01225596
TM
3113 if (ret < 0) {
3114 mlog_errno(ret);
3115 brelse(new_bh);
3116 goto out;
3117 }
3118
3119 memcpy(new_bh->b_data, old_bh->b_data, blocksize);
3120
3121 if (i == 0) {
3122 new_xh = (struct ocfs2_xattr_header *)new_bh->b_data;
3123 new_xh->xh_num_buckets = cpu_to_le16(num_buckets / 2);
3124
3125 if (first_hash)
3126 *first_hash = le32_to_cpu(
3127 new_xh->xh_entries[0].xe_name_hash);
3128 new_first_bh = new_bh;
3129 get_bh(new_first_bh);
3130 }
3131
3132 ocfs2_journal_dirty(handle, new_bh);
3133
3134 if (*header_bh == old_bh) {
3135 brelse(*header_bh);
3136 *header_bh = new_bh;
3137 get_bh(*header_bh);
3138
3139 brelse(*first_bh);
3140 *first_bh = new_first_bh;
3141 get_bh(*first_bh);
3142 }
3143 brelse(new_bh);
3144 brelse(old_bh);
3145 }
3146
3147 le16_add_cpu(&xh->xh_num_buckets, -(num_buckets / 2));
3148
3149 ocfs2_journal_dirty(handle, prev_bh);
3150out:
3151 brelse(prev_bh);
3152 brelse(new_first_bh);
3153 return ret;
3154}
3155
3156static int ocfs2_read_xattr_bucket(struct inode *inode,
3157 u64 blkno,
3158 struct buffer_head **bhs,
3159 int new)
3160{
3161 int ret = 0;
3162 u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3163
3164 if (!new)
31d33073 3165 return ocfs2_read_blocks(inode, blkno,
1efd47f8 3166 blk_per_bucket, bhs, 0);
01225596
TM
3167
3168 for (i = 0; i < blk_per_bucket; i++) {
3169 bhs[i] = sb_getblk(inode->i_sb, blkno + i);
3170 if (bhs[i] == NULL) {
3171 ret = -EIO;
3172 mlog_errno(ret);
3173 break;
3174 }
3175 ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
3176 }
3177
3178 return ret;
3179}
3180
3181/*
3182 * Move half num of the xattrs in old bucket(blk) to new bucket(new_blk).
3183 * first_hash will record the 1st hash of the new bucket.
3184 */
3185static int ocfs2_half_xattr_bucket(struct inode *inode,
3186 handle_t *handle,
3187 u64 blk,
3188 u64 new_blk,
3189 u32 *first_hash,
3190 int new_bucket_head)
3191{
3192 int ret, i;
3193 u16 count, start, len, name_value_len, xe_len, name_offset;
3194 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3195 struct buffer_head **s_bhs, **t_bhs = NULL;
3196 struct ocfs2_xattr_header *xh;
3197 struct ocfs2_xattr_entry *xe;
3198 int blocksize = inode->i_sb->s_blocksize;
3199
3200 mlog(0, "move half of xattrs from bucket %llu to %llu\n",
3201 blk, new_blk);
3202
3203 s_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
3204 if (!s_bhs)
3205 return -ENOMEM;
3206
3207 ret = ocfs2_read_xattr_bucket(inode, blk, s_bhs, 0);
3208 if (ret) {
3209 mlog_errno(ret);
3210 goto out;
3211 }
3212
3213 ret = ocfs2_journal_access(handle, inode, s_bhs[0],
3214 OCFS2_JOURNAL_ACCESS_WRITE);
3215 if (ret) {
3216 mlog_errno(ret);
3217 goto out;
3218 }
3219
3220 t_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
3221 if (!t_bhs) {
3222 ret = -ENOMEM;
3223 goto out;
3224 }
3225
3226 ret = ocfs2_read_xattr_bucket(inode, new_blk, t_bhs, new_bucket_head);
3227 if (ret) {
3228 mlog_errno(ret);
3229 goto out;
3230 }
3231
3232 for (i = 0; i < blk_per_bucket; i++) {
3233 ret = ocfs2_journal_access(handle, inode, t_bhs[i],
eb6ff239
JB
3234 new_bucket_head ?
3235 OCFS2_JOURNAL_ACCESS_CREATE :
3236 OCFS2_JOURNAL_ACCESS_WRITE);
01225596
TM
3237 if (ret) {
3238 mlog_errno(ret);
3239 goto out;
3240 }
3241 }
3242
3243 /* copy the whole bucket to the new first. */
3244 for (i = 0; i < blk_per_bucket; i++)
3245 memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
3246
3247 /* update the new bucket. */
3248 xh = (struct ocfs2_xattr_header *)t_bhs[0]->b_data;
3249 count = le16_to_cpu(xh->xh_count);
3250 start = count / 2;
3251
3252 /*
3253 * Calculate the total name/value len and xh_free_start for
3254 * the old bucket first.
3255 */
3256 name_offset = OCFS2_XATTR_BUCKET_SIZE;
3257 name_value_len = 0;
3258 for (i = 0; i < start; i++) {
3259 xe = &xh->xh_entries[i];
3260 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3261 if (ocfs2_xattr_is_local(xe))
3262 xe_len +=
3263 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3264 else
3265 xe_len += OCFS2_XATTR_ROOT_SIZE;
3266 name_value_len += xe_len;
3267 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
3268 name_offset = le16_to_cpu(xe->xe_name_offset);
3269 }
3270
3271 /*
3272 * Now begin the modification to the new bucket.
3273 *
3274 * In the new bucket, We just move the xattr entry to the beginning
3275 * and don't touch the name/value. So there will be some holes in the
3276 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
3277 * called.
3278 */
3279 xe = &xh->xh_entries[start];
3280 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
3281 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
ff1ec20e
MF
3282 (int)((char *)xe - (char *)xh),
3283 (int)((char *)xh->xh_entries - (char *)xh));
01225596
TM
3284 memmove((char *)xh->xh_entries, (char *)xe, len);
3285 xe = &xh->xh_entries[count - start];
3286 len = sizeof(struct ocfs2_xattr_entry) * start;
3287 memset((char *)xe, 0, len);
3288
3289 le16_add_cpu(&xh->xh_count, -start);
3290 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
3291
3292 /* Calculate xh_free_start for the new bucket. */
3293 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3294 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3295 xe = &xh->xh_entries[i];
3296 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3297 if (ocfs2_xattr_is_local(xe))
3298 xe_len +=
3299 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3300 else
3301 xe_len += OCFS2_XATTR_ROOT_SIZE;
3302 if (le16_to_cpu(xe->xe_name_offset) <
3303 le16_to_cpu(xh->xh_free_start))
3304 xh->xh_free_start = xe->xe_name_offset;
3305 }
3306
3307 /* set xh->xh_num_buckets for the new xh. */
3308 if (new_bucket_head)
3309 xh->xh_num_buckets = cpu_to_le16(1);
3310 else
3311 xh->xh_num_buckets = 0;
3312
3313 for (i = 0; i < blk_per_bucket; i++) {
3314 ocfs2_journal_dirty(handle, t_bhs[i]);
3315 if (ret)
3316 mlog_errno(ret);
3317 }
3318
3319 /* store the first_hash of the new bucket. */
3320 if (first_hash)
3321 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3322
3323 /*
3324 * Now only update the 1st block of the old bucket.
3325 * Please note that the entry has been sorted already above.
3326 */
3327 xh = (struct ocfs2_xattr_header *)s_bhs[0]->b_data;
3328 memset(&xh->xh_entries[start], 0,
3329 sizeof(struct ocfs2_xattr_entry) * (count - start));
3330 xh->xh_count = cpu_to_le16(start);
3331 xh->xh_free_start = cpu_to_le16(name_offset);
3332 xh->xh_name_value_len = cpu_to_le16(name_value_len);
3333
3334 ocfs2_journal_dirty(handle, s_bhs[0]);
3335 if (ret)
3336 mlog_errno(ret);
3337
3338out:
3339 if (s_bhs) {
3340 for (i = 0; i < blk_per_bucket; i++)
3341 brelse(s_bhs[i]);
3342 }
3343 kfree(s_bhs);
3344
3345 if (t_bhs) {
3346 for (i = 0; i < blk_per_bucket; i++)
3347 brelse(t_bhs[i]);
3348 }
3349 kfree(t_bhs);
3350
3351 return ret;
3352}
3353
3354/*
3355 * Copy xattr from one bucket to another bucket.
3356 *
3357 * The caller must make sure that the journal transaction
3358 * has enough space for journaling.
3359 */
3360static int ocfs2_cp_xattr_bucket(struct inode *inode,
3361 handle_t *handle,
3362 u64 s_blkno,
3363 u64 t_blkno,
3364 int t_is_new)
3365{
3366 int ret, i;
3367 int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3368 int blocksize = inode->i_sb->s_blocksize;
3369 struct buffer_head **s_bhs, **t_bhs = NULL;
3370
3371 BUG_ON(s_blkno == t_blkno);
3372
3373 mlog(0, "cp bucket %llu to %llu, target is %d\n",
3374 s_blkno, t_blkno, t_is_new);
3375
3376 s_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
3377 GFP_NOFS);
3378 if (!s_bhs)
3379 return -ENOMEM;
3380
3381 ret = ocfs2_read_xattr_bucket(inode, s_blkno, s_bhs, 0);
3382 if (ret)
3383 goto out;
3384
3385 t_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
3386 GFP_NOFS);
3387 if (!t_bhs) {
3388 ret = -ENOMEM;
3389 goto out;
3390 }
3391
3392 ret = ocfs2_read_xattr_bucket(inode, t_blkno, t_bhs, t_is_new);
3393 if (ret)
3394 goto out;
3395
3396 for (i = 0; i < blk_per_bucket; i++) {
3397 ret = ocfs2_journal_access(handle, inode, t_bhs[i],
eb6ff239
JB
3398 t_is_new ?
3399 OCFS2_JOURNAL_ACCESS_CREATE :
01225596
TM
3400 OCFS2_JOURNAL_ACCESS_WRITE);
3401 if (ret)
3402 goto out;
3403 }
3404
3405 for (i = 0; i < blk_per_bucket; i++) {
3406 memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
3407 ocfs2_journal_dirty(handle, t_bhs[i]);
3408 }
3409
3410out:
3411 if (s_bhs) {
3412 for (i = 0; i < blk_per_bucket; i++)
3413 brelse(s_bhs[i]);
3414 }
3415 kfree(s_bhs);
3416
3417 if (t_bhs) {
3418 for (i = 0; i < blk_per_bucket; i++)
3419 brelse(t_bhs[i]);
3420 }
3421 kfree(t_bhs);
3422
3423 return ret;
3424}
3425
3426/*
3427 * Copy one xattr cluster from src_blk to to_blk.
3428 * The to_blk will become the first bucket header of the cluster, so its
3429 * xh_num_buckets will be initialized as the bucket num in the cluster.
3430 */
3431static int ocfs2_cp_xattr_cluster(struct inode *inode,
3432 handle_t *handle,
3433 struct buffer_head *first_bh,
3434 u64 src_blk,
3435 u64 to_blk,
3436 u32 *first_hash)
3437{
3438 int i, ret, credits;
3439 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3440 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3441 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3442 struct buffer_head *bh = NULL;
3443 struct ocfs2_xattr_header *xh;
3444 u64 to_blk_start = to_blk;
3445
3446 mlog(0, "cp xattrs from cluster %llu to %llu\n", src_blk, to_blk);
3447
3448 /*
3449 * We need to update the new cluster and 1 more for the update of
3450 * the 1st bucket of the previous extent rec.
3451 */
3452 credits = bpc + 1;
3453 ret = ocfs2_extend_trans(handle, credits);
3454 if (ret) {
3455 mlog_errno(ret);
3456 goto out;
3457 }
3458
3459 ret = ocfs2_journal_access(handle, inode, first_bh,
3460 OCFS2_JOURNAL_ACCESS_WRITE);
3461 if (ret) {
3462 mlog_errno(ret);
3463 goto out;
3464 }
3465
3466 for (i = 0; i < num_buckets; i++) {
3467 ret = ocfs2_cp_xattr_bucket(inode, handle,
3468 src_blk, to_blk, 1);
3469 if (ret) {
3470 mlog_errno(ret);
3471 goto out;
3472 }
3473
3474 src_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3475 to_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3476 }
3477
3478 /* update the old bucket header. */
3479 xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3480 le16_add_cpu(&xh->xh_num_buckets, -num_buckets);
3481
3482 ocfs2_journal_dirty(handle, first_bh);
3483
3484 /* update the new bucket header. */
0fcaa56a 3485 ret = ocfs2_read_block(inode, to_blk_start, &bh);
01225596
TM
3486 if (ret < 0) {
3487 mlog_errno(ret);
3488 goto out;
3489 }
3490
3491 ret = ocfs2_journal_access(handle, inode, bh,
3492 OCFS2_JOURNAL_ACCESS_WRITE);
3493 if (ret) {
3494 mlog_errno(ret);
3495 goto out;
3496 }
3497
3498 xh = (struct ocfs2_xattr_header *)bh->b_data;
3499 xh->xh_num_buckets = cpu_to_le16(num_buckets);
3500
3501 ocfs2_journal_dirty(handle, bh);
3502
3503 if (first_hash)
3504 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3505out:
3506 brelse(bh);
3507 return ret;
3508}
3509
3510/*
3511 * Move half of the xattrs in this cluster to the new cluster.
3512 * This function should only be called when bucket size == cluster size.
3513 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
3514 */
3515static int ocfs2_half_xattr_cluster(struct inode *inode,
3516 handle_t *handle,
3517 u64 prev_blk,
3518 u64 new_blk,
3519 u32 *first_hash)
3520{
3521 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3522 int ret, credits = 2 * blk_per_bucket;
3523
3524 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
3525
3526 ret = ocfs2_extend_trans(handle, credits);
3527 if (ret) {
3528 mlog_errno(ret);
3529 return ret;
3530 }
3531
3532 /* Move half of the xattr in start_blk to the next bucket. */
3533 return ocfs2_half_xattr_bucket(inode, handle, prev_blk,
3534 new_blk, first_hash, 1);
3535}
3536
3537/*
3538 * Move some xattrs from the old cluster to the new one since they are not
3539 * contiguous in ocfs2 xattr tree.
3540 *
3541 * new_blk starts a new separate cluster, and we will move some xattrs from
3542 * prev_blk to it. v_start will be set as the first name hash value in this
3543 * new cluster so that it can be used as e_cpos during tree insertion and
3544 * don't collide with our original b-tree operations. first_bh and header_bh
3545 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
3546 * to extend the insert bucket.
3547 *
3548 * The problem is how much xattr should we move to the new one and when should
3549 * we update first_bh and header_bh?
3550 * 1. If cluster size > bucket size, that means the previous cluster has more
3551 * than 1 bucket, so just move half nums of bucket into the new cluster and
3552 * update the first_bh and header_bh if the insert bucket has been moved
3553 * to the new cluster.
3554 * 2. If cluster_size == bucket_size:
3555 * a) If the previous extent rec has more than one cluster and the insert
3556 * place isn't in the last cluster, copy the entire last cluster to the
3557 * new one. This time, we don't need to upate the first_bh and header_bh
3558 * since they will not be moved into the new cluster.
3559 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
3560 * the new one. And we set the extend flag to zero if the insert place is
3561 * moved into the new allocated cluster since no extend is needed.
3562 */
3563static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
3564 handle_t *handle,
3565 struct buffer_head **first_bh,
3566 struct buffer_head **header_bh,
3567 u64 new_blk,
3568 u64 prev_blk,
3569 u32 prev_clusters,
3570 u32 *v_start,
3571 int *extend)
3572{
3573 int ret = 0;
3574 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3575
3576 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
3577 prev_blk, prev_clusters, new_blk);
3578
3579 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1)
3580 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
3581 handle,
3582 first_bh,
3583 header_bh,
3584 new_blk,
3585 prev_blk,
3586 prev_clusters,
3587 v_start);
3588 else {
3589 u64 last_blk = prev_blk + bpc * (prev_clusters - 1);
3590
3591 if (prev_clusters > 1 && (*header_bh)->b_blocknr != last_blk)
3592 ret = ocfs2_cp_xattr_cluster(inode, handle, *first_bh,
3593 last_blk, new_blk,
3594 v_start);
3595 else {
3596 ret = ocfs2_half_xattr_cluster(inode, handle,
3597 last_blk, new_blk,
3598 v_start);
3599
3600 if ((*header_bh)->b_blocknr == last_blk && extend)
3601 *extend = 0;
3602 }
3603 }
3604
3605 return ret;
3606}
3607
3608/*
3609 * Add a new cluster for xattr storage.
3610 *
3611 * If the new cluster is contiguous with the previous one, it will be
3612 * appended to the same extent record, and num_clusters will be updated.
3613 * If not, we will insert a new extent for it and move some xattrs in
3614 * the last cluster into the new allocated one.
3615 * We also need to limit the maximum size of a btree leaf, otherwise we'll
3616 * lose the benefits of hashing because we'll have to search large leaves.
3617 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
3618 * if it's bigger).
3619 *
3620 * first_bh is the first block of the previous extent rec and header_bh
3621 * indicates the bucket we will insert the new xattrs. They will be updated
3622 * when the header_bh is moved into the new cluster.
3623 */
3624static int ocfs2_add_new_xattr_cluster(struct inode *inode,
3625 struct buffer_head *root_bh,
3626 struct buffer_head **first_bh,
3627 struct buffer_head **header_bh,
3628 u32 *num_clusters,
3629 u32 prev_cpos,
3630 u64 prev_blkno,
3631 int *extend)
3632{
3633 int ret, credits;
3634 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3635 u32 prev_clusters = *num_clusters;
3636 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
3637 u64 block;
3638 handle_t *handle = NULL;
3639 struct ocfs2_alloc_context *data_ac = NULL;
3640 struct ocfs2_alloc_context *meta_ac = NULL;
3641 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
f99b9b7c 3642 struct ocfs2_extent_tree et;
01225596
TM
3643
3644 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
3645 "previous xattr blkno = %llu\n",
3646 (unsigned long long)OCFS2_I(inode)->ip_blkno,
3647 prev_cpos, prev_blkno);
3648
8d6220d6 3649 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
f99b9b7c
JB
3650
3651 ret = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
3652 &data_ac, &meta_ac);
01225596
TM
3653 if (ret) {
3654 mlog_errno(ret);
3655 goto leave;
3656 }
3657
f99b9b7c
JB
3658 credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
3659 clusters_to_add);
01225596
TM
3660 handle = ocfs2_start_trans(osb, credits);
3661 if (IS_ERR(handle)) {
3662 ret = PTR_ERR(handle);
3663 handle = NULL;
3664 mlog_errno(ret);
3665 goto leave;
3666 }
3667
3668 ret = ocfs2_journal_access(handle, inode, root_bh,
3669 OCFS2_JOURNAL_ACCESS_WRITE);
3670 if (ret < 0) {
3671 mlog_errno(ret);
3672 goto leave;
3673 }
3674
3675 ret = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
3676 clusters_to_add, &bit_off, &num_bits);
3677 if (ret < 0) {
3678 if (ret != -ENOSPC)
3679 mlog_errno(ret);
3680 goto leave;
3681 }
3682
3683 BUG_ON(num_bits > clusters_to_add);
3684
3685 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
3686 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
3687 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
3688
3689 if (prev_blkno + prev_clusters * bpc == block &&
3690 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
3691 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
3692 /*
3693 * If this cluster is contiguous with the old one and
3694 * adding this new cluster, we don't surpass the limit of
3695 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
3696 * initialized and used like other buckets in the previous
3697 * cluster.
3698 * So add it as a contiguous one. The caller will handle
3699 * its init process.
3700 */
3701 v_start = prev_cpos + prev_clusters;
3702 *num_clusters = prev_clusters + num_bits;
3703 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
3704 num_bits);
3705 } else {
3706 ret = ocfs2_adjust_xattr_cross_cluster(inode,
3707 handle,
3708 first_bh,
3709 header_bh,
3710 block,
3711 prev_blkno,
3712 prev_clusters,
3713 &v_start,
3714 extend);
3715 if (ret) {
3716 mlog_errno(ret);
3717 goto leave;
3718 }
3719 }
3720
28b8ca0b
TM
3721 if (handle->h_buffer_credits < credits) {
3722 /*
3723 * The journal has been restarted before, and don't
3724 * have enough space for the insertion, so extend it
3725 * here.
3726 */
3727 ret = ocfs2_extend_trans(handle, credits);
3728 if (ret) {
3729 mlog_errno(ret);
3730 goto leave;
3731 }
3732 }
01225596
TM
3733 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
3734 num_bits, block, v_start);
f99b9b7c
JB
3735 ret = ocfs2_insert_extent(osb, handle, inode, &et, v_start, block,
3736 num_bits, 0, meta_ac);
01225596
TM
3737 if (ret < 0) {
3738 mlog_errno(ret);
3739 goto leave;
3740 }
3741
3742 ret = ocfs2_journal_dirty(handle, root_bh);
3743 if (ret < 0) {
3744 mlog_errno(ret);
3745 goto leave;
3746 }
3747
3748leave:
3749 if (handle)
3750 ocfs2_commit_trans(osb, handle);
3751 if (data_ac)
3752 ocfs2_free_alloc_context(data_ac);
3753 if (meta_ac)
3754 ocfs2_free_alloc_context(meta_ac);
3755
3756 return ret;
3757}
3758
3759/*
3760 * Extend a new xattr bucket and move xattrs to the end one by one until
3761 * We meet with start_bh. Only move half of the xattrs to the bucket after it.
3762 */
3763static int ocfs2_extend_xattr_bucket(struct inode *inode,
3764 struct buffer_head *first_bh,
3765 struct buffer_head *start_bh,
3766 u32 num_clusters)
3767{
3768 int ret, credits;
3769 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3770 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3771 u64 start_blk = start_bh->b_blocknr, end_blk;
3772 u32 num_buckets = num_clusters * ocfs2_xattr_buckets_per_cluster(osb);
3773 handle_t *handle;
3774 struct ocfs2_xattr_header *first_xh =
3775 (struct ocfs2_xattr_header *)first_bh->b_data;
3776 u16 bucket = le16_to_cpu(first_xh->xh_num_buckets);
3777
3778 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
3779 "from %llu, len = %u\n", start_blk,
3780 (unsigned long long)first_bh->b_blocknr, num_clusters);
3781
3782 BUG_ON(bucket >= num_buckets);
3783
3784 end_blk = first_bh->b_blocknr + (bucket - 1) * blk_per_bucket;
3785
3786 /*
3787 * We will touch all the buckets after the start_bh(include it).
3788 * Add one more bucket and modify the first_bh.
3789 */
3790 credits = end_blk - start_blk + 2 * blk_per_bucket + 1;
3791 handle = ocfs2_start_trans(osb, credits);
3792 if (IS_ERR(handle)) {
3793 ret = PTR_ERR(handle);
3794 handle = NULL;
3795 mlog_errno(ret);
3796 goto out;
3797 }
3798
3799 ret = ocfs2_journal_access(handle, inode, first_bh,
3800 OCFS2_JOURNAL_ACCESS_WRITE);
3801 if (ret) {
3802 mlog_errno(ret);
3803 goto commit;
3804 }
3805
3806 while (end_blk != start_blk) {
3807 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
3808 end_blk + blk_per_bucket, 0);
3809 if (ret)
3810 goto commit;
3811 end_blk -= blk_per_bucket;
3812 }
3813
3814 /* Move half of the xattr in start_blk to the next bucket. */
3815 ret = ocfs2_half_xattr_bucket(inode, handle, start_blk,
3816 start_blk + blk_per_bucket, NULL, 0);
3817
3818 le16_add_cpu(&first_xh->xh_num_buckets, 1);
3819 ocfs2_journal_dirty(handle, first_bh);
3820
3821commit:
3822 ocfs2_commit_trans(osb, handle);
3823out:
3824 return ret;
3825}
3826
3827/*
3828 * Add new xattr bucket in an extent record and adjust the buckets accordingly.
3829 * xb_bh is the ocfs2_xattr_block.
3830 * We will move all the buckets starting from header_bh to the next place. As
3831 * for this one, half num of its xattrs will be moved to the next one.
3832 *
3833 * We will allocate a new cluster if current cluster is full and adjust
3834 * header_bh and first_bh if the insert place is moved to the new cluster.
3835 */
3836static int ocfs2_add_new_xattr_bucket(struct inode *inode,
3837 struct buffer_head *xb_bh,
3838 struct buffer_head *header_bh)
3839{
3840 struct ocfs2_xattr_header *first_xh = NULL;
3841 struct buffer_head *first_bh = NULL;
3842 struct ocfs2_xattr_block *xb =
3843 (struct ocfs2_xattr_block *)xb_bh->b_data;
3844 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3845 struct ocfs2_extent_list *el = &xb_root->xt_list;
3846 struct ocfs2_xattr_header *xh =
3847 (struct ocfs2_xattr_header *)header_bh->b_data;
3848 u32 name_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3849 struct super_block *sb = inode->i_sb;
3850 struct ocfs2_super *osb = OCFS2_SB(sb);
3851 int ret, num_buckets, extend = 1;
3852 u64 p_blkno;
3853 u32 e_cpos, num_clusters;
3854
3855 mlog(0, "Add new xattr bucket starting form %llu\n",
3856 (unsigned long long)header_bh->b_blocknr);
3857
3858 /*
3859 * Add refrence for header_bh here because it may be
3860 * changed in ocfs2_add_new_xattr_cluster and we need
3861 * to free it in the end.
3862 */
3863 get_bh(header_bh);
3864
3865 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
3866 &num_clusters, el);
3867 if (ret) {
3868 mlog_errno(ret);
3869 goto out;
3870 }
3871
0fcaa56a 3872 ret = ocfs2_read_block(inode, p_blkno, &first_bh);
01225596
TM
3873 if (ret) {
3874 mlog_errno(ret);
3875 goto out;
3876 }
3877
3878 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
3879 first_xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3880
3881 if (num_buckets == le16_to_cpu(first_xh->xh_num_buckets)) {
3882 ret = ocfs2_add_new_xattr_cluster(inode,
3883 xb_bh,
3884 &first_bh,
3885 &header_bh,
3886 &num_clusters,
3887 e_cpos,
3888 p_blkno,
3889 &extend);
3890 if (ret) {
3891 mlog_errno(ret);
3892 goto out;
3893 }
3894 }
3895
3896 if (extend)
3897 ret = ocfs2_extend_xattr_bucket(inode,
3898 first_bh,
3899 header_bh,
3900 num_clusters);
3901 if (ret)
3902 mlog_errno(ret);
3903out:
3904 brelse(first_bh);
3905 brelse(header_bh);
3906 return ret;
3907}
3908
3909static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
3910 struct ocfs2_xattr_bucket *bucket,
3911 int offs)
3912{
3913 int block_off = offs >> inode->i_sb->s_blocksize_bits;
3914
3915 offs = offs % inode->i_sb->s_blocksize;
3916 return bucket->bhs[block_off]->b_data + offs;
3917}
3918
3919/*
3920 * Handle the normal xattr set, including replace, delete and new.
01225596
TM
3921 *
3922 * Note: "local" indicates the real data's locality. So we can't
3923 * just its bucket locality by its length.
3924 */
3925static void ocfs2_xattr_set_entry_normal(struct inode *inode,
3926 struct ocfs2_xattr_info *xi,
3927 struct ocfs2_xattr_search *xs,
3928 u32 name_hash,
5a095611 3929 int local)
01225596
TM
3930{
3931 struct ocfs2_xattr_entry *last, *xe;
3932 int name_len = strlen(xi->name);
3933 struct ocfs2_xattr_header *xh = xs->header;
3934 u16 count = le16_to_cpu(xh->xh_count), start;
3935 size_t blocksize = inode->i_sb->s_blocksize;
3936 char *val;
3937 size_t offs, size, new_size;
3938
3939 last = &xh->xh_entries[count];
3940 if (!xs->not_found) {
3941 xe = xs->here;
3942 offs = le16_to_cpu(xe->xe_name_offset);
3943 if (ocfs2_xattr_is_local(xe))
3944 size = OCFS2_XATTR_SIZE(name_len) +
3945 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3946 else
3947 size = OCFS2_XATTR_SIZE(name_len) +
3948 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
3949
3950 /*
3951 * If the new value will be stored outside, xi->value has been
3952 * initalized as an empty ocfs2_xattr_value_root, and the same
3953 * goes with xi->value_len, so we can set new_size safely here.
3954 * See ocfs2_xattr_set_in_bucket.
3955 */
3956 new_size = OCFS2_XATTR_SIZE(name_len) +
3957 OCFS2_XATTR_SIZE(xi->value_len);
3958
3959 le16_add_cpu(&xh->xh_name_value_len, -size);
3960 if (xi->value) {
3961 if (new_size > size)
3962 goto set_new_name_value;
3963
3964 /* Now replace the old value with new one. */
3965 if (local)
3966 xe->xe_value_size = cpu_to_le64(xi->value_len);
3967 else
3968 xe->xe_value_size = 0;
3969
3970 val = ocfs2_xattr_bucket_get_val(inode,
3971 &xs->bucket, offs);
3972 memset(val + OCFS2_XATTR_SIZE(name_len), 0,
3973 size - OCFS2_XATTR_SIZE(name_len));
3974 if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
3975 memcpy(val + OCFS2_XATTR_SIZE(name_len),
3976 xi->value, xi->value_len);
3977
3978 le16_add_cpu(&xh->xh_name_value_len, new_size);
3979 ocfs2_xattr_set_local(xe, local);
3980 return;
3981 } else {
5a095611
TM
3982 /*
3983 * Remove the old entry if there is more than one.
3984 * We don't remove the last entry so that we can
3985 * use it to indicate the hash value of the empty
3986 * bucket.
3987 */
01225596 3988 last -= 1;
01225596 3989 le16_add_cpu(&xh->xh_count, -1);
5a095611
TM
3990 if (xh->xh_count) {
3991 memmove(xe, xe + 1,
3992 (void *)last - (void *)xe);
3993 memset(last, 0,
3994 sizeof(struct ocfs2_xattr_entry));
3995 } else
3996 xh->xh_free_start =
3997 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3998
01225596
TM
3999 return;
4000 }
4001 } else {
4002 /* find a new entry for insert. */
4003 int low = 0, high = count - 1, tmp;
4004 struct ocfs2_xattr_entry *tmp_xe;
4005
5a095611 4006 while (low <= high && count) {
01225596
TM
4007 tmp = (low + high) / 2;
4008 tmp_xe = &xh->xh_entries[tmp];
4009
4010 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
4011 low = tmp + 1;
4012 else if (name_hash <
4013 le32_to_cpu(tmp_xe->xe_name_hash))
4014 high = tmp - 1;
06b240d8
TM
4015 else {
4016 low = tmp;
01225596 4017 break;
06b240d8 4018 }
01225596
TM
4019 }
4020
4021 xe = &xh->xh_entries[low];
4022 if (low != count)
4023 memmove(xe + 1, xe, (void *)last - (void *)xe);
4024
4025 le16_add_cpu(&xh->xh_count, 1);
4026 memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
4027 xe->xe_name_hash = cpu_to_le32(name_hash);
4028 xe->xe_name_len = name_len;
4029 ocfs2_xattr_set_type(xe, xi->name_index);
4030 }
4031
4032set_new_name_value:
4033 /* Insert the new name+value. */
4034 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
4035
4036 /*
4037 * We must make sure that the name/value pair
4038 * exists in the same block.
4039 */
4040 offs = le16_to_cpu(xh->xh_free_start);
4041 start = offs - size;
4042
4043 if (start >> inode->i_sb->s_blocksize_bits !=
4044 (offs - 1) >> inode->i_sb->s_blocksize_bits) {
4045 offs = offs - offs % blocksize;
4046 xh->xh_free_start = cpu_to_le16(offs);
4047 }
4048
4049 val = ocfs2_xattr_bucket_get_val(inode,
4050 &xs->bucket, offs - size);
4051 xe->xe_name_offset = cpu_to_le16(offs - size);
4052
4053 memset(val, 0, size);
4054 memcpy(val, xi->name, name_len);
4055 memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
4056
4057 xe->xe_value_size = cpu_to_le64(xi->value_len);
4058 ocfs2_xattr_set_local(xe, local);
4059 xs->here = xe;
4060 le16_add_cpu(&xh->xh_free_start, -size);
4061 le16_add_cpu(&xh->xh_name_value_len, size);
4062
4063 return;
4064}
4065
4066static int ocfs2_xattr_bucket_handle_journal(struct inode *inode,
4067 handle_t *handle,
4068 struct ocfs2_xattr_search *xs,
4069 struct buffer_head **bhs,
4070 u16 bh_num)
4071{
4072 int ret = 0, off, block_off;
4073 struct ocfs2_xattr_entry *xe = xs->here;
4074
4075 /*
4076 * First calculate all the blocks we should journal_access
4077 * and journal_dirty. The first block should always be touched.
4078 */
4079 ret = ocfs2_journal_dirty(handle, bhs[0]);
4080 if (ret)
4081 mlog_errno(ret);
4082
4083 /* calc the data. */
4084 off = le16_to_cpu(xe->xe_name_offset);
4085 block_off = off >> inode->i_sb->s_blocksize_bits;
4086 ret = ocfs2_journal_dirty(handle, bhs[block_off]);
4087 if (ret)
4088 mlog_errno(ret);
4089
4090 return ret;
4091}
4092
4093/*
4094 * Set the xattr entry in the specified bucket.
4095 * The bucket is indicated by xs->bucket and it should have the enough
4096 * space for the xattr insertion.
4097 */
4098static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
4099 struct ocfs2_xattr_info *xi,
4100 struct ocfs2_xattr_search *xs,
4101 u32 name_hash,
5a095611 4102 int local)
01225596
TM
4103{
4104 int i, ret;
4105 handle_t *handle = NULL;
4106 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4107 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4108
ff1ec20e
MF
4109 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4110 (unsigned long)xi->value_len, xi->name_index,
01225596
TM
4111 (unsigned long long)xs->bucket.bhs[0]->b_blocknr);
4112
4113 if (!xs->bucket.bhs[1]) {
31d33073 4114 ret = ocfs2_read_blocks(inode,
01225596
TM
4115 xs->bucket.bhs[0]->b_blocknr + 1,
4116 blk_per_bucket - 1, &xs->bucket.bhs[1],
1efd47f8 4117 0);
01225596
TM
4118 if (ret) {
4119 mlog_errno(ret);
4120 goto out;
4121 }
4122 }
4123
4124 handle = ocfs2_start_trans(osb, blk_per_bucket);
4125 if (IS_ERR(handle)) {
4126 ret = PTR_ERR(handle);
4127 handle = NULL;
4128 mlog_errno(ret);
4129 goto out;
4130 }
4131
4132 for (i = 0; i < blk_per_bucket; i++) {
4133 ret = ocfs2_journal_access(handle, inode, xs->bucket.bhs[i],
4134 OCFS2_JOURNAL_ACCESS_WRITE);
4135 if (ret < 0) {
4136 mlog_errno(ret);
4137 goto out;
4138 }
4139 }
4140
5a095611 4141 ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
01225596
TM
4142
4143 /*Only dirty the blocks we have touched in set xattr. */
4144 ret = ocfs2_xattr_bucket_handle_journal(inode, handle, xs,
4145 xs->bucket.bhs, blk_per_bucket);
4146 if (ret)
4147 mlog_errno(ret);
4148out:
4149 ocfs2_commit_trans(osb, handle);
4150
4151 return ret;
4152}
4153
4154static int ocfs2_xattr_value_update_size(struct inode *inode,
4155 struct buffer_head *xe_bh,
4156 struct ocfs2_xattr_entry *xe,
4157 u64 new_size)
4158{
4159 int ret;
4160 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4161 handle_t *handle = NULL;
4162
4163 handle = ocfs2_start_trans(osb, 1);
4164 if (handle == NULL) {
4165 ret = -ENOMEM;
4166 mlog_errno(ret);
4167 goto out;
4168 }
4169
4170 ret = ocfs2_journal_access(handle, inode, xe_bh,
4171 OCFS2_JOURNAL_ACCESS_WRITE);
4172 if (ret < 0) {
4173 mlog_errno(ret);
4174 goto out_commit;
4175 }
4176
4177 xe->xe_value_size = cpu_to_le64(new_size);
4178
4179 ret = ocfs2_journal_dirty(handle, xe_bh);
4180 if (ret < 0)
4181 mlog_errno(ret);
4182
4183out_commit:
4184 ocfs2_commit_trans(osb, handle);
4185out:
4186 return ret;
4187}
4188
4189/*
4190 * Truncate the specified xe_off entry in xattr bucket.
4191 * bucket is indicated by header_bh and len is the new length.
4192 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4193 *
4194 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4195 */
4196static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
4197 struct buffer_head *header_bh,
4198 int xe_off,
4199 int len)
4200{
4201 int ret, offset;
4202 u64 value_blk;
4203 struct buffer_head *value_bh = NULL;
4204 struct ocfs2_xattr_value_root *xv;
4205 struct ocfs2_xattr_entry *xe;
4206 struct ocfs2_xattr_header *xh =
4207 (struct ocfs2_xattr_header *)header_bh->b_data;
4208 size_t blocksize = inode->i_sb->s_blocksize;
4209
4210 xe = &xh->xh_entries[xe_off];
4211
4212 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
4213
4214 offset = le16_to_cpu(xe->xe_name_offset) +
4215 OCFS2_XATTR_SIZE(xe->xe_name_len);
4216
4217 value_blk = offset / blocksize;
4218
4219 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4220 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
4221 value_blk += header_bh->b_blocknr;
4222
0fcaa56a 4223 ret = ocfs2_read_block(inode, value_blk, &value_bh);
01225596
TM
4224 if (ret) {
4225 mlog_errno(ret);
4226 goto out;
4227 }
4228
4229 xv = (struct ocfs2_xattr_value_root *)
4230 (value_bh->b_data + offset % blocksize);
4231
4232 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
4233 xe_off, (unsigned long long)header_bh->b_blocknr, len);
4234 ret = ocfs2_xattr_value_truncate(inode, value_bh, xv, len);
4235 if (ret) {
4236 mlog_errno(ret);
4237 goto out;
4238 }
4239
4240 ret = ocfs2_xattr_value_update_size(inode, header_bh, xe, len);
4241 if (ret) {
4242 mlog_errno(ret);
4243 goto out;
4244 }
4245
4246out:
4247 brelse(value_bh);
4248 return ret;
4249}
4250
4251static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
4252 struct ocfs2_xattr_search *xs,
4253 int len)
4254{
4255 int ret, offset;
4256 struct ocfs2_xattr_entry *xe = xs->here;
4257 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
4258
4259 BUG_ON(!xs->bucket.bhs[0] || !xe || ocfs2_xattr_is_local(xe));
4260
4261 offset = xe - xh->xh_entries;
4262 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket.bhs[0],
4263 offset, len);
4264 if (ret)
4265 mlog_errno(ret);
4266
4267 return ret;
4268}
4269
4270static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
4271 struct ocfs2_xattr_search *xs,
4272 char *val,
4273 int value_len)
4274{
4275 int offset;
4276 struct ocfs2_xattr_value_root *xv;
4277 struct ocfs2_xattr_entry *xe = xs->here;
4278
4279 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
4280
4281 offset = le16_to_cpu(xe->xe_name_offset) +
4282 OCFS2_XATTR_SIZE(xe->xe_name_len);
4283
4284 xv = (struct ocfs2_xattr_value_root *)(xs->base + offset);
4285
4286 return __ocfs2_xattr_set_value_outside(inode, xv, val, value_len);
4287}
4288
01225596
TM
4289static int ocfs2_rm_xattr_cluster(struct inode *inode,
4290 struct buffer_head *root_bh,
4291 u64 blkno,
4292 u32 cpos,
4293 u32 len)
4294{
4295 int ret;
4296 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4297 struct inode *tl_inode = osb->osb_tl_inode;
4298 handle_t *handle;
4299 struct ocfs2_xattr_block *xb =
4300 (struct ocfs2_xattr_block *)root_bh->b_data;
01225596
TM
4301 struct ocfs2_alloc_context *meta_ac = NULL;
4302 struct ocfs2_cached_dealloc_ctxt dealloc;
f99b9b7c
JB
4303 struct ocfs2_extent_tree et;
4304
8d6220d6 4305 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
01225596
TM
4306
4307 ocfs2_init_dealloc_ctxt(&dealloc);
4308
4309 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
4310 cpos, len, (unsigned long long)blkno);
4311
4312 ocfs2_remove_xattr_clusters_from_cache(inode, blkno, len);
4313
f99b9b7c 4314 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
01225596
TM
4315 if (ret) {
4316 mlog_errno(ret);
4317 return ret;
4318 }
4319
4320 mutex_lock(&tl_inode->i_mutex);
4321
4322 if (ocfs2_truncate_log_needs_flush(osb)) {
4323 ret = __ocfs2_flush_truncate_log(osb);
4324 if (ret < 0) {
4325 mlog_errno(ret);
4326 goto out;
4327 }
4328 }
4329
4330 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
4331 if (handle == NULL) {
4332 ret = -ENOMEM;
4333 mlog_errno(ret);
4334 goto out;
4335 }
4336
4337 ret = ocfs2_journal_access(handle, inode, root_bh,
4338 OCFS2_JOURNAL_ACCESS_WRITE);
4339 if (ret) {
4340 mlog_errno(ret);
4341 goto out_commit;
4342 }
4343
f99b9b7c
JB
4344 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
4345 &dealloc);
01225596
TM
4346 if (ret) {
4347 mlog_errno(ret);
4348 goto out_commit;
4349 }
4350
4351 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
4352
4353 ret = ocfs2_journal_dirty(handle, root_bh);
4354 if (ret) {
4355 mlog_errno(ret);
4356 goto out_commit;
4357 }
4358
4359 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
4360 if (ret)
4361 mlog_errno(ret);
4362
4363out_commit:
4364 ocfs2_commit_trans(osb, handle);
4365out:
4366 ocfs2_schedule_truncate_log_flush(osb, 1);
4367
4368 mutex_unlock(&tl_inode->i_mutex);
4369
4370 if (meta_ac)
4371 ocfs2_free_alloc_context(meta_ac);
4372
4373 ocfs2_run_deallocs(osb, &dealloc);
4374
4375 return ret;
4376}
4377
01225596
TM
4378static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
4379 struct ocfs2_xattr_search *xs)
4380{
4381 handle_t *handle = NULL;
4382 struct ocfs2_xattr_header *xh = xs->bucket.xh;
4383 struct ocfs2_xattr_entry *last = &xh->xh_entries[
4384 le16_to_cpu(xh->xh_count) - 1];
4385 int ret = 0;
4386
4387 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), 1);
4388 if (IS_ERR(handle)) {
4389 ret = PTR_ERR(handle);
4390 mlog_errno(ret);
4391 return;
4392 }
4393
4394 ret = ocfs2_journal_access(handle, inode, xs->bucket.bhs[0],
4395 OCFS2_JOURNAL_ACCESS_WRITE);
4396 if (ret) {
4397 mlog_errno(ret);
4398 goto out_commit;
4399 }
4400
4401 /* Remove the old entry. */
4402 memmove(xs->here, xs->here + 1,
4403 (void *)last - (void *)xs->here);
4404 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
4405 le16_add_cpu(&xh->xh_count, -1);
4406
4407 ret = ocfs2_journal_dirty(handle, xs->bucket.bhs[0]);
4408 if (ret < 0)
4409 mlog_errno(ret);
4410out_commit:
4411 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
4412}
4413
4414/*
4415 * Set the xattr name/value in the bucket specified in xs.
4416 *
4417 * As the new value in xi may be stored in the bucket or in an outside cluster,
4418 * we divide the whole process into 3 steps:
4419 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
4420 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
4421 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
4422 * 4. If the clusters for the new outside value can't be allocated, we need
4423 * to free the xattr we allocated in set.
4424 */
4425static int ocfs2_xattr_set_in_bucket(struct inode *inode,
4426 struct ocfs2_xattr_info *xi,
4427 struct ocfs2_xattr_search *xs)
4428{
5a095611 4429 int ret, local = 1;
01225596
TM
4430 size_t value_len;
4431 char *val = (char *)xi->value;
4432 struct ocfs2_xattr_entry *xe = xs->here;
2057e5c6
TM
4433 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name,
4434 strlen(xi->name));
01225596
TM
4435
4436 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
4437 /*
4438 * We need to truncate the xattr storage first.
4439 *
4440 * If both the old and new value are stored to
4441 * outside block, we only need to truncate
4442 * the storage and then set the value outside.
4443 *
4444 * If the new value should be stored within block,
4445 * we should free all the outside block first and
4446 * the modification to the xattr block will be done
4447 * by following steps.
4448 */
4449 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4450 value_len = xi->value_len;
4451 else
4452 value_len = 0;
4453
4454 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4455 value_len);
4456 if (ret)
4457 goto out;
4458
4459 if (value_len)
4460 goto set_value_outside;
4461 }
4462
4463 value_len = xi->value_len;
4464 /* So we have to handle the inside block change now. */
4465 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
4466 /*
4467 * If the new value will be stored outside of block,
4468 * initalize a new empty value root and insert it first.
4469 */
4470 local = 0;
4471 xi->value = &def_xv;
4472 xi->value_len = OCFS2_XATTR_ROOT_SIZE;
4473 }
4474
5a095611 4475 ret = ocfs2_xattr_set_entry_in_bucket(inode, xi, xs, name_hash, local);
01225596
TM
4476 if (ret) {
4477 mlog_errno(ret);
4478 goto out;
4479 }
4480
5a095611
TM
4481 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
4482 goto out;
01225596 4483
5a095611
TM
4484 /* allocate the space now for the outside block storage. */
4485 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4486 value_len);
4487 if (ret) {
4488 mlog_errno(ret);
4489
4490 if (xs->not_found) {
4491 /*
4492 * We can't allocate enough clusters for outside
4493 * storage and we have allocated xattr already,
4494 * so need to remove it.
4495 */
4496 ocfs2_xattr_bucket_remove_xs(inode, xs);
01225596 4497 }
01225596
TM
4498 goto out;
4499 }
4500
4501set_value_outside:
4502 ret = ocfs2_xattr_bucket_set_value_outside(inode, xs, val, value_len);
4503out:
4504 return ret;
4505}
4506
4507/* check whether the xattr bucket is filled up with the same hash value. */
4508static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
4509 struct ocfs2_xattr_bucket *bucket)
4510{
4511 struct ocfs2_xattr_header *xh = bucket->xh;
4512
4513 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
4514 xh->xh_entries[0].xe_name_hash) {
4515 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
4516 "hash = %u\n",
4517 (unsigned long long)bucket->bhs[0]->b_blocknr,
4518 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
4519 return -ENOSPC;
4520 }
4521
4522 return 0;
4523}
4524
4525static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
4526 struct ocfs2_xattr_info *xi,
4527 struct ocfs2_xattr_search *xs)
4528{
4529 struct ocfs2_xattr_header *xh;
4530 struct ocfs2_xattr_entry *xe;
4531 u16 count, header_size, xh_free_start;
4532 int i, free, max_free, need, old;
4533 size_t value_size = 0, name_len = strlen(xi->name);
4534 size_t blocksize = inode->i_sb->s_blocksize;
4535 int ret, allocation = 0;
4536 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4537
4538 mlog_entry("Set xattr %s in xattr index block\n", xi->name);
4539
4540try_again:
4541 xh = xs->header;
4542 count = le16_to_cpu(xh->xh_count);
4543 xh_free_start = le16_to_cpu(xh->xh_free_start);
4544 header_size = sizeof(struct ocfs2_xattr_header) +
4545 count * sizeof(struct ocfs2_xattr_entry);
4546 max_free = OCFS2_XATTR_BUCKET_SIZE -
4547 le16_to_cpu(xh->xh_name_value_len) - header_size;
4548
4549 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
4550 "of %u which exceed block size\n",
4551 (unsigned long long)xs->bucket.bhs[0]->b_blocknr,
4552 header_size);
4553
4554 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4555 value_size = OCFS2_XATTR_ROOT_SIZE;
4556 else if (xi->value)
4557 value_size = OCFS2_XATTR_SIZE(xi->value_len);
4558
4559 if (xs->not_found)
4560 need = sizeof(struct ocfs2_xattr_entry) +
4561 OCFS2_XATTR_SIZE(name_len) + value_size;
4562 else {
4563 need = value_size + OCFS2_XATTR_SIZE(name_len);
4564
4565 /*
4566 * We only replace the old value if the new length is smaller
4567 * than the old one. Otherwise we will allocate new space in the
4568 * bucket to store it.
4569 */
4570 xe = xs->here;
4571 if (ocfs2_xattr_is_local(xe))
4572 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4573 else
4574 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4575
4576 if (old >= value_size)
4577 need = 0;
4578 }
4579
4580 free = xh_free_start - header_size;
4581 /*
4582 * We need to make sure the new name/value pair
4583 * can exist in the same block.
4584 */
4585 if (xh_free_start % blocksize < need)
4586 free -= xh_free_start % blocksize;
4587
4588 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
4589 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
4590 " %u\n", xs->not_found,
4591 (unsigned long long)xs->bucket.bhs[0]->b_blocknr,
4592 free, need, max_free, le16_to_cpu(xh->xh_free_start),
4593 le16_to_cpu(xh->xh_name_value_len));
4594
4595 if (free < need || count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4596 if (need <= max_free &&
4597 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4598 /*
4599 * We can create the space by defragment. Since only the
4600 * name/value will be moved, the xe shouldn't be changed
4601 * in xs.
4602 */
4603 ret = ocfs2_defrag_xattr_bucket(inode, &xs->bucket);
4604 if (ret) {
4605 mlog_errno(ret);
4606 goto out;
4607 }
4608
4609 xh_free_start = le16_to_cpu(xh->xh_free_start);
4610 free = xh_free_start - header_size;
4611 if (xh_free_start % blocksize < need)
4612 free -= xh_free_start % blocksize;
4613
4614 if (free >= need)
4615 goto xattr_set;
4616
4617 mlog(0, "Can't get enough space for xattr insert by "
4618 "defragment. Need %u bytes, but we have %d, so "
4619 "allocate new bucket for it.\n", need, free);
4620 }
4621
4622 /*
4623 * We have to add new buckets or clusters and one
4624 * allocation should leave us enough space for insert.
4625 */
4626 BUG_ON(allocation);
4627
4628 /*
4629 * We do not allow for overlapping ranges between buckets. And
4630 * the maximum number of collisions we will allow for then is
4631 * one bucket's worth, so check it here whether we need to
4632 * add a new bucket for the insert.
4633 */
4634 ret = ocfs2_check_xattr_bucket_collision(inode, &xs->bucket);
4635 if (ret) {
4636 mlog_errno(ret);
4637 goto out;
4638 }
4639
4640 ret = ocfs2_add_new_xattr_bucket(inode,
4641 xs->xattr_bh,
4642 xs->bucket.bhs[0]);
4643 if (ret) {
4644 mlog_errno(ret);
4645 goto out;
4646 }
4647
4648 for (i = 0; i < blk_per_bucket; i++)
4649 brelse(xs->bucket.bhs[i]);
4650
4651 memset(&xs->bucket, 0, sizeof(xs->bucket));
4652
4653 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
4654 xi->name_index,
4655 xi->name, xs);
4656 if (ret && ret != -ENODATA)
4657 goto out;
4658 xs->not_found = ret;
4659 allocation = 1;
4660 goto try_again;
4661 }
4662
4663xattr_set:
4664 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs);
4665out:
4666 mlog_exit(ret);
4667 return ret;
4668}
a3944256
TM
4669
4670static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
4671 struct ocfs2_xattr_bucket *bucket,
4672 void *para)
4673{
4674 int ret = 0;
4675 struct ocfs2_xattr_header *xh = bucket->xh;
4676 u16 i;
4677 struct ocfs2_xattr_entry *xe;
4678
4679 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4680 xe = &xh->xh_entries[i];
4681 if (ocfs2_xattr_is_local(xe))
4682 continue;
4683
4684 ret = ocfs2_xattr_bucket_value_truncate(inode,
4685 bucket->bhs[0],
4686 i, 0);
4687 if (ret) {
4688 mlog_errno(ret);
4689 break;
4690 }
4691 }
4692
4693 return ret;
4694}
4695
4696static int ocfs2_delete_xattr_index_block(struct inode *inode,
4697 struct buffer_head *xb_bh)
4698{
4699 struct ocfs2_xattr_block *xb =
4700 (struct ocfs2_xattr_block *)xb_bh->b_data;
4701 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
4702 int ret = 0;
4703 u32 name_hash = UINT_MAX, e_cpos, num_clusters;
4704 u64 p_blkno;
4705
4706 if (le16_to_cpu(el->l_next_free_rec) == 0)
4707 return 0;
4708
4709 while (name_hash > 0) {
4710 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
4711 &e_cpos, &num_clusters, el);
4712 if (ret) {
4713 mlog_errno(ret);
4714 goto out;
4715 }
4716
4717 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
4718 ocfs2_delete_xattr_in_bucket,
4719 NULL);
4720 if (ret) {
4721 mlog_errno(ret);
4722 goto out;
4723 }
4724
4725 ret = ocfs2_rm_xattr_cluster(inode, xb_bh,
4726 p_blkno, e_cpos, num_clusters);
4727 if (ret) {
4728 mlog_errno(ret);
4729 break;
4730 }
4731
4732 if (e_cpos == 0)
4733 break;
4734
4735 name_hash = e_cpos - 1;
4736 }
4737
4738out:
4739 return ret;
4740}
99219aea
MF
4741
4742/*
4743 * 'trusted' attributes support
4744 */
99219aea
MF
4745static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list,
4746 size_t list_size, const char *name,
4747 size_t name_len)
4748{
ceb1eba3 4749 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
99219aea
MF
4750 const size_t total_len = prefix_len + name_len + 1;
4751
4752 if (list && total_len <= list_size) {
4753 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
4754 memcpy(list + prefix_len, name, name_len);
4755 list[prefix_len + name_len] = '\0';
4756 }
4757 return total_len;
4758}
4759
4760static int ocfs2_xattr_trusted_get(struct inode *inode, const char *name,
4761 void *buffer, size_t size)
4762{
4763 if (strcmp(name, "") == 0)
4764 return -EINVAL;
4765 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, name,
4766 buffer, size);
4767}
4768
4769static int ocfs2_xattr_trusted_set(struct inode *inode, const char *name,
4770 const void *value, size_t size, int flags)
4771{
4772 if (strcmp(name, "") == 0)
4773 return -EINVAL;
4774
4775 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, name, value,
4776 size, flags);
4777}
4778
4779struct xattr_handler ocfs2_xattr_trusted_handler = {
4780 .prefix = XATTR_TRUSTED_PREFIX,
4781 .list = ocfs2_xattr_trusted_list,
4782 .get = ocfs2_xattr_trusted_get,
4783 .set = ocfs2_xattr_trusted_set,
4784};
4785
99219aea
MF
4786/*
4787 * 'user' attributes support
4788 */
99219aea
MF
4789static size_t ocfs2_xattr_user_list(struct inode *inode, char *list,
4790 size_t list_size, const char *name,
4791 size_t name_len)
4792{
ceb1eba3 4793 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
99219aea
MF
4794 const size_t total_len = prefix_len + name_len + 1;
4795 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4796
4797 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4798 return 0;
4799
4800 if (list && total_len <= list_size) {
4801 memcpy(list, XATTR_USER_PREFIX, prefix_len);
4802 memcpy(list + prefix_len, name, name_len);
4803 list[prefix_len + name_len] = '\0';
4804 }
4805 return total_len;
4806}
4807
4808static int ocfs2_xattr_user_get(struct inode *inode, const char *name,
4809 void *buffer, size_t size)
4810{
4811 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4812
4813 if (strcmp(name, "") == 0)
4814 return -EINVAL;
4815 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4816 return -EOPNOTSUPP;
4817 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name,
4818 buffer, size);
4819}
4820
4821static int ocfs2_xattr_user_set(struct inode *inode, const char *name,
4822 const void *value, size_t size, int flags)
4823{
4824 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4825
4826 if (strcmp(name, "") == 0)
4827 return -EINVAL;
4828 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4829 return -EOPNOTSUPP;
4830
4831 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, name, value,
4832 size, flags);
4833}
4834
4835struct xattr_handler ocfs2_xattr_user_handler = {
4836 .prefix = XATTR_USER_PREFIX,
4837 .list = ocfs2_xattr_user_list,
4838 .get = ocfs2_xattr_user_get,
4839 .set = ocfs2_xattr_user_set,
4840};