blob: 3b059cf2eb45b31f59054da0a1263076a05a0541 [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,
576 struct buffer_head *xattr_bh,
Tao Ma78f30c32008-11-12 08:27:00 +0800577 struct ocfs2_xattr_value_root *xv,
578 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800579{
580 int status = 0;
Tao Ma85db90e2008-11-12 08:27:01 +0800581 handle_t *handle = ctxt->handle;
Tao Maf56654c2008-08-18 17:38:48 +0800582 enum ocfs2_alloc_restarted why;
583 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Joel Becker2a50a742008-12-09 14:24:33 -0800584 struct ocfs2_xattr_value_buf vb = {
585 .vb_bh = xattr_bh,
586 .vb_xv = xv,
587 .vb_access = ocfs2_journal_access,
588 };
589 u32 prev_clusters, logical_start = le32_to_cpu(vb.vb_xv->xr_clusters);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700590 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +0800591
592 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
593
Joel Becker2a50a742008-12-09 14:24:33 -0800594 ocfs2_init_xattr_value_extent_tree(&et, inode, &vb);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700595
Joel Becker2a50a742008-12-09 14:24:33 -0800596 status = vb.vb_access(handle, inode, vb.vb_bh,
597 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Maf56654c2008-08-18 17:38:48 +0800598 if (status < 0) {
599 mlog_errno(status);
600 goto leave;
601 }
602
Joel Becker2a50a742008-12-09 14:24:33 -0800603 prev_clusters = le32_to_cpu(vb.vb_xv->xr_clusters);
Tao Maf56654c2008-08-18 17:38:48 +0800604 status = ocfs2_add_clusters_in_btree(osb,
605 inode,
606 &logical_start,
607 clusters_to_add,
608 0,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700609 &et,
Tao Maf56654c2008-08-18 17:38:48 +0800610 handle,
Tao Ma78f30c32008-11-12 08:27:00 +0800611 ctxt->data_ac,
612 ctxt->meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700613 &why);
Tao Ma85db90e2008-11-12 08:27:01 +0800614 if (status < 0) {
615 mlog_errno(status);
Tao Maf56654c2008-08-18 17:38:48 +0800616 goto leave;
617 }
618
Joel Becker2a50a742008-12-09 14:24:33 -0800619 status = ocfs2_journal_dirty(handle, vb.vb_bh);
Tao Maf56654c2008-08-18 17:38:48 +0800620 if (status < 0) {
621 mlog_errno(status);
622 goto leave;
623 }
624
Joel Becker2a50a742008-12-09 14:24:33 -0800625 clusters_to_add -= le32_to_cpu(vb.vb_xv->xr_clusters) - prev_clusters;
Tao Maf56654c2008-08-18 17:38:48 +0800626
Tao Ma85db90e2008-11-12 08:27:01 +0800627 /*
628 * We should have already allocated enough space before the transaction,
629 * so no need to restart.
630 */
631 BUG_ON(why != RESTART_NONE || clusters_to_add);
Tao Maf56654c2008-08-18 17:38:48 +0800632
633leave:
Tao Maf56654c2008-08-18 17:38:48 +0800634
635 return status;
636}
637
638static int __ocfs2_remove_xattr_range(struct inode *inode,
Joel Beckerd72cc722008-12-09 14:30:41 -0800639 struct ocfs2_xattr_value_buf *vb,
Tao Maf56654c2008-08-18 17:38:48 +0800640 u32 cpos, u32 phys_cpos, u32 len,
Tao Ma78f30c32008-11-12 08:27:00 +0800641 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800642{
643 int ret;
644 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
Tao Ma85db90e2008-11-12 08:27:01 +0800645 handle_t *handle = ctxt->handle;
Joel Beckerf99b9b72008-08-20 19:36:33 -0700646 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +0800647
Joel Beckerd72cc722008-12-09 14:30:41 -0800648 ocfs2_init_xattr_value_extent_tree(&et, inode, vb);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700649
Joel Beckerd72cc722008-12-09 14:30:41 -0800650 ret = vb->vb_access(handle, inode, vb->vb_bh,
651 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Maf56654c2008-08-18 17:38:48 +0800652 if (ret) {
653 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800654 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800655 }
656
Tao Ma78f30c32008-11-12 08:27:00 +0800657 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, ctxt->meta_ac,
658 &ctxt->dealloc);
Tao Maf56654c2008-08-18 17:38:48 +0800659 if (ret) {
660 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800661 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800662 }
663
Joel Beckerd72cc722008-12-09 14:30:41 -0800664 le32_add_cpu(&vb->vb_xv->xr_clusters, -len);
Tao Maf56654c2008-08-18 17:38:48 +0800665
Joel Beckerd72cc722008-12-09 14:30:41 -0800666 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
Tao Maf56654c2008-08-18 17:38:48 +0800667 if (ret) {
668 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800669 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800670 }
671
Tao Ma78f30c32008-11-12 08:27:00 +0800672 ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc, phys_blkno, len);
Tao Maf56654c2008-08-18 17:38:48 +0800673 if (ret)
674 mlog_errno(ret);
675
Tao Maf56654c2008-08-18 17:38:48 +0800676out:
Tao Maf56654c2008-08-18 17:38:48 +0800677 return ret;
678}
679
680static int ocfs2_xattr_shrink_size(struct inode *inode,
681 u32 old_clusters,
682 u32 new_clusters,
683 struct buffer_head *root_bh,
Tao Ma78f30c32008-11-12 08:27:00 +0800684 struct ocfs2_xattr_value_root *xv,
685 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800686{
687 int ret = 0;
688 u32 trunc_len, cpos, phys_cpos, alloc_size;
689 u64 block;
Joel Beckerd72cc722008-12-09 14:30:41 -0800690 struct ocfs2_xattr_value_buf vb = {
691 .vb_bh = root_bh,
692 .vb_xv = xv,
693 .vb_access = ocfs2_journal_access,
694 };
Tao Maf56654c2008-08-18 17:38:48 +0800695
696 if (old_clusters <= new_clusters)
697 return 0;
698
699 cpos = new_clusters;
700 trunc_len = old_clusters - new_clusters;
701 while (trunc_len) {
702 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
Joel Beckerd72cc722008-12-09 14:30:41 -0800703 &alloc_size,
704 &vb.vb_xv->xr_list);
Tao Maf56654c2008-08-18 17:38:48 +0800705 if (ret) {
706 mlog_errno(ret);
707 goto out;
708 }
709
710 if (alloc_size > trunc_len)
711 alloc_size = trunc_len;
712
Joel Beckerd72cc722008-12-09 14:30:41 -0800713 ret = __ocfs2_remove_xattr_range(inode, &vb, cpos,
Tao Maf56654c2008-08-18 17:38:48 +0800714 phys_cpos, alloc_size,
Tao Ma78f30c32008-11-12 08:27:00 +0800715 ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800716 if (ret) {
717 mlog_errno(ret);
718 goto out;
719 }
720
721 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
722 ocfs2_remove_xattr_clusters_from_cache(inode, block,
723 alloc_size);
724 cpos += alloc_size;
725 trunc_len -= alloc_size;
726 }
727
728out:
Tao Maf56654c2008-08-18 17:38:48 +0800729 return ret;
730}
731
732static int ocfs2_xattr_value_truncate(struct inode *inode,
733 struct buffer_head *root_bh,
734 struct ocfs2_xattr_value_root *xv,
Tao Ma78f30c32008-11-12 08:27:00 +0800735 int len,
736 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800737{
738 int ret;
739 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
740 u32 old_clusters = le32_to_cpu(xv->xr_clusters);
741
742 if (new_clusters == old_clusters)
743 return 0;
744
745 if (new_clusters > old_clusters)
746 ret = ocfs2_xattr_extend_allocation(inode,
747 new_clusters - old_clusters,
Tao Ma78f30c32008-11-12 08:27:00 +0800748 root_bh, xv, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800749 else
750 ret = ocfs2_xattr_shrink_size(inode,
751 old_clusters, new_clusters,
Tao Ma78f30c32008-11-12 08:27:00 +0800752 root_bh, xv, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800753
754 return ret;
755}
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800756
Tao Ma936b8832008-10-09 23:06:14 +0800757static int ocfs2_xattr_list_entry(char *buffer, size_t size,
758 size_t *result, const char *prefix,
759 const char *name, int name_len)
760{
761 char *p = buffer + *result;
762 int prefix_len = strlen(prefix);
763 int total_len = prefix_len + name_len + 1;
764
765 *result += total_len;
766
767 /* we are just looking for how big our buffer needs to be */
768 if (!size)
769 return 0;
770
771 if (*result > size)
772 return -ERANGE;
773
774 memcpy(p, prefix, prefix_len);
775 memcpy(p + prefix_len, name, name_len);
776 p[prefix_len + name_len] = '\0';
777
778 return 0;
779}
780
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800781static int ocfs2_xattr_list_entries(struct inode *inode,
782 struct ocfs2_xattr_header *header,
783 char *buffer, size_t buffer_size)
784{
Tao Ma936b8832008-10-09 23:06:14 +0800785 size_t result = 0;
786 int i, type, ret;
787 const char *prefix, *name;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800788
789 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
790 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
Tao Ma936b8832008-10-09 23:06:14 +0800791 type = ocfs2_xattr_get_type(entry);
792 prefix = ocfs2_xattr_prefix(type);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800793
Tao Ma936b8832008-10-09 23:06:14 +0800794 if (prefix) {
795 name = (const char *)header +
796 le16_to_cpu(entry->xe_name_offset);
797
798 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
799 &result, prefix, name,
800 entry->xe_name_len);
801 if (ret)
802 return ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800803 }
804 }
805
Tao Ma936b8832008-10-09 23:06:14 +0800806 return result;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800807}
808
809static int ocfs2_xattr_ibody_list(struct inode *inode,
810 struct ocfs2_dinode *di,
811 char *buffer,
812 size_t buffer_size)
813{
814 struct ocfs2_xattr_header *header = NULL;
815 struct ocfs2_inode_info *oi = OCFS2_I(inode);
816 int ret = 0;
817
818 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
819 return ret;
820
821 header = (struct ocfs2_xattr_header *)
822 ((void *)di + inode->i_sb->s_blocksize -
823 le16_to_cpu(di->i_xattr_inline_size));
824
825 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
826
827 return ret;
828}
829
830static int ocfs2_xattr_block_list(struct inode *inode,
831 struct ocfs2_dinode *di,
832 char *buffer,
833 size_t buffer_size)
834{
835 struct buffer_head *blk_bh = NULL;
Tao Ma0c044f02008-08-18 17:38:50 +0800836 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800837 int ret = 0;
838
839 if (!di->i_xattr_loc)
840 return ret;
841
Joel Becker4ae1d692008-11-13 14:49:18 -0800842 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
843 &blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800844 if (ret < 0) {
845 mlog_errno(ret);
846 return ret;
847 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800848
Tao Ma0c044f02008-08-18 17:38:50 +0800849 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Ma0c044f02008-08-18 17:38:50 +0800850 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
851 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
852 ret = ocfs2_xattr_list_entries(inode, header,
853 buffer, buffer_size);
854 } else {
855 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
856 ret = ocfs2_xattr_tree_list_index_block(inode, xt,
857 buffer, buffer_size);
858 }
Joel Becker4ae1d692008-11-13 14:49:18 -0800859
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800860 brelse(blk_bh);
861
862 return ret;
863}
864
865ssize_t ocfs2_listxattr(struct dentry *dentry,
866 char *buffer,
867 size_t size)
868{
869 int ret = 0, i_ret = 0, b_ret = 0;
870 struct buffer_head *di_bh = NULL;
871 struct ocfs2_dinode *di = NULL;
872 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
873
Tiger Yang8154da32008-08-18 17:11:46 +0800874 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
875 return -EOPNOTSUPP;
876
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800877 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
878 return ret;
879
880 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
881 if (ret < 0) {
882 mlog_errno(ret);
883 return ret;
884 }
885
886 di = (struct ocfs2_dinode *)di_bh->b_data;
887
888 down_read(&oi->ip_xattr_sem);
889 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
890 if (i_ret < 0)
891 b_ret = 0;
892 else {
893 if (buffer) {
894 buffer += i_ret;
895 size -= i_ret;
896 }
897 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
898 buffer, size);
899 if (b_ret < 0)
900 i_ret = 0;
901 }
902 up_read(&oi->ip_xattr_sem);
903 ocfs2_inode_unlock(dentry->d_inode, 0);
904
905 brelse(di_bh);
906
907 return i_ret + b_ret;
908}
909
910static int ocfs2_xattr_find_entry(int name_index,
911 const char *name,
912 struct ocfs2_xattr_search *xs)
913{
914 struct ocfs2_xattr_entry *entry;
915 size_t name_len;
916 int i, cmp = 1;
917
918 if (name == NULL)
919 return -EINVAL;
920
921 name_len = strlen(name);
922 entry = xs->here;
923 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
924 cmp = name_index - ocfs2_xattr_get_type(entry);
925 if (!cmp)
926 cmp = name_len - entry->xe_name_len;
927 if (!cmp)
928 cmp = memcmp(name, (xs->base +
929 le16_to_cpu(entry->xe_name_offset)),
930 name_len);
931 if (cmp == 0)
932 break;
933 entry += 1;
934 }
935 xs->here = entry;
936
937 return cmp ? -ENODATA : 0;
938}
939
940static int ocfs2_xattr_get_value_outside(struct inode *inode,
Tao Ma589dc262008-08-18 17:38:51 +0800941 struct ocfs2_xattr_value_root *xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800942 void *buffer,
943 size_t len)
944{
945 u32 cpos, p_cluster, num_clusters, bpc, clusters;
946 u64 blkno;
947 int i, ret = 0;
948 size_t cplen, blocksize;
949 struct buffer_head *bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800950 struct ocfs2_extent_list *el;
951
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800952 el = &xv->xr_list;
953 clusters = le32_to_cpu(xv->xr_clusters);
954 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
955 blocksize = inode->i_sb->s_blocksize;
956
957 cpos = 0;
958 while (cpos < clusters) {
959 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
960 &num_clusters, el);
961 if (ret) {
962 mlog_errno(ret);
963 goto out;
964 }
965
966 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
967 /* Copy ocfs2_xattr_value */
968 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
Joel Becker970e4932008-11-13 14:49:19 -0800969 ret = ocfs2_read_block(inode, blkno, &bh, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800970 if (ret) {
971 mlog_errno(ret);
972 goto out;
973 }
974
975 cplen = len >= blocksize ? blocksize : len;
976 memcpy(buffer, bh->b_data, cplen);
977 len -= cplen;
978 buffer += cplen;
979
980 brelse(bh);
981 bh = NULL;
982 if (len == 0)
983 break;
984 }
985 cpos += num_clusters;
986 }
987out:
988 return ret;
989}
990
991static int ocfs2_xattr_ibody_get(struct inode *inode,
992 int name_index,
993 const char *name,
994 void *buffer,
995 size_t buffer_size,
996 struct ocfs2_xattr_search *xs)
997{
998 struct ocfs2_inode_info *oi = OCFS2_I(inode);
999 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
Tao Ma589dc262008-08-18 17:38:51 +08001000 struct ocfs2_xattr_value_root *xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001001 size_t size;
1002 int ret = 0;
1003
1004 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
1005 return -ENODATA;
1006
1007 xs->end = (void *)di + inode->i_sb->s_blocksize;
1008 xs->header = (struct ocfs2_xattr_header *)
1009 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1010 xs->base = (void *)xs->header;
1011 xs->here = xs->header->xh_entries;
1012
1013 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1014 if (ret)
1015 return ret;
1016 size = le64_to_cpu(xs->here->xe_value_size);
1017 if (buffer) {
1018 if (size > buffer_size)
1019 return -ERANGE;
1020 if (ocfs2_xattr_is_local(xs->here)) {
1021 memcpy(buffer, (void *)xs->base +
1022 le16_to_cpu(xs->here->xe_name_offset) +
1023 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
1024 } else {
Tao Ma589dc262008-08-18 17:38:51 +08001025 xv = (struct ocfs2_xattr_value_root *)
1026 (xs->base + le16_to_cpu(
1027 xs->here->xe_name_offset) +
1028 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
1029 ret = ocfs2_xattr_get_value_outside(inode, xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001030 buffer, size);
1031 if (ret < 0) {
1032 mlog_errno(ret);
1033 return ret;
1034 }
1035 }
1036 }
1037
1038 return size;
1039}
1040
1041static int ocfs2_xattr_block_get(struct inode *inode,
1042 int name_index,
1043 const char *name,
1044 void *buffer,
1045 size_t buffer_size,
1046 struct ocfs2_xattr_search *xs)
1047{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001048 struct ocfs2_xattr_block *xb;
Tao Ma589dc262008-08-18 17:38:51 +08001049 struct ocfs2_xattr_value_root *xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001050 size_t size;
Tao Ma589dc262008-08-18 17:38:51 +08001051 int ret = -ENODATA, name_offset, name_len, block_off, i;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001052
Joel Beckerba937122008-10-24 19:13:20 -07001053 xs->bucket = ocfs2_xattr_bucket_new(inode);
1054 if (!xs->bucket) {
1055 ret = -ENOMEM;
1056 mlog_errno(ret);
1057 goto cleanup;
1058 }
Tao Ma589dc262008-08-18 17:38:51 +08001059
Joel Becker54f443f2008-10-20 18:43:07 -07001060 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
1061 if (ret) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001062 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001063 goto cleanup;
1064 }
1065
Tiger Yang6c1e1832008-11-02 19:04:21 +08001066 if (xs->not_found) {
1067 ret = -ENODATA;
1068 goto cleanup;
1069 }
1070
Joel Becker54f443f2008-10-20 18:43:07 -07001071 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001072 size = le64_to_cpu(xs->here->xe_value_size);
1073 if (buffer) {
1074 ret = -ERANGE;
1075 if (size > buffer_size)
1076 goto cleanup;
Tao Ma589dc262008-08-18 17:38:51 +08001077
1078 name_offset = le16_to_cpu(xs->here->xe_name_offset);
1079 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
1080 i = xs->here - xs->header->xh_entries;
1081
1082 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
1083 ret = ocfs2_xattr_bucket_get_name_value(inode,
Joel Beckerba937122008-10-24 19:13:20 -07001084 bucket_xh(xs->bucket),
Tao Ma589dc262008-08-18 17:38:51 +08001085 i,
1086 &block_off,
1087 &name_offset);
Joel Beckerba937122008-10-24 19:13:20 -07001088 xs->base = bucket_block(xs->bucket, block_off);
Tao Ma589dc262008-08-18 17:38:51 +08001089 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001090 if (ocfs2_xattr_is_local(xs->here)) {
1091 memcpy(buffer, (void *)xs->base +
Tao Ma589dc262008-08-18 17:38:51 +08001092 name_offset + name_len, size);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001093 } else {
Tao Ma589dc262008-08-18 17:38:51 +08001094 xv = (struct ocfs2_xattr_value_root *)
1095 (xs->base + name_offset + name_len);
1096 ret = ocfs2_xattr_get_value_outside(inode, xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001097 buffer, size);
1098 if (ret < 0) {
1099 mlog_errno(ret);
1100 goto cleanup;
1101 }
1102 }
1103 }
1104 ret = size;
1105cleanup:
Joel Beckerba937122008-10-24 19:13:20 -07001106 ocfs2_xattr_bucket_free(xs->bucket);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001107
Joel Becker54f443f2008-10-20 18:43:07 -07001108 brelse(xs->xattr_bh);
1109 xs->xattr_bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001110 return ret;
1111}
1112
Tiger Yang4e3e9d022008-11-14 11:16:53 +08001113int ocfs2_xattr_get_nolock(struct inode *inode,
1114 struct buffer_head *di_bh,
Tiger Yang0030e002008-10-23 16:33:33 +08001115 int name_index,
1116 const char *name,
1117 void *buffer,
1118 size_t buffer_size)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001119{
1120 int ret;
1121 struct ocfs2_dinode *di = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001122 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1123 struct ocfs2_xattr_search xis = {
1124 .not_found = -ENODATA,
1125 };
1126 struct ocfs2_xattr_search xbs = {
1127 .not_found = -ENODATA,
1128 };
1129
Tiger Yang8154da32008-08-18 17:11:46 +08001130 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1131 return -EOPNOTSUPP;
1132
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001133 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1134 ret = -ENODATA;
1135
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001136 xis.inode_bh = xbs.inode_bh = di_bh;
1137 di = (struct ocfs2_dinode *)di_bh->b_data;
1138
1139 down_read(&oi->ip_xattr_sem);
1140 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1141 buffer_size, &xis);
Tiger Yang6c1e1832008-11-02 19:04:21 +08001142 if (ret == -ENODATA && di->i_xattr_loc)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001143 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1144 buffer_size, &xbs);
1145 up_read(&oi->ip_xattr_sem);
Tiger Yang4e3e9d022008-11-14 11:16:53 +08001146
1147 return ret;
1148}
1149
1150/* ocfs2_xattr_get()
1151 *
1152 * Copy an extended attribute into the buffer provided.
1153 * Buffer is NULL to compute the size of buffer required.
1154 */
1155static int ocfs2_xattr_get(struct inode *inode,
1156 int name_index,
1157 const char *name,
1158 void *buffer,
1159 size_t buffer_size)
1160{
1161 int ret;
1162 struct buffer_head *di_bh = NULL;
1163
1164 ret = ocfs2_inode_lock(inode, &di_bh, 0);
1165 if (ret < 0) {
1166 mlog_errno(ret);
1167 return ret;
1168 }
1169 ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
1170 name, buffer, buffer_size);
1171
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001172 ocfs2_inode_unlock(inode, 0);
1173
1174 brelse(di_bh);
1175
1176 return ret;
1177}
1178
1179static int __ocfs2_xattr_set_value_outside(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001180 handle_t *handle,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001181 struct ocfs2_xattr_value_root *xv,
1182 const void *value,
1183 int value_len)
1184{
1185 int ret = 0, i, cp_len, credits;
1186 u16 blocksize = inode->i_sb->s_blocksize;
1187 u32 p_cluster, num_clusters;
1188 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1189 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1190 u64 blkno;
1191 struct buffer_head *bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001192
1193 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1194
Tao Ma85db90e2008-11-12 08:27:01 +08001195 /*
1196 * In __ocfs2_xattr_set_value_outside has already been dirtied,
1197 * so we don't need to worry about whether ocfs2_extend_trans
1198 * will create a new transactio for us or not.
1199 */
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001200 credits = clusters * bpc;
Tao Ma85db90e2008-11-12 08:27:01 +08001201 ret = ocfs2_extend_trans(handle, credits);
1202 if (ret) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001203 mlog_errno(ret);
1204 goto out;
1205 }
1206
1207 while (cpos < clusters) {
1208 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1209 &num_clusters, &xv->xr_list);
1210 if (ret) {
1211 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001212 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001213 }
1214
1215 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1216
1217 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
Joel Becker970e4932008-11-13 14:49:19 -08001218 ret = ocfs2_read_block(inode, blkno, &bh, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001219 if (ret) {
1220 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001221 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001222 }
1223
1224 ret = ocfs2_journal_access(handle,
1225 inode,
1226 bh,
1227 OCFS2_JOURNAL_ACCESS_WRITE);
1228 if (ret < 0) {
1229 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001230 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001231 }
1232
1233 cp_len = value_len > blocksize ? blocksize : value_len;
1234 memcpy(bh->b_data, value, cp_len);
1235 value_len -= cp_len;
1236 value += cp_len;
1237 if (cp_len < blocksize)
1238 memset(bh->b_data + cp_len, 0,
1239 blocksize - cp_len);
1240
1241 ret = ocfs2_journal_dirty(handle, bh);
1242 if (ret < 0) {
1243 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001244 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001245 }
1246 brelse(bh);
1247 bh = NULL;
1248
1249 /*
1250 * XXX: do we need to empty all the following
1251 * blocks in this cluster?
1252 */
1253 if (!value_len)
1254 break;
1255 }
1256 cpos += num_clusters;
1257 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001258out:
1259 brelse(bh);
1260
1261 return ret;
1262}
1263
1264static int ocfs2_xattr_cleanup(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001265 handle_t *handle,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001266 struct ocfs2_xattr_info *xi,
1267 struct ocfs2_xattr_search *xs,
1268 size_t offs)
1269{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001270 int ret = 0;
1271 size_t name_len = strlen(xi->name);
1272 void *val = xs->base + offs;
1273 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1274
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001275 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1276 OCFS2_JOURNAL_ACCESS_WRITE);
1277 if (ret) {
1278 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001279 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001280 }
1281 /* Decrease xattr count */
1282 le16_add_cpu(&xs->header->xh_count, -1);
1283 /* Remove the xattr entry and tree root which has already be set*/
1284 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
1285 memset(val, 0, size);
1286
1287 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1288 if (ret < 0)
1289 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001290out:
1291 return ret;
1292}
1293
1294static int ocfs2_xattr_update_entry(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001295 handle_t *handle,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001296 struct ocfs2_xattr_info *xi,
1297 struct ocfs2_xattr_search *xs,
1298 size_t offs)
1299{
Tao Ma85db90e2008-11-12 08:27:01 +08001300 int ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001301
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001302 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1303 OCFS2_JOURNAL_ACCESS_WRITE);
1304 if (ret) {
1305 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001306 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001307 }
1308
1309 xs->here->xe_name_offset = cpu_to_le16(offs);
1310 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1311 if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
1312 ocfs2_xattr_set_local(xs->here, 1);
1313 else
1314 ocfs2_xattr_set_local(xs->here, 0);
1315 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1316
1317 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1318 if (ret < 0)
1319 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001320out:
1321 return ret;
1322}
1323
1324/*
1325 * ocfs2_xattr_set_value_outside()
1326 *
1327 * Set large size value in B tree.
1328 */
1329static int ocfs2_xattr_set_value_outside(struct inode *inode,
1330 struct ocfs2_xattr_info *xi,
1331 struct ocfs2_xattr_search *xs,
Tao Ma78f30c32008-11-12 08:27:00 +08001332 struct ocfs2_xattr_set_ctxt *ctxt,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001333 size_t offs)
1334{
1335 size_t name_len = strlen(xi->name);
1336 void *val = xs->base + offs;
1337 struct ocfs2_xattr_value_root *xv = NULL;
1338 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1339 int ret = 0;
1340
1341 memset(val, 0, size);
1342 memcpy(val, xi->name, name_len);
1343 xv = (struct ocfs2_xattr_value_root *)
1344 (val + OCFS2_XATTR_SIZE(name_len));
1345 xv->xr_clusters = 0;
1346 xv->xr_last_eb_blk = 0;
1347 xv->xr_list.l_tree_depth = 0;
1348 xv->xr_list.l_count = cpu_to_le16(1);
1349 xv->xr_list.l_next_free_rec = 0;
1350
1351 ret = ocfs2_xattr_value_truncate(inode, xs->xattr_bh, xv,
Tao Ma78f30c32008-11-12 08:27:00 +08001352 xi->value_len, ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001353 if (ret < 0) {
1354 mlog_errno(ret);
1355 return ret;
1356 }
Tao Ma85db90e2008-11-12 08:27:01 +08001357 ret = ocfs2_xattr_update_entry(inode, ctxt->handle, xi, xs, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001358 if (ret < 0) {
1359 mlog_errno(ret);
1360 return ret;
1361 }
Tao Ma85db90e2008-11-12 08:27:01 +08001362 ret = __ocfs2_xattr_set_value_outside(inode, ctxt->handle, xv,
1363 xi->value, xi->value_len);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001364 if (ret < 0)
1365 mlog_errno(ret);
1366
1367 return ret;
1368}
1369
1370/*
1371 * ocfs2_xattr_set_entry_local()
1372 *
1373 * Set, replace or remove extended attribute in local.
1374 */
1375static void ocfs2_xattr_set_entry_local(struct inode *inode,
1376 struct ocfs2_xattr_info *xi,
1377 struct ocfs2_xattr_search *xs,
1378 struct ocfs2_xattr_entry *last,
1379 size_t min_offs)
1380{
1381 size_t name_len = strlen(xi->name);
1382 int i;
1383
1384 if (xi->value && xs->not_found) {
1385 /* Insert the new xattr entry. */
1386 le16_add_cpu(&xs->header->xh_count, 1);
1387 ocfs2_xattr_set_type(last, xi->name_index);
1388 ocfs2_xattr_set_local(last, 1);
1389 last->xe_name_len = name_len;
1390 } else {
1391 void *first_val;
1392 void *val;
1393 size_t offs, size;
1394
1395 first_val = xs->base + min_offs;
1396 offs = le16_to_cpu(xs->here->xe_name_offset);
1397 val = xs->base + offs;
1398
1399 if (le64_to_cpu(xs->here->xe_value_size) >
1400 OCFS2_XATTR_INLINE_SIZE)
1401 size = OCFS2_XATTR_SIZE(name_len) +
1402 OCFS2_XATTR_ROOT_SIZE;
1403 else
1404 size = OCFS2_XATTR_SIZE(name_len) +
1405 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1406
1407 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1408 OCFS2_XATTR_SIZE(xi->value_len)) {
1409 /* The old and the new value have the
1410 same size. Just replace the value. */
1411 ocfs2_xattr_set_local(xs->here, 1);
1412 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1413 /* Clear value bytes. */
1414 memset(val + OCFS2_XATTR_SIZE(name_len),
1415 0,
1416 OCFS2_XATTR_SIZE(xi->value_len));
1417 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1418 xi->value,
1419 xi->value_len);
1420 return;
1421 }
1422 /* Remove the old name+value. */
1423 memmove(first_val + size, first_val, val - first_val);
1424 memset(first_val, 0, size);
1425 xs->here->xe_name_hash = 0;
1426 xs->here->xe_name_offset = 0;
1427 ocfs2_xattr_set_local(xs->here, 1);
1428 xs->here->xe_value_size = 0;
1429
1430 min_offs += size;
1431
1432 /* Adjust all value offsets. */
1433 last = xs->header->xh_entries;
1434 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1435 size_t o = le16_to_cpu(last->xe_name_offset);
1436
1437 if (o < offs)
1438 last->xe_name_offset = cpu_to_le16(o + size);
1439 last += 1;
1440 }
1441
1442 if (!xi->value) {
1443 /* Remove the old entry. */
1444 last -= 1;
1445 memmove(xs->here, xs->here + 1,
1446 (void *)last - (void *)xs->here);
1447 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1448 le16_add_cpu(&xs->header->xh_count, -1);
1449 }
1450 }
1451 if (xi->value) {
1452 /* Insert the new name+value. */
1453 size_t size = OCFS2_XATTR_SIZE(name_len) +
1454 OCFS2_XATTR_SIZE(xi->value_len);
1455 void *val = xs->base + min_offs - size;
1456
1457 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1458 memset(val, 0, size);
1459 memcpy(val, xi->name, name_len);
1460 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1461 xi->value,
1462 xi->value_len);
1463 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1464 ocfs2_xattr_set_local(xs->here, 1);
1465 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1466 }
1467
1468 return;
1469}
1470
1471/*
1472 * ocfs2_xattr_set_entry()
1473 *
1474 * Set extended attribute entry into inode or block.
1475 *
1476 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1477 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1478 * then set value in B tree with set_value_outside().
1479 */
1480static int ocfs2_xattr_set_entry(struct inode *inode,
1481 struct ocfs2_xattr_info *xi,
1482 struct ocfs2_xattr_search *xs,
Tao Ma78f30c32008-11-12 08:27:00 +08001483 struct ocfs2_xattr_set_ctxt *ctxt,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001484 int flag)
1485{
1486 struct ocfs2_xattr_entry *last;
1487 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1488 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1489 size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1490 size_t size_l = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08001491 handle_t *handle = ctxt->handle;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001492 int free, i, ret;
1493 struct ocfs2_xattr_info xi_l = {
1494 .name_index = xi->name_index,
1495 .name = xi->name,
1496 .value = xi->value,
1497 .value_len = xi->value_len,
1498 };
1499
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,
Tao Ma78f30c32008-11-12 08:27:00 +08001554 ctxt, 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 */
1560 struct ocfs2_xattr_value_root *xv = NULL;
1561 xv = (struct ocfs2_xattr_value_root *)(val +
1562 OCFS2_XATTR_SIZE(name_len));
1563
1564 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1565 /*
1566 * If new value need set outside also,
1567 * first truncate old value to new value,
1568 * then set new value with set_value_outside().
1569 */
1570 ret = ocfs2_xattr_value_truncate(inode,
1571 xs->xattr_bh,
1572 xv,
Tao Ma78f30c32008-11-12 08:27:00 +08001573 xi->value_len,
1574 ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001575 if (ret < 0) {
1576 mlog_errno(ret);
1577 goto out;
1578 }
1579
Tao Ma85db90e2008-11-12 08:27:01 +08001580 ret = ocfs2_xattr_update_entry(inode,
1581 handle,
1582 xi,
1583 xs,
1584 offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001585 if (ret < 0) {
1586 mlog_errno(ret);
1587 goto out;
1588 }
1589
Tao Ma85db90e2008-11-12 08:27:01 +08001590 ret = __ocfs2_xattr_set_value_outside(inode,
1591 handle,
1592 xv,
1593 xi->value,
1594 xi->value_len);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001595 if (ret < 0)
1596 mlog_errno(ret);
1597 goto out;
1598 } else {
1599 /*
1600 * If new value need set in local,
1601 * just trucate old value to zero.
1602 */
1603 ret = ocfs2_xattr_value_truncate(inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001604 xs->xattr_bh,
1605 xv,
1606 0,
1607 ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001608 if (ret < 0)
1609 mlog_errno(ret);
1610 }
1611 }
1612 }
1613
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001614 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1615 OCFS2_JOURNAL_ACCESS_WRITE);
1616 if (ret) {
1617 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001618 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001619 }
1620
1621 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001622 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1623 OCFS2_JOURNAL_ACCESS_WRITE);
1624 if (ret) {
1625 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001626 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001627 }
1628 }
1629
1630 /*
1631 * Set value in local, include set tree root in local.
1632 * This is the first step for value size >INLINE_SIZE.
1633 */
1634 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1635
1636 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1637 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1638 if (ret < 0) {
1639 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001640 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001641 }
1642 }
1643
1644 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1645 (flag & OCFS2_INLINE_XATTR_FL)) {
1646 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1647 unsigned int xattrsize = osb->s_xattr_inline_size;
1648
1649 /*
1650 * Adjust extent record count or inline data size
1651 * to reserve space for extended attribute.
1652 */
1653 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1654 struct ocfs2_inline_data *idata = &di->id2.i_data;
1655 le16_add_cpu(&idata->id_count, -xattrsize);
1656 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1657 struct ocfs2_extent_list *el = &di->id2.i_list;
1658 le16_add_cpu(&el->l_count, -(xattrsize /
1659 sizeof(struct ocfs2_extent_rec)));
1660 }
1661 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1662 }
1663 /* Update xattr flag */
1664 spin_lock(&oi->ip_lock);
1665 oi->ip_dyn_features |= flag;
1666 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1667 spin_unlock(&oi->ip_lock);
1668 /* Update inode ctime */
1669 inode->i_ctime = CURRENT_TIME;
1670 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
1671 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
1672
1673 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1674 if (ret < 0)
1675 mlog_errno(ret);
1676
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001677 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1678 /*
1679 * Set value outside in B tree.
1680 * This is the second step for value size > INLINE_SIZE.
1681 */
1682 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
Tao Ma78f30c32008-11-12 08:27:00 +08001683 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, ctxt, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001684 if (ret < 0) {
1685 int ret2;
1686
1687 mlog_errno(ret);
1688 /*
1689 * If set value outside failed, we have to clean
1690 * the junk tree root we have already set in local.
1691 */
Tao Ma85db90e2008-11-12 08:27:01 +08001692 ret2 = ocfs2_xattr_cleanup(inode, ctxt->handle,
1693 xi, xs, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001694 if (ret2 < 0)
1695 mlog_errno(ret2);
1696 }
1697 }
1698out:
1699 return ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001700}
1701
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001702static int ocfs2_remove_value_outside(struct inode*inode,
1703 struct buffer_head *bh,
1704 struct ocfs2_xattr_header *header)
1705{
1706 int ret = 0, i;
Tao Ma78f30c32008-11-12 08:27:00 +08001707 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1708 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
1709
1710 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001711
Jan Karaa90714c2008-10-09 19:38:40 +02001712 ctxt.handle = ocfs2_start_trans(osb,
1713 ocfs2_remove_extent_credits(osb->sb));
Tao Ma85db90e2008-11-12 08:27:01 +08001714 if (IS_ERR(ctxt.handle)) {
1715 ret = PTR_ERR(ctxt.handle);
1716 mlog_errno(ret);
1717 goto out;
1718 }
1719
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001720 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1721 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1722
1723 if (!ocfs2_xattr_is_local(entry)) {
1724 struct ocfs2_xattr_value_root *xv;
1725 void *val;
1726
1727 val = (void *)header +
1728 le16_to_cpu(entry->xe_name_offset);
1729 xv = (struct ocfs2_xattr_value_root *)
1730 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
Tao Ma78f30c32008-11-12 08:27:00 +08001731 ret = ocfs2_xattr_value_truncate(inode, bh, xv,
1732 0, &ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001733 if (ret < 0) {
1734 mlog_errno(ret);
Tao Ma78f30c32008-11-12 08:27:00 +08001735 break;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001736 }
1737 }
1738 }
1739
Tao Ma85db90e2008-11-12 08:27:01 +08001740 ocfs2_commit_trans(osb, ctxt.handle);
Tao Ma78f30c32008-11-12 08:27:00 +08001741 ocfs2_schedule_truncate_log_flush(osb, 1);
1742 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tao Ma85db90e2008-11-12 08:27:01 +08001743out:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001744 return ret;
1745}
1746
1747static int ocfs2_xattr_ibody_remove(struct inode *inode,
1748 struct buffer_head *di_bh)
1749{
1750
1751 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1752 struct ocfs2_xattr_header *header;
1753 int ret;
1754
1755 header = (struct ocfs2_xattr_header *)
1756 ((void *)di + inode->i_sb->s_blocksize -
1757 le16_to_cpu(di->i_xattr_inline_size));
1758
1759 ret = ocfs2_remove_value_outside(inode, di_bh, header);
1760
1761 return ret;
1762}
1763
1764static int ocfs2_xattr_block_remove(struct inode *inode,
1765 struct buffer_head *blk_bh)
1766{
1767 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001768 int ret = 0;
1769
1770 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Maa3944252008-08-18 17:38:54 +08001771 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1772 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
1773 ret = ocfs2_remove_value_outside(inode, blk_bh, header);
1774 } else
1775 ret = ocfs2_delete_xattr_index_block(inode, blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001776
1777 return ret;
1778}
1779
Tao Ma08413892008-08-29 09:00:19 +08001780static int ocfs2_xattr_free_block(struct inode *inode,
1781 u64 block)
1782{
1783 struct inode *xb_alloc_inode;
1784 struct buffer_head *xb_alloc_bh = NULL;
1785 struct buffer_head *blk_bh = NULL;
1786 struct ocfs2_xattr_block *xb;
1787 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1788 handle_t *handle;
1789 int ret = 0;
1790 u64 blk, bg_blkno;
1791 u16 bit;
1792
Joel Becker4ae1d692008-11-13 14:49:18 -08001793 ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
Tao Ma08413892008-08-29 09:00:19 +08001794 if (ret < 0) {
1795 mlog_errno(ret);
1796 goto out;
1797 }
1798
Tao Ma08413892008-08-29 09:00:19 +08001799 ret = ocfs2_xattr_block_remove(inode, blk_bh);
1800 if (ret < 0) {
1801 mlog_errno(ret);
1802 goto out;
1803 }
1804
Joel Becker4ae1d692008-11-13 14:49:18 -08001805 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Ma08413892008-08-29 09:00:19 +08001806 blk = le64_to_cpu(xb->xb_blkno);
1807 bit = le16_to_cpu(xb->xb_suballoc_bit);
1808 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1809
1810 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1811 EXTENT_ALLOC_SYSTEM_INODE,
1812 le16_to_cpu(xb->xb_suballoc_slot));
1813 if (!xb_alloc_inode) {
1814 ret = -ENOMEM;
1815 mlog_errno(ret);
1816 goto out;
1817 }
1818 mutex_lock(&xb_alloc_inode->i_mutex);
1819
1820 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1821 if (ret < 0) {
1822 mlog_errno(ret);
1823 goto out_mutex;
1824 }
1825
1826 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
1827 if (IS_ERR(handle)) {
1828 ret = PTR_ERR(handle);
1829 mlog_errno(ret);
1830 goto out_unlock;
1831 }
1832
1833 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1834 bit, bg_blkno, 1);
1835 if (ret < 0)
1836 mlog_errno(ret);
1837
1838 ocfs2_commit_trans(osb, handle);
1839out_unlock:
1840 ocfs2_inode_unlock(xb_alloc_inode, 1);
1841 brelse(xb_alloc_bh);
1842out_mutex:
1843 mutex_unlock(&xb_alloc_inode->i_mutex);
1844 iput(xb_alloc_inode);
1845out:
1846 brelse(blk_bh);
1847 return ret;
1848}
1849
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001850/*
1851 * ocfs2_xattr_remove()
1852 *
1853 * Free extended attribute resources associated with this inode.
1854 */
1855int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
1856{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001857 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1858 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1859 handle_t *handle;
1860 int ret;
1861
Tiger Yang8154da32008-08-18 17:11:46 +08001862 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1863 return 0;
1864
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001865 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1866 return 0;
1867
1868 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1869 ret = ocfs2_xattr_ibody_remove(inode, di_bh);
1870 if (ret < 0) {
1871 mlog_errno(ret);
1872 goto out;
1873 }
1874 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001875
Tao Ma08413892008-08-29 09:00:19 +08001876 if (di->i_xattr_loc) {
1877 ret = ocfs2_xattr_free_block(inode,
1878 le64_to_cpu(di->i_xattr_loc));
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001879 if (ret < 0) {
1880 mlog_errno(ret);
1881 goto out;
1882 }
1883 }
1884
1885 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1886 OCFS2_INODE_UPDATE_CREDITS);
1887 if (IS_ERR(handle)) {
1888 ret = PTR_ERR(handle);
1889 mlog_errno(ret);
1890 goto out;
1891 }
1892 ret = ocfs2_journal_access(handle, inode, di_bh,
1893 OCFS2_JOURNAL_ACCESS_WRITE);
1894 if (ret) {
1895 mlog_errno(ret);
1896 goto out_commit;
1897 }
1898
Tao Ma08413892008-08-29 09:00:19 +08001899 di->i_xattr_loc = 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001900
1901 spin_lock(&oi->ip_lock);
1902 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
1903 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1904 spin_unlock(&oi->ip_lock);
1905
1906 ret = ocfs2_journal_dirty(handle, di_bh);
1907 if (ret < 0)
1908 mlog_errno(ret);
1909out_commit:
1910 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1911out:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001912 return ret;
1913}
1914
1915static int ocfs2_xattr_has_space_inline(struct inode *inode,
1916 struct ocfs2_dinode *di)
1917{
1918 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1919 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
1920 int free;
1921
1922 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
1923 return 0;
1924
1925 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1926 struct ocfs2_inline_data *idata = &di->id2.i_data;
1927 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
1928 } else if (ocfs2_inode_is_fast_symlink(inode)) {
1929 free = ocfs2_fast_symlink_chars(inode->i_sb) -
1930 le64_to_cpu(di->i_size);
1931 } else {
1932 struct ocfs2_extent_list *el = &di->id2.i_list;
1933 free = (le16_to_cpu(el->l_count) -
1934 le16_to_cpu(el->l_next_free_rec)) *
1935 sizeof(struct ocfs2_extent_rec);
1936 }
1937 if (free >= xattrsize)
1938 return 1;
1939
1940 return 0;
1941}
1942
1943/*
1944 * ocfs2_xattr_ibody_find()
1945 *
1946 * Find extended attribute in inode block and
1947 * fill search info into struct ocfs2_xattr_search.
1948 */
1949static int ocfs2_xattr_ibody_find(struct inode *inode,
1950 int name_index,
1951 const char *name,
1952 struct ocfs2_xattr_search *xs)
1953{
1954 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1955 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1956 int ret;
1957 int has_space = 0;
1958
1959 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1960 return 0;
1961
1962 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1963 down_read(&oi->ip_alloc_sem);
1964 has_space = ocfs2_xattr_has_space_inline(inode, di);
1965 up_read(&oi->ip_alloc_sem);
1966 if (!has_space)
1967 return 0;
1968 }
1969
1970 xs->xattr_bh = xs->inode_bh;
1971 xs->end = (void *)di + inode->i_sb->s_blocksize;
1972 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
1973 xs->header = (struct ocfs2_xattr_header *)
1974 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1975 else
1976 xs->header = (struct ocfs2_xattr_header *)
1977 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
1978 xs->base = (void *)xs->header;
1979 xs->here = xs->header->xh_entries;
1980
1981 /* Find the named attribute. */
1982 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1983 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1984 if (ret && ret != -ENODATA)
1985 return ret;
1986 xs->not_found = ret;
1987 }
1988
1989 return 0;
1990}
1991
1992/*
1993 * ocfs2_xattr_ibody_set()
1994 *
1995 * Set, replace or remove an extended attribute into inode block.
1996 *
1997 */
1998static int ocfs2_xattr_ibody_set(struct inode *inode,
1999 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08002000 struct ocfs2_xattr_search *xs,
2001 struct ocfs2_xattr_set_ctxt *ctxt)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002002{
2003 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2004 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2005 int ret;
2006
2007 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2008 return -ENOSPC;
2009
2010 down_write(&oi->ip_alloc_sem);
2011 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2012 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2013 ret = -ENOSPC;
2014 goto out;
2015 }
2016 }
2017
Tao Ma78f30c32008-11-12 08:27:00 +08002018 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002019 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
2020out:
2021 up_write(&oi->ip_alloc_sem);
2022
2023 return ret;
2024}
2025
2026/*
2027 * ocfs2_xattr_block_find()
2028 *
2029 * Find extended attribute in external block and
2030 * fill search info into struct ocfs2_xattr_search.
2031 */
2032static int ocfs2_xattr_block_find(struct inode *inode,
2033 int name_index,
2034 const char *name,
2035 struct ocfs2_xattr_search *xs)
2036{
2037 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2038 struct buffer_head *blk_bh = NULL;
Tao Ma589dc262008-08-18 17:38:51 +08002039 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002040 int ret = 0;
2041
2042 if (!di->i_xattr_loc)
2043 return ret;
2044
Joel Becker4ae1d692008-11-13 14:49:18 -08002045 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
2046 &blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002047 if (ret < 0) {
2048 mlog_errno(ret);
2049 return ret;
2050 }
Joel Beckerf6087fb2008-10-20 18:20:43 -07002051
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002052 xs->xattr_bh = blk_bh;
Joel Becker4ae1d692008-11-13 14:49:18 -08002053 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002054
Tao Ma589dc262008-08-18 17:38:51 +08002055 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2056 xs->header = &xb->xb_attrs.xb_header;
2057 xs->base = (void *)xs->header;
2058 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
2059 xs->here = xs->header->xh_entries;
2060
2061 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2062 } else
2063 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
2064 name_index,
2065 name, xs);
2066
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002067 if (ret && ret != -ENODATA) {
2068 xs->xattr_bh = NULL;
2069 goto cleanup;
2070 }
2071 xs->not_found = ret;
2072 return 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002073cleanup:
2074 brelse(blk_bh);
2075
2076 return ret;
2077}
2078
2079/*
2080 * ocfs2_xattr_block_set()
2081 *
2082 * Set, replace or remove an extended attribute into external block.
2083 *
2084 */
2085static int ocfs2_xattr_block_set(struct inode *inode,
2086 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08002087 struct ocfs2_xattr_search *xs,
2088 struct ocfs2_xattr_set_ctxt *ctxt)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002089{
2090 struct buffer_head *new_bh = NULL;
2091 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2092 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
Tao Ma85db90e2008-11-12 08:27:01 +08002093 handle_t *handle = ctxt->handle;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002094 struct ocfs2_xattr_block *xblk = NULL;
2095 u16 suballoc_bit_start;
2096 u32 num_got;
2097 u64 first_blkno;
2098 int ret;
2099
2100 if (!xs->xattr_bh) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002101 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
2102 OCFS2_JOURNAL_ACCESS_CREATE);
2103 if (ret < 0) {
2104 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08002105 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002106 }
2107
Tao Ma78f30c32008-11-12 08:27:00 +08002108 ret = ocfs2_claim_metadata(osb, handle, ctxt->meta_ac, 1,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002109 &suballoc_bit_start, &num_got,
2110 &first_blkno);
2111 if (ret < 0) {
2112 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08002113 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002114 }
2115
2116 new_bh = sb_getblk(inode->i_sb, first_blkno);
2117 ocfs2_set_new_buffer_uptodate(inode, new_bh);
2118
2119 ret = ocfs2_journal_access(handle, inode, new_bh,
2120 OCFS2_JOURNAL_ACCESS_CREATE);
2121 if (ret < 0) {
2122 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08002123 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002124 }
2125
2126 /* Initialize ocfs2_xattr_block */
2127 xs->xattr_bh = new_bh;
2128 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2129 memset(xblk, 0, inode->i_sb->s_blocksize);
2130 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
2131 xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
2132 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
2133 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
2134 xblk->xb_blkno = cpu_to_le64(first_blkno);
2135
2136 xs->header = &xblk->xb_attrs.xb_header;
2137 xs->base = (void *)xs->header;
2138 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2139 xs->here = xs->header->xh_entries;
2140
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002141 ret = ocfs2_journal_dirty(handle, new_bh);
2142 if (ret < 0) {
2143 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08002144 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002145 }
2146 di->i_xattr_loc = cpu_to_le64(first_blkno);
Tao Ma85db90e2008-11-12 08:27:01 +08002147 ocfs2_journal_dirty(handle, xs->inode_bh);
Tao Ma01225592008-08-18 17:38:53 +08002148 } else
2149 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2150
2151 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
2152 /* Set extended attribute into external block */
Tao Ma78f30c32008-11-12 08:27:00 +08002153 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
2154 OCFS2_HAS_XATTR_FL);
Tao Ma01225592008-08-18 17:38:53 +08002155 if (!ret || ret != -ENOSPC)
2156 goto end;
2157
Tao Ma78f30c32008-11-12 08:27:00 +08002158 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08002159 if (ret)
2160 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002161 }
2162
Tao Ma78f30c32008-11-12 08:27:00 +08002163 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08002164
2165end:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002166
2167 return ret;
2168}
2169
Tao Ma78f30c32008-11-12 08:27:00 +08002170/* Check whether the new xattr can be inserted into the inode. */
2171static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
2172 struct ocfs2_xattr_info *xi,
2173 struct ocfs2_xattr_search *xs)
2174{
2175 u64 value_size;
2176 struct ocfs2_xattr_entry *last;
2177 int free, i;
2178 size_t min_offs = xs->end - xs->base;
2179
2180 if (!xs->header)
2181 return 0;
2182
2183 last = xs->header->xh_entries;
2184
2185 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
2186 size_t offs = le16_to_cpu(last->xe_name_offset);
2187 if (offs < min_offs)
2188 min_offs = offs;
2189 last += 1;
2190 }
2191
2192 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
2193 if (free < 0)
2194 return 0;
2195
2196 BUG_ON(!xs->not_found);
2197
2198 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
2199 value_size = OCFS2_XATTR_ROOT_SIZE;
2200 else
2201 value_size = OCFS2_XATTR_SIZE(xi->value_len);
2202
2203 if (free >= sizeof(struct ocfs2_xattr_entry) +
2204 OCFS2_XATTR_SIZE(strlen(xi->name)) + value_size)
2205 return 1;
2206
2207 return 0;
2208}
2209
2210static int ocfs2_calc_xattr_set_need(struct inode *inode,
2211 struct ocfs2_dinode *di,
2212 struct ocfs2_xattr_info *xi,
2213 struct ocfs2_xattr_search *xis,
2214 struct ocfs2_xattr_search *xbs,
2215 int *clusters_need,
Tao Ma85db90e2008-11-12 08:27:01 +08002216 int *meta_need,
2217 int *credits_need)
Tao Ma78f30c32008-11-12 08:27:00 +08002218{
2219 int ret = 0, old_in_xb = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08002220 int clusters_add = 0, meta_add = 0, credits = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08002221 struct buffer_head *bh = NULL;
2222 struct ocfs2_xattr_block *xb = NULL;
2223 struct ocfs2_xattr_entry *xe = NULL;
2224 struct ocfs2_xattr_value_root *xv = NULL;
2225 char *base = NULL;
2226 int name_offset, name_len = 0;
2227 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2228 xi->value_len);
2229 u64 value_size;
2230
Tao Ma78f30c32008-11-12 08:27:00 +08002231 if (xis->not_found && xbs->not_found) {
Tao Ma85db90e2008-11-12 08:27:01 +08002232 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2233
2234 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
Tao Ma78f30c32008-11-12 08:27:00 +08002235 clusters_add += new_clusters;
Tao Ma85db90e2008-11-12 08:27:01 +08002236 credits += ocfs2_calc_extend_credits(inode->i_sb,
2237 &def_xv.xv.xr_list,
2238 new_clusters);
2239 }
Tao Ma78f30c32008-11-12 08:27:00 +08002240
2241 goto meta_guess;
2242 }
2243
2244 if (!xis->not_found) {
2245 xe = xis->here;
2246 name_offset = le16_to_cpu(xe->xe_name_offset);
2247 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2248 base = xis->base;
Tao Ma85db90e2008-11-12 08:27:01 +08002249 credits += OCFS2_INODE_UPDATE_CREDITS;
Tao Ma78f30c32008-11-12 08:27:00 +08002250 } else {
Joel Becker970e4932008-11-13 14:49:19 -08002251 int i, block_off = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08002252 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2253 xe = xbs->here;
2254 name_offset = le16_to_cpu(xe->xe_name_offset);
2255 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2256 i = xbs->here - xbs->header->xh_entries;
2257 old_in_xb = 1;
2258
2259 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2260 ret = ocfs2_xattr_bucket_get_name_value(inode,
2261 bucket_xh(xbs->bucket),
2262 i, &block_off,
2263 &name_offset);
2264 base = bucket_block(xbs->bucket, block_off);
Tao Ma85db90e2008-11-12 08:27:01 +08002265 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2266 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08002267 base = xbs->base;
Tao Ma85db90e2008-11-12 08:27:01 +08002268 credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
2269 }
2270 }
2271
2272 /*
2273 * delete a xattr doesn't need metadata and cluster allocation.
2274 * so just calculate the credits and return.
2275 *
2276 * The credits for removing the value tree will be extended
2277 * by ocfs2_remove_extent itself.
2278 */
2279 if (!xi->value) {
2280 if (!ocfs2_xattr_is_local(xe))
Jan Karaa90714c2008-10-09 19:38:40 +02002281 credits += ocfs2_remove_extent_credits(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08002282
2283 goto out;
Tao Ma78f30c32008-11-12 08:27:00 +08002284 }
2285
2286 /* do cluster allocation guess first. */
2287 value_size = le64_to_cpu(xe->xe_value_size);
2288
2289 if (old_in_xb) {
2290 /*
2291 * In xattr set, we always try to set the xe in inode first,
2292 * so if it can be inserted into inode successfully, the old
2293 * one will be removed from the xattr block, and this xattr
2294 * will be inserted into inode as a new xattr in inode.
2295 */
2296 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
2297 clusters_add += new_clusters;
Jan Karaa90714c2008-10-09 19:38:40 +02002298 credits += ocfs2_remove_extent_credits(inode->i_sb) +
Tao Ma85db90e2008-11-12 08:27:01 +08002299 OCFS2_INODE_UPDATE_CREDITS;
2300 if (!ocfs2_xattr_is_local(xe))
2301 credits += ocfs2_calc_extend_credits(
2302 inode->i_sb,
2303 &def_xv.xv.xr_list,
2304 new_clusters);
Tao Ma78f30c32008-11-12 08:27:00 +08002305 goto out;
2306 }
2307 }
2308
2309 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
2310 /* the new values will be stored outside. */
2311 u32 old_clusters = 0;
2312
2313 if (!ocfs2_xattr_is_local(xe)) {
2314 old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2315 value_size);
2316 xv = (struct ocfs2_xattr_value_root *)
2317 (base + name_offset + name_len);
Tao Ma97aff522008-11-19 16:48:41 +08002318 value_size = OCFS2_XATTR_ROOT_SIZE;
Tao Ma78f30c32008-11-12 08:27:00 +08002319 } else
2320 xv = &def_xv.xv;
2321
Tao Ma85db90e2008-11-12 08:27:01 +08002322 if (old_clusters >= new_clusters) {
Jan Karaa90714c2008-10-09 19:38:40 +02002323 credits += ocfs2_remove_extent_credits(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08002324 goto out;
Tao Ma85db90e2008-11-12 08:27:01 +08002325 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08002326 meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
2327 clusters_add += new_clusters - old_clusters;
Tao Ma85db90e2008-11-12 08:27:01 +08002328 credits += ocfs2_calc_extend_credits(inode->i_sb,
2329 &xv->xr_list,
2330 new_clusters -
2331 old_clusters);
Tao Ma97aff522008-11-19 16:48:41 +08002332 if (value_size >= OCFS2_XATTR_ROOT_SIZE)
2333 goto out;
Tao Ma78f30c32008-11-12 08:27:00 +08002334 }
2335 } else {
2336 /*
2337 * Now the new value will be stored inside. So if the new
2338 * value is smaller than the size of value root or the old
2339 * value, we don't need any allocation, otherwise we have
2340 * to guess metadata allocation.
2341 */
2342 if ((ocfs2_xattr_is_local(xe) && value_size >= xi->value_len) ||
2343 (!ocfs2_xattr_is_local(xe) &&
2344 OCFS2_XATTR_ROOT_SIZE >= xi->value_len))
2345 goto out;
2346 }
2347
2348meta_guess:
2349 /* calculate metadata allocation. */
2350 if (di->i_xattr_loc) {
2351 if (!xbs->xattr_bh) {
Joel Becker4ae1d692008-11-13 14:49:18 -08002352 ret = ocfs2_read_xattr_block(inode,
2353 le64_to_cpu(di->i_xattr_loc),
2354 &bh);
Tao Ma78f30c32008-11-12 08:27:00 +08002355 if (ret) {
2356 mlog_errno(ret);
2357 goto out;
2358 }
2359
2360 xb = (struct ocfs2_xattr_block *)bh->b_data;
2361 } else
2362 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2363
2364 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2365 struct ocfs2_extent_list *el =
2366 &xb->xb_attrs.xb_root.xt_list;
2367 meta_add += ocfs2_extend_meta_needed(el);
Tao Ma85db90e2008-11-12 08:27:01 +08002368 credits += ocfs2_calc_extend_credits(inode->i_sb,
2369 el, 1);
Tao Ma78f30c32008-11-12 08:27:00 +08002370 }
2371
2372 /*
2373 * This cluster will be used either for new bucket or for
2374 * new xattr block.
2375 * If the cluster size is the same as the bucket size, one
2376 * more is needed since we may need to extend the bucket
2377 * also.
2378 */
2379 clusters_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08002380 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08002381 if (OCFS2_XATTR_BUCKET_SIZE ==
Tao Ma85db90e2008-11-12 08:27:01 +08002382 OCFS2_SB(inode->i_sb)->s_clustersize) {
2383 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08002384 clusters_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08002385 }
2386 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08002387 meta_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08002388 credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
2389 }
Tao Ma78f30c32008-11-12 08:27:00 +08002390out:
2391 if (clusters_need)
2392 *clusters_need = clusters_add;
2393 if (meta_need)
2394 *meta_need = meta_add;
Tao Ma85db90e2008-11-12 08:27:01 +08002395 if (credits_need)
2396 *credits_need = credits;
Tao Ma78f30c32008-11-12 08:27:00 +08002397 brelse(bh);
2398 return ret;
2399}
2400
2401static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
2402 struct ocfs2_dinode *di,
2403 struct ocfs2_xattr_info *xi,
2404 struct ocfs2_xattr_search *xis,
2405 struct ocfs2_xattr_search *xbs,
Tao Ma85db90e2008-11-12 08:27:01 +08002406 struct ocfs2_xattr_set_ctxt *ctxt,
2407 int *credits)
Tao Ma78f30c32008-11-12 08:27:00 +08002408{
2409 int clusters_add, meta_add, ret;
2410 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2411
2412 memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
2413
2414 ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
2415
2416 ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
Tao Ma85db90e2008-11-12 08:27:01 +08002417 &clusters_add, &meta_add, credits);
Tao Ma78f30c32008-11-12 08:27:00 +08002418 if (ret) {
2419 mlog_errno(ret);
2420 return ret;
2421 }
2422
Tao Ma85db90e2008-11-12 08:27:01 +08002423 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
2424 "credits = %d\n", xi->name, meta_add, clusters_add, *credits);
Tao Ma78f30c32008-11-12 08:27:00 +08002425
2426 if (meta_add) {
2427 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
2428 &ctxt->meta_ac);
2429 if (ret) {
2430 mlog_errno(ret);
2431 goto out;
2432 }
2433 }
2434
2435 if (clusters_add) {
2436 ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
2437 if (ret)
2438 mlog_errno(ret);
2439 }
2440out:
2441 if (ret) {
2442 if (ctxt->meta_ac) {
2443 ocfs2_free_alloc_context(ctxt->meta_ac);
2444 ctxt->meta_ac = NULL;
2445 }
2446
2447 /*
2448 * We cannot have an error and a non null ctxt->data_ac.
2449 */
2450 }
2451
2452 return ret;
2453}
2454
Tao Ma85db90e2008-11-12 08:27:01 +08002455static int __ocfs2_xattr_set_handle(struct inode *inode,
2456 struct ocfs2_dinode *di,
2457 struct ocfs2_xattr_info *xi,
2458 struct ocfs2_xattr_search *xis,
2459 struct ocfs2_xattr_search *xbs,
2460 struct ocfs2_xattr_set_ctxt *ctxt)
2461{
Tao Ma9f868f12008-11-19 16:48:42 +08002462 int ret = 0, credits, old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08002463
2464 if (!xi->value) {
2465 /* Remove existing extended attribute */
2466 if (!xis->not_found)
2467 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2468 else if (!xbs->not_found)
2469 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2470 } else {
2471 /* We always try to set extended attribute into inode first*/
2472 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2473 if (!ret && !xbs->not_found) {
2474 /*
2475 * If succeed and that extended attribute existing in
2476 * external block, then we will remove it.
2477 */
2478 xi->value = NULL;
2479 xi->value_len = 0;
2480
Tao Ma9f868f12008-11-19 16:48:42 +08002481 old_found = xis->not_found;
Tao Ma85db90e2008-11-12 08:27:01 +08002482 xis->not_found = -ENODATA;
2483 ret = ocfs2_calc_xattr_set_need(inode,
2484 di,
2485 xi,
2486 xis,
2487 xbs,
2488 NULL,
2489 NULL,
2490 &credits);
Tao Ma9f868f12008-11-19 16:48:42 +08002491 xis->not_found = old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08002492 if (ret) {
2493 mlog_errno(ret);
2494 goto out;
2495 }
2496
2497 ret = ocfs2_extend_trans(ctxt->handle, credits +
2498 ctxt->handle->h_buffer_credits);
2499 if (ret) {
2500 mlog_errno(ret);
2501 goto out;
2502 }
2503 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2504 } else if (ret == -ENOSPC) {
2505 if (di->i_xattr_loc && !xbs->xattr_bh) {
2506 ret = ocfs2_xattr_block_find(inode,
2507 xi->name_index,
2508 xi->name, xbs);
2509 if (ret)
2510 goto out;
2511
Tao Ma9f868f12008-11-19 16:48:42 +08002512 old_found = xis->not_found;
Tao Ma85db90e2008-11-12 08:27:01 +08002513 xis->not_found = -ENODATA;
2514 ret = ocfs2_calc_xattr_set_need(inode,
2515 di,
2516 xi,
2517 xis,
2518 xbs,
2519 NULL,
2520 NULL,
2521 &credits);
Tao Ma9f868f12008-11-19 16:48:42 +08002522 xis->not_found = old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08002523 if (ret) {
2524 mlog_errno(ret);
2525 goto out;
2526 }
2527
2528 ret = ocfs2_extend_trans(ctxt->handle, credits +
2529 ctxt->handle->h_buffer_credits);
2530 if (ret) {
2531 mlog_errno(ret);
2532 goto out;
2533 }
2534 }
2535 /*
2536 * If no space in inode, we will set extended attribute
2537 * into external block.
2538 */
2539 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2540 if (ret)
2541 goto out;
2542 if (!xis->not_found) {
2543 /*
2544 * If succeed and that extended attribute
2545 * existing in inode, we will remove it.
2546 */
2547 xi->value = NULL;
2548 xi->value_len = 0;
2549 xbs->not_found = -ENODATA;
2550 ret = ocfs2_calc_xattr_set_need(inode,
2551 di,
2552 xi,
2553 xis,
2554 xbs,
2555 NULL,
2556 NULL,
2557 &credits);
2558 if (ret) {
2559 mlog_errno(ret);
2560 goto out;
2561 }
2562
2563 ret = ocfs2_extend_trans(ctxt->handle, credits +
2564 ctxt->handle->h_buffer_credits);
2565 if (ret) {
2566 mlog_errno(ret);
2567 goto out;
2568 }
2569 ret = ocfs2_xattr_ibody_set(inode, xi,
2570 xis, ctxt);
2571 }
2572 }
2573 }
2574
2575out:
2576 return ret;
2577}
2578
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002579/*
Tiger Yang6c3faba2008-11-14 11:16:03 +08002580 * This function only called duing creating inode
2581 * for init security/acl xattrs of the new inode.
2582 * The xattrs could be put into ibody or extent block,
2583 * xattr bucket would not be use in this case.
2584 * transanction credits also be reserved in here.
2585 */
2586int ocfs2_xattr_set_handle(handle_t *handle,
2587 struct inode *inode,
2588 struct buffer_head *di_bh,
2589 int name_index,
2590 const char *name,
2591 const void *value,
2592 size_t value_len,
2593 int flags,
2594 struct ocfs2_alloc_context *meta_ac,
2595 struct ocfs2_alloc_context *data_ac)
2596{
2597 struct ocfs2_dinode *di;
2598 int ret;
2599
2600 struct ocfs2_xattr_info xi = {
2601 .name_index = name_index,
2602 .name = name,
2603 .value = value,
2604 .value_len = value_len,
2605 };
2606
2607 struct ocfs2_xattr_search xis = {
2608 .not_found = -ENODATA,
2609 };
2610
2611 struct ocfs2_xattr_search xbs = {
2612 .not_found = -ENODATA,
2613 };
2614
2615 struct ocfs2_xattr_set_ctxt ctxt = {
2616 .handle = handle,
2617 .meta_ac = meta_ac,
2618 .data_ac = data_ac,
2619 };
2620
2621 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2622 return -EOPNOTSUPP;
2623
2624 xis.inode_bh = xbs.inode_bh = di_bh;
2625 di = (struct ocfs2_dinode *)di_bh->b_data;
2626
2627 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2628
2629 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2630 if (ret)
2631 goto cleanup;
2632 if (xis.not_found) {
2633 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2634 if (ret)
2635 goto cleanup;
2636 }
2637
2638 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
2639
2640cleanup:
2641 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2642 brelse(xbs.xattr_bh);
2643
2644 return ret;
2645}
2646
2647/*
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002648 * ocfs2_xattr_set()
2649 *
2650 * Set, replace or remove an extended attribute for this inode.
2651 * value is NULL to remove an existing extended attribute, else either
2652 * create or replace an extended attribute.
2653 */
2654int ocfs2_xattr_set(struct inode *inode,
2655 int name_index,
2656 const char *name,
2657 const void *value,
2658 size_t value_len,
2659 int flags)
2660{
2661 struct buffer_head *di_bh = NULL;
2662 struct ocfs2_dinode *di;
Tao Ma85db90e2008-11-12 08:27:01 +08002663 int ret, credits;
Tao Ma78f30c32008-11-12 08:27:00 +08002664 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08002665 struct inode *tl_inode = osb->osb_tl_inode;
Tao Ma78f30c32008-11-12 08:27:00 +08002666 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002667
2668 struct ocfs2_xattr_info xi = {
2669 .name_index = name_index,
2670 .name = name,
2671 .value = value,
2672 .value_len = value_len,
2673 };
2674
2675 struct ocfs2_xattr_search xis = {
2676 .not_found = -ENODATA,
2677 };
2678
2679 struct ocfs2_xattr_search xbs = {
2680 .not_found = -ENODATA,
2681 };
2682
Tiger Yang8154da32008-08-18 17:11:46 +08002683 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2684 return -EOPNOTSUPP;
2685
Joel Beckerba937122008-10-24 19:13:20 -07002686 /*
2687 * Only xbs will be used on indexed trees. xis doesn't need a
2688 * bucket.
2689 */
2690 xbs.bucket = ocfs2_xattr_bucket_new(inode);
2691 if (!xbs.bucket) {
2692 mlog_errno(-ENOMEM);
2693 return -ENOMEM;
2694 }
2695
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002696 ret = ocfs2_inode_lock(inode, &di_bh, 1);
2697 if (ret < 0) {
2698 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07002699 goto cleanup_nolock;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002700 }
2701 xis.inode_bh = xbs.inode_bh = di_bh;
2702 di = (struct ocfs2_dinode *)di_bh->b_data;
2703
2704 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2705 /*
2706 * Scan inode and external block to find the same name
2707 * extended attribute and collect search infomation.
2708 */
2709 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2710 if (ret)
2711 goto cleanup;
2712 if (xis.not_found) {
2713 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2714 if (ret)
2715 goto cleanup;
2716 }
2717
2718 if (xis.not_found && xbs.not_found) {
2719 ret = -ENODATA;
2720 if (flags & XATTR_REPLACE)
2721 goto cleanup;
2722 ret = 0;
2723 if (!value)
2724 goto cleanup;
2725 } else {
2726 ret = -EEXIST;
2727 if (flags & XATTR_CREATE)
2728 goto cleanup;
2729 }
2730
Tao Ma85db90e2008-11-12 08:27:01 +08002731
2732 mutex_lock(&tl_inode->i_mutex);
2733
2734 if (ocfs2_truncate_log_needs_flush(osb)) {
2735 ret = __ocfs2_flush_truncate_log(osb);
2736 if (ret < 0) {
2737 mutex_unlock(&tl_inode->i_mutex);
2738 mlog_errno(ret);
2739 goto cleanup;
2740 }
2741 }
2742 mutex_unlock(&tl_inode->i_mutex);
2743
2744 ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
2745 &xbs, &ctxt, &credits);
Tao Ma78f30c32008-11-12 08:27:00 +08002746 if (ret) {
2747 mlog_errno(ret);
2748 goto cleanup;
2749 }
2750
Tao Ma85db90e2008-11-12 08:27:01 +08002751 ctxt.handle = ocfs2_start_trans(osb, credits);
2752 if (IS_ERR(ctxt.handle)) {
2753 ret = PTR_ERR(ctxt.handle);
2754 mlog_errno(ret);
2755 goto cleanup;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002756 }
Tao Ma85db90e2008-11-12 08:27:01 +08002757
2758 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
2759
2760 ocfs2_commit_trans(osb, ctxt.handle);
2761
Tao Ma78f30c32008-11-12 08:27:00 +08002762 if (ctxt.data_ac)
2763 ocfs2_free_alloc_context(ctxt.data_ac);
2764 if (ctxt.meta_ac)
2765 ocfs2_free_alloc_context(ctxt.meta_ac);
2766 if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
2767 ocfs2_schedule_truncate_log_flush(osb, 1);
2768 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002769cleanup:
2770 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2771 ocfs2_inode_unlock(inode, 1);
Joel Beckerba937122008-10-24 19:13:20 -07002772cleanup_nolock:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002773 brelse(di_bh);
2774 brelse(xbs.xattr_bh);
Joel Beckerba937122008-10-24 19:13:20 -07002775 ocfs2_xattr_bucket_free(xbs.bucket);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002776
2777 return ret;
2778}
2779
Tao Ma0c044f02008-08-18 17:38:50 +08002780/*
2781 * Find the xattr extent rec which may contains name_hash.
2782 * e_cpos will be the first name hash of the xattr rec.
2783 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
2784 */
2785static int ocfs2_xattr_get_rec(struct inode *inode,
2786 u32 name_hash,
2787 u64 *p_blkno,
2788 u32 *e_cpos,
2789 u32 *num_clusters,
2790 struct ocfs2_extent_list *el)
2791{
2792 int ret = 0, i;
2793 struct buffer_head *eb_bh = NULL;
2794 struct ocfs2_extent_block *eb;
2795 struct ocfs2_extent_rec *rec = NULL;
2796 u64 e_blkno = 0;
2797
2798 if (el->l_tree_depth) {
2799 ret = ocfs2_find_leaf(inode, el, name_hash, &eb_bh);
2800 if (ret) {
2801 mlog_errno(ret);
2802 goto out;
2803 }
2804
2805 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
2806 el = &eb->h_list;
2807
2808 if (el->l_tree_depth) {
2809 ocfs2_error(inode->i_sb,
2810 "Inode %lu has non zero tree depth in "
2811 "xattr tree block %llu\n", inode->i_ino,
2812 (unsigned long long)eb_bh->b_blocknr);
2813 ret = -EROFS;
2814 goto out;
2815 }
2816 }
2817
2818 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
2819 rec = &el->l_recs[i];
2820
2821 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
2822 e_blkno = le64_to_cpu(rec->e_blkno);
2823 break;
2824 }
2825 }
2826
2827 if (!e_blkno) {
2828 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
2829 "record (%u, %u, 0) in xattr", inode->i_ino,
2830 le32_to_cpu(rec->e_cpos),
2831 ocfs2_rec_clusters(el, rec));
2832 ret = -EROFS;
2833 goto out;
2834 }
2835
2836 *p_blkno = le64_to_cpu(rec->e_blkno);
2837 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
2838 if (e_cpos)
2839 *e_cpos = le32_to_cpu(rec->e_cpos);
2840out:
2841 brelse(eb_bh);
2842 return ret;
2843}
2844
2845typedef int (xattr_bucket_func)(struct inode *inode,
2846 struct ocfs2_xattr_bucket *bucket,
2847 void *para);
2848
Tao Ma589dc262008-08-18 17:38:51 +08002849static int ocfs2_find_xe_in_bucket(struct inode *inode,
Joel Beckere2356a32008-10-27 15:01:54 -07002850 struct ocfs2_xattr_bucket *bucket,
Tao Ma589dc262008-08-18 17:38:51 +08002851 int name_index,
2852 const char *name,
2853 u32 name_hash,
2854 u16 *xe_index,
2855 int *found)
2856{
2857 int i, ret = 0, cmp = 1, block_off, new_offset;
Joel Beckere2356a32008-10-27 15:01:54 -07002858 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma589dc262008-08-18 17:38:51 +08002859 size_t name_len = strlen(name);
2860 struct ocfs2_xattr_entry *xe = NULL;
Tao Ma589dc262008-08-18 17:38:51 +08002861 char *xe_name;
2862
2863 /*
2864 * We don't use binary search in the bucket because there
2865 * may be multiple entries with the same name hash.
2866 */
2867 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
2868 xe = &xh->xh_entries[i];
2869
2870 if (name_hash > le32_to_cpu(xe->xe_name_hash))
2871 continue;
2872 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
2873 break;
2874
2875 cmp = name_index - ocfs2_xattr_get_type(xe);
2876 if (!cmp)
2877 cmp = name_len - xe->xe_name_len;
2878 if (cmp)
2879 continue;
2880
2881 ret = ocfs2_xattr_bucket_get_name_value(inode,
2882 xh,
2883 i,
2884 &block_off,
2885 &new_offset);
2886 if (ret) {
2887 mlog_errno(ret);
2888 break;
2889 }
2890
Joel Becker970e4932008-11-13 14:49:19 -08002891
Joel Beckere2356a32008-10-27 15:01:54 -07002892 xe_name = bucket_block(bucket, block_off) + new_offset;
2893 if (!memcmp(name, xe_name, name_len)) {
Tao Ma589dc262008-08-18 17:38:51 +08002894 *xe_index = i;
2895 *found = 1;
2896 ret = 0;
2897 break;
2898 }
2899 }
2900
2901 return ret;
2902}
2903
2904/*
2905 * Find the specified xattr entry in a series of buckets.
2906 * This series start from p_blkno and last for num_clusters.
2907 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
2908 * the num of the valid buckets.
2909 *
2910 * Return the buffer_head this xattr should reside in. And if the xattr's
2911 * hash is in the gap of 2 buckets, return the lower bucket.
2912 */
2913static int ocfs2_xattr_bucket_find(struct inode *inode,
2914 int name_index,
2915 const char *name,
2916 u32 name_hash,
2917 u64 p_blkno,
2918 u32 first_hash,
2919 u32 num_clusters,
2920 struct ocfs2_xattr_search *xs)
2921{
2922 int ret, found = 0;
Tao Ma589dc262008-08-18 17:38:51 +08002923 struct ocfs2_xattr_header *xh = NULL;
2924 struct ocfs2_xattr_entry *xe = NULL;
2925 u16 index = 0;
2926 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2927 int low_bucket = 0, bucket, high_bucket;
Joel Beckere2356a32008-10-27 15:01:54 -07002928 struct ocfs2_xattr_bucket *search;
Tao Ma589dc262008-08-18 17:38:51 +08002929 u32 last_hash;
Joel Beckere2356a32008-10-27 15:01:54 -07002930 u64 blkno, lower_blkno = 0;
Tao Ma589dc262008-08-18 17:38:51 +08002931
Joel Beckere2356a32008-10-27 15:01:54 -07002932 search = ocfs2_xattr_bucket_new(inode);
2933 if (!search) {
2934 ret = -ENOMEM;
2935 mlog_errno(ret);
2936 goto out;
2937 }
2938
2939 ret = ocfs2_read_xattr_bucket(search, p_blkno);
Tao Ma589dc262008-08-18 17:38:51 +08002940 if (ret) {
2941 mlog_errno(ret);
2942 goto out;
2943 }
2944
Joel Beckere2356a32008-10-27 15:01:54 -07002945 xh = bucket_xh(search);
Tao Ma589dc262008-08-18 17:38:51 +08002946 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
Tao Ma589dc262008-08-18 17:38:51 +08002947 while (low_bucket <= high_bucket) {
Joel Beckere2356a32008-10-27 15:01:54 -07002948 ocfs2_xattr_bucket_relse(search);
2949
Tao Ma589dc262008-08-18 17:38:51 +08002950 bucket = (low_bucket + high_bucket) / 2;
Tao Ma589dc262008-08-18 17:38:51 +08002951 blkno = p_blkno + bucket * blk_per_bucket;
Joel Beckere2356a32008-10-27 15:01:54 -07002952 ret = ocfs2_read_xattr_bucket(search, blkno);
Tao Ma589dc262008-08-18 17:38:51 +08002953 if (ret) {
2954 mlog_errno(ret);
2955 goto out;
2956 }
2957
Joel Beckere2356a32008-10-27 15:01:54 -07002958 xh = bucket_xh(search);
Tao Ma589dc262008-08-18 17:38:51 +08002959 xe = &xh->xh_entries[0];
2960 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
2961 high_bucket = bucket - 1;
2962 continue;
2963 }
2964
2965 /*
2966 * Check whether the hash of the last entry in our
Tao Ma5a0956112008-09-19 22:17:41 +08002967 * bucket is larger than the search one. for an empty
2968 * bucket, the last one is also the first one.
Tao Ma589dc262008-08-18 17:38:51 +08002969 */
Tao Ma5a0956112008-09-19 22:17:41 +08002970 if (xh->xh_count)
2971 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
2972
Tao Ma589dc262008-08-18 17:38:51 +08002973 last_hash = le32_to_cpu(xe->xe_name_hash);
2974
Joel Beckere2356a32008-10-27 15:01:54 -07002975 /* record lower_blkno which may be the insert place. */
2976 lower_blkno = blkno;
Tao Ma589dc262008-08-18 17:38:51 +08002977
2978 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
2979 low_bucket = bucket + 1;
2980 continue;
2981 }
2982
2983 /* the searched xattr should reside in this bucket if exists. */
Joel Beckere2356a32008-10-27 15:01:54 -07002984 ret = ocfs2_find_xe_in_bucket(inode, search,
Tao Ma589dc262008-08-18 17:38:51 +08002985 name_index, name, name_hash,
2986 &index, &found);
2987 if (ret) {
2988 mlog_errno(ret);
2989 goto out;
2990 }
2991 break;
2992 }
2993
2994 /*
2995 * Record the bucket we have found.
2996 * When the xattr's hash value is in the gap of 2 buckets, we will
2997 * always set it to the previous bucket.
2998 */
Joel Beckere2356a32008-10-27 15:01:54 -07002999 if (!lower_blkno)
3000 lower_blkno = p_blkno;
3001
3002 /* This should be in cache - we just read it during the search */
3003 ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
3004 if (ret) {
3005 mlog_errno(ret);
3006 goto out;
Tao Ma589dc262008-08-18 17:38:51 +08003007 }
Tao Ma589dc262008-08-18 17:38:51 +08003008
Joel Beckerba937122008-10-24 19:13:20 -07003009 xs->header = bucket_xh(xs->bucket);
3010 xs->base = bucket_block(xs->bucket, 0);
Tao Ma589dc262008-08-18 17:38:51 +08003011 xs->end = xs->base + inode->i_sb->s_blocksize;
3012
3013 if (found) {
Tao Ma589dc262008-08-18 17:38:51 +08003014 xs->here = &xs->header->xh_entries[index];
3015 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
Joel Beckerba937122008-10-24 19:13:20 -07003016 (unsigned long long)bucket_blkno(xs->bucket), index);
Tao Ma589dc262008-08-18 17:38:51 +08003017 } else
3018 ret = -ENODATA;
3019
3020out:
Joel Beckere2356a32008-10-27 15:01:54 -07003021 ocfs2_xattr_bucket_free(search);
Tao Ma589dc262008-08-18 17:38:51 +08003022 return ret;
3023}
3024
3025static int ocfs2_xattr_index_block_find(struct inode *inode,
3026 struct buffer_head *root_bh,
3027 int name_index,
3028 const char *name,
3029 struct ocfs2_xattr_search *xs)
3030{
3031 int ret;
3032 struct ocfs2_xattr_block *xb =
3033 (struct ocfs2_xattr_block *)root_bh->b_data;
3034 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3035 struct ocfs2_extent_list *el = &xb_root->xt_list;
3036 u64 p_blkno = 0;
3037 u32 first_hash, num_clusters = 0;
Tao Ma2057e5c2008-10-09 23:06:13 +08003038 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
Tao Ma589dc262008-08-18 17:38:51 +08003039
3040 if (le16_to_cpu(el->l_next_free_rec) == 0)
3041 return -ENODATA;
3042
3043 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
3044 name, name_hash, name_index);
3045
3046 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
3047 &num_clusters, el);
3048 if (ret) {
3049 mlog_errno(ret);
3050 goto out;
3051 }
3052
3053 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
3054
3055 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
Mark Fashehde29c082008-10-29 14:45:30 -07003056 "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
3057 first_hash);
Tao Ma589dc262008-08-18 17:38:51 +08003058
3059 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
3060 p_blkno, first_hash, num_clusters, xs);
3061
3062out:
3063 return ret;
3064}
3065
Tao Ma0c044f02008-08-18 17:38:50 +08003066static int ocfs2_iterate_xattr_buckets(struct inode *inode,
3067 u64 blkno,
3068 u32 clusters,
3069 xattr_bucket_func *func,
3070 void *para)
3071{
Joel Becker6dde41d2008-10-24 17:16:48 -07003072 int i, ret = 0;
Tao Ma0c044f02008-08-18 17:38:50 +08003073 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
3074 u32 num_buckets = clusters * bpc;
Joel Beckerba937122008-10-24 19:13:20 -07003075 struct ocfs2_xattr_bucket *bucket;
Tao Ma0c044f02008-08-18 17:38:50 +08003076
Joel Beckerba937122008-10-24 19:13:20 -07003077 bucket = ocfs2_xattr_bucket_new(inode);
3078 if (!bucket) {
3079 mlog_errno(-ENOMEM);
3080 return -ENOMEM;
3081 }
Tao Ma0c044f02008-08-18 17:38:50 +08003082
3083 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003084 clusters, (unsigned long long)blkno);
Tao Ma0c044f02008-08-18 17:38:50 +08003085
Joel Beckerba937122008-10-24 19:13:20 -07003086 for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
3087 ret = ocfs2_read_xattr_bucket(bucket, blkno);
Tao Ma0c044f02008-08-18 17:38:50 +08003088 if (ret) {
3089 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07003090 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003091 }
3092
Tao Ma0c044f02008-08-18 17:38:50 +08003093 /*
3094 * The real bucket num in this series of blocks is stored
3095 * in the 1st bucket.
3096 */
3097 if (i == 0)
Joel Beckerba937122008-10-24 19:13:20 -07003098 num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
Tao Ma0c044f02008-08-18 17:38:50 +08003099
Mark Fashehde29c082008-10-29 14:45:30 -07003100 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
3101 (unsigned long long)blkno,
Joel Beckerba937122008-10-24 19:13:20 -07003102 le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
Tao Ma0c044f02008-08-18 17:38:50 +08003103 if (func) {
Joel Beckerba937122008-10-24 19:13:20 -07003104 ret = func(inode, bucket, para);
3105 if (ret)
Tao Ma0c044f02008-08-18 17:38:50 +08003106 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07003107 /* Fall through to bucket_relse() */
Tao Ma0c044f02008-08-18 17:38:50 +08003108 }
3109
Joel Beckerba937122008-10-24 19:13:20 -07003110 ocfs2_xattr_bucket_relse(bucket);
3111 if (ret)
3112 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003113 }
3114
Joel Beckerba937122008-10-24 19:13:20 -07003115 ocfs2_xattr_bucket_free(bucket);
Tao Ma0c044f02008-08-18 17:38:50 +08003116 return ret;
3117}
3118
3119struct ocfs2_xattr_tree_list {
3120 char *buffer;
3121 size_t buffer_size;
Tao Ma936b8832008-10-09 23:06:14 +08003122 size_t result;
Tao Ma0c044f02008-08-18 17:38:50 +08003123};
3124
3125static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
3126 struct ocfs2_xattr_header *xh,
3127 int index,
3128 int *block_off,
3129 int *new_offset)
3130{
3131 u16 name_offset;
3132
3133 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
3134 return -EINVAL;
3135
3136 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
3137
3138 *block_off = name_offset >> inode->i_sb->s_blocksize_bits;
3139 *new_offset = name_offset % inode->i_sb->s_blocksize;
3140
3141 return 0;
3142}
3143
3144static int ocfs2_list_xattr_bucket(struct inode *inode,
3145 struct ocfs2_xattr_bucket *bucket,
3146 void *para)
3147{
Tao Ma936b8832008-10-09 23:06:14 +08003148 int ret = 0, type;
Tao Ma0c044f02008-08-18 17:38:50 +08003149 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
Tao Ma0c044f02008-08-18 17:38:50 +08003150 int i, block_off, new_offset;
Tao Ma936b8832008-10-09 23:06:14 +08003151 const char *prefix, *name;
Tao Ma0c044f02008-08-18 17:38:50 +08003152
Joel Becker3e632942008-10-24 17:04:49 -07003153 for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
3154 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
Tao Ma936b8832008-10-09 23:06:14 +08003155 type = ocfs2_xattr_get_type(entry);
3156 prefix = ocfs2_xattr_prefix(type);
Tao Ma0c044f02008-08-18 17:38:50 +08003157
Tao Ma936b8832008-10-09 23:06:14 +08003158 if (prefix) {
Tao Ma0c044f02008-08-18 17:38:50 +08003159 ret = ocfs2_xattr_bucket_get_name_value(inode,
Joel Becker3e632942008-10-24 17:04:49 -07003160 bucket_xh(bucket),
Tao Ma0c044f02008-08-18 17:38:50 +08003161 i,
3162 &block_off,
3163 &new_offset);
3164 if (ret)
3165 break;
Tao Ma936b8832008-10-09 23:06:14 +08003166
Joel Becker51def392008-10-24 16:57:21 -07003167 name = (const char *)bucket_block(bucket, block_off) +
Tao Ma936b8832008-10-09 23:06:14 +08003168 new_offset;
3169 ret = ocfs2_xattr_list_entry(xl->buffer,
3170 xl->buffer_size,
3171 &xl->result,
3172 prefix, name,
3173 entry->xe_name_len);
3174 if (ret)
3175 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003176 }
3177 }
3178
3179 return ret;
3180}
3181
3182static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
3183 struct ocfs2_xattr_tree_root *xt,
3184 char *buffer,
3185 size_t buffer_size)
3186{
3187 struct ocfs2_extent_list *el = &xt->xt_list;
3188 int ret = 0;
3189 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
3190 u64 p_blkno = 0;
3191 struct ocfs2_xattr_tree_list xl = {
3192 .buffer = buffer,
3193 .buffer_size = buffer_size,
Tao Ma936b8832008-10-09 23:06:14 +08003194 .result = 0,
Tao Ma0c044f02008-08-18 17:38:50 +08003195 };
3196
3197 if (le16_to_cpu(el->l_next_free_rec) == 0)
3198 return 0;
3199
3200 while (name_hash > 0) {
3201 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
3202 &e_cpos, &num_clusters, el);
3203 if (ret) {
3204 mlog_errno(ret);
3205 goto out;
3206 }
3207
3208 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
3209 ocfs2_list_xattr_bucket,
3210 &xl);
3211 if (ret) {
3212 mlog_errno(ret);
3213 goto out;
3214 }
3215
3216 if (e_cpos == 0)
3217 break;
3218
3219 name_hash = e_cpos - 1;
3220 }
3221
Tao Ma936b8832008-10-09 23:06:14 +08003222 ret = xl.result;
Tao Ma0c044f02008-08-18 17:38:50 +08003223out:
3224 return ret;
3225}
Tao Ma01225592008-08-18 17:38:53 +08003226
3227static int cmp_xe(const void *a, const void *b)
3228{
3229 const struct ocfs2_xattr_entry *l = a, *r = b;
3230 u32 l_hash = le32_to_cpu(l->xe_name_hash);
3231 u32 r_hash = le32_to_cpu(r->xe_name_hash);
3232
3233 if (l_hash > r_hash)
3234 return 1;
3235 if (l_hash < r_hash)
3236 return -1;
3237 return 0;
3238}
3239
3240static void swap_xe(void *a, void *b, int size)
3241{
3242 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
3243
3244 tmp = *l;
3245 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
3246 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
3247}
3248
3249/*
3250 * When the ocfs2_xattr_block is filled up, new bucket will be created
3251 * and all the xattr entries will be moved to the new bucket.
Joel Becker178eeac2008-10-27 15:18:29 -07003252 * The header goes at the start of the bucket, and the names+values are
3253 * filled from the end. This is why *target starts as the last buffer.
Tao Ma01225592008-08-18 17:38:53 +08003254 * Note: we need to sort the entries since they are not saved in order
3255 * in the ocfs2_xattr_block.
3256 */
3257static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
3258 struct buffer_head *xb_bh,
Joel Becker178eeac2008-10-27 15:18:29 -07003259 struct ocfs2_xattr_bucket *bucket)
Tao Ma01225592008-08-18 17:38:53 +08003260{
3261 int i, blocksize = inode->i_sb->s_blocksize;
Joel Becker178eeac2008-10-27 15:18:29 -07003262 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08003263 u16 offset, size, off_change;
3264 struct ocfs2_xattr_entry *xe;
3265 struct ocfs2_xattr_block *xb =
3266 (struct ocfs2_xattr_block *)xb_bh->b_data;
3267 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
Joel Becker178eeac2008-10-27 15:18:29 -07003268 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma01225592008-08-18 17:38:53 +08003269 u16 count = le16_to_cpu(xb_xh->xh_count);
Joel Becker178eeac2008-10-27 15:18:29 -07003270 char *src = xb_bh->b_data;
3271 char *target = bucket_block(bucket, blks - 1);
Tao Ma01225592008-08-18 17:38:53 +08003272
3273 mlog(0, "cp xattr from block %llu to bucket %llu\n",
3274 (unsigned long long)xb_bh->b_blocknr,
Joel Becker178eeac2008-10-27 15:18:29 -07003275 (unsigned long long)bucket_blkno(bucket));
Tao Ma01225592008-08-18 17:38:53 +08003276
Joel Becker178eeac2008-10-27 15:18:29 -07003277 for (i = 0; i < blks; i++)
3278 memset(bucket_block(bucket, i), 0, blocksize);
3279
Tao Ma01225592008-08-18 17:38:53 +08003280 /*
3281 * Since the xe_name_offset is based on ocfs2_xattr_header,
3282 * there is a offset change corresponding to the change of
3283 * ocfs2_xattr_header's position.
3284 */
3285 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
3286 xe = &xb_xh->xh_entries[count - 1];
3287 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
3288 size = blocksize - offset;
3289
3290 /* copy all the names and values. */
Tao Ma01225592008-08-18 17:38:53 +08003291 memcpy(target + offset, src + offset, size);
3292
3293 /* Init new header now. */
3294 xh->xh_count = xb_xh->xh_count;
3295 xh->xh_num_buckets = cpu_to_le16(1);
3296 xh->xh_name_value_len = cpu_to_le16(size);
3297 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
3298
3299 /* copy all the entries. */
Joel Becker178eeac2008-10-27 15:18:29 -07003300 target = bucket_block(bucket, 0);
Tao Ma01225592008-08-18 17:38:53 +08003301 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
3302 size = count * sizeof(struct ocfs2_xattr_entry);
3303 memcpy(target + offset, (char *)xb_xh + offset, size);
3304
3305 /* Change the xe offset for all the xe because of the move. */
3306 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
3307 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
3308 for (i = 0; i < count; i++)
3309 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
3310
3311 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
3312 offset, size, off_change);
3313
3314 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
3315 cmp_xe, swap_xe);
3316}
3317
3318/*
3319 * After we move xattr from block to index btree, we have to
3320 * update ocfs2_xattr_search to the new xe and base.
3321 *
3322 * When the entry is in xattr block, xattr_bh indicates the storage place.
3323 * While if the entry is in index b-tree, "bucket" indicates the
3324 * real place of the xattr.
3325 */
Joel Becker178eeac2008-10-27 15:18:29 -07003326static void ocfs2_xattr_update_xattr_search(struct inode *inode,
3327 struct ocfs2_xattr_search *xs,
3328 struct buffer_head *old_bh)
Tao Ma01225592008-08-18 17:38:53 +08003329{
Tao Ma01225592008-08-18 17:38:53 +08003330 char *buf = old_bh->b_data;
3331 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
3332 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
Joel Becker178eeac2008-10-27 15:18:29 -07003333 int i;
Tao Ma01225592008-08-18 17:38:53 +08003334
Joel Beckerba937122008-10-24 19:13:20 -07003335 xs->header = bucket_xh(xs->bucket);
Joel Becker178eeac2008-10-27 15:18:29 -07003336 xs->base = bucket_block(xs->bucket, 0);
Tao Ma01225592008-08-18 17:38:53 +08003337 xs->end = xs->base + inode->i_sb->s_blocksize;
3338
Joel Becker178eeac2008-10-27 15:18:29 -07003339 if (xs->not_found)
3340 return;
Tao Ma01225592008-08-18 17:38:53 +08003341
Joel Becker178eeac2008-10-27 15:18:29 -07003342 i = xs->here - old_xh->xh_entries;
3343 xs->here = &xs->header->xh_entries[i];
Tao Ma01225592008-08-18 17:38:53 +08003344}
3345
3346static int ocfs2_xattr_create_index_block(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +08003347 struct ocfs2_xattr_search *xs,
3348 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08003349{
Tao Ma85db90e2008-11-12 08:27:01 +08003350 int ret;
Tao Ma01225592008-08-18 17:38:53 +08003351 u32 bit_off, len;
3352 u64 blkno;
Tao Ma85db90e2008-11-12 08:27:01 +08003353 handle_t *handle = ctxt->handle;
Tao Ma01225592008-08-18 17:38:53 +08003354 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3355 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Tao Ma01225592008-08-18 17:38:53 +08003356 struct buffer_head *xb_bh = xs->xattr_bh;
3357 struct ocfs2_xattr_block *xb =
3358 (struct ocfs2_xattr_block *)xb_bh->b_data;
3359 struct ocfs2_xattr_tree_root *xr;
3360 u16 xb_flags = le16_to_cpu(xb->xb_flags);
Tao Ma01225592008-08-18 17:38:53 +08003361
3362 mlog(0, "create xattr index block for %llu\n",
3363 (unsigned long long)xb_bh->b_blocknr);
3364
3365 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
Joel Becker178eeac2008-10-27 15:18:29 -07003366 BUG_ON(!xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08003367
Tao Ma01225592008-08-18 17:38:53 +08003368 /*
3369 * XXX:
3370 * We can use this lock for now, and maybe move to a dedicated mutex
3371 * if performance becomes a problem later.
3372 */
3373 down_write(&oi->ip_alloc_sem);
3374
Tao Ma01225592008-08-18 17:38:53 +08003375 ret = ocfs2_journal_access(handle, inode, xb_bh,
3376 OCFS2_JOURNAL_ACCESS_WRITE);
3377 if (ret) {
3378 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003379 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003380 }
3381
Tao Ma78f30c32008-11-12 08:27:00 +08003382 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac,
3383 1, 1, &bit_off, &len);
Tao Ma01225592008-08-18 17:38:53 +08003384 if (ret) {
3385 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003386 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003387 }
3388
3389 /*
3390 * The bucket may spread in many blocks, and
3391 * we will only touch the 1st block and the last block
3392 * in the whole bucket(one for entry and one for data).
3393 */
3394 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
3395
Mark Fashehde29c082008-10-29 14:45:30 -07003396 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
3397 (unsigned long long)blkno);
Tao Ma01225592008-08-18 17:38:53 +08003398
Joel Becker178eeac2008-10-27 15:18:29 -07003399 ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
Tao Ma01225592008-08-18 17:38:53 +08003400 if (ret) {
3401 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003402 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003403 }
3404
Joel Becker178eeac2008-10-27 15:18:29 -07003405 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
3406 OCFS2_JOURNAL_ACCESS_CREATE);
Joel Beckerbd60bd32008-10-20 18:25:56 -07003407 if (ret) {
3408 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003409 goto out;
Joel Beckerbd60bd32008-10-20 18:25:56 -07003410 }
Tao Ma01225592008-08-18 17:38:53 +08003411
Joel Becker178eeac2008-10-27 15:18:29 -07003412 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
3413 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
3414
3415 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
3416
Tao Ma01225592008-08-18 17:38:53 +08003417 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
3418 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
3419 offsetof(struct ocfs2_xattr_block, xb_attrs));
3420
3421 xr = &xb->xb_attrs.xb_root;
3422 xr->xt_clusters = cpu_to_le32(1);
3423 xr->xt_last_eb_blk = 0;
3424 xr->xt_list.l_tree_depth = 0;
3425 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
3426 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
3427
3428 xr->xt_list.l_recs[0].e_cpos = 0;
3429 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
3430 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
3431
3432 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
3433
Tao Ma85db90e2008-11-12 08:27:01 +08003434 ocfs2_journal_dirty(handle, xb_bh);
Tao Ma01225592008-08-18 17:38:53 +08003435
Tao Ma85db90e2008-11-12 08:27:01 +08003436out:
Tao Ma01225592008-08-18 17:38:53 +08003437 up_write(&oi->ip_alloc_sem);
3438
Tao Ma01225592008-08-18 17:38:53 +08003439 return ret;
3440}
3441
3442static int cmp_xe_offset(const void *a, const void *b)
3443{
3444 const struct ocfs2_xattr_entry *l = a, *r = b;
3445 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
3446 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
3447
3448 if (l_name_offset < r_name_offset)
3449 return 1;
3450 if (l_name_offset > r_name_offset)
3451 return -1;
3452 return 0;
3453}
3454
3455/*
3456 * defrag a xattr bucket if we find that the bucket has some
3457 * holes beteen name/value pairs.
3458 * We will move all the name/value pairs to the end of the bucket
3459 * so that we can spare some space for insertion.
3460 */
3461static int ocfs2_defrag_xattr_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08003462 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08003463 struct ocfs2_xattr_bucket *bucket)
3464{
3465 int ret, i;
3466 size_t end, offset, len, value_len;
3467 struct ocfs2_xattr_header *xh;
3468 char *entries, *buf, *bucket_buf = NULL;
Joel Becker9c7759a2008-10-24 16:21:03 -07003469 u64 blkno = bucket_blkno(bucket);
Tao Ma01225592008-08-18 17:38:53 +08003470 u16 xh_free_start;
Tao Ma01225592008-08-18 17:38:53 +08003471 size_t blocksize = inode->i_sb->s_blocksize;
Tao Ma01225592008-08-18 17:38:53 +08003472 struct ocfs2_xattr_entry *xe;
Tao Ma01225592008-08-18 17:38:53 +08003473
3474 /*
3475 * In order to make the operation more efficient and generic,
3476 * we copy all the blocks into a contiguous memory and do the
3477 * defragment there, so if anything is error, we will not touch
3478 * the real block.
3479 */
3480 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
3481 if (!bucket_buf) {
3482 ret = -EIO;
3483 goto out;
3484 }
3485
Joel Becker161d6f32008-10-27 15:25:18 -07003486 buf = bucket_buf;
Tao Ma1c32a2f2008-11-06 08:10:47 +08003487 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
3488 memcpy(buf, bucket_block(bucket, i), blocksize);
Joel Becker161d6f32008-10-27 15:25:18 -07003489
Tao Ma1c32a2f2008-11-06 08:10:47 +08003490 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
Joel Becker161d6f32008-10-27 15:25:18 -07003491 OCFS2_JOURNAL_ACCESS_WRITE);
3492 if (ret < 0) {
3493 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003494 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003495 }
3496
3497 xh = (struct ocfs2_xattr_header *)bucket_buf;
3498 entries = (char *)xh->xh_entries;
3499 xh_free_start = le16_to_cpu(xh->xh_free_start);
3500
3501 mlog(0, "adjust xattr bucket in %llu, count = %u, "
3502 "xh_free_start = %u, xh_name_value_len = %u.\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003503 (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
3504 xh_free_start, le16_to_cpu(xh->xh_name_value_len));
Tao Ma01225592008-08-18 17:38:53 +08003505
3506 /*
3507 * sort all the entries by their offset.
3508 * the largest will be the first, so that we can
3509 * move them to the end one by one.
3510 */
3511 sort(entries, le16_to_cpu(xh->xh_count),
3512 sizeof(struct ocfs2_xattr_entry),
3513 cmp_xe_offset, swap_xe);
3514
3515 /* Move all name/values to the end of the bucket. */
3516 xe = xh->xh_entries;
3517 end = OCFS2_XATTR_BUCKET_SIZE;
3518 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
3519 offset = le16_to_cpu(xe->xe_name_offset);
3520 if (ocfs2_xattr_is_local(xe))
3521 value_len = OCFS2_XATTR_SIZE(
3522 le64_to_cpu(xe->xe_value_size));
3523 else
3524 value_len = OCFS2_XATTR_ROOT_SIZE;
3525 len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
3526
3527 /*
3528 * We must make sure that the name/value pair
3529 * exist in the same block. So adjust end to
3530 * the previous block end if needed.
3531 */
3532 if (((end - len) / blocksize !=
3533 (end - 1) / blocksize))
3534 end = end - end % blocksize;
3535
3536 if (end > offset + len) {
3537 memmove(bucket_buf + end - len,
3538 bucket_buf + offset, len);
3539 xe->xe_name_offset = cpu_to_le16(end - len);
3540 }
3541
3542 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
3543 "bucket %llu\n", (unsigned long long)blkno);
3544
3545 end -= len;
3546 }
3547
3548 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
3549 "bucket %llu\n", (unsigned long long)blkno);
3550
3551 if (xh_free_start == end)
Tao Ma85db90e2008-11-12 08:27:01 +08003552 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003553
3554 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
3555 xh->xh_free_start = cpu_to_le16(end);
3556
3557 /* sort the entries by their name_hash. */
3558 sort(entries, le16_to_cpu(xh->xh_count),
3559 sizeof(struct ocfs2_xattr_entry),
3560 cmp_xe, swap_xe);
3561
3562 buf = bucket_buf;
Tao Ma1c32a2f2008-11-06 08:10:47 +08003563 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
3564 memcpy(bucket_block(bucket, i), buf, blocksize);
3565 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
Tao Ma01225592008-08-18 17:38:53 +08003566
Tao Ma01225592008-08-18 17:38:53 +08003567out:
Tao Ma01225592008-08-18 17:38:53 +08003568 kfree(bucket_buf);
3569 return ret;
3570}
3571
3572/*
Joel Beckerb5c03e72008-11-25 19:58:16 -08003573 * prev_blkno points to the start of an existing extent. new_blkno
3574 * points to a newly allocated extent. Because we know each of our
3575 * clusters contains more than bucket, we can easily split one cluster
3576 * at a bucket boundary. So we take the last cluster of the existing
3577 * extent and split it down the middle. We move the last half of the
3578 * buckets in the last cluster of the existing extent over to the new
3579 * extent.
Tao Ma01225592008-08-18 17:38:53 +08003580 *
Joel Beckerb5c03e72008-11-25 19:58:16 -08003581 * first_bh is the buffer at prev_blkno so we can update the existing
3582 * extent's bucket count. header_bh is the bucket were we were hoping
3583 * to insert our xattr. If the bucket move places the target in the new
3584 * extent, we'll update first_bh and header_bh after modifying the old
3585 * extent.
3586 *
3587 * first_hash will be set as the 1st xe's name_hash in the new extent.
Tao Ma01225592008-08-18 17:38:53 +08003588 */
3589static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
3590 handle_t *handle,
Joel Becker41cb8142008-11-26 14:25:21 -08003591 struct ocfs2_xattr_bucket *first,
3592 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08003593 u64 new_blkno,
Tao Ma01225592008-08-18 17:38:53 +08003594 u32 num_clusters,
3595 u32 *first_hash)
3596{
Joel Beckerc58b6032008-11-26 13:36:24 -08003597 int ret;
Joel Becker41cb8142008-11-26 14:25:21 -08003598 struct super_block *sb = inode->i_sb;
3599 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb);
3600 int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
Joel Beckerb5c03e72008-11-25 19:58:16 -08003601 int to_move = num_buckets / 2;
Joel Beckerc58b6032008-11-26 13:36:24 -08003602 u64 src_blkno;
Joel Becker41cb8142008-11-26 14:25:21 -08003603 u64 last_cluster_blkno = bucket_blkno(first) +
3604 ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1));
Tao Ma01225592008-08-18 17:38:53 +08003605
Joel Becker41cb8142008-11-26 14:25:21 -08003606 BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets);
3607 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize);
Tao Ma01225592008-08-18 17:38:53 +08003608
Tao Ma01225592008-08-18 17:38:53 +08003609 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
Joel Beckerc58b6032008-11-26 13:36:24 -08003610 (unsigned long long)last_cluster_blkno, (unsigned long long)new_blkno);
Tao Ma01225592008-08-18 17:38:53 +08003611
Joel Becker41cb8142008-11-26 14:25:21 -08003612 ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first),
Joel Beckerc58b6032008-11-26 13:36:24 -08003613 last_cluster_blkno, new_blkno,
3614 to_move, first_hash);
Joel Beckerb5c03e72008-11-25 19:58:16 -08003615 if (ret) {
3616 mlog_errno(ret);
3617 goto out;
3618 }
3619
Joel Beckerc58b6032008-11-26 13:36:24 -08003620 /* This is the first bucket that got moved */
3621 src_blkno = last_cluster_blkno + (to_move * blks_per_bucket);
3622
Tao Ma01225592008-08-18 17:38:53 +08003623 /*
Joel Beckerc58b6032008-11-26 13:36:24 -08003624 * If the target bucket was part of the moved buckets, we need to
Joel Becker41cb8142008-11-26 14:25:21 -08003625 * update first and target.
Joel Beckerb5c03e72008-11-25 19:58:16 -08003626 */
Joel Becker41cb8142008-11-26 14:25:21 -08003627 if (bucket_blkno(target) >= src_blkno) {
Joel Beckerb5c03e72008-11-25 19:58:16 -08003628 /* Find the block for the new target bucket */
3629 src_blkno = new_blkno +
Joel Becker41cb8142008-11-26 14:25:21 -08003630 (bucket_blkno(target) - src_blkno);
3631
3632 ocfs2_xattr_bucket_relse(first);
3633 ocfs2_xattr_bucket_relse(target);
Joel Beckerb5c03e72008-11-25 19:58:16 -08003634
3635 /*
Joel Beckerc58b6032008-11-26 13:36:24 -08003636 * These shouldn't fail - the buffers are in the
Joel Beckerb5c03e72008-11-25 19:58:16 -08003637 * journal from ocfs2_cp_xattr_bucket().
3638 */
Joel Becker41cb8142008-11-26 14:25:21 -08003639 ret = ocfs2_read_xattr_bucket(first, new_blkno);
Joel Beckerc58b6032008-11-26 13:36:24 -08003640 if (ret) {
3641 mlog_errno(ret);
3642 goto out;
3643 }
Joel Becker41cb8142008-11-26 14:25:21 -08003644 ret = ocfs2_read_xattr_bucket(target, src_blkno);
3645 if (ret)
Joel Beckerb5c03e72008-11-25 19:58:16 -08003646 mlog_errno(ret);
Joel Beckerb5c03e72008-11-25 19:58:16 -08003647
Joel Beckerb5c03e72008-11-25 19:58:16 -08003648 }
3649
Tao Ma01225592008-08-18 17:38:53 +08003650out:
Tao Ma01225592008-08-18 17:38:53 +08003651 return ret;
3652}
3653
Tao Ma01225592008-08-18 17:38:53 +08003654/*
Tao Ma80bcaf32008-10-27 06:06:24 +08003655 * Find the suitable pos when we divide a bucket into 2.
3656 * We have to make sure the xattrs with the same hash value exist
3657 * in the same bucket.
3658 *
3659 * If this ocfs2_xattr_header covers more than one hash value, find a
3660 * place where the hash value changes. Try to find the most even split.
3661 * The most common case is that all entries have different hash values,
3662 * and the first check we make will find a place to split.
Tao Ma01225592008-08-18 17:38:53 +08003663 */
Tao Ma80bcaf32008-10-27 06:06:24 +08003664static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
3665{
3666 struct ocfs2_xattr_entry *entries = xh->xh_entries;
3667 int count = le16_to_cpu(xh->xh_count);
3668 int delta, middle = count / 2;
3669
3670 /*
3671 * We start at the middle. Each step gets farther away in both
3672 * directions. We therefore hit the change in hash value
3673 * nearest to the middle. Note that this loop does not execute for
3674 * count < 2.
3675 */
3676 for (delta = 0; delta < middle; delta++) {
3677 /* Let's check delta earlier than middle */
3678 if (cmp_xe(&entries[middle - delta - 1],
3679 &entries[middle - delta]))
3680 return middle - delta;
3681
3682 /* For even counts, don't walk off the end */
3683 if ((middle + delta + 1) == count)
3684 continue;
3685
3686 /* Now try delta past middle */
3687 if (cmp_xe(&entries[middle + delta],
3688 &entries[middle + delta + 1]))
3689 return middle + delta + 1;
3690 }
3691
3692 /* Every entry had the same hash */
3693 return count;
3694}
3695
3696/*
3697 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
3698 * first_hash will record the 1st hash of the new bucket.
3699 *
3700 * Normally half of the xattrs will be moved. But we have to make
3701 * sure that the xattrs with the same hash value are stored in the
3702 * same bucket. If all the xattrs in this bucket have the same hash
3703 * value, the new bucket will be initialized as an empty one and the
3704 * first_hash will be initialized as (hash_value+1).
3705 */
3706static int ocfs2_divide_xattr_bucket(struct inode *inode,
3707 handle_t *handle,
3708 u64 blk,
3709 u64 new_blk,
3710 u32 *first_hash,
3711 int new_bucket_head)
Tao Ma01225592008-08-18 17:38:53 +08003712{
3713 int ret, i;
Tao Ma80bcaf32008-10-27 06:06:24 +08003714 int count, start, len, name_value_len = 0, xe_len, name_offset = 0;
Joel Beckerba937122008-10-24 19:13:20 -07003715 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
Tao Ma01225592008-08-18 17:38:53 +08003716 struct ocfs2_xattr_header *xh;
3717 struct ocfs2_xattr_entry *xe;
3718 int blocksize = inode->i_sb->s_blocksize;
3719
Tao Ma80bcaf32008-10-27 06:06:24 +08003720 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003721 (unsigned long long)blk, (unsigned long long)new_blk);
Tao Ma01225592008-08-18 17:38:53 +08003722
Joel Beckerba937122008-10-24 19:13:20 -07003723 s_bucket = ocfs2_xattr_bucket_new(inode);
3724 t_bucket = ocfs2_xattr_bucket_new(inode);
3725 if (!s_bucket || !t_bucket) {
3726 ret = -ENOMEM;
3727 mlog_errno(ret);
3728 goto out;
3729 }
Tao Ma01225592008-08-18 17:38:53 +08003730
Joel Beckerba937122008-10-24 19:13:20 -07003731 ret = ocfs2_read_xattr_bucket(s_bucket, blk);
Tao Ma01225592008-08-18 17:38:53 +08003732 if (ret) {
3733 mlog_errno(ret);
3734 goto out;
3735 }
3736
Joel Beckerba937122008-10-24 19:13:20 -07003737 ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07003738 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08003739 if (ret) {
3740 mlog_errno(ret);
3741 goto out;
3742 }
3743
Joel Becker784b8162008-10-24 17:33:40 -07003744 /*
3745 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
3746 * there's no need to read it.
3747 */
Joel Beckerba937122008-10-24 19:13:20 -07003748 ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
Tao Ma01225592008-08-18 17:38:53 +08003749 if (ret) {
3750 mlog_errno(ret);
3751 goto out;
3752 }
3753
Joel Becker2b656c12008-11-25 19:00:15 -08003754 /*
3755 * Hey, if we're overwriting t_bucket, what difference does
3756 * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the
3757 * same part of ocfs2_cp_xattr_bucket().
3758 */
Joel Beckerba937122008-10-24 19:13:20 -07003759 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07003760 new_bucket_head ?
3761 OCFS2_JOURNAL_ACCESS_CREATE :
3762 OCFS2_JOURNAL_ACCESS_WRITE);
3763 if (ret) {
3764 mlog_errno(ret);
3765 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003766 }
3767
Joel Beckerba937122008-10-24 19:13:20 -07003768 xh = bucket_xh(s_bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08003769 count = le16_to_cpu(xh->xh_count);
3770 start = ocfs2_xattr_find_divide_pos(xh);
3771
3772 if (start == count) {
3773 xe = &xh->xh_entries[start-1];
3774
3775 /*
3776 * initialized a new empty bucket here.
3777 * The hash value is set as one larger than
3778 * that of the last entry in the previous bucket.
3779 */
Joel Beckerba937122008-10-24 19:13:20 -07003780 for (i = 0; i < t_bucket->bu_blocks; i++)
3781 memset(bucket_block(t_bucket, i), 0, blocksize);
Tao Ma80bcaf32008-10-27 06:06:24 +08003782
Joel Beckerba937122008-10-24 19:13:20 -07003783 xh = bucket_xh(t_bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08003784 xh->xh_free_start = cpu_to_le16(blocksize);
3785 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
3786 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
3787
3788 goto set_num_buckets;
3789 }
3790
Tao Ma01225592008-08-18 17:38:53 +08003791 /* copy the whole bucket to the new first. */
Joel Beckerba937122008-10-24 19:13:20 -07003792 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003793
3794 /* update the new bucket. */
Joel Beckerba937122008-10-24 19:13:20 -07003795 xh = bucket_xh(t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003796
3797 /*
3798 * Calculate the total name/value len and xh_free_start for
3799 * the old bucket first.
3800 */
3801 name_offset = OCFS2_XATTR_BUCKET_SIZE;
3802 name_value_len = 0;
3803 for (i = 0; i < start; i++) {
3804 xe = &xh->xh_entries[i];
3805 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3806 if (ocfs2_xattr_is_local(xe))
3807 xe_len +=
3808 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3809 else
3810 xe_len += OCFS2_XATTR_ROOT_SIZE;
3811 name_value_len += xe_len;
3812 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
3813 name_offset = le16_to_cpu(xe->xe_name_offset);
3814 }
3815
3816 /*
3817 * Now begin the modification to the new bucket.
3818 *
3819 * In the new bucket, We just move the xattr entry to the beginning
3820 * and don't touch the name/value. So there will be some holes in the
3821 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
3822 * called.
3823 */
3824 xe = &xh->xh_entries[start];
3825 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
3826 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
Mark Fashehff1ec202008-08-19 10:54:29 -07003827 (int)((char *)xe - (char *)xh),
3828 (int)((char *)xh->xh_entries - (char *)xh));
Tao Ma01225592008-08-18 17:38:53 +08003829 memmove((char *)xh->xh_entries, (char *)xe, len);
3830 xe = &xh->xh_entries[count - start];
3831 len = sizeof(struct ocfs2_xattr_entry) * start;
3832 memset((char *)xe, 0, len);
3833
3834 le16_add_cpu(&xh->xh_count, -start);
3835 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
3836
3837 /* Calculate xh_free_start for the new bucket. */
3838 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3839 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3840 xe = &xh->xh_entries[i];
3841 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3842 if (ocfs2_xattr_is_local(xe))
3843 xe_len +=
3844 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3845 else
3846 xe_len += OCFS2_XATTR_ROOT_SIZE;
3847 if (le16_to_cpu(xe->xe_name_offset) <
3848 le16_to_cpu(xh->xh_free_start))
3849 xh->xh_free_start = xe->xe_name_offset;
3850 }
3851
Tao Ma80bcaf32008-10-27 06:06:24 +08003852set_num_buckets:
Tao Ma01225592008-08-18 17:38:53 +08003853 /* set xh->xh_num_buckets for the new xh. */
3854 if (new_bucket_head)
3855 xh->xh_num_buckets = cpu_to_le16(1);
3856 else
3857 xh->xh_num_buckets = 0;
3858
Joel Beckerba937122008-10-24 19:13:20 -07003859 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003860
3861 /* store the first_hash of the new bucket. */
3862 if (first_hash)
3863 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3864
3865 /*
Tao Ma80bcaf32008-10-27 06:06:24 +08003866 * Now only update the 1st block of the old bucket. If we
3867 * just added a new empty bucket, there is no need to modify
3868 * it.
Tao Ma01225592008-08-18 17:38:53 +08003869 */
Tao Ma80bcaf32008-10-27 06:06:24 +08003870 if (start == count)
3871 goto out;
3872
Joel Beckerba937122008-10-24 19:13:20 -07003873 xh = bucket_xh(s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003874 memset(&xh->xh_entries[start], 0,
3875 sizeof(struct ocfs2_xattr_entry) * (count - start));
3876 xh->xh_count = cpu_to_le16(start);
3877 xh->xh_free_start = cpu_to_le16(name_offset);
3878 xh->xh_name_value_len = cpu_to_le16(name_value_len);
3879
Joel Beckerba937122008-10-24 19:13:20 -07003880 ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003881
3882out:
Joel Beckerba937122008-10-24 19:13:20 -07003883 ocfs2_xattr_bucket_free(s_bucket);
3884 ocfs2_xattr_bucket_free(t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003885
3886 return ret;
3887}
3888
3889/*
3890 * Copy xattr from one bucket to another bucket.
3891 *
3892 * The caller must make sure that the journal transaction
3893 * has enough space for journaling.
3894 */
3895static int ocfs2_cp_xattr_bucket(struct inode *inode,
3896 handle_t *handle,
3897 u64 s_blkno,
3898 u64 t_blkno,
3899 int t_is_new)
3900{
Joel Becker4980c6d2008-10-24 18:54:43 -07003901 int ret;
Joel Beckerba937122008-10-24 19:13:20 -07003902 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
Tao Ma01225592008-08-18 17:38:53 +08003903
3904 BUG_ON(s_blkno == t_blkno);
3905
3906 mlog(0, "cp bucket %llu to %llu, target is %d\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003907 (unsigned long long)s_blkno, (unsigned long long)t_blkno,
3908 t_is_new);
Tao Ma01225592008-08-18 17:38:53 +08003909
Joel Beckerba937122008-10-24 19:13:20 -07003910 s_bucket = ocfs2_xattr_bucket_new(inode);
3911 t_bucket = ocfs2_xattr_bucket_new(inode);
3912 if (!s_bucket || !t_bucket) {
3913 ret = -ENOMEM;
3914 mlog_errno(ret);
3915 goto out;
3916 }
Joel Becker92de1092008-11-25 17:06:40 -08003917
Joel Beckerba937122008-10-24 19:13:20 -07003918 ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
Tao Ma01225592008-08-18 17:38:53 +08003919 if (ret)
3920 goto out;
3921
Joel Becker784b8162008-10-24 17:33:40 -07003922 /*
3923 * Even if !t_is_new, we're overwriting t_bucket. Thus,
3924 * there's no need to read it.
3925 */
Joel Beckerba937122008-10-24 19:13:20 -07003926 ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
Tao Ma01225592008-08-18 17:38:53 +08003927 if (ret)
3928 goto out;
3929
Joel Becker2b656c12008-11-25 19:00:15 -08003930 /*
3931 * Hey, if we're overwriting t_bucket, what difference does
3932 * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new
Joel Becker874d65a2008-11-26 13:02:18 -08003933 * cluster to fill, we came here from
3934 * ocfs2_mv_xattr_buckets(), and it is really new -
3935 * ACCESS_CREATE is required. But we also might have moved data
3936 * out of t_bucket before extending back into it.
3937 * ocfs2_add_new_xattr_bucket() can do this - its call to
3938 * ocfs2_add_new_xattr_cluster() may have created a new extent
Joel Becker2b656c12008-11-25 19:00:15 -08003939 * and copied out the end of the old extent. Then it re-extends
3940 * the old extent back to create space for new xattrs. That's
3941 * how we get here, and the bucket isn't really new.
3942 */
Joel Beckerba937122008-10-24 19:13:20 -07003943 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07003944 t_is_new ?
3945 OCFS2_JOURNAL_ACCESS_CREATE :
3946 OCFS2_JOURNAL_ACCESS_WRITE);
3947 if (ret)
3948 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003949
Joel Beckerba937122008-10-24 19:13:20 -07003950 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
3951 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003952
3953out:
Joel Beckerba937122008-10-24 19:13:20 -07003954 ocfs2_xattr_bucket_free(t_bucket);
3955 ocfs2_xattr_bucket_free(s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003956
3957 return ret;
3958}
3959
3960/*
Joel Becker874d65a2008-11-26 13:02:18 -08003961 * src_blk points to the start of an existing extent. last_blk points to
3962 * last cluster in that extent. to_blk points to a newly allocated
Joel Becker54ecb6b2008-11-26 13:18:31 -08003963 * extent. We copy the buckets from the cluster at last_blk to the new
3964 * extent. If start_bucket is non-zero, we skip that many buckets before
3965 * we start copying. The new extent's xh_num_buckets gets set to the
3966 * number of buckets we copied. The old extent's xh_num_buckets shrinks
3967 * by the same amount.
Tao Ma01225592008-08-18 17:38:53 +08003968 */
Joel Becker54ecb6b2008-11-26 13:18:31 -08003969static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
3970 u64 src_blk, u64 last_blk, u64 to_blk,
3971 unsigned int start_bucket,
3972 u32 *first_hash)
Tao Ma01225592008-08-18 17:38:53 +08003973{
3974 int i, ret, credits;
3975 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Joel Becker15d60922008-11-25 18:36:42 -08003976 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08003977 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
Joel Becker15d60922008-11-25 18:36:42 -08003978 struct ocfs2_xattr_bucket *old_first, *new_first;
Tao Ma01225592008-08-18 17:38:53 +08003979
Joel Becker874d65a2008-11-26 13:02:18 -08003980 mlog(0, "mv xattrs from cluster %llu to %llu\n",
3981 (unsigned long long)last_blk, (unsigned long long)to_blk);
Tao Ma01225592008-08-18 17:38:53 +08003982
Joel Becker54ecb6b2008-11-26 13:18:31 -08003983 BUG_ON(start_bucket >= num_buckets);
3984 if (start_bucket) {
3985 num_buckets -= start_bucket;
3986 last_blk += (start_bucket * blks_per_bucket);
3987 }
3988
Joel Becker15d60922008-11-25 18:36:42 -08003989 /* The first bucket of the original extent */
3990 old_first = ocfs2_xattr_bucket_new(inode);
3991 /* The first bucket of the new extent */
3992 new_first = ocfs2_xattr_bucket_new(inode);
3993 if (!old_first || !new_first) {
3994 ret = -ENOMEM;
3995 mlog_errno(ret);
3996 goto out;
3997 }
3998
Joel Becker874d65a2008-11-26 13:02:18 -08003999 ret = ocfs2_read_xattr_bucket(old_first, src_blk);
Joel Becker15d60922008-11-25 18:36:42 -08004000 if (ret) {
4001 mlog_errno(ret);
4002 goto out;
4003 }
4004
Tao Ma01225592008-08-18 17:38:53 +08004005 /*
Joel Becker54ecb6b2008-11-26 13:18:31 -08004006 * We need to update the first bucket of the old extent and all
4007 * the buckets going to the new extent.
Tao Ma01225592008-08-18 17:38:53 +08004008 */
Joel Becker54ecb6b2008-11-26 13:18:31 -08004009 credits = ((num_buckets + 1) * blks_per_bucket) +
4010 handle->h_buffer_credits;
Tao Ma01225592008-08-18 17:38:53 +08004011 ret = ocfs2_extend_trans(handle, credits);
4012 if (ret) {
4013 mlog_errno(ret);
4014 goto out;
4015 }
4016
Joel Becker15d60922008-11-25 18:36:42 -08004017 ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
4018 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004019 if (ret) {
4020 mlog_errno(ret);
4021 goto out;
4022 }
4023
4024 for (i = 0; i < num_buckets; i++) {
4025 ret = ocfs2_cp_xattr_bucket(inode, handle,
Joel Becker874d65a2008-11-26 13:02:18 -08004026 last_blk + (i * blks_per_bucket),
Joel Becker15d60922008-11-25 18:36:42 -08004027 to_blk + (i * blks_per_bucket),
4028 1);
Tao Ma01225592008-08-18 17:38:53 +08004029 if (ret) {
4030 mlog_errno(ret);
4031 goto out;
4032 }
Tao Ma01225592008-08-18 17:38:53 +08004033 }
4034
Joel Becker15d60922008-11-25 18:36:42 -08004035 /*
4036 * Get the new bucket ready before we dirty anything
4037 * (This actually shouldn't fail, because we already dirtied
4038 * it once in ocfs2_cp_xattr_bucket()).
4039 */
4040 ret = ocfs2_read_xattr_bucket(new_first, to_blk);
4041 if (ret) {
Tao Ma01225592008-08-18 17:38:53 +08004042 mlog_errno(ret);
4043 goto out;
4044 }
Joel Becker15d60922008-11-25 18:36:42 -08004045 ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
4046 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004047 if (ret) {
4048 mlog_errno(ret);
4049 goto out;
4050 }
4051
Joel Becker15d60922008-11-25 18:36:42 -08004052 /* Now update the headers */
4053 le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets);
4054 ocfs2_xattr_bucket_journal_dirty(handle, old_first);
Tao Ma01225592008-08-18 17:38:53 +08004055
Joel Becker15d60922008-11-25 18:36:42 -08004056 bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets);
4057 ocfs2_xattr_bucket_journal_dirty(handle, new_first);
Tao Ma01225592008-08-18 17:38:53 +08004058
4059 if (first_hash)
Joel Becker15d60922008-11-25 18:36:42 -08004060 *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash);
4061
Tao Ma01225592008-08-18 17:38:53 +08004062out:
Joel Becker15d60922008-11-25 18:36:42 -08004063 ocfs2_xattr_bucket_free(new_first);
4064 ocfs2_xattr_bucket_free(old_first);
Tao Ma01225592008-08-18 17:38:53 +08004065 return ret;
4066}
4067
4068/*
Tao Ma80bcaf32008-10-27 06:06:24 +08004069 * Move some xattrs in this cluster to the new cluster.
Tao Ma01225592008-08-18 17:38:53 +08004070 * This function should only be called when bucket size == cluster size.
4071 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
4072 */
Tao Ma80bcaf32008-10-27 06:06:24 +08004073static int ocfs2_divide_xattr_cluster(struct inode *inode,
4074 handle_t *handle,
4075 u64 prev_blk,
4076 u64 new_blk,
4077 u32 *first_hash)
Tao Ma01225592008-08-18 17:38:53 +08004078{
4079 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08004080 int ret, credits = 2 * blk_per_bucket + handle->h_buffer_credits;
Tao Ma01225592008-08-18 17:38:53 +08004081
4082 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
4083
4084 ret = ocfs2_extend_trans(handle, credits);
4085 if (ret) {
4086 mlog_errno(ret);
4087 return ret;
4088 }
4089
4090 /* Move half of the xattr in start_blk to the next bucket. */
Tao Ma80bcaf32008-10-27 06:06:24 +08004091 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
4092 new_blk, first_hash, 1);
Tao Ma01225592008-08-18 17:38:53 +08004093}
4094
4095/*
4096 * Move some xattrs from the old cluster to the new one since they are not
4097 * contiguous in ocfs2 xattr tree.
4098 *
4099 * new_blk starts a new separate cluster, and we will move some xattrs from
4100 * prev_blk to it. v_start will be set as the first name hash value in this
4101 * new cluster so that it can be used as e_cpos during tree insertion and
4102 * don't collide with our original b-tree operations. first_bh and header_bh
4103 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
4104 * to extend the insert bucket.
4105 *
4106 * The problem is how much xattr should we move to the new one and when should
4107 * we update first_bh and header_bh?
4108 * 1. If cluster size > bucket size, that means the previous cluster has more
4109 * than 1 bucket, so just move half nums of bucket into the new cluster and
4110 * update the first_bh and header_bh if the insert bucket has been moved
4111 * to the new cluster.
4112 * 2. If cluster_size == bucket_size:
4113 * a) If the previous extent rec has more than one cluster and the insert
4114 * place isn't in the last cluster, copy the entire last cluster to the
4115 * new one. This time, we don't need to upate the first_bh and header_bh
4116 * since they will not be moved into the new cluster.
4117 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
4118 * the new one. And we set the extend flag to zero if the insert place is
4119 * moved into the new allocated cluster since no extend is needed.
4120 */
4121static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
4122 handle_t *handle,
Joel Becker012ee912008-11-26 14:43:31 -08004123 struct ocfs2_xattr_bucket *first,
4124 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08004125 u64 new_blk,
Tao Ma01225592008-08-18 17:38:53 +08004126 u32 prev_clusters,
4127 u32 *v_start,
4128 int *extend)
4129{
Joel Becker92cf3ad2008-11-26 14:12:09 -08004130 int ret;
Tao Ma01225592008-08-18 17:38:53 +08004131
4132 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
Joel Becker012ee912008-11-26 14:43:31 -08004133 (unsigned long long)bucket_blkno(first), prev_clusters,
Mark Fashehde29c082008-10-29 14:45:30 -07004134 (unsigned long long)new_blk);
Tao Ma01225592008-08-18 17:38:53 +08004135
Joel Becker41cb8142008-11-26 14:25:21 -08004136 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) {
Tao Ma01225592008-08-18 17:38:53 +08004137 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
4138 handle,
Joel Becker41cb8142008-11-26 14:25:21 -08004139 first, target,
Tao Ma01225592008-08-18 17:38:53 +08004140 new_blk,
Tao Ma01225592008-08-18 17:38:53 +08004141 prev_clusters,
4142 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08004143 if (ret)
Joel Becker41cb8142008-11-26 14:25:21 -08004144 mlog_errno(ret);
Joel Becker41cb8142008-11-26 14:25:21 -08004145 } else {
Joel Becker92cf3ad2008-11-26 14:12:09 -08004146 /* The start of the last cluster in the first extent */
4147 u64 last_blk = bucket_blkno(first) +
4148 ((prev_clusters - 1) *
4149 ocfs2_clusters_to_blocks(inode->i_sb, 1));
Tao Ma01225592008-08-18 17:38:53 +08004150
Joel Becker012ee912008-11-26 14:43:31 -08004151 if (prev_clusters > 1 && bucket_blkno(target) != last_blk) {
Joel Becker874d65a2008-11-26 13:02:18 -08004152 ret = ocfs2_mv_xattr_buckets(inode, handle,
Joel Becker92cf3ad2008-11-26 14:12:09 -08004153 bucket_blkno(first),
Joel Becker54ecb6b2008-11-26 13:18:31 -08004154 last_blk, new_blk, 0,
Tao Ma01225592008-08-18 17:38:53 +08004155 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08004156 if (ret)
4157 mlog_errno(ret);
4158 } else {
Tao Ma80bcaf32008-10-27 06:06:24 +08004159 ret = ocfs2_divide_xattr_cluster(inode, handle,
4160 last_blk, new_blk,
4161 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08004162 if (ret)
4163 mlog_errno(ret);
Tao Ma01225592008-08-18 17:38:53 +08004164
Joel Becker92cf3ad2008-11-26 14:12:09 -08004165 if ((bucket_blkno(target) == last_blk) && extend)
Tao Ma01225592008-08-18 17:38:53 +08004166 *extend = 0;
4167 }
4168 }
4169
4170 return ret;
4171}
4172
4173/*
4174 * Add a new cluster for xattr storage.
4175 *
4176 * If the new cluster is contiguous with the previous one, it will be
4177 * appended to the same extent record, and num_clusters will be updated.
4178 * If not, we will insert a new extent for it and move some xattrs in
4179 * the last cluster into the new allocated one.
4180 * We also need to limit the maximum size of a btree leaf, otherwise we'll
4181 * lose the benefits of hashing because we'll have to search large leaves.
4182 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
4183 * if it's bigger).
4184 *
4185 * first_bh is the first block of the previous extent rec and header_bh
4186 * indicates the bucket we will insert the new xattrs. They will be updated
4187 * when the header_bh is moved into the new cluster.
4188 */
4189static int ocfs2_add_new_xattr_cluster(struct inode *inode,
4190 struct buffer_head *root_bh,
Joel Beckered29c0c2008-11-26 15:08:44 -08004191 struct ocfs2_xattr_bucket *first,
4192 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08004193 u32 *num_clusters,
4194 u32 prev_cpos,
Tao Ma78f30c32008-11-12 08:27:00 +08004195 int *extend,
4196 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004197{
Tao Ma85db90e2008-11-12 08:27:01 +08004198 int ret;
Tao Ma01225592008-08-18 17:38:53 +08004199 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
4200 u32 prev_clusters = *num_clusters;
4201 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
4202 u64 block;
Tao Ma85db90e2008-11-12 08:27:01 +08004203 handle_t *handle = ctxt->handle;
Tao Ma01225592008-08-18 17:38:53 +08004204 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004205 struct ocfs2_extent_tree et;
Tao Ma01225592008-08-18 17:38:53 +08004206
4207 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
4208 "previous xattr blkno = %llu\n",
4209 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Joel Beckered29c0c2008-11-26 15:08:44 -08004210 prev_cpos, (unsigned long long)bucket_blkno(first));
Tao Ma01225592008-08-18 17:38:53 +08004211
Joel Becker8d6220d2008-08-22 12:46:09 -07004212 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004213
Tao Ma01225592008-08-18 17:38:53 +08004214 ret = ocfs2_journal_access(handle, inode, root_bh,
4215 OCFS2_JOURNAL_ACCESS_WRITE);
4216 if (ret < 0) {
4217 mlog_errno(ret);
4218 goto leave;
4219 }
4220
Tao Ma78f30c32008-11-12 08:27:00 +08004221 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, 1,
Tao Ma01225592008-08-18 17:38:53 +08004222 clusters_to_add, &bit_off, &num_bits);
4223 if (ret < 0) {
4224 if (ret != -ENOSPC)
4225 mlog_errno(ret);
4226 goto leave;
4227 }
4228
4229 BUG_ON(num_bits > clusters_to_add);
4230
4231 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4232 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
4233 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4234
Joel Beckered29c0c2008-11-26 15:08:44 -08004235 if (bucket_blkno(first) + (prev_clusters * bpc) == block &&
Tao Ma01225592008-08-18 17:38:53 +08004236 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
4237 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
4238 /*
4239 * If this cluster is contiguous with the old one and
4240 * adding this new cluster, we don't surpass the limit of
4241 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
4242 * initialized and used like other buckets in the previous
4243 * cluster.
4244 * So add it as a contiguous one. The caller will handle
4245 * its init process.
4246 */
4247 v_start = prev_cpos + prev_clusters;
4248 *num_clusters = prev_clusters + num_bits;
4249 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
4250 num_bits);
4251 } else {
4252 ret = ocfs2_adjust_xattr_cross_cluster(inode,
4253 handle,
Joel Becker012ee912008-11-26 14:43:31 -08004254 first,
4255 target,
Tao Ma01225592008-08-18 17:38:53 +08004256 block,
Tao Ma01225592008-08-18 17:38:53 +08004257 prev_clusters,
4258 &v_start,
4259 extend);
4260 if (ret) {
4261 mlog_errno(ret);
4262 goto leave;
4263 }
4264 }
4265
4266 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
Mark Fashehde29c082008-10-29 14:45:30 -07004267 num_bits, (unsigned long long)block, v_start);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004268 ret = ocfs2_insert_extent(osb, handle, inode, &et, v_start, block,
Tao Ma78f30c32008-11-12 08:27:00 +08004269 num_bits, 0, ctxt->meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08004270 if (ret < 0) {
4271 mlog_errno(ret);
4272 goto leave;
4273 }
4274
4275 ret = ocfs2_journal_dirty(handle, root_bh);
Tao Ma85db90e2008-11-12 08:27:01 +08004276 if (ret < 0)
Tao Ma01225592008-08-18 17:38:53 +08004277 mlog_errno(ret);
Tao Ma01225592008-08-18 17:38:53 +08004278
4279leave:
Tao Ma01225592008-08-18 17:38:53 +08004280 return ret;
4281}
4282
4283/*
Joel Becker92de1092008-11-25 17:06:40 -08004284 * We are given an extent. 'first' is the bucket at the very front of
4285 * the extent. The extent has space for an additional bucket past
4286 * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
4287 * of the target bucket. We wish to shift every bucket past the target
4288 * down one, filling in that additional space. When we get back to the
4289 * target, we split the target between itself and the now-empty bucket
4290 * at target+1 (aka, target_blkno + blks_per_bucket).
Tao Ma01225592008-08-18 17:38:53 +08004291 */
4292static int ocfs2_extend_xattr_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004293 handle_t *handle,
Joel Becker92de1092008-11-25 17:06:40 -08004294 struct ocfs2_xattr_bucket *first,
4295 u64 target_blk,
Tao Ma01225592008-08-18 17:38:53 +08004296 u32 num_clusters)
4297{
4298 int ret, credits;
4299 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4300 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Joel Becker92de1092008-11-25 17:06:40 -08004301 u64 end_blk;
4302 u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets);
Tao Ma01225592008-08-18 17:38:53 +08004303
4304 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
Joel Becker92de1092008-11-25 17:06:40 -08004305 "from %llu, len = %u\n", (unsigned long long)target_blk,
4306 (unsigned long long)bucket_blkno(first), num_clusters);
Tao Ma01225592008-08-18 17:38:53 +08004307
Joel Becker92de1092008-11-25 17:06:40 -08004308 /* The extent must have room for an additional bucket */
4309 BUG_ON(new_bucket >=
4310 (num_clusters * ocfs2_xattr_buckets_per_cluster(osb)));
Tao Ma01225592008-08-18 17:38:53 +08004311
Joel Becker92de1092008-11-25 17:06:40 -08004312 /* end_blk points to the last existing bucket */
4313 end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004314
4315 /*
Joel Becker92de1092008-11-25 17:06:40 -08004316 * end_blk is the start of the last existing bucket.
4317 * Thus, (end_blk - target_blk) covers the target bucket and
4318 * every bucket after it up to, but not including, the last
4319 * existing bucket. Then we add the last existing bucket, the
4320 * new bucket, and the first bucket (3 * blk_per_bucket).
Tao Ma01225592008-08-18 17:38:53 +08004321 */
Joel Becker92de1092008-11-25 17:06:40 -08004322 credits = (end_blk - target_blk) + (3 * blk_per_bucket) +
Tao Ma85db90e2008-11-12 08:27:01 +08004323 handle->h_buffer_credits;
4324 ret = ocfs2_extend_trans(handle, credits);
4325 if (ret) {
Tao Ma01225592008-08-18 17:38:53 +08004326 mlog_errno(ret);
4327 goto out;
4328 }
4329
Joel Becker92de1092008-11-25 17:06:40 -08004330 ret = ocfs2_xattr_bucket_journal_access(handle, first,
4331 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004332 if (ret) {
4333 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004334 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004335 }
4336
Joel Becker92de1092008-11-25 17:06:40 -08004337 while (end_blk != target_blk) {
Tao Ma01225592008-08-18 17:38:53 +08004338 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
4339 end_blk + blk_per_bucket, 0);
4340 if (ret)
Tao Ma85db90e2008-11-12 08:27:01 +08004341 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004342 end_blk -= blk_per_bucket;
4343 }
4344
Joel Becker92de1092008-11-25 17:06:40 -08004345 /* Move half of the xattr in target_blkno to the next bucket. */
4346 ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk,
4347 target_blk + blk_per_bucket, NULL, 0);
Tao Ma01225592008-08-18 17:38:53 +08004348
Joel Becker92de1092008-11-25 17:06:40 -08004349 le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1);
4350 ocfs2_xattr_bucket_journal_dirty(handle, first);
Tao Ma01225592008-08-18 17:38:53 +08004351
Tao Ma01225592008-08-18 17:38:53 +08004352out:
4353 return ret;
4354}
4355
4356/*
Joel Becker91f20332008-11-26 15:25:41 -08004357 * Add new xattr bucket in an extent record and adjust the buckets
4358 * accordingly. xb_bh is the ocfs2_xattr_block, and target is the
4359 * bucket we want to insert into.
Tao Ma01225592008-08-18 17:38:53 +08004360 *
Joel Becker91f20332008-11-26 15:25:41 -08004361 * In the easy case, we will move all the buckets after target down by
4362 * one. Half of target's xattrs will be moved to the next bucket.
4363 *
4364 * If current cluster is full, we'll allocate a new one. This may not
4365 * be contiguous. The underlying calls will make sure that there is
4366 * space for the insert, shifting buckets around if necessary.
4367 * 'target' may be moved by those calls.
Tao Ma01225592008-08-18 17:38:53 +08004368 */
4369static int ocfs2_add_new_xattr_bucket(struct inode *inode,
4370 struct buffer_head *xb_bh,
Joel Becker91f20332008-11-26 15:25:41 -08004371 struct ocfs2_xattr_bucket *target,
Tao Ma78f30c32008-11-12 08:27:00 +08004372 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004373{
Tao Ma01225592008-08-18 17:38:53 +08004374 struct ocfs2_xattr_block *xb =
4375 (struct ocfs2_xattr_block *)xb_bh->b_data;
4376 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
4377 struct ocfs2_extent_list *el = &xb_root->xt_list;
Joel Becker91f20332008-11-26 15:25:41 -08004378 u32 name_hash =
4379 le32_to_cpu(bucket_xh(target)->xh_entries[0].xe_name_hash);
Joel Beckered29c0c2008-11-26 15:08:44 -08004380 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08004381 int ret, num_buckets, extend = 1;
4382 u64 p_blkno;
4383 u32 e_cpos, num_clusters;
Joel Becker92de1092008-11-25 17:06:40 -08004384 /* The bucket at the front of the extent */
Joel Becker91f20332008-11-26 15:25:41 -08004385 struct ocfs2_xattr_bucket *first;
Tao Ma01225592008-08-18 17:38:53 +08004386
Joel Becker91f20332008-11-26 15:25:41 -08004387 mlog(0, "Add new xattr bucket starting from %llu\n",
4388 (unsigned long long)bucket_blkno(target));
Tao Ma01225592008-08-18 17:38:53 +08004389
Joel Beckered29c0c2008-11-26 15:08:44 -08004390 /* The first bucket of the original extent */
Joel Becker92de1092008-11-25 17:06:40 -08004391 first = ocfs2_xattr_bucket_new(inode);
Joel Becker91f20332008-11-26 15:25:41 -08004392 if (!first) {
Joel Becker92de1092008-11-25 17:06:40 -08004393 ret = -ENOMEM;
4394 mlog_errno(ret);
4395 goto out;
4396 }
4397
Tao Ma01225592008-08-18 17:38:53 +08004398 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
4399 &num_clusters, el);
4400 if (ret) {
4401 mlog_errno(ret);
4402 goto out;
4403 }
4404
Joel Beckered29c0c2008-11-26 15:08:44 -08004405 ret = ocfs2_read_xattr_bucket(first, p_blkno);
4406 if (ret) {
4407 mlog_errno(ret);
4408 goto out;
4409 }
4410
Tao Ma01225592008-08-18 17:38:53 +08004411 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
Joel Beckered29c0c2008-11-26 15:08:44 -08004412 if (num_buckets == le16_to_cpu(bucket_xh(first)->xh_num_buckets)) {
4413 /*
4414 * This can move first+target if the target bucket moves
4415 * to the new extent.
4416 */
Tao Ma01225592008-08-18 17:38:53 +08004417 ret = ocfs2_add_new_xattr_cluster(inode,
4418 xb_bh,
Joel Beckered29c0c2008-11-26 15:08:44 -08004419 first,
4420 target,
Tao Ma01225592008-08-18 17:38:53 +08004421 &num_clusters,
4422 e_cpos,
Tao Ma78f30c32008-11-12 08:27:00 +08004423 &extend,
4424 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004425 if (ret) {
4426 mlog_errno(ret);
4427 goto out;
4428 }
4429 }
4430
Joel Becker92de1092008-11-25 17:06:40 -08004431 if (extend) {
Tao Ma01225592008-08-18 17:38:53 +08004432 ret = ocfs2_extend_xattr_bucket(inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004433 ctxt->handle,
Joel Beckered29c0c2008-11-26 15:08:44 -08004434 first,
4435 bucket_blkno(target),
Tao Ma01225592008-08-18 17:38:53 +08004436 num_clusters);
Joel Becker92de1092008-11-25 17:06:40 -08004437 if (ret)
4438 mlog_errno(ret);
4439 }
4440
Tao Ma01225592008-08-18 17:38:53 +08004441out:
Joel Becker92de1092008-11-25 17:06:40 -08004442 ocfs2_xattr_bucket_free(first);
Joel Beckered29c0c2008-11-26 15:08:44 -08004443
Tao Ma01225592008-08-18 17:38:53 +08004444 return ret;
4445}
4446
4447static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
4448 struct ocfs2_xattr_bucket *bucket,
4449 int offs)
4450{
4451 int block_off = offs >> inode->i_sb->s_blocksize_bits;
4452
4453 offs = offs % inode->i_sb->s_blocksize;
Joel Becker51def392008-10-24 16:57:21 -07004454 return bucket_block(bucket, block_off) + offs;
Tao Ma01225592008-08-18 17:38:53 +08004455}
4456
4457/*
4458 * Handle the normal xattr set, including replace, delete and new.
Tao Ma01225592008-08-18 17:38:53 +08004459 *
4460 * Note: "local" indicates the real data's locality. So we can't
4461 * just its bucket locality by its length.
4462 */
4463static void ocfs2_xattr_set_entry_normal(struct inode *inode,
4464 struct ocfs2_xattr_info *xi,
4465 struct ocfs2_xattr_search *xs,
4466 u32 name_hash,
Tao Ma5a0956112008-09-19 22:17:41 +08004467 int local)
Tao Ma01225592008-08-18 17:38:53 +08004468{
4469 struct ocfs2_xattr_entry *last, *xe;
4470 int name_len = strlen(xi->name);
4471 struct ocfs2_xattr_header *xh = xs->header;
4472 u16 count = le16_to_cpu(xh->xh_count), start;
4473 size_t blocksize = inode->i_sb->s_blocksize;
4474 char *val;
4475 size_t offs, size, new_size;
4476
4477 last = &xh->xh_entries[count];
4478 if (!xs->not_found) {
4479 xe = xs->here;
4480 offs = le16_to_cpu(xe->xe_name_offset);
4481 if (ocfs2_xattr_is_local(xe))
4482 size = OCFS2_XATTR_SIZE(name_len) +
4483 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4484 else
4485 size = OCFS2_XATTR_SIZE(name_len) +
4486 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4487
4488 /*
4489 * If the new value will be stored outside, xi->value has been
4490 * initalized as an empty ocfs2_xattr_value_root, and the same
4491 * goes with xi->value_len, so we can set new_size safely here.
4492 * See ocfs2_xattr_set_in_bucket.
4493 */
4494 new_size = OCFS2_XATTR_SIZE(name_len) +
4495 OCFS2_XATTR_SIZE(xi->value_len);
4496
4497 le16_add_cpu(&xh->xh_name_value_len, -size);
4498 if (xi->value) {
4499 if (new_size > size)
4500 goto set_new_name_value;
4501
4502 /* Now replace the old value with new one. */
4503 if (local)
4504 xe->xe_value_size = cpu_to_le64(xi->value_len);
4505 else
4506 xe->xe_value_size = 0;
4507
4508 val = ocfs2_xattr_bucket_get_val(inode,
Joel Beckerba937122008-10-24 19:13:20 -07004509 xs->bucket, offs);
Tao Ma01225592008-08-18 17:38:53 +08004510 memset(val + OCFS2_XATTR_SIZE(name_len), 0,
4511 size - OCFS2_XATTR_SIZE(name_len));
4512 if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
4513 memcpy(val + OCFS2_XATTR_SIZE(name_len),
4514 xi->value, xi->value_len);
4515
4516 le16_add_cpu(&xh->xh_name_value_len, new_size);
4517 ocfs2_xattr_set_local(xe, local);
4518 return;
4519 } else {
Tao Ma5a0956112008-09-19 22:17:41 +08004520 /*
4521 * Remove the old entry if there is more than one.
4522 * We don't remove the last entry so that we can
4523 * use it to indicate the hash value of the empty
4524 * bucket.
4525 */
Tao Ma01225592008-08-18 17:38:53 +08004526 last -= 1;
Tao Ma01225592008-08-18 17:38:53 +08004527 le16_add_cpu(&xh->xh_count, -1);
Tao Ma5a0956112008-09-19 22:17:41 +08004528 if (xh->xh_count) {
4529 memmove(xe, xe + 1,
4530 (void *)last - (void *)xe);
4531 memset(last, 0,
4532 sizeof(struct ocfs2_xattr_entry));
4533 } else
4534 xh->xh_free_start =
4535 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4536
Tao Ma01225592008-08-18 17:38:53 +08004537 return;
4538 }
4539 } else {
4540 /* find a new entry for insert. */
4541 int low = 0, high = count - 1, tmp;
4542 struct ocfs2_xattr_entry *tmp_xe;
4543
Tao Ma5a0956112008-09-19 22:17:41 +08004544 while (low <= high && count) {
Tao Ma01225592008-08-18 17:38:53 +08004545 tmp = (low + high) / 2;
4546 tmp_xe = &xh->xh_entries[tmp];
4547
4548 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
4549 low = tmp + 1;
4550 else if (name_hash <
4551 le32_to_cpu(tmp_xe->xe_name_hash))
4552 high = tmp - 1;
Tao Ma06b240d2008-09-19 22:16:34 +08004553 else {
4554 low = tmp;
Tao Ma01225592008-08-18 17:38:53 +08004555 break;
Tao Ma06b240d2008-09-19 22:16:34 +08004556 }
Tao Ma01225592008-08-18 17:38:53 +08004557 }
4558
4559 xe = &xh->xh_entries[low];
4560 if (low != count)
4561 memmove(xe + 1, xe, (void *)last - (void *)xe);
4562
4563 le16_add_cpu(&xh->xh_count, 1);
4564 memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
4565 xe->xe_name_hash = cpu_to_le32(name_hash);
4566 xe->xe_name_len = name_len;
4567 ocfs2_xattr_set_type(xe, xi->name_index);
4568 }
4569
4570set_new_name_value:
4571 /* Insert the new name+value. */
4572 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
4573
4574 /*
4575 * We must make sure that the name/value pair
4576 * exists in the same block.
4577 */
4578 offs = le16_to_cpu(xh->xh_free_start);
4579 start = offs - size;
4580
4581 if (start >> inode->i_sb->s_blocksize_bits !=
4582 (offs - 1) >> inode->i_sb->s_blocksize_bits) {
4583 offs = offs - offs % blocksize;
4584 xh->xh_free_start = cpu_to_le16(offs);
4585 }
4586
Joel Beckerba937122008-10-24 19:13:20 -07004587 val = ocfs2_xattr_bucket_get_val(inode, xs->bucket, offs - size);
Tao Ma01225592008-08-18 17:38:53 +08004588 xe->xe_name_offset = cpu_to_le16(offs - size);
4589
4590 memset(val, 0, size);
4591 memcpy(val, xi->name, name_len);
4592 memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
4593
4594 xe->xe_value_size = cpu_to_le64(xi->value_len);
4595 ocfs2_xattr_set_local(xe, local);
4596 xs->here = xe;
4597 le16_add_cpu(&xh->xh_free_start, -size);
4598 le16_add_cpu(&xh->xh_name_value_len, size);
4599
4600 return;
4601}
4602
Tao Ma01225592008-08-18 17:38:53 +08004603/*
4604 * Set the xattr entry in the specified bucket.
4605 * The bucket is indicated by xs->bucket and it should have the enough
4606 * space for the xattr insertion.
4607 */
4608static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004609 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004610 struct ocfs2_xattr_info *xi,
4611 struct ocfs2_xattr_search *xs,
4612 u32 name_hash,
Tao Ma5a0956112008-09-19 22:17:41 +08004613 int local)
Tao Ma01225592008-08-18 17:38:53 +08004614{
Joel Becker1224be02008-10-24 18:47:33 -07004615 int ret;
Joel Becker02dbf382008-10-27 18:07:45 -07004616 u64 blkno;
Tao Ma01225592008-08-18 17:38:53 +08004617
Mark Fashehff1ec202008-08-19 10:54:29 -07004618 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4619 (unsigned long)xi->value_len, xi->name_index,
Joel Beckerba937122008-10-24 19:13:20 -07004620 (unsigned long long)bucket_blkno(xs->bucket));
Tao Ma01225592008-08-18 17:38:53 +08004621
Joel Beckerba937122008-10-24 19:13:20 -07004622 if (!xs->bucket->bu_bhs[1]) {
Joel Becker02dbf382008-10-27 18:07:45 -07004623 blkno = bucket_blkno(xs->bucket);
4624 ocfs2_xattr_bucket_relse(xs->bucket);
4625 ret = ocfs2_read_xattr_bucket(xs->bucket, blkno);
Tao Ma01225592008-08-18 17:38:53 +08004626 if (ret) {
4627 mlog_errno(ret);
4628 goto out;
4629 }
4630 }
4631
Joel Beckerba937122008-10-24 19:13:20 -07004632 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004633 OCFS2_JOURNAL_ACCESS_WRITE);
4634 if (ret < 0) {
4635 mlog_errno(ret);
4636 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004637 }
4638
Tao Ma5a0956112008-09-19 22:17:41 +08004639 ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
Joel Beckerba937122008-10-24 19:13:20 -07004640 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004641
Tao Ma01225592008-08-18 17:38:53 +08004642out:
Tao Ma01225592008-08-18 17:38:53 +08004643 return ret;
4644}
4645
Tao Ma01225592008-08-18 17:38:53 +08004646/*
4647 * Truncate the specified xe_off entry in xattr bucket.
4648 * bucket is indicated by header_bh and len is the new length.
4649 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4650 *
4651 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4652 */
4653static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
Joel Becker548b0f22008-11-24 19:32:13 -08004654 struct ocfs2_xattr_bucket *bucket,
Tao Ma01225592008-08-18 17:38:53 +08004655 int xe_off,
Tao Ma78f30c32008-11-12 08:27:00 +08004656 int len,
4657 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004658{
4659 int ret, offset;
4660 u64 value_blk;
4661 struct buffer_head *value_bh = NULL;
4662 struct ocfs2_xattr_value_root *xv;
4663 struct ocfs2_xattr_entry *xe;
Joel Becker548b0f22008-11-24 19:32:13 -08004664 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma01225592008-08-18 17:38:53 +08004665 size_t blocksize = inode->i_sb->s_blocksize;
4666
4667 xe = &xh->xh_entries[xe_off];
4668
4669 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
4670
4671 offset = le16_to_cpu(xe->xe_name_offset) +
4672 OCFS2_XATTR_SIZE(xe->xe_name_len);
4673
4674 value_blk = offset / blocksize;
4675
4676 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4677 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
Tao Ma01225592008-08-18 17:38:53 +08004678
Joel Becker548b0f22008-11-24 19:32:13 -08004679 value_bh = bucket->bu_bhs[value_blk];
4680 BUG_ON(!value_bh);
Tao Ma01225592008-08-18 17:38:53 +08004681
4682 xv = (struct ocfs2_xattr_value_root *)
4683 (value_bh->b_data + offset % blocksize);
4684
Joel Becker548b0f22008-11-24 19:32:13 -08004685 ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
4686 OCFS2_JOURNAL_ACCESS_WRITE);
4687 if (ret) {
4688 mlog_errno(ret);
4689 goto out;
4690 }
4691
4692 /*
4693 * From here on out we have to dirty the bucket. The generic
4694 * value calls only modify one of the bucket's bhs, but we need
4695 * to send the bucket at once. So if they error, they *could* have
4696 * modified something. We have to assume they did, and dirty
4697 * the whole bucket. This leaves us in a consistent state.
4698 */
Tao Ma01225592008-08-18 17:38:53 +08004699 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
Joel Becker548b0f22008-11-24 19:32:13 -08004700 xe_off, (unsigned long long)bucket_blkno(bucket), len);
Tao Ma78f30c32008-11-12 08:27:00 +08004701 ret = ocfs2_xattr_value_truncate(inode, value_bh, xv, len, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004702 if (ret) {
4703 mlog_errno(ret);
Joel Becker548b0f22008-11-24 19:32:13 -08004704 goto out_dirty;
Tao Ma01225592008-08-18 17:38:53 +08004705 }
4706
Joel Becker548b0f22008-11-24 19:32:13 -08004707 xe->xe_value_size = cpu_to_le64(len);
4708
4709out_dirty:
4710 ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket);
Tao Ma01225592008-08-18 17:38:53 +08004711
4712out:
Tao Ma01225592008-08-18 17:38:53 +08004713 return ret;
4714}
4715
4716static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +08004717 struct ocfs2_xattr_search *xs,
4718 int len,
4719 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004720{
4721 int ret, offset;
4722 struct ocfs2_xattr_entry *xe = xs->here;
4723 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
4724
Joel Beckerba937122008-10-24 19:13:20 -07004725 BUG_ON(!xs->bucket->bu_bhs[0] || !xe || ocfs2_xattr_is_local(xe));
Tao Ma01225592008-08-18 17:38:53 +08004726
4727 offset = xe - xh->xh_entries;
Joel Becker548b0f22008-11-24 19:32:13 -08004728 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket,
Tao Ma78f30c32008-11-12 08:27:00 +08004729 offset, len, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004730 if (ret)
4731 mlog_errno(ret);
4732
4733 return ret;
4734}
4735
4736static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004737 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004738 struct ocfs2_xattr_search *xs,
4739 char *val,
4740 int value_len)
4741{
4742 int offset;
4743 struct ocfs2_xattr_value_root *xv;
4744 struct ocfs2_xattr_entry *xe = xs->here;
4745
4746 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
4747
4748 offset = le16_to_cpu(xe->xe_name_offset) +
4749 OCFS2_XATTR_SIZE(xe->xe_name_len);
4750
4751 xv = (struct ocfs2_xattr_value_root *)(xs->base + offset);
4752
Tao Ma85db90e2008-11-12 08:27:01 +08004753 return __ocfs2_xattr_set_value_outside(inode, handle,
4754 xv, val, value_len);
Tao Ma01225592008-08-18 17:38:53 +08004755}
4756
Tao Ma01225592008-08-18 17:38:53 +08004757static int ocfs2_rm_xattr_cluster(struct inode *inode,
4758 struct buffer_head *root_bh,
4759 u64 blkno,
4760 u32 cpos,
4761 u32 len)
4762{
4763 int ret;
4764 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4765 struct inode *tl_inode = osb->osb_tl_inode;
4766 handle_t *handle;
4767 struct ocfs2_xattr_block *xb =
4768 (struct ocfs2_xattr_block *)root_bh->b_data;
Tao Ma01225592008-08-18 17:38:53 +08004769 struct ocfs2_alloc_context *meta_ac = NULL;
4770 struct ocfs2_cached_dealloc_ctxt dealloc;
Joel Beckerf99b9b72008-08-20 19:36:33 -07004771 struct ocfs2_extent_tree et;
4772
Joel Becker8d6220d2008-08-22 12:46:09 -07004773 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
Tao Ma01225592008-08-18 17:38:53 +08004774
4775 ocfs2_init_dealloc_ctxt(&dealloc);
4776
4777 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
4778 cpos, len, (unsigned long long)blkno);
4779
4780 ocfs2_remove_xattr_clusters_from_cache(inode, blkno, len);
4781
Joel Beckerf99b9b72008-08-20 19:36:33 -07004782 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08004783 if (ret) {
4784 mlog_errno(ret);
4785 return ret;
4786 }
4787
4788 mutex_lock(&tl_inode->i_mutex);
4789
4790 if (ocfs2_truncate_log_needs_flush(osb)) {
4791 ret = __ocfs2_flush_truncate_log(osb);
4792 if (ret < 0) {
4793 mlog_errno(ret);
4794 goto out;
4795 }
4796 }
4797
Jan Karaa90714c2008-10-09 19:38:40 +02004798 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
Tao Mad3264792008-10-24 07:57:28 +08004799 if (IS_ERR(handle)) {
Tao Ma01225592008-08-18 17:38:53 +08004800 ret = -ENOMEM;
4801 mlog_errno(ret);
4802 goto out;
4803 }
4804
4805 ret = ocfs2_journal_access(handle, inode, root_bh,
4806 OCFS2_JOURNAL_ACCESS_WRITE);
4807 if (ret) {
4808 mlog_errno(ret);
4809 goto out_commit;
4810 }
4811
Joel Beckerf99b9b72008-08-20 19:36:33 -07004812 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
4813 &dealloc);
Tao Ma01225592008-08-18 17:38:53 +08004814 if (ret) {
4815 mlog_errno(ret);
4816 goto out_commit;
4817 }
4818
4819 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
4820
4821 ret = ocfs2_journal_dirty(handle, root_bh);
4822 if (ret) {
4823 mlog_errno(ret);
4824 goto out_commit;
4825 }
4826
4827 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
4828 if (ret)
4829 mlog_errno(ret);
4830
4831out_commit:
4832 ocfs2_commit_trans(osb, handle);
4833out:
4834 ocfs2_schedule_truncate_log_flush(osb, 1);
4835
4836 mutex_unlock(&tl_inode->i_mutex);
4837
4838 if (meta_ac)
4839 ocfs2_free_alloc_context(meta_ac);
4840
4841 ocfs2_run_deallocs(osb, &dealloc);
4842
4843 return ret;
4844}
4845
Tao Ma01225592008-08-18 17:38:53 +08004846static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004847 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004848 struct ocfs2_xattr_search *xs)
4849{
Joel Beckerba937122008-10-24 19:13:20 -07004850 struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004851 struct ocfs2_xattr_entry *last = &xh->xh_entries[
4852 le16_to_cpu(xh->xh_count) - 1];
4853 int ret = 0;
4854
Joel Beckerba937122008-10-24 19:13:20 -07004855 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004856 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004857 if (ret) {
4858 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004859 return;
Tao Ma01225592008-08-18 17:38:53 +08004860 }
4861
4862 /* Remove the old entry. */
4863 memmove(xs->here, xs->here + 1,
4864 (void *)last - (void *)xs->here);
4865 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
4866 le16_add_cpu(&xh->xh_count, -1);
4867
Joel Beckerba937122008-10-24 19:13:20 -07004868 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004869}
4870
4871/*
4872 * Set the xattr name/value in the bucket specified in xs.
4873 *
4874 * As the new value in xi may be stored in the bucket or in an outside cluster,
4875 * we divide the whole process into 3 steps:
4876 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
4877 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
4878 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
4879 * 4. If the clusters for the new outside value can't be allocated, we need
4880 * to free the xattr we allocated in set.
4881 */
4882static int ocfs2_xattr_set_in_bucket(struct inode *inode,
4883 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08004884 struct ocfs2_xattr_search *xs,
4885 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004886{
Tao Ma5a0956112008-09-19 22:17:41 +08004887 int ret, local = 1;
Tao Ma01225592008-08-18 17:38:53 +08004888 size_t value_len;
4889 char *val = (char *)xi->value;
4890 struct ocfs2_xattr_entry *xe = xs->here;
Tao Ma2057e5c2008-10-09 23:06:13 +08004891 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name,
4892 strlen(xi->name));
Tao Ma01225592008-08-18 17:38:53 +08004893
4894 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
4895 /*
4896 * We need to truncate the xattr storage first.
4897 *
4898 * If both the old and new value are stored to
4899 * outside block, we only need to truncate
4900 * the storage and then set the value outside.
4901 *
4902 * If the new value should be stored within block,
4903 * we should free all the outside block first and
4904 * the modification to the xattr block will be done
4905 * by following steps.
4906 */
4907 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4908 value_len = xi->value_len;
4909 else
4910 value_len = 0;
4911
4912 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
Tao Ma78f30c32008-11-12 08:27:00 +08004913 value_len,
4914 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004915 if (ret)
4916 goto out;
4917
4918 if (value_len)
4919 goto set_value_outside;
4920 }
4921
4922 value_len = xi->value_len;
4923 /* So we have to handle the inside block change now. */
4924 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
4925 /*
4926 * If the new value will be stored outside of block,
4927 * initalize a new empty value root and insert it first.
4928 */
4929 local = 0;
4930 xi->value = &def_xv;
4931 xi->value_len = OCFS2_XATTR_ROOT_SIZE;
4932 }
4933
Tao Ma85db90e2008-11-12 08:27:01 +08004934 ret = ocfs2_xattr_set_entry_in_bucket(inode, ctxt->handle, xi, xs,
4935 name_hash, local);
Tao Ma01225592008-08-18 17:38:53 +08004936 if (ret) {
4937 mlog_errno(ret);
4938 goto out;
4939 }
4940
Tao Ma5a0956112008-09-19 22:17:41 +08004941 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
4942 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004943
Tao Ma5a0956112008-09-19 22:17:41 +08004944 /* allocate the space now for the outside block storage. */
4945 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
Tao Ma78f30c32008-11-12 08:27:00 +08004946 value_len, ctxt);
Tao Ma5a0956112008-09-19 22:17:41 +08004947 if (ret) {
4948 mlog_errno(ret);
4949
4950 if (xs->not_found) {
4951 /*
4952 * We can't allocate enough clusters for outside
4953 * storage and we have allocated xattr already,
4954 * so need to remove it.
4955 */
Tao Ma85db90e2008-11-12 08:27:01 +08004956 ocfs2_xattr_bucket_remove_xs(inode, ctxt->handle, xs);
Tao Ma01225592008-08-18 17:38:53 +08004957 }
Tao Ma01225592008-08-18 17:38:53 +08004958 goto out;
4959 }
4960
4961set_value_outside:
Tao Ma85db90e2008-11-12 08:27:01 +08004962 ret = ocfs2_xattr_bucket_set_value_outside(inode, ctxt->handle,
4963 xs, val, value_len);
Tao Ma01225592008-08-18 17:38:53 +08004964out:
4965 return ret;
4966}
4967
Tao Ma80bcaf32008-10-27 06:06:24 +08004968/*
4969 * check whether the xattr bucket is filled up with the same hash value.
4970 * If we want to insert the xattr with the same hash, return -ENOSPC.
4971 * If we want to insert a xattr with different hash value, go ahead
4972 * and ocfs2_divide_xattr_bucket will handle this.
4973 */
Tao Ma01225592008-08-18 17:38:53 +08004974static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
Tao Ma80bcaf32008-10-27 06:06:24 +08004975 struct ocfs2_xattr_bucket *bucket,
4976 const char *name)
Tao Ma01225592008-08-18 17:38:53 +08004977{
Joel Becker3e632942008-10-24 17:04:49 -07004978 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08004979 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
4980
4981 if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
4982 return 0;
Tao Ma01225592008-08-18 17:38:53 +08004983
4984 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
4985 xh->xh_entries[0].xe_name_hash) {
4986 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
4987 "hash = %u\n",
Joel Becker9c7759a2008-10-24 16:21:03 -07004988 (unsigned long long)bucket_blkno(bucket),
Tao Ma01225592008-08-18 17:38:53 +08004989 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
4990 return -ENOSPC;
4991 }
4992
4993 return 0;
4994}
4995
4996static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
4997 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08004998 struct ocfs2_xattr_search *xs,
4999 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005000{
5001 struct ocfs2_xattr_header *xh;
5002 struct ocfs2_xattr_entry *xe;
5003 u16 count, header_size, xh_free_start;
Joel Becker6dde41d2008-10-24 17:16:48 -07005004 int free, max_free, need, old;
Tao Ma01225592008-08-18 17:38:53 +08005005 size_t value_size = 0, name_len = strlen(xi->name);
5006 size_t blocksize = inode->i_sb->s_blocksize;
5007 int ret, allocation = 0;
Tao Ma01225592008-08-18 17:38:53 +08005008
5009 mlog_entry("Set xattr %s in xattr index block\n", xi->name);
5010
5011try_again:
5012 xh = xs->header;
5013 count = le16_to_cpu(xh->xh_count);
5014 xh_free_start = le16_to_cpu(xh->xh_free_start);
5015 header_size = sizeof(struct ocfs2_xattr_header) +
5016 count * sizeof(struct ocfs2_xattr_entry);
5017 max_free = OCFS2_XATTR_BUCKET_SIZE -
5018 le16_to_cpu(xh->xh_name_value_len) - header_size;
5019
5020 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
5021 "of %u which exceed block size\n",
Joel Beckerba937122008-10-24 19:13:20 -07005022 (unsigned long long)bucket_blkno(xs->bucket),
Tao Ma01225592008-08-18 17:38:53 +08005023 header_size);
5024
5025 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
5026 value_size = OCFS2_XATTR_ROOT_SIZE;
5027 else if (xi->value)
5028 value_size = OCFS2_XATTR_SIZE(xi->value_len);
5029
5030 if (xs->not_found)
5031 need = sizeof(struct ocfs2_xattr_entry) +
5032 OCFS2_XATTR_SIZE(name_len) + value_size;
5033 else {
5034 need = value_size + OCFS2_XATTR_SIZE(name_len);
5035
5036 /*
5037 * We only replace the old value if the new length is smaller
5038 * than the old one. Otherwise we will allocate new space in the
5039 * bucket to store it.
5040 */
5041 xe = xs->here;
5042 if (ocfs2_xattr_is_local(xe))
5043 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
5044 else
5045 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
5046
5047 if (old >= value_size)
5048 need = 0;
5049 }
5050
5051 free = xh_free_start - header_size;
5052 /*
5053 * We need to make sure the new name/value pair
5054 * can exist in the same block.
5055 */
5056 if (xh_free_start % blocksize < need)
5057 free -= xh_free_start % blocksize;
5058
5059 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
5060 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
5061 " %u\n", xs->not_found,
Joel Beckerba937122008-10-24 19:13:20 -07005062 (unsigned long long)bucket_blkno(xs->bucket),
Tao Ma01225592008-08-18 17:38:53 +08005063 free, need, max_free, le16_to_cpu(xh->xh_free_start),
5064 le16_to_cpu(xh->xh_name_value_len));
5065
Tao Ma976331d2008-11-12 08:26:57 +08005066 if (free < need ||
5067 (xs->not_found &&
5068 count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb))) {
Tao Ma01225592008-08-18 17:38:53 +08005069 if (need <= max_free &&
5070 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
5071 /*
5072 * We can create the space by defragment. Since only the
5073 * name/value will be moved, the xe shouldn't be changed
5074 * in xs.
5075 */
Tao Ma85db90e2008-11-12 08:27:01 +08005076 ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
5077 xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08005078 if (ret) {
5079 mlog_errno(ret);
5080 goto out;
5081 }
5082
5083 xh_free_start = le16_to_cpu(xh->xh_free_start);
5084 free = xh_free_start - header_size;
5085 if (xh_free_start % blocksize < need)
5086 free -= xh_free_start % blocksize;
5087
5088 if (free >= need)
5089 goto xattr_set;
5090
5091 mlog(0, "Can't get enough space for xattr insert by "
5092 "defragment. Need %u bytes, but we have %d, so "
5093 "allocate new bucket for it.\n", need, free);
5094 }
5095
5096 /*
5097 * We have to add new buckets or clusters and one
5098 * allocation should leave us enough space for insert.
5099 */
5100 BUG_ON(allocation);
5101
5102 /*
5103 * We do not allow for overlapping ranges between buckets. And
5104 * the maximum number of collisions we will allow for then is
5105 * one bucket's worth, so check it here whether we need to
5106 * add a new bucket for the insert.
5107 */
Tao Ma80bcaf32008-10-27 06:06:24 +08005108 ret = ocfs2_check_xattr_bucket_collision(inode,
Joel Beckerba937122008-10-24 19:13:20 -07005109 xs->bucket,
Tao Ma80bcaf32008-10-27 06:06:24 +08005110 xi->name);
Tao Ma01225592008-08-18 17:38:53 +08005111 if (ret) {
5112 mlog_errno(ret);
5113 goto out;
5114 }
5115
5116 ret = ocfs2_add_new_xattr_bucket(inode,
5117 xs->xattr_bh,
Joel Becker91f20332008-11-26 15:25:41 -08005118 xs->bucket,
Tao Ma78f30c32008-11-12 08:27:00 +08005119 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005120 if (ret) {
5121 mlog_errno(ret);
5122 goto out;
5123 }
5124
Joel Becker91f20332008-11-26 15:25:41 -08005125 /*
5126 * ocfs2_add_new_xattr_bucket() will have updated
5127 * xs->bucket if it moved, but it will not have updated
5128 * any of the other search fields. Thus, we drop it and
5129 * re-search. Everything should be cached, so it'll be
5130 * quick.
5131 */
Joel Beckerba937122008-10-24 19:13:20 -07005132 ocfs2_xattr_bucket_relse(xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08005133 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
5134 xi->name_index,
5135 xi->name, xs);
5136 if (ret && ret != -ENODATA)
5137 goto out;
5138 xs->not_found = ret;
5139 allocation = 1;
5140 goto try_again;
5141 }
5142
5143xattr_set:
Tao Ma78f30c32008-11-12 08:27:00 +08005144 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005145out:
5146 mlog_exit(ret);
5147 return ret;
5148}
Tao Maa3944252008-08-18 17:38:54 +08005149
5150static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
5151 struct ocfs2_xattr_bucket *bucket,
5152 void *para)
5153{
5154 int ret = 0;
Joel Becker3e632942008-10-24 17:04:49 -07005155 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Maa3944252008-08-18 17:38:54 +08005156 u16 i;
5157 struct ocfs2_xattr_entry *xe;
Tao Ma78f30c32008-11-12 08:27:00 +08005158 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5159 struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
Joel Becker548b0f22008-11-24 19:32:13 -08005160 int credits = ocfs2_remove_extent_credits(osb->sb) +
5161 ocfs2_blocks_per_xattr_bucket(inode->i_sb);
5162
Tao Ma78f30c32008-11-12 08:27:00 +08005163
5164 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
Tao Maa3944252008-08-18 17:38:54 +08005165
5166 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
5167 xe = &xh->xh_entries[i];
5168 if (ocfs2_xattr_is_local(xe))
5169 continue;
5170
Tao Ma88c3b062008-12-11 08:54:11 +08005171 ctxt.handle = ocfs2_start_trans(osb, credits);
5172 if (IS_ERR(ctxt.handle)) {
5173 ret = PTR_ERR(ctxt.handle);
5174 mlog_errno(ret);
5175 break;
5176 }
5177
Joel Becker548b0f22008-11-24 19:32:13 -08005178 ret = ocfs2_xattr_bucket_value_truncate(inode, bucket,
Tao Ma78f30c32008-11-12 08:27:00 +08005179 i, 0, &ctxt);
Tao Ma88c3b062008-12-11 08:54:11 +08005180
5181 ocfs2_commit_trans(osb, ctxt.handle);
Tao Maa3944252008-08-18 17:38:54 +08005182 if (ret) {
5183 mlog_errno(ret);
5184 break;
5185 }
5186 }
5187
Tao Ma78f30c32008-11-12 08:27:00 +08005188 ocfs2_schedule_truncate_log_flush(osb, 1);
5189 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tao Maa3944252008-08-18 17:38:54 +08005190 return ret;
5191}
5192
5193static int ocfs2_delete_xattr_index_block(struct inode *inode,
5194 struct buffer_head *xb_bh)
5195{
5196 struct ocfs2_xattr_block *xb =
5197 (struct ocfs2_xattr_block *)xb_bh->b_data;
5198 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
5199 int ret = 0;
5200 u32 name_hash = UINT_MAX, e_cpos, num_clusters;
5201 u64 p_blkno;
5202
5203 if (le16_to_cpu(el->l_next_free_rec) == 0)
5204 return 0;
5205
5206 while (name_hash > 0) {
5207 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
5208 &e_cpos, &num_clusters, el);
5209 if (ret) {
5210 mlog_errno(ret);
5211 goto out;
5212 }
5213
5214 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
5215 ocfs2_delete_xattr_in_bucket,
5216 NULL);
5217 if (ret) {
5218 mlog_errno(ret);
5219 goto out;
5220 }
5221
5222 ret = ocfs2_rm_xattr_cluster(inode, xb_bh,
5223 p_blkno, e_cpos, num_clusters);
5224 if (ret) {
5225 mlog_errno(ret);
5226 break;
5227 }
5228
5229 if (e_cpos == 0)
5230 break;
5231
5232 name_hash = e_cpos - 1;
5233 }
5234
5235out:
5236 return ret;
5237}
Mark Fasheh99219ae2008-10-07 14:52:59 -07005238
5239/*
Tiger Yang923f7f32008-11-14 11:16:27 +08005240 * 'security' attributes support
5241 */
5242static size_t ocfs2_xattr_security_list(struct inode *inode, char *list,
5243 size_t list_size, const char *name,
5244 size_t name_len)
5245{
5246 const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN;
5247 const size_t total_len = prefix_len + name_len + 1;
5248
5249 if (list && total_len <= list_size) {
5250 memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
5251 memcpy(list + prefix_len, name, name_len);
5252 list[prefix_len + name_len] = '\0';
5253 }
5254 return total_len;
5255}
5256
5257static int ocfs2_xattr_security_get(struct inode *inode, const char *name,
5258 void *buffer, size_t size)
5259{
5260 if (strcmp(name, "") == 0)
5261 return -EINVAL;
5262 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_SECURITY, name,
5263 buffer, size);
5264}
5265
5266static int ocfs2_xattr_security_set(struct inode *inode, const char *name,
5267 const void *value, size_t size, int flags)
5268{
5269 if (strcmp(name, "") == 0)
5270 return -EINVAL;
5271
5272 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY, name, value,
5273 size, flags);
5274}
5275
Tiger Yang534eadd2008-11-14 11:16:41 +08005276int ocfs2_init_security_get(struct inode *inode,
5277 struct inode *dir,
5278 struct ocfs2_security_xattr_info *si)
5279{
5280 return security_inode_init_security(inode, dir, &si->name, &si->value,
5281 &si->value_len);
5282}
5283
5284int ocfs2_init_security_set(handle_t *handle,
5285 struct inode *inode,
5286 struct buffer_head *di_bh,
5287 struct ocfs2_security_xattr_info *si,
5288 struct ocfs2_alloc_context *xattr_ac,
5289 struct ocfs2_alloc_context *data_ac)
5290{
5291 return ocfs2_xattr_set_handle(handle, inode, di_bh,
5292 OCFS2_XATTR_INDEX_SECURITY,
5293 si->name, si->value, si->value_len, 0,
5294 xattr_ac, data_ac);
5295}
5296
Tiger Yang923f7f32008-11-14 11:16:27 +08005297struct xattr_handler ocfs2_xattr_security_handler = {
5298 .prefix = XATTR_SECURITY_PREFIX,
5299 .list = ocfs2_xattr_security_list,
5300 .get = ocfs2_xattr_security_get,
5301 .set = ocfs2_xattr_security_set,
5302};
5303
5304/*
Mark Fasheh99219ae2008-10-07 14:52:59 -07005305 * 'trusted' attributes support
5306 */
Mark Fasheh99219ae2008-10-07 14:52:59 -07005307static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list,
5308 size_t list_size, const char *name,
5309 size_t name_len)
5310{
Tiger Yangceb1eba2008-10-23 16:34:13 +08005311 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
Mark Fasheh99219ae2008-10-07 14:52:59 -07005312 const size_t total_len = prefix_len + name_len + 1;
5313
5314 if (list && total_len <= list_size) {
5315 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
5316 memcpy(list + prefix_len, name, name_len);
5317 list[prefix_len + name_len] = '\0';
5318 }
5319 return total_len;
5320}
5321
5322static int ocfs2_xattr_trusted_get(struct inode *inode, const char *name,
5323 void *buffer, size_t size)
5324{
5325 if (strcmp(name, "") == 0)
5326 return -EINVAL;
5327 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, name,
5328 buffer, size);
5329}
5330
5331static int ocfs2_xattr_trusted_set(struct inode *inode, const char *name,
5332 const void *value, size_t size, int flags)
5333{
5334 if (strcmp(name, "") == 0)
5335 return -EINVAL;
5336
5337 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, name, value,
5338 size, flags);
5339}
5340
5341struct xattr_handler ocfs2_xattr_trusted_handler = {
5342 .prefix = XATTR_TRUSTED_PREFIX,
5343 .list = ocfs2_xattr_trusted_list,
5344 .get = ocfs2_xattr_trusted_get,
5345 .set = ocfs2_xattr_trusted_set,
5346};
5347
Mark Fasheh99219ae2008-10-07 14:52:59 -07005348/*
5349 * 'user' attributes support
5350 */
Mark Fasheh99219ae2008-10-07 14:52:59 -07005351static size_t ocfs2_xattr_user_list(struct inode *inode, char *list,
5352 size_t list_size, const char *name,
5353 size_t name_len)
5354{
Tiger Yangceb1eba2008-10-23 16:34:13 +08005355 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
Mark Fasheh99219ae2008-10-07 14:52:59 -07005356 const size_t total_len = prefix_len + name_len + 1;
5357 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5358
5359 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
5360 return 0;
5361
5362 if (list && total_len <= list_size) {
5363 memcpy(list, XATTR_USER_PREFIX, prefix_len);
5364 memcpy(list + prefix_len, name, name_len);
5365 list[prefix_len + name_len] = '\0';
5366 }
5367 return total_len;
5368}
5369
5370static int ocfs2_xattr_user_get(struct inode *inode, const char *name,
5371 void *buffer, size_t size)
5372{
5373 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5374
5375 if (strcmp(name, "") == 0)
5376 return -EINVAL;
5377 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
5378 return -EOPNOTSUPP;
5379 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name,
5380 buffer, size);
5381}
5382
5383static int ocfs2_xattr_user_set(struct inode *inode, const char *name,
5384 const void *value, size_t size, int flags)
5385{
5386 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5387
5388 if (strcmp(name, "") == 0)
5389 return -EINVAL;
5390 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
5391 return -EOPNOTSUPP;
5392
5393 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, name, value,
5394 size, flags);
5395}
5396
5397struct xattr_handler ocfs2_xattr_user_handler = {
5398 .prefix = XATTR_USER_PREFIX,
5399 .list = ocfs2_xattr_user_list,
5400 .get = ocfs2_xattr_user_get,
5401 .set = ocfs2_xattr_user_set,
5402};