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