blob: 8d1a0abc105c438d65ae22d9e263ee5d8df0164a [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"
Tao Ma492a8a32009-08-18 11:43:17 +080058#include "refcounttree.h"
Tiger Yangcf1d6c72008-08-18 17:11:00 +080059
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 Yang4442f512009-02-20 11:11:50 +080085#define OCFS2_XATTR_HEADER_GAP 4
Tiger Yang534eadd2008-11-14 11:16:41 +080086#define OCFS2_XATTR_FREE_IN_IBODY (OCFS2_MIN_XATTR_INLINE_SIZE \
87 - sizeof(struct ocfs2_xattr_header) \
Tiger Yang4442f512009-02-20 11:11:50 +080088 - OCFS2_XATTR_HEADER_GAP)
Tiger Yang89c38bd2008-11-14 11:17:41 +080089#define OCFS2_XATTR_FREE_IN_BLOCK(ptr) ((ptr)->i_sb->s_blocksize \
90 - sizeof(struct ocfs2_xattr_block) \
91 - sizeof(struct ocfs2_xattr_header) \
Tiger Yang4442f512009-02-20 11:11:50 +080092 - OCFS2_XATTR_HEADER_GAP)
Tiger Yangcf1d6c72008-08-18 17:11:00 +080093
94static struct ocfs2_xattr_def_value_root def_xv = {
95 .xv.xr_list.l_count = cpu_to_le16(1),
96};
97
98struct xattr_handler *ocfs2_xattr_handlers[] = {
99 &ocfs2_xattr_user_handler,
Tiger Yang929fb012008-11-14 11:17:04 +0800100#ifdef CONFIG_OCFS2_FS_POSIX_ACL
101 &ocfs2_xattr_acl_access_handler,
102 &ocfs2_xattr_acl_default_handler,
103#endif
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800104 &ocfs2_xattr_trusted_handler,
Tiger Yang923f7f32008-11-14 11:16:27 +0800105 &ocfs2_xattr_security_handler,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800106 NULL
107};
108
Tiger Yangc988fd02008-10-23 16:34:44 +0800109static struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800110 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
Tiger Yang929fb012008-11-14 11:17:04 +0800111#ifdef CONFIG_OCFS2_FS_POSIX_ACL
112 [OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS]
113 = &ocfs2_xattr_acl_access_handler,
114 [OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT]
115 = &ocfs2_xattr_acl_default_handler,
116#endif
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800117 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
Tiger Yang923f7f32008-11-14 11:16:27 +0800118 [OCFS2_XATTR_INDEX_SECURITY] = &ocfs2_xattr_security_handler,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800119};
120
121struct ocfs2_xattr_info {
122 int name_index;
123 const char *name;
124 const void *value;
125 size_t value_len;
126};
127
128struct ocfs2_xattr_search {
129 struct buffer_head *inode_bh;
130 /*
131 * xattr_bh point to the block buffer head which has extended attribute
132 * when extended attribute in inode, xattr_bh is equal to inode_bh.
133 */
134 struct buffer_head *xattr_bh;
135 struct ocfs2_xattr_header *header;
Joel Beckerba937122008-10-24 19:13:20 -0700136 struct ocfs2_xattr_bucket *bucket;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800137 void *base;
138 void *end;
139 struct ocfs2_xattr_entry *here;
140 int not_found;
141};
142
Tao Mafd68a892009-08-18 11:43:21 +0800143static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
Tao Ma589dc262008-08-18 17:38:51 +0800144 struct ocfs2_xattr_header *xh,
145 int index,
146 int *block_off,
147 int *new_offset);
148
Joel Becker54f443f2008-10-20 18:43:07 -0700149static int ocfs2_xattr_block_find(struct inode *inode,
150 int name_index,
151 const char *name,
152 struct ocfs2_xattr_search *xs);
Tao Ma589dc262008-08-18 17:38:51 +0800153static int ocfs2_xattr_index_block_find(struct inode *inode,
154 struct buffer_head *root_bh,
155 int name_index,
156 const char *name,
157 struct ocfs2_xattr_search *xs);
158
Tao Ma0c044f02008-08-18 17:38:50 +0800159static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
Tao Ma47bca492009-08-18 11:43:42 +0800160 struct buffer_head *blk_bh,
Tao Ma0c044f02008-08-18 17:38:50 +0800161 char *buffer,
162 size_t buffer_size);
163
Tao Ma01225592008-08-18 17:38:53 +0800164static int ocfs2_xattr_create_index_block(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +0800165 struct ocfs2_xattr_search *xs,
166 struct ocfs2_xattr_set_ctxt *ctxt);
Tao Ma01225592008-08-18 17:38:53 +0800167
168static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
169 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +0800170 struct ocfs2_xattr_search *xs,
171 struct ocfs2_xattr_set_ctxt *ctxt);
Tao Ma01225592008-08-18 17:38:53 +0800172
Tao Ma47bca492009-08-18 11:43:42 +0800173typedef int (xattr_tree_rec_func)(struct inode *inode,
174 struct buffer_head *root_bh,
175 u64 blkno, u32 cpos, u32 len, void *para);
176static int ocfs2_iterate_xattr_index_block(struct inode *inode,
177 struct buffer_head *root_bh,
178 xattr_tree_rec_func *rec_func,
179 void *para);
180static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
181 struct ocfs2_xattr_bucket *bucket,
182 void *para);
183static int ocfs2_rm_xattr_cluster(struct inode *inode,
184 struct buffer_head *root_bh,
185 u64 blkno,
186 u32 cpos,
187 u32 len,
188 void *para);
189
Joel Beckerc58b6032008-11-26 13:36:24 -0800190static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
191 u64 src_blk, u64 last_blk, u64 to_blk,
192 unsigned int start_bucket,
193 u32 *first_hash);
Tao Ma492a8a32009-08-18 11:43:17 +0800194static int ocfs2_prepare_refcount_xattr(struct inode *inode,
195 struct ocfs2_dinode *di,
196 struct ocfs2_xattr_info *xi,
197 struct ocfs2_xattr_search *xis,
198 struct ocfs2_xattr_search *xbs,
199 struct ocfs2_refcount_tree **ref_tree,
200 int *meta_need,
201 int *credits);
Tao Mace9c5a52009-08-18 11:43:59 +0800202static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
203 struct ocfs2_xattr_bucket *bucket,
204 int offset,
205 struct ocfs2_xattr_value_root **xv,
206 struct buffer_head **bh);
Tao Maa3944252008-08-18 17:38:54 +0800207
Tiger Yang0030e002008-10-23 16:33:33 +0800208static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
209{
210 return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
211}
212
213static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
214{
215 return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
216}
217
218static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb)
219{
220 u16 len = sb->s_blocksize -
221 offsetof(struct ocfs2_xattr_header, xh_entries);
222
223 return len / sizeof(struct ocfs2_xattr_entry);
224}
225
Joel Becker9c7759a2008-10-24 16:21:03 -0700226#define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
Joel Becker51def392008-10-24 16:57:21 -0700227#define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
Joel Becker3e632942008-10-24 17:04:49 -0700228#define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
Joel Becker9c7759a2008-10-24 16:21:03 -0700229
Joel Beckerba937122008-10-24 19:13:20 -0700230static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
Joel Becker6dde41d2008-10-24 17:16:48 -0700231{
Joel Beckerba937122008-10-24 19:13:20 -0700232 struct ocfs2_xattr_bucket *bucket;
233 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Joel Becker6dde41d2008-10-24 17:16:48 -0700234
Joel Beckerba937122008-10-24 19:13:20 -0700235 BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
236
237 bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
238 if (bucket) {
239 bucket->bu_inode = inode;
240 bucket->bu_blocks = blks;
241 }
242
243 return bucket;
244}
245
246static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
247{
248 int i;
249
250 for (i = 0; i < bucket->bu_blocks; i++) {
Joel Becker6dde41d2008-10-24 17:16:48 -0700251 brelse(bucket->bu_bhs[i]);
252 bucket->bu_bhs[i] = NULL;
253 }
254}
255
Joel Beckerba937122008-10-24 19:13:20 -0700256static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
257{
258 if (bucket) {
259 ocfs2_xattr_bucket_relse(bucket);
260 bucket->bu_inode = NULL;
261 kfree(bucket);
262 }
263}
264
Joel Becker784b8162008-10-24 17:33:40 -0700265/*
266 * A bucket that has never been written to disk doesn't need to be
267 * read. We just need the buffer_heads. Don't call this for
268 * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
269 * them fully.
270 */
Joel Beckerba937122008-10-24 19:13:20 -0700271static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
Joel Becker784b8162008-10-24 17:33:40 -0700272 u64 xb_blkno)
273{
274 int i, rc = 0;
Joel Becker784b8162008-10-24 17:33:40 -0700275
Joel Beckerba937122008-10-24 19:13:20 -0700276 for (i = 0; i < bucket->bu_blocks; i++) {
277 bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
278 xb_blkno + i);
Joel Becker784b8162008-10-24 17:33:40 -0700279 if (!bucket->bu_bhs[i]) {
280 rc = -EIO;
281 mlog_errno(rc);
282 break;
283 }
284
Joel Becker8cb471e2009-02-10 20:00:41 -0800285 if (!ocfs2_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
Tao Ma757055a2008-11-06 08:10:48 +0800286 bucket->bu_bhs[i]))
Joel Becker8cb471e2009-02-10 20:00:41 -0800287 ocfs2_set_new_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
Tao Ma757055a2008-11-06 08:10:48 +0800288 bucket->bu_bhs[i]);
Joel Becker784b8162008-10-24 17:33:40 -0700289 }
290
291 if (rc)
Joel Beckerba937122008-10-24 19:13:20 -0700292 ocfs2_xattr_bucket_relse(bucket);
Joel Becker784b8162008-10-24 17:33:40 -0700293 return rc;
294}
295
296/* Read the xattr bucket at xb_blkno */
Joel Beckerba937122008-10-24 19:13:20 -0700297static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
Joel Becker784b8162008-10-24 17:33:40 -0700298 u64 xb_blkno)
299{
Joel Beckerba937122008-10-24 19:13:20 -0700300 int rc;
Joel Becker784b8162008-10-24 17:33:40 -0700301
Joel Becker8cb471e2009-02-10 20:00:41 -0800302 rc = ocfs2_read_blocks(INODE_CACHE(bucket->bu_inode), xb_blkno,
Joel Becker970e4932008-11-13 14:49:19 -0800303 bucket->bu_blocks, bucket->bu_bhs, 0,
304 NULL);
Joel Becker4d0e2142008-12-05 11:19:37 -0800305 if (!rc) {
Tao Mac8b9cf92009-02-24 17:40:26 -0800306 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
Joel Becker4d0e2142008-12-05 11:19:37 -0800307 rc = ocfs2_validate_meta_ecc_bhs(bucket->bu_inode->i_sb,
308 bucket->bu_bhs,
309 bucket->bu_blocks,
310 &bucket_xh(bucket)->xh_check);
Tao Mac8b9cf92009-02-24 17:40:26 -0800311 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
Joel Becker4d0e2142008-12-05 11:19:37 -0800312 if (rc)
313 mlog_errno(rc);
314 }
315
Joel Becker784b8162008-10-24 17:33:40 -0700316 if (rc)
Joel Beckerba937122008-10-24 19:13:20 -0700317 ocfs2_xattr_bucket_relse(bucket);
Joel Becker784b8162008-10-24 17:33:40 -0700318 return rc;
319}
320
Joel Becker1224be02008-10-24 18:47:33 -0700321static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
Joel Becker1224be02008-10-24 18:47:33 -0700322 struct ocfs2_xattr_bucket *bucket,
323 int type)
324{
325 int i, rc = 0;
Joel Becker1224be02008-10-24 18:47:33 -0700326
Joel Beckerba937122008-10-24 19:13:20 -0700327 for (i = 0; i < bucket->bu_blocks; i++) {
Joel Becker0cf2f762009-02-12 16:41:25 -0800328 rc = ocfs2_journal_access(handle,
329 INODE_CACHE(bucket->bu_inode),
Joel Becker1224be02008-10-24 18:47:33 -0700330 bucket->bu_bhs[i], type);
331 if (rc) {
332 mlog_errno(rc);
333 break;
334 }
335 }
336
337 return rc;
338}
339
340static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
Joel Becker1224be02008-10-24 18:47:33 -0700341 struct ocfs2_xattr_bucket *bucket)
342{
Joel Beckerba937122008-10-24 19:13:20 -0700343 int i;
Joel Becker1224be02008-10-24 18:47:33 -0700344
Tao Mac8b9cf92009-02-24 17:40:26 -0800345 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
Joel Becker4d0e2142008-12-05 11:19:37 -0800346 ocfs2_compute_meta_ecc_bhs(bucket->bu_inode->i_sb,
347 bucket->bu_bhs, bucket->bu_blocks,
348 &bucket_xh(bucket)->xh_check);
Tao Mac8b9cf92009-02-24 17:40:26 -0800349 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
Joel Becker4d0e2142008-12-05 11:19:37 -0800350
Joel Beckerba937122008-10-24 19:13:20 -0700351 for (i = 0; i < bucket->bu_blocks; i++)
Joel Becker1224be02008-10-24 18:47:33 -0700352 ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
353}
354
Joel Beckerba937122008-10-24 19:13:20 -0700355static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
Joel Becker4980c6d2008-10-24 18:54:43 -0700356 struct ocfs2_xattr_bucket *src)
357{
358 int i;
Joel Beckerba937122008-10-24 19:13:20 -0700359 int blocksize = src->bu_inode->i_sb->s_blocksize;
Joel Becker4980c6d2008-10-24 18:54:43 -0700360
Joel Beckerba937122008-10-24 19:13:20 -0700361 BUG_ON(dest->bu_blocks != src->bu_blocks);
362 BUG_ON(dest->bu_inode != src->bu_inode);
363
364 for (i = 0; i < src->bu_blocks; i++) {
Joel Becker4980c6d2008-10-24 18:54:43 -0700365 memcpy(bucket_block(dest, i), bucket_block(src, i),
366 blocksize);
367 }
368}
Joel Becker1224be02008-10-24 18:47:33 -0700369
Joel Becker4ae1d692008-11-13 14:49:18 -0800370static int ocfs2_validate_xattr_block(struct super_block *sb,
371 struct buffer_head *bh)
372{
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700373 int rc;
Joel Becker4ae1d692008-11-13 14:49:18 -0800374 struct ocfs2_xattr_block *xb =
375 (struct ocfs2_xattr_block *)bh->b_data;
376
377 mlog(0, "Validating xattr block %llu\n",
378 (unsigned long long)bh->b_blocknr);
379
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700380 BUG_ON(!buffer_uptodate(bh));
381
382 /*
383 * If the ecc fails, we return the error but otherwise
384 * leave the filesystem running. We know any error is
385 * local to this block.
386 */
387 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &xb->xb_check);
388 if (rc)
389 return rc;
390
391 /*
392 * Errors after here are fatal
393 */
394
Joel Becker4ae1d692008-11-13 14:49:18 -0800395 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
396 ocfs2_error(sb,
397 "Extended attribute block #%llu has bad "
398 "signature %.*s",
399 (unsigned long long)bh->b_blocknr, 7,
400 xb->xb_signature);
401 return -EINVAL;
402 }
403
404 if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) {
405 ocfs2_error(sb,
406 "Extended attribute block #%llu has an "
407 "invalid xb_blkno of %llu",
408 (unsigned long long)bh->b_blocknr,
409 (unsigned long long)le64_to_cpu(xb->xb_blkno));
410 return -EINVAL;
411 }
412
413 if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) {
414 ocfs2_error(sb,
415 "Extended attribute block #%llu has an invalid "
416 "xb_fs_generation of #%u",
417 (unsigned long long)bh->b_blocknr,
418 le32_to_cpu(xb->xb_fs_generation));
419 return -EINVAL;
420 }
421
422 return 0;
423}
424
425static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno,
426 struct buffer_head **bh)
427{
428 int rc;
429 struct buffer_head *tmp = *bh;
430
Joel Becker8cb471e2009-02-10 20:00:41 -0800431 rc = ocfs2_read_block(INODE_CACHE(inode), xb_blkno, &tmp,
Joel Becker970e4932008-11-13 14:49:19 -0800432 ocfs2_validate_xattr_block);
Joel Becker4ae1d692008-11-13 14:49:18 -0800433
434 /* If ocfs2_read_block() got us a new bh, pass it up. */
435 if (!rc && !*bh)
436 *bh = tmp;
437
438 return rc;
439}
440
Tao Ma936b8832008-10-09 23:06:14 +0800441static inline const char *ocfs2_xattr_prefix(int name_index)
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800442{
443 struct xattr_handler *handler = NULL;
444
445 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
446 handler = ocfs2_xattr_handler_map[name_index];
447
Tao Ma936b8832008-10-09 23:06:14 +0800448 return handler ? handler->prefix : NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800449}
450
Mark Fasheh40daa162008-10-07 14:31:42 -0700451static u32 ocfs2_xattr_name_hash(struct inode *inode,
Tao Ma2057e5c2008-10-09 23:06:13 +0800452 const char *name,
Mark Fasheh40daa162008-10-07 14:31:42 -0700453 int name_len)
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800454{
455 /* Get hash value of uuid from super block */
456 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
457 int i;
458
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800459 /* hash extended attribute name */
460 for (i = 0; i < name_len; i++) {
461 hash = (hash << OCFS2_HASH_SHIFT) ^
462 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
463 *name++;
464 }
465
466 return hash;
467}
468
469/*
470 * ocfs2_xattr_hash_entry()
471 *
472 * Compute the hash of an extended attribute.
473 */
474static void ocfs2_xattr_hash_entry(struct inode *inode,
475 struct ocfs2_xattr_header *header,
476 struct ocfs2_xattr_entry *entry)
477{
478 u32 hash = 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800479 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800480
Tao Ma2057e5c2008-10-09 23:06:13 +0800481 hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800482 entry->xe_name_hash = cpu_to_le32(hash);
483
484 return;
485}
Tao Maf56654c2008-08-18 17:38:48 +0800486
Tiger Yang534eadd2008-11-14 11:16:41 +0800487static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
488{
489 int size = 0;
490
491 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
492 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
493 else
494 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
495 size += sizeof(struct ocfs2_xattr_entry);
496
497 return size;
498}
499
500int ocfs2_calc_security_init(struct inode *dir,
501 struct ocfs2_security_xattr_info *si,
502 int *want_clusters,
503 int *xattr_credits,
504 struct ocfs2_alloc_context **xattr_ac)
505{
506 int ret = 0;
507 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
508 int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
509 si->value_len);
510
511 /*
512 * The max space of security xattr taken inline is
513 * 256(name) + 80(value) + 16(entry) = 352 bytes,
514 * So reserve one metadata block for it is ok.
515 */
516 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
517 s_size > OCFS2_XATTR_FREE_IN_IBODY) {
518 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
519 if (ret) {
520 mlog_errno(ret);
521 return ret;
522 }
523 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
524 }
525
526 /* reserve clusters for xattr value which will be set in B tree*/
Tiger Yang0e445b62008-12-09 16:42:51 +0800527 if (si->value_len > OCFS2_XATTR_INLINE_SIZE) {
528 int new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
529 si->value_len);
530
531 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
532 new_clusters);
533 *want_clusters += new_clusters;
534 }
Tiger Yang534eadd2008-11-14 11:16:41 +0800535 return ret;
536}
537
Tiger Yang89c38bd2008-11-14 11:17:41 +0800538int ocfs2_calc_xattr_init(struct inode *dir,
539 struct buffer_head *dir_bh,
540 int mode,
541 struct ocfs2_security_xattr_info *si,
542 int *want_clusters,
543 int *xattr_credits,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800544 int *want_meta)
Tiger Yang89c38bd2008-11-14 11:17:41 +0800545{
546 int ret = 0;
547 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
Tiger Yang0e445b62008-12-09 16:42:51 +0800548 int s_size = 0, a_size = 0, acl_len = 0, new_clusters;
Tiger Yang89c38bd2008-11-14 11:17:41 +0800549
550 if (si->enable)
551 s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
552 si->value_len);
553
554 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
555 acl_len = ocfs2_xattr_get_nolock(dir, dir_bh,
556 OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
557 "", NULL, 0);
558 if (acl_len > 0) {
559 a_size = ocfs2_xattr_entry_real_size(0, acl_len);
560 if (S_ISDIR(mode))
561 a_size <<= 1;
562 } else if (acl_len != 0 && acl_len != -ENODATA) {
563 mlog_errno(ret);
564 return ret;
565 }
566 }
567
568 if (!(s_size + a_size))
569 return ret;
570
571 /*
572 * The max space of security xattr taken inline is
573 * 256(name) + 80(value) + 16(entry) = 352 bytes,
574 * The max space of acl xattr taken inline is
575 * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
576 * when blocksize = 512, may reserve one more cluser for
577 * xattr bucket, otherwise reserve one metadata block
578 * for them is ok.
Tiger Yang6c9fd1d2009-03-06 10:19:30 +0800579 * If this is a new directory with inline data,
580 * we choose to reserve the entire inline area for
581 * directory contents and force an external xattr block.
Tiger Yang89c38bd2008-11-14 11:17:41 +0800582 */
583 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
Tiger Yang6c9fd1d2009-03-06 10:19:30 +0800584 (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) ||
Tiger Yang89c38bd2008-11-14 11:17:41 +0800585 (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) {
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800586 *want_meta = *want_meta + 1;
Tiger Yang89c38bd2008-11-14 11:17:41 +0800587 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
588 }
589
590 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
591 (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
592 *want_clusters += 1;
593 *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
594 }
595
Tiger Yang0e445b62008-12-09 16:42:51 +0800596 /*
597 * reserve credits and clusters for xattrs which has large value
598 * and have to be set outside
599 */
600 if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE) {
601 new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
602 si->value_len);
603 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
604 new_clusters);
605 *want_clusters += new_clusters;
606 }
Tiger Yang89c38bd2008-11-14 11:17:41 +0800607 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
608 acl_len > OCFS2_XATTR_INLINE_SIZE) {
Tiger Yang0e445b62008-12-09 16:42:51 +0800609 /* for directory, it has DEFAULT and ACCESS two types of acls */
610 new_clusters = (S_ISDIR(mode) ? 2 : 1) *
611 ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
612 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
613 new_clusters);
614 *want_clusters += new_clusters;
Tiger Yang89c38bd2008-11-14 11:17:41 +0800615 }
616
617 return ret;
618}
619
Tao Maf56654c2008-08-18 17:38:48 +0800620static int ocfs2_xattr_extend_allocation(struct inode *inode,
621 u32 clusters_to_add,
Joel Becker19b801f2008-12-09 14:36:50 -0800622 struct ocfs2_xattr_value_buf *vb,
Tao Ma78f30c32008-11-12 08:27:00 +0800623 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800624{
625 int status = 0;
Tao Ma85db90e2008-11-12 08:27:01 +0800626 handle_t *handle = ctxt->handle;
Tao Maf56654c2008-08-18 17:38:48 +0800627 enum ocfs2_alloc_restarted why;
Joel Becker19b801f2008-12-09 14:36:50 -0800628 u32 prev_clusters, logical_start = le32_to_cpu(vb->vb_xv->xr_clusters);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700629 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +0800630
631 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
632
Joel Becker5e404e92009-02-13 03:54:22 -0800633 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700634
Joel Becker0cf2f762009-02-12 16:41:25 -0800635 status = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
Joel Becker2a50a742008-12-09 14:24:33 -0800636 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Maf56654c2008-08-18 17:38:48 +0800637 if (status < 0) {
638 mlog_errno(status);
639 goto leave;
640 }
641
Joel Becker19b801f2008-12-09 14:36:50 -0800642 prev_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
Joel Beckercbee7e12009-02-13 03:34:15 -0800643 status = ocfs2_add_clusters_in_btree(handle,
644 &et,
Tao Maf56654c2008-08-18 17:38:48 +0800645 &logical_start,
646 clusters_to_add,
647 0,
Tao Ma78f30c32008-11-12 08:27:00 +0800648 ctxt->data_ac,
649 ctxt->meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700650 &why);
Tao Ma85db90e2008-11-12 08:27:01 +0800651 if (status < 0) {
652 mlog_errno(status);
Tao Maf56654c2008-08-18 17:38:48 +0800653 goto leave;
654 }
655
Joel Becker19b801f2008-12-09 14:36:50 -0800656 status = ocfs2_journal_dirty(handle, vb->vb_bh);
Tao Maf56654c2008-08-18 17:38:48 +0800657 if (status < 0) {
658 mlog_errno(status);
659 goto leave;
660 }
661
Joel Becker19b801f2008-12-09 14:36:50 -0800662 clusters_to_add -= le32_to_cpu(vb->vb_xv->xr_clusters) - prev_clusters;
Tao Maf56654c2008-08-18 17:38:48 +0800663
Tao Ma85db90e2008-11-12 08:27:01 +0800664 /*
665 * We should have already allocated enough space before the transaction,
666 * so no need to restart.
667 */
668 BUG_ON(why != RESTART_NONE || clusters_to_add);
Tao Maf56654c2008-08-18 17:38:48 +0800669
670leave:
Tao Maf56654c2008-08-18 17:38:48 +0800671
672 return status;
673}
674
675static int __ocfs2_remove_xattr_range(struct inode *inode,
Joel Beckerd72cc722008-12-09 14:30:41 -0800676 struct ocfs2_xattr_value_buf *vb,
Tao Maf56654c2008-08-18 17:38:48 +0800677 u32 cpos, u32 phys_cpos, u32 len,
Tao Ma492a8a32009-08-18 11:43:17 +0800678 unsigned int ext_flags,
Tao Ma78f30c32008-11-12 08:27:00 +0800679 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800680{
681 int ret;
682 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
Tao Ma85db90e2008-11-12 08:27:01 +0800683 handle_t *handle = ctxt->handle;
Joel Beckerf99b9b72008-08-20 19:36:33 -0700684 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +0800685
Joel Becker5e404e92009-02-13 03:54:22 -0800686 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700687
Joel Becker0cf2f762009-02-12 16:41:25 -0800688 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
Joel Beckerd72cc722008-12-09 14:30:41 -0800689 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Maf56654c2008-08-18 17:38:48 +0800690 if (ret) {
691 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800692 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800693 }
694
Joel Beckerdbdcf6a2009-02-13 03:41:26 -0800695 ret = ocfs2_remove_extent(handle, &et, cpos, len, ctxt->meta_ac,
Tao Ma78f30c32008-11-12 08:27:00 +0800696 &ctxt->dealloc);
Tao Maf56654c2008-08-18 17:38:48 +0800697 if (ret) {
698 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800699 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800700 }
701
Joel Beckerd72cc722008-12-09 14:30:41 -0800702 le32_add_cpu(&vb->vb_xv->xr_clusters, -len);
Tao Maf56654c2008-08-18 17:38:48 +0800703
Joel Beckerd72cc722008-12-09 14:30:41 -0800704 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
Tao Maf56654c2008-08-18 17:38:48 +0800705 if (ret) {
706 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800707 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800708 }
709
Tao Ma492a8a32009-08-18 11:43:17 +0800710 if (ext_flags & OCFS2_EXT_REFCOUNTED)
711 ret = ocfs2_decrease_refcount(inode, handle,
712 ocfs2_blocks_to_clusters(inode->i_sb,
713 phys_blkno),
714 len, ctxt->meta_ac, &ctxt->dealloc, 1);
715 else
716 ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc,
717 phys_blkno, len);
Tao Maf56654c2008-08-18 17:38:48 +0800718 if (ret)
719 mlog_errno(ret);
720
Tao Maf56654c2008-08-18 17:38:48 +0800721out:
Tao Maf56654c2008-08-18 17:38:48 +0800722 return ret;
723}
724
725static int ocfs2_xattr_shrink_size(struct inode *inode,
726 u32 old_clusters,
727 u32 new_clusters,
Joel Becker19b801f2008-12-09 14:36:50 -0800728 struct ocfs2_xattr_value_buf *vb,
Tao Ma78f30c32008-11-12 08:27:00 +0800729 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800730{
731 int ret = 0;
Tao Ma492a8a32009-08-18 11:43:17 +0800732 unsigned int ext_flags;
Tao Maf56654c2008-08-18 17:38:48 +0800733 u32 trunc_len, cpos, phys_cpos, alloc_size;
734 u64 block;
Tao Maf56654c2008-08-18 17:38:48 +0800735
736 if (old_clusters <= new_clusters)
737 return 0;
738
739 cpos = new_clusters;
740 trunc_len = old_clusters - new_clusters;
741 while (trunc_len) {
742 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
Joel Beckerd72cc722008-12-09 14:30:41 -0800743 &alloc_size,
Tao Ma492a8a32009-08-18 11:43:17 +0800744 &vb->vb_xv->xr_list, &ext_flags);
Tao Maf56654c2008-08-18 17:38:48 +0800745 if (ret) {
746 mlog_errno(ret);
747 goto out;
748 }
749
750 if (alloc_size > trunc_len)
751 alloc_size = trunc_len;
752
Joel Becker19b801f2008-12-09 14:36:50 -0800753 ret = __ocfs2_remove_xattr_range(inode, vb, cpos,
Tao Maf56654c2008-08-18 17:38:48 +0800754 phys_cpos, alloc_size,
Tao Ma492a8a32009-08-18 11:43:17 +0800755 ext_flags, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800756 if (ret) {
757 mlog_errno(ret);
758 goto out;
759 }
760
761 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
Joel Becker8cb471e2009-02-10 20:00:41 -0800762 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode),
763 block, alloc_size);
Tao Maf56654c2008-08-18 17:38:48 +0800764 cpos += alloc_size;
765 trunc_len -= alloc_size;
766 }
767
768out:
Tao Maf56654c2008-08-18 17:38:48 +0800769 return ret;
770}
771
772static int ocfs2_xattr_value_truncate(struct inode *inode,
Joel Beckerb3e5d372008-12-09 15:01:04 -0800773 struct ocfs2_xattr_value_buf *vb,
Tao Ma78f30c32008-11-12 08:27:00 +0800774 int len,
775 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800776{
777 int ret;
778 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
Joel Beckerb3e5d372008-12-09 15:01:04 -0800779 u32 old_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
Tao Maf56654c2008-08-18 17:38:48 +0800780
781 if (new_clusters == old_clusters)
782 return 0;
783
784 if (new_clusters > old_clusters)
785 ret = ocfs2_xattr_extend_allocation(inode,
786 new_clusters - old_clusters,
Joel Beckerb3e5d372008-12-09 15:01:04 -0800787 vb, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800788 else
789 ret = ocfs2_xattr_shrink_size(inode,
790 old_clusters, new_clusters,
Joel Beckerb3e5d372008-12-09 15:01:04 -0800791 vb, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800792
793 return ret;
794}
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800795
Tao Ma936b8832008-10-09 23:06:14 +0800796static int ocfs2_xattr_list_entry(char *buffer, size_t size,
797 size_t *result, const char *prefix,
798 const char *name, int name_len)
799{
800 char *p = buffer + *result;
801 int prefix_len = strlen(prefix);
802 int total_len = prefix_len + name_len + 1;
803
804 *result += total_len;
805
806 /* we are just looking for how big our buffer needs to be */
807 if (!size)
808 return 0;
809
810 if (*result > size)
811 return -ERANGE;
812
813 memcpy(p, prefix, prefix_len);
814 memcpy(p + prefix_len, name, name_len);
815 p[prefix_len + name_len] = '\0';
816
817 return 0;
818}
819
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800820static int ocfs2_xattr_list_entries(struct inode *inode,
821 struct ocfs2_xattr_header *header,
822 char *buffer, size_t buffer_size)
823{
Tao Ma936b8832008-10-09 23:06:14 +0800824 size_t result = 0;
825 int i, type, ret;
826 const char *prefix, *name;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800827
828 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
829 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
Tao Ma936b8832008-10-09 23:06:14 +0800830 type = ocfs2_xattr_get_type(entry);
831 prefix = ocfs2_xattr_prefix(type);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800832
Tao Ma936b8832008-10-09 23:06:14 +0800833 if (prefix) {
834 name = (const char *)header +
835 le16_to_cpu(entry->xe_name_offset);
836
837 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
838 &result, prefix, name,
839 entry->xe_name_len);
840 if (ret)
841 return ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800842 }
843 }
844
Tao Ma936b8832008-10-09 23:06:14 +0800845 return result;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800846}
847
Tao Ma8b2c0db2009-08-18 11:43:49 +0800848int ocfs2_has_inline_xattr_value_outside(struct inode *inode,
849 struct ocfs2_dinode *di)
850{
851 struct ocfs2_xattr_header *xh;
852 int i;
853
854 xh = (struct ocfs2_xattr_header *)
855 ((void *)di + inode->i_sb->s_blocksize -
856 le16_to_cpu(di->i_xattr_inline_size));
857
858 for (i = 0; i < le16_to_cpu(xh->xh_count); i++)
859 if (!ocfs2_xattr_is_local(&xh->xh_entries[i]))
860 return 1;
861
862 return 0;
863}
864
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800865static int ocfs2_xattr_ibody_list(struct inode *inode,
866 struct ocfs2_dinode *di,
867 char *buffer,
868 size_t buffer_size)
869{
870 struct ocfs2_xattr_header *header = NULL;
871 struct ocfs2_inode_info *oi = OCFS2_I(inode);
872 int ret = 0;
873
874 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
875 return ret;
876
877 header = (struct ocfs2_xattr_header *)
878 ((void *)di + inode->i_sb->s_blocksize -
879 le16_to_cpu(di->i_xattr_inline_size));
880
881 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
882
883 return ret;
884}
885
886static int ocfs2_xattr_block_list(struct inode *inode,
887 struct ocfs2_dinode *di,
888 char *buffer,
889 size_t buffer_size)
890{
891 struct buffer_head *blk_bh = NULL;
Tao Ma0c044f02008-08-18 17:38:50 +0800892 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800893 int ret = 0;
894
895 if (!di->i_xattr_loc)
896 return ret;
897
Joel Becker4ae1d692008-11-13 14:49:18 -0800898 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
899 &blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800900 if (ret < 0) {
901 mlog_errno(ret);
902 return ret;
903 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800904
Tao Ma0c044f02008-08-18 17:38:50 +0800905 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Ma0c044f02008-08-18 17:38:50 +0800906 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
907 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
908 ret = ocfs2_xattr_list_entries(inode, header,
909 buffer, buffer_size);
Tao Ma47bca492009-08-18 11:43:42 +0800910 } else
911 ret = ocfs2_xattr_tree_list_index_block(inode, blk_bh,
Tao Ma0c044f02008-08-18 17:38:50 +0800912 buffer, buffer_size);
Joel Becker4ae1d692008-11-13 14:49:18 -0800913
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800914 brelse(blk_bh);
915
916 return ret;
917}
918
919ssize_t ocfs2_listxattr(struct dentry *dentry,
920 char *buffer,
921 size_t size)
922{
923 int ret = 0, i_ret = 0, b_ret = 0;
924 struct buffer_head *di_bh = NULL;
925 struct ocfs2_dinode *di = NULL;
926 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
927
Tiger Yang8154da32008-08-18 17:11:46 +0800928 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
929 return -EOPNOTSUPP;
930
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800931 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
932 return ret;
933
934 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
935 if (ret < 0) {
936 mlog_errno(ret);
937 return ret;
938 }
939
940 di = (struct ocfs2_dinode *)di_bh->b_data;
941
942 down_read(&oi->ip_xattr_sem);
943 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
944 if (i_ret < 0)
945 b_ret = 0;
946 else {
947 if (buffer) {
948 buffer += i_ret;
949 size -= i_ret;
950 }
951 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
952 buffer, size);
953 if (b_ret < 0)
954 i_ret = 0;
955 }
956 up_read(&oi->ip_xattr_sem);
957 ocfs2_inode_unlock(dentry->d_inode, 0);
958
959 brelse(di_bh);
960
961 return i_ret + b_ret;
962}
963
964static int ocfs2_xattr_find_entry(int name_index,
965 const char *name,
966 struct ocfs2_xattr_search *xs)
967{
968 struct ocfs2_xattr_entry *entry;
969 size_t name_len;
970 int i, cmp = 1;
971
972 if (name == NULL)
973 return -EINVAL;
974
975 name_len = strlen(name);
976 entry = xs->here;
977 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
978 cmp = name_index - ocfs2_xattr_get_type(entry);
979 if (!cmp)
980 cmp = name_len - entry->xe_name_len;
981 if (!cmp)
982 cmp = memcmp(name, (xs->base +
983 le16_to_cpu(entry->xe_name_offset)),
984 name_len);
985 if (cmp == 0)
986 break;
987 entry += 1;
988 }
989 xs->here = entry;
990
991 return cmp ? -ENODATA : 0;
992}
993
994static int ocfs2_xattr_get_value_outside(struct inode *inode,
Tao Ma589dc262008-08-18 17:38:51 +0800995 struct ocfs2_xattr_value_root *xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800996 void *buffer,
997 size_t len)
998{
999 u32 cpos, p_cluster, num_clusters, bpc, clusters;
1000 u64 blkno;
1001 int i, ret = 0;
1002 size_t cplen, blocksize;
1003 struct buffer_head *bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001004 struct ocfs2_extent_list *el;
1005
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001006 el = &xv->xr_list;
1007 clusters = le32_to_cpu(xv->xr_clusters);
1008 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1009 blocksize = inode->i_sb->s_blocksize;
1010
1011 cpos = 0;
1012 while (cpos < clusters) {
1013 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
Tao Ma1061f9c2009-08-18 11:41:57 +08001014 &num_clusters, el, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001015 if (ret) {
1016 mlog_errno(ret);
1017 goto out;
1018 }
1019
1020 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1021 /* Copy ocfs2_xattr_value */
1022 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
Joel Becker8cb471e2009-02-10 20:00:41 -08001023 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1024 &bh, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001025 if (ret) {
1026 mlog_errno(ret);
1027 goto out;
1028 }
1029
1030 cplen = len >= blocksize ? blocksize : len;
1031 memcpy(buffer, bh->b_data, cplen);
1032 len -= cplen;
1033 buffer += cplen;
1034
1035 brelse(bh);
1036 bh = NULL;
1037 if (len == 0)
1038 break;
1039 }
1040 cpos += num_clusters;
1041 }
1042out:
1043 return ret;
1044}
1045
1046static int ocfs2_xattr_ibody_get(struct inode *inode,
1047 int name_index,
1048 const char *name,
1049 void *buffer,
1050 size_t buffer_size,
1051 struct ocfs2_xattr_search *xs)
1052{
1053 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1054 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
Tao Ma589dc262008-08-18 17:38:51 +08001055 struct ocfs2_xattr_value_root *xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001056 size_t size;
1057 int ret = 0;
1058
1059 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
1060 return -ENODATA;
1061
1062 xs->end = (void *)di + inode->i_sb->s_blocksize;
1063 xs->header = (struct ocfs2_xattr_header *)
1064 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1065 xs->base = (void *)xs->header;
1066 xs->here = xs->header->xh_entries;
1067
1068 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1069 if (ret)
1070 return ret;
1071 size = le64_to_cpu(xs->here->xe_value_size);
1072 if (buffer) {
1073 if (size > buffer_size)
1074 return -ERANGE;
1075 if (ocfs2_xattr_is_local(xs->here)) {
1076 memcpy(buffer, (void *)xs->base +
1077 le16_to_cpu(xs->here->xe_name_offset) +
1078 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
1079 } else {
Tao Ma589dc262008-08-18 17:38:51 +08001080 xv = (struct ocfs2_xattr_value_root *)
1081 (xs->base + le16_to_cpu(
1082 xs->here->xe_name_offset) +
1083 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
1084 ret = ocfs2_xattr_get_value_outside(inode, xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001085 buffer, size);
1086 if (ret < 0) {
1087 mlog_errno(ret);
1088 return ret;
1089 }
1090 }
1091 }
1092
1093 return size;
1094}
1095
1096static int ocfs2_xattr_block_get(struct inode *inode,
1097 int name_index,
1098 const char *name,
1099 void *buffer,
1100 size_t buffer_size,
1101 struct ocfs2_xattr_search *xs)
1102{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001103 struct ocfs2_xattr_block *xb;
Tao Ma589dc262008-08-18 17:38:51 +08001104 struct ocfs2_xattr_value_root *xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001105 size_t size;
Subrata Modak44d8e4e2009-07-14 01:19:31 +05301106 int ret = -ENODATA, name_offset, name_len, i;
1107 int uninitialized_var(block_off);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001108
Joel Beckerba937122008-10-24 19:13:20 -07001109 xs->bucket = ocfs2_xattr_bucket_new(inode);
1110 if (!xs->bucket) {
1111 ret = -ENOMEM;
1112 mlog_errno(ret);
1113 goto cleanup;
1114 }
Tao Ma589dc262008-08-18 17:38:51 +08001115
Joel Becker54f443f2008-10-20 18:43:07 -07001116 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
1117 if (ret) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001118 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001119 goto cleanup;
1120 }
1121
Tiger Yang6c1e1832008-11-02 19:04:21 +08001122 if (xs->not_found) {
1123 ret = -ENODATA;
1124 goto cleanup;
1125 }
1126
Joel Becker54f443f2008-10-20 18:43:07 -07001127 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001128 size = le64_to_cpu(xs->here->xe_value_size);
1129 if (buffer) {
1130 ret = -ERANGE;
1131 if (size > buffer_size)
1132 goto cleanup;
Tao Ma589dc262008-08-18 17:38:51 +08001133
1134 name_offset = le16_to_cpu(xs->here->xe_name_offset);
1135 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
1136 i = xs->here - xs->header->xh_entries;
1137
1138 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
Tao Mafd68a892009-08-18 11:43:21 +08001139 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Joel Beckerba937122008-10-24 19:13:20 -07001140 bucket_xh(xs->bucket),
Tao Ma589dc262008-08-18 17:38:51 +08001141 i,
1142 &block_off,
1143 &name_offset);
Joel Beckerba937122008-10-24 19:13:20 -07001144 xs->base = bucket_block(xs->bucket, block_off);
Tao Ma589dc262008-08-18 17:38:51 +08001145 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001146 if (ocfs2_xattr_is_local(xs->here)) {
1147 memcpy(buffer, (void *)xs->base +
Tao Ma589dc262008-08-18 17:38:51 +08001148 name_offset + name_len, size);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001149 } else {
Tao Ma589dc262008-08-18 17:38:51 +08001150 xv = (struct ocfs2_xattr_value_root *)
1151 (xs->base + name_offset + name_len);
1152 ret = ocfs2_xattr_get_value_outside(inode, xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001153 buffer, size);
1154 if (ret < 0) {
1155 mlog_errno(ret);
1156 goto cleanup;
1157 }
1158 }
1159 }
1160 ret = size;
1161cleanup:
Joel Beckerba937122008-10-24 19:13:20 -07001162 ocfs2_xattr_bucket_free(xs->bucket);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001163
Joel Becker54f443f2008-10-20 18:43:07 -07001164 brelse(xs->xattr_bh);
1165 xs->xattr_bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001166 return ret;
1167}
1168
Tiger Yang4e3e9d02008-11-14 11:16:53 +08001169int ocfs2_xattr_get_nolock(struct inode *inode,
1170 struct buffer_head *di_bh,
Tiger Yang0030e002008-10-23 16:33:33 +08001171 int name_index,
1172 const char *name,
1173 void *buffer,
1174 size_t buffer_size)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001175{
1176 int ret;
1177 struct ocfs2_dinode *di = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001178 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1179 struct ocfs2_xattr_search xis = {
1180 .not_found = -ENODATA,
1181 };
1182 struct ocfs2_xattr_search xbs = {
1183 .not_found = -ENODATA,
1184 };
1185
Tiger Yang8154da32008-08-18 17:11:46 +08001186 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1187 return -EOPNOTSUPP;
1188
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001189 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1190 ret = -ENODATA;
1191
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001192 xis.inode_bh = xbs.inode_bh = di_bh;
1193 di = (struct ocfs2_dinode *)di_bh->b_data;
1194
1195 down_read(&oi->ip_xattr_sem);
1196 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1197 buffer_size, &xis);
Tiger Yang6c1e1832008-11-02 19:04:21 +08001198 if (ret == -ENODATA && di->i_xattr_loc)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001199 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1200 buffer_size, &xbs);
1201 up_read(&oi->ip_xattr_sem);
Tiger Yang4e3e9d02008-11-14 11:16:53 +08001202
1203 return ret;
1204}
1205
1206/* ocfs2_xattr_get()
1207 *
1208 * Copy an extended attribute into the buffer provided.
1209 * Buffer is NULL to compute the size of buffer required.
1210 */
1211static int ocfs2_xattr_get(struct inode *inode,
1212 int name_index,
1213 const char *name,
1214 void *buffer,
1215 size_t buffer_size)
1216{
1217 int ret;
1218 struct buffer_head *di_bh = NULL;
1219
1220 ret = ocfs2_inode_lock(inode, &di_bh, 0);
1221 if (ret < 0) {
1222 mlog_errno(ret);
1223 return ret;
1224 }
1225 ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
1226 name, buffer, buffer_size);
1227
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001228 ocfs2_inode_unlock(inode, 0);
1229
1230 brelse(di_bh);
1231
1232 return ret;
1233}
1234
1235static int __ocfs2_xattr_set_value_outside(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001236 handle_t *handle,
Tao Ma492a8a32009-08-18 11:43:17 +08001237 struct ocfs2_xattr_value_buf *vb,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001238 const void *value,
1239 int value_len)
1240{
Tao Ma71d548a2008-12-05 06:20:54 +08001241 int ret = 0, i, cp_len;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001242 u16 blocksize = inode->i_sb->s_blocksize;
1243 u32 p_cluster, num_clusters;
1244 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1245 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1246 u64 blkno;
1247 struct buffer_head *bh = NULL;
Tao Ma492a8a32009-08-18 11:43:17 +08001248 unsigned int ext_flags;
1249 struct ocfs2_xattr_value_root *xv = vb->vb_xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001250
1251 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1252
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001253 while (cpos < clusters) {
1254 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
Tao Ma1061f9c2009-08-18 11:41:57 +08001255 &num_clusters, &xv->xr_list,
Tao Ma492a8a32009-08-18 11:43:17 +08001256 &ext_flags);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001257 if (ret) {
1258 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001259 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001260 }
1261
Tao Ma492a8a32009-08-18 11:43:17 +08001262 BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED);
1263
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001264 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1265
1266 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
Joel Becker8cb471e2009-02-10 20:00:41 -08001267 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1268 &bh, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001269 if (ret) {
1270 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001271 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001272 }
1273
1274 ret = ocfs2_journal_access(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -08001275 INODE_CACHE(inode),
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001276 bh,
1277 OCFS2_JOURNAL_ACCESS_WRITE);
1278 if (ret < 0) {
1279 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001280 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001281 }
1282
1283 cp_len = value_len > blocksize ? blocksize : value_len;
1284 memcpy(bh->b_data, value, cp_len);
1285 value_len -= cp_len;
1286 value += cp_len;
1287 if (cp_len < blocksize)
1288 memset(bh->b_data + cp_len, 0,
1289 blocksize - cp_len);
1290
1291 ret = ocfs2_journal_dirty(handle, bh);
1292 if (ret < 0) {
1293 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001294 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001295 }
1296 brelse(bh);
1297 bh = NULL;
1298
1299 /*
1300 * XXX: do we need to empty all the following
1301 * blocks in this cluster?
1302 */
1303 if (!value_len)
1304 break;
1305 }
1306 cpos += num_clusters;
1307 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001308out:
1309 brelse(bh);
1310
1311 return ret;
1312}
1313
1314static int ocfs2_xattr_cleanup(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001315 handle_t *handle,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001316 struct ocfs2_xattr_info *xi,
1317 struct ocfs2_xattr_search *xs,
Joel Becker512620f2008-12-09 15:58:35 -08001318 struct ocfs2_xattr_value_buf *vb,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001319 size_t offs)
1320{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001321 int ret = 0;
1322 size_t name_len = strlen(xi->name);
1323 void *val = xs->base + offs;
1324 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1325
Joel Becker0cf2f762009-02-12 16:41:25 -08001326 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
Joel Becker512620f2008-12-09 15:58:35 -08001327 OCFS2_JOURNAL_ACCESS_WRITE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001328 if (ret) {
1329 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001330 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001331 }
1332 /* Decrease xattr count */
1333 le16_add_cpu(&xs->header->xh_count, -1);
1334 /* Remove the xattr entry and tree root which has already be set*/
1335 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
1336 memset(val, 0, size);
1337
Joel Becker512620f2008-12-09 15:58:35 -08001338 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001339 if (ret < 0)
1340 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001341out:
1342 return ret;
1343}
1344
1345static int ocfs2_xattr_update_entry(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001346 handle_t *handle,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001347 struct ocfs2_xattr_info *xi,
1348 struct ocfs2_xattr_search *xs,
Joel Becker0c748e92008-12-09 15:46:15 -08001349 struct ocfs2_xattr_value_buf *vb,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001350 size_t offs)
1351{
Tao Ma85db90e2008-11-12 08:27:01 +08001352 int ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001353
Joel Becker0cf2f762009-02-12 16:41:25 -08001354 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
Joel Becker0c748e92008-12-09 15:46:15 -08001355 OCFS2_JOURNAL_ACCESS_WRITE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001356 if (ret) {
1357 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001358 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001359 }
1360
1361 xs->here->xe_name_offset = cpu_to_le16(offs);
1362 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1363 if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
1364 ocfs2_xattr_set_local(xs->here, 1);
1365 else
1366 ocfs2_xattr_set_local(xs->here, 0);
1367 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1368
Joel Becker0c748e92008-12-09 15:46:15 -08001369 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001370 if (ret < 0)
1371 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001372out:
1373 return ret;
1374}
1375
1376/*
1377 * ocfs2_xattr_set_value_outside()
1378 *
1379 * Set large size value in B tree.
1380 */
1381static int ocfs2_xattr_set_value_outside(struct inode *inode,
1382 struct ocfs2_xattr_info *xi,
1383 struct ocfs2_xattr_search *xs,
Tao Ma78f30c32008-11-12 08:27:00 +08001384 struct ocfs2_xattr_set_ctxt *ctxt,
Joel Becker512620f2008-12-09 15:58:35 -08001385 struct ocfs2_xattr_value_buf *vb,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001386 size_t offs)
1387{
1388 size_t name_len = strlen(xi->name);
1389 void *val = xs->base + offs;
1390 struct ocfs2_xattr_value_root *xv = NULL;
1391 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1392 int ret = 0;
1393
1394 memset(val, 0, size);
1395 memcpy(val, xi->name, name_len);
1396 xv = (struct ocfs2_xattr_value_root *)
1397 (val + OCFS2_XATTR_SIZE(name_len));
1398 xv->xr_clusters = 0;
1399 xv->xr_last_eb_blk = 0;
1400 xv->xr_list.l_tree_depth = 0;
1401 xv->xr_list.l_count = cpu_to_le16(1);
1402 xv->xr_list.l_next_free_rec = 0;
Joel Becker512620f2008-12-09 15:58:35 -08001403 vb->vb_xv = xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001404
Joel Becker512620f2008-12-09 15:58:35 -08001405 ret = ocfs2_xattr_value_truncate(inode, vb, xi->value_len, ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001406 if (ret < 0) {
1407 mlog_errno(ret);
1408 return ret;
1409 }
Joel Becker512620f2008-12-09 15:58:35 -08001410 ret = ocfs2_xattr_update_entry(inode, ctxt->handle, xi, xs, vb, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001411 if (ret < 0) {
1412 mlog_errno(ret);
1413 return ret;
1414 }
Tao Ma492a8a32009-08-18 11:43:17 +08001415 ret = __ocfs2_xattr_set_value_outside(inode, ctxt->handle, vb,
Tao Ma85db90e2008-11-12 08:27:01 +08001416 xi->value, xi->value_len);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001417 if (ret < 0)
1418 mlog_errno(ret);
1419
1420 return ret;
1421}
1422
1423/*
1424 * ocfs2_xattr_set_entry_local()
1425 *
1426 * Set, replace or remove extended attribute in local.
1427 */
1428static void ocfs2_xattr_set_entry_local(struct inode *inode,
1429 struct ocfs2_xattr_info *xi,
1430 struct ocfs2_xattr_search *xs,
1431 struct ocfs2_xattr_entry *last,
1432 size_t min_offs)
1433{
1434 size_t name_len = strlen(xi->name);
1435 int i;
1436
1437 if (xi->value && xs->not_found) {
1438 /* Insert the new xattr entry. */
1439 le16_add_cpu(&xs->header->xh_count, 1);
1440 ocfs2_xattr_set_type(last, xi->name_index);
1441 ocfs2_xattr_set_local(last, 1);
1442 last->xe_name_len = name_len;
1443 } else {
1444 void *first_val;
1445 void *val;
1446 size_t offs, size;
1447
1448 first_val = xs->base + min_offs;
1449 offs = le16_to_cpu(xs->here->xe_name_offset);
1450 val = xs->base + offs;
1451
1452 if (le64_to_cpu(xs->here->xe_value_size) >
1453 OCFS2_XATTR_INLINE_SIZE)
1454 size = OCFS2_XATTR_SIZE(name_len) +
1455 OCFS2_XATTR_ROOT_SIZE;
1456 else
1457 size = OCFS2_XATTR_SIZE(name_len) +
1458 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1459
1460 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1461 OCFS2_XATTR_SIZE(xi->value_len)) {
1462 /* The old and the new value have the
1463 same size. Just replace the value. */
1464 ocfs2_xattr_set_local(xs->here, 1);
1465 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1466 /* Clear value bytes. */
1467 memset(val + OCFS2_XATTR_SIZE(name_len),
1468 0,
1469 OCFS2_XATTR_SIZE(xi->value_len));
1470 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1471 xi->value,
1472 xi->value_len);
1473 return;
1474 }
1475 /* Remove the old name+value. */
1476 memmove(first_val + size, first_val, val - first_val);
1477 memset(first_val, 0, size);
1478 xs->here->xe_name_hash = 0;
1479 xs->here->xe_name_offset = 0;
1480 ocfs2_xattr_set_local(xs->here, 1);
1481 xs->here->xe_value_size = 0;
1482
1483 min_offs += size;
1484
1485 /* Adjust all value offsets. */
1486 last = xs->header->xh_entries;
1487 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1488 size_t o = le16_to_cpu(last->xe_name_offset);
1489
1490 if (o < offs)
1491 last->xe_name_offset = cpu_to_le16(o + size);
1492 last += 1;
1493 }
1494
1495 if (!xi->value) {
1496 /* Remove the old entry. */
1497 last -= 1;
1498 memmove(xs->here, xs->here + 1,
1499 (void *)last - (void *)xs->here);
1500 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1501 le16_add_cpu(&xs->header->xh_count, -1);
1502 }
1503 }
1504 if (xi->value) {
1505 /* Insert the new name+value. */
1506 size_t size = OCFS2_XATTR_SIZE(name_len) +
1507 OCFS2_XATTR_SIZE(xi->value_len);
1508 void *val = xs->base + min_offs - size;
1509
1510 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1511 memset(val, 0, size);
1512 memcpy(val, xi->name, name_len);
1513 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1514 xi->value,
1515 xi->value_len);
1516 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1517 ocfs2_xattr_set_local(xs->here, 1);
1518 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1519 }
1520
1521 return;
1522}
1523
1524/*
1525 * ocfs2_xattr_set_entry()
1526 *
1527 * Set extended attribute entry into inode or block.
1528 *
1529 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1530 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1531 * then set value in B tree with set_value_outside().
1532 */
1533static int ocfs2_xattr_set_entry(struct inode *inode,
1534 struct ocfs2_xattr_info *xi,
1535 struct ocfs2_xattr_search *xs,
Tao Ma78f30c32008-11-12 08:27:00 +08001536 struct ocfs2_xattr_set_ctxt *ctxt,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001537 int flag)
1538{
1539 struct ocfs2_xattr_entry *last;
1540 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1541 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1542 size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1543 size_t size_l = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08001544 handle_t *handle = ctxt->handle;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001545 int free, i, ret;
1546 struct ocfs2_xattr_info xi_l = {
1547 .name_index = xi->name_index,
1548 .name = xi->name,
1549 .value = xi->value,
1550 .value_len = xi->value_len,
1551 };
Joel Becker512620f2008-12-09 15:58:35 -08001552 struct ocfs2_xattr_value_buf vb = {
1553 .vb_bh = xs->xattr_bh,
1554 .vb_access = ocfs2_journal_access_di,
1555 };
1556
1557 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1558 BUG_ON(xs->xattr_bh == xs->inode_bh);
1559 vb.vb_access = ocfs2_journal_access_xb;
1560 } else
1561 BUG_ON(xs->xattr_bh != xs->inode_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001562
1563 /* Compute min_offs, last and free space. */
1564 last = xs->header->xh_entries;
1565
1566 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1567 size_t offs = le16_to_cpu(last->xe_name_offset);
1568 if (offs < min_offs)
1569 min_offs = offs;
1570 last += 1;
1571 }
1572
Tiger Yang4442f512009-02-20 11:11:50 +08001573 free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001574 if (free < 0)
Joel Beckerb37c4d82008-10-20 18:24:03 -07001575 return -EIO;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001576
1577 if (!xs->not_found) {
1578 size_t size = 0;
1579 if (ocfs2_xattr_is_local(xs->here))
1580 size = OCFS2_XATTR_SIZE(name_len) +
1581 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1582 else
1583 size = OCFS2_XATTR_SIZE(name_len) +
1584 OCFS2_XATTR_ROOT_SIZE;
1585 free += (size + sizeof(struct ocfs2_xattr_entry));
1586 }
1587 /* Check free space in inode or block */
1588 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1589 if (free < sizeof(struct ocfs2_xattr_entry) +
1590 OCFS2_XATTR_SIZE(name_len) +
1591 OCFS2_XATTR_ROOT_SIZE) {
1592 ret = -ENOSPC;
1593 goto out;
1594 }
1595 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1596 xi_l.value = (void *)&def_xv;
1597 xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
1598 } else if (xi->value) {
1599 if (free < sizeof(struct ocfs2_xattr_entry) +
1600 OCFS2_XATTR_SIZE(name_len) +
1601 OCFS2_XATTR_SIZE(xi->value_len)) {
1602 ret = -ENOSPC;
1603 goto out;
1604 }
1605 }
1606
1607 if (!xs->not_found) {
1608 /* For existing extended attribute */
1609 size_t size = OCFS2_XATTR_SIZE(name_len) +
1610 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1611 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1612 void *val = xs->base + offs;
1613
1614 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1615 /* Replace existing local xattr with tree root */
1616 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
Joel Becker512620f2008-12-09 15:58:35 -08001617 ctxt, &vb, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001618 if (ret < 0)
1619 mlog_errno(ret);
1620 goto out;
1621 } else if (!ocfs2_xattr_is_local(xs->here)) {
1622 /* For existing xattr which has value outside */
Joel Becker512620f2008-12-09 15:58:35 -08001623 vb.vb_xv = (struct ocfs2_xattr_value_root *)
1624 (val + OCFS2_XATTR_SIZE(name_len));
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001625
1626 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1627 /*
1628 * If new value need set outside also,
1629 * first truncate old value to new value,
1630 * then set new value with set_value_outside().
1631 */
1632 ret = ocfs2_xattr_value_truncate(inode,
Joel Beckerb3e5d372008-12-09 15:01:04 -08001633 &vb,
Tao Ma78f30c32008-11-12 08:27:00 +08001634 xi->value_len,
1635 ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001636 if (ret < 0) {
1637 mlog_errno(ret);
1638 goto out;
1639 }
1640
Tao Ma85db90e2008-11-12 08:27:01 +08001641 ret = ocfs2_xattr_update_entry(inode,
1642 handle,
1643 xi,
1644 xs,
Joel Becker0c748e92008-12-09 15:46:15 -08001645 &vb,
Tao Ma85db90e2008-11-12 08:27:01 +08001646 offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001647 if (ret < 0) {
1648 mlog_errno(ret);
1649 goto out;
1650 }
1651
Tao Ma85db90e2008-11-12 08:27:01 +08001652 ret = __ocfs2_xattr_set_value_outside(inode,
1653 handle,
Tao Ma492a8a32009-08-18 11:43:17 +08001654 &vb,
Tao Ma85db90e2008-11-12 08:27:01 +08001655 xi->value,
1656 xi->value_len);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001657 if (ret < 0)
1658 mlog_errno(ret);
1659 goto out;
1660 } else {
1661 /*
1662 * If new value need set in local,
1663 * just trucate old value to zero.
1664 */
1665 ret = ocfs2_xattr_value_truncate(inode,
Joel Beckerb3e5d372008-12-09 15:01:04 -08001666 &vb,
Tao Ma85db90e2008-11-12 08:27:01 +08001667 0,
1668 ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001669 if (ret < 0)
1670 mlog_errno(ret);
1671 }
1672 }
1673 }
1674
Joel Becker0cf2f762009-02-12 16:41:25 -08001675 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), xs->inode_bh,
Joel Becker512620f2008-12-09 15:58:35 -08001676 OCFS2_JOURNAL_ACCESS_WRITE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001677 if (ret) {
1678 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001679 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001680 }
1681
1682 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
Joel Becker0cf2f762009-02-12 16:41:25 -08001683 ret = vb.vb_access(handle, INODE_CACHE(inode), vb.vb_bh,
Joel Becker512620f2008-12-09 15:58:35 -08001684 OCFS2_JOURNAL_ACCESS_WRITE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001685 if (ret) {
1686 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001687 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001688 }
1689 }
1690
1691 /*
1692 * Set value in local, include set tree root in local.
1693 * This is the first step for value size >INLINE_SIZE.
1694 */
1695 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1696
1697 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1698 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1699 if (ret < 0) {
1700 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001701 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001702 }
1703 }
1704
1705 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1706 (flag & OCFS2_INLINE_XATTR_FL)) {
1707 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1708 unsigned int xattrsize = osb->s_xattr_inline_size;
1709
1710 /*
1711 * Adjust extent record count or inline data size
1712 * to reserve space for extended attribute.
1713 */
1714 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1715 struct ocfs2_inline_data *idata = &di->id2.i_data;
1716 le16_add_cpu(&idata->id_count, -xattrsize);
1717 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1718 struct ocfs2_extent_list *el = &di->id2.i_list;
1719 le16_add_cpu(&el->l_count, -(xattrsize /
1720 sizeof(struct ocfs2_extent_rec)));
1721 }
1722 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1723 }
1724 /* Update xattr flag */
1725 spin_lock(&oi->ip_lock);
1726 oi->ip_dyn_features |= flag;
1727 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1728 spin_unlock(&oi->ip_lock);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001729
1730 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1731 if (ret < 0)
1732 mlog_errno(ret);
1733
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001734 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1735 /*
1736 * Set value outside in B tree.
1737 * This is the second step for value size > INLINE_SIZE.
1738 */
1739 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
Joel Becker512620f2008-12-09 15:58:35 -08001740 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, ctxt,
1741 &vb, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001742 if (ret < 0) {
1743 int ret2;
1744
1745 mlog_errno(ret);
1746 /*
1747 * If set value outside failed, we have to clean
1748 * the junk tree root we have already set in local.
1749 */
Tao Ma85db90e2008-11-12 08:27:01 +08001750 ret2 = ocfs2_xattr_cleanup(inode, ctxt->handle,
Joel Becker512620f2008-12-09 15:58:35 -08001751 xi, xs, &vb, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001752 if (ret2 < 0)
1753 mlog_errno(ret2);
1754 }
1755 }
1756out:
1757 return ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001758}
1759
Tao Mace9c5a52009-08-18 11:43:59 +08001760/*
1761 * In xattr remove, if it is stored outside and refcounted, we may have
1762 * the chance to split the refcount tree. So need the allocators.
1763 */
1764static int ocfs2_lock_xattr_remove_allocators(struct inode *inode,
1765 struct ocfs2_xattr_value_root *xv,
1766 struct ocfs2_caching_info *ref_ci,
1767 struct buffer_head *ref_root_bh,
1768 struct ocfs2_alloc_context **meta_ac,
1769 int *ref_credits)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001770{
Tao Mace9c5a52009-08-18 11:43:59 +08001771 int ret, meta_add = 0;
1772 u32 p_cluster, num_clusters;
1773 unsigned int ext_flags;
Tao Ma78f30c32008-11-12 08:27:00 +08001774
Tao Mace9c5a52009-08-18 11:43:59 +08001775 *ref_credits = 0;
1776 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
1777 &num_clusters,
1778 &xv->xr_list,
1779 &ext_flags);
1780 if (ret) {
Tao Ma85db90e2008-11-12 08:27:01 +08001781 mlog_errno(ret);
1782 goto out;
1783 }
1784
Tao Mace9c5a52009-08-18 11:43:59 +08001785 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
1786 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001787
Tao Mace9c5a52009-08-18 11:43:59 +08001788 ret = ocfs2_refcounted_xattr_delete_need(inode, ref_ci,
1789 ref_root_bh, xv,
1790 &meta_add, ref_credits);
1791 if (ret) {
1792 mlog_errno(ret);
1793 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001794 }
1795
Tao Mace9c5a52009-08-18 11:43:59 +08001796 ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
1797 meta_add, meta_ac);
1798 if (ret)
1799 mlog_errno(ret);
1800
Tao Ma85db90e2008-11-12 08:27:01 +08001801out:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001802 return ret;
1803}
1804
Tao Mace9c5a52009-08-18 11:43:59 +08001805static int ocfs2_remove_value_outside(struct inode*inode,
1806 struct ocfs2_xattr_value_buf *vb,
1807 struct ocfs2_xattr_header *header,
1808 struct ocfs2_caching_info *ref_ci,
1809 struct buffer_head *ref_root_bh)
1810{
1811 int ret = 0, i, ref_credits;
1812 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1813 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
1814 void *val;
1815
1816 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
1817
1818 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1819 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1820
1821 if (ocfs2_xattr_is_local(entry))
1822 continue;
1823
1824 val = (void *)header +
1825 le16_to_cpu(entry->xe_name_offset);
1826 vb->vb_xv = (struct ocfs2_xattr_value_root *)
1827 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
1828
1829 ret = ocfs2_lock_xattr_remove_allocators(inode, vb->vb_xv,
1830 ref_ci, ref_root_bh,
1831 &ctxt.meta_ac,
1832 &ref_credits);
1833
1834 ctxt.handle = ocfs2_start_trans(osb, ref_credits +
1835 ocfs2_remove_extent_credits(osb->sb));
1836 if (IS_ERR(ctxt.handle)) {
1837 ret = PTR_ERR(ctxt.handle);
1838 mlog_errno(ret);
1839 break;
1840 }
1841
1842 ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt);
1843 if (ret < 0) {
1844 mlog_errno(ret);
1845 break;
1846 }
1847
1848 ocfs2_commit_trans(osb, ctxt.handle);
1849 if (ctxt.meta_ac) {
1850 ocfs2_free_alloc_context(ctxt.meta_ac);
1851 ctxt.meta_ac = NULL;
1852 }
1853 }
1854
1855 if (ctxt.meta_ac)
1856 ocfs2_free_alloc_context(ctxt.meta_ac);
1857 ocfs2_schedule_truncate_log_flush(osb, 1);
1858 ocfs2_run_deallocs(osb, &ctxt.dealloc);
1859 return ret;
1860}
1861
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001862static int ocfs2_xattr_ibody_remove(struct inode *inode,
Tao Mace9c5a52009-08-18 11:43:59 +08001863 struct buffer_head *di_bh,
1864 struct ocfs2_caching_info *ref_ci,
1865 struct buffer_head *ref_root_bh)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001866{
1867
1868 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1869 struct ocfs2_xattr_header *header;
1870 int ret;
Joel Becker43119012008-12-09 16:24:43 -08001871 struct ocfs2_xattr_value_buf vb = {
1872 .vb_bh = di_bh,
1873 .vb_access = ocfs2_journal_access_di,
1874 };
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001875
1876 header = (struct ocfs2_xattr_header *)
1877 ((void *)di + inode->i_sb->s_blocksize -
1878 le16_to_cpu(di->i_xattr_inline_size));
1879
Tao Mace9c5a52009-08-18 11:43:59 +08001880 ret = ocfs2_remove_value_outside(inode, &vb, header,
1881 ref_ci, ref_root_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001882
1883 return ret;
1884}
1885
Tao Mace9c5a52009-08-18 11:43:59 +08001886struct ocfs2_rm_xattr_bucket_para {
1887 struct ocfs2_caching_info *ref_ci;
1888 struct buffer_head *ref_root_bh;
1889};
1890
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001891static int ocfs2_xattr_block_remove(struct inode *inode,
Tao Mace9c5a52009-08-18 11:43:59 +08001892 struct buffer_head *blk_bh,
1893 struct ocfs2_caching_info *ref_ci,
1894 struct buffer_head *ref_root_bh)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001895{
1896 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001897 int ret = 0;
Joel Becker43119012008-12-09 16:24:43 -08001898 struct ocfs2_xattr_value_buf vb = {
1899 .vb_bh = blk_bh,
1900 .vb_access = ocfs2_journal_access_xb,
1901 };
Tao Mace9c5a52009-08-18 11:43:59 +08001902 struct ocfs2_rm_xattr_bucket_para args = {
1903 .ref_ci = ref_ci,
1904 .ref_root_bh = ref_root_bh,
1905 };
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001906
1907 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Maa3944252008-08-18 17:38:54 +08001908 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1909 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
Tao Mace9c5a52009-08-18 11:43:59 +08001910 ret = ocfs2_remove_value_outside(inode, &vb, header,
1911 ref_ci, ref_root_bh);
Tao Maa3944252008-08-18 17:38:54 +08001912 } else
Tao Ma47bca492009-08-18 11:43:42 +08001913 ret = ocfs2_iterate_xattr_index_block(inode,
1914 blk_bh,
1915 ocfs2_rm_xattr_cluster,
Tao Mace9c5a52009-08-18 11:43:59 +08001916 &args);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001917
1918 return ret;
1919}
1920
Tao Ma08413892008-08-29 09:00:19 +08001921static int ocfs2_xattr_free_block(struct inode *inode,
Tao Mace9c5a52009-08-18 11:43:59 +08001922 u64 block,
1923 struct ocfs2_caching_info *ref_ci,
1924 struct buffer_head *ref_root_bh)
Tao Ma08413892008-08-29 09:00:19 +08001925{
1926 struct inode *xb_alloc_inode;
1927 struct buffer_head *xb_alloc_bh = NULL;
1928 struct buffer_head *blk_bh = NULL;
1929 struct ocfs2_xattr_block *xb;
1930 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1931 handle_t *handle;
1932 int ret = 0;
1933 u64 blk, bg_blkno;
1934 u16 bit;
1935
Joel Becker4ae1d692008-11-13 14:49:18 -08001936 ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
Tao Ma08413892008-08-29 09:00:19 +08001937 if (ret < 0) {
1938 mlog_errno(ret);
1939 goto out;
1940 }
1941
Tao Mace9c5a52009-08-18 11:43:59 +08001942 ret = ocfs2_xattr_block_remove(inode, blk_bh, ref_ci, ref_root_bh);
Tao Ma08413892008-08-29 09:00:19 +08001943 if (ret < 0) {
1944 mlog_errno(ret);
1945 goto out;
1946 }
1947
Joel Becker4ae1d692008-11-13 14:49:18 -08001948 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Ma08413892008-08-29 09:00:19 +08001949 blk = le64_to_cpu(xb->xb_blkno);
1950 bit = le16_to_cpu(xb->xb_suballoc_bit);
1951 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1952
1953 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1954 EXTENT_ALLOC_SYSTEM_INODE,
1955 le16_to_cpu(xb->xb_suballoc_slot));
1956 if (!xb_alloc_inode) {
1957 ret = -ENOMEM;
1958 mlog_errno(ret);
1959 goto out;
1960 }
1961 mutex_lock(&xb_alloc_inode->i_mutex);
1962
1963 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1964 if (ret < 0) {
1965 mlog_errno(ret);
1966 goto out_mutex;
1967 }
1968
1969 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
1970 if (IS_ERR(handle)) {
1971 ret = PTR_ERR(handle);
1972 mlog_errno(ret);
1973 goto out_unlock;
1974 }
1975
1976 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1977 bit, bg_blkno, 1);
1978 if (ret < 0)
1979 mlog_errno(ret);
1980
1981 ocfs2_commit_trans(osb, handle);
1982out_unlock:
1983 ocfs2_inode_unlock(xb_alloc_inode, 1);
1984 brelse(xb_alloc_bh);
1985out_mutex:
1986 mutex_unlock(&xb_alloc_inode->i_mutex);
1987 iput(xb_alloc_inode);
1988out:
1989 brelse(blk_bh);
1990 return ret;
1991}
1992
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001993/*
1994 * ocfs2_xattr_remove()
1995 *
1996 * Free extended attribute resources associated with this inode.
1997 */
1998int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
1999{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002000 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2001 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
Tao Mace9c5a52009-08-18 11:43:59 +08002002 struct ocfs2_refcount_tree *ref_tree = NULL;
2003 struct buffer_head *ref_root_bh = NULL;
2004 struct ocfs2_caching_info *ref_ci = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002005 handle_t *handle;
2006 int ret;
2007
Tiger Yang8154da32008-08-18 17:11:46 +08002008 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2009 return 0;
2010
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002011 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
2012 return 0;
2013
Tao Mace9c5a52009-08-18 11:43:59 +08002014 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) {
2015 ret = ocfs2_lock_refcount_tree(OCFS2_SB(inode->i_sb),
2016 le64_to_cpu(di->i_refcount_loc),
2017 1, &ref_tree, &ref_root_bh);
2018 if (ret) {
2019 mlog_errno(ret);
2020 goto out;
2021 }
2022 ref_ci = &ref_tree->rf_ci;
2023
2024 }
2025
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002026 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
Tao Mace9c5a52009-08-18 11:43:59 +08002027 ret = ocfs2_xattr_ibody_remove(inode, di_bh,
2028 ref_ci, ref_root_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002029 if (ret < 0) {
2030 mlog_errno(ret);
2031 goto out;
2032 }
2033 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002034
Tao Ma08413892008-08-29 09:00:19 +08002035 if (di->i_xattr_loc) {
2036 ret = ocfs2_xattr_free_block(inode,
Tao Mace9c5a52009-08-18 11:43:59 +08002037 le64_to_cpu(di->i_xattr_loc),
2038 ref_ci, ref_root_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002039 if (ret < 0) {
2040 mlog_errno(ret);
2041 goto out;
2042 }
2043 }
2044
2045 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
2046 OCFS2_INODE_UPDATE_CREDITS);
2047 if (IS_ERR(handle)) {
2048 ret = PTR_ERR(handle);
2049 mlog_errno(ret);
2050 goto out;
2051 }
Joel Becker0cf2f762009-02-12 16:41:25 -08002052 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker84008972008-12-09 16:11:49 -08002053 OCFS2_JOURNAL_ACCESS_WRITE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002054 if (ret) {
2055 mlog_errno(ret);
2056 goto out_commit;
2057 }
2058
Tao Ma08413892008-08-29 09:00:19 +08002059 di->i_xattr_loc = 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002060
2061 spin_lock(&oi->ip_lock);
2062 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
2063 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2064 spin_unlock(&oi->ip_lock);
2065
2066 ret = ocfs2_journal_dirty(handle, di_bh);
2067 if (ret < 0)
2068 mlog_errno(ret);
2069out_commit:
2070 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
2071out:
Tao Mace9c5a52009-08-18 11:43:59 +08002072 if (ref_tree)
2073 ocfs2_unlock_refcount_tree(OCFS2_SB(inode->i_sb), ref_tree, 1);
2074 brelse(ref_root_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002075 return ret;
2076}
2077
2078static int ocfs2_xattr_has_space_inline(struct inode *inode,
2079 struct ocfs2_dinode *di)
2080{
2081 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2082 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
2083 int free;
2084
2085 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
2086 return 0;
2087
2088 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2089 struct ocfs2_inline_data *idata = &di->id2.i_data;
2090 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
2091 } else if (ocfs2_inode_is_fast_symlink(inode)) {
2092 free = ocfs2_fast_symlink_chars(inode->i_sb) -
2093 le64_to_cpu(di->i_size);
2094 } else {
2095 struct ocfs2_extent_list *el = &di->id2.i_list;
2096 free = (le16_to_cpu(el->l_count) -
2097 le16_to_cpu(el->l_next_free_rec)) *
2098 sizeof(struct ocfs2_extent_rec);
2099 }
2100 if (free >= xattrsize)
2101 return 1;
2102
2103 return 0;
2104}
2105
2106/*
2107 * ocfs2_xattr_ibody_find()
2108 *
2109 * Find extended attribute in inode block and
2110 * fill search info into struct ocfs2_xattr_search.
2111 */
2112static int ocfs2_xattr_ibody_find(struct inode *inode,
2113 int name_index,
2114 const char *name,
2115 struct ocfs2_xattr_search *xs)
2116{
2117 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2118 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2119 int ret;
2120 int has_space = 0;
2121
2122 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2123 return 0;
2124
2125 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2126 down_read(&oi->ip_alloc_sem);
2127 has_space = ocfs2_xattr_has_space_inline(inode, di);
2128 up_read(&oi->ip_alloc_sem);
2129 if (!has_space)
2130 return 0;
2131 }
2132
2133 xs->xattr_bh = xs->inode_bh;
2134 xs->end = (void *)di + inode->i_sb->s_blocksize;
2135 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
2136 xs->header = (struct ocfs2_xattr_header *)
2137 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
2138 else
2139 xs->header = (struct ocfs2_xattr_header *)
2140 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
2141 xs->base = (void *)xs->header;
2142 xs->here = xs->header->xh_entries;
2143
2144 /* Find the named attribute. */
2145 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2146 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2147 if (ret && ret != -ENODATA)
2148 return ret;
2149 xs->not_found = ret;
2150 }
2151
2152 return 0;
2153}
2154
2155/*
2156 * ocfs2_xattr_ibody_set()
2157 *
2158 * Set, replace or remove an extended attribute into inode block.
2159 *
2160 */
2161static int ocfs2_xattr_ibody_set(struct inode *inode,
2162 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08002163 struct ocfs2_xattr_search *xs,
2164 struct ocfs2_xattr_set_ctxt *ctxt)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002165{
2166 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2167 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2168 int ret;
2169
2170 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2171 return -ENOSPC;
2172
2173 down_write(&oi->ip_alloc_sem);
2174 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2175 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2176 ret = -ENOSPC;
2177 goto out;
2178 }
2179 }
2180
Tao Ma78f30c32008-11-12 08:27:00 +08002181 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002182 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
2183out:
2184 up_write(&oi->ip_alloc_sem);
2185
2186 return ret;
2187}
2188
2189/*
2190 * ocfs2_xattr_block_find()
2191 *
2192 * Find extended attribute in external block and
2193 * fill search info into struct ocfs2_xattr_search.
2194 */
2195static int ocfs2_xattr_block_find(struct inode *inode,
2196 int name_index,
2197 const char *name,
2198 struct ocfs2_xattr_search *xs)
2199{
2200 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2201 struct buffer_head *blk_bh = NULL;
Tao Ma589dc262008-08-18 17:38:51 +08002202 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002203 int ret = 0;
2204
2205 if (!di->i_xattr_loc)
2206 return ret;
2207
Joel Becker4ae1d692008-11-13 14:49:18 -08002208 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
2209 &blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002210 if (ret < 0) {
2211 mlog_errno(ret);
2212 return ret;
2213 }
Joel Beckerf6087fb2008-10-20 18:20:43 -07002214
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002215 xs->xattr_bh = blk_bh;
Joel Becker4ae1d692008-11-13 14:49:18 -08002216 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002217
Tao Ma589dc262008-08-18 17:38:51 +08002218 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2219 xs->header = &xb->xb_attrs.xb_header;
2220 xs->base = (void *)xs->header;
2221 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
2222 xs->here = xs->header->xh_entries;
2223
2224 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2225 } else
2226 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
2227 name_index,
2228 name, xs);
2229
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002230 if (ret && ret != -ENODATA) {
2231 xs->xattr_bh = NULL;
2232 goto cleanup;
2233 }
2234 xs->not_found = ret;
2235 return 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002236cleanup:
2237 brelse(blk_bh);
2238
2239 return ret;
2240}
2241
Tao Ma5aea1f02009-08-18 11:43:24 +08002242static int ocfs2_create_xattr_block(handle_t *handle,
2243 struct inode *inode,
2244 struct buffer_head *inode_bh,
2245 struct ocfs2_alloc_context *meta_ac,
Tao Maa7fe7a32009-08-18 11:43:52 +08002246 struct buffer_head **ret_bh,
2247 int indexed)
Tao Ma5aea1f02009-08-18 11:43:24 +08002248{
2249 int ret;
2250 u16 suballoc_bit_start;
2251 u32 num_got;
2252 u64 first_blkno;
2253 struct ocfs2_dinode *di = (struct ocfs2_dinode *)inode_bh->b_data;
2254 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2255 struct buffer_head *new_bh = NULL;
2256 struct ocfs2_xattr_block *xblk;
2257
2258 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), inode_bh,
2259 OCFS2_JOURNAL_ACCESS_CREATE);
2260 if (ret < 0) {
2261 mlog_errno(ret);
2262 goto end;
2263 }
2264
2265 ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
2266 &suballoc_bit_start, &num_got,
2267 &first_blkno);
2268 if (ret < 0) {
2269 mlog_errno(ret);
2270 goto end;
2271 }
2272
2273 new_bh = sb_getblk(inode->i_sb, first_blkno);
2274 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
2275
2276 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode),
2277 new_bh,
2278 OCFS2_JOURNAL_ACCESS_CREATE);
2279 if (ret < 0) {
2280 mlog_errno(ret);
2281 goto end;
2282 }
2283
2284 /* Initialize ocfs2_xattr_block */
2285 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2286 memset(xblk, 0, inode->i_sb->s_blocksize);
2287 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
2288 xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
2289 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
2290 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
2291 xblk->xb_blkno = cpu_to_le64(first_blkno);
2292
Tao Maa7fe7a32009-08-18 11:43:52 +08002293 if (indexed) {
2294 struct ocfs2_xattr_tree_root *xr = &xblk->xb_attrs.xb_root;
2295 xr->xt_clusters = cpu_to_le32(1);
2296 xr->xt_last_eb_blk = 0;
2297 xr->xt_list.l_tree_depth = 0;
2298 xr->xt_list.l_count = cpu_to_le16(
2299 ocfs2_xattr_recs_per_xb(inode->i_sb));
2300 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2301 xblk->xb_flags = cpu_to_le16(OCFS2_XATTR_INDEXED);
2302 }
2303
Tao Ma5aea1f02009-08-18 11:43:24 +08002304 ret = ocfs2_journal_dirty(handle, new_bh);
2305 if (ret < 0) {
2306 mlog_errno(ret);
2307 goto end;
2308 }
2309 di->i_xattr_loc = cpu_to_le64(first_blkno);
2310 ocfs2_journal_dirty(handle, inode_bh);
2311
2312 *ret_bh = new_bh;
2313 new_bh = NULL;
2314
2315end:
2316 brelse(new_bh);
2317 return ret;
2318}
2319
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002320/*
2321 * ocfs2_xattr_block_set()
2322 *
2323 * Set, replace or remove an extended attribute into external block.
2324 *
2325 */
2326static int ocfs2_xattr_block_set(struct inode *inode,
2327 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08002328 struct ocfs2_xattr_search *xs,
2329 struct ocfs2_xattr_set_ctxt *ctxt)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002330{
2331 struct buffer_head *new_bh = NULL;
Tao Ma85db90e2008-11-12 08:27:01 +08002332 handle_t *handle = ctxt->handle;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002333 struct ocfs2_xattr_block *xblk = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002334 int ret;
2335
2336 if (!xs->xattr_bh) {
Tao Ma5aea1f02009-08-18 11:43:24 +08002337 ret = ocfs2_create_xattr_block(handle, inode, xs->inode_bh,
Tao Maa7fe7a32009-08-18 11:43:52 +08002338 ctxt->meta_ac, &new_bh, 0);
Tao Ma5aea1f02009-08-18 11:43:24 +08002339 if (ret) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002340 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08002341 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002342 }
2343
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002344 xs->xattr_bh = new_bh;
Tao Ma5aea1f02009-08-18 11:43:24 +08002345 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002346 xs->header = &xblk->xb_attrs.xb_header;
2347 xs->base = (void *)xs->header;
2348 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2349 xs->here = xs->header->xh_entries;
Tao Ma01225592008-08-18 17:38:53 +08002350 } else
2351 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2352
2353 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
2354 /* Set extended attribute into external block */
Tao Ma78f30c32008-11-12 08:27:00 +08002355 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
2356 OCFS2_HAS_XATTR_FL);
Tao Ma01225592008-08-18 17:38:53 +08002357 if (!ret || ret != -ENOSPC)
2358 goto end;
2359
Tao Ma78f30c32008-11-12 08:27:00 +08002360 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08002361 if (ret)
2362 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002363 }
2364
Tao Ma78f30c32008-11-12 08:27:00 +08002365 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08002366
2367end:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002368
2369 return ret;
2370}
2371
Tao Ma78f30c32008-11-12 08:27:00 +08002372/* Check whether the new xattr can be inserted into the inode. */
2373static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
2374 struct ocfs2_xattr_info *xi,
2375 struct ocfs2_xattr_search *xs)
2376{
2377 u64 value_size;
2378 struct ocfs2_xattr_entry *last;
2379 int free, i;
2380 size_t min_offs = xs->end - xs->base;
2381
2382 if (!xs->header)
2383 return 0;
2384
2385 last = xs->header->xh_entries;
2386
2387 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
2388 size_t offs = le16_to_cpu(last->xe_name_offset);
2389 if (offs < min_offs)
2390 min_offs = offs;
2391 last += 1;
2392 }
2393
Tiger Yang4442f512009-02-20 11:11:50 +08002394 free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
Tao Ma78f30c32008-11-12 08:27:00 +08002395 if (free < 0)
2396 return 0;
2397
2398 BUG_ON(!xs->not_found);
2399
2400 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
2401 value_size = OCFS2_XATTR_ROOT_SIZE;
2402 else
2403 value_size = OCFS2_XATTR_SIZE(xi->value_len);
2404
2405 if (free >= sizeof(struct ocfs2_xattr_entry) +
2406 OCFS2_XATTR_SIZE(strlen(xi->name)) + value_size)
2407 return 1;
2408
2409 return 0;
2410}
2411
2412static int ocfs2_calc_xattr_set_need(struct inode *inode,
2413 struct ocfs2_dinode *di,
2414 struct ocfs2_xattr_info *xi,
2415 struct ocfs2_xattr_search *xis,
2416 struct ocfs2_xattr_search *xbs,
2417 int *clusters_need,
Tao Ma85db90e2008-11-12 08:27:01 +08002418 int *meta_need,
2419 int *credits_need)
Tao Ma78f30c32008-11-12 08:27:00 +08002420{
2421 int ret = 0, old_in_xb = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08002422 int clusters_add = 0, meta_add = 0, credits = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08002423 struct buffer_head *bh = NULL;
2424 struct ocfs2_xattr_block *xb = NULL;
2425 struct ocfs2_xattr_entry *xe = NULL;
2426 struct ocfs2_xattr_value_root *xv = NULL;
2427 char *base = NULL;
2428 int name_offset, name_len = 0;
2429 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2430 xi->value_len);
2431 u64 value_size;
2432
Tao Ma71d548a2008-12-05 06:20:54 +08002433 /*
2434 * Calculate the clusters we need to write.
2435 * No matter whether we replace an old one or add a new one,
2436 * we need this for writing.
2437 */
2438 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
2439 credits += new_clusters *
2440 ocfs2_clusters_to_blocks(inode->i_sb, 1);
2441
Tao Ma78f30c32008-11-12 08:27:00 +08002442 if (xis->not_found && xbs->not_found) {
Tao Ma85db90e2008-11-12 08:27:01 +08002443 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2444
2445 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
Tao Ma78f30c32008-11-12 08:27:00 +08002446 clusters_add += new_clusters;
Tao Ma85db90e2008-11-12 08:27:01 +08002447 credits += ocfs2_calc_extend_credits(inode->i_sb,
2448 &def_xv.xv.xr_list,
2449 new_clusters);
2450 }
Tao Ma78f30c32008-11-12 08:27:00 +08002451
2452 goto meta_guess;
2453 }
2454
2455 if (!xis->not_found) {
2456 xe = xis->here;
2457 name_offset = le16_to_cpu(xe->xe_name_offset);
2458 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2459 base = xis->base;
Tao Ma85db90e2008-11-12 08:27:01 +08002460 credits += OCFS2_INODE_UPDATE_CREDITS;
Tao Ma78f30c32008-11-12 08:27:00 +08002461 } else {
Joel Becker970e4932008-11-13 14:49:19 -08002462 int i, block_off = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08002463 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2464 xe = xbs->here;
2465 name_offset = le16_to_cpu(xe->xe_name_offset);
2466 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2467 i = xbs->here - xbs->header->xh_entries;
2468 old_in_xb = 1;
2469
2470 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
Tao Mafd68a892009-08-18 11:43:21 +08002471 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Tao Ma78f30c32008-11-12 08:27:00 +08002472 bucket_xh(xbs->bucket),
2473 i, &block_off,
2474 &name_offset);
2475 base = bucket_block(xbs->bucket, block_off);
Tao Ma85db90e2008-11-12 08:27:01 +08002476 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2477 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08002478 base = xbs->base;
Tao Ma85db90e2008-11-12 08:27:01 +08002479 credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
2480 }
2481 }
2482
2483 /*
2484 * delete a xattr doesn't need metadata and cluster allocation.
2485 * so just calculate the credits and return.
2486 *
2487 * The credits for removing the value tree will be extended
2488 * by ocfs2_remove_extent itself.
2489 */
2490 if (!xi->value) {
2491 if (!ocfs2_xattr_is_local(xe))
Jan Karaa90714c2008-10-09 19:38:40 +02002492 credits += ocfs2_remove_extent_credits(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08002493
2494 goto out;
Tao Ma78f30c32008-11-12 08:27:00 +08002495 }
2496
2497 /* do cluster allocation guess first. */
2498 value_size = le64_to_cpu(xe->xe_value_size);
2499
2500 if (old_in_xb) {
2501 /*
2502 * In xattr set, we always try to set the xe in inode first,
2503 * so if it can be inserted into inode successfully, the old
2504 * one will be removed from the xattr block, and this xattr
2505 * will be inserted into inode as a new xattr in inode.
2506 */
2507 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
2508 clusters_add += new_clusters;
Jan Karaa90714c2008-10-09 19:38:40 +02002509 credits += ocfs2_remove_extent_credits(inode->i_sb) +
Tao Ma85db90e2008-11-12 08:27:01 +08002510 OCFS2_INODE_UPDATE_CREDITS;
2511 if (!ocfs2_xattr_is_local(xe))
2512 credits += ocfs2_calc_extend_credits(
2513 inode->i_sb,
2514 &def_xv.xv.xr_list,
2515 new_clusters);
Tao Ma78f30c32008-11-12 08:27:00 +08002516 goto out;
2517 }
2518 }
2519
2520 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
2521 /* the new values will be stored outside. */
2522 u32 old_clusters = 0;
2523
2524 if (!ocfs2_xattr_is_local(xe)) {
2525 old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2526 value_size);
2527 xv = (struct ocfs2_xattr_value_root *)
2528 (base + name_offset + name_len);
Tao Ma97aff522008-11-19 16:48:41 +08002529 value_size = OCFS2_XATTR_ROOT_SIZE;
Tao Ma78f30c32008-11-12 08:27:00 +08002530 } else
2531 xv = &def_xv.xv;
2532
Tao Ma85db90e2008-11-12 08:27:01 +08002533 if (old_clusters >= new_clusters) {
Jan Karaa90714c2008-10-09 19:38:40 +02002534 credits += ocfs2_remove_extent_credits(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08002535 goto out;
Tao Ma85db90e2008-11-12 08:27:01 +08002536 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08002537 meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
2538 clusters_add += new_clusters - old_clusters;
Tao Ma85db90e2008-11-12 08:27:01 +08002539 credits += ocfs2_calc_extend_credits(inode->i_sb,
2540 &xv->xr_list,
2541 new_clusters -
2542 old_clusters);
Tao Ma97aff522008-11-19 16:48:41 +08002543 if (value_size >= OCFS2_XATTR_ROOT_SIZE)
2544 goto out;
Tao Ma78f30c32008-11-12 08:27:00 +08002545 }
2546 } else {
2547 /*
2548 * Now the new value will be stored inside. So if the new
2549 * value is smaller than the size of value root or the old
2550 * value, we don't need any allocation, otherwise we have
2551 * to guess metadata allocation.
2552 */
2553 if ((ocfs2_xattr_is_local(xe) && value_size >= xi->value_len) ||
2554 (!ocfs2_xattr_is_local(xe) &&
2555 OCFS2_XATTR_ROOT_SIZE >= xi->value_len))
2556 goto out;
2557 }
2558
2559meta_guess:
2560 /* calculate metadata allocation. */
2561 if (di->i_xattr_loc) {
2562 if (!xbs->xattr_bh) {
Joel Becker4ae1d692008-11-13 14:49:18 -08002563 ret = ocfs2_read_xattr_block(inode,
2564 le64_to_cpu(di->i_xattr_loc),
2565 &bh);
Tao Ma78f30c32008-11-12 08:27:00 +08002566 if (ret) {
2567 mlog_errno(ret);
2568 goto out;
2569 }
2570
2571 xb = (struct ocfs2_xattr_block *)bh->b_data;
2572 } else
2573 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2574
Tao Ma90cb5462008-12-05 06:20:56 +08002575 /*
2576 * If there is already an xattr tree, good, we can calculate
2577 * like other b-trees. Otherwise we may have the chance of
2578 * create a tree, the credit calculation is borrowed from
2579 * ocfs2_calc_extend_credits with root_el = NULL. And the
2580 * new tree will be cluster based, so no meta is needed.
2581 */
Tao Ma78f30c32008-11-12 08:27:00 +08002582 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2583 struct ocfs2_extent_list *el =
2584 &xb->xb_attrs.xb_root.xt_list;
2585 meta_add += ocfs2_extend_meta_needed(el);
Tao Ma85db90e2008-11-12 08:27:01 +08002586 credits += ocfs2_calc_extend_credits(inode->i_sb,
2587 el, 1);
Tao Ma90cb5462008-12-05 06:20:56 +08002588 } else
2589 credits += OCFS2_SUBALLOC_ALLOC + 1;
Tao Ma78f30c32008-11-12 08:27:00 +08002590
2591 /*
2592 * This cluster will be used either for new bucket or for
2593 * new xattr block.
2594 * If the cluster size is the same as the bucket size, one
2595 * more is needed since we may need to extend the bucket
2596 * also.
2597 */
2598 clusters_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08002599 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08002600 if (OCFS2_XATTR_BUCKET_SIZE ==
Tao Ma85db90e2008-11-12 08:27:01 +08002601 OCFS2_SB(inode->i_sb)->s_clustersize) {
2602 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08002603 clusters_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08002604 }
2605 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08002606 meta_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08002607 credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
2608 }
Tao Ma78f30c32008-11-12 08:27:00 +08002609out:
2610 if (clusters_need)
2611 *clusters_need = clusters_add;
2612 if (meta_need)
2613 *meta_need = meta_add;
Tao Ma85db90e2008-11-12 08:27:01 +08002614 if (credits_need)
2615 *credits_need = credits;
Tao Ma78f30c32008-11-12 08:27:00 +08002616 brelse(bh);
2617 return ret;
2618}
2619
2620static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
2621 struct ocfs2_dinode *di,
2622 struct ocfs2_xattr_info *xi,
2623 struct ocfs2_xattr_search *xis,
2624 struct ocfs2_xattr_search *xbs,
Tao Ma85db90e2008-11-12 08:27:01 +08002625 struct ocfs2_xattr_set_ctxt *ctxt,
Tao Ma492a8a32009-08-18 11:43:17 +08002626 int extra_meta,
Tao Ma85db90e2008-11-12 08:27:01 +08002627 int *credits)
Tao Ma78f30c32008-11-12 08:27:00 +08002628{
2629 int clusters_add, meta_add, ret;
2630 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2631
2632 memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
2633
2634 ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
2635
2636 ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
Tao Ma85db90e2008-11-12 08:27:01 +08002637 &clusters_add, &meta_add, credits);
Tao Ma78f30c32008-11-12 08:27:00 +08002638 if (ret) {
2639 mlog_errno(ret);
2640 return ret;
2641 }
2642
Tao Ma492a8a32009-08-18 11:43:17 +08002643 meta_add += extra_meta;
Tao Ma85db90e2008-11-12 08:27:01 +08002644 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
2645 "credits = %d\n", xi->name, meta_add, clusters_add, *credits);
Tao Ma78f30c32008-11-12 08:27:00 +08002646
2647 if (meta_add) {
2648 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
2649 &ctxt->meta_ac);
2650 if (ret) {
2651 mlog_errno(ret);
2652 goto out;
2653 }
2654 }
2655
2656 if (clusters_add) {
2657 ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
2658 if (ret)
2659 mlog_errno(ret);
2660 }
2661out:
2662 if (ret) {
2663 if (ctxt->meta_ac) {
2664 ocfs2_free_alloc_context(ctxt->meta_ac);
2665 ctxt->meta_ac = NULL;
2666 }
2667
2668 /*
2669 * We cannot have an error and a non null ctxt->data_ac.
2670 */
2671 }
2672
2673 return ret;
2674}
2675
Tao Ma85db90e2008-11-12 08:27:01 +08002676static int __ocfs2_xattr_set_handle(struct inode *inode,
2677 struct ocfs2_dinode *di,
2678 struct ocfs2_xattr_info *xi,
2679 struct ocfs2_xattr_search *xis,
2680 struct ocfs2_xattr_search *xbs,
2681 struct ocfs2_xattr_set_ctxt *ctxt)
2682{
Tao Ma9f868f12008-11-19 16:48:42 +08002683 int ret = 0, credits, old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08002684
2685 if (!xi->value) {
2686 /* Remove existing extended attribute */
2687 if (!xis->not_found)
2688 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2689 else if (!xbs->not_found)
2690 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2691 } else {
2692 /* We always try to set extended attribute into inode first*/
2693 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2694 if (!ret && !xbs->not_found) {
2695 /*
2696 * If succeed and that extended attribute existing in
2697 * external block, then we will remove it.
2698 */
2699 xi->value = NULL;
2700 xi->value_len = 0;
2701
Tao Ma9f868f12008-11-19 16:48:42 +08002702 old_found = xis->not_found;
Tao Ma85db90e2008-11-12 08:27:01 +08002703 xis->not_found = -ENODATA;
2704 ret = ocfs2_calc_xattr_set_need(inode,
2705 di,
2706 xi,
2707 xis,
2708 xbs,
2709 NULL,
2710 NULL,
2711 &credits);
Tao Ma9f868f12008-11-19 16:48:42 +08002712 xis->not_found = old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08002713 if (ret) {
2714 mlog_errno(ret);
2715 goto out;
2716 }
2717
2718 ret = ocfs2_extend_trans(ctxt->handle, credits +
2719 ctxt->handle->h_buffer_credits);
2720 if (ret) {
2721 mlog_errno(ret);
2722 goto out;
2723 }
2724 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2725 } else if (ret == -ENOSPC) {
2726 if (di->i_xattr_loc && !xbs->xattr_bh) {
2727 ret = ocfs2_xattr_block_find(inode,
2728 xi->name_index,
2729 xi->name, xbs);
2730 if (ret)
2731 goto out;
2732
Tao Ma9f868f12008-11-19 16:48:42 +08002733 old_found = xis->not_found;
Tao Ma85db90e2008-11-12 08:27:01 +08002734 xis->not_found = -ENODATA;
2735 ret = ocfs2_calc_xattr_set_need(inode,
2736 di,
2737 xi,
2738 xis,
2739 xbs,
2740 NULL,
2741 NULL,
2742 &credits);
Tao Ma9f868f12008-11-19 16:48:42 +08002743 xis->not_found = old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08002744 if (ret) {
2745 mlog_errno(ret);
2746 goto out;
2747 }
2748
2749 ret = ocfs2_extend_trans(ctxt->handle, credits +
2750 ctxt->handle->h_buffer_credits);
2751 if (ret) {
2752 mlog_errno(ret);
2753 goto out;
2754 }
2755 }
2756 /*
2757 * If no space in inode, we will set extended attribute
2758 * into external block.
2759 */
2760 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2761 if (ret)
2762 goto out;
2763 if (!xis->not_found) {
2764 /*
2765 * If succeed and that extended attribute
2766 * existing in inode, we will remove it.
2767 */
2768 xi->value = NULL;
2769 xi->value_len = 0;
2770 xbs->not_found = -ENODATA;
2771 ret = ocfs2_calc_xattr_set_need(inode,
2772 di,
2773 xi,
2774 xis,
2775 xbs,
2776 NULL,
2777 NULL,
2778 &credits);
2779 if (ret) {
2780 mlog_errno(ret);
2781 goto out;
2782 }
2783
2784 ret = ocfs2_extend_trans(ctxt->handle, credits +
2785 ctxt->handle->h_buffer_credits);
2786 if (ret) {
2787 mlog_errno(ret);
2788 goto out;
2789 }
2790 ret = ocfs2_xattr_ibody_set(inode, xi,
2791 xis, ctxt);
2792 }
2793 }
2794 }
2795
Tao Ma4b3f6202008-12-05 06:20:55 +08002796 if (!ret) {
2797 /* Update inode ctime. */
Joel Becker0cf2f762009-02-12 16:41:25 -08002798 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
Tao Ma89a907a2009-02-17 04:39:28 +08002799 xis->inode_bh,
2800 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma4b3f6202008-12-05 06:20:55 +08002801 if (ret) {
2802 mlog_errno(ret);
2803 goto out;
2804 }
2805
2806 inode->i_ctime = CURRENT_TIME;
2807 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
2808 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
2809 ocfs2_journal_dirty(ctxt->handle, xis->inode_bh);
2810 }
Tao Ma85db90e2008-11-12 08:27:01 +08002811out:
2812 return ret;
2813}
2814
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002815/*
Tiger Yang6c3faba2008-11-14 11:16:03 +08002816 * This function only called duing creating inode
2817 * for init security/acl xattrs of the new inode.
Tiger Yang008aafa2008-12-09 16:43:08 +08002818 * All transanction credits have been reserved in mknod.
Tiger Yang6c3faba2008-11-14 11:16:03 +08002819 */
2820int ocfs2_xattr_set_handle(handle_t *handle,
2821 struct inode *inode,
2822 struct buffer_head *di_bh,
2823 int name_index,
2824 const char *name,
2825 const void *value,
2826 size_t value_len,
2827 int flags,
2828 struct ocfs2_alloc_context *meta_ac,
2829 struct ocfs2_alloc_context *data_ac)
2830{
2831 struct ocfs2_dinode *di;
2832 int ret;
2833
2834 struct ocfs2_xattr_info xi = {
2835 .name_index = name_index,
2836 .name = name,
2837 .value = value,
2838 .value_len = value_len,
2839 };
2840
2841 struct ocfs2_xattr_search xis = {
2842 .not_found = -ENODATA,
2843 };
2844
2845 struct ocfs2_xattr_search xbs = {
2846 .not_found = -ENODATA,
2847 };
2848
2849 struct ocfs2_xattr_set_ctxt ctxt = {
2850 .handle = handle,
2851 .meta_ac = meta_ac,
2852 .data_ac = data_ac,
2853 };
2854
2855 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2856 return -EOPNOTSUPP;
2857
Tiger Yang008aafa2008-12-09 16:43:08 +08002858 /*
2859 * In extreme situation, may need xattr bucket when
2860 * block size is too small. And we have already reserved
2861 * the credits for bucket in mknod.
2862 */
2863 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) {
2864 xbs.bucket = ocfs2_xattr_bucket_new(inode);
2865 if (!xbs.bucket) {
2866 mlog_errno(-ENOMEM);
2867 return -ENOMEM;
2868 }
2869 }
2870
Tiger Yang6c3faba2008-11-14 11:16:03 +08002871 xis.inode_bh = xbs.inode_bh = di_bh;
2872 di = (struct ocfs2_dinode *)di_bh->b_data;
2873
2874 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2875
2876 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2877 if (ret)
2878 goto cleanup;
2879 if (xis.not_found) {
2880 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2881 if (ret)
2882 goto cleanup;
2883 }
2884
2885 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
2886
2887cleanup:
2888 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2889 brelse(xbs.xattr_bh);
Tiger Yang008aafa2008-12-09 16:43:08 +08002890 ocfs2_xattr_bucket_free(xbs.bucket);
Tiger Yang6c3faba2008-11-14 11:16:03 +08002891
2892 return ret;
2893}
2894
2895/*
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002896 * ocfs2_xattr_set()
2897 *
2898 * Set, replace or remove an extended attribute for this inode.
2899 * value is NULL to remove an existing extended attribute, else either
2900 * create or replace an extended attribute.
2901 */
2902int ocfs2_xattr_set(struct inode *inode,
2903 int name_index,
2904 const char *name,
2905 const void *value,
2906 size_t value_len,
2907 int flags)
2908{
2909 struct buffer_head *di_bh = NULL;
2910 struct ocfs2_dinode *di;
Tao Ma492a8a32009-08-18 11:43:17 +08002911 int ret, credits, ref_meta = 0, ref_credits = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08002912 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08002913 struct inode *tl_inode = osb->osb_tl_inode;
Tao Ma78f30c32008-11-12 08:27:00 +08002914 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
Tao Ma492a8a32009-08-18 11:43:17 +08002915 struct ocfs2_refcount_tree *ref_tree = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002916
2917 struct ocfs2_xattr_info xi = {
2918 .name_index = name_index,
2919 .name = name,
2920 .value = value,
2921 .value_len = value_len,
2922 };
2923
2924 struct ocfs2_xattr_search xis = {
2925 .not_found = -ENODATA,
2926 };
2927
2928 struct ocfs2_xattr_search xbs = {
2929 .not_found = -ENODATA,
2930 };
2931
Tiger Yang8154da32008-08-18 17:11:46 +08002932 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2933 return -EOPNOTSUPP;
2934
Joel Beckerba937122008-10-24 19:13:20 -07002935 /*
2936 * Only xbs will be used on indexed trees. xis doesn't need a
2937 * bucket.
2938 */
2939 xbs.bucket = ocfs2_xattr_bucket_new(inode);
2940 if (!xbs.bucket) {
2941 mlog_errno(-ENOMEM);
2942 return -ENOMEM;
2943 }
2944
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002945 ret = ocfs2_inode_lock(inode, &di_bh, 1);
2946 if (ret < 0) {
2947 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07002948 goto cleanup_nolock;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002949 }
2950 xis.inode_bh = xbs.inode_bh = di_bh;
2951 di = (struct ocfs2_dinode *)di_bh->b_data;
2952
2953 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2954 /*
2955 * Scan inode and external block to find the same name
2956 * extended attribute and collect search infomation.
2957 */
2958 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2959 if (ret)
2960 goto cleanup;
2961 if (xis.not_found) {
2962 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2963 if (ret)
2964 goto cleanup;
2965 }
2966
2967 if (xis.not_found && xbs.not_found) {
2968 ret = -ENODATA;
2969 if (flags & XATTR_REPLACE)
2970 goto cleanup;
2971 ret = 0;
2972 if (!value)
2973 goto cleanup;
2974 } else {
2975 ret = -EEXIST;
2976 if (flags & XATTR_CREATE)
2977 goto cleanup;
2978 }
2979
Tao Ma492a8a32009-08-18 11:43:17 +08002980 /* Check whether the value is refcounted and do some prepartion. */
2981 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL &&
2982 (!xis.not_found || !xbs.not_found)) {
2983 ret = ocfs2_prepare_refcount_xattr(inode, di, &xi,
2984 &xis, &xbs, &ref_tree,
2985 &ref_meta, &ref_credits);
2986 if (ret) {
2987 mlog_errno(ret);
2988 goto cleanup;
2989 }
2990 }
Tao Ma85db90e2008-11-12 08:27:01 +08002991
2992 mutex_lock(&tl_inode->i_mutex);
2993
2994 if (ocfs2_truncate_log_needs_flush(osb)) {
2995 ret = __ocfs2_flush_truncate_log(osb);
2996 if (ret < 0) {
2997 mutex_unlock(&tl_inode->i_mutex);
2998 mlog_errno(ret);
2999 goto cleanup;
3000 }
3001 }
3002 mutex_unlock(&tl_inode->i_mutex);
3003
3004 ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
Tao Ma492a8a32009-08-18 11:43:17 +08003005 &xbs, &ctxt, ref_meta, &credits);
Tao Ma78f30c32008-11-12 08:27:00 +08003006 if (ret) {
3007 mlog_errno(ret);
3008 goto cleanup;
3009 }
3010
Tao Ma4b3f6202008-12-05 06:20:55 +08003011 /* we need to update inode's ctime field, so add credit for it. */
3012 credits += OCFS2_INODE_UPDATE_CREDITS;
Tao Ma492a8a32009-08-18 11:43:17 +08003013 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
Tao Ma85db90e2008-11-12 08:27:01 +08003014 if (IS_ERR(ctxt.handle)) {
3015 ret = PTR_ERR(ctxt.handle);
3016 mlog_errno(ret);
3017 goto cleanup;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003018 }
Tao Ma85db90e2008-11-12 08:27:01 +08003019
3020 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3021
3022 ocfs2_commit_trans(osb, ctxt.handle);
3023
Tao Ma78f30c32008-11-12 08:27:00 +08003024 if (ctxt.data_ac)
3025 ocfs2_free_alloc_context(ctxt.data_ac);
3026 if (ctxt.meta_ac)
3027 ocfs2_free_alloc_context(ctxt.meta_ac);
3028 if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
3029 ocfs2_schedule_truncate_log_flush(osb, 1);
3030 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tao Ma8b2c0db2009-08-18 11:43:49 +08003031
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003032cleanup:
Tao Ma492a8a32009-08-18 11:43:17 +08003033 if (ref_tree)
3034 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003035 up_write(&OCFS2_I(inode)->ip_xattr_sem);
Tao Ma8b2c0db2009-08-18 11:43:49 +08003036 if (!value && !ret) {
3037 ret = ocfs2_try_remove_refcount_tree(inode, di_bh);
3038 if (ret)
3039 mlog_errno(ret);
3040 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003041 ocfs2_inode_unlock(inode, 1);
Joel Beckerba937122008-10-24 19:13:20 -07003042cleanup_nolock:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003043 brelse(di_bh);
3044 brelse(xbs.xattr_bh);
Joel Beckerba937122008-10-24 19:13:20 -07003045 ocfs2_xattr_bucket_free(xbs.bucket);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003046
3047 return ret;
3048}
3049
Tao Ma0c044f02008-08-18 17:38:50 +08003050/*
3051 * Find the xattr extent rec which may contains name_hash.
3052 * e_cpos will be the first name hash of the xattr rec.
3053 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
3054 */
3055static int ocfs2_xattr_get_rec(struct inode *inode,
3056 u32 name_hash,
3057 u64 *p_blkno,
3058 u32 *e_cpos,
3059 u32 *num_clusters,
3060 struct ocfs2_extent_list *el)
3061{
3062 int ret = 0, i;
3063 struct buffer_head *eb_bh = NULL;
3064 struct ocfs2_extent_block *eb;
3065 struct ocfs2_extent_rec *rec = NULL;
3066 u64 e_blkno = 0;
3067
3068 if (el->l_tree_depth) {
Joel Beckerfacdb772009-02-12 18:08:48 -08003069 ret = ocfs2_find_leaf(INODE_CACHE(inode), el, name_hash,
3070 &eb_bh);
Tao Ma0c044f02008-08-18 17:38:50 +08003071 if (ret) {
3072 mlog_errno(ret);
3073 goto out;
3074 }
3075
3076 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
3077 el = &eb->h_list;
3078
3079 if (el->l_tree_depth) {
3080 ocfs2_error(inode->i_sb,
3081 "Inode %lu has non zero tree depth in "
3082 "xattr tree block %llu\n", inode->i_ino,
3083 (unsigned long long)eb_bh->b_blocknr);
3084 ret = -EROFS;
3085 goto out;
3086 }
3087 }
3088
3089 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
3090 rec = &el->l_recs[i];
3091
3092 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
3093 e_blkno = le64_to_cpu(rec->e_blkno);
3094 break;
3095 }
3096 }
3097
3098 if (!e_blkno) {
3099 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
3100 "record (%u, %u, 0) in xattr", inode->i_ino,
3101 le32_to_cpu(rec->e_cpos),
3102 ocfs2_rec_clusters(el, rec));
3103 ret = -EROFS;
3104 goto out;
3105 }
3106
3107 *p_blkno = le64_to_cpu(rec->e_blkno);
3108 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
3109 if (e_cpos)
3110 *e_cpos = le32_to_cpu(rec->e_cpos);
3111out:
3112 brelse(eb_bh);
3113 return ret;
3114}
3115
3116typedef int (xattr_bucket_func)(struct inode *inode,
3117 struct ocfs2_xattr_bucket *bucket,
3118 void *para);
3119
Tao Ma589dc262008-08-18 17:38:51 +08003120static int ocfs2_find_xe_in_bucket(struct inode *inode,
Joel Beckere2356a32008-10-27 15:01:54 -07003121 struct ocfs2_xattr_bucket *bucket,
Tao Ma589dc262008-08-18 17:38:51 +08003122 int name_index,
3123 const char *name,
3124 u32 name_hash,
3125 u16 *xe_index,
3126 int *found)
3127{
3128 int i, ret = 0, cmp = 1, block_off, new_offset;
Joel Beckere2356a32008-10-27 15:01:54 -07003129 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma589dc262008-08-18 17:38:51 +08003130 size_t name_len = strlen(name);
3131 struct ocfs2_xattr_entry *xe = NULL;
Tao Ma589dc262008-08-18 17:38:51 +08003132 char *xe_name;
3133
3134 /*
3135 * We don't use binary search in the bucket because there
3136 * may be multiple entries with the same name hash.
3137 */
3138 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3139 xe = &xh->xh_entries[i];
3140
3141 if (name_hash > le32_to_cpu(xe->xe_name_hash))
3142 continue;
3143 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
3144 break;
3145
3146 cmp = name_index - ocfs2_xattr_get_type(xe);
3147 if (!cmp)
3148 cmp = name_len - xe->xe_name_len;
3149 if (cmp)
3150 continue;
3151
Tao Mafd68a892009-08-18 11:43:21 +08003152 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Tao Ma589dc262008-08-18 17:38:51 +08003153 xh,
3154 i,
3155 &block_off,
3156 &new_offset);
3157 if (ret) {
3158 mlog_errno(ret);
3159 break;
3160 }
3161
Joel Becker970e4932008-11-13 14:49:19 -08003162
Joel Beckere2356a32008-10-27 15:01:54 -07003163 xe_name = bucket_block(bucket, block_off) + new_offset;
3164 if (!memcmp(name, xe_name, name_len)) {
Tao Ma589dc262008-08-18 17:38:51 +08003165 *xe_index = i;
3166 *found = 1;
3167 ret = 0;
3168 break;
3169 }
3170 }
3171
3172 return ret;
3173}
3174
3175/*
3176 * Find the specified xattr entry in a series of buckets.
3177 * This series start from p_blkno and last for num_clusters.
3178 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
3179 * the num of the valid buckets.
3180 *
3181 * Return the buffer_head this xattr should reside in. And if the xattr's
3182 * hash is in the gap of 2 buckets, return the lower bucket.
3183 */
3184static int ocfs2_xattr_bucket_find(struct inode *inode,
3185 int name_index,
3186 const char *name,
3187 u32 name_hash,
3188 u64 p_blkno,
3189 u32 first_hash,
3190 u32 num_clusters,
3191 struct ocfs2_xattr_search *xs)
3192{
3193 int ret, found = 0;
Tao Ma589dc262008-08-18 17:38:51 +08003194 struct ocfs2_xattr_header *xh = NULL;
3195 struct ocfs2_xattr_entry *xe = NULL;
3196 u16 index = 0;
3197 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3198 int low_bucket = 0, bucket, high_bucket;
Joel Beckere2356a32008-10-27 15:01:54 -07003199 struct ocfs2_xattr_bucket *search;
Tao Ma589dc262008-08-18 17:38:51 +08003200 u32 last_hash;
Joel Beckere2356a32008-10-27 15:01:54 -07003201 u64 blkno, lower_blkno = 0;
Tao Ma589dc262008-08-18 17:38:51 +08003202
Joel Beckere2356a32008-10-27 15:01:54 -07003203 search = ocfs2_xattr_bucket_new(inode);
3204 if (!search) {
3205 ret = -ENOMEM;
3206 mlog_errno(ret);
3207 goto out;
3208 }
3209
3210 ret = ocfs2_read_xattr_bucket(search, p_blkno);
Tao Ma589dc262008-08-18 17:38:51 +08003211 if (ret) {
3212 mlog_errno(ret);
3213 goto out;
3214 }
3215
Joel Beckere2356a32008-10-27 15:01:54 -07003216 xh = bucket_xh(search);
Tao Ma589dc262008-08-18 17:38:51 +08003217 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
Tao Ma589dc262008-08-18 17:38:51 +08003218 while (low_bucket <= high_bucket) {
Joel Beckere2356a32008-10-27 15:01:54 -07003219 ocfs2_xattr_bucket_relse(search);
3220
Tao Ma589dc262008-08-18 17:38:51 +08003221 bucket = (low_bucket + high_bucket) / 2;
Tao Ma589dc262008-08-18 17:38:51 +08003222 blkno = p_blkno + bucket * blk_per_bucket;
Joel Beckere2356a32008-10-27 15:01:54 -07003223 ret = ocfs2_read_xattr_bucket(search, blkno);
Tao Ma589dc262008-08-18 17:38:51 +08003224 if (ret) {
3225 mlog_errno(ret);
3226 goto out;
3227 }
3228
Joel Beckere2356a32008-10-27 15:01:54 -07003229 xh = bucket_xh(search);
Tao Ma589dc262008-08-18 17:38:51 +08003230 xe = &xh->xh_entries[0];
3231 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
3232 high_bucket = bucket - 1;
3233 continue;
3234 }
3235
3236 /*
3237 * Check whether the hash of the last entry in our
Tao Ma5a095612008-09-19 22:17:41 +08003238 * bucket is larger than the search one. for an empty
3239 * bucket, the last one is also the first one.
Tao Ma589dc262008-08-18 17:38:51 +08003240 */
Tao Ma5a095612008-09-19 22:17:41 +08003241 if (xh->xh_count)
3242 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
3243
Tao Ma589dc262008-08-18 17:38:51 +08003244 last_hash = le32_to_cpu(xe->xe_name_hash);
3245
Joel Beckere2356a32008-10-27 15:01:54 -07003246 /* record lower_blkno which may be the insert place. */
3247 lower_blkno = blkno;
Tao Ma589dc262008-08-18 17:38:51 +08003248
3249 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
3250 low_bucket = bucket + 1;
3251 continue;
3252 }
3253
3254 /* the searched xattr should reside in this bucket if exists. */
Joel Beckere2356a32008-10-27 15:01:54 -07003255 ret = ocfs2_find_xe_in_bucket(inode, search,
Tao Ma589dc262008-08-18 17:38:51 +08003256 name_index, name, name_hash,
3257 &index, &found);
3258 if (ret) {
3259 mlog_errno(ret);
3260 goto out;
3261 }
3262 break;
3263 }
3264
3265 /*
3266 * Record the bucket we have found.
3267 * When the xattr's hash value is in the gap of 2 buckets, we will
3268 * always set it to the previous bucket.
3269 */
Joel Beckere2356a32008-10-27 15:01:54 -07003270 if (!lower_blkno)
3271 lower_blkno = p_blkno;
3272
3273 /* This should be in cache - we just read it during the search */
3274 ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
3275 if (ret) {
3276 mlog_errno(ret);
3277 goto out;
Tao Ma589dc262008-08-18 17:38:51 +08003278 }
Tao Ma589dc262008-08-18 17:38:51 +08003279
Joel Beckerba937122008-10-24 19:13:20 -07003280 xs->header = bucket_xh(xs->bucket);
3281 xs->base = bucket_block(xs->bucket, 0);
Tao Ma589dc262008-08-18 17:38:51 +08003282 xs->end = xs->base + inode->i_sb->s_blocksize;
3283
3284 if (found) {
Tao Ma589dc262008-08-18 17:38:51 +08003285 xs->here = &xs->header->xh_entries[index];
3286 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
Joel Beckerba937122008-10-24 19:13:20 -07003287 (unsigned long long)bucket_blkno(xs->bucket), index);
Tao Ma589dc262008-08-18 17:38:51 +08003288 } else
3289 ret = -ENODATA;
3290
3291out:
Joel Beckere2356a32008-10-27 15:01:54 -07003292 ocfs2_xattr_bucket_free(search);
Tao Ma589dc262008-08-18 17:38:51 +08003293 return ret;
3294}
3295
3296static int ocfs2_xattr_index_block_find(struct inode *inode,
3297 struct buffer_head *root_bh,
3298 int name_index,
3299 const char *name,
3300 struct ocfs2_xattr_search *xs)
3301{
3302 int ret;
3303 struct ocfs2_xattr_block *xb =
3304 (struct ocfs2_xattr_block *)root_bh->b_data;
3305 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3306 struct ocfs2_extent_list *el = &xb_root->xt_list;
3307 u64 p_blkno = 0;
3308 u32 first_hash, num_clusters = 0;
Tao Ma2057e5c2008-10-09 23:06:13 +08003309 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
Tao Ma589dc262008-08-18 17:38:51 +08003310
3311 if (le16_to_cpu(el->l_next_free_rec) == 0)
3312 return -ENODATA;
3313
3314 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
3315 name, name_hash, name_index);
3316
3317 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
3318 &num_clusters, el);
3319 if (ret) {
3320 mlog_errno(ret);
3321 goto out;
3322 }
3323
3324 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
3325
3326 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
Mark Fashehde29c082008-10-29 14:45:30 -07003327 "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
3328 first_hash);
Tao Ma589dc262008-08-18 17:38:51 +08003329
3330 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
3331 p_blkno, first_hash, num_clusters, xs);
3332
3333out:
3334 return ret;
3335}
3336
Tao Ma0c044f02008-08-18 17:38:50 +08003337static int ocfs2_iterate_xattr_buckets(struct inode *inode,
3338 u64 blkno,
3339 u32 clusters,
3340 xattr_bucket_func *func,
3341 void *para)
3342{
Joel Becker6dde41d2008-10-24 17:16:48 -07003343 int i, ret = 0;
Tao Ma0c044f02008-08-18 17:38:50 +08003344 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
3345 u32 num_buckets = clusters * bpc;
Joel Beckerba937122008-10-24 19:13:20 -07003346 struct ocfs2_xattr_bucket *bucket;
Tao Ma0c044f02008-08-18 17:38:50 +08003347
Joel Beckerba937122008-10-24 19:13:20 -07003348 bucket = ocfs2_xattr_bucket_new(inode);
3349 if (!bucket) {
3350 mlog_errno(-ENOMEM);
3351 return -ENOMEM;
3352 }
Tao Ma0c044f02008-08-18 17:38:50 +08003353
3354 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003355 clusters, (unsigned long long)blkno);
Tao Ma0c044f02008-08-18 17:38:50 +08003356
Joel Beckerba937122008-10-24 19:13:20 -07003357 for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
3358 ret = ocfs2_read_xattr_bucket(bucket, blkno);
Tao Ma0c044f02008-08-18 17:38:50 +08003359 if (ret) {
3360 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07003361 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003362 }
3363
Tao Ma0c044f02008-08-18 17:38:50 +08003364 /*
3365 * The real bucket num in this series of blocks is stored
3366 * in the 1st bucket.
3367 */
3368 if (i == 0)
Joel Beckerba937122008-10-24 19:13:20 -07003369 num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
Tao Ma0c044f02008-08-18 17:38:50 +08003370
Mark Fashehde29c082008-10-29 14:45:30 -07003371 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
3372 (unsigned long long)blkno,
Joel Beckerba937122008-10-24 19:13:20 -07003373 le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
Tao Ma0c044f02008-08-18 17:38:50 +08003374 if (func) {
Joel Beckerba937122008-10-24 19:13:20 -07003375 ret = func(inode, bucket, para);
Tao Maa46fa682009-05-04 05:18:09 +08003376 if (ret && ret != -ERANGE)
Tao Ma0c044f02008-08-18 17:38:50 +08003377 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07003378 /* Fall through to bucket_relse() */
Tao Ma0c044f02008-08-18 17:38:50 +08003379 }
3380
Joel Beckerba937122008-10-24 19:13:20 -07003381 ocfs2_xattr_bucket_relse(bucket);
3382 if (ret)
3383 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003384 }
3385
Joel Beckerba937122008-10-24 19:13:20 -07003386 ocfs2_xattr_bucket_free(bucket);
Tao Ma0c044f02008-08-18 17:38:50 +08003387 return ret;
3388}
3389
3390struct ocfs2_xattr_tree_list {
3391 char *buffer;
3392 size_t buffer_size;
Tao Ma936b8832008-10-09 23:06:14 +08003393 size_t result;
Tao Ma0c044f02008-08-18 17:38:50 +08003394};
3395
Tao Mafd68a892009-08-18 11:43:21 +08003396static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
Tao Ma0c044f02008-08-18 17:38:50 +08003397 struct ocfs2_xattr_header *xh,
3398 int index,
3399 int *block_off,
3400 int *new_offset)
3401{
3402 u16 name_offset;
3403
3404 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
3405 return -EINVAL;
3406
3407 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
3408
Tao Mafd68a892009-08-18 11:43:21 +08003409 *block_off = name_offset >> sb->s_blocksize_bits;
3410 *new_offset = name_offset % sb->s_blocksize;
Tao Ma0c044f02008-08-18 17:38:50 +08003411
3412 return 0;
3413}
3414
3415static int ocfs2_list_xattr_bucket(struct inode *inode,
3416 struct ocfs2_xattr_bucket *bucket,
3417 void *para)
3418{
Tao Ma936b8832008-10-09 23:06:14 +08003419 int ret = 0, type;
Tao Ma0c044f02008-08-18 17:38:50 +08003420 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
Tao Ma0c044f02008-08-18 17:38:50 +08003421 int i, block_off, new_offset;
Tao Ma936b8832008-10-09 23:06:14 +08003422 const char *prefix, *name;
Tao Ma0c044f02008-08-18 17:38:50 +08003423
Joel Becker3e632942008-10-24 17:04:49 -07003424 for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
3425 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
Tao Ma936b8832008-10-09 23:06:14 +08003426 type = ocfs2_xattr_get_type(entry);
3427 prefix = ocfs2_xattr_prefix(type);
Tao Ma0c044f02008-08-18 17:38:50 +08003428
Tao Ma936b8832008-10-09 23:06:14 +08003429 if (prefix) {
Tao Mafd68a892009-08-18 11:43:21 +08003430 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Joel Becker3e632942008-10-24 17:04:49 -07003431 bucket_xh(bucket),
Tao Ma0c044f02008-08-18 17:38:50 +08003432 i,
3433 &block_off,
3434 &new_offset);
3435 if (ret)
3436 break;
Tao Ma936b8832008-10-09 23:06:14 +08003437
Joel Becker51def392008-10-24 16:57:21 -07003438 name = (const char *)bucket_block(bucket, block_off) +
Tao Ma936b8832008-10-09 23:06:14 +08003439 new_offset;
3440 ret = ocfs2_xattr_list_entry(xl->buffer,
3441 xl->buffer_size,
3442 &xl->result,
3443 prefix, name,
3444 entry->xe_name_len);
3445 if (ret)
3446 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003447 }
3448 }
3449
3450 return ret;
3451}
3452
Tao Ma47bca492009-08-18 11:43:42 +08003453static int ocfs2_iterate_xattr_index_block(struct inode *inode,
3454 struct buffer_head *blk_bh,
3455 xattr_tree_rec_func *rec_func,
3456 void *para)
Tao Ma0c044f02008-08-18 17:38:50 +08003457{
Tao Ma47bca492009-08-18 11:43:42 +08003458 struct ocfs2_xattr_block *xb =
3459 (struct ocfs2_xattr_block *)blk_bh->b_data;
3460 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
Tao Ma0c044f02008-08-18 17:38:50 +08003461 int ret = 0;
3462 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
3463 u64 p_blkno = 0;
Tao Ma0c044f02008-08-18 17:38:50 +08003464
Tao Ma47bca492009-08-18 11:43:42 +08003465 if (!el->l_next_free_rec || !rec_func)
Tao Ma0c044f02008-08-18 17:38:50 +08003466 return 0;
3467
3468 while (name_hash > 0) {
3469 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
3470 &e_cpos, &num_clusters, el);
3471 if (ret) {
3472 mlog_errno(ret);
Tao Ma47bca492009-08-18 11:43:42 +08003473 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003474 }
3475
Tao Ma47bca492009-08-18 11:43:42 +08003476 ret = rec_func(inode, blk_bh, p_blkno, e_cpos,
3477 num_clusters, para);
Tao Ma0c044f02008-08-18 17:38:50 +08003478 if (ret) {
Tao Maa46fa682009-05-04 05:18:09 +08003479 if (ret != -ERANGE)
3480 mlog_errno(ret);
Tao Ma47bca492009-08-18 11:43:42 +08003481 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003482 }
3483
3484 if (e_cpos == 0)
3485 break;
3486
3487 name_hash = e_cpos - 1;
3488 }
3489
Tao Ma47bca492009-08-18 11:43:42 +08003490 return ret;
3491
3492}
3493
3494static int ocfs2_list_xattr_tree_rec(struct inode *inode,
3495 struct buffer_head *root_bh,
3496 u64 blkno, u32 cpos, u32 len, void *para)
3497{
3498 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
3499 ocfs2_list_xattr_bucket, para);
3500}
3501
3502static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
3503 struct buffer_head *blk_bh,
3504 char *buffer,
3505 size_t buffer_size)
3506{
3507 int ret;
3508 struct ocfs2_xattr_tree_list xl = {
3509 .buffer = buffer,
3510 .buffer_size = buffer_size,
3511 .result = 0,
3512 };
3513
3514 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
3515 ocfs2_list_xattr_tree_rec, &xl);
3516 if (ret) {
3517 mlog_errno(ret);
3518 goto out;
3519 }
3520
Tao Ma936b8832008-10-09 23:06:14 +08003521 ret = xl.result;
Tao Ma0c044f02008-08-18 17:38:50 +08003522out:
3523 return ret;
3524}
Tao Ma01225592008-08-18 17:38:53 +08003525
3526static int cmp_xe(const void *a, const void *b)
3527{
3528 const struct ocfs2_xattr_entry *l = a, *r = b;
3529 u32 l_hash = le32_to_cpu(l->xe_name_hash);
3530 u32 r_hash = le32_to_cpu(r->xe_name_hash);
3531
3532 if (l_hash > r_hash)
3533 return 1;
3534 if (l_hash < r_hash)
3535 return -1;
3536 return 0;
3537}
3538
3539static void swap_xe(void *a, void *b, int size)
3540{
3541 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
3542
3543 tmp = *l;
3544 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
3545 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
3546}
3547
3548/*
3549 * When the ocfs2_xattr_block is filled up, new bucket will be created
3550 * and all the xattr entries will be moved to the new bucket.
Joel Becker178eeac2008-10-27 15:18:29 -07003551 * The header goes at the start of the bucket, and the names+values are
3552 * filled from the end. This is why *target starts as the last buffer.
Tao Ma01225592008-08-18 17:38:53 +08003553 * Note: we need to sort the entries since they are not saved in order
3554 * in the ocfs2_xattr_block.
3555 */
3556static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
3557 struct buffer_head *xb_bh,
Joel Becker178eeac2008-10-27 15:18:29 -07003558 struct ocfs2_xattr_bucket *bucket)
Tao Ma01225592008-08-18 17:38:53 +08003559{
3560 int i, blocksize = inode->i_sb->s_blocksize;
Joel Becker178eeac2008-10-27 15:18:29 -07003561 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08003562 u16 offset, size, off_change;
3563 struct ocfs2_xattr_entry *xe;
3564 struct ocfs2_xattr_block *xb =
3565 (struct ocfs2_xattr_block *)xb_bh->b_data;
3566 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
Joel Becker178eeac2008-10-27 15:18:29 -07003567 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma01225592008-08-18 17:38:53 +08003568 u16 count = le16_to_cpu(xb_xh->xh_count);
Joel Becker178eeac2008-10-27 15:18:29 -07003569 char *src = xb_bh->b_data;
3570 char *target = bucket_block(bucket, blks - 1);
Tao Ma01225592008-08-18 17:38:53 +08003571
3572 mlog(0, "cp xattr from block %llu to bucket %llu\n",
3573 (unsigned long long)xb_bh->b_blocknr,
Joel Becker178eeac2008-10-27 15:18:29 -07003574 (unsigned long long)bucket_blkno(bucket));
Tao Ma01225592008-08-18 17:38:53 +08003575
Joel Becker178eeac2008-10-27 15:18:29 -07003576 for (i = 0; i < blks; i++)
3577 memset(bucket_block(bucket, i), 0, blocksize);
3578
Tao Ma01225592008-08-18 17:38:53 +08003579 /*
3580 * Since the xe_name_offset is based on ocfs2_xattr_header,
3581 * there is a offset change corresponding to the change of
3582 * ocfs2_xattr_header's position.
3583 */
3584 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
3585 xe = &xb_xh->xh_entries[count - 1];
3586 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
3587 size = blocksize - offset;
3588
3589 /* copy all the names and values. */
Tao Ma01225592008-08-18 17:38:53 +08003590 memcpy(target + offset, src + offset, size);
3591
3592 /* Init new header now. */
3593 xh->xh_count = xb_xh->xh_count;
3594 xh->xh_num_buckets = cpu_to_le16(1);
3595 xh->xh_name_value_len = cpu_to_le16(size);
3596 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
3597
3598 /* copy all the entries. */
Joel Becker178eeac2008-10-27 15:18:29 -07003599 target = bucket_block(bucket, 0);
Tao Ma01225592008-08-18 17:38:53 +08003600 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
3601 size = count * sizeof(struct ocfs2_xattr_entry);
3602 memcpy(target + offset, (char *)xb_xh + offset, size);
3603
3604 /* Change the xe offset for all the xe because of the move. */
3605 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
3606 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
3607 for (i = 0; i < count; i++)
3608 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
3609
3610 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
3611 offset, size, off_change);
3612
3613 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
3614 cmp_xe, swap_xe);
3615}
3616
3617/*
3618 * After we move xattr from block to index btree, we have to
3619 * update ocfs2_xattr_search to the new xe and base.
3620 *
3621 * When the entry is in xattr block, xattr_bh indicates the storage place.
3622 * While if the entry is in index b-tree, "bucket" indicates the
3623 * real place of the xattr.
3624 */
Joel Becker178eeac2008-10-27 15:18:29 -07003625static void ocfs2_xattr_update_xattr_search(struct inode *inode,
3626 struct ocfs2_xattr_search *xs,
3627 struct buffer_head *old_bh)
Tao Ma01225592008-08-18 17:38:53 +08003628{
Tao Ma01225592008-08-18 17:38:53 +08003629 char *buf = old_bh->b_data;
3630 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
3631 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
Joel Becker178eeac2008-10-27 15:18:29 -07003632 int i;
Tao Ma01225592008-08-18 17:38:53 +08003633
Joel Beckerba937122008-10-24 19:13:20 -07003634 xs->header = bucket_xh(xs->bucket);
Joel Becker178eeac2008-10-27 15:18:29 -07003635 xs->base = bucket_block(xs->bucket, 0);
Tao Ma01225592008-08-18 17:38:53 +08003636 xs->end = xs->base + inode->i_sb->s_blocksize;
3637
Joel Becker178eeac2008-10-27 15:18:29 -07003638 if (xs->not_found)
3639 return;
Tao Ma01225592008-08-18 17:38:53 +08003640
Joel Becker178eeac2008-10-27 15:18:29 -07003641 i = xs->here - old_xh->xh_entries;
3642 xs->here = &xs->header->xh_entries[i];
Tao Ma01225592008-08-18 17:38:53 +08003643}
3644
3645static int ocfs2_xattr_create_index_block(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +08003646 struct ocfs2_xattr_search *xs,
3647 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08003648{
Tao Ma85db90e2008-11-12 08:27:01 +08003649 int ret;
Tao Ma01225592008-08-18 17:38:53 +08003650 u32 bit_off, len;
3651 u64 blkno;
Tao Ma85db90e2008-11-12 08:27:01 +08003652 handle_t *handle = ctxt->handle;
Tao Ma01225592008-08-18 17:38:53 +08003653 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3654 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Tao Ma01225592008-08-18 17:38:53 +08003655 struct buffer_head *xb_bh = xs->xattr_bh;
3656 struct ocfs2_xattr_block *xb =
3657 (struct ocfs2_xattr_block *)xb_bh->b_data;
3658 struct ocfs2_xattr_tree_root *xr;
3659 u16 xb_flags = le16_to_cpu(xb->xb_flags);
Tao Ma01225592008-08-18 17:38:53 +08003660
3661 mlog(0, "create xattr index block for %llu\n",
3662 (unsigned long long)xb_bh->b_blocknr);
3663
3664 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
Joel Becker178eeac2008-10-27 15:18:29 -07003665 BUG_ON(!xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08003666
Tao Ma01225592008-08-18 17:38:53 +08003667 /*
3668 * XXX:
3669 * We can use this lock for now, and maybe move to a dedicated mutex
3670 * if performance becomes a problem later.
3671 */
3672 down_write(&oi->ip_alloc_sem);
3673
Joel Becker0cf2f762009-02-12 16:41:25 -08003674 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), xb_bh,
Joel Becker84008972008-12-09 16:11:49 -08003675 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08003676 if (ret) {
3677 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003678 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003679 }
3680
Tao Ma78f30c32008-11-12 08:27:00 +08003681 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac,
3682 1, 1, &bit_off, &len);
Tao Ma01225592008-08-18 17:38:53 +08003683 if (ret) {
3684 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003685 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003686 }
3687
3688 /*
3689 * The bucket may spread in many blocks, and
3690 * we will only touch the 1st block and the last block
3691 * in the whole bucket(one for entry and one for data).
3692 */
3693 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
3694
Mark Fashehde29c082008-10-29 14:45:30 -07003695 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
3696 (unsigned long long)blkno);
Tao Ma01225592008-08-18 17:38:53 +08003697
Joel Becker178eeac2008-10-27 15:18:29 -07003698 ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
Tao Ma01225592008-08-18 17:38:53 +08003699 if (ret) {
3700 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003701 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003702 }
3703
Joel Becker178eeac2008-10-27 15:18:29 -07003704 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
3705 OCFS2_JOURNAL_ACCESS_CREATE);
Joel Beckerbd60bd32008-10-20 18:25:56 -07003706 if (ret) {
3707 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003708 goto out;
Joel Beckerbd60bd32008-10-20 18:25:56 -07003709 }
Tao Ma01225592008-08-18 17:38:53 +08003710
Joel Becker178eeac2008-10-27 15:18:29 -07003711 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
3712 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
3713
3714 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
3715
Tao Ma01225592008-08-18 17:38:53 +08003716 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
3717 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
3718 offsetof(struct ocfs2_xattr_block, xb_attrs));
3719
3720 xr = &xb->xb_attrs.xb_root;
3721 xr->xt_clusters = cpu_to_le32(1);
3722 xr->xt_last_eb_blk = 0;
3723 xr->xt_list.l_tree_depth = 0;
3724 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
3725 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
3726
3727 xr->xt_list.l_recs[0].e_cpos = 0;
3728 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
3729 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
3730
3731 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
3732
Tao Ma85db90e2008-11-12 08:27:01 +08003733 ocfs2_journal_dirty(handle, xb_bh);
Tao Ma01225592008-08-18 17:38:53 +08003734
Tao Ma85db90e2008-11-12 08:27:01 +08003735out:
Tao Ma01225592008-08-18 17:38:53 +08003736 up_write(&oi->ip_alloc_sem);
3737
Tao Ma01225592008-08-18 17:38:53 +08003738 return ret;
3739}
3740
3741static int cmp_xe_offset(const void *a, const void *b)
3742{
3743 const struct ocfs2_xattr_entry *l = a, *r = b;
3744 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
3745 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
3746
3747 if (l_name_offset < r_name_offset)
3748 return 1;
3749 if (l_name_offset > r_name_offset)
3750 return -1;
3751 return 0;
3752}
3753
3754/*
3755 * defrag a xattr bucket if we find that the bucket has some
3756 * holes beteen name/value pairs.
3757 * We will move all the name/value pairs to the end of the bucket
3758 * so that we can spare some space for insertion.
3759 */
3760static int ocfs2_defrag_xattr_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08003761 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08003762 struct ocfs2_xattr_bucket *bucket)
3763{
3764 int ret, i;
3765 size_t end, offset, len, value_len;
3766 struct ocfs2_xattr_header *xh;
3767 char *entries, *buf, *bucket_buf = NULL;
Joel Becker9c7759a2008-10-24 16:21:03 -07003768 u64 blkno = bucket_blkno(bucket);
Tao Ma01225592008-08-18 17:38:53 +08003769 u16 xh_free_start;
Tao Ma01225592008-08-18 17:38:53 +08003770 size_t blocksize = inode->i_sb->s_blocksize;
Tao Ma01225592008-08-18 17:38:53 +08003771 struct ocfs2_xattr_entry *xe;
Tao Ma01225592008-08-18 17:38:53 +08003772
3773 /*
3774 * In order to make the operation more efficient and generic,
3775 * we copy all the blocks into a contiguous memory and do the
3776 * defragment there, so if anything is error, we will not touch
3777 * the real block.
3778 */
3779 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
3780 if (!bucket_buf) {
3781 ret = -EIO;
3782 goto out;
3783 }
3784
Joel Becker161d6f32008-10-27 15:25:18 -07003785 buf = bucket_buf;
Tao Ma1c32a2f2008-11-06 08:10:47 +08003786 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
3787 memcpy(buf, bucket_block(bucket, i), blocksize);
Joel Becker161d6f32008-10-27 15:25:18 -07003788
Tao Ma1c32a2f2008-11-06 08:10:47 +08003789 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
Joel Becker161d6f32008-10-27 15:25:18 -07003790 OCFS2_JOURNAL_ACCESS_WRITE);
3791 if (ret < 0) {
3792 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003793 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003794 }
3795
3796 xh = (struct ocfs2_xattr_header *)bucket_buf;
3797 entries = (char *)xh->xh_entries;
3798 xh_free_start = le16_to_cpu(xh->xh_free_start);
3799
3800 mlog(0, "adjust xattr bucket in %llu, count = %u, "
3801 "xh_free_start = %u, xh_name_value_len = %u.\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003802 (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
3803 xh_free_start, le16_to_cpu(xh->xh_name_value_len));
Tao Ma01225592008-08-18 17:38:53 +08003804
3805 /*
3806 * sort all the entries by their offset.
3807 * the largest will be the first, so that we can
3808 * move them to the end one by one.
3809 */
3810 sort(entries, le16_to_cpu(xh->xh_count),
3811 sizeof(struct ocfs2_xattr_entry),
3812 cmp_xe_offset, swap_xe);
3813
3814 /* Move all name/values to the end of the bucket. */
3815 xe = xh->xh_entries;
3816 end = OCFS2_XATTR_BUCKET_SIZE;
3817 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
3818 offset = le16_to_cpu(xe->xe_name_offset);
3819 if (ocfs2_xattr_is_local(xe))
3820 value_len = OCFS2_XATTR_SIZE(
3821 le64_to_cpu(xe->xe_value_size));
3822 else
3823 value_len = OCFS2_XATTR_ROOT_SIZE;
3824 len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
3825
3826 /*
3827 * We must make sure that the name/value pair
3828 * exist in the same block. So adjust end to
3829 * the previous block end if needed.
3830 */
3831 if (((end - len) / blocksize !=
3832 (end - 1) / blocksize))
3833 end = end - end % blocksize;
3834
3835 if (end > offset + len) {
3836 memmove(bucket_buf + end - len,
3837 bucket_buf + offset, len);
3838 xe->xe_name_offset = cpu_to_le16(end - len);
3839 }
3840
3841 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
3842 "bucket %llu\n", (unsigned long long)blkno);
3843
3844 end -= len;
3845 }
3846
3847 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
3848 "bucket %llu\n", (unsigned long long)blkno);
3849
3850 if (xh_free_start == end)
Tao Ma85db90e2008-11-12 08:27:01 +08003851 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003852
3853 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
3854 xh->xh_free_start = cpu_to_le16(end);
3855
3856 /* sort the entries by their name_hash. */
3857 sort(entries, le16_to_cpu(xh->xh_count),
3858 sizeof(struct ocfs2_xattr_entry),
3859 cmp_xe, swap_xe);
3860
3861 buf = bucket_buf;
Tao Ma1c32a2f2008-11-06 08:10:47 +08003862 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
3863 memcpy(bucket_block(bucket, i), buf, blocksize);
3864 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
Tao Ma01225592008-08-18 17:38:53 +08003865
Tao Ma01225592008-08-18 17:38:53 +08003866out:
Tao Ma01225592008-08-18 17:38:53 +08003867 kfree(bucket_buf);
3868 return ret;
3869}
3870
3871/*
Joel Beckerb5c03e72008-11-25 19:58:16 -08003872 * prev_blkno points to the start of an existing extent. new_blkno
3873 * points to a newly allocated extent. Because we know each of our
3874 * clusters contains more than bucket, we can easily split one cluster
3875 * at a bucket boundary. So we take the last cluster of the existing
3876 * extent and split it down the middle. We move the last half of the
3877 * buckets in the last cluster of the existing extent over to the new
3878 * extent.
Tao Ma01225592008-08-18 17:38:53 +08003879 *
Joel Beckerb5c03e72008-11-25 19:58:16 -08003880 * first_bh is the buffer at prev_blkno so we can update the existing
3881 * extent's bucket count. header_bh is the bucket were we were hoping
3882 * to insert our xattr. If the bucket move places the target in the new
3883 * extent, we'll update first_bh and header_bh after modifying the old
3884 * extent.
3885 *
3886 * first_hash will be set as the 1st xe's name_hash in the new extent.
Tao Ma01225592008-08-18 17:38:53 +08003887 */
3888static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
3889 handle_t *handle,
Joel Becker41cb8142008-11-26 14:25:21 -08003890 struct ocfs2_xattr_bucket *first,
3891 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08003892 u64 new_blkno,
Tao Ma01225592008-08-18 17:38:53 +08003893 u32 num_clusters,
3894 u32 *first_hash)
3895{
Joel Beckerc58b6032008-11-26 13:36:24 -08003896 int ret;
Joel Becker41cb8142008-11-26 14:25:21 -08003897 struct super_block *sb = inode->i_sb;
3898 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb);
3899 int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
Joel Beckerb5c03e72008-11-25 19:58:16 -08003900 int to_move = num_buckets / 2;
Joel Beckerc58b6032008-11-26 13:36:24 -08003901 u64 src_blkno;
Joel Becker41cb8142008-11-26 14:25:21 -08003902 u64 last_cluster_blkno = bucket_blkno(first) +
3903 ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1));
Tao Ma01225592008-08-18 17:38:53 +08003904
Joel Becker41cb8142008-11-26 14:25:21 -08003905 BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets);
3906 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize);
Tao Ma01225592008-08-18 17:38:53 +08003907
Tao Ma01225592008-08-18 17:38:53 +08003908 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
Joel Beckerc58b6032008-11-26 13:36:24 -08003909 (unsigned long long)last_cluster_blkno, (unsigned long long)new_blkno);
Tao Ma01225592008-08-18 17:38:53 +08003910
Joel Becker41cb8142008-11-26 14:25:21 -08003911 ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first),
Joel Beckerc58b6032008-11-26 13:36:24 -08003912 last_cluster_blkno, new_blkno,
3913 to_move, first_hash);
Joel Beckerb5c03e72008-11-25 19:58:16 -08003914 if (ret) {
3915 mlog_errno(ret);
3916 goto out;
3917 }
3918
Joel Beckerc58b6032008-11-26 13:36:24 -08003919 /* This is the first bucket that got moved */
3920 src_blkno = last_cluster_blkno + (to_move * blks_per_bucket);
3921
Tao Ma01225592008-08-18 17:38:53 +08003922 /*
Joel Beckerc58b6032008-11-26 13:36:24 -08003923 * If the target bucket was part of the moved buckets, we need to
Joel Becker41cb8142008-11-26 14:25:21 -08003924 * update first and target.
Joel Beckerb5c03e72008-11-25 19:58:16 -08003925 */
Joel Becker41cb8142008-11-26 14:25:21 -08003926 if (bucket_blkno(target) >= src_blkno) {
Joel Beckerb5c03e72008-11-25 19:58:16 -08003927 /* Find the block for the new target bucket */
3928 src_blkno = new_blkno +
Joel Becker41cb8142008-11-26 14:25:21 -08003929 (bucket_blkno(target) - src_blkno);
3930
3931 ocfs2_xattr_bucket_relse(first);
3932 ocfs2_xattr_bucket_relse(target);
Joel Beckerb5c03e72008-11-25 19:58:16 -08003933
3934 /*
Joel Beckerc58b6032008-11-26 13:36:24 -08003935 * These shouldn't fail - the buffers are in the
Joel Beckerb5c03e72008-11-25 19:58:16 -08003936 * journal from ocfs2_cp_xattr_bucket().
3937 */
Joel Becker41cb8142008-11-26 14:25:21 -08003938 ret = ocfs2_read_xattr_bucket(first, new_blkno);
Joel Beckerc58b6032008-11-26 13:36:24 -08003939 if (ret) {
3940 mlog_errno(ret);
3941 goto out;
3942 }
Joel Becker41cb8142008-11-26 14:25:21 -08003943 ret = ocfs2_read_xattr_bucket(target, src_blkno);
3944 if (ret)
Joel Beckerb5c03e72008-11-25 19:58:16 -08003945 mlog_errno(ret);
Joel Beckerb5c03e72008-11-25 19:58:16 -08003946
Joel Beckerb5c03e72008-11-25 19:58:16 -08003947 }
3948
Tao Ma01225592008-08-18 17:38:53 +08003949out:
Tao Ma01225592008-08-18 17:38:53 +08003950 return ret;
3951}
3952
Tao Ma01225592008-08-18 17:38:53 +08003953/*
Tao Ma80bcaf32008-10-27 06:06:24 +08003954 * Find the suitable pos when we divide a bucket into 2.
3955 * We have to make sure the xattrs with the same hash value exist
3956 * in the same bucket.
3957 *
3958 * If this ocfs2_xattr_header covers more than one hash value, find a
3959 * place where the hash value changes. Try to find the most even split.
3960 * The most common case is that all entries have different hash values,
3961 * and the first check we make will find a place to split.
Tao Ma01225592008-08-18 17:38:53 +08003962 */
Tao Ma80bcaf32008-10-27 06:06:24 +08003963static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
3964{
3965 struct ocfs2_xattr_entry *entries = xh->xh_entries;
3966 int count = le16_to_cpu(xh->xh_count);
3967 int delta, middle = count / 2;
3968
3969 /*
3970 * We start at the middle. Each step gets farther away in both
3971 * directions. We therefore hit the change in hash value
3972 * nearest to the middle. Note that this loop does not execute for
3973 * count < 2.
3974 */
3975 for (delta = 0; delta < middle; delta++) {
3976 /* Let's check delta earlier than middle */
3977 if (cmp_xe(&entries[middle - delta - 1],
3978 &entries[middle - delta]))
3979 return middle - delta;
3980
3981 /* For even counts, don't walk off the end */
3982 if ((middle + delta + 1) == count)
3983 continue;
3984
3985 /* Now try delta past middle */
3986 if (cmp_xe(&entries[middle + delta],
3987 &entries[middle + delta + 1]))
3988 return middle + delta + 1;
3989 }
3990
3991 /* Every entry had the same hash */
3992 return count;
3993}
3994
3995/*
3996 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
3997 * first_hash will record the 1st hash of the new bucket.
3998 *
3999 * Normally half of the xattrs will be moved. But we have to make
4000 * sure that the xattrs with the same hash value are stored in the
4001 * same bucket. If all the xattrs in this bucket have the same hash
4002 * value, the new bucket will be initialized as an empty one and the
4003 * first_hash will be initialized as (hash_value+1).
4004 */
4005static int ocfs2_divide_xattr_bucket(struct inode *inode,
4006 handle_t *handle,
4007 u64 blk,
4008 u64 new_blk,
4009 u32 *first_hash,
4010 int new_bucket_head)
Tao Ma01225592008-08-18 17:38:53 +08004011{
4012 int ret, i;
Tao Ma80bcaf32008-10-27 06:06:24 +08004013 int count, start, len, name_value_len = 0, xe_len, name_offset = 0;
Joel Beckerba937122008-10-24 19:13:20 -07004014 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
Tao Ma01225592008-08-18 17:38:53 +08004015 struct ocfs2_xattr_header *xh;
4016 struct ocfs2_xattr_entry *xe;
4017 int blocksize = inode->i_sb->s_blocksize;
4018
Tao Ma80bcaf32008-10-27 06:06:24 +08004019 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07004020 (unsigned long long)blk, (unsigned long long)new_blk);
Tao Ma01225592008-08-18 17:38:53 +08004021
Joel Beckerba937122008-10-24 19:13:20 -07004022 s_bucket = ocfs2_xattr_bucket_new(inode);
4023 t_bucket = ocfs2_xattr_bucket_new(inode);
4024 if (!s_bucket || !t_bucket) {
4025 ret = -ENOMEM;
4026 mlog_errno(ret);
4027 goto out;
4028 }
Tao Ma01225592008-08-18 17:38:53 +08004029
Joel Beckerba937122008-10-24 19:13:20 -07004030 ret = ocfs2_read_xattr_bucket(s_bucket, blk);
Tao Ma01225592008-08-18 17:38:53 +08004031 if (ret) {
4032 mlog_errno(ret);
4033 goto out;
4034 }
4035
Joel Beckerba937122008-10-24 19:13:20 -07004036 ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004037 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004038 if (ret) {
4039 mlog_errno(ret);
4040 goto out;
4041 }
4042
Joel Becker784b8162008-10-24 17:33:40 -07004043 /*
4044 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
4045 * there's no need to read it.
4046 */
Joel Beckerba937122008-10-24 19:13:20 -07004047 ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
Tao Ma01225592008-08-18 17:38:53 +08004048 if (ret) {
4049 mlog_errno(ret);
4050 goto out;
4051 }
4052
Joel Becker2b656c12008-11-25 19:00:15 -08004053 /*
4054 * Hey, if we're overwriting t_bucket, what difference does
4055 * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the
4056 * same part of ocfs2_cp_xattr_bucket().
4057 */
Joel Beckerba937122008-10-24 19:13:20 -07004058 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004059 new_bucket_head ?
4060 OCFS2_JOURNAL_ACCESS_CREATE :
4061 OCFS2_JOURNAL_ACCESS_WRITE);
4062 if (ret) {
4063 mlog_errno(ret);
4064 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004065 }
4066
Joel Beckerba937122008-10-24 19:13:20 -07004067 xh = bucket_xh(s_bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08004068 count = le16_to_cpu(xh->xh_count);
4069 start = ocfs2_xattr_find_divide_pos(xh);
4070
4071 if (start == count) {
4072 xe = &xh->xh_entries[start-1];
4073
4074 /*
4075 * initialized a new empty bucket here.
4076 * The hash value is set as one larger than
4077 * that of the last entry in the previous bucket.
4078 */
Joel Beckerba937122008-10-24 19:13:20 -07004079 for (i = 0; i < t_bucket->bu_blocks; i++)
4080 memset(bucket_block(t_bucket, i), 0, blocksize);
Tao Ma80bcaf32008-10-27 06:06:24 +08004081
Joel Beckerba937122008-10-24 19:13:20 -07004082 xh = bucket_xh(t_bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08004083 xh->xh_free_start = cpu_to_le16(blocksize);
4084 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
4085 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
4086
4087 goto set_num_buckets;
4088 }
4089
Tao Ma01225592008-08-18 17:38:53 +08004090 /* copy the whole bucket to the new first. */
Joel Beckerba937122008-10-24 19:13:20 -07004091 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004092
4093 /* update the new bucket. */
Joel Beckerba937122008-10-24 19:13:20 -07004094 xh = bucket_xh(t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004095
4096 /*
4097 * Calculate the total name/value len and xh_free_start for
4098 * the old bucket first.
4099 */
4100 name_offset = OCFS2_XATTR_BUCKET_SIZE;
4101 name_value_len = 0;
4102 for (i = 0; i < start; i++) {
4103 xe = &xh->xh_entries[i];
4104 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
4105 if (ocfs2_xattr_is_local(xe))
4106 xe_len +=
4107 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4108 else
4109 xe_len += OCFS2_XATTR_ROOT_SIZE;
4110 name_value_len += xe_len;
4111 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
4112 name_offset = le16_to_cpu(xe->xe_name_offset);
4113 }
4114
4115 /*
4116 * Now begin the modification to the new bucket.
4117 *
4118 * In the new bucket, We just move the xattr entry to the beginning
4119 * and don't touch the name/value. So there will be some holes in the
4120 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
4121 * called.
4122 */
4123 xe = &xh->xh_entries[start];
4124 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
4125 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
Mark Fashehff1ec202008-08-19 10:54:29 -07004126 (int)((char *)xe - (char *)xh),
4127 (int)((char *)xh->xh_entries - (char *)xh));
Tao Ma01225592008-08-18 17:38:53 +08004128 memmove((char *)xh->xh_entries, (char *)xe, len);
4129 xe = &xh->xh_entries[count - start];
4130 len = sizeof(struct ocfs2_xattr_entry) * start;
4131 memset((char *)xe, 0, len);
4132
4133 le16_add_cpu(&xh->xh_count, -start);
4134 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
4135
4136 /* Calculate xh_free_start for the new bucket. */
4137 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4138 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4139 xe = &xh->xh_entries[i];
4140 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
4141 if (ocfs2_xattr_is_local(xe))
4142 xe_len +=
4143 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4144 else
4145 xe_len += OCFS2_XATTR_ROOT_SIZE;
4146 if (le16_to_cpu(xe->xe_name_offset) <
4147 le16_to_cpu(xh->xh_free_start))
4148 xh->xh_free_start = xe->xe_name_offset;
4149 }
4150
Tao Ma80bcaf32008-10-27 06:06:24 +08004151set_num_buckets:
Tao Ma01225592008-08-18 17:38:53 +08004152 /* set xh->xh_num_buckets for the new xh. */
4153 if (new_bucket_head)
4154 xh->xh_num_buckets = cpu_to_le16(1);
4155 else
4156 xh->xh_num_buckets = 0;
4157
Joel Beckerba937122008-10-24 19:13:20 -07004158 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004159
4160 /* store the first_hash of the new bucket. */
4161 if (first_hash)
4162 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
4163
4164 /*
Tao Ma80bcaf32008-10-27 06:06:24 +08004165 * Now only update the 1st block of the old bucket. If we
4166 * just added a new empty bucket, there is no need to modify
4167 * it.
Tao Ma01225592008-08-18 17:38:53 +08004168 */
Tao Ma80bcaf32008-10-27 06:06:24 +08004169 if (start == count)
4170 goto out;
4171
Joel Beckerba937122008-10-24 19:13:20 -07004172 xh = bucket_xh(s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004173 memset(&xh->xh_entries[start], 0,
4174 sizeof(struct ocfs2_xattr_entry) * (count - start));
4175 xh->xh_count = cpu_to_le16(start);
4176 xh->xh_free_start = cpu_to_le16(name_offset);
4177 xh->xh_name_value_len = cpu_to_le16(name_value_len);
4178
Joel Beckerba937122008-10-24 19:13:20 -07004179 ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004180
4181out:
Joel Beckerba937122008-10-24 19:13:20 -07004182 ocfs2_xattr_bucket_free(s_bucket);
4183 ocfs2_xattr_bucket_free(t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004184
4185 return ret;
4186}
4187
4188/*
4189 * Copy xattr from one bucket to another bucket.
4190 *
4191 * The caller must make sure that the journal transaction
4192 * has enough space for journaling.
4193 */
4194static int ocfs2_cp_xattr_bucket(struct inode *inode,
4195 handle_t *handle,
4196 u64 s_blkno,
4197 u64 t_blkno,
4198 int t_is_new)
4199{
Joel Becker4980c6d2008-10-24 18:54:43 -07004200 int ret;
Joel Beckerba937122008-10-24 19:13:20 -07004201 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
Tao Ma01225592008-08-18 17:38:53 +08004202
4203 BUG_ON(s_blkno == t_blkno);
4204
4205 mlog(0, "cp bucket %llu to %llu, target is %d\n",
Mark Fashehde29c082008-10-29 14:45:30 -07004206 (unsigned long long)s_blkno, (unsigned long long)t_blkno,
4207 t_is_new);
Tao Ma01225592008-08-18 17:38:53 +08004208
Joel Beckerba937122008-10-24 19:13:20 -07004209 s_bucket = ocfs2_xattr_bucket_new(inode);
4210 t_bucket = ocfs2_xattr_bucket_new(inode);
4211 if (!s_bucket || !t_bucket) {
4212 ret = -ENOMEM;
4213 mlog_errno(ret);
4214 goto out;
4215 }
Joel Becker92de1092008-11-25 17:06:40 -08004216
Joel Beckerba937122008-10-24 19:13:20 -07004217 ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
Tao Ma01225592008-08-18 17:38:53 +08004218 if (ret)
4219 goto out;
4220
Joel Becker784b8162008-10-24 17:33:40 -07004221 /*
4222 * Even if !t_is_new, we're overwriting t_bucket. Thus,
4223 * there's no need to read it.
4224 */
Joel Beckerba937122008-10-24 19:13:20 -07004225 ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
Tao Ma01225592008-08-18 17:38:53 +08004226 if (ret)
4227 goto out;
4228
Joel Becker2b656c12008-11-25 19:00:15 -08004229 /*
4230 * Hey, if we're overwriting t_bucket, what difference does
4231 * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new
Joel Becker874d65a2008-11-26 13:02:18 -08004232 * cluster to fill, we came here from
4233 * ocfs2_mv_xattr_buckets(), and it is really new -
4234 * ACCESS_CREATE is required. But we also might have moved data
4235 * out of t_bucket before extending back into it.
4236 * ocfs2_add_new_xattr_bucket() can do this - its call to
4237 * ocfs2_add_new_xattr_cluster() may have created a new extent
Joel Becker2b656c12008-11-25 19:00:15 -08004238 * and copied out the end of the old extent. Then it re-extends
4239 * the old extent back to create space for new xattrs. That's
4240 * how we get here, and the bucket isn't really new.
4241 */
Joel Beckerba937122008-10-24 19:13:20 -07004242 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004243 t_is_new ?
4244 OCFS2_JOURNAL_ACCESS_CREATE :
4245 OCFS2_JOURNAL_ACCESS_WRITE);
4246 if (ret)
4247 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004248
Joel Beckerba937122008-10-24 19:13:20 -07004249 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4250 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004251
4252out:
Joel Beckerba937122008-10-24 19:13:20 -07004253 ocfs2_xattr_bucket_free(t_bucket);
4254 ocfs2_xattr_bucket_free(s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004255
4256 return ret;
4257}
4258
4259/*
Joel Becker874d65a2008-11-26 13:02:18 -08004260 * src_blk points to the start of an existing extent. last_blk points to
4261 * last cluster in that extent. to_blk points to a newly allocated
Joel Becker54ecb6b2008-11-26 13:18:31 -08004262 * extent. We copy the buckets from the cluster at last_blk to the new
4263 * extent. If start_bucket is non-zero, we skip that many buckets before
4264 * we start copying. The new extent's xh_num_buckets gets set to the
4265 * number of buckets we copied. The old extent's xh_num_buckets shrinks
4266 * by the same amount.
Tao Ma01225592008-08-18 17:38:53 +08004267 */
Joel Becker54ecb6b2008-11-26 13:18:31 -08004268static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
4269 u64 src_blk, u64 last_blk, u64 to_blk,
4270 unsigned int start_bucket,
4271 u32 *first_hash)
Tao Ma01225592008-08-18 17:38:53 +08004272{
4273 int i, ret, credits;
4274 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Joel Becker15d60922008-11-25 18:36:42 -08004275 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08004276 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
Joel Becker15d60922008-11-25 18:36:42 -08004277 struct ocfs2_xattr_bucket *old_first, *new_first;
Tao Ma01225592008-08-18 17:38:53 +08004278
Joel Becker874d65a2008-11-26 13:02:18 -08004279 mlog(0, "mv xattrs from cluster %llu to %llu\n",
4280 (unsigned long long)last_blk, (unsigned long long)to_blk);
Tao Ma01225592008-08-18 17:38:53 +08004281
Joel Becker54ecb6b2008-11-26 13:18:31 -08004282 BUG_ON(start_bucket >= num_buckets);
4283 if (start_bucket) {
4284 num_buckets -= start_bucket;
4285 last_blk += (start_bucket * blks_per_bucket);
4286 }
4287
Joel Becker15d60922008-11-25 18:36:42 -08004288 /* The first bucket of the original extent */
4289 old_first = ocfs2_xattr_bucket_new(inode);
4290 /* The first bucket of the new extent */
4291 new_first = ocfs2_xattr_bucket_new(inode);
4292 if (!old_first || !new_first) {
4293 ret = -ENOMEM;
4294 mlog_errno(ret);
4295 goto out;
4296 }
4297
Joel Becker874d65a2008-11-26 13:02:18 -08004298 ret = ocfs2_read_xattr_bucket(old_first, src_blk);
Joel Becker15d60922008-11-25 18:36:42 -08004299 if (ret) {
4300 mlog_errno(ret);
4301 goto out;
4302 }
4303
Tao Ma01225592008-08-18 17:38:53 +08004304 /*
Joel Becker54ecb6b2008-11-26 13:18:31 -08004305 * We need to update the first bucket of the old extent and all
4306 * the buckets going to the new extent.
Tao Ma01225592008-08-18 17:38:53 +08004307 */
Joel Becker54ecb6b2008-11-26 13:18:31 -08004308 credits = ((num_buckets + 1) * blks_per_bucket) +
4309 handle->h_buffer_credits;
Tao Ma01225592008-08-18 17:38:53 +08004310 ret = ocfs2_extend_trans(handle, credits);
4311 if (ret) {
4312 mlog_errno(ret);
4313 goto out;
4314 }
4315
Joel Becker15d60922008-11-25 18:36:42 -08004316 ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
4317 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004318 if (ret) {
4319 mlog_errno(ret);
4320 goto out;
4321 }
4322
4323 for (i = 0; i < num_buckets; i++) {
4324 ret = ocfs2_cp_xattr_bucket(inode, handle,
Joel Becker874d65a2008-11-26 13:02:18 -08004325 last_blk + (i * blks_per_bucket),
Joel Becker15d60922008-11-25 18:36:42 -08004326 to_blk + (i * blks_per_bucket),
4327 1);
Tao Ma01225592008-08-18 17:38:53 +08004328 if (ret) {
4329 mlog_errno(ret);
4330 goto out;
4331 }
Tao Ma01225592008-08-18 17:38:53 +08004332 }
4333
Joel Becker15d60922008-11-25 18:36:42 -08004334 /*
4335 * Get the new bucket ready before we dirty anything
4336 * (This actually shouldn't fail, because we already dirtied
4337 * it once in ocfs2_cp_xattr_bucket()).
4338 */
4339 ret = ocfs2_read_xattr_bucket(new_first, to_blk);
4340 if (ret) {
Tao Ma01225592008-08-18 17:38:53 +08004341 mlog_errno(ret);
4342 goto out;
4343 }
Joel Becker15d60922008-11-25 18:36:42 -08004344 ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
4345 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004346 if (ret) {
4347 mlog_errno(ret);
4348 goto out;
4349 }
4350
Joel Becker15d60922008-11-25 18:36:42 -08004351 /* Now update the headers */
4352 le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets);
4353 ocfs2_xattr_bucket_journal_dirty(handle, old_first);
Tao Ma01225592008-08-18 17:38:53 +08004354
Joel Becker15d60922008-11-25 18:36:42 -08004355 bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets);
4356 ocfs2_xattr_bucket_journal_dirty(handle, new_first);
Tao Ma01225592008-08-18 17:38:53 +08004357
4358 if (first_hash)
Joel Becker15d60922008-11-25 18:36:42 -08004359 *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash);
4360
Tao Ma01225592008-08-18 17:38:53 +08004361out:
Joel Becker15d60922008-11-25 18:36:42 -08004362 ocfs2_xattr_bucket_free(new_first);
4363 ocfs2_xattr_bucket_free(old_first);
Tao Ma01225592008-08-18 17:38:53 +08004364 return ret;
4365}
4366
4367/*
Tao Ma80bcaf32008-10-27 06:06:24 +08004368 * Move some xattrs in this cluster to the new cluster.
Tao Ma01225592008-08-18 17:38:53 +08004369 * This function should only be called when bucket size == cluster size.
4370 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
4371 */
Tao Ma80bcaf32008-10-27 06:06:24 +08004372static int ocfs2_divide_xattr_cluster(struct inode *inode,
4373 handle_t *handle,
4374 u64 prev_blk,
4375 u64 new_blk,
4376 u32 *first_hash)
Tao Ma01225592008-08-18 17:38:53 +08004377{
4378 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08004379 int ret, credits = 2 * blk_per_bucket + handle->h_buffer_credits;
Tao Ma01225592008-08-18 17:38:53 +08004380
4381 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
4382
4383 ret = ocfs2_extend_trans(handle, credits);
4384 if (ret) {
4385 mlog_errno(ret);
4386 return ret;
4387 }
4388
4389 /* Move half of the xattr in start_blk to the next bucket. */
Tao Ma80bcaf32008-10-27 06:06:24 +08004390 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
4391 new_blk, first_hash, 1);
Tao Ma01225592008-08-18 17:38:53 +08004392}
4393
4394/*
4395 * Move some xattrs from the old cluster to the new one since they are not
4396 * contiguous in ocfs2 xattr tree.
4397 *
4398 * new_blk starts a new separate cluster, and we will move some xattrs from
4399 * prev_blk to it. v_start will be set as the first name hash value in this
4400 * new cluster so that it can be used as e_cpos during tree insertion and
4401 * don't collide with our original b-tree operations. first_bh and header_bh
4402 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
4403 * to extend the insert bucket.
4404 *
4405 * The problem is how much xattr should we move to the new one and when should
4406 * we update first_bh and header_bh?
4407 * 1. If cluster size > bucket size, that means the previous cluster has more
4408 * than 1 bucket, so just move half nums of bucket into the new cluster and
4409 * update the first_bh and header_bh if the insert bucket has been moved
4410 * to the new cluster.
4411 * 2. If cluster_size == bucket_size:
4412 * a) If the previous extent rec has more than one cluster and the insert
4413 * place isn't in the last cluster, copy the entire last cluster to the
4414 * new one. This time, we don't need to upate the first_bh and header_bh
4415 * since they will not be moved into the new cluster.
4416 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
4417 * the new one. And we set the extend flag to zero if the insert place is
4418 * moved into the new allocated cluster since no extend is needed.
4419 */
4420static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
4421 handle_t *handle,
Joel Becker012ee912008-11-26 14:43:31 -08004422 struct ocfs2_xattr_bucket *first,
4423 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08004424 u64 new_blk,
Tao Ma01225592008-08-18 17:38:53 +08004425 u32 prev_clusters,
4426 u32 *v_start,
4427 int *extend)
4428{
Joel Becker92cf3ad2008-11-26 14:12:09 -08004429 int ret;
Tao Ma01225592008-08-18 17:38:53 +08004430
4431 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
Joel Becker012ee912008-11-26 14:43:31 -08004432 (unsigned long long)bucket_blkno(first), prev_clusters,
Mark Fashehde29c082008-10-29 14:45:30 -07004433 (unsigned long long)new_blk);
Tao Ma01225592008-08-18 17:38:53 +08004434
Joel Becker41cb8142008-11-26 14:25:21 -08004435 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) {
Tao Ma01225592008-08-18 17:38:53 +08004436 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
4437 handle,
Joel Becker41cb8142008-11-26 14:25:21 -08004438 first, target,
Tao Ma01225592008-08-18 17:38:53 +08004439 new_blk,
Tao Ma01225592008-08-18 17:38:53 +08004440 prev_clusters,
4441 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08004442 if (ret)
Joel Becker41cb8142008-11-26 14:25:21 -08004443 mlog_errno(ret);
Joel Becker41cb8142008-11-26 14:25:21 -08004444 } else {
Joel Becker92cf3ad2008-11-26 14:12:09 -08004445 /* The start of the last cluster in the first extent */
4446 u64 last_blk = bucket_blkno(first) +
4447 ((prev_clusters - 1) *
4448 ocfs2_clusters_to_blocks(inode->i_sb, 1));
Tao Ma01225592008-08-18 17:38:53 +08004449
Joel Becker012ee912008-11-26 14:43:31 -08004450 if (prev_clusters > 1 && bucket_blkno(target) != last_blk) {
Joel Becker874d65a2008-11-26 13:02:18 -08004451 ret = ocfs2_mv_xattr_buckets(inode, handle,
Joel Becker92cf3ad2008-11-26 14:12:09 -08004452 bucket_blkno(first),
Joel Becker54ecb6b2008-11-26 13:18:31 -08004453 last_blk, new_blk, 0,
Tao Ma01225592008-08-18 17:38:53 +08004454 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08004455 if (ret)
4456 mlog_errno(ret);
4457 } else {
Tao Ma80bcaf32008-10-27 06:06:24 +08004458 ret = ocfs2_divide_xattr_cluster(inode, handle,
4459 last_blk, new_blk,
4460 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08004461 if (ret)
4462 mlog_errno(ret);
Tao Ma01225592008-08-18 17:38:53 +08004463
Joel Becker92cf3ad2008-11-26 14:12:09 -08004464 if ((bucket_blkno(target) == last_blk) && extend)
Tao Ma01225592008-08-18 17:38:53 +08004465 *extend = 0;
4466 }
4467 }
4468
4469 return ret;
4470}
4471
4472/*
4473 * Add a new cluster for xattr storage.
4474 *
4475 * If the new cluster is contiguous with the previous one, it will be
4476 * appended to the same extent record, and num_clusters will be updated.
4477 * If not, we will insert a new extent for it and move some xattrs in
4478 * the last cluster into the new allocated one.
4479 * We also need to limit the maximum size of a btree leaf, otherwise we'll
4480 * lose the benefits of hashing because we'll have to search large leaves.
4481 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
4482 * if it's bigger).
4483 *
4484 * first_bh is the first block of the previous extent rec and header_bh
4485 * indicates the bucket we will insert the new xattrs. They will be updated
4486 * when the header_bh is moved into the new cluster.
4487 */
4488static int ocfs2_add_new_xattr_cluster(struct inode *inode,
4489 struct buffer_head *root_bh,
Joel Beckered29c0c2008-11-26 15:08:44 -08004490 struct ocfs2_xattr_bucket *first,
4491 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08004492 u32 *num_clusters,
4493 u32 prev_cpos,
Tao Ma78f30c32008-11-12 08:27:00 +08004494 int *extend,
4495 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004496{
Tao Ma85db90e2008-11-12 08:27:01 +08004497 int ret;
Tao Ma01225592008-08-18 17:38:53 +08004498 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
4499 u32 prev_clusters = *num_clusters;
4500 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
4501 u64 block;
Tao Ma85db90e2008-11-12 08:27:01 +08004502 handle_t *handle = ctxt->handle;
Tao Ma01225592008-08-18 17:38:53 +08004503 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004504 struct ocfs2_extent_tree et;
Tao Ma01225592008-08-18 17:38:53 +08004505
4506 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
4507 "previous xattr blkno = %llu\n",
4508 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Joel Beckered29c0c2008-11-26 15:08:44 -08004509 prev_cpos, (unsigned long long)bucket_blkno(first));
Tao Ma01225592008-08-18 17:38:53 +08004510
Joel Becker5e404e92009-02-13 03:54:22 -08004511 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004512
Joel Becker0cf2f762009-02-12 16:41:25 -08004513 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
Joel Becker84008972008-12-09 16:11:49 -08004514 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004515 if (ret < 0) {
4516 mlog_errno(ret);
4517 goto leave;
4518 }
4519
Tao Ma78f30c32008-11-12 08:27:00 +08004520 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, 1,
Tao Ma01225592008-08-18 17:38:53 +08004521 clusters_to_add, &bit_off, &num_bits);
4522 if (ret < 0) {
4523 if (ret != -ENOSPC)
4524 mlog_errno(ret);
4525 goto leave;
4526 }
4527
4528 BUG_ON(num_bits > clusters_to_add);
4529
4530 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4531 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
4532 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4533
Joel Beckered29c0c2008-11-26 15:08:44 -08004534 if (bucket_blkno(first) + (prev_clusters * bpc) == block &&
Tao Ma01225592008-08-18 17:38:53 +08004535 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
4536 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
4537 /*
4538 * If this cluster is contiguous with the old one and
4539 * adding this new cluster, we don't surpass the limit of
4540 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
4541 * initialized and used like other buckets in the previous
4542 * cluster.
4543 * So add it as a contiguous one. The caller will handle
4544 * its init process.
4545 */
4546 v_start = prev_cpos + prev_clusters;
4547 *num_clusters = prev_clusters + num_bits;
4548 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
4549 num_bits);
4550 } else {
4551 ret = ocfs2_adjust_xattr_cross_cluster(inode,
4552 handle,
Joel Becker012ee912008-11-26 14:43:31 -08004553 first,
4554 target,
Tao Ma01225592008-08-18 17:38:53 +08004555 block,
Tao Ma01225592008-08-18 17:38:53 +08004556 prev_clusters,
4557 &v_start,
4558 extend);
4559 if (ret) {
4560 mlog_errno(ret);
4561 goto leave;
4562 }
4563 }
4564
4565 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
Mark Fashehde29c082008-10-29 14:45:30 -07004566 num_bits, (unsigned long long)block, v_start);
Joel Beckercc79d8c2009-02-13 03:24:43 -08004567 ret = ocfs2_insert_extent(handle, &et, v_start, block,
Tao Ma78f30c32008-11-12 08:27:00 +08004568 num_bits, 0, ctxt->meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08004569 if (ret < 0) {
4570 mlog_errno(ret);
4571 goto leave;
4572 }
4573
4574 ret = ocfs2_journal_dirty(handle, root_bh);
Tao Ma85db90e2008-11-12 08:27:01 +08004575 if (ret < 0)
Tao Ma01225592008-08-18 17:38:53 +08004576 mlog_errno(ret);
Tao Ma01225592008-08-18 17:38:53 +08004577
4578leave:
Tao Ma01225592008-08-18 17:38:53 +08004579 return ret;
4580}
4581
4582/*
Joel Becker92de1092008-11-25 17:06:40 -08004583 * We are given an extent. 'first' is the bucket at the very front of
4584 * the extent. The extent has space for an additional bucket past
4585 * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
4586 * of the target bucket. We wish to shift every bucket past the target
4587 * down one, filling in that additional space. When we get back to the
4588 * target, we split the target between itself and the now-empty bucket
4589 * at target+1 (aka, target_blkno + blks_per_bucket).
Tao Ma01225592008-08-18 17:38:53 +08004590 */
4591static int ocfs2_extend_xattr_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004592 handle_t *handle,
Joel Becker92de1092008-11-25 17:06:40 -08004593 struct ocfs2_xattr_bucket *first,
4594 u64 target_blk,
Tao Ma01225592008-08-18 17:38:53 +08004595 u32 num_clusters)
4596{
4597 int ret, credits;
4598 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4599 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Joel Becker92de1092008-11-25 17:06:40 -08004600 u64 end_blk;
4601 u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets);
Tao Ma01225592008-08-18 17:38:53 +08004602
4603 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
Joel Becker92de1092008-11-25 17:06:40 -08004604 "from %llu, len = %u\n", (unsigned long long)target_blk,
4605 (unsigned long long)bucket_blkno(first), num_clusters);
Tao Ma01225592008-08-18 17:38:53 +08004606
Joel Becker92de1092008-11-25 17:06:40 -08004607 /* The extent must have room for an additional bucket */
4608 BUG_ON(new_bucket >=
4609 (num_clusters * ocfs2_xattr_buckets_per_cluster(osb)));
Tao Ma01225592008-08-18 17:38:53 +08004610
Joel Becker92de1092008-11-25 17:06:40 -08004611 /* end_blk points to the last existing bucket */
4612 end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004613
4614 /*
Joel Becker92de1092008-11-25 17:06:40 -08004615 * end_blk is the start of the last existing bucket.
4616 * Thus, (end_blk - target_blk) covers the target bucket and
4617 * every bucket after it up to, but not including, the last
4618 * existing bucket. Then we add the last existing bucket, the
4619 * new bucket, and the first bucket (3 * blk_per_bucket).
Tao Ma01225592008-08-18 17:38:53 +08004620 */
Joel Becker92de1092008-11-25 17:06:40 -08004621 credits = (end_blk - target_blk) + (3 * blk_per_bucket) +
Tao Ma85db90e2008-11-12 08:27:01 +08004622 handle->h_buffer_credits;
4623 ret = ocfs2_extend_trans(handle, credits);
4624 if (ret) {
Tao Ma01225592008-08-18 17:38:53 +08004625 mlog_errno(ret);
4626 goto out;
4627 }
4628
Joel Becker92de1092008-11-25 17:06:40 -08004629 ret = ocfs2_xattr_bucket_journal_access(handle, first,
4630 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004631 if (ret) {
4632 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004633 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004634 }
4635
Joel Becker92de1092008-11-25 17:06:40 -08004636 while (end_blk != target_blk) {
Tao Ma01225592008-08-18 17:38:53 +08004637 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
4638 end_blk + blk_per_bucket, 0);
4639 if (ret)
Tao Ma85db90e2008-11-12 08:27:01 +08004640 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004641 end_blk -= blk_per_bucket;
4642 }
4643
Joel Becker92de1092008-11-25 17:06:40 -08004644 /* Move half of the xattr in target_blkno to the next bucket. */
4645 ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk,
4646 target_blk + blk_per_bucket, NULL, 0);
Tao Ma01225592008-08-18 17:38:53 +08004647
Joel Becker92de1092008-11-25 17:06:40 -08004648 le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1);
4649 ocfs2_xattr_bucket_journal_dirty(handle, first);
Tao Ma01225592008-08-18 17:38:53 +08004650
Tao Ma01225592008-08-18 17:38:53 +08004651out:
4652 return ret;
4653}
4654
4655/*
Joel Becker91f20332008-11-26 15:25:41 -08004656 * Add new xattr bucket in an extent record and adjust the buckets
4657 * accordingly. xb_bh is the ocfs2_xattr_block, and target is the
4658 * bucket we want to insert into.
Tao Ma01225592008-08-18 17:38:53 +08004659 *
Joel Becker91f20332008-11-26 15:25:41 -08004660 * In the easy case, we will move all the buckets after target down by
4661 * one. Half of target's xattrs will be moved to the next bucket.
4662 *
4663 * If current cluster is full, we'll allocate a new one. This may not
4664 * be contiguous. The underlying calls will make sure that there is
4665 * space for the insert, shifting buckets around if necessary.
4666 * 'target' may be moved by those calls.
Tao Ma01225592008-08-18 17:38:53 +08004667 */
4668static int ocfs2_add_new_xattr_bucket(struct inode *inode,
4669 struct buffer_head *xb_bh,
Joel Becker91f20332008-11-26 15:25:41 -08004670 struct ocfs2_xattr_bucket *target,
Tao Ma78f30c32008-11-12 08:27:00 +08004671 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004672{
Tao Ma01225592008-08-18 17:38:53 +08004673 struct ocfs2_xattr_block *xb =
4674 (struct ocfs2_xattr_block *)xb_bh->b_data;
4675 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
4676 struct ocfs2_extent_list *el = &xb_root->xt_list;
Joel Becker91f20332008-11-26 15:25:41 -08004677 u32 name_hash =
4678 le32_to_cpu(bucket_xh(target)->xh_entries[0].xe_name_hash);
Joel Beckered29c0c2008-11-26 15:08:44 -08004679 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08004680 int ret, num_buckets, extend = 1;
4681 u64 p_blkno;
4682 u32 e_cpos, num_clusters;
Joel Becker92de1092008-11-25 17:06:40 -08004683 /* The bucket at the front of the extent */
Joel Becker91f20332008-11-26 15:25:41 -08004684 struct ocfs2_xattr_bucket *first;
Tao Ma01225592008-08-18 17:38:53 +08004685
Joel Becker91f20332008-11-26 15:25:41 -08004686 mlog(0, "Add new xattr bucket starting from %llu\n",
4687 (unsigned long long)bucket_blkno(target));
Tao Ma01225592008-08-18 17:38:53 +08004688
Joel Beckered29c0c2008-11-26 15:08:44 -08004689 /* The first bucket of the original extent */
Joel Becker92de1092008-11-25 17:06:40 -08004690 first = ocfs2_xattr_bucket_new(inode);
Joel Becker91f20332008-11-26 15:25:41 -08004691 if (!first) {
Joel Becker92de1092008-11-25 17:06:40 -08004692 ret = -ENOMEM;
4693 mlog_errno(ret);
4694 goto out;
4695 }
4696
Tao Ma01225592008-08-18 17:38:53 +08004697 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
4698 &num_clusters, el);
4699 if (ret) {
4700 mlog_errno(ret);
4701 goto out;
4702 }
4703
Joel Beckered29c0c2008-11-26 15:08:44 -08004704 ret = ocfs2_read_xattr_bucket(first, p_blkno);
4705 if (ret) {
4706 mlog_errno(ret);
4707 goto out;
4708 }
4709
Tao Ma01225592008-08-18 17:38:53 +08004710 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
Joel Beckered29c0c2008-11-26 15:08:44 -08004711 if (num_buckets == le16_to_cpu(bucket_xh(first)->xh_num_buckets)) {
4712 /*
4713 * This can move first+target if the target bucket moves
4714 * to the new extent.
4715 */
Tao Ma01225592008-08-18 17:38:53 +08004716 ret = ocfs2_add_new_xattr_cluster(inode,
4717 xb_bh,
Joel Beckered29c0c2008-11-26 15:08:44 -08004718 first,
4719 target,
Tao Ma01225592008-08-18 17:38:53 +08004720 &num_clusters,
4721 e_cpos,
Tao Ma78f30c32008-11-12 08:27:00 +08004722 &extend,
4723 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004724 if (ret) {
4725 mlog_errno(ret);
4726 goto out;
4727 }
4728 }
4729
Joel Becker92de1092008-11-25 17:06:40 -08004730 if (extend) {
Tao Ma01225592008-08-18 17:38:53 +08004731 ret = ocfs2_extend_xattr_bucket(inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004732 ctxt->handle,
Joel Beckered29c0c2008-11-26 15:08:44 -08004733 first,
4734 bucket_blkno(target),
Tao Ma01225592008-08-18 17:38:53 +08004735 num_clusters);
Joel Becker92de1092008-11-25 17:06:40 -08004736 if (ret)
4737 mlog_errno(ret);
4738 }
4739
Tao Ma01225592008-08-18 17:38:53 +08004740out:
Joel Becker92de1092008-11-25 17:06:40 -08004741 ocfs2_xattr_bucket_free(first);
Joel Beckered29c0c2008-11-26 15:08:44 -08004742
Tao Ma01225592008-08-18 17:38:53 +08004743 return ret;
4744}
4745
4746static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
4747 struct ocfs2_xattr_bucket *bucket,
4748 int offs)
4749{
4750 int block_off = offs >> inode->i_sb->s_blocksize_bits;
4751
4752 offs = offs % inode->i_sb->s_blocksize;
Joel Becker51def392008-10-24 16:57:21 -07004753 return bucket_block(bucket, block_off) + offs;
Tao Ma01225592008-08-18 17:38:53 +08004754}
4755
4756/*
4757 * Handle the normal xattr set, including replace, delete and new.
Tao Ma01225592008-08-18 17:38:53 +08004758 *
4759 * Note: "local" indicates the real data's locality. So we can't
4760 * just its bucket locality by its length.
4761 */
4762static void ocfs2_xattr_set_entry_normal(struct inode *inode,
4763 struct ocfs2_xattr_info *xi,
4764 struct ocfs2_xattr_search *xs,
4765 u32 name_hash,
Tao Ma5a095612008-09-19 22:17:41 +08004766 int local)
Tao Ma01225592008-08-18 17:38:53 +08004767{
4768 struct ocfs2_xattr_entry *last, *xe;
4769 int name_len = strlen(xi->name);
4770 struct ocfs2_xattr_header *xh = xs->header;
4771 u16 count = le16_to_cpu(xh->xh_count), start;
4772 size_t blocksize = inode->i_sb->s_blocksize;
4773 char *val;
4774 size_t offs, size, new_size;
4775
4776 last = &xh->xh_entries[count];
4777 if (!xs->not_found) {
4778 xe = xs->here;
4779 offs = le16_to_cpu(xe->xe_name_offset);
4780 if (ocfs2_xattr_is_local(xe))
4781 size = OCFS2_XATTR_SIZE(name_len) +
4782 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4783 else
4784 size = OCFS2_XATTR_SIZE(name_len) +
4785 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4786
4787 /*
4788 * If the new value will be stored outside, xi->value has been
4789 * initalized as an empty ocfs2_xattr_value_root, and the same
4790 * goes with xi->value_len, so we can set new_size safely here.
4791 * See ocfs2_xattr_set_in_bucket.
4792 */
4793 new_size = OCFS2_XATTR_SIZE(name_len) +
4794 OCFS2_XATTR_SIZE(xi->value_len);
4795
4796 le16_add_cpu(&xh->xh_name_value_len, -size);
4797 if (xi->value) {
4798 if (new_size > size)
4799 goto set_new_name_value;
4800
4801 /* Now replace the old value with new one. */
4802 if (local)
4803 xe->xe_value_size = cpu_to_le64(xi->value_len);
4804 else
4805 xe->xe_value_size = 0;
4806
4807 val = ocfs2_xattr_bucket_get_val(inode,
Joel Beckerba937122008-10-24 19:13:20 -07004808 xs->bucket, offs);
Tao Ma01225592008-08-18 17:38:53 +08004809 memset(val + OCFS2_XATTR_SIZE(name_len), 0,
4810 size - OCFS2_XATTR_SIZE(name_len));
4811 if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
4812 memcpy(val + OCFS2_XATTR_SIZE(name_len),
4813 xi->value, xi->value_len);
4814
4815 le16_add_cpu(&xh->xh_name_value_len, new_size);
4816 ocfs2_xattr_set_local(xe, local);
4817 return;
4818 } else {
Tao Ma5a095612008-09-19 22:17:41 +08004819 /*
4820 * Remove the old entry if there is more than one.
4821 * We don't remove the last entry so that we can
4822 * use it to indicate the hash value of the empty
4823 * bucket.
4824 */
Tao Ma01225592008-08-18 17:38:53 +08004825 last -= 1;
Tao Ma01225592008-08-18 17:38:53 +08004826 le16_add_cpu(&xh->xh_count, -1);
Tao Ma5a095612008-09-19 22:17:41 +08004827 if (xh->xh_count) {
4828 memmove(xe, xe + 1,
4829 (void *)last - (void *)xe);
4830 memset(last, 0,
4831 sizeof(struct ocfs2_xattr_entry));
4832 } else
4833 xh->xh_free_start =
4834 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4835
Tao Ma01225592008-08-18 17:38:53 +08004836 return;
4837 }
4838 } else {
4839 /* find a new entry for insert. */
4840 int low = 0, high = count - 1, tmp;
4841 struct ocfs2_xattr_entry *tmp_xe;
4842
Tao Ma5a095612008-09-19 22:17:41 +08004843 while (low <= high && count) {
Tao Ma01225592008-08-18 17:38:53 +08004844 tmp = (low + high) / 2;
4845 tmp_xe = &xh->xh_entries[tmp];
4846
4847 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
4848 low = tmp + 1;
4849 else if (name_hash <
4850 le32_to_cpu(tmp_xe->xe_name_hash))
4851 high = tmp - 1;
Tao Ma06b240d2008-09-19 22:16:34 +08004852 else {
4853 low = tmp;
Tao Ma01225592008-08-18 17:38:53 +08004854 break;
Tao Ma06b240d2008-09-19 22:16:34 +08004855 }
Tao Ma01225592008-08-18 17:38:53 +08004856 }
4857
4858 xe = &xh->xh_entries[low];
4859 if (low != count)
4860 memmove(xe + 1, xe, (void *)last - (void *)xe);
4861
4862 le16_add_cpu(&xh->xh_count, 1);
4863 memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
4864 xe->xe_name_hash = cpu_to_le32(name_hash);
4865 xe->xe_name_len = name_len;
4866 ocfs2_xattr_set_type(xe, xi->name_index);
4867 }
4868
4869set_new_name_value:
4870 /* Insert the new name+value. */
4871 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
4872
4873 /*
4874 * We must make sure that the name/value pair
4875 * exists in the same block.
4876 */
4877 offs = le16_to_cpu(xh->xh_free_start);
4878 start = offs - size;
4879
4880 if (start >> inode->i_sb->s_blocksize_bits !=
4881 (offs - 1) >> inode->i_sb->s_blocksize_bits) {
4882 offs = offs - offs % blocksize;
4883 xh->xh_free_start = cpu_to_le16(offs);
4884 }
4885
Joel Beckerba937122008-10-24 19:13:20 -07004886 val = ocfs2_xattr_bucket_get_val(inode, xs->bucket, offs - size);
Tao Ma01225592008-08-18 17:38:53 +08004887 xe->xe_name_offset = cpu_to_le16(offs - size);
4888
4889 memset(val, 0, size);
4890 memcpy(val, xi->name, name_len);
4891 memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
4892
4893 xe->xe_value_size = cpu_to_le64(xi->value_len);
4894 ocfs2_xattr_set_local(xe, local);
4895 xs->here = xe;
4896 le16_add_cpu(&xh->xh_free_start, -size);
4897 le16_add_cpu(&xh->xh_name_value_len, size);
4898
4899 return;
4900}
4901
Tao Ma01225592008-08-18 17:38:53 +08004902/*
4903 * Set the xattr entry in the specified bucket.
4904 * The bucket is indicated by xs->bucket and it should have the enough
4905 * space for the xattr insertion.
4906 */
4907static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004908 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004909 struct ocfs2_xattr_info *xi,
4910 struct ocfs2_xattr_search *xs,
4911 u32 name_hash,
Tao Ma5a095612008-09-19 22:17:41 +08004912 int local)
Tao Ma01225592008-08-18 17:38:53 +08004913{
Joel Becker1224be02008-10-24 18:47:33 -07004914 int ret;
Joel Becker02dbf382008-10-27 18:07:45 -07004915 u64 blkno;
Tao Ma01225592008-08-18 17:38:53 +08004916
Mark Fashehff1ec202008-08-19 10:54:29 -07004917 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4918 (unsigned long)xi->value_len, xi->name_index,
Joel Beckerba937122008-10-24 19:13:20 -07004919 (unsigned long long)bucket_blkno(xs->bucket));
Tao Ma01225592008-08-18 17:38:53 +08004920
Joel Beckerba937122008-10-24 19:13:20 -07004921 if (!xs->bucket->bu_bhs[1]) {
Joel Becker02dbf382008-10-27 18:07:45 -07004922 blkno = bucket_blkno(xs->bucket);
4923 ocfs2_xattr_bucket_relse(xs->bucket);
4924 ret = ocfs2_read_xattr_bucket(xs->bucket, blkno);
Tao Ma01225592008-08-18 17:38:53 +08004925 if (ret) {
4926 mlog_errno(ret);
4927 goto out;
4928 }
4929 }
4930
Joel Beckerba937122008-10-24 19:13:20 -07004931 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004932 OCFS2_JOURNAL_ACCESS_WRITE);
4933 if (ret < 0) {
4934 mlog_errno(ret);
4935 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004936 }
4937
Tao Ma5a095612008-09-19 22:17:41 +08004938 ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
Joel Beckerba937122008-10-24 19:13:20 -07004939 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004940
Tao Ma01225592008-08-18 17:38:53 +08004941out:
Tao Ma01225592008-08-18 17:38:53 +08004942 return ret;
4943}
4944
Tao Ma01225592008-08-18 17:38:53 +08004945/*
4946 * Truncate the specified xe_off entry in xattr bucket.
4947 * bucket is indicated by header_bh and len is the new length.
4948 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4949 *
4950 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4951 */
4952static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
Joel Becker548b0f22008-11-24 19:32:13 -08004953 struct ocfs2_xattr_bucket *bucket,
Tao Ma01225592008-08-18 17:38:53 +08004954 int xe_off,
Tao Ma78f30c32008-11-12 08:27:00 +08004955 int len,
4956 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004957{
4958 int ret, offset;
4959 u64 value_blk;
Tao Ma01225592008-08-18 17:38:53 +08004960 struct ocfs2_xattr_entry *xe;
Joel Becker548b0f22008-11-24 19:32:13 -08004961 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma01225592008-08-18 17:38:53 +08004962 size_t blocksize = inode->i_sb->s_blocksize;
Joel Beckerb3e5d372008-12-09 15:01:04 -08004963 struct ocfs2_xattr_value_buf vb = {
4964 .vb_access = ocfs2_journal_access,
4965 };
Tao Ma01225592008-08-18 17:38:53 +08004966
4967 xe = &xh->xh_entries[xe_off];
4968
4969 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
4970
4971 offset = le16_to_cpu(xe->xe_name_offset) +
4972 OCFS2_XATTR_SIZE(xe->xe_name_len);
4973
4974 value_blk = offset / blocksize;
4975
4976 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4977 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
Tao Ma01225592008-08-18 17:38:53 +08004978
Joel Beckerb3e5d372008-12-09 15:01:04 -08004979 vb.vb_bh = bucket->bu_bhs[value_blk];
4980 BUG_ON(!vb.vb_bh);
Tao Ma01225592008-08-18 17:38:53 +08004981
Joel Beckerb3e5d372008-12-09 15:01:04 -08004982 vb.vb_xv = (struct ocfs2_xattr_value_root *)
4983 (vb.vb_bh->b_data + offset % blocksize);
Tao Ma01225592008-08-18 17:38:53 +08004984
Joel Becker548b0f22008-11-24 19:32:13 -08004985 /*
4986 * From here on out we have to dirty the bucket. The generic
4987 * value calls only modify one of the bucket's bhs, but we need
4988 * to send the bucket at once. So if they error, they *could* have
4989 * modified something. We have to assume they did, and dirty
4990 * the whole bucket. This leaves us in a consistent state.
4991 */
Tao Ma01225592008-08-18 17:38:53 +08004992 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
Joel Becker548b0f22008-11-24 19:32:13 -08004993 xe_off, (unsigned long long)bucket_blkno(bucket), len);
Joel Beckerb3e5d372008-12-09 15:01:04 -08004994 ret = ocfs2_xattr_value_truncate(inode, &vb, len, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004995 if (ret) {
4996 mlog_errno(ret);
Tao Ma554e7f92009-01-08 08:21:43 +08004997 goto out;
4998 }
4999
5000 ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
5001 OCFS2_JOURNAL_ACCESS_WRITE);
5002 if (ret) {
5003 mlog_errno(ret);
5004 goto out;
Tao Ma01225592008-08-18 17:38:53 +08005005 }
5006
Joel Becker548b0f22008-11-24 19:32:13 -08005007 xe->xe_value_size = cpu_to_le64(len);
5008
Joel Becker548b0f22008-11-24 19:32:13 -08005009 ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket);
Tao Ma01225592008-08-18 17:38:53 +08005010
5011out:
Tao Ma01225592008-08-18 17:38:53 +08005012 return ret;
5013}
5014
5015static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +08005016 struct ocfs2_xattr_search *xs,
5017 int len,
5018 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005019{
5020 int ret, offset;
5021 struct ocfs2_xattr_entry *xe = xs->here;
5022 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
5023
Joel Beckerba937122008-10-24 19:13:20 -07005024 BUG_ON(!xs->bucket->bu_bhs[0] || !xe || ocfs2_xattr_is_local(xe));
Tao Ma01225592008-08-18 17:38:53 +08005025
5026 offset = xe - xh->xh_entries;
Joel Becker548b0f22008-11-24 19:32:13 -08005027 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket,
Tao Ma78f30c32008-11-12 08:27:00 +08005028 offset, len, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005029 if (ret)
5030 mlog_errno(ret);
5031
5032 return ret;
5033}
5034
5035static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08005036 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08005037 struct ocfs2_xattr_search *xs,
5038 char *val,
5039 int value_len)
5040{
Tao Ma712e53e2009-03-12 08:37:34 +08005041 int ret, offset, block_off;
Tao Ma01225592008-08-18 17:38:53 +08005042 struct ocfs2_xattr_value_root *xv;
5043 struct ocfs2_xattr_entry *xe = xs->here;
Tao Ma712e53e2009-03-12 08:37:34 +08005044 struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
5045 void *base;
Tao Ma492a8a32009-08-18 11:43:17 +08005046 struct ocfs2_xattr_value_buf vb = {
5047 .vb_access = ocfs2_journal_access,
5048 };
Tao Ma01225592008-08-18 17:38:53 +08005049
5050 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
5051
Tao Mafd68a892009-08-18 11:43:21 +08005052 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb, xh,
Tao Ma712e53e2009-03-12 08:37:34 +08005053 xe - xh->xh_entries,
5054 &block_off,
5055 &offset);
5056 if (ret) {
5057 mlog_errno(ret);
5058 goto out;
5059 }
Tao Ma01225592008-08-18 17:38:53 +08005060
Tao Ma712e53e2009-03-12 08:37:34 +08005061 base = bucket_block(xs->bucket, block_off);
5062 xv = (struct ocfs2_xattr_value_root *)(base + offset +
5063 OCFS2_XATTR_SIZE(xe->xe_name_len));
Tao Ma01225592008-08-18 17:38:53 +08005064
Tao Ma492a8a32009-08-18 11:43:17 +08005065 vb.vb_xv = xv;
5066 vb.vb_bh = xs->bucket->bu_bhs[block_off];
Tao Ma712e53e2009-03-12 08:37:34 +08005067 ret = __ocfs2_xattr_set_value_outside(inode, handle,
Tao Ma492a8a32009-08-18 11:43:17 +08005068 &vb, val, value_len);
Tao Ma712e53e2009-03-12 08:37:34 +08005069 if (ret)
5070 mlog_errno(ret);
5071out:
5072 return ret;
Tao Ma01225592008-08-18 17:38:53 +08005073}
5074
Tao Ma01225592008-08-18 17:38:53 +08005075static int ocfs2_rm_xattr_cluster(struct inode *inode,
5076 struct buffer_head *root_bh,
5077 u64 blkno,
5078 u32 cpos,
Tao Ma47bca492009-08-18 11:43:42 +08005079 u32 len,
5080 void *para)
Tao Ma01225592008-08-18 17:38:53 +08005081{
5082 int ret;
5083 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5084 struct inode *tl_inode = osb->osb_tl_inode;
5085 handle_t *handle;
5086 struct ocfs2_xattr_block *xb =
5087 (struct ocfs2_xattr_block *)root_bh->b_data;
Tao Ma01225592008-08-18 17:38:53 +08005088 struct ocfs2_alloc_context *meta_ac = NULL;
5089 struct ocfs2_cached_dealloc_ctxt dealloc;
Joel Beckerf99b9b72008-08-20 19:36:33 -07005090 struct ocfs2_extent_tree et;
5091
Tao Ma47bca492009-08-18 11:43:42 +08005092 ret = ocfs2_iterate_xattr_buckets(inode, blkno, len,
Tao Mace9c5a52009-08-18 11:43:59 +08005093 ocfs2_delete_xattr_in_bucket, para);
Tao Ma47bca492009-08-18 11:43:42 +08005094 if (ret) {
5095 mlog_errno(ret);
5096 return ret;
5097 }
5098
Joel Becker5e404e92009-02-13 03:54:22 -08005099 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
Tao Ma01225592008-08-18 17:38:53 +08005100
5101 ocfs2_init_dealloc_ctxt(&dealloc);
5102
5103 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
5104 cpos, len, (unsigned long long)blkno);
5105
Joel Becker8cb471e2009-02-10 20:00:41 -08005106 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode), blkno,
5107 len);
Tao Ma01225592008-08-18 17:38:53 +08005108
Joel Beckerf99b9b72008-08-20 19:36:33 -07005109 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08005110 if (ret) {
5111 mlog_errno(ret);
5112 return ret;
5113 }
5114
5115 mutex_lock(&tl_inode->i_mutex);
5116
5117 if (ocfs2_truncate_log_needs_flush(osb)) {
5118 ret = __ocfs2_flush_truncate_log(osb);
5119 if (ret < 0) {
5120 mlog_errno(ret);
5121 goto out;
5122 }
5123 }
5124
Jan Karaa90714c2008-10-09 19:38:40 +02005125 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
Tao Mad3264792008-10-24 07:57:28 +08005126 if (IS_ERR(handle)) {
Tao Ma01225592008-08-18 17:38:53 +08005127 ret = -ENOMEM;
5128 mlog_errno(ret);
5129 goto out;
5130 }
5131
Joel Becker0cf2f762009-02-12 16:41:25 -08005132 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
Joel Becker84008972008-12-09 16:11:49 -08005133 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08005134 if (ret) {
5135 mlog_errno(ret);
5136 goto out_commit;
5137 }
5138
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005139 ret = ocfs2_remove_extent(handle, &et, cpos, len, meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005140 &dealloc);
Tao Ma01225592008-08-18 17:38:53 +08005141 if (ret) {
5142 mlog_errno(ret);
5143 goto out_commit;
5144 }
5145
5146 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
5147
5148 ret = ocfs2_journal_dirty(handle, root_bh);
5149 if (ret) {
5150 mlog_errno(ret);
5151 goto out_commit;
5152 }
5153
5154 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
5155 if (ret)
5156 mlog_errno(ret);
5157
5158out_commit:
5159 ocfs2_commit_trans(osb, handle);
5160out:
5161 ocfs2_schedule_truncate_log_flush(osb, 1);
5162
5163 mutex_unlock(&tl_inode->i_mutex);
5164
5165 if (meta_ac)
5166 ocfs2_free_alloc_context(meta_ac);
5167
5168 ocfs2_run_deallocs(osb, &dealloc);
5169
5170 return ret;
5171}
5172
Tao Ma01225592008-08-18 17:38:53 +08005173static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08005174 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08005175 struct ocfs2_xattr_search *xs)
5176{
Joel Beckerba937122008-10-24 19:13:20 -07005177 struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08005178 struct ocfs2_xattr_entry *last = &xh->xh_entries[
5179 le16_to_cpu(xh->xh_count) - 1];
5180 int ret = 0;
5181
Joel Beckerba937122008-10-24 19:13:20 -07005182 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
Joel Becker1224be02008-10-24 18:47:33 -07005183 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08005184 if (ret) {
5185 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08005186 return;
Tao Ma01225592008-08-18 17:38:53 +08005187 }
5188
5189 /* Remove the old entry. */
5190 memmove(xs->here, xs->here + 1,
5191 (void *)last - (void *)xs->here);
5192 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
5193 le16_add_cpu(&xh->xh_count, -1);
5194
Joel Beckerba937122008-10-24 19:13:20 -07005195 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08005196}
5197
5198/*
5199 * Set the xattr name/value in the bucket specified in xs.
5200 *
5201 * As the new value in xi may be stored in the bucket or in an outside cluster,
5202 * we divide the whole process into 3 steps:
5203 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
5204 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
5205 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
5206 * 4. If the clusters for the new outside value can't be allocated, we need
5207 * to free the xattr we allocated in set.
5208 */
5209static int ocfs2_xattr_set_in_bucket(struct inode *inode,
5210 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08005211 struct ocfs2_xattr_search *xs,
5212 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005213{
Tao Ma5a095612008-09-19 22:17:41 +08005214 int ret, local = 1;
Tao Ma01225592008-08-18 17:38:53 +08005215 size_t value_len;
5216 char *val = (char *)xi->value;
5217 struct ocfs2_xattr_entry *xe = xs->here;
Tao Ma2057e5c2008-10-09 23:06:13 +08005218 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name,
5219 strlen(xi->name));
Tao Ma01225592008-08-18 17:38:53 +08005220
5221 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
5222 /*
5223 * We need to truncate the xattr storage first.
5224 *
5225 * If both the old and new value are stored to
5226 * outside block, we only need to truncate
5227 * the storage and then set the value outside.
5228 *
5229 * If the new value should be stored within block,
5230 * we should free all the outside block first and
5231 * the modification to the xattr block will be done
5232 * by following steps.
5233 */
5234 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
5235 value_len = xi->value_len;
5236 else
5237 value_len = 0;
5238
5239 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
Tao Ma78f30c32008-11-12 08:27:00 +08005240 value_len,
5241 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005242 if (ret)
5243 goto out;
5244
5245 if (value_len)
5246 goto set_value_outside;
5247 }
5248
5249 value_len = xi->value_len;
5250 /* So we have to handle the inside block change now. */
5251 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
5252 /*
5253 * If the new value will be stored outside of block,
5254 * initalize a new empty value root and insert it first.
5255 */
5256 local = 0;
5257 xi->value = &def_xv;
5258 xi->value_len = OCFS2_XATTR_ROOT_SIZE;
5259 }
5260
Tao Ma85db90e2008-11-12 08:27:01 +08005261 ret = ocfs2_xattr_set_entry_in_bucket(inode, ctxt->handle, xi, xs,
5262 name_hash, local);
Tao Ma01225592008-08-18 17:38:53 +08005263 if (ret) {
5264 mlog_errno(ret);
5265 goto out;
5266 }
5267
Tao Ma5a095612008-09-19 22:17:41 +08005268 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
5269 goto out;
Tao Ma01225592008-08-18 17:38:53 +08005270
Tao Ma5a095612008-09-19 22:17:41 +08005271 /* allocate the space now for the outside block storage. */
5272 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
Tao Ma78f30c32008-11-12 08:27:00 +08005273 value_len, ctxt);
Tao Ma5a095612008-09-19 22:17:41 +08005274 if (ret) {
5275 mlog_errno(ret);
5276
5277 if (xs->not_found) {
5278 /*
5279 * We can't allocate enough clusters for outside
5280 * storage and we have allocated xattr already,
5281 * so need to remove it.
5282 */
Tao Ma85db90e2008-11-12 08:27:01 +08005283 ocfs2_xattr_bucket_remove_xs(inode, ctxt->handle, xs);
Tao Ma01225592008-08-18 17:38:53 +08005284 }
Tao Ma01225592008-08-18 17:38:53 +08005285 goto out;
5286 }
5287
5288set_value_outside:
Tao Ma85db90e2008-11-12 08:27:01 +08005289 ret = ocfs2_xattr_bucket_set_value_outside(inode, ctxt->handle,
5290 xs, val, value_len);
Tao Ma01225592008-08-18 17:38:53 +08005291out:
5292 return ret;
5293}
5294
Tao Ma80bcaf32008-10-27 06:06:24 +08005295/*
5296 * check whether the xattr bucket is filled up with the same hash value.
5297 * If we want to insert the xattr with the same hash, return -ENOSPC.
5298 * If we want to insert a xattr with different hash value, go ahead
5299 * and ocfs2_divide_xattr_bucket will handle this.
5300 */
Tao Ma01225592008-08-18 17:38:53 +08005301static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
Tao Ma80bcaf32008-10-27 06:06:24 +08005302 struct ocfs2_xattr_bucket *bucket,
5303 const char *name)
Tao Ma01225592008-08-18 17:38:53 +08005304{
Joel Becker3e632942008-10-24 17:04:49 -07005305 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08005306 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
5307
5308 if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
5309 return 0;
Tao Ma01225592008-08-18 17:38:53 +08005310
5311 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
5312 xh->xh_entries[0].xe_name_hash) {
5313 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
5314 "hash = %u\n",
Joel Becker9c7759a2008-10-24 16:21:03 -07005315 (unsigned long long)bucket_blkno(bucket),
Tao Ma01225592008-08-18 17:38:53 +08005316 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
5317 return -ENOSPC;
5318 }
5319
5320 return 0;
5321}
5322
5323static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
5324 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08005325 struct ocfs2_xattr_search *xs,
5326 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005327{
5328 struct ocfs2_xattr_header *xh;
5329 struct ocfs2_xattr_entry *xe;
5330 u16 count, header_size, xh_free_start;
Joel Becker6dde41d2008-10-24 17:16:48 -07005331 int free, max_free, need, old;
Tao Ma01225592008-08-18 17:38:53 +08005332 size_t value_size = 0, name_len = strlen(xi->name);
5333 size_t blocksize = inode->i_sb->s_blocksize;
5334 int ret, allocation = 0;
Tao Ma01225592008-08-18 17:38:53 +08005335
5336 mlog_entry("Set xattr %s in xattr index block\n", xi->name);
5337
5338try_again:
5339 xh = xs->header;
5340 count = le16_to_cpu(xh->xh_count);
5341 xh_free_start = le16_to_cpu(xh->xh_free_start);
5342 header_size = sizeof(struct ocfs2_xattr_header) +
5343 count * sizeof(struct ocfs2_xattr_entry);
Tiger Yang4442f512009-02-20 11:11:50 +08005344 max_free = OCFS2_XATTR_BUCKET_SIZE - header_size -
5345 le16_to_cpu(xh->xh_name_value_len) - OCFS2_XATTR_HEADER_GAP;
Tao Ma01225592008-08-18 17:38:53 +08005346
5347 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
5348 "of %u which exceed block size\n",
Joel Beckerba937122008-10-24 19:13:20 -07005349 (unsigned long long)bucket_blkno(xs->bucket),
Tao Ma01225592008-08-18 17:38:53 +08005350 header_size);
5351
5352 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
5353 value_size = OCFS2_XATTR_ROOT_SIZE;
5354 else if (xi->value)
5355 value_size = OCFS2_XATTR_SIZE(xi->value_len);
5356
5357 if (xs->not_found)
5358 need = sizeof(struct ocfs2_xattr_entry) +
5359 OCFS2_XATTR_SIZE(name_len) + value_size;
5360 else {
5361 need = value_size + OCFS2_XATTR_SIZE(name_len);
5362
5363 /*
5364 * We only replace the old value if the new length is smaller
5365 * than the old one. Otherwise we will allocate new space in the
5366 * bucket to store it.
5367 */
5368 xe = xs->here;
5369 if (ocfs2_xattr_is_local(xe))
5370 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
5371 else
5372 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
5373
5374 if (old >= value_size)
5375 need = 0;
5376 }
5377
Tiger Yang4442f512009-02-20 11:11:50 +08005378 free = xh_free_start - header_size - OCFS2_XATTR_HEADER_GAP;
Tao Ma01225592008-08-18 17:38:53 +08005379 /*
5380 * We need to make sure the new name/value pair
5381 * can exist in the same block.
5382 */
5383 if (xh_free_start % blocksize < need)
5384 free -= xh_free_start % blocksize;
5385
5386 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
5387 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
5388 " %u\n", xs->not_found,
Joel Beckerba937122008-10-24 19:13:20 -07005389 (unsigned long long)bucket_blkno(xs->bucket),
Tao Ma01225592008-08-18 17:38:53 +08005390 free, need, max_free, le16_to_cpu(xh->xh_free_start),
5391 le16_to_cpu(xh->xh_name_value_len));
5392
Tao Ma976331d2008-11-12 08:26:57 +08005393 if (free < need ||
5394 (xs->not_found &&
5395 count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb))) {
Tao Ma01225592008-08-18 17:38:53 +08005396 if (need <= max_free &&
5397 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
5398 /*
5399 * We can create the space by defragment. Since only the
5400 * name/value will be moved, the xe shouldn't be changed
5401 * in xs.
5402 */
Tao Ma85db90e2008-11-12 08:27:01 +08005403 ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
5404 xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08005405 if (ret) {
5406 mlog_errno(ret);
5407 goto out;
5408 }
5409
5410 xh_free_start = le16_to_cpu(xh->xh_free_start);
Tiger Yang4442f512009-02-20 11:11:50 +08005411 free = xh_free_start - header_size
5412 - OCFS2_XATTR_HEADER_GAP;
Tao Ma01225592008-08-18 17:38:53 +08005413 if (xh_free_start % blocksize < need)
5414 free -= xh_free_start % blocksize;
5415
5416 if (free >= need)
5417 goto xattr_set;
5418
5419 mlog(0, "Can't get enough space for xattr insert by "
5420 "defragment. Need %u bytes, but we have %d, so "
5421 "allocate new bucket for it.\n", need, free);
5422 }
5423
5424 /*
5425 * We have to add new buckets or clusters and one
5426 * allocation should leave us enough space for insert.
5427 */
5428 BUG_ON(allocation);
5429
5430 /*
5431 * We do not allow for overlapping ranges between buckets. And
5432 * the maximum number of collisions we will allow for then is
5433 * one bucket's worth, so check it here whether we need to
5434 * add a new bucket for the insert.
5435 */
Tao Ma80bcaf32008-10-27 06:06:24 +08005436 ret = ocfs2_check_xattr_bucket_collision(inode,
Joel Beckerba937122008-10-24 19:13:20 -07005437 xs->bucket,
Tao Ma80bcaf32008-10-27 06:06:24 +08005438 xi->name);
Tao Ma01225592008-08-18 17:38:53 +08005439 if (ret) {
5440 mlog_errno(ret);
5441 goto out;
5442 }
5443
5444 ret = ocfs2_add_new_xattr_bucket(inode,
5445 xs->xattr_bh,
Joel Becker91f20332008-11-26 15:25:41 -08005446 xs->bucket,
Tao Ma78f30c32008-11-12 08:27:00 +08005447 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005448 if (ret) {
5449 mlog_errno(ret);
5450 goto out;
5451 }
5452
Joel Becker91f20332008-11-26 15:25:41 -08005453 /*
5454 * ocfs2_add_new_xattr_bucket() will have updated
5455 * xs->bucket if it moved, but it will not have updated
5456 * any of the other search fields. Thus, we drop it and
5457 * re-search. Everything should be cached, so it'll be
5458 * quick.
5459 */
Joel Beckerba937122008-10-24 19:13:20 -07005460 ocfs2_xattr_bucket_relse(xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08005461 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
5462 xi->name_index,
5463 xi->name, xs);
5464 if (ret && ret != -ENODATA)
5465 goto out;
5466 xs->not_found = ret;
5467 allocation = 1;
5468 goto try_again;
5469 }
5470
5471xattr_set:
Tao Ma78f30c32008-11-12 08:27:00 +08005472 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005473out:
5474 mlog_exit(ret);
5475 return ret;
5476}
Tao Maa3944252008-08-18 17:38:54 +08005477
5478static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
5479 struct ocfs2_xattr_bucket *bucket,
5480 void *para)
5481{
Tao Mace9c5a52009-08-18 11:43:59 +08005482 int ret = 0, ref_credits;
Joel Becker3e632942008-10-24 17:04:49 -07005483 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Maa3944252008-08-18 17:38:54 +08005484 u16 i;
5485 struct ocfs2_xattr_entry *xe;
Tao Ma78f30c32008-11-12 08:27:00 +08005486 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5487 struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
Joel Becker548b0f22008-11-24 19:32:13 -08005488 int credits = ocfs2_remove_extent_credits(osb->sb) +
5489 ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Mace9c5a52009-08-18 11:43:59 +08005490 struct ocfs2_xattr_value_root *xv;
5491 struct ocfs2_rm_xattr_bucket_para *args =
5492 (struct ocfs2_rm_xattr_bucket_para *)para;
Tao Ma78f30c32008-11-12 08:27:00 +08005493
5494 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
Tao Maa3944252008-08-18 17:38:54 +08005495
5496 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
5497 xe = &xh->xh_entries[i];
5498 if (ocfs2_xattr_is_local(xe))
5499 continue;
5500
Tao Mace9c5a52009-08-18 11:43:59 +08005501 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket,
5502 i, &xv, NULL);
5503
5504 ret = ocfs2_lock_xattr_remove_allocators(inode, xv,
5505 args->ref_ci,
5506 args->ref_root_bh,
5507 &ctxt.meta_ac,
5508 &ref_credits);
5509
5510 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
Tao Ma88c3b062008-12-11 08:54:11 +08005511 if (IS_ERR(ctxt.handle)) {
5512 ret = PTR_ERR(ctxt.handle);
5513 mlog_errno(ret);
5514 break;
5515 }
5516
Joel Becker548b0f22008-11-24 19:32:13 -08005517 ret = ocfs2_xattr_bucket_value_truncate(inode, bucket,
Tao Ma78f30c32008-11-12 08:27:00 +08005518 i, 0, &ctxt);
Tao Ma88c3b062008-12-11 08:54:11 +08005519
5520 ocfs2_commit_trans(osb, ctxt.handle);
Tao Mace9c5a52009-08-18 11:43:59 +08005521 if (ctxt.meta_ac) {
5522 ocfs2_free_alloc_context(ctxt.meta_ac);
5523 ctxt.meta_ac = NULL;
5524 }
Tao Maa3944252008-08-18 17:38:54 +08005525 if (ret) {
5526 mlog_errno(ret);
5527 break;
5528 }
5529 }
5530
Tao Mace9c5a52009-08-18 11:43:59 +08005531 if (ctxt.meta_ac)
5532 ocfs2_free_alloc_context(ctxt.meta_ac);
Tao Ma78f30c32008-11-12 08:27:00 +08005533 ocfs2_schedule_truncate_log_flush(osb, 1);
5534 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tao Maa3944252008-08-18 17:38:54 +08005535 return ret;
5536}
5537
Mark Fasheh99219ae2008-10-07 14:52:59 -07005538/*
Tao Ma492a8a32009-08-18 11:43:17 +08005539 * Whenever we modify a xattr value root in the bucket(e.g, CoW
5540 * or change the extent record flag), we need to recalculate
5541 * the metaecc for the whole bucket. So it is done here.
5542 *
5543 * Note:
5544 * We have to give the extra credits for the caller.
5545 */
5546static int ocfs2_xattr_bucket_post_refcount(struct inode *inode,
5547 handle_t *handle,
5548 void *para)
5549{
5550 int ret;
5551 struct ocfs2_xattr_bucket *bucket =
5552 (struct ocfs2_xattr_bucket *)para;
5553
5554 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
5555 OCFS2_JOURNAL_ACCESS_WRITE);
5556 if (ret) {
5557 mlog_errno(ret);
5558 return ret;
5559 }
5560
5561 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
5562
5563 return 0;
5564}
5565
5566/*
5567 * Special action we need if the xattr value is refcounted.
5568 *
5569 * 1. If the xattr is refcounted, lock the tree.
5570 * 2. CoW the xattr if we are setting the new value and the value
5571 * will be stored outside.
5572 * 3. In other case, decrease_refcount will work for us, so just
5573 * lock the refcount tree, calculate the meta and credits is OK.
5574 *
5575 * We have to do CoW before ocfs2_init_xattr_set_ctxt since
5576 * currently CoW is a completed transaction, while this function
5577 * will also lock the allocators and let us deadlock. So we will
5578 * CoW the whole xattr value.
5579 */
5580static int ocfs2_prepare_refcount_xattr(struct inode *inode,
5581 struct ocfs2_dinode *di,
5582 struct ocfs2_xattr_info *xi,
5583 struct ocfs2_xattr_search *xis,
5584 struct ocfs2_xattr_search *xbs,
5585 struct ocfs2_refcount_tree **ref_tree,
5586 int *meta_add,
5587 int *credits)
5588{
5589 int ret = 0;
5590 struct ocfs2_xattr_block *xb;
5591 struct ocfs2_xattr_entry *xe;
5592 char *base;
5593 u32 p_cluster, num_clusters;
5594 unsigned int ext_flags;
5595 int name_offset, name_len;
5596 struct ocfs2_xattr_value_buf vb;
5597 struct ocfs2_xattr_bucket *bucket = NULL;
5598 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5599 struct ocfs2_post_refcount refcount;
5600 struct ocfs2_post_refcount *p = NULL;
5601 struct buffer_head *ref_root_bh = NULL;
5602
5603 if (!xis->not_found) {
5604 xe = xis->here;
5605 name_offset = le16_to_cpu(xe->xe_name_offset);
5606 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5607 base = xis->base;
5608 vb.vb_bh = xis->inode_bh;
5609 vb.vb_access = ocfs2_journal_access_di;
5610 } else {
5611 int i, block_off = 0;
5612 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
5613 xe = xbs->here;
5614 name_offset = le16_to_cpu(xe->xe_name_offset);
5615 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5616 i = xbs->here - xbs->header->xh_entries;
5617
5618 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
Tao Mafd68a892009-08-18 11:43:21 +08005619 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Tao Ma492a8a32009-08-18 11:43:17 +08005620 bucket_xh(xbs->bucket),
5621 i, &block_off,
5622 &name_offset);
5623 if (ret) {
5624 mlog_errno(ret);
5625 goto out;
5626 }
5627 base = bucket_block(xbs->bucket, block_off);
5628 vb.vb_bh = xbs->bucket->bu_bhs[block_off];
5629 vb.vb_access = ocfs2_journal_access;
5630
5631 if (ocfs2_meta_ecc(osb)) {
5632 /*create parameters for ocfs2_post_refcount. */
5633 bucket = xbs->bucket;
5634 refcount.credits = bucket->bu_blocks;
5635 refcount.para = bucket;
5636 refcount.func =
5637 ocfs2_xattr_bucket_post_refcount;
5638 p = &refcount;
5639 }
5640 } else {
5641 base = xbs->base;
5642 vb.vb_bh = xbs->xattr_bh;
5643 vb.vb_access = ocfs2_journal_access_xb;
5644 }
5645 }
5646
5647 if (ocfs2_xattr_is_local(xe))
5648 goto out;
5649
5650 vb.vb_xv = (struct ocfs2_xattr_value_root *)
5651 (base + name_offset + name_len);
5652
5653 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
5654 &num_clusters, &vb.vb_xv->xr_list,
5655 &ext_flags);
5656 if (ret) {
5657 mlog_errno(ret);
5658 goto out;
5659 }
5660
5661 /*
5662 * We just need to check the 1st extent record, since we always
5663 * CoW the whole xattr. So there shouldn't be a xattr with
5664 * some REFCOUNT extent recs after the 1st one.
5665 */
5666 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
5667 goto out;
5668
5669 ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
5670 1, ref_tree, &ref_root_bh);
5671 if (ret) {
5672 mlog_errno(ret);
5673 goto out;
5674 }
5675
5676 /*
5677 * If we are deleting the xattr or the new size will be stored inside,
5678 * cool, leave it there, the xattr truncate process will remove them
5679 * for us(it still needs the refcount tree lock and the meta, credits).
5680 * And the worse case is that every cluster truncate will split the
5681 * refcount tree, and make the original extent become 3. So we will need
5682 * 2 * cluster more extent recs at most.
5683 */
5684 if (!xi->value || xi->value_len <= OCFS2_XATTR_INLINE_SIZE) {
5685
5686 ret = ocfs2_refcounted_xattr_delete_need(inode,
5687 &(*ref_tree)->rf_ci,
5688 ref_root_bh, vb.vb_xv,
5689 meta_add, credits);
5690 if (ret)
5691 mlog_errno(ret);
5692 goto out;
5693 }
5694
5695 ret = ocfs2_refcount_cow_xattr(inode, di, &vb,
5696 *ref_tree, ref_root_bh, 0,
5697 le32_to_cpu(vb.vb_xv->xr_clusters), p);
5698 if (ret)
5699 mlog_errno(ret);
5700
5701out:
5702 brelse(ref_root_bh);
5703 return ret;
5704}
5705
5706/*
Tao Ma01292412009-09-21 13:04:19 +08005707 * Add the REFCOUNTED flags for all the extent rec in ocfs2_xattr_value_root.
5708 * The physical clusters will be added to refcount tree.
5709 */
5710static int ocfs2_xattr_value_attach_refcount(struct inode *inode,
5711 struct ocfs2_xattr_value_root *xv,
5712 struct ocfs2_extent_tree *value_et,
5713 struct ocfs2_caching_info *ref_ci,
5714 struct buffer_head *ref_root_bh,
5715 struct ocfs2_cached_dealloc_ctxt *dealloc,
5716 struct ocfs2_post_refcount *refcount)
5717{
5718 int ret = 0;
5719 u32 clusters = le32_to_cpu(xv->xr_clusters);
5720 u32 cpos, p_cluster, num_clusters;
5721 struct ocfs2_extent_list *el = &xv->xr_list;
5722 unsigned int ext_flags;
5723
5724 cpos = 0;
5725 while (cpos < clusters) {
5726 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
5727 &num_clusters, el, &ext_flags);
5728
5729 cpos += num_clusters;
5730 if ((ext_flags & OCFS2_EXT_REFCOUNTED))
5731 continue;
5732
5733 BUG_ON(!p_cluster);
5734
5735 ret = ocfs2_add_refcount_flag(inode, value_et,
5736 ref_ci, ref_root_bh,
5737 cpos - num_clusters,
5738 p_cluster, num_clusters,
5739 dealloc, refcount);
5740 if (ret) {
5741 mlog_errno(ret);
5742 break;
5743 }
5744 }
5745
5746 return ret;
5747}
5748
5749/*
5750 * Given a normal ocfs2_xattr_header, refcount all the entries which
5751 * have value stored outside.
5752 * Used for xattrs stored in inode and ocfs2_xattr_block.
5753 */
5754static int ocfs2_xattr_attach_refcount_normal(struct inode *inode,
5755 struct ocfs2_xattr_value_buf *vb,
5756 struct ocfs2_xattr_header *header,
5757 struct ocfs2_caching_info *ref_ci,
5758 struct buffer_head *ref_root_bh,
5759 struct ocfs2_cached_dealloc_ctxt *dealloc)
5760{
5761
5762 struct ocfs2_xattr_entry *xe;
5763 struct ocfs2_xattr_value_root *xv;
5764 struct ocfs2_extent_tree et;
5765 int i, ret = 0;
5766
5767 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
5768 xe = &header->xh_entries[i];
5769
5770 if (ocfs2_xattr_is_local(xe))
5771 continue;
5772
5773 xv = (struct ocfs2_xattr_value_root *)((void *)header +
5774 le16_to_cpu(xe->xe_name_offset) +
5775 OCFS2_XATTR_SIZE(xe->xe_name_len));
5776
5777 vb->vb_xv = xv;
5778 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
5779
5780 ret = ocfs2_xattr_value_attach_refcount(inode, xv, &et,
5781 ref_ci, ref_root_bh,
5782 dealloc, NULL);
5783 if (ret) {
5784 mlog_errno(ret);
5785 break;
5786 }
5787 }
5788
5789 return ret;
5790}
5791
5792static int ocfs2_xattr_inline_attach_refcount(struct inode *inode,
5793 struct buffer_head *fe_bh,
5794 struct ocfs2_caching_info *ref_ci,
5795 struct buffer_head *ref_root_bh,
5796 struct ocfs2_cached_dealloc_ctxt *dealloc)
5797{
5798 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
5799 struct ocfs2_xattr_header *header = (struct ocfs2_xattr_header *)
5800 (fe_bh->b_data + inode->i_sb->s_blocksize -
5801 le16_to_cpu(di->i_xattr_inline_size));
5802 struct ocfs2_xattr_value_buf vb = {
5803 .vb_bh = fe_bh,
5804 .vb_access = ocfs2_journal_access_di,
5805 };
5806
5807 return ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
5808 ref_ci, ref_root_bh, dealloc);
5809}
5810
5811struct ocfs2_xattr_tree_value_refcount_para {
5812 struct ocfs2_caching_info *ref_ci;
5813 struct buffer_head *ref_root_bh;
5814 struct ocfs2_cached_dealloc_ctxt *dealloc;
5815};
5816
5817static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
5818 struct ocfs2_xattr_bucket *bucket,
5819 int offset,
5820 struct ocfs2_xattr_value_root **xv,
5821 struct buffer_head **bh)
5822{
5823 int ret, block_off, name_offset;
5824 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5825 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
5826 void *base;
5827
5828 ret = ocfs2_xattr_bucket_get_name_value(sb,
5829 bucket_xh(bucket),
5830 offset,
5831 &block_off,
5832 &name_offset);
5833 if (ret) {
5834 mlog_errno(ret);
5835 goto out;
5836 }
5837
5838 base = bucket_block(bucket, block_off);
5839
5840 *xv = (struct ocfs2_xattr_value_root *)(base + name_offset +
5841 OCFS2_XATTR_SIZE(xe->xe_name_len));
5842
5843 if (bh)
5844 *bh = bucket->bu_bhs[block_off];
5845out:
5846 return ret;
5847}
5848
5849/*
5850 * For a given xattr bucket, refcount all the entries which
5851 * have value stored outside.
5852 */
5853static int ocfs2_xattr_bucket_value_refcount(struct inode *inode,
5854 struct ocfs2_xattr_bucket *bucket,
5855 void *para)
5856{
5857 int i, ret = 0;
5858 struct ocfs2_extent_tree et;
5859 struct ocfs2_xattr_tree_value_refcount_para *ref =
5860 (struct ocfs2_xattr_tree_value_refcount_para *)para;
5861 struct ocfs2_xattr_header *xh =
5862 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
5863 struct ocfs2_xattr_entry *xe;
5864 struct ocfs2_xattr_value_buf vb = {
5865 .vb_access = ocfs2_journal_access,
5866 };
5867 struct ocfs2_post_refcount refcount = {
5868 .credits = bucket->bu_blocks,
5869 .para = bucket,
5870 .func = ocfs2_xattr_bucket_post_refcount,
5871 };
5872 struct ocfs2_post_refcount *p = NULL;
5873
5874 /* We only need post_refcount if we support metaecc. */
5875 if (ocfs2_meta_ecc(OCFS2_SB(inode->i_sb)))
5876 p = &refcount;
5877
5878 mlog(0, "refcount bucket %llu, count = %u\n",
5879 (unsigned long long)bucket_blkno(bucket),
5880 le16_to_cpu(xh->xh_count));
5881 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
5882 xe = &xh->xh_entries[i];
5883
5884 if (ocfs2_xattr_is_local(xe))
5885 continue;
5886
5887 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket, i,
5888 &vb.vb_xv, &vb.vb_bh);
5889 if (ret) {
5890 mlog_errno(ret);
5891 break;
5892 }
5893
5894 ocfs2_init_xattr_value_extent_tree(&et,
5895 INODE_CACHE(inode), &vb);
5896
5897 ret = ocfs2_xattr_value_attach_refcount(inode, vb.vb_xv,
5898 &et, ref->ref_ci,
5899 ref->ref_root_bh,
5900 ref->dealloc, p);
5901 if (ret) {
5902 mlog_errno(ret);
5903 break;
5904 }
5905 }
5906
5907 return ret;
5908
5909}
5910
5911static int ocfs2_refcount_xattr_tree_rec(struct inode *inode,
5912 struct buffer_head *root_bh,
5913 u64 blkno, u32 cpos, u32 len, void *para)
5914{
5915 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
5916 ocfs2_xattr_bucket_value_refcount,
5917 para);
5918}
5919
5920static int ocfs2_xattr_block_attach_refcount(struct inode *inode,
5921 struct buffer_head *blk_bh,
5922 struct ocfs2_caching_info *ref_ci,
5923 struct buffer_head *ref_root_bh,
5924 struct ocfs2_cached_dealloc_ctxt *dealloc)
5925{
5926 int ret = 0;
5927 struct ocfs2_xattr_block *xb =
5928 (struct ocfs2_xattr_block *)blk_bh->b_data;
5929
5930 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
5931 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
5932 struct ocfs2_xattr_value_buf vb = {
5933 .vb_bh = blk_bh,
5934 .vb_access = ocfs2_journal_access_xb,
5935 };
5936
5937 ret = ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
5938 ref_ci, ref_root_bh,
5939 dealloc);
5940 } else {
5941 struct ocfs2_xattr_tree_value_refcount_para para = {
5942 .ref_ci = ref_ci,
5943 .ref_root_bh = ref_root_bh,
5944 .dealloc = dealloc,
5945 };
5946
5947 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
5948 ocfs2_refcount_xattr_tree_rec,
5949 &para);
5950 }
5951
5952 return ret;
5953}
5954
5955int ocfs2_xattr_attach_refcount_tree(struct inode *inode,
5956 struct buffer_head *fe_bh,
5957 struct ocfs2_caching_info *ref_ci,
5958 struct buffer_head *ref_root_bh,
5959 struct ocfs2_cached_dealloc_ctxt *dealloc)
5960{
5961 int ret = 0;
5962 struct ocfs2_inode_info *oi = OCFS2_I(inode);
5963 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
5964 struct buffer_head *blk_bh = NULL;
5965
5966 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
5967 ret = ocfs2_xattr_inline_attach_refcount(inode, fe_bh,
5968 ref_ci, ref_root_bh,
5969 dealloc);
5970 if (ret) {
5971 mlog_errno(ret);
5972 goto out;
5973 }
5974 }
5975
5976 if (!di->i_xattr_loc)
5977 goto out;
5978
5979 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
5980 &blk_bh);
5981 if (ret < 0) {
5982 mlog_errno(ret);
5983 goto out;
5984 }
5985
5986 ret = ocfs2_xattr_block_attach_refcount(inode, blk_bh, ref_ci,
5987 ref_root_bh, dealloc);
5988 if (ret)
5989 mlog_errno(ret);
5990
5991 brelse(blk_bh);
5992out:
5993
5994 return ret;
5995}
5996
5997/*
Tao Ma2999d122009-08-18 11:43:55 +08005998 * Store the information we need in xattr reflink.
5999 * old_bh and new_bh are inode bh for the old and new inode.
6000 */
6001struct ocfs2_xattr_reflink {
6002 struct inode *old_inode;
6003 struct inode *new_inode;
6004 struct buffer_head *old_bh;
6005 struct buffer_head *new_bh;
6006 struct ocfs2_caching_info *ref_ci;
6007 struct buffer_head *ref_root_bh;
6008 struct ocfs2_cached_dealloc_ctxt *dealloc;
6009};
6010
6011/*
6012 * Given a xattr header and xe offset,
6013 * return the proper xv and the corresponding bh.
6014 * xattr in inode, block and xattr tree have different implementaions.
6015 */
6016typedef int (get_xattr_value_root)(struct super_block *sb,
6017 struct buffer_head *bh,
6018 struct ocfs2_xattr_header *xh,
6019 int offset,
6020 struct ocfs2_xattr_value_root **xv,
6021 struct buffer_head **ret_bh,
6022 void *para);
6023
6024/*
6025 * Calculate all the xattr value root metadata stored in this xattr header and
6026 * credits we need if we create them from the scratch.
6027 * We use get_xattr_value_root so that all types of xattr container can use it.
6028 */
6029static int ocfs2_value_metas_in_xattr_header(struct super_block *sb,
6030 struct buffer_head *bh,
6031 struct ocfs2_xattr_header *xh,
6032 int *metas, int *credits,
6033 int *num_recs,
6034 get_xattr_value_root *func,
6035 void *para)
6036{
6037 int i, ret = 0;
6038 struct ocfs2_xattr_value_root *xv;
6039 struct ocfs2_xattr_entry *xe;
6040
6041 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6042 xe = &xh->xh_entries[i];
6043 if (ocfs2_xattr_is_local(xe))
6044 continue;
6045
6046 ret = func(sb, bh, xh, i, &xv, NULL, para);
6047 if (ret) {
6048 mlog_errno(ret);
6049 break;
6050 }
6051
6052 *metas += le16_to_cpu(xv->xr_list.l_tree_depth) *
6053 le16_to_cpu(xv->xr_list.l_next_free_rec);
6054
6055 *credits += ocfs2_calc_extend_credits(sb,
6056 &def_xv.xv.xr_list,
6057 le32_to_cpu(xv->xr_clusters));
6058
6059 /*
6060 * If the value is a tree with depth > 1, We don't go deep
6061 * to the extent block, so just calculate a maximum record num.
6062 */
6063 if (!xv->xr_list.l_tree_depth)
6064 *num_recs += xv->xr_list.l_next_free_rec;
6065 else
6066 *num_recs += ocfs2_clusters_for_bytes(sb,
6067 XATTR_SIZE_MAX);
6068 }
6069
6070 return ret;
6071}
6072
6073/* Used by xattr inode and block to return the right xv and buffer_head. */
6074static int ocfs2_get_xattr_value_root(struct super_block *sb,
6075 struct buffer_head *bh,
6076 struct ocfs2_xattr_header *xh,
6077 int offset,
6078 struct ocfs2_xattr_value_root **xv,
6079 struct buffer_head **ret_bh,
6080 void *para)
6081{
6082 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
6083
6084 *xv = (struct ocfs2_xattr_value_root *)((void *)xh +
6085 le16_to_cpu(xe->xe_name_offset) +
6086 OCFS2_XATTR_SIZE(xe->xe_name_len));
6087
6088 if (ret_bh)
6089 *ret_bh = bh;
6090
6091 return 0;
6092}
6093
6094/*
6095 * Lock the meta_ac and caculate how much credits we need for reflink xattrs.
6096 * It is only used for inline xattr and xattr block.
6097 */
6098static int ocfs2_reflink_lock_xattr_allocators(struct ocfs2_super *osb,
6099 struct ocfs2_xattr_header *xh,
6100 struct buffer_head *ref_root_bh,
6101 int *credits,
6102 struct ocfs2_alloc_context **meta_ac)
6103{
6104 int ret, meta_add = 0, num_recs = 0;
6105 struct ocfs2_refcount_block *rb =
6106 (struct ocfs2_refcount_block *)ref_root_bh->b_data;
6107
6108 *credits = 0;
6109
6110 ret = ocfs2_value_metas_in_xattr_header(osb->sb, NULL, xh,
6111 &meta_add, credits, &num_recs,
6112 ocfs2_get_xattr_value_root,
6113 NULL);
6114 if (ret) {
6115 mlog_errno(ret);
6116 goto out;
6117 }
6118
6119 /*
6120 * We need to add/modify num_recs in refcount tree, so just calculate
6121 * an approximate number we need for refcount tree change.
6122 * Sometimes we need to split the tree, and after split, half recs
6123 * will be moved to the new block, and a new block can only provide
6124 * half number of recs. So we multiple new blocks by 2.
6125 */
6126 num_recs = num_recs / ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6127 meta_add += num_recs;
6128 *credits += num_recs + num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6129 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6130 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6131 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6132 else
6133 *credits += 1;
6134
6135 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add, meta_ac);
6136 if (ret)
6137 mlog_errno(ret);
6138
6139out:
6140 return ret;
6141}
6142
6143/*
6144 * Given a xattr header, reflink all the xattrs in this container.
6145 * It can be used for inode, block and bucket.
6146 *
6147 * NOTE:
6148 * Before we call this function, the caller has memcpy the xattr in
6149 * old_xh to the new_xh.
6150 */
6151static int ocfs2_reflink_xattr_header(handle_t *handle,
6152 struct ocfs2_xattr_reflink *args,
6153 struct buffer_head *old_bh,
6154 struct ocfs2_xattr_header *xh,
6155 struct buffer_head *new_bh,
6156 struct ocfs2_xattr_header *new_xh,
6157 struct ocfs2_xattr_value_buf *vb,
6158 struct ocfs2_alloc_context *meta_ac,
6159 get_xattr_value_root *func,
6160 void *para)
6161{
6162 int ret = 0, i;
6163 struct super_block *sb = args->old_inode->i_sb;
6164 struct buffer_head *value_bh;
6165 struct ocfs2_xattr_entry *xe;
6166 struct ocfs2_xattr_value_root *xv, *new_xv;
6167 struct ocfs2_extent_tree data_et;
6168 u32 clusters, cpos, p_cluster, num_clusters;
6169 unsigned int ext_flags = 0;
6170
6171 mlog(0, "reflink xattr in container %llu, count = %u\n",
6172 (unsigned long long)old_bh->b_blocknr, le16_to_cpu(xh->xh_count));
6173 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6174 xe = &xh->xh_entries[i];
6175
6176 if (ocfs2_xattr_is_local(xe))
6177 continue;
6178
6179 ret = func(sb, old_bh, xh, i, &xv, NULL, para);
6180 if (ret) {
6181 mlog_errno(ret);
6182 break;
6183 }
6184
6185 ret = func(sb, new_bh, new_xh, i, &new_xv, &value_bh, para);
6186 if (ret) {
6187 mlog_errno(ret);
6188 break;
6189 }
6190
6191 /*
6192 * For the xattr which has l_tree_depth = 0, all the extent
6193 * recs have already be copied to the new xh with the
6194 * propriate OCFS2_EXT_REFCOUNTED flag we just need to
6195 * increase the refount count int the refcount tree.
6196 *
6197 * For the xattr which has l_tree_depth > 0, we need
6198 * to initialize it to the empty default value root,
6199 * and then insert the extents one by one.
6200 */
6201 if (xv->xr_list.l_tree_depth) {
6202 memcpy(new_xv, &def_xv, sizeof(def_xv));
6203 vb->vb_xv = new_xv;
6204 vb->vb_bh = value_bh;
6205 ocfs2_init_xattr_value_extent_tree(&data_et,
6206 INODE_CACHE(args->new_inode), vb);
6207 }
6208
6209 clusters = le32_to_cpu(xv->xr_clusters);
6210 cpos = 0;
6211 while (cpos < clusters) {
6212 ret = ocfs2_xattr_get_clusters(args->old_inode,
6213 cpos,
6214 &p_cluster,
6215 &num_clusters,
6216 &xv->xr_list,
6217 &ext_flags);
6218 if (ret) {
6219 mlog_errno(ret);
6220 goto out;
6221 }
6222
6223 BUG_ON(!p_cluster);
6224
6225 if (xv->xr_list.l_tree_depth) {
6226 ret = ocfs2_insert_extent(handle,
6227 &data_et, cpos,
6228 ocfs2_clusters_to_blocks(
6229 args->old_inode->i_sb,
6230 p_cluster),
6231 num_clusters, ext_flags,
6232 meta_ac);
6233 if (ret) {
6234 mlog_errno(ret);
6235 goto out;
6236 }
6237 }
6238
6239 ret = ocfs2_increase_refcount(handle, args->ref_ci,
6240 args->ref_root_bh,
6241 p_cluster, num_clusters,
6242 meta_ac, args->dealloc);
6243 if (ret) {
6244 mlog_errno(ret);
6245 goto out;
6246 }
6247
6248 cpos += num_clusters;
6249 }
6250 }
6251
6252out:
6253 return ret;
6254}
6255
6256static int ocfs2_reflink_xattr_inline(struct ocfs2_xattr_reflink *args)
6257{
6258 int ret = 0, credits = 0;
6259 handle_t *handle;
6260 struct ocfs2_super *osb = OCFS2_SB(args->old_inode->i_sb);
6261 struct ocfs2_dinode *di = (struct ocfs2_dinode *)args->old_bh->b_data;
6262 int inline_size = le16_to_cpu(di->i_xattr_inline_size);
6263 int header_off = osb->sb->s_blocksize - inline_size;
6264 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)
6265 (args->old_bh->b_data + header_off);
6266 struct ocfs2_xattr_header *new_xh = (struct ocfs2_xattr_header *)
6267 (args->new_bh->b_data + header_off);
6268 struct ocfs2_alloc_context *meta_ac = NULL;
6269 struct ocfs2_inode_info *new_oi;
6270 struct ocfs2_dinode *new_di;
6271 struct ocfs2_xattr_value_buf vb = {
6272 .vb_bh = args->new_bh,
6273 .vb_access = ocfs2_journal_access_di,
6274 };
6275
6276 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6277 &credits, &meta_ac);
6278 if (ret) {
6279 mlog_errno(ret);
6280 goto out;
6281 }
6282
6283 handle = ocfs2_start_trans(osb, credits);
6284 if (IS_ERR(handle)) {
6285 ret = PTR_ERR(handle);
6286 mlog_errno(ret);
6287 goto out;
6288 }
6289
6290 ret = ocfs2_journal_access_di(handle, INODE_CACHE(args->new_inode),
6291 args->new_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6292 if (ret) {
6293 mlog_errno(ret);
6294 goto out_commit;
6295 }
6296
6297 memcpy(args->new_bh->b_data + header_off,
6298 args->old_bh->b_data + header_off, inline_size);
6299
6300 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6301 new_di->i_xattr_inline_size = cpu_to_le16(inline_size);
6302
6303 ret = ocfs2_reflink_xattr_header(handle, args, args->old_bh, xh,
6304 args->new_bh, new_xh, &vb, meta_ac,
6305 ocfs2_get_xattr_value_root, NULL);
6306 if (ret) {
6307 mlog_errno(ret);
6308 goto out_commit;
6309 }
6310
6311 new_oi = OCFS2_I(args->new_inode);
6312 spin_lock(&new_oi->ip_lock);
6313 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL | OCFS2_INLINE_XATTR_FL;
6314 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6315 spin_unlock(&new_oi->ip_lock);
6316
6317 ocfs2_journal_dirty(handle, args->new_bh);
6318
6319out_commit:
6320 ocfs2_commit_trans(osb, handle);
6321
6322out:
6323 if (meta_ac)
6324 ocfs2_free_alloc_context(meta_ac);
6325 return ret;
6326}
6327
6328static int ocfs2_create_empty_xattr_block(struct inode *inode,
6329 struct buffer_head *fe_bh,
6330 struct buffer_head **ret_bh,
6331 int indexed)
6332{
6333 int ret;
6334 handle_t *handle;
6335 struct ocfs2_alloc_context *meta_ac;
6336 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6337
6338 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
6339 if (ret < 0) {
6340 mlog_errno(ret);
6341 return ret;
6342 }
6343
6344 handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_CREATE_CREDITS);
6345 if (IS_ERR(handle)) {
6346 ret = PTR_ERR(handle);
6347 mlog_errno(ret);
6348 goto out;
6349 }
6350
6351 mlog(0, "create new xattr block for inode %llu, index = %d\n",
6352 (unsigned long long)fe_bh->b_blocknr, indexed);
6353 ret = ocfs2_create_xattr_block(handle, inode, fe_bh,
6354 meta_ac, ret_bh, indexed);
6355 if (ret)
6356 mlog_errno(ret);
6357
6358 ocfs2_commit_trans(osb, handle);
6359out:
6360 ocfs2_free_alloc_context(meta_ac);
6361 return ret;
6362}
6363
6364static int ocfs2_reflink_xattr_block(struct ocfs2_xattr_reflink *args,
6365 struct buffer_head *blk_bh,
6366 struct buffer_head *new_blk_bh)
6367{
6368 int ret = 0, credits = 0;
6369 handle_t *handle;
6370 struct ocfs2_inode_info *new_oi = OCFS2_I(args->new_inode);
6371 struct ocfs2_dinode *new_di;
6372 struct ocfs2_super *osb = OCFS2_SB(args->new_inode->i_sb);
6373 int header_off = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
6374 struct ocfs2_xattr_block *xb =
6375 (struct ocfs2_xattr_block *)blk_bh->b_data;
6376 struct ocfs2_xattr_header *xh = &xb->xb_attrs.xb_header;
6377 struct ocfs2_xattr_block *new_xb =
6378 (struct ocfs2_xattr_block *)new_blk_bh->b_data;
6379 struct ocfs2_xattr_header *new_xh = &new_xb->xb_attrs.xb_header;
6380 struct ocfs2_alloc_context *meta_ac;
6381 struct ocfs2_xattr_value_buf vb = {
6382 .vb_bh = new_blk_bh,
6383 .vb_access = ocfs2_journal_access_xb,
6384 };
6385
6386 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6387 &credits, &meta_ac);
6388 if (ret) {
6389 mlog_errno(ret);
6390 return ret;
6391 }
6392
6393 /* One more credits in case we need to add xattr flags in new inode. */
6394 handle = ocfs2_start_trans(osb, credits + 1);
6395 if (IS_ERR(handle)) {
6396 ret = PTR_ERR(handle);
6397 mlog_errno(ret);
6398 goto out;
6399 }
6400
6401 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6402 ret = ocfs2_journal_access_di(handle,
6403 INODE_CACHE(args->new_inode),
6404 args->new_bh,
6405 OCFS2_JOURNAL_ACCESS_WRITE);
6406 if (ret) {
6407 mlog_errno(ret);
6408 goto out_commit;
6409 }
6410 }
6411
6412 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(args->new_inode),
6413 new_blk_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6414 if (ret) {
6415 mlog_errno(ret);
6416 goto out_commit;
6417 }
6418
6419 memcpy(new_blk_bh->b_data + header_off, blk_bh->b_data + header_off,
6420 osb->sb->s_blocksize - header_off);
6421
6422 ret = ocfs2_reflink_xattr_header(handle, args, blk_bh, xh,
6423 new_blk_bh, new_xh, &vb, meta_ac,
6424 ocfs2_get_xattr_value_root, NULL);
6425 if (ret) {
6426 mlog_errno(ret);
6427 goto out_commit;
6428 }
6429
6430 ocfs2_journal_dirty(handle, new_blk_bh);
6431
6432 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6433 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6434 spin_lock(&new_oi->ip_lock);
6435 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
6436 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6437 spin_unlock(&new_oi->ip_lock);
6438
6439 ocfs2_journal_dirty(handle, args->new_bh);
6440 }
6441
6442out_commit:
6443 ocfs2_commit_trans(osb, handle);
6444
6445out:
6446 ocfs2_free_alloc_context(meta_ac);
6447 return ret;
6448}
6449
6450struct ocfs2_reflink_xattr_tree_args {
6451 struct ocfs2_xattr_reflink *reflink;
6452 struct buffer_head *old_blk_bh;
6453 struct buffer_head *new_blk_bh;
6454 struct ocfs2_xattr_bucket *old_bucket;
6455 struct ocfs2_xattr_bucket *new_bucket;
6456};
6457
6458/*
6459 * NOTE:
6460 * We have to handle the case that both old bucket and new bucket
6461 * will call this function to get the right ret_bh.
6462 * So The caller must give us the right bh.
6463 */
6464static int ocfs2_get_reflink_xattr_value_root(struct super_block *sb,
6465 struct buffer_head *bh,
6466 struct ocfs2_xattr_header *xh,
6467 int offset,
6468 struct ocfs2_xattr_value_root **xv,
6469 struct buffer_head **ret_bh,
6470 void *para)
6471{
6472 struct ocfs2_reflink_xattr_tree_args *args =
6473 (struct ocfs2_reflink_xattr_tree_args *)para;
6474 struct ocfs2_xattr_bucket *bucket;
6475
6476 if (bh == args->old_bucket->bu_bhs[0])
6477 bucket = args->old_bucket;
6478 else
6479 bucket = args->new_bucket;
6480
6481 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6482 xv, ret_bh);
6483}
6484
6485struct ocfs2_value_tree_metas {
6486 int num_metas;
6487 int credits;
6488 int num_recs;
6489};
6490
6491static int ocfs2_value_tree_metas_in_bucket(struct super_block *sb,
6492 struct buffer_head *bh,
6493 struct ocfs2_xattr_header *xh,
6494 int offset,
6495 struct ocfs2_xattr_value_root **xv,
6496 struct buffer_head **ret_bh,
6497 void *para)
6498{
6499 struct ocfs2_xattr_bucket *bucket =
6500 (struct ocfs2_xattr_bucket *)para;
6501
6502 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6503 xv, ret_bh);
6504}
6505
6506static int ocfs2_calc_value_tree_metas(struct inode *inode,
6507 struct ocfs2_xattr_bucket *bucket,
6508 void *para)
6509{
6510 struct ocfs2_value_tree_metas *metas =
6511 (struct ocfs2_value_tree_metas *)para;
6512 struct ocfs2_xattr_header *xh =
6513 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6514
6515 /* Add the credits for this bucket first. */
6516 metas->credits += bucket->bu_blocks;
6517 return ocfs2_value_metas_in_xattr_header(inode->i_sb, bucket->bu_bhs[0],
6518 xh, &metas->num_metas,
6519 &metas->credits, &metas->num_recs,
6520 ocfs2_value_tree_metas_in_bucket,
6521 bucket);
6522}
6523
6524/*
6525 * Given a xattr extent rec starting from blkno and having len clusters,
6526 * iterate all the buckets calculate how much metadata we need for reflinking
6527 * all the ocfs2_xattr_value_root and lock the allocators accordingly.
6528 */
6529static int ocfs2_lock_reflink_xattr_rec_allocators(
6530 struct ocfs2_reflink_xattr_tree_args *args,
6531 struct ocfs2_extent_tree *xt_et,
6532 u64 blkno, u32 len, int *credits,
6533 struct ocfs2_alloc_context **meta_ac,
6534 struct ocfs2_alloc_context **data_ac)
6535{
6536 int ret, num_free_extents;
6537 struct ocfs2_value_tree_metas metas;
6538 struct ocfs2_super *osb = OCFS2_SB(args->reflink->old_inode->i_sb);
6539 struct ocfs2_refcount_block *rb;
6540
6541 memset(&metas, 0, sizeof(metas));
6542
6543 ret = ocfs2_iterate_xattr_buckets(args->reflink->old_inode, blkno, len,
6544 ocfs2_calc_value_tree_metas, &metas);
6545 if (ret) {
6546 mlog_errno(ret);
6547 goto out;
6548 }
6549
6550 *credits = metas.credits;
6551
6552 /*
6553 * Calculate we need for refcount tree change.
6554 *
6555 * We need to add/modify num_recs in refcount tree, so just calculate
6556 * an approximate number we need for refcount tree change.
6557 * Sometimes we need to split the tree, and after split, half recs
6558 * will be moved to the new block, and a new block can only provide
6559 * half number of recs. So we multiple new blocks by 2.
6560 * In the end, we have to add credits for modifying the already
6561 * existed refcount block.
6562 */
6563 rb = (struct ocfs2_refcount_block *)args->reflink->ref_root_bh->b_data;
6564 metas.num_recs =
6565 (metas.num_recs + ocfs2_refcount_recs_per_rb(osb->sb) - 1) /
6566 ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6567 metas.num_metas += metas.num_recs;
6568 *credits += metas.num_recs +
6569 metas.num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6570 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6571 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6572 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6573 else
6574 *credits += 1;
6575
6576 /* count in the xattr tree change. */
6577 num_free_extents = ocfs2_num_free_extents(osb, xt_et);
6578 if (num_free_extents < 0) {
6579 ret = num_free_extents;
6580 mlog_errno(ret);
6581 goto out;
6582 }
6583
6584 if (num_free_extents < len)
6585 metas.num_metas += ocfs2_extend_meta_needed(xt_et->et_root_el);
6586
6587 *credits += ocfs2_calc_extend_credits(osb->sb,
6588 xt_et->et_root_el, len);
6589
6590 if (metas.num_metas) {
6591 ret = ocfs2_reserve_new_metadata_blocks(osb, metas.num_metas,
6592 meta_ac);
6593 if (ret) {
6594 mlog_errno(ret);
6595 goto out;
6596 }
6597 }
6598
6599 if (len) {
6600 ret = ocfs2_reserve_clusters(osb, len, data_ac);
6601 if (ret)
6602 mlog_errno(ret);
6603 }
6604out:
6605 if (ret) {
6606 if (*meta_ac) {
6607 ocfs2_free_alloc_context(*meta_ac);
6608 meta_ac = NULL;
6609 }
6610 }
6611
6612 return ret;
6613}
6614
6615static int ocfs2_reflink_xattr_buckets(handle_t *handle,
6616 u64 blkno, u64 new_blkno, u32 clusters,
6617 struct ocfs2_alloc_context *meta_ac,
6618 struct ocfs2_alloc_context *data_ac,
6619 struct ocfs2_reflink_xattr_tree_args *args)
6620{
6621 int i, j, ret = 0;
6622 struct super_block *sb = args->reflink->old_inode->i_sb;
6623 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
6624 u32 num_buckets = clusters * bpc;
6625 int bpb = args->old_bucket->bu_blocks;
6626 struct ocfs2_xattr_value_buf vb = {
6627 .vb_access = ocfs2_journal_access,
6628 };
6629
6630 for (i = 0; i < num_buckets; i++, blkno += bpb, new_blkno += bpb) {
6631 ret = ocfs2_read_xattr_bucket(args->old_bucket, blkno);
6632 if (ret) {
6633 mlog_errno(ret);
6634 break;
6635 }
6636
6637 ret = ocfs2_init_xattr_bucket(args->new_bucket, new_blkno);
6638 if (ret) {
6639 mlog_errno(ret);
6640 break;
6641 }
6642
6643 /*
6644 * The real bucket num in this series of blocks is stored
6645 * in the 1st bucket.
6646 */
6647 if (i == 0)
6648 num_buckets = le16_to_cpu(
6649 bucket_xh(args->old_bucket)->xh_num_buckets);
6650
6651 ret = ocfs2_xattr_bucket_journal_access(handle,
6652 args->new_bucket,
6653 OCFS2_JOURNAL_ACCESS_CREATE);
6654 if (ret) {
6655 mlog_errno(ret);
6656 break;
6657 }
6658
6659 for (j = 0; j < bpb; j++)
6660 memcpy(bucket_block(args->new_bucket, j),
6661 bucket_block(args->old_bucket, j),
6662 sb->s_blocksize);
6663
6664 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
6665
6666 ret = ocfs2_reflink_xattr_header(handle, args->reflink,
6667 args->old_bucket->bu_bhs[0],
6668 bucket_xh(args->old_bucket),
6669 args->new_bucket->bu_bhs[0],
6670 bucket_xh(args->new_bucket),
6671 &vb, meta_ac,
6672 ocfs2_get_reflink_xattr_value_root,
6673 args);
6674 if (ret) {
6675 mlog_errno(ret);
6676 break;
6677 }
6678
6679 /*
6680 * Re-access and dirty the bucket to calculate metaecc.
6681 * Because we may extend the transaction in reflink_xattr_header
6682 * which will let the already accessed block gone.
6683 */
6684 ret = ocfs2_xattr_bucket_journal_access(handle,
6685 args->new_bucket,
6686 OCFS2_JOURNAL_ACCESS_WRITE);
6687 if (ret) {
6688 mlog_errno(ret);
6689 break;
6690 }
6691
6692 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
6693 ocfs2_xattr_bucket_relse(args->old_bucket);
6694 ocfs2_xattr_bucket_relse(args->new_bucket);
6695 }
6696
6697 ocfs2_xattr_bucket_relse(args->old_bucket);
6698 ocfs2_xattr_bucket_relse(args->new_bucket);
6699 return ret;
6700}
6701/*
6702 * Create the same xattr extent record in the new inode's xattr tree.
6703 */
6704static int ocfs2_reflink_xattr_rec(struct inode *inode,
6705 struct buffer_head *root_bh,
6706 u64 blkno,
6707 u32 cpos,
6708 u32 len,
6709 void *para)
6710{
6711 int ret, credits = 0;
6712 u32 p_cluster, num_clusters;
6713 u64 new_blkno;
6714 handle_t *handle;
6715 struct ocfs2_reflink_xattr_tree_args *args =
6716 (struct ocfs2_reflink_xattr_tree_args *)para;
6717 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6718 struct ocfs2_alloc_context *meta_ac = NULL;
6719 struct ocfs2_alloc_context *data_ac = NULL;
6720 struct ocfs2_extent_tree et;
6721
6722 ocfs2_init_xattr_tree_extent_tree(&et,
6723 INODE_CACHE(args->reflink->new_inode),
6724 args->new_blk_bh);
6725
6726 ret = ocfs2_lock_reflink_xattr_rec_allocators(args, &et, blkno,
6727 len, &credits,
6728 &meta_ac, &data_ac);
6729 if (ret) {
6730 mlog_errno(ret);
6731 goto out;
6732 }
6733
6734 handle = ocfs2_start_trans(osb, credits);
6735 if (IS_ERR(handle)) {
6736 ret = PTR_ERR(handle);
6737 mlog_errno(ret);
6738 goto out;
6739 }
6740
6741 ret = ocfs2_claim_clusters(osb, handle, data_ac,
6742 len, &p_cluster, &num_clusters);
6743 if (ret) {
6744 mlog_errno(ret);
6745 goto out_commit;
6746 }
6747
6748 new_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cluster);
6749
6750 mlog(0, "reflink xattr buckets %llu to %llu, len %u\n",
6751 (unsigned long long)blkno, (unsigned long long)new_blkno, len);
6752 ret = ocfs2_reflink_xattr_buckets(handle, blkno, new_blkno, len,
6753 meta_ac, data_ac, args);
6754 if (ret) {
6755 mlog_errno(ret);
6756 goto out_commit;
6757 }
6758
6759 mlog(0, "insert new xattr extent rec start %llu len %u to %u\n",
6760 (unsigned long long)new_blkno, len, cpos);
6761 ret = ocfs2_insert_extent(handle, &et, cpos, new_blkno,
6762 len, 0, meta_ac);
6763 if (ret)
6764 mlog_errno(ret);
6765
6766out_commit:
6767 ocfs2_commit_trans(osb, handle);
6768
6769out:
6770 if (meta_ac)
6771 ocfs2_free_alloc_context(meta_ac);
6772 if (data_ac)
6773 ocfs2_free_alloc_context(data_ac);
6774 return ret;
6775}
6776
6777/*
6778 * Create reflinked xattr buckets.
6779 * We will add bucket one by one, and refcount all the xattrs in the bucket
6780 * if they are stored outside.
6781 */
6782static int ocfs2_reflink_xattr_tree(struct ocfs2_xattr_reflink *args,
6783 struct buffer_head *blk_bh,
6784 struct buffer_head *new_blk_bh)
6785{
6786 int ret;
6787 struct ocfs2_reflink_xattr_tree_args para;
6788
6789 memset(&para, 0, sizeof(para));
6790 para.reflink = args;
6791 para.old_blk_bh = blk_bh;
6792 para.new_blk_bh = new_blk_bh;
6793
6794 para.old_bucket = ocfs2_xattr_bucket_new(args->old_inode);
6795 if (!para.old_bucket) {
6796 mlog_errno(-ENOMEM);
6797 return -ENOMEM;
6798 }
6799
6800 para.new_bucket = ocfs2_xattr_bucket_new(args->new_inode);
6801 if (!para.new_bucket) {
6802 ret = -ENOMEM;
6803 mlog_errno(ret);
6804 goto out;
6805 }
6806
6807 ret = ocfs2_iterate_xattr_index_block(args->old_inode, blk_bh,
6808 ocfs2_reflink_xattr_rec,
6809 &para);
6810 if (ret)
6811 mlog_errno(ret);
6812
6813out:
6814 ocfs2_xattr_bucket_free(para.old_bucket);
6815 ocfs2_xattr_bucket_free(para.new_bucket);
6816 return ret;
6817}
6818
6819static int ocfs2_reflink_xattr_in_block(struct ocfs2_xattr_reflink *args,
6820 struct buffer_head *blk_bh)
6821{
6822 int ret, indexed = 0;
6823 struct buffer_head *new_blk_bh = NULL;
6824 struct ocfs2_xattr_block *xb =
6825 (struct ocfs2_xattr_block *)blk_bh->b_data;
6826
6827
6828 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)
6829 indexed = 1;
6830
6831 ret = ocfs2_create_empty_xattr_block(args->new_inode, args->new_bh,
6832 &new_blk_bh, indexed);
6833 if (ret) {
6834 mlog_errno(ret);
6835 goto out;
6836 }
6837
6838 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED))
6839 ret = ocfs2_reflink_xattr_block(args, blk_bh, new_blk_bh);
6840 else
6841 ret = ocfs2_reflink_xattr_tree(args, blk_bh, new_blk_bh);
6842 if (ret)
6843 mlog_errno(ret);
6844
6845out:
6846 brelse(new_blk_bh);
6847 return ret;
6848}
6849
6850int ocfs2_reflink_xattrs(struct inode *old_inode,
6851 struct buffer_head *old_bh,
6852 struct inode *new_inode,
6853 struct buffer_head *new_bh)
6854{
6855 int ret;
6856 struct ocfs2_xattr_reflink args;
6857 struct ocfs2_inode_info *oi = OCFS2_I(old_inode);
6858 struct ocfs2_dinode *di = (struct ocfs2_dinode *)old_bh->b_data;
6859 struct buffer_head *blk_bh = NULL;
6860 struct ocfs2_cached_dealloc_ctxt dealloc;
6861 struct ocfs2_refcount_tree *ref_tree;
6862 struct buffer_head *ref_root_bh = NULL;
6863
6864 ret = ocfs2_lock_refcount_tree(OCFS2_SB(old_inode->i_sb),
6865 le64_to_cpu(di->i_refcount_loc),
6866 1, &ref_tree, &ref_root_bh);
6867 if (ret) {
6868 mlog_errno(ret);
6869 goto out;
6870 }
6871
6872 ocfs2_init_dealloc_ctxt(&dealloc);
6873
6874 args.old_inode = old_inode;
6875 args.new_inode = new_inode;
6876 args.old_bh = old_bh;
6877 args.new_bh = new_bh;
6878 args.ref_ci = &ref_tree->rf_ci;
6879 args.ref_root_bh = ref_root_bh;
6880 args.dealloc = &dealloc;
6881
6882 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
6883 ret = ocfs2_reflink_xattr_inline(&args);
6884 if (ret) {
6885 mlog_errno(ret);
6886 goto out_unlock;
6887 }
6888 }
6889
6890 if (!di->i_xattr_loc)
6891 goto out_unlock;
6892
6893 ret = ocfs2_read_xattr_block(old_inode, le64_to_cpu(di->i_xattr_loc),
6894 &blk_bh);
6895 if (ret < 0) {
6896 mlog_errno(ret);
6897 goto out_unlock;
6898 }
6899
6900 ret = ocfs2_reflink_xattr_in_block(&args, blk_bh);
6901 if (ret)
6902 mlog_errno(ret);
6903
6904 brelse(blk_bh);
6905
6906out_unlock:
6907 ocfs2_unlock_refcount_tree(OCFS2_SB(old_inode->i_sb),
6908 ref_tree, 1);
6909 brelse(ref_root_bh);
6910
6911 if (ocfs2_dealloc_has_cluster(&dealloc)) {
6912 ocfs2_schedule_truncate_log_flush(OCFS2_SB(old_inode->i_sb), 1);
6913 ocfs2_run_deallocs(OCFS2_SB(old_inode->i_sb), &dealloc);
6914 }
6915
6916out:
6917 return ret;
6918}
6919
6920/*
Tiger Yang923f7f32008-11-14 11:16:27 +08006921 * 'security' attributes support
6922 */
6923static size_t ocfs2_xattr_security_list(struct inode *inode, char *list,
6924 size_t list_size, const char *name,
6925 size_t name_len)
6926{
6927 const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN;
6928 const size_t total_len = prefix_len + name_len + 1;
6929
6930 if (list && total_len <= list_size) {
6931 memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
6932 memcpy(list + prefix_len, name, name_len);
6933 list[prefix_len + name_len] = '\0';
6934 }
6935 return total_len;
6936}
6937
6938static int ocfs2_xattr_security_get(struct inode *inode, const char *name,
6939 void *buffer, size_t size)
6940{
6941 if (strcmp(name, "") == 0)
6942 return -EINVAL;
6943 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_SECURITY, name,
6944 buffer, size);
6945}
6946
6947static int ocfs2_xattr_security_set(struct inode *inode, const char *name,
6948 const void *value, size_t size, int flags)
6949{
6950 if (strcmp(name, "") == 0)
6951 return -EINVAL;
6952
6953 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY, name, value,
6954 size, flags);
6955}
6956
Tiger Yang534eadd2008-11-14 11:16:41 +08006957int ocfs2_init_security_get(struct inode *inode,
6958 struct inode *dir,
6959 struct ocfs2_security_xattr_info *si)
6960{
Tiger Yang38d59ef2008-12-17 10:22:56 +08006961 /* check whether ocfs2 support feature xattr */
6962 if (!ocfs2_supports_xattr(OCFS2_SB(dir->i_sb)))
6963 return -EOPNOTSUPP;
Tiger Yang534eadd2008-11-14 11:16:41 +08006964 return security_inode_init_security(inode, dir, &si->name, &si->value,
6965 &si->value_len);
6966}
6967
6968int ocfs2_init_security_set(handle_t *handle,
6969 struct inode *inode,
6970 struct buffer_head *di_bh,
6971 struct ocfs2_security_xattr_info *si,
6972 struct ocfs2_alloc_context *xattr_ac,
6973 struct ocfs2_alloc_context *data_ac)
6974{
6975 return ocfs2_xattr_set_handle(handle, inode, di_bh,
6976 OCFS2_XATTR_INDEX_SECURITY,
6977 si->name, si->value, si->value_len, 0,
6978 xattr_ac, data_ac);
6979}
6980
Tiger Yang923f7f32008-11-14 11:16:27 +08006981struct xattr_handler ocfs2_xattr_security_handler = {
6982 .prefix = XATTR_SECURITY_PREFIX,
6983 .list = ocfs2_xattr_security_list,
6984 .get = ocfs2_xattr_security_get,
6985 .set = ocfs2_xattr_security_set,
6986};
6987
6988/*
Mark Fasheh99219ae2008-10-07 14:52:59 -07006989 * 'trusted' attributes support
6990 */
Mark Fasheh99219ae2008-10-07 14:52:59 -07006991static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list,
6992 size_t list_size, const char *name,
6993 size_t name_len)
6994{
Tiger Yangceb1eba2008-10-23 16:34:13 +08006995 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
Mark Fasheh99219ae2008-10-07 14:52:59 -07006996 const size_t total_len = prefix_len + name_len + 1;
6997
6998 if (list && total_len <= list_size) {
6999 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
7000 memcpy(list + prefix_len, name, name_len);
7001 list[prefix_len + name_len] = '\0';
7002 }
7003 return total_len;
7004}
7005
7006static int ocfs2_xattr_trusted_get(struct inode *inode, const char *name,
7007 void *buffer, size_t size)
7008{
7009 if (strcmp(name, "") == 0)
7010 return -EINVAL;
7011 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, name,
7012 buffer, size);
7013}
7014
7015static int ocfs2_xattr_trusted_set(struct inode *inode, const char *name,
7016 const void *value, size_t size, int flags)
7017{
7018 if (strcmp(name, "") == 0)
7019 return -EINVAL;
7020
7021 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, name, value,
7022 size, flags);
7023}
7024
7025struct xattr_handler ocfs2_xattr_trusted_handler = {
7026 .prefix = XATTR_TRUSTED_PREFIX,
7027 .list = ocfs2_xattr_trusted_list,
7028 .get = ocfs2_xattr_trusted_get,
7029 .set = ocfs2_xattr_trusted_set,
7030};
7031
Mark Fasheh99219ae2008-10-07 14:52:59 -07007032/*
7033 * 'user' attributes support
7034 */
Mark Fasheh99219ae2008-10-07 14:52:59 -07007035static size_t ocfs2_xattr_user_list(struct inode *inode, char *list,
7036 size_t list_size, const char *name,
7037 size_t name_len)
7038{
Tiger Yangceb1eba2008-10-23 16:34:13 +08007039 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
Mark Fasheh99219ae2008-10-07 14:52:59 -07007040 const size_t total_len = prefix_len + name_len + 1;
7041 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7042
7043 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7044 return 0;
7045
7046 if (list && total_len <= list_size) {
7047 memcpy(list, XATTR_USER_PREFIX, prefix_len);
7048 memcpy(list + prefix_len, name, name_len);
7049 list[prefix_len + name_len] = '\0';
7050 }
7051 return total_len;
7052}
7053
7054static int ocfs2_xattr_user_get(struct inode *inode, const char *name,
7055 void *buffer, size_t size)
7056{
7057 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7058
7059 if (strcmp(name, "") == 0)
7060 return -EINVAL;
7061 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7062 return -EOPNOTSUPP;
7063 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name,
7064 buffer, size);
7065}
7066
7067static int ocfs2_xattr_user_set(struct inode *inode, const char *name,
7068 const void *value, size_t size, int flags)
7069{
7070 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7071
7072 if (strcmp(name, "") == 0)
7073 return -EINVAL;
7074 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7075 return -EOPNOTSUPP;
7076
7077 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, name, value,
7078 size, flags);
7079}
7080
7081struct xattr_handler ocfs2_xattr_user_handler = {
7082 .prefix = XATTR_USER_PREFIX,
7083 .list = ocfs2_xattr_user_list,
7084 .get = ocfs2_xattr_user_get,
7085 .set = ocfs2_xattr_user_set,
7086};