blob: 17028aa7bc26ca364ebbfe54198f36dacf5b29d3 [file] [log] [blame]
Tao Maf56654c2008-08-18 17:38:48 +08001/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * xattr.c
5 *
Tiger Yangc3cb6822008-10-23 16:33:03 +08006 * Copyright (C) 2004, 2008 Oracle. All rights reserved.
Tao Maf56654c2008-08-18 17:38:48 +08007 *
Tiger Yangcf1d6c72008-08-18 17:11:00 +08008 * CREDITS:
Tiger Yangc3cb6822008-10-23 16:33:03 +08009 * Lots of code in this file is copy from linux/fs/ext3/xattr.c.
10 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
Tiger Yangcf1d6c72008-08-18 17:11:00 +080011 *
Tao Maf56654c2008-08-18 17:38:48 +080012 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public
Tiger Yangc3cb6822008-10-23 16:33:03 +080014 * License version 2 as published by the Free Software Foundation.
Tao Maf56654c2008-08-18 17:38:48 +080015 *
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.
Tao Maf56654c2008-08-18 17:38:48 +080020 */
21
Tiger Yangcf1d6c72008-08-18 17:11:00 +080022#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>
Tao Ma01225592008-08-18 17:38:53 +080034#include <linux/sort.h>
Mark Fasheh99219ae2008-10-07 14:52:59 -070035#include <linux/init.h>
36#include <linux/module.h>
37#include <linux/string.h>
Tiger Yang923f7f32008-11-14 11:16:27 +080038#include <linux/security.h>
Tiger Yangcf1d6c72008-08-18 17:11:00 +080039
Tao Maf56654c2008-08-18 17:38:48 +080040#define MLOG_MASK_PREFIX ML_XATTR
41#include <cluster/masklog.h>
42
43#include "ocfs2.h"
44#include "alloc.h"
Joel Beckerd6b32bb2008-10-17 14:55:01 -070045#include "blockcheck.h"
Tao Maf56654c2008-08-18 17:38:48 +080046#include "dlmglue.h"
47#include "file.h"
Tiger Yangcf1d6c72008-08-18 17:11:00 +080048#include "symlink.h"
49#include "sysfile.h"
Tao Maf56654c2008-08-18 17:38:48 +080050#include "inode.h"
51#include "journal.h"
52#include "ocfs2_fs.h"
53#include "suballoc.h"
54#include "uptodate.h"
55#include "buffer_head_io.h"
Tao Ma0c044f02008-08-18 17:38:50 +080056#include "super.h"
Tiger Yangcf1d6c72008-08-18 17:11:00 +080057#include "xattr.h"
58
59
60struct ocfs2_xattr_def_value_root {
61 struct ocfs2_xattr_value_root xv;
62 struct ocfs2_extent_rec er;
63};
64
Tao Ma0c044f02008-08-18 17:38:50 +080065struct ocfs2_xattr_bucket {
Joel Beckerba937122008-10-24 19:13:20 -070066 /* The inode these xattrs are associated with */
67 struct inode *bu_inode;
68
69 /* The actual buffers that make up the bucket */
Joel Becker4ac60322008-10-18 19:11:42 -070070 struct buffer_head *bu_bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
Joel Beckerba937122008-10-24 19:13:20 -070071
72 /* How many blocks make up one bucket for this filesystem */
73 int bu_blocks;
Tao Ma0c044f02008-08-18 17:38:50 +080074};
75
Tao Ma78f30c32008-11-12 08:27:00 +080076struct ocfs2_xattr_set_ctxt {
Tao Ma85db90e2008-11-12 08:27:01 +080077 handle_t *handle;
Tao Ma78f30c32008-11-12 08:27:00 +080078 struct ocfs2_alloc_context *meta_ac;
79 struct ocfs2_alloc_context *data_ac;
80 struct ocfs2_cached_dealloc_ctxt dealloc;
81};
82
Tiger Yangcf1d6c72008-08-18 17:11:00 +080083#define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
84#define OCFS2_XATTR_INLINE_SIZE 80
Tiger Yang534eadd2008-11-14 11:16:41 +080085#define OCFS2_XATTR_FREE_IN_IBODY (OCFS2_MIN_XATTR_INLINE_SIZE \
86 - sizeof(struct ocfs2_xattr_header) \
87 - sizeof(__u32))
Tiger Yang89c38bd2008-11-14 11:17:41 +080088#define OCFS2_XATTR_FREE_IN_BLOCK(ptr) ((ptr)->i_sb->s_blocksize \
89 - sizeof(struct ocfs2_xattr_block) \
90 - sizeof(struct ocfs2_xattr_header) \
91 - sizeof(__u32))
Tiger Yangcf1d6c72008-08-18 17:11:00 +080092
93static struct ocfs2_xattr_def_value_root def_xv = {
94 .xv.xr_list.l_count = cpu_to_le16(1),
95};
96
97struct xattr_handler *ocfs2_xattr_handlers[] = {
98 &ocfs2_xattr_user_handler,
Tiger Yang929fb012008-11-14 11:17:04 +080099#ifdef CONFIG_OCFS2_FS_POSIX_ACL
100 &ocfs2_xattr_acl_access_handler,
101 &ocfs2_xattr_acl_default_handler,
102#endif
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800103 &ocfs2_xattr_trusted_handler,
Tiger Yang923f7f32008-11-14 11:16:27 +0800104 &ocfs2_xattr_security_handler,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800105 NULL
106};
107
Tiger Yangc988fd02008-10-23 16:34:44 +0800108static struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800109 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
Tiger Yang929fb012008-11-14 11:17:04 +0800110#ifdef CONFIG_OCFS2_FS_POSIX_ACL
111 [OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS]
112 = &ocfs2_xattr_acl_access_handler,
113 [OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT]
114 = &ocfs2_xattr_acl_default_handler,
115#endif
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800116 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
Tiger Yang923f7f32008-11-14 11:16:27 +0800117 [OCFS2_XATTR_INDEX_SECURITY] = &ocfs2_xattr_security_handler,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800118};
119
120struct ocfs2_xattr_info {
121 int name_index;
122 const char *name;
123 const void *value;
124 size_t value_len;
125};
126
127struct ocfs2_xattr_search {
128 struct buffer_head *inode_bh;
129 /*
130 * xattr_bh point to the block buffer head which has extended attribute
131 * when extended attribute in inode, xattr_bh is equal to inode_bh.
132 */
133 struct buffer_head *xattr_bh;
134 struct ocfs2_xattr_header *header;
Joel Beckerba937122008-10-24 19:13:20 -0700135 struct ocfs2_xattr_bucket *bucket;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800136 void *base;
137 void *end;
138 struct ocfs2_xattr_entry *here;
139 int not_found;
140};
141
Tao Ma589dc262008-08-18 17:38:51 +0800142static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
143 struct ocfs2_xattr_header *xh,
144 int index,
145 int *block_off,
146 int *new_offset);
147
Joel Becker54f443f2008-10-20 18:43:07 -0700148static int ocfs2_xattr_block_find(struct inode *inode,
149 int name_index,
150 const char *name,
151 struct ocfs2_xattr_search *xs);
Tao Ma589dc262008-08-18 17:38:51 +0800152static int ocfs2_xattr_index_block_find(struct inode *inode,
153 struct buffer_head *root_bh,
154 int name_index,
155 const char *name,
156 struct ocfs2_xattr_search *xs);
157
Tao Ma0c044f02008-08-18 17:38:50 +0800158static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
159 struct ocfs2_xattr_tree_root *xt,
160 char *buffer,
161 size_t buffer_size);
162
Tao Ma01225592008-08-18 17:38:53 +0800163static int ocfs2_xattr_create_index_block(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +0800164 struct ocfs2_xattr_search *xs,
165 struct ocfs2_xattr_set_ctxt *ctxt);
Tao Ma01225592008-08-18 17:38:53 +0800166
167static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
168 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +0800169 struct ocfs2_xattr_search *xs,
170 struct ocfs2_xattr_set_ctxt *ctxt);
Tao Ma01225592008-08-18 17:38:53 +0800171
Tao Maa3944252008-08-18 17:38:54 +0800172static int ocfs2_delete_xattr_index_block(struct inode *inode,
173 struct buffer_head *xb_bh);
Joel Beckerc58b6032008-11-26 13:36:24 -0800174static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
175 u64 src_blk, u64 last_blk, u64 to_blk,
176 unsigned int start_bucket,
177 u32 *first_hash);
Tao Maa3944252008-08-18 17:38:54 +0800178
Tiger Yang0030e002008-10-23 16:33:33 +0800179static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
180{
181 return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
182}
183
184static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
185{
186 return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
187}
188
189static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb)
190{
191 u16 len = sb->s_blocksize -
192 offsetof(struct ocfs2_xattr_header, xh_entries);
193
194 return len / sizeof(struct ocfs2_xattr_entry);
195}
196
Joel Becker9c7759a2008-10-24 16:21:03 -0700197#define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
Joel Becker51def392008-10-24 16:57:21 -0700198#define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
Joel Becker3e632942008-10-24 17:04:49 -0700199#define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
Joel Becker9c7759a2008-10-24 16:21:03 -0700200
Joel Beckerba937122008-10-24 19:13:20 -0700201static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
Joel Becker6dde41d2008-10-24 17:16:48 -0700202{
Joel Beckerba937122008-10-24 19:13:20 -0700203 struct ocfs2_xattr_bucket *bucket;
204 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Joel Becker6dde41d2008-10-24 17:16:48 -0700205
Joel Beckerba937122008-10-24 19:13:20 -0700206 BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
207
208 bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
209 if (bucket) {
210 bucket->bu_inode = inode;
211 bucket->bu_blocks = blks;
212 }
213
214 return bucket;
215}
216
217static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
218{
219 int i;
220
221 for (i = 0; i < bucket->bu_blocks; i++) {
Joel Becker6dde41d2008-10-24 17:16:48 -0700222 brelse(bucket->bu_bhs[i]);
223 bucket->bu_bhs[i] = NULL;
224 }
225}
226
Joel Beckerba937122008-10-24 19:13:20 -0700227static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
228{
229 if (bucket) {
230 ocfs2_xattr_bucket_relse(bucket);
231 bucket->bu_inode = NULL;
232 kfree(bucket);
233 }
234}
235
Joel Becker784b8162008-10-24 17:33:40 -0700236/*
237 * A bucket that has never been written to disk doesn't need to be
238 * read. We just need the buffer_heads. Don't call this for
239 * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
240 * them fully.
241 */
Joel Beckerba937122008-10-24 19:13:20 -0700242static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
Joel Becker784b8162008-10-24 17:33:40 -0700243 u64 xb_blkno)
244{
245 int i, rc = 0;
Joel Becker784b8162008-10-24 17:33:40 -0700246
Joel Beckerba937122008-10-24 19:13:20 -0700247 for (i = 0; i < bucket->bu_blocks; i++) {
248 bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
249 xb_blkno + i);
Joel Becker784b8162008-10-24 17:33:40 -0700250 if (!bucket->bu_bhs[i]) {
251 rc = -EIO;
252 mlog_errno(rc);
253 break;
254 }
255
Tao Ma757055a2008-11-06 08:10:48 +0800256 if (!ocfs2_buffer_uptodate(bucket->bu_inode,
257 bucket->bu_bhs[i]))
258 ocfs2_set_new_buffer_uptodate(bucket->bu_inode,
259 bucket->bu_bhs[i]);
Joel Becker784b8162008-10-24 17:33:40 -0700260 }
261
262 if (rc)
Joel Beckerba937122008-10-24 19:13:20 -0700263 ocfs2_xattr_bucket_relse(bucket);
Joel Becker784b8162008-10-24 17:33:40 -0700264 return rc;
265}
266
267/* Read the xattr bucket at xb_blkno */
Joel Beckerba937122008-10-24 19:13:20 -0700268static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
Joel Becker784b8162008-10-24 17:33:40 -0700269 u64 xb_blkno)
270{
Joel Beckerba937122008-10-24 19:13:20 -0700271 int rc;
Joel Becker784b8162008-10-24 17:33:40 -0700272
Joel Beckerba937122008-10-24 19:13:20 -0700273 rc = ocfs2_read_blocks(bucket->bu_inode, xb_blkno,
Joel Becker970e4932008-11-13 14:49:19 -0800274 bucket->bu_blocks, bucket->bu_bhs, 0,
275 NULL);
Joel Becker4d0e2142008-12-05 11:19:37 -0800276 if (!rc) {
277 rc = ocfs2_validate_meta_ecc_bhs(bucket->bu_inode->i_sb,
278 bucket->bu_bhs,
279 bucket->bu_blocks,
280 &bucket_xh(bucket)->xh_check);
281 if (rc)
282 mlog_errno(rc);
283 }
284
Joel Becker784b8162008-10-24 17:33:40 -0700285 if (rc)
Joel Beckerba937122008-10-24 19:13:20 -0700286 ocfs2_xattr_bucket_relse(bucket);
Joel Becker784b8162008-10-24 17:33:40 -0700287 return rc;
288}
289
Joel Becker1224be02008-10-24 18:47:33 -0700290static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
Joel Becker1224be02008-10-24 18:47:33 -0700291 struct ocfs2_xattr_bucket *bucket,
292 int type)
293{
294 int i, rc = 0;
Joel Becker1224be02008-10-24 18:47:33 -0700295
Joel Beckerba937122008-10-24 19:13:20 -0700296 for (i = 0; i < bucket->bu_blocks; i++) {
297 rc = ocfs2_journal_access(handle, bucket->bu_inode,
Joel Becker1224be02008-10-24 18:47:33 -0700298 bucket->bu_bhs[i], type);
299 if (rc) {
300 mlog_errno(rc);
301 break;
302 }
303 }
304
305 return rc;
306}
307
308static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
Joel Becker1224be02008-10-24 18:47:33 -0700309 struct ocfs2_xattr_bucket *bucket)
310{
Joel Beckerba937122008-10-24 19:13:20 -0700311 int i;
Joel Becker1224be02008-10-24 18:47:33 -0700312
Joel Becker4d0e2142008-12-05 11:19:37 -0800313 ocfs2_compute_meta_ecc_bhs(bucket->bu_inode->i_sb,
314 bucket->bu_bhs, bucket->bu_blocks,
315 &bucket_xh(bucket)->xh_check);
316
Joel Beckerba937122008-10-24 19:13:20 -0700317 for (i = 0; i < bucket->bu_blocks; i++)
Joel Becker1224be02008-10-24 18:47:33 -0700318 ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
319}
320
Joel Beckerba937122008-10-24 19:13:20 -0700321static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
Joel Becker4980c6d2008-10-24 18:54:43 -0700322 struct ocfs2_xattr_bucket *src)
323{
324 int i;
Joel Beckerba937122008-10-24 19:13:20 -0700325 int blocksize = src->bu_inode->i_sb->s_blocksize;
Joel Becker4980c6d2008-10-24 18:54:43 -0700326
Joel Beckerba937122008-10-24 19:13:20 -0700327 BUG_ON(dest->bu_blocks != src->bu_blocks);
328 BUG_ON(dest->bu_inode != src->bu_inode);
329
330 for (i = 0; i < src->bu_blocks; i++) {
Joel Becker4980c6d2008-10-24 18:54:43 -0700331 memcpy(bucket_block(dest, i), bucket_block(src, i),
332 blocksize);
333 }
334}
Joel Becker1224be02008-10-24 18:47:33 -0700335
Joel Becker4ae1d692008-11-13 14:49:18 -0800336static int ocfs2_validate_xattr_block(struct super_block *sb,
337 struct buffer_head *bh)
338{
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700339 int rc;
Joel Becker4ae1d692008-11-13 14:49:18 -0800340 struct ocfs2_xattr_block *xb =
341 (struct ocfs2_xattr_block *)bh->b_data;
342
343 mlog(0, "Validating xattr block %llu\n",
344 (unsigned long long)bh->b_blocknr);
345
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700346 BUG_ON(!buffer_uptodate(bh));
347
348 /*
349 * If the ecc fails, we return the error but otherwise
350 * leave the filesystem running. We know any error is
351 * local to this block.
352 */
353 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &xb->xb_check);
354 if (rc)
355 return rc;
356
357 /*
358 * Errors after here are fatal
359 */
360
Joel Becker4ae1d692008-11-13 14:49:18 -0800361 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
362 ocfs2_error(sb,
363 "Extended attribute block #%llu has bad "
364 "signature %.*s",
365 (unsigned long long)bh->b_blocknr, 7,
366 xb->xb_signature);
367 return -EINVAL;
368 }
369
370 if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) {
371 ocfs2_error(sb,
372 "Extended attribute block #%llu has an "
373 "invalid xb_blkno of %llu",
374 (unsigned long long)bh->b_blocknr,
375 (unsigned long long)le64_to_cpu(xb->xb_blkno));
376 return -EINVAL;
377 }
378
379 if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) {
380 ocfs2_error(sb,
381 "Extended attribute block #%llu has an invalid "
382 "xb_fs_generation of #%u",
383 (unsigned long long)bh->b_blocknr,
384 le32_to_cpu(xb->xb_fs_generation));
385 return -EINVAL;
386 }
387
388 return 0;
389}
390
391static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno,
392 struct buffer_head **bh)
393{
394 int rc;
395 struct buffer_head *tmp = *bh;
396
Joel Becker970e4932008-11-13 14:49:19 -0800397 rc = ocfs2_read_block(inode, xb_blkno, &tmp,
398 ocfs2_validate_xattr_block);
Joel Becker4ae1d692008-11-13 14:49:18 -0800399
400 /* If ocfs2_read_block() got us a new bh, pass it up. */
401 if (!rc && !*bh)
402 *bh = tmp;
403
404 return rc;
405}
406
Tao Ma936b8832008-10-09 23:06:14 +0800407static inline const char *ocfs2_xattr_prefix(int name_index)
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800408{
409 struct xattr_handler *handler = NULL;
410
411 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
412 handler = ocfs2_xattr_handler_map[name_index];
413
Tao Ma936b8832008-10-09 23:06:14 +0800414 return handler ? handler->prefix : NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800415}
416
Mark Fasheh40daa162008-10-07 14:31:42 -0700417static u32 ocfs2_xattr_name_hash(struct inode *inode,
Tao Ma2057e5c2008-10-09 23:06:13 +0800418 const char *name,
Mark Fasheh40daa162008-10-07 14:31:42 -0700419 int name_len)
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800420{
421 /* Get hash value of uuid from super block */
422 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
423 int i;
424
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800425 /* hash extended attribute name */
426 for (i = 0; i < name_len; i++) {
427 hash = (hash << OCFS2_HASH_SHIFT) ^
428 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
429 *name++;
430 }
431
432 return hash;
433}
434
435/*
436 * ocfs2_xattr_hash_entry()
437 *
438 * Compute the hash of an extended attribute.
439 */
440static void ocfs2_xattr_hash_entry(struct inode *inode,
441 struct ocfs2_xattr_header *header,
442 struct ocfs2_xattr_entry *entry)
443{
444 u32 hash = 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800445 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800446
Tao Ma2057e5c2008-10-09 23:06:13 +0800447 hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800448 entry->xe_name_hash = cpu_to_le32(hash);
449
450 return;
451}
Tao Maf56654c2008-08-18 17:38:48 +0800452
Tiger Yang534eadd2008-11-14 11:16:41 +0800453static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
454{
455 int size = 0;
456
457 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
458 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
459 else
460 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
461 size += sizeof(struct ocfs2_xattr_entry);
462
463 return size;
464}
465
466int ocfs2_calc_security_init(struct inode *dir,
467 struct ocfs2_security_xattr_info *si,
468 int *want_clusters,
469 int *xattr_credits,
470 struct ocfs2_alloc_context **xattr_ac)
471{
472 int ret = 0;
473 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
474 int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
475 si->value_len);
476
477 /*
478 * The max space of security xattr taken inline is
479 * 256(name) + 80(value) + 16(entry) = 352 bytes,
480 * So reserve one metadata block for it is ok.
481 */
482 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
483 s_size > OCFS2_XATTR_FREE_IN_IBODY) {
484 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
485 if (ret) {
486 mlog_errno(ret);
487 return ret;
488 }
489 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
490 }
491
492 /* reserve clusters for xattr value which will be set in B tree*/
493 if (si->value_len > OCFS2_XATTR_INLINE_SIZE)
494 *want_clusters += ocfs2_clusters_for_bytes(dir->i_sb,
495 si->value_len);
496 return ret;
497}
498
Tiger Yang89c38bd2008-11-14 11:17:41 +0800499int ocfs2_calc_xattr_init(struct inode *dir,
500 struct buffer_head *dir_bh,
501 int mode,
502 struct ocfs2_security_xattr_info *si,
503 int *want_clusters,
504 int *xattr_credits,
505 struct ocfs2_alloc_context **xattr_ac)
506{
507 int ret = 0;
508 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
509 int s_size = 0;
510 int a_size = 0;
511 int acl_len = 0;
512
513 if (si->enable)
514 s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
515 si->value_len);
516
517 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
518 acl_len = ocfs2_xattr_get_nolock(dir, dir_bh,
519 OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
520 "", NULL, 0);
521 if (acl_len > 0) {
522 a_size = ocfs2_xattr_entry_real_size(0, acl_len);
523 if (S_ISDIR(mode))
524 a_size <<= 1;
525 } else if (acl_len != 0 && acl_len != -ENODATA) {
526 mlog_errno(ret);
527 return ret;
528 }
529 }
530
531 if (!(s_size + a_size))
532 return ret;
533
534 /*
535 * The max space of security xattr taken inline is
536 * 256(name) + 80(value) + 16(entry) = 352 bytes,
537 * The max space of acl xattr taken inline is
538 * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
539 * when blocksize = 512, may reserve one more cluser for
540 * xattr bucket, otherwise reserve one metadata block
541 * for them is ok.
542 */
543 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
544 (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) {
545 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
546 if (ret) {
547 mlog_errno(ret);
548 return ret;
549 }
550 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
551 }
552
553 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
554 (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
555 *want_clusters += 1;
556 *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
557 }
558
559 /* reserve clusters for xattr value which will be set in B tree*/
560 if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE)
561 *want_clusters += ocfs2_clusters_for_bytes(dir->i_sb,
562 si->value_len);
563 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
564 acl_len > OCFS2_XATTR_INLINE_SIZE) {
565 *want_clusters += ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
566 if (S_ISDIR(mode))
567 *want_clusters += ocfs2_clusters_for_bytes(dir->i_sb,
568 acl_len);
569 }
570
571 return ret;
572}
573
Tao Maf56654c2008-08-18 17:38:48 +0800574static int ocfs2_xattr_extend_allocation(struct inode *inode,
575 u32 clusters_to_add,
Joel Becker19b801f2008-12-09 14:36:50 -0800576 struct ocfs2_xattr_value_buf *vb,
Tao Ma78f30c32008-11-12 08:27:00 +0800577 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800578{
579 int status = 0;
Tao Ma85db90e2008-11-12 08:27:01 +0800580 handle_t *handle = ctxt->handle;
Tao Maf56654c2008-08-18 17:38:48 +0800581 enum ocfs2_alloc_restarted why;
582 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Joel Becker19b801f2008-12-09 14:36:50 -0800583 u32 prev_clusters, logical_start = le32_to_cpu(vb->vb_xv->xr_clusters);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700584 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +0800585
586 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
587
Joel Becker19b801f2008-12-09 14:36:50 -0800588 ocfs2_init_xattr_value_extent_tree(&et, inode, vb);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700589
Joel Becker19b801f2008-12-09 14:36:50 -0800590 status = vb->vb_access(handle, inode, vb->vb_bh,
Joel Becker2a50a742008-12-09 14:24:33 -0800591 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Maf56654c2008-08-18 17:38:48 +0800592 if (status < 0) {
593 mlog_errno(status);
594 goto leave;
595 }
596
Joel Becker19b801f2008-12-09 14:36:50 -0800597 prev_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
Tao Maf56654c2008-08-18 17:38:48 +0800598 status = ocfs2_add_clusters_in_btree(osb,
599 inode,
600 &logical_start,
601 clusters_to_add,
602 0,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700603 &et,
Tao Maf56654c2008-08-18 17:38:48 +0800604 handle,
Tao Ma78f30c32008-11-12 08:27:00 +0800605 ctxt->data_ac,
606 ctxt->meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700607 &why);
Tao Ma85db90e2008-11-12 08:27:01 +0800608 if (status < 0) {
609 mlog_errno(status);
Tao Maf56654c2008-08-18 17:38:48 +0800610 goto leave;
611 }
612
Joel Becker19b801f2008-12-09 14:36:50 -0800613 status = ocfs2_journal_dirty(handle, vb->vb_bh);
Tao Maf56654c2008-08-18 17:38:48 +0800614 if (status < 0) {
615 mlog_errno(status);
616 goto leave;
617 }
618
Joel Becker19b801f2008-12-09 14:36:50 -0800619 clusters_to_add -= le32_to_cpu(vb->vb_xv->xr_clusters) - prev_clusters;
Tao Maf56654c2008-08-18 17:38:48 +0800620
Tao Ma85db90e2008-11-12 08:27:01 +0800621 /*
622 * We should have already allocated enough space before the transaction,
623 * so no need to restart.
624 */
625 BUG_ON(why != RESTART_NONE || clusters_to_add);
Tao Maf56654c2008-08-18 17:38:48 +0800626
627leave:
Tao Maf56654c2008-08-18 17:38:48 +0800628
629 return status;
630}
631
632static int __ocfs2_remove_xattr_range(struct inode *inode,
Joel Beckerd72cc722008-12-09 14:30:41 -0800633 struct ocfs2_xattr_value_buf *vb,
Tao Maf56654c2008-08-18 17:38:48 +0800634 u32 cpos, u32 phys_cpos, u32 len,
Tao Ma78f30c32008-11-12 08:27:00 +0800635 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800636{
637 int ret;
638 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
Tao Ma85db90e2008-11-12 08:27:01 +0800639 handle_t *handle = ctxt->handle;
Joel Beckerf99b9b72008-08-20 19:36:33 -0700640 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +0800641
Joel Beckerd72cc722008-12-09 14:30:41 -0800642 ocfs2_init_xattr_value_extent_tree(&et, inode, vb);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700643
Joel Beckerd72cc722008-12-09 14:30:41 -0800644 ret = vb->vb_access(handle, inode, vb->vb_bh,
645 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Maf56654c2008-08-18 17:38:48 +0800646 if (ret) {
647 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800648 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800649 }
650
Tao Ma78f30c32008-11-12 08:27:00 +0800651 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, ctxt->meta_ac,
652 &ctxt->dealloc);
Tao Maf56654c2008-08-18 17:38:48 +0800653 if (ret) {
654 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800655 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800656 }
657
Joel Beckerd72cc722008-12-09 14:30:41 -0800658 le32_add_cpu(&vb->vb_xv->xr_clusters, -len);
Tao Maf56654c2008-08-18 17:38:48 +0800659
Joel Beckerd72cc722008-12-09 14:30:41 -0800660 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
Tao Maf56654c2008-08-18 17:38:48 +0800661 if (ret) {
662 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800663 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800664 }
665
Tao Ma78f30c32008-11-12 08:27:00 +0800666 ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc, phys_blkno, len);
Tao Maf56654c2008-08-18 17:38:48 +0800667 if (ret)
668 mlog_errno(ret);
669
Tao Maf56654c2008-08-18 17:38:48 +0800670out:
Tao Maf56654c2008-08-18 17:38:48 +0800671 return ret;
672}
673
674static int ocfs2_xattr_shrink_size(struct inode *inode,
675 u32 old_clusters,
676 u32 new_clusters,
Joel Becker19b801f2008-12-09 14:36:50 -0800677 struct ocfs2_xattr_value_buf *vb,
Tao Ma78f30c32008-11-12 08:27:00 +0800678 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800679{
680 int ret = 0;
681 u32 trunc_len, cpos, phys_cpos, alloc_size;
682 u64 block;
Tao Maf56654c2008-08-18 17:38:48 +0800683
684 if (old_clusters <= new_clusters)
685 return 0;
686
687 cpos = new_clusters;
688 trunc_len = old_clusters - new_clusters;
689 while (trunc_len) {
690 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
Joel Beckerd72cc722008-12-09 14:30:41 -0800691 &alloc_size,
Joel Becker19b801f2008-12-09 14:36:50 -0800692 &vb->vb_xv->xr_list);
Tao Maf56654c2008-08-18 17:38:48 +0800693 if (ret) {
694 mlog_errno(ret);
695 goto out;
696 }
697
698 if (alloc_size > trunc_len)
699 alloc_size = trunc_len;
700
Joel Becker19b801f2008-12-09 14:36:50 -0800701 ret = __ocfs2_remove_xattr_range(inode, vb, cpos,
Tao Maf56654c2008-08-18 17:38:48 +0800702 phys_cpos, alloc_size,
Tao Ma78f30c32008-11-12 08:27:00 +0800703 ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800704 if (ret) {
705 mlog_errno(ret);
706 goto out;
707 }
708
709 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
710 ocfs2_remove_xattr_clusters_from_cache(inode, block,
711 alloc_size);
712 cpos += alloc_size;
713 trunc_len -= alloc_size;
714 }
715
716out:
Tao Maf56654c2008-08-18 17:38:48 +0800717 return ret;
718}
719
720static int ocfs2_xattr_value_truncate(struct inode *inode,
Joel Beckerb3e5d372008-12-09 15:01:04 -0800721 struct ocfs2_xattr_value_buf *vb,
Tao Ma78f30c32008-11-12 08:27:00 +0800722 int len,
723 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800724{
725 int ret;
726 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
Joel Beckerb3e5d372008-12-09 15:01:04 -0800727 u32 old_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
Tao Maf56654c2008-08-18 17:38:48 +0800728
729 if (new_clusters == old_clusters)
730 return 0;
731
732 if (new_clusters > old_clusters)
733 ret = ocfs2_xattr_extend_allocation(inode,
734 new_clusters - old_clusters,
Joel Beckerb3e5d372008-12-09 15:01:04 -0800735 vb, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800736 else
737 ret = ocfs2_xattr_shrink_size(inode,
738 old_clusters, new_clusters,
Joel Beckerb3e5d372008-12-09 15:01:04 -0800739 vb, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800740
741 return ret;
742}
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800743
Tao Ma936b8832008-10-09 23:06:14 +0800744static int ocfs2_xattr_list_entry(char *buffer, size_t size,
745 size_t *result, const char *prefix,
746 const char *name, int name_len)
747{
748 char *p = buffer + *result;
749 int prefix_len = strlen(prefix);
750 int total_len = prefix_len + name_len + 1;
751
752 *result += total_len;
753
754 /* we are just looking for how big our buffer needs to be */
755 if (!size)
756 return 0;
757
758 if (*result > size)
759 return -ERANGE;
760
761 memcpy(p, prefix, prefix_len);
762 memcpy(p + prefix_len, name, name_len);
763 p[prefix_len + name_len] = '\0';
764
765 return 0;
766}
767
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800768static int ocfs2_xattr_list_entries(struct inode *inode,
769 struct ocfs2_xattr_header *header,
770 char *buffer, size_t buffer_size)
771{
Tao Ma936b8832008-10-09 23:06:14 +0800772 size_t result = 0;
773 int i, type, ret;
774 const char *prefix, *name;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800775
776 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
777 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
Tao Ma936b8832008-10-09 23:06:14 +0800778 type = ocfs2_xattr_get_type(entry);
779 prefix = ocfs2_xattr_prefix(type);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800780
Tao Ma936b8832008-10-09 23:06:14 +0800781 if (prefix) {
782 name = (const char *)header +
783 le16_to_cpu(entry->xe_name_offset);
784
785 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
786 &result, prefix, name,
787 entry->xe_name_len);
788 if (ret)
789 return ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800790 }
791 }
792
Tao Ma936b8832008-10-09 23:06:14 +0800793 return result;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800794}
795
796static int ocfs2_xattr_ibody_list(struct inode *inode,
797 struct ocfs2_dinode *di,
798 char *buffer,
799 size_t buffer_size)
800{
801 struct ocfs2_xattr_header *header = NULL;
802 struct ocfs2_inode_info *oi = OCFS2_I(inode);
803 int ret = 0;
804
805 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
806 return ret;
807
808 header = (struct ocfs2_xattr_header *)
809 ((void *)di + inode->i_sb->s_blocksize -
810 le16_to_cpu(di->i_xattr_inline_size));
811
812 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
813
814 return ret;
815}
816
817static int ocfs2_xattr_block_list(struct inode *inode,
818 struct ocfs2_dinode *di,
819 char *buffer,
820 size_t buffer_size)
821{
822 struct buffer_head *blk_bh = NULL;
Tao Ma0c044f02008-08-18 17:38:50 +0800823 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800824 int ret = 0;
825
826 if (!di->i_xattr_loc)
827 return ret;
828
Joel Becker4ae1d692008-11-13 14:49:18 -0800829 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
830 &blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800831 if (ret < 0) {
832 mlog_errno(ret);
833 return ret;
834 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800835
Tao Ma0c044f02008-08-18 17:38:50 +0800836 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Ma0c044f02008-08-18 17:38:50 +0800837 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
838 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
839 ret = ocfs2_xattr_list_entries(inode, header,
840 buffer, buffer_size);
841 } else {
842 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
843 ret = ocfs2_xattr_tree_list_index_block(inode, xt,
844 buffer, buffer_size);
845 }
Joel Becker4ae1d692008-11-13 14:49:18 -0800846
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800847 brelse(blk_bh);
848
849 return ret;
850}
851
852ssize_t ocfs2_listxattr(struct dentry *dentry,
853 char *buffer,
854 size_t size)
855{
856 int ret = 0, i_ret = 0, b_ret = 0;
857 struct buffer_head *di_bh = NULL;
858 struct ocfs2_dinode *di = NULL;
859 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
860
Tiger Yang8154da32008-08-18 17:11:46 +0800861 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
862 return -EOPNOTSUPP;
863
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800864 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
865 return ret;
866
867 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
868 if (ret < 0) {
869 mlog_errno(ret);
870 return ret;
871 }
872
873 di = (struct ocfs2_dinode *)di_bh->b_data;
874
875 down_read(&oi->ip_xattr_sem);
876 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
877 if (i_ret < 0)
878 b_ret = 0;
879 else {
880 if (buffer) {
881 buffer += i_ret;
882 size -= i_ret;
883 }
884 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
885 buffer, size);
886 if (b_ret < 0)
887 i_ret = 0;
888 }
889 up_read(&oi->ip_xattr_sem);
890 ocfs2_inode_unlock(dentry->d_inode, 0);
891
892 brelse(di_bh);
893
894 return i_ret + b_ret;
895}
896
897static int ocfs2_xattr_find_entry(int name_index,
898 const char *name,
899 struct ocfs2_xattr_search *xs)
900{
901 struct ocfs2_xattr_entry *entry;
902 size_t name_len;
903 int i, cmp = 1;
904
905 if (name == NULL)
906 return -EINVAL;
907
908 name_len = strlen(name);
909 entry = xs->here;
910 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
911 cmp = name_index - ocfs2_xattr_get_type(entry);
912 if (!cmp)
913 cmp = name_len - entry->xe_name_len;
914 if (!cmp)
915 cmp = memcmp(name, (xs->base +
916 le16_to_cpu(entry->xe_name_offset)),
917 name_len);
918 if (cmp == 0)
919 break;
920 entry += 1;
921 }
922 xs->here = entry;
923
924 return cmp ? -ENODATA : 0;
925}
926
927static int ocfs2_xattr_get_value_outside(struct inode *inode,
Tao Ma589dc262008-08-18 17:38:51 +0800928 struct ocfs2_xattr_value_root *xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800929 void *buffer,
930 size_t len)
931{
932 u32 cpos, p_cluster, num_clusters, bpc, clusters;
933 u64 blkno;
934 int i, ret = 0;
935 size_t cplen, blocksize;
936 struct buffer_head *bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800937 struct ocfs2_extent_list *el;
938
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800939 el = &xv->xr_list;
940 clusters = le32_to_cpu(xv->xr_clusters);
941 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
942 blocksize = inode->i_sb->s_blocksize;
943
944 cpos = 0;
945 while (cpos < clusters) {
946 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
947 &num_clusters, el);
948 if (ret) {
949 mlog_errno(ret);
950 goto out;
951 }
952
953 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
954 /* Copy ocfs2_xattr_value */
955 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
Joel Becker970e4932008-11-13 14:49:19 -0800956 ret = ocfs2_read_block(inode, blkno, &bh, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800957 if (ret) {
958 mlog_errno(ret);
959 goto out;
960 }
961
962 cplen = len >= blocksize ? blocksize : len;
963 memcpy(buffer, bh->b_data, cplen);
964 len -= cplen;
965 buffer += cplen;
966
967 brelse(bh);
968 bh = NULL;
969 if (len == 0)
970 break;
971 }
972 cpos += num_clusters;
973 }
974out:
975 return ret;
976}
977
978static int ocfs2_xattr_ibody_get(struct inode *inode,
979 int name_index,
980 const char *name,
981 void *buffer,
982 size_t buffer_size,
983 struct ocfs2_xattr_search *xs)
984{
985 struct ocfs2_inode_info *oi = OCFS2_I(inode);
986 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
Tao Ma589dc262008-08-18 17:38:51 +0800987 struct ocfs2_xattr_value_root *xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800988 size_t size;
989 int ret = 0;
990
991 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
992 return -ENODATA;
993
994 xs->end = (void *)di + inode->i_sb->s_blocksize;
995 xs->header = (struct ocfs2_xattr_header *)
996 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
997 xs->base = (void *)xs->header;
998 xs->here = xs->header->xh_entries;
999
1000 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1001 if (ret)
1002 return ret;
1003 size = le64_to_cpu(xs->here->xe_value_size);
1004 if (buffer) {
1005 if (size > buffer_size)
1006 return -ERANGE;
1007 if (ocfs2_xattr_is_local(xs->here)) {
1008 memcpy(buffer, (void *)xs->base +
1009 le16_to_cpu(xs->here->xe_name_offset) +
1010 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
1011 } else {
Tao Ma589dc262008-08-18 17:38:51 +08001012 xv = (struct ocfs2_xattr_value_root *)
1013 (xs->base + le16_to_cpu(
1014 xs->here->xe_name_offset) +
1015 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
1016 ret = ocfs2_xattr_get_value_outside(inode, xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001017 buffer, size);
1018 if (ret < 0) {
1019 mlog_errno(ret);
1020 return ret;
1021 }
1022 }
1023 }
1024
1025 return size;
1026}
1027
1028static int ocfs2_xattr_block_get(struct inode *inode,
1029 int name_index,
1030 const char *name,
1031 void *buffer,
1032 size_t buffer_size,
1033 struct ocfs2_xattr_search *xs)
1034{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001035 struct ocfs2_xattr_block *xb;
Tao Ma589dc262008-08-18 17:38:51 +08001036 struct ocfs2_xattr_value_root *xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001037 size_t size;
Tao Ma589dc262008-08-18 17:38:51 +08001038 int ret = -ENODATA, name_offset, name_len, block_off, i;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001039
Joel Beckerba937122008-10-24 19:13:20 -07001040 xs->bucket = ocfs2_xattr_bucket_new(inode);
1041 if (!xs->bucket) {
1042 ret = -ENOMEM;
1043 mlog_errno(ret);
1044 goto cleanup;
1045 }
Tao Ma589dc262008-08-18 17:38:51 +08001046
Joel Becker54f443f2008-10-20 18:43:07 -07001047 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
1048 if (ret) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001049 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001050 goto cleanup;
1051 }
1052
Tiger Yang6c1e1832008-11-02 19:04:21 +08001053 if (xs->not_found) {
1054 ret = -ENODATA;
1055 goto cleanup;
1056 }
1057
Joel Becker54f443f2008-10-20 18:43:07 -07001058 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001059 size = le64_to_cpu(xs->here->xe_value_size);
1060 if (buffer) {
1061 ret = -ERANGE;
1062 if (size > buffer_size)
1063 goto cleanup;
Tao Ma589dc262008-08-18 17:38:51 +08001064
1065 name_offset = le16_to_cpu(xs->here->xe_name_offset);
1066 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
1067 i = xs->here - xs->header->xh_entries;
1068
1069 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
1070 ret = ocfs2_xattr_bucket_get_name_value(inode,
Joel Beckerba937122008-10-24 19:13:20 -07001071 bucket_xh(xs->bucket),
Tao Ma589dc262008-08-18 17:38:51 +08001072 i,
1073 &block_off,
1074 &name_offset);
Joel Beckerba937122008-10-24 19:13:20 -07001075 xs->base = bucket_block(xs->bucket, block_off);
Tao Ma589dc262008-08-18 17:38:51 +08001076 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001077 if (ocfs2_xattr_is_local(xs->here)) {
1078 memcpy(buffer, (void *)xs->base +
Tao Ma589dc262008-08-18 17:38:51 +08001079 name_offset + name_len, size);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001080 } else {
Tao Ma589dc262008-08-18 17:38:51 +08001081 xv = (struct ocfs2_xattr_value_root *)
1082 (xs->base + name_offset + name_len);
1083 ret = ocfs2_xattr_get_value_outside(inode, xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001084 buffer, size);
1085 if (ret < 0) {
1086 mlog_errno(ret);
1087 goto cleanup;
1088 }
1089 }
1090 }
1091 ret = size;
1092cleanup:
Joel Beckerba937122008-10-24 19:13:20 -07001093 ocfs2_xattr_bucket_free(xs->bucket);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001094
Joel Becker54f443f2008-10-20 18:43:07 -07001095 brelse(xs->xattr_bh);
1096 xs->xattr_bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001097 return ret;
1098}
1099
Tiger Yang4e3e9d02008-11-14 11:16:53 +08001100int ocfs2_xattr_get_nolock(struct inode *inode,
1101 struct buffer_head *di_bh,
Tiger Yang0030e002008-10-23 16:33:33 +08001102 int name_index,
1103 const char *name,
1104 void *buffer,
1105 size_t buffer_size)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001106{
1107 int ret;
1108 struct ocfs2_dinode *di = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001109 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1110 struct ocfs2_xattr_search xis = {
1111 .not_found = -ENODATA,
1112 };
1113 struct ocfs2_xattr_search xbs = {
1114 .not_found = -ENODATA,
1115 };
1116
Tiger Yang8154da32008-08-18 17:11:46 +08001117 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1118 return -EOPNOTSUPP;
1119
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001120 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1121 ret = -ENODATA;
1122
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001123 xis.inode_bh = xbs.inode_bh = di_bh;
1124 di = (struct ocfs2_dinode *)di_bh->b_data;
1125
1126 down_read(&oi->ip_xattr_sem);
1127 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1128 buffer_size, &xis);
Tiger Yang6c1e1832008-11-02 19:04:21 +08001129 if (ret == -ENODATA && di->i_xattr_loc)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001130 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1131 buffer_size, &xbs);
1132 up_read(&oi->ip_xattr_sem);
Tiger Yang4e3e9d02008-11-14 11:16:53 +08001133
1134 return ret;
1135}
1136
1137/* ocfs2_xattr_get()
1138 *
1139 * Copy an extended attribute into the buffer provided.
1140 * Buffer is NULL to compute the size of buffer required.
1141 */
1142static int ocfs2_xattr_get(struct inode *inode,
1143 int name_index,
1144 const char *name,
1145 void *buffer,
1146 size_t buffer_size)
1147{
1148 int ret;
1149 struct buffer_head *di_bh = NULL;
1150
1151 ret = ocfs2_inode_lock(inode, &di_bh, 0);
1152 if (ret < 0) {
1153 mlog_errno(ret);
1154 return ret;
1155 }
1156 ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
1157 name, buffer, buffer_size);
1158
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001159 ocfs2_inode_unlock(inode, 0);
1160
1161 brelse(di_bh);
1162
1163 return ret;
1164}
1165
1166static int __ocfs2_xattr_set_value_outside(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001167 handle_t *handle,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001168 struct ocfs2_xattr_value_root *xv,
1169 const void *value,
1170 int value_len)
1171{
1172 int ret = 0, i, cp_len, credits;
1173 u16 blocksize = inode->i_sb->s_blocksize;
1174 u32 p_cluster, num_clusters;
1175 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1176 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1177 u64 blkno;
1178 struct buffer_head *bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001179
1180 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1181
Tao Ma85db90e2008-11-12 08:27:01 +08001182 /*
1183 * In __ocfs2_xattr_set_value_outside has already been dirtied,
1184 * so we don't need to worry about whether ocfs2_extend_trans
1185 * will create a new transactio for us or not.
1186 */
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001187 credits = clusters * bpc;
Tao Ma85db90e2008-11-12 08:27:01 +08001188 ret = ocfs2_extend_trans(handle, credits);
1189 if (ret) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001190 mlog_errno(ret);
1191 goto out;
1192 }
1193
1194 while (cpos < clusters) {
1195 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1196 &num_clusters, &xv->xr_list);
1197 if (ret) {
1198 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001199 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001200 }
1201
1202 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1203
1204 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
Joel Becker970e4932008-11-13 14:49:19 -08001205 ret = ocfs2_read_block(inode, blkno, &bh, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001206 if (ret) {
1207 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001208 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001209 }
1210
1211 ret = ocfs2_journal_access(handle,
1212 inode,
1213 bh,
1214 OCFS2_JOURNAL_ACCESS_WRITE);
1215 if (ret < 0) {
1216 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001217 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001218 }
1219
1220 cp_len = value_len > blocksize ? blocksize : value_len;
1221 memcpy(bh->b_data, value, cp_len);
1222 value_len -= cp_len;
1223 value += cp_len;
1224 if (cp_len < blocksize)
1225 memset(bh->b_data + cp_len, 0,
1226 blocksize - cp_len);
1227
1228 ret = ocfs2_journal_dirty(handle, bh);
1229 if (ret < 0) {
1230 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001231 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001232 }
1233 brelse(bh);
1234 bh = NULL;
1235
1236 /*
1237 * XXX: do we need to empty all the following
1238 * blocks in this cluster?
1239 */
1240 if (!value_len)
1241 break;
1242 }
1243 cpos += num_clusters;
1244 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001245out:
1246 brelse(bh);
1247
1248 return ret;
1249}
1250
1251static int ocfs2_xattr_cleanup(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001252 handle_t *handle,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001253 struct ocfs2_xattr_info *xi,
1254 struct ocfs2_xattr_search *xs,
Joel Becker512620f2008-12-09 15:58:35 -08001255 struct ocfs2_xattr_value_buf *vb,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001256 size_t offs)
1257{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001258 int ret = 0;
1259 size_t name_len = strlen(xi->name);
1260 void *val = xs->base + offs;
1261 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1262
Joel Becker512620f2008-12-09 15:58:35 -08001263 ret = vb->vb_access(handle, inode, vb->vb_bh,
1264 OCFS2_JOURNAL_ACCESS_WRITE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001265 if (ret) {
1266 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001267 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001268 }
1269 /* Decrease xattr count */
1270 le16_add_cpu(&xs->header->xh_count, -1);
1271 /* Remove the xattr entry and tree root which has already be set*/
1272 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
1273 memset(val, 0, size);
1274
Joel Becker512620f2008-12-09 15:58:35 -08001275 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001276 if (ret < 0)
1277 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001278out:
1279 return ret;
1280}
1281
1282static int ocfs2_xattr_update_entry(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001283 handle_t *handle,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001284 struct ocfs2_xattr_info *xi,
1285 struct ocfs2_xattr_search *xs,
Joel Becker0c748e92008-12-09 15:46:15 -08001286 struct ocfs2_xattr_value_buf *vb,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001287 size_t offs)
1288{
Tao Ma85db90e2008-11-12 08:27:01 +08001289 int ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001290
Joel Becker0c748e92008-12-09 15:46:15 -08001291 ret = vb->vb_access(handle, inode, vb->vb_bh,
1292 OCFS2_JOURNAL_ACCESS_WRITE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001293 if (ret) {
1294 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001295 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001296 }
1297
1298 xs->here->xe_name_offset = cpu_to_le16(offs);
1299 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1300 if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
1301 ocfs2_xattr_set_local(xs->here, 1);
1302 else
1303 ocfs2_xattr_set_local(xs->here, 0);
1304 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1305
Joel Becker0c748e92008-12-09 15:46:15 -08001306 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001307 if (ret < 0)
1308 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001309out:
1310 return ret;
1311}
1312
1313/*
1314 * ocfs2_xattr_set_value_outside()
1315 *
1316 * Set large size value in B tree.
1317 */
1318static int ocfs2_xattr_set_value_outside(struct inode *inode,
1319 struct ocfs2_xattr_info *xi,
1320 struct ocfs2_xattr_search *xs,
Tao Ma78f30c32008-11-12 08:27:00 +08001321 struct ocfs2_xattr_set_ctxt *ctxt,
Joel Becker512620f2008-12-09 15:58:35 -08001322 struct ocfs2_xattr_value_buf *vb,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001323 size_t offs)
1324{
1325 size_t name_len = strlen(xi->name);
1326 void *val = xs->base + offs;
1327 struct ocfs2_xattr_value_root *xv = NULL;
1328 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1329 int ret = 0;
1330
1331 memset(val, 0, size);
1332 memcpy(val, xi->name, name_len);
1333 xv = (struct ocfs2_xattr_value_root *)
1334 (val + OCFS2_XATTR_SIZE(name_len));
1335 xv->xr_clusters = 0;
1336 xv->xr_last_eb_blk = 0;
1337 xv->xr_list.l_tree_depth = 0;
1338 xv->xr_list.l_count = cpu_to_le16(1);
1339 xv->xr_list.l_next_free_rec = 0;
Joel Becker512620f2008-12-09 15:58:35 -08001340 vb->vb_xv = xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001341
Joel Becker512620f2008-12-09 15:58:35 -08001342 ret = ocfs2_xattr_value_truncate(inode, vb, xi->value_len, ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001343 if (ret < 0) {
1344 mlog_errno(ret);
1345 return ret;
1346 }
Joel Becker512620f2008-12-09 15:58:35 -08001347 ret = ocfs2_xattr_update_entry(inode, ctxt->handle, xi, xs, vb, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001348 if (ret < 0) {
1349 mlog_errno(ret);
1350 return ret;
1351 }
Joel Becker512620f2008-12-09 15:58:35 -08001352 ret = __ocfs2_xattr_set_value_outside(inode, ctxt->handle, vb->vb_xv,
Tao Ma85db90e2008-11-12 08:27:01 +08001353 xi->value, xi->value_len);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001354 if (ret < 0)
1355 mlog_errno(ret);
1356
1357 return ret;
1358}
1359
1360/*
1361 * ocfs2_xattr_set_entry_local()
1362 *
1363 * Set, replace or remove extended attribute in local.
1364 */
1365static void ocfs2_xattr_set_entry_local(struct inode *inode,
1366 struct ocfs2_xattr_info *xi,
1367 struct ocfs2_xattr_search *xs,
1368 struct ocfs2_xattr_entry *last,
1369 size_t min_offs)
1370{
1371 size_t name_len = strlen(xi->name);
1372 int i;
1373
1374 if (xi->value && xs->not_found) {
1375 /* Insert the new xattr entry. */
1376 le16_add_cpu(&xs->header->xh_count, 1);
1377 ocfs2_xattr_set_type(last, xi->name_index);
1378 ocfs2_xattr_set_local(last, 1);
1379 last->xe_name_len = name_len;
1380 } else {
1381 void *first_val;
1382 void *val;
1383 size_t offs, size;
1384
1385 first_val = xs->base + min_offs;
1386 offs = le16_to_cpu(xs->here->xe_name_offset);
1387 val = xs->base + offs;
1388
1389 if (le64_to_cpu(xs->here->xe_value_size) >
1390 OCFS2_XATTR_INLINE_SIZE)
1391 size = OCFS2_XATTR_SIZE(name_len) +
1392 OCFS2_XATTR_ROOT_SIZE;
1393 else
1394 size = OCFS2_XATTR_SIZE(name_len) +
1395 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1396
1397 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1398 OCFS2_XATTR_SIZE(xi->value_len)) {
1399 /* The old and the new value have the
1400 same size. Just replace the value. */
1401 ocfs2_xattr_set_local(xs->here, 1);
1402 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1403 /* Clear value bytes. */
1404 memset(val + OCFS2_XATTR_SIZE(name_len),
1405 0,
1406 OCFS2_XATTR_SIZE(xi->value_len));
1407 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1408 xi->value,
1409 xi->value_len);
1410 return;
1411 }
1412 /* Remove the old name+value. */
1413 memmove(first_val + size, first_val, val - first_val);
1414 memset(first_val, 0, size);
1415 xs->here->xe_name_hash = 0;
1416 xs->here->xe_name_offset = 0;
1417 ocfs2_xattr_set_local(xs->here, 1);
1418 xs->here->xe_value_size = 0;
1419
1420 min_offs += size;
1421
1422 /* Adjust all value offsets. */
1423 last = xs->header->xh_entries;
1424 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1425 size_t o = le16_to_cpu(last->xe_name_offset);
1426
1427 if (o < offs)
1428 last->xe_name_offset = cpu_to_le16(o + size);
1429 last += 1;
1430 }
1431
1432 if (!xi->value) {
1433 /* Remove the old entry. */
1434 last -= 1;
1435 memmove(xs->here, xs->here + 1,
1436 (void *)last - (void *)xs->here);
1437 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1438 le16_add_cpu(&xs->header->xh_count, -1);
1439 }
1440 }
1441 if (xi->value) {
1442 /* Insert the new name+value. */
1443 size_t size = OCFS2_XATTR_SIZE(name_len) +
1444 OCFS2_XATTR_SIZE(xi->value_len);
1445 void *val = xs->base + min_offs - size;
1446
1447 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1448 memset(val, 0, size);
1449 memcpy(val, xi->name, name_len);
1450 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1451 xi->value,
1452 xi->value_len);
1453 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1454 ocfs2_xattr_set_local(xs->here, 1);
1455 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1456 }
1457
1458 return;
1459}
1460
1461/*
1462 * ocfs2_xattr_set_entry()
1463 *
1464 * Set extended attribute entry into inode or block.
1465 *
1466 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1467 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1468 * then set value in B tree with set_value_outside().
1469 */
1470static int ocfs2_xattr_set_entry(struct inode *inode,
1471 struct ocfs2_xattr_info *xi,
1472 struct ocfs2_xattr_search *xs,
Tao Ma78f30c32008-11-12 08:27:00 +08001473 struct ocfs2_xattr_set_ctxt *ctxt,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001474 int flag)
1475{
1476 struct ocfs2_xattr_entry *last;
1477 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1478 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1479 size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1480 size_t size_l = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08001481 handle_t *handle = ctxt->handle;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001482 int free, i, ret;
1483 struct ocfs2_xattr_info xi_l = {
1484 .name_index = xi->name_index,
1485 .name = xi->name,
1486 .value = xi->value,
1487 .value_len = xi->value_len,
1488 };
Joel Becker512620f2008-12-09 15:58:35 -08001489 struct ocfs2_xattr_value_buf vb = {
1490 .vb_bh = xs->xattr_bh,
1491 .vb_access = ocfs2_journal_access_di,
1492 };
1493
1494 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1495 BUG_ON(xs->xattr_bh == xs->inode_bh);
1496 vb.vb_access = ocfs2_journal_access_xb;
1497 } else
1498 BUG_ON(xs->xattr_bh != xs->inode_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001499
1500 /* Compute min_offs, last and free space. */
1501 last = xs->header->xh_entries;
1502
1503 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1504 size_t offs = le16_to_cpu(last->xe_name_offset);
1505 if (offs < min_offs)
1506 min_offs = offs;
1507 last += 1;
1508 }
1509
1510 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
1511 if (free < 0)
Joel Beckerb37c4d82008-10-20 18:24:03 -07001512 return -EIO;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001513
1514 if (!xs->not_found) {
1515 size_t size = 0;
1516 if (ocfs2_xattr_is_local(xs->here))
1517 size = OCFS2_XATTR_SIZE(name_len) +
1518 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1519 else
1520 size = OCFS2_XATTR_SIZE(name_len) +
1521 OCFS2_XATTR_ROOT_SIZE;
1522 free += (size + sizeof(struct ocfs2_xattr_entry));
1523 }
1524 /* Check free space in inode or block */
1525 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1526 if (free < sizeof(struct ocfs2_xattr_entry) +
1527 OCFS2_XATTR_SIZE(name_len) +
1528 OCFS2_XATTR_ROOT_SIZE) {
1529 ret = -ENOSPC;
1530 goto out;
1531 }
1532 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1533 xi_l.value = (void *)&def_xv;
1534 xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
1535 } else if (xi->value) {
1536 if (free < sizeof(struct ocfs2_xattr_entry) +
1537 OCFS2_XATTR_SIZE(name_len) +
1538 OCFS2_XATTR_SIZE(xi->value_len)) {
1539 ret = -ENOSPC;
1540 goto out;
1541 }
1542 }
1543
1544 if (!xs->not_found) {
1545 /* For existing extended attribute */
1546 size_t size = OCFS2_XATTR_SIZE(name_len) +
1547 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1548 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1549 void *val = xs->base + offs;
1550
1551 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1552 /* Replace existing local xattr with tree root */
1553 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
Joel Becker512620f2008-12-09 15:58:35 -08001554 ctxt, &vb, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001555 if (ret < 0)
1556 mlog_errno(ret);
1557 goto out;
1558 } else if (!ocfs2_xattr_is_local(xs->here)) {
1559 /* For existing xattr which has value outside */
Joel Becker512620f2008-12-09 15:58:35 -08001560 vb.vb_xv = (struct ocfs2_xattr_value_root *)
1561 (val + OCFS2_XATTR_SIZE(name_len));
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001562
1563 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1564 /*
1565 * If new value need set outside also,
1566 * first truncate old value to new value,
1567 * then set new value with set_value_outside().
1568 */
1569 ret = ocfs2_xattr_value_truncate(inode,
Joel Beckerb3e5d372008-12-09 15:01:04 -08001570 &vb,
Tao Ma78f30c32008-11-12 08:27:00 +08001571 xi->value_len,
1572 ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001573 if (ret < 0) {
1574 mlog_errno(ret);
1575 goto out;
1576 }
1577
Tao Ma85db90e2008-11-12 08:27:01 +08001578 ret = ocfs2_xattr_update_entry(inode,
1579 handle,
1580 xi,
1581 xs,
Joel Becker0c748e92008-12-09 15:46:15 -08001582 &vb,
Tao Ma85db90e2008-11-12 08:27:01 +08001583 offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001584 if (ret < 0) {
1585 mlog_errno(ret);
1586 goto out;
1587 }
1588
Tao Ma85db90e2008-11-12 08:27:01 +08001589 ret = __ocfs2_xattr_set_value_outside(inode,
1590 handle,
Joel Beckerb3e5d372008-12-09 15:01:04 -08001591 vb.vb_xv,
Tao Ma85db90e2008-11-12 08:27:01 +08001592 xi->value,
1593 xi->value_len);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001594 if (ret < 0)
1595 mlog_errno(ret);
1596 goto out;
1597 } else {
1598 /*
1599 * If new value need set in local,
1600 * just trucate old value to zero.
1601 */
1602 ret = ocfs2_xattr_value_truncate(inode,
Joel Beckerb3e5d372008-12-09 15:01:04 -08001603 &vb,
Tao Ma85db90e2008-11-12 08:27:01 +08001604 0,
1605 ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001606 if (ret < 0)
1607 mlog_errno(ret);
1608 }
1609 }
1610 }
1611
Joel Becker512620f2008-12-09 15:58:35 -08001612 ret = ocfs2_journal_access_di(handle, inode, xs->inode_bh,
1613 OCFS2_JOURNAL_ACCESS_WRITE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001614 if (ret) {
1615 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001616 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001617 }
1618
1619 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
Joel Becker512620f2008-12-09 15:58:35 -08001620 ret = vb.vb_access(handle, inode, vb.vb_bh,
1621 OCFS2_JOURNAL_ACCESS_WRITE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001622 if (ret) {
1623 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001624 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001625 }
1626 }
1627
1628 /*
1629 * Set value in local, include set tree root in local.
1630 * This is the first step for value size >INLINE_SIZE.
1631 */
1632 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1633
1634 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1635 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1636 if (ret < 0) {
1637 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001638 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001639 }
1640 }
1641
1642 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1643 (flag & OCFS2_INLINE_XATTR_FL)) {
1644 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1645 unsigned int xattrsize = osb->s_xattr_inline_size;
1646
1647 /*
1648 * Adjust extent record count or inline data size
1649 * to reserve space for extended attribute.
1650 */
1651 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1652 struct ocfs2_inline_data *idata = &di->id2.i_data;
1653 le16_add_cpu(&idata->id_count, -xattrsize);
1654 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1655 struct ocfs2_extent_list *el = &di->id2.i_list;
1656 le16_add_cpu(&el->l_count, -(xattrsize /
1657 sizeof(struct ocfs2_extent_rec)));
1658 }
1659 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1660 }
1661 /* Update xattr flag */
1662 spin_lock(&oi->ip_lock);
1663 oi->ip_dyn_features |= flag;
1664 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1665 spin_unlock(&oi->ip_lock);
1666 /* Update inode ctime */
1667 inode->i_ctime = CURRENT_TIME;
1668 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
1669 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
1670
1671 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1672 if (ret < 0)
1673 mlog_errno(ret);
1674
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001675 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1676 /*
1677 * Set value outside in B tree.
1678 * This is the second step for value size > INLINE_SIZE.
1679 */
1680 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
Joel Becker512620f2008-12-09 15:58:35 -08001681 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, ctxt,
1682 &vb, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001683 if (ret < 0) {
1684 int ret2;
1685
1686 mlog_errno(ret);
1687 /*
1688 * If set value outside failed, we have to clean
1689 * the junk tree root we have already set in local.
1690 */
Tao Ma85db90e2008-11-12 08:27:01 +08001691 ret2 = ocfs2_xattr_cleanup(inode, ctxt->handle,
Joel Becker512620f2008-12-09 15:58:35 -08001692 xi, xs, &vb, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001693 if (ret2 < 0)
1694 mlog_errno(ret2);
1695 }
1696 }
1697out:
1698 return ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001699}
1700
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001701static int ocfs2_remove_value_outside(struct inode*inode,
Joel Becker43119012008-12-09 16:24:43 -08001702 struct ocfs2_xattr_value_buf *vb,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001703 struct ocfs2_xattr_header *header)
1704{
1705 int ret = 0, i;
Tao Ma78f30c32008-11-12 08:27:00 +08001706 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1707 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
1708
1709 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001710
Jan Karaa90714c2008-10-09 19:38:40 +02001711 ctxt.handle = ocfs2_start_trans(osb,
1712 ocfs2_remove_extent_credits(osb->sb));
Tao Ma85db90e2008-11-12 08:27:01 +08001713 if (IS_ERR(ctxt.handle)) {
1714 ret = PTR_ERR(ctxt.handle);
1715 mlog_errno(ret);
1716 goto out;
1717 }
1718
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001719 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1720 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1721
1722 if (!ocfs2_xattr_is_local(entry)) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001723 void *val;
1724
1725 val = (void *)header +
1726 le16_to_cpu(entry->xe_name_offset);
Joel Becker43119012008-12-09 16:24:43 -08001727 vb->vb_xv = (struct ocfs2_xattr_value_root *)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001728 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
Joel Becker43119012008-12-09 16:24:43 -08001729 ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001730 if (ret < 0) {
1731 mlog_errno(ret);
Tao Ma78f30c32008-11-12 08:27:00 +08001732 break;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001733 }
1734 }
1735 }
1736
Tao Ma85db90e2008-11-12 08:27:01 +08001737 ocfs2_commit_trans(osb, ctxt.handle);
Tao Ma78f30c32008-11-12 08:27:00 +08001738 ocfs2_schedule_truncate_log_flush(osb, 1);
1739 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tao Ma85db90e2008-11-12 08:27:01 +08001740out:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001741 return ret;
1742}
1743
1744static int ocfs2_xattr_ibody_remove(struct inode *inode,
1745 struct buffer_head *di_bh)
1746{
1747
1748 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1749 struct ocfs2_xattr_header *header;
1750 int ret;
Joel Becker43119012008-12-09 16:24:43 -08001751 struct ocfs2_xattr_value_buf vb = {
1752 .vb_bh = di_bh,
1753 .vb_access = ocfs2_journal_access_di,
1754 };
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001755
1756 header = (struct ocfs2_xattr_header *)
1757 ((void *)di + inode->i_sb->s_blocksize -
1758 le16_to_cpu(di->i_xattr_inline_size));
1759
Joel Becker43119012008-12-09 16:24:43 -08001760 ret = ocfs2_remove_value_outside(inode, &vb, header);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001761
1762 return ret;
1763}
1764
1765static int ocfs2_xattr_block_remove(struct inode *inode,
1766 struct buffer_head *blk_bh)
1767{
1768 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001769 int ret = 0;
Joel Becker43119012008-12-09 16:24:43 -08001770 struct ocfs2_xattr_value_buf vb = {
1771 .vb_bh = blk_bh,
1772 .vb_access = ocfs2_journal_access_xb,
1773 };
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001774
1775 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Maa3944252008-08-18 17:38:54 +08001776 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1777 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
Joel Becker43119012008-12-09 16:24:43 -08001778 ret = ocfs2_remove_value_outside(inode, &vb, header);
Tao Maa3944252008-08-18 17:38:54 +08001779 } else
1780 ret = ocfs2_delete_xattr_index_block(inode, blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001781
1782 return ret;
1783}
1784
Tao Ma08413892008-08-29 09:00:19 +08001785static int ocfs2_xattr_free_block(struct inode *inode,
1786 u64 block)
1787{
1788 struct inode *xb_alloc_inode;
1789 struct buffer_head *xb_alloc_bh = NULL;
1790 struct buffer_head *blk_bh = NULL;
1791 struct ocfs2_xattr_block *xb;
1792 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1793 handle_t *handle;
1794 int ret = 0;
1795 u64 blk, bg_blkno;
1796 u16 bit;
1797
Joel Becker4ae1d692008-11-13 14:49:18 -08001798 ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
Tao Ma08413892008-08-29 09:00:19 +08001799 if (ret < 0) {
1800 mlog_errno(ret);
1801 goto out;
1802 }
1803
Tao Ma08413892008-08-29 09:00:19 +08001804 ret = ocfs2_xattr_block_remove(inode, blk_bh);
1805 if (ret < 0) {
1806 mlog_errno(ret);
1807 goto out;
1808 }
1809
Joel Becker4ae1d692008-11-13 14:49:18 -08001810 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Ma08413892008-08-29 09:00:19 +08001811 blk = le64_to_cpu(xb->xb_blkno);
1812 bit = le16_to_cpu(xb->xb_suballoc_bit);
1813 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1814
1815 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1816 EXTENT_ALLOC_SYSTEM_INODE,
1817 le16_to_cpu(xb->xb_suballoc_slot));
1818 if (!xb_alloc_inode) {
1819 ret = -ENOMEM;
1820 mlog_errno(ret);
1821 goto out;
1822 }
1823 mutex_lock(&xb_alloc_inode->i_mutex);
1824
1825 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1826 if (ret < 0) {
1827 mlog_errno(ret);
1828 goto out_mutex;
1829 }
1830
1831 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
1832 if (IS_ERR(handle)) {
1833 ret = PTR_ERR(handle);
1834 mlog_errno(ret);
1835 goto out_unlock;
1836 }
1837
1838 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1839 bit, bg_blkno, 1);
1840 if (ret < 0)
1841 mlog_errno(ret);
1842
1843 ocfs2_commit_trans(osb, handle);
1844out_unlock:
1845 ocfs2_inode_unlock(xb_alloc_inode, 1);
1846 brelse(xb_alloc_bh);
1847out_mutex:
1848 mutex_unlock(&xb_alloc_inode->i_mutex);
1849 iput(xb_alloc_inode);
1850out:
1851 brelse(blk_bh);
1852 return ret;
1853}
1854
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001855/*
1856 * ocfs2_xattr_remove()
1857 *
1858 * Free extended attribute resources associated with this inode.
1859 */
1860int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
1861{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001862 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1863 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1864 handle_t *handle;
1865 int ret;
1866
Tiger Yang8154da32008-08-18 17:11:46 +08001867 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1868 return 0;
1869
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001870 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1871 return 0;
1872
1873 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1874 ret = ocfs2_xattr_ibody_remove(inode, di_bh);
1875 if (ret < 0) {
1876 mlog_errno(ret);
1877 goto out;
1878 }
1879 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001880
Tao Ma08413892008-08-29 09:00:19 +08001881 if (di->i_xattr_loc) {
1882 ret = ocfs2_xattr_free_block(inode,
1883 le64_to_cpu(di->i_xattr_loc));
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001884 if (ret < 0) {
1885 mlog_errno(ret);
1886 goto out;
1887 }
1888 }
1889
1890 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1891 OCFS2_INODE_UPDATE_CREDITS);
1892 if (IS_ERR(handle)) {
1893 ret = PTR_ERR(handle);
1894 mlog_errno(ret);
1895 goto out;
1896 }
Joel Becker84008972008-12-09 16:11:49 -08001897 ret = ocfs2_journal_access_di(handle, inode, di_bh,
1898 OCFS2_JOURNAL_ACCESS_WRITE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001899 if (ret) {
1900 mlog_errno(ret);
1901 goto out_commit;
1902 }
1903
Tao Ma08413892008-08-29 09:00:19 +08001904 di->i_xattr_loc = 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001905
1906 spin_lock(&oi->ip_lock);
1907 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
1908 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1909 spin_unlock(&oi->ip_lock);
1910
1911 ret = ocfs2_journal_dirty(handle, di_bh);
1912 if (ret < 0)
1913 mlog_errno(ret);
1914out_commit:
1915 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1916out:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001917 return ret;
1918}
1919
1920static int ocfs2_xattr_has_space_inline(struct inode *inode,
1921 struct ocfs2_dinode *di)
1922{
1923 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1924 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
1925 int free;
1926
1927 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
1928 return 0;
1929
1930 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1931 struct ocfs2_inline_data *idata = &di->id2.i_data;
1932 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
1933 } else if (ocfs2_inode_is_fast_symlink(inode)) {
1934 free = ocfs2_fast_symlink_chars(inode->i_sb) -
1935 le64_to_cpu(di->i_size);
1936 } else {
1937 struct ocfs2_extent_list *el = &di->id2.i_list;
1938 free = (le16_to_cpu(el->l_count) -
1939 le16_to_cpu(el->l_next_free_rec)) *
1940 sizeof(struct ocfs2_extent_rec);
1941 }
1942 if (free >= xattrsize)
1943 return 1;
1944
1945 return 0;
1946}
1947
1948/*
1949 * ocfs2_xattr_ibody_find()
1950 *
1951 * Find extended attribute in inode block and
1952 * fill search info into struct ocfs2_xattr_search.
1953 */
1954static int ocfs2_xattr_ibody_find(struct inode *inode,
1955 int name_index,
1956 const char *name,
1957 struct ocfs2_xattr_search *xs)
1958{
1959 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1960 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1961 int ret;
1962 int has_space = 0;
1963
1964 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1965 return 0;
1966
1967 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1968 down_read(&oi->ip_alloc_sem);
1969 has_space = ocfs2_xattr_has_space_inline(inode, di);
1970 up_read(&oi->ip_alloc_sem);
1971 if (!has_space)
1972 return 0;
1973 }
1974
1975 xs->xattr_bh = xs->inode_bh;
1976 xs->end = (void *)di + inode->i_sb->s_blocksize;
1977 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
1978 xs->header = (struct ocfs2_xattr_header *)
1979 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1980 else
1981 xs->header = (struct ocfs2_xattr_header *)
1982 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
1983 xs->base = (void *)xs->header;
1984 xs->here = xs->header->xh_entries;
1985
1986 /* Find the named attribute. */
1987 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1988 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1989 if (ret && ret != -ENODATA)
1990 return ret;
1991 xs->not_found = ret;
1992 }
1993
1994 return 0;
1995}
1996
1997/*
1998 * ocfs2_xattr_ibody_set()
1999 *
2000 * Set, replace or remove an extended attribute into inode block.
2001 *
2002 */
2003static int ocfs2_xattr_ibody_set(struct inode *inode,
2004 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08002005 struct ocfs2_xattr_search *xs,
2006 struct ocfs2_xattr_set_ctxt *ctxt)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002007{
2008 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2009 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2010 int ret;
2011
2012 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2013 return -ENOSPC;
2014
2015 down_write(&oi->ip_alloc_sem);
2016 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2017 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2018 ret = -ENOSPC;
2019 goto out;
2020 }
2021 }
2022
Tao Ma78f30c32008-11-12 08:27:00 +08002023 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002024 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
2025out:
2026 up_write(&oi->ip_alloc_sem);
2027
2028 return ret;
2029}
2030
2031/*
2032 * ocfs2_xattr_block_find()
2033 *
2034 * Find extended attribute in external block and
2035 * fill search info into struct ocfs2_xattr_search.
2036 */
2037static int ocfs2_xattr_block_find(struct inode *inode,
2038 int name_index,
2039 const char *name,
2040 struct ocfs2_xattr_search *xs)
2041{
2042 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2043 struct buffer_head *blk_bh = NULL;
Tao Ma589dc262008-08-18 17:38:51 +08002044 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002045 int ret = 0;
2046
2047 if (!di->i_xattr_loc)
2048 return ret;
2049
Joel Becker4ae1d692008-11-13 14:49:18 -08002050 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
2051 &blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002052 if (ret < 0) {
2053 mlog_errno(ret);
2054 return ret;
2055 }
Joel Beckerf6087fb2008-10-20 18:20:43 -07002056
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002057 xs->xattr_bh = blk_bh;
Joel Becker4ae1d692008-11-13 14:49:18 -08002058 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002059
Tao Ma589dc262008-08-18 17:38:51 +08002060 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2061 xs->header = &xb->xb_attrs.xb_header;
2062 xs->base = (void *)xs->header;
2063 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
2064 xs->here = xs->header->xh_entries;
2065
2066 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2067 } else
2068 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
2069 name_index,
2070 name, xs);
2071
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002072 if (ret && ret != -ENODATA) {
2073 xs->xattr_bh = NULL;
2074 goto cleanup;
2075 }
2076 xs->not_found = ret;
2077 return 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002078cleanup:
2079 brelse(blk_bh);
2080
2081 return ret;
2082}
2083
2084/*
2085 * ocfs2_xattr_block_set()
2086 *
2087 * Set, replace or remove an extended attribute into external block.
2088 *
2089 */
2090static int ocfs2_xattr_block_set(struct inode *inode,
2091 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08002092 struct ocfs2_xattr_search *xs,
2093 struct ocfs2_xattr_set_ctxt *ctxt)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002094{
2095 struct buffer_head *new_bh = NULL;
2096 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2097 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
Tao Ma85db90e2008-11-12 08:27:01 +08002098 handle_t *handle = ctxt->handle;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002099 struct ocfs2_xattr_block *xblk = NULL;
2100 u16 suballoc_bit_start;
2101 u32 num_got;
2102 u64 first_blkno;
2103 int ret;
2104
2105 if (!xs->xattr_bh) {
Joel Becker84008972008-12-09 16:11:49 -08002106 ret = ocfs2_journal_access_di(handle, inode, xs->inode_bh,
2107 OCFS2_JOURNAL_ACCESS_CREATE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002108 if (ret < 0) {
2109 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08002110 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002111 }
2112
Tao Ma78f30c32008-11-12 08:27:00 +08002113 ret = ocfs2_claim_metadata(osb, handle, ctxt->meta_ac, 1,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002114 &suballoc_bit_start, &num_got,
2115 &first_blkno);
2116 if (ret < 0) {
2117 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08002118 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002119 }
2120
2121 new_bh = sb_getblk(inode->i_sb, first_blkno);
2122 ocfs2_set_new_buffer_uptodate(inode, new_bh);
2123
Joel Becker84008972008-12-09 16:11:49 -08002124 ret = ocfs2_journal_access_xb(handle, inode, new_bh,
2125 OCFS2_JOURNAL_ACCESS_CREATE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002126 if (ret < 0) {
2127 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08002128 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002129 }
2130
2131 /* Initialize ocfs2_xattr_block */
2132 xs->xattr_bh = new_bh;
2133 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2134 memset(xblk, 0, inode->i_sb->s_blocksize);
2135 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
2136 xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
2137 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
2138 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
2139 xblk->xb_blkno = cpu_to_le64(first_blkno);
2140
2141 xs->header = &xblk->xb_attrs.xb_header;
2142 xs->base = (void *)xs->header;
2143 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2144 xs->here = xs->header->xh_entries;
2145
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002146 ret = ocfs2_journal_dirty(handle, new_bh);
2147 if (ret < 0) {
2148 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08002149 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002150 }
2151 di->i_xattr_loc = cpu_to_le64(first_blkno);
Tao Ma85db90e2008-11-12 08:27:01 +08002152 ocfs2_journal_dirty(handle, xs->inode_bh);
Tao Ma01225592008-08-18 17:38:53 +08002153 } else
2154 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2155
2156 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
2157 /* Set extended attribute into external block */
Tao Ma78f30c32008-11-12 08:27:00 +08002158 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
2159 OCFS2_HAS_XATTR_FL);
Tao Ma01225592008-08-18 17:38:53 +08002160 if (!ret || ret != -ENOSPC)
2161 goto end;
2162
Tao Ma78f30c32008-11-12 08:27:00 +08002163 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08002164 if (ret)
2165 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002166 }
2167
Tao Ma78f30c32008-11-12 08:27:00 +08002168 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08002169
2170end:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002171
2172 return ret;
2173}
2174
Tao Ma78f30c32008-11-12 08:27:00 +08002175/* Check whether the new xattr can be inserted into the inode. */
2176static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
2177 struct ocfs2_xattr_info *xi,
2178 struct ocfs2_xattr_search *xs)
2179{
2180 u64 value_size;
2181 struct ocfs2_xattr_entry *last;
2182 int free, i;
2183 size_t min_offs = xs->end - xs->base;
2184
2185 if (!xs->header)
2186 return 0;
2187
2188 last = xs->header->xh_entries;
2189
2190 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
2191 size_t offs = le16_to_cpu(last->xe_name_offset);
2192 if (offs < min_offs)
2193 min_offs = offs;
2194 last += 1;
2195 }
2196
2197 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
2198 if (free < 0)
2199 return 0;
2200
2201 BUG_ON(!xs->not_found);
2202
2203 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
2204 value_size = OCFS2_XATTR_ROOT_SIZE;
2205 else
2206 value_size = OCFS2_XATTR_SIZE(xi->value_len);
2207
2208 if (free >= sizeof(struct ocfs2_xattr_entry) +
2209 OCFS2_XATTR_SIZE(strlen(xi->name)) + value_size)
2210 return 1;
2211
2212 return 0;
2213}
2214
2215static int ocfs2_calc_xattr_set_need(struct inode *inode,
2216 struct ocfs2_dinode *di,
2217 struct ocfs2_xattr_info *xi,
2218 struct ocfs2_xattr_search *xis,
2219 struct ocfs2_xattr_search *xbs,
2220 int *clusters_need,
Tao Ma85db90e2008-11-12 08:27:01 +08002221 int *meta_need,
2222 int *credits_need)
Tao Ma78f30c32008-11-12 08:27:00 +08002223{
2224 int ret = 0, old_in_xb = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08002225 int clusters_add = 0, meta_add = 0, credits = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08002226 struct buffer_head *bh = NULL;
2227 struct ocfs2_xattr_block *xb = NULL;
2228 struct ocfs2_xattr_entry *xe = NULL;
2229 struct ocfs2_xattr_value_root *xv = NULL;
2230 char *base = NULL;
2231 int name_offset, name_len = 0;
2232 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2233 xi->value_len);
2234 u64 value_size;
2235
Tao Ma78f30c32008-11-12 08:27:00 +08002236 if (xis->not_found && xbs->not_found) {
Tao Ma85db90e2008-11-12 08:27:01 +08002237 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2238
2239 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
Tao Ma78f30c32008-11-12 08:27:00 +08002240 clusters_add += new_clusters;
Tao Ma85db90e2008-11-12 08:27:01 +08002241 credits += ocfs2_calc_extend_credits(inode->i_sb,
2242 &def_xv.xv.xr_list,
2243 new_clusters);
2244 }
Tao Ma78f30c32008-11-12 08:27:00 +08002245
2246 goto meta_guess;
2247 }
2248
2249 if (!xis->not_found) {
2250 xe = xis->here;
2251 name_offset = le16_to_cpu(xe->xe_name_offset);
2252 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2253 base = xis->base;
Tao Ma85db90e2008-11-12 08:27:01 +08002254 credits += OCFS2_INODE_UPDATE_CREDITS;
Tao Ma78f30c32008-11-12 08:27:00 +08002255 } else {
Joel Becker970e4932008-11-13 14:49:19 -08002256 int i, block_off = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08002257 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2258 xe = xbs->here;
2259 name_offset = le16_to_cpu(xe->xe_name_offset);
2260 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2261 i = xbs->here - xbs->header->xh_entries;
2262 old_in_xb = 1;
2263
2264 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2265 ret = ocfs2_xattr_bucket_get_name_value(inode,
2266 bucket_xh(xbs->bucket),
2267 i, &block_off,
2268 &name_offset);
2269 base = bucket_block(xbs->bucket, block_off);
Tao Ma85db90e2008-11-12 08:27:01 +08002270 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2271 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08002272 base = xbs->base;
Tao Ma85db90e2008-11-12 08:27:01 +08002273 credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
2274 }
2275 }
2276
2277 /*
2278 * delete a xattr doesn't need metadata and cluster allocation.
2279 * so just calculate the credits and return.
2280 *
2281 * The credits for removing the value tree will be extended
2282 * by ocfs2_remove_extent itself.
2283 */
2284 if (!xi->value) {
2285 if (!ocfs2_xattr_is_local(xe))
Jan Karaa90714c2008-10-09 19:38:40 +02002286 credits += ocfs2_remove_extent_credits(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08002287
2288 goto out;
Tao Ma78f30c32008-11-12 08:27:00 +08002289 }
2290
2291 /* do cluster allocation guess first. */
2292 value_size = le64_to_cpu(xe->xe_value_size);
2293
2294 if (old_in_xb) {
2295 /*
2296 * In xattr set, we always try to set the xe in inode first,
2297 * so if it can be inserted into inode successfully, the old
2298 * one will be removed from the xattr block, and this xattr
2299 * will be inserted into inode as a new xattr in inode.
2300 */
2301 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
2302 clusters_add += new_clusters;
Jan Karaa90714c2008-10-09 19:38:40 +02002303 credits += ocfs2_remove_extent_credits(inode->i_sb) +
Tao Ma85db90e2008-11-12 08:27:01 +08002304 OCFS2_INODE_UPDATE_CREDITS;
2305 if (!ocfs2_xattr_is_local(xe))
2306 credits += ocfs2_calc_extend_credits(
2307 inode->i_sb,
2308 &def_xv.xv.xr_list,
2309 new_clusters);
Tao Ma78f30c32008-11-12 08:27:00 +08002310 goto out;
2311 }
2312 }
2313
2314 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
2315 /* the new values will be stored outside. */
2316 u32 old_clusters = 0;
2317
2318 if (!ocfs2_xattr_is_local(xe)) {
2319 old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2320 value_size);
2321 xv = (struct ocfs2_xattr_value_root *)
2322 (base + name_offset + name_len);
Tao Ma97aff522008-11-19 16:48:41 +08002323 value_size = OCFS2_XATTR_ROOT_SIZE;
Tao Ma78f30c32008-11-12 08:27:00 +08002324 } else
2325 xv = &def_xv.xv;
2326
Tao Ma85db90e2008-11-12 08:27:01 +08002327 if (old_clusters >= new_clusters) {
Jan Karaa90714c2008-10-09 19:38:40 +02002328 credits += ocfs2_remove_extent_credits(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08002329 goto out;
Tao Ma85db90e2008-11-12 08:27:01 +08002330 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08002331 meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
2332 clusters_add += new_clusters - old_clusters;
Tao Ma85db90e2008-11-12 08:27:01 +08002333 credits += ocfs2_calc_extend_credits(inode->i_sb,
2334 &xv->xr_list,
2335 new_clusters -
2336 old_clusters);
Tao Ma97aff522008-11-19 16:48:41 +08002337 if (value_size >= OCFS2_XATTR_ROOT_SIZE)
2338 goto out;
Tao Ma78f30c32008-11-12 08:27:00 +08002339 }
2340 } else {
2341 /*
2342 * Now the new value will be stored inside. So if the new
2343 * value is smaller than the size of value root or the old
2344 * value, we don't need any allocation, otherwise we have
2345 * to guess metadata allocation.
2346 */
2347 if ((ocfs2_xattr_is_local(xe) && value_size >= xi->value_len) ||
2348 (!ocfs2_xattr_is_local(xe) &&
2349 OCFS2_XATTR_ROOT_SIZE >= xi->value_len))
2350 goto out;
2351 }
2352
2353meta_guess:
2354 /* calculate metadata allocation. */
2355 if (di->i_xattr_loc) {
2356 if (!xbs->xattr_bh) {
Joel Becker4ae1d692008-11-13 14:49:18 -08002357 ret = ocfs2_read_xattr_block(inode,
2358 le64_to_cpu(di->i_xattr_loc),
2359 &bh);
Tao Ma78f30c32008-11-12 08:27:00 +08002360 if (ret) {
2361 mlog_errno(ret);
2362 goto out;
2363 }
2364
2365 xb = (struct ocfs2_xattr_block *)bh->b_data;
2366 } else
2367 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2368
2369 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2370 struct ocfs2_extent_list *el =
2371 &xb->xb_attrs.xb_root.xt_list;
2372 meta_add += ocfs2_extend_meta_needed(el);
Tao Ma85db90e2008-11-12 08:27:01 +08002373 credits += ocfs2_calc_extend_credits(inode->i_sb,
2374 el, 1);
Tao Ma78f30c32008-11-12 08:27:00 +08002375 }
2376
2377 /*
2378 * This cluster will be used either for new bucket or for
2379 * new xattr block.
2380 * If the cluster size is the same as the bucket size, one
2381 * more is needed since we may need to extend the bucket
2382 * also.
2383 */
2384 clusters_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08002385 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08002386 if (OCFS2_XATTR_BUCKET_SIZE ==
Tao Ma85db90e2008-11-12 08:27:01 +08002387 OCFS2_SB(inode->i_sb)->s_clustersize) {
2388 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08002389 clusters_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08002390 }
2391 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08002392 meta_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08002393 credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
2394 }
Tao Ma78f30c32008-11-12 08:27:00 +08002395out:
2396 if (clusters_need)
2397 *clusters_need = clusters_add;
2398 if (meta_need)
2399 *meta_need = meta_add;
Tao Ma85db90e2008-11-12 08:27:01 +08002400 if (credits_need)
2401 *credits_need = credits;
Tao Ma78f30c32008-11-12 08:27:00 +08002402 brelse(bh);
2403 return ret;
2404}
2405
2406static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
2407 struct ocfs2_dinode *di,
2408 struct ocfs2_xattr_info *xi,
2409 struct ocfs2_xattr_search *xis,
2410 struct ocfs2_xattr_search *xbs,
Tao Ma85db90e2008-11-12 08:27:01 +08002411 struct ocfs2_xattr_set_ctxt *ctxt,
2412 int *credits)
Tao Ma78f30c32008-11-12 08:27:00 +08002413{
2414 int clusters_add, meta_add, ret;
2415 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2416
2417 memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
2418
2419 ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
2420
2421 ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
Tao Ma85db90e2008-11-12 08:27:01 +08002422 &clusters_add, &meta_add, credits);
Tao Ma78f30c32008-11-12 08:27:00 +08002423 if (ret) {
2424 mlog_errno(ret);
2425 return ret;
2426 }
2427
Tao Ma85db90e2008-11-12 08:27:01 +08002428 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
2429 "credits = %d\n", xi->name, meta_add, clusters_add, *credits);
Tao Ma78f30c32008-11-12 08:27:00 +08002430
2431 if (meta_add) {
2432 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
2433 &ctxt->meta_ac);
2434 if (ret) {
2435 mlog_errno(ret);
2436 goto out;
2437 }
2438 }
2439
2440 if (clusters_add) {
2441 ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
2442 if (ret)
2443 mlog_errno(ret);
2444 }
2445out:
2446 if (ret) {
2447 if (ctxt->meta_ac) {
2448 ocfs2_free_alloc_context(ctxt->meta_ac);
2449 ctxt->meta_ac = NULL;
2450 }
2451
2452 /*
2453 * We cannot have an error and a non null ctxt->data_ac.
2454 */
2455 }
2456
2457 return ret;
2458}
2459
Tao Ma85db90e2008-11-12 08:27:01 +08002460static int __ocfs2_xattr_set_handle(struct inode *inode,
2461 struct ocfs2_dinode *di,
2462 struct ocfs2_xattr_info *xi,
2463 struct ocfs2_xattr_search *xis,
2464 struct ocfs2_xattr_search *xbs,
2465 struct ocfs2_xattr_set_ctxt *ctxt)
2466{
Tao Ma9f868f12008-11-19 16:48:42 +08002467 int ret = 0, credits, old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08002468
2469 if (!xi->value) {
2470 /* Remove existing extended attribute */
2471 if (!xis->not_found)
2472 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2473 else if (!xbs->not_found)
2474 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2475 } else {
2476 /* We always try to set extended attribute into inode first*/
2477 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2478 if (!ret && !xbs->not_found) {
2479 /*
2480 * If succeed and that extended attribute existing in
2481 * external block, then we will remove it.
2482 */
2483 xi->value = NULL;
2484 xi->value_len = 0;
2485
Tao Ma9f868f12008-11-19 16:48:42 +08002486 old_found = xis->not_found;
Tao Ma85db90e2008-11-12 08:27:01 +08002487 xis->not_found = -ENODATA;
2488 ret = ocfs2_calc_xattr_set_need(inode,
2489 di,
2490 xi,
2491 xis,
2492 xbs,
2493 NULL,
2494 NULL,
2495 &credits);
Tao Ma9f868f12008-11-19 16:48:42 +08002496 xis->not_found = old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08002497 if (ret) {
2498 mlog_errno(ret);
2499 goto out;
2500 }
2501
2502 ret = ocfs2_extend_trans(ctxt->handle, credits +
2503 ctxt->handle->h_buffer_credits);
2504 if (ret) {
2505 mlog_errno(ret);
2506 goto out;
2507 }
2508 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2509 } else if (ret == -ENOSPC) {
2510 if (di->i_xattr_loc && !xbs->xattr_bh) {
2511 ret = ocfs2_xattr_block_find(inode,
2512 xi->name_index,
2513 xi->name, xbs);
2514 if (ret)
2515 goto out;
2516
Tao Ma9f868f12008-11-19 16:48:42 +08002517 old_found = xis->not_found;
Tao Ma85db90e2008-11-12 08:27:01 +08002518 xis->not_found = -ENODATA;
2519 ret = ocfs2_calc_xattr_set_need(inode,
2520 di,
2521 xi,
2522 xis,
2523 xbs,
2524 NULL,
2525 NULL,
2526 &credits);
Tao Ma9f868f12008-11-19 16:48:42 +08002527 xis->not_found = old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08002528 if (ret) {
2529 mlog_errno(ret);
2530 goto out;
2531 }
2532
2533 ret = ocfs2_extend_trans(ctxt->handle, credits +
2534 ctxt->handle->h_buffer_credits);
2535 if (ret) {
2536 mlog_errno(ret);
2537 goto out;
2538 }
2539 }
2540 /*
2541 * If no space in inode, we will set extended attribute
2542 * into external block.
2543 */
2544 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2545 if (ret)
2546 goto out;
2547 if (!xis->not_found) {
2548 /*
2549 * If succeed and that extended attribute
2550 * existing in inode, we will remove it.
2551 */
2552 xi->value = NULL;
2553 xi->value_len = 0;
2554 xbs->not_found = -ENODATA;
2555 ret = ocfs2_calc_xattr_set_need(inode,
2556 di,
2557 xi,
2558 xis,
2559 xbs,
2560 NULL,
2561 NULL,
2562 &credits);
2563 if (ret) {
2564 mlog_errno(ret);
2565 goto out;
2566 }
2567
2568 ret = ocfs2_extend_trans(ctxt->handle, credits +
2569 ctxt->handle->h_buffer_credits);
2570 if (ret) {
2571 mlog_errno(ret);
2572 goto out;
2573 }
2574 ret = ocfs2_xattr_ibody_set(inode, xi,
2575 xis, ctxt);
2576 }
2577 }
2578 }
2579
2580out:
2581 return ret;
2582}
2583
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002584/*
Tiger Yang6c3faba2008-11-14 11:16:03 +08002585 * This function only called duing creating inode
2586 * for init security/acl xattrs of the new inode.
2587 * The xattrs could be put into ibody or extent block,
2588 * xattr bucket would not be use in this case.
2589 * transanction credits also be reserved in here.
2590 */
2591int ocfs2_xattr_set_handle(handle_t *handle,
2592 struct inode *inode,
2593 struct buffer_head *di_bh,
2594 int name_index,
2595 const char *name,
2596 const void *value,
2597 size_t value_len,
2598 int flags,
2599 struct ocfs2_alloc_context *meta_ac,
2600 struct ocfs2_alloc_context *data_ac)
2601{
2602 struct ocfs2_dinode *di;
2603 int ret;
2604
2605 struct ocfs2_xattr_info xi = {
2606 .name_index = name_index,
2607 .name = name,
2608 .value = value,
2609 .value_len = value_len,
2610 };
2611
2612 struct ocfs2_xattr_search xis = {
2613 .not_found = -ENODATA,
2614 };
2615
2616 struct ocfs2_xattr_search xbs = {
2617 .not_found = -ENODATA,
2618 };
2619
2620 struct ocfs2_xattr_set_ctxt ctxt = {
2621 .handle = handle,
2622 .meta_ac = meta_ac,
2623 .data_ac = data_ac,
2624 };
2625
2626 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2627 return -EOPNOTSUPP;
2628
2629 xis.inode_bh = xbs.inode_bh = di_bh;
2630 di = (struct ocfs2_dinode *)di_bh->b_data;
2631
2632 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2633
2634 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2635 if (ret)
2636 goto cleanup;
2637 if (xis.not_found) {
2638 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2639 if (ret)
2640 goto cleanup;
2641 }
2642
2643 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
2644
2645cleanup:
2646 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2647 brelse(xbs.xattr_bh);
2648
2649 return ret;
2650}
2651
2652/*
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002653 * ocfs2_xattr_set()
2654 *
2655 * Set, replace or remove an extended attribute for this inode.
2656 * value is NULL to remove an existing extended attribute, else either
2657 * create or replace an extended attribute.
2658 */
2659int ocfs2_xattr_set(struct inode *inode,
2660 int name_index,
2661 const char *name,
2662 const void *value,
2663 size_t value_len,
2664 int flags)
2665{
2666 struct buffer_head *di_bh = NULL;
2667 struct ocfs2_dinode *di;
Tao Ma85db90e2008-11-12 08:27:01 +08002668 int ret, credits;
Tao Ma78f30c32008-11-12 08:27:00 +08002669 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08002670 struct inode *tl_inode = osb->osb_tl_inode;
Tao Ma78f30c32008-11-12 08:27:00 +08002671 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002672
2673 struct ocfs2_xattr_info xi = {
2674 .name_index = name_index,
2675 .name = name,
2676 .value = value,
2677 .value_len = value_len,
2678 };
2679
2680 struct ocfs2_xattr_search xis = {
2681 .not_found = -ENODATA,
2682 };
2683
2684 struct ocfs2_xattr_search xbs = {
2685 .not_found = -ENODATA,
2686 };
2687
Tiger Yang8154da32008-08-18 17:11:46 +08002688 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2689 return -EOPNOTSUPP;
2690
Joel Beckerba937122008-10-24 19:13:20 -07002691 /*
2692 * Only xbs will be used on indexed trees. xis doesn't need a
2693 * bucket.
2694 */
2695 xbs.bucket = ocfs2_xattr_bucket_new(inode);
2696 if (!xbs.bucket) {
2697 mlog_errno(-ENOMEM);
2698 return -ENOMEM;
2699 }
2700
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002701 ret = ocfs2_inode_lock(inode, &di_bh, 1);
2702 if (ret < 0) {
2703 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07002704 goto cleanup_nolock;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002705 }
2706 xis.inode_bh = xbs.inode_bh = di_bh;
2707 di = (struct ocfs2_dinode *)di_bh->b_data;
2708
2709 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2710 /*
2711 * Scan inode and external block to find the same name
2712 * extended attribute and collect search infomation.
2713 */
2714 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2715 if (ret)
2716 goto cleanup;
2717 if (xis.not_found) {
2718 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2719 if (ret)
2720 goto cleanup;
2721 }
2722
2723 if (xis.not_found && xbs.not_found) {
2724 ret = -ENODATA;
2725 if (flags & XATTR_REPLACE)
2726 goto cleanup;
2727 ret = 0;
2728 if (!value)
2729 goto cleanup;
2730 } else {
2731 ret = -EEXIST;
2732 if (flags & XATTR_CREATE)
2733 goto cleanup;
2734 }
2735
Tao Ma85db90e2008-11-12 08:27:01 +08002736
2737 mutex_lock(&tl_inode->i_mutex);
2738
2739 if (ocfs2_truncate_log_needs_flush(osb)) {
2740 ret = __ocfs2_flush_truncate_log(osb);
2741 if (ret < 0) {
2742 mutex_unlock(&tl_inode->i_mutex);
2743 mlog_errno(ret);
2744 goto cleanup;
2745 }
2746 }
2747 mutex_unlock(&tl_inode->i_mutex);
2748
2749 ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
2750 &xbs, &ctxt, &credits);
Tao Ma78f30c32008-11-12 08:27:00 +08002751 if (ret) {
2752 mlog_errno(ret);
2753 goto cleanup;
2754 }
2755
Tao Ma85db90e2008-11-12 08:27:01 +08002756 ctxt.handle = ocfs2_start_trans(osb, credits);
2757 if (IS_ERR(ctxt.handle)) {
2758 ret = PTR_ERR(ctxt.handle);
2759 mlog_errno(ret);
2760 goto cleanup;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002761 }
Tao Ma85db90e2008-11-12 08:27:01 +08002762
2763 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
2764
2765 ocfs2_commit_trans(osb, ctxt.handle);
2766
Tao Ma78f30c32008-11-12 08:27:00 +08002767 if (ctxt.data_ac)
2768 ocfs2_free_alloc_context(ctxt.data_ac);
2769 if (ctxt.meta_ac)
2770 ocfs2_free_alloc_context(ctxt.meta_ac);
2771 if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
2772 ocfs2_schedule_truncate_log_flush(osb, 1);
2773 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002774cleanup:
2775 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2776 ocfs2_inode_unlock(inode, 1);
Joel Beckerba937122008-10-24 19:13:20 -07002777cleanup_nolock:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002778 brelse(di_bh);
2779 brelse(xbs.xattr_bh);
Joel Beckerba937122008-10-24 19:13:20 -07002780 ocfs2_xattr_bucket_free(xbs.bucket);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002781
2782 return ret;
2783}
2784
Tao Ma0c044f02008-08-18 17:38:50 +08002785/*
2786 * Find the xattr extent rec which may contains name_hash.
2787 * e_cpos will be the first name hash of the xattr rec.
2788 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
2789 */
2790static int ocfs2_xattr_get_rec(struct inode *inode,
2791 u32 name_hash,
2792 u64 *p_blkno,
2793 u32 *e_cpos,
2794 u32 *num_clusters,
2795 struct ocfs2_extent_list *el)
2796{
2797 int ret = 0, i;
2798 struct buffer_head *eb_bh = NULL;
2799 struct ocfs2_extent_block *eb;
2800 struct ocfs2_extent_rec *rec = NULL;
2801 u64 e_blkno = 0;
2802
2803 if (el->l_tree_depth) {
2804 ret = ocfs2_find_leaf(inode, el, name_hash, &eb_bh);
2805 if (ret) {
2806 mlog_errno(ret);
2807 goto out;
2808 }
2809
2810 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
2811 el = &eb->h_list;
2812
2813 if (el->l_tree_depth) {
2814 ocfs2_error(inode->i_sb,
2815 "Inode %lu has non zero tree depth in "
2816 "xattr tree block %llu\n", inode->i_ino,
2817 (unsigned long long)eb_bh->b_blocknr);
2818 ret = -EROFS;
2819 goto out;
2820 }
2821 }
2822
2823 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
2824 rec = &el->l_recs[i];
2825
2826 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
2827 e_blkno = le64_to_cpu(rec->e_blkno);
2828 break;
2829 }
2830 }
2831
2832 if (!e_blkno) {
2833 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
2834 "record (%u, %u, 0) in xattr", inode->i_ino,
2835 le32_to_cpu(rec->e_cpos),
2836 ocfs2_rec_clusters(el, rec));
2837 ret = -EROFS;
2838 goto out;
2839 }
2840
2841 *p_blkno = le64_to_cpu(rec->e_blkno);
2842 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
2843 if (e_cpos)
2844 *e_cpos = le32_to_cpu(rec->e_cpos);
2845out:
2846 brelse(eb_bh);
2847 return ret;
2848}
2849
2850typedef int (xattr_bucket_func)(struct inode *inode,
2851 struct ocfs2_xattr_bucket *bucket,
2852 void *para);
2853
Tao Ma589dc262008-08-18 17:38:51 +08002854static int ocfs2_find_xe_in_bucket(struct inode *inode,
Joel Beckere2356a32008-10-27 15:01:54 -07002855 struct ocfs2_xattr_bucket *bucket,
Tao Ma589dc262008-08-18 17:38:51 +08002856 int name_index,
2857 const char *name,
2858 u32 name_hash,
2859 u16 *xe_index,
2860 int *found)
2861{
2862 int i, ret = 0, cmp = 1, block_off, new_offset;
Joel Beckere2356a32008-10-27 15:01:54 -07002863 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma589dc262008-08-18 17:38:51 +08002864 size_t name_len = strlen(name);
2865 struct ocfs2_xattr_entry *xe = NULL;
Tao Ma589dc262008-08-18 17:38:51 +08002866 char *xe_name;
2867
2868 /*
2869 * We don't use binary search in the bucket because there
2870 * may be multiple entries with the same name hash.
2871 */
2872 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
2873 xe = &xh->xh_entries[i];
2874
2875 if (name_hash > le32_to_cpu(xe->xe_name_hash))
2876 continue;
2877 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
2878 break;
2879
2880 cmp = name_index - ocfs2_xattr_get_type(xe);
2881 if (!cmp)
2882 cmp = name_len - xe->xe_name_len;
2883 if (cmp)
2884 continue;
2885
2886 ret = ocfs2_xattr_bucket_get_name_value(inode,
2887 xh,
2888 i,
2889 &block_off,
2890 &new_offset);
2891 if (ret) {
2892 mlog_errno(ret);
2893 break;
2894 }
2895
Joel Becker970e4932008-11-13 14:49:19 -08002896
Joel Beckere2356a32008-10-27 15:01:54 -07002897 xe_name = bucket_block(bucket, block_off) + new_offset;
2898 if (!memcmp(name, xe_name, name_len)) {
Tao Ma589dc262008-08-18 17:38:51 +08002899 *xe_index = i;
2900 *found = 1;
2901 ret = 0;
2902 break;
2903 }
2904 }
2905
2906 return ret;
2907}
2908
2909/*
2910 * Find the specified xattr entry in a series of buckets.
2911 * This series start from p_blkno and last for num_clusters.
2912 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
2913 * the num of the valid buckets.
2914 *
2915 * Return the buffer_head this xattr should reside in. And if the xattr's
2916 * hash is in the gap of 2 buckets, return the lower bucket.
2917 */
2918static int ocfs2_xattr_bucket_find(struct inode *inode,
2919 int name_index,
2920 const char *name,
2921 u32 name_hash,
2922 u64 p_blkno,
2923 u32 first_hash,
2924 u32 num_clusters,
2925 struct ocfs2_xattr_search *xs)
2926{
2927 int ret, found = 0;
Tao Ma589dc262008-08-18 17:38:51 +08002928 struct ocfs2_xattr_header *xh = NULL;
2929 struct ocfs2_xattr_entry *xe = NULL;
2930 u16 index = 0;
2931 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2932 int low_bucket = 0, bucket, high_bucket;
Joel Beckere2356a32008-10-27 15:01:54 -07002933 struct ocfs2_xattr_bucket *search;
Tao Ma589dc262008-08-18 17:38:51 +08002934 u32 last_hash;
Joel Beckere2356a32008-10-27 15:01:54 -07002935 u64 blkno, lower_blkno = 0;
Tao Ma589dc262008-08-18 17:38:51 +08002936
Joel Beckere2356a32008-10-27 15:01:54 -07002937 search = ocfs2_xattr_bucket_new(inode);
2938 if (!search) {
2939 ret = -ENOMEM;
2940 mlog_errno(ret);
2941 goto out;
2942 }
2943
2944 ret = ocfs2_read_xattr_bucket(search, p_blkno);
Tao Ma589dc262008-08-18 17:38:51 +08002945 if (ret) {
2946 mlog_errno(ret);
2947 goto out;
2948 }
2949
Joel Beckere2356a32008-10-27 15:01:54 -07002950 xh = bucket_xh(search);
Tao Ma589dc262008-08-18 17:38:51 +08002951 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
Tao Ma589dc262008-08-18 17:38:51 +08002952 while (low_bucket <= high_bucket) {
Joel Beckere2356a32008-10-27 15:01:54 -07002953 ocfs2_xattr_bucket_relse(search);
2954
Tao Ma589dc262008-08-18 17:38:51 +08002955 bucket = (low_bucket + high_bucket) / 2;
Tao Ma589dc262008-08-18 17:38:51 +08002956 blkno = p_blkno + bucket * blk_per_bucket;
Joel Beckere2356a32008-10-27 15:01:54 -07002957 ret = ocfs2_read_xattr_bucket(search, blkno);
Tao Ma589dc262008-08-18 17:38:51 +08002958 if (ret) {
2959 mlog_errno(ret);
2960 goto out;
2961 }
2962
Joel Beckere2356a32008-10-27 15:01:54 -07002963 xh = bucket_xh(search);
Tao Ma589dc262008-08-18 17:38:51 +08002964 xe = &xh->xh_entries[0];
2965 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
2966 high_bucket = bucket - 1;
2967 continue;
2968 }
2969
2970 /*
2971 * Check whether the hash of the last entry in our
Tao Ma5a095612008-09-19 22:17:41 +08002972 * bucket is larger than the search one. for an empty
2973 * bucket, the last one is also the first one.
Tao Ma589dc262008-08-18 17:38:51 +08002974 */
Tao Ma5a095612008-09-19 22:17:41 +08002975 if (xh->xh_count)
2976 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
2977
Tao Ma589dc262008-08-18 17:38:51 +08002978 last_hash = le32_to_cpu(xe->xe_name_hash);
2979
Joel Beckere2356a32008-10-27 15:01:54 -07002980 /* record lower_blkno which may be the insert place. */
2981 lower_blkno = blkno;
Tao Ma589dc262008-08-18 17:38:51 +08002982
2983 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
2984 low_bucket = bucket + 1;
2985 continue;
2986 }
2987
2988 /* the searched xattr should reside in this bucket if exists. */
Joel Beckere2356a32008-10-27 15:01:54 -07002989 ret = ocfs2_find_xe_in_bucket(inode, search,
Tao Ma589dc262008-08-18 17:38:51 +08002990 name_index, name, name_hash,
2991 &index, &found);
2992 if (ret) {
2993 mlog_errno(ret);
2994 goto out;
2995 }
2996 break;
2997 }
2998
2999 /*
3000 * Record the bucket we have found.
3001 * When the xattr's hash value is in the gap of 2 buckets, we will
3002 * always set it to the previous bucket.
3003 */
Joel Beckere2356a32008-10-27 15:01:54 -07003004 if (!lower_blkno)
3005 lower_blkno = p_blkno;
3006
3007 /* This should be in cache - we just read it during the search */
3008 ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
3009 if (ret) {
3010 mlog_errno(ret);
3011 goto out;
Tao Ma589dc262008-08-18 17:38:51 +08003012 }
Tao Ma589dc262008-08-18 17:38:51 +08003013
Joel Beckerba937122008-10-24 19:13:20 -07003014 xs->header = bucket_xh(xs->bucket);
3015 xs->base = bucket_block(xs->bucket, 0);
Tao Ma589dc262008-08-18 17:38:51 +08003016 xs->end = xs->base + inode->i_sb->s_blocksize;
3017
3018 if (found) {
Tao Ma589dc262008-08-18 17:38:51 +08003019 xs->here = &xs->header->xh_entries[index];
3020 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
Joel Beckerba937122008-10-24 19:13:20 -07003021 (unsigned long long)bucket_blkno(xs->bucket), index);
Tao Ma589dc262008-08-18 17:38:51 +08003022 } else
3023 ret = -ENODATA;
3024
3025out:
Joel Beckere2356a32008-10-27 15:01:54 -07003026 ocfs2_xattr_bucket_free(search);
Tao Ma589dc262008-08-18 17:38:51 +08003027 return ret;
3028}
3029
3030static int ocfs2_xattr_index_block_find(struct inode *inode,
3031 struct buffer_head *root_bh,
3032 int name_index,
3033 const char *name,
3034 struct ocfs2_xattr_search *xs)
3035{
3036 int ret;
3037 struct ocfs2_xattr_block *xb =
3038 (struct ocfs2_xattr_block *)root_bh->b_data;
3039 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3040 struct ocfs2_extent_list *el = &xb_root->xt_list;
3041 u64 p_blkno = 0;
3042 u32 first_hash, num_clusters = 0;
Tao Ma2057e5c2008-10-09 23:06:13 +08003043 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
Tao Ma589dc262008-08-18 17:38:51 +08003044
3045 if (le16_to_cpu(el->l_next_free_rec) == 0)
3046 return -ENODATA;
3047
3048 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
3049 name, name_hash, name_index);
3050
3051 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
3052 &num_clusters, el);
3053 if (ret) {
3054 mlog_errno(ret);
3055 goto out;
3056 }
3057
3058 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
3059
3060 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
Mark Fashehde29c082008-10-29 14:45:30 -07003061 "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
3062 first_hash);
Tao Ma589dc262008-08-18 17:38:51 +08003063
3064 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
3065 p_blkno, first_hash, num_clusters, xs);
3066
3067out:
3068 return ret;
3069}
3070
Tao Ma0c044f02008-08-18 17:38:50 +08003071static int ocfs2_iterate_xattr_buckets(struct inode *inode,
3072 u64 blkno,
3073 u32 clusters,
3074 xattr_bucket_func *func,
3075 void *para)
3076{
Joel Becker6dde41d2008-10-24 17:16:48 -07003077 int i, ret = 0;
Tao Ma0c044f02008-08-18 17:38:50 +08003078 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
3079 u32 num_buckets = clusters * bpc;
Joel Beckerba937122008-10-24 19:13:20 -07003080 struct ocfs2_xattr_bucket *bucket;
Tao Ma0c044f02008-08-18 17:38:50 +08003081
Joel Beckerba937122008-10-24 19:13:20 -07003082 bucket = ocfs2_xattr_bucket_new(inode);
3083 if (!bucket) {
3084 mlog_errno(-ENOMEM);
3085 return -ENOMEM;
3086 }
Tao Ma0c044f02008-08-18 17:38:50 +08003087
3088 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003089 clusters, (unsigned long long)blkno);
Tao Ma0c044f02008-08-18 17:38:50 +08003090
Joel Beckerba937122008-10-24 19:13:20 -07003091 for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
3092 ret = ocfs2_read_xattr_bucket(bucket, blkno);
Tao Ma0c044f02008-08-18 17:38:50 +08003093 if (ret) {
3094 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07003095 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003096 }
3097
Tao Ma0c044f02008-08-18 17:38:50 +08003098 /*
3099 * The real bucket num in this series of blocks is stored
3100 * in the 1st bucket.
3101 */
3102 if (i == 0)
Joel Beckerba937122008-10-24 19:13:20 -07003103 num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
Tao Ma0c044f02008-08-18 17:38:50 +08003104
Mark Fashehde29c082008-10-29 14:45:30 -07003105 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
3106 (unsigned long long)blkno,
Joel Beckerba937122008-10-24 19:13:20 -07003107 le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
Tao Ma0c044f02008-08-18 17:38:50 +08003108 if (func) {
Joel Beckerba937122008-10-24 19:13:20 -07003109 ret = func(inode, bucket, para);
3110 if (ret)
Tao Ma0c044f02008-08-18 17:38:50 +08003111 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07003112 /* Fall through to bucket_relse() */
Tao Ma0c044f02008-08-18 17:38:50 +08003113 }
3114
Joel Beckerba937122008-10-24 19:13:20 -07003115 ocfs2_xattr_bucket_relse(bucket);
3116 if (ret)
3117 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003118 }
3119
Joel Beckerba937122008-10-24 19:13:20 -07003120 ocfs2_xattr_bucket_free(bucket);
Tao Ma0c044f02008-08-18 17:38:50 +08003121 return ret;
3122}
3123
3124struct ocfs2_xattr_tree_list {
3125 char *buffer;
3126 size_t buffer_size;
Tao Ma936b8832008-10-09 23:06:14 +08003127 size_t result;
Tao Ma0c044f02008-08-18 17:38:50 +08003128};
3129
3130static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
3131 struct ocfs2_xattr_header *xh,
3132 int index,
3133 int *block_off,
3134 int *new_offset)
3135{
3136 u16 name_offset;
3137
3138 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
3139 return -EINVAL;
3140
3141 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
3142
3143 *block_off = name_offset >> inode->i_sb->s_blocksize_bits;
3144 *new_offset = name_offset % inode->i_sb->s_blocksize;
3145
3146 return 0;
3147}
3148
3149static int ocfs2_list_xattr_bucket(struct inode *inode,
3150 struct ocfs2_xattr_bucket *bucket,
3151 void *para)
3152{
Tao Ma936b8832008-10-09 23:06:14 +08003153 int ret = 0, type;
Tao Ma0c044f02008-08-18 17:38:50 +08003154 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
Tao Ma0c044f02008-08-18 17:38:50 +08003155 int i, block_off, new_offset;
Tao Ma936b8832008-10-09 23:06:14 +08003156 const char *prefix, *name;
Tao Ma0c044f02008-08-18 17:38:50 +08003157
Joel Becker3e632942008-10-24 17:04:49 -07003158 for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
3159 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
Tao Ma936b8832008-10-09 23:06:14 +08003160 type = ocfs2_xattr_get_type(entry);
3161 prefix = ocfs2_xattr_prefix(type);
Tao Ma0c044f02008-08-18 17:38:50 +08003162
Tao Ma936b8832008-10-09 23:06:14 +08003163 if (prefix) {
Tao Ma0c044f02008-08-18 17:38:50 +08003164 ret = ocfs2_xattr_bucket_get_name_value(inode,
Joel Becker3e632942008-10-24 17:04:49 -07003165 bucket_xh(bucket),
Tao Ma0c044f02008-08-18 17:38:50 +08003166 i,
3167 &block_off,
3168 &new_offset);
3169 if (ret)
3170 break;
Tao Ma936b8832008-10-09 23:06:14 +08003171
Joel Becker51def392008-10-24 16:57:21 -07003172 name = (const char *)bucket_block(bucket, block_off) +
Tao Ma936b8832008-10-09 23:06:14 +08003173 new_offset;
3174 ret = ocfs2_xattr_list_entry(xl->buffer,
3175 xl->buffer_size,
3176 &xl->result,
3177 prefix, name,
3178 entry->xe_name_len);
3179 if (ret)
3180 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003181 }
3182 }
3183
3184 return ret;
3185}
3186
3187static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
3188 struct ocfs2_xattr_tree_root *xt,
3189 char *buffer,
3190 size_t buffer_size)
3191{
3192 struct ocfs2_extent_list *el = &xt->xt_list;
3193 int ret = 0;
3194 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
3195 u64 p_blkno = 0;
3196 struct ocfs2_xattr_tree_list xl = {
3197 .buffer = buffer,
3198 .buffer_size = buffer_size,
Tao Ma936b8832008-10-09 23:06:14 +08003199 .result = 0,
Tao Ma0c044f02008-08-18 17:38:50 +08003200 };
3201
3202 if (le16_to_cpu(el->l_next_free_rec) == 0)
3203 return 0;
3204
3205 while (name_hash > 0) {
3206 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
3207 &e_cpos, &num_clusters, el);
3208 if (ret) {
3209 mlog_errno(ret);
3210 goto out;
3211 }
3212
3213 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
3214 ocfs2_list_xattr_bucket,
3215 &xl);
3216 if (ret) {
3217 mlog_errno(ret);
3218 goto out;
3219 }
3220
3221 if (e_cpos == 0)
3222 break;
3223
3224 name_hash = e_cpos - 1;
3225 }
3226
Tao Ma936b8832008-10-09 23:06:14 +08003227 ret = xl.result;
Tao Ma0c044f02008-08-18 17:38:50 +08003228out:
3229 return ret;
3230}
Tao Ma01225592008-08-18 17:38:53 +08003231
3232static int cmp_xe(const void *a, const void *b)
3233{
3234 const struct ocfs2_xattr_entry *l = a, *r = b;
3235 u32 l_hash = le32_to_cpu(l->xe_name_hash);
3236 u32 r_hash = le32_to_cpu(r->xe_name_hash);
3237
3238 if (l_hash > r_hash)
3239 return 1;
3240 if (l_hash < r_hash)
3241 return -1;
3242 return 0;
3243}
3244
3245static void swap_xe(void *a, void *b, int size)
3246{
3247 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
3248
3249 tmp = *l;
3250 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
3251 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
3252}
3253
3254/*
3255 * When the ocfs2_xattr_block is filled up, new bucket will be created
3256 * and all the xattr entries will be moved to the new bucket.
Joel Becker178eeac2008-10-27 15:18:29 -07003257 * The header goes at the start of the bucket, and the names+values are
3258 * filled from the end. This is why *target starts as the last buffer.
Tao Ma01225592008-08-18 17:38:53 +08003259 * Note: we need to sort the entries since they are not saved in order
3260 * in the ocfs2_xattr_block.
3261 */
3262static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
3263 struct buffer_head *xb_bh,
Joel Becker178eeac2008-10-27 15:18:29 -07003264 struct ocfs2_xattr_bucket *bucket)
Tao Ma01225592008-08-18 17:38:53 +08003265{
3266 int i, blocksize = inode->i_sb->s_blocksize;
Joel Becker178eeac2008-10-27 15:18:29 -07003267 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08003268 u16 offset, size, off_change;
3269 struct ocfs2_xattr_entry *xe;
3270 struct ocfs2_xattr_block *xb =
3271 (struct ocfs2_xattr_block *)xb_bh->b_data;
3272 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
Joel Becker178eeac2008-10-27 15:18:29 -07003273 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma01225592008-08-18 17:38:53 +08003274 u16 count = le16_to_cpu(xb_xh->xh_count);
Joel Becker178eeac2008-10-27 15:18:29 -07003275 char *src = xb_bh->b_data;
3276 char *target = bucket_block(bucket, blks - 1);
Tao Ma01225592008-08-18 17:38:53 +08003277
3278 mlog(0, "cp xattr from block %llu to bucket %llu\n",
3279 (unsigned long long)xb_bh->b_blocknr,
Joel Becker178eeac2008-10-27 15:18:29 -07003280 (unsigned long long)bucket_blkno(bucket));
Tao Ma01225592008-08-18 17:38:53 +08003281
Joel Becker178eeac2008-10-27 15:18:29 -07003282 for (i = 0; i < blks; i++)
3283 memset(bucket_block(bucket, i), 0, blocksize);
3284
Tao Ma01225592008-08-18 17:38:53 +08003285 /*
3286 * Since the xe_name_offset is based on ocfs2_xattr_header,
3287 * there is a offset change corresponding to the change of
3288 * ocfs2_xattr_header's position.
3289 */
3290 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
3291 xe = &xb_xh->xh_entries[count - 1];
3292 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
3293 size = blocksize - offset;
3294
3295 /* copy all the names and values. */
Tao Ma01225592008-08-18 17:38:53 +08003296 memcpy(target + offset, src + offset, size);
3297
3298 /* Init new header now. */
3299 xh->xh_count = xb_xh->xh_count;
3300 xh->xh_num_buckets = cpu_to_le16(1);
3301 xh->xh_name_value_len = cpu_to_le16(size);
3302 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
3303
3304 /* copy all the entries. */
Joel Becker178eeac2008-10-27 15:18:29 -07003305 target = bucket_block(bucket, 0);
Tao Ma01225592008-08-18 17:38:53 +08003306 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
3307 size = count * sizeof(struct ocfs2_xattr_entry);
3308 memcpy(target + offset, (char *)xb_xh + offset, size);
3309
3310 /* Change the xe offset for all the xe because of the move. */
3311 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
3312 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
3313 for (i = 0; i < count; i++)
3314 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
3315
3316 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
3317 offset, size, off_change);
3318
3319 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
3320 cmp_xe, swap_xe);
3321}
3322
3323/*
3324 * After we move xattr from block to index btree, we have to
3325 * update ocfs2_xattr_search to the new xe and base.
3326 *
3327 * When the entry is in xattr block, xattr_bh indicates the storage place.
3328 * While if the entry is in index b-tree, "bucket" indicates the
3329 * real place of the xattr.
3330 */
Joel Becker178eeac2008-10-27 15:18:29 -07003331static void ocfs2_xattr_update_xattr_search(struct inode *inode,
3332 struct ocfs2_xattr_search *xs,
3333 struct buffer_head *old_bh)
Tao Ma01225592008-08-18 17:38:53 +08003334{
Tao Ma01225592008-08-18 17:38:53 +08003335 char *buf = old_bh->b_data;
3336 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
3337 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
Joel Becker178eeac2008-10-27 15:18:29 -07003338 int i;
Tao Ma01225592008-08-18 17:38:53 +08003339
Joel Beckerba937122008-10-24 19:13:20 -07003340 xs->header = bucket_xh(xs->bucket);
Joel Becker178eeac2008-10-27 15:18:29 -07003341 xs->base = bucket_block(xs->bucket, 0);
Tao Ma01225592008-08-18 17:38:53 +08003342 xs->end = xs->base + inode->i_sb->s_blocksize;
3343
Joel Becker178eeac2008-10-27 15:18:29 -07003344 if (xs->not_found)
3345 return;
Tao Ma01225592008-08-18 17:38:53 +08003346
Joel Becker178eeac2008-10-27 15:18:29 -07003347 i = xs->here - old_xh->xh_entries;
3348 xs->here = &xs->header->xh_entries[i];
Tao Ma01225592008-08-18 17:38:53 +08003349}
3350
3351static int ocfs2_xattr_create_index_block(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +08003352 struct ocfs2_xattr_search *xs,
3353 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08003354{
Tao Ma85db90e2008-11-12 08:27:01 +08003355 int ret;
Tao Ma01225592008-08-18 17:38:53 +08003356 u32 bit_off, len;
3357 u64 blkno;
Tao Ma85db90e2008-11-12 08:27:01 +08003358 handle_t *handle = ctxt->handle;
Tao Ma01225592008-08-18 17:38:53 +08003359 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3360 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Tao Ma01225592008-08-18 17:38:53 +08003361 struct buffer_head *xb_bh = xs->xattr_bh;
3362 struct ocfs2_xattr_block *xb =
3363 (struct ocfs2_xattr_block *)xb_bh->b_data;
3364 struct ocfs2_xattr_tree_root *xr;
3365 u16 xb_flags = le16_to_cpu(xb->xb_flags);
Tao Ma01225592008-08-18 17:38:53 +08003366
3367 mlog(0, "create xattr index block for %llu\n",
3368 (unsigned long long)xb_bh->b_blocknr);
3369
3370 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
Joel Becker178eeac2008-10-27 15:18:29 -07003371 BUG_ON(!xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08003372
Tao Ma01225592008-08-18 17:38:53 +08003373 /*
3374 * XXX:
3375 * We can use this lock for now, and maybe move to a dedicated mutex
3376 * if performance becomes a problem later.
3377 */
3378 down_write(&oi->ip_alloc_sem);
3379
Joel Becker84008972008-12-09 16:11:49 -08003380 ret = ocfs2_journal_access_xb(handle, inode, xb_bh,
3381 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08003382 if (ret) {
3383 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003384 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003385 }
3386
Tao Ma78f30c32008-11-12 08:27:00 +08003387 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac,
3388 1, 1, &bit_off, &len);
Tao Ma01225592008-08-18 17:38:53 +08003389 if (ret) {
3390 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003391 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003392 }
3393
3394 /*
3395 * The bucket may spread in many blocks, and
3396 * we will only touch the 1st block and the last block
3397 * in the whole bucket(one for entry and one for data).
3398 */
3399 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
3400
Mark Fashehde29c082008-10-29 14:45:30 -07003401 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
3402 (unsigned long long)blkno);
Tao Ma01225592008-08-18 17:38:53 +08003403
Joel Becker178eeac2008-10-27 15:18:29 -07003404 ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
Tao Ma01225592008-08-18 17:38:53 +08003405 if (ret) {
3406 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003407 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003408 }
3409
Joel Becker178eeac2008-10-27 15:18:29 -07003410 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
3411 OCFS2_JOURNAL_ACCESS_CREATE);
Joel Beckerbd60bd32008-10-20 18:25:56 -07003412 if (ret) {
3413 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003414 goto out;
Joel Beckerbd60bd32008-10-20 18:25:56 -07003415 }
Tao Ma01225592008-08-18 17:38:53 +08003416
Joel Becker178eeac2008-10-27 15:18:29 -07003417 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
3418 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
3419
3420 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
3421
Tao Ma01225592008-08-18 17:38:53 +08003422 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
3423 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
3424 offsetof(struct ocfs2_xattr_block, xb_attrs));
3425
3426 xr = &xb->xb_attrs.xb_root;
3427 xr->xt_clusters = cpu_to_le32(1);
3428 xr->xt_last_eb_blk = 0;
3429 xr->xt_list.l_tree_depth = 0;
3430 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
3431 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
3432
3433 xr->xt_list.l_recs[0].e_cpos = 0;
3434 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
3435 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
3436
3437 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
3438
Tao Ma85db90e2008-11-12 08:27:01 +08003439 ocfs2_journal_dirty(handle, xb_bh);
Tao Ma01225592008-08-18 17:38:53 +08003440
Tao Ma85db90e2008-11-12 08:27:01 +08003441out:
Tao Ma01225592008-08-18 17:38:53 +08003442 up_write(&oi->ip_alloc_sem);
3443
Tao Ma01225592008-08-18 17:38:53 +08003444 return ret;
3445}
3446
3447static int cmp_xe_offset(const void *a, const void *b)
3448{
3449 const struct ocfs2_xattr_entry *l = a, *r = b;
3450 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
3451 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
3452
3453 if (l_name_offset < r_name_offset)
3454 return 1;
3455 if (l_name_offset > r_name_offset)
3456 return -1;
3457 return 0;
3458}
3459
3460/*
3461 * defrag a xattr bucket if we find that the bucket has some
3462 * holes beteen name/value pairs.
3463 * We will move all the name/value pairs to the end of the bucket
3464 * so that we can spare some space for insertion.
3465 */
3466static int ocfs2_defrag_xattr_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08003467 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08003468 struct ocfs2_xattr_bucket *bucket)
3469{
3470 int ret, i;
3471 size_t end, offset, len, value_len;
3472 struct ocfs2_xattr_header *xh;
3473 char *entries, *buf, *bucket_buf = NULL;
Joel Becker9c7759a2008-10-24 16:21:03 -07003474 u64 blkno = bucket_blkno(bucket);
Tao Ma01225592008-08-18 17:38:53 +08003475 u16 xh_free_start;
Tao Ma01225592008-08-18 17:38:53 +08003476 size_t blocksize = inode->i_sb->s_blocksize;
Tao Ma01225592008-08-18 17:38:53 +08003477 struct ocfs2_xattr_entry *xe;
Tao Ma01225592008-08-18 17:38:53 +08003478
3479 /*
3480 * In order to make the operation more efficient and generic,
3481 * we copy all the blocks into a contiguous memory and do the
3482 * defragment there, so if anything is error, we will not touch
3483 * the real block.
3484 */
3485 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
3486 if (!bucket_buf) {
3487 ret = -EIO;
3488 goto out;
3489 }
3490
Joel Becker161d6f32008-10-27 15:25:18 -07003491 buf = bucket_buf;
Tao Ma1c32a2f2008-11-06 08:10:47 +08003492 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
3493 memcpy(buf, bucket_block(bucket, i), blocksize);
Joel Becker161d6f32008-10-27 15:25:18 -07003494
Tao Ma1c32a2f2008-11-06 08:10:47 +08003495 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
Joel Becker161d6f32008-10-27 15:25:18 -07003496 OCFS2_JOURNAL_ACCESS_WRITE);
3497 if (ret < 0) {
3498 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003499 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003500 }
3501
3502 xh = (struct ocfs2_xattr_header *)bucket_buf;
3503 entries = (char *)xh->xh_entries;
3504 xh_free_start = le16_to_cpu(xh->xh_free_start);
3505
3506 mlog(0, "adjust xattr bucket in %llu, count = %u, "
3507 "xh_free_start = %u, xh_name_value_len = %u.\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003508 (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
3509 xh_free_start, le16_to_cpu(xh->xh_name_value_len));
Tao Ma01225592008-08-18 17:38:53 +08003510
3511 /*
3512 * sort all the entries by their offset.
3513 * the largest will be the first, so that we can
3514 * move them to the end one by one.
3515 */
3516 sort(entries, le16_to_cpu(xh->xh_count),
3517 sizeof(struct ocfs2_xattr_entry),
3518 cmp_xe_offset, swap_xe);
3519
3520 /* Move all name/values to the end of the bucket. */
3521 xe = xh->xh_entries;
3522 end = OCFS2_XATTR_BUCKET_SIZE;
3523 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
3524 offset = le16_to_cpu(xe->xe_name_offset);
3525 if (ocfs2_xattr_is_local(xe))
3526 value_len = OCFS2_XATTR_SIZE(
3527 le64_to_cpu(xe->xe_value_size));
3528 else
3529 value_len = OCFS2_XATTR_ROOT_SIZE;
3530 len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
3531
3532 /*
3533 * We must make sure that the name/value pair
3534 * exist in the same block. So adjust end to
3535 * the previous block end if needed.
3536 */
3537 if (((end - len) / blocksize !=
3538 (end - 1) / blocksize))
3539 end = end - end % blocksize;
3540
3541 if (end > offset + len) {
3542 memmove(bucket_buf + end - len,
3543 bucket_buf + offset, len);
3544 xe->xe_name_offset = cpu_to_le16(end - len);
3545 }
3546
3547 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
3548 "bucket %llu\n", (unsigned long long)blkno);
3549
3550 end -= len;
3551 }
3552
3553 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
3554 "bucket %llu\n", (unsigned long long)blkno);
3555
3556 if (xh_free_start == end)
Tao Ma85db90e2008-11-12 08:27:01 +08003557 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003558
3559 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
3560 xh->xh_free_start = cpu_to_le16(end);
3561
3562 /* sort the entries by their name_hash. */
3563 sort(entries, le16_to_cpu(xh->xh_count),
3564 sizeof(struct ocfs2_xattr_entry),
3565 cmp_xe, swap_xe);
3566
3567 buf = bucket_buf;
Tao Ma1c32a2f2008-11-06 08:10:47 +08003568 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
3569 memcpy(bucket_block(bucket, i), buf, blocksize);
3570 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
Tao Ma01225592008-08-18 17:38:53 +08003571
Tao Ma01225592008-08-18 17:38:53 +08003572out:
Tao Ma01225592008-08-18 17:38:53 +08003573 kfree(bucket_buf);
3574 return ret;
3575}
3576
3577/*
Joel Beckerb5c03e72008-11-25 19:58:16 -08003578 * prev_blkno points to the start of an existing extent. new_blkno
3579 * points to a newly allocated extent. Because we know each of our
3580 * clusters contains more than bucket, we can easily split one cluster
3581 * at a bucket boundary. So we take the last cluster of the existing
3582 * extent and split it down the middle. We move the last half of the
3583 * buckets in the last cluster of the existing extent over to the new
3584 * extent.
Tao Ma01225592008-08-18 17:38:53 +08003585 *
Joel Beckerb5c03e72008-11-25 19:58:16 -08003586 * first_bh is the buffer at prev_blkno so we can update the existing
3587 * extent's bucket count. header_bh is the bucket were we were hoping
3588 * to insert our xattr. If the bucket move places the target in the new
3589 * extent, we'll update first_bh and header_bh after modifying the old
3590 * extent.
3591 *
3592 * first_hash will be set as the 1st xe's name_hash in the new extent.
Tao Ma01225592008-08-18 17:38:53 +08003593 */
3594static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
3595 handle_t *handle,
Joel Becker41cb8142008-11-26 14:25:21 -08003596 struct ocfs2_xattr_bucket *first,
3597 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08003598 u64 new_blkno,
Tao Ma01225592008-08-18 17:38:53 +08003599 u32 num_clusters,
3600 u32 *first_hash)
3601{
Joel Beckerc58b6032008-11-26 13:36:24 -08003602 int ret;
Joel Becker41cb8142008-11-26 14:25:21 -08003603 struct super_block *sb = inode->i_sb;
3604 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb);
3605 int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
Joel Beckerb5c03e72008-11-25 19:58:16 -08003606 int to_move = num_buckets / 2;
Joel Beckerc58b6032008-11-26 13:36:24 -08003607 u64 src_blkno;
Joel Becker41cb8142008-11-26 14:25:21 -08003608 u64 last_cluster_blkno = bucket_blkno(first) +
3609 ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1));
Tao Ma01225592008-08-18 17:38:53 +08003610
Joel Becker41cb8142008-11-26 14:25:21 -08003611 BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets);
3612 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize);
Tao Ma01225592008-08-18 17:38:53 +08003613
Tao Ma01225592008-08-18 17:38:53 +08003614 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
Joel Beckerc58b6032008-11-26 13:36:24 -08003615 (unsigned long long)last_cluster_blkno, (unsigned long long)new_blkno);
Tao Ma01225592008-08-18 17:38:53 +08003616
Joel Becker41cb8142008-11-26 14:25:21 -08003617 ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first),
Joel Beckerc58b6032008-11-26 13:36:24 -08003618 last_cluster_blkno, new_blkno,
3619 to_move, first_hash);
Joel Beckerb5c03e72008-11-25 19:58:16 -08003620 if (ret) {
3621 mlog_errno(ret);
3622 goto out;
3623 }
3624
Joel Beckerc58b6032008-11-26 13:36:24 -08003625 /* This is the first bucket that got moved */
3626 src_blkno = last_cluster_blkno + (to_move * blks_per_bucket);
3627
Tao Ma01225592008-08-18 17:38:53 +08003628 /*
Joel Beckerc58b6032008-11-26 13:36:24 -08003629 * If the target bucket was part of the moved buckets, we need to
Joel Becker41cb8142008-11-26 14:25:21 -08003630 * update first and target.
Joel Beckerb5c03e72008-11-25 19:58:16 -08003631 */
Joel Becker41cb8142008-11-26 14:25:21 -08003632 if (bucket_blkno(target) >= src_blkno) {
Joel Beckerb5c03e72008-11-25 19:58:16 -08003633 /* Find the block for the new target bucket */
3634 src_blkno = new_blkno +
Joel Becker41cb8142008-11-26 14:25:21 -08003635 (bucket_blkno(target) - src_blkno);
3636
3637 ocfs2_xattr_bucket_relse(first);
3638 ocfs2_xattr_bucket_relse(target);
Joel Beckerb5c03e72008-11-25 19:58:16 -08003639
3640 /*
Joel Beckerc58b6032008-11-26 13:36:24 -08003641 * These shouldn't fail - the buffers are in the
Joel Beckerb5c03e72008-11-25 19:58:16 -08003642 * journal from ocfs2_cp_xattr_bucket().
3643 */
Joel Becker41cb8142008-11-26 14:25:21 -08003644 ret = ocfs2_read_xattr_bucket(first, new_blkno);
Joel Beckerc58b6032008-11-26 13:36:24 -08003645 if (ret) {
3646 mlog_errno(ret);
3647 goto out;
3648 }
Joel Becker41cb8142008-11-26 14:25:21 -08003649 ret = ocfs2_read_xattr_bucket(target, src_blkno);
3650 if (ret)
Joel Beckerb5c03e72008-11-25 19:58:16 -08003651 mlog_errno(ret);
Joel Beckerb5c03e72008-11-25 19:58:16 -08003652
Joel Beckerb5c03e72008-11-25 19:58:16 -08003653 }
3654
Tao Ma01225592008-08-18 17:38:53 +08003655out:
Tao Ma01225592008-08-18 17:38:53 +08003656 return ret;
3657}
3658
Tao Ma01225592008-08-18 17:38:53 +08003659/*
Tao Ma80bcaf32008-10-27 06:06:24 +08003660 * Find the suitable pos when we divide a bucket into 2.
3661 * We have to make sure the xattrs with the same hash value exist
3662 * in the same bucket.
3663 *
3664 * If this ocfs2_xattr_header covers more than one hash value, find a
3665 * place where the hash value changes. Try to find the most even split.
3666 * The most common case is that all entries have different hash values,
3667 * and the first check we make will find a place to split.
Tao Ma01225592008-08-18 17:38:53 +08003668 */
Tao Ma80bcaf32008-10-27 06:06:24 +08003669static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
3670{
3671 struct ocfs2_xattr_entry *entries = xh->xh_entries;
3672 int count = le16_to_cpu(xh->xh_count);
3673 int delta, middle = count / 2;
3674
3675 /*
3676 * We start at the middle. Each step gets farther away in both
3677 * directions. We therefore hit the change in hash value
3678 * nearest to the middle. Note that this loop does not execute for
3679 * count < 2.
3680 */
3681 for (delta = 0; delta < middle; delta++) {
3682 /* Let's check delta earlier than middle */
3683 if (cmp_xe(&entries[middle - delta - 1],
3684 &entries[middle - delta]))
3685 return middle - delta;
3686
3687 /* For even counts, don't walk off the end */
3688 if ((middle + delta + 1) == count)
3689 continue;
3690
3691 /* Now try delta past middle */
3692 if (cmp_xe(&entries[middle + delta],
3693 &entries[middle + delta + 1]))
3694 return middle + delta + 1;
3695 }
3696
3697 /* Every entry had the same hash */
3698 return count;
3699}
3700
3701/*
3702 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
3703 * first_hash will record the 1st hash of the new bucket.
3704 *
3705 * Normally half of the xattrs will be moved. But we have to make
3706 * sure that the xattrs with the same hash value are stored in the
3707 * same bucket. If all the xattrs in this bucket have the same hash
3708 * value, the new bucket will be initialized as an empty one and the
3709 * first_hash will be initialized as (hash_value+1).
3710 */
3711static int ocfs2_divide_xattr_bucket(struct inode *inode,
3712 handle_t *handle,
3713 u64 blk,
3714 u64 new_blk,
3715 u32 *first_hash,
3716 int new_bucket_head)
Tao Ma01225592008-08-18 17:38:53 +08003717{
3718 int ret, i;
Tao Ma80bcaf32008-10-27 06:06:24 +08003719 int count, start, len, name_value_len = 0, xe_len, name_offset = 0;
Joel Beckerba937122008-10-24 19:13:20 -07003720 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
Tao Ma01225592008-08-18 17:38:53 +08003721 struct ocfs2_xattr_header *xh;
3722 struct ocfs2_xattr_entry *xe;
3723 int blocksize = inode->i_sb->s_blocksize;
3724
Tao Ma80bcaf32008-10-27 06:06:24 +08003725 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003726 (unsigned long long)blk, (unsigned long long)new_blk);
Tao Ma01225592008-08-18 17:38:53 +08003727
Joel Beckerba937122008-10-24 19:13:20 -07003728 s_bucket = ocfs2_xattr_bucket_new(inode);
3729 t_bucket = ocfs2_xattr_bucket_new(inode);
3730 if (!s_bucket || !t_bucket) {
3731 ret = -ENOMEM;
3732 mlog_errno(ret);
3733 goto out;
3734 }
Tao Ma01225592008-08-18 17:38:53 +08003735
Joel Beckerba937122008-10-24 19:13:20 -07003736 ret = ocfs2_read_xattr_bucket(s_bucket, blk);
Tao Ma01225592008-08-18 17:38:53 +08003737 if (ret) {
3738 mlog_errno(ret);
3739 goto out;
3740 }
3741
Joel Beckerba937122008-10-24 19:13:20 -07003742 ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07003743 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08003744 if (ret) {
3745 mlog_errno(ret);
3746 goto out;
3747 }
3748
Joel Becker784b8162008-10-24 17:33:40 -07003749 /*
3750 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
3751 * there's no need to read it.
3752 */
Joel Beckerba937122008-10-24 19:13:20 -07003753 ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
Tao Ma01225592008-08-18 17:38:53 +08003754 if (ret) {
3755 mlog_errno(ret);
3756 goto out;
3757 }
3758
Joel Becker2b656c12008-11-25 19:00:15 -08003759 /*
3760 * Hey, if we're overwriting t_bucket, what difference does
3761 * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the
3762 * same part of ocfs2_cp_xattr_bucket().
3763 */
Joel Beckerba937122008-10-24 19:13:20 -07003764 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07003765 new_bucket_head ?
3766 OCFS2_JOURNAL_ACCESS_CREATE :
3767 OCFS2_JOURNAL_ACCESS_WRITE);
3768 if (ret) {
3769 mlog_errno(ret);
3770 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003771 }
3772
Joel Beckerba937122008-10-24 19:13:20 -07003773 xh = bucket_xh(s_bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08003774 count = le16_to_cpu(xh->xh_count);
3775 start = ocfs2_xattr_find_divide_pos(xh);
3776
3777 if (start == count) {
3778 xe = &xh->xh_entries[start-1];
3779
3780 /*
3781 * initialized a new empty bucket here.
3782 * The hash value is set as one larger than
3783 * that of the last entry in the previous bucket.
3784 */
Joel Beckerba937122008-10-24 19:13:20 -07003785 for (i = 0; i < t_bucket->bu_blocks; i++)
3786 memset(bucket_block(t_bucket, i), 0, blocksize);
Tao Ma80bcaf32008-10-27 06:06:24 +08003787
Joel Beckerba937122008-10-24 19:13:20 -07003788 xh = bucket_xh(t_bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08003789 xh->xh_free_start = cpu_to_le16(blocksize);
3790 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
3791 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
3792
3793 goto set_num_buckets;
3794 }
3795
Tao Ma01225592008-08-18 17:38:53 +08003796 /* copy the whole bucket to the new first. */
Joel Beckerba937122008-10-24 19:13:20 -07003797 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003798
3799 /* update the new bucket. */
Joel Beckerba937122008-10-24 19:13:20 -07003800 xh = bucket_xh(t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003801
3802 /*
3803 * Calculate the total name/value len and xh_free_start for
3804 * the old bucket first.
3805 */
3806 name_offset = OCFS2_XATTR_BUCKET_SIZE;
3807 name_value_len = 0;
3808 for (i = 0; i < start; i++) {
3809 xe = &xh->xh_entries[i];
3810 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3811 if (ocfs2_xattr_is_local(xe))
3812 xe_len +=
3813 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3814 else
3815 xe_len += OCFS2_XATTR_ROOT_SIZE;
3816 name_value_len += xe_len;
3817 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
3818 name_offset = le16_to_cpu(xe->xe_name_offset);
3819 }
3820
3821 /*
3822 * Now begin the modification to the new bucket.
3823 *
3824 * In the new bucket, We just move the xattr entry to the beginning
3825 * and don't touch the name/value. So there will be some holes in the
3826 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
3827 * called.
3828 */
3829 xe = &xh->xh_entries[start];
3830 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
3831 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
Mark Fashehff1ec202008-08-19 10:54:29 -07003832 (int)((char *)xe - (char *)xh),
3833 (int)((char *)xh->xh_entries - (char *)xh));
Tao Ma01225592008-08-18 17:38:53 +08003834 memmove((char *)xh->xh_entries, (char *)xe, len);
3835 xe = &xh->xh_entries[count - start];
3836 len = sizeof(struct ocfs2_xattr_entry) * start;
3837 memset((char *)xe, 0, len);
3838
3839 le16_add_cpu(&xh->xh_count, -start);
3840 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
3841
3842 /* Calculate xh_free_start for the new bucket. */
3843 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3844 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3845 xe = &xh->xh_entries[i];
3846 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3847 if (ocfs2_xattr_is_local(xe))
3848 xe_len +=
3849 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3850 else
3851 xe_len += OCFS2_XATTR_ROOT_SIZE;
3852 if (le16_to_cpu(xe->xe_name_offset) <
3853 le16_to_cpu(xh->xh_free_start))
3854 xh->xh_free_start = xe->xe_name_offset;
3855 }
3856
Tao Ma80bcaf32008-10-27 06:06:24 +08003857set_num_buckets:
Tao Ma01225592008-08-18 17:38:53 +08003858 /* set xh->xh_num_buckets for the new xh. */
3859 if (new_bucket_head)
3860 xh->xh_num_buckets = cpu_to_le16(1);
3861 else
3862 xh->xh_num_buckets = 0;
3863
Joel Beckerba937122008-10-24 19:13:20 -07003864 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003865
3866 /* store the first_hash of the new bucket. */
3867 if (first_hash)
3868 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3869
3870 /*
Tao Ma80bcaf32008-10-27 06:06:24 +08003871 * Now only update the 1st block of the old bucket. If we
3872 * just added a new empty bucket, there is no need to modify
3873 * it.
Tao Ma01225592008-08-18 17:38:53 +08003874 */
Tao Ma80bcaf32008-10-27 06:06:24 +08003875 if (start == count)
3876 goto out;
3877
Joel Beckerba937122008-10-24 19:13:20 -07003878 xh = bucket_xh(s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003879 memset(&xh->xh_entries[start], 0,
3880 sizeof(struct ocfs2_xattr_entry) * (count - start));
3881 xh->xh_count = cpu_to_le16(start);
3882 xh->xh_free_start = cpu_to_le16(name_offset);
3883 xh->xh_name_value_len = cpu_to_le16(name_value_len);
3884
Joel Beckerba937122008-10-24 19:13:20 -07003885 ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003886
3887out:
Joel Beckerba937122008-10-24 19:13:20 -07003888 ocfs2_xattr_bucket_free(s_bucket);
3889 ocfs2_xattr_bucket_free(t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003890
3891 return ret;
3892}
3893
3894/*
3895 * Copy xattr from one bucket to another bucket.
3896 *
3897 * The caller must make sure that the journal transaction
3898 * has enough space for journaling.
3899 */
3900static int ocfs2_cp_xattr_bucket(struct inode *inode,
3901 handle_t *handle,
3902 u64 s_blkno,
3903 u64 t_blkno,
3904 int t_is_new)
3905{
Joel Becker4980c6d2008-10-24 18:54:43 -07003906 int ret;
Joel Beckerba937122008-10-24 19:13:20 -07003907 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
Tao Ma01225592008-08-18 17:38:53 +08003908
3909 BUG_ON(s_blkno == t_blkno);
3910
3911 mlog(0, "cp bucket %llu to %llu, target is %d\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003912 (unsigned long long)s_blkno, (unsigned long long)t_blkno,
3913 t_is_new);
Tao Ma01225592008-08-18 17:38:53 +08003914
Joel Beckerba937122008-10-24 19:13:20 -07003915 s_bucket = ocfs2_xattr_bucket_new(inode);
3916 t_bucket = ocfs2_xattr_bucket_new(inode);
3917 if (!s_bucket || !t_bucket) {
3918 ret = -ENOMEM;
3919 mlog_errno(ret);
3920 goto out;
3921 }
Joel Becker92de1092008-11-25 17:06:40 -08003922
Joel Beckerba937122008-10-24 19:13:20 -07003923 ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
Tao Ma01225592008-08-18 17:38:53 +08003924 if (ret)
3925 goto out;
3926
Joel Becker784b8162008-10-24 17:33:40 -07003927 /*
3928 * Even if !t_is_new, we're overwriting t_bucket. Thus,
3929 * there's no need to read it.
3930 */
Joel Beckerba937122008-10-24 19:13:20 -07003931 ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
Tao Ma01225592008-08-18 17:38:53 +08003932 if (ret)
3933 goto out;
3934
Joel Becker2b656c12008-11-25 19:00:15 -08003935 /*
3936 * Hey, if we're overwriting t_bucket, what difference does
3937 * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new
Joel Becker874d65a2008-11-26 13:02:18 -08003938 * cluster to fill, we came here from
3939 * ocfs2_mv_xattr_buckets(), and it is really new -
3940 * ACCESS_CREATE is required. But we also might have moved data
3941 * out of t_bucket before extending back into it.
3942 * ocfs2_add_new_xattr_bucket() can do this - its call to
3943 * ocfs2_add_new_xattr_cluster() may have created a new extent
Joel Becker2b656c12008-11-25 19:00:15 -08003944 * and copied out the end of the old extent. Then it re-extends
3945 * the old extent back to create space for new xattrs. That's
3946 * how we get here, and the bucket isn't really new.
3947 */
Joel Beckerba937122008-10-24 19:13:20 -07003948 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07003949 t_is_new ?
3950 OCFS2_JOURNAL_ACCESS_CREATE :
3951 OCFS2_JOURNAL_ACCESS_WRITE);
3952 if (ret)
3953 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003954
Joel Beckerba937122008-10-24 19:13:20 -07003955 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
3956 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003957
3958out:
Joel Beckerba937122008-10-24 19:13:20 -07003959 ocfs2_xattr_bucket_free(t_bucket);
3960 ocfs2_xattr_bucket_free(s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003961
3962 return ret;
3963}
3964
3965/*
Joel Becker874d65a2008-11-26 13:02:18 -08003966 * src_blk points to the start of an existing extent. last_blk points to
3967 * last cluster in that extent. to_blk points to a newly allocated
Joel Becker54ecb6b2008-11-26 13:18:31 -08003968 * extent. We copy the buckets from the cluster at last_blk to the new
3969 * extent. If start_bucket is non-zero, we skip that many buckets before
3970 * we start copying. The new extent's xh_num_buckets gets set to the
3971 * number of buckets we copied. The old extent's xh_num_buckets shrinks
3972 * by the same amount.
Tao Ma01225592008-08-18 17:38:53 +08003973 */
Joel Becker54ecb6b2008-11-26 13:18:31 -08003974static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
3975 u64 src_blk, u64 last_blk, u64 to_blk,
3976 unsigned int start_bucket,
3977 u32 *first_hash)
Tao Ma01225592008-08-18 17:38:53 +08003978{
3979 int i, ret, credits;
3980 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Joel Becker15d60922008-11-25 18:36:42 -08003981 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08003982 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
Joel Becker15d60922008-11-25 18:36:42 -08003983 struct ocfs2_xattr_bucket *old_first, *new_first;
Tao Ma01225592008-08-18 17:38:53 +08003984
Joel Becker874d65a2008-11-26 13:02:18 -08003985 mlog(0, "mv xattrs from cluster %llu to %llu\n",
3986 (unsigned long long)last_blk, (unsigned long long)to_blk);
Tao Ma01225592008-08-18 17:38:53 +08003987
Joel Becker54ecb6b2008-11-26 13:18:31 -08003988 BUG_ON(start_bucket >= num_buckets);
3989 if (start_bucket) {
3990 num_buckets -= start_bucket;
3991 last_blk += (start_bucket * blks_per_bucket);
3992 }
3993
Joel Becker15d60922008-11-25 18:36:42 -08003994 /* The first bucket of the original extent */
3995 old_first = ocfs2_xattr_bucket_new(inode);
3996 /* The first bucket of the new extent */
3997 new_first = ocfs2_xattr_bucket_new(inode);
3998 if (!old_first || !new_first) {
3999 ret = -ENOMEM;
4000 mlog_errno(ret);
4001 goto out;
4002 }
4003
Joel Becker874d65a2008-11-26 13:02:18 -08004004 ret = ocfs2_read_xattr_bucket(old_first, src_blk);
Joel Becker15d60922008-11-25 18:36:42 -08004005 if (ret) {
4006 mlog_errno(ret);
4007 goto out;
4008 }
4009
Tao Ma01225592008-08-18 17:38:53 +08004010 /*
Joel Becker54ecb6b2008-11-26 13:18:31 -08004011 * We need to update the first bucket of the old extent and all
4012 * the buckets going to the new extent.
Tao Ma01225592008-08-18 17:38:53 +08004013 */
Joel Becker54ecb6b2008-11-26 13:18:31 -08004014 credits = ((num_buckets + 1) * blks_per_bucket) +
4015 handle->h_buffer_credits;
Tao Ma01225592008-08-18 17:38:53 +08004016 ret = ocfs2_extend_trans(handle, credits);
4017 if (ret) {
4018 mlog_errno(ret);
4019 goto out;
4020 }
4021
Joel Becker15d60922008-11-25 18:36:42 -08004022 ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
4023 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004024 if (ret) {
4025 mlog_errno(ret);
4026 goto out;
4027 }
4028
4029 for (i = 0; i < num_buckets; i++) {
4030 ret = ocfs2_cp_xattr_bucket(inode, handle,
Joel Becker874d65a2008-11-26 13:02:18 -08004031 last_blk + (i * blks_per_bucket),
Joel Becker15d60922008-11-25 18:36:42 -08004032 to_blk + (i * blks_per_bucket),
4033 1);
Tao Ma01225592008-08-18 17:38:53 +08004034 if (ret) {
4035 mlog_errno(ret);
4036 goto out;
4037 }
Tao Ma01225592008-08-18 17:38:53 +08004038 }
4039
Joel Becker15d60922008-11-25 18:36:42 -08004040 /*
4041 * Get the new bucket ready before we dirty anything
4042 * (This actually shouldn't fail, because we already dirtied
4043 * it once in ocfs2_cp_xattr_bucket()).
4044 */
4045 ret = ocfs2_read_xattr_bucket(new_first, to_blk);
4046 if (ret) {
Tao Ma01225592008-08-18 17:38:53 +08004047 mlog_errno(ret);
4048 goto out;
4049 }
Joel Becker15d60922008-11-25 18:36:42 -08004050 ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
4051 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004052 if (ret) {
4053 mlog_errno(ret);
4054 goto out;
4055 }
4056
Joel Becker15d60922008-11-25 18:36:42 -08004057 /* Now update the headers */
4058 le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets);
4059 ocfs2_xattr_bucket_journal_dirty(handle, old_first);
Tao Ma01225592008-08-18 17:38:53 +08004060
Joel Becker15d60922008-11-25 18:36:42 -08004061 bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets);
4062 ocfs2_xattr_bucket_journal_dirty(handle, new_first);
Tao Ma01225592008-08-18 17:38:53 +08004063
4064 if (first_hash)
Joel Becker15d60922008-11-25 18:36:42 -08004065 *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash);
4066
Tao Ma01225592008-08-18 17:38:53 +08004067out:
Joel Becker15d60922008-11-25 18:36:42 -08004068 ocfs2_xattr_bucket_free(new_first);
4069 ocfs2_xattr_bucket_free(old_first);
Tao Ma01225592008-08-18 17:38:53 +08004070 return ret;
4071}
4072
4073/*
Tao Ma80bcaf32008-10-27 06:06:24 +08004074 * Move some xattrs in this cluster to the new cluster.
Tao Ma01225592008-08-18 17:38:53 +08004075 * This function should only be called when bucket size == cluster size.
4076 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
4077 */
Tao Ma80bcaf32008-10-27 06:06:24 +08004078static int ocfs2_divide_xattr_cluster(struct inode *inode,
4079 handle_t *handle,
4080 u64 prev_blk,
4081 u64 new_blk,
4082 u32 *first_hash)
Tao Ma01225592008-08-18 17:38:53 +08004083{
4084 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08004085 int ret, credits = 2 * blk_per_bucket + handle->h_buffer_credits;
Tao Ma01225592008-08-18 17:38:53 +08004086
4087 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
4088
4089 ret = ocfs2_extend_trans(handle, credits);
4090 if (ret) {
4091 mlog_errno(ret);
4092 return ret;
4093 }
4094
4095 /* Move half of the xattr in start_blk to the next bucket. */
Tao Ma80bcaf32008-10-27 06:06:24 +08004096 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
4097 new_blk, first_hash, 1);
Tao Ma01225592008-08-18 17:38:53 +08004098}
4099
4100/*
4101 * Move some xattrs from the old cluster to the new one since they are not
4102 * contiguous in ocfs2 xattr tree.
4103 *
4104 * new_blk starts a new separate cluster, and we will move some xattrs from
4105 * prev_blk to it. v_start will be set as the first name hash value in this
4106 * new cluster so that it can be used as e_cpos during tree insertion and
4107 * don't collide with our original b-tree operations. first_bh and header_bh
4108 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
4109 * to extend the insert bucket.
4110 *
4111 * The problem is how much xattr should we move to the new one and when should
4112 * we update first_bh and header_bh?
4113 * 1. If cluster size > bucket size, that means the previous cluster has more
4114 * than 1 bucket, so just move half nums of bucket into the new cluster and
4115 * update the first_bh and header_bh if the insert bucket has been moved
4116 * to the new cluster.
4117 * 2. If cluster_size == bucket_size:
4118 * a) If the previous extent rec has more than one cluster and the insert
4119 * place isn't in the last cluster, copy the entire last cluster to the
4120 * new one. This time, we don't need to upate the first_bh and header_bh
4121 * since they will not be moved into the new cluster.
4122 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
4123 * the new one. And we set the extend flag to zero if the insert place is
4124 * moved into the new allocated cluster since no extend is needed.
4125 */
4126static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
4127 handle_t *handle,
Joel Becker012ee912008-11-26 14:43:31 -08004128 struct ocfs2_xattr_bucket *first,
4129 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08004130 u64 new_blk,
Tao Ma01225592008-08-18 17:38:53 +08004131 u32 prev_clusters,
4132 u32 *v_start,
4133 int *extend)
4134{
Joel Becker92cf3ad2008-11-26 14:12:09 -08004135 int ret;
Tao Ma01225592008-08-18 17:38:53 +08004136
4137 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
Joel Becker012ee912008-11-26 14:43:31 -08004138 (unsigned long long)bucket_blkno(first), prev_clusters,
Mark Fashehde29c082008-10-29 14:45:30 -07004139 (unsigned long long)new_blk);
Tao Ma01225592008-08-18 17:38:53 +08004140
Joel Becker41cb8142008-11-26 14:25:21 -08004141 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) {
Tao Ma01225592008-08-18 17:38:53 +08004142 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
4143 handle,
Joel Becker41cb8142008-11-26 14:25:21 -08004144 first, target,
Tao Ma01225592008-08-18 17:38:53 +08004145 new_blk,
Tao Ma01225592008-08-18 17:38:53 +08004146 prev_clusters,
4147 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08004148 if (ret)
Joel Becker41cb8142008-11-26 14:25:21 -08004149 mlog_errno(ret);
Joel Becker41cb8142008-11-26 14:25:21 -08004150 } else {
Joel Becker92cf3ad2008-11-26 14:12:09 -08004151 /* The start of the last cluster in the first extent */
4152 u64 last_blk = bucket_blkno(first) +
4153 ((prev_clusters - 1) *
4154 ocfs2_clusters_to_blocks(inode->i_sb, 1));
Tao Ma01225592008-08-18 17:38:53 +08004155
Joel Becker012ee912008-11-26 14:43:31 -08004156 if (prev_clusters > 1 && bucket_blkno(target) != last_blk) {
Joel Becker874d65a2008-11-26 13:02:18 -08004157 ret = ocfs2_mv_xattr_buckets(inode, handle,
Joel Becker92cf3ad2008-11-26 14:12:09 -08004158 bucket_blkno(first),
Joel Becker54ecb6b2008-11-26 13:18:31 -08004159 last_blk, new_blk, 0,
Tao Ma01225592008-08-18 17:38:53 +08004160 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08004161 if (ret)
4162 mlog_errno(ret);
4163 } else {
Tao Ma80bcaf32008-10-27 06:06:24 +08004164 ret = ocfs2_divide_xattr_cluster(inode, handle,
4165 last_blk, new_blk,
4166 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08004167 if (ret)
4168 mlog_errno(ret);
Tao Ma01225592008-08-18 17:38:53 +08004169
Joel Becker92cf3ad2008-11-26 14:12:09 -08004170 if ((bucket_blkno(target) == last_blk) && extend)
Tao Ma01225592008-08-18 17:38:53 +08004171 *extend = 0;
4172 }
4173 }
4174
4175 return ret;
4176}
4177
4178/*
4179 * Add a new cluster for xattr storage.
4180 *
4181 * If the new cluster is contiguous with the previous one, it will be
4182 * appended to the same extent record, and num_clusters will be updated.
4183 * If not, we will insert a new extent for it and move some xattrs in
4184 * the last cluster into the new allocated one.
4185 * We also need to limit the maximum size of a btree leaf, otherwise we'll
4186 * lose the benefits of hashing because we'll have to search large leaves.
4187 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
4188 * if it's bigger).
4189 *
4190 * first_bh is the first block of the previous extent rec and header_bh
4191 * indicates the bucket we will insert the new xattrs. They will be updated
4192 * when the header_bh is moved into the new cluster.
4193 */
4194static int ocfs2_add_new_xattr_cluster(struct inode *inode,
4195 struct buffer_head *root_bh,
Joel Beckered29c0c2008-11-26 15:08:44 -08004196 struct ocfs2_xattr_bucket *first,
4197 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08004198 u32 *num_clusters,
4199 u32 prev_cpos,
Tao Ma78f30c32008-11-12 08:27:00 +08004200 int *extend,
4201 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004202{
Tao Ma85db90e2008-11-12 08:27:01 +08004203 int ret;
Tao Ma01225592008-08-18 17:38:53 +08004204 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
4205 u32 prev_clusters = *num_clusters;
4206 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
4207 u64 block;
Tao Ma85db90e2008-11-12 08:27:01 +08004208 handle_t *handle = ctxt->handle;
Tao Ma01225592008-08-18 17:38:53 +08004209 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004210 struct ocfs2_extent_tree et;
Tao Ma01225592008-08-18 17:38:53 +08004211
4212 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
4213 "previous xattr blkno = %llu\n",
4214 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Joel Beckered29c0c2008-11-26 15:08:44 -08004215 prev_cpos, (unsigned long long)bucket_blkno(first));
Tao Ma01225592008-08-18 17:38:53 +08004216
Joel Becker8d6220d2008-08-22 12:46:09 -07004217 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004218
Joel Becker84008972008-12-09 16:11:49 -08004219 ret = ocfs2_journal_access_xb(handle, inode, root_bh,
4220 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004221 if (ret < 0) {
4222 mlog_errno(ret);
4223 goto leave;
4224 }
4225
Tao Ma78f30c32008-11-12 08:27:00 +08004226 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, 1,
Tao Ma01225592008-08-18 17:38:53 +08004227 clusters_to_add, &bit_off, &num_bits);
4228 if (ret < 0) {
4229 if (ret != -ENOSPC)
4230 mlog_errno(ret);
4231 goto leave;
4232 }
4233
4234 BUG_ON(num_bits > clusters_to_add);
4235
4236 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4237 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
4238 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4239
Joel Beckered29c0c2008-11-26 15:08:44 -08004240 if (bucket_blkno(first) + (prev_clusters * bpc) == block &&
Tao Ma01225592008-08-18 17:38:53 +08004241 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
4242 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
4243 /*
4244 * If this cluster is contiguous with the old one and
4245 * adding this new cluster, we don't surpass the limit of
4246 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
4247 * initialized and used like other buckets in the previous
4248 * cluster.
4249 * So add it as a contiguous one. The caller will handle
4250 * its init process.
4251 */
4252 v_start = prev_cpos + prev_clusters;
4253 *num_clusters = prev_clusters + num_bits;
4254 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
4255 num_bits);
4256 } else {
4257 ret = ocfs2_adjust_xattr_cross_cluster(inode,
4258 handle,
Joel Becker012ee912008-11-26 14:43:31 -08004259 first,
4260 target,
Tao Ma01225592008-08-18 17:38:53 +08004261 block,
Tao Ma01225592008-08-18 17:38:53 +08004262 prev_clusters,
4263 &v_start,
4264 extend);
4265 if (ret) {
4266 mlog_errno(ret);
4267 goto leave;
4268 }
4269 }
4270
4271 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
Mark Fashehde29c082008-10-29 14:45:30 -07004272 num_bits, (unsigned long long)block, v_start);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004273 ret = ocfs2_insert_extent(osb, handle, inode, &et, v_start, block,
Tao Ma78f30c32008-11-12 08:27:00 +08004274 num_bits, 0, ctxt->meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08004275 if (ret < 0) {
4276 mlog_errno(ret);
4277 goto leave;
4278 }
4279
4280 ret = ocfs2_journal_dirty(handle, root_bh);
Tao Ma85db90e2008-11-12 08:27:01 +08004281 if (ret < 0)
Tao Ma01225592008-08-18 17:38:53 +08004282 mlog_errno(ret);
Tao Ma01225592008-08-18 17:38:53 +08004283
4284leave:
Tao Ma01225592008-08-18 17:38:53 +08004285 return ret;
4286}
4287
4288/*
Joel Becker92de1092008-11-25 17:06:40 -08004289 * We are given an extent. 'first' is the bucket at the very front of
4290 * the extent. The extent has space for an additional bucket past
4291 * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
4292 * of the target bucket. We wish to shift every bucket past the target
4293 * down one, filling in that additional space. When we get back to the
4294 * target, we split the target between itself and the now-empty bucket
4295 * at target+1 (aka, target_blkno + blks_per_bucket).
Tao Ma01225592008-08-18 17:38:53 +08004296 */
4297static int ocfs2_extend_xattr_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004298 handle_t *handle,
Joel Becker92de1092008-11-25 17:06:40 -08004299 struct ocfs2_xattr_bucket *first,
4300 u64 target_blk,
Tao Ma01225592008-08-18 17:38:53 +08004301 u32 num_clusters)
4302{
4303 int ret, credits;
4304 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4305 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Joel Becker92de1092008-11-25 17:06:40 -08004306 u64 end_blk;
4307 u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets);
Tao Ma01225592008-08-18 17:38:53 +08004308
4309 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
Joel Becker92de1092008-11-25 17:06:40 -08004310 "from %llu, len = %u\n", (unsigned long long)target_blk,
4311 (unsigned long long)bucket_blkno(first), num_clusters);
Tao Ma01225592008-08-18 17:38:53 +08004312
Joel Becker92de1092008-11-25 17:06:40 -08004313 /* The extent must have room for an additional bucket */
4314 BUG_ON(new_bucket >=
4315 (num_clusters * ocfs2_xattr_buckets_per_cluster(osb)));
Tao Ma01225592008-08-18 17:38:53 +08004316
Joel Becker92de1092008-11-25 17:06:40 -08004317 /* end_blk points to the last existing bucket */
4318 end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004319
4320 /*
Joel Becker92de1092008-11-25 17:06:40 -08004321 * end_blk is the start of the last existing bucket.
4322 * Thus, (end_blk - target_blk) covers the target bucket and
4323 * every bucket after it up to, but not including, the last
4324 * existing bucket. Then we add the last existing bucket, the
4325 * new bucket, and the first bucket (3 * blk_per_bucket).
Tao Ma01225592008-08-18 17:38:53 +08004326 */
Joel Becker92de1092008-11-25 17:06:40 -08004327 credits = (end_blk - target_blk) + (3 * blk_per_bucket) +
Tao Ma85db90e2008-11-12 08:27:01 +08004328 handle->h_buffer_credits;
4329 ret = ocfs2_extend_trans(handle, credits);
4330 if (ret) {
Tao Ma01225592008-08-18 17:38:53 +08004331 mlog_errno(ret);
4332 goto out;
4333 }
4334
Joel Becker92de1092008-11-25 17:06:40 -08004335 ret = ocfs2_xattr_bucket_journal_access(handle, first,
4336 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004337 if (ret) {
4338 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004339 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004340 }
4341
Joel Becker92de1092008-11-25 17:06:40 -08004342 while (end_blk != target_blk) {
Tao Ma01225592008-08-18 17:38:53 +08004343 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
4344 end_blk + blk_per_bucket, 0);
4345 if (ret)
Tao Ma85db90e2008-11-12 08:27:01 +08004346 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004347 end_blk -= blk_per_bucket;
4348 }
4349
Joel Becker92de1092008-11-25 17:06:40 -08004350 /* Move half of the xattr in target_blkno to the next bucket. */
4351 ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk,
4352 target_blk + blk_per_bucket, NULL, 0);
Tao Ma01225592008-08-18 17:38:53 +08004353
Joel Becker92de1092008-11-25 17:06:40 -08004354 le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1);
4355 ocfs2_xattr_bucket_journal_dirty(handle, first);
Tao Ma01225592008-08-18 17:38:53 +08004356
Tao Ma01225592008-08-18 17:38:53 +08004357out:
4358 return ret;
4359}
4360
4361/*
Joel Becker91f20332008-11-26 15:25:41 -08004362 * Add new xattr bucket in an extent record and adjust the buckets
4363 * accordingly. xb_bh is the ocfs2_xattr_block, and target is the
4364 * bucket we want to insert into.
Tao Ma01225592008-08-18 17:38:53 +08004365 *
Joel Becker91f20332008-11-26 15:25:41 -08004366 * In the easy case, we will move all the buckets after target down by
4367 * one. Half of target's xattrs will be moved to the next bucket.
4368 *
4369 * If current cluster is full, we'll allocate a new one. This may not
4370 * be contiguous. The underlying calls will make sure that there is
4371 * space for the insert, shifting buckets around if necessary.
4372 * 'target' may be moved by those calls.
Tao Ma01225592008-08-18 17:38:53 +08004373 */
4374static int ocfs2_add_new_xattr_bucket(struct inode *inode,
4375 struct buffer_head *xb_bh,
Joel Becker91f20332008-11-26 15:25:41 -08004376 struct ocfs2_xattr_bucket *target,
Tao Ma78f30c32008-11-12 08:27:00 +08004377 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004378{
Tao Ma01225592008-08-18 17:38:53 +08004379 struct ocfs2_xattr_block *xb =
4380 (struct ocfs2_xattr_block *)xb_bh->b_data;
4381 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
4382 struct ocfs2_extent_list *el = &xb_root->xt_list;
Joel Becker91f20332008-11-26 15:25:41 -08004383 u32 name_hash =
4384 le32_to_cpu(bucket_xh(target)->xh_entries[0].xe_name_hash);
Joel Beckered29c0c2008-11-26 15:08:44 -08004385 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08004386 int ret, num_buckets, extend = 1;
4387 u64 p_blkno;
4388 u32 e_cpos, num_clusters;
Joel Becker92de1092008-11-25 17:06:40 -08004389 /* The bucket at the front of the extent */
Joel Becker91f20332008-11-26 15:25:41 -08004390 struct ocfs2_xattr_bucket *first;
Tao Ma01225592008-08-18 17:38:53 +08004391
Joel Becker91f20332008-11-26 15:25:41 -08004392 mlog(0, "Add new xattr bucket starting from %llu\n",
4393 (unsigned long long)bucket_blkno(target));
Tao Ma01225592008-08-18 17:38:53 +08004394
Joel Beckered29c0c2008-11-26 15:08:44 -08004395 /* The first bucket of the original extent */
Joel Becker92de1092008-11-25 17:06:40 -08004396 first = ocfs2_xattr_bucket_new(inode);
Joel Becker91f20332008-11-26 15:25:41 -08004397 if (!first) {
Joel Becker92de1092008-11-25 17:06:40 -08004398 ret = -ENOMEM;
4399 mlog_errno(ret);
4400 goto out;
4401 }
4402
Tao Ma01225592008-08-18 17:38:53 +08004403 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
4404 &num_clusters, el);
4405 if (ret) {
4406 mlog_errno(ret);
4407 goto out;
4408 }
4409
Joel Beckered29c0c2008-11-26 15:08:44 -08004410 ret = ocfs2_read_xattr_bucket(first, p_blkno);
4411 if (ret) {
4412 mlog_errno(ret);
4413 goto out;
4414 }
4415
Tao Ma01225592008-08-18 17:38:53 +08004416 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
Joel Beckered29c0c2008-11-26 15:08:44 -08004417 if (num_buckets == le16_to_cpu(bucket_xh(first)->xh_num_buckets)) {
4418 /*
4419 * This can move first+target if the target bucket moves
4420 * to the new extent.
4421 */
Tao Ma01225592008-08-18 17:38:53 +08004422 ret = ocfs2_add_new_xattr_cluster(inode,
4423 xb_bh,
Joel Beckered29c0c2008-11-26 15:08:44 -08004424 first,
4425 target,
Tao Ma01225592008-08-18 17:38:53 +08004426 &num_clusters,
4427 e_cpos,
Tao Ma78f30c32008-11-12 08:27:00 +08004428 &extend,
4429 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004430 if (ret) {
4431 mlog_errno(ret);
4432 goto out;
4433 }
4434 }
4435
Joel Becker92de1092008-11-25 17:06:40 -08004436 if (extend) {
Tao Ma01225592008-08-18 17:38:53 +08004437 ret = ocfs2_extend_xattr_bucket(inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004438 ctxt->handle,
Joel Beckered29c0c2008-11-26 15:08:44 -08004439 first,
4440 bucket_blkno(target),
Tao Ma01225592008-08-18 17:38:53 +08004441 num_clusters);
Joel Becker92de1092008-11-25 17:06:40 -08004442 if (ret)
4443 mlog_errno(ret);
4444 }
4445
Tao Ma01225592008-08-18 17:38:53 +08004446out:
Joel Becker92de1092008-11-25 17:06:40 -08004447 ocfs2_xattr_bucket_free(first);
Joel Beckered29c0c2008-11-26 15:08:44 -08004448
Tao Ma01225592008-08-18 17:38:53 +08004449 return ret;
4450}
4451
4452static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
4453 struct ocfs2_xattr_bucket *bucket,
4454 int offs)
4455{
4456 int block_off = offs >> inode->i_sb->s_blocksize_bits;
4457
4458 offs = offs % inode->i_sb->s_blocksize;
Joel Becker51def392008-10-24 16:57:21 -07004459 return bucket_block(bucket, block_off) + offs;
Tao Ma01225592008-08-18 17:38:53 +08004460}
4461
4462/*
4463 * Handle the normal xattr set, including replace, delete and new.
Tao Ma01225592008-08-18 17:38:53 +08004464 *
4465 * Note: "local" indicates the real data's locality. So we can't
4466 * just its bucket locality by its length.
4467 */
4468static void ocfs2_xattr_set_entry_normal(struct inode *inode,
4469 struct ocfs2_xattr_info *xi,
4470 struct ocfs2_xattr_search *xs,
4471 u32 name_hash,
Tao Ma5a095612008-09-19 22:17:41 +08004472 int local)
Tao Ma01225592008-08-18 17:38:53 +08004473{
4474 struct ocfs2_xattr_entry *last, *xe;
4475 int name_len = strlen(xi->name);
4476 struct ocfs2_xattr_header *xh = xs->header;
4477 u16 count = le16_to_cpu(xh->xh_count), start;
4478 size_t blocksize = inode->i_sb->s_blocksize;
4479 char *val;
4480 size_t offs, size, new_size;
4481
4482 last = &xh->xh_entries[count];
4483 if (!xs->not_found) {
4484 xe = xs->here;
4485 offs = le16_to_cpu(xe->xe_name_offset);
4486 if (ocfs2_xattr_is_local(xe))
4487 size = OCFS2_XATTR_SIZE(name_len) +
4488 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4489 else
4490 size = OCFS2_XATTR_SIZE(name_len) +
4491 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4492
4493 /*
4494 * If the new value will be stored outside, xi->value has been
4495 * initalized as an empty ocfs2_xattr_value_root, and the same
4496 * goes with xi->value_len, so we can set new_size safely here.
4497 * See ocfs2_xattr_set_in_bucket.
4498 */
4499 new_size = OCFS2_XATTR_SIZE(name_len) +
4500 OCFS2_XATTR_SIZE(xi->value_len);
4501
4502 le16_add_cpu(&xh->xh_name_value_len, -size);
4503 if (xi->value) {
4504 if (new_size > size)
4505 goto set_new_name_value;
4506
4507 /* Now replace the old value with new one. */
4508 if (local)
4509 xe->xe_value_size = cpu_to_le64(xi->value_len);
4510 else
4511 xe->xe_value_size = 0;
4512
4513 val = ocfs2_xattr_bucket_get_val(inode,
Joel Beckerba937122008-10-24 19:13:20 -07004514 xs->bucket, offs);
Tao Ma01225592008-08-18 17:38:53 +08004515 memset(val + OCFS2_XATTR_SIZE(name_len), 0,
4516 size - OCFS2_XATTR_SIZE(name_len));
4517 if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
4518 memcpy(val + OCFS2_XATTR_SIZE(name_len),
4519 xi->value, xi->value_len);
4520
4521 le16_add_cpu(&xh->xh_name_value_len, new_size);
4522 ocfs2_xattr_set_local(xe, local);
4523 return;
4524 } else {
Tao Ma5a095612008-09-19 22:17:41 +08004525 /*
4526 * Remove the old entry if there is more than one.
4527 * We don't remove the last entry so that we can
4528 * use it to indicate the hash value of the empty
4529 * bucket.
4530 */
Tao Ma01225592008-08-18 17:38:53 +08004531 last -= 1;
Tao Ma01225592008-08-18 17:38:53 +08004532 le16_add_cpu(&xh->xh_count, -1);
Tao Ma5a095612008-09-19 22:17:41 +08004533 if (xh->xh_count) {
4534 memmove(xe, xe + 1,
4535 (void *)last - (void *)xe);
4536 memset(last, 0,
4537 sizeof(struct ocfs2_xattr_entry));
4538 } else
4539 xh->xh_free_start =
4540 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4541
Tao Ma01225592008-08-18 17:38:53 +08004542 return;
4543 }
4544 } else {
4545 /* find a new entry for insert. */
4546 int low = 0, high = count - 1, tmp;
4547 struct ocfs2_xattr_entry *tmp_xe;
4548
Tao Ma5a095612008-09-19 22:17:41 +08004549 while (low <= high && count) {
Tao Ma01225592008-08-18 17:38:53 +08004550 tmp = (low + high) / 2;
4551 tmp_xe = &xh->xh_entries[tmp];
4552
4553 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
4554 low = tmp + 1;
4555 else if (name_hash <
4556 le32_to_cpu(tmp_xe->xe_name_hash))
4557 high = tmp - 1;
Tao Ma06b240d2008-09-19 22:16:34 +08004558 else {
4559 low = tmp;
Tao Ma01225592008-08-18 17:38:53 +08004560 break;
Tao Ma06b240d2008-09-19 22:16:34 +08004561 }
Tao Ma01225592008-08-18 17:38:53 +08004562 }
4563
4564 xe = &xh->xh_entries[low];
4565 if (low != count)
4566 memmove(xe + 1, xe, (void *)last - (void *)xe);
4567
4568 le16_add_cpu(&xh->xh_count, 1);
4569 memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
4570 xe->xe_name_hash = cpu_to_le32(name_hash);
4571 xe->xe_name_len = name_len;
4572 ocfs2_xattr_set_type(xe, xi->name_index);
4573 }
4574
4575set_new_name_value:
4576 /* Insert the new name+value. */
4577 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
4578
4579 /*
4580 * We must make sure that the name/value pair
4581 * exists in the same block.
4582 */
4583 offs = le16_to_cpu(xh->xh_free_start);
4584 start = offs - size;
4585
4586 if (start >> inode->i_sb->s_blocksize_bits !=
4587 (offs - 1) >> inode->i_sb->s_blocksize_bits) {
4588 offs = offs - offs % blocksize;
4589 xh->xh_free_start = cpu_to_le16(offs);
4590 }
4591
Joel Beckerba937122008-10-24 19:13:20 -07004592 val = ocfs2_xattr_bucket_get_val(inode, xs->bucket, offs - size);
Tao Ma01225592008-08-18 17:38:53 +08004593 xe->xe_name_offset = cpu_to_le16(offs - size);
4594
4595 memset(val, 0, size);
4596 memcpy(val, xi->name, name_len);
4597 memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
4598
4599 xe->xe_value_size = cpu_to_le64(xi->value_len);
4600 ocfs2_xattr_set_local(xe, local);
4601 xs->here = xe;
4602 le16_add_cpu(&xh->xh_free_start, -size);
4603 le16_add_cpu(&xh->xh_name_value_len, size);
4604
4605 return;
4606}
4607
Tao Ma01225592008-08-18 17:38:53 +08004608/*
4609 * Set the xattr entry in the specified bucket.
4610 * The bucket is indicated by xs->bucket and it should have the enough
4611 * space for the xattr insertion.
4612 */
4613static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004614 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004615 struct ocfs2_xattr_info *xi,
4616 struct ocfs2_xattr_search *xs,
4617 u32 name_hash,
Tao Ma5a095612008-09-19 22:17:41 +08004618 int local)
Tao Ma01225592008-08-18 17:38:53 +08004619{
Joel Becker1224be02008-10-24 18:47:33 -07004620 int ret;
Joel Becker02dbf382008-10-27 18:07:45 -07004621 u64 blkno;
Tao Ma01225592008-08-18 17:38:53 +08004622
Mark Fashehff1ec202008-08-19 10:54:29 -07004623 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4624 (unsigned long)xi->value_len, xi->name_index,
Joel Beckerba937122008-10-24 19:13:20 -07004625 (unsigned long long)bucket_blkno(xs->bucket));
Tao Ma01225592008-08-18 17:38:53 +08004626
Joel Beckerba937122008-10-24 19:13:20 -07004627 if (!xs->bucket->bu_bhs[1]) {
Joel Becker02dbf382008-10-27 18:07:45 -07004628 blkno = bucket_blkno(xs->bucket);
4629 ocfs2_xattr_bucket_relse(xs->bucket);
4630 ret = ocfs2_read_xattr_bucket(xs->bucket, blkno);
Tao Ma01225592008-08-18 17:38:53 +08004631 if (ret) {
4632 mlog_errno(ret);
4633 goto out;
4634 }
4635 }
4636
Joel Beckerba937122008-10-24 19:13:20 -07004637 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004638 OCFS2_JOURNAL_ACCESS_WRITE);
4639 if (ret < 0) {
4640 mlog_errno(ret);
4641 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004642 }
4643
Tao Ma5a095612008-09-19 22:17:41 +08004644 ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
Joel Beckerba937122008-10-24 19:13:20 -07004645 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004646
Tao Ma01225592008-08-18 17:38:53 +08004647out:
Tao Ma01225592008-08-18 17:38:53 +08004648 return ret;
4649}
4650
Tao Ma01225592008-08-18 17:38:53 +08004651/*
4652 * Truncate the specified xe_off entry in xattr bucket.
4653 * bucket is indicated by header_bh and len is the new length.
4654 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4655 *
4656 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4657 */
4658static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
Joel Becker548b0f22008-11-24 19:32:13 -08004659 struct ocfs2_xattr_bucket *bucket,
Tao Ma01225592008-08-18 17:38:53 +08004660 int xe_off,
Tao Ma78f30c32008-11-12 08:27:00 +08004661 int len,
4662 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004663{
4664 int ret, offset;
4665 u64 value_blk;
Tao Ma01225592008-08-18 17:38:53 +08004666 struct ocfs2_xattr_entry *xe;
Joel Becker548b0f22008-11-24 19:32:13 -08004667 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma01225592008-08-18 17:38:53 +08004668 size_t blocksize = inode->i_sb->s_blocksize;
Joel Beckerb3e5d372008-12-09 15:01:04 -08004669 struct ocfs2_xattr_value_buf vb = {
4670 .vb_access = ocfs2_journal_access,
4671 };
Tao Ma01225592008-08-18 17:38:53 +08004672
4673 xe = &xh->xh_entries[xe_off];
4674
4675 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
4676
4677 offset = le16_to_cpu(xe->xe_name_offset) +
4678 OCFS2_XATTR_SIZE(xe->xe_name_len);
4679
4680 value_blk = offset / blocksize;
4681
4682 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4683 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
Tao Ma01225592008-08-18 17:38:53 +08004684
Joel Beckerb3e5d372008-12-09 15:01:04 -08004685 vb.vb_bh = bucket->bu_bhs[value_blk];
4686 BUG_ON(!vb.vb_bh);
Tao Ma01225592008-08-18 17:38:53 +08004687
Joel Beckerb3e5d372008-12-09 15:01:04 -08004688 vb.vb_xv = (struct ocfs2_xattr_value_root *)
4689 (vb.vb_bh->b_data + offset % blocksize);
Tao Ma01225592008-08-18 17:38:53 +08004690
Joel Becker548b0f22008-11-24 19:32:13 -08004691 ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
4692 OCFS2_JOURNAL_ACCESS_WRITE);
4693 if (ret) {
4694 mlog_errno(ret);
4695 goto out;
4696 }
4697
4698 /*
4699 * From here on out we have to dirty the bucket. The generic
4700 * value calls only modify one of the bucket's bhs, but we need
4701 * to send the bucket at once. So if they error, they *could* have
4702 * modified something. We have to assume they did, and dirty
4703 * the whole bucket. This leaves us in a consistent state.
4704 */
Tao Ma01225592008-08-18 17:38:53 +08004705 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
Joel Becker548b0f22008-11-24 19:32:13 -08004706 xe_off, (unsigned long long)bucket_blkno(bucket), len);
Joel Beckerb3e5d372008-12-09 15:01:04 -08004707 ret = ocfs2_xattr_value_truncate(inode, &vb, len, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004708 if (ret) {
4709 mlog_errno(ret);
Joel Becker548b0f22008-11-24 19:32:13 -08004710 goto out_dirty;
Tao Ma01225592008-08-18 17:38:53 +08004711 }
4712
Joel Becker548b0f22008-11-24 19:32:13 -08004713 xe->xe_value_size = cpu_to_le64(len);
4714
4715out_dirty:
4716 ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket);
Tao Ma01225592008-08-18 17:38:53 +08004717
4718out:
Tao Ma01225592008-08-18 17:38:53 +08004719 return ret;
4720}
4721
4722static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +08004723 struct ocfs2_xattr_search *xs,
4724 int len,
4725 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004726{
4727 int ret, offset;
4728 struct ocfs2_xattr_entry *xe = xs->here;
4729 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
4730
Joel Beckerba937122008-10-24 19:13:20 -07004731 BUG_ON(!xs->bucket->bu_bhs[0] || !xe || ocfs2_xattr_is_local(xe));
Tao Ma01225592008-08-18 17:38:53 +08004732
4733 offset = xe - xh->xh_entries;
Joel Becker548b0f22008-11-24 19:32:13 -08004734 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket,
Tao Ma78f30c32008-11-12 08:27:00 +08004735 offset, len, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004736 if (ret)
4737 mlog_errno(ret);
4738
4739 return ret;
4740}
4741
4742static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004743 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004744 struct ocfs2_xattr_search *xs,
4745 char *val,
4746 int value_len)
4747{
4748 int offset;
4749 struct ocfs2_xattr_value_root *xv;
4750 struct ocfs2_xattr_entry *xe = xs->here;
4751
4752 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
4753
4754 offset = le16_to_cpu(xe->xe_name_offset) +
4755 OCFS2_XATTR_SIZE(xe->xe_name_len);
4756
4757 xv = (struct ocfs2_xattr_value_root *)(xs->base + offset);
4758
Tao Ma85db90e2008-11-12 08:27:01 +08004759 return __ocfs2_xattr_set_value_outside(inode, handle,
4760 xv, val, value_len);
Tao Ma01225592008-08-18 17:38:53 +08004761}
4762
Tao Ma01225592008-08-18 17:38:53 +08004763static int ocfs2_rm_xattr_cluster(struct inode *inode,
4764 struct buffer_head *root_bh,
4765 u64 blkno,
4766 u32 cpos,
4767 u32 len)
4768{
4769 int ret;
4770 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4771 struct inode *tl_inode = osb->osb_tl_inode;
4772 handle_t *handle;
4773 struct ocfs2_xattr_block *xb =
4774 (struct ocfs2_xattr_block *)root_bh->b_data;
Tao Ma01225592008-08-18 17:38:53 +08004775 struct ocfs2_alloc_context *meta_ac = NULL;
4776 struct ocfs2_cached_dealloc_ctxt dealloc;
Joel Beckerf99b9b72008-08-20 19:36:33 -07004777 struct ocfs2_extent_tree et;
4778
Joel Becker8d6220d2008-08-22 12:46:09 -07004779 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
Tao Ma01225592008-08-18 17:38:53 +08004780
4781 ocfs2_init_dealloc_ctxt(&dealloc);
4782
4783 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
4784 cpos, len, (unsigned long long)blkno);
4785
4786 ocfs2_remove_xattr_clusters_from_cache(inode, blkno, len);
4787
Joel Beckerf99b9b72008-08-20 19:36:33 -07004788 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08004789 if (ret) {
4790 mlog_errno(ret);
4791 return ret;
4792 }
4793
4794 mutex_lock(&tl_inode->i_mutex);
4795
4796 if (ocfs2_truncate_log_needs_flush(osb)) {
4797 ret = __ocfs2_flush_truncate_log(osb);
4798 if (ret < 0) {
4799 mlog_errno(ret);
4800 goto out;
4801 }
4802 }
4803
Jan Karaa90714c2008-10-09 19:38:40 +02004804 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
Tao Mad3264792008-10-24 07:57:28 +08004805 if (IS_ERR(handle)) {
Tao Ma01225592008-08-18 17:38:53 +08004806 ret = -ENOMEM;
4807 mlog_errno(ret);
4808 goto out;
4809 }
4810
Joel Becker84008972008-12-09 16:11:49 -08004811 ret = ocfs2_journal_access_xb(handle, inode, root_bh,
4812 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004813 if (ret) {
4814 mlog_errno(ret);
4815 goto out_commit;
4816 }
4817
Joel Beckerf99b9b72008-08-20 19:36:33 -07004818 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
4819 &dealloc);
Tao Ma01225592008-08-18 17:38:53 +08004820 if (ret) {
4821 mlog_errno(ret);
4822 goto out_commit;
4823 }
4824
4825 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
4826
4827 ret = ocfs2_journal_dirty(handle, root_bh);
4828 if (ret) {
4829 mlog_errno(ret);
4830 goto out_commit;
4831 }
4832
4833 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
4834 if (ret)
4835 mlog_errno(ret);
4836
4837out_commit:
4838 ocfs2_commit_trans(osb, handle);
4839out:
4840 ocfs2_schedule_truncate_log_flush(osb, 1);
4841
4842 mutex_unlock(&tl_inode->i_mutex);
4843
4844 if (meta_ac)
4845 ocfs2_free_alloc_context(meta_ac);
4846
4847 ocfs2_run_deallocs(osb, &dealloc);
4848
4849 return ret;
4850}
4851
Tao Ma01225592008-08-18 17:38:53 +08004852static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004853 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004854 struct ocfs2_xattr_search *xs)
4855{
Joel Beckerba937122008-10-24 19:13:20 -07004856 struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004857 struct ocfs2_xattr_entry *last = &xh->xh_entries[
4858 le16_to_cpu(xh->xh_count) - 1];
4859 int ret = 0;
4860
Joel Beckerba937122008-10-24 19:13:20 -07004861 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004862 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004863 if (ret) {
4864 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004865 return;
Tao Ma01225592008-08-18 17:38:53 +08004866 }
4867
4868 /* Remove the old entry. */
4869 memmove(xs->here, xs->here + 1,
4870 (void *)last - (void *)xs->here);
4871 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
4872 le16_add_cpu(&xh->xh_count, -1);
4873
Joel Beckerba937122008-10-24 19:13:20 -07004874 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004875}
4876
4877/*
4878 * Set the xattr name/value in the bucket specified in xs.
4879 *
4880 * As the new value in xi may be stored in the bucket or in an outside cluster,
4881 * we divide the whole process into 3 steps:
4882 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
4883 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
4884 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
4885 * 4. If the clusters for the new outside value can't be allocated, we need
4886 * to free the xattr we allocated in set.
4887 */
4888static int ocfs2_xattr_set_in_bucket(struct inode *inode,
4889 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08004890 struct ocfs2_xattr_search *xs,
4891 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004892{
Tao Ma5a095612008-09-19 22:17:41 +08004893 int ret, local = 1;
Tao Ma01225592008-08-18 17:38:53 +08004894 size_t value_len;
4895 char *val = (char *)xi->value;
4896 struct ocfs2_xattr_entry *xe = xs->here;
Tao Ma2057e5c2008-10-09 23:06:13 +08004897 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name,
4898 strlen(xi->name));
Tao Ma01225592008-08-18 17:38:53 +08004899
4900 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
4901 /*
4902 * We need to truncate the xattr storage first.
4903 *
4904 * If both the old and new value are stored to
4905 * outside block, we only need to truncate
4906 * the storage and then set the value outside.
4907 *
4908 * If the new value should be stored within block,
4909 * we should free all the outside block first and
4910 * the modification to the xattr block will be done
4911 * by following steps.
4912 */
4913 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4914 value_len = xi->value_len;
4915 else
4916 value_len = 0;
4917
4918 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
Tao Ma78f30c32008-11-12 08:27:00 +08004919 value_len,
4920 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004921 if (ret)
4922 goto out;
4923
4924 if (value_len)
4925 goto set_value_outside;
4926 }
4927
4928 value_len = xi->value_len;
4929 /* So we have to handle the inside block change now. */
4930 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
4931 /*
4932 * If the new value will be stored outside of block,
4933 * initalize a new empty value root and insert it first.
4934 */
4935 local = 0;
4936 xi->value = &def_xv;
4937 xi->value_len = OCFS2_XATTR_ROOT_SIZE;
4938 }
4939
Tao Ma85db90e2008-11-12 08:27:01 +08004940 ret = ocfs2_xattr_set_entry_in_bucket(inode, ctxt->handle, xi, xs,
4941 name_hash, local);
Tao Ma01225592008-08-18 17:38:53 +08004942 if (ret) {
4943 mlog_errno(ret);
4944 goto out;
4945 }
4946
Tao Ma5a095612008-09-19 22:17:41 +08004947 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
4948 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004949
Tao Ma5a095612008-09-19 22:17:41 +08004950 /* allocate the space now for the outside block storage. */
4951 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
Tao Ma78f30c32008-11-12 08:27:00 +08004952 value_len, ctxt);
Tao Ma5a095612008-09-19 22:17:41 +08004953 if (ret) {
4954 mlog_errno(ret);
4955
4956 if (xs->not_found) {
4957 /*
4958 * We can't allocate enough clusters for outside
4959 * storage and we have allocated xattr already,
4960 * so need to remove it.
4961 */
Tao Ma85db90e2008-11-12 08:27:01 +08004962 ocfs2_xattr_bucket_remove_xs(inode, ctxt->handle, xs);
Tao Ma01225592008-08-18 17:38:53 +08004963 }
Tao Ma01225592008-08-18 17:38:53 +08004964 goto out;
4965 }
4966
4967set_value_outside:
Tao Ma85db90e2008-11-12 08:27:01 +08004968 ret = ocfs2_xattr_bucket_set_value_outside(inode, ctxt->handle,
4969 xs, val, value_len);
Tao Ma01225592008-08-18 17:38:53 +08004970out:
4971 return ret;
4972}
4973
Tao Ma80bcaf32008-10-27 06:06:24 +08004974/*
4975 * check whether the xattr bucket is filled up with the same hash value.
4976 * If we want to insert the xattr with the same hash, return -ENOSPC.
4977 * If we want to insert a xattr with different hash value, go ahead
4978 * and ocfs2_divide_xattr_bucket will handle this.
4979 */
Tao Ma01225592008-08-18 17:38:53 +08004980static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
Tao Ma80bcaf32008-10-27 06:06:24 +08004981 struct ocfs2_xattr_bucket *bucket,
4982 const char *name)
Tao Ma01225592008-08-18 17:38:53 +08004983{
Joel Becker3e632942008-10-24 17:04:49 -07004984 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08004985 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
4986
4987 if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
4988 return 0;
Tao Ma01225592008-08-18 17:38:53 +08004989
4990 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
4991 xh->xh_entries[0].xe_name_hash) {
4992 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
4993 "hash = %u\n",
Joel Becker9c7759a2008-10-24 16:21:03 -07004994 (unsigned long long)bucket_blkno(bucket),
Tao Ma01225592008-08-18 17:38:53 +08004995 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
4996 return -ENOSPC;
4997 }
4998
4999 return 0;
5000}
5001
5002static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
5003 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08005004 struct ocfs2_xattr_search *xs,
5005 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005006{
5007 struct ocfs2_xattr_header *xh;
5008 struct ocfs2_xattr_entry *xe;
5009 u16 count, header_size, xh_free_start;
Joel Becker6dde41d2008-10-24 17:16:48 -07005010 int free, max_free, need, old;
Tao Ma01225592008-08-18 17:38:53 +08005011 size_t value_size = 0, name_len = strlen(xi->name);
5012 size_t blocksize = inode->i_sb->s_blocksize;
5013 int ret, allocation = 0;
Tao Ma01225592008-08-18 17:38:53 +08005014
5015 mlog_entry("Set xattr %s in xattr index block\n", xi->name);
5016
5017try_again:
5018 xh = xs->header;
5019 count = le16_to_cpu(xh->xh_count);
5020 xh_free_start = le16_to_cpu(xh->xh_free_start);
5021 header_size = sizeof(struct ocfs2_xattr_header) +
5022 count * sizeof(struct ocfs2_xattr_entry);
5023 max_free = OCFS2_XATTR_BUCKET_SIZE -
5024 le16_to_cpu(xh->xh_name_value_len) - header_size;
5025
5026 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
5027 "of %u which exceed block size\n",
Joel Beckerba937122008-10-24 19:13:20 -07005028 (unsigned long long)bucket_blkno(xs->bucket),
Tao Ma01225592008-08-18 17:38:53 +08005029 header_size);
5030
5031 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
5032 value_size = OCFS2_XATTR_ROOT_SIZE;
5033 else if (xi->value)
5034 value_size = OCFS2_XATTR_SIZE(xi->value_len);
5035
5036 if (xs->not_found)
5037 need = sizeof(struct ocfs2_xattr_entry) +
5038 OCFS2_XATTR_SIZE(name_len) + value_size;
5039 else {
5040 need = value_size + OCFS2_XATTR_SIZE(name_len);
5041
5042 /*
5043 * We only replace the old value if the new length is smaller
5044 * than the old one. Otherwise we will allocate new space in the
5045 * bucket to store it.
5046 */
5047 xe = xs->here;
5048 if (ocfs2_xattr_is_local(xe))
5049 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
5050 else
5051 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
5052
5053 if (old >= value_size)
5054 need = 0;
5055 }
5056
5057 free = xh_free_start - header_size;
5058 /*
5059 * We need to make sure the new name/value pair
5060 * can exist in the same block.
5061 */
5062 if (xh_free_start % blocksize < need)
5063 free -= xh_free_start % blocksize;
5064
5065 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
5066 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
5067 " %u\n", xs->not_found,
Joel Beckerba937122008-10-24 19:13:20 -07005068 (unsigned long long)bucket_blkno(xs->bucket),
Tao Ma01225592008-08-18 17:38:53 +08005069 free, need, max_free, le16_to_cpu(xh->xh_free_start),
5070 le16_to_cpu(xh->xh_name_value_len));
5071
Tao Ma976331d2008-11-12 08:26:57 +08005072 if (free < need ||
5073 (xs->not_found &&
5074 count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb))) {
Tao Ma01225592008-08-18 17:38:53 +08005075 if (need <= max_free &&
5076 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
5077 /*
5078 * We can create the space by defragment. Since only the
5079 * name/value will be moved, the xe shouldn't be changed
5080 * in xs.
5081 */
Tao Ma85db90e2008-11-12 08:27:01 +08005082 ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
5083 xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08005084 if (ret) {
5085 mlog_errno(ret);
5086 goto out;
5087 }
5088
5089 xh_free_start = le16_to_cpu(xh->xh_free_start);
5090 free = xh_free_start - header_size;
5091 if (xh_free_start % blocksize < need)
5092 free -= xh_free_start % blocksize;
5093
5094 if (free >= need)
5095 goto xattr_set;
5096
5097 mlog(0, "Can't get enough space for xattr insert by "
5098 "defragment. Need %u bytes, but we have %d, so "
5099 "allocate new bucket for it.\n", need, free);
5100 }
5101
5102 /*
5103 * We have to add new buckets or clusters and one
5104 * allocation should leave us enough space for insert.
5105 */
5106 BUG_ON(allocation);
5107
5108 /*
5109 * We do not allow for overlapping ranges between buckets. And
5110 * the maximum number of collisions we will allow for then is
5111 * one bucket's worth, so check it here whether we need to
5112 * add a new bucket for the insert.
5113 */
Tao Ma80bcaf32008-10-27 06:06:24 +08005114 ret = ocfs2_check_xattr_bucket_collision(inode,
Joel Beckerba937122008-10-24 19:13:20 -07005115 xs->bucket,
Tao Ma80bcaf32008-10-27 06:06:24 +08005116 xi->name);
Tao Ma01225592008-08-18 17:38:53 +08005117 if (ret) {
5118 mlog_errno(ret);
5119 goto out;
5120 }
5121
5122 ret = ocfs2_add_new_xattr_bucket(inode,
5123 xs->xattr_bh,
Joel Becker91f20332008-11-26 15:25:41 -08005124 xs->bucket,
Tao Ma78f30c32008-11-12 08:27:00 +08005125 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005126 if (ret) {
5127 mlog_errno(ret);
5128 goto out;
5129 }
5130
Joel Becker91f20332008-11-26 15:25:41 -08005131 /*
5132 * ocfs2_add_new_xattr_bucket() will have updated
5133 * xs->bucket if it moved, but it will not have updated
5134 * any of the other search fields. Thus, we drop it and
5135 * re-search. Everything should be cached, so it'll be
5136 * quick.
5137 */
Joel Beckerba937122008-10-24 19:13:20 -07005138 ocfs2_xattr_bucket_relse(xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08005139 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
5140 xi->name_index,
5141 xi->name, xs);
5142 if (ret && ret != -ENODATA)
5143 goto out;
5144 xs->not_found = ret;
5145 allocation = 1;
5146 goto try_again;
5147 }
5148
5149xattr_set:
Tao Ma78f30c32008-11-12 08:27:00 +08005150 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005151out:
5152 mlog_exit(ret);
5153 return ret;
5154}
Tao Maa3944252008-08-18 17:38:54 +08005155
5156static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
5157 struct ocfs2_xattr_bucket *bucket,
5158 void *para)
5159{
5160 int ret = 0;
Joel Becker3e632942008-10-24 17:04:49 -07005161 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Maa3944252008-08-18 17:38:54 +08005162 u16 i;
5163 struct ocfs2_xattr_entry *xe;
Tao Ma78f30c32008-11-12 08:27:00 +08005164 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5165 struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
Joel Becker548b0f22008-11-24 19:32:13 -08005166 int credits = ocfs2_remove_extent_credits(osb->sb) +
5167 ocfs2_blocks_per_xattr_bucket(inode->i_sb);
5168
Tao Ma78f30c32008-11-12 08:27:00 +08005169
5170 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
Tao Maa3944252008-08-18 17:38:54 +08005171
5172 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
5173 xe = &xh->xh_entries[i];
5174 if (ocfs2_xattr_is_local(xe))
5175 continue;
5176
Tao Ma88c3b062008-12-11 08:54:11 +08005177 ctxt.handle = ocfs2_start_trans(osb, credits);
5178 if (IS_ERR(ctxt.handle)) {
5179 ret = PTR_ERR(ctxt.handle);
5180 mlog_errno(ret);
5181 break;
5182 }
5183
Joel Becker548b0f22008-11-24 19:32:13 -08005184 ret = ocfs2_xattr_bucket_value_truncate(inode, bucket,
Tao Ma78f30c32008-11-12 08:27:00 +08005185 i, 0, &ctxt);
Tao Ma88c3b062008-12-11 08:54:11 +08005186
5187 ocfs2_commit_trans(osb, ctxt.handle);
Tao Maa3944252008-08-18 17:38:54 +08005188 if (ret) {
5189 mlog_errno(ret);
5190 break;
5191 }
5192 }
5193
Tao Ma78f30c32008-11-12 08:27:00 +08005194 ocfs2_schedule_truncate_log_flush(osb, 1);
5195 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tao Maa3944252008-08-18 17:38:54 +08005196 return ret;
5197}
5198
5199static int ocfs2_delete_xattr_index_block(struct inode *inode,
5200 struct buffer_head *xb_bh)
5201{
5202 struct ocfs2_xattr_block *xb =
5203 (struct ocfs2_xattr_block *)xb_bh->b_data;
5204 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
5205 int ret = 0;
5206 u32 name_hash = UINT_MAX, e_cpos, num_clusters;
5207 u64 p_blkno;
5208
5209 if (le16_to_cpu(el->l_next_free_rec) == 0)
5210 return 0;
5211
5212 while (name_hash > 0) {
5213 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
5214 &e_cpos, &num_clusters, el);
5215 if (ret) {
5216 mlog_errno(ret);
5217 goto out;
5218 }
5219
5220 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
5221 ocfs2_delete_xattr_in_bucket,
5222 NULL);
5223 if (ret) {
5224 mlog_errno(ret);
5225 goto out;
5226 }
5227
5228 ret = ocfs2_rm_xattr_cluster(inode, xb_bh,
5229 p_blkno, e_cpos, num_clusters);
5230 if (ret) {
5231 mlog_errno(ret);
5232 break;
5233 }
5234
5235 if (e_cpos == 0)
5236 break;
5237
5238 name_hash = e_cpos - 1;
5239 }
5240
5241out:
5242 return ret;
5243}
Mark Fasheh99219ae2008-10-07 14:52:59 -07005244
5245/*
Tiger Yang923f7f32008-11-14 11:16:27 +08005246 * 'security' attributes support
5247 */
5248static size_t ocfs2_xattr_security_list(struct inode *inode, char *list,
5249 size_t list_size, const char *name,
5250 size_t name_len)
5251{
5252 const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN;
5253 const size_t total_len = prefix_len + name_len + 1;
5254
5255 if (list && total_len <= list_size) {
5256 memcpy(list, XATTR_SECURITY_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_security_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_SECURITY, name,
5269 buffer, size);
5270}
5271
5272static int ocfs2_xattr_security_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_SECURITY, name, value,
5279 size, flags);
5280}
5281
Tiger Yang534eadd2008-11-14 11:16:41 +08005282int ocfs2_init_security_get(struct inode *inode,
5283 struct inode *dir,
5284 struct ocfs2_security_xattr_info *si)
5285{
5286 return security_inode_init_security(inode, dir, &si->name, &si->value,
5287 &si->value_len);
5288}
5289
5290int ocfs2_init_security_set(handle_t *handle,
5291 struct inode *inode,
5292 struct buffer_head *di_bh,
5293 struct ocfs2_security_xattr_info *si,
5294 struct ocfs2_alloc_context *xattr_ac,
5295 struct ocfs2_alloc_context *data_ac)
5296{
5297 return ocfs2_xattr_set_handle(handle, inode, di_bh,
5298 OCFS2_XATTR_INDEX_SECURITY,
5299 si->name, si->value, si->value_len, 0,
5300 xattr_ac, data_ac);
5301}
5302
Tiger Yang923f7f32008-11-14 11:16:27 +08005303struct xattr_handler ocfs2_xattr_security_handler = {
5304 .prefix = XATTR_SECURITY_PREFIX,
5305 .list = ocfs2_xattr_security_list,
5306 .get = ocfs2_xattr_security_get,
5307 .set = ocfs2_xattr_security_set,
5308};
5309
5310/*
Mark Fasheh99219ae2008-10-07 14:52:59 -07005311 * 'trusted' attributes support
5312 */
Mark Fasheh99219ae2008-10-07 14:52:59 -07005313static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list,
5314 size_t list_size, const char *name,
5315 size_t name_len)
5316{
Tiger Yangceb1eba2008-10-23 16:34:13 +08005317 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
Mark Fasheh99219ae2008-10-07 14:52:59 -07005318 const size_t total_len = prefix_len + name_len + 1;
5319
5320 if (list && total_len <= list_size) {
5321 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
5322 memcpy(list + prefix_len, name, name_len);
5323 list[prefix_len + name_len] = '\0';
5324 }
5325 return total_len;
5326}
5327
5328static int ocfs2_xattr_trusted_get(struct inode *inode, const char *name,
5329 void *buffer, size_t size)
5330{
5331 if (strcmp(name, "") == 0)
5332 return -EINVAL;
5333 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, name,
5334 buffer, size);
5335}
5336
5337static int ocfs2_xattr_trusted_set(struct inode *inode, const char *name,
5338 const void *value, size_t size, int flags)
5339{
5340 if (strcmp(name, "") == 0)
5341 return -EINVAL;
5342
5343 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, name, value,
5344 size, flags);
5345}
5346
5347struct xattr_handler ocfs2_xattr_trusted_handler = {
5348 .prefix = XATTR_TRUSTED_PREFIX,
5349 .list = ocfs2_xattr_trusted_list,
5350 .get = ocfs2_xattr_trusted_get,
5351 .set = ocfs2_xattr_trusted_set,
5352};
5353
Mark Fasheh99219ae2008-10-07 14:52:59 -07005354/*
5355 * 'user' attributes support
5356 */
Mark Fasheh99219ae2008-10-07 14:52:59 -07005357static size_t ocfs2_xattr_user_list(struct inode *inode, char *list,
5358 size_t list_size, const char *name,
5359 size_t name_len)
5360{
Tiger Yangceb1eba2008-10-23 16:34:13 +08005361 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
Mark Fasheh99219ae2008-10-07 14:52:59 -07005362 const size_t total_len = prefix_len + name_len + 1;
5363 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5364
5365 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
5366 return 0;
5367
5368 if (list && total_len <= list_size) {
5369 memcpy(list, XATTR_USER_PREFIX, prefix_len);
5370 memcpy(list + prefix_len, name, name_len);
5371 list[prefix_len + name_len] = '\0';
5372 }
5373 return total_len;
5374}
5375
5376static int ocfs2_xattr_user_get(struct inode *inode, const char *name,
5377 void *buffer, size_t size)
5378{
5379 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5380
5381 if (strcmp(name, "") == 0)
5382 return -EINVAL;
5383 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
5384 return -EOPNOTSUPP;
5385 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name,
5386 buffer, size);
5387}
5388
5389static int ocfs2_xattr_user_set(struct inode *inode, const char *name,
5390 const void *value, size_t size, int flags)
5391{
5392 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5393
5394 if (strcmp(name, "") == 0)
5395 return -EINVAL;
5396 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
5397 return -EOPNOTSUPP;
5398
5399 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, name, value,
5400 size, flags);
5401}
5402
5403struct xattr_handler ocfs2_xattr_user_handler = {
5404 .prefix = XATTR_USER_PREFIX,
5405 .list = ocfs2_xattr_user_list,
5406 .get = ocfs2_xattr_user_get,
5407 .set = ocfs2_xattr_user_set,
5408};