blob: bc822d6ba5420def312f496c04ed10fccf6c19b7 [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 Becker784b8162008-10-24 17:33:40 -0700276 if (rc)
Joel Beckerba937122008-10-24 19:13:20 -0700277 ocfs2_xattr_bucket_relse(bucket);
Joel Becker784b8162008-10-24 17:33:40 -0700278 return rc;
279}
280
Joel Becker1224be02008-10-24 18:47:33 -0700281static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
Joel Becker1224be02008-10-24 18:47:33 -0700282 struct ocfs2_xattr_bucket *bucket,
283 int type)
284{
285 int i, rc = 0;
Joel Becker1224be02008-10-24 18:47:33 -0700286
Joel Beckerba937122008-10-24 19:13:20 -0700287 for (i = 0; i < bucket->bu_blocks; i++) {
288 rc = ocfs2_journal_access(handle, bucket->bu_inode,
Joel Becker1224be02008-10-24 18:47:33 -0700289 bucket->bu_bhs[i], type);
290 if (rc) {
291 mlog_errno(rc);
292 break;
293 }
294 }
295
296 return rc;
297}
298
299static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
Joel Becker1224be02008-10-24 18:47:33 -0700300 struct ocfs2_xattr_bucket *bucket)
301{
Joel Beckerba937122008-10-24 19:13:20 -0700302 int i;
Joel Becker1224be02008-10-24 18:47:33 -0700303
Joel Beckerba937122008-10-24 19:13:20 -0700304 for (i = 0; i < bucket->bu_blocks; i++)
Joel Becker1224be02008-10-24 18:47:33 -0700305 ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
306}
307
Joel Beckerba937122008-10-24 19:13:20 -0700308static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
Joel Becker4980c6d2008-10-24 18:54:43 -0700309 struct ocfs2_xattr_bucket *src)
310{
311 int i;
Joel Beckerba937122008-10-24 19:13:20 -0700312 int blocksize = src->bu_inode->i_sb->s_blocksize;
Joel Becker4980c6d2008-10-24 18:54:43 -0700313
Joel Beckerba937122008-10-24 19:13:20 -0700314 BUG_ON(dest->bu_blocks != src->bu_blocks);
315 BUG_ON(dest->bu_inode != src->bu_inode);
316
317 for (i = 0; i < src->bu_blocks; i++) {
Joel Becker4980c6d2008-10-24 18:54:43 -0700318 memcpy(bucket_block(dest, i), bucket_block(src, i),
319 blocksize);
320 }
321}
Joel Becker1224be02008-10-24 18:47:33 -0700322
Joel Becker4ae1d692008-11-13 14:49:18 -0800323static int ocfs2_validate_xattr_block(struct super_block *sb,
324 struct buffer_head *bh)
325{
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700326 int rc;
Joel Becker4ae1d692008-11-13 14:49:18 -0800327 struct ocfs2_xattr_block *xb =
328 (struct ocfs2_xattr_block *)bh->b_data;
329
330 mlog(0, "Validating xattr block %llu\n",
331 (unsigned long long)bh->b_blocknr);
332
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700333 BUG_ON(!buffer_uptodate(bh));
334
335 /*
336 * If the ecc fails, we return the error but otherwise
337 * leave the filesystem running. We know any error is
338 * local to this block.
339 */
340 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &xb->xb_check);
341 if (rc)
342 return rc;
343
344 /*
345 * Errors after here are fatal
346 */
347
Joel Becker4ae1d692008-11-13 14:49:18 -0800348 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
349 ocfs2_error(sb,
350 "Extended attribute block #%llu has bad "
351 "signature %.*s",
352 (unsigned long long)bh->b_blocknr, 7,
353 xb->xb_signature);
354 return -EINVAL;
355 }
356
357 if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) {
358 ocfs2_error(sb,
359 "Extended attribute block #%llu has an "
360 "invalid xb_blkno of %llu",
361 (unsigned long long)bh->b_blocknr,
362 (unsigned long long)le64_to_cpu(xb->xb_blkno));
363 return -EINVAL;
364 }
365
366 if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) {
367 ocfs2_error(sb,
368 "Extended attribute block #%llu has an invalid "
369 "xb_fs_generation of #%u",
370 (unsigned long long)bh->b_blocknr,
371 le32_to_cpu(xb->xb_fs_generation));
372 return -EINVAL;
373 }
374
375 return 0;
376}
377
378static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno,
379 struct buffer_head **bh)
380{
381 int rc;
382 struct buffer_head *tmp = *bh;
383
Joel Becker970e4932008-11-13 14:49:19 -0800384 rc = ocfs2_read_block(inode, xb_blkno, &tmp,
385 ocfs2_validate_xattr_block);
Joel Becker4ae1d692008-11-13 14:49:18 -0800386
387 /* If ocfs2_read_block() got us a new bh, pass it up. */
388 if (!rc && !*bh)
389 *bh = tmp;
390
391 return rc;
392}
393
Tao Ma936b8832008-10-09 23:06:14 +0800394static inline const char *ocfs2_xattr_prefix(int name_index)
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800395{
396 struct xattr_handler *handler = NULL;
397
398 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
399 handler = ocfs2_xattr_handler_map[name_index];
400
Tao Ma936b8832008-10-09 23:06:14 +0800401 return handler ? handler->prefix : NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800402}
403
Mark Fasheh40daa162008-10-07 14:31:42 -0700404static u32 ocfs2_xattr_name_hash(struct inode *inode,
Tao Ma2057e5c2008-10-09 23:06:13 +0800405 const char *name,
Mark Fasheh40daa162008-10-07 14:31:42 -0700406 int name_len)
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800407{
408 /* Get hash value of uuid from super block */
409 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
410 int i;
411
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800412 /* hash extended attribute name */
413 for (i = 0; i < name_len; i++) {
414 hash = (hash << OCFS2_HASH_SHIFT) ^
415 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
416 *name++;
417 }
418
419 return hash;
420}
421
422/*
423 * ocfs2_xattr_hash_entry()
424 *
425 * Compute the hash of an extended attribute.
426 */
427static void ocfs2_xattr_hash_entry(struct inode *inode,
428 struct ocfs2_xattr_header *header,
429 struct ocfs2_xattr_entry *entry)
430{
431 u32 hash = 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800432 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800433
Tao Ma2057e5c2008-10-09 23:06:13 +0800434 hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800435 entry->xe_name_hash = cpu_to_le32(hash);
436
437 return;
438}
Tao Maf56654c2008-08-18 17:38:48 +0800439
Tiger Yang534eadd2008-11-14 11:16:41 +0800440static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
441{
442 int size = 0;
443
444 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
445 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
446 else
447 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
448 size += sizeof(struct ocfs2_xattr_entry);
449
450 return size;
451}
452
453int ocfs2_calc_security_init(struct inode *dir,
454 struct ocfs2_security_xattr_info *si,
455 int *want_clusters,
456 int *xattr_credits,
457 struct ocfs2_alloc_context **xattr_ac)
458{
459 int ret = 0;
460 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
461 int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
462 si->value_len);
463
464 /*
465 * The max space of security xattr taken inline is
466 * 256(name) + 80(value) + 16(entry) = 352 bytes,
467 * So reserve one metadata block for it is ok.
468 */
469 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
470 s_size > OCFS2_XATTR_FREE_IN_IBODY) {
471 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
472 if (ret) {
473 mlog_errno(ret);
474 return ret;
475 }
476 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
477 }
478
479 /* reserve clusters for xattr value which will be set in B tree*/
480 if (si->value_len > OCFS2_XATTR_INLINE_SIZE)
481 *want_clusters += ocfs2_clusters_for_bytes(dir->i_sb,
482 si->value_len);
483 return ret;
484}
485
Tiger Yang89c38bd2008-11-14 11:17:41 +0800486int ocfs2_calc_xattr_init(struct inode *dir,
487 struct buffer_head *dir_bh,
488 int mode,
489 struct ocfs2_security_xattr_info *si,
490 int *want_clusters,
491 int *xattr_credits,
492 struct ocfs2_alloc_context **xattr_ac)
493{
494 int ret = 0;
495 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
496 int s_size = 0;
497 int a_size = 0;
498 int acl_len = 0;
499
500 if (si->enable)
501 s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
502 si->value_len);
503
504 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
505 acl_len = ocfs2_xattr_get_nolock(dir, dir_bh,
506 OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
507 "", NULL, 0);
508 if (acl_len > 0) {
509 a_size = ocfs2_xattr_entry_real_size(0, acl_len);
510 if (S_ISDIR(mode))
511 a_size <<= 1;
512 } else if (acl_len != 0 && acl_len != -ENODATA) {
513 mlog_errno(ret);
514 return ret;
515 }
516 }
517
518 if (!(s_size + a_size))
519 return ret;
520
521 /*
522 * The max space of security xattr taken inline is
523 * 256(name) + 80(value) + 16(entry) = 352 bytes,
524 * The max space of acl xattr taken inline is
525 * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
526 * when blocksize = 512, may reserve one more cluser for
527 * xattr bucket, otherwise reserve one metadata block
528 * for them is ok.
529 */
530 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
531 (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) {
532 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
533 if (ret) {
534 mlog_errno(ret);
535 return ret;
536 }
537 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
538 }
539
540 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
541 (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
542 *want_clusters += 1;
543 *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
544 }
545
546 /* reserve clusters for xattr value which will be set in B tree*/
547 if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE)
548 *want_clusters += ocfs2_clusters_for_bytes(dir->i_sb,
549 si->value_len);
550 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
551 acl_len > OCFS2_XATTR_INLINE_SIZE) {
552 *want_clusters += ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
553 if (S_ISDIR(mode))
554 *want_clusters += ocfs2_clusters_for_bytes(dir->i_sb,
555 acl_len);
556 }
557
558 return ret;
559}
560
Tao Maf56654c2008-08-18 17:38:48 +0800561static int ocfs2_xattr_extend_allocation(struct inode *inode,
562 u32 clusters_to_add,
563 struct buffer_head *xattr_bh,
Tao Ma78f30c32008-11-12 08:27:00 +0800564 struct ocfs2_xattr_value_root *xv,
565 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800566{
567 int status = 0;
Tao Ma85db90e2008-11-12 08:27:01 +0800568 handle_t *handle = ctxt->handle;
Tao Maf56654c2008-08-18 17:38:48 +0800569 enum ocfs2_alloc_restarted why;
570 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Maf56654c2008-08-18 17:38:48 +0800571 u32 prev_clusters, logical_start = le32_to_cpu(xv->xr_clusters);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700572 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +0800573
574 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
575
Joel Becker8d6220d2008-08-22 12:46:09 -0700576 ocfs2_init_xattr_value_extent_tree(&et, inode, xattr_bh, xv);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700577
Tao Maf56654c2008-08-18 17:38:48 +0800578 status = ocfs2_journal_access(handle, inode, xattr_bh,
579 OCFS2_JOURNAL_ACCESS_WRITE);
580 if (status < 0) {
581 mlog_errno(status);
582 goto leave;
583 }
584
585 prev_clusters = le32_to_cpu(xv->xr_clusters);
586 status = ocfs2_add_clusters_in_btree(osb,
587 inode,
588 &logical_start,
589 clusters_to_add,
590 0,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700591 &et,
Tao Maf56654c2008-08-18 17:38:48 +0800592 handle,
Tao Ma78f30c32008-11-12 08:27:00 +0800593 ctxt->data_ac,
594 ctxt->meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700595 &why);
Tao Ma85db90e2008-11-12 08:27:01 +0800596 if (status < 0) {
597 mlog_errno(status);
Tao Maf56654c2008-08-18 17:38:48 +0800598 goto leave;
599 }
600
601 status = ocfs2_journal_dirty(handle, xattr_bh);
602 if (status < 0) {
603 mlog_errno(status);
604 goto leave;
605 }
606
607 clusters_to_add -= le32_to_cpu(xv->xr_clusters) - prev_clusters;
608
Tao Ma85db90e2008-11-12 08:27:01 +0800609 /*
610 * We should have already allocated enough space before the transaction,
611 * so no need to restart.
612 */
613 BUG_ON(why != RESTART_NONE || clusters_to_add);
Tao Maf56654c2008-08-18 17:38:48 +0800614
615leave:
Tao Maf56654c2008-08-18 17:38:48 +0800616
617 return status;
618}
619
620static int __ocfs2_remove_xattr_range(struct inode *inode,
621 struct buffer_head *root_bh,
622 struct ocfs2_xattr_value_root *xv,
623 u32 cpos, u32 phys_cpos, u32 len,
Tao Ma78f30c32008-11-12 08:27:00 +0800624 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800625{
626 int ret;
627 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
Tao Ma85db90e2008-11-12 08:27:01 +0800628 handle_t *handle = ctxt->handle;
Joel Beckerf99b9b72008-08-20 19:36:33 -0700629 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +0800630
Joel Becker8d6220d2008-08-22 12:46:09 -0700631 ocfs2_init_xattr_value_extent_tree(&et, inode, root_bh, xv);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700632
Tao Maf56654c2008-08-18 17:38:48 +0800633 ret = ocfs2_journal_access(handle, inode, root_bh,
634 OCFS2_JOURNAL_ACCESS_WRITE);
635 if (ret) {
636 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800637 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800638 }
639
Tao Ma78f30c32008-11-12 08:27:00 +0800640 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, ctxt->meta_ac,
641 &ctxt->dealloc);
Tao Maf56654c2008-08-18 17:38:48 +0800642 if (ret) {
643 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800644 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800645 }
646
647 le32_add_cpu(&xv->xr_clusters, -len);
648
649 ret = ocfs2_journal_dirty(handle, root_bh);
650 if (ret) {
651 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800652 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800653 }
654
Tao Ma78f30c32008-11-12 08:27:00 +0800655 ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc, phys_blkno, len);
Tao Maf56654c2008-08-18 17:38:48 +0800656 if (ret)
657 mlog_errno(ret);
658
Tao Maf56654c2008-08-18 17:38:48 +0800659out:
Tao Maf56654c2008-08-18 17:38:48 +0800660 return ret;
661}
662
663static int ocfs2_xattr_shrink_size(struct inode *inode,
664 u32 old_clusters,
665 u32 new_clusters,
666 struct buffer_head *root_bh,
Tao Ma78f30c32008-11-12 08:27:00 +0800667 struct ocfs2_xattr_value_root *xv,
668 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800669{
670 int ret = 0;
671 u32 trunc_len, cpos, phys_cpos, alloc_size;
672 u64 block;
Tao Maf56654c2008-08-18 17:38:48 +0800673
674 if (old_clusters <= new_clusters)
675 return 0;
676
677 cpos = new_clusters;
678 trunc_len = old_clusters - new_clusters;
679 while (trunc_len) {
680 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
681 &alloc_size, &xv->xr_list);
682 if (ret) {
683 mlog_errno(ret);
684 goto out;
685 }
686
687 if (alloc_size > trunc_len)
688 alloc_size = trunc_len;
689
690 ret = __ocfs2_remove_xattr_range(inode, root_bh, xv, cpos,
691 phys_cpos, alloc_size,
Tao Ma78f30c32008-11-12 08:27:00 +0800692 ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800693 if (ret) {
694 mlog_errno(ret);
695 goto out;
696 }
697
698 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
699 ocfs2_remove_xattr_clusters_from_cache(inode, block,
700 alloc_size);
701 cpos += alloc_size;
702 trunc_len -= alloc_size;
703 }
704
705out:
Tao Maf56654c2008-08-18 17:38:48 +0800706 return ret;
707}
708
709static int ocfs2_xattr_value_truncate(struct inode *inode,
710 struct buffer_head *root_bh,
711 struct ocfs2_xattr_value_root *xv,
Tao Ma78f30c32008-11-12 08:27:00 +0800712 int len,
713 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800714{
715 int ret;
716 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
717 u32 old_clusters = le32_to_cpu(xv->xr_clusters);
718
719 if (new_clusters == old_clusters)
720 return 0;
721
722 if (new_clusters > old_clusters)
723 ret = ocfs2_xattr_extend_allocation(inode,
724 new_clusters - old_clusters,
Tao Ma78f30c32008-11-12 08:27:00 +0800725 root_bh, xv, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800726 else
727 ret = ocfs2_xattr_shrink_size(inode,
728 old_clusters, new_clusters,
Tao Ma78f30c32008-11-12 08:27:00 +0800729 root_bh, xv, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800730
731 return ret;
732}
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800733
Tao Ma936b8832008-10-09 23:06:14 +0800734static int ocfs2_xattr_list_entry(char *buffer, size_t size,
735 size_t *result, const char *prefix,
736 const char *name, int name_len)
737{
738 char *p = buffer + *result;
739 int prefix_len = strlen(prefix);
740 int total_len = prefix_len + name_len + 1;
741
742 *result += total_len;
743
744 /* we are just looking for how big our buffer needs to be */
745 if (!size)
746 return 0;
747
748 if (*result > size)
749 return -ERANGE;
750
751 memcpy(p, prefix, prefix_len);
752 memcpy(p + prefix_len, name, name_len);
753 p[prefix_len + name_len] = '\0';
754
755 return 0;
756}
757
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800758static int ocfs2_xattr_list_entries(struct inode *inode,
759 struct ocfs2_xattr_header *header,
760 char *buffer, size_t buffer_size)
761{
Tao Ma936b8832008-10-09 23:06:14 +0800762 size_t result = 0;
763 int i, type, ret;
764 const char *prefix, *name;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800765
766 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
767 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
Tao Ma936b8832008-10-09 23:06:14 +0800768 type = ocfs2_xattr_get_type(entry);
769 prefix = ocfs2_xattr_prefix(type);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800770
Tao Ma936b8832008-10-09 23:06:14 +0800771 if (prefix) {
772 name = (const char *)header +
773 le16_to_cpu(entry->xe_name_offset);
774
775 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
776 &result, prefix, name,
777 entry->xe_name_len);
778 if (ret)
779 return ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800780 }
781 }
782
Tao Ma936b8832008-10-09 23:06:14 +0800783 return result;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800784}
785
786static int ocfs2_xattr_ibody_list(struct inode *inode,
787 struct ocfs2_dinode *di,
788 char *buffer,
789 size_t buffer_size)
790{
791 struct ocfs2_xattr_header *header = NULL;
792 struct ocfs2_inode_info *oi = OCFS2_I(inode);
793 int ret = 0;
794
795 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
796 return ret;
797
798 header = (struct ocfs2_xattr_header *)
799 ((void *)di + inode->i_sb->s_blocksize -
800 le16_to_cpu(di->i_xattr_inline_size));
801
802 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
803
804 return ret;
805}
806
807static int ocfs2_xattr_block_list(struct inode *inode,
808 struct ocfs2_dinode *di,
809 char *buffer,
810 size_t buffer_size)
811{
812 struct buffer_head *blk_bh = NULL;
Tao Ma0c044f02008-08-18 17:38:50 +0800813 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800814 int ret = 0;
815
816 if (!di->i_xattr_loc)
817 return ret;
818
Joel Becker4ae1d692008-11-13 14:49:18 -0800819 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
820 &blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800821 if (ret < 0) {
822 mlog_errno(ret);
823 return ret;
824 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800825
Tao Ma0c044f02008-08-18 17:38:50 +0800826 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Ma0c044f02008-08-18 17:38:50 +0800827 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
828 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
829 ret = ocfs2_xattr_list_entries(inode, header,
830 buffer, buffer_size);
831 } else {
832 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
833 ret = ocfs2_xattr_tree_list_index_block(inode, xt,
834 buffer, buffer_size);
835 }
Joel Becker4ae1d692008-11-13 14:49:18 -0800836
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800837 brelse(blk_bh);
838
839 return ret;
840}
841
842ssize_t ocfs2_listxattr(struct dentry *dentry,
843 char *buffer,
844 size_t size)
845{
846 int ret = 0, i_ret = 0, b_ret = 0;
847 struct buffer_head *di_bh = NULL;
848 struct ocfs2_dinode *di = NULL;
849 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
850
Tiger Yang8154da32008-08-18 17:11:46 +0800851 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
852 return -EOPNOTSUPP;
853
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800854 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
855 return ret;
856
857 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
858 if (ret < 0) {
859 mlog_errno(ret);
860 return ret;
861 }
862
863 di = (struct ocfs2_dinode *)di_bh->b_data;
864
865 down_read(&oi->ip_xattr_sem);
866 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
867 if (i_ret < 0)
868 b_ret = 0;
869 else {
870 if (buffer) {
871 buffer += i_ret;
872 size -= i_ret;
873 }
874 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
875 buffer, size);
876 if (b_ret < 0)
877 i_ret = 0;
878 }
879 up_read(&oi->ip_xattr_sem);
880 ocfs2_inode_unlock(dentry->d_inode, 0);
881
882 brelse(di_bh);
883
884 return i_ret + b_ret;
885}
886
887static int ocfs2_xattr_find_entry(int name_index,
888 const char *name,
889 struct ocfs2_xattr_search *xs)
890{
891 struct ocfs2_xattr_entry *entry;
892 size_t name_len;
893 int i, cmp = 1;
894
895 if (name == NULL)
896 return -EINVAL;
897
898 name_len = strlen(name);
899 entry = xs->here;
900 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
901 cmp = name_index - ocfs2_xattr_get_type(entry);
902 if (!cmp)
903 cmp = name_len - entry->xe_name_len;
904 if (!cmp)
905 cmp = memcmp(name, (xs->base +
906 le16_to_cpu(entry->xe_name_offset)),
907 name_len);
908 if (cmp == 0)
909 break;
910 entry += 1;
911 }
912 xs->here = entry;
913
914 return cmp ? -ENODATA : 0;
915}
916
917static int ocfs2_xattr_get_value_outside(struct inode *inode,
Tao Ma589dc262008-08-18 17:38:51 +0800918 struct ocfs2_xattr_value_root *xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800919 void *buffer,
920 size_t len)
921{
922 u32 cpos, p_cluster, num_clusters, bpc, clusters;
923 u64 blkno;
924 int i, ret = 0;
925 size_t cplen, blocksize;
926 struct buffer_head *bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800927 struct ocfs2_extent_list *el;
928
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800929 el = &xv->xr_list;
930 clusters = le32_to_cpu(xv->xr_clusters);
931 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
932 blocksize = inode->i_sb->s_blocksize;
933
934 cpos = 0;
935 while (cpos < clusters) {
936 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
937 &num_clusters, el);
938 if (ret) {
939 mlog_errno(ret);
940 goto out;
941 }
942
943 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
944 /* Copy ocfs2_xattr_value */
945 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
Joel Becker970e4932008-11-13 14:49:19 -0800946 ret = ocfs2_read_block(inode, blkno, &bh, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800947 if (ret) {
948 mlog_errno(ret);
949 goto out;
950 }
951
952 cplen = len >= blocksize ? blocksize : len;
953 memcpy(buffer, bh->b_data, cplen);
954 len -= cplen;
955 buffer += cplen;
956
957 brelse(bh);
958 bh = NULL;
959 if (len == 0)
960 break;
961 }
962 cpos += num_clusters;
963 }
964out:
965 return ret;
966}
967
968static int ocfs2_xattr_ibody_get(struct inode *inode,
969 int name_index,
970 const char *name,
971 void *buffer,
972 size_t buffer_size,
973 struct ocfs2_xattr_search *xs)
974{
975 struct ocfs2_inode_info *oi = OCFS2_I(inode);
976 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
Tao Ma589dc262008-08-18 17:38:51 +0800977 struct ocfs2_xattr_value_root *xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800978 size_t size;
979 int ret = 0;
980
981 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
982 return -ENODATA;
983
984 xs->end = (void *)di + inode->i_sb->s_blocksize;
985 xs->header = (struct ocfs2_xattr_header *)
986 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
987 xs->base = (void *)xs->header;
988 xs->here = xs->header->xh_entries;
989
990 ret = ocfs2_xattr_find_entry(name_index, name, xs);
991 if (ret)
992 return ret;
993 size = le64_to_cpu(xs->here->xe_value_size);
994 if (buffer) {
995 if (size > buffer_size)
996 return -ERANGE;
997 if (ocfs2_xattr_is_local(xs->here)) {
998 memcpy(buffer, (void *)xs->base +
999 le16_to_cpu(xs->here->xe_name_offset) +
1000 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
1001 } else {
Tao Ma589dc262008-08-18 17:38:51 +08001002 xv = (struct ocfs2_xattr_value_root *)
1003 (xs->base + le16_to_cpu(
1004 xs->here->xe_name_offset) +
1005 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
1006 ret = ocfs2_xattr_get_value_outside(inode, xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001007 buffer, size);
1008 if (ret < 0) {
1009 mlog_errno(ret);
1010 return ret;
1011 }
1012 }
1013 }
1014
1015 return size;
1016}
1017
1018static int ocfs2_xattr_block_get(struct inode *inode,
1019 int name_index,
1020 const char *name,
1021 void *buffer,
1022 size_t buffer_size,
1023 struct ocfs2_xattr_search *xs)
1024{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001025 struct ocfs2_xattr_block *xb;
Tao Ma589dc262008-08-18 17:38:51 +08001026 struct ocfs2_xattr_value_root *xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001027 size_t size;
Tao Ma589dc262008-08-18 17:38:51 +08001028 int ret = -ENODATA, name_offset, name_len, block_off, i;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001029
Joel Beckerba937122008-10-24 19:13:20 -07001030 xs->bucket = ocfs2_xattr_bucket_new(inode);
1031 if (!xs->bucket) {
1032 ret = -ENOMEM;
1033 mlog_errno(ret);
1034 goto cleanup;
1035 }
Tao Ma589dc262008-08-18 17:38:51 +08001036
Joel Becker54f443f2008-10-20 18:43:07 -07001037 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
1038 if (ret) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001039 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001040 goto cleanup;
1041 }
1042
Tiger Yang6c1e1832008-11-02 19:04:21 +08001043 if (xs->not_found) {
1044 ret = -ENODATA;
1045 goto cleanup;
1046 }
1047
Joel Becker54f443f2008-10-20 18:43:07 -07001048 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001049 size = le64_to_cpu(xs->here->xe_value_size);
1050 if (buffer) {
1051 ret = -ERANGE;
1052 if (size > buffer_size)
1053 goto cleanup;
Tao Ma589dc262008-08-18 17:38:51 +08001054
1055 name_offset = le16_to_cpu(xs->here->xe_name_offset);
1056 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
1057 i = xs->here - xs->header->xh_entries;
1058
1059 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
1060 ret = ocfs2_xattr_bucket_get_name_value(inode,
Joel Beckerba937122008-10-24 19:13:20 -07001061 bucket_xh(xs->bucket),
Tao Ma589dc262008-08-18 17:38:51 +08001062 i,
1063 &block_off,
1064 &name_offset);
Joel Beckerba937122008-10-24 19:13:20 -07001065 xs->base = bucket_block(xs->bucket, block_off);
Tao Ma589dc262008-08-18 17:38:51 +08001066 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001067 if (ocfs2_xattr_is_local(xs->here)) {
1068 memcpy(buffer, (void *)xs->base +
Tao Ma589dc262008-08-18 17:38:51 +08001069 name_offset + name_len, size);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001070 } else {
Tao Ma589dc262008-08-18 17:38:51 +08001071 xv = (struct ocfs2_xattr_value_root *)
1072 (xs->base + name_offset + name_len);
1073 ret = ocfs2_xattr_get_value_outside(inode, xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001074 buffer, size);
1075 if (ret < 0) {
1076 mlog_errno(ret);
1077 goto cleanup;
1078 }
1079 }
1080 }
1081 ret = size;
1082cleanup:
Joel Beckerba937122008-10-24 19:13:20 -07001083 ocfs2_xattr_bucket_free(xs->bucket);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001084
Joel Becker54f443f2008-10-20 18:43:07 -07001085 brelse(xs->xattr_bh);
1086 xs->xattr_bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001087 return ret;
1088}
1089
Tiger Yang4e3e9d02008-11-14 11:16:53 +08001090int ocfs2_xattr_get_nolock(struct inode *inode,
1091 struct buffer_head *di_bh,
Tiger Yang0030e002008-10-23 16:33:33 +08001092 int name_index,
1093 const char *name,
1094 void *buffer,
1095 size_t buffer_size)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001096{
1097 int ret;
1098 struct ocfs2_dinode *di = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001099 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1100 struct ocfs2_xattr_search xis = {
1101 .not_found = -ENODATA,
1102 };
1103 struct ocfs2_xattr_search xbs = {
1104 .not_found = -ENODATA,
1105 };
1106
Tiger Yang8154da32008-08-18 17:11:46 +08001107 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1108 return -EOPNOTSUPP;
1109
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001110 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1111 ret = -ENODATA;
1112
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001113 xis.inode_bh = xbs.inode_bh = di_bh;
1114 di = (struct ocfs2_dinode *)di_bh->b_data;
1115
1116 down_read(&oi->ip_xattr_sem);
1117 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1118 buffer_size, &xis);
Tiger Yang6c1e1832008-11-02 19:04:21 +08001119 if (ret == -ENODATA && di->i_xattr_loc)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001120 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1121 buffer_size, &xbs);
1122 up_read(&oi->ip_xattr_sem);
Tiger Yang4e3e9d02008-11-14 11:16:53 +08001123
1124 return ret;
1125}
1126
1127/* ocfs2_xattr_get()
1128 *
1129 * Copy an extended attribute into the buffer provided.
1130 * Buffer is NULL to compute the size of buffer required.
1131 */
1132static int ocfs2_xattr_get(struct inode *inode,
1133 int name_index,
1134 const char *name,
1135 void *buffer,
1136 size_t buffer_size)
1137{
1138 int ret;
1139 struct buffer_head *di_bh = NULL;
1140
1141 ret = ocfs2_inode_lock(inode, &di_bh, 0);
1142 if (ret < 0) {
1143 mlog_errno(ret);
1144 return ret;
1145 }
1146 ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
1147 name, buffer, buffer_size);
1148
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001149 ocfs2_inode_unlock(inode, 0);
1150
1151 brelse(di_bh);
1152
1153 return ret;
1154}
1155
1156static int __ocfs2_xattr_set_value_outside(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001157 handle_t *handle,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001158 struct ocfs2_xattr_value_root *xv,
1159 const void *value,
1160 int value_len)
1161{
1162 int ret = 0, i, cp_len, credits;
1163 u16 blocksize = inode->i_sb->s_blocksize;
1164 u32 p_cluster, num_clusters;
1165 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1166 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1167 u64 blkno;
1168 struct buffer_head *bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001169
1170 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1171
Tao Ma85db90e2008-11-12 08:27:01 +08001172 /*
1173 * In __ocfs2_xattr_set_value_outside has already been dirtied,
1174 * so we don't need to worry about whether ocfs2_extend_trans
1175 * will create a new transactio for us or not.
1176 */
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001177 credits = clusters * bpc;
Tao Ma85db90e2008-11-12 08:27:01 +08001178 ret = ocfs2_extend_trans(handle, credits);
1179 if (ret) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001180 mlog_errno(ret);
1181 goto out;
1182 }
1183
1184 while (cpos < clusters) {
1185 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1186 &num_clusters, &xv->xr_list);
1187 if (ret) {
1188 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001189 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001190 }
1191
1192 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1193
1194 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
Joel Becker970e4932008-11-13 14:49:19 -08001195 ret = ocfs2_read_block(inode, blkno, &bh, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001196 if (ret) {
1197 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001198 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001199 }
1200
1201 ret = ocfs2_journal_access(handle,
1202 inode,
1203 bh,
1204 OCFS2_JOURNAL_ACCESS_WRITE);
1205 if (ret < 0) {
1206 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001207 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001208 }
1209
1210 cp_len = value_len > blocksize ? blocksize : value_len;
1211 memcpy(bh->b_data, value, cp_len);
1212 value_len -= cp_len;
1213 value += cp_len;
1214 if (cp_len < blocksize)
1215 memset(bh->b_data + cp_len, 0,
1216 blocksize - cp_len);
1217
1218 ret = ocfs2_journal_dirty(handle, bh);
1219 if (ret < 0) {
1220 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001221 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001222 }
1223 brelse(bh);
1224 bh = NULL;
1225
1226 /*
1227 * XXX: do we need to empty all the following
1228 * blocks in this cluster?
1229 */
1230 if (!value_len)
1231 break;
1232 }
1233 cpos += num_clusters;
1234 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001235out:
1236 brelse(bh);
1237
1238 return ret;
1239}
1240
1241static int ocfs2_xattr_cleanup(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001242 handle_t *handle,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001243 struct ocfs2_xattr_info *xi,
1244 struct ocfs2_xattr_search *xs,
1245 size_t offs)
1246{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001247 int ret = 0;
1248 size_t name_len = strlen(xi->name);
1249 void *val = xs->base + offs;
1250 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1251
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001252 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1253 OCFS2_JOURNAL_ACCESS_WRITE);
1254 if (ret) {
1255 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001256 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001257 }
1258 /* Decrease xattr count */
1259 le16_add_cpu(&xs->header->xh_count, -1);
1260 /* Remove the xattr entry and tree root which has already be set*/
1261 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
1262 memset(val, 0, size);
1263
1264 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1265 if (ret < 0)
1266 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001267out:
1268 return ret;
1269}
1270
1271static int ocfs2_xattr_update_entry(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001272 handle_t *handle,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001273 struct ocfs2_xattr_info *xi,
1274 struct ocfs2_xattr_search *xs,
1275 size_t offs)
1276{
Tao Ma85db90e2008-11-12 08:27:01 +08001277 int ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001278
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001279 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1280 OCFS2_JOURNAL_ACCESS_WRITE);
1281 if (ret) {
1282 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001283 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001284 }
1285
1286 xs->here->xe_name_offset = cpu_to_le16(offs);
1287 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1288 if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
1289 ocfs2_xattr_set_local(xs->here, 1);
1290 else
1291 ocfs2_xattr_set_local(xs->here, 0);
1292 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1293
1294 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1295 if (ret < 0)
1296 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001297out:
1298 return ret;
1299}
1300
1301/*
1302 * ocfs2_xattr_set_value_outside()
1303 *
1304 * Set large size value in B tree.
1305 */
1306static int ocfs2_xattr_set_value_outside(struct inode *inode,
1307 struct ocfs2_xattr_info *xi,
1308 struct ocfs2_xattr_search *xs,
Tao Ma78f30c32008-11-12 08:27:00 +08001309 struct ocfs2_xattr_set_ctxt *ctxt,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001310 size_t offs)
1311{
1312 size_t name_len = strlen(xi->name);
1313 void *val = xs->base + offs;
1314 struct ocfs2_xattr_value_root *xv = NULL;
1315 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1316 int ret = 0;
1317
1318 memset(val, 0, size);
1319 memcpy(val, xi->name, name_len);
1320 xv = (struct ocfs2_xattr_value_root *)
1321 (val + OCFS2_XATTR_SIZE(name_len));
1322 xv->xr_clusters = 0;
1323 xv->xr_last_eb_blk = 0;
1324 xv->xr_list.l_tree_depth = 0;
1325 xv->xr_list.l_count = cpu_to_le16(1);
1326 xv->xr_list.l_next_free_rec = 0;
1327
1328 ret = ocfs2_xattr_value_truncate(inode, xs->xattr_bh, xv,
Tao Ma78f30c32008-11-12 08:27:00 +08001329 xi->value_len, ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001330 if (ret < 0) {
1331 mlog_errno(ret);
1332 return ret;
1333 }
Tao Ma85db90e2008-11-12 08:27:01 +08001334 ret = ocfs2_xattr_update_entry(inode, ctxt->handle, xi, xs, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001335 if (ret < 0) {
1336 mlog_errno(ret);
1337 return ret;
1338 }
Tao Ma85db90e2008-11-12 08:27:01 +08001339 ret = __ocfs2_xattr_set_value_outside(inode, ctxt->handle, xv,
1340 xi->value, xi->value_len);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001341 if (ret < 0)
1342 mlog_errno(ret);
1343
1344 return ret;
1345}
1346
1347/*
1348 * ocfs2_xattr_set_entry_local()
1349 *
1350 * Set, replace or remove extended attribute in local.
1351 */
1352static void ocfs2_xattr_set_entry_local(struct inode *inode,
1353 struct ocfs2_xattr_info *xi,
1354 struct ocfs2_xattr_search *xs,
1355 struct ocfs2_xattr_entry *last,
1356 size_t min_offs)
1357{
1358 size_t name_len = strlen(xi->name);
1359 int i;
1360
1361 if (xi->value && xs->not_found) {
1362 /* Insert the new xattr entry. */
1363 le16_add_cpu(&xs->header->xh_count, 1);
1364 ocfs2_xattr_set_type(last, xi->name_index);
1365 ocfs2_xattr_set_local(last, 1);
1366 last->xe_name_len = name_len;
1367 } else {
1368 void *first_val;
1369 void *val;
1370 size_t offs, size;
1371
1372 first_val = xs->base + min_offs;
1373 offs = le16_to_cpu(xs->here->xe_name_offset);
1374 val = xs->base + offs;
1375
1376 if (le64_to_cpu(xs->here->xe_value_size) >
1377 OCFS2_XATTR_INLINE_SIZE)
1378 size = OCFS2_XATTR_SIZE(name_len) +
1379 OCFS2_XATTR_ROOT_SIZE;
1380 else
1381 size = OCFS2_XATTR_SIZE(name_len) +
1382 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1383
1384 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1385 OCFS2_XATTR_SIZE(xi->value_len)) {
1386 /* The old and the new value have the
1387 same size. Just replace the value. */
1388 ocfs2_xattr_set_local(xs->here, 1);
1389 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1390 /* Clear value bytes. */
1391 memset(val + OCFS2_XATTR_SIZE(name_len),
1392 0,
1393 OCFS2_XATTR_SIZE(xi->value_len));
1394 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1395 xi->value,
1396 xi->value_len);
1397 return;
1398 }
1399 /* Remove the old name+value. */
1400 memmove(first_val + size, first_val, val - first_val);
1401 memset(first_val, 0, size);
1402 xs->here->xe_name_hash = 0;
1403 xs->here->xe_name_offset = 0;
1404 ocfs2_xattr_set_local(xs->here, 1);
1405 xs->here->xe_value_size = 0;
1406
1407 min_offs += size;
1408
1409 /* Adjust all value offsets. */
1410 last = xs->header->xh_entries;
1411 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1412 size_t o = le16_to_cpu(last->xe_name_offset);
1413
1414 if (o < offs)
1415 last->xe_name_offset = cpu_to_le16(o + size);
1416 last += 1;
1417 }
1418
1419 if (!xi->value) {
1420 /* Remove the old entry. */
1421 last -= 1;
1422 memmove(xs->here, xs->here + 1,
1423 (void *)last - (void *)xs->here);
1424 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1425 le16_add_cpu(&xs->header->xh_count, -1);
1426 }
1427 }
1428 if (xi->value) {
1429 /* Insert the new name+value. */
1430 size_t size = OCFS2_XATTR_SIZE(name_len) +
1431 OCFS2_XATTR_SIZE(xi->value_len);
1432 void *val = xs->base + min_offs - size;
1433
1434 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1435 memset(val, 0, size);
1436 memcpy(val, xi->name, name_len);
1437 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1438 xi->value,
1439 xi->value_len);
1440 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1441 ocfs2_xattr_set_local(xs->here, 1);
1442 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1443 }
1444
1445 return;
1446}
1447
1448/*
1449 * ocfs2_xattr_set_entry()
1450 *
1451 * Set extended attribute entry into inode or block.
1452 *
1453 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1454 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1455 * then set value in B tree with set_value_outside().
1456 */
1457static int ocfs2_xattr_set_entry(struct inode *inode,
1458 struct ocfs2_xattr_info *xi,
1459 struct ocfs2_xattr_search *xs,
Tao Ma78f30c32008-11-12 08:27:00 +08001460 struct ocfs2_xattr_set_ctxt *ctxt,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001461 int flag)
1462{
1463 struct ocfs2_xattr_entry *last;
1464 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1465 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1466 size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1467 size_t size_l = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08001468 handle_t *handle = ctxt->handle;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001469 int free, i, ret;
1470 struct ocfs2_xattr_info xi_l = {
1471 .name_index = xi->name_index,
1472 .name = xi->name,
1473 .value = xi->value,
1474 .value_len = xi->value_len,
1475 };
1476
1477 /* Compute min_offs, last and free space. */
1478 last = xs->header->xh_entries;
1479
1480 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1481 size_t offs = le16_to_cpu(last->xe_name_offset);
1482 if (offs < min_offs)
1483 min_offs = offs;
1484 last += 1;
1485 }
1486
1487 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
1488 if (free < 0)
Joel Beckerb37c4d82008-10-20 18:24:03 -07001489 return -EIO;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001490
1491 if (!xs->not_found) {
1492 size_t size = 0;
1493 if (ocfs2_xattr_is_local(xs->here))
1494 size = OCFS2_XATTR_SIZE(name_len) +
1495 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1496 else
1497 size = OCFS2_XATTR_SIZE(name_len) +
1498 OCFS2_XATTR_ROOT_SIZE;
1499 free += (size + sizeof(struct ocfs2_xattr_entry));
1500 }
1501 /* Check free space in inode or block */
1502 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1503 if (free < sizeof(struct ocfs2_xattr_entry) +
1504 OCFS2_XATTR_SIZE(name_len) +
1505 OCFS2_XATTR_ROOT_SIZE) {
1506 ret = -ENOSPC;
1507 goto out;
1508 }
1509 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1510 xi_l.value = (void *)&def_xv;
1511 xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
1512 } else if (xi->value) {
1513 if (free < sizeof(struct ocfs2_xattr_entry) +
1514 OCFS2_XATTR_SIZE(name_len) +
1515 OCFS2_XATTR_SIZE(xi->value_len)) {
1516 ret = -ENOSPC;
1517 goto out;
1518 }
1519 }
1520
1521 if (!xs->not_found) {
1522 /* For existing extended attribute */
1523 size_t size = OCFS2_XATTR_SIZE(name_len) +
1524 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1525 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1526 void *val = xs->base + offs;
1527
1528 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1529 /* Replace existing local xattr with tree root */
1530 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
Tao Ma78f30c32008-11-12 08:27:00 +08001531 ctxt, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001532 if (ret < 0)
1533 mlog_errno(ret);
1534 goto out;
1535 } else if (!ocfs2_xattr_is_local(xs->here)) {
1536 /* For existing xattr which has value outside */
1537 struct ocfs2_xattr_value_root *xv = NULL;
1538 xv = (struct ocfs2_xattr_value_root *)(val +
1539 OCFS2_XATTR_SIZE(name_len));
1540
1541 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1542 /*
1543 * If new value need set outside also,
1544 * first truncate old value to new value,
1545 * then set new value with set_value_outside().
1546 */
1547 ret = ocfs2_xattr_value_truncate(inode,
1548 xs->xattr_bh,
1549 xv,
Tao Ma78f30c32008-11-12 08:27:00 +08001550 xi->value_len,
1551 ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001552 if (ret < 0) {
1553 mlog_errno(ret);
1554 goto out;
1555 }
1556
Tao Ma85db90e2008-11-12 08:27:01 +08001557 ret = ocfs2_xattr_update_entry(inode,
1558 handle,
1559 xi,
1560 xs,
1561 offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001562 if (ret < 0) {
1563 mlog_errno(ret);
1564 goto out;
1565 }
1566
Tao Ma85db90e2008-11-12 08:27:01 +08001567 ret = __ocfs2_xattr_set_value_outside(inode,
1568 handle,
1569 xv,
1570 xi->value,
1571 xi->value_len);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001572 if (ret < 0)
1573 mlog_errno(ret);
1574 goto out;
1575 } else {
1576 /*
1577 * If new value need set in local,
1578 * just trucate old value to zero.
1579 */
1580 ret = ocfs2_xattr_value_truncate(inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001581 xs->xattr_bh,
1582 xv,
1583 0,
1584 ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001585 if (ret < 0)
1586 mlog_errno(ret);
1587 }
1588 }
1589 }
1590
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001591 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1592 OCFS2_JOURNAL_ACCESS_WRITE);
1593 if (ret) {
1594 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001595 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001596 }
1597
1598 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001599 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1600 OCFS2_JOURNAL_ACCESS_WRITE);
1601 if (ret) {
1602 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001603 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001604 }
1605 }
1606
1607 /*
1608 * Set value in local, include set tree root in local.
1609 * This is the first step for value size >INLINE_SIZE.
1610 */
1611 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1612
1613 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1614 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1615 if (ret < 0) {
1616 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001617 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001618 }
1619 }
1620
1621 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1622 (flag & OCFS2_INLINE_XATTR_FL)) {
1623 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1624 unsigned int xattrsize = osb->s_xattr_inline_size;
1625
1626 /*
1627 * Adjust extent record count or inline data size
1628 * to reserve space for extended attribute.
1629 */
1630 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1631 struct ocfs2_inline_data *idata = &di->id2.i_data;
1632 le16_add_cpu(&idata->id_count, -xattrsize);
1633 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1634 struct ocfs2_extent_list *el = &di->id2.i_list;
1635 le16_add_cpu(&el->l_count, -(xattrsize /
1636 sizeof(struct ocfs2_extent_rec)));
1637 }
1638 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1639 }
1640 /* Update xattr flag */
1641 spin_lock(&oi->ip_lock);
1642 oi->ip_dyn_features |= flag;
1643 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1644 spin_unlock(&oi->ip_lock);
1645 /* Update inode ctime */
1646 inode->i_ctime = CURRENT_TIME;
1647 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
1648 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
1649
1650 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1651 if (ret < 0)
1652 mlog_errno(ret);
1653
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001654 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1655 /*
1656 * Set value outside in B tree.
1657 * This is the second step for value size > INLINE_SIZE.
1658 */
1659 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
Tao Ma78f30c32008-11-12 08:27:00 +08001660 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, ctxt, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001661 if (ret < 0) {
1662 int ret2;
1663
1664 mlog_errno(ret);
1665 /*
1666 * If set value outside failed, we have to clean
1667 * the junk tree root we have already set in local.
1668 */
Tao Ma85db90e2008-11-12 08:27:01 +08001669 ret2 = ocfs2_xattr_cleanup(inode, ctxt->handle,
1670 xi, xs, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001671 if (ret2 < 0)
1672 mlog_errno(ret2);
1673 }
1674 }
1675out:
1676 return ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001677}
1678
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001679static int ocfs2_remove_value_outside(struct inode*inode,
1680 struct buffer_head *bh,
1681 struct ocfs2_xattr_header *header)
1682{
1683 int ret = 0, i;
Tao Ma78f30c32008-11-12 08:27:00 +08001684 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1685 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
1686
1687 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001688
Jan Karaa90714c2008-10-09 19:38:40 +02001689 ctxt.handle = ocfs2_start_trans(osb,
1690 ocfs2_remove_extent_credits(osb->sb));
Tao Ma85db90e2008-11-12 08:27:01 +08001691 if (IS_ERR(ctxt.handle)) {
1692 ret = PTR_ERR(ctxt.handle);
1693 mlog_errno(ret);
1694 goto out;
1695 }
1696
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001697 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1698 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1699
1700 if (!ocfs2_xattr_is_local(entry)) {
1701 struct ocfs2_xattr_value_root *xv;
1702 void *val;
1703
1704 val = (void *)header +
1705 le16_to_cpu(entry->xe_name_offset);
1706 xv = (struct ocfs2_xattr_value_root *)
1707 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
Tao Ma78f30c32008-11-12 08:27:00 +08001708 ret = ocfs2_xattr_value_truncate(inode, bh, xv,
1709 0, &ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001710 if (ret < 0) {
1711 mlog_errno(ret);
Tao Ma78f30c32008-11-12 08:27:00 +08001712 break;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001713 }
1714 }
1715 }
1716
Tao Ma85db90e2008-11-12 08:27:01 +08001717 ocfs2_commit_trans(osb, ctxt.handle);
Tao Ma78f30c32008-11-12 08:27:00 +08001718 ocfs2_schedule_truncate_log_flush(osb, 1);
1719 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tao Ma85db90e2008-11-12 08:27:01 +08001720out:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001721 return ret;
1722}
1723
1724static int ocfs2_xattr_ibody_remove(struct inode *inode,
1725 struct buffer_head *di_bh)
1726{
1727
1728 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1729 struct ocfs2_xattr_header *header;
1730 int ret;
1731
1732 header = (struct ocfs2_xattr_header *)
1733 ((void *)di + inode->i_sb->s_blocksize -
1734 le16_to_cpu(di->i_xattr_inline_size));
1735
1736 ret = ocfs2_remove_value_outside(inode, di_bh, header);
1737
1738 return ret;
1739}
1740
1741static int ocfs2_xattr_block_remove(struct inode *inode,
1742 struct buffer_head *blk_bh)
1743{
1744 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001745 int ret = 0;
1746
1747 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Maa3944252008-08-18 17:38:54 +08001748 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1749 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
1750 ret = ocfs2_remove_value_outside(inode, blk_bh, header);
1751 } else
1752 ret = ocfs2_delete_xattr_index_block(inode, blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001753
1754 return ret;
1755}
1756
Tao Ma08413892008-08-29 09:00:19 +08001757static int ocfs2_xattr_free_block(struct inode *inode,
1758 u64 block)
1759{
1760 struct inode *xb_alloc_inode;
1761 struct buffer_head *xb_alloc_bh = NULL;
1762 struct buffer_head *blk_bh = NULL;
1763 struct ocfs2_xattr_block *xb;
1764 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1765 handle_t *handle;
1766 int ret = 0;
1767 u64 blk, bg_blkno;
1768 u16 bit;
1769
Joel Becker4ae1d692008-11-13 14:49:18 -08001770 ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
Tao Ma08413892008-08-29 09:00:19 +08001771 if (ret < 0) {
1772 mlog_errno(ret);
1773 goto out;
1774 }
1775
Tao Ma08413892008-08-29 09:00:19 +08001776 ret = ocfs2_xattr_block_remove(inode, blk_bh);
1777 if (ret < 0) {
1778 mlog_errno(ret);
1779 goto out;
1780 }
1781
Joel Becker4ae1d692008-11-13 14:49:18 -08001782 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Ma08413892008-08-29 09:00:19 +08001783 blk = le64_to_cpu(xb->xb_blkno);
1784 bit = le16_to_cpu(xb->xb_suballoc_bit);
1785 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1786
1787 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1788 EXTENT_ALLOC_SYSTEM_INODE,
1789 le16_to_cpu(xb->xb_suballoc_slot));
1790 if (!xb_alloc_inode) {
1791 ret = -ENOMEM;
1792 mlog_errno(ret);
1793 goto out;
1794 }
1795 mutex_lock(&xb_alloc_inode->i_mutex);
1796
1797 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1798 if (ret < 0) {
1799 mlog_errno(ret);
1800 goto out_mutex;
1801 }
1802
1803 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
1804 if (IS_ERR(handle)) {
1805 ret = PTR_ERR(handle);
1806 mlog_errno(ret);
1807 goto out_unlock;
1808 }
1809
1810 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1811 bit, bg_blkno, 1);
1812 if (ret < 0)
1813 mlog_errno(ret);
1814
1815 ocfs2_commit_trans(osb, handle);
1816out_unlock:
1817 ocfs2_inode_unlock(xb_alloc_inode, 1);
1818 brelse(xb_alloc_bh);
1819out_mutex:
1820 mutex_unlock(&xb_alloc_inode->i_mutex);
1821 iput(xb_alloc_inode);
1822out:
1823 brelse(blk_bh);
1824 return ret;
1825}
1826
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001827/*
1828 * ocfs2_xattr_remove()
1829 *
1830 * Free extended attribute resources associated with this inode.
1831 */
1832int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
1833{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001834 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1835 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1836 handle_t *handle;
1837 int ret;
1838
Tiger Yang8154da32008-08-18 17:11:46 +08001839 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1840 return 0;
1841
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001842 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1843 return 0;
1844
1845 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1846 ret = ocfs2_xattr_ibody_remove(inode, di_bh);
1847 if (ret < 0) {
1848 mlog_errno(ret);
1849 goto out;
1850 }
1851 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001852
Tao Ma08413892008-08-29 09:00:19 +08001853 if (di->i_xattr_loc) {
1854 ret = ocfs2_xattr_free_block(inode,
1855 le64_to_cpu(di->i_xattr_loc));
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001856 if (ret < 0) {
1857 mlog_errno(ret);
1858 goto out;
1859 }
1860 }
1861
1862 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1863 OCFS2_INODE_UPDATE_CREDITS);
1864 if (IS_ERR(handle)) {
1865 ret = PTR_ERR(handle);
1866 mlog_errno(ret);
1867 goto out;
1868 }
1869 ret = ocfs2_journal_access(handle, inode, di_bh,
1870 OCFS2_JOURNAL_ACCESS_WRITE);
1871 if (ret) {
1872 mlog_errno(ret);
1873 goto out_commit;
1874 }
1875
Tao Ma08413892008-08-29 09:00:19 +08001876 di->i_xattr_loc = 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001877
1878 spin_lock(&oi->ip_lock);
1879 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
1880 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1881 spin_unlock(&oi->ip_lock);
1882
1883 ret = ocfs2_journal_dirty(handle, di_bh);
1884 if (ret < 0)
1885 mlog_errno(ret);
1886out_commit:
1887 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1888out:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001889 return ret;
1890}
1891
1892static int ocfs2_xattr_has_space_inline(struct inode *inode,
1893 struct ocfs2_dinode *di)
1894{
1895 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1896 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
1897 int free;
1898
1899 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
1900 return 0;
1901
1902 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1903 struct ocfs2_inline_data *idata = &di->id2.i_data;
1904 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
1905 } else if (ocfs2_inode_is_fast_symlink(inode)) {
1906 free = ocfs2_fast_symlink_chars(inode->i_sb) -
1907 le64_to_cpu(di->i_size);
1908 } else {
1909 struct ocfs2_extent_list *el = &di->id2.i_list;
1910 free = (le16_to_cpu(el->l_count) -
1911 le16_to_cpu(el->l_next_free_rec)) *
1912 sizeof(struct ocfs2_extent_rec);
1913 }
1914 if (free >= xattrsize)
1915 return 1;
1916
1917 return 0;
1918}
1919
1920/*
1921 * ocfs2_xattr_ibody_find()
1922 *
1923 * Find extended attribute in inode block and
1924 * fill search info into struct ocfs2_xattr_search.
1925 */
1926static int ocfs2_xattr_ibody_find(struct inode *inode,
1927 int name_index,
1928 const char *name,
1929 struct ocfs2_xattr_search *xs)
1930{
1931 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1932 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1933 int ret;
1934 int has_space = 0;
1935
1936 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1937 return 0;
1938
1939 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1940 down_read(&oi->ip_alloc_sem);
1941 has_space = ocfs2_xattr_has_space_inline(inode, di);
1942 up_read(&oi->ip_alloc_sem);
1943 if (!has_space)
1944 return 0;
1945 }
1946
1947 xs->xattr_bh = xs->inode_bh;
1948 xs->end = (void *)di + inode->i_sb->s_blocksize;
1949 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
1950 xs->header = (struct ocfs2_xattr_header *)
1951 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1952 else
1953 xs->header = (struct ocfs2_xattr_header *)
1954 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
1955 xs->base = (void *)xs->header;
1956 xs->here = xs->header->xh_entries;
1957
1958 /* Find the named attribute. */
1959 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1960 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1961 if (ret && ret != -ENODATA)
1962 return ret;
1963 xs->not_found = ret;
1964 }
1965
1966 return 0;
1967}
1968
1969/*
1970 * ocfs2_xattr_ibody_set()
1971 *
1972 * Set, replace or remove an extended attribute into inode block.
1973 *
1974 */
1975static int ocfs2_xattr_ibody_set(struct inode *inode,
1976 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08001977 struct ocfs2_xattr_search *xs,
1978 struct ocfs2_xattr_set_ctxt *ctxt)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001979{
1980 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1981 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1982 int ret;
1983
1984 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1985 return -ENOSPC;
1986
1987 down_write(&oi->ip_alloc_sem);
1988 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1989 if (!ocfs2_xattr_has_space_inline(inode, di)) {
1990 ret = -ENOSPC;
1991 goto out;
1992 }
1993 }
1994
Tao Ma78f30c32008-11-12 08:27:00 +08001995 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001996 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
1997out:
1998 up_write(&oi->ip_alloc_sem);
1999
2000 return ret;
2001}
2002
2003/*
2004 * ocfs2_xattr_block_find()
2005 *
2006 * Find extended attribute in external block and
2007 * fill search info into struct ocfs2_xattr_search.
2008 */
2009static int ocfs2_xattr_block_find(struct inode *inode,
2010 int name_index,
2011 const char *name,
2012 struct ocfs2_xattr_search *xs)
2013{
2014 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2015 struct buffer_head *blk_bh = NULL;
Tao Ma589dc262008-08-18 17:38:51 +08002016 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002017 int ret = 0;
2018
2019 if (!di->i_xattr_loc)
2020 return ret;
2021
Joel Becker4ae1d692008-11-13 14:49:18 -08002022 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
2023 &blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002024 if (ret < 0) {
2025 mlog_errno(ret);
2026 return ret;
2027 }
Joel Beckerf6087fb2008-10-20 18:20:43 -07002028
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002029 xs->xattr_bh = blk_bh;
Joel Becker4ae1d692008-11-13 14:49:18 -08002030 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002031
Tao Ma589dc262008-08-18 17:38:51 +08002032 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2033 xs->header = &xb->xb_attrs.xb_header;
2034 xs->base = (void *)xs->header;
2035 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
2036 xs->here = xs->header->xh_entries;
2037
2038 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2039 } else
2040 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
2041 name_index,
2042 name, xs);
2043
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002044 if (ret && ret != -ENODATA) {
2045 xs->xattr_bh = NULL;
2046 goto cleanup;
2047 }
2048 xs->not_found = ret;
2049 return 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002050cleanup:
2051 brelse(blk_bh);
2052
2053 return ret;
2054}
2055
2056/*
2057 * ocfs2_xattr_block_set()
2058 *
2059 * Set, replace or remove an extended attribute into external block.
2060 *
2061 */
2062static int ocfs2_xattr_block_set(struct inode *inode,
2063 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08002064 struct ocfs2_xattr_search *xs,
2065 struct ocfs2_xattr_set_ctxt *ctxt)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002066{
2067 struct buffer_head *new_bh = NULL;
2068 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2069 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
Tao Ma85db90e2008-11-12 08:27:01 +08002070 handle_t *handle = ctxt->handle;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002071 struct ocfs2_xattr_block *xblk = NULL;
2072 u16 suballoc_bit_start;
2073 u32 num_got;
2074 u64 first_blkno;
2075 int ret;
2076
2077 if (!xs->xattr_bh) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002078 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
2079 OCFS2_JOURNAL_ACCESS_CREATE);
2080 if (ret < 0) {
2081 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08002082 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002083 }
2084
Tao Ma78f30c32008-11-12 08:27:00 +08002085 ret = ocfs2_claim_metadata(osb, handle, ctxt->meta_ac, 1,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002086 &suballoc_bit_start, &num_got,
2087 &first_blkno);
2088 if (ret < 0) {
2089 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08002090 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002091 }
2092
2093 new_bh = sb_getblk(inode->i_sb, first_blkno);
2094 ocfs2_set_new_buffer_uptodate(inode, new_bh);
2095
2096 ret = ocfs2_journal_access(handle, inode, new_bh,
2097 OCFS2_JOURNAL_ACCESS_CREATE);
2098 if (ret < 0) {
2099 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08002100 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002101 }
2102
2103 /* Initialize ocfs2_xattr_block */
2104 xs->xattr_bh = new_bh;
2105 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2106 memset(xblk, 0, inode->i_sb->s_blocksize);
2107 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
2108 xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
2109 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
2110 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
2111 xblk->xb_blkno = cpu_to_le64(first_blkno);
2112
2113 xs->header = &xblk->xb_attrs.xb_header;
2114 xs->base = (void *)xs->header;
2115 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2116 xs->here = xs->header->xh_entries;
2117
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002118 ret = ocfs2_journal_dirty(handle, new_bh);
2119 if (ret < 0) {
2120 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08002121 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002122 }
2123 di->i_xattr_loc = cpu_to_le64(first_blkno);
Tao Ma85db90e2008-11-12 08:27:01 +08002124 ocfs2_journal_dirty(handle, xs->inode_bh);
Tao Ma01225592008-08-18 17:38:53 +08002125 } else
2126 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2127
2128 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
2129 /* Set extended attribute into external block */
Tao Ma78f30c32008-11-12 08:27:00 +08002130 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
2131 OCFS2_HAS_XATTR_FL);
Tao Ma01225592008-08-18 17:38:53 +08002132 if (!ret || ret != -ENOSPC)
2133 goto end;
2134
Tao Ma78f30c32008-11-12 08:27:00 +08002135 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08002136 if (ret)
2137 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002138 }
2139
Tao Ma78f30c32008-11-12 08:27:00 +08002140 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08002141
2142end:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002143
2144 return ret;
2145}
2146
Tao Ma78f30c32008-11-12 08:27:00 +08002147/* Check whether the new xattr can be inserted into the inode. */
2148static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
2149 struct ocfs2_xattr_info *xi,
2150 struct ocfs2_xattr_search *xs)
2151{
2152 u64 value_size;
2153 struct ocfs2_xattr_entry *last;
2154 int free, i;
2155 size_t min_offs = xs->end - xs->base;
2156
2157 if (!xs->header)
2158 return 0;
2159
2160 last = xs->header->xh_entries;
2161
2162 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
2163 size_t offs = le16_to_cpu(last->xe_name_offset);
2164 if (offs < min_offs)
2165 min_offs = offs;
2166 last += 1;
2167 }
2168
2169 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
2170 if (free < 0)
2171 return 0;
2172
2173 BUG_ON(!xs->not_found);
2174
2175 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
2176 value_size = OCFS2_XATTR_ROOT_SIZE;
2177 else
2178 value_size = OCFS2_XATTR_SIZE(xi->value_len);
2179
2180 if (free >= sizeof(struct ocfs2_xattr_entry) +
2181 OCFS2_XATTR_SIZE(strlen(xi->name)) + value_size)
2182 return 1;
2183
2184 return 0;
2185}
2186
2187static int ocfs2_calc_xattr_set_need(struct inode *inode,
2188 struct ocfs2_dinode *di,
2189 struct ocfs2_xattr_info *xi,
2190 struct ocfs2_xattr_search *xis,
2191 struct ocfs2_xattr_search *xbs,
2192 int *clusters_need,
Tao Ma85db90e2008-11-12 08:27:01 +08002193 int *meta_need,
2194 int *credits_need)
Tao Ma78f30c32008-11-12 08:27:00 +08002195{
2196 int ret = 0, old_in_xb = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08002197 int clusters_add = 0, meta_add = 0, credits = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08002198 struct buffer_head *bh = NULL;
2199 struct ocfs2_xattr_block *xb = NULL;
2200 struct ocfs2_xattr_entry *xe = NULL;
2201 struct ocfs2_xattr_value_root *xv = NULL;
2202 char *base = NULL;
2203 int name_offset, name_len = 0;
2204 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2205 xi->value_len);
2206 u64 value_size;
2207
Tao Ma78f30c32008-11-12 08:27:00 +08002208 if (xis->not_found && xbs->not_found) {
Tao Ma85db90e2008-11-12 08:27:01 +08002209 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2210
2211 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
Tao Ma78f30c32008-11-12 08:27:00 +08002212 clusters_add += new_clusters;
Tao Ma85db90e2008-11-12 08:27:01 +08002213 credits += ocfs2_calc_extend_credits(inode->i_sb,
2214 &def_xv.xv.xr_list,
2215 new_clusters);
2216 }
Tao Ma78f30c32008-11-12 08:27:00 +08002217
2218 goto meta_guess;
2219 }
2220
2221 if (!xis->not_found) {
2222 xe = xis->here;
2223 name_offset = le16_to_cpu(xe->xe_name_offset);
2224 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2225 base = xis->base;
Tao Ma85db90e2008-11-12 08:27:01 +08002226 credits += OCFS2_INODE_UPDATE_CREDITS;
Tao Ma78f30c32008-11-12 08:27:00 +08002227 } else {
Joel Becker970e4932008-11-13 14:49:19 -08002228 int i, block_off = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08002229 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2230 xe = xbs->here;
2231 name_offset = le16_to_cpu(xe->xe_name_offset);
2232 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2233 i = xbs->here - xbs->header->xh_entries;
2234 old_in_xb = 1;
2235
2236 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2237 ret = ocfs2_xattr_bucket_get_name_value(inode,
2238 bucket_xh(xbs->bucket),
2239 i, &block_off,
2240 &name_offset);
2241 base = bucket_block(xbs->bucket, block_off);
Tao Ma85db90e2008-11-12 08:27:01 +08002242 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2243 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08002244 base = xbs->base;
Tao Ma85db90e2008-11-12 08:27:01 +08002245 credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
2246 }
2247 }
2248
2249 /*
2250 * delete a xattr doesn't need metadata and cluster allocation.
2251 * so just calculate the credits and return.
2252 *
2253 * The credits for removing the value tree will be extended
2254 * by ocfs2_remove_extent itself.
2255 */
2256 if (!xi->value) {
2257 if (!ocfs2_xattr_is_local(xe))
Jan Karaa90714c2008-10-09 19:38:40 +02002258 credits += ocfs2_remove_extent_credits(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08002259
2260 goto out;
Tao Ma78f30c32008-11-12 08:27:00 +08002261 }
2262
2263 /* do cluster allocation guess first. */
2264 value_size = le64_to_cpu(xe->xe_value_size);
2265
2266 if (old_in_xb) {
2267 /*
2268 * In xattr set, we always try to set the xe in inode first,
2269 * so if it can be inserted into inode successfully, the old
2270 * one will be removed from the xattr block, and this xattr
2271 * will be inserted into inode as a new xattr in inode.
2272 */
2273 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
2274 clusters_add += new_clusters;
Jan Karaa90714c2008-10-09 19:38:40 +02002275 credits += ocfs2_remove_extent_credits(inode->i_sb) +
Tao Ma85db90e2008-11-12 08:27:01 +08002276 OCFS2_INODE_UPDATE_CREDITS;
2277 if (!ocfs2_xattr_is_local(xe))
2278 credits += ocfs2_calc_extend_credits(
2279 inode->i_sb,
2280 &def_xv.xv.xr_list,
2281 new_clusters);
Tao Ma78f30c32008-11-12 08:27:00 +08002282 goto out;
2283 }
2284 }
2285
2286 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
2287 /* the new values will be stored outside. */
2288 u32 old_clusters = 0;
2289
2290 if (!ocfs2_xattr_is_local(xe)) {
2291 old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2292 value_size);
2293 xv = (struct ocfs2_xattr_value_root *)
2294 (base + name_offset + name_len);
Tao Ma97aff522008-11-19 16:48:41 +08002295 value_size = OCFS2_XATTR_ROOT_SIZE;
Tao Ma78f30c32008-11-12 08:27:00 +08002296 } else
2297 xv = &def_xv.xv;
2298
Tao Ma85db90e2008-11-12 08:27:01 +08002299 if (old_clusters >= new_clusters) {
Jan Karaa90714c2008-10-09 19:38:40 +02002300 credits += ocfs2_remove_extent_credits(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08002301 goto out;
Tao Ma85db90e2008-11-12 08:27:01 +08002302 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08002303 meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
2304 clusters_add += new_clusters - old_clusters;
Tao Ma85db90e2008-11-12 08:27:01 +08002305 credits += ocfs2_calc_extend_credits(inode->i_sb,
2306 &xv->xr_list,
2307 new_clusters -
2308 old_clusters);
Tao Ma97aff522008-11-19 16:48:41 +08002309 if (value_size >= OCFS2_XATTR_ROOT_SIZE)
2310 goto out;
Tao Ma78f30c32008-11-12 08:27:00 +08002311 }
2312 } else {
2313 /*
2314 * Now the new value will be stored inside. So if the new
2315 * value is smaller than the size of value root or the old
2316 * value, we don't need any allocation, otherwise we have
2317 * to guess metadata allocation.
2318 */
2319 if ((ocfs2_xattr_is_local(xe) && value_size >= xi->value_len) ||
2320 (!ocfs2_xattr_is_local(xe) &&
2321 OCFS2_XATTR_ROOT_SIZE >= xi->value_len))
2322 goto out;
2323 }
2324
2325meta_guess:
2326 /* calculate metadata allocation. */
2327 if (di->i_xattr_loc) {
2328 if (!xbs->xattr_bh) {
Joel Becker4ae1d692008-11-13 14:49:18 -08002329 ret = ocfs2_read_xattr_block(inode,
2330 le64_to_cpu(di->i_xattr_loc),
2331 &bh);
Tao Ma78f30c32008-11-12 08:27:00 +08002332 if (ret) {
2333 mlog_errno(ret);
2334 goto out;
2335 }
2336
2337 xb = (struct ocfs2_xattr_block *)bh->b_data;
2338 } else
2339 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2340
2341 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2342 struct ocfs2_extent_list *el =
2343 &xb->xb_attrs.xb_root.xt_list;
2344 meta_add += ocfs2_extend_meta_needed(el);
Tao Ma85db90e2008-11-12 08:27:01 +08002345 credits += ocfs2_calc_extend_credits(inode->i_sb,
2346 el, 1);
Tao Ma78f30c32008-11-12 08:27:00 +08002347 }
2348
2349 /*
2350 * This cluster will be used either for new bucket or for
2351 * new xattr block.
2352 * If the cluster size is the same as the bucket size, one
2353 * more is needed since we may need to extend the bucket
2354 * also.
2355 */
2356 clusters_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08002357 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08002358 if (OCFS2_XATTR_BUCKET_SIZE ==
Tao Ma85db90e2008-11-12 08:27:01 +08002359 OCFS2_SB(inode->i_sb)->s_clustersize) {
2360 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08002361 clusters_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08002362 }
2363 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08002364 meta_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08002365 credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
2366 }
Tao Ma78f30c32008-11-12 08:27:00 +08002367out:
2368 if (clusters_need)
2369 *clusters_need = clusters_add;
2370 if (meta_need)
2371 *meta_need = meta_add;
Tao Ma85db90e2008-11-12 08:27:01 +08002372 if (credits_need)
2373 *credits_need = credits;
Tao Ma78f30c32008-11-12 08:27:00 +08002374 brelse(bh);
2375 return ret;
2376}
2377
2378static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
2379 struct ocfs2_dinode *di,
2380 struct ocfs2_xattr_info *xi,
2381 struct ocfs2_xattr_search *xis,
2382 struct ocfs2_xattr_search *xbs,
Tao Ma85db90e2008-11-12 08:27:01 +08002383 struct ocfs2_xattr_set_ctxt *ctxt,
2384 int *credits)
Tao Ma78f30c32008-11-12 08:27:00 +08002385{
2386 int clusters_add, meta_add, ret;
2387 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2388
2389 memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
2390
2391 ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
2392
2393 ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
Tao Ma85db90e2008-11-12 08:27:01 +08002394 &clusters_add, &meta_add, credits);
Tao Ma78f30c32008-11-12 08:27:00 +08002395 if (ret) {
2396 mlog_errno(ret);
2397 return ret;
2398 }
2399
Tao Ma85db90e2008-11-12 08:27:01 +08002400 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
2401 "credits = %d\n", xi->name, meta_add, clusters_add, *credits);
Tao Ma78f30c32008-11-12 08:27:00 +08002402
2403 if (meta_add) {
2404 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
2405 &ctxt->meta_ac);
2406 if (ret) {
2407 mlog_errno(ret);
2408 goto out;
2409 }
2410 }
2411
2412 if (clusters_add) {
2413 ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
2414 if (ret)
2415 mlog_errno(ret);
2416 }
2417out:
2418 if (ret) {
2419 if (ctxt->meta_ac) {
2420 ocfs2_free_alloc_context(ctxt->meta_ac);
2421 ctxt->meta_ac = NULL;
2422 }
2423
2424 /*
2425 * We cannot have an error and a non null ctxt->data_ac.
2426 */
2427 }
2428
2429 return ret;
2430}
2431
Tao Ma85db90e2008-11-12 08:27:01 +08002432static int __ocfs2_xattr_set_handle(struct inode *inode,
2433 struct ocfs2_dinode *di,
2434 struct ocfs2_xattr_info *xi,
2435 struct ocfs2_xattr_search *xis,
2436 struct ocfs2_xattr_search *xbs,
2437 struct ocfs2_xattr_set_ctxt *ctxt)
2438{
Tao Ma9f868f12008-11-19 16:48:42 +08002439 int ret = 0, credits, old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08002440
2441 if (!xi->value) {
2442 /* Remove existing extended attribute */
2443 if (!xis->not_found)
2444 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2445 else if (!xbs->not_found)
2446 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2447 } else {
2448 /* We always try to set extended attribute into inode first*/
2449 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2450 if (!ret && !xbs->not_found) {
2451 /*
2452 * If succeed and that extended attribute existing in
2453 * external block, then we will remove it.
2454 */
2455 xi->value = NULL;
2456 xi->value_len = 0;
2457
Tao Ma9f868f12008-11-19 16:48:42 +08002458 old_found = xis->not_found;
Tao Ma85db90e2008-11-12 08:27:01 +08002459 xis->not_found = -ENODATA;
2460 ret = ocfs2_calc_xattr_set_need(inode,
2461 di,
2462 xi,
2463 xis,
2464 xbs,
2465 NULL,
2466 NULL,
2467 &credits);
Tao Ma9f868f12008-11-19 16:48:42 +08002468 xis->not_found = old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08002469 if (ret) {
2470 mlog_errno(ret);
2471 goto out;
2472 }
2473
2474 ret = ocfs2_extend_trans(ctxt->handle, credits +
2475 ctxt->handle->h_buffer_credits);
2476 if (ret) {
2477 mlog_errno(ret);
2478 goto out;
2479 }
2480 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2481 } else if (ret == -ENOSPC) {
2482 if (di->i_xattr_loc && !xbs->xattr_bh) {
2483 ret = ocfs2_xattr_block_find(inode,
2484 xi->name_index,
2485 xi->name, xbs);
2486 if (ret)
2487 goto out;
2488
Tao Ma9f868f12008-11-19 16:48:42 +08002489 old_found = xis->not_found;
Tao Ma85db90e2008-11-12 08:27:01 +08002490 xis->not_found = -ENODATA;
2491 ret = ocfs2_calc_xattr_set_need(inode,
2492 di,
2493 xi,
2494 xis,
2495 xbs,
2496 NULL,
2497 NULL,
2498 &credits);
Tao Ma9f868f12008-11-19 16:48:42 +08002499 xis->not_found = old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08002500 if (ret) {
2501 mlog_errno(ret);
2502 goto out;
2503 }
2504
2505 ret = ocfs2_extend_trans(ctxt->handle, credits +
2506 ctxt->handle->h_buffer_credits);
2507 if (ret) {
2508 mlog_errno(ret);
2509 goto out;
2510 }
2511 }
2512 /*
2513 * If no space in inode, we will set extended attribute
2514 * into external block.
2515 */
2516 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2517 if (ret)
2518 goto out;
2519 if (!xis->not_found) {
2520 /*
2521 * If succeed and that extended attribute
2522 * existing in inode, we will remove it.
2523 */
2524 xi->value = NULL;
2525 xi->value_len = 0;
2526 xbs->not_found = -ENODATA;
2527 ret = ocfs2_calc_xattr_set_need(inode,
2528 di,
2529 xi,
2530 xis,
2531 xbs,
2532 NULL,
2533 NULL,
2534 &credits);
2535 if (ret) {
2536 mlog_errno(ret);
2537 goto out;
2538 }
2539
2540 ret = ocfs2_extend_trans(ctxt->handle, credits +
2541 ctxt->handle->h_buffer_credits);
2542 if (ret) {
2543 mlog_errno(ret);
2544 goto out;
2545 }
2546 ret = ocfs2_xattr_ibody_set(inode, xi,
2547 xis, ctxt);
2548 }
2549 }
2550 }
2551
2552out:
2553 return ret;
2554}
2555
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002556/*
Tiger Yang6c3faba2008-11-14 11:16:03 +08002557 * This function only called duing creating inode
2558 * for init security/acl xattrs of the new inode.
2559 * The xattrs could be put into ibody or extent block,
2560 * xattr bucket would not be use in this case.
2561 * transanction credits also be reserved in here.
2562 */
2563int ocfs2_xattr_set_handle(handle_t *handle,
2564 struct inode *inode,
2565 struct buffer_head *di_bh,
2566 int name_index,
2567 const char *name,
2568 const void *value,
2569 size_t value_len,
2570 int flags,
2571 struct ocfs2_alloc_context *meta_ac,
2572 struct ocfs2_alloc_context *data_ac)
2573{
2574 struct ocfs2_dinode *di;
2575 int ret;
2576
2577 struct ocfs2_xattr_info xi = {
2578 .name_index = name_index,
2579 .name = name,
2580 .value = value,
2581 .value_len = value_len,
2582 };
2583
2584 struct ocfs2_xattr_search xis = {
2585 .not_found = -ENODATA,
2586 };
2587
2588 struct ocfs2_xattr_search xbs = {
2589 .not_found = -ENODATA,
2590 };
2591
2592 struct ocfs2_xattr_set_ctxt ctxt = {
2593 .handle = handle,
2594 .meta_ac = meta_ac,
2595 .data_ac = data_ac,
2596 };
2597
2598 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2599 return -EOPNOTSUPP;
2600
2601 xis.inode_bh = xbs.inode_bh = di_bh;
2602 di = (struct ocfs2_dinode *)di_bh->b_data;
2603
2604 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2605
2606 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2607 if (ret)
2608 goto cleanup;
2609 if (xis.not_found) {
2610 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2611 if (ret)
2612 goto cleanup;
2613 }
2614
2615 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
2616
2617cleanup:
2618 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2619 brelse(xbs.xattr_bh);
2620
2621 return ret;
2622}
2623
2624/*
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002625 * ocfs2_xattr_set()
2626 *
2627 * Set, replace or remove an extended attribute for this inode.
2628 * value is NULL to remove an existing extended attribute, else either
2629 * create or replace an extended attribute.
2630 */
2631int ocfs2_xattr_set(struct inode *inode,
2632 int name_index,
2633 const char *name,
2634 const void *value,
2635 size_t value_len,
2636 int flags)
2637{
2638 struct buffer_head *di_bh = NULL;
2639 struct ocfs2_dinode *di;
Tao Ma85db90e2008-11-12 08:27:01 +08002640 int ret, credits;
Tao Ma78f30c32008-11-12 08:27:00 +08002641 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08002642 struct inode *tl_inode = osb->osb_tl_inode;
Tao Ma78f30c32008-11-12 08:27:00 +08002643 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002644
2645 struct ocfs2_xattr_info xi = {
2646 .name_index = name_index,
2647 .name = name,
2648 .value = value,
2649 .value_len = value_len,
2650 };
2651
2652 struct ocfs2_xattr_search xis = {
2653 .not_found = -ENODATA,
2654 };
2655
2656 struct ocfs2_xattr_search xbs = {
2657 .not_found = -ENODATA,
2658 };
2659
Tiger Yang8154da32008-08-18 17:11:46 +08002660 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2661 return -EOPNOTSUPP;
2662
Joel Beckerba937122008-10-24 19:13:20 -07002663 /*
2664 * Only xbs will be used on indexed trees. xis doesn't need a
2665 * bucket.
2666 */
2667 xbs.bucket = ocfs2_xattr_bucket_new(inode);
2668 if (!xbs.bucket) {
2669 mlog_errno(-ENOMEM);
2670 return -ENOMEM;
2671 }
2672
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002673 ret = ocfs2_inode_lock(inode, &di_bh, 1);
2674 if (ret < 0) {
2675 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07002676 goto cleanup_nolock;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002677 }
2678 xis.inode_bh = xbs.inode_bh = di_bh;
2679 di = (struct ocfs2_dinode *)di_bh->b_data;
2680
2681 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2682 /*
2683 * Scan inode and external block to find the same name
2684 * extended attribute and collect search infomation.
2685 */
2686 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2687 if (ret)
2688 goto cleanup;
2689 if (xis.not_found) {
2690 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2691 if (ret)
2692 goto cleanup;
2693 }
2694
2695 if (xis.not_found && xbs.not_found) {
2696 ret = -ENODATA;
2697 if (flags & XATTR_REPLACE)
2698 goto cleanup;
2699 ret = 0;
2700 if (!value)
2701 goto cleanup;
2702 } else {
2703 ret = -EEXIST;
2704 if (flags & XATTR_CREATE)
2705 goto cleanup;
2706 }
2707
Tao Ma85db90e2008-11-12 08:27:01 +08002708
2709 mutex_lock(&tl_inode->i_mutex);
2710
2711 if (ocfs2_truncate_log_needs_flush(osb)) {
2712 ret = __ocfs2_flush_truncate_log(osb);
2713 if (ret < 0) {
2714 mutex_unlock(&tl_inode->i_mutex);
2715 mlog_errno(ret);
2716 goto cleanup;
2717 }
2718 }
2719 mutex_unlock(&tl_inode->i_mutex);
2720
2721 ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
2722 &xbs, &ctxt, &credits);
Tao Ma78f30c32008-11-12 08:27:00 +08002723 if (ret) {
2724 mlog_errno(ret);
2725 goto cleanup;
2726 }
2727
Tao Ma85db90e2008-11-12 08:27:01 +08002728 ctxt.handle = ocfs2_start_trans(osb, credits);
2729 if (IS_ERR(ctxt.handle)) {
2730 ret = PTR_ERR(ctxt.handle);
2731 mlog_errno(ret);
2732 goto cleanup;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002733 }
Tao Ma85db90e2008-11-12 08:27:01 +08002734
2735 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
2736
2737 ocfs2_commit_trans(osb, ctxt.handle);
2738
Tao Ma78f30c32008-11-12 08:27:00 +08002739 if (ctxt.data_ac)
2740 ocfs2_free_alloc_context(ctxt.data_ac);
2741 if (ctxt.meta_ac)
2742 ocfs2_free_alloc_context(ctxt.meta_ac);
2743 if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
2744 ocfs2_schedule_truncate_log_flush(osb, 1);
2745 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002746cleanup:
2747 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2748 ocfs2_inode_unlock(inode, 1);
Joel Beckerba937122008-10-24 19:13:20 -07002749cleanup_nolock:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002750 brelse(di_bh);
2751 brelse(xbs.xattr_bh);
Joel Beckerba937122008-10-24 19:13:20 -07002752 ocfs2_xattr_bucket_free(xbs.bucket);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002753
2754 return ret;
2755}
2756
Tao Ma0c044f02008-08-18 17:38:50 +08002757/*
2758 * Find the xattr extent rec which may contains name_hash.
2759 * e_cpos will be the first name hash of the xattr rec.
2760 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
2761 */
2762static int ocfs2_xattr_get_rec(struct inode *inode,
2763 u32 name_hash,
2764 u64 *p_blkno,
2765 u32 *e_cpos,
2766 u32 *num_clusters,
2767 struct ocfs2_extent_list *el)
2768{
2769 int ret = 0, i;
2770 struct buffer_head *eb_bh = NULL;
2771 struct ocfs2_extent_block *eb;
2772 struct ocfs2_extent_rec *rec = NULL;
2773 u64 e_blkno = 0;
2774
2775 if (el->l_tree_depth) {
2776 ret = ocfs2_find_leaf(inode, el, name_hash, &eb_bh);
2777 if (ret) {
2778 mlog_errno(ret);
2779 goto out;
2780 }
2781
2782 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
2783 el = &eb->h_list;
2784
2785 if (el->l_tree_depth) {
2786 ocfs2_error(inode->i_sb,
2787 "Inode %lu has non zero tree depth in "
2788 "xattr tree block %llu\n", inode->i_ino,
2789 (unsigned long long)eb_bh->b_blocknr);
2790 ret = -EROFS;
2791 goto out;
2792 }
2793 }
2794
2795 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
2796 rec = &el->l_recs[i];
2797
2798 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
2799 e_blkno = le64_to_cpu(rec->e_blkno);
2800 break;
2801 }
2802 }
2803
2804 if (!e_blkno) {
2805 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
2806 "record (%u, %u, 0) in xattr", inode->i_ino,
2807 le32_to_cpu(rec->e_cpos),
2808 ocfs2_rec_clusters(el, rec));
2809 ret = -EROFS;
2810 goto out;
2811 }
2812
2813 *p_blkno = le64_to_cpu(rec->e_blkno);
2814 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
2815 if (e_cpos)
2816 *e_cpos = le32_to_cpu(rec->e_cpos);
2817out:
2818 brelse(eb_bh);
2819 return ret;
2820}
2821
2822typedef int (xattr_bucket_func)(struct inode *inode,
2823 struct ocfs2_xattr_bucket *bucket,
2824 void *para);
2825
Tao Ma589dc262008-08-18 17:38:51 +08002826static int ocfs2_find_xe_in_bucket(struct inode *inode,
Joel Beckere2356a32008-10-27 15:01:54 -07002827 struct ocfs2_xattr_bucket *bucket,
Tao Ma589dc262008-08-18 17:38:51 +08002828 int name_index,
2829 const char *name,
2830 u32 name_hash,
2831 u16 *xe_index,
2832 int *found)
2833{
2834 int i, ret = 0, cmp = 1, block_off, new_offset;
Joel Beckere2356a32008-10-27 15:01:54 -07002835 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma589dc262008-08-18 17:38:51 +08002836 size_t name_len = strlen(name);
2837 struct ocfs2_xattr_entry *xe = NULL;
Tao Ma589dc262008-08-18 17:38:51 +08002838 char *xe_name;
2839
2840 /*
2841 * We don't use binary search in the bucket because there
2842 * may be multiple entries with the same name hash.
2843 */
2844 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
2845 xe = &xh->xh_entries[i];
2846
2847 if (name_hash > le32_to_cpu(xe->xe_name_hash))
2848 continue;
2849 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
2850 break;
2851
2852 cmp = name_index - ocfs2_xattr_get_type(xe);
2853 if (!cmp)
2854 cmp = name_len - xe->xe_name_len;
2855 if (cmp)
2856 continue;
2857
2858 ret = ocfs2_xattr_bucket_get_name_value(inode,
2859 xh,
2860 i,
2861 &block_off,
2862 &new_offset);
2863 if (ret) {
2864 mlog_errno(ret);
2865 break;
2866 }
2867
Joel Becker970e4932008-11-13 14:49:19 -08002868
Joel Beckere2356a32008-10-27 15:01:54 -07002869 xe_name = bucket_block(bucket, block_off) + new_offset;
2870 if (!memcmp(name, xe_name, name_len)) {
Tao Ma589dc262008-08-18 17:38:51 +08002871 *xe_index = i;
2872 *found = 1;
2873 ret = 0;
2874 break;
2875 }
2876 }
2877
2878 return ret;
2879}
2880
2881/*
2882 * Find the specified xattr entry in a series of buckets.
2883 * This series start from p_blkno and last for num_clusters.
2884 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
2885 * the num of the valid buckets.
2886 *
2887 * Return the buffer_head this xattr should reside in. And if the xattr's
2888 * hash is in the gap of 2 buckets, return the lower bucket.
2889 */
2890static int ocfs2_xattr_bucket_find(struct inode *inode,
2891 int name_index,
2892 const char *name,
2893 u32 name_hash,
2894 u64 p_blkno,
2895 u32 first_hash,
2896 u32 num_clusters,
2897 struct ocfs2_xattr_search *xs)
2898{
2899 int ret, found = 0;
Tao Ma589dc262008-08-18 17:38:51 +08002900 struct ocfs2_xattr_header *xh = NULL;
2901 struct ocfs2_xattr_entry *xe = NULL;
2902 u16 index = 0;
2903 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2904 int low_bucket = 0, bucket, high_bucket;
Joel Beckere2356a32008-10-27 15:01:54 -07002905 struct ocfs2_xattr_bucket *search;
Tao Ma589dc262008-08-18 17:38:51 +08002906 u32 last_hash;
Joel Beckere2356a32008-10-27 15:01:54 -07002907 u64 blkno, lower_blkno = 0;
Tao Ma589dc262008-08-18 17:38:51 +08002908
Joel Beckere2356a32008-10-27 15:01:54 -07002909 search = ocfs2_xattr_bucket_new(inode);
2910 if (!search) {
2911 ret = -ENOMEM;
2912 mlog_errno(ret);
2913 goto out;
2914 }
2915
2916 ret = ocfs2_read_xattr_bucket(search, p_blkno);
Tao Ma589dc262008-08-18 17:38:51 +08002917 if (ret) {
2918 mlog_errno(ret);
2919 goto out;
2920 }
2921
Joel Beckere2356a32008-10-27 15:01:54 -07002922 xh = bucket_xh(search);
Tao Ma589dc262008-08-18 17:38:51 +08002923 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
Tao Ma589dc262008-08-18 17:38:51 +08002924 while (low_bucket <= high_bucket) {
Joel Beckere2356a32008-10-27 15:01:54 -07002925 ocfs2_xattr_bucket_relse(search);
2926
Tao Ma589dc262008-08-18 17:38:51 +08002927 bucket = (low_bucket + high_bucket) / 2;
Tao Ma589dc262008-08-18 17:38:51 +08002928 blkno = p_blkno + bucket * blk_per_bucket;
Joel Beckere2356a32008-10-27 15:01:54 -07002929 ret = ocfs2_read_xattr_bucket(search, blkno);
Tao Ma589dc262008-08-18 17:38:51 +08002930 if (ret) {
2931 mlog_errno(ret);
2932 goto out;
2933 }
2934
Joel Beckere2356a32008-10-27 15:01:54 -07002935 xh = bucket_xh(search);
Tao Ma589dc262008-08-18 17:38:51 +08002936 xe = &xh->xh_entries[0];
2937 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
2938 high_bucket = bucket - 1;
2939 continue;
2940 }
2941
2942 /*
2943 * Check whether the hash of the last entry in our
Tao Ma5a095612008-09-19 22:17:41 +08002944 * bucket is larger than the search one. for an empty
2945 * bucket, the last one is also the first one.
Tao Ma589dc262008-08-18 17:38:51 +08002946 */
Tao Ma5a095612008-09-19 22:17:41 +08002947 if (xh->xh_count)
2948 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
2949
Tao Ma589dc262008-08-18 17:38:51 +08002950 last_hash = le32_to_cpu(xe->xe_name_hash);
2951
Joel Beckere2356a32008-10-27 15:01:54 -07002952 /* record lower_blkno which may be the insert place. */
2953 lower_blkno = blkno;
Tao Ma589dc262008-08-18 17:38:51 +08002954
2955 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
2956 low_bucket = bucket + 1;
2957 continue;
2958 }
2959
2960 /* the searched xattr should reside in this bucket if exists. */
Joel Beckere2356a32008-10-27 15:01:54 -07002961 ret = ocfs2_find_xe_in_bucket(inode, search,
Tao Ma589dc262008-08-18 17:38:51 +08002962 name_index, name, name_hash,
2963 &index, &found);
2964 if (ret) {
2965 mlog_errno(ret);
2966 goto out;
2967 }
2968 break;
2969 }
2970
2971 /*
2972 * Record the bucket we have found.
2973 * When the xattr's hash value is in the gap of 2 buckets, we will
2974 * always set it to the previous bucket.
2975 */
Joel Beckere2356a32008-10-27 15:01:54 -07002976 if (!lower_blkno)
2977 lower_blkno = p_blkno;
2978
2979 /* This should be in cache - we just read it during the search */
2980 ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
2981 if (ret) {
2982 mlog_errno(ret);
2983 goto out;
Tao Ma589dc262008-08-18 17:38:51 +08002984 }
Tao Ma589dc262008-08-18 17:38:51 +08002985
Joel Beckerba937122008-10-24 19:13:20 -07002986 xs->header = bucket_xh(xs->bucket);
2987 xs->base = bucket_block(xs->bucket, 0);
Tao Ma589dc262008-08-18 17:38:51 +08002988 xs->end = xs->base + inode->i_sb->s_blocksize;
2989
2990 if (found) {
Tao Ma589dc262008-08-18 17:38:51 +08002991 xs->here = &xs->header->xh_entries[index];
2992 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
Joel Beckerba937122008-10-24 19:13:20 -07002993 (unsigned long long)bucket_blkno(xs->bucket), index);
Tao Ma589dc262008-08-18 17:38:51 +08002994 } else
2995 ret = -ENODATA;
2996
2997out:
Joel Beckere2356a32008-10-27 15:01:54 -07002998 ocfs2_xattr_bucket_free(search);
Tao Ma589dc262008-08-18 17:38:51 +08002999 return ret;
3000}
3001
3002static int ocfs2_xattr_index_block_find(struct inode *inode,
3003 struct buffer_head *root_bh,
3004 int name_index,
3005 const char *name,
3006 struct ocfs2_xattr_search *xs)
3007{
3008 int ret;
3009 struct ocfs2_xattr_block *xb =
3010 (struct ocfs2_xattr_block *)root_bh->b_data;
3011 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3012 struct ocfs2_extent_list *el = &xb_root->xt_list;
3013 u64 p_blkno = 0;
3014 u32 first_hash, num_clusters = 0;
Tao Ma2057e5c2008-10-09 23:06:13 +08003015 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
Tao Ma589dc262008-08-18 17:38:51 +08003016
3017 if (le16_to_cpu(el->l_next_free_rec) == 0)
3018 return -ENODATA;
3019
3020 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
3021 name, name_hash, name_index);
3022
3023 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
3024 &num_clusters, el);
3025 if (ret) {
3026 mlog_errno(ret);
3027 goto out;
3028 }
3029
3030 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
3031
3032 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
Mark Fashehde29c082008-10-29 14:45:30 -07003033 "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
3034 first_hash);
Tao Ma589dc262008-08-18 17:38:51 +08003035
3036 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
3037 p_blkno, first_hash, num_clusters, xs);
3038
3039out:
3040 return ret;
3041}
3042
Tao Ma0c044f02008-08-18 17:38:50 +08003043static int ocfs2_iterate_xattr_buckets(struct inode *inode,
3044 u64 blkno,
3045 u32 clusters,
3046 xattr_bucket_func *func,
3047 void *para)
3048{
Joel Becker6dde41d2008-10-24 17:16:48 -07003049 int i, ret = 0;
Tao Ma0c044f02008-08-18 17:38:50 +08003050 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
3051 u32 num_buckets = clusters * bpc;
Joel Beckerba937122008-10-24 19:13:20 -07003052 struct ocfs2_xattr_bucket *bucket;
Tao Ma0c044f02008-08-18 17:38:50 +08003053
Joel Beckerba937122008-10-24 19:13:20 -07003054 bucket = ocfs2_xattr_bucket_new(inode);
3055 if (!bucket) {
3056 mlog_errno(-ENOMEM);
3057 return -ENOMEM;
3058 }
Tao Ma0c044f02008-08-18 17:38:50 +08003059
3060 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003061 clusters, (unsigned long long)blkno);
Tao Ma0c044f02008-08-18 17:38:50 +08003062
Joel Beckerba937122008-10-24 19:13:20 -07003063 for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
3064 ret = ocfs2_read_xattr_bucket(bucket, blkno);
Tao Ma0c044f02008-08-18 17:38:50 +08003065 if (ret) {
3066 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07003067 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003068 }
3069
Tao Ma0c044f02008-08-18 17:38:50 +08003070 /*
3071 * The real bucket num in this series of blocks is stored
3072 * in the 1st bucket.
3073 */
3074 if (i == 0)
Joel Beckerba937122008-10-24 19:13:20 -07003075 num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
Tao Ma0c044f02008-08-18 17:38:50 +08003076
Mark Fashehde29c082008-10-29 14:45:30 -07003077 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
3078 (unsigned long long)blkno,
Joel Beckerba937122008-10-24 19:13:20 -07003079 le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
Tao Ma0c044f02008-08-18 17:38:50 +08003080 if (func) {
Joel Beckerba937122008-10-24 19:13:20 -07003081 ret = func(inode, bucket, para);
3082 if (ret)
Tao Ma0c044f02008-08-18 17:38:50 +08003083 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07003084 /* Fall through to bucket_relse() */
Tao Ma0c044f02008-08-18 17:38:50 +08003085 }
3086
Joel Beckerba937122008-10-24 19:13:20 -07003087 ocfs2_xattr_bucket_relse(bucket);
3088 if (ret)
3089 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003090 }
3091
Joel Beckerba937122008-10-24 19:13:20 -07003092 ocfs2_xattr_bucket_free(bucket);
Tao Ma0c044f02008-08-18 17:38:50 +08003093 return ret;
3094}
3095
3096struct ocfs2_xattr_tree_list {
3097 char *buffer;
3098 size_t buffer_size;
Tao Ma936b8832008-10-09 23:06:14 +08003099 size_t result;
Tao Ma0c044f02008-08-18 17:38:50 +08003100};
3101
3102static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
3103 struct ocfs2_xattr_header *xh,
3104 int index,
3105 int *block_off,
3106 int *new_offset)
3107{
3108 u16 name_offset;
3109
3110 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
3111 return -EINVAL;
3112
3113 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
3114
3115 *block_off = name_offset >> inode->i_sb->s_blocksize_bits;
3116 *new_offset = name_offset % inode->i_sb->s_blocksize;
3117
3118 return 0;
3119}
3120
3121static int ocfs2_list_xattr_bucket(struct inode *inode,
3122 struct ocfs2_xattr_bucket *bucket,
3123 void *para)
3124{
Tao Ma936b8832008-10-09 23:06:14 +08003125 int ret = 0, type;
Tao Ma0c044f02008-08-18 17:38:50 +08003126 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
Tao Ma0c044f02008-08-18 17:38:50 +08003127 int i, block_off, new_offset;
Tao Ma936b8832008-10-09 23:06:14 +08003128 const char *prefix, *name;
Tao Ma0c044f02008-08-18 17:38:50 +08003129
Joel Becker3e632942008-10-24 17:04:49 -07003130 for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
3131 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
Tao Ma936b8832008-10-09 23:06:14 +08003132 type = ocfs2_xattr_get_type(entry);
3133 prefix = ocfs2_xattr_prefix(type);
Tao Ma0c044f02008-08-18 17:38:50 +08003134
Tao Ma936b8832008-10-09 23:06:14 +08003135 if (prefix) {
Tao Ma0c044f02008-08-18 17:38:50 +08003136 ret = ocfs2_xattr_bucket_get_name_value(inode,
Joel Becker3e632942008-10-24 17:04:49 -07003137 bucket_xh(bucket),
Tao Ma0c044f02008-08-18 17:38:50 +08003138 i,
3139 &block_off,
3140 &new_offset);
3141 if (ret)
3142 break;
Tao Ma936b8832008-10-09 23:06:14 +08003143
Joel Becker51def392008-10-24 16:57:21 -07003144 name = (const char *)bucket_block(bucket, block_off) +
Tao Ma936b8832008-10-09 23:06:14 +08003145 new_offset;
3146 ret = ocfs2_xattr_list_entry(xl->buffer,
3147 xl->buffer_size,
3148 &xl->result,
3149 prefix, name,
3150 entry->xe_name_len);
3151 if (ret)
3152 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003153 }
3154 }
3155
3156 return ret;
3157}
3158
3159static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
3160 struct ocfs2_xattr_tree_root *xt,
3161 char *buffer,
3162 size_t buffer_size)
3163{
3164 struct ocfs2_extent_list *el = &xt->xt_list;
3165 int ret = 0;
3166 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
3167 u64 p_blkno = 0;
3168 struct ocfs2_xattr_tree_list xl = {
3169 .buffer = buffer,
3170 .buffer_size = buffer_size,
Tao Ma936b8832008-10-09 23:06:14 +08003171 .result = 0,
Tao Ma0c044f02008-08-18 17:38:50 +08003172 };
3173
3174 if (le16_to_cpu(el->l_next_free_rec) == 0)
3175 return 0;
3176
3177 while (name_hash > 0) {
3178 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
3179 &e_cpos, &num_clusters, el);
3180 if (ret) {
3181 mlog_errno(ret);
3182 goto out;
3183 }
3184
3185 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
3186 ocfs2_list_xattr_bucket,
3187 &xl);
3188 if (ret) {
3189 mlog_errno(ret);
3190 goto out;
3191 }
3192
3193 if (e_cpos == 0)
3194 break;
3195
3196 name_hash = e_cpos - 1;
3197 }
3198
Tao Ma936b8832008-10-09 23:06:14 +08003199 ret = xl.result;
Tao Ma0c044f02008-08-18 17:38:50 +08003200out:
3201 return ret;
3202}
Tao Ma01225592008-08-18 17:38:53 +08003203
3204static int cmp_xe(const void *a, const void *b)
3205{
3206 const struct ocfs2_xattr_entry *l = a, *r = b;
3207 u32 l_hash = le32_to_cpu(l->xe_name_hash);
3208 u32 r_hash = le32_to_cpu(r->xe_name_hash);
3209
3210 if (l_hash > r_hash)
3211 return 1;
3212 if (l_hash < r_hash)
3213 return -1;
3214 return 0;
3215}
3216
3217static void swap_xe(void *a, void *b, int size)
3218{
3219 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
3220
3221 tmp = *l;
3222 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
3223 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
3224}
3225
3226/*
3227 * When the ocfs2_xattr_block is filled up, new bucket will be created
3228 * and all the xattr entries will be moved to the new bucket.
Joel Becker178eeac2008-10-27 15:18:29 -07003229 * The header goes at the start of the bucket, and the names+values are
3230 * filled from the end. This is why *target starts as the last buffer.
Tao Ma01225592008-08-18 17:38:53 +08003231 * Note: we need to sort the entries since they are not saved in order
3232 * in the ocfs2_xattr_block.
3233 */
3234static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
3235 struct buffer_head *xb_bh,
Joel Becker178eeac2008-10-27 15:18:29 -07003236 struct ocfs2_xattr_bucket *bucket)
Tao Ma01225592008-08-18 17:38:53 +08003237{
3238 int i, blocksize = inode->i_sb->s_blocksize;
Joel Becker178eeac2008-10-27 15:18:29 -07003239 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08003240 u16 offset, size, off_change;
3241 struct ocfs2_xattr_entry *xe;
3242 struct ocfs2_xattr_block *xb =
3243 (struct ocfs2_xattr_block *)xb_bh->b_data;
3244 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
Joel Becker178eeac2008-10-27 15:18:29 -07003245 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma01225592008-08-18 17:38:53 +08003246 u16 count = le16_to_cpu(xb_xh->xh_count);
Joel Becker178eeac2008-10-27 15:18:29 -07003247 char *src = xb_bh->b_data;
3248 char *target = bucket_block(bucket, blks - 1);
Tao Ma01225592008-08-18 17:38:53 +08003249
3250 mlog(0, "cp xattr from block %llu to bucket %llu\n",
3251 (unsigned long long)xb_bh->b_blocknr,
Joel Becker178eeac2008-10-27 15:18:29 -07003252 (unsigned long long)bucket_blkno(bucket));
Tao Ma01225592008-08-18 17:38:53 +08003253
Joel Becker178eeac2008-10-27 15:18:29 -07003254 for (i = 0; i < blks; i++)
3255 memset(bucket_block(bucket, i), 0, blocksize);
3256
Tao Ma01225592008-08-18 17:38:53 +08003257 /*
3258 * Since the xe_name_offset is based on ocfs2_xattr_header,
3259 * there is a offset change corresponding to the change of
3260 * ocfs2_xattr_header's position.
3261 */
3262 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
3263 xe = &xb_xh->xh_entries[count - 1];
3264 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
3265 size = blocksize - offset;
3266
3267 /* copy all the names and values. */
Tao Ma01225592008-08-18 17:38:53 +08003268 memcpy(target + offset, src + offset, size);
3269
3270 /* Init new header now. */
3271 xh->xh_count = xb_xh->xh_count;
3272 xh->xh_num_buckets = cpu_to_le16(1);
3273 xh->xh_name_value_len = cpu_to_le16(size);
3274 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
3275
3276 /* copy all the entries. */
Joel Becker178eeac2008-10-27 15:18:29 -07003277 target = bucket_block(bucket, 0);
Tao Ma01225592008-08-18 17:38:53 +08003278 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
3279 size = count * sizeof(struct ocfs2_xattr_entry);
3280 memcpy(target + offset, (char *)xb_xh + offset, size);
3281
3282 /* Change the xe offset for all the xe because of the move. */
3283 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
3284 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
3285 for (i = 0; i < count; i++)
3286 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
3287
3288 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
3289 offset, size, off_change);
3290
3291 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
3292 cmp_xe, swap_xe);
3293}
3294
3295/*
3296 * After we move xattr from block to index btree, we have to
3297 * update ocfs2_xattr_search to the new xe and base.
3298 *
3299 * When the entry is in xattr block, xattr_bh indicates the storage place.
3300 * While if the entry is in index b-tree, "bucket" indicates the
3301 * real place of the xattr.
3302 */
Joel Becker178eeac2008-10-27 15:18:29 -07003303static void ocfs2_xattr_update_xattr_search(struct inode *inode,
3304 struct ocfs2_xattr_search *xs,
3305 struct buffer_head *old_bh)
Tao Ma01225592008-08-18 17:38:53 +08003306{
Tao Ma01225592008-08-18 17:38:53 +08003307 char *buf = old_bh->b_data;
3308 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
3309 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
Joel Becker178eeac2008-10-27 15:18:29 -07003310 int i;
Tao Ma01225592008-08-18 17:38:53 +08003311
Joel Beckerba937122008-10-24 19:13:20 -07003312 xs->header = bucket_xh(xs->bucket);
Joel Becker178eeac2008-10-27 15:18:29 -07003313 xs->base = bucket_block(xs->bucket, 0);
Tao Ma01225592008-08-18 17:38:53 +08003314 xs->end = xs->base + inode->i_sb->s_blocksize;
3315
Joel Becker178eeac2008-10-27 15:18:29 -07003316 if (xs->not_found)
3317 return;
Tao Ma01225592008-08-18 17:38:53 +08003318
Joel Becker178eeac2008-10-27 15:18:29 -07003319 i = xs->here - old_xh->xh_entries;
3320 xs->here = &xs->header->xh_entries[i];
Tao Ma01225592008-08-18 17:38:53 +08003321}
3322
3323static int ocfs2_xattr_create_index_block(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +08003324 struct ocfs2_xattr_search *xs,
3325 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08003326{
Tao Ma85db90e2008-11-12 08:27:01 +08003327 int ret;
Tao Ma01225592008-08-18 17:38:53 +08003328 u32 bit_off, len;
3329 u64 blkno;
Tao Ma85db90e2008-11-12 08:27:01 +08003330 handle_t *handle = ctxt->handle;
Tao Ma01225592008-08-18 17:38:53 +08003331 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3332 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Tao Ma01225592008-08-18 17:38:53 +08003333 struct buffer_head *xb_bh = xs->xattr_bh;
3334 struct ocfs2_xattr_block *xb =
3335 (struct ocfs2_xattr_block *)xb_bh->b_data;
3336 struct ocfs2_xattr_tree_root *xr;
3337 u16 xb_flags = le16_to_cpu(xb->xb_flags);
Tao Ma01225592008-08-18 17:38:53 +08003338
3339 mlog(0, "create xattr index block for %llu\n",
3340 (unsigned long long)xb_bh->b_blocknr);
3341
3342 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
Joel Becker178eeac2008-10-27 15:18:29 -07003343 BUG_ON(!xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08003344
Tao Ma01225592008-08-18 17:38:53 +08003345 /*
3346 * XXX:
3347 * We can use this lock for now, and maybe move to a dedicated mutex
3348 * if performance becomes a problem later.
3349 */
3350 down_write(&oi->ip_alloc_sem);
3351
Tao Ma01225592008-08-18 17:38:53 +08003352 ret = ocfs2_journal_access(handle, inode, xb_bh,
3353 OCFS2_JOURNAL_ACCESS_WRITE);
3354 if (ret) {
3355 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003356 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003357 }
3358
Tao Ma78f30c32008-11-12 08:27:00 +08003359 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac,
3360 1, 1, &bit_off, &len);
Tao Ma01225592008-08-18 17:38:53 +08003361 if (ret) {
3362 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003363 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003364 }
3365
3366 /*
3367 * The bucket may spread in many blocks, and
3368 * we will only touch the 1st block and the last block
3369 * in the whole bucket(one for entry and one for data).
3370 */
3371 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
3372
Mark Fashehde29c082008-10-29 14:45:30 -07003373 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
3374 (unsigned long long)blkno);
Tao Ma01225592008-08-18 17:38:53 +08003375
Joel Becker178eeac2008-10-27 15:18:29 -07003376 ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
Tao Ma01225592008-08-18 17:38:53 +08003377 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
Joel Becker178eeac2008-10-27 15:18:29 -07003382 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
3383 OCFS2_JOURNAL_ACCESS_CREATE);
Joel Beckerbd60bd32008-10-20 18:25:56 -07003384 if (ret) {
3385 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003386 goto out;
Joel Beckerbd60bd32008-10-20 18:25:56 -07003387 }
Tao Ma01225592008-08-18 17:38:53 +08003388
Joel Becker178eeac2008-10-27 15:18:29 -07003389 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
3390 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
3391
3392 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
3393
Tao Ma01225592008-08-18 17:38:53 +08003394 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
3395 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
3396 offsetof(struct ocfs2_xattr_block, xb_attrs));
3397
3398 xr = &xb->xb_attrs.xb_root;
3399 xr->xt_clusters = cpu_to_le32(1);
3400 xr->xt_last_eb_blk = 0;
3401 xr->xt_list.l_tree_depth = 0;
3402 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
3403 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
3404
3405 xr->xt_list.l_recs[0].e_cpos = 0;
3406 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
3407 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
3408
3409 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
3410
Tao Ma85db90e2008-11-12 08:27:01 +08003411 ocfs2_journal_dirty(handle, xb_bh);
Tao Ma01225592008-08-18 17:38:53 +08003412
Tao Ma85db90e2008-11-12 08:27:01 +08003413out:
Tao Ma01225592008-08-18 17:38:53 +08003414 up_write(&oi->ip_alloc_sem);
3415
Tao Ma01225592008-08-18 17:38:53 +08003416 return ret;
3417}
3418
3419static int cmp_xe_offset(const void *a, const void *b)
3420{
3421 const struct ocfs2_xattr_entry *l = a, *r = b;
3422 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
3423 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
3424
3425 if (l_name_offset < r_name_offset)
3426 return 1;
3427 if (l_name_offset > r_name_offset)
3428 return -1;
3429 return 0;
3430}
3431
3432/*
3433 * defrag a xattr bucket if we find that the bucket has some
3434 * holes beteen name/value pairs.
3435 * We will move all the name/value pairs to the end of the bucket
3436 * so that we can spare some space for insertion.
3437 */
3438static int ocfs2_defrag_xattr_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08003439 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08003440 struct ocfs2_xattr_bucket *bucket)
3441{
3442 int ret, i;
3443 size_t end, offset, len, value_len;
3444 struct ocfs2_xattr_header *xh;
3445 char *entries, *buf, *bucket_buf = NULL;
Joel Becker9c7759a2008-10-24 16:21:03 -07003446 u64 blkno = bucket_blkno(bucket);
Tao Ma01225592008-08-18 17:38:53 +08003447 u16 xh_free_start;
Tao Ma01225592008-08-18 17:38:53 +08003448 size_t blocksize = inode->i_sb->s_blocksize;
Tao Ma01225592008-08-18 17:38:53 +08003449 struct ocfs2_xattr_entry *xe;
Tao Ma01225592008-08-18 17:38:53 +08003450
3451 /*
3452 * In order to make the operation more efficient and generic,
3453 * we copy all the blocks into a contiguous memory and do the
3454 * defragment there, so if anything is error, we will not touch
3455 * the real block.
3456 */
3457 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
3458 if (!bucket_buf) {
3459 ret = -EIO;
3460 goto out;
3461 }
3462
Joel Becker161d6f32008-10-27 15:25:18 -07003463 buf = bucket_buf;
Tao Ma1c32a2f2008-11-06 08:10:47 +08003464 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
3465 memcpy(buf, bucket_block(bucket, i), blocksize);
Joel Becker161d6f32008-10-27 15:25:18 -07003466
Tao Ma1c32a2f2008-11-06 08:10:47 +08003467 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
Joel Becker161d6f32008-10-27 15:25:18 -07003468 OCFS2_JOURNAL_ACCESS_WRITE);
3469 if (ret < 0) {
3470 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003471 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003472 }
3473
3474 xh = (struct ocfs2_xattr_header *)bucket_buf;
3475 entries = (char *)xh->xh_entries;
3476 xh_free_start = le16_to_cpu(xh->xh_free_start);
3477
3478 mlog(0, "adjust xattr bucket in %llu, count = %u, "
3479 "xh_free_start = %u, xh_name_value_len = %u.\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003480 (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
3481 xh_free_start, le16_to_cpu(xh->xh_name_value_len));
Tao Ma01225592008-08-18 17:38:53 +08003482
3483 /*
3484 * sort all the entries by their offset.
3485 * the largest will be the first, so that we can
3486 * move them to the end one by one.
3487 */
3488 sort(entries, le16_to_cpu(xh->xh_count),
3489 sizeof(struct ocfs2_xattr_entry),
3490 cmp_xe_offset, swap_xe);
3491
3492 /* Move all name/values to the end of the bucket. */
3493 xe = xh->xh_entries;
3494 end = OCFS2_XATTR_BUCKET_SIZE;
3495 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
3496 offset = le16_to_cpu(xe->xe_name_offset);
3497 if (ocfs2_xattr_is_local(xe))
3498 value_len = OCFS2_XATTR_SIZE(
3499 le64_to_cpu(xe->xe_value_size));
3500 else
3501 value_len = OCFS2_XATTR_ROOT_SIZE;
3502 len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
3503
3504 /*
3505 * We must make sure that the name/value pair
3506 * exist in the same block. So adjust end to
3507 * the previous block end if needed.
3508 */
3509 if (((end - len) / blocksize !=
3510 (end - 1) / blocksize))
3511 end = end - end % blocksize;
3512
3513 if (end > offset + len) {
3514 memmove(bucket_buf + end - len,
3515 bucket_buf + offset, len);
3516 xe->xe_name_offset = cpu_to_le16(end - len);
3517 }
3518
3519 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
3520 "bucket %llu\n", (unsigned long long)blkno);
3521
3522 end -= len;
3523 }
3524
3525 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
3526 "bucket %llu\n", (unsigned long long)blkno);
3527
3528 if (xh_free_start == end)
Tao Ma85db90e2008-11-12 08:27:01 +08003529 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003530
3531 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
3532 xh->xh_free_start = cpu_to_le16(end);
3533
3534 /* sort the entries by their name_hash. */
3535 sort(entries, le16_to_cpu(xh->xh_count),
3536 sizeof(struct ocfs2_xattr_entry),
3537 cmp_xe, swap_xe);
3538
3539 buf = bucket_buf;
Tao Ma1c32a2f2008-11-06 08:10:47 +08003540 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
3541 memcpy(bucket_block(bucket, i), buf, blocksize);
3542 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
Tao Ma01225592008-08-18 17:38:53 +08003543
Tao Ma01225592008-08-18 17:38:53 +08003544out:
Tao Ma01225592008-08-18 17:38:53 +08003545 kfree(bucket_buf);
3546 return ret;
3547}
3548
3549/*
Joel Beckerb5c03e72008-11-25 19:58:16 -08003550 * prev_blkno points to the start of an existing extent. new_blkno
3551 * points to a newly allocated extent. Because we know each of our
3552 * clusters contains more than bucket, we can easily split one cluster
3553 * at a bucket boundary. So we take the last cluster of the existing
3554 * extent and split it down the middle. We move the last half of the
3555 * buckets in the last cluster of the existing extent over to the new
3556 * extent.
Tao Ma01225592008-08-18 17:38:53 +08003557 *
Joel Beckerb5c03e72008-11-25 19:58:16 -08003558 * first_bh is the buffer at prev_blkno so we can update the existing
3559 * extent's bucket count. header_bh is the bucket were we were hoping
3560 * to insert our xattr. If the bucket move places the target in the new
3561 * extent, we'll update first_bh and header_bh after modifying the old
3562 * extent.
3563 *
3564 * first_hash will be set as the 1st xe's name_hash in the new extent.
Tao Ma01225592008-08-18 17:38:53 +08003565 */
3566static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
3567 handle_t *handle,
Joel Becker41cb8142008-11-26 14:25:21 -08003568 struct ocfs2_xattr_bucket *first,
3569 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08003570 u64 new_blkno,
Tao Ma01225592008-08-18 17:38:53 +08003571 u32 num_clusters,
3572 u32 *first_hash)
3573{
Joel Beckerc58b6032008-11-26 13:36:24 -08003574 int ret;
Joel Becker41cb8142008-11-26 14:25:21 -08003575 struct super_block *sb = inode->i_sb;
3576 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb);
3577 int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
Joel Beckerb5c03e72008-11-25 19:58:16 -08003578 int to_move = num_buckets / 2;
Joel Beckerc58b6032008-11-26 13:36:24 -08003579 u64 src_blkno;
Joel Becker41cb8142008-11-26 14:25:21 -08003580 u64 last_cluster_blkno = bucket_blkno(first) +
3581 ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1));
Tao Ma01225592008-08-18 17:38:53 +08003582
Joel Becker41cb8142008-11-26 14:25:21 -08003583 BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets);
3584 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize);
Tao Ma01225592008-08-18 17:38:53 +08003585
Tao Ma01225592008-08-18 17:38:53 +08003586 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
Joel Beckerc58b6032008-11-26 13:36:24 -08003587 (unsigned long long)last_cluster_blkno, (unsigned long long)new_blkno);
Tao Ma01225592008-08-18 17:38:53 +08003588
Joel Becker41cb8142008-11-26 14:25:21 -08003589 ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first),
Joel Beckerc58b6032008-11-26 13:36:24 -08003590 last_cluster_blkno, new_blkno,
3591 to_move, first_hash);
Joel Beckerb5c03e72008-11-25 19:58:16 -08003592 if (ret) {
3593 mlog_errno(ret);
3594 goto out;
3595 }
3596
Joel Beckerc58b6032008-11-26 13:36:24 -08003597 /* This is the first bucket that got moved */
3598 src_blkno = last_cluster_blkno + (to_move * blks_per_bucket);
3599
Tao Ma01225592008-08-18 17:38:53 +08003600 /*
Joel Beckerc58b6032008-11-26 13:36:24 -08003601 * If the target bucket was part of the moved buckets, we need to
Joel Becker41cb8142008-11-26 14:25:21 -08003602 * update first and target.
Joel Beckerb5c03e72008-11-25 19:58:16 -08003603 */
Joel Becker41cb8142008-11-26 14:25:21 -08003604 if (bucket_blkno(target) >= src_blkno) {
Joel Beckerb5c03e72008-11-25 19:58:16 -08003605 /* Find the block for the new target bucket */
3606 src_blkno = new_blkno +
Joel Becker41cb8142008-11-26 14:25:21 -08003607 (bucket_blkno(target) - src_blkno);
3608
3609 ocfs2_xattr_bucket_relse(first);
3610 ocfs2_xattr_bucket_relse(target);
Joel Beckerb5c03e72008-11-25 19:58:16 -08003611
3612 /*
Joel Beckerc58b6032008-11-26 13:36:24 -08003613 * These shouldn't fail - the buffers are in the
Joel Beckerb5c03e72008-11-25 19:58:16 -08003614 * journal from ocfs2_cp_xattr_bucket().
3615 */
Joel Becker41cb8142008-11-26 14:25:21 -08003616 ret = ocfs2_read_xattr_bucket(first, new_blkno);
Joel Beckerc58b6032008-11-26 13:36:24 -08003617 if (ret) {
3618 mlog_errno(ret);
3619 goto out;
3620 }
Joel Becker41cb8142008-11-26 14:25:21 -08003621 ret = ocfs2_read_xattr_bucket(target, src_blkno);
3622 if (ret)
Joel Beckerb5c03e72008-11-25 19:58:16 -08003623 mlog_errno(ret);
Joel Beckerb5c03e72008-11-25 19:58:16 -08003624
Joel Beckerb5c03e72008-11-25 19:58:16 -08003625 }
3626
Tao Ma01225592008-08-18 17:38:53 +08003627out:
Tao Ma01225592008-08-18 17:38:53 +08003628 return ret;
3629}
3630
Tao Ma01225592008-08-18 17:38:53 +08003631/*
Tao Ma80bcaf32008-10-27 06:06:24 +08003632 * Find the suitable pos when we divide a bucket into 2.
3633 * We have to make sure the xattrs with the same hash value exist
3634 * in the same bucket.
3635 *
3636 * If this ocfs2_xattr_header covers more than one hash value, find a
3637 * place where the hash value changes. Try to find the most even split.
3638 * The most common case is that all entries have different hash values,
3639 * and the first check we make will find a place to split.
Tao Ma01225592008-08-18 17:38:53 +08003640 */
Tao Ma80bcaf32008-10-27 06:06:24 +08003641static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
3642{
3643 struct ocfs2_xattr_entry *entries = xh->xh_entries;
3644 int count = le16_to_cpu(xh->xh_count);
3645 int delta, middle = count / 2;
3646
3647 /*
3648 * We start at the middle. Each step gets farther away in both
3649 * directions. We therefore hit the change in hash value
3650 * nearest to the middle. Note that this loop does not execute for
3651 * count < 2.
3652 */
3653 for (delta = 0; delta < middle; delta++) {
3654 /* Let's check delta earlier than middle */
3655 if (cmp_xe(&entries[middle - delta - 1],
3656 &entries[middle - delta]))
3657 return middle - delta;
3658
3659 /* For even counts, don't walk off the end */
3660 if ((middle + delta + 1) == count)
3661 continue;
3662
3663 /* Now try delta past middle */
3664 if (cmp_xe(&entries[middle + delta],
3665 &entries[middle + delta + 1]))
3666 return middle + delta + 1;
3667 }
3668
3669 /* Every entry had the same hash */
3670 return count;
3671}
3672
3673/*
3674 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
3675 * first_hash will record the 1st hash of the new bucket.
3676 *
3677 * Normally half of the xattrs will be moved. But we have to make
3678 * sure that the xattrs with the same hash value are stored in the
3679 * same bucket. If all the xattrs in this bucket have the same hash
3680 * value, the new bucket will be initialized as an empty one and the
3681 * first_hash will be initialized as (hash_value+1).
3682 */
3683static int ocfs2_divide_xattr_bucket(struct inode *inode,
3684 handle_t *handle,
3685 u64 blk,
3686 u64 new_blk,
3687 u32 *first_hash,
3688 int new_bucket_head)
Tao Ma01225592008-08-18 17:38:53 +08003689{
3690 int ret, i;
Tao Ma80bcaf32008-10-27 06:06:24 +08003691 int count, start, len, name_value_len = 0, xe_len, name_offset = 0;
Joel Beckerba937122008-10-24 19:13:20 -07003692 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
Tao Ma01225592008-08-18 17:38:53 +08003693 struct ocfs2_xattr_header *xh;
3694 struct ocfs2_xattr_entry *xe;
3695 int blocksize = inode->i_sb->s_blocksize;
3696
Tao Ma80bcaf32008-10-27 06:06:24 +08003697 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003698 (unsigned long long)blk, (unsigned long long)new_blk);
Tao Ma01225592008-08-18 17:38:53 +08003699
Joel Beckerba937122008-10-24 19:13:20 -07003700 s_bucket = ocfs2_xattr_bucket_new(inode);
3701 t_bucket = ocfs2_xattr_bucket_new(inode);
3702 if (!s_bucket || !t_bucket) {
3703 ret = -ENOMEM;
3704 mlog_errno(ret);
3705 goto out;
3706 }
Tao Ma01225592008-08-18 17:38:53 +08003707
Joel Beckerba937122008-10-24 19:13:20 -07003708 ret = ocfs2_read_xattr_bucket(s_bucket, blk);
Tao Ma01225592008-08-18 17:38:53 +08003709 if (ret) {
3710 mlog_errno(ret);
3711 goto out;
3712 }
3713
Joel Beckerba937122008-10-24 19:13:20 -07003714 ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07003715 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08003716 if (ret) {
3717 mlog_errno(ret);
3718 goto out;
3719 }
3720
Joel Becker784b8162008-10-24 17:33:40 -07003721 /*
3722 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
3723 * there's no need to read it.
3724 */
Joel Beckerba937122008-10-24 19:13:20 -07003725 ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
Tao Ma01225592008-08-18 17:38:53 +08003726 if (ret) {
3727 mlog_errno(ret);
3728 goto out;
3729 }
3730
Joel Becker2b656c12008-11-25 19:00:15 -08003731 /*
3732 * Hey, if we're overwriting t_bucket, what difference does
3733 * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the
3734 * same part of ocfs2_cp_xattr_bucket().
3735 */
Joel Beckerba937122008-10-24 19:13:20 -07003736 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07003737 new_bucket_head ?
3738 OCFS2_JOURNAL_ACCESS_CREATE :
3739 OCFS2_JOURNAL_ACCESS_WRITE);
3740 if (ret) {
3741 mlog_errno(ret);
3742 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003743 }
3744
Joel Beckerba937122008-10-24 19:13:20 -07003745 xh = bucket_xh(s_bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08003746 count = le16_to_cpu(xh->xh_count);
3747 start = ocfs2_xattr_find_divide_pos(xh);
3748
3749 if (start == count) {
3750 xe = &xh->xh_entries[start-1];
3751
3752 /*
3753 * initialized a new empty bucket here.
3754 * The hash value is set as one larger than
3755 * that of the last entry in the previous bucket.
3756 */
Joel Beckerba937122008-10-24 19:13:20 -07003757 for (i = 0; i < t_bucket->bu_blocks; i++)
3758 memset(bucket_block(t_bucket, i), 0, blocksize);
Tao Ma80bcaf32008-10-27 06:06:24 +08003759
Joel Beckerba937122008-10-24 19:13:20 -07003760 xh = bucket_xh(t_bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08003761 xh->xh_free_start = cpu_to_le16(blocksize);
3762 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
3763 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
3764
3765 goto set_num_buckets;
3766 }
3767
Tao Ma01225592008-08-18 17:38:53 +08003768 /* copy the whole bucket to the new first. */
Joel Beckerba937122008-10-24 19:13:20 -07003769 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003770
3771 /* update the new bucket. */
Joel Beckerba937122008-10-24 19:13:20 -07003772 xh = bucket_xh(t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003773
3774 /*
3775 * Calculate the total name/value len and xh_free_start for
3776 * the old bucket first.
3777 */
3778 name_offset = OCFS2_XATTR_BUCKET_SIZE;
3779 name_value_len = 0;
3780 for (i = 0; i < start; i++) {
3781 xe = &xh->xh_entries[i];
3782 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3783 if (ocfs2_xattr_is_local(xe))
3784 xe_len +=
3785 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3786 else
3787 xe_len += OCFS2_XATTR_ROOT_SIZE;
3788 name_value_len += xe_len;
3789 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
3790 name_offset = le16_to_cpu(xe->xe_name_offset);
3791 }
3792
3793 /*
3794 * Now begin the modification to the new bucket.
3795 *
3796 * In the new bucket, We just move the xattr entry to the beginning
3797 * and don't touch the name/value. So there will be some holes in the
3798 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
3799 * called.
3800 */
3801 xe = &xh->xh_entries[start];
3802 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
3803 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
Mark Fashehff1ec202008-08-19 10:54:29 -07003804 (int)((char *)xe - (char *)xh),
3805 (int)((char *)xh->xh_entries - (char *)xh));
Tao Ma01225592008-08-18 17:38:53 +08003806 memmove((char *)xh->xh_entries, (char *)xe, len);
3807 xe = &xh->xh_entries[count - start];
3808 len = sizeof(struct ocfs2_xattr_entry) * start;
3809 memset((char *)xe, 0, len);
3810
3811 le16_add_cpu(&xh->xh_count, -start);
3812 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
3813
3814 /* Calculate xh_free_start for the new bucket. */
3815 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3816 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3817 xe = &xh->xh_entries[i];
3818 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3819 if (ocfs2_xattr_is_local(xe))
3820 xe_len +=
3821 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3822 else
3823 xe_len += OCFS2_XATTR_ROOT_SIZE;
3824 if (le16_to_cpu(xe->xe_name_offset) <
3825 le16_to_cpu(xh->xh_free_start))
3826 xh->xh_free_start = xe->xe_name_offset;
3827 }
3828
Tao Ma80bcaf32008-10-27 06:06:24 +08003829set_num_buckets:
Tao Ma01225592008-08-18 17:38:53 +08003830 /* set xh->xh_num_buckets for the new xh. */
3831 if (new_bucket_head)
3832 xh->xh_num_buckets = cpu_to_le16(1);
3833 else
3834 xh->xh_num_buckets = 0;
3835
Joel Beckerba937122008-10-24 19:13:20 -07003836 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003837
3838 /* store the first_hash of the new bucket. */
3839 if (first_hash)
3840 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3841
3842 /*
Tao Ma80bcaf32008-10-27 06:06:24 +08003843 * Now only update the 1st block of the old bucket. If we
3844 * just added a new empty bucket, there is no need to modify
3845 * it.
Tao Ma01225592008-08-18 17:38:53 +08003846 */
Tao Ma80bcaf32008-10-27 06:06:24 +08003847 if (start == count)
3848 goto out;
3849
Joel Beckerba937122008-10-24 19:13:20 -07003850 xh = bucket_xh(s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003851 memset(&xh->xh_entries[start], 0,
3852 sizeof(struct ocfs2_xattr_entry) * (count - start));
3853 xh->xh_count = cpu_to_le16(start);
3854 xh->xh_free_start = cpu_to_le16(name_offset);
3855 xh->xh_name_value_len = cpu_to_le16(name_value_len);
3856
Joel Beckerba937122008-10-24 19:13:20 -07003857 ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003858
3859out:
Joel Beckerba937122008-10-24 19:13:20 -07003860 ocfs2_xattr_bucket_free(s_bucket);
3861 ocfs2_xattr_bucket_free(t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003862
3863 return ret;
3864}
3865
3866/*
3867 * Copy xattr from one bucket to another bucket.
3868 *
3869 * The caller must make sure that the journal transaction
3870 * has enough space for journaling.
3871 */
3872static int ocfs2_cp_xattr_bucket(struct inode *inode,
3873 handle_t *handle,
3874 u64 s_blkno,
3875 u64 t_blkno,
3876 int t_is_new)
3877{
Joel Becker4980c6d2008-10-24 18:54:43 -07003878 int ret;
Joel Beckerba937122008-10-24 19:13:20 -07003879 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
Tao Ma01225592008-08-18 17:38:53 +08003880
3881 BUG_ON(s_blkno == t_blkno);
3882
3883 mlog(0, "cp bucket %llu to %llu, target is %d\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003884 (unsigned long long)s_blkno, (unsigned long long)t_blkno,
3885 t_is_new);
Tao Ma01225592008-08-18 17:38:53 +08003886
Joel Beckerba937122008-10-24 19:13:20 -07003887 s_bucket = ocfs2_xattr_bucket_new(inode);
3888 t_bucket = ocfs2_xattr_bucket_new(inode);
3889 if (!s_bucket || !t_bucket) {
3890 ret = -ENOMEM;
3891 mlog_errno(ret);
3892 goto out;
3893 }
Joel Becker92de1092008-11-25 17:06:40 -08003894
Joel Beckerba937122008-10-24 19:13:20 -07003895 ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
Tao Ma01225592008-08-18 17:38:53 +08003896 if (ret)
3897 goto out;
3898
Joel Becker784b8162008-10-24 17:33:40 -07003899 /*
3900 * Even if !t_is_new, we're overwriting t_bucket. Thus,
3901 * there's no need to read it.
3902 */
Joel Beckerba937122008-10-24 19:13:20 -07003903 ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
Tao Ma01225592008-08-18 17:38:53 +08003904 if (ret)
3905 goto out;
3906
Joel Becker2b656c12008-11-25 19:00:15 -08003907 /*
3908 * Hey, if we're overwriting t_bucket, what difference does
3909 * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new
Joel Becker874d65a2008-11-26 13:02:18 -08003910 * cluster to fill, we came here from
3911 * ocfs2_mv_xattr_buckets(), and it is really new -
3912 * ACCESS_CREATE is required. But we also might have moved data
3913 * out of t_bucket before extending back into it.
3914 * ocfs2_add_new_xattr_bucket() can do this - its call to
3915 * ocfs2_add_new_xattr_cluster() may have created a new extent
Joel Becker2b656c12008-11-25 19:00:15 -08003916 * and copied out the end of the old extent. Then it re-extends
3917 * the old extent back to create space for new xattrs. That's
3918 * how we get here, and the bucket isn't really new.
3919 */
Joel Beckerba937122008-10-24 19:13:20 -07003920 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07003921 t_is_new ?
3922 OCFS2_JOURNAL_ACCESS_CREATE :
3923 OCFS2_JOURNAL_ACCESS_WRITE);
3924 if (ret)
3925 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003926
Joel Beckerba937122008-10-24 19:13:20 -07003927 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
3928 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003929
3930out:
Joel Beckerba937122008-10-24 19:13:20 -07003931 ocfs2_xattr_bucket_free(t_bucket);
3932 ocfs2_xattr_bucket_free(s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003933
3934 return ret;
3935}
3936
3937/*
Joel Becker874d65a2008-11-26 13:02:18 -08003938 * src_blk points to the start of an existing extent. last_blk points to
3939 * last cluster in that extent. to_blk points to a newly allocated
Joel Becker54ecb6b2008-11-26 13:18:31 -08003940 * extent. We copy the buckets from the cluster at last_blk to the new
3941 * extent. If start_bucket is non-zero, we skip that many buckets before
3942 * we start copying. The new extent's xh_num_buckets gets set to the
3943 * number of buckets we copied. The old extent's xh_num_buckets shrinks
3944 * by the same amount.
Tao Ma01225592008-08-18 17:38:53 +08003945 */
Joel Becker54ecb6b2008-11-26 13:18:31 -08003946static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
3947 u64 src_blk, u64 last_blk, u64 to_blk,
3948 unsigned int start_bucket,
3949 u32 *first_hash)
Tao Ma01225592008-08-18 17:38:53 +08003950{
3951 int i, ret, credits;
3952 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Joel Becker15d60922008-11-25 18:36:42 -08003953 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08003954 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
Joel Becker15d60922008-11-25 18:36:42 -08003955 struct ocfs2_xattr_bucket *old_first, *new_first;
Tao Ma01225592008-08-18 17:38:53 +08003956
Joel Becker874d65a2008-11-26 13:02:18 -08003957 mlog(0, "mv xattrs from cluster %llu to %llu\n",
3958 (unsigned long long)last_blk, (unsigned long long)to_blk);
Tao Ma01225592008-08-18 17:38:53 +08003959
Joel Becker54ecb6b2008-11-26 13:18:31 -08003960 BUG_ON(start_bucket >= num_buckets);
3961 if (start_bucket) {
3962 num_buckets -= start_bucket;
3963 last_blk += (start_bucket * blks_per_bucket);
3964 }
3965
Joel Becker15d60922008-11-25 18:36:42 -08003966 /* The first bucket of the original extent */
3967 old_first = ocfs2_xattr_bucket_new(inode);
3968 /* The first bucket of the new extent */
3969 new_first = ocfs2_xattr_bucket_new(inode);
3970 if (!old_first || !new_first) {
3971 ret = -ENOMEM;
3972 mlog_errno(ret);
3973 goto out;
3974 }
3975
Joel Becker874d65a2008-11-26 13:02:18 -08003976 ret = ocfs2_read_xattr_bucket(old_first, src_blk);
Joel Becker15d60922008-11-25 18:36:42 -08003977 if (ret) {
3978 mlog_errno(ret);
3979 goto out;
3980 }
3981
Tao Ma01225592008-08-18 17:38:53 +08003982 /*
Joel Becker54ecb6b2008-11-26 13:18:31 -08003983 * We need to update the first bucket of the old extent and all
3984 * the buckets going to the new extent.
Tao Ma01225592008-08-18 17:38:53 +08003985 */
Joel Becker54ecb6b2008-11-26 13:18:31 -08003986 credits = ((num_buckets + 1) * blks_per_bucket) +
3987 handle->h_buffer_credits;
Tao Ma01225592008-08-18 17:38:53 +08003988 ret = ocfs2_extend_trans(handle, credits);
3989 if (ret) {
3990 mlog_errno(ret);
3991 goto out;
3992 }
3993
Joel Becker15d60922008-11-25 18:36:42 -08003994 ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
3995 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08003996 if (ret) {
3997 mlog_errno(ret);
3998 goto out;
3999 }
4000
4001 for (i = 0; i < num_buckets; i++) {
4002 ret = ocfs2_cp_xattr_bucket(inode, handle,
Joel Becker874d65a2008-11-26 13:02:18 -08004003 last_blk + (i * blks_per_bucket),
Joel Becker15d60922008-11-25 18:36:42 -08004004 to_blk + (i * blks_per_bucket),
4005 1);
Tao Ma01225592008-08-18 17:38:53 +08004006 if (ret) {
4007 mlog_errno(ret);
4008 goto out;
4009 }
Tao Ma01225592008-08-18 17:38:53 +08004010 }
4011
Joel Becker15d60922008-11-25 18:36:42 -08004012 /*
4013 * Get the new bucket ready before we dirty anything
4014 * (This actually shouldn't fail, because we already dirtied
4015 * it once in ocfs2_cp_xattr_bucket()).
4016 */
4017 ret = ocfs2_read_xattr_bucket(new_first, to_blk);
4018 if (ret) {
Tao Ma01225592008-08-18 17:38:53 +08004019 mlog_errno(ret);
4020 goto out;
4021 }
Joel Becker15d60922008-11-25 18:36:42 -08004022 ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
4023 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004024 if (ret) {
4025 mlog_errno(ret);
4026 goto out;
4027 }
4028
Joel Becker15d60922008-11-25 18:36:42 -08004029 /* Now update the headers */
4030 le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets);
4031 ocfs2_xattr_bucket_journal_dirty(handle, old_first);
Tao Ma01225592008-08-18 17:38:53 +08004032
Joel Becker15d60922008-11-25 18:36:42 -08004033 bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets);
4034 ocfs2_xattr_bucket_journal_dirty(handle, new_first);
Tao Ma01225592008-08-18 17:38:53 +08004035
4036 if (first_hash)
Joel Becker15d60922008-11-25 18:36:42 -08004037 *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash);
4038
Tao Ma01225592008-08-18 17:38:53 +08004039out:
Joel Becker15d60922008-11-25 18:36:42 -08004040 ocfs2_xattr_bucket_free(new_first);
4041 ocfs2_xattr_bucket_free(old_first);
Tao Ma01225592008-08-18 17:38:53 +08004042 return ret;
4043}
4044
4045/*
Tao Ma80bcaf32008-10-27 06:06:24 +08004046 * Move some xattrs in this cluster to the new cluster.
Tao Ma01225592008-08-18 17:38:53 +08004047 * This function should only be called when bucket size == cluster size.
4048 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
4049 */
Tao Ma80bcaf32008-10-27 06:06:24 +08004050static int ocfs2_divide_xattr_cluster(struct inode *inode,
4051 handle_t *handle,
4052 u64 prev_blk,
4053 u64 new_blk,
4054 u32 *first_hash)
Tao Ma01225592008-08-18 17:38:53 +08004055{
4056 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08004057 int ret, credits = 2 * blk_per_bucket + handle->h_buffer_credits;
Tao Ma01225592008-08-18 17:38:53 +08004058
4059 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
4060
4061 ret = ocfs2_extend_trans(handle, credits);
4062 if (ret) {
4063 mlog_errno(ret);
4064 return ret;
4065 }
4066
4067 /* Move half of the xattr in start_blk to the next bucket. */
Tao Ma80bcaf32008-10-27 06:06:24 +08004068 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
4069 new_blk, first_hash, 1);
Tao Ma01225592008-08-18 17:38:53 +08004070}
4071
4072/*
4073 * Move some xattrs from the old cluster to the new one since they are not
4074 * contiguous in ocfs2 xattr tree.
4075 *
4076 * new_blk starts a new separate cluster, and we will move some xattrs from
4077 * prev_blk to it. v_start will be set as the first name hash value in this
4078 * new cluster so that it can be used as e_cpos during tree insertion and
4079 * don't collide with our original b-tree operations. first_bh and header_bh
4080 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
4081 * to extend the insert bucket.
4082 *
4083 * The problem is how much xattr should we move to the new one and when should
4084 * we update first_bh and header_bh?
4085 * 1. If cluster size > bucket size, that means the previous cluster has more
4086 * than 1 bucket, so just move half nums of bucket into the new cluster and
4087 * update the first_bh and header_bh if the insert bucket has been moved
4088 * to the new cluster.
4089 * 2. If cluster_size == bucket_size:
4090 * a) If the previous extent rec has more than one cluster and the insert
4091 * place isn't in the last cluster, copy the entire last cluster to the
4092 * new one. This time, we don't need to upate the first_bh and header_bh
4093 * since they will not be moved into the new cluster.
4094 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
4095 * the new one. And we set the extend flag to zero if the insert place is
4096 * moved into the new allocated cluster since no extend is needed.
4097 */
4098static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
4099 handle_t *handle,
Joel Becker012ee912008-11-26 14:43:31 -08004100 struct ocfs2_xattr_bucket *first,
4101 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08004102 u64 new_blk,
Tao Ma01225592008-08-18 17:38:53 +08004103 u32 prev_clusters,
4104 u32 *v_start,
4105 int *extend)
4106{
Joel Becker92cf3ad2008-11-26 14:12:09 -08004107 int ret;
Tao Ma01225592008-08-18 17:38:53 +08004108
4109 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
Joel Becker012ee912008-11-26 14:43:31 -08004110 (unsigned long long)bucket_blkno(first), prev_clusters,
Mark Fashehde29c082008-10-29 14:45:30 -07004111 (unsigned long long)new_blk);
Tao Ma01225592008-08-18 17:38:53 +08004112
Joel Becker41cb8142008-11-26 14:25:21 -08004113 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) {
Tao Ma01225592008-08-18 17:38:53 +08004114 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
4115 handle,
Joel Becker41cb8142008-11-26 14:25:21 -08004116 first, target,
Tao Ma01225592008-08-18 17:38:53 +08004117 new_blk,
Tao Ma01225592008-08-18 17:38:53 +08004118 prev_clusters,
4119 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08004120 if (ret)
Joel Becker41cb8142008-11-26 14:25:21 -08004121 mlog_errno(ret);
Joel Becker41cb8142008-11-26 14:25:21 -08004122 } else {
Joel Becker92cf3ad2008-11-26 14:12:09 -08004123 /* The start of the last cluster in the first extent */
4124 u64 last_blk = bucket_blkno(first) +
4125 ((prev_clusters - 1) *
4126 ocfs2_clusters_to_blocks(inode->i_sb, 1));
Tao Ma01225592008-08-18 17:38:53 +08004127
Joel Becker012ee912008-11-26 14:43:31 -08004128 if (prev_clusters > 1 && bucket_blkno(target) != last_blk) {
Joel Becker874d65a2008-11-26 13:02:18 -08004129 ret = ocfs2_mv_xattr_buckets(inode, handle,
Joel Becker92cf3ad2008-11-26 14:12:09 -08004130 bucket_blkno(first),
Joel Becker54ecb6b2008-11-26 13:18:31 -08004131 last_blk, new_blk, 0,
Tao Ma01225592008-08-18 17:38:53 +08004132 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08004133 if (ret)
4134 mlog_errno(ret);
4135 } else {
Tao Ma80bcaf32008-10-27 06:06:24 +08004136 ret = ocfs2_divide_xattr_cluster(inode, handle,
4137 last_blk, new_blk,
4138 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08004139 if (ret)
4140 mlog_errno(ret);
Tao Ma01225592008-08-18 17:38:53 +08004141
Joel Becker92cf3ad2008-11-26 14:12:09 -08004142 if ((bucket_blkno(target) == last_blk) && extend)
Tao Ma01225592008-08-18 17:38:53 +08004143 *extend = 0;
4144 }
4145 }
4146
4147 return ret;
4148}
4149
4150/*
4151 * Add a new cluster for xattr storage.
4152 *
4153 * If the new cluster is contiguous with the previous one, it will be
4154 * appended to the same extent record, and num_clusters will be updated.
4155 * If not, we will insert a new extent for it and move some xattrs in
4156 * the last cluster into the new allocated one.
4157 * We also need to limit the maximum size of a btree leaf, otherwise we'll
4158 * lose the benefits of hashing because we'll have to search large leaves.
4159 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
4160 * if it's bigger).
4161 *
4162 * first_bh is the first block of the previous extent rec and header_bh
4163 * indicates the bucket we will insert the new xattrs. They will be updated
4164 * when the header_bh is moved into the new cluster.
4165 */
4166static int ocfs2_add_new_xattr_cluster(struct inode *inode,
4167 struct buffer_head *root_bh,
Joel Beckered29c0c2008-11-26 15:08:44 -08004168 struct ocfs2_xattr_bucket *first,
4169 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08004170 u32 *num_clusters,
4171 u32 prev_cpos,
Tao Ma78f30c32008-11-12 08:27:00 +08004172 int *extend,
4173 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004174{
Tao Ma85db90e2008-11-12 08:27:01 +08004175 int ret;
Tao Ma01225592008-08-18 17:38:53 +08004176 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
4177 u32 prev_clusters = *num_clusters;
4178 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
4179 u64 block;
Tao Ma85db90e2008-11-12 08:27:01 +08004180 handle_t *handle = ctxt->handle;
Tao Ma01225592008-08-18 17:38:53 +08004181 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004182 struct ocfs2_extent_tree et;
Tao Ma01225592008-08-18 17:38:53 +08004183
4184 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
4185 "previous xattr blkno = %llu\n",
4186 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Joel Beckered29c0c2008-11-26 15:08:44 -08004187 prev_cpos, (unsigned long long)bucket_blkno(first));
Tao Ma01225592008-08-18 17:38:53 +08004188
Joel Becker8d6220d2008-08-22 12:46:09 -07004189 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004190
Tao Ma01225592008-08-18 17:38:53 +08004191 ret = ocfs2_journal_access(handle, inode, root_bh,
4192 OCFS2_JOURNAL_ACCESS_WRITE);
4193 if (ret < 0) {
4194 mlog_errno(ret);
4195 goto leave;
4196 }
4197
Tao Ma78f30c32008-11-12 08:27:00 +08004198 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, 1,
Tao Ma01225592008-08-18 17:38:53 +08004199 clusters_to_add, &bit_off, &num_bits);
4200 if (ret < 0) {
4201 if (ret != -ENOSPC)
4202 mlog_errno(ret);
4203 goto leave;
4204 }
4205
4206 BUG_ON(num_bits > clusters_to_add);
4207
4208 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4209 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
4210 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4211
Joel Beckered29c0c2008-11-26 15:08:44 -08004212 if (bucket_blkno(first) + (prev_clusters * bpc) == block &&
Tao Ma01225592008-08-18 17:38:53 +08004213 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
4214 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
4215 /*
4216 * If this cluster is contiguous with the old one and
4217 * adding this new cluster, we don't surpass the limit of
4218 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
4219 * initialized and used like other buckets in the previous
4220 * cluster.
4221 * So add it as a contiguous one. The caller will handle
4222 * its init process.
4223 */
4224 v_start = prev_cpos + prev_clusters;
4225 *num_clusters = prev_clusters + num_bits;
4226 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
4227 num_bits);
4228 } else {
4229 ret = ocfs2_adjust_xattr_cross_cluster(inode,
4230 handle,
Joel Becker012ee912008-11-26 14:43:31 -08004231 first,
4232 target,
Tao Ma01225592008-08-18 17:38:53 +08004233 block,
Tao Ma01225592008-08-18 17:38:53 +08004234 prev_clusters,
4235 &v_start,
4236 extend);
4237 if (ret) {
4238 mlog_errno(ret);
4239 goto leave;
4240 }
4241 }
4242
4243 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
Mark Fashehde29c082008-10-29 14:45:30 -07004244 num_bits, (unsigned long long)block, v_start);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004245 ret = ocfs2_insert_extent(osb, handle, inode, &et, v_start, block,
Tao Ma78f30c32008-11-12 08:27:00 +08004246 num_bits, 0, ctxt->meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08004247 if (ret < 0) {
4248 mlog_errno(ret);
4249 goto leave;
4250 }
4251
4252 ret = ocfs2_journal_dirty(handle, root_bh);
Tao Ma85db90e2008-11-12 08:27:01 +08004253 if (ret < 0)
Tao Ma01225592008-08-18 17:38:53 +08004254 mlog_errno(ret);
Tao Ma01225592008-08-18 17:38:53 +08004255
4256leave:
Tao Ma01225592008-08-18 17:38:53 +08004257 return ret;
4258}
4259
4260/*
Joel Becker92de1092008-11-25 17:06:40 -08004261 * We are given an extent. 'first' is the bucket at the very front of
4262 * the extent. The extent has space for an additional bucket past
4263 * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
4264 * of the target bucket. We wish to shift every bucket past the target
4265 * down one, filling in that additional space. When we get back to the
4266 * target, we split the target between itself and the now-empty bucket
4267 * at target+1 (aka, target_blkno + blks_per_bucket).
Tao Ma01225592008-08-18 17:38:53 +08004268 */
4269static int ocfs2_extend_xattr_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004270 handle_t *handle,
Joel Becker92de1092008-11-25 17:06:40 -08004271 struct ocfs2_xattr_bucket *first,
4272 u64 target_blk,
Tao Ma01225592008-08-18 17:38:53 +08004273 u32 num_clusters)
4274{
4275 int ret, credits;
4276 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4277 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Joel Becker92de1092008-11-25 17:06:40 -08004278 u64 end_blk;
4279 u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets);
Tao Ma01225592008-08-18 17:38:53 +08004280
4281 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
Joel Becker92de1092008-11-25 17:06:40 -08004282 "from %llu, len = %u\n", (unsigned long long)target_blk,
4283 (unsigned long long)bucket_blkno(first), num_clusters);
Tao Ma01225592008-08-18 17:38:53 +08004284
Joel Becker92de1092008-11-25 17:06:40 -08004285 /* The extent must have room for an additional bucket */
4286 BUG_ON(new_bucket >=
4287 (num_clusters * ocfs2_xattr_buckets_per_cluster(osb)));
Tao Ma01225592008-08-18 17:38:53 +08004288
Joel Becker92de1092008-11-25 17:06:40 -08004289 /* end_blk points to the last existing bucket */
4290 end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004291
4292 /*
Joel Becker92de1092008-11-25 17:06:40 -08004293 * end_blk is the start of the last existing bucket.
4294 * Thus, (end_blk - target_blk) covers the target bucket and
4295 * every bucket after it up to, but not including, the last
4296 * existing bucket. Then we add the last existing bucket, the
4297 * new bucket, and the first bucket (3 * blk_per_bucket).
Tao Ma01225592008-08-18 17:38:53 +08004298 */
Joel Becker92de1092008-11-25 17:06:40 -08004299 credits = (end_blk - target_blk) + (3 * blk_per_bucket) +
Tao Ma85db90e2008-11-12 08:27:01 +08004300 handle->h_buffer_credits;
4301 ret = ocfs2_extend_trans(handle, credits);
4302 if (ret) {
Tao Ma01225592008-08-18 17:38:53 +08004303 mlog_errno(ret);
4304 goto out;
4305 }
4306
Joel Becker92de1092008-11-25 17:06:40 -08004307 ret = ocfs2_xattr_bucket_journal_access(handle, first,
4308 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004309 if (ret) {
4310 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004311 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004312 }
4313
Joel Becker92de1092008-11-25 17:06:40 -08004314 while (end_blk != target_blk) {
Tao Ma01225592008-08-18 17:38:53 +08004315 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
4316 end_blk + blk_per_bucket, 0);
4317 if (ret)
Tao Ma85db90e2008-11-12 08:27:01 +08004318 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004319 end_blk -= blk_per_bucket;
4320 }
4321
Joel Becker92de1092008-11-25 17:06:40 -08004322 /* Move half of the xattr in target_blkno to the next bucket. */
4323 ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk,
4324 target_blk + blk_per_bucket, NULL, 0);
Tao Ma01225592008-08-18 17:38:53 +08004325
Joel Becker92de1092008-11-25 17:06:40 -08004326 le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1);
4327 ocfs2_xattr_bucket_journal_dirty(handle, first);
Tao Ma01225592008-08-18 17:38:53 +08004328
Tao Ma01225592008-08-18 17:38:53 +08004329out:
4330 return ret;
4331}
4332
4333/*
Joel Becker91f20332008-11-26 15:25:41 -08004334 * Add new xattr bucket in an extent record and adjust the buckets
4335 * accordingly. xb_bh is the ocfs2_xattr_block, and target is the
4336 * bucket we want to insert into.
Tao Ma01225592008-08-18 17:38:53 +08004337 *
Joel Becker91f20332008-11-26 15:25:41 -08004338 * In the easy case, we will move all the buckets after target down by
4339 * one. Half of target's xattrs will be moved to the next bucket.
4340 *
4341 * If current cluster is full, we'll allocate a new one. This may not
4342 * be contiguous. The underlying calls will make sure that there is
4343 * space for the insert, shifting buckets around if necessary.
4344 * 'target' may be moved by those calls.
Tao Ma01225592008-08-18 17:38:53 +08004345 */
4346static int ocfs2_add_new_xattr_bucket(struct inode *inode,
4347 struct buffer_head *xb_bh,
Joel Becker91f20332008-11-26 15:25:41 -08004348 struct ocfs2_xattr_bucket *target,
Tao Ma78f30c32008-11-12 08:27:00 +08004349 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004350{
Tao Ma01225592008-08-18 17:38:53 +08004351 struct ocfs2_xattr_block *xb =
4352 (struct ocfs2_xattr_block *)xb_bh->b_data;
4353 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
4354 struct ocfs2_extent_list *el = &xb_root->xt_list;
Joel Becker91f20332008-11-26 15:25:41 -08004355 u32 name_hash =
4356 le32_to_cpu(bucket_xh(target)->xh_entries[0].xe_name_hash);
Joel Beckered29c0c2008-11-26 15:08:44 -08004357 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08004358 int ret, num_buckets, extend = 1;
4359 u64 p_blkno;
4360 u32 e_cpos, num_clusters;
Joel Becker92de1092008-11-25 17:06:40 -08004361 /* The bucket at the front of the extent */
Joel Becker91f20332008-11-26 15:25:41 -08004362 struct ocfs2_xattr_bucket *first;
Tao Ma01225592008-08-18 17:38:53 +08004363
Joel Becker91f20332008-11-26 15:25:41 -08004364 mlog(0, "Add new xattr bucket starting from %llu\n",
4365 (unsigned long long)bucket_blkno(target));
Tao Ma01225592008-08-18 17:38:53 +08004366
Joel Beckered29c0c2008-11-26 15:08:44 -08004367 /* The first bucket of the original extent */
Joel Becker92de1092008-11-25 17:06:40 -08004368 first = ocfs2_xattr_bucket_new(inode);
Joel Becker91f20332008-11-26 15:25:41 -08004369 if (!first) {
Joel Becker92de1092008-11-25 17:06:40 -08004370 ret = -ENOMEM;
4371 mlog_errno(ret);
4372 goto out;
4373 }
4374
Tao Ma01225592008-08-18 17:38:53 +08004375 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
4376 &num_clusters, el);
4377 if (ret) {
4378 mlog_errno(ret);
4379 goto out;
4380 }
4381
Joel Beckered29c0c2008-11-26 15:08:44 -08004382 ret = ocfs2_read_xattr_bucket(first, p_blkno);
4383 if (ret) {
4384 mlog_errno(ret);
4385 goto out;
4386 }
4387
Tao Ma01225592008-08-18 17:38:53 +08004388 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
Joel Beckered29c0c2008-11-26 15:08:44 -08004389 if (num_buckets == le16_to_cpu(bucket_xh(first)->xh_num_buckets)) {
4390 /*
4391 * This can move first+target if the target bucket moves
4392 * to the new extent.
4393 */
Tao Ma01225592008-08-18 17:38:53 +08004394 ret = ocfs2_add_new_xattr_cluster(inode,
4395 xb_bh,
Joel Beckered29c0c2008-11-26 15:08:44 -08004396 first,
4397 target,
Tao Ma01225592008-08-18 17:38:53 +08004398 &num_clusters,
4399 e_cpos,
Tao Ma78f30c32008-11-12 08:27:00 +08004400 &extend,
4401 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004402 if (ret) {
4403 mlog_errno(ret);
4404 goto out;
4405 }
4406 }
4407
Joel Becker92de1092008-11-25 17:06:40 -08004408 if (extend) {
Tao Ma01225592008-08-18 17:38:53 +08004409 ret = ocfs2_extend_xattr_bucket(inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004410 ctxt->handle,
Joel Beckered29c0c2008-11-26 15:08:44 -08004411 first,
4412 bucket_blkno(target),
Tao Ma01225592008-08-18 17:38:53 +08004413 num_clusters);
Joel Becker92de1092008-11-25 17:06:40 -08004414 if (ret)
4415 mlog_errno(ret);
4416 }
4417
Tao Ma01225592008-08-18 17:38:53 +08004418out:
Joel Becker92de1092008-11-25 17:06:40 -08004419 ocfs2_xattr_bucket_free(first);
Joel Beckered29c0c2008-11-26 15:08:44 -08004420
Tao Ma01225592008-08-18 17:38:53 +08004421 return ret;
4422}
4423
4424static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
4425 struct ocfs2_xattr_bucket *bucket,
4426 int offs)
4427{
4428 int block_off = offs >> inode->i_sb->s_blocksize_bits;
4429
4430 offs = offs % inode->i_sb->s_blocksize;
Joel Becker51def392008-10-24 16:57:21 -07004431 return bucket_block(bucket, block_off) + offs;
Tao Ma01225592008-08-18 17:38:53 +08004432}
4433
4434/*
4435 * Handle the normal xattr set, including replace, delete and new.
Tao Ma01225592008-08-18 17:38:53 +08004436 *
4437 * Note: "local" indicates the real data's locality. So we can't
4438 * just its bucket locality by its length.
4439 */
4440static void ocfs2_xattr_set_entry_normal(struct inode *inode,
4441 struct ocfs2_xattr_info *xi,
4442 struct ocfs2_xattr_search *xs,
4443 u32 name_hash,
Tao Ma5a095612008-09-19 22:17:41 +08004444 int local)
Tao Ma01225592008-08-18 17:38:53 +08004445{
4446 struct ocfs2_xattr_entry *last, *xe;
4447 int name_len = strlen(xi->name);
4448 struct ocfs2_xattr_header *xh = xs->header;
4449 u16 count = le16_to_cpu(xh->xh_count), start;
4450 size_t blocksize = inode->i_sb->s_blocksize;
4451 char *val;
4452 size_t offs, size, new_size;
4453
4454 last = &xh->xh_entries[count];
4455 if (!xs->not_found) {
4456 xe = xs->here;
4457 offs = le16_to_cpu(xe->xe_name_offset);
4458 if (ocfs2_xattr_is_local(xe))
4459 size = OCFS2_XATTR_SIZE(name_len) +
4460 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4461 else
4462 size = OCFS2_XATTR_SIZE(name_len) +
4463 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4464
4465 /*
4466 * If the new value will be stored outside, xi->value has been
4467 * initalized as an empty ocfs2_xattr_value_root, and the same
4468 * goes with xi->value_len, so we can set new_size safely here.
4469 * See ocfs2_xattr_set_in_bucket.
4470 */
4471 new_size = OCFS2_XATTR_SIZE(name_len) +
4472 OCFS2_XATTR_SIZE(xi->value_len);
4473
4474 le16_add_cpu(&xh->xh_name_value_len, -size);
4475 if (xi->value) {
4476 if (new_size > size)
4477 goto set_new_name_value;
4478
4479 /* Now replace the old value with new one. */
4480 if (local)
4481 xe->xe_value_size = cpu_to_le64(xi->value_len);
4482 else
4483 xe->xe_value_size = 0;
4484
4485 val = ocfs2_xattr_bucket_get_val(inode,
Joel Beckerba937122008-10-24 19:13:20 -07004486 xs->bucket, offs);
Tao Ma01225592008-08-18 17:38:53 +08004487 memset(val + OCFS2_XATTR_SIZE(name_len), 0,
4488 size - OCFS2_XATTR_SIZE(name_len));
4489 if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
4490 memcpy(val + OCFS2_XATTR_SIZE(name_len),
4491 xi->value, xi->value_len);
4492
4493 le16_add_cpu(&xh->xh_name_value_len, new_size);
4494 ocfs2_xattr_set_local(xe, local);
4495 return;
4496 } else {
Tao Ma5a095612008-09-19 22:17:41 +08004497 /*
4498 * Remove the old entry if there is more than one.
4499 * We don't remove the last entry so that we can
4500 * use it to indicate the hash value of the empty
4501 * bucket.
4502 */
Tao Ma01225592008-08-18 17:38:53 +08004503 last -= 1;
Tao Ma01225592008-08-18 17:38:53 +08004504 le16_add_cpu(&xh->xh_count, -1);
Tao Ma5a095612008-09-19 22:17:41 +08004505 if (xh->xh_count) {
4506 memmove(xe, xe + 1,
4507 (void *)last - (void *)xe);
4508 memset(last, 0,
4509 sizeof(struct ocfs2_xattr_entry));
4510 } else
4511 xh->xh_free_start =
4512 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4513
Tao Ma01225592008-08-18 17:38:53 +08004514 return;
4515 }
4516 } else {
4517 /* find a new entry for insert. */
4518 int low = 0, high = count - 1, tmp;
4519 struct ocfs2_xattr_entry *tmp_xe;
4520
Tao Ma5a095612008-09-19 22:17:41 +08004521 while (low <= high && count) {
Tao Ma01225592008-08-18 17:38:53 +08004522 tmp = (low + high) / 2;
4523 tmp_xe = &xh->xh_entries[tmp];
4524
4525 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
4526 low = tmp + 1;
4527 else if (name_hash <
4528 le32_to_cpu(tmp_xe->xe_name_hash))
4529 high = tmp - 1;
Tao Ma06b240d2008-09-19 22:16:34 +08004530 else {
4531 low = tmp;
Tao Ma01225592008-08-18 17:38:53 +08004532 break;
Tao Ma06b240d2008-09-19 22:16:34 +08004533 }
Tao Ma01225592008-08-18 17:38:53 +08004534 }
4535
4536 xe = &xh->xh_entries[low];
4537 if (low != count)
4538 memmove(xe + 1, xe, (void *)last - (void *)xe);
4539
4540 le16_add_cpu(&xh->xh_count, 1);
4541 memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
4542 xe->xe_name_hash = cpu_to_le32(name_hash);
4543 xe->xe_name_len = name_len;
4544 ocfs2_xattr_set_type(xe, xi->name_index);
4545 }
4546
4547set_new_name_value:
4548 /* Insert the new name+value. */
4549 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
4550
4551 /*
4552 * We must make sure that the name/value pair
4553 * exists in the same block.
4554 */
4555 offs = le16_to_cpu(xh->xh_free_start);
4556 start = offs - size;
4557
4558 if (start >> inode->i_sb->s_blocksize_bits !=
4559 (offs - 1) >> inode->i_sb->s_blocksize_bits) {
4560 offs = offs - offs % blocksize;
4561 xh->xh_free_start = cpu_to_le16(offs);
4562 }
4563
Joel Beckerba937122008-10-24 19:13:20 -07004564 val = ocfs2_xattr_bucket_get_val(inode, xs->bucket, offs - size);
Tao Ma01225592008-08-18 17:38:53 +08004565 xe->xe_name_offset = cpu_to_le16(offs - size);
4566
4567 memset(val, 0, size);
4568 memcpy(val, xi->name, name_len);
4569 memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
4570
4571 xe->xe_value_size = cpu_to_le64(xi->value_len);
4572 ocfs2_xattr_set_local(xe, local);
4573 xs->here = xe;
4574 le16_add_cpu(&xh->xh_free_start, -size);
4575 le16_add_cpu(&xh->xh_name_value_len, size);
4576
4577 return;
4578}
4579
Tao Ma01225592008-08-18 17:38:53 +08004580/*
4581 * Set the xattr entry in the specified bucket.
4582 * The bucket is indicated by xs->bucket and it should have the enough
4583 * space for the xattr insertion.
4584 */
4585static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004586 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004587 struct ocfs2_xattr_info *xi,
4588 struct ocfs2_xattr_search *xs,
4589 u32 name_hash,
Tao Ma5a095612008-09-19 22:17:41 +08004590 int local)
Tao Ma01225592008-08-18 17:38:53 +08004591{
Joel Becker1224be02008-10-24 18:47:33 -07004592 int ret;
Joel Becker02dbf382008-10-27 18:07:45 -07004593 u64 blkno;
Tao Ma01225592008-08-18 17:38:53 +08004594
Mark Fashehff1ec202008-08-19 10:54:29 -07004595 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4596 (unsigned long)xi->value_len, xi->name_index,
Joel Beckerba937122008-10-24 19:13:20 -07004597 (unsigned long long)bucket_blkno(xs->bucket));
Tao Ma01225592008-08-18 17:38:53 +08004598
Joel Beckerba937122008-10-24 19:13:20 -07004599 if (!xs->bucket->bu_bhs[1]) {
Joel Becker02dbf382008-10-27 18:07:45 -07004600 blkno = bucket_blkno(xs->bucket);
4601 ocfs2_xattr_bucket_relse(xs->bucket);
4602 ret = ocfs2_read_xattr_bucket(xs->bucket, blkno);
Tao Ma01225592008-08-18 17:38:53 +08004603 if (ret) {
4604 mlog_errno(ret);
4605 goto out;
4606 }
4607 }
4608
Joel Beckerba937122008-10-24 19:13:20 -07004609 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004610 OCFS2_JOURNAL_ACCESS_WRITE);
4611 if (ret < 0) {
4612 mlog_errno(ret);
4613 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004614 }
4615
Tao Ma5a095612008-09-19 22:17:41 +08004616 ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
Joel Beckerba937122008-10-24 19:13:20 -07004617 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004618
Tao Ma01225592008-08-18 17:38:53 +08004619out:
Tao Ma01225592008-08-18 17:38:53 +08004620 return ret;
4621}
4622
Tao Ma01225592008-08-18 17:38:53 +08004623/*
4624 * Truncate the specified xe_off entry in xattr bucket.
4625 * bucket is indicated by header_bh and len is the new length.
4626 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4627 *
4628 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4629 */
4630static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
Joel Becker548b0f22008-11-24 19:32:13 -08004631 struct ocfs2_xattr_bucket *bucket,
Tao Ma01225592008-08-18 17:38:53 +08004632 int xe_off,
Tao Ma78f30c32008-11-12 08:27:00 +08004633 int len,
4634 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004635{
4636 int ret, offset;
4637 u64 value_blk;
4638 struct buffer_head *value_bh = NULL;
4639 struct ocfs2_xattr_value_root *xv;
4640 struct ocfs2_xattr_entry *xe;
Joel Becker548b0f22008-11-24 19:32:13 -08004641 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma01225592008-08-18 17:38:53 +08004642 size_t blocksize = inode->i_sb->s_blocksize;
4643
4644 xe = &xh->xh_entries[xe_off];
4645
4646 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
4647
4648 offset = le16_to_cpu(xe->xe_name_offset) +
4649 OCFS2_XATTR_SIZE(xe->xe_name_len);
4650
4651 value_blk = offset / blocksize;
4652
4653 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4654 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
Tao Ma01225592008-08-18 17:38:53 +08004655
Joel Becker548b0f22008-11-24 19:32:13 -08004656 value_bh = bucket->bu_bhs[value_blk];
4657 BUG_ON(!value_bh);
Tao Ma01225592008-08-18 17:38:53 +08004658
4659 xv = (struct ocfs2_xattr_value_root *)
4660 (value_bh->b_data + offset % blocksize);
4661
Joel Becker548b0f22008-11-24 19:32:13 -08004662 ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
4663 OCFS2_JOURNAL_ACCESS_WRITE);
4664 if (ret) {
4665 mlog_errno(ret);
4666 goto out;
4667 }
4668
4669 /*
4670 * From here on out we have to dirty the bucket. The generic
4671 * value calls only modify one of the bucket's bhs, but we need
4672 * to send the bucket at once. So if they error, they *could* have
4673 * modified something. We have to assume they did, and dirty
4674 * the whole bucket. This leaves us in a consistent state.
4675 */
Tao Ma01225592008-08-18 17:38:53 +08004676 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
Joel Becker548b0f22008-11-24 19:32:13 -08004677 xe_off, (unsigned long long)bucket_blkno(bucket), len);
Tao Ma78f30c32008-11-12 08:27:00 +08004678 ret = ocfs2_xattr_value_truncate(inode, value_bh, xv, len, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004679 if (ret) {
4680 mlog_errno(ret);
Joel Becker548b0f22008-11-24 19:32:13 -08004681 goto out_dirty;
Tao Ma01225592008-08-18 17:38:53 +08004682 }
4683
Joel Becker548b0f22008-11-24 19:32:13 -08004684 xe->xe_value_size = cpu_to_le64(len);
4685
4686out_dirty:
4687 ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket);
Tao Ma01225592008-08-18 17:38:53 +08004688
4689out:
Tao Ma01225592008-08-18 17:38:53 +08004690 return ret;
4691}
4692
4693static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +08004694 struct ocfs2_xattr_search *xs,
4695 int len,
4696 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004697{
4698 int ret, offset;
4699 struct ocfs2_xattr_entry *xe = xs->here;
4700 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
4701
Joel Beckerba937122008-10-24 19:13:20 -07004702 BUG_ON(!xs->bucket->bu_bhs[0] || !xe || ocfs2_xattr_is_local(xe));
Tao Ma01225592008-08-18 17:38:53 +08004703
4704 offset = xe - xh->xh_entries;
Joel Becker548b0f22008-11-24 19:32:13 -08004705 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket,
Tao Ma78f30c32008-11-12 08:27:00 +08004706 offset, len, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004707 if (ret)
4708 mlog_errno(ret);
4709
4710 return ret;
4711}
4712
4713static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004714 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004715 struct ocfs2_xattr_search *xs,
4716 char *val,
4717 int value_len)
4718{
4719 int offset;
4720 struct ocfs2_xattr_value_root *xv;
4721 struct ocfs2_xattr_entry *xe = xs->here;
4722
4723 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
4724
4725 offset = le16_to_cpu(xe->xe_name_offset) +
4726 OCFS2_XATTR_SIZE(xe->xe_name_len);
4727
4728 xv = (struct ocfs2_xattr_value_root *)(xs->base + offset);
4729
Tao Ma85db90e2008-11-12 08:27:01 +08004730 return __ocfs2_xattr_set_value_outside(inode, handle,
4731 xv, val, value_len);
Tao Ma01225592008-08-18 17:38:53 +08004732}
4733
Tao Ma01225592008-08-18 17:38:53 +08004734static int ocfs2_rm_xattr_cluster(struct inode *inode,
4735 struct buffer_head *root_bh,
4736 u64 blkno,
4737 u32 cpos,
4738 u32 len)
4739{
4740 int ret;
4741 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4742 struct inode *tl_inode = osb->osb_tl_inode;
4743 handle_t *handle;
4744 struct ocfs2_xattr_block *xb =
4745 (struct ocfs2_xattr_block *)root_bh->b_data;
Tao Ma01225592008-08-18 17:38:53 +08004746 struct ocfs2_alloc_context *meta_ac = NULL;
4747 struct ocfs2_cached_dealloc_ctxt dealloc;
Joel Beckerf99b9b72008-08-20 19:36:33 -07004748 struct ocfs2_extent_tree et;
4749
Joel Becker8d6220d2008-08-22 12:46:09 -07004750 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
Tao Ma01225592008-08-18 17:38:53 +08004751
4752 ocfs2_init_dealloc_ctxt(&dealloc);
4753
4754 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
4755 cpos, len, (unsigned long long)blkno);
4756
4757 ocfs2_remove_xattr_clusters_from_cache(inode, blkno, len);
4758
Joel Beckerf99b9b72008-08-20 19:36:33 -07004759 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08004760 if (ret) {
4761 mlog_errno(ret);
4762 return ret;
4763 }
4764
4765 mutex_lock(&tl_inode->i_mutex);
4766
4767 if (ocfs2_truncate_log_needs_flush(osb)) {
4768 ret = __ocfs2_flush_truncate_log(osb);
4769 if (ret < 0) {
4770 mlog_errno(ret);
4771 goto out;
4772 }
4773 }
4774
Jan Karaa90714c2008-10-09 19:38:40 +02004775 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
Tao Mad3264792008-10-24 07:57:28 +08004776 if (IS_ERR(handle)) {
Tao Ma01225592008-08-18 17:38:53 +08004777 ret = -ENOMEM;
4778 mlog_errno(ret);
4779 goto out;
4780 }
4781
4782 ret = ocfs2_journal_access(handle, inode, root_bh,
4783 OCFS2_JOURNAL_ACCESS_WRITE);
4784 if (ret) {
4785 mlog_errno(ret);
4786 goto out_commit;
4787 }
4788
Joel Beckerf99b9b72008-08-20 19:36:33 -07004789 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
4790 &dealloc);
Tao Ma01225592008-08-18 17:38:53 +08004791 if (ret) {
4792 mlog_errno(ret);
4793 goto out_commit;
4794 }
4795
4796 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
4797
4798 ret = ocfs2_journal_dirty(handle, root_bh);
4799 if (ret) {
4800 mlog_errno(ret);
4801 goto out_commit;
4802 }
4803
4804 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
4805 if (ret)
4806 mlog_errno(ret);
4807
4808out_commit:
4809 ocfs2_commit_trans(osb, handle);
4810out:
4811 ocfs2_schedule_truncate_log_flush(osb, 1);
4812
4813 mutex_unlock(&tl_inode->i_mutex);
4814
4815 if (meta_ac)
4816 ocfs2_free_alloc_context(meta_ac);
4817
4818 ocfs2_run_deallocs(osb, &dealloc);
4819
4820 return ret;
4821}
4822
Tao Ma01225592008-08-18 17:38:53 +08004823static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004824 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004825 struct ocfs2_xattr_search *xs)
4826{
Joel Beckerba937122008-10-24 19:13:20 -07004827 struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004828 struct ocfs2_xattr_entry *last = &xh->xh_entries[
4829 le16_to_cpu(xh->xh_count) - 1];
4830 int ret = 0;
4831
Joel Beckerba937122008-10-24 19:13:20 -07004832 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004833 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004834 if (ret) {
4835 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004836 return;
Tao Ma01225592008-08-18 17:38:53 +08004837 }
4838
4839 /* Remove the old entry. */
4840 memmove(xs->here, xs->here + 1,
4841 (void *)last - (void *)xs->here);
4842 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
4843 le16_add_cpu(&xh->xh_count, -1);
4844
Joel Beckerba937122008-10-24 19:13:20 -07004845 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004846}
4847
4848/*
4849 * Set the xattr name/value in the bucket specified in xs.
4850 *
4851 * As the new value in xi may be stored in the bucket or in an outside cluster,
4852 * we divide the whole process into 3 steps:
4853 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
4854 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
4855 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
4856 * 4. If the clusters for the new outside value can't be allocated, we need
4857 * to free the xattr we allocated in set.
4858 */
4859static int ocfs2_xattr_set_in_bucket(struct inode *inode,
4860 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08004861 struct ocfs2_xattr_search *xs,
4862 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004863{
Tao Ma5a095612008-09-19 22:17:41 +08004864 int ret, local = 1;
Tao Ma01225592008-08-18 17:38:53 +08004865 size_t value_len;
4866 char *val = (char *)xi->value;
4867 struct ocfs2_xattr_entry *xe = xs->here;
Tao Ma2057e5c2008-10-09 23:06:13 +08004868 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name,
4869 strlen(xi->name));
Tao Ma01225592008-08-18 17:38:53 +08004870
4871 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
4872 /*
4873 * We need to truncate the xattr storage first.
4874 *
4875 * If both the old and new value are stored to
4876 * outside block, we only need to truncate
4877 * the storage and then set the value outside.
4878 *
4879 * If the new value should be stored within block,
4880 * we should free all the outside block first and
4881 * the modification to the xattr block will be done
4882 * by following steps.
4883 */
4884 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4885 value_len = xi->value_len;
4886 else
4887 value_len = 0;
4888
4889 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
Tao Ma78f30c32008-11-12 08:27:00 +08004890 value_len,
4891 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004892 if (ret)
4893 goto out;
4894
4895 if (value_len)
4896 goto set_value_outside;
4897 }
4898
4899 value_len = xi->value_len;
4900 /* So we have to handle the inside block change now. */
4901 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
4902 /*
4903 * If the new value will be stored outside of block,
4904 * initalize a new empty value root and insert it first.
4905 */
4906 local = 0;
4907 xi->value = &def_xv;
4908 xi->value_len = OCFS2_XATTR_ROOT_SIZE;
4909 }
4910
Tao Ma85db90e2008-11-12 08:27:01 +08004911 ret = ocfs2_xattr_set_entry_in_bucket(inode, ctxt->handle, xi, xs,
4912 name_hash, local);
Tao Ma01225592008-08-18 17:38:53 +08004913 if (ret) {
4914 mlog_errno(ret);
4915 goto out;
4916 }
4917
Tao Ma5a095612008-09-19 22:17:41 +08004918 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
4919 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004920
Tao Ma5a095612008-09-19 22:17:41 +08004921 /* allocate the space now for the outside block storage. */
4922 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
Tao Ma78f30c32008-11-12 08:27:00 +08004923 value_len, ctxt);
Tao Ma5a095612008-09-19 22:17:41 +08004924 if (ret) {
4925 mlog_errno(ret);
4926
4927 if (xs->not_found) {
4928 /*
4929 * We can't allocate enough clusters for outside
4930 * storage and we have allocated xattr already,
4931 * so need to remove it.
4932 */
Tao Ma85db90e2008-11-12 08:27:01 +08004933 ocfs2_xattr_bucket_remove_xs(inode, ctxt->handle, xs);
Tao Ma01225592008-08-18 17:38:53 +08004934 }
Tao Ma01225592008-08-18 17:38:53 +08004935 goto out;
4936 }
4937
4938set_value_outside:
Tao Ma85db90e2008-11-12 08:27:01 +08004939 ret = ocfs2_xattr_bucket_set_value_outside(inode, ctxt->handle,
4940 xs, val, value_len);
Tao Ma01225592008-08-18 17:38:53 +08004941out:
4942 return ret;
4943}
4944
Tao Ma80bcaf32008-10-27 06:06:24 +08004945/*
4946 * check whether the xattr bucket is filled up with the same hash value.
4947 * If we want to insert the xattr with the same hash, return -ENOSPC.
4948 * If we want to insert a xattr with different hash value, go ahead
4949 * and ocfs2_divide_xattr_bucket will handle this.
4950 */
Tao Ma01225592008-08-18 17:38:53 +08004951static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
Tao Ma80bcaf32008-10-27 06:06:24 +08004952 struct ocfs2_xattr_bucket *bucket,
4953 const char *name)
Tao Ma01225592008-08-18 17:38:53 +08004954{
Joel Becker3e632942008-10-24 17:04:49 -07004955 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08004956 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
4957
4958 if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
4959 return 0;
Tao Ma01225592008-08-18 17:38:53 +08004960
4961 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
4962 xh->xh_entries[0].xe_name_hash) {
4963 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
4964 "hash = %u\n",
Joel Becker9c7759a2008-10-24 16:21:03 -07004965 (unsigned long long)bucket_blkno(bucket),
Tao Ma01225592008-08-18 17:38:53 +08004966 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
4967 return -ENOSPC;
4968 }
4969
4970 return 0;
4971}
4972
4973static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
4974 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08004975 struct ocfs2_xattr_search *xs,
4976 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004977{
4978 struct ocfs2_xattr_header *xh;
4979 struct ocfs2_xattr_entry *xe;
4980 u16 count, header_size, xh_free_start;
Joel Becker6dde41d2008-10-24 17:16:48 -07004981 int free, max_free, need, old;
Tao Ma01225592008-08-18 17:38:53 +08004982 size_t value_size = 0, name_len = strlen(xi->name);
4983 size_t blocksize = inode->i_sb->s_blocksize;
4984 int ret, allocation = 0;
Tao Ma01225592008-08-18 17:38:53 +08004985
4986 mlog_entry("Set xattr %s in xattr index block\n", xi->name);
4987
4988try_again:
4989 xh = xs->header;
4990 count = le16_to_cpu(xh->xh_count);
4991 xh_free_start = le16_to_cpu(xh->xh_free_start);
4992 header_size = sizeof(struct ocfs2_xattr_header) +
4993 count * sizeof(struct ocfs2_xattr_entry);
4994 max_free = OCFS2_XATTR_BUCKET_SIZE -
4995 le16_to_cpu(xh->xh_name_value_len) - header_size;
4996
4997 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
4998 "of %u which exceed block size\n",
Joel Beckerba937122008-10-24 19:13:20 -07004999 (unsigned long long)bucket_blkno(xs->bucket),
Tao Ma01225592008-08-18 17:38:53 +08005000 header_size);
5001
5002 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
5003 value_size = OCFS2_XATTR_ROOT_SIZE;
5004 else if (xi->value)
5005 value_size = OCFS2_XATTR_SIZE(xi->value_len);
5006
5007 if (xs->not_found)
5008 need = sizeof(struct ocfs2_xattr_entry) +
5009 OCFS2_XATTR_SIZE(name_len) + value_size;
5010 else {
5011 need = value_size + OCFS2_XATTR_SIZE(name_len);
5012
5013 /*
5014 * We only replace the old value if the new length is smaller
5015 * than the old one. Otherwise we will allocate new space in the
5016 * bucket to store it.
5017 */
5018 xe = xs->here;
5019 if (ocfs2_xattr_is_local(xe))
5020 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
5021 else
5022 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
5023
5024 if (old >= value_size)
5025 need = 0;
5026 }
5027
5028 free = xh_free_start - header_size;
5029 /*
5030 * We need to make sure the new name/value pair
5031 * can exist in the same block.
5032 */
5033 if (xh_free_start % blocksize < need)
5034 free -= xh_free_start % blocksize;
5035
5036 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
5037 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
5038 " %u\n", xs->not_found,
Joel Beckerba937122008-10-24 19:13:20 -07005039 (unsigned long long)bucket_blkno(xs->bucket),
Tao Ma01225592008-08-18 17:38:53 +08005040 free, need, max_free, le16_to_cpu(xh->xh_free_start),
5041 le16_to_cpu(xh->xh_name_value_len));
5042
Tao Ma976331d2008-11-12 08:26:57 +08005043 if (free < need ||
5044 (xs->not_found &&
5045 count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb))) {
Tao Ma01225592008-08-18 17:38:53 +08005046 if (need <= max_free &&
5047 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
5048 /*
5049 * We can create the space by defragment. Since only the
5050 * name/value will be moved, the xe shouldn't be changed
5051 * in xs.
5052 */
Tao Ma85db90e2008-11-12 08:27:01 +08005053 ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
5054 xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08005055 if (ret) {
5056 mlog_errno(ret);
5057 goto out;
5058 }
5059
5060 xh_free_start = le16_to_cpu(xh->xh_free_start);
5061 free = xh_free_start - header_size;
5062 if (xh_free_start % blocksize < need)
5063 free -= xh_free_start % blocksize;
5064
5065 if (free >= need)
5066 goto xattr_set;
5067
5068 mlog(0, "Can't get enough space for xattr insert by "
5069 "defragment. Need %u bytes, but we have %d, so "
5070 "allocate new bucket for it.\n", need, free);
5071 }
5072
5073 /*
5074 * We have to add new buckets or clusters and one
5075 * allocation should leave us enough space for insert.
5076 */
5077 BUG_ON(allocation);
5078
5079 /*
5080 * We do not allow for overlapping ranges between buckets. And
5081 * the maximum number of collisions we will allow for then is
5082 * one bucket's worth, so check it here whether we need to
5083 * add a new bucket for the insert.
5084 */
Tao Ma80bcaf32008-10-27 06:06:24 +08005085 ret = ocfs2_check_xattr_bucket_collision(inode,
Joel Beckerba937122008-10-24 19:13:20 -07005086 xs->bucket,
Tao Ma80bcaf32008-10-27 06:06:24 +08005087 xi->name);
Tao Ma01225592008-08-18 17:38:53 +08005088 if (ret) {
5089 mlog_errno(ret);
5090 goto out;
5091 }
5092
5093 ret = ocfs2_add_new_xattr_bucket(inode,
5094 xs->xattr_bh,
Joel Becker91f20332008-11-26 15:25:41 -08005095 xs->bucket,
Tao Ma78f30c32008-11-12 08:27:00 +08005096 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005097 if (ret) {
5098 mlog_errno(ret);
5099 goto out;
5100 }
5101
Joel Becker91f20332008-11-26 15:25:41 -08005102 /*
5103 * ocfs2_add_new_xattr_bucket() will have updated
5104 * xs->bucket if it moved, but it will not have updated
5105 * any of the other search fields. Thus, we drop it and
5106 * re-search. Everything should be cached, so it'll be
5107 * quick.
5108 */
Joel Beckerba937122008-10-24 19:13:20 -07005109 ocfs2_xattr_bucket_relse(xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08005110 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
5111 xi->name_index,
5112 xi->name, xs);
5113 if (ret && ret != -ENODATA)
5114 goto out;
5115 xs->not_found = ret;
5116 allocation = 1;
5117 goto try_again;
5118 }
5119
5120xattr_set:
Tao Ma78f30c32008-11-12 08:27:00 +08005121 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005122out:
5123 mlog_exit(ret);
5124 return ret;
5125}
Tao Maa3944252008-08-18 17:38:54 +08005126
5127static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
5128 struct ocfs2_xattr_bucket *bucket,
5129 void *para)
5130{
5131 int ret = 0;
Joel Becker3e632942008-10-24 17:04:49 -07005132 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Maa3944252008-08-18 17:38:54 +08005133 u16 i;
5134 struct ocfs2_xattr_entry *xe;
Tao Ma78f30c32008-11-12 08:27:00 +08005135 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5136 struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
Joel Becker548b0f22008-11-24 19:32:13 -08005137 int credits = ocfs2_remove_extent_credits(osb->sb) +
5138 ocfs2_blocks_per_xattr_bucket(inode->i_sb);
5139
Tao Ma78f30c32008-11-12 08:27:00 +08005140
5141 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
Tao Maa3944252008-08-18 17:38:54 +08005142
5143 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
5144 xe = &xh->xh_entries[i];
5145 if (ocfs2_xattr_is_local(xe))
5146 continue;
5147
Tao Ma88c3b062008-12-11 08:54:11 +08005148 ctxt.handle = ocfs2_start_trans(osb, credits);
5149 if (IS_ERR(ctxt.handle)) {
5150 ret = PTR_ERR(ctxt.handle);
5151 mlog_errno(ret);
5152 break;
5153 }
5154
Joel Becker548b0f22008-11-24 19:32:13 -08005155 ret = ocfs2_xattr_bucket_value_truncate(inode, bucket,
Tao Ma78f30c32008-11-12 08:27:00 +08005156 i, 0, &ctxt);
Tao Ma88c3b062008-12-11 08:54:11 +08005157
5158 ocfs2_commit_trans(osb, ctxt.handle);
Tao Maa3944252008-08-18 17:38:54 +08005159 if (ret) {
5160 mlog_errno(ret);
5161 break;
5162 }
5163 }
5164
Tao Ma78f30c32008-11-12 08:27:00 +08005165 ocfs2_schedule_truncate_log_flush(osb, 1);
5166 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tao Maa3944252008-08-18 17:38:54 +08005167 return ret;
5168}
5169
5170static int ocfs2_delete_xattr_index_block(struct inode *inode,
5171 struct buffer_head *xb_bh)
5172{
5173 struct ocfs2_xattr_block *xb =
5174 (struct ocfs2_xattr_block *)xb_bh->b_data;
5175 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
5176 int ret = 0;
5177 u32 name_hash = UINT_MAX, e_cpos, num_clusters;
5178 u64 p_blkno;
5179
5180 if (le16_to_cpu(el->l_next_free_rec) == 0)
5181 return 0;
5182
5183 while (name_hash > 0) {
5184 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
5185 &e_cpos, &num_clusters, el);
5186 if (ret) {
5187 mlog_errno(ret);
5188 goto out;
5189 }
5190
5191 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
5192 ocfs2_delete_xattr_in_bucket,
5193 NULL);
5194 if (ret) {
5195 mlog_errno(ret);
5196 goto out;
5197 }
5198
5199 ret = ocfs2_rm_xattr_cluster(inode, xb_bh,
5200 p_blkno, e_cpos, num_clusters);
5201 if (ret) {
5202 mlog_errno(ret);
5203 break;
5204 }
5205
5206 if (e_cpos == 0)
5207 break;
5208
5209 name_hash = e_cpos - 1;
5210 }
5211
5212out:
5213 return ret;
5214}
Mark Fasheh99219ae2008-10-07 14:52:59 -07005215
5216/*
Tiger Yang923f7f32008-11-14 11:16:27 +08005217 * 'security' attributes support
5218 */
5219static size_t ocfs2_xattr_security_list(struct inode *inode, char *list,
5220 size_t list_size, const char *name,
5221 size_t name_len)
5222{
5223 const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN;
5224 const size_t total_len = prefix_len + name_len + 1;
5225
5226 if (list && total_len <= list_size) {
5227 memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
5228 memcpy(list + prefix_len, name, name_len);
5229 list[prefix_len + name_len] = '\0';
5230 }
5231 return total_len;
5232}
5233
5234static int ocfs2_xattr_security_get(struct inode *inode, const char *name,
5235 void *buffer, size_t size)
5236{
5237 if (strcmp(name, "") == 0)
5238 return -EINVAL;
5239 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_SECURITY, name,
5240 buffer, size);
5241}
5242
5243static int ocfs2_xattr_security_set(struct inode *inode, const char *name,
5244 const void *value, size_t size, int flags)
5245{
5246 if (strcmp(name, "") == 0)
5247 return -EINVAL;
5248
5249 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY, name, value,
5250 size, flags);
5251}
5252
Tiger Yang534eadd2008-11-14 11:16:41 +08005253int ocfs2_init_security_get(struct inode *inode,
5254 struct inode *dir,
5255 struct ocfs2_security_xattr_info *si)
5256{
5257 return security_inode_init_security(inode, dir, &si->name, &si->value,
5258 &si->value_len);
5259}
5260
5261int ocfs2_init_security_set(handle_t *handle,
5262 struct inode *inode,
5263 struct buffer_head *di_bh,
5264 struct ocfs2_security_xattr_info *si,
5265 struct ocfs2_alloc_context *xattr_ac,
5266 struct ocfs2_alloc_context *data_ac)
5267{
5268 return ocfs2_xattr_set_handle(handle, inode, di_bh,
5269 OCFS2_XATTR_INDEX_SECURITY,
5270 si->name, si->value, si->value_len, 0,
5271 xattr_ac, data_ac);
5272}
5273
Tiger Yang923f7f32008-11-14 11:16:27 +08005274struct xattr_handler ocfs2_xattr_security_handler = {
5275 .prefix = XATTR_SECURITY_PREFIX,
5276 .list = ocfs2_xattr_security_list,
5277 .get = ocfs2_xattr_security_get,
5278 .set = ocfs2_xattr_security_set,
5279};
5280
5281/*
Mark Fasheh99219ae2008-10-07 14:52:59 -07005282 * 'trusted' attributes support
5283 */
Mark Fasheh99219ae2008-10-07 14:52:59 -07005284static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list,
5285 size_t list_size, const char *name,
5286 size_t name_len)
5287{
Tiger Yangceb1eba2008-10-23 16:34:13 +08005288 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
Mark Fasheh99219ae2008-10-07 14:52:59 -07005289 const size_t total_len = prefix_len + name_len + 1;
5290
5291 if (list && total_len <= list_size) {
5292 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
5293 memcpy(list + prefix_len, name, name_len);
5294 list[prefix_len + name_len] = '\0';
5295 }
5296 return total_len;
5297}
5298
5299static int ocfs2_xattr_trusted_get(struct inode *inode, const char *name,
5300 void *buffer, size_t size)
5301{
5302 if (strcmp(name, "") == 0)
5303 return -EINVAL;
5304 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, name,
5305 buffer, size);
5306}
5307
5308static int ocfs2_xattr_trusted_set(struct inode *inode, const char *name,
5309 const void *value, size_t size, int flags)
5310{
5311 if (strcmp(name, "") == 0)
5312 return -EINVAL;
5313
5314 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, name, value,
5315 size, flags);
5316}
5317
5318struct xattr_handler ocfs2_xattr_trusted_handler = {
5319 .prefix = XATTR_TRUSTED_PREFIX,
5320 .list = ocfs2_xattr_trusted_list,
5321 .get = ocfs2_xattr_trusted_get,
5322 .set = ocfs2_xattr_trusted_set,
5323};
5324
Mark Fasheh99219ae2008-10-07 14:52:59 -07005325/*
5326 * 'user' attributes support
5327 */
Mark Fasheh99219ae2008-10-07 14:52:59 -07005328static size_t ocfs2_xattr_user_list(struct inode *inode, char *list,
5329 size_t list_size, const char *name,
5330 size_t name_len)
5331{
Tiger Yangceb1eba2008-10-23 16:34:13 +08005332 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
Mark Fasheh99219ae2008-10-07 14:52:59 -07005333 const size_t total_len = prefix_len + name_len + 1;
5334 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5335
5336 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
5337 return 0;
5338
5339 if (list && total_len <= list_size) {
5340 memcpy(list, XATTR_USER_PREFIX, prefix_len);
5341 memcpy(list + prefix_len, name, name_len);
5342 list[prefix_len + name_len] = '\0';
5343 }
5344 return total_len;
5345}
5346
5347static int ocfs2_xattr_user_get(struct inode *inode, const char *name,
5348 void *buffer, size_t size)
5349{
5350 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5351
5352 if (strcmp(name, "") == 0)
5353 return -EINVAL;
5354 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
5355 return -EOPNOTSUPP;
5356 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name,
5357 buffer, size);
5358}
5359
5360static int ocfs2_xattr_user_set(struct inode *inode, const char *name,
5361 const void *value, size_t size, int flags)
5362{
5363 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5364
5365 if (strcmp(name, "") == 0)
5366 return -EINVAL;
5367 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
5368 return -EOPNOTSUPP;
5369
5370 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, name, value,
5371 size, flags);
5372}
5373
5374struct xattr_handler ocfs2_xattr_user_handler = {
5375 .prefix = XATTR_USER_PREFIX,
5376 .list = ocfs2_xattr_user_list,
5377 .get = ocfs2_xattr_user_get,
5378 .set = ocfs2_xattr_user_set,
5379};