blob: fbec116102230efe21a1e5f4571bba4f804ace15 [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"
Tao Ma0fe9b662009-08-18 11:47:56 +080059#include "acl.h"
Tiger Yangcf1d6c72008-08-18 17:11:00 +080060
61struct ocfs2_xattr_def_value_root {
62 struct ocfs2_xattr_value_root xv;
63 struct ocfs2_extent_rec er;
64};
65
Tao Ma0c044f02008-08-18 17:38:50 +080066struct ocfs2_xattr_bucket {
Joel Beckerba937122008-10-24 19:13:20 -070067 /* The inode these xattrs are associated with */
68 struct inode *bu_inode;
69
70 /* The actual buffers that make up the bucket */
Joel Becker4ac60322008-10-18 19:11:42 -070071 struct buffer_head *bu_bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
Joel Beckerba937122008-10-24 19:13:20 -070072
73 /* How many blocks make up one bucket for this filesystem */
74 int bu_blocks;
Tao Ma0c044f02008-08-18 17:38:50 +080075};
76
Tao Ma78f30c32008-11-12 08:27:00 +080077struct ocfs2_xattr_set_ctxt {
Tao Ma85db90e2008-11-12 08:27:01 +080078 handle_t *handle;
Tao Ma78f30c32008-11-12 08:27:00 +080079 struct ocfs2_alloc_context *meta_ac;
80 struct ocfs2_alloc_context *data_ac;
81 struct ocfs2_cached_dealloc_ctxt dealloc;
82};
83
Tiger Yangcf1d6c72008-08-18 17:11:00 +080084#define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
85#define OCFS2_XATTR_INLINE_SIZE 80
Tiger Yang4442f512009-02-20 11:11:50 +080086#define OCFS2_XATTR_HEADER_GAP 4
Tiger Yang534eadd2008-11-14 11:16:41 +080087#define OCFS2_XATTR_FREE_IN_IBODY (OCFS2_MIN_XATTR_INLINE_SIZE \
88 - sizeof(struct ocfs2_xattr_header) \
Tiger Yang4442f512009-02-20 11:11:50 +080089 - OCFS2_XATTR_HEADER_GAP)
Tiger Yang89c38bd2008-11-14 11:17:41 +080090#define OCFS2_XATTR_FREE_IN_BLOCK(ptr) ((ptr)->i_sb->s_blocksize \
91 - sizeof(struct ocfs2_xattr_block) \
92 - sizeof(struct ocfs2_xattr_header) \
Tiger Yang4442f512009-02-20 11:11:50 +080093 - OCFS2_XATTR_HEADER_GAP)
Tiger Yangcf1d6c72008-08-18 17:11:00 +080094
95static struct ocfs2_xattr_def_value_root def_xv = {
96 .xv.xr_list.l_count = cpu_to_le16(1),
97};
98
99struct xattr_handler *ocfs2_xattr_handlers[] = {
100 &ocfs2_xattr_user_handler,
Tiger Yang929fb012008-11-14 11:17:04 +0800101 &ocfs2_xattr_acl_access_handler,
102 &ocfs2_xattr_acl_default_handler,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800103 &ocfs2_xattr_trusted_handler,
Tiger Yang923f7f32008-11-14 11:16:27 +0800104 &ocfs2_xattr_security_handler,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800105 NULL
106};
107
Tiger Yangc988fd02008-10-23 16:34:44 +0800108static struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800109 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
Tiger Yang929fb012008-11-14 11:17:04 +0800110 [OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS]
111 = &ocfs2_xattr_acl_access_handler,
112 [OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT]
113 = &ocfs2_xattr_acl_default_handler,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800114 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
Tiger Yang923f7f32008-11-14 11:16:27 +0800115 [OCFS2_XATTR_INDEX_SECURITY] = &ocfs2_xattr_security_handler,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800116};
117
118struct ocfs2_xattr_info {
Joel Becker6b240ff2009-08-14 18:02:52 -0700119 int xi_name_index;
120 const char *xi_name;
Joel Becker18853b92009-08-14 18:17:07 -0700121 int xi_name_len;
Joel Becker6b240ff2009-08-14 18:02:52 -0700122 const void *xi_value;
123 size_t xi_value_len;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800124};
125
126struct ocfs2_xattr_search {
127 struct buffer_head *inode_bh;
128 /*
129 * xattr_bh point to the block buffer head which has extended attribute
130 * when extended attribute in inode, xattr_bh is equal to inode_bh.
131 */
132 struct buffer_head *xattr_bh;
133 struct ocfs2_xattr_header *header;
Joel Beckerba937122008-10-24 19:13:20 -0700134 struct ocfs2_xattr_bucket *bucket;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800135 void *base;
136 void *end;
137 struct ocfs2_xattr_entry *here;
138 int not_found;
139};
140
Joel Becker11179f22009-08-14 16:07:44 -0700141/* Operations on struct ocfs2_xa_entry */
142struct ocfs2_xa_loc;
143struct ocfs2_xa_loc_operations {
144 /*
Joel Beckercf2bc802009-08-18 13:52:38 -0700145 * Journal functions
146 */
147 int (*xlo_journal_access)(handle_t *handle, struct ocfs2_xa_loc *loc,
148 int type);
149 void (*xlo_journal_dirty)(handle_t *handle, struct ocfs2_xa_loc *loc);
150
151 /*
Joel Becker11179f22009-08-14 16:07:44 -0700152 * Return a pointer to the appropriate buffer in loc->xl_storage
153 * at the given offset from loc->xl_header.
154 */
155 void *(*xlo_offset_pointer)(struct ocfs2_xa_loc *loc, int offset);
156
Joel Becker69a3e532009-08-17 12:24:39 -0700157 /* Can we reuse the existing entry for the new value? */
158 int (*xlo_can_reuse)(struct ocfs2_xa_loc *loc,
159 struct ocfs2_xattr_info *xi);
160
161 /* How much space is needed for the new value? */
162 int (*xlo_check_space)(struct ocfs2_xa_loc *loc,
163 struct ocfs2_xattr_info *xi);
164
165 /*
166 * Return the offset of the first name+value pair. This is
167 * the start of our downward-filling free space.
168 */
169 int (*xlo_get_free_start)(struct ocfs2_xa_loc *loc);
170
Joel Becker11179f22009-08-14 16:07:44 -0700171 /*
172 * Remove the name+value at this location. Do whatever is
173 * appropriate with the remaining name+value pairs.
174 */
175 void (*xlo_wipe_namevalue)(struct ocfs2_xa_loc *loc);
Joel Becker69a3e532009-08-17 12:24:39 -0700176
177 /* Fill xl_entry with a new entry */
178 void (*xlo_add_entry)(struct ocfs2_xa_loc *loc, u32 name_hash);
179
180 /* Add name+value storage to an entry */
181 void (*xlo_add_namevalue)(struct ocfs2_xa_loc *loc, int size);
Joel Becker3fc12af2009-08-18 13:20:27 -0700182
183 /*
184 * Initialize the value buf's access and bh fields for this entry.
185 * ocfs2_xa_fill_value_buf() will handle the xv pointer.
186 */
187 void (*xlo_fill_value_buf)(struct ocfs2_xa_loc *loc,
188 struct ocfs2_xattr_value_buf *vb);
Joel Becker11179f22009-08-14 16:07:44 -0700189};
190
191/*
192 * Describes an xattr entry location. This is a memory structure
193 * tracking the on-disk structure.
194 */
195struct ocfs2_xa_loc {
Joel Beckercf2bc802009-08-18 13:52:38 -0700196 /* This xattr belongs to this inode */
197 struct inode *xl_inode;
198
Joel Becker11179f22009-08-14 16:07:44 -0700199 /* The ocfs2_xattr_header inside the on-disk storage. Not NULL. */
200 struct ocfs2_xattr_header *xl_header;
201
202 /* Bytes from xl_header to the end of the storage */
203 int xl_size;
204
205 /*
206 * The ocfs2_xattr_entry this location describes. If this is
207 * NULL, this location describes the on-disk structure where it
208 * would have been.
209 */
210 struct ocfs2_xattr_entry *xl_entry;
211
212 /*
213 * Internal housekeeping
214 */
215
216 /* Buffer(s) containing this entry */
217 void *xl_storage;
218
219 /* Operations on the storage backing this location */
220 const struct ocfs2_xa_loc_operations *xl_ops;
221};
222
Joel Becker199799a2009-08-14 19:04:15 -0700223/*
224 * Convenience functions to calculate how much space is needed for a
225 * given name+value pair
226 */
227static int namevalue_size(int name_len, uint64_t value_len)
228{
229 if (value_len > OCFS2_XATTR_INLINE_SIZE)
230 return OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
231 else
232 return OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
233}
234
235static int namevalue_size_xi(struct ocfs2_xattr_info *xi)
236{
237 return namevalue_size(xi->xi_name_len, xi->xi_value_len);
238}
239
240static int namevalue_size_xe(struct ocfs2_xattr_entry *xe)
241{
242 u64 value_len = le64_to_cpu(xe->xe_value_size);
243
244 BUG_ON((value_len > OCFS2_XATTR_INLINE_SIZE) &&
245 ocfs2_xattr_is_local(xe));
246 return namevalue_size(xe->xe_name_len, value_len);
247}
248
249
Tao Mafd68a892009-08-18 11:43:21 +0800250static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
Tao Ma589dc262008-08-18 17:38:51 +0800251 struct ocfs2_xattr_header *xh,
252 int index,
253 int *block_off,
254 int *new_offset);
255
Joel Becker54f443f2008-10-20 18:43:07 -0700256static int ocfs2_xattr_block_find(struct inode *inode,
257 int name_index,
258 const char *name,
259 struct ocfs2_xattr_search *xs);
Tao Ma589dc262008-08-18 17:38:51 +0800260static int ocfs2_xattr_index_block_find(struct inode *inode,
261 struct buffer_head *root_bh,
262 int name_index,
263 const char *name,
264 struct ocfs2_xattr_search *xs);
265
Tao Ma0c044f02008-08-18 17:38:50 +0800266static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
Tao Ma47bca492009-08-18 11:43:42 +0800267 struct buffer_head *blk_bh,
Tao Ma0c044f02008-08-18 17:38:50 +0800268 char *buffer,
269 size_t buffer_size);
270
Tao Ma01225592008-08-18 17:38:53 +0800271static int ocfs2_xattr_create_index_block(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +0800272 struct ocfs2_xattr_search *xs,
273 struct ocfs2_xattr_set_ctxt *ctxt);
Tao Ma01225592008-08-18 17:38:53 +0800274
275static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
276 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +0800277 struct ocfs2_xattr_search *xs,
278 struct ocfs2_xattr_set_ctxt *ctxt);
Tao Ma01225592008-08-18 17:38:53 +0800279
Tao Ma47bca492009-08-18 11:43:42 +0800280typedef int (xattr_tree_rec_func)(struct inode *inode,
281 struct buffer_head *root_bh,
282 u64 blkno, u32 cpos, u32 len, void *para);
283static int ocfs2_iterate_xattr_index_block(struct inode *inode,
284 struct buffer_head *root_bh,
285 xattr_tree_rec_func *rec_func,
286 void *para);
287static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
288 struct ocfs2_xattr_bucket *bucket,
289 void *para);
290static int ocfs2_rm_xattr_cluster(struct inode *inode,
291 struct buffer_head *root_bh,
292 u64 blkno,
293 u32 cpos,
294 u32 len,
295 void *para);
296
Joel Beckerc58b6032008-11-26 13:36:24 -0800297static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
298 u64 src_blk, u64 last_blk, u64 to_blk,
299 unsigned int start_bucket,
300 u32 *first_hash);
Tao Ma492a8a32009-08-18 11:43:17 +0800301static int ocfs2_prepare_refcount_xattr(struct inode *inode,
302 struct ocfs2_dinode *di,
303 struct ocfs2_xattr_info *xi,
304 struct ocfs2_xattr_search *xis,
305 struct ocfs2_xattr_search *xbs,
306 struct ocfs2_refcount_tree **ref_tree,
307 int *meta_need,
308 int *credits);
Tao Mace9c5a52009-08-18 11:43:59 +0800309static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
310 struct ocfs2_xattr_bucket *bucket,
311 int offset,
312 struct ocfs2_xattr_value_root **xv,
313 struct buffer_head **bh);
Tao Maa3944252008-08-18 17:38:54 +0800314
Tiger Yang0030e002008-10-23 16:33:33 +0800315static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
316{
317 return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
318}
319
320static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
321{
322 return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
323}
324
325static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb)
326{
327 u16 len = sb->s_blocksize -
328 offsetof(struct ocfs2_xattr_header, xh_entries);
329
330 return len / sizeof(struct ocfs2_xattr_entry);
331}
332
Joel Becker9c7759a2008-10-24 16:21:03 -0700333#define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
Joel Becker51def392008-10-24 16:57:21 -0700334#define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
Joel Becker3e632942008-10-24 17:04:49 -0700335#define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
Joel Becker9c7759a2008-10-24 16:21:03 -0700336
Joel Beckerba937122008-10-24 19:13:20 -0700337static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
Joel Becker6dde41d2008-10-24 17:16:48 -0700338{
Joel Beckerba937122008-10-24 19:13:20 -0700339 struct ocfs2_xattr_bucket *bucket;
340 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Joel Becker6dde41d2008-10-24 17:16:48 -0700341
Joel Beckerba937122008-10-24 19:13:20 -0700342 BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
343
344 bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
345 if (bucket) {
346 bucket->bu_inode = inode;
347 bucket->bu_blocks = blks;
348 }
349
350 return bucket;
351}
352
353static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
354{
355 int i;
356
357 for (i = 0; i < bucket->bu_blocks; i++) {
Joel Becker6dde41d2008-10-24 17:16:48 -0700358 brelse(bucket->bu_bhs[i]);
359 bucket->bu_bhs[i] = NULL;
360 }
361}
362
Joel Beckerba937122008-10-24 19:13:20 -0700363static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
364{
365 if (bucket) {
366 ocfs2_xattr_bucket_relse(bucket);
367 bucket->bu_inode = NULL;
368 kfree(bucket);
369 }
370}
371
Joel Becker784b8162008-10-24 17:33:40 -0700372/*
373 * A bucket that has never been written to disk doesn't need to be
374 * read. We just need the buffer_heads. Don't call this for
375 * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
376 * them fully.
377 */
Joel Beckerba937122008-10-24 19:13:20 -0700378static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
Joel Becker784b8162008-10-24 17:33:40 -0700379 u64 xb_blkno)
380{
381 int i, rc = 0;
Joel Becker784b8162008-10-24 17:33:40 -0700382
Joel Beckerba937122008-10-24 19:13:20 -0700383 for (i = 0; i < bucket->bu_blocks; i++) {
384 bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
385 xb_blkno + i);
Joel Becker784b8162008-10-24 17:33:40 -0700386 if (!bucket->bu_bhs[i]) {
387 rc = -EIO;
388 mlog_errno(rc);
389 break;
390 }
391
Joel Becker8cb471e2009-02-10 20:00:41 -0800392 if (!ocfs2_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
Tao Ma757055a2008-11-06 08:10:48 +0800393 bucket->bu_bhs[i]))
Joel Becker8cb471e2009-02-10 20:00:41 -0800394 ocfs2_set_new_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
Tao Ma757055a2008-11-06 08:10:48 +0800395 bucket->bu_bhs[i]);
Joel Becker784b8162008-10-24 17:33:40 -0700396 }
397
398 if (rc)
Joel Beckerba937122008-10-24 19:13:20 -0700399 ocfs2_xattr_bucket_relse(bucket);
Joel Becker784b8162008-10-24 17:33:40 -0700400 return rc;
401}
402
403/* Read the xattr bucket at xb_blkno */
Joel Beckerba937122008-10-24 19:13:20 -0700404static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
Joel Becker784b8162008-10-24 17:33:40 -0700405 u64 xb_blkno)
406{
Joel Beckerba937122008-10-24 19:13:20 -0700407 int rc;
Joel Becker784b8162008-10-24 17:33:40 -0700408
Joel Becker8cb471e2009-02-10 20:00:41 -0800409 rc = ocfs2_read_blocks(INODE_CACHE(bucket->bu_inode), xb_blkno,
Joel Becker970e4932008-11-13 14:49:19 -0800410 bucket->bu_blocks, bucket->bu_bhs, 0,
411 NULL);
Joel Becker4d0e2142008-12-05 11:19:37 -0800412 if (!rc) {
Tao Mac8b9cf92009-02-24 17:40:26 -0800413 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
Joel Becker4d0e2142008-12-05 11:19:37 -0800414 rc = ocfs2_validate_meta_ecc_bhs(bucket->bu_inode->i_sb,
415 bucket->bu_bhs,
416 bucket->bu_blocks,
417 &bucket_xh(bucket)->xh_check);
Tao Mac8b9cf92009-02-24 17:40:26 -0800418 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
Joel Becker4d0e2142008-12-05 11:19:37 -0800419 if (rc)
420 mlog_errno(rc);
421 }
422
Joel Becker784b8162008-10-24 17:33:40 -0700423 if (rc)
Joel Beckerba937122008-10-24 19:13:20 -0700424 ocfs2_xattr_bucket_relse(bucket);
Joel Becker784b8162008-10-24 17:33:40 -0700425 return rc;
426}
427
Joel Becker1224be02008-10-24 18:47:33 -0700428static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
Joel Becker1224be02008-10-24 18:47:33 -0700429 struct ocfs2_xattr_bucket *bucket,
430 int type)
431{
432 int i, rc = 0;
Joel Becker1224be02008-10-24 18:47:33 -0700433
Joel Beckerba937122008-10-24 19:13:20 -0700434 for (i = 0; i < bucket->bu_blocks; i++) {
Joel Becker0cf2f762009-02-12 16:41:25 -0800435 rc = ocfs2_journal_access(handle,
436 INODE_CACHE(bucket->bu_inode),
Joel Becker1224be02008-10-24 18:47:33 -0700437 bucket->bu_bhs[i], type);
438 if (rc) {
439 mlog_errno(rc);
440 break;
441 }
442 }
443
444 return rc;
445}
446
447static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
Joel Becker1224be02008-10-24 18:47:33 -0700448 struct ocfs2_xattr_bucket *bucket)
449{
Joel Beckerba937122008-10-24 19:13:20 -0700450 int i;
Joel Becker1224be02008-10-24 18:47:33 -0700451
Tao Mac8b9cf92009-02-24 17:40:26 -0800452 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
Joel Becker4d0e2142008-12-05 11:19:37 -0800453 ocfs2_compute_meta_ecc_bhs(bucket->bu_inode->i_sb,
454 bucket->bu_bhs, bucket->bu_blocks,
455 &bucket_xh(bucket)->xh_check);
Tao Mac8b9cf92009-02-24 17:40:26 -0800456 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
Joel Becker4d0e2142008-12-05 11:19:37 -0800457
Joel Beckerba937122008-10-24 19:13:20 -0700458 for (i = 0; i < bucket->bu_blocks; i++)
Joel Becker1224be02008-10-24 18:47:33 -0700459 ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
460}
461
Joel Beckerba937122008-10-24 19:13:20 -0700462static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
Joel Becker4980c6d2008-10-24 18:54:43 -0700463 struct ocfs2_xattr_bucket *src)
464{
465 int i;
Joel Beckerba937122008-10-24 19:13:20 -0700466 int blocksize = src->bu_inode->i_sb->s_blocksize;
Joel Becker4980c6d2008-10-24 18:54:43 -0700467
Joel Beckerba937122008-10-24 19:13:20 -0700468 BUG_ON(dest->bu_blocks != src->bu_blocks);
469 BUG_ON(dest->bu_inode != src->bu_inode);
470
471 for (i = 0; i < src->bu_blocks; i++) {
Joel Becker4980c6d2008-10-24 18:54:43 -0700472 memcpy(bucket_block(dest, i), bucket_block(src, i),
473 blocksize);
474 }
475}
Joel Becker1224be02008-10-24 18:47:33 -0700476
Joel Becker4ae1d692008-11-13 14:49:18 -0800477static int ocfs2_validate_xattr_block(struct super_block *sb,
478 struct buffer_head *bh)
479{
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700480 int rc;
Joel Becker4ae1d692008-11-13 14:49:18 -0800481 struct ocfs2_xattr_block *xb =
482 (struct ocfs2_xattr_block *)bh->b_data;
483
484 mlog(0, "Validating xattr block %llu\n",
485 (unsigned long long)bh->b_blocknr);
486
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700487 BUG_ON(!buffer_uptodate(bh));
488
489 /*
490 * If the ecc fails, we return the error but otherwise
491 * leave the filesystem running. We know any error is
492 * local to this block.
493 */
494 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &xb->xb_check);
495 if (rc)
496 return rc;
497
498 /*
499 * Errors after here are fatal
500 */
501
Joel Becker4ae1d692008-11-13 14:49:18 -0800502 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
503 ocfs2_error(sb,
504 "Extended attribute block #%llu has bad "
505 "signature %.*s",
506 (unsigned long long)bh->b_blocknr, 7,
507 xb->xb_signature);
508 return -EINVAL;
509 }
510
511 if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) {
512 ocfs2_error(sb,
513 "Extended attribute block #%llu has an "
514 "invalid xb_blkno of %llu",
515 (unsigned long long)bh->b_blocknr,
516 (unsigned long long)le64_to_cpu(xb->xb_blkno));
517 return -EINVAL;
518 }
519
520 if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) {
521 ocfs2_error(sb,
522 "Extended attribute block #%llu has an invalid "
523 "xb_fs_generation of #%u",
524 (unsigned long long)bh->b_blocknr,
525 le32_to_cpu(xb->xb_fs_generation));
526 return -EINVAL;
527 }
528
529 return 0;
530}
531
532static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno,
533 struct buffer_head **bh)
534{
535 int rc;
536 struct buffer_head *tmp = *bh;
537
Joel Becker8cb471e2009-02-10 20:00:41 -0800538 rc = ocfs2_read_block(INODE_CACHE(inode), xb_blkno, &tmp,
Joel Becker970e4932008-11-13 14:49:19 -0800539 ocfs2_validate_xattr_block);
Joel Becker4ae1d692008-11-13 14:49:18 -0800540
541 /* If ocfs2_read_block() got us a new bh, pass it up. */
542 if (!rc && !*bh)
543 *bh = tmp;
544
545 return rc;
546}
547
Tao Ma936b8832008-10-09 23:06:14 +0800548static inline const char *ocfs2_xattr_prefix(int name_index)
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800549{
550 struct xattr_handler *handler = NULL;
551
552 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
553 handler = ocfs2_xattr_handler_map[name_index];
554
Tao Ma936b8832008-10-09 23:06:14 +0800555 return handler ? handler->prefix : NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800556}
557
Mark Fasheh40daa162008-10-07 14:31:42 -0700558static u32 ocfs2_xattr_name_hash(struct inode *inode,
Tao Ma2057e5c2008-10-09 23:06:13 +0800559 const char *name,
Mark Fasheh40daa162008-10-07 14:31:42 -0700560 int name_len)
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800561{
562 /* Get hash value of uuid from super block */
563 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
564 int i;
565
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800566 /* hash extended attribute name */
567 for (i = 0; i < name_len; i++) {
568 hash = (hash << OCFS2_HASH_SHIFT) ^
569 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
570 *name++;
571 }
572
573 return hash;
574}
575
576/*
577 * ocfs2_xattr_hash_entry()
578 *
579 * Compute the hash of an extended attribute.
580 */
581static void ocfs2_xattr_hash_entry(struct inode *inode,
582 struct ocfs2_xattr_header *header,
583 struct ocfs2_xattr_entry *entry)
584{
585 u32 hash = 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800586 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800587
Tao Ma2057e5c2008-10-09 23:06:13 +0800588 hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800589 entry->xe_name_hash = cpu_to_le32(hash);
590
591 return;
592}
Tao Maf56654c2008-08-18 17:38:48 +0800593
Tiger Yang534eadd2008-11-14 11:16:41 +0800594static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
595{
Joel Becker199799a2009-08-14 19:04:15 -0700596 return namevalue_size(name_len, value_len) +
597 sizeof(struct ocfs2_xattr_entry);
598}
Tiger Yang534eadd2008-11-14 11:16:41 +0800599
Joel Becker199799a2009-08-14 19:04:15 -0700600static int ocfs2_xi_entry_usage(struct ocfs2_xattr_info *xi)
601{
602 return namevalue_size_xi(xi) +
603 sizeof(struct ocfs2_xattr_entry);
604}
Tiger Yang534eadd2008-11-14 11:16:41 +0800605
Joel Becker199799a2009-08-14 19:04:15 -0700606static int ocfs2_xe_entry_usage(struct ocfs2_xattr_entry *xe)
607{
608 return namevalue_size_xe(xe) +
609 sizeof(struct ocfs2_xattr_entry);
Tiger Yang534eadd2008-11-14 11:16:41 +0800610}
611
612int ocfs2_calc_security_init(struct inode *dir,
613 struct ocfs2_security_xattr_info *si,
614 int *want_clusters,
615 int *xattr_credits,
616 struct ocfs2_alloc_context **xattr_ac)
617{
618 int ret = 0;
619 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
620 int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
621 si->value_len);
622
623 /*
624 * The max space of security xattr taken inline is
625 * 256(name) + 80(value) + 16(entry) = 352 bytes,
626 * So reserve one metadata block for it is ok.
627 */
628 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
629 s_size > OCFS2_XATTR_FREE_IN_IBODY) {
630 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
631 if (ret) {
632 mlog_errno(ret);
633 return ret;
634 }
635 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
636 }
637
638 /* reserve clusters for xattr value which will be set in B tree*/
Tiger Yang0e445b62008-12-09 16:42:51 +0800639 if (si->value_len > OCFS2_XATTR_INLINE_SIZE) {
640 int new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
641 si->value_len);
642
643 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
644 new_clusters);
645 *want_clusters += new_clusters;
646 }
Tiger Yang534eadd2008-11-14 11:16:41 +0800647 return ret;
648}
649
Tiger Yang89c38bd2008-11-14 11:17:41 +0800650int ocfs2_calc_xattr_init(struct inode *dir,
651 struct buffer_head *dir_bh,
652 int mode,
653 struct ocfs2_security_xattr_info *si,
654 int *want_clusters,
655 int *xattr_credits,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800656 int *want_meta)
Tiger Yang89c38bd2008-11-14 11:17:41 +0800657{
658 int ret = 0;
659 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
Tiger Yang0e445b62008-12-09 16:42:51 +0800660 int s_size = 0, a_size = 0, acl_len = 0, new_clusters;
Tiger Yang89c38bd2008-11-14 11:17:41 +0800661
662 if (si->enable)
663 s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
664 si->value_len);
665
666 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
667 acl_len = ocfs2_xattr_get_nolock(dir, dir_bh,
668 OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
669 "", NULL, 0);
670 if (acl_len > 0) {
671 a_size = ocfs2_xattr_entry_real_size(0, acl_len);
672 if (S_ISDIR(mode))
673 a_size <<= 1;
674 } else if (acl_len != 0 && acl_len != -ENODATA) {
675 mlog_errno(ret);
676 return ret;
677 }
678 }
679
680 if (!(s_size + a_size))
681 return ret;
682
683 /*
684 * The max space of security xattr taken inline is
685 * 256(name) + 80(value) + 16(entry) = 352 bytes,
686 * The max space of acl xattr taken inline is
687 * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
688 * when blocksize = 512, may reserve one more cluser for
689 * xattr bucket, otherwise reserve one metadata block
690 * for them is ok.
Tiger Yang6c9fd1d2009-03-06 10:19:30 +0800691 * If this is a new directory with inline data,
692 * we choose to reserve the entire inline area for
693 * directory contents and force an external xattr block.
Tiger Yang89c38bd2008-11-14 11:17:41 +0800694 */
695 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
Tiger Yang6c9fd1d2009-03-06 10:19:30 +0800696 (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) ||
Tiger Yang89c38bd2008-11-14 11:17:41 +0800697 (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) {
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800698 *want_meta = *want_meta + 1;
Tiger Yang89c38bd2008-11-14 11:17:41 +0800699 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
700 }
701
702 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
703 (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
704 *want_clusters += 1;
705 *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
706 }
707
Tiger Yang0e445b62008-12-09 16:42:51 +0800708 /*
709 * reserve credits and clusters for xattrs which has large value
710 * and have to be set outside
711 */
712 if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE) {
713 new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
714 si->value_len);
715 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
716 new_clusters);
717 *want_clusters += new_clusters;
718 }
Tiger Yang89c38bd2008-11-14 11:17:41 +0800719 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
720 acl_len > OCFS2_XATTR_INLINE_SIZE) {
Tiger Yang0e445b62008-12-09 16:42:51 +0800721 /* for directory, it has DEFAULT and ACCESS two types of acls */
722 new_clusters = (S_ISDIR(mode) ? 2 : 1) *
723 ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
724 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
725 new_clusters);
726 *want_clusters += new_clusters;
Tiger Yang89c38bd2008-11-14 11:17:41 +0800727 }
728
729 return ret;
730}
731
Tao Maf56654c2008-08-18 17:38:48 +0800732static int ocfs2_xattr_extend_allocation(struct inode *inode,
733 u32 clusters_to_add,
Joel Becker19b801f2008-12-09 14:36:50 -0800734 struct ocfs2_xattr_value_buf *vb,
Tao Ma78f30c32008-11-12 08:27:00 +0800735 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800736{
737 int status = 0;
Tao Ma85db90e2008-11-12 08:27:01 +0800738 handle_t *handle = ctxt->handle;
Tao Maf56654c2008-08-18 17:38:48 +0800739 enum ocfs2_alloc_restarted why;
Joel Becker19b801f2008-12-09 14:36:50 -0800740 u32 prev_clusters, logical_start = le32_to_cpu(vb->vb_xv->xr_clusters);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700741 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +0800742
743 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
744
Joel Becker5e404e92009-02-13 03:54:22 -0800745 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700746
Joel Becker0cf2f762009-02-12 16:41:25 -0800747 status = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
Joel Becker2a50a742008-12-09 14:24:33 -0800748 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Maf56654c2008-08-18 17:38:48 +0800749 if (status < 0) {
750 mlog_errno(status);
751 goto leave;
752 }
753
Joel Becker19b801f2008-12-09 14:36:50 -0800754 prev_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
Joel Beckercbee7e12009-02-13 03:34:15 -0800755 status = ocfs2_add_clusters_in_btree(handle,
756 &et,
Tao Maf56654c2008-08-18 17:38:48 +0800757 &logical_start,
758 clusters_to_add,
759 0,
Tao Ma78f30c32008-11-12 08:27:00 +0800760 ctxt->data_ac,
761 ctxt->meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700762 &why);
Tao Ma85db90e2008-11-12 08:27:01 +0800763 if (status < 0) {
764 mlog_errno(status);
Tao Maf56654c2008-08-18 17:38:48 +0800765 goto leave;
766 }
767
Joel Becker19b801f2008-12-09 14:36:50 -0800768 status = ocfs2_journal_dirty(handle, vb->vb_bh);
Tao Maf56654c2008-08-18 17:38:48 +0800769 if (status < 0) {
770 mlog_errno(status);
771 goto leave;
772 }
773
Joel Becker19b801f2008-12-09 14:36:50 -0800774 clusters_to_add -= le32_to_cpu(vb->vb_xv->xr_clusters) - prev_clusters;
Tao Maf56654c2008-08-18 17:38:48 +0800775
Tao Ma85db90e2008-11-12 08:27:01 +0800776 /*
777 * We should have already allocated enough space before the transaction,
778 * so no need to restart.
779 */
780 BUG_ON(why != RESTART_NONE || clusters_to_add);
Tao Maf56654c2008-08-18 17:38:48 +0800781
782leave:
Tao Maf56654c2008-08-18 17:38:48 +0800783
784 return status;
785}
786
787static int __ocfs2_remove_xattr_range(struct inode *inode,
Joel Beckerd72cc722008-12-09 14:30:41 -0800788 struct ocfs2_xattr_value_buf *vb,
Tao Maf56654c2008-08-18 17:38:48 +0800789 u32 cpos, u32 phys_cpos, u32 len,
Tao Ma492a8a32009-08-18 11:43:17 +0800790 unsigned int ext_flags,
Tao Ma78f30c32008-11-12 08:27:00 +0800791 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800792{
793 int ret;
794 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
Tao Ma85db90e2008-11-12 08:27:01 +0800795 handle_t *handle = ctxt->handle;
Joel Beckerf99b9b72008-08-20 19:36:33 -0700796 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +0800797
Joel Becker5e404e92009-02-13 03:54:22 -0800798 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700799
Joel Becker0cf2f762009-02-12 16:41:25 -0800800 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
Joel Beckerd72cc722008-12-09 14:30:41 -0800801 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Maf56654c2008-08-18 17:38:48 +0800802 if (ret) {
803 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800804 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800805 }
806
Joel Beckerdbdcf6a2009-02-13 03:41:26 -0800807 ret = ocfs2_remove_extent(handle, &et, cpos, len, ctxt->meta_ac,
Tao Ma78f30c32008-11-12 08:27:00 +0800808 &ctxt->dealloc);
Tao Maf56654c2008-08-18 17:38:48 +0800809 if (ret) {
810 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800811 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800812 }
813
Joel Beckerd72cc722008-12-09 14:30:41 -0800814 le32_add_cpu(&vb->vb_xv->xr_clusters, -len);
Tao Maf56654c2008-08-18 17:38:48 +0800815
Joel Beckerd72cc722008-12-09 14:30:41 -0800816 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
Tao Maf56654c2008-08-18 17:38:48 +0800817 if (ret) {
818 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800819 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800820 }
821
Tao Ma492a8a32009-08-18 11:43:17 +0800822 if (ext_flags & OCFS2_EXT_REFCOUNTED)
823 ret = ocfs2_decrease_refcount(inode, handle,
824 ocfs2_blocks_to_clusters(inode->i_sb,
825 phys_blkno),
826 len, ctxt->meta_ac, &ctxt->dealloc, 1);
827 else
828 ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc,
829 phys_blkno, len);
Tao Maf56654c2008-08-18 17:38:48 +0800830 if (ret)
831 mlog_errno(ret);
832
Tao Maf56654c2008-08-18 17:38:48 +0800833out:
Tao Maf56654c2008-08-18 17:38:48 +0800834 return ret;
835}
836
837static int ocfs2_xattr_shrink_size(struct inode *inode,
838 u32 old_clusters,
839 u32 new_clusters,
Joel Becker19b801f2008-12-09 14:36:50 -0800840 struct ocfs2_xattr_value_buf *vb,
Tao Ma78f30c32008-11-12 08:27:00 +0800841 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800842{
843 int ret = 0;
Tao Ma492a8a32009-08-18 11:43:17 +0800844 unsigned int ext_flags;
Tao Maf56654c2008-08-18 17:38:48 +0800845 u32 trunc_len, cpos, phys_cpos, alloc_size;
846 u64 block;
Tao Maf56654c2008-08-18 17:38:48 +0800847
848 if (old_clusters <= new_clusters)
849 return 0;
850
851 cpos = new_clusters;
852 trunc_len = old_clusters - new_clusters;
853 while (trunc_len) {
854 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
Joel Beckerd72cc722008-12-09 14:30:41 -0800855 &alloc_size,
Tao Ma492a8a32009-08-18 11:43:17 +0800856 &vb->vb_xv->xr_list, &ext_flags);
Tao Maf56654c2008-08-18 17:38:48 +0800857 if (ret) {
858 mlog_errno(ret);
859 goto out;
860 }
861
862 if (alloc_size > trunc_len)
863 alloc_size = trunc_len;
864
Joel Becker19b801f2008-12-09 14:36:50 -0800865 ret = __ocfs2_remove_xattr_range(inode, vb, cpos,
Tao Maf56654c2008-08-18 17:38:48 +0800866 phys_cpos, alloc_size,
Tao Ma492a8a32009-08-18 11:43:17 +0800867 ext_flags, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800868 if (ret) {
869 mlog_errno(ret);
870 goto out;
871 }
872
873 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
Joel Becker8cb471e2009-02-10 20:00:41 -0800874 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode),
875 block, alloc_size);
Tao Maf56654c2008-08-18 17:38:48 +0800876 cpos += alloc_size;
877 trunc_len -= alloc_size;
878 }
879
880out:
Tao Maf56654c2008-08-18 17:38:48 +0800881 return ret;
882}
883
884static int ocfs2_xattr_value_truncate(struct inode *inode,
Joel Beckerb3e5d372008-12-09 15:01:04 -0800885 struct ocfs2_xattr_value_buf *vb,
Tao Ma78f30c32008-11-12 08:27:00 +0800886 int len,
887 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800888{
889 int ret;
890 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
Joel Beckerb3e5d372008-12-09 15:01:04 -0800891 u32 old_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
Tao Maf56654c2008-08-18 17:38:48 +0800892
893 if (new_clusters == old_clusters)
894 return 0;
895
896 if (new_clusters > old_clusters)
897 ret = ocfs2_xattr_extend_allocation(inode,
898 new_clusters - old_clusters,
Joel Beckerb3e5d372008-12-09 15:01:04 -0800899 vb, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800900 else
901 ret = ocfs2_xattr_shrink_size(inode,
902 old_clusters, new_clusters,
Joel Beckerb3e5d372008-12-09 15:01:04 -0800903 vb, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800904
905 return ret;
906}
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800907
Tao Ma936b8832008-10-09 23:06:14 +0800908static int ocfs2_xattr_list_entry(char *buffer, size_t size,
909 size_t *result, const char *prefix,
910 const char *name, int name_len)
911{
912 char *p = buffer + *result;
913 int prefix_len = strlen(prefix);
914 int total_len = prefix_len + name_len + 1;
915
916 *result += total_len;
917
918 /* we are just looking for how big our buffer needs to be */
919 if (!size)
920 return 0;
921
922 if (*result > size)
923 return -ERANGE;
924
925 memcpy(p, prefix, prefix_len);
926 memcpy(p + prefix_len, name, name_len);
927 p[prefix_len + name_len] = '\0';
928
929 return 0;
930}
931
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800932static int ocfs2_xattr_list_entries(struct inode *inode,
933 struct ocfs2_xattr_header *header,
934 char *buffer, size_t buffer_size)
935{
Tao Ma936b8832008-10-09 23:06:14 +0800936 size_t result = 0;
937 int i, type, ret;
938 const char *prefix, *name;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800939
940 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
941 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
Tao Ma936b8832008-10-09 23:06:14 +0800942 type = ocfs2_xattr_get_type(entry);
943 prefix = ocfs2_xattr_prefix(type);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800944
Tao Ma936b8832008-10-09 23:06:14 +0800945 if (prefix) {
946 name = (const char *)header +
947 le16_to_cpu(entry->xe_name_offset);
948
949 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
950 &result, prefix, name,
951 entry->xe_name_len);
952 if (ret)
953 return ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800954 }
955 }
956
Tao Ma936b8832008-10-09 23:06:14 +0800957 return result;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800958}
959
Tao Ma8b2c0db2009-08-18 11:43:49 +0800960int ocfs2_has_inline_xattr_value_outside(struct inode *inode,
961 struct ocfs2_dinode *di)
962{
963 struct ocfs2_xattr_header *xh;
964 int i;
965
966 xh = (struct ocfs2_xattr_header *)
967 ((void *)di + inode->i_sb->s_blocksize -
968 le16_to_cpu(di->i_xattr_inline_size));
969
970 for (i = 0; i < le16_to_cpu(xh->xh_count); i++)
971 if (!ocfs2_xattr_is_local(&xh->xh_entries[i]))
972 return 1;
973
974 return 0;
975}
976
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800977static int ocfs2_xattr_ibody_list(struct inode *inode,
978 struct ocfs2_dinode *di,
979 char *buffer,
980 size_t buffer_size)
981{
982 struct ocfs2_xattr_header *header = NULL;
983 struct ocfs2_inode_info *oi = OCFS2_I(inode);
984 int ret = 0;
985
986 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
987 return ret;
988
989 header = (struct ocfs2_xattr_header *)
990 ((void *)di + inode->i_sb->s_blocksize -
991 le16_to_cpu(di->i_xattr_inline_size));
992
993 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
994
995 return ret;
996}
997
998static int ocfs2_xattr_block_list(struct inode *inode,
999 struct ocfs2_dinode *di,
1000 char *buffer,
1001 size_t buffer_size)
1002{
1003 struct buffer_head *blk_bh = NULL;
Tao Ma0c044f02008-08-18 17:38:50 +08001004 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001005 int ret = 0;
1006
1007 if (!di->i_xattr_loc)
1008 return ret;
1009
Joel Becker4ae1d692008-11-13 14:49:18 -08001010 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
1011 &blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001012 if (ret < 0) {
1013 mlog_errno(ret);
1014 return ret;
1015 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001016
Tao Ma0c044f02008-08-18 17:38:50 +08001017 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Ma0c044f02008-08-18 17:38:50 +08001018 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1019 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
1020 ret = ocfs2_xattr_list_entries(inode, header,
1021 buffer, buffer_size);
Tao Ma47bca492009-08-18 11:43:42 +08001022 } else
1023 ret = ocfs2_xattr_tree_list_index_block(inode, blk_bh,
Tao Ma0c044f02008-08-18 17:38:50 +08001024 buffer, buffer_size);
Joel Becker4ae1d692008-11-13 14:49:18 -08001025
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001026 brelse(blk_bh);
1027
1028 return ret;
1029}
1030
1031ssize_t ocfs2_listxattr(struct dentry *dentry,
1032 char *buffer,
1033 size_t size)
1034{
1035 int ret = 0, i_ret = 0, b_ret = 0;
1036 struct buffer_head *di_bh = NULL;
1037 struct ocfs2_dinode *di = NULL;
1038 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
1039
Tiger Yang8154da32008-08-18 17:11:46 +08001040 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
1041 return -EOPNOTSUPP;
1042
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001043 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1044 return ret;
1045
1046 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
1047 if (ret < 0) {
1048 mlog_errno(ret);
1049 return ret;
1050 }
1051
1052 di = (struct ocfs2_dinode *)di_bh->b_data;
1053
1054 down_read(&oi->ip_xattr_sem);
1055 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
1056 if (i_ret < 0)
1057 b_ret = 0;
1058 else {
1059 if (buffer) {
1060 buffer += i_ret;
1061 size -= i_ret;
1062 }
1063 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
1064 buffer, size);
1065 if (b_ret < 0)
1066 i_ret = 0;
1067 }
1068 up_read(&oi->ip_xattr_sem);
1069 ocfs2_inode_unlock(dentry->d_inode, 0);
1070
1071 brelse(di_bh);
1072
1073 return i_ret + b_ret;
1074}
1075
1076static int ocfs2_xattr_find_entry(int name_index,
1077 const char *name,
1078 struct ocfs2_xattr_search *xs)
1079{
1080 struct ocfs2_xattr_entry *entry;
1081 size_t name_len;
1082 int i, cmp = 1;
1083
1084 if (name == NULL)
1085 return -EINVAL;
1086
1087 name_len = strlen(name);
1088 entry = xs->here;
1089 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
1090 cmp = name_index - ocfs2_xattr_get_type(entry);
1091 if (!cmp)
1092 cmp = name_len - entry->xe_name_len;
1093 if (!cmp)
1094 cmp = memcmp(name, (xs->base +
1095 le16_to_cpu(entry->xe_name_offset)),
1096 name_len);
1097 if (cmp == 0)
1098 break;
1099 entry += 1;
1100 }
1101 xs->here = entry;
1102
1103 return cmp ? -ENODATA : 0;
1104}
1105
1106static int ocfs2_xattr_get_value_outside(struct inode *inode,
Tao Ma589dc262008-08-18 17:38:51 +08001107 struct ocfs2_xattr_value_root *xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001108 void *buffer,
1109 size_t len)
1110{
1111 u32 cpos, p_cluster, num_clusters, bpc, clusters;
1112 u64 blkno;
1113 int i, ret = 0;
1114 size_t cplen, blocksize;
1115 struct buffer_head *bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001116 struct ocfs2_extent_list *el;
1117
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001118 el = &xv->xr_list;
1119 clusters = le32_to_cpu(xv->xr_clusters);
1120 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1121 blocksize = inode->i_sb->s_blocksize;
1122
1123 cpos = 0;
1124 while (cpos < clusters) {
1125 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
Tao Ma1061f9c2009-08-18 11:41:57 +08001126 &num_clusters, el, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001127 if (ret) {
1128 mlog_errno(ret);
1129 goto out;
1130 }
1131
1132 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1133 /* Copy ocfs2_xattr_value */
1134 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
Joel Becker8cb471e2009-02-10 20:00:41 -08001135 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1136 &bh, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001137 if (ret) {
1138 mlog_errno(ret);
1139 goto out;
1140 }
1141
1142 cplen = len >= blocksize ? blocksize : len;
1143 memcpy(buffer, bh->b_data, cplen);
1144 len -= cplen;
1145 buffer += cplen;
1146
1147 brelse(bh);
1148 bh = NULL;
1149 if (len == 0)
1150 break;
1151 }
1152 cpos += num_clusters;
1153 }
1154out:
1155 return ret;
1156}
1157
1158static int ocfs2_xattr_ibody_get(struct inode *inode,
1159 int name_index,
1160 const char *name,
1161 void *buffer,
1162 size_t buffer_size,
1163 struct ocfs2_xattr_search *xs)
1164{
1165 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1166 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
Tao Ma589dc262008-08-18 17:38:51 +08001167 struct ocfs2_xattr_value_root *xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001168 size_t size;
1169 int ret = 0;
1170
1171 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
1172 return -ENODATA;
1173
1174 xs->end = (void *)di + inode->i_sb->s_blocksize;
1175 xs->header = (struct ocfs2_xattr_header *)
1176 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1177 xs->base = (void *)xs->header;
1178 xs->here = xs->header->xh_entries;
1179
1180 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1181 if (ret)
1182 return ret;
1183 size = le64_to_cpu(xs->here->xe_value_size);
1184 if (buffer) {
1185 if (size > buffer_size)
1186 return -ERANGE;
1187 if (ocfs2_xattr_is_local(xs->here)) {
1188 memcpy(buffer, (void *)xs->base +
1189 le16_to_cpu(xs->here->xe_name_offset) +
1190 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
1191 } else {
Tao Ma589dc262008-08-18 17:38:51 +08001192 xv = (struct ocfs2_xattr_value_root *)
1193 (xs->base + le16_to_cpu(
1194 xs->here->xe_name_offset) +
1195 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
1196 ret = ocfs2_xattr_get_value_outside(inode, xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001197 buffer, size);
1198 if (ret < 0) {
1199 mlog_errno(ret);
1200 return ret;
1201 }
1202 }
1203 }
1204
1205 return size;
1206}
1207
1208static int ocfs2_xattr_block_get(struct inode *inode,
1209 int name_index,
1210 const char *name,
1211 void *buffer,
1212 size_t buffer_size,
1213 struct ocfs2_xattr_search *xs)
1214{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001215 struct ocfs2_xattr_block *xb;
Tao Ma589dc262008-08-18 17:38:51 +08001216 struct ocfs2_xattr_value_root *xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001217 size_t size;
Subrata Modak44d8e4e2009-07-14 01:19:31 +05301218 int ret = -ENODATA, name_offset, name_len, i;
1219 int uninitialized_var(block_off);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001220
Joel Beckerba937122008-10-24 19:13:20 -07001221 xs->bucket = ocfs2_xattr_bucket_new(inode);
1222 if (!xs->bucket) {
1223 ret = -ENOMEM;
1224 mlog_errno(ret);
1225 goto cleanup;
1226 }
Tao Ma589dc262008-08-18 17:38:51 +08001227
Joel Becker54f443f2008-10-20 18:43:07 -07001228 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
1229 if (ret) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001230 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001231 goto cleanup;
1232 }
1233
Tiger Yang6c1e1832008-11-02 19:04:21 +08001234 if (xs->not_found) {
1235 ret = -ENODATA;
1236 goto cleanup;
1237 }
1238
Joel Becker54f443f2008-10-20 18:43:07 -07001239 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001240 size = le64_to_cpu(xs->here->xe_value_size);
1241 if (buffer) {
1242 ret = -ERANGE;
1243 if (size > buffer_size)
1244 goto cleanup;
Tao Ma589dc262008-08-18 17:38:51 +08001245
1246 name_offset = le16_to_cpu(xs->here->xe_name_offset);
1247 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
1248 i = xs->here - xs->header->xh_entries;
1249
1250 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
Tao Mafd68a892009-08-18 11:43:21 +08001251 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Joel Beckerba937122008-10-24 19:13:20 -07001252 bucket_xh(xs->bucket),
Tao Ma589dc262008-08-18 17:38:51 +08001253 i,
1254 &block_off,
1255 &name_offset);
Joel Beckerba937122008-10-24 19:13:20 -07001256 xs->base = bucket_block(xs->bucket, block_off);
Tao Ma589dc262008-08-18 17:38:51 +08001257 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001258 if (ocfs2_xattr_is_local(xs->here)) {
1259 memcpy(buffer, (void *)xs->base +
Tao Ma589dc262008-08-18 17:38:51 +08001260 name_offset + name_len, size);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001261 } else {
Tao Ma589dc262008-08-18 17:38:51 +08001262 xv = (struct ocfs2_xattr_value_root *)
1263 (xs->base + name_offset + name_len);
1264 ret = ocfs2_xattr_get_value_outside(inode, xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001265 buffer, size);
1266 if (ret < 0) {
1267 mlog_errno(ret);
1268 goto cleanup;
1269 }
1270 }
1271 }
1272 ret = size;
1273cleanup:
Joel Beckerba937122008-10-24 19:13:20 -07001274 ocfs2_xattr_bucket_free(xs->bucket);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001275
Joel Becker54f443f2008-10-20 18:43:07 -07001276 brelse(xs->xattr_bh);
1277 xs->xattr_bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001278 return ret;
1279}
1280
Tiger Yang4e3e9d02008-11-14 11:16:53 +08001281int ocfs2_xattr_get_nolock(struct inode *inode,
1282 struct buffer_head *di_bh,
Tiger Yang0030e002008-10-23 16:33:33 +08001283 int name_index,
1284 const char *name,
1285 void *buffer,
1286 size_t buffer_size)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001287{
1288 int ret;
1289 struct ocfs2_dinode *di = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001290 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1291 struct ocfs2_xattr_search xis = {
1292 .not_found = -ENODATA,
1293 };
1294 struct ocfs2_xattr_search xbs = {
1295 .not_found = -ENODATA,
1296 };
1297
Tiger Yang8154da32008-08-18 17:11:46 +08001298 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1299 return -EOPNOTSUPP;
1300
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001301 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1302 ret = -ENODATA;
1303
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001304 xis.inode_bh = xbs.inode_bh = di_bh;
1305 di = (struct ocfs2_dinode *)di_bh->b_data;
1306
1307 down_read(&oi->ip_xattr_sem);
1308 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1309 buffer_size, &xis);
Tiger Yang6c1e1832008-11-02 19:04:21 +08001310 if (ret == -ENODATA && di->i_xattr_loc)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001311 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1312 buffer_size, &xbs);
1313 up_read(&oi->ip_xattr_sem);
Tiger Yang4e3e9d02008-11-14 11:16:53 +08001314
1315 return ret;
1316}
1317
1318/* ocfs2_xattr_get()
1319 *
1320 * Copy an extended attribute into the buffer provided.
1321 * Buffer is NULL to compute the size of buffer required.
1322 */
1323static int ocfs2_xattr_get(struct inode *inode,
1324 int name_index,
1325 const char *name,
1326 void *buffer,
1327 size_t buffer_size)
1328{
1329 int ret;
1330 struct buffer_head *di_bh = NULL;
1331
1332 ret = ocfs2_inode_lock(inode, &di_bh, 0);
1333 if (ret < 0) {
1334 mlog_errno(ret);
1335 return ret;
1336 }
1337 ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
1338 name, buffer, buffer_size);
1339
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001340 ocfs2_inode_unlock(inode, 0);
1341
1342 brelse(di_bh);
1343
1344 return ret;
1345}
1346
1347static int __ocfs2_xattr_set_value_outside(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001348 handle_t *handle,
Tao Ma492a8a32009-08-18 11:43:17 +08001349 struct ocfs2_xattr_value_buf *vb,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001350 const void *value,
1351 int value_len)
1352{
Tao Ma71d548a2008-12-05 06:20:54 +08001353 int ret = 0, i, cp_len;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001354 u16 blocksize = inode->i_sb->s_blocksize;
1355 u32 p_cluster, num_clusters;
1356 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1357 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1358 u64 blkno;
1359 struct buffer_head *bh = NULL;
Tao Ma492a8a32009-08-18 11:43:17 +08001360 unsigned int ext_flags;
1361 struct ocfs2_xattr_value_root *xv = vb->vb_xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001362
1363 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1364
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001365 while (cpos < clusters) {
1366 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
Tao Ma1061f9c2009-08-18 11:41:57 +08001367 &num_clusters, &xv->xr_list,
Tao Ma492a8a32009-08-18 11:43:17 +08001368 &ext_flags);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001369 if (ret) {
1370 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001371 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001372 }
1373
Tao Ma492a8a32009-08-18 11:43:17 +08001374 BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED);
1375
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001376 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1377
1378 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
Joel Becker8cb471e2009-02-10 20:00:41 -08001379 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1380 &bh, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001381 if (ret) {
1382 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001383 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001384 }
1385
1386 ret = ocfs2_journal_access(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -08001387 INODE_CACHE(inode),
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001388 bh,
1389 OCFS2_JOURNAL_ACCESS_WRITE);
1390 if (ret < 0) {
1391 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001392 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001393 }
1394
1395 cp_len = value_len > blocksize ? blocksize : value_len;
1396 memcpy(bh->b_data, value, cp_len);
1397 value_len -= cp_len;
1398 value += cp_len;
1399 if (cp_len < blocksize)
1400 memset(bh->b_data + cp_len, 0,
1401 blocksize - cp_len);
1402
1403 ret = ocfs2_journal_dirty(handle, bh);
1404 if (ret < 0) {
1405 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001406 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001407 }
1408 brelse(bh);
1409 bh = NULL;
1410
1411 /*
1412 * XXX: do we need to empty all the following
1413 * blocks in this cluster?
1414 */
1415 if (!value_len)
1416 break;
1417 }
1418 cpos += num_clusters;
1419 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001420out:
1421 brelse(bh);
1422
1423 return ret;
1424}
1425
1426static int ocfs2_xattr_cleanup(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001427 handle_t *handle,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001428 struct ocfs2_xattr_info *xi,
1429 struct ocfs2_xattr_search *xs,
Joel Becker512620f2008-12-09 15:58:35 -08001430 struct ocfs2_xattr_value_buf *vb,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001431 size_t offs)
1432{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001433 int ret = 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001434 void *val = xs->base + offs;
Joel Becker199799a2009-08-14 19:04:15 -07001435 size_t size = namevalue_size_xi(xi);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001436
Joel Becker0cf2f762009-02-12 16:41:25 -08001437 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
Joel Becker512620f2008-12-09 15:58:35 -08001438 OCFS2_JOURNAL_ACCESS_WRITE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001439 if (ret) {
1440 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001441 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001442 }
1443 /* Decrease xattr count */
1444 le16_add_cpu(&xs->header->xh_count, -1);
1445 /* Remove the xattr entry and tree root which has already be set*/
1446 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
1447 memset(val, 0, size);
1448
Joel Becker512620f2008-12-09 15:58:35 -08001449 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001450 if (ret < 0)
1451 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001452out:
1453 return ret;
1454}
1455
1456static int ocfs2_xattr_update_entry(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001457 handle_t *handle,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001458 struct ocfs2_xattr_info *xi,
1459 struct ocfs2_xattr_search *xs,
Joel Becker0c748e92008-12-09 15:46:15 -08001460 struct ocfs2_xattr_value_buf *vb,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001461 size_t offs)
1462{
Tao Ma85db90e2008-11-12 08:27:01 +08001463 int ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001464
Joel Becker0cf2f762009-02-12 16:41:25 -08001465 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
Joel Becker0c748e92008-12-09 15:46:15 -08001466 OCFS2_JOURNAL_ACCESS_WRITE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001467 if (ret) {
1468 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001469 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001470 }
1471
1472 xs->here->xe_name_offset = cpu_to_le16(offs);
Joel Becker6b240ff2009-08-14 18:02:52 -07001473 xs->here->xe_value_size = cpu_to_le64(xi->xi_value_len);
1474 if (xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001475 ocfs2_xattr_set_local(xs->here, 1);
1476 else
1477 ocfs2_xattr_set_local(xs->here, 0);
1478 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1479
Joel Becker0c748e92008-12-09 15:46:15 -08001480 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001481 if (ret < 0)
1482 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001483out:
1484 return ret;
1485}
1486
1487/*
1488 * ocfs2_xattr_set_value_outside()
1489 *
1490 * Set large size value in B tree.
1491 */
1492static int ocfs2_xattr_set_value_outside(struct inode *inode,
1493 struct ocfs2_xattr_info *xi,
1494 struct ocfs2_xattr_search *xs,
Tao Ma78f30c32008-11-12 08:27:00 +08001495 struct ocfs2_xattr_set_ctxt *ctxt,
Joel Becker512620f2008-12-09 15:58:35 -08001496 struct ocfs2_xattr_value_buf *vb,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001497 size_t offs)
1498{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001499 void *val = xs->base + offs;
1500 struct ocfs2_xattr_value_root *xv = NULL;
Joel Becker199799a2009-08-14 19:04:15 -07001501 size_t size = namevalue_size_xi(xi);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001502 int ret = 0;
1503
1504 memset(val, 0, size);
Joel Becker18853b92009-08-14 18:17:07 -07001505 memcpy(val, xi->xi_name, xi->xi_name_len);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001506 xv = (struct ocfs2_xattr_value_root *)
Joel Becker18853b92009-08-14 18:17:07 -07001507 (val + OCFS2_XATTR_SIZE(xi->xi_name_len));
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001508 xv->xr_clusters = 0;
1509 xv->xr_last_eb_blk = 0;
1510 xv->xr_list.l_tree_depth = 0;
1511 xv->xr_list.l_count = cpu_to_le16(1);
1512 xv->xr_list.l_next_free_rec = 0;
Joel Becker512620f2008-12-09 15:58:35 -08001513 vb->vb_xv = xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001514
Joel Becker6b240ff2009-08-14 18:02:52 -07001515 ret = ocfs2_xattr_value_truncate(inode, vb, xi->xi_value_len, ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001516 if (ret < 0) {
1517 mlog_errno(ret);
1518 return ret;
1519 }
Joel Becker512620f2008-12-09 15:58:35 -08001520 ret = ocfs2_xattr_update_entry(inode, ctxt->handle, xi, xs, vb, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001521 if (ret < 0) {
1522 mlog_errno(ret);
1523 return ret;
1524 }
Tao Ma492a8a32009-08-18 11:43:17 +08001525 ret = __ocfs2_xattr_set_value_outside(inode, ctxt->handle, vb,
Joel Becker6b240ff2009-08-14 18:02:52 -07001526 xi->xi_value, xi->xi_value_len);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001527 if (ret < 0)
1528 mlog_errno(ret);
1529
1530 return ret;
1531}
1532
Joel Becker69a3e532009-08-17 12:24:39 -07001533static int ocfs2_xa_check_space_helper(int needed_space, int free_start,
1534 int num_entries)
1535{
1536 int free_space;
1537
1538 if (!needed_space)
1539 return 0;
1540
1541 free_space = free_start -
1542 sizeof(struct ocfs2_xattr_header) -
1543 (num_entries * sizeof(struct ocfs2_xattr_entry)) -
1544 OCFS2_XATTR_HEADER_GAP;
1545 if (free_space < 0)
1546 return -EIO;
1547 if (free_space < needed_space)
1548 return -ENOSPC;
1549
1550 return 0;
1551}
1552
Joel Beckercf2bc802009-08-18 13:52:38 -07001553static int ocfs2_xa_journal_access(handle_t *handle, struct ocfs2_xa_loc *loc,
1554 int type)
1555{
1556 return loc->xl_ops->xlo_journal_access(handle, loc, type);
1557}
1558
1559static void ocfs2_xa_journal_dirty(handle_t *handle, struct ocfs2_xa_loc *loc)
1560{
1561 loc->xl_ops->xlo_journal_dirty(handle, loc);
1562}
1563
Joel Becker69a3e532009-08-17 12:24:39 -07001564/* Give a pointer into the storage for the given offset */
1565static void *ocfs2_xa_offset_pointer(struct ocfs2_xa_loc *loc, int offset)
1566{
1567 BUG_ON(offset >= loc->xl_size);
1568 return loc->xl_ops->xlo_offset_pointer(loc, offset);
1569}
1570
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001571/*
Joel Becker11179f22009-08-14 16:07:44 -07001572 * Wipe the name+value pair and allow the storage to reclaim it. This
1573 * must be followed by either removal of the entry or a call to
1574 * ocfs2_xa_add_namevalue().
1575 */
1576static void ocfs2_xa_wipe_namevalue(struct ocfs2_xa_loc *loc)
1577{
1578 loc->xl_ops->xlo_wipe_namevalue(loc);
1579}
1580
Joel Becker69a3e532009-08-17 12:24:39 -07001581/*
1582 * Find lowest offset to a name+value pair. This is the start of our
1583 * downward-growing free space.
1584 */
1585static int ocfs2_xa_get_free_start(struct ocfs2_xa_loc *loc)
1586{
1587 return loc->xl_ops->xlo_get_free_start(loc);
1588}
1589
1590/* Can we reuse loc->xl_entry for xi? */
1591static int ocfs2_xa_can_reuse_entry(struct ocfs2_xa_loc *loc,
1592 struct ocfs2_xattr_info *xi)
1593{
1594 return loc->xl_ops->xlo_can_reuse(loc, xi);
1595}
1596
1597/* How much free space is needed to set the new value */
1598static int ocfs2_xa_check_space(struct ocfs2_xa_loc *loc,
1599 struct ocfs2_xattr_info *xi)
1600{
1601 return loc->xl_ops->xlo_check_space(loc, xi);
1602}
1603
1604static void ocfs2_xa_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1605{
1606 loc->xl_ops->xlo_add_entry(loc, name_hash);
1607 loc->xl_entry->xe_name_hash = cpu_to_le32(name_hash);
1608 /*
1609 * We can't leave the new entry's xe_name_offset at zero or
1610 * add_namevalue() will go nuts. We set it to the size of our
1611 * storage so that it can never be less than any other entry.
1612 */
1613 loc->xl_entry->xe_name_offset = cpu_to_le16(loc->xl_size);
1614}
1615
1616static void ocfs2_xa_add_namevalue(struct ocfs2_xa_loc *loc,
1617 struct ocfs2_xattr_info *xi)
1618{
1619 int size = namevalue_size_xi(xi);
1620 int nameval_offset;
1621 char *nameval_buf;
1622
1623 loc->xl_ops->xlo_add_namevalue(loc, size);
1624 loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
1625 loc->xl_entry->xe_name_len = xi->xi_name_len;
1626 ocfs2_xattr_set_type(loc->xl_entry, xi->xi_name_index);
1627 ocfs2_xattr_set_local(loc->xl_entry,
1628 xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE);
1629
1630 nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1631 nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
1632 memset(nameval_buf, 0, size);
1633 memcpy(nameval_buf, xi->xi_name, xi->xi_name_len);
1634}
1635
Joel Becker3fc12af2009-08-18 13:20:27 -07001636static void ocfs2_xa_fill_value_buf(struct ocfs2_xa_loc *loc,
1637 struct ocfs2_xattr_value_buf *vb)
1638{
1639 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1640 int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len);
1641
1642 /* Value bufs are for value trees */
1643 BUG_ON(namevalue_size_xe(loc->xl_entry) !=
1644 (name_size + OCFS2_XATTR_ROOT_SIZE));
1645
1646 loc->xl_ops->xlo_fill_value_buf(loc, vb);
1647 vb->vb_xv =
1648 (struct ocfs2_xattr_value_root *)ocfs2_xa_offset_pointer(loc,
1649 nameval_offset +
1650 name_size);
1651}
1652
Joel Beckercf2bc802009-08-18 13:52:38 -07001653static int ocfs2_xa_block_journal_access(handle_t *handle,
1654 struct ocfs2_xa_loc *loc, int type)
1655{
1656 struct buffer_head *bh = loc->xl_storage;
1657 ocfs2_journal_access_func access;
1658
1659 if (loc->xl_size == (bh->b_size -
1660 offsetof(struct ocfs2_xattr_block,
1661 xb_attrs.xb_header)))
1662 access = ocfs2_journal_access_xb;
1663 else
1664 access = ocfs2_journal_access_di;
1665 return access(handle, INODE_CACHE(loc->xl_inode), bh, type);
1666}
1667
1668static void ocfs2_xa_block_journal_dirty(handle_t *handle,
1669 struct ocfs2_xa_loc *loc)
1670{
1671 struct buffer_head *bh = loc->xl_storage;
1672
1673 ocfs2_journal_dirty(handle, bh);
1674}
1675
Joel Becker11179f22009-08-14 16:07:44 -07001676static void *ocfs2_xa_block_offset_pointer(struct ocfs2_xa_loc *loc,
1677 int offset)
1678{
Joel Becker11179f22009-08-14 16:07:44 -07001679 return (char *)loc->xl_header + offset;
1680}
1681
Joel Becker69a3e532009-08-17 12:24:39 -07001682static int ocfs2_xa_block_can_reuse(struct ocfs2_xa_loc *loc,
1683 struct ocfs2_xattr_info *xi)
1684{
1685 /*
1686 * Block storage is strict. If the sizes aren't exact, we will
1687 * remove the old one and reinsert the new.
1688 */
1689 return namevalue_size_xe(loc->xl_entry) ==
1690 namevalue_size_xi(xi);
1691}
1692
1693static int ocfs2_xa_block_get_free_start(struct ocfs2_xa_loc *loc)
1694{
1695 struct ocfs2_xattr_header *xh = loc->xl_header;
1696 int i, count = le16_to_cpu(xh->xh_count);
1697 int offset, free_start = loc->xl_size;
1698
1699 for (i = 0; i < count; i++) {
1700 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1701 if (offset < free_start)
1702 free_start = offset;
1703 }
1704
1705 return free_start;
1706}
1707
1708static int ocfs2_xa_block_check_space(struct ocfs2_xa_loc *loc,
1709 struct ocfs2_xattr_info *xi)
1710{
1711 int count = le16_to_cpu(loc->xl_header->xh_count);
1712 int free_start = ocfs2_xa_get_free_start(loc);
1713 int needed_space = ocfs2_xi_entry_usage(xi);
1714
1715 /*
1716 * Block storage will reclaim the original entry before inserting
1717 * the new value, so we only need the difference. If the new
1718 * entry is smaller than the old one, we don't need anything.
1719 */
1720 if (loc->xl_entry) {
1721 /* Don't need space if we're reusing! */
1722 if (ocfs2_xa_can_reuse_entry(loc, xi))
1723 needed_space = 0;
1724 else
1725 needed_space -= ocfs2_xe_entry_usage(loc->xl_entry);
1726 }
1727 if (needed_space < 0)
1728 needed_space = 0;
1729 return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1730}
1731
Joel Becker11179f22009-08-14 16:07:44 -07001732/*
1733 * Block storage for xattrs keeps the name+value pairs compacted. When
1734 * we remove one, we have to shift any that preceded it towards the end.
1735 */
1736static void ocfs2_xa_block_wipe_namevalue(struct ocfs2_xa_loc *loc)
1737{
1738 int i, offset;
1739 int namevalue_offset, first_namevalue_offset, namevalue_size;
1740 struct ocfs2_xattr_entry *entry = loc->xl_entry;
1741 struct ocfs2_xattr_header *xh = loc->xl_header;
Joel Becker11179f22009-08-14 16:07:44 -07001742 int count = le16_to_cpu(xh->xh_count);
1743
1744 namevalue_offset = le16_to_cpu(entry->xe_name_offset);
Joel Becker199799a2009-08-14 19:04:15 -07001745 namevalue_size = namevalue_size_xe(entry);
Joel Becker69a3e532009-08-17 12:24:39 -07001746 first_namevalue_offset = ocfs2_xa_get_free_start(loc);
Joel Becker11179f22009-08-14 16:07:44 -07001747
1748 /* Shift the name+value pairs */
1749 memmove((char *)xh + first_namevalue_offset + namevalue_size,
1750 (char *)xh + first_namevalue_offset,
1751 namevalue_offset - first_namevalue_offset);
1752 memset((char *)xh + first_namevalue_offset, 0, namevalue_size);
1753
1754 /* Now tell xh->xh_entries about it */
1755 for (i = 0; i < count; i++) {
1756 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1757 if (offset < namevalue_offset)
1758 le16_add_cpu(&xh->xh_entries[i].xe_name_offset,
1759 namevalue_size);
1760 }
1761
1762 /*
1763 * Note that we don't update xh_free_start or xh_name_value_len
1764 * because they're not used in block-stored xattrs.
1765 */
1766}
1767
Joel Becker69a3e532009-08-17 12:24:39 -07001768static void ocfs2_xa_block_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1769{
1770 int count = le16_to_cpu(loc->xl_header->xh_count);
1771 loc->xl_entry = &(loc->xl_header->xh_entries[count]);
1772 le16_add_cpu(&loc->xl_header->xh_count, 1);
1773 memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1774}
1775
1776static void ocfs2_xa_block_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1777{
1778 int free_start = ocfs2_xa_get_free_start(loc);
1779
1780 loc->xl_entry->xe_name_offset = cpu_to_le16(free_start - size);
1781}
1782
Joel Becker3fc12af2009-08-18 13:20:27 -07001783static void ocfs2_xa_block_fill_value_buf(struct ocfs2_xa_loc *loc,
1784 struct ocfs2_xattr_value_buf *vb)
1785{
1786 struct buffer_head *bh = loc->xl_storage;
1787
1788 if (loc->xl_size == (bh->b_size -
1789 offsetof(struct ocfs2_xattr_block,
1790 xb_attrs.xb_header)))
1791 vb->vb_access = ocfs2_journal_access_xb;
1792 else
1793 vb->vb_access = ocfs2_journal_access_di;
1794 vb->vb_bh = bh;
1795}
1796
Joel Becker11179f22009-08-14 16:07:44 -07001797/*
1798 * Operations for xattrs stored in blocks. This includes inline inode
1799 * storage and unindexed ocfs2_xattr_blocks.
1800 */
1801static const struct ocfs2_xa_loc_operations ocfs2_xa_block_loc_ops = {
Joel Beckercf2bc802009-08-18 13:52:38 -07001802 .xlo_journal_access = ocfs2_xa_block_journal_access,
1803 .xlo_journal_dirty = ocfs2_xa_block_journal_dirty,
Joel Becker11179f22009-08-14 16:07:44 -07001804 .xlo_offset_pointer = ocfs2_xa_block_offset_pointer,
Joel Becker69a3e532009-08-17 12:24:39 -07001805 .xlo_check_space = ocfs2_xa_block_check_space,
1806 .xlo_can_reuse = ocfs2_xa_block_can_reuse,
1807 .xlo_get_free_start = ocfs2_xa_block_get_free_start,
Joel Becker11179f22009-08-14 16:07:44 -07001808 .xlo_wipe_namevalue = ocfs2_xa_block_wipe_namevalue,
Joel Becker69a3e532009-08-17 12:24:39 -07001809 .xlo_add_entry = ocfs2_xa_block_add_entry,
1810 .xlo_add_namevalue = ocfs2_xa_block_add_namevalue,
Joel Becker3fc12af2009-08-18 13:20:27 -07001811 .xlo_fill_value_buf = ocfs2_xa_block_fill_value_buf,
Joel Becker11179f22009-08-14 16:07:44 -07001812};
1813
Joel Beckercf2bc802009-08-18 13:52:38 -07001814static int ocfs2_xa_bucket_journal_access(handle_t *handle,
1815 struct ocfs2_xa_loc *loc, int type)
1816{
1817 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1818
1819 return ocfs2_xattr_bucket_journal_access(handle, bucket, type);
1820}
1821
1822static void ocfs2_xa_bucket_journal_dirty(handle_t *handle,
1823 struct ocfs2_xa_loc *loc)
1824{
1825 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1826
1827 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
1828}
1829
Joel Becker11179f22009-08-14 16:07:44 -07001830static void *ocfs2_xa_bucket_offset_pointer(struct ocfs2_xa_loc *loc,
1831 int offset)
1832{
1833 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1834 int block, block_offset;
1835
Joel Becker11179f22009-08-14 16:07:44 -07001836 /* The header is at the front of the bucket */
Joel Beckercf2bc802009-08-18 13:52:38 -07001837 block = offset >> loc->xl_inode->i_sb->s_blocksize_bits;
1838 block_offset = offset % loc->xl_inode->i_sb->s_blocksize;
Joel Becker11179f22009-08-14 16:07:44 -07001839
1840 return bucket_block(bucket, block) + block_offset;
1841}
1842
Joel Becker69a3e532009-08-17 12:24:39 -07001843static int ocfs2_xa_bucket_can_reuse(struct ocfs2_xa_loc *loc,
1844 struct ocfs2_xattr_info *xi)
1845{
1846 return namevalue_size_xe(loc->xl_entry) >=
1847 namevalue_size_xi(xi);
1848}
1849
1850static int ocfs2_xa_bucket_get_free_start(struct ocfs2_xa_loc *loc)
1851{
1852 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1853 return le16_to_cpu(bucket_xh(bucket)->xh_free_start);
1854}
1855
1856static int ocfs2_bucket_align_free_start(struct super_block *sb,
1857 int free_start, int size)
1858{
1859 /*
1860 * We need to make sure that the name+value pair fits within
1861 * one block.
1862 */
1863 if (((free_start - size) >> sb->s_blocksize_bits) !=
1864 ((free_start - 1) >> sb->s_blocksize_bits))
1865 free_start -= free_start % sb->s_blocksize;
1866
1867 return free_start;
1868}
1869
1870static int ocfs2_xa_bucket_check_space(struct ocfs2_xa_loc *loc,
1871 struct ocfs2_xattr_info *xi)
1872{
1873 int rc;
1874 int count = le16_to_cpu(loc->xl_header->xh_count);
1875 int free_start = ocfs2_xa_get_free_start(loc);
1876 int needed_space = ocfs2_xi_entry_usage(xi);
1877 int size = namevalue_size_xi(xi);
Joel Beckercf2bc802009-08-18 13:52:38 -07001878 struct super_block *sb = loc->xl_inode->i_sb;
Joel Becker69a3e532009-08-17 12:24:39 -07001879
1880 /*
1881 * Bucket storage does not reclaim name+value pairs it cannot
1882 * reuse. They live as holes until the bucket fills, and then
1883 * the bucket is defragmented. However, the bucket can reclaim
1884 * the ocfs2_xattr_entry.
1885 */
1886 if (loc->xl_entry) {
1887 /* Don't need space if we're reusing! */
1888 if (ocfs2_xa_can_reuse_entry(loc, xi))
1889 needed_space = 0;
1890 else
1891 needed_space -= sizeof(struct ocfs2_xattr_entry);
1892 }
1893 BUG_ON(needed_space < 0);
1894
1895 if (free_start < size) {
1896 if (needed_space)
1897 return -ENOSPC;
1898 } else {
1899 /*
1900 * First we check if it would fit in the first place.
1901 * Below, we align the free start to a block. This may
1902 * slide us below the minimum gap. By checking unaligned
1903 * first, we avoid that error.
1904 */
1905 rc = ocfs2_xa_check_space_helper(needed_space, free_start,
1906 count);
1907 if (rc)
1908 return rc;
1909 free_start = ocfs2_bucket_align_free_start(sb, free_start,
1910 size);
1911 }
1912 return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1913}
1914
Joel Becker11179f22009-08-14 16:07:44 -07001915static void ocfs2_xa_bucket_wipe_namevalue(struct ocfs2_xa_loc *loc)
1916{
Joel Becker199799a2009-08-14 19:04:15 -07001917 le16_add_cpu(&loc->xl_header->xh_name_value_len,
1918 -namevalue_size_xe(loc->xl_entry));
Joel Becker11179f22009-08-14 16:07:44 -07001919}
1920
Joel Becker69a3e532009-08-17 12:24:39 -07001921static void ocfs2_xa_bucket_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1922{
1923 struct ocfs2_xattr_header *xh = loc->xl_header;
1924 int count = le16_to_cpu(xh->xh_count);
1925 int low = 0, high = count - 1, tmp;
1926 struct ocfs2_xattr_entry *tmp_xe;
1927
1928 /*
1929 * We keep buckets sorted by name_hash, so we need to find
1930 * our insert place.
1931 */
1932 while (low <= high && count) {
1933 tmp = (low + high) / 2;
1934 tmp_xe = &xh->xh_entries[tmp];
1935
1936 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
1937 low = tmp + 1;
1938 else if (name_hash < le32_to_cpu(tmp_xe->xe_name_hash))
1939 high = tmp - 1;
1940 else {
1941 low = tmp;
1942 break;
1943 }
1944 }
1945
1946 if (low != count)
1947 memmove(&xh->xh_entries[low + 1],
1948 &xh->xh_entries[low],
1949 ((count - low) * sizeof(struct ocfs2_xattr_entry)));
1950
1951 le16_add_cpu(&xh->xh_count, 1);
1952 loc->xl_entry = &xh->xh_entries[low];
1953 memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1954}
1955
1956static void ocfs2_xa_bucket_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1957{
1958 int free_start = ocfs2_xa_get_free_start(loc);
1959 struct ocfs2_xattr_header *xh = loc->xl_header;
Joel Beckercf2bc802009-08-18 13:52:38 -07001960 struct super_block *sb = loc->xl_inode->i_sb;
Joel Becker69a3e532009-08-17 12:24:39 -07001961 int nameval_offset;
1962
1963 free_start = ocfs2_bucket_align_free_start(sb, free_start, size);
1964 nameval_offset = free_start - size;
1965 loc->xl_entry->xe_name_offset = cpu_to_le16(nameval_offset);
1966 xh->xh_free_start = cpu_to_le16(nameval_offset);
1967 le16_add_cpu(&xh->xh_name_value_len, size);
1968
1969}
1970
Joel Becker3fc12af2009-08-18 13:20:27 -07001971static void ocfs2_xa_bucket_fill_value_buf(struct ocfs2_xa_loc *loc,
1972 struct ocfs2_xattr_value_buf *vb)
1973{
1974 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
Joel Beckercf2bc802009-08-18 13:52:38 -07001975 struct super_block *sb = loc->xl_inode->i_sb;
Joel Becker3fc12af2009-08-18 13:20:27 -07001976 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1977 int size = namevalue_size_xe(loc->xl_entry);
1978 int block_offset = nameval_offset >> sb->s_blocksize_bits;
1979
1980 /* Values are not allowed to straddle block boundaries */
1981 BUG_ON(block_offset !=
1982 ((nameval_offset + size - 1) >> sb->s_blocksize_bits));
1983 /* We expect the bucket to be filled in */
1984 BUG_ON(!bucket->bu_bhs[block_offset]);
1985
1986 vb->vb_access = ocfs2_journal_access;
1987 vb->vb_bh = bucket->bu_bhs[block_offset];
1988}
1989
Joel Becker11179f22009-08-14 16:07:44 -07001990/* Operations for xattrs stored in buckets. */
1991static const struct ocfs2_xa_loc_operations ocfs2_xa_bucket_loc_ops = {
Joel Beckercf2bc802009-08-18 13:52:38 -07001992 .xlo_journal_access = ocfs2_xa_bucket_journal_access,
1993 .xlo_journal_dirty = ocfs2_xa_bucket_journal_dirty,
Joel Becker11179f22009-08-14 16:07:44 -07001994 .xlo_offset_pointer = ocfs2_xa_bucket_offset_pointer,
Joel Becker69a3e532009-08-17 12:24:39 -07001995 .xlo_check_space = ocfs2_xa_bucket_check_space,
1996 .xlo_can_reuse = ocfs2_xa_bucket_can_reuse,
1997 .xlo_get_free_start = ocfs2_xa_bucket_get_free_start,
Joel Becker11179f22009-08-14 16:07:44 -07001998 .xlo_wipe_namevalue = ocfs2_xa_bucket_wipe_namevalue,
Joel Becker69a3e532009-08-17 12:24:39 -07001999 .xlo_add_entry = ocfs2_xa_bucket_add_entry,
2000 .xlo_add_namevalue = ocfs2_xa_bucket_add_namevalue,
Joel Becker3fc12af2009-08-18 13:20:27 -07002001 .xlo_fill_value_buf = ocfs2_xa_bucket_fill_value_buf,
Joel Becker11179f22009-08-14 16:07:44 -07002002};
2003
2004static void ocfs2_xa_remove_entry(struct ocfs2_xa_loc *loc)
2005{
Joel Beckerbde1e542009-08-14 16:58:38 -07002006 int index, count;
2007 struct ocfs2_xattr_header *xh = loc->xl_header;
2008 struct ocfs2_xattr_entry *entry = loc->xl_entry;
2009
Joel Becker11179f22009-08-14 16:07:44 -07002010 ocfs2_xa_wipe_namevalue(loc);
Joel Beckerbde1e542009-08-14 16:58:38 -07002011 loc->xl_entry = NULL;
2012
2013 le16_add_cpu(&xh->xh_count, -1);
2014 count = le16_to_cpu(xh->xh_count);
2015
2016 /*
2017 * Only zero out the entry if there are more remaining. This is
2018 * important for an empty bucket, as it keeps track of the
2019 * bucket's hash value. It doesn't hurt empty block storage.
2020 */
2021 if (count) {
2022 index = ((char *)entry - (char *)&xh->xh_entries) /
2023 sizeof(struct ocfs2_xattr_entry);
2024 memmove(&xh->xh_entries[index], &xh->xh_entries[index + 1],
2025 (count - index) * sizeof(struct ocfs2_xattr_entry));
2026 memset(&xh->xh_entries[count], 0,
2027 sizeof(struct ocfs2_xattr_entry));
2028 }
Joel Becker11179f22009-08-14 16:07:44 -07002029}
2030
Joel Becker69a3e532009-08-17 12:24:39 -07002031/*
2032 * Prepares loc->xl_entry to receive the new xattr. This includes
2033 * properly setting up the name+value pair region. If loc->xl_entry
2034 * already exists, it will take care of modifying it appropriately.
2035 * This also includes deleting entries, but don't call this to remove
2036 * a non-existant entry. That's just a bug.
2037 *
2038 * Note that this modifies the data. You did journal_access already,
2039 * right?
2040 */
2041static int ocfs2_xa_prepare_entry(struct ocfs2_xa_loc *loc,
2042 struct ocfs2_xattr_info *xi,
2043 u32 name_hash)
2044{
2045 int rc = 0;
2046 int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
2047 char *nameval_buf;
2048
2049 if (!xi->xi_value) {
2050 ocfs2_xa_remove_entry(loc);
2051 goto out;
2052 }
2053
2054 rc = ocfs2_xa_check_space(loc, xi);
2055 if (rc)
2056 goto out;
2057
2058 if (loc->xl_entry) {
2059 if (ocfs2_xa_can_reuse_entry(loc, xi)) {
2060 nameval_buf = ocfs2_xa_offset_pointer(loc,
2061 le16_to_cpu(loc->xl_entry->xe_name_offset));
2062 memset(nameval_buf + name_size, 0,
2063 namevalue_size_xe(loc->xl_entry) - name_size);
2064 loc->xl_entry->xe_value_size =
2065 cpu_to_le64(xi->xi_value_len);
2066 goto out;
2067 }
2068
2069 ocfs2_xa_wipe_namevalue(loc);
2070 } else
2071 ocfs2_xa_add_entry(loc, name_hash);
2072
2073 /*
2074 * If we get here, we have a blank entry. Fill it. We grow our
2075 * name+value pair back from the end.
2076 */
2077 ocfs2_xa_add_namevalue(loc, xi);
2078
2079out:
2080 return rc;
2081}
2082
2083/*
2084 * Store the value portion of the name+value pair. This is either an
2085 * inline value or the tree root of an external value.
2086 */
2087static void ocfs2_xa_store_inline_value(struct ocfs2_xa_loc *loc,
2088 struct ocfs2_xattr_info *xi)
2089{
2090 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
2091 int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
Joel Becker9dc47402009-08-17 19:56:01 -07002092 int inline_value_size = namevalue_size_xi(xi) - name_size;
2093 const void *value = xi->xi_value;
Joel Becker69a3e532009-08-17 12:24:39 -07002094 char *nameval_buf;
2095
2096 if (!xi->xi_value)
2097 return;
2098
Joel Becker9dc47402009-08-17 19:56:01 -07002099 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2100 value = &def_xv;
2101 inline_value_size = OCFS2_XATTR_ROOT_SIZE;
2102 }
Joel Becker69a3e532009-08-17 12:24:39 -07002103 nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
Joel Becker9dc47402009-08-17 19:56:01 -07002104 memcpy(nameval_buf + name_size, value, inline_value_size);
Joel Becker69a3e532009-08-17 12:24:39 -07002105}
2106
Joel Becker11179f22009-08-14 16:07:44 -07002107static void ocfs2_init_dinode_xa_loc(struct ocfs2_xa_loc *loc,
2108 struct inode *inode,
2109 struct buffer_head *bh,
2110 struct ocfs2_xattr_entry *entry)
2111{
2112 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
2113
Joel Beckercf2bc802009-08-18 13:52:38 -07002114 loc->xl_inode = inode;
Joel Becker11179f22009-08-14 16:07:44 -07002115 loc->xl_ops = &ocfs2_xa_block_loc_ops;
2116 loc->xl_storage = bh;
2117 loc->xl_entry = entry;
2118
2119 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
2120 loc->xl_size = le16_to_cpu(di->i_xattr_inline_size);
2121 else {
2122 BUG_ON(entry);
2123 loc->xl_size = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
2124 }
2125 loc->xl_header =
2126 (struct ocfs2_xattr_header *)(bh->b_data + bh->b_size -
2127 loc->xl_size);
2128}
2129
2130static void ocfs2_init_xattr_block_xa_loc(struct ocfs2_xa_loc *loc,
Joel Beckercf2bc802009-08-18 13:52:38 -07002131 struct inode *inode,
Joel Becker11179f22009-08-14 16:07:44 -07002132 struct buffer_head *bh,
2133 struct ocfs2_xattr_entry *entry)
2134{
2135 struct ocfs2_xattr_block *xb =
2136 (struct ocfs2_xattr_block *)bh->b_data;
2137
2138 BUG_ON(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED);
2139
Joel Beckercf2bc802009-08-18 13:52:38 -07002140 loc->xl_inode = inode;
Joel Becker11179f22009-08-14 16:07:44 -07002141 loc->xl_ops = &ocfs2_xa_block_loc_ops;
2142 loc->xl_storage = bh;
2143 loc->xl_header = &(xb->xb_attrs.xb_header);
2144 loc->xl_entry = entry;
2145 loc->xl_size = bh->b_size - offsetof(struct ocfs2_xattr_block,
2146 xb_attrs.xb_header);
2147}
2148
2149static void ocfs2_init_xattr_bucket_xa_loc(struct ocfs2_xa_loc *loc,
2150 struct ocfs2_xattr_bucket *bucket,
2151 struct ocfs2_xattr_entry *entry)
2152{
Joel Beckercf2bc802009-08-18 13:52:38 -07002153 loc->xl_inode = bucket->bu_inode;
Joel Becker11179f22009-08-14 16:07:44 -07002154 loc->xl_ops = &ocfs2_xa_bucket_loc_ops;
2155 loc->xl_storage = bucket;
2156 loc->xl_header = bucket_xh(bucket);
2157 loc->xl_entry = entry;
2158 loc->xl_size = OCFS2_XATTR_BUCKET_SIZE;
2159}
2160
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002161
2162/*
2163 * ocfs2_xattr_set_entry()
2164 *
2165 * Set extended attribute entry into inode or block.
2166 *
2167 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
Joel Becker69a3e532009-08-17 12:24:39 -07002168 * We first insert tree root(ocfs2_xattr_value_root) like a normal value,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002169 * then set value in B tree with set_value_outside().
2170 */
2171static int ocfs2_xattr_set_entry(struct inode *inode,
2172 struct ocfs2_xattr_info *xi,
2173 struct ocfs2_xattr_search *xs,
Tao Ma78f30c32008-11-12 08:27:00 +08002174 struct ocfs2_xattr_set_ctxt *ctxt,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002175 int flag)
2176{
2177 struct ocfs2_xattr_entry *last;
2178 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2179 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
Joel Becker18853b92009-08-14 18:17:07 -07002180 size_t min_offs = xs->end - xs->base;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002181 size_t size_l = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08002182 handle_t *handle = ctxt->handle;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002183 int free, i, ret;
Joel Becker69a3e532009-08-17 12:24:39 -07002184 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->xi_name,
2185 xi->xi_name_len);
2186 struct ocfs2_xa_loc loc;
Joel Becker512620f2008-12-09 15:58:35 -08002187 struct ocfs2_xattr_value_buf vb = {
2188 .vb_bh = xs->xattr_bh,
2189 .vb_access = ocfs2_journal_access_di,
2190 };
2191
2192 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
2193 BUG_ON(xs->xattr_bh == xs->inode_bh);
2194 vb.vb_access = ocfs2_journal_access_xb;
2195 } else
2196 BUG_ON(xs->xattr_bh != xs->inode_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002197
2198 /* Compute min_offs, last and free space. */
2199 last = xs->header->xh_entries;
2200
2201 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
2202 size_t offs = le16_to_cpu(last->xe_name_offset);
2203 if (offs < min_offs)
2204 min_offs = offs;
2205 last += 1;
2206 }
2207
Tiger Yang4442f512009-02-20 11:11:50 +08002208 free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002209 if (free < 0)
Joel Beckerb37c4d82008-10-20 18:24:03 -07002210 return -EIO;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002211
Joel Becker199799a2009-08-14 19:04:15 -07002212 if (!xs->not_found)
2213 free += ocfs2_xe_entry_usage(xs->here);
2214
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002215 /* Check free space in inode or block */
Joel Becker9dc47402009-08-17 19:56:01 -07002216 if (xi->xi_value && (free < ocfs2_xi_entry_usage(xi))) {
2217 ret = -ENOSPC;
2218 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002219 }
2220
2221 if (!xs->not_found) {
2222 /* For existing extended attribute */
Joel Becker199799a2009-08-14 19:04:15 -07002223 size_t size = namevalue_size_xe(xs->here);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002224 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
2225 void *val = xs->base + offs;
2226
2227 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
2228 /* Replace existing local xattr with tree root */
2229 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
Joel Becker512620f2008-12-09 15:58:35 -08002230 ctxt, &vb, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002231 if (ret < 0)
2232 mlog_errno(ret);
2233 goto out;
2234 } else if (!ocfs2_xattr_is_local(xs->here)) {
2235 /* For existing xattr which has value outside */
Joel Becker512620f2008-12-09 15:58:35 -08002236 vb.vb_xv = (struct ocfs2_xattr_value_root *)
Joel Becker18853b92009-08-14 18:17:07 -07002237 (val + OCFS2_XATTR_SIZE(xi->xi_name_len));
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002238
Joel Becker6b240ff2009-08-14 18:02:52 -07002239 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002240 /*
2241 * If new value need set outside also,
2242 * first truncate old value to new value,
2243 * then set new value with set_value_outside().
2244 */
2245 ret = ocfs2_xattr_value_truncate(inode,
Joel Becker6b240ff2009-08-14 18:02:52 -07002246 &vb,
2247 xi->xi_value_len,
2248 ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002249 if (ret < 0) {
2250 mlog_errno(ret);
2251 goto out;
2252 }
2253
Tao Ma85db90e2008-11-12 08:27:01 +08002254 ret = ocfs2_xattr_update_entry(inode,
2255 handle,
2256 xi,
2257 xs,
Joel Becker0c748e92008-12-09 15:46:15 -08002258 &vb,
Tao Ma85db90e2008-11-12 08:27:01 +08002259 offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002260 if (ret < 0) {
2261 mlog_errno(ret);
2262 goto out;
2263 }
2264
Tao Ma85db90e2008-11-12 08:27:01 +08002265 ret = __ocfs2_xattr_set_value_outside(inode,
Joel Becker6b240ff2009-08-14 18:02:52 -07002266 handle,
2267 &vb,
2268 xi->xi_value,
2269 xi->xi_value_len);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002270 if (ret < 0)
2271 mlog_errno(ret);
2272 goto out;
2273 } else {
2274 /*
2275 * If new value need set in local,
2276 * just trucate old value to zero.
2277 */
2278 ret = ocfs2_xattr_value_truncate(inode,
Joel Beckerb3e5d372008-12-09 15:01:04 -08002279 &vb,
Tao Ma85db90e2008-11-12 08:27:01 +08002280 0,
2281 ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002282 if (ret < 0)
2283 mlog_errno(ret);
2284 }
2285 }
2286 }
2287
Joel Becker0cf2f762009-02-12 16:41:25 -08002288 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), xs->inode_bh,
Joel Becker512620f2008-12-09 15:58:35 -08002289 OCFS2_JOURNAL_ACCESS_WRITE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002290 if (ret) {
2291 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08002292 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002293 }
2294
Joel Becker69a3e532009-08-17 12:24:39 -07002295 if (xs->xattr_bh == xs->inode_bh)
2296 ocfs2_init_dinode_xa_loc(&loc, inode, xs->inode_bh,
2297 xs->not_found ? NULL : xs->here);
2298 else
Joel Beckercf2bc802009-08-18 13:52:38 -07002299 ocfs2_init_xattr_block_xa_loc(&loc, inode, xs->xattr_bh,
Joel Becker69a3e532009-08-17 12:24:39 -07002300 xs->not_found ? NULL : xs->here);
Joel Beckercf2bc802009-08-18 13:52:38 -07002301 ret = ocfs2_xa_journal_access(handle, &loc,
2302 OCFS2_JOURNAL_ACCESS_WRITE);
2303 if (ret) {
2304 mlog_errno(ret);
2305 goto out;
2306 }
Joel Becker69a3e532009-08-17 12:24:39 -07002307
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002308 /*
Joel Becker69a3e532009-08-17 12:24:39 -07002309 * Prepare our entry and insert the inline value. This will
2310 * be a value tree root for values that are larger than
2311 * OCFS2_XATTR_INLINE_SIZE.
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002312 */
Joel Becker69a3e532009-08-17 12:24:39 -07002313 ret = ocfs2_xa_prepare_entry(&loc, xi, name_hash);
2314 if (ret) {
2315 if (ret != -ENOSPC)
2316 mlog_errno(ret);
2317 goto out;
2318 }
2319 /* XXX For now, until we make ocfs2_xa_prepare_entry() primary */
2320 BUG_ON(ret == -ENOSPC);
2321 ocfs2_xa_store_inline_value(&loc, xi);
2322 xs->here = loc.xl_entry;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002323
Joel Beckercf2bc802009-08-18 13:52:38 -07002324 ocfs2_xa_journal_dirty(handle, &loc);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002325
2326 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
2327 (flag & OCFS2_INLINE_XATTR_FL)) {
2328 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2329 unsigned int xattrsize = osb->s_xattr_inline_size;
2330
2331 /*
2332 * Adjust extent record count or inline data size
2333 * to reserve space for extended attribute.
2334 */
2335 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2336 struct ocfs2_inline_data *idata = &di->id2.i_data;
2337 le16_add_cpu(&idata->id_count, -xattrsize);
2338 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
2339 struct ocfs2_extent_list *el = &di->id2.i_list;
2340 le16_add_cpu(&el->l_count, -(xattrsize /
2341 sizeof(struct ocfs2_extent_rec)));
2342 }
2343 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
2344 }
2345 /* Update xattr flag */
2346 spin_lock(&oi->ip_lock);
2347 oi->ip_dyn_features |= flag;
2348 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2349 spin_unlock(&oi->ip_lock);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002350
2351 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
2352 if (ret < 0)
2353 mlog_errno(ret);
2354
Joel Becker6b240ff2009-08-14 18:02:52 -07002355 if (!ret && xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002356 /*
2357 * Set value outside in B tree.
2358 * This is the second step for value size > INLINE_SIZE.
2359 */
2360 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
Joel Becker512620f2008-12-09 15:58:35 -08002361 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, ctxt,
2362 &vb, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002363 if (ret < 0) {
2364 int ret2;
2365
2366 mlog_errno(ret);
2367 /*
2368 * If set value outside failed, we have to clean
2369 * the junk tree root we have already set in local.
2370 */
Tao Ma85db90e2008-11-12 08:27:01 +08002371 ret2 = ocfs2_xattr_cleanup(inode, ctxt->handle,
Joel Becker512620f2008-12-09 15:58:35 -08002372 xi, xs, &vb, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002373 if (ret2 < 0)
2374 mlog_errno(ret2);
2375 }
2376 }
2377out:
2378 return ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002379}
2380
Tao Mace9c5a52009-08-18 11:43:59 +08002381/*
2382 * In xattr remove, if it is stored outside and refcounted, we may have
2383 * the chance to split the refcount tree. So need the allocators.
2384 */
2385static int ocfs2_lock_xattr_remove_allocators(struct inode *inode,
2386 struct ocfs2_xattr_value_root *xv,
2387 struct ocfs2_caching_info *ref_ci,
2388 struct buffer_head *ref_root_bh,
2389 struct ocfs2_alloc_context **meta_ac,
2390 int *ref_credits)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002391{
Tao Mace9c5a52009-08-18 11:43:59 +08002392 int ret, meta_add = 0;
2393 u32 p_cluster, num_clusters;
2394 unsigned int ext_flags;
Tao Ma78f30c32008-11-12 08:27:00 +08002395
Tao Mace9c5a52009-08-18 11:43:59 +08002396 *ref_credits = 0;
2397 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
2398 &num_clusters,
2399 &xv->xr_list,
2400 &ext_flags);
2401 if (ret) {
Tao Ma85db90e2008-11-12 08:27:01 +08002402 mlog_errno(ret);
2403 goto out;
2404 }
2405
Tao Mace9c5a52009-08-18 11:43:59 +08002406 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
2407 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002408
Tao Mace9c5a52009-08-18 11:43:59 +08002409 ret = ocfs2_refcounted_xattr_delete_need(inode, ref_ci,
2410 ref_root_bh, xv,
2411 &meta_add, ref_credits);
2412 if (ret) {
2413 mlog_errno(ret);
2414 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002415 }
2416
Tao Mace9c5a52009-08-18 11:43:59 +08002417 ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
2418 meta_add, meta_ac);
2419 if (ret)
2420 mlog_errno(ret);
2421
Tao Ma85db90e2008-11-12 08:27:01 +08002422out:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002423 return ret;
2424}
2425
Tao Mace9c5a52009-08-18 11:43:59 +08002426static int ocfs2_remove_value_outside(struct inode*inode,
2427 struct ocfs2_xattr_value_buf *vb,
2428 struct ocfs2_xattr_header *header,
2429 struct ocfs2_caching_info *ref_ci,
2430 struct buffer_head *ref_root_bh)
2431{
2432 int ret = 0, i, ref_credits;
2433 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2434 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
2435 void *val;
2436
2437 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
2438
2439 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
2440 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
2441
2442 if (ocfs2_xattr_is_local(entry))
2443 continue;
2444
2445 val = (void *)header +
2446 le16_to_cpu(entry->xe_name_offset);
2447 vb->vb_xv = (struct ocfs2_xattr_value_root *)
2448 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
2449
2450 ret = ocfs2_lock_xattr_remove_allocators(inode, vb->vb_xv,
2451 ref_ci, ref_root_bh,
2452 &ctxt.meta_ac,
2453 &ref_credits);
2454
2455 ctxt.handle = ocfs2_start_trans(osb, ref_credits +
2456 ocfs2_remove_extent_credits(osb->sb));
2457 if (IS_ERR(ctxt.handle)) {
2458 ret = PTR_ERR(ctxt.handle);
2459 mlog_errno(ret);
2460 break;
2461 }
2462
2463 ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt);
2464 if (ret < 0) {
2465 mlog_errno(ret);
2466 break;
2467 }
2468
2469 ocfs2_commit_trans(osb, ctxt.handle);
2470 if (ctxt.meta_ac) {
2471 ocfs2_free_alloc_context(ctxt.meta_ac);
2472 ctxt.meta_ac = NULL;
2473 }
2474 }
2475
2476 if (ctxt.meta_ac)
2477 ocfs2_free_alloc_context(ctxt.meta_ac);
2478 ocfs2_schedule_truncate_log_flush(osb, 1);
2479 ocfs2_run_deallocs(osb, &ctxt.dealloc);
2480 return ret;
2481}
2482
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002483static int ocfs2_xattr_ibody_remove(struct inode *inode,
Tao Mace9c5a52009-08-18 11:43:59 +08002484 struct buffer_head *di_bh,
2485 struct ocfs2_caching_info *ref_ci,
2486 struct buffer_head *ref_root_bh)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002487{
2488
2489 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2490 struct ocfs2_xattr_header *header;
2491 int ret;
Joel Becker43119012008-12-09 16:24:43 -08002492 struct ocfs2_xattr_value_buf vb = {
2493 .vb_bh = di_bh,
2494 .vb_access = ocfs2_journal_access_di,
2495 };
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002496
2497 header = (struct ocfs2_xattr_header *)
2498 ((void *)di + inode->i_sb->s_blocksize -
2499 le16_to_cpu(di->i_xattr_inline_size));
2500
Tao Mace9c5a52009-08-18 11:43:59 +08002501 ret = ocfs2_remove_value_outside(inode, &vb, header,
2502 ref_ci, ref_root_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002503
2504 return ret;
2505}
2506
Tao Mace9c5a52009-08-18 11:43:59 +08002507struct ocfs2_rm_xattr_bucket_para {
2508 struct ocfs2_caching_info *ref_ci;
2509 struct buffer_head *ref_root_bh;
2510};
2511
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002512static int ocfs2_xattr_block_remove(struct inode *inode,
Tao Mace9c5a52009-08-18 11:43:59 +08002513 struct buffer_head *blk_bh,
2514 struct ocfs2_caching_info *ref_ci,
2515 struct buffer_head *ref_root_bh)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002516{
2517 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002518 int ret = 0;
Joel Becker43119012008-12-09 16:24:43 -08002519 struct ocfs2_xattr_value_buf vb = {
2520 .vb_bh = blk_bh,
2521 .vb_access = ocfs2_journal_access_xb,
2522 };
Tao Mace9c5a52009-08-18 11:43:59 +08002523 struct ocfs2_rm_xattr_bucket_para args = {
2524 .ref_ci = ref_ci,
2525 .ref_root_bh = ref_root_bh,
2526 };
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002527
2528 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Maa3944252008-08-18 17:38:54 +08002529 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2530 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
Tao Mace9c5a52009-08-18 11:43:59 +08002531 ret = ocfs2_remove_value_outside(inode, &vb, header,
2532 ref_ci, ref_root_bh);
Tao Maa3944252008-08-18 17:38:54 +08002533 } else
Tao Ma47bca492009-08-18 11:43:42 +08002534 ret = ocfs2_iterate_xattr_index_block(inode,
2535 blk_bh,
2536 ocfs2_rm_xattr_cluster,
Tao Mace9c5a52009-08-18 11:43:59 +08002537 &args);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002538
2539 return ret;
2540}
2541
Tao Ma08413892008-08-29 09:00:19 +08002542static int ocfs2_xattr_free_block(struct inode *inode,
Tao Mace9c5a52009-08-18 11:43:59 +08002543 u64 block,
2544 struct ocfs2_caching_info *ref_ci,
2545 struct buffer_head *ref_root_bh)
Tao Ma08413892008-08-29 09:00:19 +08002546{
2547 struct inode *xb_alloc_inode;
2548 struct buffer_head *xb_alloc_bh = NULL;
2549 struct buffer_head *blk_bh = NULL;
2550 struct ocfs2_xattr_block *xb;
2551 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2552 handle_t *handle;
2553 int ret = 0;
2554 u64 blk, bg_blkno;
2555 u16 bit;
2556
Joel Becker4ae1d692008-11-13 14:49:18 -08002557 ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
Tao Ma08413892008-08-29 09:00:19 +08002558 if (ret < 0) {
2559 mlog_errno(ret);
2560 goto out;
2561 }
2562
Tao Mace9c5a52009-08-18 11:43:59 +08002563 ret = ocfs2_xattr_block_remove(inode, blk_bh, ref_ci, ref_root_bh);
Tao Ma08413892008-08-29 09:00:19 +08002564 if (ret < 0) {
2565 mlog_errno(ret);
2566 goto out;
2567 }
2568
Joel Becker4ae1d692008-11-13 14:49:18 -08002569 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Ma08413892008-08-29 09:00:19 +08002570 blk = le64_to_cpu(xb->xb_blkno);
2571 bit = le16_to_cpu(xb->xb_suballoc_bit);
2572 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2573
2574 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
2575 EXTENT_ALLOC_SYSTEM_INODE,
2576 le16_to_cpu(xb->xb_suballoc_slot));
2577 if (!xb_alloc_inode) {
2578 ret = -ENOMEM;
2579 mlog_errno(ret);
2580 goto out;
2581 }
2582 mutex_lock(&xb_alloc_inode->i_mutex);
2583
2584 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
2585 if (ret < 0) {
2586 mlog_errno(ret);
2587 goto out_mutex;
2588 }
2589
2590 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
2591 if (IS_ERR(handle)) {
2592 ret = PTR_ERR(handle);
2593 mlog_errno(ret);
2594 goto out_unlock;
2595 }
2596
2597 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
2598 bit, bg_blkno, 1);
2599 if (ret < 0)
2600 mlog_errno(ret);
2601
2602 ocfs2_commit_trans(osb, handle);
2603out_unlock:
2604 ocfs2_inode_unlock(xb_alloc_inode, 1);
2605 brelse(xb_alloc_bh);
2606out_mutex:
2607 mutex_unlock(&xb_alloc_inode->i_mutex);
2608 iput(xb_alloc_inode);
2609out:
2610 brelse(blk_bh);
2611 return ret;
2612}
2613
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002614/*
2615 * ocfs2_xattr_remove()
2616 *
2617 * Free extended attribute resources associated with this inode.
2618 */
2619int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
2620{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002621 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2622 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
Tao Mace9c5a52009-08-18 11:43:59 +08002623 struct ocfs2_refcount_tree *ref_tree = NULL;
2624 struct buffer_head *ref_root_bh = NULL;
2625 struct ocfs2_caching_info *ref_ci = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002626 handle_t *handle;
2627 int ret;
2628
Tiger Yang8154da32008-08-18 17:11:46 +08002629 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2630 return 0;
2631
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002632 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
2633 return 0;
2634
Tao Mace9c5a52009-08-18 11:43:59 +08002635 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) {
2636 ret = ocfs2_lock_refcount_tree(OCFS2_SB(inode->i_sb),
2637 le64_to_cpu(di->i_refcount_loc),
2638 1, &ref_tree, &ref_root_bh);
2639 if (ret) {
2640 mlog_errno(ret);
2641 goto out;
2642 }
2643 ref_ci = &ref_tree->rf_ci;
2644
2645 }
2646
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002647 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
Tao Mace9c5a52009-08-18 11:43:59 +08002648 ret = ocfs2_xattr_ibody_remove(inode, di_bh,
2649 ref_ci, ref_root_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002650 if (ret < 0) {
2651 mlog_errno(ret);
2652 goto out;
2653 }
2654 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002655
Tao Ma08413892008-08-29 09:00:19 +08002656 if (di->i_xattr_loc) {
2657 ret = ocfs2_xattr_free_block(inode,
Tao Mace9c5a52009-08-18 11:43:59 +08002658 le64_to_cpu(di->i_xattr_loc),
2659 ref_ci, ref_root_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002660 if (ret < 0) {
2661 mlog_errno(ret);
2662 goto out;
2663 }
2664 }
2665
2666 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
2667 OCFS2_INODE_UPDATE_CREDITS);
2668 if (IS_ERR(handle)) {
2669 ret = PTR_ERR(handle);
2670 mlog_errno(ret);
2671 goto out;
2672 }
Joel Becker0cf2f762009-02-12 16:41:25 -08002673 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker84008972008-12-09 16:11:49 -08002674 OCFS2_JOURNAL_ACCESS_WRITE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002675 if (ret) {
2676 mlog_errno(ret);
2677 goto out_commit;
2678 }
2679
Tao Ma08413892008-08-29 09:00:19 +08002680 di->i_xattr_loc = 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002681
2682 spin_lock(&oi->ip_lock);
2683 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
2684 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2685 spin_unlock(&oi->ip_lock);
2686
2687 ret = ocfs2_journal_dirty(handle, di_bh);
2688 if (ret < 0)
2689 mlog_errno(ret);
2690out_commit:
2691 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
2692out:
Tao Mace9c5a52009-08-18 11:43:59 +08002693 if (ref_tree)
2694 ocfs2_unlock_refcount_tree(OCFS2_SB(inode->i_sb), ref_tree, 1);
2695 brelse(ref_root_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002696 return ret;
2697}
2698
2699static int ocfs2_xattr_has_space_inline(struct inode *inode,
2700 struct ocfs2_dinode *di)
2701{
2702 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2703 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
2704 int free;
2705
2706 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
2707 return 0;
2708
2709 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2710 struct ocfs2_inline_data *idata = &di->id2.i_data;
2711 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
2712 } else if (ocfs2_inode_is_fast_symlink(inode)) {
2713 free = ocfs2_fast_symlink_chars(inode->i_sb) -
2714 le64_to_cpu(di->i_size);
2715 } else {
2716 struct ocfs2_extent_list *el = &di->id2.i_list;
2717 free = (le16_to_cpu(el->l_count) -
2718 le16_to_cpu(el->l_next_free_rec)) *
2719 sizeof(struct ocfs2_extent_rec);
2720 }
2721 if (free >= xattrsize)
2722 return 1;
2723
2724 return 0;
2725}
2726
2727/*
2728 * ocfs2_xattr_ibody_find()
2729 *
2730 * Find extended attribute in inode block and
2731 * fill search info into struct ocfs2_xattr_search.
2732 */
2733static int ocfs2_xattr_ibody_find(struct inode *inode,
2734 int name_index,
2735 const char *name,
2736 struct ocfs2_xattr_search *xs)
2737{
2738 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2739 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2740 int ret;
2741 int has_space = 0;
2742
2743 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2744 return 0;
2745
2746 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2747 down_read(&oi->ip_alloc_sem);
2748 has_space = ocfs2_xattr_has_space_inline(inode, di);
2749 up_read(&oi->ip_alloc_sem);
2750 if (!has_space)
2751 return 0;
2752 }
2753
2754 xs->xattr_bh = xs->inode_bh;
2755 xs->end = (void *)di + inode->i_sb->s_blocksize;
2756 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
2757 xs->header = (struct ocfs2_xattr_header *)
2758 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
2759 else
2760 xs->header = (struct ocfs2_xattr_header *)
2761 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
2762 xs->base = (void *)xs->header;
2763 xs->here = xs->header->xh_entries;
2764
2765 /* Find the named attribute. */
2766 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2767 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2768 if (ret && ret != -ENODATA)
2769 return ret;
2770 xs->not_found = ret;
2771 }
2772
2773 return 0;
2774}
2775
2776/*
2777 * ocfs2_xattr_ibody_set()
2778 *
2779 * Set, replace or remove an extended attribute into inode block.
2780 *
2781 */
2782static int ocfs2_xattr_ibody_set(struct inode *inode,
2783 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08002784 struct ocfs2_xattr_search *xs,
2785 struct ocfs2_xattr_set_ctxt *ctxt)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002786{
2787 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2788 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2789 int ret;
2790
2791 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2792 return -ENOSPC;
2793
2794 down_write(&oi->ip_alloc_sem);
2795 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2796 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2797 ret = -ENOSPC;
2798 goto out;
2799 }
2800 }
2801
Tao Ma78f30c32008-11-12 08:27:00 +08002802 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002803 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
2804out:
2805 up_write(&oi->ip_alloc_sem);
2806
2807 return ret;
2808}
2809
2810/*
2811 * ocfs2_xattr_block_find()
2812 *
2813 * Find extended attribute in external block and
2814 * fill search info into struct ocfs2_xattr_search.
2815 */
2816static int ocfs2_xattr_block_find(struct inode *inode,
2817 int name_index,
2818 const char *name,
2819 struct ocfs2_xattr_search *xs)
2820{
2821 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2822 struct buffer_head *blk_bh = NULL;
Tao Ma589dc262008-08-18 17:38:51 +08002823 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002824 int ret = 0;
2825
2826 if (!di->i_xattr_loc)
2827 return ret;
2828
Joel Becker4ae1d692008-11-13 14:49:18 -08002829 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
2830 &blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002831 if (ret < 0) {
2832 mlog_errno(ret);
2833 return ret;
2834 }
Joel Beckerf6087fb2008-10-20 18:20:43 -07002835
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002836 xs->xattr_bh = blk_bh;
Joel Becker4ae1d692008-11-13 14:49:18 -08002837 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002838
Tao Ma589dc262008-08-18 17:38:51 +08002839 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2840 xs->header = &xb->xb_attrs.xb_header;
2841 xs->base = (void *)xs->header;
2842 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
2843 xs->here = xs->header->xh_entries;
2844
2845 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2846 } else
2847 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
2848 name_index,
2849 name, xs);
2850
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002851 if (ret && ret != -ENODATA) {
2852 xs->xattr_bh = NULL;
2853 goto cleanup;
2854 }
2855 xs->not_found = ret;
2856 return 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002857cleanup:
2858 brelse(blk_bh);
2859
2860 return ret;
2861}
2862
Tao Ma5aea1f02009-08-18 11:43:24 +08002863static int ocfs2_create_xattr_block(handle_t *handle,
2864 struct inode *inode,
2865 struct buffer_head *inode_bh,
2866 struct ocfs2_alloc_context *meta_ac,
Tao Maa7fe7a32009-08-18 11:43:52 +08002867 struct buffer_head **ret_bh,
2868 int indexed)
Tao Ma5aea1f02009-08-18 11:43:24 +08002869{
2870 int ret;
2871 u16 suballoc_bit_start;
2872 u32 num_got;
2873 u64 first_blkno;
2874 struct ocfs2_dinode *di = (struct ocfs2_dinode *)inode_bh->b_data;
2875 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2876 struct buffer_head *new_bh = NULL;
2877 struct ocfs2_xattr_block *xblk;
2878
2879 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), inode_bh,
2880 OCFS2_JOURNAL_ACCESS_CREATE);
2881 if (ret < 0) {
2882 mlog_errno(ret);
2883 goto end;
2884 }
2885
2886 ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
2887 &suballoc_bit_start, &num_got,
2888 &first_blkno);
2889 if (ret < 0) {
2890 mlog_errno(ret);
2891 goto end;
2892 }
2893
2894 new_bh = sb_getblk(inode->i_sb, first_blkno);
2895 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
2896
2897 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode),
2898 new_bh,
2899 OCFS2_JOURNAL_ACCESS_CREATE);
2900 if (ret < 0) {
2901 mlog_errno(ret);
2902 goto end;
2903 }
2904
2905 /* Initialize ocfs2_xattr_block */
2906 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2907 memset(xblk, 0, inode->i_sb->s_blocksize);
2908 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
Tiger Yangb89c5422010-01-25 14:11:06 +08002909 xblk->xb_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
Tao Ma5aea1f02009-08-18 11:43:24 +08002910 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
2911 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
2912 xblk->xb_blkno = cpu_to_le64(first_blkno);
2913
Tao Maa7fe7a32009-08-18 11:43:52 +08002914 if (indexed) {
2915 struct ocfs2_xattr_tree_root *xr = &xblk->xb_attrs.xb_root;
2916 xr->xt_clusters = cpu_to_le32(1);
2917 xr->xt_last_eb_blk = 0;
2918 xr->xt_list.l_tree_depth = 0;
2919 xr->xt_list.l_count = cpu_to_le16(
2920 ocfs2_xattr_recs_per_xb(inode->i_sb));
2921 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2922 xblk->xb_flags = cpu_to_le16(OCFS2_XATTR_INDEXED);
2923 }
2924
Tao Ma5aea1f02009-08-18 11:43:24 +08002925 ret = ocfs2_journal_dirty(handle, new_bh);
2926 if (ret < 0) {
2927 mlog_errno(ret);
2928 goto end;
2929 }
2930 di->i_xattr_loc = cpu_to_le64(first_blkno);
2931 ocfs2_journal_dirty(handle, inode_bh);
2932
2933 *ret_bh = new_bh;
2934 new_bh = NULL;
2935
2936end:
2937 brelse(new_bh);
2938 return ret;
2939}
2940
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002941/*
2942 * ocfs2_xattr_block_set()
2943 *
2944 * Set, replace or remove an extended attribute into external block.
2945 *
2946 */
2947static int ocfs2_xattr_block_set(struct inode *inode,
2948 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08002949 struct ocfs2_xattr_search *xs,
2950 struct ocfs2_xattr_set_ctxt *ctxt)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002951{
2952 struct buffer_head *new_bh = NULL;
Tao Ma85db90e2008-11-12 08:27:01 +08002953 handle_t *handle = ctxt->handle;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002954 struct ocfs2_xattr_block *xblk = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002955 int ret;
2956
2957 if (!xs->xattr_bh) {
Tao Ma5aea1f02009-08-18 11:43:24 +08002958 ret = ocfs2_create_xattr_block(handle, inode, xs->inode_bh,
Tao Maa7fe7a32009-08-18 11:43:52 +08002959 ctxt->meta_ac, &new_bh, 0);
Tao Ma5aea1f02009-08-18 11:43:24 +08002960 if (ret) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002961 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08002962 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002963 }
2964
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002965 xs->xattr_bh = new_bh;
Tao Ma5aea1f02009-08-18 11:43:24 +08002966 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002967 xs->header = &xblk->xb_attrs.xb_header;
2968 xs->base = (void *)xs->header;
2969 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2970 xs->here = xs->header->xh_entries;
Tao Ma01225592008-08-18 17:38:53 +08002971 } else
2972 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2973
2974 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
2975 /* Set extended attribute into external block */
Tao Ma78f30c32008-11-12 08:27:00 +08002976 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
2977 OCFS2_HAS_XATTR_FL);
Tao Ma01225592008-08-18 17:38:53 +08002978 if (!ret || ret != -ENOSPC)
2979 goto end;
2980
Tao Ma78f30c32008-11-12 08:27:00 +08002981 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08002982 if (ret)
2983 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002984 }
2985
Tao Ma78f30c32008-11-12 08:27:00 +08002986 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08002987
2988end:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002989
2990 return ret;
2991}
2992
Tao Ma78f30c32008-11-12 08:27:00 +08002993/* Check whether the new xattr can be inserted into the inode. */
2994static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
2995 struct ocfs2_xattr_info *xi,
2996 struct ocfs2_xattr_search *xs)
2997{
Tao Ma78f30c32008-11-12 08:27:00 +08002998 struct ocfs2_xattr_entry *last;
2999 int free, i;
3000 size_t min_offs = xs->end - xs->base;
3001
3002 if (!xs->header)
3003 return 0;
3004
3005 last = xs->header->xh_entries;
3006
3007 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
3008 size_t offs = le16_to_cpu(last->xe_name_offset);
3009 if (offs < min_offs)
3010 min_offs = offs;
3011 last += 1;
3012 }
3013
Tiger Yang4442f512009-02-20 11:11:50 +08003014 free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
Tao Ma78f30c32008-11-12 08:27:00 +08003015 if (free < 0)
3016 return 0;
3017
3018 BUG_ON(!xs->not_found);
3019
Joel Becker199799a2009-08-14 19:04:15 -07003020 if (free >= (sizeof(struct ocfs2_xattr_entry) + namevalue_size_xi(xi)))
Tao Ma78f30c32008-11-12 08:27:00 +08003021 return 1;
3022
3023 return 0;
3024}
3025
3026static int ocfs2_calc_xattr_set_need(struct inode *inode,
3027 struct ocfs2_dinode *di,
3028 struct ocfs2_xattr_info *xi,
3029 struct ocfs2_xattr_search *xis,
3030 struct ocfs2_xattr_search *xbs,
3031 int *clusters_need,
Tao Ma85db90e2008-11-12 08:27:01 +08003032 int *meta_need,
3033 int *credits_need)
Tao Ma78f30c32008-11-12 08:27:00 +08003034{
3035 int ret = 0, old_in_xb = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08003036 int clusters_add = 0, meta_add = 0, credits = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08003037 struct buffer_head *bh = NULL;
3038 struct ocfs2_xattr_block *xb = NULL;
3039 struct ocfs2_xattr_entry *xe = NULL;
3040 struct ocfs2_xattr_value_root *xv = NULL;
3041 char *base = NULL;
3042 int name_offset, name_len = 0;
3043 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
Joel Becker6b240ff2009-08-14 18:02:52 -07003044 xi->xi_value_len);
Tao Ma78f30c32008-11-12 08:27:00 +08003045 u64 value_size;
3046
Tao Ma71d548a2008-12-05 06:20:54 +08003047 /*
3048 * Calculate the clusters we need to write.
3049 * No matter whether we replace an old one or add a new one,
3050 * we need this for writing.
3051 */
Joel Becker6b240ff2009-08-14 18:02:52 -07003052 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
Tao Ma71d548a2008-12-05 06:20:54 +08003053 credits += new_clusters *
3054 ocfs2_clusters_to_blocks(inode->i_sb, 1);
3055
Tao Ma78f30c32008-11-12 08:27:00 +08003056 if (xis->not_found && xbs->not_found) {
Tao Ma85db90e2008-11-12 08:27:01 +08003057 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3058
Joel Becker6b240ff2009-08-14 18:02:52 -07003059 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
Tao Ma78f30c32008-11-12 08:27:00 +08003060 clusters_add += new_clusters;
Tao Ma85db90e2008-11-12 08:27:01 +08003061 credits += ocfs2_calc_extend_credits(inode->i_sb,
3062 &def_xv.xv.xr_list,
3063 new_clusters);
3064 }
Tao Ma78f30c32008-11-12 08:27:00 +08003065
3066 goto meta_guess;
3067 }
3068
3069 if (!xis->not_found) {
3070 xe = xis->here;
3071 name_offset = le16_to_cpu(xe->xe_name_offset);
3072 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3073 base = xis->base;
Tao Ma85db90e2008-11-12 08:27:01 +08003074 credits += OCFS2_INODE_UPDATE_CREDITS;
Tao Ma78f30c32008-11-12 08:27:00 +08003075 } else {
Joel Becker970e4932008-11-13 14:49:19 -08003076 int i, block_off = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08003077 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
3078 xe = xbs->here;
3079 name_offset = le16_to_cpu(xe->xe_name_offset);
3080 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3081 i = xbs->here - xbs->header->xh_entries;
3082 old_in_xb = 1;
3083
3084 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
Tao Mafd68a892009-08-18 11:43:21 +08003085 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Tao Ma78f30c32008-11-12 08:27:00 +08003086 bucket_xh(xbs->bucket),
3087 i, &block_off,
3088 &name_offset);
3089 base = bucket_block(xbs->bucket, block_off);
Tao Ma85db90e2008-11-12 08:27:01 +08003090 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3091 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08003092 base = xbs->base;
Tao Ma85db90e2008-11-12 08:27:01 +08003093 credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
3094 }
3095 }
3096
3097 /*
3098 * delete a xattr doesn't need metadata and cluster allocation.
3099 * so just calculate the credits and return.
3100 *
3101 * The credits for removing the value tree will be extended
3102 * by ocfs2_remove_extent itself.
3103 */
Joel Becker6b240ff2009-08-14 18:02:52 -07003104 if (!xi->xi_value) {
Tao Ma85db90e2008-11-12 08:27:01 +08003105 if (!ocfs2_xattr_is_local(xe))
Jan Karaa90714c2008-10-09 19:38:40 +02003106 credits += ocfs2_remove_extent_credits(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08003107
3108 goto out;
Tao Ma78f30c32008-11-12 08:27:00 +08003109 }
3110
3111 /* do cluster allocation guess first. */
3112 value_size = le64_to_cpu(xe->xe_value_size);
3113
3114 if (old_in_xb) {
3115 /*
3116 * In xattr set, we always try to set the xe in inode first,
3117 * so if it can be inserted into inode successfully, the old
3118 * one will be removed from the xattr block, and this xattr
3119 * will be inserted into inode as a new xattr in inode.
3120 */
3121 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
3122 clusters_add += new_clusters;
Jan Karaa90714c2008-10-09 19:38:40 +02003123 credits += ocfs2_remove_extent_credits(inode->i_sb) +
Tao Ma85db90e2008-11-12 08:27:01 +08003124 OCFS2_INODE_UPDATE_CREDITS;
3125 if (!ocfs2_xattr_is_local(xe))
3126 credits += ocfs2_calc_extend_credits(
3127 inode->i_sb,
3128 &def_xv.xv.xr_list,
3129 new_clusters);
Tao Ma78f30c32008-11-12 08:27:00 +08003130 goto out;
3131 }
3132 }
3133
Joel Becker6b240ff2009-08-14 18:02:52 -07003134 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
Tao Ma78f30c32008-11-12 08:27:00 +08003135 /* the new values will be stored outside. */
3136 u32 old_clusters = 0;
3137
3138 if (!ocfs2_xattr_is_local(xe)) {
3139 old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
3140 value_size);
3141 xv = (struct ocfs2_xattr_value_root *)
3142 (base + name_offset + name_len);
Tao Ma97aff522008-11-19 16:48:41 +08003143 value_size = OCFS2_XATTR_ROOT_SIZE;
Tao Ma78f30c32008-11-12 08:27:00 +08003144 } else
3145 xv = &def_xv.xv;
3146
Tao Ma85db90e2008-11-12 08:27:01 +08003147 if (old_clusters >= new_clusters) {
Jan Karaa90714c2008-10-09 19:38:40 +02003148 credits += ocfs2_remove_extent_credits(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08003149 goto out;
Tao Ma85db90e2008-11-12 08:27:01 +08003150 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08003151 meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
3152 clusters_add += new_clusters - old_clusters;
Tao Ma85db90e2008-11-12 08:27:01 +08003153 credits += ocfs2_calc_extend_credits(inode->i_sb,
3154 &xv->xr_list,
3155 new_clusters -
3156 old_clusters);
Tao Ma97aff522008-11-19 16:48:41 +08003157 if (value_size >= OCFS2_XATTR_ROOT_SIZE)
3158 goto out;
Tao Ma78f30c32008-11-12 08:27:00 +08003159 }
3160 } else {
3161 /*
3162 * Now the new value will be stored inside. So if the new
3163 * value is smaller than the size of value root or the old
3164 * value, we don't need any allocation, otherwise we have
3165 * to guess metadata allocation.
3166 */
Joel Becker6b240ff2009-08-14 18:02:52 -07003167 if ((ocfs2_xattr_is_local(xe) &&
3168 (value_size >= xi->xi_value_len)) ||
Tao Ma78f30c32008-11-12 08:27:00 +08003169 (!ocfs2_xattr_is_local(xe) &&
Joel Becker6b240ff2009-08-14 18:02:52 -07003170 OCFS2_XATTR_ROOT_SIZE >= xi->xi_value_len))
Tao Ma78f30c32008-11-12 08:27:00 +08003171 goto out;
3172 }
3173
3174meta_guess:
3175 /* calculate metadata allocation. */
3176 if (di->i_xattr_loc) {
3177 if (!xbs->xattr_bh) {
Joel Becker4ae1d692008-11-13 14:49:18 -08003178 ret = ocfs2_read_xattr_block(inode,
3179 le64_to_cpu(di->i_xattr_loc),
3180 &bh);
Tao Ma78f30c32008-11-12 08:27:00 +08003181 if (ret) {
3182 mlog_errno(ret);
3183 goto out;
3184 }
3185
3186 xb = (struct ocfs2_xattr_block *)bh->b_data;
3187 } else
3188 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
3189
Tao Ma90cb5462008-12-05 06:20:56 +08003190 /*
3191 * If there is already an xattr tree, good, we can calculate
3192 * like other b-trees. Otherwise we may have the chance of
3193 * create a tree, the credit calculation is borrowed from
3194 * ocfs2_calc_extend_credits with root_el = NULL. And the
3195 * new tree will be cluster based, so no meta is needed.
3196 */
Tao Ma78f30c32008-11-12 08:27:00 +08003197 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
3198 struct ocfs2_extent_list *el =
3199 &xb->xb_attrs.xb_root.xt_list;
3200 meta_add += ocfs2_extend_meta_needed(el);
Tao Ma85db90e2008-11-12 08:27:01 +08003201 credits += ocfs2_calc_extend_credits(inode->i_sb,
3202 el, 1);
Tao Ma90cb5462008-12-05 06:20:56 +08003203 } else
3204 credits += OCFS2_SUBALLOC_ALLOC + 1;
Tao Ma78f30c32008-11-12 08:27:00 +08003205
3206 /*
3207 * This cluster will be used either for new bucket or for
3208 * new xattr block.
3209 * If the cluster size is the same as the bucket size, one
3210 * more is needed since we may need to extend the bucket
3211 * also.
3212 */
3213 clusters_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08003214 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08003215 if (OCFS2_XATTR_BUCKET_SIZE ==
Tao Ma85db90e2008-11-12 08:27:01 +08003216 OCFS2_SB(inode->i_sb)->s_clustersize) {
3217 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08003218 clusters_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08003219 }
3220 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08003221 meta_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08003222 credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
3223 }
Tao Ma78f30c32008-11-12 08:27:00 +08003224out:
3225 if (clusters_need)
3226 *clusters_need = clusters_add;
3227 if (meta_need)
3228 *meta_need = meta_add;
Tao Ma85db90e2008-11-12 08:27:01 +08003229 if (credits_need)
3230 *credits_need = credits;
Tao Ma78f30c32008-11-12 08:27:00 +08003231 brelse(bh);
3232 return ret;
3233}
3234
3235static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
3236 struct ocfs2_dinode *di,
3237 struct ocfs2_xattr_info *xi,
3238 struct ocfs2_xattr_search *xis,
3239 struct ocfs2_xattr_search *xbs,
Tao Ma85db90e2008-11-12 08:27:01 +08003240 struct ocfs2_xattr_set_ctxt *ctxt,
Tao Ma492a8a32009-08-18 11:43:17 +08003241 int extra_meta,
Tao Ma85db90e2008-11-12 08:27:01 +08003242 int *credits)
Tao Ma78f30c32008-11-12 08:27:00 +08003243{
3244 int clusters_add, meta_add, ret;
3245 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3246
3247 memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
3248
3249 ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
3250
3251 ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
Tao Ma85db90e2008-11-12 08:27:01 +08003252 &clusters_add, &meta_add, credits);
Tao Ma78f30c32008-11-12 08:27:00 +08003253 if (ret) {
3254 mlog_errno(ret);
3255 return ret;
3256 }
3257
Tao Ma492a8a32009-08-18 11:43:17 +08003258 meta_add += extra_meta;
Tao Ma85db90e2008-11-12 08:27:01 +08003259 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
Joel Becker6b240ff2009-08-14 18:02:52 -07003260 "credits = %d\n", xi->xi_name, meta_add, clusters_add, *credits);
Tao Ma78f30c32008-11-12 08:27:00 +08003261
3262 if (meta_add) {
3263 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
3264 &ctxt->meta_ac);
3265 if (ret) {
3266 mlog_errno(ret);
3267 goto out;
3268 }
3269 }
3270
3271 if (clusters_add) {
3272 ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
3273 if (ret)
3274 mlog_errno(ret);
3275 }
3276out:
3277 if (ret) {
3278 if (ctxt->meta_ac) {
3279 ocfs2_free_alloc_context(ctxt->meta_ac);
3280 ctxt->meta_ac = NULL;
3281 }
3282
3283 /*
3284 * We cannot have an error and a non null ctxt->data_ac.
3285 */
3286 }
3287
3288 return ret;
3289}
3290
Tao Ma85db90e2008-11-12 08:27:01 +08003291static int __ocfs2_xattr_set_handle(struct inode *inode,
3292 struct ocfs2_dinode *di,
3293 struct ocfs2_xattr_info *xi,
3294 struct ocfs2_xattr_search *xis,
3295 struct ocfs2_xattr_search *xbs,
3296 struct ocfs2_xattr_set_ctxt *ctxt)
3297{
Tao Ma9f868f12008-11-19 16:48:42 +08003298 int ret = 0, credits, old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08003299
Joel Becker6b240ff2009-08-14 18:02:52 -07003300 if (!xi->xi_value) {
Tao Ma85db90e2008-11-12 08:27:01 +08003301 /* Remove existing extended attribute */
3302 if (!xis->not_found)
3303 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3304 else if (!xbs->not_found)
3305 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3306 } else {
3307 /* We always try to set extended attribute into inode first*/
3308 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3309 if (!ret && !xbs->not_found) {
3310 /*
3311 * If succeed and that extended attribute existing in
3312 * external block, then we will remove it.
3313 */
Joel Becker6b240ff2009-08-14 18:02:52 -07003314 xi->xi_value = NULL;
3315 xi->xi_value_len = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08003316
Tao Ma9f868f12008-11-19 16:48:42 +08003317 old_found = xis->not_found;
Tao Ma85db90e2008-11-12 08:27:01 +08003318 xis->not_found = -ENODATA;
3319 ret = ocfs2_calc_xattr_set_need(inode,
3320 di,
3321 xi,
3322 xis,
3323 xbs,
3324 NULL,
3325 NULL,
3326 &credits);
Tao Ma9f868f12008-11-19 16:48:42 +08003327 xis->not_found = old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08003328 if (ret) {
3329 mlog_errno(ret);
3330 goto out;
3331 }
3332
3333 ret = ocfs2_extend_trans(ctxt->handle, credits +
3334 ctxt->handle->h_buffer_credits);
3335 if (ret) {
3336 mlog_errno(ret);
3337 goto out;
3338 }
3339 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3340 } else if (ret == -ENOSPC) {
3341 if (di->i_xattr_loc && !xbs->xattr_bh) {
3342 ret = ocfs2_xattr_block_find(inode,
Joel Becker6b240ff2009-08-14 18:02:52 -07003343 xi->xi_name_index,
3344 xi->xi_name, xbs);
Tao Ma85db90e2008-11-12 08:27:01 +08003345 if (ret)
3346 goto out;
3347
Tao Ma9f868f12008-11-19 16:48:42 +08003348 old_found = xis->not_found;
Tao Ma85db90e2008-11-12 08:27:01 +08003349 xis->not_found = -ENODATA;
3350 ret = ocfs2_calc_xattr_set_need(inode,
3351 di,
3352 xi,
3353 xis,
3354 xbs,
3355 NULL,
3356 NULL,
3357 &credits);
Tao Ma9f868f12008-11-19 16:48:42 +08003358 xis->not_found = old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08003359 if (ret) {
3360 mlog_errno(ret);
3361 goto out;
3362 }
3363
3364 ret = ocfs2_extend_trans(ctxt->handle, credits +
3365 ctxt->handle->h_buffer_credits);
3366 if (ret) {
3367 mlog_errno(ret);
3368 goto out;
3369 }
3370 }
3371 /*
3372 * If no space in inode, we will set extended attribute
3373 * into external block.
3374 */
3375 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3376 if (ret)
3377 goto out;
3378 if (!xis->not_found) {
3379 /*
3380 * If succeed and that extended attribute
3381 * existing in inode, we will remove it.
3382 */
Joel Becker6b240ff2009-08-14 18:02:52 -07003383 xi->xi_value = NULL;
3384 xi->xi_value_len = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08003385 xbs->not_found = -ENODATA;
3386 ret = ocfs2_calc_xattr_set_need(inode,
3387 di,
3388 xi,
3389 xis,
3390 xbs,
3391 NULL,
3392 NULL,
3393 &credits);
3394 if (ret) {
3395 mlog_errno(ret);
3396 goto out;
3397 }
3398
3399 ret = ocfs2_extend_trans(ctxt->handle, credits +
3400 ctxt->handle->h_buffer_credits);
3401 if (ret) {
3402 mlog_errno(ret);
3403 goto out;
3404 }
3405 ret = ocfs2_xattr_ibody_set(inode, xi,
3406 xis, ctxt);
3407 }
3408 }
3409 }
3410
Tao Ma4b3f6202008-12-05 06:20:55 +08003411 if (!ret) {
3412 /* Update inode ctime. */
Joel Becker0cf2f762009-02-12 16:41:25 -08003413 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
Tao Ma89a907a2009-02-17 04:39:28 +08003414 xis->inode_bh,
3415 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma4b3f6202008-12-05 06:20:55 +08003416 if (ret) {
3417 mlog_errno(ret);
3418 goto out;
3419 }
3420
3421 inode->i_ctime = CURRENT_TIME;
3422 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
3423 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
3424 ocfs2_journal_dirty(ctxt->handle, xis->inode_bh);
3425 }
Tao Ma85db90e2008-11-12 08:27:01 +08003426out:
3427 return ret;
3428}
3429
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003430/*
Tiger Yang6c3faba2008-11-14 11:16:03 +08003431 * This function only called duing creating inode
3432 * for init security/acl xattrs of the new inode.
Tiger Yang008aafa2008-12-09 16:43:08 +08003433 * All transanction credits have been reserved in mknod.
Tiger Yang6c3faba2008-11-14 11:16:03 +08003434 */
3435int ocfs2_xattr_set_handle(handle_t *handle,
3436 struct inode *inode,
3437 struct buffer_head *di_bh,
3438 int name_index,
3439 const char *name,
3440 const void *value,
3441 size_t value_len,
3442 int flags,
3443 struct ocfs2_alloc_context *meta_ac,
3444 struct ocfs2_alloc_context *data_ac)
3445{
3446 struct ocfs2_dinode *di;
3447 int ret;
3448
3449 struct ocfs2_xattr_info xi = {
Joel Becker6b240ff2009-08-14 18:02:52 -07003450 .xi_name_index = name_index,
3451 .xi_name = name,
Joel Becker18853b92009-08-14 18:17:07 -07003452 .xi_name_len = strlen(name),
Joel Becker6b240ff2009-08-14 18:02:52 -07003453 .xi_value = value,
3454 .xi_value_len = value_len,
Tiger Yang6c3faba2008-11-14 11:16:03 +08003455 };
3456
3457 struct ocfs2_xattr_search xis = {
3458 .not_found = -ENODATA,
3459 };
3460
3461 struct ocfs2_xattr_search xbs = {
3462 .not_found = -ENODATA,
3463 };
3464
3465 struct ocfs2_xattr_set_ctxt ctxt = {
3466 .handle = handle,
3467 .meta_ac = meta_ac,
3468 .data_ac = data_ac,
3469 };
3470
3471 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3472 return -EOPNOTSUPP;
3473
Tiger Yang008aafa2008-12-09 16:43:08 +08003474 /*
3475 * In extreme situation, may need xattr bucket when
3476 * block size is too small. And we have already reserved
3477 * the credits for bucket in mknod.
3478 */
3479 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) {
3480 xbs.bucket = ocfs2_xattr_bucket_new(inode);
3481 if (!xbs.bucket) {
3482 mlog_errno(-ENOMEM);
3483 return -ENOMEM;
3484 }
3485 }
3486
Tiger Yang6c3faba2008-11-14 11:16:03 +08003487 xis.inode_bh = xbs.inode_bh = di_bh;
3488 di = (struct ocfs2_dinode *)di_bh->b_data;
3489
3490 down_write(&OCFS2_I(inode)->ip_xattr_sem);
3491
3492 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3493 if (ret)
3494 goto cleanup;
3495 if (xis.not_found) {
3496 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3497 if (ret)
3498 goto cleanup;
3499 }
3500
3501 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3502
3503cleanup:
3504 up_write(&OCFS2_I(inode)->ip_xattr_sem);
3505 brelse(xbs.xattr_bh);
Tiger Yang008aafa2008-12-09 16:43:08 +08003506 ocfs2_xattr_bucket_free(xbs.bucket);
Tiger Yang6c3faba2008-11-14 11:16:03 +08003507
3508 return ret;
3509}
3510
3511/*
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003512 * ocfs2_xattr_set()
3513 *
3514 * Set, replace or remove an extended attribute for this inode.
3515 * value is NULL to remove an existing extended attribute, else either
3516 * create or replace an extended attribute.
3517 */
3518int ocfs2_xattr_set(struct inode *inode,
3519 int name_index,
3520 const char *name,
3521 const void *value,
3522 size_t value_len,
3523 int flags)
3524{
3525 struct buffer_head *di_bh = NULL;
3526 struct ocfs2_dinode *di;
Tao Ma492a8a32009-08-18 11:43:17 +08003527 int ret, credits, ref_meta = 0, ref_credits = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08003528 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08003529 struct inode *tl_inode = osb->osb_tl_inode;
Tao Ma78f30c32008-11-12 08:27:00 +08003530 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
Tao Ma492a8a32009-08-18 11:43:17 +08003531 struct ocfs2_refcount_tree *ref_tree = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003532
3533 struct ocfs2_xattr_info xi = {
Joel Becker6b240ff2009-08-14 18:02:52 -07003534 .xi_name_index = name_index,
3535 .xi_name = name,
Joel Becker18853b92009-08-14 18:17:07 -07003536 .xi_name_len = strlen(name),
Joel Becker6b240ff2009-08-14 18:02:52 -07003537 .xi_value = value,
3538 .xi_value_len = value_len,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003539 };
3540
3541 struct ocfs2_xattr_search xis = {
3542 .not_found = -ENODATA,
3543 };
3544
3545 struct ocfs2_xattr_search xbs = {
3546 .not_found = -ENODATA,
3547 };
3548
Tiger Yang8154da32008-08-18 17:11:46 +08003549 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3550 return -EOPNOTSUPP;
3551
Joel Beckerba937122008-10-24 19:13:20 -07003552 /*
3553 * Only xbs will be used on indexed trees. xis doesn't need a
3554 * bucket.
3555 */
3556 xbs.bucket = ocfs2_xattr_bucket_new(inode);
3557 if (!xbs.bucket) {
3558 mlog_errno(-ENOMEM);
3559 return -ENOMEM;
3560 }
3561
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003562 ret = ocfs2_inode_lock(inode, &di_bh, 1);
3563 if (ret < 0) {
3564 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07003565 goto cleanup_nolock;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003566 }
3567 xis.inode_bh = xbs.inode_bh = di_bh;
3568 di = (struct ocfs2_dinode *)di_bh->b_data;
3569
3570 down_write(&OCFS2_I(inode)->ip_xattr_sem);
3571 /*
3572 * Scan inode and external block to find the same name
3573 * extended attribute and collect search infomation.
3574 */
3575 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3576 if (ret)
3577 goto cleanup;
3578 if (xis.not_found) {
3579 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3580 if (ret)
3581 goto cleanup;
3582 }
3583
3584 if (xis.not_found && xbs.not_found) {
3585 ret = -ENODATA;
3586 if (flags & XATTR_REPLACE)
3587 goto cleanup;
3588 ret = 0;
3589 if (!value)
3590 goto cleanup;
3591 } else {
3592 ret = -EEXIST;
3593 if (flags & XATTR_CREATE)
3594 goto cleanup;
3595 }
3596
Tao Ma492a8a32009-08-18 11:43:17 +08003597 /* Check whether the value is refcounted and do some prepartion. */
3598 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL &&
3599 (!xis.not_found || !xbs.not_found)) {
3600 ret = ocfs2_prepare_refcount_xattr(inode, di, &xi,
3601 &xis, &xbs, &ref_tree,
3602 &ref_meta, &ref_credits);
3603 if (ret) {
3604 mlog_errno(ret);
3605 goto cleanup;
3606 }
3607 }
Tao Ma85db90e2008-11-12 08:27:01 +08003608
3609 mutex_lock(&tl_inode->i_mutex);
3610
3611 if (ocfs2_truncate_log_needs_flush(osb)) {
3612 ret = __ocfs2_flush_truncate_log(osb);
3613 if (ret < 0) {
3614 mutex_unlock(&tl_inode->i_mutex);
3615 mlog_errno(ret);
3616 goto cleanup;
3617 }
3618 }
3619 mutex_unlock(&tl_inode->i_mutex);
3620
3621 ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
Tao Ma492a8a32009-08-18 11:43:17 +08003622 &xbs, &ctxt, ref_meta, &credits);
Tao Ma78f30c32008-11-12 08:27:00 +08003623 if (ret) {
3624 mlog_errno(ret);
3625 goto cleanup;
3626 }
3627
Tao Ma4b3f6202008-12-05 06:20:55 +08003628 /* we need to update inode's ctime field, so add credit for it. */
3629 credits += OCFS2_INODE_UPDATE_CREDITS;
Tao Ma492a8a32009-08-18 11:43:17 +08003630 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
Tao Ma85db90e2008-11-12 08:27:01 +08003631 if (IS_ERR(ctxt.handle)) {
3632 ret = PTR_ERR(ctxt.handle);
3633 mlog_errno(ret);
3634 goto cleanup;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003635 }
Tao Ma85db90e2008-11-12 08:27:01 +08003636
3637 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3638
3639 ocfs2_commit_trans(osb, ctxt.handle);
3640
Tao Ma78f30c32008-11-12 08:27:00 +08003641 if (ctxt.data_ac)
3642 ocfs2_free_alloc_context(ctxt.data_ac);
3643 if (ctxt.meta_ac)
3644 ocfs2_free_alloc_context(ctxt.meta_ac);
3645 if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
3646 ocfs2_schedule_truncate_log_flush(osb, 1);
3647 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tao Ma8b2c0db2009-08-18 11:43:49 +08003648
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003649cleanup:
Tao Ma492a8a32009-08-18 11:43:17 +08003650 if (ref_tree)
3651 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003652 up_write(&OCFS2_I(inode)->ip_xattr_sem);
Tao Ma8b2c0db2009-08-18 11:43:49 +08003653 if (!value && !ret) {
3654 ret = ocfs2_try_remove_refcount_tree(inode, di_bh);
3655 if (ret)
3656 mlog_errno(ret);
3657 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003658 ocfs2_inode_unlock(inode, 1);
Joel Beckerba937122008-10-24 19:13:20 -07003659cleanup_nolock:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003660 brelse(di_bh);
3661 brelse(xbs.xattr_bh);
Joel Beckerba937122008-10-24 19:13:20 -07003662 ocfs2_xattr_bucket_free(xbs.bucket);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003663
3664 return ret;
3665}
3666
Tao Ma0c044f02008-08-18 17:38:50 +08003667/*
3668 * Find the xattr extent rec which may contains name_hash.
3669 * e_cpos will be the first name hash of the xattr rec.
3670 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
3671 */
3672static int ocfs2_xattr_get_rec(struct inode *inode,
3673 u32 name_hash,
3674 u64 *p_blkno,
3675 u32 *e_cpos,
3676 u32 *num_clusters,
3677 struct ocfs2_extent_list *el)
3678{
3679 int ret = 0, i;
3680 struct buffer_head *eb_bh = NULL;
3681 struct ocfs2_extent_block *eb;
3682 struct ocfs2_extent_rec *rec = NULL;
3683 u64 e_blkno = 0;
3684
3685 if (el->l_tree_depth) {
Joel Beckerfacdb772009-02-12 18:08:48 -08003686 ret = ocfs2_find_leaf(INODE_CACHE(inode), el, name_hash,
3687 &eb_bh);
Tao Ma0c044f02008-08-18 17:38:50 +08003688 if (ret) {
3689 mlog_errno(ret);
3690 goto out;
3691 }
3692
3693 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
3694 el = &eb->h_list;
3695
3696 if (el->l_tree_depth) {
3697 ocfs2_error(inode->i_sb,
3698 "Inode %lu has non zero tree depth in "
3699 "xattr tree block %llu\n", inode->i_ino,
3700 (unsigned long long)eb_bh->b_blocknr);
3701 ret = -EROFS;
3702 goto out;
3703 }
3704 }
3705
3706 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
3707 rec = &el->l_recs[i];
3708
3709 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
3710 e_blkno = le64_to_cpu(rec->e_blkno);
3711 break;
3712 }
3713 }
3714
3715 if (!e_blkno) {
3716 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
3717 "record (%u, %u, 0) in xattr", inode->i_ino,
3718 le32_to_cpu(rec->e_cpos),
3719 ocfs2_rec_clusters(el, rec));
3720 ret = -EROFS;
3721 goto out;
3722 }
3723
3724 *p_blkno = le64_to_cpu(rec->e_blkno);
3725 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
3726 if (e_cpos)
3727 *e_cpos = le32_to_cpu(rec->e_cpos);
3728out:
3729 brelse(eb_bh);
3730 return ret;
3731}
3732
3733typedef int (xattr_bucket_func)(struct inode *inode,
3734 struct ocfs2_xattr_bucket *bucket,
3735 void *para);
3736
Tao Ma589dc262008-08-18 17:38:51 +08003737static int ocfs2_find_xe_in_bucket(struct inode *inode,
Joel Beckere2356a32008-10-27 15:01:54 -07003738 struct ocfs2_xattr_bucket *bucket,
Tao Ma589dc262008-08-18 17:38:51 +08003739 int name_index,
3740 const char *name,
3741 u32 name_hash,
3742 u16 *xe_index,
3743 int *found)
3744{
3745 int i, ret = 0, cmp = 1, block_off, new_offset;
Joel Beckere2356a32008-10-27 15:01:54 -07003746 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma589dc262008-08-18 17:38:51 +08003747 size_t name_len = strlen(name);
3748 struct ocfs2_xattr_entry *xe = NULL;
Tao Ma589dc262008-08-18 17:38:51 +08003749 char *xe_name;
3750
3751 /*
3752 * We don't use binary search in the bucket because there
3753 * may be multiple entries with the same name hash.
3754 */
3755 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3756 xe = &xh->xh_entries[i];
3757
3758 if (name_hash > le32_to_cpu(xe->xe_name_hash))
3759 continue;
3760 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
3761 break;
3762
3763 cmp = name_index - ocfs2_xattr_get_type(xe);
3764 if (!cmp)
3765 cmp = name_len - xe->xe_name_len;
3766 if (cmp)
3767 continue;
3768
Tao Mafd68a892009-08-18 11:43:21 +08003769 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Tao Ma589dc262008-08-18 17:38:51 +08003770 xh,
3771 i,
3772 &block_off,
3773 &new_offset);
3774 if (ret) {
3775 mlog_errno(ret);
3776 break;
3777 }
3778
Joel Becker970e4932008-11-13 14:49:19 -08003779
Joel Beckere2356a32008-10-27 15:01:54 -07003780 xe_name = bucket_block(bucket, block_off) + new_offset;
3781 if (!memcmp(name, xe_name, name_len)) {
Tao Ma589dc262008-08-18 17:38:51 +08003782 *xe_index = i;
3783 *found = 1;
3784 ret = 0;
3785 break;
3786 }
3787 }
3788
3789 return ret;
3790}
3791
3792/*
3793 * Find the specified xattr entry in a series of buckets.
3794 * This series start from p_blkno and last for num_clusters.
3795 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
3796 * the num of the valid buckets.
3797 *
3798 * Return the buffer_head this xattr should reside in. And if the xattr's
3799 * hash is in the gap of 2 buckets, return the lower bucket.
3800 */
3801static int ocfs2_xattr_bucket_find(struct inode *inode,
3802 int name_index,
3803 const char *name,
3804 u32 name_hash,
3805 u64 p_blkno,
3806 u32 first_hash,
3807 u32 num_clusters,
3808 struct ocfs2_xattr_search *xs)
3809{
3810 int ret, found = 0;
Tao Ma589dc262008-08-18 17:38:51 +08003811 struct ocfs2_xattr_header *xh = NULL;
3812 struct ocfs2_xattr_entry *xe = NULL;
3813 u16 index = 0;
3814 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3815 int low_bucket = 0, bucket, high_bucket;
Joel Beckere2356a32008-10-27 15:01:54 -07003816 struct ocfs2_xattr_bucket *search;
Tao Ma589dc262008-08-18 17:38:51 +08003817 u32 last_hash;
Joel Beckere2356a32008-10-27 15:01:54 -07003818 u64 blkno, lower_blkno = 0;
Tao Ma589dc262008-08-18 17:38:51 +08003819
Joel Beckere2356a32008-10-27 15:01:54 -07003820 search = ocfs2_xattr_bucket_new(inode);
3821 if (!search) {
3822 ret = -ENOMEM;
3823 mlog_errno(ret);
3824 goto out;
3825 }
3826
3827 ret = ocfs2_read_xattr_bucket(search, p_blkno);
Tao Ma589dc262008-08-18 17:38:51 +08003828 if (ret) {
3829 mlog_errno(ret);
3830 goto out;
3831 }
3832
Joel Beckere2356a32008-10-27 15:01:54 -07003833 xh = bucket_xh(search);
Tao Ma589dc262008-08-18 17:38:51 +08003834 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
Tao Ma589dc262008-08-18 17:38:51 +08003835 while (low_bucket <= high_bucket) {
Joel Beckere2356a32008-10-27 15:01:54 -07003836 ocfs2_xattr_bucket_relse(search);
3837
Tao Ma589dc262008-08-18 17:38:51 +08003838 bucket = (low_bucket + high_bucket) / 2;
Tao Ma589dc262008-08-18 17:38:51 +08003839 blkno = p_blkno + bucket * blk_per_bucket;
Joel Beckere2356a32008-10-27 15:01:54 -07003840 ret = ocfs2_read_xattr_bucket(search, blkno);
Tao Ma589dc262008-08-18 17:38:51 +08003841 if (ret) {
3842 mlog_errno(ret);
3843 goto out;
3844 }
3845
Joel Beckere2356a32008-10-27 15:01:54 -07003846 xh = bucket_xh(search);
Tao Ma589dc262008-08-18 17:38:51 +08003847 xe = &xh->xh_entries[0];
3848 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
3849 high_bucket = bucket - 1;
3850 continue;
3851 }
3852
3853 /*
3854 * Check whether the hash of the last entry in our
Tao Ma5a095612008-09-19 22:17:41 +08003855 * bucket is larger than the search one. for an empty
3856 * bucket, the last one is also the first one.
Tao Ma589dc262008-08-18 17:38:51 +08003857 */
Tao Ma5a095612008-09-19 22:17:41 +08003858 if (xh->xh_count)
3859 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
3860
Tao Ma589dc262008-08-18 17:38:51 +08003861 last_hash = le32_to_cpu(xe->xe_name_hash);
3862
Joel Beckere2356a32008-10-27 15:01:54 -07003863 /* record lower_blkno which may be the insert place. */
3864 lower_blkno = blkno;
Tao Ma589dc262008-08-18 17:38:51 +08003865
3866 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
3867 low_bucket = bucket + 1;
3868 continue;
3869 }
3870
3871 /* the searched xattr should reside in this bucket if exists. */
Joel Beckere2356a32008-10-27 15:01:54 -07003872 ret = ocfs2_find_xe_in_bucket(inode, search,
Tao Ma589dc262008-08-18 17:38:51 +08003873 name_index, name, name_hash,
3874 &index, &found);
3875 if (ret) {
3876 mlog_errno(ret);
3877 goto out;
3878 }
3879 break;
3880 }
3881
3882 /*
3883 * Record the bucket we have found.
3884 * When the xattr's hash value is in the gap of 2 buckets, we will
3885 * always set it to the previous bucket.
3886 */
Joel Beckere2356a32008-10-27 15:01:54 -07003887 if (!lower_blkno)
3888 lower_blkno = p_blkno;
3889
3890 /* This should be in cache - we just read it during the search */
3891 ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
3892 if (ret) {
3893 mlog_errno(ret);
3894 goto out;
Tao Ma589dc262008-08-18 17:38:51 +08003895 }
Tao Ma589dc262008-08-18 17:38:51 +08003896
Joel Beckerba937122008-10-24 19:13:20 -07003897 xs->header = bucket_xh(xs->bucket);
3898 xs->base = bucket_block(xs->bucket, 0);
Tao Ma589dc262008-08-18 17:38:51 +08003899 xs->end = xs->base + inode->i_sb->s_blocksize;
3900
3901 if (found) {
Tao Ma589dc262008-08-18 17:38:51 +08003902 xs->here = &xs->header->xh_entries[index];
3903 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
Joel Beckerba937122008-10-24 19:13:20 -07003904 (unsigned long long)bucket_blkno(xs->bucket), index);
Tao Ma589dc262008-08-18 17:38:51 +08003905 } else
3906 ret = -ENODATA;
3907
3908out:
Joel Beckere2356a32008-10-27 15:01:54 -07003909 ocfs2_xattr_bucket_free(search);
Tao Ma589dc262008-08-18 17:38:51 +08003910 return ret;
3911}
3912
3913static int ocfs2_xattr_index_block_find(struct inode *inode,
3914 struct buffer_head *root_bh,
3915 int name_index,
3916 const char *name,
3917 struct ocfs2_xattr_search *xs)
3918{
3919 int ret;
3920 struct ocfs2_xattr_block *xb =
3921 (struct ocfs2_xattr_block *)root_bh->b_data;
3922 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3923 struct ocfs2_extent_list *el = &xb_root->xt_list;
3924 u64 p_blkno = 0;
3925 u32 first_hash, num_clusters = 0;
Tao Ma2057e5c2008-10-09 23:06:13 +08003926 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
Tao Ma589dc262008-08-18 17:38:51 +08003927
3928 if (le16_to_cpu(el->l_next_free_rec) == 0)
3929 return -ENODATA;
3930
3931 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
3932 name, name_hash, name_index);
3933
3934 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
3935 &num_clusters, el);
3936 if (ret) {
3937 mlog_errno(ret);
3938 goto out;
3939 }
3940
3941 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
3942
3943 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
Mark Fashehde29c082008-10-29 14:45:30 -07003944 "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
3945 first_hash);
Tao Ma589dc262008-08-18 17:38:51 +08003946
3947 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
3948 p_blkno, first_hash, num_clusters, xs);
3949
3950out:
3951 return ret;
3952}
3953
Tao Ma0c044f02008-08-18 17:38:50 +08003954static int ocfs2_iterate_xattr_buckets(struct inode *inode,
3955 u64 blkno,
3956 u32 clusters,
3957 xattr_bucket_func *func,
3958 void *para)
3959{
Joel Becker6dde41d2008-10-24 17:16:48 -07003960 int i, ret = 0;
Tao Ma0c044f02008-08-18 17:38:50 +08003961 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
3962 u32 num_buckets = clusters * bpc;
Joel Beckerba937122008-10-24 19:13:20 -07003963 struct ocfs2_xattr_bucket *bucket;
Tao Ma0c044f02008-08-18 17:38:50 +08003964
Joel Beckerba937122008-10-24 19:13:20 -07003965 bucket = ocfs2_xattr_bucket_new(inode);
3966 if (!bucket) {
3967 mlog_errno(-ENOMEM);
3968 return -ENOMEM;
3969 }
Tao Ma0c044f02008-08-18 17:38:50 +08003970
3971 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003972 clusters, (unsigned long long)blkno);
Tao Ma0c044f02008-08-18 17:38:50 +08003973
Joel Beckerba937122008-10-24 19:13:20 -07003974 for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
3975 ret = ocfs2_read_xattr_bucket(bucket, blkno);
Tao Ma0c044f02008-08-18 17:38:50 +08003976 if (ret) {
3977 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07003978 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003979 }
3980
Tao Ma0c044f02008-08-18 17:38:50 +08003981 /*
3982 * The real bucket num in this series of blocks is stored
3983 * in the 1st bucket.
3984 */
3985 if (i == 0)
Joel Beckerba937122008-10-24 19:13:20 -07003986 num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
Tao Ma0c044f02008-08-18 17:38:50 +08003987
Mark Fashehde29c082008-10-29 14:45:30 -07003988 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
3989 (unsigned long long)blkno,
Joel Beckerba937122008-10-24 19:13:20 -07003990 le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
Tao Ma0c044f02008-08-18 17:38:50 +08003991 if (func) {
Joel Beckerba937122008-10-24 19:13:20 -07003992 ret = func(inode, bucket, para);
Tao Maa46fa682009-05-04 05:18:09 +08003993 if (ret && ret != -ERANGE)
Tao Ma0c044f02008-08-18 17:38:50 +08003994 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07003995 /* Fall through to bucket_relse() */
Tao Ma0c044f02008-08-18 17:38:50 +08003996 }
3997
Joel Beckerba937122008-10-24 19:13:20 -07003998 ocfs2_xattr_bucket_relse(bucket);
3999 if (ret)
4000 break;
Tao Ma0c044f02008-08-18 17:38:50 +08004001 }
4002
Joel Beckerba937122008-10-24 19:13:20 -07004003 ocfs2_xattr_bucket_free(bucket);
Tao Ma0c044f02008-08-18 17:38:50 +08004004 return ret;
4005}
4006
4007struct ocfs2_xattr_tree_list {
4008 char *buffer;
4009 size_t buffer_size;
Tao Ma936b8832008-10-09 23:06:14 +08004010 size_t result;
Tao Ma0c044f02008-08-18 17:38:50 +08004011};
4012
Tao Mafd68a892009-08-18 11:43:21 +08004013static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
Tao Ma0c044f02008-08-18 17:38:50 +08004014 struct ocfs2_xattr_header *xh,
4015 int index,
4016 int *block_off,
4017 int *new_offset)
4018{
4019 u16 name_offset;
4020
4021 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
4022 return -EINVAL;
4023
4024 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
4025
Tao Mafd68a892009-08-18 11:43:21 +08004026 *block_off = name_offset >> sb->s_blocksize_bits;
4027 *new_offset = name_offset % sb->s_blocksize;
Tao Ma0c044f02008-08-18 17:38:50 +08004028
4029 return 0;
4030}
4031
4032static int ocfs2_list_xattr_bucket(struct inode *inode,
4033 struct ocfs2_xattr_bucket *bucket,
4034 void *para)
4035{
Tao Ma936b8832008-10-09 23:06:14 +08004036 int ret = 0, type;
Tao Ma0c044f02008-08-18 17:38:50 +08004037 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
Tao Ma0c044f02008-08-18 17:38:50 +08004038 int i, block_off, new_offset;
Tao Ma936b8832008-10-09 23:06:14 +08004039 const char *prefix, *name;
Tao Ma0c044f02008-08-18 17:38:50 +08004040
Joel Becker3e632942008-10-24 17:04:49 -07004041 for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
4042 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
Tao Ma936b8832008-10-09 23:06:14 +08004043 type = ocfs2_xattr_get_type(entry);
4044 prefix = ocfs2_xattr_prefix(type);
Tao Ma0c044f02008-08-18 17:38:50 +08004045
Tao Ma936b8832008-10-09 23:06:14 +08004046 if (prefix) {
Tao Mafd68a892009-08-18 11:43:21 +08004047 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Joel Becker3e632942008-10-24 17:04:49 -07004048 bucket_xh(bucket),
Tao Ma0c044f02008-08-18 17:38:50 +08004049 i,
4050 &block_off,
4051 &new_offset);
4052 if (ret)
4053 break;
Tao Ma936b8832008-10-09 23:06:14 +08004054
Joel Becker51def392008-10-24 16:57:21 -07004055 name = (const char *)bucket_block(bucket, block_off) +
Tao Ma936b8832008-10-09 23:06:14 +08004056 new_offset;
4057 ret = ocfs2_xattr_list_entry(xl->buffer,
4058 xl->buffer_size,
4059 &xl->result,
4060 prefix, name,
4061 entry->xe_name_len);
4062 if (ret)
4063 break;
Tao Ma0c044f02008-08-18 17:38:50 +08004064 }
4065 }
4066
4067 return ret;
4068}
4069
Tao Ma47bca492009-08-18 11:43:42 +08004070static int ocfs2_iterate_xattr_index_block(struct inode *inode,
4071 struct buffer_head *blk_bh,
4072 xattr_tree_rec_func *rec_func,
4073 void *para)
Tao Ma0c044f02008-08-18 17:38:50 +08004074{
Tao Ma47bca492009-08-18 11:43:42 +08004075 struct ocfs2_xattr_block *xb =
4076 (struct ocfs2_xattr_block *)blk_bh->b_data;
4077 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
Tao Ma0c044f02008-08-18 17:38:50 +08004078 int ret = 0;
4079 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
4080 u64 p_blkno = 0;
Tao Ma0c044f02008-08-18 17:38:50 +08004081
Tao Ma47bca492009-08-18 11:43:42 +08004082 if (!el->l_next_free_rec || !rec_func)
Tao Ma0c044f02008-08-18 17:38:50 +08004083 return 0;
4084
4085 while (name_hash > 0) {
4086 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
4087 &e_cpos, &num_clusters, el);
4088 if (ret) {
4089 mlog_errno(ret);
Tao Ma47bca492009-08-18 11:43:42 +08004090 break;
Tao Ma0c044f02008-08-18 17:38:50 +08004091 }
4092
Tao Ma47bca492009-08-18 11:43:42 +08004093 ret = rec_func(inode, blk_bh, p_blkno, e_cpos,
4094 num_clusters, para);
Tao Ma0c044f02008-08-18 17:38:50 +08004095 if (ret) {
Tao Maa46fa682009-05-04 05:18:09 +08004096 if (ret != -ERANGE)
4097 mlog_errno(ret);
Tao Ma47bca492009-08-18 11:43:42 +08004098 break;
Tao Ma0c044f02008-08-18 17:38:50 +08004099 }
4100
4101 if (e_cpos == 0)
4102 break;
4103
4104 name_hash = e_cpos - 1;
4105 }
4106
Tao Ma47bca492009-08-18 11:43:42 +08004107 return ret;
4108
4109}
4110
4111static int ocfs2_list_xattr_tree_rec(struct inode *inode,
4112 struct buffer_head *root_bh,
4113 u64 blkno, u32 cpos, u32 len, void *para)
4114{
4115 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
4116 ocfs2_list_xattr_bucket, para);
4117}
4118
4119static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
4120 struct buffer_head *blk_bh,
4121 char *buffer,
4122 size_t buffer_size)
4123{
4124 int ret;
4125 struct ocfs2_xattr_tree_list xl = {
4126 .buffer = buffer,
4127 .buffer_size = buffer_size,
4128 .result = 0,
4129 };
4130
4131 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
4132 ocfs2_list_xattr_tree_rec, &xl);
4133 if (ret) {
4134 mlog_errno(ret);
4135 goto out;
4136 }
4137
Tao Ma936b8832008-10-09 23:06:14 +08004138 ret = xl.result;
Tao Ma0c044f02008-08-18 17:38:50 +08004139out:
4140 return ret;
4141}
Tao Ma01225592008-08-18 17:38:53 +08004142
4143static int cmp_xe(const void *a, const void *b)
4144{
4145 const struct ocfs2_xattr_entry *l = a, *r = b;
4146 u32 l_hash = le32_to_cpu(l->xe_name_hash);
4147 u32 r_hash = le32_to_cpu(r->xe_name_hash);
4148
4149 if (l_hash > r_hash)
4150 return 1;
4151 if (l_hash < r_hash)
4152 return -1;
4153 return 0;
4154}
4155
4156static void swap_xe(void *a, void *b, int size)
4157{
4158 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
4159
4160 tmp = *l;
4161 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
4162 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
4163}
4164
4165/*
4166 * When the ocfs2_xattr_block is filled up, new bucket will be created
4167 * and all the xattr entries will be moved to the new bucket.
Joel Becker178eeac2008-10-27 15:18:29 -07004168 * The header goes at the start of the bucket, and the names+values are
4169 * filled from the end. This is why *target starts as the last buffer.
Tao Ma01225592008-08-18 17:38:53 +08004170 * Note: we need to sort the entries since they are not saved in order
4171 * in the ocfs2_xattr_block.
4172 */
4173static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
4174 struct buffer_head *xb_bh,
Joel Becker178eeac2008-10-27 15:18:29 -07004175 struct ocfs2_xattr_bucket *bucket)
Tao Ma01225592008-08-18 17:38:53 +08004176{
4177 int i, blocksize = inode->i_sb->s_blocksize;
Joel Becker178eeac2008-10-27 15:18:29 -07004178 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08004179 u16 offset, size, off_change;
4180 struct ocfs2_xattr_entry *xe;
4181 struct ocfs2_xattr_block *xb =
4182 (struct ocfs2_xattr_block *)xb_bh->b_data;
4183 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
Joel Becker178eeac2008-10-27 15:18:29 -07004184 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma01225592008-08-18 17:38:53 +08004185 u16 count = le16_to_cpu(xb_xh->xh_count);
Joel Becker178eeac2008-10-27 15:18:29 -07004186 char *src = xb_bh->b_data;
4187 char *target = bucket_block(bucket, blks - 1);
Tao Ma01225592008-08-18 17:38:53 +08004188
4189 mlog(0, "cp xattr from block %llu to bucket %llu\n",
4190 (unsigned long long)xb_bh->b_blocknr,
Joel Becker178eeac2008-10-27 15:18:29 -07004191 (unsigned long long)bucket_blkno(bucket));
Tao Ma01225592008-08-18 17:38:53 +08004192
Joel Becker178eeac2008-10-27 15:18:29 -07004193 for (i = 0; i < blks; i++)
4194 memset(bucket_block(bucket, i), 0, blocksize);
4195
Tao Ma01225592008-08-18 17:38:53 +08004196 /*
4197 * Since the xe_name_offset is based on ocfs2_xattr_header,
4198 * there is a offset change corresponding to the change of
4199 * ocfs2_xattr_header's position.
4200 */
4201 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
4202 xe = &xb_xh->xh_entries[count - 1];
4203 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
4204 size = blocksize - offset;
4205
4206 /* copy all the names and values. */
Tao Ma01225592008-08-18 17:38:53 +08004207 memcpy(target + offset, src + offset, size);
4208
4209 /* Init new header now. */
4210 xh->xh_count = xb_xh->xh_count;
4211 xh->xh_num_buckets = cpu_to_le16(1);
4212 xh->xh_name_value_len = cpu_to_le16(size);
4213 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
4214
4215 /* copy all the entries. */
Joel Becker178eeac2008-10-27 15:18:29 -07004216 target = bucket_block(bucket, 0);
Tao Ma01225592008-08-18 17:38:53 +08004217 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
4218 size = count * sizeof(struct ocfs2_xattr_entry);
4219 memcpy(target + offset, (char *)xb_xh + offset, size);
4220
4221 /* Change the xe offset for all the xe because of the move. */
4222 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
4223 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
4224 for (i = 0; i < count; i++)
4225 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
4226
4227 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
4228 offset, size, off_change);
4229
4230 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
4231 cmp_xe, swap_xe);
4232}
4233
4234/*
4235 * After we move xattr from block to index btree, we have to
4236 * update ocfs2_xattr_search to the new xe and base.
4237 *
4238 * When the entry is in xattr block, xattr_bh indicates the storage place.
4239 * While if the entry is in index b-tree, "bucket" indicates the
4240 * real place of the xattr.
4241 */
Joel Becker178eeac2008-10-27 15:18:29 -07004242static void ocfs2_xattr_update_xattr_search(struct inode *inode,
4243 struct ocfs2_xattr_search *xs,
4244 struct buffer_head *old_bh)
Tao Ma01225592008-08-18 17:38:53 +08004245{
Tao Ma01225592008-08-18 17:38:53 +08004246 char *buf = old_bh->b_data;
4247 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
4248 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
Joel Becker178eeac2008-10-27 15:18:29 -07004249 int i;
Tao Ma01225592008-08-18 17:38:53 +08004250
Joel Beckerba937122008-10-24 19:13:20 -07004251 xs->header = bucket_xh(xs->bucket);
Joel Becker178eeac2008-10-27 15:18:29 -07004252 xs->base = bucket_block(xs->bucket, 0);
Tao Ma01225592008-08-18 17:38:53 +08004253 xs->end = xs->base + inode->i_sb->s_blocksize;
4254
Joel Becker178eeac2008-10-27 15:18:29 -07004255 if (xs->not_found)
4256 return;
Tao Ma01225592008-08-18 17:38:53 +08004257
Joel Becker178eeac2008-10-27 15:18:29 -07004258 i = xs->here - old_xh->xh_entries;
4259 xs->here = &xs->header->xh_entries[i];
Tao Ma01225592008-08-18 17:38:53 +08004260}
4261
4262static int ocfs2_xattr_create_index_block(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +08004263 struct ocfs2_xattr_search *xs,
4264 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004265{
Tao Ma85db90e2008-11-12 08:27:01 +08004266 int ret;
Tao Ma01225592008-08-18 17:38:53 +08004267 u32 bit_off, len;
4268 u64 blkno;
Tao Ma85db90e2008-11-12 08:27:01 +08004269 handle_t *handle = ctxt->handle;
Tao Ma01225592008-08-18 17:38:53 +08004270 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4271 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Tao Ma01225592008-08-18 17:38:53 +08004272 struct buffer_head *xb_bh = xs->xattr_bh;
4273 struct ocfs2_xattr_block *xb =
4274 (struct ocfs2_xattr_block *)xb_bh->b_data;
4275 struct ocfs2_xattr_tree_root *xr;
4276 u16 xb_flags = le16_to_cpu(xb->xb_flags);
Tao Ma01225592008-08-18 17:38:53 +08004277
4278 mlog(0, "create xattr index block for %llu\n",
4279 (unsigned long long)xb_bh->b_blocknr);
4280
4281 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
Joel Becker178eeac2008-10-27 15:18:29 -07004282 BUG_ON(!xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004283
Tao Ma01225592008-08-18 17:38:53 +08004284 /*
4285 * XXX:
4286 * We can use this lock for now, and maybe move to a dedicated mutex
4287 * if performance becomes a problem later.
4288 */
4289 down_write(&oi->ip_alloc_sem);
4290
Joel Becker0cf2f762009-02-12 16:41:25 -08004291 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), xb_bh,
Joel Becker84008972008-12-09 16:11:49 -08004292 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004293 if (ret) {
4294 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004295 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004296 }
4297
Tao Ma78f30c32008-11-12 08:27:00 +08004298 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac,
4299 1, 1, &bit_off, &len);
Tao Ma01225592008-08-18 17:38:53 +08004300 if (ret) {
4301 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004302 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004303 }
4304
4305 /*
4306 * The bucket may spread in many blocks, and
4307 * we will only touch the 1st block and the last block
4308 * in the whole bucket(one for entry and one for data).
4309 */
4310 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
4311
Mark Fashehde29c082008-10-29 14:45:30 -07004312 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
4313 (unsigned long long)blkno);
Tao Ma01225592008-08-18 17:38:53 +08004314
Joel Becker178eeac2008-10-27 15:18:29 -07004315 ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
Tao Ma01225592008-08-18 17:38:53 +08004316 if (ret) {
4317 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004318 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004319 }
4320
Joel Becker178eeac2008-10-27 15:18:29 -07004321 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
4322 OCFS2_JOURNAL_ACCESS_CREATE);
Joel Beckerbd60bd32008-10-20 18:25:56 -07004323 if (ret) {
4324 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004325 goto out;
Joel Beckerbd60bd32008-10-20 18:25:56 -07004326 }
Tao Ma01225592008-08-18 17:38:53 +08004327
Joel Becker178eeac2008-10-27 15:18:29 -07004328 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
4329 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
4330
4331 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
4332
Tao Ma01225592008-08-18 17:38:53 +08004333 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
4334 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
4335 offsetof(struct ocfs2_xattr_block, xb_attrs));
4336
4337 xr = &xb->xb_attrs.xb_root;
4338 xr->xt_clusters = cpu_to_le32(1);
4339 xr->xt_last_eb_blk = 0;
4340 xr->xt_list.l_tree_depth = 0;
4341 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
4342 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
4343
4344 xr->xt_list.l_recs[0].e_cpos = 0;
4345 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
4346 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
4347
4348 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
4349
Tao Ma85db90e2008-11-12 08:27:01 +08004350 ocfs2_journal_dirty(handle, xb_bh);
Tao Ma01225592008-08-18 17:38:53 +08004351
Tao Ma85db90e2008-11-12 08:27:01 +08004352out:
Tao Ma01225592008-08-18 17:38:53 +08004353 up_write(&oi->ip_alloc_sem);
4354
Tao Ma01225592008-08-18 17:38:53 +08004355 return ret;
4356}
4357
4358static int cmp_xe_offset(const void *a, const void *b)
4359{
4360 const struct ocfs2_xattr_entry *l = a, *r = b;
4361 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
4362 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
4363
4364 if (l_name_offset < r_name_offset)
4365 return 1;
4366 if (l_name_offset > r_name_offset)
4367 return -1;
4368 return 0;
4369}
4370
4371/*
4372 * defrag a xattr bucket if we find that the bucket has some
4373 * holes beteen name/value pairs.
4374 * We will move all the name/value pairs to the end of the bucket
4375 * so that we can spare some space for insertion.
4376 */
4377static int ocfs2_defrag_xattr_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004378 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004379 struct ocfs2_xattr_bucket *bucket)
4380{
4381 int ret, i;
Joel Becker199799a2009-08-14 19:04:15 -07004382 size_t end, offset, len;
Tao Ma01225592008-08-18 17:38:53 +08004383 struct ocfs2_xattr_header *xh;
4384 char *entries, *buf, *bucket_buf = NULL;
Joel Becker9c7759a2008-10-24 16:21:03 -07004385 u64 blkno = bucket_blkno(bucket);
Tao Ma01225592008-08-18 17:38:53 +08004386 u16 xh_free_start;
Tao Ma01225592008-08-18 17:38:53 +08004387 size_t blocksize = inode->i_sb->s_blocksize;
Tao Ma01225592008-08-18 17:38:53 +08004388 struct ocfs2_xattr_entry *xe;
Tao Ma01225592008-08-18 17:38:53 +08004389
4390 /*
4391 * In order to make the operation more efficient and generic,
4392 * we copy all the blocks into a contiguous memory and do the
4393 * defragment there, so if anything is error, we will not touch
4394 * the real block.
4395 */
4396 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
4397 if (!bucket_buf) {
4398 ret = -EIO;
4399 goto out;
4400 }
4401
Joel Becker161d6f32008-10-27 15:25:18 -07004402 buf = bucket_buf;
Tao Ma1c32a2f2008-11-06 08:10:47 +08004403 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4404 memcpy(buf, bucket_block(bucket, i), blocksize);
Joel Becker161d6f32008-10-27 15:25:18 -07004405
Tao Ma1c32a2f2008-11-06 08:10:47 +08004406 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
Joel Becker161d6f32008-10-27 15:25:18 -07004407 OCFS2_JOURNAL_ACCESS_WRITE);
4408 if (ret < 0) {
4409 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004410 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004411 }
4412
4413 xh = (struct ocfs2_xattr_header *)bucket_buf;
4414 entries = (char *)xh->xh_entries;
4415 xh_free_start = le16_to_cpu(xh->xh_free_start);
4416
4417 mlog(0, "adjust xattr bucket in %llu, count = %u, "
4418 "xh_free_start = %u, xh_name_value_len = %u.\n",
Mark Fashehde29c082008-10-29 14:45:30 -07004419 (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
4420 xh_free_start, le16_to_cpu(xh->xh_name_value_len));
Tao Ma01225592008-08-18 17:38:53 +08004421
4422 /*
4423 * sort all the entries by their offset.
4424 * the largest will be the first, so that we can
4425 * move them to the end one by one.
4426 */
4427 sort(entries, le16_to_cpu(xh->xh_count),
4428 sizeof(struct ocfs2_xattr_entry),
4429 cmp_xe_offset, swap_xe);
4430
4431 /* Move all name/values to the end of the bucket. */
4432 xe = xh->xh_entries;
4433 end = OCFS2_XATTR_BUCKET_SIZE;
4434 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
4435 offset = le16_to_cpu(xe->xe_name_offset);
Joel Becker199799a2009-08-14 19:04:15 -07004436 len = namevalue_size_xe(xe);
Tao Ma01225592008-08-18 17:38:53 +08004437
4438 /*
4439 * We must make sure that the name/value pair
4440 * exist in the same block. So adjust end to
4441 * the previous block end if needed.
4442 */
4443 if (((end - len) / blocksize !=
4444 (end - 1) / blocksize))
4445 end = end - end % blocksize;
4446
4447 if (end > offset + len) {
4448 memmove(bucket_buf + end - len,
4449 bucket_buf + offset, len);
4450 xe->xe_name_offset = cpu_to_le16(end - len);
4451 }
4452
4453 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
4454 "bucket %llu\n", (unsigned long long)blkno);
4455
4456 end -= len;
4457 }
4458
4459 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
4460 "bucket %llu\n", (unsigned long long)blkno);
4461
4462 if (xh_free_start == end)
Tao Ma85db90e2008-11-12 08:27:01 +08004463 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004464
4465 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
4466 xh->xh_free_start = cpu_to_le16(end);
4467
4468 /* sort the entries by their name_hash. */
4469 sort(entries, le16_to_cpu(xh->xh_count),
4470 sizeof(struct ocfs2_xattr_entry),
4471 cmp_xe, swap_xe);
4472
4473 buf = bucket_buf;
Tao Ma1c32a2f2008-11-06 08:10:47 +08004474 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4475 memcpy(bucket_block(bucket, i), buf, blocksize);
4476 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
Tao Ma01225592008-08-18 17:38:53 +08004477
Tao Ma01225592008-08-18 17:38:53 +08004478out:
Tao Ma01225592008-08-18 17:38:53 +08004479 kfree(bucket_buf);
4480 return ret;
4481}
4482
4483/*
Joel Beckerb5c03e72008-11-25 19:58:16 -08004484 * prev_blkno points to the start of an existing extent. new_blkno
4485 * points to a newly allocated extent. Because we know each of our
4486 * clusters contains more than bucket, we can easily split one cluster
4487 * at a bucket boundary. So we take the last cluster of the existing
4488 * extent and split it down the middle. We move the last half of the
4489 * buckets in the last cluster of the existing extent over to the new
4490 * extent.
Tao Ma01225592008-08-18 17:38:53 +08004491 *
Joel Beckerb5c03e72008-11-25 19:58:16 -08004492 * first_bh is the buffer at prev_blkno so we can update the existing
4493 * extent's bucket count. header_bh is the bucket were we were hoping
4494 * to insert our xattr. If the bucket move places the target in the new
4495 * extent, we'll update first_bh and header_bh after modifying the old
4496 * extent.
4497 *
4498 * first_hash will be set as the 1st xe's name_hash in the new extent.
Tao Ma01225592008-08-18 17:38:53 +08004499 */
4500static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
4501 handle_t *handle,
Joel Becker41cb8142008-11-26 14:25:21 -08004502 struct ocfs2_xattr_bucket *first,
4503 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08004504 u64 new_blkno,
Tao Ma01225592008-08-18 17:38:53 +08004505 u32 num_clusters,
4506 u32 *first_hash)
4507{
Joel Beckerc58b6032008-11-26 13:36:24 -08004508 int ret;
Joel Becker41cb8142008-11-26 14:25:21 -08004509 struct super_block *sb = inode->i_sb;
4510 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb);
4511 int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
Joel Beckerb5c03e72008-11-25 19:58:16 -08004512 int to_move = num_buckets / 2;
Joel Beckerc58b6032008-11-26 13:36:24 -08004513 u64 src_blkno;
Joel Becker41cb8142008-11-26 14:25:21 -08004514 u64 last_cluster_blkno = bucket_blkno(first) +
4515 ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1));
Tao Ma01225592008-08-18 17:38:53 +08004516
Joel Becker41cb8142008-11-26 14:25:21 -08004517 BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets);
4518 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize);
Tao Ma01225592008-08-18 17:38:53 +08004519
Tao Ma01225592008-08-18 17:38:53 +08004520 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
Joel Beckerc58b6032008-11-26 13:36:24 -08004521 (unsigned long long)last_cluster_blkno, (unsigned long long)new_blkno);
Tao Ma01225592008-08-18 17:38:53 +08004522
Joel Becker41cb8142008-11-26 14:25:21 -08004523 ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first),
Joel Beckerc58b6032008-11-26 13:36:24 -08004524 last_cluster_blkno, new_blkno,
4525 to_move, first_hash);
Joel Beckerb5c03e72008-11-25 19:58:16 -08004526 if (ret) {
4527 mlog_errno(ret);
4528 goto out;
4529 }
4530
Joel Beckerc58b6032008-11-26 13:36:24 -08004531 /* This is the first bucket that got moved */
4532 src_blkno = last_cluster_blkno + (to_move * blks_per_bucket);
4533
Tao Ma01225592008-08-18 17:38:53 +08004534 /*
Joel Beckerc58b6032008-11-26 13:36:24 -08004535 * If the target bucket was part of the moved buckets, we need to
Joel Becker41cb8142008-11-26 14:25:21 -08004536 * update first and target.
Joel Beckerb5c03e72008-11-25 19:58:16 -08004537 */
Joel Becker41cb8142008-11-26 14:25:21 -08004538 if (bucket_blkno(target) >= src_blkno) {
Joel Beckerb5c03e72008-11-25 19:58:16 -08004539 /* Find the block for the new target bucket */
4540 src_blkno = new_blkno +
Joel Becker41cb8142008-11-26 14:25:21 -08004541 (bucket_blkno(target) - src_blkno);
4542
4543 ocfs2_xattr_bucket_relse(first);
4544 ocfs2_xattr_bucket_relse(target);
Joel Beckerb5c03e72008-11-25 19:58:16 -08004545
4546 /*
Joel Beckerc58b6032008-11-26 13:36:24 -08004547 * These shouldn't fail - the buffers are in the
Joel Beckerb5c03e72008-11-25 19:58:16 -08004548 * journal from ocfs2_cp_xattr_bucket().
4549 */
Joel Becker41cb8142008-11-26 14:25:21 -08004550 ret = ocfs2_read_xattr_bucket(first, new_blkno);
Joel Beckerc58b6032008-11-26 13:36:24 -08004551 if (ret) {
4552 mlog_errno(ret);
4553 goto out;
4554 }
Joel Becker41cb8142008-11-26 14:25:21 -08004555 ret = ocfs2_read_xattr_bucket(target, src_blkno);
4556 if (ret)
Joel Beckerb5c03e72008-11-25 19:58:16 -08004557 mlog_errno(ret);
Joel Beckerb5c03e72008-11-25 19:58:16 -08004558
Joel Beckerb5c03e72008-11-25 19:58:16 -08004559 }
4560
Tao Ma01225592008-08-18 17:38:53 +08004561out:
Tao Ma01225592008-08-18 17:38:53 +08004562 return ret;
4563}
4564
Tao Ma01225592008-08-18 17:38:53 +08004565/*
Tao Ma80bcaf32008-10-27 06:06:24 +08004566 * Find the suitable pos when we divide a bucket into 2.
4567 * We have to make sure the xattrs with the same hash value exist
4568 * in the same bucket.
4569 *
4570 * If this ocfs2_xattr_header covers more than one hash value, find a
4571 * place where the hash value changes. Try to find the most even split.
4572 * The most common case is that all entries have different hash values,
4573 * and the first check we make will find a place to split.
Tao Ma01225592008-08-18 17:38:53 +08004574 */
Tao Ma80bcaf32008-10-27 06:06:24 +08004575static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
4576{
4577 struct ocfs2_xattr_entry *entries = xh->xh_entries;
4578 int count = le16_to_cpu(xh->xh_count);
4579 int delta, middle = count / 2;
4580
4581 /*
4582 * We start at the middle. Each step gets farther away in both
4583 * directions. We therefore hit the change in hash value
4584 * nearest to the middle. Note that this loop does not execute for
4585 * count < 2.
4586 */
4587 for (delta = 0; delta < middle; delta++) {
4588 /* Let's check delta earlier than middle */
4589 if (cmp_xe(&entries[middle - delta - 1],
4590 &entries[middle - delta]))
4591 return middle - delta;
4592
4593 /* For even counts, don't walk off the end */
4594 if ((middle + delta + 1) == count)
4595 continue;
4596
4597 /* Now try delta past middle */
4598 if (cmp_xe(&entries[middle + delta],
4599 &entries[middle + delta + 1]))
4600 return middle + delta + 1;
4601 }
4602
4603 /* Every entry had the same hash */
4604 return count;
4605}
4606
4607/*
4608 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
4609 * first_hash will record the 1st hash of the new bucket.
4610 *
4611 * Normally half of the xattrs will be moved. But we have to make
4612 * sure that the xattrs with the same hash value are stored in the
4613 * same bucket. If all the xattrs in this bucket have the same hash
4614 * value, the new bucket will be initialized as an empty one and the
4615 * first_hash will be initialized as (hash_value+1).
4616 */
4617static int ocfs2_divide_xattr_bucket(struct inode *inode,
4618 handle_t *handle,
4619 u64 blk,
4620 u64 new_blk,
4621 u32 *first_hash,
4622 int new_bucket_head)
Tao Ma01225592008-08-18 17:38:53 +08004623{
4624 int ret, i;
Joel Becker199799a2009-08-14 19:04:15 -07004625 int count, start, len, name_value_len = 0, name_offset = 0;
Joel Beckerba937122008-10-24 19:13:20 -07004626 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
Tao Ma01225592008-08-18 17:38:53 +08004627 struct ocfs2_xattr_header *xh;
4628 struct ocfs2_xattr_entry *xe;
4629 int blocksize = inode->i_sb->s_blocksize;
4630
Tao Ma80bcaf32008-10-27 06:06:24 +08004631 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07004632 (unsigned long long)blk, (unsigned long long)new_blk);
Tao Ma01225592008-08-18 17:38:53 +08004633
Joel Beckerba937122008-10-24 19:13:20 -07004634 s_bucket = ocfs2_xattr_bucket_new(inode);
4635 t_bucket = ocfs2_xattr_bucket_new(inode);
4636 if (!s_bucket || !t_bucket) {
4637 ret = -ENOMEM;
4638 mlog_errno(ret);
4639 goto out;
4640 }
Tao Ma01225592008-08-18 17:38:53 +08004641
Joel Beckerba937122008-10-24 19:13:20 -07004642 ret = ocfs2_read_xattr_bucket(s_bucket, blk);
Tao Ma01225592008-08-18 17:38:53 +08004643 if (ret) {
4644 mlog_errno(ret);
4645 goto out;
4646 }
4647
Joel Beckerba937122008-10-24 19:13:20 -07004648 ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004649 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004650 if (ret) {
4651 mlog_errno(ret);
4652 goto out;
4653 }
4654
Joel Becker784b8162008-10-24 17:33:40 -07004655 /*
4656 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
4657 * there's no need to read it.
4658 */
Joel Beckerba937122008-10-24 19:13:20 -07004659 ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
Tao Ma01225592008-08-18 17:38:53 +08004660 if (ret) {
4661 mlog_errno(ret);
4662 goto out;
4663 }
4664
Joel Becker2b656c12008-11-25 19:00:15 -08004665 /*
4666 * Hey, if we're overwriting t_bucket, what difference does
4667 * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the
4668 * same part of ocfs2_cp_xattr_bucket().
4669 */
Joel Beckerba937122008-10-24 19:13:20 -07004670 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004671 new_bucket_head ?
4672 OCFS2_JOURNAL_ACCESS_CREATE :
4673 OCFS2_JOURNAL_ACCESS_WRITE);
4674 if (ret) {
4675 mlog_errno(ret);
4676 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004677 }
4678
Joel Beckerba937122008-10-24 19:13:20 -07004679 xh = bucket_xh(s_bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08004680 count = le16_to_cpu(xh->xh_count);
4681 start = ocfs2_xattr_find_divide_pos(xh);
4682
4683 if (start == count) {
4684 xe = &xh->xh_entries[start-1];
4685
4686 /*
4687 * initialized a new empty bucket here.
4688 * The hash value is set as one larger than
4689 * that of the last entry in the previous bucket.
4690 */
Joel Beckerba937122008-10-24 19:13:20 -07004691 for (i = 0; i < t_bucket->bu_blocks; i++)
4692 memset(bucket_block(t_bucket, i), 0, blocksize);
Tao Ma80bcaf32008-10-27 06:06:24 +08004693
Joel Beckerba937122008-10-24 19:13:20 -07004694 xh = bucket_xh(t_bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08004695 xh->xh_free_start = cpu_to_le16(blocksize);
4696 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
4697 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
4698
4699 goto set_num_buckets;
4700 }
4701
Tao Ma01225592008-08-18 17:38:53 +08004702 /* copy the whole bucket to the new first. */
Joel Beckerba937122008-10-24 19:13:20 -07004703 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004704
4705 /* update the new bucket. */
Joel Beckerba937122008-10-24 19:13:20 -07004706 xh = bucket_xh(t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004707
4708 /*
4709 * Calculate the total name/value len and xh_free_start for
4710 * the old bucket first.
4711 */
4712 name_offset = OCFS2_XATTR_BUCKET_SIZE;
4713 name_value_len = 0;
4714 for (i = 0; i < start; i++) {
4715 xe = &xh->xh_entries[i];
Joel Becker199799a2009-08-14 19:04:15 -07004716 name_value_len += namevalue_size_xe(xe);
Tao Ma01225592008-08-18 17:38:53 +08004717 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
4718 name_offset = le16_to_cpu(xe->xe_name_offset);
4719 }
4720
4721 /*
4722 * Now begin the modification to the new bucket.
4723 *
4724 * In the new bucket, We just move the xattr entry to the beginning
4725 * and don't touch the name/value. So there will be some holes in the
4726 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
4727 * called.
4728 */
4729 xe = &xh->xh_entries[start];
4730 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
4731 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
Mark Fashehff1ec202008-08-19 10:54:29 -07004732 (int)((char *)xe - (char *)xh),
4733 (int)((char *)xh->xh_entries - (char *)xh));
Tao Ma01225592008-08-18 17:38:53 +08004734 memmove((char *)xh->xh_entries, (char *)xe, len);
4735 xe = &xh->xh_entries[count - start];
4736 len = sizeof(struct ocfs2_xattr_entry) * start;
4737 memset((char *)xe, 0, len);
4738
4739 le16_add_cpu(&xh->xh_count, -start);
4740 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
4741
4742 /* Calculate xh_free_start for the new bucket. */
4743 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4744 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4745 xe = &xh->xh_entries[i];
Tao Ma01225592008-08-18 17:38:53 +08004746 if (le16_to_cpu(xe->xe_name_offset) <
4747 le16_to_cpu(xh->xh_free_start))
4748 xh->xh_free_start = xe->xe_name_offset;
4749 }
4750
Tao Ma80bcaf32008-10-27 06:06:24 +08004751set_num_buckets:
Tao Ma01225592008-08-18 17:38:53 +08004752 /* set xh->xh_num_buckets for the new xh. */
4753 if (new_bucket_head)
4754 xh->xh_num_buckets = cpu_to_le16(1);
4755 else
4756 xh->xh_num_buckets = 0;
4757
Joel Beckerba937122008-10-24 19:13:20 -07004758 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004759
4760 /* store the first_hash of the new bucket. */
4761 if (first_hash)
4762 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
4763
4764 /*
Tao Ma80bcaf32008-10-27 06:06:24 +08004765 * Now only update the 1st block of the old bucket. If we
4766 * just added a new empty bucket, there is no need to modify
4767 * it.
Tao Ma01225592008-08-18 17:38:53 +08004768 */
Tao Ma80bcaf32008-10-27 06:06:24 +08004769 if (start == count)
4770 goto out;
4771
Joel Beckerba937122008-10-24 19:13:20 -07004772 xh = bucket_xh(s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004773 memset(&xh->xh_entries[start], 0,
4774 sizeof(struct ocfs2_xattr_entry) * (count - start));
4775 xh->xh_count = cpu_to_le16(start);
4776 xh->xh_free_start = cpu_to_le16(name_offset);
4777 xh->xh_name_value_len = cpu_to_le16(name_value_len);
4778
Joel Beckerba937122008-10-24 19:13:20 -07004779 ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004780
4781out:
Joel Beckerba937122008-10-24 19:13:20 -07004782 ocfs2_xattr_bucket_free(s_bucket);
4783 ocfs2_xattr_bucket_free(t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004784
4785 return ret;
4786}
4787
4788/*
4789 * Copy xattr from one bucket to another bucket.
4790 *
4791 * The caller must make sure that the journal transaction
4792 * has enough space for journaling.
4793 */
4794static int ocfs2_cp_xattr_bucket(struct inode *inode,
4795 handle_t *handle,
4796 u64 s_blkno,
4797 u64 t_blkno,
4798 int t_is_new)
4799{
Joel Becker4980c6d2008-10-24 18:54:43 -07004800 int ret;
Joel Beckerba937122008-10-24 19:13:20 -07004801 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
Tao Ma01225592008-08-18 17:38:53 +08004802
4803 BUG_ON(s_blkno == t_blkno);
4804
4805 mlog(0, "cp bucket %llu to %llu, target is %d\n",
Mark Fashehde29c082008-10-29 14:45:30 -07004806 (unsigned long long)s_blkno, (unsigned long long)t_blkno,
4807 t_is_new);
Tao Ma01225592008-08-18 17:38:53 +08004808
Joel Beckerba937122008-10-24 19:13:20 -07004809 s_bucket = ocfs2_xattr_bucket_new(inode);
4810 t_bucket = ocfs2_xattr_bucket_new(inode);
4811 if (!s_bucket || !t_bucket) {
4812 ret = -ENOMEM;
4813 mlog_errno(ret);
4814 goto out;
4815 }
Joel Becker92de1092008-11-25 17:06:40 -08004816
Joel Beckerba937122008-10-24 19:13:20 -07004817 ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
Tao Ma01225592008-08-18 17:38:53 +08004818 if (ret)
4819 goto out;
4820
Joel Becker784b8162008-10-24 17:33:40 -07004821 /*
4822 * Even if !t_is_new, we're overwriting t_bucket. Thus,
4823 * there's no need to read it.
4824 */
Joel Beckerba937122008-10-24 19:13:20 -07004825 ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
Tao Ma01225592008-08-18 17:38:53 +08004826 if (ret)
4827 goto out;
4828
Joel Becker2b656c12008-11-25 19:00:15 -08004829 /*
4830 * Hey, if we're overwriting t_bucket, what difference does
4831 * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new
Joel Becker874d65a2008-11-26 13:02:18 -08004832 * cluster to fill, we came here from
4833 * ocfs2_mv_xattr_buckets(), and it is really new -
4834 * ACCESS_CREATE is required. But we also might have moved data
4835 * out of t_bucket before extending back into it.
4836 * ocfs2_add_new_xattr_bucket() can do this - its call to
4837 * ocfs2_add_new_xattr_cluster() may have created a new extent
Joel Becker2b656c12008-11-25 19:00:15 -08004838 * and copied out the end of the old extent. Then it re-extends
4839 * the old extent back to create space for new xattrs. That's
4840 * how we get here, and the bucket isn't really new.
4841 */
Joel Beckerba937122008-10-24 19:13:20 -07004842 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004843 t_is_new ?
4844 OCFS2_JOURNAL_ACCESS_CREATE :
4845 OCFS2_JOURNAL_ACCESS_WRITE);
4846 if (ret)
4847 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004848
Joel Beckerba937122008-10-24 19:13:20 -07004849 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4850 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004851
4852out:
Joel Beckerba937122008-10-24 19:13:20 -07004853 ocfs2_xattr_bucket_free(t_bucket);
4854 ocfs2_xattr_bucket_free(s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004855
4856 return ret;
4857}
4858
4859/*
Joel Becker874d65a2008-11-26 13:02:18 -08004860 * src_blk points to the start of an existing extent. last_blk points to
4861 * last cluster in that extent. to_blk points to a newly allocated
Joel Becker54ecb6b2008-11-26 13:18:31 -08004862 * extent. We copy the buckets from the cluster at last_blk to the new
4863 * extent. If start_bucket is non-zero, we skip that many buckets before
4864 * we start copying. The new extent's xh_num_buckets gets set to the
4865 * number of buckets we copied. The old extent's xh_num_buckets shrinks
4866 * by the same amount.
Tao Ma01225592008-08-18 17:38:53 +08004867 */
Joel Becker54ecb6b2008-11-26 13:18:31 -08004868static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
4869 u64 src_blk, u64 last_blk, u64 to_blk,
4870 unsigned int start_bucket,
4871 u32 *first_hash)
Tao Ma01225592008-08-18 17:38:53 +08004872{
4873 int i, ret, credits;
4874 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Joel Becker15d60922008-11-25 18:36:42 -08004875 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08004876 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
Joel Becker15d60922008-11-25 18:36:42 -08004877 struct ocfs2_xattr_bucket *old_first, *new_first;
Tao Ma01225592008-08-18 17:38:53 +08004878
Joel Becker874d65a2008-11-26 13:02:18 -08004879 mlog(0, "mv xattrs from cluster %llu to %llu\n",
4880 (unsigned long long)last_blk, (unsigned long long)to_blk);
Tao Ma01225592008-08-18 17:38:53 +08004881
Joel Becker54ecb6b2008-11-26 13:18:31 -08004882 BUG_ON(start_bucket >= num_buckets);
4883 if (start_bucket) {
4884 num_buckets -= start_bucket;
4885 last_blk += (start_bucket * blks_per_bucket);
4886 }
4887
Joel Becker15d60922008-11-25 18:36:42 -08004888 /* The first bucket of the original extent */
4889 old_first = ocfs2_xattr_bucket_new(inode);
4890 /* The first bucket of the new extent */
4891 new_first = ocfs2_xattr_bucket_new(inode);
4892 if (!old_first || !new_first) {
4893 ret = -ENOMEM;
4894 mlog_errno(ret);
4895 goto out;
4896 }
4897
Joel Becker874d65a2008-11-26 13:02:18 -08004898 ret = ocfs2_read_xattr_bucket(old_first, src_blk);
Joel Becker15d60922008-11-25 18:36:42 -08004899 if (ret) {
4900 mlog_errno(ret);
4901 goto out;
4902 }
4903
Tao Ma01225592008-08-18 17:38:53 +08004904 /*
Joel Becker54ecb6b2008-11-26 13:18:31 -08004905 * We need to update the first bucket of the old extent and all
4906 * the buckets going to the new extent.
Tao Ma01225592008-08-18 17:38:53 +08004907 */
Joel Becker54ecb6b2008-11-26 13:18:31 -08004908 credits = ((num_buckets + 1) * blks_per_bucket) +
4909 handle->h_buffer_credits;
Tao Ma01225592008-08-18 17:38:53 +08004910 ret = ocfs2_extend_trans(handle, credits);
4911 if (ret) {
4912 mlog_errno(ret);
4913 goto out;
4914 }
4915
Joel Becker15d60922008-11-25 18:36:42 -08004916 ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
4917 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004918 if (ret) {
4919 mlog_errno(ret);
4920 goto out;
4921 }
4922
4923 for (i = 0; i < num_buckets; i++) {
4924 ret = ocfs2_cp_xattr_bucket(inode, handle,
Joel Becker874d65a2008-11-26 13:02:18 -08004925 last_blk + (i * blks_per_bucket),
Joel Becker15d60922008-11-25 18:36:42 -08004926 to_blk + (i * blks_per_bucket),
4927 1);
Tao Ma01225592008-08-18 17:38:53 +08004928 if (ret) {
4929 mlog_errno(ret);
4930 goto out;
4931 }
Tao Ma01225592008-08-18 17:38:53 +08004932 }
4933
Joel Becker15d60922008-11-25 18:36:42 -08004934 /*
4935 * Get the new bucket ready before we dirty anything
4936 * (This actually shouldn't fail, because we already dirtied
4937 * it once in ocfs2_cp_xattr_bucket()).
4938 */
4939 ret = ocfs2_read_xattr_bucket(new_first, to_blk);
4940 if (ret) {
Tao Ma01225592008-08-18 17:38:53 +08004941 mlog_errno(ret);
4942 goto out;
4943 }
Joel Becker15d60922008-11-25 18:36:42 -08004944 ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
4945 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004946 if (ret) {
4947 mlog_errno(ret);
4948 goto out;
4949 }
4950
Joel Becker15d60922008-11-25 18:36:42 -08004951 /* Now update the headers */
4952 le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets);
4953 ocfs2_xattr_bucket_journal_dirty(handle, old_first);
Tao Ma01225592008-08-18 17:38:53 +08004954
Joel Becker15d60922008-11-25 18:36:42 -08004955 bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets);
4956 ocfs2_xattr_bucket_journal_dirty(handle, new_first);
Tao Ma01225592008-08-18 17:38:53 +08004957
4958 if (first_hash)
Joel Becker15d60922008-11-25 18:36:42 -08004959 *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash);
4960
Tao Ma01225592008-08-18 17:38:53 +08004961out:
Joel Becker15d60922008-11-25 18:36:42 -08004962 ocfs2_xattr_bucket_free(new_first);
4963 ocfs2_xattr_bucket_free(old_first);
Tao Ma01225592008-08-18 17:38:53 +08004964 return ret;
4965}
4966
4967/*
Tao Ma80bcaf32008-10-27 06:06:24 +08004968 * Move some xattrs in this cluster to the new cluster.
Tao Ma01225592008-08-18 17:38:53 +08004969 * This function should only be called when bucket size == cluster size.
4970 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
4971 */
Tao Ma80bcaf32008-10-27 06:06:24 +08004972static int ocfs2_divide_xattr_cluster(struct inode *inode,
4973 handle_t *handle,
4974 u64 prev_blk,
4975 u64 new_blk,
4976 u32 *first_hash)
Tao Ma01225592008-08-18 17:38:53 +08004977{
4978 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08004979 int ret, credits = 2 * blk_per_bucket + handle->h_buffer_credits;
Tao Ma01225592008-08-18 17:38:53 +08004980
4981 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
4982
4983 ret = ocfs2_extend_trans(handle, credits);
4984 if (ret) {
4985 mlog_errno(ret);
4986 return ret;
4987 }
4988
4989 /* Move half of the xattr in start_blk to the next bucket. */
Tao Ma80bcaf32008-10-27 06:06:24 +08004990 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
4991 new_blk, first_hash, 1);
Tao Ma01225592008-08-18 17:38:53 +08004992}
4993
4994/*
4995 * Move some xattrs from the old cluster to the new one since they are not
4996 * contiguous in ocfs2 xattr tree.
4997 *
4998 * new_blk starts a new separate cluster, and we will move some xattrs from
4999 * prev_blk to it. v_start will be set as the first name hash value in this
5000 * new cluster so that it can be used as e_cpos during tree insertion and
5001 * don't collide with our original b-tree operations. first_bh and header_bh
5002 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
5003 * to extend the insert bucket.
5004 *
5005 * The problem is how much xattr should we move to the new one and when should
5006 * we update first_bh and header_bh?
5007 * 1. If cluster size > bucket size, that means the previous cluster has more
5008 * than 1 bucket, so just move half nums of bucket into the new cluster and
5009 * update the first_bh and header_bh if the insert bucket has been moved
5010 * to the new cluster.
5011 * 2. If cluster_size == bucket_size:
5012 * a) If the previous extent rec has more than one cluster and the insert
5013 * place isn't in the last cluster, copy the entire last cluster to the
5014 * new one. This time, we don't need to upate the first_bh and header_bh
5015 * since they will not be moved into the new cluster.
5016 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
5017 * the new one. And we set the extend flag to zero if the insert place is
5018 * moved into the new allocated cluster since no extend is needed.
5019 */
5020static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
5021 handle_t *handle,
Joel Becker012ee912008-11-26 14:43:31 -08005022 struct ocfs2_xattr_bucket *first,
5023 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08005024 u64 new_blk,
Tao Ma01225592008-08-18 17:38:53 +08005025 u32 prev_clusters,
5026 u32 *v_start,
5027 int *extend)
5028{
Joel Becker92cf3ad2008-11-26 14:12:09 -08005029 int ret;
Tao Ma01225592008-08-18 17:38:53 +08005030
5031 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
Joel Becker012ee912008-11-26 14:43:31 -08005032 (unsigned long long)bucket_blkno(first), prev_clusters,
Mark Fashehde29c082008-10-29 14:45:30 -07005033 (unsigned long long)new_blk);
Tao Ma01225592008-08-18 17:38:53 +08005034
Joel Becker41cb8142008-11-26 14:25:21 -08005035 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) {
Tao Ma01225592008-08-18 17:38:53 +08005036 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
5037 handle,
Joel Becker41cb8142008-11-26 14:25:21 -08005038 first, target,
Tao Ma01225592008-08-18 17:38:53 +08005039 new_blk,
Tao Ma01225592008-08-18 17:38:53 +08005040 prev_clusters,
5041 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08005042 if (ret)
Joel Becker41cb8142008-11-26 14:25:21 -08005043 mlog_errno(ret);
Joel Becker41cb8142008-11-26 14:25:21 -08005044 } else {
Joel Becker92cf3ad2008-11-26 14:12:09 -08005045 /* The start of the last cluster in the first extent */
5046 u64 last_blk = bucket_blkno(first) +
5047 ((prev_clusters - 1) *
5048 ocfs2_clusters_to_blocks(inode->i_sb, 1));
Tao Ma01225592008-08-18 17:38:53 +08005049
Joel Becker012ee912008-11-26 14:43:31 -08005050 if (prev_clusters > 1 && bucket_blkno(target) != last_blk) {
Joel Becker874d65a2008-11-26 13:02:18 -08005051 ret = ocfs2_mv_xattr_buckets(inode, handle,
Joel Becker92cf3ad2008-11-26 14:12:09 -08005052 bucket_blkno(first),
Joel Becker54ecb6b2008-11-26 13:18:31 -08005053 last_blk, new_blk, 0,
Tao Ma01225592008-08-18 17:38:53 +08005054 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08005055 if (ret)
5056 mlog_errno(ret);
5057 } else {
Tao Ma80bcaf32008-10-27 06:06:24 +08005058 ret = ocfs2_divide_xattr_cluster(inode, handle,
5059 last_blk, new_blk,
5060 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08005061 if (ret)
5062 mlog_errno(ret);
Tao Ma01225592008-08-18 17:38:53 +08005063
Joel Becker92cf3ad2008-11-26 14:12:09 -08005064 if ((bucket_blkno(target) == last_blk) && extend)
Tao Ma01225592008-08-18 17:38:53 +08005065 *extend = 0;
5066 }
5067 }
5068
5069 return ret;
5070}
5071
5072/*
5073 * Add a new cluster for xattr storage.
5074 *
5075 * If the new cluster is contiguous with the previous one, it will be
5076 * appended to the same extent record, and num_clusters will be updated.
5077 * If not, we will insert a new extent for it and move some xattrs in
5078 * the last cluster into the new allocated one.
5079 * We also need to limit the maximum size of a btree leaf, otherwise we'll
5080 * lose the benefits of hashing because we'll have to search large leaves.
5081 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
5082 * if it's bigger).
5083 *
5084 * first_bh is the first block of the previous extent rec and header_bh
5085 * indicates the bucket we will insert the new xattrs. They will be updated
5086 * when the header_bh is moved into the new cluster.
5087 */
5088static int ocfs2_add_new_xattr_cluster(struct inode *inode,
5089 struct buffer_head *root_bh,
Joel Beckered29c0c2008-11-26 15:08:44 -08005090 struct ocfs2_xattr_bucket *first,
5091 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08005092 u32 *num_clusters,
5093 u32 prev_cpos,
Tao Ma78f30c32008-11-12 08:27:00 +08005094 int *extend,
5095 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005096{
Tao Ma85db90e2008-11-12 08:27:01 +08005097 int ret;
Tao Ma01225592008-08-18 17:38:53 +08005098 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
5099 u32 prev_clusters = *num_clusters;
5100 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
5101 u64 block;
Tao Ma85db90e2008-11-12 08:27:01 +08005102 handle_t *handle = ctxt->handle;
Tao Ma01225592008-08-18 17:38:53 +08005103 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Joel Beckerf99b9b72008-08-20 19:36:33 -07005104 struct ocfs2_extent_tree et;
Tao Ma01225592008-08-18 17:38:53 +08005105
5106 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
5107 "previous xattr blkno = %llu\n",
5108 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Joel Beckered29c0c2008-11-26 15:08:44 -08005109 prev_cpos, (unsigned long long)bucket_blkno(first));
Tao Ma01225592008-08-18 17:38:53 +08005110
Joel Becker5e404e92009-02-13 03:54:22 -08005111 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07005112
Joel Becker0cf2f762009-02-12 16:41:25 -08005113 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
Joel Becker84008972008-12-09 16:11:49 -08005114 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08005115 if (ret < 0) {
5116 mlog_errno(ret);
5117 goto leave;
5118 }
5119
Tao Ma78f30c32008-11-12 08:27:00 +08005120 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, 1,
Tao Ma01225592008-08-18 17:38:53 +08005121 clusters_to_add, &bit_off, &num_bits);
5122 if (ret < 0) {
5123 if (ret != -ENOSPC)
5124 mlog_errno(ret);
5125 goto leave;
5126 }
5127
5128 BUG_ON(num_bits > clusters_to_add);
5129
5130 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
5131 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
5132 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
5133
Joel Beckered29c0c2008-11-26 15:08:44 -08005134 if (bucket_blkno(first) + (prev_clusters * bpc) == block &&
Tao Ma01225592008-08-18 17:38:53 +08005135 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
5136 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
5137 /*
5138 * If this cluster is contiguous with the old one and
5139 * adding this new cluster, we don't surpass the limit of
5140 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
5141 * initialized and used like other buckets in the previous
5142 * cluster.
5143 * So add it as a contiguous one. The caller will handle
5144 * its init process.
5145 */
5146 v_start = prev_cpos + prev_clusters;
5147 *num_clusters = prev_clusters + num_bits;
5148 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
5149 num_bits);
5150 } else {
5151 ret = ocfs2_adjust_xattr_cross_cluster(inode,
5152 handle,
Joel Becker012ee912008-11-26 14:43:31 -08005153 first,
5154 target,
Tao Ma01225592008-08-18 17:38:53 +08005155 block,
Tao Ma01225592008-08-18 17:38:53 +08005156 prev_clusters,
5157 &v_start,
5158 extend);
5159 if (ret) {
5160 mlog_errno(ret);
5161 goto leave;
5162 }
5163 }
5164
5165 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
Mark Fashehde29c082008-10-29 14:45:30 -07005166 num_bits, (unsigned long long)block, v_start);
Joel Beckercc79d8c2009-02-13 03:24:43 -08005167 ret = ocfs2_insert_extent(handle, &et, v_start, block,
Tao Ma78f30c32008-11-12 08:27:00 +08005168 num_bits, 0, ctxt->meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08005169 if (ret < 0) {
5170 mlog_errno(ret);
5171 goto leave;
5172 }
5173
5174 ret = ocfs2_journal_dirty(handle, root_bh);
Tao Ma85db90e2008-11-12 08:27:01 +08005175 if (ret < 0)
Tao Ma01225592008-08-18 17:38:53 +08005176 mlog_errno(ret);
Tao Ma01225592008-08-18 17:38:53 +08005177
5178leave:
Tao Ma01225592008-08-18 17:38:53 +08005179 return ret;
5180}
5181
5182/*
Joel Becker92de1092008-11-25 17:06:40 -08005183 * We are given an extent. 'first' is the bucket at the very front of
5184 * the extent. The extent has space for an additional bucket past
5185 * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
5186 * of the target bucket. We wish to shift every bucket past the target
5187 * down one, filling in that additional space. When we get back to the
5188 * target, we split the target between itself and the now-empty bucket
5189 * at target+1 (aka, target_blkno + blks_per_bucket).
Tao Ma01225592008-08-18 17:38:53 +08005190 */
5191static int ocfs2_extend_xattr_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08005192 handle_t *handle,
Joel Becker92de1092008-11-25 17:06:40 -08005193 struct ocfs2_xattr_bucket *first,
5194 u64 target_blk,
Tao Ma01225592008-08-18 17:38:53 +08005195 u32 num_clusters)
5196{
5197 int ret, credits;
5198 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5199 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Joel Becker92de1092008-11-25 17:06:40 -08005200 u64 end_blk;
5201 u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets);
Tao Ma01225592008-08-18 17:38:53 +08005202
5203 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
Joel Becker92de1092008-11-25 17:06:40 -08005204 "from %llu, len = %u\n", (unsigned long long)target_blk,
5205 (unsigned long long)bucket_blkno(first), num_clusters);
Tao Ma01225592008-08-18 17:38:53 +08005206
Joel Becker92de1092008-11-25 17:06:40 -08005207 /* The extent must have room for an additional bucket */
5208 BUG_ON(new_bucket >=
5209 (num_clusters * ocfs2_xattr_buckets_per_cluster(osb)));
Tao Ma01225592008-08-18 17:38:53 +08005210
Joel Becker92de1092008-11-25 17:06:40 -08005211 /* end_blk points to the last existing bucket */
5212 end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket);
Tao Ma01225592008-08-18 17:38:53 +08005213
5214 /*
Joel Becker92de1092008-11-25 17:06:40 -08005215 * end_blk is the start of the last existing bucket.
5216 * Thus, (end_blk - target_blk) covers the target bucket and
5217 * every bucket after it up to, but not including, the last
5218 * existing bucket. Then we add the last existing bucket, the
5219 * new bucket, and the first bucket (3 * blk_per_bucket).
Tao Ma01225592008-08-18 17:38:53 +08005220 */
Joel Becker92de1092008-11-25 17:06:40 -08005221 credits = (end_blk - target_blk) + (3 * blk_per_bucket) +
Tao Ma85db90e2008-11-12 08:27:01 +08005222 handle->h_buffer_credits;
5223 ret = ocfs2_extend_trans(handle, credits);
5224 if (ret) {
Tao Ma01225592008-08-18 17:38:53 +08005225 mlog_errno(ret);
5226 goto out;
5227 }
5228
Joel Becker92de1092008-11-25 17:06:40 -08005229 ret = ocfs2_xattr_bucket_journal_access(handle, first,
5230 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08005231 if (ret) {
5232 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08005233 goto out;
Tao Ma01225592008-08-18 17:38:53 +08005234 }
5235
Joel Becker92de1092008-11-25 17:06:40 -08005236 while (end_blk != target_blk) {
Tao Ma01225592008-08-18 17:38:53 +08005237 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
5238 end_blk + blk_per_bucket, 0);
5239 if (ret)
Tao Ma85db90e2008-11-12 08:27:01 +08005240 goto out;
Tao Ma01225592008-08-18 17:38:53 +08005241 end_blk -= blk_per_bucket;
5242 }
5243
Joel Becker92de1092008-11-25 17:06:40 -08005244 /* Move half of the xattr in target_blkno to the next bucket. */
5245 ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk,
5246 target_blk + blk_per_bucket, NULL, 0);
Tao Ma01225592008-08-18 17:38:53 +08005247
Joel Becker92de1092008-11-25 17:06:40 -08005248 le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1);
5249 ocfs2_xattr_bucket_journal_dirty(handle, first);
Tao Ma01225592008-08-18 17:38:53 +08005250
Tao Ma01225592008-08-18 17:38:53 +08005251out:
5252 return ret;
5253}
5254
5255/*
Joel Becker91f20332008-11-26 15:25:41 -08005256 * Add new xattr bucket in an extent record and adjust the buckets
5257 * accordingly. xb_bh is the ocfs2_xattr_block, and target is the
5258 * bucket we want to insert into.
Tao Ma01225592008-08-18 17:38:53 +08005259 *
Joel Becker91f20332008-11-26 15:25:41 -08005260 * In the easy case, we will move all the buckets after target down by
5261 * one. Half of target's xattrs will be moved to the next bucket.
5262 *
5263 * If current cluster is full, we'll allocate a new one. This may not
5264 * be contiguous. The underlying calls will make sure that there is
5265 * space for the insert, shifting buckets around if necessary.
5266 * 'target' may be moved by those calls.
Tao Ma01225592008-08-18 17:38:53 +08005267 */
5268static int ocfs2_add_new_xattr_bucket(struct inode *inode,
5269 struct buffer_head *xb_bh,
Joel Becker91f20332008-11-26 15:25:41 -08005270 struct ocfs2_xattr_bucket *target,
Tao Ma78f30c32008-11-12 08:27:00 +08005271 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005272{
Tao Ma01225592008-08-18 17:38:53 +08005273 struct ocfs2_xattr_block *xb =
5274 (struct ocfs2_xattr_block *)xb_bh->b_data;
5275 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
5276 struct ocfs2_extent_list *el = &xb_root->xt_list;
Joel Becker91f20332008-11-26 15:25:41 -08005277 u32 name_hash =
5278 le32_to_cpu(bucket_xh(target)->xh_entries[0].xe_name_hash);
Joel Beckered29c0c2008-11-26 15:08:44 -08005279 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08005280 int ret, num_buckets, extend = 1;
5281 u64 p_blkno;
5282 u32 e_cpos, num_clusters;
Joel Becker92de1092008-11-25 17:06:40 -08005283 /* The bucket at the front of the extent */
Joel Becker91f20332008-11-26 15:25:41 -08005284 struct ocfs2_xattr_bucket *first;
Tao Ma01225592008-08-18 17:38:53 +08005285
Joel Becker91f20332008-11-26 15:25:41 -08005286 mlog(0, "Add new xattr bucket starting from %llu\n",
5287 (unsigned long long)bucket_blkno(target));
Tao Ma01225592008-08-18 17:38:53 +08005288
Joel Beckered29c0c2008-11-26 15:08:44 -08005289 /* The first bucket of the original extent */
Joel Becker92de1092008-11-25 17:06:40 -08005290 first = ocfs2_xattr_bucket_new(inode);
Joel Becker91f20332008-11-26 15:25:41 -08005291 if (!first) {
Joel Becker92de1092008-11-25 17:06:40 -08005292 ret = -ENOMEM;
5293 mlog_errno(ret);
5294 goto out;
5295 }
5296
Tao Ma01225592008-08-18 17:38:53 +08005297 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
5298 &num_clusters, el);
5299 if (ret) {
5300 mlog_errno(ret);
5301 goto out;
5302 }
5303
Joel Beckered29c0c2008-11-26 15:08:44 -08005304 ret = ocfs2_read_xattr_bucket(first, p_blkno);
5305 if (ret) {
5306 mlog_errno(ret);
5307 goto out;
5308 }
5309
Tao Ma01225592008-08-18 17:38:53 +08005310 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
Joel Beckered29c0c2008-11-26 15:08:44 -08005311 if (num_buckets == le16_to_cpu(bucket_xh(first)->xh_num_buckets)) {
5312 /*
5313 * This can move first+target if the target bucket moves
5314 * to the new extent.
5315 */
Tao Ma01225592008-08-18 17:38:53 +08005316 ret = ocfs2_add_new_xattr_cluster(inode,
5317 xb_bh,
Joel Beckered29c0c2008-11-26 15:08:44 -08005318 first,
5319 target,
Tao Ma01225592008-08-18 17:38:53 +08005320 &num_clusters,
5321 e_cpos,
Tao Ma78f30c32008-11-12 08:27:00 +08005322 &extend,
5323 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005324 if (ret) {
5325 mlog_errno(ret);
5326 goto out;
5327 }
5328 }
5329
Joel Becker92de1092008-11-25 17:06:40 -08005330 if (extend) {
Tao Ma01225592008-08-18 17:38:53 +08005331 ret = ocfs2_extend_xattr_bucket(inode,
Tao Ma85db90e2008-11-12 08:27:01 +08005332 ctxt->handle,
Joel Beckered29c0c2008-11-26 15:08:44 -08005333 first,
5334 bucket_blkno(target),
Tao Ma01225592008-08-18 17:38:53 +08005335 num_clusters);
Joel Becker92de1092008-11-25 17:06:40 -08005336 if (ret)
5337 mlog_errno(ret);
5338 }
5339
Tao Ma01225592008-08-18 17:38:53 +08005340out:
Joel Becker92de1092008-11-25 17:06:40 -08005341 ocfs2_xattr_bucket_free(first);
Joel Beckered29c0c2008-11-26 15:08:44 -08005342
Tao Ma01225592008-08-18 17:38:53 +08005343 return ret;
5344}
5345
5346static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
5347 struct ocfs2_xattr_bucket *bucket,
5348 int offs)
5349{
5350 int block_off = offs >> inode->i_sb->s_blocksize_bits;
5351
5352 offs = offs % inode->i_sb->s_blocksize;
Joel Becker51def392008-10-24 16:57:21 -07005353 return bucket_block(bucket, block_off) + offs;
Tao Ma01225592008-08-18 17:38:53 +08005354}
5355
5356/*
Tao Ma01225592008-08-18 17:38:53 +08005357 * Set the xattr entry in the specified bucket.
5358 * The bucket is indicated by xs->bucket and it should have the enough
5359 * space for the xattr insertion.
5360 */
5361static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08005362 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08005363 struct ocfs2_xattr_info *xi,
5364 struct ocfs2_xattr_search *xs,
Joel Becker9dc47402009-08-17 19:56:01 -07005365 u32 name_hash)
Tao Ma01225592008-08-18 17:38:53 +08005366{
Joel Becker1224be02008-10-24 18:47:33 -07005367 int ret;
Joel Becker02dbf382008-10-27 18:07:45 -07005368 u64 blkno;
Joel Becker69a3e532009-08-17 12:24:39 -07005369 struct ocfs2_xa_loc loc;
Tao Ma01225592008-08-18 17:38:53 +08005370
Mark Fashehff1ec202008-08-19 10:54:29 -07005371 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
Joel Becker6b240ff2009-08-14 18:02:52 -07005372 (unsigned long)xi->xi_value_len, xi->xi_name_index,
Joel Beckerba937122008-10-24 19:13:20 -07005373 (unsigned long long)bucket_blkno(xs->bucket));
Tao Ma01225592008-08-18 17:38:53 +08005374
Joel Beckerba937122008-10-24 19:13:20 -07005375 if (!xs->bucket->bu_bhs[1]) {
Joel Becker02dbf382008-10-27 18:07:45 -07005376 blkno = bucket_blkno(xs->bucket);
5377 ocfs2_xattr_bucket_relse(xs->bucket);
5378 ret = ocfs2_read_xattr_bucket(xs->bucket, blkno);
Tao Ma01225592008-08-18 17:38:53 +08005379 if (ret) {
5380 mlog_errno(ret);
5381 goto out;
5382 }
5383 }
5384
Joel Beckercf2bc802009-08-18 13:52:38 -07005385 ocfs2_init_xattr_bucket_xa_loc(&loc, xs->bucket,
5386 xs->not_found ? NULL : xs->here);
5387 ret = ocfs2_xa_journal_access(handle, &loc,
5388 OCFS2_JOURNAL_ACCESS_WRITE);
Joel Becker1224be02008-10-24 18:47:33 -07005389 if (ret < 0) {
5390 mlog_errno(ret);
5391 goto out;
Tao Ma01225592008-08-18 17:38:53 +08005392 }
5393
Joel Becker69a3e532009-08-17 12:24:39 -07005394 ret = ocfs2_xa_prepare_entry(&loc, xi, name_hash);
5395 if (ret) {
5396 if (ret != -ENOSPC)
5397 mlog_errno(ret);
5398 goto out;
5399 }
5400 /* XXX For now, until we make ocfs2_xa_prepare_entry() primary */
5401 BUG_ON(ret == -ENOSPC);
5402 ocfs2_xa_store_inline_value(&loc, xi);
5403 xs->here = loc.xl_entry;
5404
Joel Beckercf2bc802009-08-18 13:52:38 -07005405 ocfs2_xa_journal_dirty(handle, &loc);
Tao Ma01225592008-08-18 17:38:53 +08005406
Tao Ma01225592008-08-18 17:38:53 +08005407out:
Tao Ma01225592008-08-18 17:38:53 +08005408 return ret;
5409}
5410
Tao Ma01225592008-08-18 17:38:53 +08005411/*
5412 * Truncate the specified xe_off entry in xattr bucket.
5413 * bucket is indicated by header_bh and len is the new length.
5414 * Both the ocfs2_xattr_value_root and the entry will be updated here.
5415 *
5416 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
5417 */
5418static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
Joel Becker548b0f22008-11-24 19:32:13 -08005419 struct ocfs2_xattr_bucket *bucket,
Tao Ma01225592008-08-18 17:38:53 +08005420 int xe_off,
Tao Ma78f30c32008-11-12 08:27:00 +08005421 int len,
5422 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005423{
5424 int ret, offset;
5425 u64 value_blk;
Tao Ma01225592008-08-18 17:38:53 +08005426 struct ocfs2_xattr_entry *xe;
Joel Becker548b0f22008-11-24 19:32:13 -08005427 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma01225592008-08-18 17:38:53 +08005428 size_t blocksize = inode->i_sb->s_blocksize;
Joel Beckerb3e5d372008-12-09 15:01:04 -08005429 struct ocfs2_xattr_value_buf vb = {
5430 .vb_access = ocfs2_journal_access,
5431 };
Tao Ma01225592008-08-18 17:38:53 +08005432
5433 xe = &xh->xh_entries[xe_off];
5434
5435 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
5436
5437 offset = le16_to_cpu(xe->xe_name_offset) +
5438 OCFS2_XATTR_SIZE(xe->xe_name_len);
5439
5440 value_blk = offset / blocksize;
5441
5442 /* We don't allow ocfs2_xattr_value to be stored in different block. */
5443 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
Tao Ma01225592008-08-18 17:38:53 +08005444
Joel Beckerb3e5d372008-12-09 15:01:04 -08005445 vb.vb_bh = bucket->bu_bhs[value_blk];
5446 BUG_ON(!vb.vb_bh);
Tao Ma01225592008-08-18 17:38:53 +08005447
Joel Beckerb3e5d372008-12-09 15:01:04 -08005448 vb.vb_xv = (struct ocfs2_xattr_value_root *)
5449 (vb.vb_bh->b_data + offset % blocksize);
Tao Ma01225592008-08-18 17:38:53 +08005450
Joel Becker548b0f22008-11-24 19:32:13 -08005451 /*
5452 * From here on out we have to dirty the bucket. The generic
5453 * value calls only modify one of the bucket's bhs, but we need
5454 * to send the bucket at once. So if they error, they *could* have
5455 * modified something. We have to assume they did, and dirty
5456 * the whole bucket. This leaves us in a consistent state.
5457 */
Tao Ma01225592008-08-18 17:38:53 +08005458 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
Joel Becker548b0f22008-11-24 19:32:13 -08005459 xe_off, (unsigned long long)bucket_blkno(bucket), len);
Joel Beckerb3e5d372008-12-09 15:01:04 -08005460 ret = ocfs2_xattr_value_truncate(inode, &vb, len, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005461 if (ret) {
5462 mlog_errno(ret);
Tao Ma554e7f92009-01-08 08:21:43 +08005463 goto out;
5464 }
5465
5466 ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
5467 OCFS2_JOURNAL_ACCESS_WRITE);
5468 if (ret) {
5469 mlog_errno(ret);
5470 goto out;
Tao Ma01225592008-08-18 17:38:53 +08005471 }
5472
Joel Becker548b0f22008-11-24 19:32:13 -08005473 xe->xe_value_size = cpu_to_le64(len);
5474
Joel Becker548b0f22008-11-24 19:32:13 -08005475 ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket);
Tao Ma01225592008-08-18 17:38:53 +08005476
5477out:
Tao Ma01225592008-08-18 17:38:53 +08005478 return ret;
5479}
5480
5481static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +08005482 struct ocfs2_xattr_search *xs,
5483 int len,
5484 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005485{
5486 int ret, offset;
5487 struct ocfs2_xattr_entry *xe = xs->here;
5488 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
5489
Joel Beckerba937122008-10-24 19:13:20 -07005490 BUG_ON(!xs->bucket->bu_bhs[0] || !xe || ocfs2_xattr_is_local(xe));
Tao Ma01225592008-08-18 17:38:53 +08005491
5492 offset = xe - xh->xh_entries;
Joel Becker548b0f22008-11-24 19:32:13 -08005493 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket,
Tao Ma78f30c32008-11-12 08:27:00 +08005494 offset, len, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005495 if (ret)
5496 mlog_errno(ret);
5497
5498 return ret;
5499}
5500
5501static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08005502 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08005503 struct ocfs2_xattr_search *xs,
5504 char *val,
5505 int value_len)
5506{
Tao Ma712e53e2009-03-12 08:37:34 +08005507 int ret, offset, block_off;
Tao Ma01225592008-08-18 17:38:53 +08005508 struct ocfs2_xattr_value_root *xv;
5509 struct ocfs2_xattr_entry *xe = xs->here;
Tao Ma712e53e2009-03-12 08:37:34 +08005510 struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
5511 void *base;
Tao Ma492a8a32009-08-18 11:43:17 +08005512 struct ocfs2_xattr_value_buf vb = {
5513 .vb_access = ocfs2_journal_access,
5514 };
Tao Ma01225592008-08-18 17:38:53 +08005515
5516 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
5517
Tao Mafd68a892009-08-18 11:43:21 +08005518 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb, xh,
Tao Ma712e53e2009-03-12 08:37:34 +08005519 xe - xh->xh_entries,
5520 &block_off,
5521 &offset);
5522 if (ret) {
5523 mlog_errno(ret);
5524 goto out;
5525 }
Tao Ma01225592008-08-18 17:38:53 +08005526
Tao Ma712e53e2009-03-12 08:37:34 +08005527 base = bucket_block(xs->bucket, block_off);
5528 xv = (struct ocfs2_xattr_value_root *)(base + offset +
5529 OCFS2_XATTR_SIZE(xe->xe_name_len));
Tao Ma01225592008-08-18 17:38:53 +08005530
Tao Ma492a8a32009-08-18 11:43:17 +08005531 vb.vb_xv = xv;
5532 vb.vb_bh = xs->bucket->bu_bhs[block_off];
Tao Ma712e53e2009-03-12 08:37:34 +08005533 ret = __ocfs2_xattr_set_value_outside(inode, handle,
Tao Ma492a8a32009-08-18 11:43:17 +08005534 &vb, val, value_len);
Tao Ma712e53e2009-03-12 08:37:34 +08005535 if (ret)
5536 mlog_errno(ret);
5537out:
5538 return ret;
Tao Ma01225592008-08-18 17:38:53 +08005539}
5540
Tao Ma01225592008-08-18 17:38:53 +08005541static int ocfs2_rm_xattr_cluster(struct inode *inode,
5542 struct buffer_head *root_bh,
5543 u64 blkno,
5544 u32 cpos,
Tao Ma47bca492009-08-18 11:43:42 +08005545 u32 len,
5546 void *para)
Tao Ma01225592008-08-18 17:38:53 +08005547{
5548 int ret;
5549 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5550 struct inode *tl_inode = osb->osb_tl_inode;
5551 handle_t *handle;
5552 struct ocfs2_xattr_block *xb =
5553 (struct ocfs2_xattr_block *)root_bh->b_data;
Tao Ma01225592008-08-18 17:38:53 +08005554 struct ocfs2_alloc_context *meta_ac = NULL;
5555 struct ocfs2_cached_dealloc_ctxt dealloc;
Joel Beckerf99b9b72008-08-20 19:36:33 -07005556 struct ocfs2_extent_tree et;
5557
Tao Ma47bca492009-08-18 11:43:42 +08005558 ret = ocfs2_iterate_xattr_buckets(inode, blkno, len,
Tao Mace9c5a52009-08-18 11:43:59 +08005559 ocfs2_delete_xattr_in_bucket, para);
Tao Ma47bca492009-08-18 11:43:42 +08005560 if (ret) {
5561 mlog_errno(ret);
5562 return ret;
5563 }
5564
Joel Becker5e404e92009-02-13 03:54:22 -08005565 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
Tao Ma01225592008-08-18 17:38:53 +08005566
5567 ocfs2_init_dealloc_ctxt(&dealloc);
5568
5569 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
5570 cpos, len, (unsigned long long)blkno);
5571
Joel Becker8cb471e2009-02-10 20:00:41 -08005572 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode), blkno,
5573 len);
Tao Ma01225592008-08-18 17:38:53 +08005574
Joel Beckerf99b9b72008-08-20 19:36:33 -07005575 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08005576 if (ret) {
5577 mlog_errno(ret);
5578 return ret;
5579 }
5580
5581 mutex_lock(&tl_inode->i_mutex);
5582
5583 if (ocfs2_truncate_log_needs_flush(osb)) {
5584 ret = __ocfs2_flush_truncate_log(osb);
5585 if (ret < 0) {
5586 mlog_errno(ret);
5587 goto out;
5588 }
5589 }
5590
Jan Karaa90714c2008-10-09 19:38:40 +02005591 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
Tao Mad3264792008-10-24 07:57:28 +08005592 if (IS_ERR(handle)) {
Tao Ma01225592008-08-18 17:38:53 +08005593 ret = -ENOMEM;
5594 mlog_errno(ret);
5595 goto out;
5596 }
5597
Joel Becker0cf2f762009-02-12 16:41:25 -08005598 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
Joel Becker84008972008-12-09 16:11:49 -08005599 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08005600 if (ret) {
5601 mlog_errno(ret);
5602 goto out_commit;
5603 }
5604
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005605 ret = ocfs2_remove_extent(handle, &et, cpos, len, meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005606 &dealloc);
Tao Ma01225592008-08-18 17:38:53 +08005607 if (ret) {
5608 mlog_errno(ret);
5609 goto out_commit;
5610 }
5611
5612 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
5613
5614 ret = ocfs2_journal_dirty(handle, root_bh);
5615 if (ret) {
5616 mlog_errno(ret);
5617 goto out_commit;
5618 }
5619
5620 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
5621 if (ret)
5622 mlog_errno(ret);
5623
5624out_commit:
5625 ocfs2_commit_trans(osb, handle);
5626out:
5627 ocfs2_schedule_truncate_log_flush(osb, 1);
5628
5629 mutex_unlock(&tl_inode->i_mutex);
5630
5631 if (meta_ac)
5632 ocfs2_free_alloc_context(meta_ac);
5633
5634 ocfs2_run_deallocs(osb, &dealloc);
5635
5636 return ret;
5637}
5638
Tao Ma01225592008-08-18 17:38:53 +08005639static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08005640 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08005641 struct ocfs2_xattr_search *xs)
5642{
Joel Beckerba937122008-10-24 19:13:20 -07005643 struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08005644 struct ocfs2_xattr_entry *last = &xh->xh_entries[
5645 le16_to_cpu(xh->xh_count) - 1];
5646 int ret = 0;
5647
Joel Beckerba937122008-10-24 19:13:20 -07005648 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
Joel Becker1224be02008-10-24 18:47:33 -07005649 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08005650 if (ret) {
5651 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08005652 return;
Tao Ma01225592008-08-18 17:38:53 +08005653 }
5654
5655 /* Remove the old entry. */
5656 memmove(xs->here, xs->here + 1,
5657 (void *)last - (void *)xs->here);
5658 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
5659 le16_add_cpu(&xh->xh_count, -1);
5660
Joel Beckerba937122008-10-24 19:13:20 -07005661 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08005662}
5663
5664/*
5665 * Set the xattr name/value in the bucket specified in xs.
5666 *
5667 * As the new value in xi may be stored in the bucket or in an outside cluster,
5668 * we divide the whole process into 3 steps:
5669 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
5670 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
5671 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
5672 * 4. If the clusters for the new outside value can't be allocated, we need
5673 * to free the xattr we allocated in set.
5674 */
5675static int ocfs2_xattr_set_in_bucket(struct inode *inode,
5676 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08005677 struct ocfs2_xattr_search *xs,
5678 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005679{
Joel Becker9dc47402009-08-17 19:56:01 -07005680 int ret;
Tao Ma01225592008-08-18 17:38:53 +08005681 size_t value_len;
Joel Becker6b240ff2009-08-14 18:02:52 -07005682 char *val = (char *)xi->xi_value;
Tao Ma01225592008-08-18 17:38:53 +08005683 struct ocfs2_xattr_entry *xe = xs->here;
Joel Becker6b240ff2009-08-14 18:02:52 -07005684 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->xi_name,
Joel Becker18853b92009-08-14 18:17:07 -07005685 xi->xi_name_len);
Tao Ma01225592008-08-18 17:38:53 +08005686
Joel Becker9dc47402009-08-17 19:56:01 -07005687 value_len = xi->xi_value_len;
Tao Ma01225592008-08-18 17:38:53 +08005688 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
5689 /*
5690 * We need to truncate the xattr storage first.
5691 *
5692 * If both the old and new value are stored to
5693 * outside block, we only need to truncate
5694 * the storage and then set the value outside.
5695 *
5696 * If the new value should be stored within block,
5697 * we should free all the outside block first and
5698 * the modification to the xattr block will be done
5699 * by following steps.
5700 */
Joel Becker9dc47402009-08-17 19:56:01 -07005701 if (xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE)
Tao Ma01225592008-08-18 17:38:53 +08005702 value_len = 0;
5703
5704 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
Tao Ma78f30c32008-11-12 08:27:00 +08005705 value_len,
5706 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005707 if (ret)
5708 goto out;
5709
5710 if (value_len)
5711 goto set_value_outside;
5712 }
5713
Tao Ma01225592008-08-18 17:38:53 +08005714 /* So we have to handle the inside block change now. */
Tao Ma85db90e2008-11-12 08:27:01 +08005715 ret = ocfs2_xattr_set_entry_in_bucket(inode, ctxt->handle, xi, xs,
Joel Becker9dc47402009-08-17 19:56:01 -07005716 name_hash);
Tao Ma01225592008-08-18 17:38:53 +08005717 if (ret) {
5718 mlog_errno(ret);
5719 goto out;
5720 }
5721
Joel Becker9dc47402009-08-17 19:56:01 -07005722 if (xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE)
Tao Ma5a095612008-09-19 22:17:41 +08005723 goto out;
Tao Ma01225592008-08-18 17:38:53 +08005724
Tao Ma5a095612008-09-19 22:17:41 +08005725 /* allocate the space now for the outside block storage. */
5726 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
Tao Ma78f30c32008-11-12 08:27:00 +08005727 value_len, ctxt);
Tao Ma5a095612008-09-19 22:17:41 +08005728 if (ret) {
5729 mlog_errno(ret);
5730
5731 if (xs->not_found) {
5732 /*
5733 * We can't allocate enough clusters for outside
5734 * storage and we have allocated xattr already,
5735 * so need to remove it.
5736 */
Tao Ma85db90e2008-11-12 08:27:01 +08005737 ocfs2_xattr_bucket_remove_xs(inode, ctxt->handle, xs);
Tao Ma01225592008-08-18 17:38:53 +08005738 }
Tao Ma01225592008-08-18 17:38:53 +08005739 goto out;
5740 }
5741
5742set_value_outside:
Tao Ma85db90e2008-11-12 08:27:01 +08005743 ret = ocfs2_xattr_bucket_set_value_outside(inode, ctxt->handle,
5744 xs, val, value_len);
Tao Ma01225592008-08-18 17:38:53 +08005745out:
5746 return ret;
5747}
5748
Tao Ma80bcaf32008-10-27 06:06:24 +08005749/*
5750 * check whether the xattr bucket is filled up with the same hash value.
5751 * If we want to insert the xattr with the same hash, return -ENOSPC.
5752 * If we want to insert a xattr with different hash value, go ahead
5753 * and ocfs2_divide_xattr_bucket will handle this.
5754 */
Tao Ma01225592008-08-18 17:38:53 +08005755static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
Tao Ma80bcaf32008-10-27 06:06:24 +08005756 struct ocfs2_xattr_bucket *bucket,
5757 const char *name)
Tao Ma01225592008-08-18 17:38:53 +08005758{
Joel Becker3e632942008-10-24 17:04:49 -07005759 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08005760 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
5761
5762 if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
5763 return 0;
Tao Ma01225592008-08-18 17:38:53 +08005764
5765 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
5766 xh->xh_entries[0].xe_name_hash) {
5767 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
5768 "hash = %u\n",
Joel Becker9c7759a2008-10-24 16:21:03 -07005769 (unsigned long long)bucket_blkno(bucket),
Tao Ma01225592008-08-18 17:38:53 +08005770 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
5771 return -ENOSPC;
5772 }
5773
5774 return 0;
5775}
5776
5777static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
5778 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08005779 struct ocfs2_xattr_search *xs,
5780 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005781{
5782 struct ocfs2_xattr_header *xh;
5783 struct ocfs2_xattr_entry *xe;
5784 u16 count, header_size, xh_free_start;
Joel Becker6dde41d2008-10-24 17:16:48 -07005785 int free, max_free, need, old;
Joel Becker18853b92009-08-14 18:17:07 -07005786 size_t value_size = 0;
Tao Ma01225592008-08-18 17:38:53 +08005787 size_t blocksize = inode->i_sb->s_blocksize;
5788 int ret, allocation = 0;
Tao Ma01225592008-08-18 17:38:53 +08005789
Joel Becker6b240ff2009-08-14 18:02:52 -07005790 mlog_entry("Set xattr %s in xattr index block\n", xi->xi_name);
Tao Ma01225592008-08-18 17:38:53 +08005791
5792try_again:
5793 xh = xs->header;
5794 count = le16_to_cpu(xh->xh_count);
5795 xh_free_start = le16_to_cpu(xh->xh_free_start);
5796 header_size = sizeof(struct ocfs2_xattr_header) +
5797 count * sizeof(struct ocfs2_xattr_entry);
Tiger Yang4442f512009-02-20 11:11:50 +08005798 max_free = OCFS2_XATTR_BUCKET_SIZE - header_size -
5799 le16_to_cpu(xh->xh_name_value_len) - OCFS2_XATTR_HEADER_GAP;
Tao Ma01225592008-08-18 17:38:53 +08005800
5801 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
5802 "of %u which exceed block size\n",
Joel Beckerba937122008-10-24 19:13:20 -07005803 (unsigned long long)bucket_blkno(xs->bucket),
Tao Ma01225592008-08-18 17:38:53 +08005804 header_size);
5805
Joel Becker6b240ff2009-08-14 18:02:52 -07005806 if (xi->xi_value && xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
Tao Ma01225592008-08-18 17:38:53 +08005807 value_size = OCFS2_XATTR_ROOT_SIZE;
Joel Becker6b240ff2009-08-14 18:02:52 -07005808 else if (xi->xi_value)
5809 value_size = OCFS2_XATTR_SIZE(xi->xi_value_len);
Tao Ma01225592008-08-18 17:38:53 +08005810
5811 if (xs->not_found)
5812 need = sizeof(struct ocfs2_xattr_entry) +
Joel Becker18853b92009-08-14 18:17:07 -07005813 OCFS2_XATTR_SIZE(xi->xi_name_len) + value_size;
Tao Ma01225592008-08-18 17:38:53 +08005814 else {
Joel Becker18853b92009-08-14 18:17:07 -07005815 need = value_size + OCFS2_XATTR_SIZE(xi->xi_name_len);
Tao Ma01225592008-08-18 17:38:53 +08005816
5817 /*
5818 * We only replace the old value if the new length is smaller
5819 * than the old one. Otherwise we will allocate new space in the
5820 * bucket to store it.
5821 */
5822 xe = xs->here;
5823 if (ocfs2_xattr_is_local(xe))
5824 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
5825 else
5826 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
5827
5828 if (old >= value_size)
5829 need = 0;
5830 }
5831
Tiger Yang4442f512009-02-20 11:11:50 +08005832 free = xh_free_start - header_size - OCFS2_XATTR_HEADER_GAP;
Tao Ma01225592008-08-18 17:38:53 +08005833 /*
5834 * We need to make sure the new name/value pair
5835 * can exist in the same block.
5836 */
5837 if (xh_free_start % blocksize < need)
5838 free -= xh_free_start % blocksize;
5839
5840 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
5841 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
5842 " %u\n", xs->not_found,
Joel Beckerba937122008-10-24 19:13:20 -07005843 (unsigned long long)bucket_blkno(xs->bucket),
Tao Ma01225592008-08-18 17:38:53 +08005844 free, need, max_free, le16_to_cpu(xh->xh_free_start),
5845 le16_to_cpu(xh->xh_name_value_len));
5846
Tao Ma976331d2008-11-12 08:26:57 +08005847 if (free < need ||
5848 (xs->not_found &&
5849 count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb))) {
Tao Ma01225592008-08-18 17:38:53 +08005850 if (need <= max_free &&
5851 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
5852 /*
5853 * We can create the space by defragment. Since only the
5854 * name/value will be moved, the xe shouldn't be changed
5855 * in xs.
5856 */
Tao Ma85db90e2008-11-12 08:27:01 +08005857 ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
5858 xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08005859 if (ret) {
5860 mlog_errno(ret);
5861 goto out;
5862 }
5863
5864 xh_free_start = le16_to_cpu(xh->xh_free_start);
Tiger Yang4442f512009-02-20 11:11:50 +08005865 free = xh_free_start - header_size
5866 - OCFS2_XATTR_HEADER_GAP;
Tao Ma01225592008-08-18 17:38:53 +08005867 if (xh_free_start % blocksize < need)
5868 free -= xh_free_start % blocksize;
5869
5870 if (free >= need)
5871 goto xattr_set;
5872
5873 mlog(0, "Can't get enough space for xattr insert by "
5874 "defragment. Need %u bytes, but we have %d, so "
5875 "allocate new bucket for it.\n", need, free);
5876 }
5877
5878 /*
5879 * We have to add new buckets or clusters and one
5880 * allocation should leave us enough space for insert.
5881 */
5882 BUG_ON(allocation);
5883
5884 /*
5885 * We do not allow for overlapping ranges between buckets. And
5886 * the maximum number of collisions we will allow for then is
5887 * one bucket's worth, so check it here whether we need to
5888 * add a new bucket for the insert.
5889 */
Tao Ma80bcaf32008-10-27 06:06:24 +08005890 ret = ocfs2_check_xattr_bucket_collision(inode,
Joel Beckerba937122008-10-24 19:13:20 -07005891 xs->bucket,
Joel Becker6b240ff2009-08-14 18:02:52 -07005892 xi->xi_name);
Tao Ma01225592008-08-18 17:38:53 +08005893 if (ret) {
5894 mlog_errno(ret);
5895 goto out;
5896 }
5897
5898 ret = ocfs2_add_new_xattr_bucket(inode,
5899 xs->xattr_bh,
Joel Becker91f20332008-11-26 15:25:41 -08005900 xs->bucket,
Tao Ma78f30c32008-11-12 08:27:00 +08005901 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005902 if (ret) {
5903 mlog_errno(ret);
5904 goto out;
5905 }
5906
Joel Becker91f20332008-11-26 15:25:41 -08005907 /*
5908 * ocfs2_add_new_xattr_bucket() will have updated
5909 * xs->bucket if it moved, but it will not have updated
5910 * any of the other search fields. Thus, we drop it and
5911 * re-search. Everything should be cached, so it'll be
5912 * quick.
5913 */
Joel Beckerba937122008-10-24 19:13:20 -07005914 ocfs2_xattr_bucket_relse(xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08005915 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
Joel Becker6b240ff2009-08-14 18:02:52 -07005916 xi->xi_name_index,
5917 xi->xi_name, xs);
Tao Ma01225592008-08-18 17:38:53 +08005918 if (ret && ret != -ENODATA)
5919 goto out;
5920 xs->not_found = ret;
5921 allocation = 1;
5922 goto try_again;
5923 }
5924
5925xattr_set:
Tao Ma78f30c32008-11-12 08:27:00 +08005926 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005927out:
5928 mlog_exit(ret);
5929 return ret;
5930}
Tao Maa3944252008-08-18 17:38:54 +08005931
5932static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
5933 struct ocfs2_xattr_bucket *bucket,
5934 void *para)
5935{
Tao Mace9c5a52009-08-18 11:43:59 +08005936 int ret = 0, ref_credits;
Joel Becker3e632942008-10-24 17:04:49 -07005937 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Maa3944252008-08-18 17:38:54 +08005938 u16 i;
5939 struct ocfs2_xattr_entry *xe;
Tao Ma78f30c32008-11-12 08:27:00 +08005940 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5941 struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
Joel Becker548b0f22008-11-24 19:32:13 -08005942 int credits = ocfs2_remove_extent_credits(osb->sb) +
5943 ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Mace9c5a52009-08-18 11:43:59 +08005944 struct ocfs2_xattr_value_root *xv;
5945 struct ocfs2_rm_xattr_bucket_para *args =
5946 (struct ocfs2_rm_xattr_bucket_para *)para;
Tao Ma78f30c32008-11-12 08:27:00 +08005947
5948 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
Tao Maa3944252008-08-18 17:38:54 +08005949
5950 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
5951 xe = &xh->xh_entries[i];
5952 if (ocfs2_xattr_is_local(xe))
5953 continue;
5954
Tao Mace9c5a52009-08-18 11:43:59 +08005955 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket,
5956 i, &xv, NULL);
5957
5958 ret = ocfs2_lock_xattr_remove_allocators(inode, xv,
5959 args->ref_ci,
5960 args->ref_root_bh,
5961 &ctxt.meta_ac,
5962 &ref_credits);
5963
5964 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
Tao Ma88c3b062008-12-11 08:54:11 +08005965 if (IS_ERR(ctxt.handle)) {
5966 ret = PTR_ERR(ctxt.handle);
5967 mlog_errno(ret);
5968 break;
5969 }
5970
Joel Becker548b0f22008-11-24 19:32:13 -08005971 ret = ocfs2_xattr_bucket_value_truncate(inode, bucket,
Tao Ma78f30c32008-11-12 08:27:00 +08005972 i, 0, &ctxt);
Tao Ma88c3b062008-12-11 08:54:11 +08005973
5974 ocfs2_commit_trans(osb, ctxt.handle);
Tao Mace9c5a52009-08-18 11:43:59 +08005975 if (ctxt.meta_ac) {
5976 ocfs2_free_alloc_context(ctxt.meta_ac);
5977 ctxt.meta_ac = NULL;
5978 }
Tao Maa3944252008-08-18 17:38:54 +08005979 if (ret) {
5980 mlog_errno(ret);
5981 break;
5982 }
5983 }
5984
Tao Mace9c5a52009-08-18 11:43:59 +08005985 if (ctxt.meta_ac)
5986 ocfs2_free_alloc_context(ctxt.meta_ac);
Tao Ma78f30c32008-11-12 08:27:00 +08005987 ocfs2_schedule_truncate_log_flush(osb, 1);
5988 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tao Maa3944252008-08-18 17:38:54 +08005989 return ret;
5990}
5991
Mark Fasheh99219ae2008-10-07 14:52:59 -07005992/*
Tao Ma492a8a32009-08-18 11:43:17 +08005993 * Whenever we modify a xattr value root in the bucket(e.g, CoW
5994 * or change the extent record flag), we need to recalculate
5995 * the metaecc for the whole bucket. So it is done here.
5996 *
5997 * Note:
5998 * We have to give the extra credits for the caller.
5999 */
6000static int ocfs2_xattr_bucket_post_refcount(struct inode *inode,
6001 handle_t *handle,
6002 void *para)
6003{
6004 int ret;
6005 struct ocfs2_xattr_bucket *bucket =
6006 (struct ocfs2_xattr_bucket *)para;
6007
6008 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
6009 OCFS2_JOURNAL_ACCESS_WRITE);
6010 if (ret) {
6011 mlog_errno(ret);
6012 return ret;
6013 }
6014
6015 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
6016
6017 return 0;
6018}
6019
6020/*
6021 * Special action we need if the xattr value is refcounted.
6022 *
6023 * 1. If the xattr is refcounted, lock the tree.
6024 * 2. CoW the xattr if we are setting the new value and the value
6025 * will be stored outside.
6026 * 3. In other case, decrease_refcount will work for us, so just
6027 * lock the refcount tree, calculate the meta and credits is OK.
6028 *
6029 * We have to do CoW before ocfs2_init_xattr_set_ctxt since
6030 * currently CoW is a completed transaction, while this function
6031 * will also lock the allocators and let us deadlock. So we will
6032 * CoW the whole xattr value.
6033 */
6034static int ocfs2_prepare_refcount_xattr(struct inode *inode,
6035 struct ocfs2_dinode *di,
6036 struct ocfs2_xattr_info *xi,
6037 struct ocfs2_xattr_search *xis,
6038 struct ocfs2_xattr_search *xbs,
6039 struct ocfs2_refcount_tree **ref_tree,
6040 int *meta_add,
6041 int *credits)
6042{
6043 int ret = 0;
6044 struct ocfs2_xattr_block *xb;
6045 struct ocfs2_xattr_entry *xe;
6046 char *base;
6047 u32 p_cluster, num_clusters;
6048 unsigned int ext_flags;
6049 int name_offset, name_len;
6050 struct ocfs2_xattr_value_buf vb;
6051 struct ocfs2_xattr_bucket *bucket = NULL;
6052 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6053 struct ocfs2_post_refcount refcount;
6054 struct ocfs2_post_refcount *p = NULL;
6055 struct buffer_head *ref_root_bh = NULL;
6056
6057 if (!xis->not_found) {
6058 xe = xis->here;
6059 name_offset = le16_to_cpu(xe->xe_name_offset);
6060 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
6061 base = xis->base;
6062 vb.vb_bh = xis->inode_bh;
6063 vb.vb_access = ocfs2_journal_access_di;
6064 } else {
6065 int i, block_off = 0;
6066 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
6067 xe = xbs->here;
6068 name_offset = le16_to_cpu(xe->xe_name_offset);
6069 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
6070 i = xbs->here - xbs->header->xh_entries;
6071
6072 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
Tao Mafd68a892009-08-18 11:43:21 +08006073 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Tao Ma492a8a32009-08-18 11:43:17 +08006074 bucket_xh(xbs->bucket),
6075 i, &block_off,
6076 &name_offset);
6077 if (ret) {
6078 mlog_errno(ret);
6079 goto out;
6080 }
6081 base = bucket_block(xbs->bucket, block_off);
6082 vb.vb_bh = xbs->bucket->bu_bhs[block_off];
6083 vb.vb_access = ocfs2_journal_access;
6084
6085 if (ocfs2_meta_ecc(osb)) {
6086 /*create parameters for ocfs2_post_refcount. */
6087 bucket = xbs->bucket;
6088 refcount.credits = bucket->bu_blocks;
6089 refcount.para = bucket;
6090 refcount.func =
6091 ocfs2_xattr_bucket_post_refcount;
6092 p = &refcount;
6093 }
6094 } else {
6095 base = xbs->base;
6096 vb.vb_bh = xbs->xattr_bh;
6097 vb.vb_access = ocfs2_journal_access_xb;
6098 }
6099 }
6100
6101 if (ocfs2_xattr_is_local(xe))
6102 goto out;
6103
6104 vb.vb_xv = (struct ocfs2_xattr_value_root *)
6105 (base + name_offset + name_len);
6106
6107 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
6108 &num_clusters, &vb.vb_xv->xr_list,
6109 &ext_flags);
6110 if (ret) {
6111 mlog_errno(ret);
6112 goto out;
6113 }
6114
6115 /*
6116 * We just need to check the 1st extent record, since we always
6117 * CoW the whole xattr. So there shouldn't be a xattr with
6118 * some REFCOUNT extent recs after the 1st one.
6119 */
6120 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
6121 goto out;
6122
6123 ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
6124 1, ref_tree, &ref_root_bh);
6125 if (ret) {
6126 mlog_errno(ret);
6127 goto out;
6128 }
6129
6130 /*
6131 * If we are deleting the xattr or the new size will be stored inside,
6132 * cool, leave it there, the xattr truncate process will remove them
6133 * for us(it still needs the refcount tree lock and the meta, credits).
6134 * And the worse case is that every cluster truncate will split the
6135 * refcount tree, and make the original extent become 3. So we will need
6136 * 2 * cluster more extent recs at most.
6137 */
Joel Becker6b240ff2009-08-14 18:02:52 -07006138 if (!xi->xi_value || xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE) {
Tao Ma492a8a32009-08-18 11:43:17 +08006139
6140 ret = ocfs2_refcounted_xattr_delete_need(inode,
6141 &(*ref_tree)->rf_ci,
6142 ref_root_bh, vb.vb_xv,
6143 meta_add, credits);
6144 if (ret)
6145 mlog_errno(ret);
6146 goto out;
6147 }
6148
6149 ret = ocfs2_refcount_cow_xattr(inode, di, &vb,
6150 *ref_tree, ref_root_bh, 0,
6151 le32_to_cpu(vb.vb_xv->xr_clusters), p);
6152 if (ret)
6153 mlog_errno(ret);
6154
6155out:
6156 brelse(ref_root_bh);
6157 return ret;
6158}
6159
6160/*
Tao Ma01292412009-09-21 13:04:19 +08006161 * Add the REFCOUNTED flags for all the extent rec in ocfs2_xattr_value_root.
6162 * The physical clusters will be added to refcount tree.
6163 */
6164static int ocfs2_xattr_value_attach_refcount(struct inode *inode,
6165 struct ocfs2_xattr_value_root *xv,
6166 struct ocfs2_extent_tree *value_et,
6167 struct ocfs2_caching_info *ref_ci,
6168 struct buffer_head *ref_root_bh,
6169 struct ocfs2_cached_dealloc_ctxt *dealloc,
6170 struct ocfs2_post_refcount *refcount)
6171{
6172 int ret = 0;
6173 u32 clusters = le32_to_cpu(xv->xr_clusters);
6174 u32 cpos, p_cluster, num_clusters;
6175 struct ocfs2_extent_list *el = &xv->xr_list;
6176 unsigned int ext_flags;
6177
6178 cpos = 0;
6179 while (cpos < clusters) {
6180 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
6181 &num_clusters, el, &ext_flags);
6182
6183 cpos += num_clusters;
6184 if ((ext_flags & OCFS2_EXT_REFCOUNTED))
6185 continue;
6186
6187 BUG_ON(!p_cluster);
6188
6189 ret = ocfs2_add_refcount_flag(inode, value_et,
6190 ref_ci, ref_root_bh,
6191 cpos - num_clusters,
6192 p_cluster, num_clusters,
6193 dealloc, refcount);
6194 if (ret) {
6195 mlog_errno(ret);
6196 break;
6197 }
6198 }
6199
6200 return ret;
6201}
6202
6203/*
6204 * Given a normal ocfs2_xattr_header, refcount all the entries which
6205 * have value stored outside.
6206 * Used for xattrs stored in inode and ocfs2_xattr_block.
6207 */
6208static int ocfs2_xattr_attach_refcount_normal(struct inode *inode,
6209 struct ocfs2_xattr_value_buf *vb,
6210 struct ocfs2_xattr_header *header,
6211 struct ocfs2_caching_info *ref_ci,
6212 struct buffer_head *ref_root_bh,
6213 struct ocfs2_cached_dealloc_ctxt *dealloc)
6214{
6215
6216 struct ocfs2_xattr_entry *xe;
6217 struct ocfs2_xattr_value_root *xv;
6218 struct ocfs2_extent_tree et;
6219 int i, ret = 0;
6220
6221 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
6222 xe = &header->xh_entries[i];
6223
6224 if (ocfs2_xattr_is_local(xe))
6225 continue;
6226
6227 xv = (struct ocfs2_xattr_value_root *)((void *)header +
6228 le16_to_cpu(xe->xe_name_offset) +
6229 OCFS2_XATTR_SIZE(xe->xe_name_len));
6230
6231 vb->vb_xv = xv;
6232 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
6233
6234 ret = ocfs2_xattr_value_attach_refcount(inode, xv, &et,
6235 ref_ci, ref_root_bh,
6236 dealloc, NULL);
6237 if (ret) {
6238 mlog_errno(ret);
6239 break;
6240 }
6241 }
6242
6243 return ret;
6244}
6245
6246static int ocfs2_xattr_inline_attach_refcount(struct inode *inode,
6247 struct buffer_head *fe_bh,
6248 struct ocfs2_caching_info *ref_ci,
6249 struct buffer_head *ref_root_bh,
6250 struct ocfs2_cached_dealloc_ctxt *dealloc)
6251{
6252 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
6253 struct ocfs2_xattr_header *header = (struct ocfs2_xattr_header *)
6254 (fe_bh->b_data + inode->i_sb->s_blocksize -
6255 le16_to_cpu(di->i_xattr_inline_size));
6256 struct ocfs2_xattr_value_buf vb = {
6257 .vb_bh = fe_bh,
6258 .vb_access = ocfs2_journal_access_di,
6259 };
6260
6261 return ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
6262 ref_ci, ref_root_bh, dealloc);
6263}
6264
6265struct ocfs2_xattr_tree_value_refcount_para {
6266 struct ocfs2_caching_info *ref_ci;
6267 struct buffer_head *ref_root_bh;
6268 struct ocfs2_cached_dealloc_ctxt *dealloc;
6269};
6270
6271static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
6272 struct ocfs2_xattr_bucket *bucket,
6273 int offset,
6274 struct ocfs2_xattr_value_root **xv,
6275 struct buffer_head **bh)
6276{
6277 int ret, block_off, name_offset;
6278 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
6279 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
6280 void *base;
6281
6282 ret = ocfs2_xattr_bucket_get_name_value(sb,
6283 bucket_xh(bucket),
6284 offset,
6285 &block_off,
6286 &name_offset);
6287 if (ret) {
6288 mlog_errno(ret);
6289 goto out;
6290 }
6291
6292 base = bucket_block(bucket, block_off);
6293
6294 *xv = (struct ocfs2_xattr_value_root *)(base + name_offset +
6295 OCFS2_XATTR_SIZE(xe->xe_name_len));
6296
6297 if (bh)
6298 *bh = bucket->bu_bhs[block_off];
6299out:
6300 return ret;
6301}
6302
6303/*
6304 * For a given xattr bucket, refcount all the entries which
6305 * have value stored outside.
6306 */
6307static int ocfs2_xattr_bucket_value_refcount(struct inode *inode,
6308 struct ocfs2_xattr_bucket *bucket,
6309 void *para)
6310{
6311 int i, ret = 0;
6312 struct ocfs2_extent_tree et;
6313 struct ocfs2_xattr_tree_value_refcount_para *ref =
6314 (struct ocfs2_xattr_tree_value_refcount_para *)para;
6315 struct ocfs2_xattr_header *xh =
6316 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6317 struct ocfs2_xattr_entry *xe;
6318 struct ocfs2_xattr_value_buf vb = {
6319 .vb_access = ocfs2_journal_access,
6320 };
6321 struct ocfs2_post_refcount refcount = {
6322 .credits = bucket->bu_blocks,
6323 .para = bucket,
6324 .func = ocfs2_xattr_bucket_post_refcount,
6325 };
6326 struct ocfs2_post_refcount *p = NULL;
6327
6328 /* We only need post_refcount if we support metaecc. */
6329 if (ocfs2_meta_ecc(OCFS2_SB(inode->i_sb)))
6330 p = &refcount;
6331
6332 mlog(0, "refcount bucket %llu, count = %u\n",
6333 (unsigned long long)bucket_blkno(bucket),
6334 le16_to_cpu(xh->xh_count));
6335 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6336 xe = &xh->xh_entries[i];
6337
6338 if (ocfs2_xattr_is_local(xe))
6339 continue;
6340
6341 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket, i,
6342 &vb.vb_xv, &vb.vb_bh);
6343 if (ret) {
6344 mlog_errno(ret);
6345 break;
6346 }
6347
6348 ocfs2_init_xattr_value_extent_tree(&et,
6349 INODE_CACHE(inode), &vb);
6350
6351 ret = ocfs2_xattr_value_attach_refcount(inode, vb.vb_xv,
6352 &et, ref->ref_ci,
6353 ref->ref_root_bh,
6354 ref->dealloc, p);
6355 if (ret) {
6356 mlog_errno(ret);
6357 break;
6358 }
6359 }
6360
6361 return ret;
6362
6363}
6364
6365static int ocfs2_refcount_xattr_tree_rec(struct inode *inode,
6366 struct buffer_head *root_bh,
6367 u64 blkno, u32 cpos, u32 len, void *para)
6368{
6369 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
6370 ocfs2_xattr_bucket_value_refcount,
6371 para);
6372}
6373
6374static int ocfs2_xattr_block_attach_refcount(struct inode *inode,
6375 struct buffer_head *blk_bh,
6376 struct ocfs2_caching_info *ref_ci,
6377 struct buffer_head *ref_root_bh,
6378 struct ocfs2_cached_dealloc_ctxt *dealloc)
6379{
6380 int ret = 0;
6381 struct ocfs2_xattr_block *xb =
6382 (struct ocfs2_xattr_block *)blk_bh->b_data;
6383
6384 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
6385 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
6386 struct ocfs2_xattr_value_buf vb = {
6387 .vb_bh = blk_bh,
6388 .vb_access = ocfs2_journal_access_xb,
6389 };
6390
6391 ret = ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
6392 ref_ci, ref_root_bh,
6393 dealloc);
6394 } else {
6395 struct ocfs2_xattr_tree_value_refcount_para para = {
6396 .ref_ci = ref_ci,
6397 .ref_root_bh = ref_root_bh,
6398 .dealloc = dealloc,
6399 };
6400
6401 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
6402 ocfs2_refcount_xattr_tree_rec,
6403 &para);
6404 }
6405
6406 return ret;
6407}
6408
6409int ocfs2_xattr_attach_refcount_tree(struct inode *inode,
6410 struct buffer_head *fe_bh,
6411 struct ocfs2_caching_info *ref_ci,
6412 struct buffer_head *ref_root_bh,
6413 struct ocfs2_cached_dealloc_ctxt *dealloc)
6414{
6415 int ret = 0;
6416 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6417 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
6418 struct buffer_head *blk_bh = NULL;
6419
6420 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
6421 ret = ocfs2_xattr_inline_attach_refcount(inode, fe_bh,
6422 ref_ci, ref_root_bh,
6423 dealloc);
6424 if (ret) {
6425 mlog_errno(ret);
6426 goto out;
6427 }
6428 }
6429
6430 if (!di->i_xattr_loc)
6431 goto out;
6432
6433 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
6434 &blk_bh);
6435 if (ret < 0) {
6436 mlog_errno(ret);
6437 goto out;
6438 }
6439
6440 ret = ocfs2_xattr_block_attach_refcount(inode, blk_bh, ref_ci,
6441 ref_root_bh, dealloc);
6442 if (ret)
6443 mlog_errno(ret);
6444
6445 brelse(blk_bh);
6446out:
6447
6448 return ret;
6449}
6450
Tao Ma0fe9b662009-08-18 11:47:56 +08006451typedef int (should_xattr_reflinked)(struct ocfs2_xattr_entry *xe);
Tao Ma01292412009-09-21 13:04:19 +08006452/*
Tao Ma2999d122009-08-18 11:43:55 +08006453 * Store the information we need in xattr reflink.
6454 * old_bh and new_bh are inode bh for the old and new inode.
6455 */
6456struct ocfs2_xattr_reflink {
6457 struct inode *old_inode;
6458 struct inode *new_inode;
6459 struct buffer_head *old_bh;
6460 struct buffer_head *new_bh;
6461 struct ocfs2_caching_info *ref_ci;
6462 struct buffer_head *ref_root_bh;
6463 struct ocfs2_cached_dealloc_ctxt *dealloc;
Tao Ma0fe9b662009-08-18 11:47:56 +08006464 should_xattr_reflinked *xattr_reflinked;
Tao Ma2999d122009-08-18 11:43:55 +08006465};
6466
6467/*
6468 * Given a xattr header and xe offset,
6469 * return the proper xv and the corresponding bh.
6470 * xattr in inode, block and xattr tree have different implementaions.
6471 */
6472typedef int (get_xattr_value_root)(struct super_block *sb,
6473 struct buffer_head *bh,
6474 struct ocfs2_xattr_header *xh,
6475 int offset,
6476 struct ocfs2_xattr_value_root **xv,
6477 struct buffer_head **ret_bh,
6478 void *para);
6479
6480/*
6481 * Calculate all the xattr value root metadata stored in this xattr header and
6482 * credits we need if we create them from the scratch.
6483 * We use get_xattr_value_root so that all types of xattr container can use it.
6484 */
6485static int ocfs2_value_metas_in_xattr_header(struct super_block *sb,
6486 struct buffer_head *bh,
6487 struct ocfs2_xattr_header *xh,
6488 int *metas, int *credits,
6489 int *num_recs,
6490 get_xattr_value_root *func,
6491 void *para)
6492{
6493 int i, ret = 0;
6494 struct ocfs2_xattr_value_root *xv;
6495 struct ocfs2_xattr_entry *xe;
6496
6497 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6498 xe = &xh->xh_entries[i];
6499 if (ocfs2_xattr_is_local(xe))
6500 continue;
6501
6502 ret = func(sb, bh, xh, i, &xv, NULL, para);
6503 if (ret) {
6504 mlog_errno(ret);
6505 break;
6506 }
6507
6508 *metas += le16_to_cpu(xv->xr_list.l_tree_depth) *
6509 le16_to_cpu(xv->xr_list.l_next_free_rec);
6510
6511 *credits += ocfs2_calc_extend_credits(sb,
6512 &def_xv.xv.xr_list,
6513 le32_to_cpu(xv->xr_clusters));
6514
6515 /*
6516 * If the value is a tree with depth > 1, We don't go deep
6517 * to the extent block, so just calculate a maximum record num.
6518 */
6519 if (!xv->xr_list.l_tree_depth)
Tao Ma8ff6af82009-12-23 14:31:15 +08006520 *num_recs += le16_to_cpu(xv->xr_list.l_next_free_rec);
Tao Ma2999d122009-08-18 11:43:55 +08006521 else
6522 *num_recs += ocfs2_clusters_for_bytes(sb,
6523 XATTR_SIZE_MAX);
6524 }
6525
6526 return ret;
6527}
6528
6529/* Used by xattr inode and block to return the right xv and buffer_head. */
6530static int ocfs2_get_xattr_value_root(struct super_block *sb,
6531 struct buffer_head *bh,
6532 struct ocfs2_xattr_header *xh,
6533 int offset,
6534 struct ocfs2_xattr_value_root **xv,
6535 struct buffer_head **ret_bh,
6536 void *para)
6537{
6538 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
6539
6540 *xv = (struct ocfs2_xattr_value_root *)((void *)xh +
6541 le16_to_cpu(xe->xe_name_offset) +
6542 OCFS2_XATTR_SIZE(xe->xe_name_len));
6543
6544 if (ret_bh)
6545 *ret_bh = bh;
6546
6547 return 0;
6548}
6549
6550/*
6551 * Lock the meta_ac and caculate how much credits we need for reflink xattrs.
6552 * It is only used for inline xattr and xattr block.
6553 */
6554static int ocfs2_reflink_lock_xattr_allocators(struct ocfs2_super *osb,
6555 struct ocfs2_xattr_header *xh,
6556 struct buffer_head *ref_root_bh,
6557 int *credits,
6558 struct ocfs2_alloc_context **meta_ac)
6559{
6560 int ret, meta_add = 0, num_recs = 0;
6561 struct ocfs2_refcount_block *rb =
6562 (struct ocfs2_refcount_block *)ref_root_bh->b_data;
6563
6564 *credits = 0;
6565
6566 ret = ocfs2_value_metas_in_xattr_header(osb->sb, NULL, xh,
6567 &meta_add, credits, &num_recs,
6568 ocfs2_get_xattr_value_root,
6569 NULL);
6570 if (ret) {
6571 mlog_errno(ret);
6572 goto out;
6573 }
6574
6575 /*
6576 * We need to add/modify num_recs in refcount tree, so just calculate
6577 * an approximate number we need for refcount tree change.
6578 * Sometimes we need to split the tree, and after split, half recs
6579 * will be moved to the new block, and a new block can only provide
6580 * half number of recs. So we multiple new blocks by 2.
6581 */
6582 num_recs = num_recs / ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6583 meta_add += num_recs;
6584 *credits += num_recs + num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6585 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6586 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6587 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6588 else
6589 *credits += 1;
6590
6591 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add, meta_ac);
6592 if (ret)
6593 mlog_errno(ret);
6594
6595out:
6596 return ret;
6597}
6598
6599/*
6600 * Given a xattr header, reflink all the xattrs in this container.
6601 * It can be used for inode, block and bucket.
6602 *
6603 * NOTE:
6604 * Before we call this function, the caller has memcpy the xattr in
6605 * old_xh to the new_xh.
Tao Ma0fe9b662009-08-18 11:47:56 +08006606 *
6607 * If args.xattr_reflinked is set, call it to decide whether the xe should
6608 * be reflinked or not. If not, remove it from the new xattr header.
Tao Ma2999d122009-08-18 11:43:55 +08006609 */
6610static int ocfs2_reflink_xattr_header(handle_t *handle,
6611 struct ocfs2_xattr_reflink *args,
6612 struct buffer_head *old_bh,
6613 struct ocfs2_xattr_header *xh,
6614 struct buffer_head *new_bh,
6615 struct ocfs2_xattr_header *new_xh,
6616 struct ocfs2_xattr_value_buf *vb,
6617 struct ocfs2_alloc_context *meta_ac,
6618 get_xattr_value_root *func,
6619 void *para)
6620{
Tao Ma0fe9b662009-08-18 11:47:56 +08006621 int ret = 0, i, j;
Tao Ma2999d122009-08-18 11:43:55 +08006622 struct super_block *sb = args->old_inode->i_sb;
6623 struct buffer_head *value_bh;
Tao Ma0fe9b662009-08-18 11:47:56 +08006624 struct ocfs2_xattr_entry *xe, *last;
Tao Ma2999d122009-08-18 11:43:55 +08006625 struct ocfs2_xattr_value_root *xv, *new_xv;
6626 struct ocfs2_extent_tree data_et;
6627 u32 clusters, cpos, p_cluster, num_clusters;
6628 unsigned int ext_flags = 0;
6629
6630 mlog(0, "reflink xattr in container %llu, count = %u\n",
6631 (unsigned long long)old_bh->b_blocknr, le16_to_cpu(xh->xh_count));
Tao Ma0fe9b662009-08-18 11:47:56 +08006632
6633 last = &new_xh->xh_entries[le16_to_cpu(new_xh->xh_count)];
6634 for (i = 0, j = 0; i < le16_to_cpu(xh->xh_count); i++, j++) {
Tao Ma2999d122009-08-18 11:43:55 +08006635 xe = &xh->xh_entries[i];
6636
Tao Ma0fe9b662009-08-18 11:47:56 +08006637 if (args->xattr_reflinked && !args->xattr_reflinked(xe)) {
6638 xe = &new_xh->xh_entries[j];
6639
6640 le16_add_cpu(&new_xh->xh_count, -1);
6641 if (new_xh->xh_count) {
6642 memmove(xe, xe + 1,
6643 (void *)last - (void *)xe);
6644 memset(last, 0,
6645 sizeof(struct ocfs2_xattr_entry));
6646 }
6647
6648 /*
6649 * We don't want j to increase in the next round since
6650 * it is already moved ahead.
6651 */
6652 j--;
6653 continue;
6654 }
6655
Tao Ma2999d122009-08-18 11:43:55 +08006656 if (ocfs2_xattr_is_local(xe))
6657 continue;
6658
6659 ret = func(sb, old_bh, xh, i, &xv, NULL, para);
6660 if (ret) {
6661 mlog_errno(ret);
6662 break;
6663 }
6664
Tao Ma0fe9b662009-08-18 11:47:56 +08006665 ret = func(sb, new_bh, new_xh, j, &new_xv, &value_bh, para);
Tao Ma2999d122009-08-18 11:43:55 +08006666 if (ret) {
6667 mlog_errno(ret);
6668 break;
6669 }
6670
6671 /*
6672 * For the xattr which has l_tree_depth = 0, all the extent
6673 * recs have already be copied to the new xh with the
6674 * propriate OCFS2_EXT_REFCOUNTED flag we just need to
6675 * increase the refount count int the refcount tree.
6676 *
6677 * For the xattr which has l_tree_depth > 0, we need
6678 * to initialize it to the empty default value root,
6679 * and then insert the extents one by one.
6680 */
6681 if (xv->xr_list.l_tree_depth) {
6682 memcpy(new_xv, &def_xv, sizeof(def_xv));
6683 vb->vb_xv = new_xv;
6684 vb->vb_bh = value_bh;
6685 ocfs2_init_xattr_value_extent_tree(&data_et,
6686 INODE_CACHE(args->new_inode), vb);
6687 }
6688
6689 clusters = le32_to_cpu(xv->xr_clusters);
6690 cpos = 0;
6691 while (cpos < clusters) {
6692 ret = ocfs2_xattr_get_clusters(args->old_inode,
6693 cpos,
6694 &p_cluster,
6695 &num_clusters,
6696 &xv->xr_list,
6697 &ext_flags);
6698 if (ret) {
6699 mlog_errno(ret);
6700 goto out;
6701 }
6702
6703 BUG_ON(!p_cluster);
6704
6705 if (xv->xr_list.l_tree_depth) {
6706 ret = ocfs2_insert_extent(handle,
6707 &data_et, cpos,
6708 ocfs2_clusters_to_blocks(
6709 args->old_inode->i_sb,
6710 p_cluster),
6711 num_clusters, ext_flags,
6712 meta_ac);
6713 if (ret) {
6714 mlog_errno(ret);
6715 goto out;
6716 }
6717 }
6718
6719 ret = ocfs2_increase_refcount(handle, args->ref_ci,
6720 args->ref_root_bh,
6721 p_cluster, num_clusters,
6722 meta_ac, args->dealloc);
6723 if (ret) {
6724 mlog_errno(ret);
6725 goto out;
6726 }
6727
6728 cpos += num_clusters;
6729 }
6730 }
6731
6732out:
6733 return ret;
6734}
6735
6736static int ocfs2_reflink_xattr_inline(struct ocfs2_xattr_reflink *args)
6737{
6738 int ret = 0, credits = 0;
6739 handle_t *handle;
6740 struct ocfs2_super *osb = OCFS2_SB(args->old_inode->i_sb);
6741 struct ocfs2_dinode *di = (struct ocfs2_dinode *)args->old_bh->b_data;
6742 int inline_size = le16_to_cpu(di->i_xattr_inline_size);
6743 int header_off = osb->sb->s_blocksize - inline_size;
6744 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)
6745 (args->old_bh->b_data + header_off);
6746 struct ocfs2_xattr_header *new_xh = (struct ocfs2_xattr_header *)
6747 (args->new_bh->b_data + header_off);
6748 struct ocfs2_alloc_context *meta_ac = NULL;
6749 struct ocfs2_inode_info *new_oi;
6750 struct ocfs2_dinode *new_di;
6751 struct ocfs2_xattr_value_buf vb = {
6752 .vb_bh = args->new_bh,
6753 .vb_access = ocfs2_journal_access_di,
6754 };
6755
6756 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6757 &credits, &meta_ac);
6758 if (ret) {
6759 mlog_errno(ret);
6760 goto out;
6761 }
6762
6763 handle = ocfs2_start_trans(osb, credits);
6764 if (IS_ERR(handle)) {
6765 ret = PTR_ERR(handle);
6766 mlog_errno(ret);
6767 goto out;
6768 }
6769
6770 ret = ocfs2_journal_access_di(handle, INODE_CACHE(args->new_inode),
6771 args->new_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6772 if (ret) {
6773 mlog_errno(ret);
6774 goto out_commit;
6775 }
6776
6777 memcpy(args->new_bh->b_data + header_off,
6778 args->old_bh->b_data + header_off, inline_size);
6779
6780 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6781 new_di->i_xattr_inline_size = cpu_to_le16(inline_size);
6782
6783 ret = ocfs2_reflink_xattr_header(handle, args, args->old_bh, xh,
6784 args->new_bh, new_xh, &vb, meta_ac,
6785 ocfs2_get_xattr_value_root, NULL);
6786 if (ret) {
6787 mlog_errno(ret);
6788 goto out_commit;
6789 }
6790
6791 new_oi = OCFS2_I(args->new_inode);
6792 spin_lock(&new_oi->ip_lock);
6793 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL | OCFS2_INLINE_XATTR_FL;
6794 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6795 spin_unlock(&new_oi->ip_lock);
6796
6797 ocfs2_journal_dirty(handle, args->new_bh);
6798
6799out_commit:
6800 ocfs2_commit_trans(osb, handle);
6801
6802out:
6803 if (meta_ac)
6804 ocfs2_free_alloc_context(meta_ac);
6805 return ret;
6806}
6807
6808static int ocfs2_create_empty_xattr_block(struct inode *inode,
6809 struct buffer_head *fe_bh,
6810 struct buffer_head **ret_bh,
6811 int indexed)
6812{
6813 int ret;
6814 handle_t *handle;
6815 struct ocfs2_alloc_context *meta_ac;
6816 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6817
6818 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
6819 if (ret < 0) {
6820 mlog_errno(ret);
6821 return ret;
6822 }
6823
6824 handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_CREATE_CREDITS);
6825 if (IS_ERR(handle)) {
6826 ret = PTR_ERR(handle);
6827 mlog_errno(ret);
6828 goto out;
6829 }
6830
6831 mlog(0, "create new xattr block for inode %llu, index = %d\n",
6832 (unsigned long long)fe_bh->b_blocknr, indexed);
6833 ret = ocfs2_create_xattr_block(handle, inode, fe_bh,
6834 meta_ac, ret_bh, indexed);
6835 if (ret)
6836 mlog_errno(ret);
6837
6838 ocfs2_commit_trans(osb, handle);
6839out:
6840 ocfs2_free_alloc_context(meta_ac);
6841 return ret;
6842}
6843
6844static int ocfs2_reflink_xattr_block(struct ocfs2_xattr_reflink *args,
6845 struct buffer_head *blk_bh,
6846 struct buffer_head *new_blk_bh)
6847{
6848 int ret = 0, credits = 0;
6849 handle_t *handle;
6850 struct ocfs2_inode_info *new_oi = OCFS2_I(args->new_inode);
6851 struct ocfs2_dinode *new_di;
6852 struct ocfs2_super *osb = OCFS2_SB(args->new_inode->i_sb);
6853 int header_off = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
6854 struct ocfs2_xattr_block *xb =
6855 (struct ocfs2_xattr_block *)blk_bh->b_data;
6856 struct ocfs2_xattr_header *xh = &xb->xb_attrs.xb_header;
6857 struct ocfs2_xattr_block *new_xb =
6858 (struct ocfs2_xattr_block *)new_blk_bh->b_data;
6859 struct ocfs2_xattr_header *new_xh = &new_xb->xb_attrs.xb_header;
6860 struct ocfs2_alloc_context *meta_ac;
6861 struct ocfs2_xattr_value_buf vb = {
6862 .vb_bh = new_blk_bh,
6863 .vb_access = ocfs2_journal_access_xb,
6864 };
6865
6866 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6867 &credits, &meta_ac);
6868 if (ret) {
6869 mlog_errno(ret);
6870 return ret;
6871 }
6872
6873 /* One more credits in case we need to add xattr flags in new inode. */
6874 handle = ocfs2_start_trans(osb, credits + 1);
6875 if (IS_ERR(handle)) {
6876 ret = PTR_ERR(handle);
6877 mlog_errno(ret);
6878 goto out;
6879 }
6880
6881 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6882 ret = ocfs2_journal_access_di(handle,
6883 INODE_CACHE(args->new_inode),
6884 args->new_bh,
6885 OCFS2_JOURNAL_ACCESS_WRITE);
6886 if (ret) {
6887 mlog_errno(ret);
6888 goto out_commit;
6889 }
6890 }
6891
6892 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(args->new_inode),
6893 new_blk_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6894 if (ret) {
6895 mlog_errno(ret);
6896 goto out_commit;
6897 }
6898
6899 memcpy(new_blk_bh->b_data + header_off, blk_bh->b_data + header_off,
6900 osb->sb->s_blocksize - header_off);
6901
6902 ret = ocfs2_reflink_xattr_header(handle, args, blk_bh, xh,
6903 new_blk_bh, new_xh, &vb, meta_ac,
6904 ocfs2_get_xattr_value_root, NULL);
6905 if (ret) {
6906 mlog_errno(ret);
6907 goto out_commit;
6908 }
6909
6910 ocfs2_journal_dirty(handle, new_blk_bh);
6911
6912 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6913 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6914 spin_lock(&new_oi->ip_lock);
6915 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
6916 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6917 spin_unlock(&new_oi->ip_lock);
6918
6919 ocfs2_journal_dirty(handle, args->new_bh);
6920 }
6921
6922out_commit:
6923 ocfs2_commit_trans(osb, handle);
6924
6925out:
6926 ocfs2_free_alloc_context(meta_ac);
6927 return ret;
6928}
6929
6930struct ocfs2_reflink_xattr_tree_args {
6931 struct ocfs2_xattr_reflink *reflink;
6932 struct buffer_head *old_blk_bh;
6933 struct buffer_head *new_blk_bh;
6934 struct ocfs2_xattr_bucket *old_bucket;
6935 struct ocfs2_xattr_bucket *new_bucket;
6936};
6937
6938/*
6939 * NOTE:
6940 * We have to handle the case that both old bucket and new bucket
6941 * will call this function to get the right ret_bh.
6942 * So The caller must give us the right bh.
6943 */
6944static int ocfs2_get_reflink_xattr_value_root(struct super_block *sb,
6945 struct buffer_head *bh,
6946 struct ocfs2_xattr_header *xh,
6947 int offset,
6948 struct ocfs2_xattr_value_root **xv,
6949 struct buffer_head **ret_bh,
6950 void *para)
6951{
6952 struct ocfs2_reflink_xattr_tree_args *args =
6953 (struct ocfs2_reflink_xattr_tree_args *)para;
6954 struct ocfs2_xattr_bucket *bucket;
6955
6956 if (bh == args->old_bucket->bu_bhs[0])
6957 bucket = args->old_bucket;
6958 else
6959 bucket = args->new_bucket;
6960
6961 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6962 xv, ret_bh);
6963}
6964
6965struct ocfs2_value_tree_metas {
6966 int num_metas;
6967 int credits;
6968 int num_recs;
6969};
6970
6971static int ocfs2_value_tree_metas_in_bucket(struct super_block *sb,
6972 struct buffer_head *bh,
6973 struct ocfs2_xattr_header *xh,
6974 int offset,
6975 struct ocfs2_xattr_value_root **xv,
6976 struct buffer_head **ret_bh,
6977 void *para)
6978{
6979 struct ocfs2_xattr_bucket *bucket =
6980 (struct ocfs2_xattr_bucket *)para;
6981
6982 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6983 xv, ret_bh);
6984}
6985
6986static int ocfs2_calc_value_tree_metas(struct inode *inode,
6987 struct ocfs2_xattr_bucket *bucket,
6988 void *para)
6989{
6990 struct ocfs2_value_tree_metas *metas =
6991 (struct ocfs2_value_tree_metas *)para;
6992 struct ocfs2_xattr_header *xh =
6993 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6994
6995 /* Add the credits for this bucket first. */
6996 metas->credits += bucket->bu_blocks;
6997 return ocfs2_value_metas_in_xattr_header(inode->i_sb, bucket->bu_bhs[0],
6998 xh, &metas->num_metas,
6999 &metas->credits, &metas->num_recs,
7000 ocfs2_value_tree_metas_in_bucket,
7001 bucket);
7002}
7003
7004/*
7005 * Given a xattr extent rec starting from blkno and having len clusters,
7006 * iterate all the buckets calculate how much metadata we need for reflinking
7007 * all the ocfs2_xattr_value_root and lock the allocators accordingly.
7008 */
7009static int ocfs2_lock_reflink_xattr_rec_allocators(
7010 struct ocfs2_reflink_xattr_tree_args *args,
7011 struct ocfs2_extent_tree *xt_et,
7012 u64 blkno, u32 len, int *credits,
7013 struct ocfs2_alloc_context **meta_ac,
7014 struct ocfs2_alloc_context **data_ac)
7015{
7016 int ret, num_free_extents;
7017 struct ocfs2_value_tree_metas metas;
7018 struct ocfs2_super *osb = OCFS2_SB(args->reflink->old_inode->i_sb);
7019 struct ocfs2_refcount_block *rb;
7020
7021 memset(&metas, 0, sizeof(metas));
7022
7023 ret = ocfs2_iterate_xattr_buckets(args->reflink->old_inode, blkno, len,
7024 ocfs2_calc_value_tree_metas, &metas);
7025 if (ret) {
7026 mlog_errno(ret);
7027 goto out;
7028 }
7029
7030 *credits = metas.credits;
7031
7032 /*
7033 * Calculate we need for refcount tree change.
7034 *
7035 * We need to add/modify num_recs in refcount tree, so just calculate
7036 * an approximate number we need for refcount tree change.
7037 * Sometimes we need to split the tree, and after split, half recs
7038 * will be moved to the new block, and a new block can only provide
7039 * half number of recs. So we multiple new blocks by 2.
7040 * In the end, we have to add credits for modifying the already
7041 * existed refcount block.
7042 */
7043 rb = (struct ocfs2_refcount_block *)args->reflink->ref_root_bh->b_data;
7044 metas.num_recs =
7045 (metas.num_recs + ocfs2_refcount_recs_per_rb(osb->sb) - 1) /
7046 ocfs2_refcount_recs_per_rb(osb->sb) * 2;
7047 metas.num_metas += metas.num_recs;
7048 *credits += metas.num_recs +
7049 metas.num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
7050 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
7051 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
7052 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
7053 else
7054 *credits += 1;
7055
7056 /* count in the xattr tree change. */
7057 num_free_extents = ocfs2_num_free_extents(osb, xt_et);
7058 if (num_free_extents < 0) {
7059 ret = num_free_extents;
7060 mlog_errno(ret);
7061 goto out;
7062 }
7063
7064 if (num_free_extents < len)
7065 metas.num_metas += ocfs2_extend_meta_needed(xt_et->et_root_el);
7066
7067 *credits += ocfs2_calc_extend_credits(osb->sb,
7068 xt_et->et_root_el, len);
7069
7070 if (metas.num_metas) {
7071 ret = ocfs2_reserve_new_metadata_blocks(osb, metas.num_metas,
7072 meta_ac);
7073 if (ret) {
7074 mlog_errno(ret);
7075 goto out;
7076 }
7077 }
7078
7079 if (len) {
7080 ret = ocfs2_reserve_clusters(osb, len, data_ac);
7081 if (ret)
7082 mlog_errno(ret);
7083 }
7084out:
7085 if (ret) {
7086 if (*meta_ac) {
7087 ocfs2_free_alloc_context(*meta_ac);
7088 meta_ac = NULL;
7089 }
7090 }
7091
7092 return ret;
7093}
7094
7095static int ocfs2_reflink_xattr_buckets(handle_t *handle,
7096 u64 blkno, u64 new_blkno, u32 clusters,
7097 struct ocfs2_alloc_context *meta_ac,
7098 struct ocfs2_alloc_context *data_ac,
7099 struct ocfs2_reflink_xattr_tree_args *args)
7100{
7101 int i, j, ret = 0;
7102 struct super_block *sb = args->reflink->old_inode->i_sb;
7103 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
7104 u32 num_buckets = clusters * bpc;
7105 int bpb = args->old_bucket->bu_blocks;
7106 struct ocfs2_xattr_value_buf vb = {
7107 .vb_access = ocfs2_journal_access,
7108 };
7109
7110 for (i = 0; i < num_buckets; i++, blkno += bpb, new_blkno += bpb) {
7111 ret = ocfs2_read_xattr_bucket(args->old_bucket, blkno);
7112 if (ret) {
7113 mlog_errno(ret);
7114 break;
7115 }
7116
7117 ret = ocfs2_init_xattr_bucket(args->new_bucket, new_blkno);
7118 if (ret) {
7119 mlog_errno(ret);
7120 break;
7121 }
7122
7123 /*
7124 * The real bucket num in this series of blocks is stored
7125 * in the 1st bucket.
7126 */
7127 if (i == 0)
7128 num_buckets = le16_to_cpu(
7129 bucket_xh(args->old_bucket)->xh_num_buckets);
7130
7131 ret = ocfs2_xattr_bucket_journal_access(handle,
7132 args->new_bucket,
7133 OCFS2_JOURNAL_ACCESS_CREATE);
7134 if (ret) {
7135 mlog_errno(ret);
7136 break;
7137 }
7138
7139 for (j = 0; j < bpb; j++)
7140 memcpy(bucket_block(args->new_bucket, j),
7141 bucket_block(args->old_bucket, j),
7142 sb->s_blocksize);
7143
7144 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
7145
7146 ret = ocfs2_reflink_xattr_header(handle, args->reflink,
7147 args->old_bucket->bu_bhs[0],
7148 bucket_xh(args->old_bucket),
7149 args->new_bucket->bu_bhs[0],
7150 bucket_xh(args->new_bucket),
7151 &vb, meta_ac,
7152 ocfs2_get_reflink_xattr_value_root,
7153 args);
7154 if (ret) {
7155 mlog_errno(ret);
7156 break;
7157 }
7158
7159 /*
7160 * Re-access and dirty the bucket to calculate metaecc.
7161 * Because we may extend the transaction in reflink_xattr_header
7162 * which will let the already accessed block gone.
7163 */
7164 ret = ocfs2_xattr_bucket_journal_access(handle,
7165 args->new_bucket,
7166 OCFS2_JOURNAL_ACCESS_WRITE);
7167 if (ret) {
7168 mlog_errno(ret);
7169 break;
7170 }
7171
7172 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
7173 ocfs2_xattr_bucket_relse(args->old_bucket);
7174 ocfs2_xattr_bucket_relse(args->new_bucket);
7175 }
7176
7177 ocfs2_xattr_bucket_relse(args->old_bucket);
7178 ocfs2_xattr_bucket_relse(args->new_bucket);
7179 return ret;
7180}
7181/*
7182 * Create the same xattr extent record in the new inode's xattr tree.
7183 */
7184static int ocfs2_reflink_xattr_rec(struct inode *inode,
7185 struct buffer_head *root_bh,
7186 u64 blkno,
7187 u32 cpos,
7188 u32 len,
7189 void *para)
7190{
7191 int ret, credits = 0;
7192 u32 p_cluster, num_clusters;
7193 u64 new_blkno;
7194 handle_t *handle;
7195 struct ocfs2_reflink_xattr_tree_args *args =
7196 (struct ocfs2_reflink_xattr_tree_args *)para;
7197 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7198 struct ocfs2_alloc_context *meta_ac = NULL;
7199 struct ocfs2_alloc_context *data_ac = NULL;
7200 struct ocfs2_extent_tree et;
7201
7202 ocfs2_init_xattr_tree_extent_tree(&et,
7203 INODE_CACHE(args->reflink->new_inode),
7204 args->new_blk_bh);
7205
7206 ret = ocfs2_lock_reflink_xattr_rec_allocators(args, &et, blkno,
7207 len, &credits,
7208 &meta_ac, &data_ac);
7209 if (ret) {
7210 mlog_errno(ret);
7211 goto out;
7212 }
7213
7214 handle = ocfs2_start_trans(osb, credits);
7215 if (IS_ERR(handle)) {
7216 ret = PTR_ERR(handle);
7217 mlog_errno(ret);
7218 goto out;
7219 }
7220
7221 ret = ocfs2_claim_clusters(osb, handle, data_ac,
7222 len, &p_cluster, &num_clusters);
7223 if (ret) {
7224 mlog_errno(ret);
7225 goto out_commit;
7226 }
7227
7228 new_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cluster);
7229
7230 mlog(0, "reflink xattr buckets %llu to %llu, len %u\n",
7231 (unsigned long long)blkno, (unsigned long long)new_blkno, len);
7232 ret = ocfs2_reflink_xattr_buckets(handle, blkno, new_blkno, len,
7233 meta_ac, data_ac, args);
7234 if (ret) {
7235 mlog_errno(ret);
7236 goto out_commit;
7237 }
7238
7239 mlog(0, "insert new xattr extent rec start %llu len %u to %u\n",
7240 (unsigned long long)new_blkno, len, cpos);
7241 ret = ocfs2_insert_extent(handle, &et, cpos, new_blkno,
7242 len, 0, meta_ac);
7243 if (ret)
7244 mlog_errno(ret);
7245
7246out_commit:
7247 ocfs2_commit_trans(osb, handle);
7248
7249out:
7250 if (meta_ac)
7251 ocfs2_free_alloc_context(meta_ac);
7252 if (data_ac)
7253 ocfs2_free_alloc_context(data_ac);
7254 return ret;
7255}
7256
7257/*
7258 * Create reflinked xattr buckets.
7259 * We will add bucket one by one, and refcount all the xattrs in the bucket
7260 * if they are stored outside.
7261 */
7262static int ocfs2_reflink_xattr_tree(struct ocfs2_xattr_reflink *args,
7263 struct buffer_head *blk_bh,
7264 struct buffer_head *new_blk_bh)
7265{
7266 int ret;
7267 struct ocfs2_reflink_xattr_tree_args para;
7268
7269 memset(&para, 0, sizeof(para));
7270 para.reflink = args;
7271 para.old_blk_bh = blk_bh;
7272 para.new_blk_bh = new_blk_bh;
7273
7274 para.old_bucket = ocfs2_xattr_bucket_new(args->old_inode);
7275 if (!para.old_bucket) {
7276 mlog_errno(-ENOMEM);
7277 return -ENOMEM;
7278 }
7279
7280 para.new_bucket = ocfs2_xattr_bucket_new(args->new_inode);
7281 if (!para.new_bucket) {
7282 ret = -ENOMEM;
7283 mlog_errno(ret);
7284 goto out;
7285 }
7286
7287 ret = ocfs2_iterate_xattr_index_block(args->old_inode, blk_bh,
7288 ocfs2_reflink_xattr_rec,
7289 &para);
7290 if (ret)
7291 mlog_errno(ret);
7292
7293out:
7294 ocfs2_xattr_bucket_free(para.old_bucket);
7295 ocfs2_xattr_bucket_free(para.new_bucket);
7296 return ret;
7297}
7298
7299static int ocfs2_reflink_xattr_in_block(struct ocfs2_xattr_reflink *args,
7300 struct buffer_head *blk_bh)
7301{
7302 int ret, indexed = 0;
7303 struct buffer_head *new_blk_bh = NULL;
7304 struct ocfs2_xattr_block *xb =
7305 (struct ocfs2_xattr_block *)blk_bh->b_data;
7306
7307
7308 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)
7309 indexed = 1;
7310
7311 ret = ocfs2_create_empty_xattr_block(args->new_inode, args->new_bh,
7312 &new_blk_bh, indexed);
7313 if (ret) {
7314 mlog_errno(ret);
7315 goto out;
7316 }
7317
7318 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED))
7319 ret = ocfs2_reflink_xattr_block(args, blk_bh, new_blk_bh);
7320 else
7321 ret = ocfs2_reflink_xattr_tree(args, blk_bh, new_blk_bh);
7322 if (ret)
7323 mlog_errno(ret);
7324
7325out:
7326 brelse(new_blk_bh);
7327 return ret;
7328}
7329
Tao Ma0fe9b662009-08-18 11:47:56 +08007330static int ocfs2_reflink_xattr_no_security(struct ocfs2_xattr_entry *xe)
7331{
7332 int type = ocfs2_xattr_get_type(xe);
7333
7334 return type != OCFS2_XATTR_INDEX_SECURITY &&
7335 type != OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS &&
7336 type != OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
7337}
7338
Tao Ma2999d122009-08-18 11:43:55 +08007339int ocfs2_reflink_xattrs(struct inode *old_inode,
7340 struct buffer_head *old_bh,
7341 struct inode *new_inode,
Tao Ma0fe9b662009-08-18 11:47:56 +08007342 struct buffer_head *new_bh,
7343 bool preserve_security)
Tao Ma2999d122009-08-18 11:43:55 +08007344{
7345 int ret;
7346 struct ocfs2_xattr_reflink args;
7347 struct ocfs2_inode_info *oi = OCFS2_I(old_inode);
7348 struct ocfs2_dinode *di = (struct ocfs2_dinode *)old_bh->b_data;
7349 struct buffer_head *blk_bh = NULL;
7350 struct ocfs2_cached_dealloc_ctxt dealloc;
7351 struct ocfs2_refcount_tree *ref_tree;
7352 struct buffer_head *ref_root_bh = NULL;
7353
7354 ret = ocfs2_lock_refcount_tree(OCFS2_SB(old_inode->i_sb),
7355 le64_to_cpu(di->i_refcount_loc),
7356 1, &ref_tree, &ref_root_bh);
7357 if (ret) {
7358 mlog_errno(ret);
7359 goto out;
7360 }
7361
7362 ocfs2_init_dealloc_ctxt(&dealloc);
7363
7364 args.old_inode = old_inode;
7365 args.new_inode = new_inode;
7366 args.old_bh = old_bh;
7367 args.new_bh = new_bh;
7368 args.ref_ci = &ref_tree->rf_ci;
7369 args.ref_root_bh = ref_root_bh;
7370 args.dealloc = &dealloc;
Tao Ma0fe9b662009-08-18 11:47:56 +08007371 if (preserve_security)
7372 args.xattr_reflinked = NULL;
7373 else
7374 args.xattr_reflinked = ocfs2_reflink_xattr_no_security;
Tao Ma2999d122009-08-18 11:43:55 +08007375
7376 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
7377 ret = ocfs2_reflink_xattr_inline(&args);
7378 if (ret) {
7379 mlog_errno(ret);
7380 goto out_unlock;
7381 }
7382 }
7383
7384 if (!di->i_xattr_loc)
7385 goto out_unlock;
7386
7387 ret = ocfs2_read_xattr_block(old_inode, le64_to_cpu(di->i_xattr_loc),
7388 &blk_bh);
7389 if (ret < 0) {
7390 mlog_errno(ret);
7391 goto out_unlock;
7392 }
7393
7394 ret = ocfs2_reflink_xattr_in_block(&args, blk_bh);
7395 if (ret)
7396 mlog_errno(ret);
7397
7398 brelse(blk_bh);
7399
7400out_unlock:
7401 ocfs2_unlock_refcount_tree(OCFS2_SB(old_inode->i_sb),
7402 ref_tree, 1);
7403 brelse(ref_root_bh);
7404
7405 if (ocfs2_dealloc_has_cluster(&dealloc)) {
7406 ocfs2_schedule_truncate_log_flush(OCFS2_SB(old_inode->i_sb), 1);
7407 ocfs2_run_deallocs(OCFS2_SB(old_inode->i_sb), &dealloc);
7408 }
7409
7410out:
7411 return ret;
7412}
7413
7414/*
Tao Ma0fe9b662009-08-18 11:47:56 +08007415 * Initialize security and acl for a already created inode.
7416 * Used for reflink a non-preserve-security file.
7417 *
7418 * It uses common api like ocfs2_xattr_set, so the caller
7419 * must not hold any lock expect i_mutex.
7420 */
7421int ocfs2_init_security_and_acl(struct inode *dir,
7422 struct inode *inode)
7423{
7424 int ret = 0;
7425 struct buffer_head *dir_bh = NULL;
7426 struct ocfs2_security_xattr_info si = {
7427 .enable = 1,
7428 };
7429
7430 ret = ocfs2_init_security_get(inode, dir, &si);
7431 if (!ret) {
Christoph Hellwig431547b2009-11-13 09:52:56 +00007432 ret = ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY,
7433 si.name, si.value, si.value_len,
7434 XATTR_CREATE);
Tao Ma0fe9b662009-08-18 11:47:56 +08007435 if (ret) {
7436 mlog_errno(ret);
7437 goto leave;
7438 }
7439 } else if (ret != -EOPNOTSUPP) {
7440 mlog_errno(ret);
7441 goto leave;
7442 }
7443
7444 ret = ocfs2_inode_lock(dir, &dir_bh, 0);
7445 if (ret) {
7446 mlog_errno(ret);
7447 goto leave;
7448 }
7449
7450 ret = ocfs2_init_acl(NULL, inode, dir, NULL, dir_bh, NULL, NULL);
7451 if (ret)
7452 mlog_errno(ret);
7453
7454 ocfs2_inode_unlock(dir, 0);
7455 brelse(dir_bh);
7456leave:
7457 return ret;
7458}
7459/*
Tiger Yang923f7f32008-11-14 11:16:27 +08007460 * 'security' attributes support
7461 */
Christoph Hellwig431547b2009-11-13 09:52:56 +00007462static size_t ocfs2_xattr_security_list(struct dentry *dentry, char *list,
Tiger Yang923f7f32008-11-14 11:16:27 +08007463 size_t list_size, const char *name,
Christoph Hellwig431547b2009-11-13 09:52:56 +00007464 size_t name_len, int type)
Tiger Yang923f7f32008-11-14 11:16:27 +08007465{
7466 const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN;
7467 const size_t total_len = prefix_len + name_len + 1;
7468
7469 if (list && total_len <= list_size) {
7470 memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
7471 memcpy(list + prefix_len, name, name_len);
7472 list[prefix_len + name_len] = '\0';
7473 }
7474 return total_len;
7475}
7476
Christoph Hellwig431547b2009-11-13 09:52:56 +00007477static int ocfs2_xattr_security_get(struct dentry *dentry, const char *name,
7478 void *buffer, size_t size, int type)
Tiger Yang923f7f32008-11-14 11:16:27 +08007479{
7480 if (strcmp(name, "") == 0)
7481 return -EINVAL;
Christoph Hellwig431547b2009-11-13 09:52:56 +00007482 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
7483 name, buffer, size);
Tiger Yang923f7f32008-11-14 11:16:27 +08007484}
7485
Christoph Hellwig431547b2009-11-13 09:52:56 +00007486static int ocfs2_xattr_security_set(struct dentry *dentry, const char *name,
7487 const void *value, size_t size, int flags, int type)
Tiger Yang923f7f32008-11-14 11:16:27 +08007488{
7489 if (strcmp(name, "") == 0)
7490 return -EINVAL;
7491
Christoph Hellwig431547b2009-11-13 09:52:56 +00007492 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
7493 name, value, size, flags);
Tiger Yang923f7f32008-11-14 11:16:27 +08007494}
7495
Tiger Yang534eadd2008-11-14 11:16:41 +08007496int ocfs2_init_security_get(struct inode *inode,
7497 struct inode *dir,
7498 struct ocfs2_security_xattr_info *si)
7499{
Tiger Yang38d59ef2008-12-17 10:22:56 +08007500 /* check whether ocfs2 support feature xattr */
7501 if (!ocfs2_supports_xattr(OCFS2_SB(dir->i_sb)))
7502 return -EOPNOTSUPP;
Tiger Yang534eadd2008-11-14 11:16:41 +08007503 return security_inode_init_security(inode, dir, &si->name, &si->value,
7504 &si->value_len);
7505}
7506
7507int ocfs2_init_security_set(handle_t *handle,
7508 struct inode *inode,
7509 struct buffer_head *di_bh,
7510 struct ocfs2_security_xattr_info *si,
7511 struct ocfs2_alloc_context *xattr_ac,
7512 struct ocfs2_alloc_context *data_ac)
7513{
7514 return ocfs2_xattr_set_handle(handle, inode, di_bh,
7515 OCFS2_XATTR_INDEX_SECURITY,
7516 si->name, si->value, si->value_len, 0,
7517 xattr_ac, data_ac);
7518}
7519
Tiger Yang923f7f32008-11-14 11:16:27 +08007520struct xattr_handler ocfs2_xattr_security_handler = {
7521 .prefix = XATTR_SECURITY_PREFIX,
7522 .list = ocfs2_xattr_security_list,
7523 .get = ocfs2_xattr_security_get,
7524 .set = ocfs2_xattr_security_set,
7525};
7526
7527/*
Mark Fasheh99219ae2008-10-07 14:52:59 -07007528 * 'trusted' attributes support
7529 */
Christoph Hellwig431547b2009-11-13 09:52:56 +00007530static size_t ocfs2_xattr_trusted_list(struct dentry *dentry, char *list,
Mark Fasheh99219ae2008-10-07 14:52:59 -07007531 size_t list_size, const char *name,
Christoph Hellwig431547b2009-11-13 09:52:56 +00007532 size_t name_len, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007533{
Tiger Yangceb1eba2008-10-23 16:34:13 +08007534 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
Mark Fasheh99219ae2008-10-07 14:52:59 -07007535 const size_t total_len = prefix_len + name_len + 1;
7536
7537 if (list && total_len <= list_size) {
7538 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
7539 memcpy(list + prefix_len, name, name_len);
7540 list[prefix_len + name_len] = '\0';
7541 }
7542 return total_len;
7543}
7544
Christoph Hellwig431547b2009-11-13 09:52:56 +00007545static int ocfs2_xattr_trusted_get(struct dentry *dentry, const char *name,
7546 void *buffer, size_t size, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007547{
7548 if (strcmp(name, "") == 0)
7549 return -EINVAL;
Christoph Hellwig431547b2009-11-13 09:52:56 +00007550 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
7551 name, buffer, size);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007552}
7553
Christoph Hellwig431547b2009-11-13 09:52:56 +00007554static int ocfs2_xattr_trusted_set(struct dentry *dentry, const char *name,
7555 const void *value, size_t size, int flags, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007556{
7557 if (strcmp(name, "") == 0)
7558 return -EINVAL;
7559
Christoph Hellwig431547b2009-11-13 09:52:56 +00007560 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
7561 name, value, size, flags);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007562}
7563
7564struct xattr_handler ocfs2_xattr_trusted_handler = {
7565 .prefix = XATTR_TRUSTED_PREFIX,
7566 .list = ocfs2_xattr_trusted_list,
7567 .get = ocfs2_xattr_trusted_get,
7568 .set = ocfs2_xattr_trusted_set,
7569};
7570
Mark Fasheh99219ae2008-10-07 14:52:59 -07007571/*
7572 * 'user' attributes support
7573 */
Christoph Hellwig431547b2009-11-13 09:52:56 +00007574static size_t ocfs2_xattr_user_list(struct dentry *dentry, char *list,
Mark Fasheh99219ae2008-10-07 14:52:59 -07007575 size_t list_size, const char *name,
Christoph Hellwig431547b2009-11-13 09:52:56 +00007576 size_t name_len, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007577{
Tiger Yangceb1eba2008-10-23 16:34:13 +08007578 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
Mark Fasheh99219ae2008-10-07 14:52:59 -07007579 const size_t total_len = prefix_len + name_len + 1;
Christoph Hellwig431547b2009-11-13 09:52:56 +00007580 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007581
7582 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7583 return 0;
7584
7585 if (list && total_len <= list_size) {
7586 memcpy(list, XATTR_USER_PREFIX, prefix_len);
7587 memcpy(list + prefix_len, name, name_len);
7588 list[prefix_len + name_len] = '\0';
7589 }
7590 return total_len;
7591}
7592
Christoph Hellwig431547b2009-11-13 09:52:56 +00007593static int ocfs2_xattr_user_get(struct dentry *dentry, const char *name,
7594 void *buffer, size_t size, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007595{
Christoph Hellwig431547b2009-11-13 09:52:56 +00007596 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007597
7598 if (strcmp(name, "") == 0)
7599 return -EINVAL;
7600 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7601 return -EOPNOTSUPP;
Christoph Hellwig431547b2009-11-13 09:52:56 +00007602 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_USER, name,
Mark Fasheh99219ae2008-10-07 14:52:59 -07007603 buffer, size);
7604}
7605
Christoph Hellwig431547b2009-11-13 09:52:56 +00007606static int ocfs2_xattr_user_set(struct dentry *dentry, const char *name,
7607 const void *value, size_t size, int flags, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007608{
Christoph Hellwig431547b2009-11-13 09:52:56 +00007609 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007610
7611 if (strcmp(name, "") == 0)
7612 return -EINVAL;
7613 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7614 return -EOPNOTSUPP;
7615
Christoph Hellwig431547b2009-11-13 09:52:56 +00007616 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_USER,
7617 name, value, size, flags);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007618}
7619
7620struct xattr_handler ocfs2_xattr_user_handler = {
7621 .prefix = XATTR_USER_PREFIX,
7622 .list = ocfs2_xattr_user_list,
7623 .get = ocfs2_xattr_user_get,
7624 .set = ocfs2_xattr_user_set,
7625};