]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/ocfs2/xattr.c
ocfs2: Calculate EA hash only by its suffix.
[mirror_ubuntu-bionic-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 *
6 * Copyright (C) 2008 Oracle. All rights reserved.
7 *
cf1d6c76
TY
8 * CREDITS:
9 * Lots of code in this file is taken from ext3.
10 *
f56654c4
TM
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
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.
20 *
21 * You should have received a copy of the GNU General Public
22 * License along with this program; if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 021110-1307, USA.
25 */
26
cf1d6c76
TY
27#include <linux/capability.h>
28#include <linux/fs.h>
29#include <linux/types.h>
30#include <linux/slab.h>
31#include <linux/highmem.h>
32#include <linux/pagemap.h>
33#include <linux/uio.h>
34#include <linux/sched.h>
35#include <linux/splice.h>
36#include <linux/mount.h>
37#include <linux/writeback.h>
38#include <linux/falloc.h>
01225596 39#include <linux/sort.h>
99219aea
MF
40#include <linux/init.h>
41#include <linux/module.h>
42#include <linux/string.h>
cf1d6c76 43
f56654c4
TM
44#define MLOG_MASK_PREFIX ML_XATTR
45#include <cluster/masklog.h>
46
47#include "ocfs2.h"
48#include "alloc.h"
49#include "dlmglue.h"
50#include "file.h"
cf1d6c76
TY
51#include "symlink.h"
52#include "sysfile.h"
f56654c4
TM
53#include "inode.h"
54#include "journal.h"
55#include "ocfs2_fs.h"
56#include "suballoc.h"
57#include "uptodate.h"
58#include "buffer_head_io.h"
0c044f0b 59#include "super.h"
cf1d6c76
TY
60#include "xattr.h"
61
62
63struct ocfs2_xattr_def_value_root {
64 struct ocfs2_xattr_value_root xv;
65 struct ocfs2_extent_rec er;
66};
67
0c044f0b
TM
68struct ocfs2_xattr_bucket {
69 struct buffer_head *bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
70 struct ocfs2_xattr_header *xh;
71};
72
cf1d6c76
TY
73#define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
74#define OCFS2_XATTR_INLINE_SIZE 80
75
76static struct ocfs2_xattr_def_value_root def_xv = {
77 .xv.xr_list.l_count = cpu_to_le16(1),
78};
79
80struct xattr_handler *ocfs2_xattr_handlers[] = {
81 &ocfs2_xattr_user_handler,
82 &ocfs2_xattr_trusted_handler,
83 NULL
84};
85
86static struct xattr_handler *ocfs2_xattr_handler_map[] = {
87 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
88 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
89};
90
91struct ocfs2_xattr_info {
92 int name_index;
93 const char *name;
94 const void *value;
95 size_t value_len;
96};
97
98struct ocfs2_xattr_search {
99 struct buffer_head *inode_bh;
100 /*
101 * xattr_bh point to the block buffer head which has extended attribute
102 * when extended attribute in inode, xattr_bh is equal to inode_bh.
103 */
104 struct buffer_head *xattr_bh;
105 struct ocfs2_xattr_header *header;
589dc260 106 struct ocfs2_xattr_bucket bucket;
cf1d6c76
TY
107 void *base;
108 void *end;
109 struct ocfs2_xattr_entry *here;
110 int not_found;
111};
112
589dc260
TM
113static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
114 struct ocfs2_xattr_header *xh,
115 int index,
116 int *block_off,
117 int *new_offset);
118
119static int ocfs2_xattr_index_block_find(struct inode *inode,
120 struct buffer_head *root_bh,
121 int name_index,
122 const char *name,
123 struct ocfs2_xattr_search *xs);
124
0c044f0b
TM
125static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
126 struct ocfs2_xattr_tree_root *xt,
127 char *buffer,
128 size_t buffer_size);
129
01225596
TM
130static int ocfs2_xattr_create_index_block(struct inode *inode,
131 struct ocfs2_xattr_search *xs);
132
133static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
134 struct ocfs2_xattr_info *xi,
135 struct ocfs2_xattr_search *xs);
136
a3944256
TM
137static int ocfs2_delete_xattr_index_block(struct inode *inode,
138 struct buffer_head *xb_bh);
139
cf1d6c76
TY
140static inline struct xattr_handler *ocfs2_xattr_handler(int name_index)
141{
142 struct xattr_handler *handler = NULL;
143
144 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
145 handler = ocfs2_xattr_handler_map[name_index];
146
147 return handler;
148}
149
40daa16a 150static u32 ocfs2_xattr_name_hash(struct inode *inode,
2057e5c6 151 const char *name,
40daa16a 152 int name_len)
cf1d6c76
TY
153{
154 /* Get hash value of uuid from super block */
155 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
156 int i;
157
cf1d6c76
TY
158 /* hash extended attribute name */
159 for (i = 0; i < name_len; i++) {
160 hash = (hash << OCFS2_HASH_SHIFT) ^
161 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
162 *name++;
163 }
164
165 return hash;
166}
167
168/*
169 * ocfs2_xattr_hash_entry()
170 *
171 * Compute the hash of an extended attribute.
172 */
173static void ocfs2_xattr_hash_entry(struct inode *inode,
174 struct ocfs2_xattr_header *header,
175 struct ocfs2_xattr_entry *entry)
176{
177 u32 hash = 0;
cf1d6c76 178 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
cf1d6c76 179
2057e5c6 180 hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
cf1d6c76
TY
181 entry->xe_name_hash = cpu_to_le32(hash);
182
183 return;
184}
f56654c4
TM
185
186static int ocfs2_xattr_extend_allocation(struct inode *inode,
187 u32 clusters_to_add,
188 struct buffer_head *xattr_bh,
189 struct ocfs2_xattr_value_root *xv)
190{
191 int status = 0;
192 int restart_func = 0;
193 int credits = 0;
194 handle_t *handle = NULL;
195 struct ocfs2_alloc_context *data_ac = NULL;
196 struct ocfs2_alloc_context *meta_ac = NULL;
197 enum ocfs2_alloc_restarted why;
198 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
f56654c4 199 u32 prev_clusters, logical_start = le32_to_cpu(xv->xr_clusters);
f99b9b7c 200 struct ocfs2_extent_tree et;
f56654c4
TM
201
202 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
203
8d6220d6 204 ocfs2_init_xattr_value_extent_tree(&et, inode, xattr_bh, xv);
f99b9b7c 205
f56654c4
TM
206restart_all:
207
f99b9b7c
JB
208 status = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
209 &data_ac, &meta_ac);
f56654c4
TM
210 if (status) {
211 mlog_errno(status);
212 goto leave;
213 }
214
f99b9b7c
JB
215 credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
216 clusters_to_add);
f56654c4
TM
217 handle = ocfs2_start_trans(osb, credits);
218 if (IS_ERR(handle)) {
219 status = PTR_ERR(handle);
220 handle = NULL;
221 mlog_errno(status);
222 goto leave;
223 }
224
225restarted_transaction:
226 status = ocfs2_journal_access(handle, inode, xattr_bh,
227 OCFS2_JOURNAL_ACCESS_WRITE);
228 if (status < 0) {
229 mlog_errno(status);
230 goto leave;
231 }
232
233 prev_clusters = le32_to_cpu(xv->xr_clusters);
234 status = ocfs2_add_clusters_in_btree(osb,
235 inode,
236 &logical_start,
237 clusters_to_add,
238 0,
f99b9b7c 239 &et,
f56654c4
TM
240 handle,
241 data_ac,
242 meta_ac,
f99b9b7c 243 &why);
f56654c4
TM
244 if ((status < 0) && (status != -EAGAIN)) {
245 if (status != -ENOSPC)
246 mlog_errno(status);
247 goto leave;
248 }
249
250 status = ocfs2_journal_dirty(handle, xattr_bh);
251 if (status < 0) {
252 mlog_errno(status);
253 goto leave;
254 }
255
256 clusters_to_add -= le32_to_cpu(xv->xr_clusters) - prev_clusters;
257
258 if (why != RESTART_NONE && clusters_to_add) {
259 if (why == RESTART_META) {
260 mlog(0, "restarting function.\n");
261 restart_func = 1;
262 } else {
263 BUG_ON(why != RESTART_TRANS);
264
265 mlog(0, "restarting transaction.\n");
266 /* TODO: This can be more intelligent. */
267 credits = ocfs2_calc_extend_credits(osb->sb,
f99b9b7c 268 et.et_root_el,
f56654c4
TM
269 clusters_to_add);
270 status = ocfs2_extend_trans(handle, credits);
271 if (status < 0) {
272 /* handle still has to be committed at
273 * this point. */
274 status = -ENOMEM;
275 mlog_errno(status);
276 goto leave;
277 }
278 goto restarted_transaction;
279 }
280 }
281
282leave:
283 if (handle) {
284 ocfs2_commit_trans(osb, handle);
285 handle = NULL;
286 }
287 if (data_ac) {
288 ocfs2_free_alloc_context(data_ac);
289 data_ac = NULL;
290 }
291 if (meta_ac) {
292 ocfs2_free_alloc_context(meta_ac);
293 meta_ac = NULL;
294 }
295 if ((!status) && restart_func) {
296 restart_func = 0;
297 goto restart_all;
298 }
299
300 return status;
301}
302
303static int __ocfs2_remove_xattr_range(struct inode *inode,
304 struct buffer_head *root_bh,
305 struct ocfs2_xattr_value_root *xv,
306 u32 cpos, u32 phys_cpos, u32 len,
307 struct ocfs2_cached_dealloc_ctxt *dealloc)
308{
309 int ret;
310 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
311 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
312 struct inode *tl_inode = osb->osb_tl_inode;
313 handle_t *handle;
314 struct ocfs2_alloc_context *meta_ac = NULL;
f99b9b7c
JB
315 struct ocfs2_extent_tree et;
316
8d6220d6 317 ocfs2_init_xattr_value_extent_tree(&et, inode, root_bh, xv);
f56654c4 318
f99b9b7c 319 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
f56654c4
TM
320 if (ret) {
321 mlog_errno(ret);
322 return ret;
323 }
324
325 mutex_lock(&tl_inode->i_mutex);
326
327 if (ocfs2_truncate_log_needs_flush(osb)) {
328 ret = __ocfs2_flush_truncate_log(osb);
329 if (ret < 0) {
330 mlog_errno(ret);
331 goto out;
332 }
333 }
334
335 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
336 if (IS_ERR(handle)) {
337 ret = PTR_ERR(handle);
338 mlog_errno(ret);
339 goto out;
340 }
341
342 ret = ocfs2_journal_access(handle, inode, root_bh,
343 OCFS2_JOURNAL_ACCESS_WRITE);
344 if (ret) {
345 mlog_errno(ret);
346 goto out_commit;
347 }
348
f99b9b7c
JB
349 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
350 dealloc);
f56654c4
TM
351 if (ret) {
352 mlog_errno(ret);
353 goto out_commit;
354 }
355
356 le32_add_cpu(&xv->xr_clusters, -len);
357
358 ret = ocfs2_journal_dirty(handle, root_bh);
359 if (ret) {
360 mlog_errno(ret);
361 goto out_commit;
362 }
363
364 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
365 if (ret)
366 mlog_errno(ret);
367
368out_commit:
369 ocfs2_commit_trans(osb, handle);
370out:
371 mutex_unlock(&tl_inode->i_mutex);
372
373 if (meta_ac)
374 ocfs2_free_alloc_context(meta_ac);
375
376 return ret;
377}
378
379static int ocfs2_xattr_shrink_size(struct inode *inode,
380 u32 old_clusters,
381 u32 new_clusters,
382 struct buffer_head *root_bh,
383 struct ocfs2_xattr_value_root *xv)
384{
385 int ret = 0;
386 u32 trunc_len, cpos, phys_cpos, alloc_size;
387 u64 block;
388 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
389 struct ocfs2_cached_dealloc_ctxt dealloc;
390
391 ocfs2_init_dealloc_ctxt(&dealloc);
392
393 if (old_clusters <= new_clusters)
394 return 0;
395
396 cpos = new_clusters;
397 trunc_len = old_clusters - new_clusters;
398 while (trunc_len) {
399 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
400 &alloc_size, &xv->xr_list);
401 if (ret) {
402 mlog_errno(ret);
403 goto out;
404 }
405
406 if (alloc_size > trunc_len)
407 alloc_size = trunc_len;
408
409 ret = __ocfs2_remove_xattr_range(inode, root_bh, xv, cpos,
410 phys_cpos, alloc_size,
411 &dealloc);
412 if (ret) {
413 mlog_errno(ret);
414 goto out;
415 }
416
417 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
418 ocfs2_remove_xattr_clusters_from_cache(inode, block,
419 alloc_size);
420 cpos += alloc_size;
421 trunc_len -= alloc_size;
422 }
423
424out:
425 ocfs2_schedule_truncate_log_flush(osb, 1);
426 ocfs2_run_deallocs(osb, &dealloc);
427
428 return ret;
429}
430
431static int ocfs2_xattr_value_truncate(struct inode *inode,
432 struct buffer_head *root_bh,
433 struct ocfs2_xattr_value_root *xv,
434 int len)
435{
436 int ret;
437 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
438 u32 old_clusters = le32_to_cpu(xv->xr_clusters);
439
440 if (new_clusters == old_clusters)
441 return 0;
442
443 if (new_clusters > old_clusters)
444 ret = ocfs2_xattr_extend_allocation(inode,
445 new_clusters - old_clusters,
446 root_bh, xv);
447 else
448 ret = ocfs2_xattr_shrink_size(inode,
449 old_clusters, new_clusters,
450 root_bh, xv);
451
452 return ret;
453}
cf1d6c76
TY
454
455static int ocfs2_xattr_list_entries(struct inode *inode,
456 struct ocfs2_xattr_header *header,
457 char *buffer, size_t buffer_size)
458{
459 size_t rest = buffer_size;
460 int i;
461
462 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
463 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
464 struct xattr_handler *handler =
465 ocfs2_xattr_handler(ocfs2_xattr_get_type(entry));
466
467 if (handler) {
468 size_t size = handler->list(inode, buffer, rest,
469 ((char *)header +
470 le16_to_cpu(entry->xe_name_offset)),
471 entry->xe_name_len);
472 if (buffer) {
473 if (size > rest)
474 return -ERANGE;
475 buffer += size;
476 }
477 rest -= size;
478 }
479 }
480
481 return buffer_size - rest;
482}
483
484static int ocfs2_xattr_ibody_list(struct inode *inode,
485 struct ocfs2_dinode *di,
486 char *buffer,
487 size_t buffer_size)
488{
489 struct ocfs2_xattr_header *header = NULL;
490 struct ocfs2_inode_info *oi = OCFS2_I(inode);
491 int ret = 0;
492
493 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
494 return ret;
495
496 header = (struct ocfs2_xattr_header *)
497 ((void *)di + inode->i_sb->s_blocksize -
498 le16_to_cpu(di->i_xattr_inline_size));
499
500 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
501
502 return ret;
503}
504
505static int ocfs2_xattr_block_list(struct inode *inode,
506 struct ocfs2_dinode *di,
507 char *buffer,
508 size_t buffer_size)
509{
510 struct buffer_head *blk_bh = NULL;
0c044f0b 511 struct ocfs2_xattr_block *xb;
cf1d6c76
TY
512 int ret = 0;
513
514 if (!di->i_xattr_loc)
515 return ret;
516
517 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
518 le64_to_cpu(di->i_xattr_loc),
519 &blk_bh, OCFS2_BH_CACHED, inode);
520 if (ret < 0) {
521 mlog_errno(ret);
522 return ret;
523 }
524 /*Verify the signature of xattr block*/
525 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
526 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
527 ret = -EFAULT;
528 goto cleanup;
529 }
530
0c044f0b 531 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
cf1d6c76 532
0c044f0b
TM
533 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
534 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
535 ret = ocfs2_xattr_list_entries(inode, header,
536 buffer, buffer_size);
537 } else {
538 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
539 ret = ocfs2_xattr_tree_list_index_block(inode, xt,
540 buffer, buffer_size);
541 }
cf1d6c76
TY
542cleanup:
543 brelse(blk_bh);
544
545 return ret;
546}
547
548ssize_t ocfs2_listxattr(struct dentry *dentry,
549 char *buffer,
550 size_t size)
551{
552 int ret = 0, i_ret = 0, b_ret = 0;
553 struct buffer_head *di_bh = NULL;
554 struct ocfs2_dinode *di = NULL;
555 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
556
8154da3d
TY
557 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
558 return -EOPNOTSUPP;
559
cf1d6c76
TY
560 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
561 return ret;
562
563 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
564 if (ret < 0) {
565 mlog_errno(ret);
566 return ret;
567 }
568
569 di = (struct ocfs2_dinode *)di_bh->b_data;
570
571 down_read(&oi->ip_xattr_sem);
572 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
573 if (i_ret < 0)
574 b_ret = 0;
575 else {
576 if (buffer) {
577 buffer += i_ret;
578 size -= i_ret;
579 }
580 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
581 buffer, size);
582 if (b_ret < 0)
583 i_ret = 0;
584 }
585 up_read(&oi->ip_xattr_sem);
586 ocfs2_inode_unlock(dentry->d_inode, 0);
587
588 brelse(di_bh);
589
590 return i_ret + b_ret;
591}
592
593static int ocfs2_xattr_find_entry(int name_index,
594 const char *name,
595 struct ocfs2_xattr_search *xs)
596{
597 struct ocfs2_xattr_entry *entry;
598 size_t name_len;
599 int i, cmp = 1;
600
601 if (name == NULL)
602 return -EINVAL;
603
604 name_len = strlen(name);
605 entry = xs->here;
606 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
607 cmp = name_index - ocfs2_xattr_get_type(entry);
608 if (!cmp)
609 cmp = name_len - entry->xe_name_len;
610 if (!cmp)
611 cmp = memcmp(name, (xs->base +
612 le16_to_cpu(entry->xe_name_offset)),
613 name_len);
614 if (cmp == 0)
615 break;
616 entry += 1;
617 }
618 xs->here = entry;
619
620 return cmp ? -ENODATA : 0;
621}
622
623static int ocfs2_xattr_get_value_outside(struct inode *inode,
589dc260 624 struct ocfs2_xattr_value_root *xv,
cf1d6c76
TY
625 void *buffer,
626 size_t len)
627{
628 u32 cpos, p_cluster, num_clusters, bpc, clusters;
629 u64 blkno;
630 int i, ret = 0;
631 size_t cplen, blocksize;
632 struct buffer_head *bh = NULL;
cf1d6c76
TY
633 struct ocfs2_extent_list *el;
634
cf1d6c76
TY
635 el = &xv->xr_list;
636 clusters = le32_to_cpu(xv->xr_clusters);
637 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
638 blocksize = inode->i_sb->s_blocksize;
639
640 cpos = 0;
641 while (cpos < clusters) {
642 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
643 &num_clusters, el);
644 if (ret) {
645 mlog_errno(ret);
646 goto out;
647 }
648
649 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
650 /* Copy ocfs2_xattr_value */
651 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
652 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
653 &bh, OCFS2_BH_CACHED, inode);
654 if (ret) {
655 mlog_errno(ret);
656 goto out;
657 }
658
659 cplen = len >= blocksize ? blocksize : len;
660 memcpy(buffer, bh->b_data, cplen);
661 len -= cplen;
662 buffer += cplen;
663
664 brelse(bh);
665 bh = NULL;
666 if (len == 0)
667 break;
668 }
669 cpos += num_clusters;
670 }
671out:
672 return ret;
673}
674
675static int ocfs2_xattr_ibody_get(struct inode *inode,
676 int name_index,
677 const char *name,
678 void *buffer,
679 size_t buffer_size,
680 struct ocfs2_xattr_search *xs)
681{
682 struct ocfs2_inode_info *oi = OCFS2_I(inode);
683 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
589dc260 684 struct ocfs2_xattr_value_root *xv;
cf1d6c76
TY
685 size_t size;
686 int ret = 0;
687
688 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
689 return -ENODATA;
690
691 xs->end = (void *)di + inode->i_sb->s_blocksize;
692 xs->header = (struct ocfs2_xattr_header *)
693 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
694 xs->base = (void *)xs->header;
695 xs->here = xs->header->xh_entries;
696
697 ret = ocfs2_xattr_find_entry(name_index, name, xs);
698 if (ret)
699 return ret;
700 size = le64_to_cpu(xs->here->xe_value_size);
701 if (buffer) {
702 if (size > buffer_size)
703 return -ERANGE;
704 if (ocfs2_xattr_is_local(xs->here)) {
705 memcpy(buffer, (void *)xs->base +
706 le16_to_cpu(xs->here->xe_name_offset) +
707 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
708 } else {
589dc260
TM
709 xv = (struct ocfs2_xattr_value_root *)
710 (xs->base + le16_to_cpu(
711 xs->here->xe_name_offset) +
712 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
713 ret = ocfs2_xattr_get_value_outside(inode, xv,
cf1d6c76
TY
714 buffer, size);
715 if (ret < 0) {
716 mlog_errno(ret);
717 return ret;
718 }
719 }
720 }
721
722 return size;
723}
724
725static int ocfs2_xattr_block_get(struct inode *inode,
726 int name_index,
727 const char *name,
728 void *buffer,
729 size_t buffer_size,
730 struct ocfs2_xattr_search *xs)
731{
732 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
733 struct buffer_head *blk_bh = NULL;
734 struct ocfs2_xattr_block *xb;
589dc260 735 struct ocfs2_xattr_value_root *xv;
cf1d6c76 736 size_t size;
589dc260 737 int ret = -ENODATA, name_offset, name_len, block_off, i;
cf1d6c76
TY
738
739 if (!di->i_xattr_loc)
740 return ret;
741
589dc260
TM
742 memset(&xs->bucket, 0, sizeof(xs->bucket));
743
cf1d6c76
TY
744 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
745 le64_to_cpu(di->i_xattr_loc),
746 &blk_bh, OCFS2_BH_CACHED, inode);
747 if (ret < 0) {
748 mlog_errno(ret);
749 return ret;
750 }
751 /*Verify the signature of xattr block*/
752 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
753 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
754 ret = -EFAULT;
755 goto cleanup;
756 }
757
758 xs->xattr_bh = blk_bh;
759 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
cf1d6c76 760
589dc260
TM
761 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
762 xs->header = &xb->xb_attrs.xb_header;
763 xs->base = (void *)xs->header;
764 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
765 xs->here = xs->header->xh_entries;
766
767 ret = ocfs2_xattr_find_entry(name_index, name, xs);
768 } else
769 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
770 name_index,
771 name, xs);
772
cf1d6c76
TY
773 if (ret)
774 goto cleanup;
775 size = le64_to_cpu(xs->here->xe_value_size);
776 if (buffer) {
777 ret = -ERANGE;
778 if (size > buffer_size)
779 goto cleanup;
589dc260
TM
780
781 name_offset = le16_to_cpu(xs->here->xe_name_offset);
782 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
783 i = xs->here - xs->header->xh_entries;
784
785 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
786 ret = ocfs2_xattr_bucket_get_name_value(inode,
787 xs->bucket.xh,
788 i,
789 &block_off,
790 &name_offset);
791 xs->base = xs->bucket.bhs[block_off]->b_data;
792 }
cf1d6c76
TY
793 if (ocfs2_xattr_is_local(xs->here)) {
794 memcpy(buffer, (void *)xs->base +
589dc260 795 name_offset + name_len, size);
cf1d6c76 796 } else {
589dc260
TM
797 xv = (struct ocfs2_xattr_value_root *)
798 (xs->base + name_offset + name_len);
799 ret = ocfs2_xattr_get_value_outside(inode, xv,
cf1d6c76
TY
800 buffer, size);
801 if (ret < 0) {
802 mlog_errno(ret);
803 goto cleanup;
804 }
805 }
806 }
807 ret = size;
808cleanup:
589dc260
TM
809 for (i = 0; i < OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET; i++)
810 brelse(xs->bucket.bhs[i]);
811 memset(&xs->bucket, 0, sizeof(xs->bucket));
cf1d6c76 812
589dc260 813 brelse(blk_bh);
cf1d6c76
TY
814 return ret;
815}
816
817/* ocfs2_xattr_get()
818 *
819 * Copy an extended attribute into the buffer provided.
820 * Buffer is NULL to compute the size of buffer required.
821 */
822int ocfs2_xattr_get(struct inode *inode,
823 int name_index,
824 const char *name,
825 void *buffer,
826 size_t buffer_size)
827{
828 int ret;
829 struct ocfs2_dinode *di = NULL;
830 struct buffer_head *di_bh = NULL;
831 struct ocfs2_inode_info *oi = OCFS2_I(inode);
832 struct ocfs2_xattr_search xis = {
833 .not_found = -ENODATA,
834 };
835 struct ocfs2_xattr_search xbs = {
836 .not_found = -ENODATA,
837 };
838
8154da3d
TY
839 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
840 return -EOPNOTSUPP;
841
cf1d6c76
TY
842 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
843 ret = -ENODATA;
844
845 ret = ocfs2_inode_lock(inode, &di_bh, 0);
846 if (ret < 0) {
847 mlog_errno(ret);
848 return ret;
849 }
850 xis.inode_bh = xbs.inode_bh = di_bh;
851 di = (struct ocfs2_dinode *)di_bh->b_data;
852
853 down_read(&oi->ip_xattr_sem);
854 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
855 buffer_size, &xis);
856 if (ret == -ENODATA)
857 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
858 buffer_size, &xbs);
859 up_read(&oi->ip_xattr_sem);
860 ocfs2_inode_unlock(inode, 0);
861
862 brelse(di_bh);
863
864 return ret;
865}
866
867static int __ocfs2_xattr_set_value_outside(struct inode *inode,
868 struct ocfs2_xattr_value_root *xv,
869 const void *value,
870 int value_len)
871{
872 int ret = 0, i, cp_len, credits;
873 u16 blocksize = inode->i_sb->s_blocksize;
874 u32 p_cluster, num_clusters;
875 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
876 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
877 u64 blkno;
878 struct buffer_head *bh = NULL;
879 handle_t *handle;
880
881 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
882
883 credits = clusters * bpc;
884 handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb), credits);
885 if (IS_ERR(handle)) {
886 ret = PTR_ERR(handle);
887 mlog_errno(ret);
888 goto out;
889 }
890
891 while (cpos < clusters) {
892 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
893 &num_clusters, &xv->xr_list);
894 if (ret) {
895 mlog_errno(ret);
896 goto out_commit;
897 }
898
899 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
900
901 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
902 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
903 &bh, OCFS2_BH_CACHED, inode);
904 if (ret) {
905 mlog_errno(ret);
906 goto out_commit;
907 }
908
909 ret = ocfs2_journal_access(handle,
910 inode,
911 bh,
912 OCFS2_JOURNAL_ACCESS_WRITE);
913 if (ret < 0) {
914 mlog_errno(ret);
915 goto out_commit;
916 }
917
918 cp_len = value_len > blocksize ? blocksize : value_len;
919 memcpy(bh->b_data, value, cp_len);
920 value_len -= cp_len;
921 value += cp_len;
922 if (cp_len < blocksize)
923 memset(bh->b_data + cp_len, 0,
924 blocksize - cp_len);
925
926 ret = ocfs2_journal_dirty(handle, bh);
927 if (ret < 0) {
928 mlog_errno(ret);
929 goto out_commit;
930 }
931 brelse(bh);
932 bh = NULL;
933
934 /*
935 * XXX: do we need to empty all the following
936 * blocks in this cluster?
937 */
938 if (!value_len)
939 break;
940 }
941 cpos += num_clusters;
942 }
943out_commit:
944 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
945out:
946 brelse(bh);
947
948 return ret;
949}
950
951static int ocfs2_xattr_cleanup(struct inode *inode,
952 struct ocfs2_xattr_info *xi,
953 struct ocfs2_xattr_search *xs,
954 size_t offs)
955{
956 handle_t *handle = NULL;
957 int ret = 0;
958 size_t name_len = strlen(xi->name);
959 void *val = xs->base + offs;
960 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
961
962 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
963 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
964 if (IS_ERR(handle)) {
965 ret = PTR_ERR(handle);
966 mlog_errno(ret);
967 goto out;
968 }
969 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
970 OCFS2_JOURNAL_ACCESS_WRITE);
971 if (ret) {
972 mlog_errno(ret);
973 goto out_commit;
974 }
975 /* Decrease xattr count */
976 le16_add_cpu(&xs->header->xh_count, -1);
977 /* Remove the xattr entry and tree root which has already be set*/
978 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
979 memset(val, 0, size);
980
981 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
982 if (ret < 0)
983 mlog_errno(ret);
984out_commit:
985 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
986out:
987 return ret;
988}
989
990static int ocfs2_xattr_update_entry(struct inode *inode,
991 struct ocfs2_xattr_info *xi,
992 struct ocfs2_xattr_search *xs,
993 size_t offs)
994{
995 handle_t *handle = NULL;
996 int ret = 0;
997
998 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
999 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1000 if (IS_ERR(handle)) {
1001 ret = PTR_ERR(handle);
1002 mlog_errno(ret);
1003 goto out;
1004 }
1005 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1006 OCFS2_JOURNAL_ACCESS_WRITE);
1007 if (ret) {
1008 mlog_errno(ret);
1009 goto out_commit;
1010 }
1011
1012 xs->here->xe_name_offset = cpu_to_le16(offs);
1013 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1014 if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
1015 ocfs2_xattr_set_local(xs->here, 1);
1016 else
1017 ocfs2_xattr_set_local(xs->here, 0);
1018 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1019
1020 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1021 if (ret < 0)
1022 mlog_errno(ret);
1023out_commit:
1024 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1025out:
1026 return ret;
1027}
1028
1029/*
1030 * ocfs2_xattr_set_value_outside()
1031 *
1032 * Set large size value in B tree.
1033 */
1034static int ocfs2_xattr_set_value_outside(struct inode *inode,
1035 struct ocfs2_xattr_info *xi,
1036 struct ocfs2_xattr_search *xs,
1037 size_t offs)
1038{
1039 size_t name_len = strlen(xi->name);
1040 void *val = xs->base + offs;
1041 struct ocfs2_xattr_value_root *xv = NULL;
1042 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1043 int ret = 0;
1044
1045 memset(val, 0, size);
1046 memcpy(val, xi->name, name_len);
1047 xv = (struct ocfs2_xattr_value_root *)
1048 (val + OCFS2_XATTR_SIZE(name_len));
1049 xv->xr_clusters = 0;
1050 xv->xr_last_eb_blk = 0;
1051 xv->xr_list.l_tree_depth = 0;
1052 xv->xr_list.l_count = cpu_to_le16(1);
1053 xv->xr_list.l_next_free_rec = 0;
1054
1055 ret = ocfs2_xattr_value_truncate(inode, xs->xattr_bh, xv,
1056 xi->value_len);
1057 if (ret < 0) {
1058 mlog_errno(ret);
1059 return ret;
1060 }
1061 ret = __ocfs2_xattr_set_value_outside(inode, xv, xi->value,
1062 xi->value_len);
1063 if (ret < 0) {
1064 mlog_errno(ret);
1065 return ret;
1066 }
1067 ret = ocfs2_xattr_update_entry(inode, xi, xs, offs);
1068 if (ret < 0)
1069 mlog_errno(ret);
1070
1071 return ret;
1072}
1073
1074/*
1075 * ocfs2_xattr_set_entry_local()
1076 *
1077 * Set, replace or remove extended attribute in local.
1078 */
1079static void ocfs2_xattr_set_entry_local(struct inode *inode,
1080 struct ocfs2_xattr_info *xi,
1081 struct ocfs2_xattr_search *xs,
1082 struct ocfs2_xattr_entry *last,
1083 size_t min_offs)
1084{
1085 size_t name_len = strlen(xi->name);
1086 int i;
1087
1088 if (xi->value && xs->not_found) {
1089 /* Insert the new xattr entry. */
1090 le16_add_cpu(&xs->header->xh_count, 1);
1091 ocfs2_xattr_set_type(last, xi->name_index);
1092 ocfs2_xattr_set_local(last, 1);
1093 last->xe_name_len = name_len;
1094 } else {
1095 void *first_val;
1096 void *val;
1097 size_t offs, size;
1098
1099 first_val = xs->base + min_offs;
1100 offs = le16_to_cpu(xs->here->xe_name_offset);
1101 val = xs->base + offs;
1102
1103 if (le64_to_cpu(xs->here->xe_value_size) >
1104 OCFS2_XATTR_INLINE_SIZE)
1105 size = OCFS2_XATTR_SIZE(name_len) +
1106 OCFS2_XATTR_ROOT_SIZE;
1107 else
1108 size = OCFS2_XATTR_SIZE(name_len) +
1109 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1110
1111 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1112 OCFS2_XATTR_SIZE(xi->value_len)) {
1113 /* The old and the new value have the
1114 same size. Just replace the value. */
1115 ocfs2_xattr_set_local(xs->here, 1);
1116 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1117 /* Clear value bytes. */
1118 memset(val + OCFS2_XATTR_SIZE(name_len),
1119 0,
1120 OCFS2_XATTR_SIZE(xi->value_len));
1121 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1122 xi->value,
1123 xi->value_len);
1124 return;
1125 }
1126 /* Remove the old name+value. */
1127 memmove(first_val + size, first_val, val - first_val);
1128 memset(first_val, 0, size);
1129 xs->here->xe_name_hash = 0;
1130 xs->here->xe_name_offset = 0;
1131 ocfs2_xattr_set_local(xs->here, 1);
1132 xs->here->xe_value_size = 0;
1133
1134 min_offs += size;
1135
1136 /* Adjust all value offsets. */
1137 last = xs->header->xh_entries;
1138 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1139 size_t o = le16_to_cpu(last->xe_name_offset);
1140
1141 if (o < offs)
1142 last->xe_name_offset = cpu_to_le16(o + size);
1143 last += 1;
1144 }
1145
1146 if (!xi->value) {
1147 /* Remove the old entry. */
1148 last -= 1;
1149 memmove(xs->here, xs->here + 1,
1150 (void *)last - (void *)xs->here);
1151 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1152 le16_add_cpu(&xs->header->xh_count, -1);
1153 }
1154 }
1155 if (xi->value) {
1156 /* Insert the new name+value. */
1157 size_t size = OCFS2_XATTR_SIZE(name_len) +
1158 OCFS2_XATTR_SIZE(xi->value_len);
1159 void *val = xs->base + min_offs - size;
1160
1161 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1162 memset(val, 0, size);
1163 memcpy(val, xi->name, name_len);
1164 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1165 xi->value,
1166 xi->value_len);
1167 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1168 ocfs2_xattr_set_local(xs->here, 1);
1169 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1170 }
1171
1172 return;
1173}
1174
1175/*
1176 * ocfs2_xattr_set_entry()
1177 *
1178 * Set extended attribute entry into inode or block.
1179 *
1180 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1181 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1182 * then set value in B tree with set_value_outside().
1183 */
1184static int ocfs2_xattr_set_entry(struct inode *inode,
1185 struct ocfs2_xattr_info *xi,
1186 struct ocfs2_xattr_search *xs,
1187 int flag)
1188{
1189 struct ocfs2_xattr_entry *last;
1190 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1191 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1192 size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1193 size_t size_l = 0;
1194 handle_t *handle = NULL;
1195 int free, i, ret;
1196 struct ocfs2_xattr_info xi_l = {
1197 .name_index = xi->name_index,
1198 .name = xi->name,
1199 .value = xi->value,
1200 .value_len = xi->value_len,
1201 };
1202
1203 /* Compute min_offs, last and free space. */
1204 last = xs->header->xh_entries;
1205
1206 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1207 size_t offs = le16_to_cpu(last->xe_name_offset);
1208 if (offs < min_offs)
1209 min_offs = offs;
1210 last += 1;
1211 }
1212
1213 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
1214 if (free < 0)
1215 return -EFAULT;
1216
1217 if (!xs->not_found) {
1218 size_t size = 0;
1219 if (ocfs2_xattr_is_local(xs->here))
1220 size = OCFS2_XATTR_SIZE(name_len) +
1221 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1222 else
1223 size = OCFS2_XATTR_SIZE(name_len) +
1224 OCFS2_XATTR_ROOT_SIZE;
1225 free += (size + sizeof(struct ocfs2_xattr_entry));
1226 }
1227 /* Check free space in inode or block */
1228 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1229 if (free < sizeof(struct ocfs2_xattr_entry) +
1230 OCFS2_XATTR_SIZE(name_len) +
1231 OCFS2_XATTR_ROOT_SIZE) {
1232 ret = -ENOSPC;
1233 goto out;
1234 }
1235 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1236 xi_l.value = (void *)&def_xv;
1237 xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
1238 } else if (xi->value) {
1239 if (free < sizeof(struct ocfs2_xattr_entry) +
1240 OCFS2_XATTR_SIZE(name_len) +
1241 OCFS2_XATTR_SIZE(xi->value_len)) {
1242 ret = -ENOSPC;
1243 goto out;
1244 }
1245 }
1246
1247 if (!xs->not_found) {
1248 /* For existing extended attribute */
1249 size_t size = OCFS2_XATTR_SIZE(name_len) +
1250 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1251 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1252 void *val = xs->base + offs;
1253
1254 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1255 /* Replace existing local xattr with tree root */
1256 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
1257 offs);
1258 if (ret < 0)
1259 mlog_errno(ret);
1260 goto out;
1261 } else if (!ocfs2_xattr_is_local(xs->here)) {
1262 /* For existing xattr which has value outside */
1263 struct ocfs2_xattr_value_root *xv = NULL;
1264 xv = (struct ocfs2_xattr_value_root *)(val +
1265 OCFS2_XATTR_SIZE(name_len));
1266
1267 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1268 /*
1269 * If new value need set outside also,
1270 * first truncate old value to new value,
1271 * then set new value with set_value_outside().
1272 */
1273 ret = ocfs2_xattr_value_truncate(inode,
1274 xs->xattr_bh,
1275 xv,
1276 xi->value_len);
1277 if (ret < 0) {
1278 mlog_errno(ret);
1279 goto out;
1280 }
1281
1282 ret = __ocfs2_xattr_set_value_outside(inode,
1283 xv,
1284 xi->value,
1285 xi->value_len);
1286 if (ret < 0) {
1287 mlog_errno(ret);
1288 goto out;
1289 }
1290
1291 ret = ocfs2_xattr_update_entry(inode,
1292 xi,
1293 xs,
1294 offs);
1295 if (ret < 0)
1296 mlog_errno(ret);
1297 goto out;
1298 } else {
1299 /*
1300 * If new value need set in local,
1301 * just trucate old value to zero.
1302 */
1303 ret = ocfs2_xattr_value_truncate(inode,
1304 xs->xattr_bh,
1305 xv,
1306 0);
1307 if (ret < 0)
1308 mlog_errno(ret);
1309 }
1310 }
1311 }
1312
1313 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1314 OCFS2_INODE_UPDATE_CREDITS);
1315 if (IS_ERR(handle)) {
1316 ret = PTR_ERR(handle);
1317 mlog_errno(ret);
1318 goto out;
1319 }
1320
1321 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1322 OCFS2_JOURNAL_ACCESS_WRITE);
1323 if (ret) {
1324 mlog_errno(ret);
1325 goto out_commit;
1326 }
1327
1328 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
28b8ca0b 1329 /* set extended attribute in external block. */
cf1d6c76 1330 ret = ocfs2_extend_trans(handle,
28b8ca0b 1331 OCFS2_INODE_UPDATE_CREDITS +
cf1d6c76
TY
1332 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1333 if (ret) {
1334 mlog_errno(ret);
1335 goto out_commit;
1336 }
1337 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1338 OCFS2_JOURNAL_ACCESS_WRITE);
1339 if (ret) {
1340 mlog_errno(ret);
1341 goto out_commit;
1342 }
1343 }
1344
1345 /*
1346 * Set value in local, include set tree root in local.
1347 * This is the first step for value size >INLINE_SIZE.
1348 */
1349 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1350
1351 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1352 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1353 if (ret < 0) {
1354 mlog_errno(ret);
1355 goto out_commit;
1356 }
1357 }
1358
1359 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1360 (flag & OCFS2_INLINE_XATTR_FL)) {
1361 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1362 unsigned int xattrsize = osb->s_xattr_inline_size;
1363
1364 /*
1365 * Adjust extent record count or inline data size
1366 * to reserve space for extended attribute.
1367 */
1368 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1369 struct ocfs2_inline_data *idata = &di->id2.i_data;
1370 le16_add_cpu(&idata->id_count, -xattrsize);
1371 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1372 struct ocfs2_extent_list *el = &di->id2.i_list;
1373 le16_add_cpu(&el->l_count, -(xattrsize /
1374 sizeof(struct ocfs2_extent_rec)));
1375 }
1376 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1377 }
1378 /* Update xattr flag */
1379 spin_lock(&oi->ip_lock);
1380 oi->ip_dyn_features |= flag;
1381 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1382 spin_unlock(&oi->ip_lock);
1383 /* Update inode ctime */
1384 inode->i_ctime = CURRENT_TIME;
1385 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
1386 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
1387
1388 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1389 if (ret < 0)
1390 mlog_errno(ret);
1391
1392out_commit:
1393 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1394
1395 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1396 /*
1397 * Set value outside in B tree.
1398 * This is the second step for value size > INLINE_SIZE.
1399 */
1400 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1401 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, offs);
1402 if (ret < 0) {
1403 int ret2;
1404
1405 mlog_errno(ret);
1406 /*
1407 * If set value outside failed, we have to clean
1408 * the junk tree root we have already set in local.
1409 */
1410 ret2 = ocfs2_xattr_cleanup(inode, xi, xs, offs);
1411 if (ret2 < 0)
1412 mlog_errno(ret2);
1413 }
1414 }
1415out:
1416 return ret;
1417
1418}
1419
cf1d6c76
TY
1420static int ocfs2_remove_value_outside(struct inode*inode,
1421 struct buffer_head *bh,
1422 struct ocfs2_xattr_header *header)
1423{
1424 int ret = 0, i;
1425
1426 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1427 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1428
1429 if (!ocfs2_xattr_is_local(entry)) {
1430 struct ocfs2_xattr_value_root *xv;
1431 void *val;
1432
1433 val = (void *)header +
1434 le16_to_cpu(entry->xe_name_offset);
1435 xv = (struct ocfs2_xattr_value_root *)
1436 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
1437 ret = ocfs2_xattr_value_truncate(inode, bh, xv, 0);
1438 if (ret < 0) {
1439 mlog_errno(ret);
1440 return ret;
1441 }
1442 }
1443 }
1444
1445 return ret;
1446}
1447
1448static int ocfs2_xattr_ibody_remove(struct inode *inode,
1449 struct buffer_head *di_bh)
1450{
1451
1452 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1453 struct ocfs2_xattr_header *header;
1454 int ret;
1455
1456 header = (struct ocfs2_xattr_header *)
1457 ((void *)di + inode->i_sb->s_blocksize -
1458 le16_to_cpu(di->i_xattr_inline_size));
1459
1460 ret = ocfs2_remove_value_outside(inode, di_bh, header);
1461
1462 return ret;
1463}
1464
1465static int ocfs2_xattr_block_remove(struct inode *inode,
1466 struct buffer_head *blk_bh)
1467{
1468 struct ocfs2_xattr_block *xb;
cf1d6c76
TY
1469 int ret = 0;
1470
1471 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
a3944256
TM
1472 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1473 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
1474 ret = ocfs2_remove_value_outside(inode, blk_bh, header);
1475 } else
1476 ret = ocfs2_delete_xattr_index_block(inode, blk_bh);
cf1d6c76
TY
1477
1478 return ret;
1479}
1480
08413899
TM
1481static int ocfs2_xattr_free_block(struct inode *inode,
1482 u64 block)
1483{
1484 struct inode *xb_alloc_inode;
1485 struct buffer_head *xb_alloc_bh = NULL;
1486 struct buffer_head *blk_bh = NULL;
1487 struct ocfs2_xattr_block *xb;
1488 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1489 handle_t *handle;
1490 int ret = 0;
1491 u64 blk, bg_blkno;
1492 u16 bit;
1493
1494 ret = ocfs2_read_block(osb, block, &blk_bh,
1495 OCFS2_BH_CACHED, inode);
1496 if (ret < 0) {
1497 mlog_errno(ret);
1498 goto out;
1499 }
1500
1501 /*Verify the signature of xattr block*/
1502 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
1503 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
1504 ret = -EFAULT;
1505 goto out;
1506 }
1507
1508 ret = ocfs2_xattr_block_remove(inode, blk_bh);
1509 if (ret < 0) {
1510 mlog_errno(ret);
1511 goto out;
1512 }
1513
1514 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1515 blk = le64_to_cpu(xb->xb_blkno);
1516 bit = le16_to_cpu(xb->xb_suballoc_bit);
1517 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1518
1519 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1520 EXTENT_ALLOC_SYSTEM_INODE,
1521 le16_to_cpu(xb->xb_suballoc_slot));
1522 if (!xb_alloc_inode) {
1523 ret = -ENOMEM;
1524 mlog_errno(ret);
1525 goto out;
1526 }
1527 mutex_lock(&xb_alloc_inode->i_mutex);
1528
1529 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1530 if (ret < 0) {
1531 mlog_errno(ret);
1532 goto out_mutex;
1533 }
1534
1535 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
1536 if (IS_ERR(handle)) {
1537 ret = PTR_ERR(handle);
1538 mlog_errno(ret);
1539 goto out_unlock;
1540 }
1541
1542 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1543 bit, bg_blkno, 1);
1544 if (ret < 0)
1545 mlog_errno(ret);
1546
1547 ocfs2_commit_trans(osb, handle);
1548out_unlock:
1549 ocfs2_inode_unlock(xb_alloc_inode, 1);
1550 brelse(xb_alloc_bh);
1551out_mutex:
1552 mutex_unlock(&xb_alloc_inode->i_mutex);
1553 iput(xb_alloc_inode);
1554out:
1555 brelse(blk_bh);
1556 return ret;
1557}
1558
cf1d6c76
TY
1559/*
1560 * ocfs2_xattr_remove()
1561 *
1562 * Free extended attribute resources associated with this inode.
1563 */
1564int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
1565{
cf1d6c76
TY
1566 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1567 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1568 handle_t *handle;
1569 int ret;
1570
8154da3d
TY
1571 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1572 return 0;
1573
cf1d6c76
TY
1574 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1575 return 0;
1576
1577 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1578 ret = ocfs2_xattr_ibody_remove(inode, di_bh);
1579 if (ret < 0) {
1580 mlog_errno(ret);
1581 goto out;
1582 }
1583 }
cf1d6c76 1584
08413899
TM
1585 if (di->i_xattr_loc) {
1586 ret = ocfs2_xattr_free_block(inode,
1587 le64_to_cpu(di->i_xattr_loc));
cf1d6c76
TY
1588 if (ret < 0) {
1589 mlog_errno(ret);
1590 goto out;
1591 }
1592 }
1593
1594 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1595 OCFS2_INODE_UPDATE_CREDITS);
1596 if (IS_ERR(handle)) {
1597 ret = PTR_ERR(handle);
1598 mlog_errno(ret);
1599 goto out;
1600 }
1601 ret = ocfs2_journal_access(handle, inode, di_bh,
1602 OCFS2_JOURNAL_ACCESS_WRITE);
1603 if (ret) {
1604 mlog_errno(ret);
1605 goto out_commit;
1606 }
1607
08413899 1608 di->i_xattr_loc = 0;
cf1d6c76
TY
1609
1610 spin_lock(&oi->ip_lock);
1611 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
1612 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1613 spin_unlock(&oi->ip_lock);
1614
1615 ret = ocfs2_journal_dirty(handle, di_bh);
1616 if (ret < 0)
1617 mlog_errno(ret);
1618out_commit:
1619 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1620out:
cf1d6c76
TY
1621 return ret;
1622}
1623
1624static int ocfs2_xattr_has_space_inline(struct inode *inode,
1625 struct ocfs2_dinode *di)
1626{
1627 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1628 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
1629 int free;
1630
1631 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
1632 return 0;
1633
1634 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1635 struct ocfs2_inline_data *idata = &di->id2.i_data;
1636 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
1637 } else if (ocfs2_inode_is_fast_symlink(inode)) {
1638 free = ocfs2_fast_symlink_chars(inode->i_sb) -
1639 le64_to_cpu(di->i_size);
1640 } else {
1641 struct ocfs2_extent_list *el = &di->id2.i_list;
1642 free = (le16_to_cpu(el->l_count) -
1643 le16_to_cpu(el->l_next_free_rec)) *
1644 sizeof(struct ocfs2_extent_rec);
1645 }
1646 if (free >= xattrsize)
1647 return 1;
1648
1649 return 0;
1650}
1651
1652/*
1653 * ocfs2_xattr_ibody_find()
1654 *
1655 * Find extended attribute in inode block and
1656 * fill search info into struct ocfs2_xattr_search.
1657 */
1658static int ocfs2_xattr_ibody_find(struct inode *inode,
1659 int name_index,
1660 const char *name,
1661 struct ocfs2_xattr_search *xs)
1662{
1663 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1664 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1665 int ret;
1666 int has_space = 0;
1667
1668 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1669 return 0;
1670
1671 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1672 down_read(&oi->ip_alloc_sem);
1673 has_space = ocfs2_xattr_has_space_inline(inode, di);
1674 up_read(&oi->ip_alloc_sem);
1675 if (!has_space)
1676 return 0;
1677 }
1678
1679 xs->xattr_bh = xs->inode_bh;
1680 xs->end = (void *)di + inode->i_sb->s_blocksize;
1681 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
1682 xs->header = (struct ocfs2_xattr_header *)
1683 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1684 else
1685 xs->header = (struct ocfs2_xattr_header *)
1686 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
1687 xs->base = (void *)xs->header;
1688 xs->here = xs->header->xh_entries;
1689
1690 /* Find the named attribute. */
1691 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1692 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1693 if (ret && ret != -ENODATA)
1694 return ret;
1695 xs->not_found = ret;
1696 }
1697
1698 return 0;
1699}
1700
1701/*
1702 * ocfs2_xattr_ibody_set()
1703 *
1704 * Set, replace or remove an extended attribute into inode block.
1705 *
1706 */
1707static int ocfs2_xattr_ibody_set(struct inode *inode,
1708 struct ocfs2_xattr_info *xi,
1709 struct ocfs2_xattr_search *xs)
1710{
1711 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1712 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1713 int ret;
1714
1715 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1716 return -ENOSPC;
1717
1718 down_write(&oi->ip_alloc_sem);
1719 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1720 if (!ocfs2_xattr_has_space_inline(inode, di)) {
1721 ret = -ENOSPC;
1722 goto out;
1723 }
1724 }
1725
1726 ret = ocfs2_xattr_set_entry(inode, xi, xs,
1727 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
1728out:
1729 up_write(&oi->ip_alloc_sem);
1730
1731 return ret;
1732}
1733
1734/*
1735 * ocfs2_xattr_block_find()
1736 *
1737 * Find extended attribute in external block and
1738 * fill search info into struct ocfs2_xattr_search.
1739 */
1740static int ocfs2_xattr_block_find(struct inode *inode,
1741 int name_index,
1742 const char *name,
1743 struct ocfs2_xattr_search *xs)
1744{
1745 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1746 struct buffer_head *blk_bh = NULL;
589dc260 1747 struct ocfs2_xattr_block *xb;
cf1d6c76
TY
1748 int ret = 0;
1749
1750 if (!di->i_xattr_loc)
1751 return ret;
1752
1753 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1754 le64_to_cpu(di->i_xattr_loc),
1755 &blk_bh, OCFS2_BH_CACHED, inode);
1756 if (ret < 0) {
1757 mlog_errno(ret);
1758 return ret;
1759 }
1760 /*Verify the signature of xattr block*/
1761 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
1762 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
1763 ret = -EFAULT;
1764 goto cleanup;
1765 }
1766
1767 xs->xattr_bh = blk_bh;
589dc260
TM
1768 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1769
1770 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1771 xs->header = &xb->xb_attrs.xb_header;
1772 xs->base = (void *)xs->header;
1773 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
1774 xs->here = xs->header->xh_entries;
1775
1776 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1777 } else
1778 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
1779 name_index,
1780 name, xs);
cf1d6c76 1781
cf1d6c76
TY
1782 if (ret && ret != -ENODATA) {
1783 xs->xattr_bh = NULL;
1784 goto cleanup;
1785 }
1786 xs->not_found = ret;
1787 return 0;
cf1d6c76
TY
1788cleanup:
1789 brelse(blk_bh);
1790
1791 return ret;
1792}
1793
01225596
TM
1794/*
1795 * When all the xattrs are deleted from index btree, the ocfs2_xattr_tree
1796 * will be erased and ocfs2_xattr_block will have its ocfs2_xattr_header
1797 * re-initialized.
1798 */
1799static int ocfs2_restore_xattr_block(struct inode *inode,
1800 struct ocfs2_xattr_search *xs)
1801{
1802 int ret;
1803 handle_t *handle;
1804 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1805 struct ocfs2_xattr_block *xb =
1806 (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1807 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
1808 u16 xb_flags = le16_to_cpu(xb->xb_flags);
1809
1810 BUG_ON(!(xb_flags & OCFS2_XATTR_INDEXED) ||
1811 le16_to_cpu(el->l_next_free_rec) != 0);
1812
1813 handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1814 if (IS_ERR(handle)) {
1815 ret = PTR_ERR(handle);
1816 handle = NULL;
1817 goto out;
1818 }
1819
1820 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1821 OCFS2_JOURNAL_ACCESS_WRITE);
1822 if (ret < 0) {
1823 mlog_errno(ret);
1824 goto out_commit;
1825 }
1826
1827 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
1828 offsetof(struct ocfs2_xattr_block, xb_attrs));
1829
1830 xb->xb_flags = cpu_to_le16(xb_flags & ~OCFS2_XATTR_INDEXED);
1831
1832 ocfs2_journal_dirty(handle, xs->xattr_bh);
1833
1834out_commit:
1835 ocfs2_commit_trans(osb, handle);
1836out:
1837 return ret;
1838}
1839
cf1d6c76
TY
1840/*
1841 * ocfs2_xattr_block_set()
1842 *
1843 * Set, replace or remove an extended attribute into external block.
1844 *
1845 */
1846static int ocfs2_xattr_block_set(struct inode *inode,
1847 struct ocfs2_xattr_info *xi,
1848 struct ocfs2_xattr_search *xs)
1849{
1850 struct buffer_head *new_bh = NULL;
1851 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1852 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1853 struct ocfs2_alloc_context *meta_ac = NULL;
1854 handle_t *handle = NULL;
1855 struct ocfs2_xattr_block *xblk = NULL;
1856 u16 suballoc_bit_start;
1857 u32 num_got;
1858 u64 first_blkno;
1859 int ret;
1860
1861 if (!xs->xattr_bh) {
1862 /*
1863 * Alloc one external block for extended attribute
1864 * outside of inode.
1865 */
1866 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
1867 if (ret < 0) {
1868 mlog_errno(ret);
1869 goto out;
1870 }
1871 handle = ocfs2_start_trans(osb,
1872 OCFS2_XATTR_BLOCK_CREATE_CREDITS);
1873 if (IS_ERR(handle)) {
1874 ret = PTR_ERR(handle);
1875 mlog_errno(ret);
1876 goto out;
1877 }
1878 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1879 OCFS2_JOURNAL_ACCESS_CREATE);
1880 if (ret < 0) {
1881 mlog_errno(ret);
1882 goto out_commit;
1883 }
1884
1885 ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
1886 &suballoc_bit_start, &num_got,
1887 &first_blkno);
1888 if (ret < 0) {
1889 mlog_errno(ret);
1890 goto out_commit;
1891 }
1892
1893 new_bh = sb_getblk(inode->i_sb, first_blkno);
1894 ocfs2_set_new_buffer_uptodate(inode, new_bh);
1895
1896 ret = ocfs2_journal_access(handle, inode, new_bh,
1897 OCFS2_JOURNAL_ACCESS_CREATE);
1898 if (ret < 0) {
1899 mlog_errno(ret);
1900 goto out_commit;
1901 }
1902
1903 /* Initialize ocfs2_xattr_block */
1904 xs->xattr_bh = new_bh;
1905 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
1906 memset(xblk, 0, inode->i_sb->s_blocksize);
1907 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
1908 xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
1909 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1910 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
1911 xblk->xb_blkno = cpu_to_le64(first_blkno);
1912
1913 xs->header = &xblk->xb_attrs.xb_header;
1914 xs->base = (void *)xs->header;
1915 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
1916 xs->here = xs->header->xh_entries;
1917
1918
1919 ret = ocfs2_journal_dirty(handle, new_bh);
1920 if (ret < 0) {
1921 mlog_errno(ret);
1922 goto out_commit;
1923 }
1924 di->i_xattr_loc = cpu_to_le64(first_blkno);
1925 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1926 if (ret < 0)
1927 mlog_errno(ret);
1928out_commit:
1929 ocfs2_commit_trans(osb, handle);
1930out:
1931 if (meta_ac)
1932 ocfs2_free_alloc_context(meta_ac);
1933 if (ret < 0)
1934 return ret;
01225596
TM
1935 } else
1936 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1937
1938 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
1939 /* Set extended attribute into external block */
1940 ret = ocfs2_xattr_set_entry(inode, xi, xs, OCFS2_HAS_XATTR_FL);
1941 if (!ret || ret != -ENOSPC)
1942 goto end;
1943
1944 ret = ocfs2_xattr_create_index_block(inode, xs);
1945 if (ret)
1946 goto end;
cf1d6c76
TY
1947 }
1948
01225596
TM
1949 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs);
1950 if (!ret && xblk->xb_attrs.xb_root.xt_list.l_next_free_rec == 0)
1951 ret = ocfs2_restore_xattr_block(inode, xs);
1952
1953end:
cf1d6c76
TY
1954
1955 return ret;
1956}
1957
1958/*
1959 * ocfs2_xattr_set()
1960 *
1961 * Set, replace or remove an extended attribute for this inode.
1962 * value is NULL to remove an existing extended attribute, else either
1963 * create or replace an extended attribute.
1964 */
1965int ocfs2_xattr_set(struct inode *inode,
1966 int name_index,
1967 const char *name,
1968 const void *value,
1969 size_t value_len,
1970 int flags)
1971{
1972 struct buffer_head *di_bh = NULL;
1973 struct ocfs2_dinode *di;
1974 int ret;
01225596 1975 u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
cf1d6c76
TY
1976
1977 struct ocfs2_xattr_info xi = {
1978 .name_index = name_index,
1979 .name = name,
1980 .value = value,
1981 .value_len = value_len,
1982 };
1983
1984 struct ocfs2_xattr_search xis = {
1985 .not_found = -ENODATA,
1986 };
1987
1988 struct ocfs2_xattr_search xbs = {
1989 .not_found = -ENODATA,
1990 };
1991
8154da3d
TY
1992 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1993 return -EOPNOTSUPP;
1994
cf1d6c76
TY
1995 ret = ocfs2_inode_lock(inode, &di_bh, 1);
1996 if (ret < 0) {
1997 mlog_errno(ret);
1998 return ret;
1999 }
2000 xis.inode_bh = xbs.inode_bh = di_bh;
2001 di = (struct ocfs2_dinode *)di_bh->b_data;
2002
2003 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2004 /*
2005 * Scan inode and external block to find the same name
2006 * extended attribute and collect search infomation.
2007 */
2008 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2009 if (ret)
2010 goto cleanup;
2011 if (xis.not_found) {
2012 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2013 if (ret)
2014 goto cleanup;
2015 }
2016
2017 if (xis.not_found && xbs.not_found) {
2018 ret = -ENODATA;
2019 if (flags & XATTR_REPLACE)
2020 goto cleanup;
2021 ret = 0;
2022 if (!value)
2023 goto cleanup;
2024 } else {
2025 ret = -EEXIST;
2026 if (flags & XATTR_CREATE)
2027 goto cleanup;
2028 }
2029
2030 if (!value) {
2031 /* Remove existing extended attribute */
2032 if (!xis.not_found)
2033 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2034 else if (!xbs.not_found)
2035 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2036 } else {
2037 /* We always try to set extended attribute into inode first*/
2038 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2039 if (!ret && !xbs.not_found) {
2040 /*
2041 * If succeed and that extended attribute existing in
2042 * external block, then we will remove it.
2043 */
2044 xi.value = NULL;
2045 xi.value_len = 0;
2046 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2047 } else if (ret == -ENOSPC) {
2048 if (di->i_xattr_loc && !xbs.xattr_bh) {
2049 ret = ocfs2_xattr_block_find(inode, name_index,
2050 name, &xbs);
2051 if (ret)
2052 goto cleanup;
2053 }
2054 /*
2055 * If no space in inode, we will set extended attribute
2056 * into external block.
2057 */
2058 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2059 if (ret)
2060 goto cleanup;
2061 if (!xis.not_found) {
2062 /*
2063 * If succeed and that extended attribute
2064 * existing in inode, we will remove it.
2065 */
2066 xi.value = NULL;
2067 xi.value_len = 0;
2068 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2069 }
2070 }
2071 }
2072cleanup:
2073 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2074 ocfs2_inode_unlock(inode, 1);
2075 brelse(di_bh);
2076 brelse(xbs.xattr_bh);
01225596
TM
2077 for (i = 0; i < blk_per_bucket; i++)
2078 brelse(xbs.bucket.bhs[i]);
cf1d6c76
TY
2079
2080 return ret;
2081}
2082
0c044f0b
TM
2083/*
2084 * Find the xattr extent rec which may contains name_hash.
2085 * e_cpos will be the first name hash of the xattr rec.
2086 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
2087 */
2088static int ocfs2_xattr_get_rec(struct inode *inode,
2089 u32 name_hash,
2090 u64 *p_blkno,
2091 u32 *e_cpos,
2092 u32 *num_clusters,
2093 struct ocfs2_extent_list *el)
2094{
2095 int ret = 0, i;
2096 struct buffer_head *eb_bh = NULL;
2097 struct ocfs2_extent_block *eb;
2098 struct ocfs2_extent_rec *rec = NULL;
2099 u64 e_blkno = 0;
2100
2101 if (el->l_tree_depth) {
2102 ret = ocfs2_find_leaf(inode, el, name_hash, &eb_bh);
2103 if (ret) {
2104 mlog_errno(ret);
2105 goto out;
2106 }
2107
2108 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
2109 el = &eb->h_list;
2110
2111 if (el->l_tree_depth) {
2112 ocfs2_error(inode->i_sb,
2113 "Inode %lu has non zero tree depth in "
2114 "xattr tree block %llu\n", inode->i_ino,
2115 (unsigned long long)eb_bh->b_blocknr);
2116 ret = -EROFS;
2117 goto out;
2118 }
2119 }
2120
2121 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
2122 rec = &el->l_recs[i];
2123
2124 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
2125 e_blkno = le64_to_cpu(rec->e_blkno);
2126 break;
2127 }
2128 }
2129
2130 if (!e_blkno) {
2131 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
2132 "record (%u, %u, 0) in xattr", inode->i_ino,
2133 le32_to_cpu(rec->e_cpos),
2134 ocfs2_rec_clusters(el, rec));
2135 ret = -EROFS;
2136 goto out;
2137 }
2138
2139 *p_blkno = le64_to_cpu(rec->e_blkno);
2140 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
2141 if (e_cpos)
2142 *e_cpos = le32_to_cpu(rec->e_cpos);
2143out:
2144 brelse(eb_bh);
2145 return ret;
2146}
2147
2148typedef int (xattr_bucket_func)(struct inode *inode,
2149 struct ocfs2_xattr_bucket *bucket,
2150 void *para);
2151
589dc260
TM
2152static int ocfs2_find_xe_in_bucket(struct inode *inode,
2153 struct buffer_head *header_bh,
2154 int name_index,
2155 const char *name,
2156 u32 name_hash,
2157 u16 *xe_index,
2158 int *found)
2159{
2160 int i, ret = 0, cmp = 1, block_off, new_offset;
2161 struct ocfs2_xattr_header *xh =
2162 (struct ocfs2_xattr_header *)header_bh->b_data;
2163 size_t name_len = strlen(name);
2164 struct ocfs2_xattr_entry *xe = NULL;
2165 struct buffer_head *name_bh = NULL;
2166 char *xe_name;
2167
2168 /*
2169 * We don't use binary search in the bucket because there
2170 * may be multiple entries with the same name hash.
2171 */
2172 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
2173 xe = &xh->xh_entries[i];
2174
2175 if (name_hash > le32_to_cpu(xe->xe_name_hash))
2176 continue;
2177 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
2178 break;
2179
2180 cmp = name_index - ocfs2_xattr_get_type(xe);
2181 if (!cmp)
2182 cmp = name_len - xe->xe_name_len;
2183 if (cmp)
2184 continue;
2185
2186 ret = ocfs2_xattr_bucket_get_name_value(inode,
2187 xh,
2188 i,
2189 &block_off,
2190 &new_offset);
2191 if (ret) {
2192 mlog_errno(ret);
2193 break;
2194 }
2195
2196 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
2197 header_bh->b_blocknr + block_off,
2198 &name_bh, OCFS2_BH_CACHED, inode);
2199 if (ret) {
2200 mlog_errno(ret);
2201 break;
2202 }
2203 xe_name = name_bh->b_data + new_offset;
2204
2205 cmp = memcmp(name, xe_name, name_len);
2206 brelse(name_bh);
2207 name_bh = NULL;
2208
2209 if (cmp == 0) {
2210 *xe_index = i;
2211 *found = 1;
2212 ret = 0;
2213 break;
2214 }
2215 }
2216
2217 return ret;
2218}
2219
2220/*
2221 * Find the specified xattr entry in a series of buckets.
2222 * This series start from p_blkno and last for num_clusters.
2223 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
2224 * the num of the valid buckets.
2225 *
2226 * Return the buffer_head this xattr should reside in. And if the xattr's
2227 * hash is in the gap of 2 buckets, return the lower bucket.
2228 */
2229static int ocfs2_xattr_bucket_find(struct inode *inode,
2230 int name_index,
2231 const char *name,
2232 u32 name_hash,
2233 u64 p_blkno,
2234 u32 first_hash,
2235 u32 num_clusters,
2236 struct ocfs2_xattr_search *xs)
2237{
2238 int ret, found = 0;
2239 struct buffer_head *bh = NULL;
2240 struct buffer_head *lower_bh = NULL;
2241 struct ocfs2_xattr_header *xh = NULL;
2242 struct ocfs2_xattr_entry *xe = NULL;
2243 u16 index = 0;
2244 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2245 int low_bucket = 0, bucket, high_bucket;
2246 u32 last_hash;
2247 u64 blkno;
2248
2249 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), p_blkno,
2250 &bh, OCFS2_BH_CACHED, inode);
2251 if (ret) {
2252 mlog_errno(ret);
2253 goto out;
2254 }
2255
2256 xh = (struct ocfs2_xattr_header *)bh->b_data;
2257 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
2258
2259 while (low_bucket <= high_bucket) {
2260 brelse(bh);
2261 bh = NULL;
2262 bucket = (low_bucket + high_bucket) / 2;
2263
2264 blkno = p_blkno + bucket * blk_per_bucket;
2265
2266 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
2267 &bh, OCFS2_BH_CACHED, inode);
2268 if (ret) {
2269 mlog_errno(ret);
2270 goto out;
2271 }
2272
2273 xh = (struct ocfs2_xattr_header *)bh->b_data;
2274 xe = &xh->xh_entries[0];
2275 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
2276 high_bucket = bucket - 1;
2277 continue;
2278 }
2279
2280 /*
2281 * Check whether the hash of the last entry in our
5a095611
TM
2282 * bucket is larger than the search one. for an empty
2283 * bucket, the last one is also the first one.
589dc260 2284 */
5a095611
TM
2285 if (xh->xh_count)
2286 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
2287
589dc260
TM
2288 last_hash = le32_to_cpu(xe->xe_name_hash);
2289
2290 /* record lower_bh which may be the insert place. */
2291 brelse(lower_bh);
2292 lower_bh = bh;
2293 bh = NULL;
2294
2295 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
2296 low_bucket = bucket + 1;
2297 continue;
2298 }
2299
2300 /* the searched xattr should reside in this bucket if exists. */
2301 ret = ocfs2_find_xe_in_bucket(inode, lower_bh,
2302 name_index, name, name_hash,
2303 &index, &found);
2304 if (ret) {
2305 mlog_errno(ret);
2306 goto out;
2307 }
2308 break;
2309 }
2310
2311 /*
2312 * Record the bucket we have found.
2313 * When the xattr's hash value is in the gap of 2 buckets, we will
2314 * always set it to the previous bucket.
2315 */
2316 if (!lower_bh) {
2317 /*
2318 * We can't find any bucket whose first name_hash is less
2319 * than the find name_hash.
2320 */
2321 BUG_ON(bh->b_blocknr != p_blkno);
2322 lower_bh = bh;
2323 bh = NULL;
2324 }
2325 xs->bucket.bhs[0] = lower_bh;
2326 xs->bucket.xh = (struct ocfs2_xattr_header *)
2327 xs->bucket.bhs[0]->b_data;
2328 lower_bh = NULL;
2329
2330 xs->header = xs->bucket.xh;
2331 xs->base = xs->bucket.bhs[0]->b_data;
2332 xs->end = xs->base + inode->i_sb->s_blocksize;
2333
2334 if (found) {
2335 /*
2336 * If we have found the xattr enty, read all the blocks in
2337 * this bucket.
2338 */
2339 ret = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
2340 xs->bucket.bhs[0]->b_blocknr + 1,
2341 blk_per_bucket - 1, &xs->bucket.bhs[1],
2342 OCFS2_BH_CACHED, inode);
2343 if (ret) {
2344 mlog_errno(ret);
2345 goto out;
2346 }
2347
2348 xs->here = &xs->header->xh_entries[index];
2349 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
2350 (unsigned long long)xs->bucket.bhs[0]->b_blocknr, index);
2351 } else
2352 ret = -ENODATA;
2353
2354out:
2355 brelse(bh);
2356 brelse(lower_bh);
2357 return ret;
2358}
2359
2360static int ocfs2_xattr_index_block_find(struct inode *inode,
2361 struct buffer_head *root_bh,
2362 int name_index,
2363 const char *name,
2364 struct ocfs2_xattr_search *xs)
2365{
2366 int ret;
2367 struct ocfs2_xattr_block *xb =
2368 (struct ocfs2_xattr_block *)root_bh->b_data;
2369 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
2370 struct ocfs2_extent_list *el = &xb_root->xt_list;
2371 u64 p_blkno = 0;
2372 u32 first_hash, num_clusters = 0;
2057e5c6 2373 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
589dc260
TM
2374
2375 if (le16_to_cpu(el->l_next_free_rec) == 0)
2376 return -ENODATA;
2377
2378 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
2379 name, name_hash, name_index);
2380
2381 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
2382 &num_clusters, el);
2383 if (ret) {
2384 mlog_errno(ret);
2385 goto out;
2386 }
2387
2388 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
2389
2390 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
2391 "in the rec is %u\n", num_clusters, p_blkno, first_hash);
2392
2393 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
2394 p_blkno, first_hash, num_clusters, xs);
2395
2396out:
2397 return ret;
2398}
2399
0c044f0b
TM
2400static int ocfs2_iterate_xattr_buckets(struct inode *inode,
2401 u64 blkno,
2402 u32 clusters,
2403 xattr_bucket_func *func,
2404 void *para)
2405{
2406 int i, j, ret = 0;
2407 int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2408 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
2409 u32 num_buckets = clusters * bpc;
2410 struct ocfs2_xattr_bucket bucket;
2411
2412 memset(&bucket, 0, sizeof(bucket));
2413
2414 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
2415 clusters, blkno);
2416
2417 for (i = 0; i < num_buckets; i++, blkno += blk_per_bucket) {
2418 ret = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
2419 blkno, blk_per_bucket,
2420 bucket.bhs, OCFS2_BH_CACHED, inode);
2421 if (ret) {
2422 mlog_errno(ret);
2423 goto out;
2424 }
2425
2426 bucket.xh = (struct ocfs2_xattr_header *)bucket.bhs[0]->b_data;
2427 /*
2428 * The real bucket num in this series of blocks is stored
2429 * in the 1st bucket.
2430 */
2431 if (i == 0)
2432 num_buckets = le16_to_cpu(bucket.xh->xh_num_buckets);
2433
5a095611
TM
2434 mlog(0, "iterating xattr bucket %llu, first hash %u\n", blkno,
2435 le32_to_cpu(bucket.xh->xh_entries[0].xe_name_hash));
0c044f0b
TM
2436 if (func) {
2437 ret = func(inode, &bucket, para);
2438 if (ret) {
2439 mlog_errno(ret);
2440 break;
2441 }
2442 }
2443
2444 for (j = 0; j < blk_per_bucket; j++)
2445 brelse(bucket.bhs[j]);
2446 memset(&bucket, 0, sizeof(bucket));
2447 }
2448
2449out:
2450 for (j = 0; j < blk_per_bucket; j++)
2451 brelse(bucket.bhs[j]);
2452
2453 return ret;
2454}
2455
2456struct ocfs2_xattr_tree_list {
2457 char *buffer;
2458 size_t buffer_size;
2459};
2460
2461static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
2462 struct ocfs2_xattr_header *xh,
2463 int index,
2464 int *block_off,
2465 int *new_offset)
2466{
2467 u16 name_offset;
2468
2469 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
2470 return -EINVAL;
2471
2472 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
2473
2474 *block_off = name_offset >> inode->i_sb->s_blocksize_bits;
2475 *new_offset = name_offset % inode->i_sb->s_blocksize;
2476
2477 return 0;
2478}
2479
2480static int ocfs2_list_xattr_bucket(struct inode *inode,
2481 struct ocfs2_xattr_bucket *bucket,
2482 void *para)
2483{
2484 int ret = 0;
2485 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
2486 size_t size;
2487 int i, block_off, new_offset;
2488
2489 for (i = 0 ; i < le16_to_cpu(bucket->xh->xh_count); i++) {
2490 struct ocfs2_xattr_entry *entry = &bucket->xh->xh_entries[i];
2491 struct xattr_handler *handler =
2492 ocfs2_xattr_handler(ocfs2_xattr_get_type(entry));
2493
2494 if (handler) {
2495 ret = ocfs2_xattr_bucket_get_name_value(inode,
2496 bucket->xh,
2497 i,
2498 &block_off,
2499 &new_offset);
2500 if (ret)
2501 break;
2502 size = handler->list(inode, xl->buffer, xl->buffer_size,
2503 bucket->bhs[block_off]->b_data +
2504 new_offset,
2505 entry->xe_name_len);
2506 if (xl->buffer) {
2507 if (size > xl->buffer_size)
2508 return -ERANGE;
2509 xl->buffer += size;
2510 }
2511 xl->buffer_size -= size;
2512 }
2513 }
2514
2515 return ret;
2516}
2517
2518static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
2519 struct ocfs2_xattr_tree_root *xt,
2520 char *buffer,
2521 size_t buffer_size)
2522{
2523 struct ocfs2_extent_list *el = &xt->xt_list;
2524 int ret = 0;
2525 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
2526 u64 p_blkno = 0;
2527 struct ocfs2_xattr_tree_list xl = {
2528 .buffer = buffer,
2529 .buffer_size = buffer_size,
2530 };
2531
2532 if (le16_to_cpu(el->l_next_free_rec) == 0)
2533 return 0;
2534
2535 while (name_hash > 0) {
2536 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
2537 &e_cpos, &num_clusters, el);
2538 if (ret) {
2539 mlog_errno(ret);
2540 goto out;
2541 }
2542
2543 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
2544 ocfs2_list_xattr_bucket,
2545 &xl);
2546 if (ret) {
2547 mlog_errno(ret);
2548 goto out;
2549 }
2550
2551 if (e_cpos == 0)
2552 break;
2553
2554 name_hash = e_cpos - 1;
2555 }
2556
2557 ret = buffer_size - xl.buffer_size;
2558out:
2559 return ret;
2560}
01225596
TM
2561
2562static int cmp_xe(const void *a, const void *b)
2563{
2564 const struct ocfs2_xattr_entry *l = a, *r = b;
2565 u32 l_hash = le32_to_cpu(l->xe_name_hash);
2566 u32 r_hash = le32_to_cpu(r->xe_name_hash);
2567
2568 if (l_hash > r_hash)
2569 return 1;
2570 if (l_hash < r_hash)
2571 return -1;
2572 return 0;
2573}
2574
2575static void swap_xe(void *a, void *b, int size)
2576{
2577 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
2578
2579 tmp = *l;
2580 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
2581 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
2582}
2583
2584/*
2585 * When the ocfs2_xattr_block is filled up, new bucket will be created
2586 * and all the xattr entries will be moved to the new bucket.
2587 * Note: we need to sort the entries since they are not saved in order
2588 * in the ocfs2_xattr_block.
2589 */
2590static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
2591 struct buffer_head *xb_bh,
2592 struct buffer_head *xh_bh,
2593 struct buffer_head *data_bh)
2594{
2595 int i, blocksize = inode->i_sb->s_blocksize;
2596 u16 offset, size, off_change;
2597 struct ocfs2_xattr_entry *xe;
2598 struct ocfs2_xattr_block *xb =
2599 (struct ocfs2_xattr_block *)xb_bh->b_data;
2600 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
2601 struct ocfs2_xattr_header *xh =
2602 (struct ocfs2_xattr_header *)xh_bh->b_data;
2603 u16 count = le16_to_cpu(xb_xh->xh_count);
2604 char *target = xh_bh->b_data, *src = xb_bh->b_data;
2605
2606 mlog(0, "cp xattr from block %llu to bucket %llu\n",
2607 (unsigned long long)xb_bh->b_blocknr,
2608 (unsigned long long)xh_bh->b_blocknr);
2609
2610 memset(xh_bh->b_data, 0, blocksize);
2611 if (data_bh)
2612 memset(data_bh->b_data, 0, blocksize);
2613 /*
2614 * Since the xe_name_offset is based on ocfs2_xattr_header,
2615 * there is a offset change corresponding to the change of
2616 * ocfs2_xattr_header's position.
2617 */
2618 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
2619 xe = &xb_xh->xh_entries[count - 1];
2620 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
2621 size = blocksize - offset;
2622
2623 /* copy all the names and values. */
2624 if (data_bh)
2625 target = data_bh->b_data;
2626 memcpy(target + offset, src + offset, size);
2627
2628 /* Init new header now. */
2629 xh->xh_count = xb_xh->xh_count;
2630 xh->xh_num_buckets = cpu_to_le16(1);
2631 xh->xh_name_value_len = cpu_to_le16(size);
2632 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
2633
2634 /* copy all the entries. */
2635 target = xh_bh->b_data;
2636 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
2637 size = count * sizeof(struct ocfs2_xattr_entry);
2638 memcpy(target + offset, (char *)xb_xh + offset, size);
2639
2640 /* Change the xe offset for all the xe because of the move. */
2641 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
2642 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
2643 for (i = 0; i < count; i++)
2644 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
2645
2646 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
2647 offset, size, off_change);
2648
2649 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
2650 cmp_xe, swap_xe);
2651}
2652
2653/*
2654 * After we move xattr from block to index btree, we have to
2655 * update ocfs2_xattr_search to the new xe and base.
2656 *
2657 * When the entry is in xattr block, xattr_bh indicates the storage place.
2658 * While if the entry is in index b-tree, "bucket" indicates the
2659 * real place of the xattr.
2660 */
2661static int ocfs2_xattr_update_xattr_search(struct inode *inode,
2662 struct ocfs2_xattr_search *xs,
2663 struct buffer_head *old_bh,
2664 struct buffer_head *new_bh)
2665{
2666 int ret = 0;
2667 char *buf = old_bh->b_data;
2668 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
2669 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
2670 int i, blocksize = inode->i_sb->s_blocksize;
2671 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2672
2673 xs->bucket.bhs[0] = new_bh;
2674 get_bh(new_bh);
2675 xs->bucket.xh = (struct ocfs2_xattr_header *)xs->bucket.bhs[0]->b_data;
2676 xs->header = xs->bucket.xh;
2677
2678 xs->base = new_bh->b_data;
2679 xs->end = xs->base + inode->i_sb->s_blocksize;
2680
2681 if (!xs->not_found) {
2682 if (OCFS2_XATTR_BUCKET_SIZE != blocksize) {
2683 ret = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
2684 xs->bucket.bhs[0]->b_blocknr + 1,
2685 blk_per_bucket - 1, &xs->bucket.bhs[1],
2686 OCFS2_BH_CACHED, inode);
2687 if (ret) {
2688 mlog_errno(ret);
2689 return ret;
2690 }
2691
2692 i = xs->here - old_xh->xh_entries;
2693 xs->here = &xs->header->xh_entries[i];
2694 }
2695 }
2696
2697 return ret;
2698}
2699
2700static int ocfs2_xattr_create_index_block(struct inode *inode,
2701 struct ocfs2_xattr_search *xs)
2702{
2703 int ret, credits = OCFS2_SUBALLOC_ALLOC;
2704 u32 bit_off, len;
2705 u64 blkno;
2706 handle_t *handle;
2707 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2708 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2709 struct ocfs2_alloc_context *data_ac;
2710 struct buffer_head *xh_bh = NULL, *data_bh = NULL;
2711 struct buffer_head *xb_bh = xs->xattr_bh;
2712 struct ocfs2_xattr_block *xb =
2713 (struct ocfs2_xattr_block *)xb_bh->b_data;
2714 struct ocfs2_xattr_tree_root *xr;
2715 u16 xb_flags = le16_to_cpu(xb->xb_flags);
2716 u16 bpb = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2717
2718 mlog(0, "create xattr index block for %llu\n",
2719 (unsigned long long)xb_bh->b_blocknr);
2720
2721 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
2722
2723 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
2724 if (ret) {
2725 mlog_errno(ret);
2726 goto out;
2727 }
2728
2729 /*
2730 * XXX:
2731 * We can use this lock for now, and maybe move to a dedicated mutex
2732 * if performance becomes a problem later.
2733 */
2734 down_write(&oi->ip_alloc_sem);
2735
2736 /*
2737 * 3 more credits, one for xattr block update, one for the 1st block
2738 * of the new xattr bucket and one for the value/data.
2739 */
2740 credits += 3;
2741 handle = ocfs2_start_trans(osb, credits);
2742 if (IS_ERR(handle)) {
2743 ret = PTR_ERR(handle);
2744 mlog_errno(ret);
2745 goto out_sem;
2746 }
2747
2748 ret = ocfs2_journal_access(handle, inode, xb_bh,
2749 OCFS2_JOURNAL_ACCESS_WRITE);
2750 if (ret) {
2751 mlog_errno(ret);
2752 goto out_commit;
2753 }
2754
2755 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
2756 if (ret) {
2757 mlog_errno(ret);
2758 goto out_commit;
2759 }
2760
2761 /*
2762 * The bucket may spread in many blocks, and
2763 * we will only touch the 1st block and the last block
2764 * in the whole bucket(one for entry and one for data).
2765 */
2766 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
2767
2768 mlog(0, "allocate 1 cluster from %llu to xattr block\n", blkno);
2769
2770 xh_bh = sb_getblk(inode->i_sb, blkno);
2771 if (!xh_bh) {
2772 ret = -EIO;
2773 mlog_errno(ret);
2774 goto out_commit;
2775 }
2776
2777 ocfs2_set_new_buffer_uptodate(inode, xh_bh);
2778
2779 ret = ocfs2_journal_access(handle, inode, xh_bh,
2780 OCFS2_JOURNAL_ACCESS_CREATE);
2781 if (ret) {
2782 mlog_errno(ret);
2783 goto out_commit;
2784 }
2785
2786 if (bpb > 1) {
2787 data_bh = sb_getblk(inode->i_sb, blkno + bpb - 1);
2788 if (!data_bh) {
2789 ret = -EIO;
2790 mlog_errno(ret);
2791 goto out_commit;
2792 }
2793
2794 ocfs2_set_new_buffer_uptodate(inode, data_bh);
2795
2796 ret = ocfs2_journal_access(handle, inode, data_bh,
2797 OCFS2_JOURNAL_ACCESS_CREATE);
2798 if (ret) {
2799 mlog_errno(ret);
2800 goto out_commit;
2801 }
2802 }
2803
2804 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xh_bh, data_bh);
2805
2806 ocfs2_journal_dirty(handle, xh_bh);
2807 if (data_bh)
2808 ocfs2_journal_dirty(handle, data_bh);
2809
2810 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh, xh_bh);
2811
2812 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
2813 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
2814 offsetof(struct ocfs2_xattr_block, xb_attrs));
2815
2816 xr = &xb->xb_attrs.xb_root;
2817 xr->xt_clusters = cpu_to_le32(1);
2818 xr->xt_last_eb_blk = 0;
2819 xr->xt_list.l_tree_depth = 0;
2820 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
2821 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2822
2823 xr->xt_list.l_recs[0].e_cpos = 0;
2824 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
2825 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
2826
2827 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
2828
2829 ret = ocfs2_journal_dirty(handle, xb_bh);
2830 if (ret) {
2831 mlog_errno(ret);
2832 goto out_commit;
2833 }
2834
2835out_commit:
2836 ocfs2_commit_trans(osb, handle);
2837
2838out_sem:
2839 up_write(&oi->ip_alloc_sem);
2840
2841out:
2842 if (data_ac)
2843 ocfs2_free_alloc_context(data_ac);
2844
2845 brelse(xh_bh);
2846 brelse(data_bh);
2847
2848 return ret;
2849}
2850
2851static int cmp_xe_offset(const void *a, const void *b)
2852{
2853 const struct ocfs2_xattr_entry *l = a, *r = b;
2854 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
2855 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
2856
2857 if (l_name_offset < r_name_offset)
2858 return 1;
2859 if (l_name_offset > r_name_offset)
2860 return -1;
2861 return 0;
2862}
2863
2864/*
2865 * defrag a xattr bucket if we find that the bucket has some
2866 * holes beteen name/value pairs.
2867 * We will move all the name/value pairs to the end of the bucket
2868 * so that we can spare some space for insertion.
2869 */
2870static int ocfs2_defrag_xattr_bucket(struct inode *inode,
2871 struct ocfs2_xattr_bucket *bucket)
2872{
2873 int ret, i;
2874 size_t end, offset, len, value_len;
2875 struct ocfs2_xattr_header *xh;
2876 char *entries, *buf, *bucket_buf = NULL;
2877 u64 blkno = bucket->bhs[0]->b_blocknr;
2878 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2879 u16 xh_free_start;
2880 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2881 size_t blocksize = inode->i_sb->s_blocksize;
2882 handle_t *handle;
2883 struct buffer_head **bhs;
2884 struct ocfs2_xattr_entry *xe;
2885
2886 bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
2887 GFP_NOFS);
2888 if (!bhs)
2889 return -ENOMEM;
2890
2891 ret = ocfs2_read_blocks(osb, blkno, blk_per_bucket, bhs,
2892 OCFS2_BH_CACHED, inode);
2893 if (ret)
2894 goto out;
2895
2896 /*
2897 * In order to make the operation more efficient and generic,
2898 * we copy all the blocks into a contiguous memory and do the
2899 * defragment there, so if anything is error, we will not touch
2900 * the real block.
2901 */
2902 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
2903 if (!bucket_buf) {
2904 ret = -EIO;
2905 goto out;
2906 }
2907
2908 buf = bucket_buf;
2909 for (i = 0; i < blk_per_bucket; i++, buf += blocksize)
2910 memcpy(buf, bhs[i]->b_data, blocksize);
2911
2912 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), blk_per_bucket);
2913 if (IS_ERR(handle)) {
2914 ret = PTR_ERR(handle);
2915 handle = NULL;
2916 mlog_errno(ret);
2917 goto out;
2918 }
2919
2920 for (i = 0; i < blk_per_bucket; i++) {
2921 ret = ocfs2_journal_access(handle, inode, bhs[i],
2922 OCFS2_JOURNAL_ACCESS_WRITE);
2923 if (ret < 0) {
2924 mlog_errno(ret);
2925 goto commit;
2926 }
2927 }
2928
2929 xh = (struct ocfs2_xattr_header *)bucket_buf;
2930 entries = (char *)xh->xh_entries;
2931 xh_free_start = le16_to_cpu(xh->xh_free_start);
2932
2933 mlog(0, "adjust xattr bucket in %llu, count = %u, "
2934 "xh_free_start = %u, xh_name_value_len = %u.\n",
2935 blkno, le16_to_cpu(xh->xh_count), xh_free_start,
2936 le16_to_cpu(xh->xh_name_value_len));
2937
2938 /*
2939 * sort all the entries by their offset.
2940 * the largest will be the first, so that we can
2941 * move them to the end one by one.
2942 */
2943 sort(entries, le16_to_cpu(xh->xh_count),
2944 sizeof(struct ocfs2_xattr_entry),
2945 cmp_xe_offset, swap_xe);
2946
2947 /* Move all name/values to the end of the bucket. */
2948 xe = xh->xh_entries;
2949 end = OCFS2_XATTR_BUCKET_SIZE;
2950 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
2951 offset = le16_to_cpu(xe->xe_name_offset);
2952 if (ocfs2_xattr_is_local(xe))
2953 value_len = OCFS2_XATTR_SIZE(
2954 le64_to_cpu(xe->xe_value_size));
2955 else
2956 value_len = OCFS2_XATTR_ROOT_SIZE;
2957 len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
2958
2959 /*
2960 * We must make sure that the name/value pair
2961 * exist in the same block. So adjust end to
2962 * the previous block end if needed.
2963 */
2964 if (((end - len) / blocksize !=
2965 (end - 1) / blocksize))
2966 end = end - end % blocksize;
2967
2968 if (end > offset + len) {
2969 memmove(bucket_buf + end - len,
2970 bucket_buf + offset, len);
2971 xe->xe_name_offset = cpu_to_le16(end - len);
2972 }
2973
2974 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
2975 "bucket %llu\n", (unsigned long long)blkno);
2976
2977 end -= len;
2978 }
2979
2980 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
2981 "bucket %llu\n", (unsigned long long)blkno);
2982
2983 if (xh_free_start == end)
2984 goto commit;
2985
2986 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
2987 xh->xh_free_start = cpu_to_le16(end);
2988
2989 /* sort the entries by their name_hash. */
2990 sort(entries, le16_to_cpu(xh->xh_count),
2991 sizeof(struct ocfs2_xattr_entry),
2992 cmp_xe, swap_xe);
2993
2994 buf = bucket_buf;
2995 for (i = 0; i < blk_per_bucket; i++, buf += blocksize) {
2996 memcpy(bhs[i]->b_data, buf, blocksize);
2997 ocfs2_journal_dirty(handle, bhs[i]);
2998 }
2999
3000commit:
3001 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
3002out:
3003
3004 if (bhs) {
3005 for (i = 0; i < blk_per_bucket; i++)
3006 brelse(bhs[i]);
3007 }
3008 kfree(bhs);
3009
3010 kfree(bucket_buf);
3011 return ret;
3012}
3013
3014/*
3015 * Move half nums of the xattr bucket in the previous cluster to this new
3016 * cluster. We only touch the last cluster of the previous extend record.
3017 *
3018 * first_bh is the first buffer_head of a series of bucket in the same
3019 * extent rec and header_bh is the header of one bucket in this cluster.
3020 * They will be updated if we move the data header_bh contains to the new
3021 * cluster. first_hash will be set as the 1st xe's name_hash of the new cluster.
3022 */
3023static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
3024 handle_t *handle,
3025 struct buffer_head **first_bh,
3026 struct buffer_head **header_bh,
3027 u64 new_blkno,
3028 u64 prev_blkno,
3029 u32 num_clusters,
3030 u32 *first_hash)
3031{
3032 int i, ret, credits;
3033 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3034 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3035 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3036 int blocksize = inode->i_sb->s_blocksize;
3037 struct buffer_head *old_bh, *new_bh, *prev_bh, *new_first_bh = NULL;
3038 struct ocfs2_xattr_header *new_xh;
3039 struct ocfs2_xattr_header *xh =
3040 (struct ocfs2_xattr_header *)((*first_bh)->b_data);
3041
3042 BUG_ON(le16_to_cpu(xh->xh_num_buckets) < num_buckets);
3043 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == osb->s_clustersize);
3044
3045 prev_bh = *first_bh;
3046 get_bh(prev_bh);
3047 xh = (struct ocfs2_xattr_header *)prev_bh->b_data;
3048
3049 prev_blkno += (num_clusters - 1) * bpc + bpc / 2;
3050
3051 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
3052 prev_blkno, new_blkno);
3053
3054 /*
3055 * We need to update the 1st half of the new cluster and
3056 * 1 more for the update of the 1st bucket of the previous
3057 * extent record.
3058 */
3059 credits = bpc / 2 + 1;
3060 ret = ocfs2_extend_trans(handle, credits);
3061 if (ret) {
3062 mlog_errno(ret);
3063 goto out;
3064 }
3065
3066 ret = ocfs2_journal_access(handle, inode, prev_bh,
3067 OCFS2_JOURNAL_ACCESS_WRITE);
3068 if (ret) {
3069 mlog_errno(ret);
3070 goto out;
3071 }
3072
3073 for (i = 0; i < bpc / 2; i++, prev_blkno++, new_blkno++) {
3074 old_bh = new_bh = NULL;
3075 new_bh = sb_getblk(inode->i_sb, new_blkno);
3076 if (!new_bh) {
3077 ret = -EIO;
3078 mlog_errno(ret);
3079 goto out;
3080 }
3081
3082 ocfs2_set_new_buffer_uptodate(inode, new_bh);
3083
3084 ret = ocfs2_journal_access(handle, inode, new_bh,
3085 OCFS2_JOURNAL_ACCESS_CREATE);
3086 if (ret < 0) {
3087 mlog_errno(ret);
3088 brelse(new_bh);
3089 goto out;
3090 }
3091
3092 ret = ocfs2_read_block(osb, prev_blkno,
3093 &old_bh, OCFS2_BH_CACHED, inode);
3094 if (ret < 0) {
3095 mlog_errno(ret);
3096 brelse(new_bh);
3097 goto out;
3098 }
3099
3100 memcpy(new_bh->b_data, old_bh->b_data, blocksize);
3101
3102 if (i == 0) {
3103 new_xh = (struct ocfs2_xattr_header *)new_bh->b_data;
3104 new_xh->xh_num_buckets = cpu_to_le16(num_buckets / 2);
3105
3106 if (first_hash)
3107 *first_hash = le32_to_cpu(
3108 new_xh->xh_entries[0].xe_name_hash);
3109 new_first_bh = new_bh;
3110 get_bh(new_first_bh);
3111 }
3112
3113 ocfs2_journal_dirty(handle, new_bh);
3114
3115 if (*header_bh == old_bh) {
3116 brelse(*header_bh);
3117 *header_bh = new_bh;
3118 get_bh(*header_bh);
3119
3120 brelse(*first_bh);
3121 *first_bh = new_first_bh;
3122 get_bh(*first_bh);
3123 }
3124 brelse(new_bh);
3125 brelse(old_bh);
3126 }
3127
3128 le16_add_cpu(&xh->xh_num_buckets, -(num_buckets / 2));
3129
3130 ocfs2_journal_dirty(handle, prev_bh);
3131out:
3132 brelse(prev_bh);
3133 brelse(new_first_bh);
3134 return ret;
3135}
3136
3137static int ocfs2_read_xattr_bucket(struct inode *inode,
3138 u64 blkno,
3139 struct buffer_head **bhs,
3140 int new)
3141{
3142 int ret = 0;
3143 u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3144
3145 if (!new)
3146 return ocfs2_read_blocks(OCFS2_SB(inode->i_sb), blkno,
3147 blk_per_bucket, bhs,
3148 OCFS2_BH_CACHED, inode);
3149
3150 for (i = 0; i < blk_per_bucket; i++) {
3151 bhs[i] = sb_getblk(inode->i_sb, blkno + i);
3152 if (bhs[i] == NULL) {
3153 ret = -EIO;
3154 mlog_errno(ret);
3155 break;
3156 }
3157 ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
3158 }
3159
3160 return ret;
3161}
3162
3163/*
3164 * Move half num of the xattrs in old bucket(blk) to new bucket(new_blk).
3165 * first_hash will record the 1st hash of the new bucket.
3166 */
3167static int ocfs2_half_xattr_bucket(struct inode *inode,
3168 handle_t *handle,
3169 u64 blk,
3170 u64 new_blk,
3171 u32 *first_hash,
3172 int new_bucket_head)
3173{
3174 int ret, i;
3175 u16 count, start, len, name_value_len, xe_len, name_offset;
3176 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3177 struct buffer_head **s_bhs, **t_bhs = NULL;
3178 struct ocfs2_xattr_header *xh;
3179 struct ocfs2_xattr_entry *xe;
3180 int blocksize = inode->i_sb->s_blocksize;
3181
3182 mlog(0, "move half of xattrs from bucket %llu to %llu\n",
3183 blk, new_blk);
3184
3185 s_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
3186 if (!s_bhs)
3187 return -ENOMEM;
3188
3189 ret = ocfs2_read_xattr_bucket(inode, blk, s_bhs, 0);
3190 if (ret) {
3191 mlog_errno(ret);
3192 goto out;
3193 }
3194
3195 ret = ocfs2_journal_access(handle, inode, s_bhs[0],
3196 OCFS2_JOURNAL_ACCESS_WRITE);
3197 if (ret) {
3198 mlog_errno(ret);
3199 goto out;
3200 }
3201
3202 t_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
3203 if (!t_bhs) {
3204 ret = -ENOMEM;
3205 goto out;
3206 }
3207
3208 ret = ocfs2_read_xattr_bucket(inode, new_blk, t_bhs, new_bucket_head);
3209 if (ret) {
3210 mlog_errno(ret);
3211 goto out;
3212 }
3213
3214 for (i = 0; i < blk_per_bucket; i++) {
3215 ret = ocfs2_journal_access(handle, inode, t_bhs[i],
3216 OCFS2_JOURNAL_ACCESS_CREATE);
3217 if (ret) {
3218 mlog_errno(ret);
3219 goto out;
3220 }
3221 }
3222
3223 /* copy the whole bucket to the new first. */
3224 for (i = 0; i < blk_per_bucket; i++)
3225 memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
3226
3227 /* update the new bucket. */
3228 xh = (struct ocfs2_xattr_header *)t_bhs[0]->b_data;
3229 count = le16_to_cpu(xh->xh_count);
3230 start = count / 2;
3231
3232 /*
3233 * Calculate the total name/value len and xh_free_start for
3234 * the old bucket first.
3235 */
3236 name_offset = OCFS2_XATTR_BUCKET_SIZE;
3237 name_value_len = 0;
3238 for (i = 0; i < start; i++) {
3239 xe = &xh->xh_entries[i];
3240 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3241 if (ocfs2_xattr_is_local(xe))
3242 xe_len +=
3243 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3244 else
3245 xe_len += OCFS2_XATTR_ROOT_SIZE;
3246 name_value_len += xe_len;
3247 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
3248 name_offset = le16_to_cpu(xe->xe_name_offset);
3249 }
3250
3251 /*
3252 * Now begin the modification to the new bucket.
3253 *
3254 * In the new bucket, We just move the xattr entry to the beginning
3255 * and don't touch the name/value. So there will be some holes in the
3256 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
3257 * called.
3258 */
3259 xe = &xh->xh_entries[start];
3260 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
3261 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
ff1ec20e
MF
3262 (int)((char *)xe - (char *)xh),
3263 (int)((char *)xh->xh_entries - (char *)xh));
01225596
TM
3264 memmove((char *)xh->xh_entries, (char *)xe, len);
3265 xe = &xh->xh_entries[count - start];
3266 len = sizeof(struct ocfs2_xattr_entry) * start;
3267 memset((char *)xe, 0, len);
3268
3269 le16_add_cpu(&xh->xh_count, -start);
3270 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
3271
3272 /* Calculate xh_free_start for the new bucket. */
3273 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3274 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3275 xe = &xh->xh_entries[i];
3276 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3277 if (ocfs2_xattr_is_local(xe))
3278 xe_len +=
3279 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3280 else
3281 xe_len += OCFS2_XATTR_ROOT_SIZE;
3282 if (le16_to_cpu(xe->xe_name_offset) <
3283 le16_to_cpu(xh->xh_free_start))
3284 xh->xh_free_start = xe->xe_name_offset;
3285 }
3286
3287 /* set xh->xh_num_buckets for the new xh. */
3288 if (new_bucket_head)
3289 xh->xh_num_buckets = cpu_to_le16(1);
3290 else
3291 xh->xh_num_buckets = 0;
3292
3293 for (i = 0; i < blk_per_bucket; i++) {
3294 ocfs2_journal_dirty(handle, t_bhs[i]);
3295 if (ret)
3296 mlog_errno(ret);
3297 }
3298
3299 /* store the first_hash of the new bucket. */
3300 if (first_hash)
3301 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3302
3303 /*
3304 * Now only update the 1st block of the old bucket.
3305 * Please note that the entry has been sorted already above.
3306 */
3307 xh = (struct ocfs2_xattr_header *)s_bhs[0]->b_data;
3308 memset(&xh->xh_entries[start], 0,
3309 sizeof(struct ocfs2_xattr_entry) * (count - start));
3310 xh->xh_count = cpu_to_le16(start);
3311 xh->xh_free_start = cpu_to_le16(name_offset);
3312 xh->xh_name_value_len = cpu_to_le16(name_value_len);
3313
3314 ocfs2_journal_dirty(handle, s_bhs[0]);
3315 if (ret)
3316 mlog_errno(ret);
3317
3318out:
3319 if (s_bhs) {
3320 for (i = 0; i < blk_per_bucket; i++)
3321 brelse(s_bhs[i]);
3322 }
3323 kfree(s_bhs);
3324
3325 if (t_bhs) {
3326 for (i = 0; i < blk_per_bucket; i++)
3327 brelse(t_bhs[i]);
3328 }
3329 kfree(t_bhs);
3330
3331 return ret;
3332}
3333
3334/*
3335 * Copy xattr from one bucket to another bucket.
3336 *
3337 * The caller must make sure that the journal transaction
3338 * has enough space for journaling.
3339 */
3340static int ocfs2_cp_xattr_bucket(struct inode *inode,
3341 handle_t *handle,
3342 u64 s_blkno,
3343 u64 t_blkno,
3344 int t_is_new)
3345{
3346 int ret, i;
3347 int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3348 int blocksize = inode->i_sb->s_blocksize;
3349 struct buffer_head **s_bhs, **t_bhs = NULL;
3350
3351 BUG_ON(s_blkno == t_blkno);
3352
3353 mlog(0, "cp bucket %llu to %llu, target is %d\n",
3354 s_blkno, t_blkno, t_is_new);
3355
3356 s_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
3357 GFP_NOFS);
3358 if (!s_bhs)
3359 return -ENOMEM;
3360
3361 ret = ocfs2_read_xattr_bucket(inode, s_blkno, s_bhs, 0);
3362 if (ret)
3363 goto out;
3364
3365 t_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
3366 GFP_NOFS);
3367 if (!t_bhs) {
3368 ret = -ENOMEM;
3369 goto out;
3370 }
3371
3372 ret = ocfs2_read_xattr_bucket(inode, t_blkno, t_bhs, t_is_new);
3373 if (ret)
3374 goto out;
3375
3376 for (i = 0; i < blk_per_bucket; i++) {
3377 ret = ocfs2_journal_access(handle, inode, t_bhs[i],
3378 OCFS2_JOURNAL_ACCESS_WRITE);
3379 if (ret)
3380 goto out;
3381 }
3382
3383 for (i = 0; i < blk_per_bucket; i++) {
3384 memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
3385 ocfs2_journal_dirty(handle, t_bhs[i]);
3386 }
3387
3388out:
3389 if (s_bhs) {
3390 for (i = 0; i < blk_per_bucket; i++)
3391 brelse(s_bhs[i]);
3392 }
3393 kfree(s_bhs);
3394
3395 if (t_bhs) {
3396 for (i = 0; i < blk_per_bucket; i++)
3397 brelse(t_bhs[i]);
3398 }
3399 kfree(t_bhs);
3400
3401 return ret;
3402}
3403
3404/*
3405 * Copy one xattr cluster from src_blk to to_blk.
3406 * The to_blk will become the first bucket header of the cluster, so its
3407 * xh_num_buckets will be initialized as the bucket num in the cluster.
3408 */
3409static int ocfs2_cp_xattr_cluster(struct inode *inode,
3410 handle_t *handle,
3411 struct buffer_head *first_bh,
3412 u64 src_blk,
3413 u64 to_blk,
3414 u32 *first_hash)
3415{
3416 int i, ret, credits;
3417 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3418 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3419 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3420 struct buffer_head *bh = NULL;
3421 struct ocfs2_xattr_header *xh;
3422 u64 to_blk_start = to_blk;
3423
3424 mlog(0, "cp xattrs from cluster %llu to %llu\n", src_blk, to_blk);
3425
3426 /*
3427 * We need to update the new cluster and 1 more for the update of
3428 * the 1st bucket of the previous extent rec.
3429 */
3430 credits = bpc + 1;
3431 ret = ocfs2_extend_trans(handle, credits);
3432 if (ret) {
3433 mlog_errno(ret);
3434 goto out;
3435 }
3436
3437 ret = ocfs2_journal_access(handle, inode, first_bh,
3438 OCFS2_JOURNAL_ACCESS_WRITE);
3439 if (ret) {
3440 mlog_errno(ret);
3441 goto out;
3442 }
3443
3444 for (i = 0; i < num_buckets; i++) {
3445 ret = ocfs2_cp_xattr_bucket(inode, handle,
3446 src_blk, to_blk, 1);
3447 if (ret) {
3448 mlog_errno(ret);
3449 goto out;
3450 }
3451
3452 src_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3453 to_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3454 }
3455
3456 /* update the old bucket header. */
3457 xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3458 le16_add_cpu(&xh->xh_num_buckets, -num_buckets);
3459
3460 ocfs2_journal_dirty(handle, first_bh);
3461
3462 /* update the new bucket header. */
3463 ret = ocfs2_read_block(osb, to_blk_start, &bh, OCFS2_BH_CACHED, inode);
3464 if (ret < 0) {
3465 mlog_errno(ret);
3466 goto out;
3467 }
3468
3469 ret = ocfs2_journal_access(handle, inode, bh,
3470 OCFS2_JOURNAL_ACCESS_WRITE);
3471 if (ret) {
3472 mlog_errno(ret);
3473 goto out;
3474 }
3475
3476 xh = (struct ocfs2_xattr_header *)bh->b_data;
3477 xh->xh_num_buckets = cpu_to_le16(num_buckets);
3478
3479 ocfs2_journal_dirty(handle, bh);
3480
3481 if (first_hash)
3482 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3483out:
3484 brelse(bh);
3485 return ret;
3486}
3487
3488/*
3489 * Move half of the xattrs in this cluster to the new cluster.
3490 * This function should only be called when bucket size == cluster size.
3491 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
3492 */
3493static int ocfs2_half_xattr_cluster(struct inode *inode,
3494 handle_t *handle,
3495 u64 prev_blk,
3496 u64 new_blk,
3497 u32 *first_hash)
3498{
3499 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3500 int ret, credits = 2 * blk_per_bucket;
3501
3502 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
3503
3504 ret = ocfs2_extend_trans(handle, credits);
3505 if (ret) {
3506 mlog_errno(ret);
3507 return ret;
3508 }
3509
3510 /* Move half of the xattr in start_blk to the next bucket. */
3511 return ocfs2_half_xattr_bucket(inode, handle, prev_blk,
3512 new_blk, first_hash, 1);
3513}
3514
3515/*
3516 * Move some xattrs from the old cluster to the new one since they are not
3517 * contiguous in ocfs2 xattr tree.
3518 *
3519 * new_blk starts a new separate cluster, and we will move some xattrs from
3520 * prev_blk to it. v_start will be set as the first name hash value in this
3521 * new cluster so that it can be used as e_cpos during tree insertion and
3522 * don't collide with our original b-tree operations. first_bh and header_bh
3523 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
3524 * to extend the insert bucket.
3525 *
3526 * The problem is how much xattr should we move to the new one and when should
3527 * we update first_bh and header_bh?
3528 * 1. If cluster size > bucket size, that means the previous cluster has more
3529 * than 1 bucket, so just move half nums of bucket into the new cluster and
3530 * update the first_bh and header_bh if the insert bucket has been moved
3531 * to the new cluster.
3532 * 2. If cluster_size == bucket_size:
3533 * a) If the previous extent rec has more than one cluster and the insert
3534 * place isn't in the last cluster, copy the entire last cluster to the
3535 * new one. This time, we don't need to upate the first_bh and header_bh
3536 * since they will not be moved into the new cluster.
3537 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
3538 * the new one. And we set the extend flag to zero if the insert place is
3539 * moved into the new allocated cluster since no extend is needed.
3540 */
3541static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
3542 handle_t *handle,
3543 struct buffer_head **first_bh,
3544 struct buffer_head **header_bh,
3545 u64 new_blk,
3546 u64 prev_blk,
3547 u32 prev_clusters,
3548 u32 *v_start,
3549 int *extend)
3550{
3551 int ret = 0;
3552 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3553
3554 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
3555 prev_blk, prev_clusters, new_blk);
3556
3557 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1)
3558 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
3559 handle,
3560 first_bh,
3561 header_bh,
3562 new_blk,
3563 prev_blk,
3564 prev_clusters,
3565 v_start);
3566 else {
3567 u64 last_blk = prev_blk + bpc * (prev_clusters - 1);
3568
3569 if (prev_clusters > 1 && (*header_bh)->b_blocknr != last_blk)
3570 ret = ocfs2_cp_xattr_cluster(inode, handle, *first_bh,
3571 last_blk, new_blk,
3572 v_start);
3573 else {
3574 ret = ocfs2_half_xattr_cluster(inode, handle,
3575 last_blk, new_blk,
3576 v_start);
3577
3578 if ((*header_bh)->b_blocknr == last_blk && extend)
3579 *extend = 0;
3580 }
3581 }
3582
3583 return ret;
3584}
3585
3586/*
3587 * Add a new cluster for xattr storage.
3588 *
3589 * If the new cluster is contiguous with the previous one, it will be
3590 * appended to the same extent record, and num_clusters will be updated.
3591 * If not, we will insert a new extent for it and move some xattrs in
3592 * the last cluster into the new allocated one.
3593 * We also need to limit the maximum size of a btree leaf, otherwise we'll
3594 * lose the benefits of hashing because we'll have to search large leaves.
3595 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
3596 * if it's bigger).
3597 *
3598 * first_bh is the first block of the previous extent rec and header_bh
3599 * indicates the bucket we will insert the new xattrs. They will be updated
3600 * when the header_bh is moved into the new cluster.
3601 */
3602static int ocfs2_add_new_xattr_cluster(struct inode *inode,
3603 struct buffer_head *root_bh,
3604 struct buffer_head **first_bh,
3605 struct buffer_head **header_bh,
3606 u32 *num_clusters,
3607 u32 prev_cpos,
3608 u64 prev_blkno,
3609 int *extend)
3610{
3611 int ret, credits;
3612 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3613 u32 prev_clusters = *num_clusters;
3614 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
3615 u64 block;
3616 handle_t *handle = NULL;
3617 struct ocfs2_alloc_context *data_ac = NULL;
3618 struct ocfs2_alloc_context *meta_ac = NULL;
3619 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
f99b9b7c 3620 struct ocfs2_extent_tree et;
01225596
TM
3621
3622 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
3623 "previous xattr blkno = %llu\n",
3624 (unsigned long long)OCFS2_I(inode)->ip_blkno,
3625 prev_cpos, prev_blkno);
3626
8d6220d6 3627 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
f99b9b7c
JB
3628
3629 ret = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
3630 &data_ac, &meta_ac);
01225596
TM
3631 if (ret) {
3632 mlog_errno(ret);
3633 goto leave;
3634 }
3635
f99b9b7c
JB
3636 credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
3637 clusters_to_add);
01225596
TM
3638 handle = ocfs2_start_trans(osb, credits);
3639 if (IS_ERR(handle)) {
3640 ret = PTR_ERR(handle);
3641 handle = NULL;
3642 mlog_errno(ret);
3643 goto leave;
3644 }
3645
3646 ret = ocfs2_journal_access(handle, inode, root_bh,
3647 OCFS2_JOURNAL_ACCESS_WRITE);
3648 if (ret < 0) {
3649 mlog_errno(ret);
3650 goto leave;
3651 }
3652
3653 ret = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
3654 clusters_to_add, &bit_off, &num_bits);
3655 if (ret < 0) {
3656 if (ret != -ENOSPC)
3657 mlog_errno(ret);
3658 goto leave;
3659 }
3660
3661 BUG_ON(num_bits > clusters_to_add);
3662
3663 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
3664 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
3665 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
3666
3667 if (prev_blkno + prev_clusters * bpc == block &&
3668 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
3669 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
3670 /*
3671 * If this cluster is contiguous with the old one and
3672 * adding this new cluster, we don't surpass the limit of
3673 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
3674 * initialized and used like other buckets in the previous
3675 * cluster.
3676 * So add it as a contiguous one. The caller will handle
3677 * its init process.
3678 */
3679 v_start = prev_cpos + prev_clusters;
3680 *num_clusters = prev_clusters + num_bits;
3681 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
3682 num_bits);
3683 } else {
3684 ret = ocfs2_adjust_xattr_cross_cluster(inode,
3685 handle,
3686 first_bh,
3687 header_bh,
3688 block,
3689 prev_blkno,
3690 prev_clusters,
3691 &v_start,
3692 extend);
3693 if (ret) {
3694 mlog_errno(ret);
3695 goto leave;
3696 }
3697 }
3698
28b8ca0b
TM
3699 if (handle->h_buffer_credits < credits) {
3700 /*
3701 * The journal has been restarted before, and don't
3702 * have enough space for the insertion, so extend it
3703 * here.
3704 */
3705 ret = ocfs2_extend_trans(handle, credits);
3706 if (ret) {
3707 mlog_errno(ret);
3708 goto leave;
3709 }
3710 }
01225596
TM
3711 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
3712 num_bits, block, v_start);
f99b9b7c
JB
3713 ret = ocfs2_insert_extent(osb, handle, inode, &et, v_start, block,
3714 num_bits, 0, meta_ac);
01225596
TM
3715 if (ret < 0) {
3716 mlog_errno(ret);
3717 goto leave;
3718 }
3719
3720 ret = ocfs2_journal_dirty(handle, root_bh);
3721 if (ret < 0) {
3722 mlog_errno(ret);
3723 goto leave;
3724 }
3725
3726leave:
3727 if (handle)
3728 ocfs2_commit_trans(osb, handle);
3729 if (data_ac)
3730 ocfs2_free_alloc_context(data_ac);
3731 if (meta_ac)
3732 ocfs2_free_alloc_context(meta_ac);
3733
3734 return ret;
3735}
3736
3737/*
3738 * Extend a new xattr bucket and move xattrs to the end one by one until
3739 * We meet with start_bh. Only move half of the xattrs to the bucket after it.
3740 */
3741static int ocfs2_extend_xattr_bucket(struct inode *inode,
3742 struct buffer_head *first_bh,
3743 struct buffer_head *start_bh,
3744 u32 num_clusters)
3745{
3746 int ret, credits;
3747 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3748 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3749 u64 start_blk = start_bh->b_blocknr, end_blk;
3750 u32 num_buckets = num_clusters * ocfs2_xattr_buckets_per_cluster(osb);
3751 handle_t *handle;
3752 struct ocfs2_xattr_header *first_xh =
3753 (struct ocfs2_xattr_header *)first_bh->b_data;
3754 u16 bucket = le16_to_cpu(first_xh->xh_num_buckets);
3755
3756 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
3757 "from %llu, len = %u\n", start_blk,
3758 (unsigned long long)first_bh->b_blocknr, num_clusters);
3759
3760 BUG_ON(bucket >= num_buckets);
3761
3762 end_blk = first_bh->b_blocknr + (bucket - 1) * blk_per_bucket;
3763
3764 /*
3765 * We will touch all the buckets after the start_bh(include it).
3766 * Add one more bucket and modify the first_bh.
3767 */
3768 credits = end_blk - start_blk + 2 * blk_per_bucket + 1;
3769 handle = ocfs2_start_trans(osb, credits);
3770 if (IS_ERR(handle)) {
3771 ret = PTR_ERR(handle);
3772 handle = NULL;
3773 mlog_errno(ret);
3774 goto out;
3775 }
3776
3777 ret = ocfs2_journal_access(handle, inode, first_bh,
3778 OCFS2_JOURNAL_ACCESS_WRITE);
3779 if (ret) {
3780 mlog_errno(ret);
3781 goto commit;
3782 }
3783
3784 while (end_blk != start_blk) {
3785 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
3786 end_blk + blk_per_bucket, 0);
3787 if (ret)
3788 goto commit;
3789 end_blk -= blk_per_bucket;
3790 }
3791
3792 /* Move half of the xattr in start_blk to the next bucket. */
3793 ret = ocfs2_half_xattr_bucket(inode, handle, start_blk,
3794 start_blk + blk_per_bucket, NULL, 0);
3795
3796 le16_add_cpu(&first_xh->xh_num_buckets, 1);
3797 ocfs2_journal_dirty(handle, first_bh);
3798
3799commit:
3800 ocfs2_commit_trans(osb, handle);
3801out:
3802 return ret;
3803}
3804
3805/*
3806 * Add new xattr bucket in an extent record and adjust the buckets accordingly.
3807 * xb_bh is the ocfs2_xattr_block.
3808 * We will move all the buckets starting from header_bh to the next place. As
3809 * for this one, half num of its xattrs will be moved to the next one.
3810 *
3811 * We will allocate a new cluster if current cluster is full and adjust
3812 * header_bh and first_bh if the insert place is moved to the new cluster.
3813 */
3814static int ocfs2_add_new_xattr_bucket(struct inode *inode,
3815 struct buffer_head *xb_bh,
3816 struct buffer_head *header_bh)
3817{
3818 struct ocfs2_xattr_header *first_xh = NULL;
3819 struct buffer_head *first_bh = NULL;
3820 struct ocfs2_xattr_block *xb =
3821 (struct ocfs2_xattr_block *)xb_bh->b_data;
3822 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3823 struct ocfs2_extent_list *el = &xb_root->xt_list;
3824 struct ocfs2_xattr_header *xh =
3825 (struct ocfs2_xattr_header *)header_bh->b_data;
3826 u32 name_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3827 struct super_block *sb = inode->i_sb;
3828 struct ocfs2_super *osb = OCFS2_SB(sb);
3829 int ret, num_buckets, extend = 1;
3830 u64 p_blkno;
3831 u32 e_cpos, num_clusters;
3832
3833 mlog(0, "Add new xattr bucket starting form %llu\n",
3834 (unsigned long long)header_bh->b_blocknr);
3835
3836 /*
3837 * Add refrence for header_bh here because it may be
3838 * changed in ocfs2_add_new_xattr_cluster and we need
3839 * to free it in the end.
3840 */
3841 get_bh(header_bh);
3842
3843 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
3844 &num_clusters, el);
3845 if (ret) {
3846 mlog_errno(ret);
3847 goto out;
3848 }
3849
3850 ret = ocfs2_read_block(osb, p_blkno,
3851 &first_bh, OCFS2_BH_CACHED, inode);
3852 if (ret) {
3853 mlog_errno(ret);
3854 goto out;
3855 }
3856
3857 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
3858 first_xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3859
3860 if (num_buckets == le16_to_cpu(first_xh->xh_num_buckets)) {
3861 ret = ocfs2_add_new_xattr_cluster(inode,
3862 xb_bh,
3863 &first_bh,
3864 &header_bh,
3865 &num_clusters,
3866 e_cpos,
3867 p_blkno,
3868 &extend);
3869 if (ret) {
3870 mlog_errno(ret);
3871 goto out;
3872 }
3873 }
3874
3875 if (extend)
3876 ret = ocfs2_extend_xattr_bucket(inode,
3877 first_bh,
3878 header_bh,
3879 num_clusters);
3880 if (ret)
3881 mlog_errno(ret);
3882out:
3883 brelse(first_bh);
3884 brelse(header_bh);
3885 return ret;
3886}
3887
3888static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
3889 struct ocfs2_xattr_bucket *bucket,
3890 int offs)
3891{
3892 int block_off = offs >> inode->i_sb->s_blocksize_bits;
3893
3894 offs = offs % inode->i_sb->s_blocksize;
3895 return bucket->bhs[block_off]->b_data + offs;
3896}
3897
3898/*
3899 * Handle the normal xattr set, including replace, delete and new.
01225596
TM
3900 *
3901 * Note: "local" indicates the real data's locality. So we can't
3902 * just its bucket locality by its length.
3903 */
3904static void ocfs2_xattr_set_entry_normal(struct inode *inode,
3905 struct ocfs2_xattr_info *xi,
3906 struct ocfs2_xattr_search *xs,
3907 u32 name_hash,
5a095611 3908 int local)
01225596
TM
3909{
3910 struct ocfs2_xattr_entry *last, *xe;
3911 int name_len = strlen(xi->name);
3912 struct ocfs2_xattr_header *xh = xs->header;
3913 u16 count = le16_to_cpu(xh->xh_count), start;
3914 size_t blocksize = inode->i_sb->s_blocksize;
3915 char *val;
3916 size_t offs, size, new_size;
3917
3918 last = &xh->xh_entries[count];
3919 if (!xs->not_found) {
3920 xe = xs->here;
3921 offs = le16_to_cpu(xe->xe_name_offset);
3922 if (ocfs2_xattr_is_local(xe))
3923 size = OCFS2_XATTR_SIZE(name_len) +
3924 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3925 else
3926 size = OCFS2_XATTR_SIZE(name_len) +
3927 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
3928
3929 /*
3930 * If the new value will be stored outside, xi->value has been
3931 * initalized as an empty ocfs2_xattr_value_root, and the same
3932 * goes with xi->value_len, so we can set new_size safely here.
3933 * See ocfs2_xattr_set_in_bucket.
3934 */
3935 new_size = OCFS2_XATTR_SIZE(name_len) +
3936 OCFS2_XATTR_SIZE(xi->value_len);
3937
3938 le16_add_cpu(&xh->xh_name_value_len, -size);
3939 if (xi->value) {
3940 if (new_size > size)
3941 goto set_new_name_value;
3942
3943 /* Now replace the old value with new one. */
3944 if (local)
3945 xe->xe_value_size = cpu_to_le64(xi->value_len);
3946 else
3947 xe->xe_value_size = 0;
3948
3949 val = ocfs2_xattr_bucket_get_val(inode,
3950 &xs->bucket, offs);
3951 memset(val + OCFS2_XATTR_SIZE(name_len), 0,
3952 size - OCFS2_XATTR_SIZE(name_len));
3953 if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
3954 memcpy(val + OCFS2_XATTR_SIZE(name_len),
3955 xi->value, xi->value_len);
3956
3957 le16_add_cpu(&xh->xh_name_value_len, new_size);
3958 ocfs2_xattr_set_local(xe, local);
3959 return;
3960 } else {
5a095611
TM
3961 /*
3962 * Remove the old entry if there is more than one.
3963 * We don't remove the last entry so that we can
3964 * use it to indicate the hash value of the empty
3965 * bucket.
3966 */
01225596 3967 last -= 1;
01225596 3968 le16_add_cpu(&xh->xh_count, -1);
5a095611
TM
3969 if (xh->xh_count) {
3970 memmove(xe, xe + 1,
3971 (void *)last - (void *)xe);
3972 memset(last, 0,
3973 sizeof(struct ocfs2_xattr_entry));
3974 } else
3975 xh->xh_free_start =
3976 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3977
01225596
TM
3978 return;
3979 }
3980 } else {
3981 /* find a new entry for insert. */
3982 int low = 0, high = count - 1, tmp;
3983 struct ocfs2_xattr_entry *tmp_xe;
3984
5a095611 3985 while (low <= high && count) {
01225596
TM
3986 tmp = (low + high) / 2;
3987 tmp_xe = &xh->xh_entries[tmp];
3988
3989 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
3990 low = tmp + 1;
3991 else if (name_hash <
3992 le32_to_cpu(tmp_xe->xe_name_hash))
3993 high = tmp - 1;
06b240d8
TM
3994 else {
3995 low = tmp;
01225596 3996 break;
06b240d8 3997 }
01225596
TM
3998 }
3999
4000 xe = &xh->xh_entries[low];
4001 if (low != count)
4002 memmove(xe + 1, xe, (void *)last - (void *)xe);
4003
4004 le16_add_cpu(&xh->xh_count, 1);
4005 memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
4006 xe->xe_name_hash = cpu_to_le32(name_hash);
4007 xe->xe_name_len = name_len;
4008 ocfs2_xattr_set_type(xe, xi->name_index);
4009 }
4010
4011set_new_name_value:
4012 /* Insert the new name+value. */
4013 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
4014
4015 /*
4016 * We must make sure that the name/value pair
4017 * exists in the same block.
4018 */
4019 offs = le16_to_cpu(xh->xh_free_start);
4020 start = offs - size;
4021
4022 if (start >> inode->i_sb->s_blocksize_bits !=
4023 (offs - 1) >> inode->i_sb->s_blocksize_bits) {
4024 offs = offs - offs % blocksize;
4025 xh->xh_free_start = cpu_to_le16(offs);
4026 }
4027
4028 val = ocfs2_xattr_bucket_get_val(inode,
4029 &xs->bucket, offs - size);
4030 xe->xe_name_offset = cpu_to_le16(offs - size);
4031
4032 memset(val, 0, size);
4033 memcpy(val, xi->name, name_len);
4034 memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
4035
4036 xe->xe_value_size = cpu_to_le64(xi->value_len);
4037 ocfs2_xattr_set_local(xe, local);
4038 xs->here = xe;
4039 le16_add_cpu(&xh->xh_free_start, -size);
4040 le16_add_cpu(&xh->xh_name_value_len, size);
4041
4042 return;
4043}
4044
4045static int ocfs2_xattr_bucket_handle_journal(struct inode *inode,
4046 handle_t *handle,
4047 struct ocfs2_xattr_search *xs,
4048 struct buffer_head **bhs,
4049 u16 bh_num)
4050{
4051 int ret = 0, off, block_off;
4052 struct ocfs2_xattr_entry *xe = xs->here;
4053
4054 /*
4055 * First calculate all the blocks we should journal_access
4056 * and journal_dirty. The first block should always be touched.
4057 */
4058 ret = ocfs2_journal_dirty(handle, bhs[0]);
4059 if (ret)
4060 mlog_errno(ret);
4061
4062 /* calc the data. */
4063 off = le16_to_cpu(xe->xe_name_offset);
4064 block_off = off >> inode->i_sb->s_blocksize_bits;
4065 ret = ocfs2_journal_dirty(handle, bhs[block_off]);
4066 if (ret)
4067 mlog_errno(ret);
4068
4069 return ret;
4070}
4071
4072/*
4073 * Set the xattr entry in the specified bucket.
4074 * The bucket is indicated by xs->bucket and it should have the enough
4075 * space for the xattr insertion.
4076 */
4077static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
4078 struct ocfs2_xattr_info *xi,
4079 struct ocfs2_xattr_search *xs,
4080 u32 name_hash,
5a095611 4081 int local)
01225596
TM
4082{
4083 int i, ret;
4084 handle_t *handle = NULL;
4085 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4086 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4087
ff1ec20e
MF
4088 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4089 (unsigned long)xi->value_len, xi->name_index,
01225596
TM
4090 (unsigned long long)xs->bucket.bhs[0]->b_blocknr);
4091
4092 if (!xs->bucket.bhs[1]) {
4093 ret = ocfs2_read_blocks(osb,
4094 xs->bucket.bhs[0]->b_blocknr + 1,
4095 blk_per_bucket - 1, &xs->bucket.bhs[1],
4096 OCFS2_BH_CACHED, inode);
4097 if (ret) {
4098 mlog_errno(ret);
4099 goto out;
4100 }
4101 }
4102
4103 handle = ocfs2_start_trans(osb, blk_per_bucket);
4104 if (IS_ERR(handle)) {
4105 ret = PTR_ERR(handle);
4106 handle = NULL;
4107 mlog_errno(ret);
4108 goto out;
4109 }
4110
4111 for (i = 0; i < blk_per_bucket; i++) {
4112 ret = ocfs2_journal_access(handle, inode, xs->bucket.bhs[i],
4113 OCFS2_JOURNAL_ACCESS_WRITE);
4114 if (ret < 0) {
4115 mlog_errno(ret);
4116 goto out;
4117 }
4118 }
4119
5a095611 4120 ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
01225596
TM
4121
4122 /*Only dirty the blocks we have touched in set xattr. */
4123 ret = ocfs2_xattr_bucket_handle_journal(inode, handle, xs,
4124 xs->bucket.bhs, blk_per_bucket);
4125 if (ret)
4126 mlog_errno(ret);
4127out:
4128 ocfs2_commit_trans(osb, handle);
4129
4130 return ret;
4131}
4132
4133static int ocfs2_xattr_value_update_size(struct inode *inode,
4134 struct buffer_head *xe_bh,
4135 struct ocfs2_xattr_entry *xe,
4136 u64 new_size)
4137{
4138 int ret;
4139 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4140 handle_t *handle = NULL;
4141
4142 handle = ocfs2_start_trans(osb, 1);
4143 if (handle == NULL) {
4144 ret = -ENOMEM;
4145 mlog_errno(ret);
4146 goto out;
4147 }
4148
4149 ret = ocfs2_journal_access(handle, inode, xe_bh,
4150 OCFS2_JOURNAL_ACCESS_WRITE);
4151 if (ret < 0) {
4152 mlog_errno(ret);
4153 goto out_commit;
4154 }
4155
4156 xe->xe_value_size = cpu_to_le64(new_size);
4157
4158 ret = ocfs2_journal_dirty(handle, xe_bh);
4159 if (ret < 0)
4160 mlog_errno(ret);
4161
4162out_commit:
4163 ocfs2_commit_trans(osb, handle);
4164out:
4165 return ret;
4166}
4167
4168/*
4169 * Truncate the specified xe_off entry in xattr bucket.
4170 * bucket is indicated by header_bh and len is the new length.
4171 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4172 *
4173 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4174 */
4175static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
4176 struct buffer_head *header_bh,
4177 int xe_off,
4178 int len)
4179{
4180 int ret, offset;
4181 u64 value_blk;
4182 struct buffer_head *value_bh = NULL;
4183 struct ocfs2_xattr_value_root *xv;
4184 struct ocfs2_xattr_entry *xe;
4185 struct ocfs2_xattr_header *xh =
4186 (struct ocfs2_xattr_header *)header_bh->b_data;
4187 size_t blocksize = inode->i_sb->s_blocksize;
4188
4189 xe = &xh->xh_entries[xe_off];
4190
4191 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
4192
4193 offset = le16_to_cpu(xe->xe_name_offset) +
4194 OCFS2_XATTR_SIZE(xe->xe_name_len);
4195
4196 value_blk = offset / blocksize;
4197
4198 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4199 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
4200 value_blk += header_bh->b_blocknr;
4201
4202 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), value_blk,
4203 &value_bh, OCFS2_BH_CACHED, inode);
4204 if (ret) {
4205 mlog_errno(ret);
4206 goto out;
4207 }
4208
4209 xv = (struct ocfs2_xattr_value_root *)
4210 (value_bh->b_data + offset % blocksize);
4211
4212 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
4213 xe_off, (unsigned long long)header_bh->b_blocknr, len);
4214 ret = ocfs2_xattr_value_truncate(inode, value_bh, xv, len);
4215 if (ret) {
4216 mlog_errno(ret);
4217 goto out;
4218 }
4219
4220 ret = ocfs2_xattr_value_update_size(inode, header_bh, xe, len);
4221 if (ret) {
4222 mlog_errno(ret);
4223 goto out;
4224 }
4225
4226out:
4227 brelse(value_bh);
4228 return ret;
4229}
4230
4231static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
4232 struct ocfs2_xattr_search *xs,
4233 int len)
4234{
4235 int ret, offset;
4236 struct ocfs2_xattr_entry *xe = xs->here;
4237 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
4238
4239 BUG_ON(!xs->bucket.bhs[0] || !xe || ocfs2_xattr_is_local(xe));
4240
4241 offset = xe - xh->xh_entries;
4242 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket.bhs[0],
4243 offset, len);
4244 if (ret)
4245 mlog_errno(ret);
4246
4247 return ret;
4248}
4249
4250static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
4251 struct ocfs2_xattr_search *xs,
4252 char *val,
4253 int value_len)
4254{
4255 int offset;
4256 struct ocfs2_xattr_value_root *xv;
4257 struct ocfs2_xattr_entry *xe = xs->here;
4258
4259 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
4260
4261 offset = le16_to_cpu(xe->xe_name_offset) +
4262 OCFS2_XATTR_SIZE(xe->xe_name_len);
4263
4264 xv = (struct ocfs2_xattr_value_root *)(xs->base + offset);
4265
4266 return __ocfs2_xattr_set_value_outside(inode, xv, val, value_len);
4267}
4268
01225596
TM
4269static int ocfs2_rm_xattr_cluster(struct inode *inode,
4270 struct buffer_head *root_bh,
4271 u64 blkno,
4272 u32 cpos,
4273 u32 len)
4274{
4275 int ret;
4276 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4277 struct inode *tl_inode = osb->osb_tl_inode;
4278 handle_t *handle;
4279 struct ocfs2_xattr_block *xb =
4280 (struct ocfs2_xattr_block *)root_bh->b_data;
01225596
TM
4281 struct ocfs2_alloc_context *meta_ac = NULL;
4282 struct ocfs2_cached_dealloc_ctxt dealloc;
f99b9b7c
JB
4283 struct ocfs2_extent_tree et;
4284
8d6220d6 4285 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
01225596
TM
4286
4287 ocfs2_init_dealloc_ctxt(&dealloc);
4288
4289 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
4290 cpos, len, (unsigned long long)blkno);
4291
4292 ocfs2_remove_xattr_clusters_from_cache(inode, blkno, len);
4293
f99b9b7c 4294 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
01225596
TM
4295 if (ret) {
4296 mlog_errno(ret);
4297 return ret;
4298 }
4299
4300 mutex_lock(&tl_inode->i_mutex);
4301
4302 if (ocfs2_truncate_log_needs_flush(osb)) {
4303 ret = __ocfs2_flush_truncate_log(osb);
4304 if (ret < 0) {
4305 mlog_errno(ret);
4306 goto out;
4307 }
4308 }
4309
4310 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
4311 if (handle == NULL) {
4312 ret = -ENOMEM;
4313 mlog_errno(ret);
4314 goto out;
4315 }
4316
4317 ret = ocfs2_journal_access(handle, inode, root_bh,
4318 OCFS2_JOURNAL_ACCESS_WRITE);
4319 if (ret) {
4320 mlog_errno(ret);
4321 goto out_commit;
4322 }
4323
f99b9b7c
JB
4324 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
4325 &dealloc);
01225596
TM
4326 if (ret) {
4327 mlog_errno(ret);
4328 goto out_commit;
4329 }
4330
4331 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
4332
4333 ret = ocfs2_journal_dirty(handle, root_bh);
4334 if (ret) {
4335 mlog_errno(ret);
4336 goto out_commit;
4337 }
4338
4339 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
4340 if (ret)
4341 mlog_errno(ret);
4342
4343out_commit:
4344 ocfs2_commit_trans(osb, handle);
4345out:
4346 ocfs2_schedule_truncate_log_flush(osb, 1);
4347
4348 mutex_unlock(&tl_inode->i_mutex);
4349
4350 if (meta_ac)
4351 ocfs2_free_alloc_context(meta_ac);
4352
4353 ocfs2_run_deallocs(osb, &dealloc);
4354
4355 return ret;
4356}
4357
01225596
TM
4358static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
4359 struct ocfs2_xattr_search *xs)
4360{
4361 handle_t *handle = NULL;
4362 struct ocfs2_xattr_header *xh = xs->bucket.xh;
4363 struct ocfs2_xattr_entry *last = &xh->xh_entries[
4364 le16_to_cpu(xh->xh_count) - 1];
4365 int ret = 0;
4366
4367 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), 1);
4368 if (IS_ERR(handle)) {
4369 ret = PTR_ERR(handle);
4370 mlog_errno(ret);
4371 return;
4372 }
4373
4374 ret = ocfs2_journal_access(handle, inode, xs->bucket.bhs[0],
4375 OCFS2_JOURNAL_ACCESS_WRITE);
4376 if (ret) {
4377 mlog_errno(ret);
4378 goto out_commit;
4379 }
4380
4381 /* Remove the old entry. */
4382 memmove(xs->here, xs->here + 1,
4383 (void *)last - (void *)xs->here);
4384 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
4385 le16_add_cpu(&xh->xh_count, -1);
4386
4387 ret = ocfs2_journal_dirty(handle, xs->bucket.bhs[0]);
4388 if (ret < 0)
4389 mlog_errno(ret);
4390out_commit:
4391 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
4392}
4393
4394/*
4395 * Set the xattr name/value in the bucket specified in xs.
4396 *
4397 * As the new value in xi may be stored in the bucket or in an outside cluster,
4398 * we divide the whole process into 3 steps:
4399 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
4400 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
4401 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
4402 * 4. If the clusters for the new outside value can't be allocated, we need
4403 * to free the xattr we allocated in set.
4404 */
4405static int ocfs2_xattr_set_in_bucket(struct inode *inode,
4406 struct ocfs2_xattr_info *xi,
4407 struct ocfs2_xattr_search *xs)
4408{
5a095611 4409 int ret, local = 1;
01225596
TM
4410 size_t value_len;
4411 char *val = (char *)xi->value;
4412 struct ocfs2_xattr_entry *xe = xs->here;
2057e5c6
TM
4413 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name,
4414 strlen(xi->name));
01225596
TM
4415
4416 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
4417 /*
4418 * We need to truncate the xattr storage first.
4419 *
4420 * If both the old and new value are stored to
4421 * outside block, we only need to truncate
4422 * the storage and then set the value outside.
4423 *
4424 * If the new value should be stored within block,
4425 * we should free all the outside block first and
4426 * the modification to the xattr block will be done
4427 * by following steps.
4428 */
4429 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4430 value_len = xi->value_len;
4431 else
4432 value_len = 0;
4433
4434 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4435 value_len);
4436 if (ret)
4437 goto out;
4438
4439 if (value_len)
4440 goto set_value_outside;
4441 }
4442
4443 value_len = xi->value_len;
4444 /* So we have to handle the inside block change now. */
4445 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
4446 /*
4447 * If the new value will be stored outside of block,
4448 * initalize a new empty value root and insert it first.
4449 */
4450 local = 0;
4451 xi->value = &def_xv;
4452 xi->value_len = OCFS2_XATTR_ROOT_SIZE;
4453 }
4454
5a095611 4455 ret = ocfs2_xattr_set_entry_in_bucket(inode, xi, xs, name_hash, local);
01225596
TM
4456 if (ret) {
4457 mlog_errno(ret);
4458 goto out;
4459 }
4460
5a095611
TM
4461 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
4462 goto out;
01225596 4463
5a095611
TM
4464 /* allocate the space now for the outside block storage. */
4465 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4466 value_len);
4467 if (ret) {
4468 mlog_errno(ret);
4469
4470 if (xs->not_found) {
4471 /*
4472 * We can't allocate enough clusters for outside
4473 * storage and we have allocated xattr already,
4474 * so need to remove it.
4475 */
4476 ocfs2_xattr_bucket_remove_xs(inode, xs);
01225596 4477 }
01225596
TM
4478 goto out;
4479 }
4480
4481set_value_outside:
4482 ret = ocfs2_xattr_bucket_set_value_outside(inode, xs, val, value_len);
4483out:
4484 return ret;
4485}
4486
4487/* check whether the xattr bucket is filled up with the same hash value. */
4488static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
4489 struct ocfs2_xattr_bucket *bucket)
4490{
4491 struct ocfs2_xattr_header *xh = bucket->xh;
4492
4493 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
4494 xh->xh_entries[0].xe_name_hash) {
4495 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
4496 "hash = %u\n",
4497 (unsigned long long)bucket->bhs[0]->b_blocknr,
4498 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
4499 return -ENOSPC;
4500 }
4501
4502 return 0;
4503}
4504
4505static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
4506 struct ocfs2_xattr_info *xi,
4507 struct ocfs2_xattr_search *xs)
4508{
4509 struct ocfs2_xattr_header *xh;
4510 struct ocfs2_xattr_entry *xe;
4511 u16 count, header_size, xh_free_start;
4512 int i, free, max_free, need, old;
4513 size_t value_size = 0, name_len = strlen(xi->name);
4514 size_t blocksize = inode->i_sb->s_blocksize;
4515 int ret, allocation = 0;
4516 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4517
4518 mlog_entry("Set xattr %s in xattr index block\n", xi->name);
4519
4520try_again:
4521 xh = xs->header;
4522 count = le16_to_cpu(xh->xh_count);
4523 xh_free_start = le16_to_cpu(xh->xh_free_start);
4524 header_size = sizeof(struct ocfs2_xattr_header) +
4525 count * sizeof(struct ocfs2_xattr_entry);
4526 max_free = OCFS2_XATTR_BUCKET_SIZE -
4527 le16_to_cpu(xh->xh_name_value_len) - header_size;
4528
4529 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
4530 "of %u which exceed block size\n",
4531 (unsigned long long)xs->bucket.bhs[0]->b_blocknr,
4532 header_size);
4533
4534 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4535 value_size = OCFS2_XATTR_ROOT_SIZE;
4536 else if (xi->value)
4537 value_size = OCFS2_XATTR_SIZE(xi->value_len);
4538
4539 if (xs->not_found)
4540 need = sizeof(struct ocfs2_xattr_entry) +
4541 OCFS2_XATTR_SIZE(name_len) + value_size;
4542 else {
4543 need = value_size + OCFS2_XATTR_SIZE(name_len);
4544
4545 /*
4546 * We only replace the old value if the new length is smaller
4547 * than the old one. Otherwise we will allocate new space in the
4548 * bucket to store it.
4549 */
4550 xe = xs->here;
4551 if (ocfs2_xattr_is_local(xe))
4552 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4553 else
4554 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4555
4556 if (old >= value_size)
4557 need = 0;
4558 }
4559
4560 free = xh_free_start - header_size;
4561 /*
4562 * We need to make sure the new name/value pair
4563 * can exist in the same block.
4564 */
4565 if (xh_free_start % blocksize < need)
4566 free -= xh_free_start % blocksize;
4567
4568 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
4569 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
4570 " %u\n", xs->not_found,
4571 (unsigned long long)xs->bucket.bhs[0]->b_blocknr,
4572 free, need, max_free, le16_to_cpu(xh->xh_free_start),
4573 le16_to_cpu(xh->xh_name_value_len));
4574
4575 if (free < need || count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4576 if (need <= max_free &&
4577 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4578 /*
4579 * We can create the space by defragment. Since only the
4580 * name/value will be moved, the xe shouldn't be changed
4581 * in xs.
4582 */
4583 ret = ocfs2_defrag_xattr_bucket(inode, &xs->bucket);
4584 if (ret) {
4585 mlog_errno(ret);
4586 goto out;
4587 }
4588
4589 xh_free_start = le16_to_cpu(xh->xh_free_start);
4590 free = xh_free_start - header_size;
4591 if (xh_free_start % blocksize < need)
4592 free -= xh_free_start % blocksize;
4593
4594 if (free >= need)
4595 goto xattr_set;
4596
4597 mlog(0, "Can't get enough space for xattr insert by "
4598 "defragment. Need %u bytes, but we have %d, so "
4599 "allocate new bucket for it.\n", need, free);
4600 }
4601
4602 /*
4603 * We have to add new buckets or clusters and one
4604 * allocation should leave us enough space for insert.
4605 */
4606 BUG_ON(allocation);
4607
4608 /*
4609 * We do not allow for overlapping ranges between buckets. And
4610 * the maximum number of collisions we will allow for then is
4611 * one bucket's worth, so check it here whether we need to
4612 * add a new bucket for the insert.
4613 */
4614 ret = ocfs2_check_xattr_bucket_collision(inode, &xs->bucket);
4615 if (ret) {
4616 mlog_errno(ret);
4617 goto out;
4618 }
4619
4620 ret = ocfs2_add_new_xattr_bucket(inode,
4621 xs->xattr_bh,
4622 xs->bucket.bhs[0]);
4623 if (ret) {
4624 mlog_errno(ret);
4625 goto out;
4626 }
4627
4628 for (i = 0; i < blk_per_bucket; i++)
4629 brelse(xs->bucket.bhs[i]);
4630
4631 memset(&xs->bucket, 0, sizeof(xs->bucket));
4632
4633 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
4634 xi->name_index,
4635 xi->name, xs);
4636 if (ret && ret != -ENODATA)
4637 goto out;
4638 xs->not_found = ret;
4639 allocation = 1;
4640 goto try_again;
4641 }
4642
4643xattr_set:
4644 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs);
4645out:
4646 mlog_exit(ret);
4647 return ret;
4648}
a3944256
TM
4649
4650static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
4651 struct ocfs2_xattr_bucket *bucket,
4652 void *para)
4653{
4654 int ret = 0;
4655 struct ocfs2_xattr_header *xh = bucket->xh;
4656 u16 i;
4657 struct ocfs2_xattr_entry *xe;
4658
4659 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4660 xe = &xh->xh_entries[i];
4661 if (ocfs2_xattr_is_local(xe))
4662 continue;
4663
4664 ret = ocfs2_xattr_bucket_value_truncate(inode,
4665 bucket->bhs[0],
4666 i, 0);
4667 if (ret) {
4668 mlog_errno(ret);
4669 break;
4670 }
4671 }
4672
4673 return ret;
4674}
4675
4676static int ocfs2_delete_xattr_index_block(struct inode *inode,
4677 struct buffer_head *xb_bh)
4678{
4679 struct ocfs2_xattr_block *xb =
4680 (struct ocfs2_xattr_block *)xb_bh->b_data;
4681 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
4682 int ret = 0;
4683 u32 name_hash = UINT_MAX, e_cpos, num_clusters;
4684 u64 p_blkno;
4685
4686 if (le16_to_cpu(el->l_next_free_rec) == 0)
4687 return 0;
4688
4689 while (name_hash > 0) {
4690 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
4691 &e_cpos, &num_clusters, el);
4692 if (ret) {
4693 mlog_errno(ret);
4694 goto out;
4695 }
4696
4697 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
4698 ocfs2_delete_xattr_in_bucket,
4699 NULL);
4700 if (ret) {
4701 mlog_errno(ret);
4702 goto out;
4703 }
4704
4705 ret = ocfs2_rm_xattr_cluster(inode, xb_bh,
4706 p_blkno, e_cpos, num_clusters);
4707 if (ret) {
4708 mlog_errno(ret);
4709 break;
4710 }
4711
4712 if (e_cpos == 0)
4713 break;
4714
4715 name_hash = e_cpos - 1;
4716 }
4717
4718out:
4719 return ret;
4720}
99219aea
MF
4721
4722/*
4723 * 'trusted' attributes support
4724 */
4725
4726#define XATTR_TRUSTED_PREFIX "trusted."
4727
4728static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list,
4729 size_t list_size, const char *name,
4730 size_t name_len)
4731{
4732 const size_t prefix_len = sizeof(XATTR_TRUSTED_PREFIX) - 1;
4733 const size_t total_len = prefix_len + name_len + 1;
4734
4735 if (list && total_len <= list_size) {
4736 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
4737 memcpy(list + prefix_len, name, name_len);
4738 list[prefix_len + name_len] = '\0';
4739 }
4740 return total_len;
4741}
4742
4743static int ocfs2_xattr_trusted_get(struct inode *inode, const char *name,
4744 void *buffer, size_t size)
4745{
4746 if (strcmp(name, "") == 0)
4747 return -EINVAL;
4748 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, name,
4749 buffer, size);
4750}
4751
4752static int ocfs2_xattr_trusted_set(struct inode *inode, const char *name,
4753 const void *value, size_t size, int flags)
4754{
4755 if (strcmp(name, "") == 0)
4756 return -EINVAL;
4757
4758 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, name, value,
4759 size, flags);
4760}
4761
4762struct xattr_handler ocfs2_xattr_trusted_handler = {
4763 .prefix = XATTR_TRUSTED_PREFIX,
4764 .list = ocfs2_xattr_trusted_list,
4765 .get = ocfs2_xattr_trusted_get,
4766 .set = ocfs2_xattr_trusted_set,
4767};
4768
4769
4770/*
4771 * 'user' attributes support
4772 */
4773
4774#define XATTR_USER_PREFIX "user."
4775
4776static size_t ocfs2_xattr_user_list(struct inode *inode, char *list,
4777 size_t list_size, const char *name,
4778 size_t name_len)
4779{
4780 const size_t prefix_len = sizeof(XATTR_USER_PREFIX) - 1;
4781 const size_t total_len = prefix_len + name_len + 1;
4782 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4783
4784 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4785 return 0;
4786
4787 if (list && total_len <= list_size) {
4788 memcpy(list, XATTR_USER_PREFIX, prefix_len);
4789 memcpy(list + prefix_len, name, name_len);
4790 list[prefix_len + name_len] = '\0';
4791 }
4792 return total_len;
4793}
4794
4795static int ocfs2_xattr_user_get(struct inode *inode, const char *name,
4796 void *buffer, size_t size)
4797{
4798 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4799
4800 if (strcmp(name, "") == 0)
4801 return -EINVAL;
4802 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4803 return -EOPNOTSUPP;
4804 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name,
4805 buffer, size);
4806}
4807
4808static int ocfs2_xattr_user_set(struct inode *inode, const char *name,
4809 const void *value, size_t size, int flags)
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
4818 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, name, value,
4819 size, flags);
4820}
4821
4822struct xattr_handler ocfs2_xattr_user_handler = {
4823 .prefix = XATTR_USER_PREFIX,
4824 .list = ocfs2_xattr_user_list,
4825 .get = ocfs2_xattr_user_get,
4826 .set = ocfs2_xattr_user_set,
4827};