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