blob: 550a3e86c9716b98e8e70e3d7dedf231ba1b00a7 [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
Tiger Yang534eadd2008-11-14 11:16:41 +0800576static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
577{
Joel Becker199799a2009-08-14 19:04:15 -0700578 return namevalue_size(name_len, value_len) +
579 sizeof(struct ocfs2_xattr_entry);
580}
Tiger Yang534eadd2008-11-14 11:16:41 +0800581
Joel Becker199799a2009-08-14 19:04:15 -0700582static int ocfs2_xi_entry_usage(struct ocfs2_xattr_info *xi)
583{
584 return namevalue_size_xi(xi) +
585 sizeof(struct ocfs2_xattr_entry);
586}
Tiger Yang534eadd2008-11-14 11:16:41 +0800587
Joel Becker199799a2009-08-14 19:04:15 -0700588static int ocfs2_xe_entry_usage(struct ocfs2_xattr_entry *xe)
589{
590 return namevalue_size_xe(xe) +
591 sizeof(struct ocfs2_xattr_entry);
Tiger Yang534eadd2008-11-14 11:16:41 +0800592}
593
594int ocfs2_calc_security_init(struct inode *dir,
595 struct ocfs2_security_xattr_info *si,
596 int *want_clusters,
597 int *xattr_credits,
598 struct ocfs2_alloc_context **xattr_ac)
599{
600 int ret = 0;
601 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
602 int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
603 si->value_len);
604
605 /*
606 * The max space of security xattr taken inline is
607 * 256(name) + 80(value) + 16(entry) = 352 bytes,
608 * So reserve one metadata block for it is ok.
609 */
610 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
611 s_size > OCFS2_XATTR_FREE_IN_IBODY) {
612 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
613 if (ret) {
614 mlog_errno(ret);
615 return ret;
616 }
617 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
618 }
619
620 /* reserve clusters for xattr value which will be set in B tree*/
Tiger Yang0e445b62008-12-09 16:42:51 +0800621 if (si->value_len > OCFS2_XATTR_INLINE_SIZE) {
622 int new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
623 si->value_len);
624
625 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
626 new_clusters);
627 *want_clusters += new_clusters;
628 }
Tiger Yang534eadd2008-11-14 11:16:41 +0800629 return ret;
630}
631
Tiger Yang89c38bd2008-11-14 11:17:41 +0800632int ocfs2_calc_xattr_init(struct inode *dir,
633 struct buffer_head *dir_bh,
634 int mode,
635 struct ocfs2_security_xattr_info *si,
636 int *want_clusters,
637 int *xattr_credits,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800638 int *want_meta)
Tiger Yang89c38bd2008-11-14 11:17:41 +0800639{
640 int ret = 0;
641 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
Tiger Yang0e445b62008-12-09 16:42:51 +0800642 int s_size = 0, a_size = 0, acl_len = 0, new_clusters;
Tiger Yang89c38bd2008-11-14 11:17:41 +0800643
644 if (si->enable)
645 s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
646 si->value_len);
647
648 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
649 acl_len = ocfs2_xattr_get_nolock(dir, dir_bh,
650 OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
651 "", NULL, 0);
652 if (acl_len > 0) {
653 a_size = ocfs2_xattr_entry_real_size(0, acl_len);
654 if (S_ISDIR(mode))
655 a_size <<= 1;
656 } else if (acl_len != 0 && acl_len != -ENODATA) {
657 mlog_errno(ret);
658 return ret;
659 }
660 }
661
662 if (!(s_size + a_size))
663 return ret;
664
665 /*
666 * The max space of security xattr taken inline is
667 * 256(name) + 80(value) + 16(entry) = 352 bytes,
668 * The max space of acl xattr taken inline is
669 * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
670 * when blocksize = 512, may reserve one more cluser for
671 * xattr bucket, otherwise reserve one metadata block
672 * for them is ok.
Tiger Yang6c9fd1d2009-03-06 10:19:30 +0800673 * If this is a new directory with inline data,
674 * we choose to reserve the entire inline area for
675 * directory contents and force an external xattr block.
Tiger Yang89c38bd2008-11-14 11:17:41 +0800676 */
677 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
Tiger Yang6c9fd1d2009-03-06 10:19:30 +0800678 (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) ||
Tiger Yang89c38bd2008-11-14 11:17:41 +0800679 (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) {
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800680 *want_meta = *want_meta + 1;
Tiger Yang89c38bd2008-11-14 11:17:41 +0800681 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
682 }
683
684 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
685 (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
686 *want_clusters += 1;
687 *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
688 }
689
Tiger Yang0e445b62008-12-09 16:42:51 +0800690 /*
691 * reserve credits and clusters for xattrs which has large value
692 * and have to be set outside
693 */
694 if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE) {
695 new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
696 si->value_len);
697 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
698 new_clusters);
699 *want_clusters += new_clusters;
700 }
Tiger Yang89c38bd2008-11-14 11:17:41 +0800701 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
702 acl_len > OCFS2_XATTR_INLINE_SIZE) {
Tiger Yang0e445b62008-12-09 16:42:51 +0800703 /* for directory, it has DEFAULT and ACCESS two types of acls */
704 new_clusters = (S_ISDIR(mode) ? 2 : 1) *
705 ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
706 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
707 new_clusters);
708 *want_clusters += new_clusters;
Tiger Yang89c38bd2008-11-14 11:17:41 +0800709 }
710
711 return ret;
712}
713
Tao Maf56654c2008-08-18 17:38:48 +0800714static int ocfs2_xattr_extend_allocation(struct inode *inode,
715 u32 clusters_to_add,
Joel Becker19b801f2008-12-09 14:36:50 -0800716 struct ocfs2_xattr_value_buf *vb,
Tao Ma78f30c32008-11-12 08:27:00 +0800717 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800718{
719 int status = 0;
Tao Ma85db90e2008-11-12 08:27:01 +0800720 handle_t *handle = ctxt->handle;
Tao Maf56654c2008-08-18 17:38:48 +0800721 enum ocfs2_alloc_restarted why;
Joel Becker19b801f2008-12-09 14:36:50 -0800722 u32 prev_clusters, logical_start = le32_to_cpu(vb->vb_xv->xr_clusters);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700723 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +0800724
725 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
726
Joel Becker5e404e92009-02-13 03:54:22 -0800727 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700728
Joel Becker0cf2f762009-02-12 16:41:25 -0800729 status = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
Joel Becker2a50a742008-12-09 14:24:33 -0800730 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Maf56654c2008-08-18 17:38:48 +0800731 if (status < 0) {
732 mlog_errno(status);
733 goto leave;
734 }
735
Joel Becker19b801f2008-12-09 14:36:50 -0800736 prev_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
Joel Beckercbee7e12009-02-13 03:34:15 -0800737 status = ocfs2_add_clusters_in_btree(handle,
738 &et,
Tao Maf56654c2008-08-18 17:38:48 +0800739 &logical_start,
740 clusters_to_add,
741 0,
Tao Ma78f30c32008-11-12 08:27:00 +0800742 ctxt->data_ac,
743 ctxt->meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700744 &why);
Tao Ma85db90e2008-11-12 08:27:01 +0800745 if (status < 0) {
746 mlog_errno(status);
Tao Maf56654c2008-08-18 17:38:48 +0800747 goto leave;
748 }
749
Joel Becker19b801f2008-12-09 14:36:50 -0800750 status = ocfs2_journal_dirty(handle, vb->vb_bh);
Tao Maf56654c2008-08-18 17:38:48 +0800751 if (status < 0) {
752 mlog_errno(status);
753 goto leave;
754 }
755
Joel Becker19b801f2008-12-09 14:36:50 -0800756 clusters_to_add -= le32_to_cpu(vb->vb_xv->xr_clusters) - prev_clusters;
Tao Maf56654c2008-08-18 17:38:48 +0800757
Tao Ma85db90e2008-11-12 08:27:01 +0800758 /*
759 * We should have already allocated enough space before the transaction,
760 * so no need to restart.
761 */
762 BUG_ON(why != RESTART_NONE || clusters_to_add);
Tao Maf56654c2008-08-18 17:38:48 +0800763
764leave:
Tao Maf56654c2008-08-18 17:38:48 +0800765
766 return status;
767}
768
769static int __ocfs2_remove_xattr_range(struct inode *inode,
Joel Beckerd72cc722008-12-09 14:30:41 -0800770 struct ocfs2_xattr_value_buf *vb,
Tao Maf56654c2008-08-18 17:38:48 +0800771 u32 cpos, u32 phys_cpos, u32 len,
Tao Ma492a8a32009-08-18 11:43:17 +0800772 unsigned int ext_flags,
Tao Ma78f30c32008-11-12 08:27:00 +0800773 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800774{
775 int ret;
776 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
Tao Ma85db90e2008-11-12 08:27:01 +0800777 handle_t *handle = ctxt->handle;
Joel Beckerf99b9b72008-08-20 19:36:33 -0700778 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +0800779
Joel Becker5e404e92009-02-13 03:54:22 -0800780 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700781
Joel Becker0cf2f762009-02-12 16:41:25 -0800782 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
Joel Beckerd72cc722008-12-09 14:30:41 -0800783 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Maf56654c2008-08-18 17:38:48 +0800784 if (ret) {
785 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800786 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800787 }
788
Joel Beckerdbdcf6a2009-02-13 03:41:26 -0800789 ret = ocfs2_remove_extent(handle, &et, cpos, len, ctxt->meta_ac,
Tao Ma78f30c32008-11-12 08:27:00 +0800790 &ctxt->dealloc);
Tao Maf56654c2008-08-18 17:38:48 +0800791 if (ret) {
792 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800793 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800794 }
795
Joel Beckerd72cc722008-12-09 14:30:41 -0800796 le32_add_cpu(&vb->vb_xv->xr_clusters, -len);
Tao Maf56654c2008-08-18 17:38:48 +0800797
Joel Beckerd72cc722008-12-09 14:30:41 -0800798 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
Tao Maf56654c2008-08-18 17:38:48 +0800799 if (ret) {
800 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800801 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800802 }
803
Tao Ma492a8a32009-08-18 11:43:17 +0800804 if (ext_flags & OCFS2_EXT_REFCOUNTED)
805 ret = ocfs2_decrease_refcount(inode, handle,
806 ocfs2_blocks_to_clusters(inode->i_sb,
807 phys_blkno),
808 len, ctxt->meta_ac, &ctxt->dealloc, 1);
809 else
810 ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc,
811 phys_blkno, len);
Tao Maf56654c2008-08-18 17:38:48 +0800812 if (ret)
813 mlog_errno(ret);
814
Tao Maf56654c2008-08-18 17:38:48 +0800815out:
Tao Maf56654c2008-08-18 17:38:48 +0800816 return ret;
817}
818
819static int ocfs2_xattr_shrink_size(struct inode *inode,
820 u32 old_clusters,
821 u32 new_clusters,
Joel Becker19b801f2008-12-09 14:36:50 -0800822 struct ocfs2_xattr_value_buf *vb,
Tao Ma78f30c32008-11-12 08:27:00 +0800823 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800824{
825 int ret = 0;
Tao Ma492a8a32009-08-18 11:43:17 +0800826 unsigned int ext_flags;
Tao Maf56654c2008-08-18 17:38:48 +0800827 u32 trunc_len, cpos, phys_cpos, alloc_size;
828 u64 block;
Tao Maf56654c2008-08-18 17:38:48 +0800829
830 if (old_clusters <= new_clusters)
831 return 0;
832
833 cpos = new_clusters;
834 trunc_len = old_clusters - new_clusters;
835 while (trunc_len) {
836 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
Joel Beckerd72cc722008-12-09 14:30:41 -0800837 &alloc_size,
Tao Ma492a8a32009-08-18 11:43:17 +0800838 &vb->vb_xv->xr_list, &ext_flags);
Tao Maf56654c2008-08-18 17:38:48 +0800839 if (ret) {
840 mlog_errno(ret);
841 goto out;
842 }
843
844 if (alloc_size > trunc_len)
845 alloc_size = trunc_len;
846
Joel Becker19b801f2008-12-09 14:36:50 -0800847 ret = __ocfs2_remove_xattr_range(inode, vb, cpos,
Tao Maf56654c2008-08-18 17:38:48 +0800848 phys_cpos, alloc_size,
Tao Ma492a8a32009-08-18 11:43:17 +0800849 ext_flags, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800850 if (ret) {
851 mlog_errno(ret);
852 goto out;
853 }
854
855 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
Joel Becker8cb471e2009-02-10 20:00:41 -0800856 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode),
857 block, alloc_size);
Tao Maf56654c2008-08-18 17:38:48 +0800858 cpos += alloc_size;
859 trunc_len -= alloc_size;
860 }
861
862out:
Tao Maf56654c2008-08-18 17:38:48 +0800863 return ret;
864}
865
866static int ocfs2_xattr_value_truncate(struct inode *inode,
Joel Beckerb3e5d372008-12-09 15:01:04 -0800867 struct ocfs2_xattr_value_buf *vb,
Tao Ma78f30c32008-11-12 08:27:00 +0800868 int len,
869 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800870{
871 int ret;
872 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
Joel Beckerb3e5d372008-12-09 15:01:04 -0800873 u32 old_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
Tao Maf56654c2008-08-18 17:38:48 +0800874
875 if (new_clusters == old_clusters)
876 return 0;
877
878 if (new_clusters > old_clusters)
879 ret = ocfs2_xattr_extend_allocation(inode,
880 new_clusters - old_clusters,
Joel Beckerb3e5d372008-12-09 15:01:04 -0800881 vb, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800882 else
883 ret = ocfs2_xattr_shrink_size(inode,
884 old_clusters, new_clusters,
Joel Beckerb3e5d372008-12-09 15:01:04 -0800885 vb, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800886
887 return ret;
888}
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800889
Tao Ma936b8832008-10-09 23:06:14 +0800890static int ocfs2_xattr_list_entry(char *buffer, size_t size,
891 size_t *result, const char *prefix,
892 const char *name, int name_len)
893{
894 char *p = buffer + *result;
895 int prefix_len = strlen(prefix);
896 int total_len = prefix_len + name_len + 1;
897
898 *result += total_len;
899
900 /* we are just looking for how big our buffer needs to be */
901 if (!size)
902 return 0;
903
904 if (*result > size)
905 return -ERANGE;
906
907 memcpy(p, prefix, prefix_len);
908 memcpy(p + prefix_len, name, name_len);
909 p[prefix_len + name_len] = '\0';
910
911 return 0;
912}
913
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800914static int ocfs2_xattr_list_entries(struct inode *inode,
915 struct ocfs2_xattr_header *header,
916 char *buffer, size_t buffer_size)
917{
Tao Ma936b8832008-10-09 23:06:14 +0800918 size_t result = 0;
919 int i, type, ret;
920 const char *prefix, *name;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800921
922 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
923 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
Tao Ma936b8832008-10-09 23:06:14 +0800924 type = ocfs2_xattr_get_type(entry);
925 prefix = ocfs2_xattr_prefix(type);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800926
Tao Ma936b8832008-10-09 23:06:14 +0800927 if (prefix) {
928 name = (const char *)header +
929 le16_to_cpu(entry->xe_name_offset);
930
931 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
932 &result, prefix, name,
933 entry->xe_name_len);
934 if (ret)
935 return ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800936 }
937 }
938
Tao Ma936b8832008-10-09 23:06:14 +0800939 return result;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800940}
941
Tao Ma8b2c0db2009-08-18 11:43:49 +0800942int ocfs2_has_inline_xattr_value_outside(struct inode *inode,
943 struct ocfs2_dinode *di)
944{
945 struct ocfs2_xattr_header *xh;
946 int i;
947
948 xh = (struct ocfs2_xattr_header *)
949 ((void *)di + inode->i_sb->s_blocksize -
950 le16_to_cpu(di->i_xattr_inline_size));
951
952 for (i = 0; i < le16_to_cpu(xh->xh_count); i++)
953 if (!ocfs2_xattr_is_local(&xh->xh_entries[i]))
954 return 1;
955
956 return 0;
957}
958
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800959static int ocfs2_xattr_ibody_list(struct inode *inode,
960 struct ocfs2_dinode *di,
961 char *buffer,
962 size_t buffer_size)
963{
964 struct ocfs2_xattr_header *header = NULL;
965 struct ocfs2_inode_info *oi = OCFS2_I(inode);
966 int ret = 0;
967
968 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
969 return ret;
970
971 header = (struct ocfs2_xattr_header *)
972 ((void *)di + inode->i_sb->s_blocksize -
973 le16_to_cpu(di->i_xattr_inline_size));
974
975 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
976
977 return ret;
978}
979
980static int ocfs2_xattr_block_list(struct inode *inode,
981 struct ocfs2_dinode *di,
982 char *buffer,
983 size_t buffer_size)
984{
985 struct buffer_head *blk_bh = NULL;
Tao Ma0c044f02008-08-18 17:38:50 +0800986 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800987 int ret = 0;
988
989 if (!di->i_xattr_loc)
990 return ret;
991
Joel Becker4ae1d692008-11-13 14:49:18 -0800992 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
993 &blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800994 if (ret < 0) {
995 mlog_errno(ret);
996 return ret;
997 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800998
Tao Ma0c044f02008-08-18 17:38:50 +0800999 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Ma0c044f02008-08-18 17:38:50 +08001000 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1001 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
1002 ret = ocfs2_xattr_list_entries(inode, header,
1003 buffer, buffer_size);
Tao Ma47bca492009-08-18 11:43:42 +08001004 } else
1005 ret = ocfs2_xattr_tree_list_index_block(inode, blk_bh,
Tao Ma0c044f02008-08-18 17:38:50 +08001006 buffer, buffer_size);
Joel Becker4ae1d692008-11-13 14:49:18 -08001007
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001008 brelse(blk_bh);
1009
1010 return ret;
1011}
1012
1013ssize_t ocfs2_listxattr(struct dentry *dentry,
1014 char *buffer,
1015 size_t size)
1016{
1017 int ret = 0, i_ret = 0, b_ret = 0;
1018 struct buffer_head *di_bh = NULL;
1019 struct ocfs2_dinode *di = NULL;
1020 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
1021
Tiger Yang8154da32008-08-18 17:11:46 +08001022 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
1023 return -EOPNOTSUPP;
1024
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001025 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1026 return ret;
1027
1028 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
1029 if (ret < 0) {
1030 mlog_errno(ret);
1031 return ret;
1032 }
1033
1034 di = (struct ocfs2_dinode *)di_bh->b_data;
1035
1036 down_read(&oi->ip_xattr_sem);
1037 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
1038 if (i_ret < 0)
1039 b_ret = 0;
1040 else {
1041 if (buffer) {
1042 buffer += i_ret;
1043 size -= i_ret;
1044 }
1045 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
1046 buffer, size);
1047 if (b_ret < 0)
1048 i_ret = 0;
1049 }
1050 up_read(&oi->ip_xattr_sem);
1051 ocfs2_inode_unlock(dentry->d_inode, 0);
1052
1053 brelse(di_bh);
1054
1055 return i_ret + b_ret;
1056}
1057
1058static int ocfs2_xattr_find_entry(int name_index,
1059 const char *name,
1060 struct ocfs2_xattr_search *xs)
1061{
1062 struct ocfs2_xattr_entry *entry;
1063 size_t name_len;
1064 int i, cmp = 1;
1065
1066 if (name == NULL)
1067 return -EINVAL;
1068
1069 name_len = strlen(name);
1070 entry = xs->here;
1071 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
1072 cmp = name_index - ocfs2_xattr_get_type(entry);
1073 if (!cmp)
1074 cmp = name_len - entry->xe_name_len;
1075 if (!cmp)
1076 cmp = memcmp(name, (xs->base +
1077 le16_to_cpu(entry->xe_name_offset)),
1078 name_len);
1079 if (cmp == 0)
1080 break;
1081 entry += 1;
1082 }
1083 xs->here = entry;
1084
1085 return cmp ? -ENODATA : 0;
1086}
1087
1088static int ocfs2_xattr_get_value_outside(struct inode *inode,
Tao Ma589dc262008-08-18 17:38:51 +08001089 struct ocfs2_xattr_value_root *xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001090 void *buffer,
1091 size_t len)
1092{
1093 u32 cpos, p_cluster, num_clusters, bpc, clusters;
1094 u64 blkno;
1095 int i, ret = 0;
1096 size_t cplen, blocksize;
1097 struct buffer_head *bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001098 struct ocfs2_extent_list *el;
1099
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001100 el = &xv->xr_list;
1101 clusters = le32_to_cpu(xv->xr_clusters);
1102 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1103 blocksize = inode->i_sb->s_blocksize;
1104
1105 cpos = 0;
1106 while (cpos < clusters) {
1107 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
Tao Ma1061f9c2009-08-18 11:41:57 +08001108 &num_clusters, el, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001109 if (ret) {
1110 mlog_errno(ret);
1111 goto out;
1112 }
1113
1114 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1115 /* Copy ocfs2_xattr_value */
1116 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
Joel Becker8cb471e2009-02-10 20:00:41 -08001117 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1118 &bh, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001119 if (ret) {
1120 mlog_errno(ret);
1121 goto out;
1122 }
1123
1124 cplen = len >= blocksize ? blocksize : len;
1125 memcpy(buffer, bh->b_data, cplen);
1126 len -= cplen;
1127 buffer += cplen;
1128
1129 brelse(bh);
1130 bh = NULL;
1131 if (len == 0)
1132 break;
1133 }
1134 cpos += num_clusters;
1135 }
1136out:
1137 return ret;
1138}
1139
1140static int ocfs2_xattr_ibody_get(struct inode *inode,
1141 int name_index,
1142 const char *name,
1143 void *buffer,
1144 size_t buffer_size,
1145 struct ocfs2_xattr_search *xs)
1146{
1147 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1148 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
Tao Ma589dc262008-08-18 17:38:51 +08001149 struct ocfs2_xattr_value_root *xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001150 size_t size;
1151 int ret = 0;
1152
1153 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
1154 return -ENODATA;
1155
1156 xs->end = (void *)di + inode->i_sb->s_blocksize;
1157 xs->header = (struct ocfs2_xattr_header *)
1158 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1159 xs->base = (void *)xs->header;
1160 xs->here = xs->header->xh_entries;
1161
1162 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1163 if (ret)
1164 return ret;
1165 size = le64_to_cpu(xs->here->xe_value_size);
1166 if (buffer) {
1167 if (size > buffer_size)
1168 return -ERANGE;
1169 if (ocfs2_xattr_is_local(xs->here)) {
1170 memcpy(buffer, (void *)xs->base +
1171 le16_to_cpu(xs->here->xe_name_offset) +
1172 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
1173 } else {
Tao Ma589dc262008-08-18 17:38:51 +08001174 xv = (struct ocfs2_xattr_value_root *)
1175 (xs->base + le16_to_cpu(
1176 xs->here->xe_name_offset) +
1177 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
1178 ret = ocfs2_xattr_get_value_outside(inode, xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001179 buffer, size);
1180 if (ret < 0) {
1181 mlog_errno(ret);
1182 return ret;
1183 }
1184 }
1185 }
1186
1187 return size;
1188}
1189
1190static int ocfs2_xattr_block_get(struct inode *inode,
1191 int name_index,
1192 const char *name,
1193 void *buffer,
1194 size_t buffer_size,
1195 struct ocfs2_xattr_search *xs)
1196{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001197 struct ocfs2_xattr_block *xb;
Tao Ma589dc262008-08-18 17:38:51 +08001198 struct ocfs2_xattr_value_root *xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001199 size_t size;
Subrata Modak44d8e4e2009-07-14 01:19:31 +05301200 int ret = -ENODATA, name_offset, name_len, i;
1201 int uninitialized_var(block_off);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001202
Joel Beckerba937122008-10-24 19:13:20 -07001203 xs->bucket = ocfs2_xattr_bucket_new(inode);
1204 if (!xs->bucket) {
1205 ret = -ENOMEM;
1206 mlog_errno(ret);
1207 goto cleanup;
1208 }
Tao Ma589dc262008-08-18 17:38:51 +08001209
Joel Becker54f443f2008-10-20 18:43:07 -07001210 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
1211 if (ret) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001212 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001213 goto cleanup;
1214 }
1215
Tiger Yang6c1e1832008-11-02 19:04:21 +08001216 if (xs->not_found) {
1217 ret = -ENODATA;
1218 goto cleanup;
1219 }
1220
Joel Becker54f443f2008-10-20 18:43:07 -07001221 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001222 size = le64_to_cpu(xs->here->xe_value_size);
1223 if (buffer) {
1224 ret = -ERANGE;
1225 if (size > buffer_size)
1226 goto cleanup;
Tao Ma589dc262008-08-18 17:38:51 +08001227
1228 name_offset = le16_to_cpu(xs->here->xe_name_offset);
1229 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
1230 i = xs->here - xs->header->xh_entries;
1231
1232 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
Tao Mafd68a892009-08-18 11:43:21 +08001233 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Joel Beckerba937122008-10-24 19:13:20 -07001234 bucket_xh(xs->bucket),
Tao Ma589dc262008-08-18 17:38:51 +08001235 i,
1236 &block_off,
1237 &name_offset);
Joel Beckerba937122008-10-24 19:13:20 -07001238 xs->base = bucket_block(xs->bucket, block_off);
Tao Ma589dc262008-08-18 17:38:51 +08001239 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001240 if (ocfs2_xattr_is_local(xs->here)) {
1241 memcpy(buffer, (void *)xs->base +
Tao Ma589dc262008-08-18 17:38:51 +08001242 name_offset + name_len, size);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001243 } else {
Tao Ma589dc262008-08-18 17:38:51 +08001244 xv = (struct ocfs2_xattr_value_root *)
1245 (xs->base + name_offset + name_len);
1246 ret = ocfs2_xattr_get_value_outside(inode, xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001247 buffer, size);
1248 if (ret < 0) {
1249 mlog_errno(ret);
1250 goto cleanup;
1251 }
1252 }
1253 }
1254 ret = size;
1255cleanup:
Joel Beckerba937122008-10-24 19:13:20 -07001256 ocfs2_xattr_bucket_free(xs->bucket);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001257
Joel Becker54f443f2008-10-20 18:43:07 -07001258 brelse(xs->xattr_bh);
1259 xs->xattr_bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001260 return ret;
1261}
1262
Tiger Yang4e3e9d02008-11-14 11:16:53 +08001263int ocfs2_xattr_get_nolock(struct inode *inode,
1264 struct buffer_head *di_bh,
Tiger Yang0030e002008-10-23 16:33:33 +08001265 int name_index,
1266 const char *name,
1267 void *buffer,
1268 size_t buffer_size)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001269{
1270 int ret;
1271 struct ocfs2_dinode *di = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001272 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1273 struct ocfs2_xattr_search xis = {
1274 .not_found = -ENODATA,
1275 };
1276 struct ocfs2_xattr_search xbs = {
1277 .not_found = -ENODATA,
1278 };
1279
Tiger Yang8154da32008-08-18 17:11:46 +08001280 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1281 return -EOPNOTSUPP;
1282
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001283 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1284 ret = -ENODATA;
1285
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001286 xis.inode_bh = xbs.inode_bh = di_bh;
1287 di = (struct ocfs2_dinode *)di_bh->b_data;
1288
1289 down_read(&oi->ip_xattr_sem);
1290 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1291 buffer_size, &xis);
Tiger Yang6c1e1832008-11-02 19:04:21 +08001292 if (ret == -ENODATA && di->i_xattr_loc)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001293 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1294 buffer_size, &xbs);
1295 up_read(&oi->ip_xattr_sem);
Tiger Yang4e3e9d02008-11-14 11:16:53 +08001296
1297 return ret;
1298}
1299
1300/* ocfs2_xattr_get()
1301 *
1302 * Copy an extended attribute into the buffer provided.
1303 * Buffer is NULL to compute the size of buffer required.
1304 */
1305static int ocfs2_xattr_get(struct inode *inode,
1306 int name_index,
1307 const char *name,
1308 void *buffer,
1309 size_t buffer_size)
1310{
1311 int ret;
1312 struct buffer_head *di_bh = NULL;
1313
1314 ret = ocfs2_inode_lock(inode, &di_bh, 0);
1315 if (ret < 0) {
1316 mlog_errno(ret);
1317 return ret;
1318 }
1319 ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
1320 name, buffer, buffer_size);
1321
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001322 ocfs2_inode_unlock(inode, 0);
1323
1324 brelse(di_bh);
1325
1326 return ret;
1327}
1328
1329static int __ocfs2_xattr_set_value_outside(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001330 handle_t *handle,
Tao Ma492a8a32009-08-18 11:43:17 +08001331 struct ocfs2_xattr_value_buf *vb,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001332 const void *value,
1333 int value_len)
1334{
Tao Ma71d548a2008-12-05 06:20:54 +08001335 int ret = 0, i, cp_len;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001336 u16 blocksize = inode->i_sb->s_blocksize;
1337 u32 p_cluster, num_clusters;
1338 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1339 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1340 u64 blkno;
1341 struct buffer_head *bh = NULL;
Tao Ma492a8a32009-08-18 11:43:17 +08001342 unsigned int ext_flags;
1343 struct ocfs2_xattr_value_root *xv = vb->vb_xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001344
1345 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1346
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001347 while (cpos < clusters) {
1348 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
Tao Ma1061f9c2009-08-18 11:41:57 +08001349 &num_clusters, &xv->xr_list,
Tao Ma492a8a32009-08-18 11:43:17 +08001350 &ext_flags);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001351 if (ret) {
1352 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001353 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001354 }
1355
Tao Ma492a8a32009-08-18 11:43:17 +08001356 BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED);
1357
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001358 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1359
1360 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
Joel Becker8cb471e2009-02-10 20:00:41 -08001361 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1362 &bh, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001363 if (ret) {
1364 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001365 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001366 }
1367
1368 ret = ocfs2_journal_access(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -08001369 INODE_CACHE(inode),
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001370 bh,
1371 OCFS2_JOURNAL_ACCESS_WRITE);
1372 if (ret < 0) {
1373 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001374 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001375 }
1376
1377 cp_len = value_len > blocksize ? blocksize : value_len;
1378 memcpy(bh->b_data, value, cp_len);
1379 value_len -= cp_len;
1380 value += cp_len;
1381 if (cp_len < blocksize)
1382 memset(bh->b_data + cp_len, 0,
1383 blocksize - cp_len);
1384
1385 ret = ocfs2_journal_dirty(handle, bh);
1386 if (ret < 0) {
1387 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001388 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001389 }
1390 brelse(bh);
1391 bh = NULL;
1392
1393 /*
1394 * XXX: do we need to empty all the following
1395 * blocks in this cluster?
1396 */
1397 if (!value_len)
1398 break;
1399 }
1400 cpos += num_clusters;
1401 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001402out:
1403 brelse(bh);
1404
1405 return ret;
1406}
1407
Joel Becker69a3e532009-08-17 12:24:39 -07001408static int ocfs2_xa_check_space_helper(int needed_space, int free_start,
1409 int num_entries)
1410{
1411 int free_space;
1412
1413 if (!needed_space)
1414 return 0;
1415
1416 free_space = free_start -
1417 sizeof(struct ocfs2_xattr_header) -
1418 (num_entries * sizeof(struct ocfs2_xattr_entry)) -
1419 OCFS2_XATTR_HEADER_GAP;
1420 if (free_space < 0)
1421 return -EIO;
1422 if (free_space < needed_space)
1423 return -ENOSPC;
1424
1425 return 0;
1426}
1427
Joel Beckercf2bc802009-08-18 13:52:38 -07001428static int ocfs2_xa_journal_access(handle_t *handle, struct ocfs2_xa_loc *loc,
1429 int type)
1430{
1431 return loc->xl_ops->xlo_journal_access(handle, loc, type);
1432}
1433
1434static void ocfs2_xa_journal_dirty(handle_t *handle, struct ocfs2_xa_loc *loc)
1435{
1436 loc->xl_ops->xlo_journal_dirty(handle, loc);
1437}
1438
Joel Becker69a3e532009-08-17 12:24:39 -07001439/* Give a pointer into the storage for the given offset */
1440static void *ocfs2_xa_offset_pointer(struct ocfs2_xa_loc *loc, int offset)
1441{
1442 BUG_ON(offset >= loc->xl_size);
1443 return loc->xl_ops->xlo_offset_pointer(loc, offset);
1444}
1445
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001446/*
Joel Becker11179f22009-08-14 16:07:44 -07001447 * Wipe the name+value pair and allow the storage to reclaim it. This
1448 * must be followed by either removal of the entry or a call to
1449 * ocfs2_xa_add_namevalue().
1450 */
1451static void ocfs2_xa_wipe_namevalue(struct ocfs2_xa_loc *loc)
1452{
1453 loc->xl_ops->xlo_wipe_namevalue(loc);
1454}
1455
Joel Becker69a3e532009-08-17 12:24:39 -07001456/*
1457 * Find lowest offset to a name+value pair. This is the start of our
1458 * downward-growing free space.
1459 */
1460static int ocfs2_xa_get_free_start(struct ocfs2_xa_loc *loc)
1461{
1462 return loc->xl_ops->xlo_get_free_start(loc);
1463}
1464
1465/* Can we reuse loc->xl_entry for xi? */
1466static int ocfs2_xa_can_reuse_entry(struct ocfs2_xa_loc *loc,
1467 struct ocfs2_xattr_info *xi)
1468{
1469 return loc->xl_ops->xlo_can_reuse(loc, xi);
1470}
1471
1472/* How much free space is needed to set the new value */
1473static int ocfs2_xa_check_space(struct ocfs2_xa_loc *loc,
1474 struct ocfs2_xattr_info *xi)
1475{
1476 return loc->xl_ops->xlo_check_space(loc, xi);
1477}
1478
1479static void ocfs2_xa_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1480{
1481 loc->xl_ops->xlo_add_entry(loc, name_hash);
1482 loc->xl_entry->xe_name_hash = cpu_to_le32(name_hash);
1483 /*
1484 * We can't leave the new entry's xe_name_offset at zero or
1485 * add_namevalue() will go nuts. We set it to the size of our
1486 * storage so that it can never be less than any other entry.
1487 */
1488 loc->xl_entry->xe_name_offset = cpu_to_le16(loc->xl_size);
1489}
1490
1491static void ocfs2_xa_add_namevalue(struct ocfs2_xa_loc *loc,
1492 struct ocfs2_xattr_info *xi)
1493{
1494 int size = namevalue_size_xi(xi);
1495 int nameval_offset;
1496 char *nameval_buf;
1497
1498 loc->xl_ops->xlo_add_namevalue(loc, size);
1499 loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
1500 loc->xl_entry->xe_name_len = xi->xi_name_len;
1501 ocfs2_xattr_set_type(loc->xl_entry, xi->xi_name_index);
1502 ocfs2_xattr_set_local(loc->xl_entry,
1503 xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE);
1504
1505 nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1506 nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
1507 memset(nameval_buf, 0, size);
1508 memcpy(nameval_buf, xi->xi_name, xi->xi_name_len);
1509}
1510
Joel Becker3fc12af2009-08-18 13:20:27 -07001511static void ocfs2_xa_fill_value_buf(struct ocfs2_xa_loc *loc,
1512 struct ocfs2_xattr_value_buf *vb)
1513{
1514 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1515 int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len);
1516
1517 /* Value bufs are for value trees */
Joel Becker73857ee2009-08-18 20:26:41 -07001518 BUG_ON(ocfs2_xattr_is_local(loc->xl_entry));
Joel Becker3fc12af2009-08-18 13:20:27 -07001519 BUG_ON(namevalue_size_xe(loc->xl_entry) !=
1520 (name_size + OCFS2_XATTR_ROOT_SIZE));
1521
1522 loc->xl_ops->xlo_fill_value_buf(loc, vb);
1523 vb->vb_xv =
1524 (struct ocfs2_xattr_value_root *)ocfs2_xa_offset_pointer(loc,
1525 nameval_offset +
1526 name_size);
1527}
1528
Joel Beckercf2bc802009-08-18 13:52:38 -07001529static int ocfs2_xa_block_journal_access(handle_t *handle,
1530 struct ocfs2_xa_loc *loc, int type)
1531{
1532 struct buffer_head *bh = loc->xl_storage;
1533 ocfs2_journal_access_func access;
1534
1535 if (loc->xl_size == (bh->b_size -
1536 offsetof(struct ocfs2_xattr_block,
1537 xb_attrs.xb_header)))
1538 access = ocfs2_journal_access_xb;
1539 else
1540 access = ocfs2_journal_access_di;
1541 return access(handle, INODE_CACHE(loc->xl_inode), bh, type);
1542}
1543
1544static void ocfs2_xa_block_journal_dirty(handle_t *handle,
1545 struct ocfs2_xa_loc *loc)
1546{
1547 struct buffer_head *bh = loc->xl_storage;
1548
1549 ocfs2_journal_dirty(handle, bh);
1550}
1551
Joel Becker11179f22009-08-14 16:07:44 -07001552static void *ocfs2_xa_block_offset_pointer(struct ocfs2_xa_loc *loc,
1553 int offset)
1554{
Joel Becker11179f22009-08-14 16:07:44 -07001555 return (char *)loc->xl_header + offset;
1556}
1557
Joel Becker69a3e532009-08-17 12:24:39 -07001558static int ocfs2_xa_block_can_reuse(struct ocfs2_xa_loc *loc,
1559 struct ocfs2_xattr_info *xi)
1560{
1561 /*
1562 * Block storage is strict. If the sizes aren't exact, we will
1563 * remove the old one and reinsert the new.
1564 */
1565 return namevalue_size_xe(loc->xl_entry) ==
1566 namevalue_size_xi(xi);
1567}
1568
1569static int ocfs2_xa_block_get_free_start(struct ocfs2_xa_loc *loc)
1570{
1571 struct ocfs2_xattr_header *xh = loc->xl_header;
1572 int i, count = le16_to_cpu(xh->xh_count);
1573 int offset, free_start = loc->xl_size;
1574
1575 for (i = 0; i < count; i++) {
1576 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1577 if (offset < free_start)
1578 free_start = offset;
1579 }
1580
1581 return free_start;
1582}
1583
1584static int ocfs2_xa_block_check_space(struct ocfs2_xa_loc *loc,
1585 struct ocfs2_xattr_info *xi)
1586{
1587 int count = le16_to_cpu(loc->xl_header->xh_count);
1588 int free_start = ocfs2_xa_get_free_start(loc);
1589 int needed_space = ocfs2_xi_entry_usage(xi);
1590
1591 /*
1592 * Block storage will reclaim the original entry before inserting
1593 * the new value, so we only need the difference. If the new
1594 * entry is smaller than the old one, we don't need anything.
1595 */
1596 if (loc->xl_entry) {
1597 /* Don't need space if we're reusing! */
1598 if (ocfs2_xa_can_reuse_entry(loc, xi))
1599 needed_space = 0;
1600 else
1601 needed_space -= ocfs2_xe_entry_usage(loc->xl_entry);
1602 }
1603 if (needed_space < 0)
1604 needed_space = 0;
1605 return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1606}
1607
Joel Becker11179f22009-08-14 16:07:44 -07001608/*
1609 * Block storage for xattrs keeps the name+value pairs compacted. When
1610 * we remove one, we have to shift any that preceded it towards the end.
1611 */
1612static void ocfs2_xa_block_wipe_namevalue(struct ocfs2_xa_loc *loc)
1613{
1614 int i, offset;
1615 int namevalue_offset, first_namevalue_offset, namevalue_size;
1616 struct ocfs2_xattr_entry *entry = loc->xl_entry;
1617 struct ocfs2_xattr_header *xh = loc->xl_header;
Joel Becker11179f22009-08-14 16:07:44 -07001618 int count = le16_to_cpu(xh->xh_count);
1619
1620 namevalue_offset = le16_to_cpu(entry->xe_name_offset);
Joel Becker199799a2009-08-14 19:04:15 -07001621 namevalue_size = namevalue_size_xe(entry);
Joel Becker69a3e532009-08-17 12:24:39 -07001622 first_namevalue_offset = ocfs2_xa_get_free_start(loc);
Joel Becker11179f22009-08-14 16:07:44 -07001623
1624 /* Shift the name+value pairs */
1625 memmove((char *)xh + first_namevalue_offset + namevalue_size,
1626 (char *)xh + first_namevalue_offset,
1627 namevalue_offset - first_namevalue_offset);
1628 memset((char *)xh + first_namevalue_offset, 0, namevalue_size);
1629
1630 /* Now tell xh->xh_entries about it */
1631 for (i = 0; i < count; i++) {
1632 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1633 if (offset < namevalue_offset)
1634 le16_add_cpu(&xh->xh_entries[i].xe_name_offset,
1635 namevalue_size);
1636 }
1637
1638 /*
1639 * Note that we don't update xh_free_start or xh_name_value_len
1640 * because they're not used in block-stored xattrs.
1641 */
1642}
1643
Joel Becker69a3e532009-08-17 12:24:39 -07001644static void ocfs2_xa_block_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1645{
1646 int count = le16_to_cpu(loc->xl_header->xh_count);
1647 loc->xl_entry = &(loc->xl_header->xh_entries[count]);
1648 le16_add_cpu(&loc->xl_header->xh_count, 1);
1649 memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1650}
1651
1652static void ocfs2_xa_block_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1653{
1654 int free_start = ocfs2_xa_get_free_start(loc);
1655
1656 loc->xl_entry->xe_name_offset = cpu_to_le16(free_start - size);
1657}
1658
Joel Becker3fc12af2009-08-18 13:20:27 -07001659static void ocfs2_xa_block_fill_value_buf(struct ocfs2_xa_loc *loc,
1660 struct ocfs2_xattr_value_buf *vb)
1661{
1662 struct buffer_head *bh = loc->xl_storage;
1663
1664 if (loc->xl_size == (bh->b_size -
1665 offsetof(struct ocfs2_xattr_block,
1666 xb_attrs.xb_header)))
1667 vb->vb_access = ocfs2_journal_access_xb;
1668 else
1669 vb->vb_access = ocfs2_journal_access_di;
1670 vb->vb_bh = bh;
1671}
1672
Joel Becker11179f22009-08-14 16:07:44 -07001673/*
1674 * Operations for xattrs stored in blocks. This includes inline inode
1675 * storage and unindexed ocfs2_xattr_blocks.
1676 */
1677static const struct ocfs2_xa_loc_operations ocfs2_xa_block_loc_ops = {
Joel Beckercf2bc802009-08-18 13:52:38 -07001678 .xlo_journal_access = ocfs2_xa_block_journal_access,
1679 .xlo_journal_dirty = ocfs2_xa_block_journal_dirty,
Joel Becker11179f22009-08-14 16:07:44 -07001680 .xlo_offset_pointer = ocfs2_xa_block_offset_pointer,
Joel Becker69a3e532009-08-17 12:24:39 -07001681 .xlo_check_space = ocfs2_xa_block_check_space,
1682 .xlo_can_reuse = ocfs2_xa_block_can_reuse,
1683 .xlo_get_free_start = ocfs2_xa_block_get_free_start,
Joel Becker11179f22009-08-14 16:07:44 -07001684 .xlo_wipe_namevalue = ocfs2_xa_block_wipe_namevalue,
Joel Becker69a3e532009-08-17 12:24:39 -07001685 .xlo_add_entry = ocfs2_xa_block_add_entry,
1686 .xlo_add_namevalue = ocfs2_xa_block_add_namevalue,
Joel Becker3fc12af2009-08-18 13:20:27 -07001687 .xlo_fill_value_buf = ocfs2_xa_block_fill_value_buf,
Joel Becker11179f22009-08-14 16:07:44 -07001688};
1689
Joel Beckercf2bc802009-08-18 13:52:38 -07001690static int ocfs2_xa_bucket_journal_access(handle_t *handle,
1691 struct ocfs2_xa_loc *loc, int type)
1692{
1693 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1694
1695 return ocfs2_xattr_bucket_journal_access(handle, bucket, type);
1696}
1697
1698static void ocfs2_xa_bucket_journal_dirty(handle_t *handle,
1699 struct ocfs2_xa_loc *loc)
1700{
1701 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1702
1703 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
1704}
1705
Joel Becker11179f22009-08-14 16:07:44 -07001706static void *ocfs2_xa_bucket_offset_pointer(struct ocfs2_xa_loc *loc,
1707 int offset)
1708{
1709 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1710 int block, block_offset;
1711
Joel Becker11179f22009-08-14 16:07:44 -07001712 /* The header is at the front of the bucket */
Joel Beckercf2bc802009-08-18 13:52:38 -07001713 block = offset >> loc->xl_inode->i_sb->s_blocksize_bits;
1714 block_offset = offset % loc->xl_inode->i_sb->s_blocksize;
Joel Becker11179f22009-08-14 16:07:44 -07001715
1716 return bucket_block(bucket, block) + block_offset;
1717}
1718
Joel Becker69a3e532009-08-17 12:24:39 -07001719static int ocfs2_xa_bucket_can_reuse(struct ocfs2_xa_loc *loc,
1720 struct ocfs2_xattr_info *xi)
1721{
1722 return namevalue_size_xe(loc->xl_entry) >=
1723 namevalue_size_xi(xi);
1724}
1725
1726static int ocfs2_xa_bucket_get_free_start(struct ocfs2_xa_loc *loc)
1727{
1728 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1729 return le16_to_cpu(bucket_xh(bucket)->xh_free_start);
1730}
1731
1732static int ocfs2_bucket_align_free_start(struct super_block *sb,
1733 int free_start, int size)
1734{
1735 /*
1736 * We need to make sure that the name+value pair fits within
1737 * one block.
1738 */
1739 if (((free_start - size) >> sb->s_blocksize_bits) !=
1740 ((free_start - 1) >> sb->s_blocksize_bits))
1741 free_start -= free_start % sb->s_blocksize;
1742
1743 return free_start;
1744}
1745
1746static int ocfs2_xa_bucket_check_space(struct ocfs2_xa_loc *loc,
1747 struct ocfs2_xattr_info *xi)
1748{
1749 int rc;
1750 int count = le16_to_cpu(loc->xl_header->xh_count);
1751 int free_start = ocfs2_xa_get_free_start(loc);
1752 int needed_space = ocfs2_xi_entry_usage(xi);
1753 int size = namevalue_size_xi(xi);
Joel Beckercf2bc802009-08-18 13:52:38 -07001754 struct super_block *sb = loc->xl_inode->i_sb;
Joel Becker69a3e532009-08-17 12:24:39 -07001755
1756 /*
1757 * Bucket storage does not reclaim name+value pairs it cannot
1758 * reuse. They live as holes until the bucket fills, and then
1759 * the bucket is defragmented. However, the bucket can reclaim
1760 * the ocfs2_xattr_entry.
1761 */
1762 if (loc->xl_entry) {
1763 /* Don't need space if we're reusing! */
1764 if (ocfs2_xa_can_reuse_entry(loc, xi))
1765 needed_space = 0;
1766 else
1767 needed_space -= sizeof(struct ocfs2_xattr_entry);
1768 }
1769 BUG_ON(needed_space < 0);
1770
1771 if (free_start < size) {
1772 if (needed_space)
1773 return -ENOSPC;
1774 } else {
1775 /*
1776 * First we check if it would fit in the first place.
1777 * Below, we align the free start to a block. This may
1778 * slide us below the minimum gap. By checking unaligned
1779 * first, we avoid that error.
1780 */
1781 rc = ocfs2_xa_check_space_helper(needed_space, free_start,
1782 count);
1783 if (rc)
1784 return rc;
1785 free_start = ocfs2_bucket_align_free_start(sb, free_start,
1786 size);
1787 }
1788 return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1789}
1790
Joel Becker11179f22009-08-14 16:07:44 -07001791static void ocfs2_xa_bucket_wipe_namevalue(struct ocfs2_xa_loc *loc)
1792{
Joel Becker199799a2009-08-14 19:04:15 -07001793 le16_add_cpu(&loc->xl_header->xh_name_value_len,
1794 -namevalue_size_xe(loc->xl_entry));
Joel Becker11179f22009-08-14 16:07:44 -07001795}
1796
Joel Becker69a3e532009-08-17 12:24:39 -07001797static void ocfs2_xa_bucket_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1798{
1799 struct ocfs2_xattr_header *xh = loc->xl_header;
1800 int count = le16_to_cpu(xh->xh_count);
1801 int low = 0, high = count - 1, tmp;
1802 struct ocfs2_xattr_entry *tmp_xe;
1803
1804 /*
1805 * We keep buckets sorted by name_hash, so we need to find
1806 * our insert place.
1807 */
1808 while (low <= high && count) {
1809 tmp = (low + high) / 2;
1810 tmp_xe = &xh->xh_entries[tmp];
1811
1812 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
1813 low = tmp + 1;
1814 else if (name_hash < le32_to_cpu(tmp_xe->xe_name_hash))
1815 high = tmp - 1;
1816 else {
1817 low = tmp;
1818 break;
1819 }
1820 }
1821
1822 if (low != count)
1823 memmove(&xh->xh_entries[low + 1],
1824 &xh->xh_entries[low],
1825 ((count - low) * sizeof(struct ocfs2_xattr_entry)));
1826
1827 le16_add_cpu(&xh->xh_count, 1);
1828 loc->xl_entry = &xh->xh_entries[low];
1829 memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1830}
1831
1832static void ocfs2_xa_bucket_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1833{
1834 int free_start = ocfs2_xa_get_free_start(loc);
1835 struct ocfs2_xattr_header *xh = loc->xl_header;
Joel Beckercf2bc802009-08-18 13:52:38 -07001836 struct super_block *sb = loc->xl_inode->i_sb;
Joel Becker69a3e532009-08-17 12:24:39 -07001837 int nameval_offset;
1838
1839 free_start = ocfs2_bucket_align_free_start(sb, free_start, size);
1840 nameval_offset = free_start - size;
1841 loc->xl_entry->xe_name_offset = cpu_to_le16(nameval_offset);
1842 xh->xh_free_start = cpu_to_le16(nameval_offset);
1843 le16_add_cpu(&xh->xh_name_value_len, size);
1844
1845}
1846
Joel Becker3fc12af2009-08-18 13:20:27 -07001847static void ocfs2_xa_bucket_fill_value_buf(struct ocfs2_xa_loc *loc,
1848 struct ocfs2_xattr_value_buf *vb)
1849{
1850 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
Joel Beckercf2bc802009-08-18 13:52:38 -07001851 struct super_block *sb = loc->xl_inode->i_sb;
Joel Becker3fc12af2009-08-18 13:20:27 -07001852 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1853 int size = namevalue_size_xe(loc->xl_entry);
1854 int block_offset = nameval_offset >> sb->s_blocksize_bits;
1855
1856 /* Values are not allowed to straddle block boundaries */
1857 BUG_ON(block_offset !=
1858 ((nameval_offset + size - 1) >> sb->s_blocksize_bits));
1859 /* We expect the bucket to be filled in */
1860 BUG_ON(!bucket->bu_bhs[block_offset]);
1861
1862 vb->vb_access = ocfs2_journal_access;
1863 vb->vb_bh = bucket->bu_bhs[block_offset];
1864}
1865
Joel Becker11179f22009-08-14 16:07:44 -07001866/* Operations for xattrs stored in buckets. */
1867static const struct ocfs2_xa_loc_operations ocfs2_xa_bucket_loc_ops = {
Joel Beckercf2bc802009-08-18 13:52:38 -07001868 .xlo_journal_access = ocfs2_xa_bucket_journal_access,
1869 .xlo_journal_dirty = ocfs2_xa_bucket_journal_dirty,
Joel Becker11179f22009-08-14 16:07:44 -07001870 .xlo_offset_pointer = ocfs2_xa_bucket_offset_pointer,
Joel Becker69a3e532009-08-17 12:24:39 -07001871 .xlo_check_space = ocfs2_xa_bucket_check_space,
1872 .xlo_can_reuse = ocfs2_xa_bucket_can_reuse,
1873 .xlo_get_free_start = ocfs2_xa_bucket_get_free_start,
Joel Becker11179f22009-08-14 16:07:44 -07001874 .xlo_wipe_namevalue = ocfs2_xa_bucket_wipe_namevalue,
Joel Becker69a3e532009-08-17 12:24:39 -07001875 .xlo_add_entry = ocfs2_xa_bucket_add_entry,
1876 .xlo_add_namevalue = ocfs2_xa_bucket_add_namevalue,
Joel Becker3fc12af2009-08-18 13:20:27 -07001877 .xlo_fill_value_buf = ocfs2_xa_bucket_fill_value_buf,
Joel Becker11179f22009-08-14 16:07:44 -07001878};
1879
Joel Becker73857ee2009-08-18 20:26:41 -07001880static int ocfs2_xa_value_truncate(struct ocfs2_xa_loc *loc, u64 bytes,
1881 struct ocfs2_xattr_set_ctxt *ctxt)
1882{
1883 int trunc_rc, access_rc;
1884 struct ocfs2_xattr_value_buf vb;
1885
1886 ocfs2_xa_fill_value_buf(loc, &vb);
1887 trunc_rc = ocfs2_xattr_value_truncate(loc->xl_inode, &vb, bytes,
1888 ctxt);
1889
1890 /*
1891 * The caller of ocfs2_xa_value_truncate() has already called
1892 * ocfs2_xa_journal_access on the loc. However, The truncate code
1893 * calls ocfs2_extend_trans(). This may commit the previous
1894 * transaction and open a new one. If this is a bucket, truncate
1895 * could leave only vb->vb_bh set up for journaling. Meanwhile,
1896 * the caller is expecting to dirty the entire bucket. So we must
1897 * reset the journal work. We do this even if truncate has failed,
1898 * as it could have failed after committing the extend.
1899 */
1900 access_rc = ocfs2_xa_journal_access(ctxt->handle, loc,
1901 OCFS2_JOURNAL_ACCESS_WRITE);
1902
1903 /* Errors in truncate take precedence */
1904 return trunc_rc ? trunc_rc : access_rc;
1905}
1906
Joel Becker11179f22009-08-14 16:07:44 -07001907static void ocfs2_xa_remove_entry(struct ocfs2_xa_loc *loc)
1908{
Joel Beckerbde1e542009-08-14 16:58:38 -07001909 int index, count;
1910 struct ocfs2_xattr_header *xh = loc->xl_header;
1911 struct ocfs2_xattr_entry *entry = loc->xl_entry;
1912
Joel Becker11179f22009-08-14 16:07:44 -07001913 ocfs2_xa_wipe_namevalue(loc);
Joel Beckerbde1e542009-08-14 16:58:38 -07001914 loc->xl_entry = NULL;
1915
1916 le16_add_cpu(&xh->xh_count, -1);
1917 count = le16_to_cpu(xh->xh_count);
1918
1919 /*
1920 * Only zero out the entry if there are more remaining. This is
1921 * important for an empty bucket, as it keeps track of the
1922 * bucket's hash value. It doesn't hurt empty block storage.
1923 */
1924 if (count) {
1925 index = ((char *)entry - (char *)&xh->xh_entries) /
1926 sizeof(struct ocfs2_xattr_entry);
1927 memmove(&xh->xh_entries[index], &xh->xh_entries[index + 1],
1928 (count - index) * sizeof(struct ocfs2_xattr_entry));
1929 memset(&xh->xh_entries[count], 0,
1930 sizeof(struct ocfs2_xattr_entry));
1931 }
Joel Becker11179f22009-08-14 16:07:44 -07001932}
1933
Joel Becker73857ee2009-08-18 20:26:41 -07001934static int ocfs2_xa_remove(struct ocfs2_xa_loc *loc,
1935 struct ocfs2_xattr_set_ctxt *ctxt)
1936{
1937 int rc = 0;
1938
1939 if (!ocfs2_xattr_is_local(loc->xl_entry)) {
1940 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
1941 if (rc) {
1942 mlog_errno(rc);
1943 goto out;
1944 }
1945 }
1946
1947 ocfs2_xa_remove_entry(loc);
1948
1949out:
1950 return rc;
1951}
1952
1953static void ocfs2_xa_install_value_root(struct ocfs2_xa_loc *loc)
1954{
1955 int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len);
1956 char *nameval_buf;
1957
1958 nameval_buf = ocfs2_xa_offset_pointer(loc,
1959 le16_to_cpu(loc->xl_entry->xe_name_offset));
1960 memcpy(nameval_buf + name_size, &def_xv, OCFS2_XATTR_ROOT_SIZE);
1961}
1962
1963/*
1964 * Take an existing entry and make it ready for the new value. This
1965 * won't allocate space, but it may free space. It should be ready for
1966 * ocfs2_xa_prepare_entry() to finish the work.
1967 */
1968static int ocfs2_xa_reuse_entry(struct ocfs2_xa_loc *loc,
1969 struct ocfs2_xattr_info *xi,
1970 struct ocfs2_xattr_set_ctxt *ctxt)
1971{
1972 int rc = 0;
1973 int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
1974 char *nameval_buf;
1975 int xe_local = ocfs2_xattr_is_local(loc->xl_entry);
1976 int xi_local = xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE;
1977
1978 BUG_ON(OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len) !=
1979 name_size);
1980
1981 nameval_buf = ocfs2_xa_offset_pointer(loc,
1982 le16_to_cpu(loc->xl_entry->xe_name_offset));
1983 if (xe_local) {
1984 memset(nameval_buf + name_size, 0,
1985 namevalue_size_xe(loc->xl_entry) - name_size);
1986 if (!xi_local)
1987 ocfs2_xa_install_value_root(loc);
1988 } else {
1989 if (xi_local) {
1990 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
1991 if (rc < 0) {
1992 mlog_errno(rc);
1993 goto out;
1994 }
1995 memset(nameval_buf + name_size, 0,
1996 namevalue_size_xe(loc->xl_entry) -
1997 name_size);
1998 } else if (le64_to_cpu(loc->xl_entry->xe_value_size) >
1999 xi->xi_value_len) {
2000 rc = ocfs2_xa_value_truncate(loc, xi->xi_value_len,
2001 ctxt);
2002 if (rc < 0) {
2003 mlog_errno(rc);
2004 goto out;
2005 }
2006 }
2007 }
2008
2009 loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
2010 ocfs2_xattr_set_local(loc->xl_entry, xi_local);
2011
2012out:
2013 return rc;
2014}
2015
Joel Becker69a3e532009-08-17 12:24:39 -07002016/*
2017 * Prepares loc->xl_entry to receive the new xattr. This includes
2018 * properly setting up the name+value pair region. If loc->xl_entry
2019 * already exists, it will take care of modifying it appropriately.
2020 * This also includes deleting entries, but don't call this to remove
2021 * a non-existant entry. That's just a bug.
2022 *
2023 * Note that this modifies the data. You did journal_access already,
2024 * right?
2025 */
2026static int ocfs2_xa_prepare_entry(struct ocfs2_xa_loc *loc,
2027 struct ocfs2_xattr_info *xi,
Joel Becker73857ee2009-08-18 20:26:41 -07002028 u32 name_hash,
2029 struct ocfs2_xattr_set_ctxt *ctxt)
Joel Becker69a3e532009-08-17 12:24:39 -07002030{
2031 int rc = 0;
Joel Becker69a3e532009-08-17 12:24:39 -07002032
2033 if (!xi->xi_value) {
Joel Becker73857ee2009-08-18 20:26:41 -07002034 rc = ocfs2_xa_remove(loc, ctxt);
Joel Becker69a3e532009-08-17 12:24:39 -07002035 goto out;
2036 }
2037
2038 rc = ocfs2_xa_check_space(loc, xi);
2039 if (rc)
2040 goto out;
2041
2042 if (loc->xl_entry) {
2043 if (ocfs2_xa_can_reuse_entry(loc, xi)) {
Joel Becker73857ee2009-08-18 20:26:41 -07002044 rc = ocfs2_xa_reuse_entry(loc, xi, ctxt);
2045 if (rc)
2046 goto out;
2047 goto alloc_value;
Joel Becker69a3e532009-08-17 12:24:39 -07002048 }
2049
Joel Becker73857ee2009-08-18 20:26:41 -07002050 if (!ocfs2_xattr_is_local(loc->xl_entry)) {
2051 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
2052 if (rc) {
2053 mlog_errno(rc);
2054 goto out;
2055 }
2056 }
Joel Becker69a3e532009-08-17 12:24:39 -07002057 ocfs2_xa_wipe_namevalue(loc);
2058 } else
2059 ocfs2_xa_add_entry(loc, name_hash);
2060
2061 /*
2062 * If we get here, we have a blank entry. Fill it. We grow our
2063 * name+value pair back from the end.
2064 */
2065 ocfs2_xa_add_namevalue(loc, xi);
Joel Becker73857ee2009-08-18 20:26:41 -07002066 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
2067 ocfs2_xa_install_value_root(loc);
2068
2069alloc_value:
2070 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2071 rc = ocfs2_xa_value_truncate(loc, xi->xi_value_len, ctxt);
2072 if (rc < 0)
2073 mlog_errno(rc);
2074 }
Joel Becker69a3e532009-08-17 12:24:39 -07002075
2076out:
2077 return rc;
2078}
2079
2080/*
Joel Becker73857ee2009-08-18 20:26:41 -07002081 * Store the value portion of the name+value pair. This will skip
2082 * values that are stored externally. Their tree roots were set up
2083 * by ocfs2_xa_prepare_entry().
Joel Becker69a3e532009-08-17 12:24:39 -07002084 */
Joel Becker73857ee2009-08-18 20:26:41 -07002085static int ocfs2_xa_store_value(struct ocfs2_xa_loc *loc,
2086 struct ocfs2_xattr_info *xi,
2087 struct ocfs2_xattr_set_ctxt *ctxt)
Joel Becker69a3e532009-08-17 12:24:39 -07002088{
Joel Becker73857ee2009-08-18 20:26:41 -07002089 int rc = 0;
Joel Becker69a3e532009-08-17 12:24:39 -07002090 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
2091 int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
Joel Becker69a3e532009-08-17 12:24:39 -07002092 char *nameval_buf;
Joel Becker73857ee2009-08-18 20:26:41 -07002093 struct ocfs2_xattr_value_buf vb;
Joel Becker69a3e532009-08-17 12:24:39 -07002094
2095 if (!xi->xi_value)
Joel Becker73857ee2009-08-18 20:26:41 -07002096 goto out;
Joel Becker69a3e532009-08-17 12:24:39 -07002097
2098 nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
Joel Becker73857ee2009-08-18 20:26:41 -07002099 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2100 ocfs2_xa_fill_value_buf(loc, &vb);
2101 rc = __ocfs2_xattr_set_value_outside(loc->xl_inode,
2102 ctxt->handle, &vb,
2103 xi->xi_value,
2104 xi->xi_value_len);
2105 } else
2106 memcpy(nameval_buf + name_size, xi->xi_value, xi->xi_value_len);
2107
2108out:
2109 return rc;
Joel Becker69a3e532009-08-17 12:24:39 -07002110}
2111
Joel Becker11179f22009-08-14 16:07:44 -07002112static void ocfs2_init_dinode_xa_loc(struct ocfs2_xa_loc *loc,
2113 struct inode *inode,
2114 struct buffer_head *bh,
2115 struct ocfs2_xattr_entry *entry)
2116{
2117 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
2118
Joel Beckercf2bc802009-08-18 13:52:38 -07002119 loc->xl_inode = inode;
Joel Becker11179f22009-08-14 16:07:44 -07002120 loc->xl_ops = &ocfs2_xa_block_loc_ops;
2121 loc->xl_storage = bh;
2122 loc->xl_entry = entry;
2123
2124 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
2125 loc->xl_size = le16_to_cpu(di->i_xattr_inline_size);
2126 else {
2127 BUG_ON(entry);
2128 loc->xl_size = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
2129 }
2130 loc->xl_header =
2131 (struct ocfs2_xattr_header *)(bh->b_data + bh->b_size -
2132 loc->xl_size);
2133}
2134
2135static void ocfs2_init_xattr_block_xa_loc(struct ocfs2_xa_loc *loc,
Joel Beckercf2bc802009-08-18 13:52:38 -07002136 struct inode *inode,
Joel Becker11179f22009-08-14 16:07:44 -07002137 struct buffer_head *bh,
2138 struct ocfs2_xattr_entry *entry)
2139{
2140 struct ocfs2_xattr_block *xb =
2141 (struct ocfs2_xattr_block *)bh->b_data;
2142
2143 BUG_ON(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED);
2144
Joel Beckercf2bc802009-08-18 13:52:38 -07002145 loc->xl_inode = inode;
Joel Becker11179f22009-08-14 16:07:44 -07002146 loc->xl_ops = &ocfs2_xa_block_loc_ops;
2147 loc->xl_storage = bh;
2148 loc->xl_header = &(xb->xb_attrs.xb_header);
2149 loc->xl_entry = entry;
2150 loc->xl_size = bh->b_size - offsetof(struct ocfs2_xattr_block,
2151 xb_attrs.xb_header);
2152}
2153
2154static void ocfs2_init_xattr_bucket_xa_loc(struct ocfs2_xa_loc *loc,
2155 struct ocfs2_xattr_bucket *bucket,
2156 struct ocfs2_xattr_entry *entry)
2157{
Joel Beckercf2bc802009-08-18 13:52:38 -07002158 loc->xl_inode = bucket->bu_inode;
Joel Becker11179f22009-08-14 16:07:44 -07002159 loc->xl_ops = &ocfs2_xa_bucket_loc_ops;
2160 loc->xl_storage = bucket;
2161 loc->xl_header = bucket_xh(bucket);
2162 loc->xl_entry = entry;
2163 loc->xl_size = OCFS2_XATTR_BUCKET_SIZE;
2164}
2165
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002166
2167/*
2168 * ocfs2_xattr_set_entry()
2169 *
2170 * Set extended attribute entry into inode or block.
2171 *
2172 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
Joel Becker69a3e532009-08-17 12:24:39 -07002173 * We first insert tree root(ocfs2_xattr_value_root) like a normal value,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002174 * then set value in B tree with set_value_outside().
2175 */
2176static int ocfs2_xattr_set_entry(struct inode *inode,
2177 struct ocfs2_xattr_info *xi,
2178 struct ocfs2_xattr_search *xs,
Tao Ma78f30c32008-11-12 08:27:00 +08002179 struct ocfs2_xattr_set_ctxt *ctxt,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002180 int flag)
2181{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002182 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2183 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
Tao Ma85db90e2008-11-12 08:27:01 +08002184 handle_t *handle = ctxt->handle;
Joel Becker73857ee2009-08-18 20:26:41 -07002185 int ret;
Joel Becker69a3e532009-08-17 12:24:39 -07002186 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->xi_name,
2187 xi->xi_name_len);
2188 struct ocfs2_xa_loc loc;
Joel Becker512620f2008-12-09 15:58:35 -08002189
Joel Becker73857ee2009-08-18 20:26:41 -07002190 if (!(flag & OCFS2_INLINE_XATTR_FL))
Joel Becker512620f2008-12-09 15:58:35 -08002191 BUG_ON(xs->xattr_bh == xs->inode_bh);
Joel Becker73857ee2009-08-18 20:26:41 -07002192 else
Joel Becker512620f2008-12-09 15:58:35 -08002193 BUG_ON(xs->xattr_bh != xs->inode_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002194
Joel Becker0cf2f762009-02-12 16:41:25 -08002195 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), xs->inode_bh,
Joel Becker512620f2008-12-09 15:58:35 -08002196 OCFS2_JOURNAL_ACCESS_WRITE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002197 if (ret) {
2198 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08002199 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002200 }
2201
Joel Becker69a3e532009-08-17 12:24:39 -07002202 if (xs->xattr_bh == xs->inode_bh)
2203 ocfs2_init_dinode_xa_loc(&loc, inode, xs->inode_bh,
2204 xs->not_found ? NULL : xs->here);
2205 else
Joel Beckercf2bc802009-08-18 13:52:38 -07002206 ocfs2_init_xattr_block_xa_loc(&loc, inode, xs->xattr_bh,
Joel Becker69a3e532009-08-17 12:24:39 -07002207 xs->not_found ? NULL : xs->here);
Joel Beckercf2bc802009-08-18 13:52:38 -07002208 ret = ocfs2_xa_journal_access(handle, &loc,
2209 OCFS2_JOURNAL_ACCESS_WRITE);
2210 if (ret) {
2211 mlog_errno(ret);
2212 goto out;
2213 }
Joel Becker69a3e532009-08-17 12:24:39 -07002214
Joel Becker73857ee2009-08-18 20:26:41 -07002215 ret = ocfs2_xa_prepare_entry(&loc, xi, name_hash, ctxt);
Joel Becker69a3e532009-08-17 12:24:39 -07002216 if (ret) {
2217 if (ret != -ENOSPC)
2218 mlog_errno(ret);
2219 goto out;
2220 }
Joel Becker69a3e532009-08-17 12:24:39 -07002221 xs->here = loc.xl_entry;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002222
Joel Becker73857ee2009-08-18 20:26:41 -07002223 ret = ocfs2_xa_store_value(&loc, xi, ctxt);
2224 if (ret) {
2225 mlog_errno(ret);
2226 goto out;
2227 }
2228
Joel Beckercf2bc802009-08-18 13:52:38 -07002229 ocfs2_xa_journal_dirty(handle, &loc);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002230
2231 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
2232 (flag & OCFS2_INLINE_XATTR_FL)) {
2233 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2234 unsigned int xattrsize = osb->s_xattr_inline_size;
2235
2236 /*
2237 * Adjust extent record count or inline data size
2238 * to reserve space for extended attribute.
2239 */
2240 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2241 struct ocfs2_inline_data *idata = &di->id2.i_data;
2242 le16_add_cpu(&idata->id_count, -xattrsize);
2243 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
2244 struct ocfs2_extent_list *el = &di->id2.i_list;
2245 le16_add_cpu(&el->l_count, -(xattrsize /
2246 sizeof(struct ocfs2_extent_rec)));
2247 }
2248 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
2249 }
2250 /* Update xattr flag */
2251 spin_lock(&oi->ip_lock);
2252 oi->ip_dyn_features |= flag;
2253 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2254 spin_unlock(&oi->ip_lock);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002255
2256 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
2257 if (ret < 0)
2258 mlog_errno(ret);
2259
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002260out:
2261 return ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002262}
2263
Tao Mace9c5a52009-08-18 11:43:59 +08002264/*
2265 * In xattr remove, if it is stored outside and refcounted, we may have
2266 * the chance to split the refcount tree. So need the allocators.
2267 */
2268static int ocfs2_lock_xattr_remove_allocators(struct inode *inode,
2269 struct ocfs2_xattr_value_root *xv,
2270 struct ocfs2_caching_info *ref_ci,
2271 struct buffer_head *ref_root_bh,
2272 struct ocfs2_alloc_context **meta_ac,
2273 int *ref_credits)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002274{
Tao Mace9c5a52009-08-18 11:43:59 +08002275 int ret, meta_add = 0;
2276 u32 p_cluster, num_clusters;
2277 unsigned int ext_flags;
Tao Ma78f30c32008-11-12 08:27:00 +08002278
Tao Mace9c5a52009-08-18 11:43:59 +08002279 *ref_credits = 0;
2280 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
2281 &num_clusters,
2282 &xv->xr_list,
2283 &ext_flags);
2284 if (ret) {
Tao Ma85db90e2008-11-12 08:27:01 +08002285 mlog_errno(ret);
2286 goto out;
2287 }
2288
Tao Mace9c5a52009-08-18 11:43:59 +08002289 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
2290 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002291
Tao Mace9c5a52009-08-18 11:43:59 +08002292 ret = ocfs2_refcounted_xattr_delete_need(inode, ref_ci,
2293 ref_root_bh, xv,
2294 &meta_add, ref_credits);
2295 if (ret) {
2296 mlog_errno(ret);
2297 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002298 }
2299
Tao Mace9c5a52009-08-18 11:43:59 +08002300 ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
2301 meta_add, meta_ac);
2302 if (ret)
2303 mlog_errno(ret);
2304
Tao Ma85db90e2008-11-12 08:27:01 +08002305out:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002306 return ret;
2307}
2308
Tao Mace9c5a52009-08-18 11:43:59 +08002309static int ocfs2_remove_value_outside(struct inode*inode,
2310 struct ocfs2_xattr_value_buf *vb,
2311 struct ocfs2_xattr_header *header,
2312 struct ocfs2_caching_info *ref_ci,
2313 struct buffer_head *ref_root_bh)
2314{
2315 int ret = 0, i, ref_credits;
2316 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2317 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
2318 void *val;
2319
2320 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
2321
2322 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
2323 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
2324
2325 if (ocfs2_xattr_is_local(entry))
2326 continue;
2327
2328 val = (void *)header +
2329 le16_to_cpu(entry->xe_name_offset);
2330 vb->vb_xv = (struct ocfs2_xattr_value_root *)
2331 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
2332
2333 ret = ocfs2_lock_xattr_remove_allocators(inode, vb->vb_xv,
2334 ref_ci, ref_root_bh,
2335 &ctxt.meta_ac,
2336 &ref_credits);
2337
2338 ctxt.handle = ocfs2_start_trans(osb, ref_credits +
2339 ocfs2_remove_extent_credits(osb->sb));
2340 if (IS_ERR(ctxt.handle)) {
2341 ret = PTR_ERR(ctxt.handle);
2342 mlog_errno(ret);
2343 break;
2344 }
2345
2346 ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt);
2347 if (ret < 0) {
2348 mlog_errno(ret);
2349 break;
2350 }
2351
2352 ocfs2_commit_trans(osb, ctxt.handle);
2353 if (ctxt.meta_ac) {
2354 ocfs2_free_alloc_context(ctxt.meta_ac);
2355 ctxt.meta_ac = NULL;
2356 }
2357 }
2358
2359 if (ctxt.meta_ac)
2360 ocfs2_free_alloc_context(ctxt.meta_ac);
2361 ocfs2_schedule_truncate_log_flush(osb, 1);
2362 ocfs2_run_deallocs(osb, &ctxt.dealloc);
2363 return ret;
2364}
2365
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002366static int ocfs2_xattr_ibody_remove(struct inode *inode,
Tao Mace9c5a52009-08-18 11:43:59 +08002367 struct buffer_head *di_bh,
2368 struct ocfs2_caching_info *ref_ci,
2369 struct buffer_head *ref_root_bh)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002370{
2371
2372 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2373 struct ocfs2_xattr_header *header;
2374 int ret;
Joel Becker43119012008-12-09 16:24:43 -08002375 struct ocfs2_xattr_value_buf vb = {
2376 .vb_bh = di_bh,
2377 .vb_access = ocfs2_journal_access_di,
2378 };
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002379
2380 header = (struct ocfs2_xattr_header *)
2381 ((void *)di + inode->i_sb->s_blocksize -
2382 le16_to_cpu(di->i_xattr_inline_size));
2383
Tao Mace9c5a52009-08-18 11:43:59 +08002384 ret = ocfs2_remove_value_outside(inode, &vb, header,
2385 ref_ci, ref_root_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002386
2387 return ret;
2388}
2389
Tao Mace9c5a52009-08-18 11:43:59 +08002390struct ocfs2_rm_xattr_bucket_para {
2391 struct ocfs2_caching_info *ref_ci;
2392 struct buffer_head *ref_root_bh;
2393};
2394
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002395static int ocfs2_xattr_block_remove(struct inode *inode,
Tao Mace9c5a52009-08-18 11:43:59 +08002396 struct buffer_head *blk_bh,
2397 struct ocfs2_caching_info *ref_ci,
2398 struct buffer_head *ref_root_bh)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002399{
2400 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002401 int ret = 0;
Joel Becker43119012008-12-09 16:24:43 -08002402 struct ocfs2_xattr_value_buf vb = {
2403 .vb_bh = blk_bh,
2404 .vb_access = ocfs2_journal_access_xb,
2405 };
Tao Mace9c5a52009-08-18 11:43:59 +08002406 struct ocfs2_rm_xattr_bucket_para args = {
2407 .ref_ci = ref_ci,
2408 .ref_root_bh = ref_root_bh,
2409 };
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002410
2411 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Maa3944252008-08-18 17:38:54 +08002412 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2413 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
Tao Mace9c5a52009-08-18 11:43:59 +08002414 ret = ocfs2_remove_value_outside(inode, &vb, header,
2415 ref_ci, ref_root_bh);
Tao Maa3944252008-08-18 17:38:54 +08002416 } else
Tao Ma47bca492009-08-18 11:43:42 +08002417 ret = ocfs2_iterate_xattr_index_block(inode,
2418 blk_bh,
2419 ocfs2_rm_xattr_cluster,
Tao Mace9c5a52009-08-18 11:43:59 +08002420 &args);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002421
2422 return ret;
2423}
2424
Tao Ma08413892008-08-29 09:00:19 +08002425static int ocfs2_xattr_free_block(struct inode *inode,
Tao Mace9c5a52009-08-18 11:43:59 +08002426 u64 block,
2427 struct ocfs2_caching_info *ref_ci,
2428 struct buffer_head *ref_root_bh)
Tao Ma08413892008-08-29 09:00:19 +08002429{
2430 struct inode *xb_alloc_inode;
2431 struct buffer_head *xb_alloc_bh = NULL;
2432 struct buffer_head *blk_bh = NULL;
2433 struct ocfs2_xattr_block *xb;
2434 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2435 handle_t *handle;
2436 int ret = 0;
2437 u64 blk, bg_blkno;
2438 u16 bit;
2439
Joel Becker4ae1d692008-11-13 14:49:18 -08002440 ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
Tao Ma08413892008-08-29 09:00:19 +08002441 if (ret < 0) {
2442 mlog_errno(ret);
2443 goto out;
2444 }
2445
Tao Mace9c5a52009-08-18 11:43:59 +08002446 ret = ocfs2_xattr_block_remove(inode, blk_bh, ref_ci, ref_root_bh);
Tao Ma08413892008-08-29 09:00:19 +08002447 if (ret < 0) {
2448 mlog_errno(ret);
2449 goto out;
2450 }
2451
Joel Becker4ae1d692008-11-13 14:49:18 -08002452 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Ma08413892008-08-29 09:00:19 +08002453 blk = le64_to_cpu(xb->xb_blkno);
2454 bit = le16_to_cpu(xb->xb_suballoc_bit);
2455 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2456
2457 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
2458 EXTENT_ALLOC_SYSTEM_INODE,
2459 le16_to_cpu(xb->xb_suballoc_slot));
2460 if (!xb_alloc_inode) {
2461 ret = -ENOMEM;
2462 mlog_errno(ret);
2463 goto out;
2464 }
2465 mutex_lock(&xb_alloc_inode->i_mutex);
2466
2467 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
2468 if (ret < 0) {
2469 mlog_errno(ret);
2470 goto out_mutex;
2471 }
2472
2473 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
2474 if (IS_ERR(handle)) {
2475 ret = PTR_ERR(handle);
2476 mlog_errno(ret);
2477 goto out_unlock;
2478 }
2479
2480 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
2481 bit, bg_blkno, 1);
2482 if (ret < 0)
2483 mlog_errno(ret);
2484
2485 ocfs2_commit_trans(osb, handle);
2486out_unlock:
2487 ocfs2_inode_unlock(xb_alloc_inode, 1);
2488 brelse(xb_alloc_bh);
2489out_mutex:
2490 mutex_unlock(&xb_alloc_inode->i_mutex);
2491 iput(xb_alloc_inode);
2492out:
2493 brelse(blk_bh);
2494 return ret;
2495}
2496
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002497/*
2498 * ocfs2_xattr_remove()
2499 *
2500 * Free extended attribute resources associated with this inode.
2501 */
2502int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
2503{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002504 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2505 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
Tao Mace9c5a52009-08-18 11:43:59 +08002506 struct ocfs2_refcount_tree *ref_tree = NULL;
2507 struct buffer_head *ref_root_bh = NULL;
2508 struct ocfs2_caching_info *ref_ci = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002509 handle_t *handle;
2510 int ret;
2511
Tiger Yang8154da32008-08-18 17:11:46 +08002512 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2513 return 0;
2514
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002515 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
2516 return 0;
2517
Tao Mace9c5a52009-08-18 11:43:59 +08002518 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) {
2519 ret = ocfs2_lock_refcount_tree(OCFS2_SB(inode->i_sb),
2520 le64_to_cpu(di->i_refcount_loc),
2521 1, &ref_tree, &ref_root_bh);
2522 if (ret) {
2523 mlog_errno(ret);
2524 goto out;
2525 }
2526 ref_ci = &ref_tree->rf_ci;
2527
2528 }
2529
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002530 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
Tao Mace9c5a52009-08-18 11:43:59 +08002531 ret = ocfs2_xattr_ibody_remove(inode, di_bh,
2532 ref_ci, ref_root_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002533 if (ret < 0) {
2534 mlog_errno(ret);
2535 goto out;
2536 }
2537 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002538
Tao Ma08413892008-08-29 09:00:19 +08002539 if (di->i_xattr_loc) {
2540 ret = ocfs2_xattr_free_block(inode,
Tao Mace9c5a52009-08-18 11:43:59 +08002541 le64_to_cpu(di->i_xattr_loc),
2542 ref_ci, ref_root_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002543 if (ret < 0) {
2544 mlog_errno(ret);
2545 goto out;
2546 }
2547 }
2548
2549 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
2550 OCFS2_INODE_UPDATE_CREDITS);
2551 if (IS_ERR(handle)) {
2552 ret = PTR_ERR(handle);
2553 mlog_errno(ret);
2554 goto out;
2555 }
Joel Becker0cf2f762009-02-12 16:41:25 -08002556 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker84008972008-12-09 16:11:49 -08002557 OCFS2_JOURNAL_ACCESS_WRITE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002558 if (ret) {
2559 mlog_errno(ret);
2560 goto out_commit;
2561 }
2562
Tao Ma08413892008-08-29 09:00:19 +08002563 di->i_xattr_loc = 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002564
2565 spin_lock(&oi->ip_lock);
2566 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
2567 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2568 spin_unlock(&oi->ip_lock);
2569
2570 ret = ocfs2_journal_dirty(handle, di_bh);
2571 if (ret < 0)
2572 mlog_errno(ret);
2573out_commit:
2574 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
2575out:
Tao Mace9c5a52009-08-18 11:43:59 +08002576 if (ref_tree)
2577 ocfs2_unlock_refcount_tree(OCFS2_SB(inode->i_sb), ref_tree, 1);
2578 brelse(ref_root_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002579 return ret;
2580}
2581
2582static int ocfs2_xattr_has_space_inline(struct inode *inode,
2583 struct ocfs2_dinode *di)
2584{
2585 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2586 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
2587 int free;
2588
2589 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
2590 return 0;
2591
2592 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2593 struct ocfs2_inline_data *idata = &di->id2.i_data;
2594 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
2595 } else if (ocfs2_inode_is_fast_symlink(inode)) {
2596 free = ocfs2_fast_symlink_chars(inode->i_sb) -
2597 le64_to_cpu(di->i_size);
2598 } else {
2599 struct ocfs2_extent_list *el = &di->id2.i_list;
2600 free = (le16_to_cpu(el->l_count) -
2601 le16_to_cpu(el->l_next_free_rec)) *
2602 sizeof(struct ocfs2_extent_rec);
2603 }
2604 if (free >= xattrsize)
2605 return 1;
2606
2607 return 0;
2608}
2609
2610/*
2611 * ocfs2_xattr_ibody_find()
2612 *
2613 * Find extended attribute in inode block and
2614 * fill search info into struct ocfs2_xattr_search.
2615 */
2616static int ocfs2_xattr_ibody_find(struct inode *inode,
2617 int name_index,
2618 const char *name,
2619 struct ocfs2_xattr_search *xs)
2620{
2621 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2622 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2623 int ret;
2624 int has_space = 0;
2625
2626 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2627 return 0;
2628
2629 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2630 down_read(&oi->ip_alloc_sem);
2631 has_space = ocfs2_xattr_has_space_inline(inode, di);
2632 up_read(&oi->ip_alloc_sem);
2633 if (!has_space)
2634 return 0;
2635 }
2636
2637 xs->xattr_bh = xs->inode_bh;
2638 xs->end = (void *)di + inode->i_sb->s_blocksize;
2639 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
2640 xs->header = (struct ocfs2_xattr_header *)
2641 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
2642 else
2643 xs->header = (struct ocfs2_xattr_header *)
2644 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
2645 xs->base = (void *)xs->header;
2646 xs->here = xs->header->xh_entries;
2647
2648 /* Find the named attribute. */
2649 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2650 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2651 if (ret && ret != -ENODATA)
2652 return ret;
2653 xs->not_found = ret;
2654 }
2655
2656 return 0;
2657}
2658
2659/*
2660 * ocfs2_xattr_ibody_set()
2661 *
2662 * Set, replace or remove an extended attribute into inode block.
2663 *
2664 */
2665static int ocfs2_xattr_ibody_set(struct inode *inode,
2666 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08002667 struct ocfs2_xattr_search *xs,
2668 struct ocfs2_xattr_set_ctxt *ctxt)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002669{
2670 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2671 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2672 int ret;
2673
2674 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2675 return -ENOSPC;
2676
2677 down_write(&oi->ip_alloc_sem);
2678 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2679 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2680 ret = -ENOSPC;
2681 goto out;
2682 }
2683 }
2684
Tao Ma78f30c32008-11-12 08:27:00 +08002685 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002686 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
2687out:
2688 up_write(&oi->ip_alloc_sem);
2689
2690 return ret;
2691}
2692
2693/*
2694 * ocfs2_xattr_block_find()
2695 *
2696 * Find extended attribute in external block and
2697 * fill search info into struct ocfs2_xattr_search.
2698 */
2699static int ocfs2_xattr_block_find(struct inode *inode,
2700 int name_index,
2701 const char *name,
2702 struct ocfs2_xattr_search *xs)
2703{
2704 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2705 struct buffer_head *blk_bh = NULL;
Tao Ma589dc262008-08-18 17:38:51 +08002706 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002707 int ret = 0;
2708
2709 if (!di->i_xattr_loc)
2710 return ret;
2711
Joel Becker4ae1d692008-11-13 14:49:18 -08002712 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
2713 &blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002714 if (ret < 0) {
2715 mlog_errno(ret);
2716 return ret;
2717 }
Joel Beckerf6087fb2008-10-20 18:20:43 -07002718
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002719 xs->xattr_bh = blk_bh;
Joel Becker4ae1d692008-11-13 14:49:18 -08002720 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002721
Tao Ma589dc262008-08-18 17:38:51 +08002722 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2723 xs->header = &xb->xb_attrs.xb_header;
2724 xs->base = (void *)xs->header;
2725 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
2726 xs->here = xs->header->xh_entries;
2727
2728 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2729 } else
2730 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
2731 name_index,
2732 name, xs);
2733
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002734 if (ret && ret != -ENODATA) {
2735 xs->xattr_bh = NULL;
2736 goto cleanup;
2737 }
2738 xs->not_found = ret;
2739 return 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002740cleanup:
2741 brelse(blk_bh);
2742
2743 return ret;
2744}
2745
Tao Ma5aea1f02009-08-18 11:43:24 +08002746static int ocfs2_create_xattr_block(handle_t *handle,
2747 struct inode *inode,
2748 struct buffer_head *inode_bh,
2749 struct ocfs2_alloc_context *meta_ac,
Tao Maa7fe7a32009-08-18 11:43:52 +08002750 struct buffer_head **ret_bh,
2751 int indexed)
Tao Ma5aea1f02009-08-18 11:43:24 +08002752{
2753 int ret;
2754 u16 suballoc_bit_start;
2755 u32 num_got;
2756 u64 first_blkno;
2757 struct ocfs2_dinode *di = (struct ocfs2_dinode *)inode_bh->b_data;
2758 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2759 struct buffer_head *new_bh = NULL;
2760 struct ocfs2_xattr_block *xblk;
2761
2762 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), inode_bh,
2763 OCFS2_JOURNAL_ACCESS_CREATE);
2764 if (ret < 0) {
2765 mlog_errno(ret);
2766 goto end;
2767 }
2768
2769 ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
2770 &suballoc_bit_start, &num_got,
2771 &first_blkno);
2772 if (ret < 0) {
2773 mlog_errno(ret);
2774 goto end;
2775 }
2776
2777 new_bh = sb_getblk(inode->i_sb, first_blkno);
2778 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
2779
2780 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode),
2781 new_bh,
2782 OCFS2_JOURNAL_ACCESS_CREATE);
2783 if (ret < 0) {
2784 mlog_errno(ret);
2785 goto end;
2786 }
2787
2788 /* Initialize ocfs2_xattr_block */
2789 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2790 memset(xblk, 0, inode->i_sb->s_blocksize);
2791 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
Tiger Yangb89c5422010-01-25 14:11:06 +08002792 xblk->xb_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
Tao Ma5aea1f02009-08-18 11:43:24 +08002793 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
2794 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
2795 xblk->xb_blkno = cpu_to_le64(first_blkno);
2796
Tao Maa7fe7a32009-08-18 11:43:52 +08002797 if (indexed) {
2798 struct ocfs2_xattr_tree_root *xr = &xblk->xb_attrs.xb_root;
2799 xr->xt_clusters = cpu_to_le32(1);
2800 xr->xt_last_eb_blk = 0;
2801 xr->xt_list.l_tree_depth = 0;
2802 xr->xt_list.l_count = cpu_to_le16(
2803 ocfs2_xattr_recs_per_xb(inode->i_sb));
2804 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2805 xblk->xb_flags = cpu_to_le16(OCFS2_XATTR_INDEXED);
2806 }
2807
Tao Ma5aea1f02009-08-18 11:43:24 +08002808 ret = ocfs2_journal_dirty(handle, new_bh);
2809 if (ret < 0) {
2810 mlog_errno(ret);
2811 goto end;
2812 }
2813 di->i_xattr_loc = cpu_to_le64(first_blkno);
2814 ocfs2_journal_dirty(handle, inode_bh);
2815
2816 *ret_bh = new_bh;
2817 new_bh = NULL;
2818
2819end:
2820 brelse(new_bh);
2821 return ret;
2822}
2823
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002824/*
2825 * ocfs2_xattr_block_set()
2826 *
2827 * Set, replace or remove an extended attribute into external block.
2828 *
2829 */
2830static int ocfs2_xattr_block_set(struct inode *inode,
2831 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08002832 struct ocfs2_xattr_search *xs,
2833 struct ocfs2_xattr_set_ctxt *ctxt)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002834{
2835 struct buffer_head *new_bh = NULL;
Tao Ma85db90e2008-11-12 08:27:01 +08002836 handle_t *handle = ctxt->handle;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002837 struct ocfs2_xattr_block *xblk = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002838 int ret;
2839
2840 if (!xs->xattr_bh) {
Tao Ma5aea1f02009-08-18 11:43:24 +08002841 ret = ocfs2_create_xattr_block(handle, inode, xs->inode_bh,
Tao Maa7fe7a32009-08-18 11:43:52 +08002842 ctxt->meta_ac, &new_bh, 0);
Tao Ma5aea1f02009-08-18 11:43:24 +08002843 if (ret) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002844 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08002845 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002846 }
2847
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002848 xs->xattr_bh = new_bh;
Tao Ma5aea1f02009-08-18 11:43:24 +08002849 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002850 xs->header = &xblk->xb_attrs.xb_header;
2851 xs->base = (void *)xs->header;
2852 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2853 xs->here = xs->header->xh_entries;
Tao Ma01225592008-08-18 17:38:53 +08002854 } else
2855 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2856
2857 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
2858 /* Set extended attribute into external block */
Tao Ma78f30c32008-11-12 08:27:00 +08002859 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
2860 OCFS2_HAS_XATTR_FL);
Tao Ma01225592008-08-18 17:38:53 +08002861 if (!ret || ret != -ENOSPC)
2862 goto end;
2863
Tao Ma78f30c32008-11-12 08:27:00 +08002864 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08002865 if (ret)
2866 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002867 }
2868
Tao Ma78f30c32008-11-12 08:27:00 +08002869 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08002870
2871end:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002872
2873 return ret;
2874}
2875
Tao Ma78f30c32008-11-12 08:27:00 +08002876/* Check whether the new xattr can be inserted into the inode. */
2877static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
2878 struct ocfs2_xattr_info *xi,
2879 struct ocfs2_xattr_search *xs)
2880{
Tao Ma78f30c32008-11-12 08:27:00 +08002881 struct ocfs2_xattr_entry *last;
2882 int free, i;
2883 size_t min_offs = xs->end - xs->base;
2884
2885 if (!xs->header)
2886 return 0;
2887
2888 last = xs->header->xh_entries;
2889
2890 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
2891 size_t offs = le16_to_cpu(last->xe_name_offset);
2892 if (offs < min_offs)
2893 min_offs = offs;
2894 last += 1;
2895 }
2896
Tiger Yang4442f512009-02-20 11:11:50 +08002897 free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
Tao Ma78f30c32008-11-12 08:27:00 +08002898 if (free < 0)
2899 return 0;
2900
2901 BUG_ON(!xs->not_found);
2902
Joel Becker199799a2009-08-14 19:04:15 -07002903 if (free >= (sizeof(struct ocfs2_xattr_entry) + namevalue_size_xi(xi)))
Tao Ma78f30c32008-11-12 08:27:00 +08002904 return 1;
2905
2906 return 0;
2907}
2908
2909static int ocfs2_calc_xattr_set_need(struct inode *inode,
2910 struct ocfs2_dinode *di,
2911 struct ocfs2_xattr_info *xi,
2912 struct ocfs2_xattr_search *xis,
2913 struct ocfs2_xattr_search *xbs,
2914 int *clusters_need,
Tao Ma85db90e2008-11-12 08:27:01 +08002915 int *meta_need,
2916 int *credits_need)
Tao Ma78f30c32008-11-12 08:27:00 +08002917{
2918 int ret = 0, old_in_xb = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08002919 int clusters_add = 0, meta_add = 0, credits = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08002920 struct buffer_head *bh = NULL;
2921 struct ocfs2_xattr_block *xb = NULL;
2922 struct ocfs2_xattr_entry *xe = NULL;
2923 struct ocfs2_xattr_value_root *xv = NULL;
2924 char *base = NULL;
2925 int name_offset, name_len = 0;
2926 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
Joel Becker6b240ff2009-08-14 18:02:52 -07002927 xi->xi_value_len);
Tao Ma78f30c32008-11-12 08:27:00 +08002928 u64 value_size;
2929
Tao Ma71d548a2008-12-05 06:20:54 +08002930 /*
2931 * Calculate the clusters we need to write.
2932 * No matter whether we replace an old one or add a new one,
2933 * we need this for writing.
2934 */
Joel Becker6b240ff2009-08-14 18:02:52 -07002935 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
Tao Ma71d548a2008-12-05 06:20:54 +08002936 credits += new_clusters *
2937 ocfs2_clusters_to_blocks(inode->i_sb, 1);
2938
Tao Ma78f30c32008-11-12 08:27:00 +08002939 if (xis->not_found && xbs->not_found) {
Tao Ma85db90e2008-11-12 08:27:01 +08002940 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2941
Joel Becker6b240ff2009-08-14 18:02:52 -07002942 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
Tao Ma78f30c32008-11-12 08:27:00 +08002943 clusters_add += new_clusters;
Tao Ma85db90e2008-11-12 08:27:01 +08002944 credits += ocfs2_calc_extend_credits(inode->i_sb,
2945 &def_xv.xv.xr_list,
2946 new_clusters);
2947 }
Tao Ma78f30c32008-11-12 08:27:00 +08002948
2949 goto meta_guess;
2950 }
2951
2952 if (!xis->not_found) {
2953 xe = xis->here;
2954 name_offset = le16_to_cpu(xe->xe_name_offset);
2955 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2956 base = xis->base;
Tao Ma85db90e2008-11-12 08:27:01 +08002957 credits += OCFS2_INODE_UPDATE_CREDITS;
Tao Ma78f30c32008-11-12 08:27:00 +08002958 } else {
Joel Becker970e4932008-11-13 14:49:19 -08002959 int i, block_off = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08002960 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2961 xe = xbs->here;
2962 name_offset = le16_to_cpu(xe->xe_name_offset);
2963 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2964 i = xbs->here - xbs->header->xh_entries;
2965 old_in_xb = 1;
2966
2967 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
Tao Mafd68a892009-08-18 11:43:21 +08002968 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Tao Ma78f30c32008-11-12 08:27:00 +08002969 bucket_xh(xbs->bucket),
2970 i, &block_off,
2971 &name_offset);
2972 base = bucket_block(xbs->bucket, block_off);
Tao Ma85db90e2008-11-12 08:27:01 +08002973 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2974 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08002975 base = xbs->base;
Tao Ma85db90e2008-11-12 08:27:01 +08002976 credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
2977 }
2978 }
2979
2980 /*
2981 * delete a xattr doesn't need metadata and cluster allocation.
2982 * so just calculate the credits and return.
2983 *
2984 * The credits for removing the value tree will be extended
2985 * by ocfs2_remove_extent itself.
2986 */
Joel Becker6b240ff2009-08-14 18:02:52 -07002987 if (!xi->xi_value) {
Tao Ma85db90e2008-11-12 08:27:01 +08002988 if (!ocfs2_xattr_is_local(xe))
Jan Karaa90714c2008-10-09 19:38:40 +02002989 credits += ocfs2_remove_extent_credits(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08002990
2991 goto out;
Tao Ma78f30c32008-11-12 08:27:00 +08002992 }
2993
2994 /* do cluster allocation guess first. */
2995 value_size = le64_to_cpu(xe->xe_value_size);
2996
2997 if (old_in_xb) {
2998 /*
2999 * In xattr set, we always try to set the xe in inode first,
3000 * so if it can be inserted into inode successfully, the old
3001 * one will be removed from the xattr block, and this xattr
3002 * will be inserted into inode as a new xattr in inode.
3003 */
3004 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
3005 clusters_add += new_clusters;
Jan Karaa90714c2008-10-09 19:38:40 +02003006 credits += ocfs2_remove_extent_credits(inode->i_sb) +
Tao Ma85db90e2008-11-12 08:27:01 +08003007 OCFS2_INODE_UPDATE_CREDITS;
3008 if (!ocfs2_xattr_is_local(xe))
3009 credits += ocfs2_calc_extend_credits(
3010 inode->i_sb,
3011 &def_xv.xv.xr_list,
3012 new_clusters);
Tao Ma78f30c32008-11-12 08:27:00 +08003013 goto out;
3014 }
3015 }
3016
Joel Becker6b240ff2009-08-14 18:02:52 -07003017 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
Tao Ma78f30c32008-11-12 08:27:00 +08003018 /* the new values will be stored outside. */
3019 u32 old_clusters = 0;
3020
3021 if (!ocfs2_xattr_is_local(xe)) {
3022 old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
3023 value_size);
3024 xv = (struct ocfs2_xattr_value_root *)
3025 (base + name_offset + name_len);
Tao Ma97aff522008-11-19 16:48:41 +08003026 value_size = OCFS2_XATTR_ROOT_SIZE;
Tao Ma78f30c32008-11-12 08:27:00 +08003027 } else
3028 xv = &def_xv.xv;
3029
Tao Ma85db90e2008-11-12 08:27:01 +08003030 if (old_clusters >= new_clusters) {
Jan Karaa90714c2008-10-09 19:38:40 +02003031 credits += ocfs2_remove_extent_credits(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08003032 goto out;
Tao Ma85db90e2008-11-12 08:27:01 +08003033 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08003034 meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
3035 clusters_add += new_clusters - old_clusters;
Tao Ma85db90e2008-11-12 08:27:01 +08003036 credits += ocfs2_calc_extend_credits(inode->i_sb,
3037 &xv->xr_list,
3038 new_clusters -
3039 old_clusters);
Tao Ma97aff522008-11-19 16:48:41 +08003040 if (value_size >= OCFS2_XATTR_ROOT_SIZE)
3041 goto out;
Tao Ma78f30c32008-11-12 08:27:00 +08003042 }
3043 } else {
3044 /*
3045 * Now the new value will be stored inside. So if the new
3046 * value is smaller than the size of value root or the old
3047 * value, we don't need any allocation, otherwise we have
3048 * to guess metadata allocation.
3049 */
Joel Becker6b240ff2009-08-14 18:02:52 -07003050 if ((ocfs2_xattr_is_local(xe) &&
3051 (value_size >= xi->xi_value_len)) ||
Tao Ma78f30c32008-11-12 08:27:00 +08003052 (!ocfs2_xattr_is_local(xe) &&
Joel Becker6b240ff2009-08-14 18:02:52 -07003053 OCFS2_XATTR_ROOT_SIZE >= xi->xi_value_len))
Tao Ma78f30c32008-11-12 08:27:00 +08003054 goto out;
3055 }
3056
3057meta_guess:
3058 /* calculate metadata allocation. */
3059 if (di->i_xattr_loc) {
3060 if (!xbs->xattr_bh) {
Joel Becker4ae1d692008-11-13 14:49:18 -08003061 ret = ocfs2_read_xattr_block(inode,
3062 le64_to_cpu(di->i_xattr_loc),
3063 &bh);
Tao Ma78f30c32008-11-12 08:27:00 +08003064 if (ret) {
3065 mlog_errno(ret);
3066 goto out;
3067 }
3068
3069 xb = (struct ocfs2_xattr_block *)bh->b_data;
3070 } else
3071 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
3072
Tao Ma90cb5462008-12-05 06:20:56 +08003073 /*
3074 * If there is already an xattr tree, good, we can calculate
3075 * like other b-trees. Otherwise we may have the chance of
3076 * create a tree, the credit calculation is borrowed from
3077 * ocfs2_calc_extend_credits with root_el = NULL. And the
3078 * new tree will be cluster based, so no meta is needed.
3079 */
Tao Ma78f30c32008-11-12 08:27:00 +08003080 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
3081 struct ocfs2_extent_list *el =
3082 &xb->xb_attrs.xb_root.xt_list;
3083 meta_add += ocfs2_extend_meta_needed(el);
Tao Ma85db90e2008-11-12 08:27:01 +08003084 credits += ocfs2_calc_extend_credits(inode->i_sb,
3085 el, 1);
Tao Ma90cb5462008-12-05 06:20:56 +08003086 } else
3087 credits += OCFS2_SUBALLOC_ALLOC + 1;
Tao Ma78f30c32008-11-12 08:27:00 +08003088
3089 /*
3090 * This cluster will be used either for new bucket or for
3091 * new xattr block.
3092 * If the cluster size is the same as the bucket size, one
3093 * more is needed since we may need to extend the bucket
3094 * also.
3095 */
3096 clusters_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08003097 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08003098 if (OCFS2_XATTR_BUCKET_SIZE ==
Tao Ma85db90e2008-11-12 08:27:01 +08003099 OCFS2_SB(inode->i_sb)->s_clustersize) {
3100 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08003101 clusters_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08003102 }
3103 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08003104 meta_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08003105 credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
3106 }
Tao Ma78f30c32008-11-12 08:27:00 +08003107out:
3108 if (clusters_need)
3109 *clusters_need = clusters_add;
3110 if (meta_need)
3111 *meta_need = meta_add;
Tao Ma85db90e2008-11-12 08:27:01 +08003112 if (credits_need)
3113 *credits_need = credits;
Tao Ma78f30c32008-11-12 08:27:00 +08003114 brelse(bh);
3115 return ret;
3116}
3117
3118static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
3119 struct ocfs2_dinode *di,
3120 struct ocfs2_xattr_info *xi,
3121 struct ocfs2_xattr_search *xis,
3122 struct ocfs2_xattr_search *xbs,
Tao Ma85db90e2008-11-12 08:27:01 +08003123 struct ocfs2_xattr_set_ctxt *ctxt,
Tao Ma492a8a32009-08-18 11:43:17 +08003124 int extra_meta,
Tao Ma85db90e2008-11-12 08:27:01 +08003125 int *credits)
Tao Ma78f30c32008-11-12 08:27:00 +08003126{
3127 int clusters_add, meta_add, ret;
3128 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3129
3130 memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
3131
3132 ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
3133
3134 ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
Tao Ma85db90e2008-11-12 08:27:01 +08003135 &clusters_add, &meta_add, credits);
Tao Ma78f30c32008-11-12 08:27:00 +08003136 if (ret) {
3137 mlog_errno(ret);
3138 return ret;
3139 }
3140
Tao Ma492a8a32009-08-18 11:43:17 +08003141 meta_add += extra_meta;
Tao Ma85db90e2008-11-12 08:27:01 +08003142 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
Joel Becker6b240ff2009-08-14 18:02:52 -07003143 "credits = %d\n", xi->xi_name, meta_add, clusters_add, *credits);
Tao Ma78f30c32008-11-12 08:27:00 +08003144
3145 if (meta_add) {
3146 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
3147 &ctxt->meta_ac);
3148 if (ret) {
3149 mlog_errno(ret);
3150 goto out;
3151 }
3152 }
3153
3154 if (clusters_add) {
3155 ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
3156 if (ret)
3157 mlog_errno(ret);
3158 }
3159out:
3160 if (ret) {
3161 if (ctxt->meta_ac) {
3162 ocfs2_free_alloc_context(ctxt->meta_ac);
3163 ctxt->meta_ac = NULL;
3164 }
3165
3166 /*
3167 * We cannot have an error and a non null ctxt->data_ac.
3168 */
3169 }
3170
3171 return ret;
3172}
3173
Tao Ma85db90e2008-11-12 08:27:01 +08003174static int __ocfs2_xattr_set_handle(struct inode *inode,
3175 struct ocfs2_dinode *di,
3176 struct ocfs2_xattr_info *xi,
3177 struct ocfs2_xattr_search *xis,
3178 struct ocfs2_xattr_search *xbs,
3179 struct ocfs2_xattr_set_ctxt *ctxt)
3180{
Tao Ma9f868f12008-11-19 16:48:42 +08003181 int ret = 0, credits, old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08003182
Joel Becker6b240ff2009-08-14 18:02:52 -07003183 if (!xi->xi_value) {
Tao Ma85db90e2008-11-12 08:27:01 +08003184 /* Remove existing extended attribute */
3185 if (!xis->not_found)
3186 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3187 else if (!xbs->not_found)
3188 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3189 } else {
3190 /* We always try to set extended attribute into inode first*/
3191 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3192 if (!ret && !xbs->not_found) {
3193 /*
3194 * If succeed and that extended attribute existing in
3195 * external block, then we will remove it.
3196 */
Joel Becker6b240ff2009-08-14 18:02:52 -07003197 xi->xi_value = NULL;
3198 xi->xi_value_len = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08003199
Tao Ma9f868f12008-11-19 16:48:42 +08003200 old_found = xis->not_found;
Tao Ma85db90e2008-11-12 08:27:01 +08003201 xis->not_found = -ENODATA;
3202 ret = ocfs2_calc_xattr_set_need(inode,
3203 di,
3204 xi,
3205 xis,
3206 xbs,
3207 NULL,
3208 NULL,
3209 &credits);
Tao Ma9f868f12008-11-19 16:48:42 +08003210 xis->not_found = old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08003211 if (ret) {
3212 mlog_errno(ret);
3213 goto out;
3214 }
3215
3216 ret = ocfs2_extend_trans(ctxt->handle, credits +
3217 ctxt->handle->h_buffer_credits);
3218 if (ret) {
3219 mlog_errno(ret);
3220 goto out;
3221 }
3222 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3223 } else if (ret == -ENOSPC) {
3224 if (di->i_xattr_loc && !xbs->xattr_bh) {
3225 ret = ocfs2_xattr_block_find(inode,
Joel Becker6b240ff2009-08-14 18:02:52 -07003226 xi->xi_name_index,
3227 xi->xi_name, xbs);
Tao Ma85db90e2008-11-12 08:27:01 +08003228 if (ret)
3229 goto out;
3230
Tao Ma9f868f12008-11-19 16:48:42 +08003231 old_found = xis->not_found;
Tao Ma85db90e2008-11-12 08:27:01 +08003232 xis->not_found = -ENODATA;
3233 ret = ocfs2_calc_xattr_set_need(inode,
3234 di,
3235 xi,
3236 xis,
3237 xbs,
3238 NULL,
3239 NULL,
3240 &credits);
Tao Ma9f868f12008-11-19 16:48:42 +08003241 xis->not_found = old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08003242 if (ret) {
3243 mlog_errno(ret);
3244 goto out;
3245 }
3246
3247 ret = ocfs2_extend_trans(ctxt->handle, credits +
3248 ctxt->handle->h_buffer_credits);
3249 if (ret) {
3250 mlog_errno(ret);
3251 goto out;
3252 }
3253 }
3254 /*
3255 * If no space in inode, we will set extended attribute
3256 * into external block.
3257 */
3258 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3259 if (ret)
3260 goto out;
3261 if (!xis->not_found) {
3262 /*
3263 * If succeed and that extended attribute
3264 * existing in inode, we will remove it.
3265 */
Joel Becker6b240ff2009-08-14 18:02:52 -07003266 xi->xi_value = NULL;
3267 xi->xi_value_len = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08003268 xbs->not_found = -ENODATA;
3269 ret = ocfs2_calc_xattr_set_need(inode,
3270 di,
3271 xi,
3272 xis,
3273 xbs,
3274 NULL,
3275 NULL,
3276 &credits);
3277 if (ret) {
3278 mlog_errno(ret);
3279 goto out;
3280 }
3281
3282 ret = ocfs2_extend_trans(ctxt->handle, credits +
3283 ctxt->handle->h_buffer_credits);
3284 if (ret) {
3285 mlog_errno(ret);
3286 goto out;
3287 }
3288 ret = ocfs2_xattr_ibody_set(inode, xi,
3289 xis, ctxt);
3290 }
3291 }
3292 }
3293
Tao Ma4b3f6202008-12-05 06:20:55 +08003294 if (!ret) {
3295 /* Update inode ctime. */
Joel Becker0cf2f762009-02-12 16:41:25 -08003296 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
Tao Ma89a907a2009-02-17 04:39:28 +08003297 xis->inode_bh,
3298 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma4b3f6202008-12-05 06:20:55 +08003299 if (ret) {
3300 mlog_errno(ret);
3301 goto out;
3302 }
3303
3304 inode->i_ctime = CURRENT_TIME;
3305 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
3306 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
3307 ocfs2_journal_dirty(ctxt->handle, xis->inode_bh);
3308 }
Tao Ma85db90e2008-11-12 08:27:01 +08003309out:
3310 return ret;
3311}
3312
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003313/*
Tiger Yang6c3faba2008-11-14 11:16:03 +08003314 * This function only called duing creating inode
3315 * for init security/acl xattrs of the new inode.
Tiger Yang008aafa2008-12-09 16:43:08 +08003316 * All transanction credits have been reserved in mknod.
Tiger Yang6c3faba2008-11-14 11:16:03 +08003317 */
3318int ocfs2_xattr_set_handle(handle_t *handle,
3319 struct inode *inode,
3320 struct buffer_head *di_bh,
3321 int name_index,
3322 const char *name,
3323 const void *value,
3324 size_t value_len,
3325 int flags,
3326 struct ocfs2_alloc_context *meta_ac,
3327 struct ocfs2_alloc_context *data_ac)
3328{
3329 struct ocfs2_dinode *di;
3330 int ret;
3331
3332 struct ocfs2_xattr_info xi = {
Joel Becker6b240ff2009-08-14 18:02:52 -07003333 .xi_name_index = name_index,
3334 .xi_name = name,
Joel Becker18853b92009-08-14 18:17:07 -07003335 .xi_name_len = strlen(name),
Joel Becker6b240ff2009-08-14 18:02:52 -07003336 .xi_value = value,
3337 .xi_value_len = value_len,
Tiger Yang6c3faba2008-11-14 11:16:03 +08003338 };
3339
3340 struct ocfs2_xattr_search xis = {
3341 .not_found = -ENODATA,
3342 };
3343
3344 struct ocfs2_xattr_search xbs = {
3345 .not_found = -ENODATA,
3346 };
3347
3348 struct ocfs2_xattr_set_ctxt ctxt = {
3349 .handle = handle,
3350 .meta_ac = meta_ac,
3351 .data_ac = data_ac,
3352 };
3353
3354 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3355 return -EOPNOTSUPP;
3356
Tiger Yang008aafa2008-12-09 16:43:08 +08003357 /*
3358 * In extreme situation, may need xattr bucket when
3359 * block size is too small. And we have already reserved
3360 * the credits for bucket in mknod.
3361 */
3362 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) {
3363 xbs.bucket = ocfs2_xattr_bucket_new(inode);
3364 if (!xbs.bucket) {
3365 mlog_errno(-ENOMEM);
3366 return -ENOMEM;
3367 }
3368 }
3369
Tiger Yang6c3faba2008-11-14 11:16:03 +08003370 xis.inode_bh = xbs.inode_bh = di_bh;
3371 di = (struct ocfs2_dinode *)di_bh->b_data;
3372
3373 down_write(&OCFS2_I(inode)->ip_xattr_sem);
3374
3375 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3376 if (ret)
3377 goto cleanup;
3378 if (xis.not_found) {
3379 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3380 if (ret)
3381 goto cleanup;
3382 }
3383
3384 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3385
3386cleanup:
3387 up_write(&OCFS2_I(inode)->ip_xattr_sem);
3388 brelse(xbs.xattr_bh);
Tiger Yang008aafa2008-12-09 16:43:08 +08003389 ocfs2_xattr_bucket_free(xbs.bucket);
Tiger Yang6c3faba2008-11-14 11:16:03 +08003390
3391 return ret;
3392}
3393
3394/*
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003395 * ocfs2_xattr_set()
3396 *
3397 * Set, replace or remove an extended attribute for this inode.
3398 * value is NULL to remove an existing extended attribute, else either
3399 * create or replace an extended attribute.
3400 */
3401int ocfs2_xattr_set(struct inode *inode,
3402 int name_index,
3403 const char *name,
3404 const void *value,
3405 size_t value_len,
3406 int flags)
3407{
3408 struct buffer_head *di_bh = NULL;
3409 struct ocfs2_dinode *di;
Tao Ma492a8a32009-08-18 11:43:17 +08003410 int ret, credits, ref_meta = 0, ref_credits = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08003411 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08003412 struct inode *tl_inode = osb->osb_tl_inode;
Tao Ma78f30c32008-11-12 08:27:00 +08003413 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
Tao Ma492a8a32009-08-18 11:43:17 +08003414 struct ocfs2_refcount_tree *ref_tree = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003415
3416 struct ocfs2_xattr_info xi = {
Joel Becker6b240ff2009-08-14 18:02:52 -07003417 .xi_name_index = name_index,
3418 .xi_name = name,
Joel Becker18853b92009-08-14 18:17:07 -07003419 .xi_name_len = strlen(name),
Joel Becker6b240ff2009-08-14 18:02:52 -07003420 .xi_value = value,
3421 .xi_value_len = value_len,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003422 };
3423
3424 struct ocfs2_xattr_search xis = {
3425 .not_found = -ENODATA,
3426 };
3427
3428 struct ocfs2_xattr_search xbs = {
3429 .not_found = -ENODATA,
3430 };
3431
Tiger Yang8154da32008-08-18 17:11:46 +08003432 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3433 return -EOPNOTSUPP;
3434
Joel Beckerba937122008-10-24 19:13:20 -07003435 /*
3436 * Only xbs will be used on indexed trees. xis doesn't need a
3437 * bucket.
3438 */
3439 xbs.bucket = ocfs2_xattr_bucket_new(inode);
3440 if (!xbs.bucket) {
3441 mlog_errno(-ENOMEM);
3442 return -ENOMEM;
3443 }
3444
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003445 ret = ocfs2_inode_lock(inode, &di_bh, 1);
3446 if (ret < 0) {
3447 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07003448 goto cleanup_nolock;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003449 }
3450 xis.inode_bh = xbs.inode_bh = di_bh;
3451 di = (struct ocfs2_dinode *)di_bh->b_data;
3452
3453 down_write(&OCFS2_I(inode)->ip_xattr_sem);
3454 /*
3455 * Scan inode and external block to find the same name
3456 * extended attribute and collect search infomation.
3457 */
3458 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3459 if (ret)
3460 goto cleanup;
3461 if (xis.not_found) {
3462 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3463 if (ret)
3464 goto cleanup;
3465 }
3466
3467 if (xis.not_found && xbs.not_found) {
3468 ret = -ENODATA;
3469 if (flags & XATTR_REPLACE)
3470 goto cleanup;
3471 ret = 0;
3472 if (!value)
3473 goto cleanup;
3474 } else {
3475 ret = -EEXIST;
3476 if (flags & XATTR_CREATE)
3477 goto cleanup;
3478 }
3479
Tao Ma492a8a32009-08-18 11:43:17 +08003480 /* Check whether the value is refcounted and do some prepartion. */
3481 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL &&
3482 (!xis.not_found || !xbs.not_found)) {
3483 ret = ocfs2_prepare_refcount_xattr(inode, di, &xi,
3484 &xis, &xbs, &ref_tree,
3485 &ref_meta, &ref_credits);
3486 if (ret) {
3487 mlog_errno(ret);
3488 goto cleanup;
3489 }
3490 }
Tao Ma85db90e2008-11-12 08:27:01 +08003491
3492 mutex_lock(&tl_inode->i_mutex);
3493
3494 if (ocfs2_truncate_log_needs_flush(osb)) {
3495 ret = __ocfs2_flush_truncate_log(osb);
3496 if (ret < 0) {
3497 mutex_unlock(&tl_inode->i_mutex);
3498 mlog_errno(ret);
3499 goto cleanup;
3500 }
3501 }
3502 mutex_unlock(&tl_inode->i_mutex);
3503
3504 ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
Tao Ma492a8a32009-08-18 11:43:17 +08003505 &xbs, &ctxt, ref_meta, &credits);
Tao Ma78f30c32008-11-12 08:27:00 +08003506 if (ret) {
3507 mlog_errno(ret);
3508 goto cleanup;
3509 }
3510
Tao Ma4b3f6202008-12-05 06:20:55 +08003511 /* we need to update inode's ctime field, so add credit for it. */
3512 credits += OCFS2_INODE_UPDATE_CREDITS;
Tao Ma492a8a32009-08-18 11:43:17 +08003513 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
Tao Ma85db90e2008-11-12 08:27:01 +08003514 if (IS_ERR(ctxt.handle)) {
3515 ret = PTR_ERR(ctxt.handle);
3516 mlog_errno(ret);
3517 goto cleanup;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003518 }
Tao Ma85db90e2008-11-12 08:27:01 +08003519
3520 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3521
3522 ocfs2_commit_trans(osb, ctxt.handle);
3523
Tao Ma78f30c32008-11-12 08:27:00 +08003524 if (ctxt.data_ac)
3525 ocfs2_free_alloc_context(ctxt.data_ac);
3526 if (ctxt.meta_ac)
3527 ocfs2_free_alloc_context(ctxt.meta_ac);
3528 if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
3529 ocfs2_schedule_truncate_log_flush(osb, 1);
3530 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tao Ma8b2c0db2009-08-18 11:43:49 +08003531
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003532cleanup:
Tao Ma492a8a32009-08-18 11:43:17 +08003533 if (ref_tree)
3534 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003535 up_write(&OCFS2_I(inode)->ip_xattr_sem);
Tao Ma8b2c0db2009-08-18 11:43:49 +08003536 if (!value && !ret) {
3537 ret = ocfs2_try_remove_refcount_tree(inode, di_bh);
3538 if (ret)
3539 mlog_errno(ret);
3540 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003541 ocfs2_inode_unlock(inode, 1);
Joel Beckerba937122008-10-24 19:13:20 -07003542cleanup_nolock:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003543 brelse(di_bh);
3544 brelse(xbs.xattr_bh);
Joel Beckerba937122008-10-24 19:13:20 -07003545 ocfs2_xattr_bucket_free(xbs.bucket);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003546
3547 return ret;
3548}
3549
Tao Ma0c044f02008-08-18 17:38:50 +08003550/*
3551 * Find the xattr extent rec which may contains name_hash.
3552 * e_cpos will be the first name hash of the xattr rec.
3553 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
3554 */
3555static int ocfs2_xattr_get_rec(struct inode *inode,
3556 u32 name_hash,
3557 u64 *p_blkno,
3558 u32 *e_cpos,
3559 u32 *num_clusters,
3560 struct ocfs2_extent_list *el)
3561{
3562 int ret = 0, i;
3563 struct buffer_head *eb_bh = NULL;
3564 struct ocfs2_extent_block *eb;
3565 struct ocfs2_extent_rec *rec = NULL;
3566 u64 e_blkno = 0;
3567
3568 if (el->l_tree_depth) {
Joel Beckerfacdb772009-02-12 18:08:48 -08003569 ret = ocfs2_find_leaf(INODE_CACHE(inode), el, name_hash,
3570 &eb_bh);
Tao Ma0c044f02008-08-18 17:38:50 +08003571 if (ret) {
3572 mlog_errno(ret);
3573 goto out;
3574 }
3575
3576 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
3577 el = &eb->h_list;
3578
3579 if (el->l_tree_depth) {
3580 ocfs2_error(inode->i_sb,
3581 "Inode %lu has non zero tree depth in "
3582 "xattr tree block %llu\n", inode->i_ino,
3583 (unsigned long long)eb_bh->b_blocknr);
3584 ret = -EROFS;
3585 goto out;
3586 }
3587 }
3588
3589 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
3590 rec = &el->l_recs[i];
3591
3592 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
3593 e_blkno = le64_to_cpu(rec->e_blkno);
3594 break;
3595 }
3596 }
3597
3598 if (!e_blkno) {
3599 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
3600 "record (%u, %u, 0) in xattr", inode->i_ino,
3601 le32_to_cpu(rec->e_cpos),
3602 ocfs2_rec_clusters(el, rec));
3603 ret = -EROFS;
3604 goto out;
3605 }
3606
3607 *p_blkno = le64_to_cpu(rec->e_blkno);
3608 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
3609 if (e_cpos)
3610 *e_cpos = le32_to_cpu(rec->e_cpos);
3611out:
3612 brelse(eb_bh);
3613 return ret;
3614}
3615
3616typedef int (xattr_bucket_func)(struct inode *inode,
3617 struct ocfs2_xattr_bucket *bucket,
3618 void *para);
3619
Tao Ma589dc262008-08-18 17:38:51 +08003620static int ocfs2_find_xe_in_bucket(struct inode *inode,
Joel Beckere2356a32008-10-27 15:01:54 -07003621 struct ocfs2_xattr_bucket *bucket,
Tao Ma589dc262008-08-18 17:38:51 +08003622 int name_index,
3623 const char *name,
3624 u32 name_hash,
3625 u16 *xe_index,
3626 int *found)
3627{
3628 int i, ret = 0, cmp = 1, block_off, new_offset;
Joel Beckere2356a32008-10-27 15:01:54 -07003629 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma589dc262008-08-18 17:38:51 +08003630 size_t name_len = strlen(name);
3631 struct ocfs2_xattr_entry *xe = NULL;
Tao Ma589dc262008-08-18 17:38:51 +08003632 char *xe_name;
3633
3634 /*
3635 * We don't use binary search in the bucket because there
3636 * may be multiple entries with the same name hash.
3637 */
3638 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3639 xe = &xh->xh_entries[i];
3640
3641 if (name_hash > le32_to_cpu(xe->xe_name_hash))
3642 continue;
3643 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
3644 break;
3645
3646 cmp = name_index - ocfs2_xattr_get_type(xe);
3647 if (!cmp)
3648 cmp = name_len - xe->xe_name_len;
3649 if (cmp)
3650 continue;
3651
Tao Mafd68a892009-08-18 11:43:21 +08003652 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Tao Ma589dc262008-08-18 17:38:51 +08003653 xh,
3654 i,
3655 &block_off,
3656 &new_offset);
3657 if (ret) {
3658 mlog_errno(ret);
3659 break;
3660 }
3661
Joel Becker970e4932008-11-13 14:49:19 -08003662
Joel Beckere2356a32008-10-27 15:01:54 -07003663 xe_name = bucket_block(bucket, block_off) + new_offset;
3664 if (!memcmp(name, xe_name, name_len)) {
Tao Ma589dc262008-08-18 17:38:51 +08003665 *xe_index = i;
3666 *found = 1;
3667 ret = 0;
3668 break;
3669 }
3670 }
3671
3672 return ret;
3673}
3674
3675/*
3676 * Find the specified xattr entry in a series of buckets.
3677 * This series start from p_blkno and last for num_clusters.
3678 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
3679 * the num of the valid buckets.
3680 *
3681 * Return the buffer_head this xattr should reside in. And if the xattr's
3682 * hash is in the gap of 2 buckets, return the lower bucket.
3683 */
3684static int ocfs2_xattr_bucket_find(struct inode *inode,
3685 int name_index,
3686 const char *name,
3687 u32 name_hash,
3688 u64 p_blkno,
3689 u32 first_hash,
3690 u32 num_clusters,
3691 struct ocfs2_xattr_search *xs)
3692{
3693 int ret, found = 0;
Tao Ma589dc262008-08-18 17:38:51 +08003694 struct ocfs2_xattr_header *xh = NULL;
3695 struct ocfs2_xattr_entry *xe = NULL;
3696 u16 index = 0;
3697 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3698 int low_bucket = 0, bucket, high_bucket;
Joel Beckere2356a32008-10-27 15:01:54 -07003699 struct ocfs2_xattr_bucket *search;
Tao Ma589dc262008-08-18 17:38:51 +08003700 u32 last_hash;
Joel Beckere2356a32008-10-27 15:01:54 -07003701 u64 blkno, lower_blkno = 0;
Tao Ma589dc262008-08-18 17:38:51 +08003702
Joel Beckere2356a32008-10-27 15:01:54 -07003703 search = ocfs2_xattr_bucket_new(inode);
3704 if (!search) {
3705 ret = -ENOMEM;
3706 mlog_errno(ret);
3707 goto out;
3708 }
3709
3710 ret = ocfs2_read_xattr_bucket(search, p_blkno);
Tao Ma589dc262008-08-18 17:38:51 +08003711 if (ret) {
3712 mlog_errno(ret);
3713 goto out;
3714 }
3715
Joel Beckere2356a32008-10-27 15:01:54 -07003716 xh = bucket_xh(search);
Tao Ma589dc262008-08-18 17:38:51 +08003717 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
Tao Ma589dc262008-08-18 17:38:51 +08003718 while (low_bucket <= high_bucket) {
Joel Beckere2356a32008-10-27 15:01:54 -07003719 ocfs2_xattr_bucket_relse(search);
3720
Tao Ma589dc262008-08-18 17:38:51 +08003721 bucket = (low_bucket + high_bucket) / 2;
Tao Ma589dc262008-08-18 17:38:51 +08003722 blkno = p_blkno + bucket * blk_per_bucket;
Joel Beckere2356a32008-10-27 15:01:54 -07003723 ret = ocfs2_read_xattr_bucket(search, blkno);
Tao Ma589dc262008-08-18 17:38:51 +08003724 if (ret) {
3725 mlog_errno(ret);
3726 goto out;
3727 }
3728
Joel Beckere2356a32008-10-27 15:01:54 -07003729 xh = bucket_xh(search);
Tao Ma589dc262008-08-18 17:38:51 +08003730 xe = &xh->xh_entries[0];
3731 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
3732 high_bucket = bucket - 1;
3733 continue;
3734 }
3735
3736 /*
3737 * Check whether the hash of the last entry in our
Tao Ma5a095612008-09-19 22:17:41 +08003738 * bucket is larger than the search one. for an empty
3739 * bucket, the last one is also the first one.
Tao Ma589dc262008-08-18 17:38:51 +08003740 */
Tao Ma5a095612008-09-19 22:17:41 +08003741 if (xh->xh_count)
3742 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
3743
Tao Ma589dc262008-08-18 17:38:51 +08003744 last_hash = le32_to_cpu(xe->xe_name_hash);
3745
Joel Beckere2356a32008-10-27 15:01:54 -07003746 /* record lower_blkno which may be the insert place. */
3747 lower_blkno = blkno;
Tao Ma589dc262008-08-18 17:38:51 +08003748
3749 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
3750 low_bucket = bucket + 1;
3751 continue;
3752 }
3753
3754 /* the searched xattr should reside in this bucket if exists. */
Joel Beckere2356a32008-10-27 15:01:54 -07003755 ret = ocfs2_find_xe_in_bucket(inode, search,
Tao Ma589dc262008-08-18 17:38:51 +08003756 name_index, name, name_hash,
3757 &index, &found);
3758 if (ret) {
3759 mlog_errno(ret);
3760 goto out;
3761 }
3762 break;
3763 }
3764
3765 /*
3766 * Record the bucket we have found.
3767 * When the xattr's hash value is in the gap of 2 buckets, we will
3768 * always set it to the previous bucket.
3769 */
Joel Beckere2356a32008-10-27 15:01:54 -07003770 if (!lower_blkno)
3771 lower_blkno = p_blkno;
3772
3773 /* This should be in cache - we just read it during the search */
3774 ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
3775 if (ret) {
3776 mlog_errno(ret);
3777 goto out;
Tao Ma589dc262008-08-18 17:38:51 +08003778 }
Tao Ma589dc262008-08-18 17:38:51 +08003779
Joel Beckerba937122008-10-24 19:13:20 -07003780 xs->header = bucket_xh(xs->bucket);
3781 xs->base = bucket_block(xs->bucket, 0);
Tao Ma589dc262008-08-18 17:38:51 +08003782 xs->end = xs->base + inode->i_sb->s_blocksize;
3783
3784 if (found) {
Tao Ma589dc262008-08-18 17:38:51 +08003785 xs->here = &xs->header->xh_entries[index];
3786 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
Joel Beckerba937122008-10-24 19:13:20 -07003787 (unsigned long long)bucket_blkno(xs->bucket), index);
Tao Ma589dc262008-08-18 17:38:51 +08003788 } else
3789 ret = -ENODATA;
3790
3791out:
Joel Beckere2356a32008-10-27 15:01:54 -07003792 ocfs2_xattr_bucket_free(search);
Tao Ma589dc262008-08-18 17:38:51 +08003793 return ret;
3794}
3795
3796static int ocfs2_xattr_index_block_find(struct inode *inode,
3797 struct buffer_head *root_bh,
3798 int name_index,
3799 const char *name,
3800 struct ocfs2_xattr_search *xs)
3801{
3802 int ret;
3803 struct ocfs2_xattr_block *xb =
3804 (struct ocfs2_xattr_block *)root_bh->b_data;
3805 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3806 struct ocfs2_extent_list *el = &xb_root->xt_list;
3807 u64 p_blkno = 0;
3808 u32 first_hash, num_clusters = 0;
Tao Ma2057e5c2008-10-09 23:06:13 +08003809 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
Tao Ma589dc262008-08-18 17:38:51 +08003810
3811 if (le16_to_cpu(el->l_next_free_rec) == 0)
3812 return -ENODATA;
3813
3814 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
3815 name, name_hash, name_index);
3816
3817 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
3818 &num_clusters, el);
3819 if (ret) {
3820 mlog_errno(ret);
3821 goto out;
3822 }
3823
3824 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
3825
3826 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
Mark Fashehde29c082008-10-29 14:45:30 -07003827 "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
3828 first_hash);
Tao Ma589dc262008-08-18 17:38:51 +08003829
3830 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
3831 p_blkno, first_hash, num_clusters, xs);
3832
3833out:
3834 return ret;
3835}
3836
Tao Ma0c044f02008-08-18 17:38:50 +08003837static int ocfs2_iterate_xattr_buckets(struct inode *inode,
3838 u64 blkno,
3839 u32 clusters,
3840 xattr_bucket_func *func,
3841 void *para)
3842{
Joel Becker6dde41d2008-10-24 17:16:48 -07003843 int i, ret = 0;
Tao Ma0c044f02008-08-18 17:38:50 +08003844 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
3845 u32 num_buckets = clusters * bpc;
Joel Beckerba937122008-10-24 19:13:20 -07003846 struct ocfs2_xattr_bucket *bucket;
Tao Ma0c044f02008-08-18 17:38:50 +08003847
Joel Beckerba937122008-10-24 19:13:20 -07003848 bucket = ocfs2_xattr_bucket_new(inode);
3849 if (!bucket) {
3850 mlog_errno(-ENOMEM);
3851 return -ENOMEM;
3852 }
Tao Ma0c044f02008-08-18 17:38:50 +08003853
3854 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003855 clusters, (unsigned long long)blkno);
Tao Ma0c044f02008-08-18 17:38:50 +08003856
Joel Beckerba937122008-10-24 19:13:20 -07003857 for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
3858 ret = ocfs2_read_xattr_bucket(bucket, blkno);
Tao Ma0c044f02008-08-18 17:38:50 +08003859 if (ret) {
3860 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07003861 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003862 }
3863
Tao Ma0c044f02008-08-18 17:38:50 +08003864 /*
3865 * The real bucket num in this series of blocks is stored
3866 * in the 1st bucket.
3867 */
3868 if (i == 0)
Joel Beckerba937122008-10-24 19:13:20 -07003869 num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
Tao Ma0c044f02008-08-18 17:38:50 +08003870
Mark Fashehde29c082008-10-29 14:45:30 -07003871 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
3872 (unsigned long long)blkno,
Joel Beckerba937122008-10-24 19:13:20 -07003873 le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
Tao Ma0c044f02008-08-18 17:38:50 +08003874 if (func) {
Joel Beckerba937122008-10-24 19:13:20 -07003875 ret = func(inode, bucket, para);
Tao Maa46fa682009-05-04 05:18:09 +08003876 if (ret && ret != -ERANGE)
Tao Ma0c044f02008-08-18 17:38:50 +08003877 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07003878 /* Fall through to bucket_relse() */
Tao Ma0c044f02008-08-18 17:38:50 +08003879 }
3880
Joel Beckerba937122008-10-24 19:13:20 -07003881 ocfs2_xattr_bucket_relse(bucket);
3882 if (ret)
3883 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003884 }
3885
Joel Beckerba937122008-10-24 19:13:20 -07003886 ocfs2_xattr_bucket_free(bucket);
Tao Ma0c044f02008-08-18 17:38:50 +08003887 return ret;
3888}
3889
3890struct ocfs2_xattr_tree_list {
3891 char *buffer;
3892 size_t buffer_size;
Tao Ma936b8832008-10-09 23:06:14 +08003893 size_t result;
Tao Ma0c044f02008-08-18 17:38:50 +08003894};
3895
Tao Mafd68a892009-08-18 11:43:21 +08003896static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
Tao Ma0c044f02008-08-18 17:38:50 +08003897 struct ocfs2_xattr_header *xh,
3898 int index,
3899 int *block_off,
3900 int *new_offset)
3901{
3902 u16 name_offset;
3903
3904 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
3905 return -EINVAL;
3906
3907 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
3908
Tao Mafd68a892009-08-18 11:43:21 +08003909 *block_off = name_offset >> sb->s_blocksize_bits;
3910 *new_offset = name_offset % sb->s_blocksize;
Tao Ma0c044f02008-08-18 17:38:50 +08003911
3912 return 0;
3913}
3914
3915static int ocfs2_list_xattr_bucket(struct inode *inode,
3916 struct ocfs2_xattr_bucket *bucket,
3917 void *para)
3918{
Tao Ma936b8832008-10-09 23:06:14 +08003919 int ret = 0, type;
Tao Ma0c044f02008-08-18 17:38:50 +08003920 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
Tao Ma0c044f02008-08-18 17:38:50 +08003921 int i, block_off, new_offset;
Tao Ma936b8832008-10-09 23:06:14 +08003922 const char *prefix, *name;
Tao Ma0c044f02008-08-18 17:38:50 +08003923
Joel Becker3e632942008-10-24 17:04:49 -07003924 for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
3925 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
Tao Ma936b8832008-10-09 23:06:14 +08003926 type = ocfs2_xattr_get_type(entry);
3927 prefix = ocfs2_xattr_prefix(type);
Tao Ma0c044f02008-08-18 17:38:50 +08003928
Tao Ma936b8832008-10-09 23:06:14 +08003929 if (prefix) {
Tao Mafd68a892009-08-18 11:43:21 +08003930 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Joel Becker3e632942008-10-24 17:04:49 -07003931 bucket_xh(bucket),
Tao Ma0c044f02008-08-18 17:38:50 +08003932 i,
3933 &block_off,
3934 &new_offset);
3935 if (ret)
3936 break;
Tao Ma936b8832008-10-09 23:06:14 +08003937
Joel Becker51def392008-10-24 16:57:21 -07003938 name = (const char *)bucket_block(bucket, block_off) +
Tao Ma936b8832008-10-09 23:06:14 +08003939 new_offset;
3940 ret = ocfs2_xattr_list_entry(xl->buffer,
3941 xl->buffer_size,
3942 &xl->result,
3943 prefix, name,
3944 entry->xe_name_len);
3945 if (ret)
3946 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003947 }
3948 }
3949
3950 return ret;
3951}
3952
Tao Ma47bca492009-08-18 11:43:42 +08003953static int ocfs2_iterate_xattr_index_block(struct inode *inode,
3954 struct buffer_head *blk_bh,
3955 xattr_tree_rec_func *rec_func,
3956 void *para)
Tao Ma0c044f02008-08-18 17:38:50 +08003957{
Tao Ma47bca492009-08-18 11:43:42 +08003958 struct ocfs2_xattr_block *xb =
3959 (struct ocfs2_xattr_block *)blk_bh->b_data;
3960 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
Tao Ma0c044f02008-08-18 17:38:50 +08003961 int ret = 0;
3962 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
3963 u64 p_blkno = 0;
Tao Ma0c044f02008-08-18 17:38:50 +08003964
Tao Ma47bca492009-08-18 11:43:42 +08003965 if (!el->l_next_free_rec || !rec_func)
Tao Ma0c044f02008-08-18 17:38:50 +08003966 return 0;
3967
3968 while (name_hash > 0) {
3969 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
3970 &e_cpos, &num_clusters, el);
3971 if (ret) {
3972 mlog_errno(ret);
Tao Ma47bca492009-08-18 11:43:42 +08003973 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003974 }
3975
Tao Ma47bca492009-08-18 11:43:42 +08003976 ret = rec_func(inode, blk_bh, p_blkno, e_cpos,
3977 num_clusters, para);
Tao Ma0c044f02008-08-18 17:38:50 +08003978 if (ret) {
Tao Maa46fa682009-05-04 05:18:09 +08003979 if (ret != -ERANGE)
3980 mlog_errno(ret);
Tao Ma47bca492009-08-18 11:43:42 +08003981 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003982 }
3983
3984 if (e_cpos == 0)
3985 break;
3986
3987 name_hash = e_cpos - 1;
3988 }
3989
Tao Ma47bca492009-08-18 11:43:42 +08003990 return ret;
3991
3992}
3993
3994static int ocfs2_list_xattr_tree_rec(struct inode *inode,
3995 struct buffer_head *root_bh,
3996 u64 blkno, u32 cpos, u32 len, void *para)
3997{
3998 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
3999 ocfs2_list_xattr_bucket, para);
4000}
4001
4002static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
4003 struct buffer_head *blk_bh,
4004 char *buffer,
4005 size_t buffer_size)
4006{
4007 int ret;
4008 struct ocfs2_xattr_tree_list xl = {
4009 .buffer = buffer,
4010 .buffer_size = buffer_size,
4011 .result = 0,
4012 };
4013
4014 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
4015 ocfs2_list_xattr_tree_rec, &xl);
4016 if (ret) {
4017 mlog_errno(ret);
4018 goto out;
4019 }
4020
Tao Ma936b8832008-10-09 23:06:14 +08004021 ret = xl.result;
Tao Ma0c044f02008-08-18 17:38:50 +08004022out:
4023 return ret;
4024}
Tao Ma01225592008-08-18 17:38:53 +08004025
4026static int cmp_xe(const void *a, const void *b)
4027{
4028 const struct ocfs2_xattr_entry *l = a, *r = b;
4029 u32 l_hash = le32_to_cpu(l->xe_name_hash);
4030 u32 r_hash = le32_to_cpu(r->xe_name_hash);
4031
4032 if (l_hash > r_hash)
4033 return 1;
4034 if (l_hash < r_hash)
4035 return -1;
4036 return 0;
4037}
4038
4039static void swap_xe(void *a, void *b, int size)
4040{
4041 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
4042
4043 tmp = *l;
4044 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
4045 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
4046}
4047
4048/*
4049 * When the ocfs2_xattr_block is filled up, new bucket will be created
4050 * and all the xattr entries will be moved to the new bucket.
Joel Becker178eeac2008-10-27 15:18:29 -07004051 * The header goes at the start of the bucket, and the names+values are
4052 * filled from the end. This is why *target starts as the last buffer.
Tao Ma01225592008-08-18 17:38:53 +08004053 * Note: we need to sort the entries since they are not saved in order
4054 * in the ocfs2_xattr_block.
4055 */
4056static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
4057 struct buffer_head *xb_bh,
Joel Becker178eeac2008-10-27 15:18:29 -07004058 struct ocfs2_xattr_bucket *bucket)
Tao Ma01225592008-08-18 17:38:53 +08004059{
4060 int i, blocksize = inode->i_sb->s_blocksize;
Joel Becker178eeac2008-10-27 15:18:29 -07004061 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08004062 u16 offset, size, off_change;
4063 struct ocfs2_xattr_entry *xe;
4064 struct ocfs2_xattr_block *xb =
4065 (struct ocfs2_xattr_block *)xb_bh->b_data;
4066 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
Joel Becker178eeac2008-10-27 15:18:29 -07004067 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma01225592008-08-18 17:38:53 +08004068 u16 count = le16_to_cpu(xb_xh->xh_count);
Joel Becker178eeac2008-10-27 15:18:29 -07004069 char *src = xb_bh->b_data;
4070 char *target = bucket_block(bucket, blks - 1);
Tao Ma01225592008-08-18 17:38:53 +08004071
4072 mlog(0, "cp xattr from block %llu to bucket %llu\n",
4073 (unsigned long long)xb_bh->b_blocknr,
Joel Becker178eeac2008-10-27 15:18:29 -07004074 (unsigned long long)bucket_blkno(bucket));
Tao Ma01225592008-08-18 17:38:53 +08004075
Joel Becker178eeac2008-10-27 15:18:29 -07004076 for (i = 0; i < blks; i++)
4077 memset(bucket_block(bucket, i), 0, blocksize);
4078
Tao Ma01225592008-08-18 17:38:53 +08004079 /*
4080 * Since the xe_name_offset is based on ocfs2_xattr_header,
4081 * there is a offset change corresponding to the change of
4082 * ocfs2_xattr_header's position.
4083 */
4084 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
4085 xe = &xb_xh->xh_entries[count - 1];
4086 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
4087 size = blocksize - offset;
4088
4089 /* copy all the names and values. */
Tao Ma01225592008-08-18 17:38:53 +08004090 memcpy(target + offset, src + offset, size);
4091
4092 /* Init new header now. */
4093 xh->xh_count = xb_xh->xh_count;
4094 xh->xh_num_buckets = cpu_to_le16(1);
4095 xh->xh_name_value_len = cpu_to_le16(size);
4096 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
4097
4098 /* copy all the entries. */
Joel Becker178eeac2008-10-27 15:18:29 -07004099 target = bucket_block(bucket, 0);
Tao Ma01225592008-08-18 17:38:53 +08004100 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
4101 size = count * sizeof(struct ocfs2_xattr_entry);
4102 memcpy(target + offset, (char *)xb_xh + offset, size);
4103
4104 /* Change the xe offset for all the xe because of the move. */
4105 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
4106 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
4107 for (i = 0; i < count; i++)
4108 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
4109
4110 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
4111 offset, size, off_change);
4112
4113 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
4114 cmp_xe, swap_xe);
4115}
4116
4117/*
4118 * After we move xattr from block to index btree, we have to
4119 * update ocfs2_xattr_search to the new xe and base.
4120 *
4121 * When the entry is in xattr block, xattr_bh indicates the storage place.
4122 * While if the entry is in index b-tree, "bucket" indicates the
4123 * real place of the xattr.
4124 */
Joel Becker178eeac2008-10-27 15:18:29 -07004125static void ocfs2_xattr_update_xattr_search(struct inode *inode,
4126 struct ocfs2_xattr_search *xs,
4127 struct buffer_head *old_bh)
Tao Ma01225592008-08-18 17:38:53 +08004128{
Tao Ma01225592008-08-18 17:38:53 +08004129 char *buf = old_bh->b_data;
4130 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
4131 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
Joel Becker178eeac2008-10-27 15:18:29 -07004132 int i;
Tao Ma01225592008-08-18 17:38:53 +08004133
Joel Beckerba937122008-10-24 19:13:20 -07004134 xs->header = bucket_xh(xs->bucket);
Joel Becker178eeac2008-10-27 15:18:29 -07004135 xs->base = bucket_block(xs->bucket, 0);
Tao Ma01225592008-08-18 17:38:53 +08004136 xs->end = xs->base + inode->i_sb->s_blocksize;
4137
Joel Becker178eeac2008-10-27 15:18:29 -07004138 if (xs->not_found)
4139 return;
Tao Ma01225592008-08-18 17:38:53 +08004140
Joel Becker178eeac2008-10-27 15:18:29 -07004141 i = xs->here - old_xh->xh_entries;
4142 xs->here = &xs->header->xh_entries[i];
Tao Ma01225592008-08-18 17:38:53 +08004143}
4144
4145static int ocfs2_xattr_create_index_block(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +08004146 struct ocfs2_xattr_search *xs,
4147 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004148{
Tao Ma85db90e2008-11-12 08:27:01 +08004149 int ret;
Tao Ma01225592008-08-18 17:38:53 +08004150 u32 bit_off, len;
4151 u64 blkno;
Tao Ma85db90e2008-11-12 08:27:01 +08004152 handle_t *handle = ctxt->handle;
Tao Ma01225592008-08-18 17:38:53 +08004153 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4154 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Tao Ma01225592008-08-18 17:38:53 +08004155 struct buffer_head *xb_bh = xs->xattr_bh;
4156 struct ocfs2_xattr_block *xb =
4157 (struct ocfs2_xattr_block *)xb_bh->b_data;
4158 struct ocfs2_xattr_tree_root *xr;
4159 u16 xb_flags = le16_to_cpu(xb->xb_flags);
Tao Ma01225592008-08-18 17:38:53 +08004160
4161 mlog(0, "create xattr index block for %llu\n",
4162 (unsigned long long)xb_bh->b_blocknr);
4163
4164 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
Joel Becker178eeac2008-10-27 15:18:29 -07004165 BUG_ON(!xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004166
Tao Ma01225592008-08-18 17:38:53 +08004167 /*
4168 * XXX:
4169 * We can use this lock for now, and maybe move to a dedicated mutex
4170 * if performance becomes a problem later.
4171 */
4172 down_write(&oi->ip_alloc_sem);
4173
Joel Becker0cf2f762009-02-12 16:41:25 -08004174 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), xb_bh,
Joel Becker84008972008-12-09 16:11:49 -08004175 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004176 if (ret) {
4177 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004178 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004179 }
4180
Tao Ma78f30c32008-11-12 08:27:00 +08004181 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac,
4182 1, 1, &bit_off, &len);
Tao Ma01225592008-08-18 17:38:53 +08004183 if (ret) {
4184 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004185 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004186 }
4187
4188 /*
4189 * The bucket may spread in many blocks, and
4190 * we will only touch the 1st block and the last block
4191 * in the whole bucket(one for entry and one for data).
4192 */
4193 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
4194
Mark Fashehde29c082008-10-29 14:45:30 -07004195 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
4196 (unsigned long long)blkno);
Tao Ma01225592008-08-18 17:38:53 +08004197
Joel Becker178eeac2008-10-27 15:18:29 -07004198 ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
Tao Ma01225592008-08-18 17:38:53 +08004199 if (ret) {
4200 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004201 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004202 }
4203
Joel Becker178eeac2008-10-27 15:18:29 -07004204 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
4205 OCFS2_JOURNAL_ACCESS_CREATE);
Joel Beckerbd60bd32008-10-20 18:25:56 -07004206 if (ret) {
4207 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004208 goto out;
Joel Beckerbd60bd32008-10-20 18:25:56 -07004209 }
Tao Ma01225592008-08-18 17:38:53 +08004210
Joel Becker178eeac2008-10-27 15:18:29 -07004211 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
4212 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
4213
4214 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
4215
Tao Ma01225592008-08-18 17:38:53 +08004216 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
4217 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
4218 offsetof(struct ocfs2_xattr_block, xb_attrs));
4219
4220 xr = &xb->xb_attrs.xb_root;
4221 xr->xt_clusters = cpu_to_le32(1);
4222 xr->xt_last_eb_blk = 0;
4223 xr->xt_list.l_tree_depth = 0;
4224 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
4225 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
4226
4227 xr->xt_list.l_recs[0].e_cpos = 0;
4228 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
4229 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
4230
4231 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
4232
Tao Ma85db90e2008-11-12 08:27:01 +08004233 ocfs2_journal_dirty(handle, xb_bh);
Tao Ma01225592008-08-18 17:38:53 +08004234
Tao Ma85db90e2008-11-12 08:27:01 +08004235out:
Tao Ma01225592008-08-18 17:38:53 +08004236 up_write(&oi->ip_alloc_sem);
4237
Tao Ma01225592008-08-18 17:38:53 +08004238 return ret;
4239}
4240
4241static int cmp_xe_offset(const void *a, const void *b)
4242{
4243 const struct ocfs2_xattr_entry *l = a, *r = b;
4244 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
4245 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
4246
4247 if (l_name_offset < r_name_offset)
4248 return 1;
4249 if (l_name_offset > r_name_offset)
4250 return -1;
4251 return 0;
4252}
4253
4254/*
4255 * defrag a xattr bucket if we find that the bucket has some
4256 * holes beteen name/value pairs.
4257 * We will move all the name/value pairs to the end of the bucket
4258 * so that we can spare some space for insertion.
4259 */
4260static int ocfs2_defrag_xattr_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004261 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004262 struct ocfs2_xattr_bucket *bucket)
4263{
4264 int ret, i;
Joel Becker199799a2009-08-14 19:04:15 -07004265 size_t end, offset, len;
Tao Ma01225592008-08-18 17:38:53 +08004266 struct ocfs2_xattr_header *xh;
4267 char *entries, *buf, *bucket_buf = NULL;
Joel Becker9c7759a2008-10-24 16:21:03 -07004268 u64 blkno = bucket_blkno(bucket);
Tao Ma01225592008-08-18 17:38:53 +08004269 u16 xh_free_start;
Tao Ma01225592008-08-18 17:38:53 +08004270 size_t blocksize = inode->i_sb->s_blocksize;
Tao Ma01225592008-08-18 17:38:53 +08004271 struct ocfs2_xattr_entry *xe;
Tao Ma01225592008-08-18 17:38:53 +08004272
4273 /*
4274 * In order to make the operation more efficient and generic,
4275 * we copy all the blocks into a contiguous memory and do the
4276 * defragment there, so if anything is error, we will not touch
4277 * the real block.
4278 */
4279 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
4280 if (!bucket_buf) {
4281 ret = -EIO;
4282 goto out;
4283 }
4284
Joel Becker161d6f32008-10-27 15:25:18 -07004285 buf = bucket_buf;
Tao Ma1c32a2f2008-11-06 08:10:47 +08004286 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4287 memcpy(buf, bucket_block(bucket, i), blocksize);
Joel Becker161d6f32008-10-27 15:25:18 -07004288
Tao Ma1c32a2f2008-11-06 08:10:47 +08004289 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
Joel Becker161d6f32008-10-27 15:25:18 -07004290 OCFS2_JOURNAL_ACCESS_WRITE);
4291 if (ret < 0) {
4292 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004293 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004294 }
4295
4296 xh = (struct ocfs2_xattr_header *)bucket_buf;
4297 entries = (char *)xh->xh_entries;
4298 xh_free_start = le16_to_cpu(xh->xh_free_start);
4299
4300 mlog(0, "adjust xattr bucket in %llu, count = %u, "
4301 "xh_free_start = %u, xh_name_value_len = %u.\n",
Mark Fashehde29c082008-10-29 14:45:30 -07004302 (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
4303 xh_free_start, le16_to_cpu(xh->xh_name_value_len));
Tao Ma01225592008-08-18 17:38:53 +08004304
4305 /*
4306 * sort all the entries by their offset.
4307 * the largest will be the first, so that we can
4308 * move them to the end one by one.
4309 */
4310 sort(entries, le16_to_cpu(xh->xh_count),
4311 sizeof(struct ocfs2_xattr_entry),
4312 cmp_xe_offset, swap_xe);
4313
4314 /* Move all name/values to the end of the bucket. */
4315 xe = xh->xh_entries;
4316 end = OCFS2_XATTR_BUCKET_SIZE;
4317 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
4318 offset = le16_to_cpu(xe->xe_name_offset);
Joel Becker199799a2009-08-14 19:04:15 -07004319 len = namevalue_size_xe(xe);
Tao Ma01225592008-08-18 17:38:53 +08004320
4321 /*
4322 * We must make sure that the name/value pair
4323 * exist in the same block. So adjust end to
4324 * the previous block end if needed.
4325 */
4326 if (((end - len) / blocksize !=
4327 (end - 1) / blocksize))
4328 end = end - end % blocksize;
4329
4330 if (end > offset + len) {
4331 memmove(bucket_buf + end - len,
4332 bucket_buf + offset, len);
4333 xe->xe_name_offset = cpu_to_le16(end - len);
4334 }
4335
4336 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
4337 "bucket %llu\n", (unsigned long long)blkno);
4338
4339 end -= len;
4340 }
4341
4342 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
4343 "bucket %llu\n", (unsigned long long)blkno);
4344
4345 if (xh_free_start == end)
Tao Ma85db90e2008-11-12 08:27:01 +08004346 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004347
4348 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
4349 xh->xh_free_start = cpu_to_le16(end);
4350
4351 /* sort the entries by their name_hash. */
4352 sort(entries, le16_to_cpu(xh->xh_count),
4353 sizeof(struct ocfs2_xattr_entry),
4354 cmp_xe, swap_xe);
4355
4356 buf = bucket_buf;
Tao Ma1c32a2f2008-11-06 08:10:47 +08004357 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4358 memcpy(bucket_block(bucket, i), buf, blocksize);
4359 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
Tao Ma01225592008-08-18 17:38:53 +08004360
Tao Ma01225592008-08-18 17:38:53 +08004361out:
Tao Ma01225592008-08-18 17:38:53 +08004362 kfree(bucket_buf);
4363 return ret;
4364}
4365
4366/*
Joel Beckerb5c03e72008-11-25 19:58:16 -08004367 * prev_blkno points to the start of an existing extent. new_blkno
4368 * points to a newly allocated extent. Because we know each of our
4369 * clusters contains more than bucket, we can easily split one cluster
4370 * at a bucket boundary. So we take the last cluster of the existing
4371 * extent and split it down the middle. We move the last half of the
4372 * buckets in the last cluster of the existing extent over to the new
4373 * extent.
Tao Ma01225592008-08-18 17:38:53 +08004374 *
Joel Beckerb5c03e72008-11-25 19:58:16 -08004375 * first_bh is the buffer at prev_blkno so we can update the existing
4376 * extent's bucket count. header_bh is the bucket were we were hoping
4377 * to insert our xattr. If the bucket move places the target in the new
4378 * extent, we'll update first_bh and header_bh after modifying the old
4379 * extent.
4380 *
4381 * first_hash will be set as the 1st xe's name_hash in the new extent.
Tao Ma01225592008-08-18 17:38:53 +08004382 */
4383static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
4384 handle_t *handle,
Joel Becker41cb8142008-11-26 14:25:21 -08004385 struct ocfs2_xattr_bucket *first,
4386 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08004387 u64 new_blkno,
Tao Ma01225592008-08-18 17:38:53 +08004388 u32 num_clusters,
4389 u32 *first_hash)
4390{
Joel Beckerc58b6032008-11-26 13:36:24 -08004391 int ret;
Joel Becker41cb8142008-11-26 14:25:21 -08004392 struct super_block *sb = inode->i_sb;
4393 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb);
4394 int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
Joel Beckerb5c03e72008-11-25 19:58:16 -08004395 int to_move = num_buckets / 2;
Joel Beckerc58b6032008-11-26 13:36:24 -08004396 u64 src_blkno;
Joel Becker41cb8142008-11-26 14:25:21 -08004397 u64 last_cluster_blkno = bucket_blkno(first) +
4398 ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1));
Tao Ma01225592008-08-18 17:38:53 +08004399
Joel Becker41cb8142008-11-26 14:25:21 -08004400 BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets);
4401 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize);
Tao Ma01225592008-08-18 17:38:53 +08004402
Tao Ma01225592008-08-18 17:38:53 +08004403 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
Joel Beckerc58b6032008-11-26 13:36:24 -08004404 (unsigned long long)last_cluster_blkno, (unsigned long long)new_blkno);
Tao Ma01225592008-08-18 17:38:53 +08004405
Joel Becker41cb8142008-11-26 14:25:21 -08004406 ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first),
Joel Beckerc58b6032008-11-26 13:36:24 -08004407 last_cluster_blkno, new_blkno,
4408 to_move, first_hash);
Joel Beckerb5c03e72008-11-25 19:58:16 -08004409 if (ret) {
4410 mlog_errno(ret);
4411 goto out;
4412 }
4413
Joel Beckerc58b6032008-11-26 13:36:24 -08004414 /* This is the first bucket that got moved */
4415 src_blkno = last_cluster_blkno + (to_move * blks_per_bucket);
4416
Tao Ma01225592008-08-18 17:38:53 +08004417 /*
Joel Beckerc58b6032008-11-26 13:36:24 -08004418 * If the target bucket was part of the moved buckets, we need to
Joel Becker41cb8142008-11-26 14:25:21 -08004419 * update first and target.
Joel Beckerb5c03e72008-11-25 19:58:16 -08004420 */
Joel Becker41cb8142008-11-26 14:25:21 -08004421 if (bucket_blkno(target) >= src_blkno) {
Joel Beckerb5c03e72008-11-25 19:58:16 -08004422 /* Find the block for the new target bucket */
4423 src_blkno = new_blkno +
Joel Becker41cb8142008-11-26 14:25:21 -08004424 (bucket_blkno(target) - src_blkno);
4425
4426 ocfs2_xattr_bucket_relse(first);
4427 ocfs2_xattr_bucket_relse(target);
Joel Beckerb5c03e72008-11-25 19:58:16 -08004428
4429 /*
Joel Beckerc58b6032008-11-26 13:36:24 -08004430 * These shouldn't fail - the buffers are in the
Joel Beckerb5c03e72008-11-25 19:58:16 -08004431 * journal from ocfs2_cp_xattr_bucket().
4432 */
Joel Becker41cb8142008-11-26 14:25:21 -08004433 ret = ocfs2_read_xattr_bucket(first, new_blkno);
Joel Beckerc58b6032008-11-26 13:36:24 -08004434 if (ret) {
4435 mlog_errno(ret);
4436 goto out;
4437 }
Joel Becker41cb8142008-11-26 14:25:21 -08004438 ret = ocfs2_read_xattr_bucket(target, src_blkno);
4439 if (ret)
Joel Beckerb5c03e72008-11-25 19:58:16 -08004440 mlog_errno(ret);
Joel Beckerb5c03e72008-11-25 19:58:16 -08004441
Joel Beckerb5c03e72008-11-25 19:58:16 -08004442 }
4443
Tao Ma01225592008-08-18 17:38:53 +08004444out:
Tao Ma01225592008-08-18 17:38:53 +08004445 return ret;
4446}
4447
Tao Ma01225592008-08-18 17:38:53 +08004448/*
Tao Ma80bcaf32008-10-27 06:06:24 +08004449 * Find the suitable pos when we divide a bucket into 2.
4450 * We have to make sure the xattrs with the same hash value exist
4451 * in the same bucket.
4452 *
4453 * If this ocfs2_xattr_header covers more than one hash value, find a
4454 * place where the hash value changes. Try to find the most even split.
4455 * The most common case is that all entries have different hash values,
4456 * and the first check we make will find a place to split.
Tao Ma01225592008-08-18 17:38:53 +08004457 */
Tao Ma80bcaf32008-10-27 06:06:24 +08004458static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
4459{
4460 struct ocfs2_xattr_entry *entries = xh->xh_entries;
4461 int count = le16_to_cpu(xh->xh_count);
4462 int delta, middle = count / 2;
4463
4464 /*
4465 * We start at the middle. Each step gets farther away in both
4466 * directions. We therefore hit the change in hash value
4467 * nearest to the middle. Note that this loop does not execute for
4468 * count < 2.
4469 */
4470 for (delta = 0; delta < middle; delta++) {
4471 /* Let's check delta earlier than middle */
4472 if (cmp_xe(&entries[middle - delta - 1],
4473 &entries[middle - delta]))
4474 return middle - delta;
4475
4476 /* For even counts, don't walk off the end */
4477 if ((middle + delta + 1) == count)
4478 continue;
4479
4480 /* Now try delta past middle */
4481 if (cmp_xe(&entries[middle + delta],
4482 &entries[middle + delta + 1]))
4483 return middle + delta + 1;
4484 }
4485
4486 /* Every entry had the same hash */
4487 return count;
4488}
4489
4490/*
4491 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
4492 * first_hash will record the 1st hash of the new bucket.
4493 *
4494 * Normally half of the xattrs will be moved. But we have to make
4495 * sure that the xattrs with the same hash value are stored in the
4496 * same bucket. If all the xattrs in this bucket have the same hash
4497 * value, the new bucket will be initialized as an empty one and the
4498 * first_hash will be initialized as (hash_value+1).
4499 */
4500static int ocfs2_divide_xattr_bucket(struct inode *inode,
4501 handle_t *handle,
4502 u64 blk,
4503 u64 new_blk,
4504 u32 *first_hash,
4505 int new_bucket_head)
Tao Ma01225592008-08-18 17:38:53 +08004506{
4507 int ret, i;
Joel Becker199799a2009-08-14 19:04:15 -07004508 int count, start, len, name_value_len = 0, name_offset = 0;
Joel Beckerba937122008-10-24 19:13:20 -07004509 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
Tao Ma01225592008-08-18 17:38:53 +08004510 struct ocfs2_xattr_header *xh;
4511 struct ocfs2_xattr_entry *xe;
4512 int blocksize = inode->i_sb->s_blocksize;
4513
Tao Ma80bcaf32008-10-27 06:06:24 +08004514 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07004515 (unsigned long long)blk, (unsigned long long)new_blk);
Tao Ma01225592008-08-18 17:38:53 +08004516
Joel Beckerba937122008-10-24 19:13:20 -07004517 s_bucket = ocfs2_xattr_bucket_new(inode);
4518 t_bucket = ocfs2_xattr_bucket_new(inode);
4519 if (!s_bucket || !t_bucket) {
4520 ret = -ENOMEM;
4521 mlog_errno(ret);
4522 goto out;
4523 }
Tao Ma01225592008-08-18 17:38:53 +08004524
Joel Beckerba937122008-10-24 19:13:20 -07004525 ret = ocfs2_read_xattr_bucket(s_bucket, blk);
Tao Ma01225592008-08-18 17:38:53 +08004526 if (ret) {
4527 mlog_errno(ret);
4528 goto out;
4529 }
4530
Joel Beckerba937122008-10-24 19:13:20 -07004531 ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004532 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004533 if (ret) {
4534 mlog_errno(ret);
4535 goto out;
4536 }
4537
Joel Becker784b8162008-10-24 17:33:40 -07004538 /*
4539 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
4540 * there's no need to read it.
4541 */
Joel Beckerba937122008-10-24 19:13:20 -07004542 ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
Tao Ma01225592008-08-18 17:38:53 +08004543 if (ret) {
4544 mlog_errno(ret);
4545 goto out;
4546 }
4547
Joel Becker2b656c12008-11-25 19:00:15 -08004548 /*
4549 * Hey, if we're overwriting t_bucket, what difference does
4550 * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the
4551 * same part of ocfs2_cp_xattr_bucket().
4552 */
Joel Beckerba937122008-10-24 19:13:20 -07004553 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004554 new_bucket_head ?
4555 OCFS2_JOURNAL_ACCESS_CREATE :
4556 OCFS2_JOURNAL_ACCESS_WRITE);
4557 if (ret) {
4558 mlog_errno(ret);
4559 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004560 }
4561
Joel Beckerba937122008-10-24 19:13:20 -07004562 xh = bucket_xh(s_bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08004563 count = le16_to_cpu(xh->xh_count);
4564 start = ocfs2_xattr_find_divide_pos(xh);
4565
4566 if (start == count) {
4567 xe = &xh->xh_entries[start-1];
4568
4569 /*
4570 * initialized a new empty bucket here.
4571 * The hash value is set as one larger than
4572 * that of the last entry in the previous bucket.
4573 */
Joel Beckerba937122008-10-24 19:13:20 -07004574 for (i = 0; i < t_bucket->bu_blocks; i++)
4575 memset(bucket_block(t_bucket, i), 0, blocksize);
Tao Ma80bcaf32008-10-27 06:06:24 +08004576
Joel Beckerba937122008-10-24 19:13:20 -07004577 xh = bucket_xh(t_bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08004578 xh->xh_free_start = cpu_to_le16(blocksize);
4579 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
4580 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
4581
4582 goto set_num_buckets;
4583 }
4584
Tao Ma01225592008-08-18 17:38:53 +08004585 /* copy the whole bucket to the new first. */
Joel Beckerba937122008-10-24 19:13:20 -07004586 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004587
4588 /* update the new bucket. */
Joel Beckerba937122008-10-24 19:13:20 -07004589 xh = bucket_xh(t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004590
4591 /*
4592 * Calculate the total name/value len and xh_free_start for
4593 * the old bucket first.
4594 */
4595 name_offset = OCFS2_XATTR_BUCKET_SIZE;
4596 name_value_len = 0;
4597 for (i = 0; i < start; i++) {
4598 xe = &xh->xh_entries[i];
Joel Becker199799a2009-08-14 19:04:15 -07004599 name_value_len += namevalue_size_xe(xe);
Tao Ma01225592008-08-18 17:38:53 +08004600 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
4601 name_offset = le16_to_cpu(xe->xe_name_offset);
4602 }
4603
4604 /*
4605 * Now begin the modification to the new bucket.
4606 *
4607 * In the new bucket, We just move the xattr entry to the beginning
4608 * and don't touch the name/value. So there will be some holes in the
4609 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
4610 * called.
4611 */
4612 xe = &xh->xh_entries[start];
4613 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
4614 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
Mark Fashehff1ec202008-08-19 10:54:29 -07004615 (int)((char *)xe - (char *)xh),
4616 (int)((char *)xh->xh_entries - (char *)xh));
Tao Ma01225592008-08-18 17:38:53 +08004617 memmove((char *)xh->xh_entries, (char *)xe, len);
4618 xe = &xh->xh_entries[count - start];
4619 len = sizeof(struct ocfs2_xattr_entry) * start;
4620 memset((char *)xe, 0, len);
4621
4622 le16_add_cpu(&xh->xh_count, -start);
4623 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
4624
4625 /* Calculate xh_free_start for the new bucket. */
4626 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4627 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4628 xe = &xh->xh_entries[i];
Tao Ma01225592008-08-18 17:38:53 +08004629 if (le16_to_cpu(xe->xe_name_offset) <
4630 le16_to_cpu(xh->xh_free_start))
4631 xh->xh_free_start = xe->xe_name_offset;
4632 }
4633
Tao Ma80bcaf32008-10-27 06:06:24 +08004634set_num_buckets:
Tao Ma01225592008-08-18 17:38:53 +08004635 /* set xh->xh_num_buckets for the new xh. */
4636 if (new_bucket_head)
4637 xh->xh_num_buckets = cpu_to_le16(1);
4638 else
4639 xh->xh_num_buckets = 0;
4640
Joel Beckerba937122008-10-24 19:13:20 -07004641 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004642
4643 /* store the first_hash of the new bucket. */
4644 if (first_hash)
4645 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
4646
4647 /*
Tao Ma80bcaf32008-10-27 06:06:24 +08004648 * Now only update the 1st block of the old bucket. If we
4649 * just added a new empty bucket, there is no need to modify
4650 * it.
Tao Ma01225592008-08-18 17:38:53 +08004651 */
Tao Ma80bcaf32008-10-27 06:06:24 +08004652 if (start == count)
4653 goto out;
4654
Joel Beckerba937122008-10-24 19:13:20 -07004655 xh = bucket_xh(s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004656 memset(&xh->xh_entries[start], 0,
4657 sizeof(struct ocfs2_xattr_entry) * (count - start));
4658 xh->xh_count = cpu_to_le16(start);
4659 xh->xh_free_start = cpu_to_le16(name_offset);
4660 xh->xh_name_value_len = cpu_to_le16(name_value_len);
4661
Joel Beckerba937122008-10-24 19:13:20 -07004662 ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004663
4664out:
Joel Beckerba937122008-10-24 19:13:20 -07004665 ocfs2_xattr_bucket_free(s_bucket);
4666 ocfs2_xattr_bucket_free(t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004667
4668 return ret;
4669}
4670
4671/*
4672 * Copy xattr from one bucket to another bucket.
4673 *
4674 * The caller must make sure that the journal transaction
4675 * has enough space for journaling.
4676 */
4677static int ocfs2_cp_xattr_bucket(struct inode *inode,
4678 handle_t *handle,
4679 u64 s_blkno,
4680 u64 t_blkno,
4681 int t_is_new)
4682{
Joel Becker4980c6d2008-10-24 18:54:43 -07004683 int ret;
Joel Beckerba937122008-10-24 19:13:20 -07004684 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
Tao Ma01225592008-08-18 17:38:53 +08004685
4686 BUG_ON(s_blkno == t_blkno);
4687
4688 mlog(0, "cp bucket %llu to %llu, target is %d\n",
Mark Fashehde29c082008-10-29 14:45:30 -07004689 (unsigned long long)s_blkno, (unsigned long long)t_blkno,
4690 t_is_new);
Tao Ma01225592008-08-18 17:38:53 +08004691
Joel Beckerba937122008-10-24 19:13:20 -07004692 s_bucket = ocfs2_xattr_bucket_new(inode);
4693 t_bucket = ocfs2_xattr_bucket_new(inode);
4694 if (!s_bucket || !t_bucket) {
4695 ret = -ENOMEM;
4696 mlog_errno(ret);
4697 goto out;
4698 }
Joel Becker92de1092008-11-25 17:06:40 -08004699
Joel Beckerba937122008-10-24 19:13:20 -07004700 ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
Tao Ma01225592008-08-18 17:38:53 +08004701 if (ret)
4702 goto out;
4703
Joel Becker784b8162008-10-24 17:33:40 -07004704 /*
4705 * Even if !t_is_new, we're overwriting t_bucket. Thus,
4706 * there's no need to read it.
4707 */
Joel Beckerba937122008-10-24 19:13:20 -07004708 ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
Tao Ma01225592008-08-18 17:38:53 +08004709 if (ret)
4710 goto out;
4711
Joel Becker2b656c12008-11-25 19:00:15 -08004712 /*
4713 * Hey, if we're overwriting t_bucket, what difference does
4714 * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new
Joel Becker874d65a2008-11-26 13:02:18 -08004715 * cluster to fill, we came here from
4716 * ocfs2_mv_xattr_buckets(), and it is really new -
4717 * ACCESS_CREATE is required. But we also might have moved data
4718 * out of t_bucket before extending back into it.
4719 * ocfs2_add_new_xattr_bucket() can do this - its call to
4720 * ocfs2_add_new_xattr_cluster() may have created a new extent
Joel Becker2b656c12008-11-25 19:00:15 -08004721 * and copied out the end of the old extent. Then it re-extends
4722 * the old extent back to create space for new xattrs. That's
4723 * how we get here, and the bucket isn't really new.
4724 */
Joel Beckerba937122008-10-24 19:13:20 -07004725 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004726 t_is_new ?
4727 OCFS2_JOURNAL_ACCESS_CREATE :
4728 OCFS2_JOURNAL_ACCESS_WRITE);
4729 if (ret)
4730 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004731
Joel Beckerba937122008-10-24 19:13:20 -07004732 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4733 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004734
4735out:
Joel Beckerba937122008-10-24 19:13:20 -07004736 ocfs2_xattr_bucket_free(t_bucket);
4737 ocfs2_xattr_bucket_free(s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004738
4739 return ret;
4740}
4741
4742/*
Joel Becker874d65a2008-11-26 13:02:18 -08004743 * src_blk points to the start of an existing extent. last_blk points to
4744 * last cluster in that extent. to_blk points to a newly allocated
Joel Becker54ecb6b2008-11-26 13:18:31 -08004745 * extent. We copy the buckets from the cluster at last_blk to the new
4746 * extent. If start_bucket is non-zero, we skip that many buckets before
4747 * we start copying. The new extent's xh_num_buckets gets set to the
4748 * number of buckets we copied. The old extent's xh_num_buckets shrinks
4749 * by the same amount.
Tao Ma01225592008-08-18 17:38:53 +08004750 */
Joel Becker54ecb6b2008-11-26 13:18:31 -08004751static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
4752 u64 src_blk, u64 last_blk, u64 to_blk,
4753 unsigned int start_bucket,
4754 u32 *first_hash)
Tao Ma01225592008-08-18 17:38:53 +08004755{
4756 int i, ret, credits;
4757 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Joel Becker15d60922008-11-25 18:36:42 -08004758 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08004759 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
Joel Becker15d60922008-11-25 18:36:42 -08004760 struct ocfs2_xattr_bucket *old_first, *new_first;
Tao Ma01225592008-08-18 17:38:53 +08004761
Joel Becker874d65a2008-11-26 13:02:18 -08004762 mlog(0, "mv xattrs from cluster %llu to %llu\n",
4763 (unsigned long long)last_blk, (unsigned long long)to_blk);
Tao Ma01225592008-08-18 17:38:53 +08004764
Joel Becker54ecb6b2008-11-26 13:18:31 -08004765 BUG_ON(start_bucket >= num_buckets);
4766 if (start_bucket) {
4767 num_buckets -= start_bucket;
4768 last_blk += (start_bucket * blks_per_bucket);
4769 }
4770
Joel Becker15d60922008-11-25 18:36:42 -08004771 /* The first bucket of the original extent */
4772 old_first = ocfs2_xattr_bucket_new(inode);
4773 /* The first bucket of the new extent */
4774 new_first = ocfs2_xattr_bucket_new(inode);
4775 if (!old_first || !new_first) {
4776 ret = -ENOMEM;
4777 mlog_errno(ret);
4778 goto out;
4779 }
4780
Joel Becker874d65a2008-11-26 13:02:18 -08004781 ret = ocfs2_read_xattr_bucket(old_first, src_blk);
Joel Becker15d60922008-11-25 18:36:42 -08004782 if (ret) {
4783 mlog_errno(ret);
4784 goto out;
4785 }
4786
Tao Ma01225592008-08-18 17:38:53 +08004787 /*
Joel Becker54ecb6b2008-11-26 13:18:31 -08004788 * We need to update the first bucket of the old extent and all
4789 * the buckets going to the new extent.
Tao Ma01225592008-08-18 17:38:53 +08004790 */
Joel Becker54ecb6b2008-11-26 13:18:31 -08004791 credits = ((num_buckets + 1) * blks_per_bucket) +
4792 handle->h_buffer_credits;
Tao Ma01225592008-08-18 17:38:53 +08004793 ret = ocfs2_extend_trans(handle, credits);
4794 if (ret) {
4795 mlog_errno(ret);
4796 goto out;
4797 }
4798
Joel Becker15d60922008-11-25 18:36:42 -08004799 ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
4800 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004801 if (ret) {
4802 mlog_errno(ret);
4803 goto out;
4804 }
4805
4806 for (i = 0; i < num_buckets; i++) {
4807 ret = ocfs2_cp_xattr_bucket(inode, handle,
Joel Becker874d65a2008-11-26 13:02:18 -08004808 last_blk + (i * blks_per_bucket),
Joel Becker15d60922008-11-25 18:36:42 -08004809 to_blk + (i * blks_per_bucket),
4810 1);
Tao Ma01225592008-08-18 17:38:53 +08004811 if (ret) {
4812 mlog_errno(ret);
4813 goto out;
4814 }
Tao Ma01225592008-08-18 17:38:53 +08004815 }
4816
Joel Becker15d60922008-11-25 18:36:42 -08004817 /*
4818 * Get the new bucket ready before we dirty anything
4819 * (This actually shouldn't fail, because we already dirtied
4820 * it once in ocfs2_cp_xattr_bucket()).
4821 */
4822 ret = ocfs2_read_xattr_bucket(new_first, to_blk);
4823 if (ret) {
Tao Ma01225592008-08-18 17:38:53 +08004824 mlog_errno(ret);
4825 goto out;
4826 }
Joel Becker15d60922008-11-25 18:36:42 -08004827 ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
4828 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004829 if (ret) {
4830 mlog_errno(ret);
4831 goto out;
4832 }
4833
Joel Becker15d60922008-11-25 18:36:42 -08004834 /* Now update the headers */
4835 le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets);
4836 ocfs2_xattr_bucket_journal_dirty(handle, old_first);
Tao Ma01225592008-08-18 17:38:53 +08004837
Joel Becker15d60922008-11-25 18:36:42 -08004838 bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets);
4839 ocfs2_xattr_bucket_journal_dirty(handle, new_first);
Tao Ma01225592008-08-18 17:38:53 +08004840
4841 if (first_hash)
Joel Becker15d60922008-11-25 18:36:42 -08004842 *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash);
4843
Tao Ma01225592008-08-18 17:38:53 +08004844out:
Joel Becker15d60922008-11-25 18:36:42 -08004845 ocfs2_xattr_bucket_free(new_first);
4846 ocfs2_xattr_bucket_free(old_first);
Tao Ma01225592008-08-18 17:38:53 +08004847 return ret;
4848}
4849
4850/*
Tao Ma80bcaf32008-10-27 06:06:24 +08004851 * Move some xattrs in this cluster to the new cluster.
Tao Ma01225592008-08-18 17:38:53 +08004852 * This function should only be called when bucket size == cluster size.
4853 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
4854 */
Tao Ma80bcaf32008-10-27 06:06:24 +08004855static int ocfs2_divide_xattr_cluster(struct inode *inode,
4856 handle_t *handle,
4857 u64 prev_blk,
4858 u64 new_blk,
4859 u32 *first_hash)
Tao Ma01225592008-08-18 17:38:53 +08004860{
4861 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08004862 int ret, credits = 2 * blk_per_bucket + handle->h_buffer_credits;
Tao Ma01225592008-08-18 17:38:53 +08004863
4864 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
4865
4866 ret = ocfs2_extend_trans(handle, credits);
4867 if (ret) {
4868 mlog_errno(ret);
4869 return ret;
4870 }
4871
4872 /* Move half of the xattr in start_blk to the next bucket. */
Tao Ma80bcaf32008-10-27 06:06:24 +08004873 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
4874 new_blk, first_hash, 1);
Tao Ma01225592008-08-18 17:38:53 +08004875}
4876
4877/*
4878 * Move some xattrs from the old cluster to the new one since they are not
4879 * contiguous in ocfs2 xattr tree.
4880 *
4881 * new_blk starts a new separate cluster, and we will move some xattrs from
4882 * prev_blk to it. v_start will be set as the first name hash value in this
4883 * new cluster so that it can be used as e_cpos during tree insertion and
4884 * don't collide with our original b-tree operations. first_bh and header_bh
4885 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
4886 * to extend the insert bucket.
4887 *
4888 * The problem is how much xattr should we move to the new one and when should
4889 * we update first_bh and header_bh?
4890 * 1. If cluster size > bucket size, that means the previous cluster has more
4891 * than 1 bucket, so just move half nums of bucket into the new cluster and
4892 * update the first_bh and header_bh if the insert bucket has been moved
4893 * to the new cluster.
4894 * 2. If cluster_size == bucket_size:
4895 * a) If the previous extent rec has more than one cluster and the insert
4896 * place isn't in the last cluster, copy the entire last cluster to the
4897 * new one. This time, we don't need to upate the first_bh and header_bh
4898 * since they will not be moved into the new cluster.
4899 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
4900 * the new one. And we set the extend flag to zero if the insert place is
4901 * moved into the new allocated cluster since no extend is needed.
4902 */
4903static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
4904 handle_t *handle,
Joel Becker012ee912008-11-26 14:43:31 -08004905 struct ocfs2_xattr_bucket *first,
4906 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08004907 u64 new_blk,
Tao Ma01225592008-08-18 17:38:53 +08004908 u32 prev_clusters,
4909 u32 *v_start,
4910 int *extend)
4911{
Joel Becker92cf3ad2008-11-26 14:12:09 -08004912 int ret;
Tao Ma01225592008-08-18 17:38:53 +08004913
4914 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
Joel Becker012ee912008-11-26 14:43:31 -08004915 (unsigned long long)bucket_blkno(first), prev_clusters,
Mark Fashehde29c082008-10-29 14:45:30 -07004916 (unsigned long long)new_blk);
Tao Ma01225592008-08-18 17:38:53 +08004917
Joel Becker41cb8142008-11-26 14:25:21 -08004918 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) {
Tao Ma01225592008-08-18 17:38:53 +08004919 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
4920 handle,
Joel Becker41cb8142008-11-26 14:25:21 -08004921 first, target,
Tao Ma01225592008-08-18 17:38:53 +08004922 new_blk,
Tao Ma01225592008-08-18 17:38:53 +08004923 prev_clusters,
4924 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08004925 if (ret)
Joel Becker41cb8142008-11-26 14:25:21 -08004926 mlog_errno(ret);
Joel Becker41cb8142008-11-26 14:25:21 -08004927 } else {
Joel Becker92cf3ad2008-11-26 14:12:09 -08004928 /* The start of the last cluster in the first extent */
4929 u64 last_blk = bucket_blkno(first) +
4930 ((prev_clusters - 1) *
4931 ocfs2_clusters_to_blocks(inode->i_sb, 1));
Tao Ma01225592008-08-18 17:38:53 +08004932
Joel Becker012ee912008-11-26 14:43:31 -08004933 if (prev_clusters > 1 && bucket_blkno(target) != last_blk) {
Joel Becker874d65a2008-11-26 13:02:18 -08004934 ret = ocfs2_mv_xattr_buckets(inode, handle,
Joel Becker92cf3ad2008-11-26 14:12:09 -08004935 bucket_blkno(first),
Joel Becker54ecb6b2008-11-26 13:18:31 -08004936 last_blk, new_blk, 0,
Tao Ma01225592008-08-18 17:38:53 +08004937 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08004938 if (ret)
4939 mlog_errno(ret);
4940 } else {
Tao Ma80bcaf32008-10-27 06:06:24 +08004941 ret = ocfs2_divide_xattr_cluster(inode, handle,
4942 last_blk, new_blk,
4943 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08004944 if (ret)
4945 mlog_errno(ret);
Tao Ma01225592008-08-18 17:38:53 +08004946
Joel Becker92cf3ad2008-11-26 14:12:09 -08004947 if ((bucket_blkno(target) == last_blk) && extend)
Tao Ma01225592008-08-18 17:38:53 +08004948 *extend = 0;
4949 }
4950 }
4951
4952 return ret;
4953}
4954
4955/*
4956 * Add a new cluster for xattr storage.
4957 *
4958 * If the new cluster is contiguous with the previous one, it will be
4959 * appended to the same extent record, and num_clusters will be updated.
4960 * If not, we will insert a new extent for it and move some xattrs in
4961 * the last cluster into the new allocated one.
4962 * We also need to limit the maximum size of a btree leaf, otherwise we'll
4963 * lose the benefits of hashing because we'll have to search large leaves.
4964 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
4965 * if it's bigger).
4966 *
4967 * first_bh is the first block of the previous extent rec and header_bh
4968 * indicates the bucket we will insert the new xattrs. They will be updated
4969 * when the header_bh is moved into the new cluster.
4970 */
4971static int ocfs2_add_new_xattr_cluster(struct inode *inode,
4972 struct buffer_head *root_bh,
Joel Beckered29c0c2008-11-26 15:08:44 -08004973 struct ocfs2_xattr_bucket *first,
4974 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08004975 u32 *num_clusters,
4976 u32 prev_cpos,
Tao Ma78f30c32008-11-12 08:27:00 +08004977 int *extend,
4978 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004979{
Tao Ma85db90e2008-11-12 08:27:01 +08004980 int ret;
Tao Ma01225592008-08-18 17:38:53 +08004981 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
4982 u32 prev_clusters = *num_clusters;
4983 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
4984 u64 block;
Tao Ma85db90e2008-11-12 08:27:01 +08004985 handle_t *handle = ctxt->handle;
Tao Ma01225592008-08-18 17:38:53 +08004986 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004987 struct ocfs2_extent_tree et;
Tao Ma01225592008-08-18 17:38:53 +08004988
4989 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
4990 "previous xattr blkno = %llu\n",
4991 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Joel Beckered29c0c2008-11-26 15:08:44 -08004992 prev_cpos, (unsigned long long)bucket_blkno(first));
Tao Ma01225592008-08-18 17:38:53 +08004993
Joel Becker5e404e92009-02-13 03:54:22 -08004994 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004995
Joel Becker0cf2f762009-02-12 16:41:25 -08004996 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
Joel Becker84008972008-12-09 16:11:49 -08004997 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004998 if (ret < 0) {
4999 mlog_errno(ret);
5000 goto leave;
5001 }
5002
Tao Ma78f30c32008-11-12 08:27:00 +08005003 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, 1,
Tao Ma01225592008-08-18 17:38:53 +08005004 clusters_to_add, &bit_off, &num_bits);
5005 if (ret < 0) {
5006 if (ret != -ENOSPC)
5007 mlog_errno(ret);
5008 goto leave;
5009 }
5010
5011 BUG_ON(num_bits > clusters_to_add);
5012
5013 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
5014 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
5015 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
5016
Joel Beckered29c0c2008-11-26 15:08:44 -08005017 if (bucket_blkno(first) + (prev_clusters * bpc) == block &&
Tao Ma01225592008-08-18 17:38:53 +08005018 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
5019 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
5020 /*
5021 * If this cluster is contiguous with the old one and
5022 * adding this new cluster, we don't surpass the limit of
5023 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
5024 * initialized and used like other buckets in the previous
5025 * cluster.
5026 * So add it as a contiguous one. The caller will handle
5027 * its init process.
5028 */
5029 v_start = prev_cpos + prev_clusters;
5030 *num_clusters = prev_clusters + num_bits;
5031 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
5032 num_bits);
5033 } else {
5034 ret = ocfs2_adjust_xattr_cross_cluster(inode,
5035 handle,
Joel Becker012ee912008-11-26 14:43:31 -08005036 first,
5037 target,
Tao Ma01225592008-08-18 17:38:53 +08005038 block,
Tao Ma01225592008-08-18 17:38:53 +08005039 prev_clusters,
5040 &v_start,
5041 extend);
5042 if (ret) {
5043 mlog_errno(ret);
5044 goto leave;
5045 }
5046 }
5047
5048 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
Mark Fashehde29c082008-10-29 14:45:30 -07005049 num_bits, (unsigned long long)block, v_start);
Joel Beckercc79d8c2009-02-13 03:24:43 -08005050 ret = ocfs2_insert_extent(handle, &et, v_start, block,
Tao Ma78f30c32008-11-12 08:27:00 +08005051 num_bits, 0, ctxt->meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08005052 if (ret < 0) {
5053 mlog_errno(ret);
5054 goto leave;
5055 }
5056
5057 ret = ocfs2_journal_dirty(handle, root_bh);
Tao Ma85db90e2008-11-12 08:27:01 +08005058 if (ret < 0)
Tao Ma01225592008-08-18 17:38:53 +08005059 mlog_errno(ret);
Tao Ma01225592008-08-18 17:38:53 +08005060
5061leave:
Tao Ma01225592008-08-18 17:38:53 +08005062 return ret;
5063}
5064
5065/*
Joel Becker92de1092008-11-25 17:06:40 -08005066 * We are given an extent. 'first' is the bucket at the very front of
5067 * the extent. The extent has space for an additional bucket past
5068 * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
5069 * of the target bucket. We wish to shift every bucket past the target
5070 * down one, filling in that additional space. When we get back to the
5071 * target, we split the target between itself and the now-empty bucket
5072 * at target+1 (aka, target_blkno + blks_per_bucket).
Tao Ma01225592008-08-18 17:38:53 +08005073 */
5074static int ocfs2_extend_xattr_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08005075 handle_t *handle,
Joel Becker92de1092008-11-25 17:06:40 -08005076 struct ocfs2_xattr_bucket *first,
5077 u64 target_blk,
Tao Ma01225592008-08-18 17:38:53 +08005078 u32 num_clusters)
5079{
5080 int ret, credits;
5081 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5082 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Joel Becker92de1092008-11-25 17:06:40 -08005083 u64 end_blk;
5084 u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets);
Tao Ma01225592008-08-18 17:38:53 +08005085
5086 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
Joel Becker92de1092008-11-25 17:06:40 -08005087 "from %llu, len = %u\n", (unsigned long long)target_blk,
5088 (unsigned long long)bucket_blkno(first), num_clusters);
Tao Ma01225592008-08-18 17:38:53 +08005089
Joel Becker92de1092008-11-25 17:06:40 -08005090 /* The extent must have room for an additional bucket */
5091 BUG_ON(new_bucket >=
5092 (num_clusters * ocfs2_xattr_buckets_per_cluster(osb)));
Tao Ma01225592008-08-18 17:38:53 +08005093
Joel Becker92de1092008-11-25 17:06:40 -08005094 /* end_blk points to the last existing bucket */
5095 end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket);
Tao Ma01225592008-08-18 17:38:53 +08005096
5097 /*
Joel Becker92de1092008-11-25 17:06:40 -08005098 * end_blk is the start of the last existing bucket.
5099 * Thus, (end_blk - target_blk) covers the target bucket and
5100 * every bucket after it up to, but not including, the last
5101 * existing bucket. Then we add the last existing bucket, the
5102 * new bucket, and the first bucket (3 * blk_per_bucket).
Tao Ma01225592008-08-18 17:38:53 +08005103 */
Joel Becker92de1092008-11-25 17:06:40 -08005104 credits = (end_blk - target_blk) + (3 * blk_per_bucket) +
Tao Ma85db90e2008-11-12 08:27:01 +08005105 handle->h_buffer_credits;
5106 ret = ocfs2_extend_trans(handle, credits);
5107 if (ret) {
Tao Ma01225592008-08-18 17:38:53 +08005108 mlog_errno(ret);
5109 goto out;
5110 }
5111
Joel Becker92de1092008-11-25 17:06:40 -08005112 ret = ocfs2_xattr_bucket_journal_access(handle, first,
5113 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08005114 if (ret) {
5115 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08005116 goto out;
Tao Ma01225592008-08-18 17:38:53 +08005117 }
5118
Joel Becker92de1092008-11-25 17:06:40 -08005119 while (end_blk != target_blk) {
Tao Ma01225592008-08-18 17:38:53 +08005120 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
5121 end_blk + blk_per_bucket, 0);
5122 if (ret)
Tao Ma85db90e2008-11-12 08:27:01 +08005123 goto out;
Tao Ma01225592008-08-18 17:38:53 +08005124 end_blk -= blk_per_bucket;
5125 }
5126
Joel Becker92de1092008-11-25 17:06:40 -08005127 /* Move half of the xattr in target_blkno to the next bucket. */
5128 ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk,
5129 target_blk + blk_per_bucket, NULL, 0);
Tao Ma01225592008-08-18 17:38:53 +08005130
Joel Becker92de1092008-11-25 17:06:40 -08005131 le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1);
5132 ocfs2_xattr_bucket_journal_dirty(handle, first);
Tao Ma01225592008-08-18 17:38:53 +08005133
Tao Ma01225592008-08-18 17:38:53 +08005134out:
5135 return ret;
5136}
5137
5138/*
Joel Becker91f20332008-11-26 15:25:41 -08005139 * Add new xattr bucket in an extent record and adjust the buckets
5140 * accordingly. xb_bh is the ocfs2_xattr_block, and target is the
5141 * bucket we want to insert into.
Tao Ma01225592008-08-18 17:38:53 +08005142 *
Joel Becker91f20332008-11-26 15:25:41 -08005143 * In the easy case, we will move all the buckets after target down by
5144 * one. Half of target's xattrs will be moved to the next bucket.
5145 *
5146 * If current cluster is full, we'll allocate a new one. This may not
5147 * be contiguous. The underlying calls will make sure that there is
5148 * space for the insert, shifting buckets around if necessary.
5149 * 'target' may be moved by those calls.
Tao Ma01225592008-08-18 17:38:53 +08005150 */
5151static int ocfs2_add_new_xattr_bucket(struct inode *inode,
5152 struct buffer_head *xb_bh,
Joel Becker91f20332008-11-26 15:25:41 -08005153 struct ocfs2_xattr_bucket *target,
Tao Ma78f30c32008-11-12 08:27:00 +08005154 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005155{
Tao Ma01225592008-08-18 17:38:53 +08005156 struct ocfs2_xattr_block *xb =
5157 (struct ocfs2_xattr_block *)xb_bh->b_data;
5158 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
5159 struct ocfs2_extent_list *el = &xb_root->xt_list;
Joel Becker91f20332008-11-26 15:25:41 -08005160 u32 name_hash =
5161 le32_to_cpu(bucket_xh(target)->xh_entries[0].xe_name_hash);
Joel Beckered29c0c2008-11-26 15:08:44 -08005162 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08005163 int ret, num_buckets, extend = 1;
5164 u64 p_blkno;
5165 u32 e_cpos, num_clusters;
Joel Becker92de1092008-11-25 17:06:40 -08005166 /* The bucket at the front of the extent */
Joel Becker91f20332008-11-26 15:25:41 -08005167 struct ocfs2_xattr_bucket *first;
Tao Ma01225592008-08-18 17:38:53 +08005168
Joel Becker91f20332008-11-26 15:25:41 -08005169 mlog(0, "Add new xattr bucket starting from %llu\n",
5170 (unsigned long long)bucket_blkno(target));
Tao Ma01225592008-08-18 17:38:53 +08005171
Joel Beckered29c0c2008-11-26 15:08:44 -08005172 /* The first bucket of the original extent */
Joel Becker92de1092008-11-25 17:06:40 -08005173 first = ocfs2_xattr_bucket_new(inode);
Joel Becker91f20332008-11-26 15:25:41 -08005174 if (!first) {
Joel Becker92de1092008-11-25 17:06:40 -08005175 ret = -ENOMEM;
5176 mlog_errno(ret);
5177 goto out;
5178 }
5179
Tao Ma01225592008-08-18 17:38:53 +08005180 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
5181 &num_clusters, el);
5182 if (ret) {
5183 mlog_errno(ret);
5184 goto out;
5185 }
5186
Joel Beckered29c0c2008-11-26 15:08:44 -08005187 ret = ocfs2_read_xattr_bucket(first, p_blkno);
5188 if (ret) {
5189 mlog_errno(ret);
5190 goto out;
5191 }
5192
Tao Ma01225592008-08-18 17:38:53 +08005193 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
Joel Beckered29c0c2008-11-26 15:08:44 -08005194 if (num_buckets == le16_to_cpu(bucket_xh(first)->xh_num_buckets)) {
5195 /*
5196 * This can move first+target if the target bucket moves
5197 * to the new extent.
5198 */
Tao Ma01225592008-08-18 17:38:53 +08005199 ret = ocfs2_add_new_xattr_cluster(inode,
5200 xb_bh,
Joel Beckered29c0c2008-11-26 15:08:44 -08005201 first,
5202 target,
Tao Ma01225592008-08-18 17:38:53 +08005203 &num_clusters,
5204 e_cpos,
Tao Ma78f30c32008-11-12 08:27:00 +08005205 &extend,
5206 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005207 if (ret) {
5208 mlog_errno(ret);
5209 goto out;
5210 }
5211 }
5212
Joel Becker92de1092008-11-25 17:06:40 -08005213 if (extend) {
Tao Ma01225592008-08-18 17:38:53 +08005214 ret = ocfs2_extend_xattr_bucket(inode,
Tao Ma85db90e2008-11-12 08:27:01 +08005215 ctxt->handle,
Joel Beckered29c0c2008-11-26 15:08:44 -08005216 first,
5217 bucket_blkno(target),
Tao Ma01225592008-08-18 17:38:53 +08005218 num_clusters);
Joel Becker92de1092008-11-25 17:06:40 -08005219 if (ret)
5220 mlog_errno(ret);
5221 }
5222
Tao Ma01225592008-08-18 17:38:53 +08005223out:
Joel Becker92de1092008-11-25 17:06:40 -08005224 ocfs2_xattr_bucket_free(first);
Joel Beckered29c0c2008-11-26 15:08:44 -08005225
Tao Ma01225592008-08-18 17:38:53 +08005226 return ret;
5227}
5228
5229static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
5230 struct ocfs2_xattr_bucket *bucket,
5231 int offs)
5232{
5233 int block_off = offs >> inode->i_sb->s_blocksize_bits;
5234
5235 offs = offs % inode->i_sb->s_blocksize;
Joel Becker51def392008-10-24 16:57:21 -07005236 return bucket_block(bucket, block_off) + offs;
Tao Ma01225592008-08-18 17:38:53 +08005237}
5238
5239/*
Tao Ma01225592008-08-18 17:38:53 +08005240 * Truncate the specified xe_off entry in xattr bucket.
5241 * bucket is indicated by header_bh and len is the new length.
5242 * Both the ocfs2_xattr_value_root and the entry will be updated here.
5243 *
5244 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
5245 */
5246static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
Joel Becker548b0f22008-11-24 19:32:13 -08005247 struct ocfs2_xattr_bucket *bucket,
Tao Ma01225592008-08-18 17:38:53 +08005248 int xe_off,
Tao Ma78f30c32008-11-12 08:27:00 +08005249 int len,
5250 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005251{
5252 int ret, offset;
5253 u64 value_blk;
Tao Ma01225592008-08-18 17:38:53 +08005254 struct ocfs2_xattr_entry *xe;
Joel Becker548b0f22008-11-24 19:32:13 -08005255 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma01225592008-08-18 17:38:53 +08005256 size_t blocksize = inode->i_sb->s_blocksize;
Joel Beckerb3e5d372008-12-09 15:01:04 -08005257 struct ocfs2_xattr_value_buf vb = {
5258 .vb_access = ocfs2_journal_access,
5259 };
Tao Ma01225592008-08-18 17:38:53 +08005260
5261 xe = &xh->xh_entries[xe_off];
5262
5263 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
5264
5265 offset = le16_to_cpu(xe->xe_name_offset) +
5266 OCFS2_XATTR_SIZE(xe->xe_name_len);
5267
5268 value_blk = offset / blocksize;
5269
5270 /* We don't allow ocfs2_xattr_value to be stored in different block. */
5271 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
Tao Ma01225592008-08-18 17:38:53 +08005272
Joel Beckerb3e5d372008-12-09 15:01:04 -08005273 vb.vb_bh = bucket->bu_bhs[value_blk];
5274 BUG_ON(!vb.vb_bh);
Tao Ma01225592008-08-18 17:38:53 +08005275
Joel Beckerb3e5d372008-12-09 15:01:04 -08005276 vb.vb_xv = (struct ocfs2_xattr_value_root *)
5277 (vb.vb_bh->b_data + offset % blocksize);
Tao Ma01225592008-08-18 17:38:53 +08005278
Joel Becker548b0f22008-11-24 19:32:13 -08005279 /*
5280 * From here on out we have to dirty the bucket. The generic
5281 * value calls only modify one of the bucket's bhs, but we need
5282 * to send the bucket at once. So if they error, they *could* have
5283 * modified something. We have to assume they did, and dirty
5284 * the whole bucket. This leaves us in a consistent state.
5285 */
Tao Ma01225592008-08-18 17:38:53 +08005286 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
Joel Becker548b0f22008-11-24 19:32:13 -08005287 xe_off, (unsigned long long)bucket_blkno(bucket), len);
Joel Beckerb3e5d372008-12-09 15:01:04 -08005288 ret = ocfs2_xattr_value_truncate(inode, &vb, len, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005289 if (ret) {
5290 mlog_errno(ret);
Tao Ma554e7f92009-01-08 08:21:43 +08005291 goto out;
5292 }
5293
5294 ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
5295 OCFS2_JOURNAL_ACCESS_WRITE);
5296 if (ret) {
5297 mlog_errno(ret);
5298 goto out;
Tao Ma01225592008-08-18 17:38:53 +08005299 }
5300
Joel Becker548b0f22008-11-24 19:32:13 -08005301 xe->xe_value_size = cpu_to_le64(len);
5302
Joel Becker548b0f22008-11-24 19:32:13 -08005303 ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket);
Tao Ma01225592008-08-18 17:38:53 +08005304
5305out:
Tao Ma01225592008-08-18 17:38:53 +08005306 return ret;
5307}
5308
Tao Ma01225592008-08-18 17:38:53 +08005309static int ocfs2_rm_xattr_cluster(struct inode *inode,
5310 struct buffer_head *root_bh,
5311 u64 blkno,
5312 u32 cpos,
Tao Ma47bca492009-08-18 11:43:42 +08005313 u32 len,
5314 void *para)
Tao Ma01225592008-08-18 17:38:53 +08005315{
5316 int ret;
5317 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5318 struct inode *tl_inode = osb->osb_tl_inode;
5319 handle_t *handle;
5320 struct ocfs2_xattr_block *xb =
5321 (struct ocfs2_xattr_block *)root_bh->b_data;
Tao Ma01225592008-08-18 17:38:53 +08005322 struct ocfs2_alloc_context *meta_ac = NULL;
5323 struct ocfs2_cached_dealloc_ctxt dealloc;
Joel Beckerf99b9b72008-08-20 19:36:33 -07005324 struct ocfs2_extent_tree et;
5325
Tao Ma47bca492009-08-18 11:43:42 +08005326 ret = ocfs2_iterate_xattr_buckets(inode, blkno, len,
Tao Mace9c5a52009-08-18 11:43:59 +08005327 ocfs2_delete_xattr_in_bucket, para);
Tao Ma47bca492009-08-18 11:43:42 +08005328 if (ret) {
5329 mlog_errno(ret);
5330 return ret;
5331 }
5332
Joel Becker5e404e92009-02-13 03:54:22 -08005333 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
Tao Ma01225592008-08-18 17:38:53 +08005334
5335 ocfs2_init_dealloc_ctxt(&dealloc);
5336
5337 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
5338 cpos, len, (unsigned long long)blkno);
5339
Joel Becker8cb471e2009-02-10 20:00:41 -08005340 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode), blkno,
5341 len);
Tao Ma01225592008-08-18 17:38:53 +08005342
Joel Beckerf99b9b72008-08-20 19:36:33 -07005343 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08005344 if (ret) {
5345 mlog_errno(ret);
5346 return ret;
5347 }
5348
5349 mutex_lock(&tl_inode->i_mutex);
5350
5351 if (ocfs2_truncate_log_needs_flush(osb)) {
5352 ret = __ocfs2_flush_truncate_log(osb);
5353 if (ret < 0) {
5354 mlog_errno(ret);
5355 goto out;
5356 }
5357 }
5358
Jan Karaa90714c2008-10-09 19:38:40 +02005359 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
Tao Mad3264792008-10-24 07:57:28 +08005360 if (IS_ERR(handle)) {
Tao Ma01225592008-08-18 17:38:53 +08005361 ret = -ENOMEM;
5362 mlog_errno(ret);
5363 goto out;
5364 }
5365
Joel Becker0cf2f762009-02-12 16:41:25 -08005366 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
Joel Becker84008972008-12-09 16:11:49 -08005367 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08005368 if (ret) {
5369 mlog_errno(ret);
5370 goto out_commit;
5371 }
5372
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005373 ret = ocfs2_remove_extent(handle, &et, cpos, len, meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005374 &dealloc);
Tao Ma01225592008-08-18 17:38:53 +08005375 if (ret) {
5376 mlog_errno(ret);
5377 goto out_commit;
5378 }
5379
5380 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
5381
5382 ret = ocfs2_journal_dirty(handle, root_bh);
5383 if (ret) {
5384 mlog_errno(ret);
5385 goto out_commit;
5386 }
5387
5388 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
5389 if (ret)
5390 mlog_errno(ret);
5391
5392out_commit:
5393 ocfs2_commit_trans(osb, handle);
5394out:
5395 ocfs2_schedule_truncate_log_flush(osb, 1);
5396
5397 mutex_unlock(&tl_inode->i_mutex);
5398
5399 if (meta_ac)
5400 ocfs2_free_alloc_context(meta_ac);
5401
5402 ocfs2_run_deallocs(osb, &dealloc);
5403
5404 return ret;
5405}
5406
Tao Ma01225592008-08-18 17:38:53 +08005407/*
5408 * Set the xattr name/value in the bucket specified in xs.
Tao Ma01225592008-08-18 17:38:53 +08005409 */
5410static int ocfs2_xattr_set_in_bucket(struct inode *inode,
5411 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08005412 struct ocfs2_xattr_search *xs,
5413 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005414{
Joel Becker9dc47402009-08-17 19:56:01 -07005415 int ret;
Joel Becker73857ee2009-08-18 20:26:41 -07005416 u64 blkno;
5417 struct ocfs2_xa_loc loc;
Joel Becker6b240ff2009-08-14 18:02:52 -07005418 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->xi_name,
Joel Becker18853b92009-08-14 18:17:07 -07005419 xi->xi_name_len);
Tao Ma01225592008-08-18 17:38:53 +08005420
Joel Becker73857ee2009-08-18 20:26:41 -07005421 if (!xs->bucket->bu_bhs[1]) {
5422 blkno = bucket_blkno(xs->bucket);
5423 ocfs2_xattr_bucket_relse(xs->bucket);
5424 ret = ocfs2_read_xattr_bucket(xs->bucket, blkno);
5425 if (ret) {
5426 mlog_errno(ret);
Tao Ma01225592008-08-18 17:38:53 +08005427 goto out;
Tao Ma01225592008-08-18 17:38:53 +08005428 }
Joel Becker73857ee2009-08-18 20:26:41 -07005429 }
5430
5431 ocfs2_init_xattr_bucket_xa_loc(&loc, xs->bucket,
5432 xs->not_found ? NULL : xs->here);
5433 ret = ocfs2_xa_journal_access(ctxt->handle, &loc,
5434 OCFS2_JOURNAL_ACCESS_WRITE);
5435 if (ret < 0) {
5436 mlog_errno(ret);
Tao Ma01225592008-08-18 17:38:53 +08005437 goto out;
5438 }
5439
Joel Becker73857ee2009-08-18 20:26:41 -07005440 ret = ocfs2_xa_prepare_entry(&loc, xi, name_hash, ctxt);
5441 if (ret) {
5442 if (ret != -ENOSPC)
5443 mlog_errno(ret);
5444 goto out;
5445 }
5446 xs->here = loc.xl_entry;
5447
5448 ret = ocfs2_xa_store_value(&loc, xi, ctxt);
5449 if (ret) {
5450 mlog_errno(ret);
5451 goto out;
5452 }
5453
5454 ocfs2_xa_journal_dirty(ctxt->handle, &loc);
5455
Tao Ma01225592008-08-18 17:38:53 +08005456out:
5457 return ret;
5458}
5459
Tao Ma80bcaf32008-10-27 06:06:24 +08005460/*
5461 * check whether the xattr bucket is filled up with the same hash value.
5462 * If we want to insert the xattr with the same hash, return -ENOSPC.
5463 * If we want to insert a xattr with different hash value, go ahead
5464 * and ocfs2_divide_xattr_bucket will handle this.
5465 */
Tao Ma01225592008-08-18 17:38:53 +08005466static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
Tao Ma80bcaf32008-10-27 06:06:24 +08005467 struct ocfs2_xattr_bucket *bucket,
5468 const char *name)
Tao Ma01225592008-08-18 17:38:53 +08005469{
Joel Becker3e632942008-10-24 17:04:49 -07005470 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08005471 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
5472
5473 if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
5474 return 0;
Tao Ma01225592008-08-18 17:38:53 +08005475
5476 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
5477 xh->xh_entries[0].xe_name_hash) {
5478 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
5479 "hash = %u\n",
Joel Becker9c7759a2008-10-24 16:21:03 -07005480 (unsigned long long)bucket_blkno(bucket),
Tao Ma01225592008-08-18 17:38:53 +08005481 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
5482 return -ENOSPC;
5483 }
5484
5485 return 0;
5486}
5487
5488static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
5489 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08005490 struct ocfs2_xattr_search *xs,
5491 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005492{
5493 struct ocfs2_xattr_header *xh;
5494 struct ocfs2_xattr_entry *xe;
5495 u16 count, header_size, xh_free_start;
Joel Becker6dde41d2008-10-24 17:16:48 -07005496 int free, max_free, need, old;
Joel Becker18853b92009-08-14 18:17:07 -07005497 size_t value_size = 0;
Tao Ma01225592008-08-18 17:38:53 +08005498 size_t blocksize = inode->i_sb->s_blocksize;
5499 int ret, allocation = 0;
Tao Ma01225592008-08-18 17:38:53 +08005500
Joel Becker6b240ff2009-08-14 18:02:52 -07005501 mlog_entry("Set xattr %s in xattr index block\n", xi->xi_name);
Tao Ma01225592008-08-18 17:38:53 +08005502
5503try_again:
5504 xh = xs->header;
5505 count = le16_to_cpu(xh->xh_count);
5506 xh_free_start = le16_to_cpu(xh->xh_free_start);
5507 header_size = sizeof(struct ocfs2_xattr_header) +
5508 count * sizeof(struct ocfs2_xattr_entry);
Tiger Yang4442f512009-02-20 11:11:50 +08005509 max_free = OCFS2_XATTR_BUCKET_SIZE - header_size -
5510 le16_to_cpu(xh->xh_name_value_len) - OCFS2_XATTR_HEADER_GAP;
Tao Ma01225592008-08-18 17:38:53 +08005511
5512 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
5513 "of %u which exceed block size\n",
Joel Beckerba937122008-10-24 19:13:20 -07005514 (unsigned long long)bucket_blkno(xs->bucket),
Tao Ma01225592008-08-18 17:38:53 +08005515 header_size);
5516
Joel Becker6b240ff2009-08-14 18:02:52 -07005517 if (xi->xi_value && xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
Tao Ma01225592008-08-18 17:38:53 +08005518 value_size = OCFS2_XATTR_ROOT_SIZE;
Joel Becker6b240ff2009-08-14 18:02:52 -07005519 else if (xi->xi_value)
5520 value_size = OCFS2_XATTR_SIZE(xi->xi_value_len);
Tao Ma01225592008-08-18 17:38:53 +08005521
5522 if (xs->not_found)
5523 need = sizeof(struct ocfs2_xattr_entry) +
Joel Becker18853b92009-08-14 18:17:07 -07005524 OCFS2_XATTR_SIZE(xi->xi_name_len) + value_size;
Tao Ma01225592008-08-18 17:38:53 +08005525 else {
Joel Becker18853b92009-08-14 18:17:07 -07005526 need = value_size + OCFS2_XATTR_SIZE(xi->xi_name_len);
Tao Ma01225592008-08-18 17:38:53 +08005527
5528 /*
5529 * We only replace the old value if the new length is smaller
5530 * than the old one. Otherwise we will allocate new space in the
5531 * bucket to store it.
5532 */
5533 xe = xs->here;
5534 if (ocfs2_xattr_is_local(xe))
5535 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
5536 else
5537 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
5538
5539 if (old >= value_size)
5540 need = 0;
5541 }
5542
Tiger Yang4442f512009-02-20 11:11:50 +08005543 free = xh_free_start - header_size - OCFS2_XATTR_HEADER_GAP;
Tao Ma01225592008-08-18 17:38:53 +08005544 /*
5545 * We need to make sure the new name/value pair
5546 * can exist in the same block.
5547 */
5548 if (xh_free_start % blocksize < need)
5549 free -= xh_free_start % blocksize;
5550
5551 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
5552 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
5553 " %u\n", xs->not_found,
Joel Beckerba937122008-10-24 19:13:20 -07005554 (unsigned long long)bucket_blkno(xs->bucket),
Tao Ma01225592008-08-18 17:38:53 +08005555 free, need, max_free, le16_to_cpu(xh->xh_free_start),
5556 le16_to_cpu(xh->xh_name_value_len));
5557
Tao Ma976331d2008-11-12 08:26:57 +08005558 if (free < need ||
5559 (xs->not_found &&
5560 count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb))) {
Tao Ma01225592008-08-18 17:38:53 +08005561 if (need <= max_free &&
5562 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
5563 /*
5564 * We can create the space by defragment. Since only the
5565 * name/value will be moved, the xe shouldn't be changed
5566 * in xs.
5567 */
Tao Ma85db90e2008-11-12 08:27:01 +08005568 ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
5569 xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08005570 if (ret) {
5571 mlog_errno(ret);
5572 goto out;
5573 }
5574
5575 xh_free_start = le16_to_cpu(xh->xh_free_start);
Tiger Yang4442f512009-02-20 11:11:50 +08005576 free = xh_free_start - header_size
5577 - OCFS2_XATTR_HEADER_GAP;
Tao Ma01225592008-08-18 17:38:53 +08005578 if (xh_free_start % blocksize < need)
5579 free -= xh_free_start % blocksize;
5580
5581 if (free >= need)
5582 goto xattr_set;
5583
5584 mlog(0, "Can't get enough space for xattr insert by "
5585 "defragment. Need %u bytes, but we have %d, so "
5586 "allocate new bucket for it.\n", need, free);
5587 }
5588
5589 /*
5590 * We have to add new buckets or clusters and one
5591 * allocation should leave us enough space for insert.
5592 */
5593 BUG_ON(allocation);
5594
5595 /*
5596 * We do not allow for overlapping ranges between buckets. And
5597 * the maximum number of collisions we will allow for then is
5598 * one bucket's worth, so check it here whether we need to
5599 * add a new bucket for the insert.
5600 */
Tao Ma80bcaf32008-10-27 06:06:24 +08005601 ret = ocfs2_check_xattr_bucket_collision(inode,
Joel Beckerba937122008-10-24 19:13:20 -07005602 xs->bucket,
Joel Becker6b240ff2009-08-14 18:02:52 -07005603 xi->xi_name);
Tao Ma01225592008-08-18 17:38:53 +08005604 if (ret) {
5605 mlog_errno(ret);
5606 goto out;
5607 }
5608
5609 ret = ocfs2_add_new_xattr_bucket(inode,
5610 xs->xattr_bh,
Joel Becker91f20332008-11-26 15:25:41 -08005611 xs->bucket,
Tao Ma78f30c32008-11-12 08:27:00 +08005612 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005613 if (ret) {
5614 mlog_errno(ret);
5615 goto out;
5616 }
5617
Joel Becker91f20332008-11-26 15:25:41 -08005618 /*
5619 * ocfs2_add_new_xattr_bucket() will have updated
5620 * xs->bucket if it moved, but it will not have updated
5621 * any of the other search fields. Thus, we drop it and
5622 * re-search. Everything should be cached, so it'll be
5623 * quick.
5624 */
Joel Beckerba937122008-10-24 19:13:20 -07005625 ocfs2_xattr_bucket_relse(xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08005626 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
Joel Becker6b240ff2009-08-14 18:02:52 -07005627 xi->xi_name_index,
5628 xi->xi_name, xs);
Tao Ma01225592008-08-18 17:38:53 +08005629 if (ret && ret != -ENODATA)
5630 goto out;
5631 xs->not_found = ret;
5632 allocation = 1;
5633 goto try_again;
5634 }
5635
5636xattr_set:
Tao Ma78f30c32008-11-12 08:27:00 +08005637 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005638out:
5639 mlog_exit(ret);
5640 return ret;
5641}
Tao Maa3944252008-08-18 17:38:54 +08005642
5643static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
5644 struct ocfs2_xattr_bucket *bucket,
5645 void *para)
5646{
Tao Mace9c5a52009-08-18 11:43:59 +08005647 int ret = 0, ref_credits;
Joel Becker3e632942008-10-24 17:04:49 -07005648 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Maa3944252008-08-18 17:38:54 +08005649 u16 i;
5650 struct ocfs2_xattr_entry *xe;
Tao Ma78f30c32008-11-12 08:27:00 +08005651 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5652 struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
Joel Becker548b0f22008-11-24 19:32:13 -08005653 int credits = ocfs2_remove_extent_credits(osb->sb) +
5654 ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Mace9c5a52009-08-18 11:43:59 +08005655 struct ocfs2_xattr_value_root *xv;
5656 struct ocfs2_rm_xattr_bucket_para *args =
5657 (struct ocfs2_rm_xattr_bucket_para *)para;
Tao Ma78f30c32008-11-12 08:27:00 +08005658
5659 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
Tao Maa3944252008-08-18 17:38:54 +08005660
5661 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
5662 xe = &xh->xh_entries[i];
5663 if (ocfs2_xattr_is_local(xe))
5664 continue;
5665
Tao Mace9c5a52009-08-18 11:43:59 +08005666 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket,
5667 i, &xv, NULL);
5668
5669 ret = ocfs2_lock_xattr_remove_allocators(inode, xv,
5670 args->ref_ci,
5671 args->ref_root_bh,
5672 &ctxt.meta_ac,
5673 &ref_credits);
5674
5675 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
Tao Ma88c3b062008-12-11 08:54:11 +08005676 if (IS_ERR(ctxt.handle)) {
5677 ret = PTR_ERR(ctxt.handle);
5678 mlog_errno(ret);
5679 break;
5680 }
5681
Joel Becker548b0f22008-11-24 19:32:13 -08005682 ret = ocfs2_xattr_bucket_value_truncate(inode, bucket,
Tao Ma78f30c32008-11-12 08:27:00 +08005683 i, 0, &ctxt);
Tao Ma88c3b062008-12-11 08:54:11 +08005684
5685 ocfs2_commit_trans(osb, ctxt.handle);
Tao Mace9c5a52009-08-18 11:43:59 +08005686 if (ctxt.meta_ac) {
5687 ocfs2_free_alloc_context(ctxt.meta_ac);
5688 ctxt.meta_ac = NULL;
5689 }
Tao Maa3944252008-08-18 17:38:54 +08005690 if (ret) {
5691 mlog_errno(ret);
5692 break;
5693 }
5694 }
5695
Tao Mace9c5a52009-08-18 11:43:59 +08005696 if (ctxt.meta_ac)
5697 ocfs2_free_alloc_context(ctxt.meta_ac);
Tao Ma78f30c32008-11-12 08:27:00 +08005698 ocfs2_schedule_truncate_log_flush(osb, 1);
5699 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tao Maa3944252008-08-18 17:38:54 +08005700 return ret;
5701}
5702
Mark Fasheh99219ae2008-10-07 14:52:59 -07005703/*
Tao Ma492a8a32009-08-18 11:43:17 +08005704 * Whenever we modify a xattr value root in the bucket(e.g, CoW
5705 * or change the extent record flag), we need to recalculate
5706 * the metaecc for the whole bucket. So it is done here.
5707 *
5708 * Note:
5709 * We have to give the extra credits for the caller.
5710 */
5711static int ocfs2_xattr_bucket_post_refcount(struct inode *inode,
5712 handle_t *handle,
5713 void *para)
5714{
5715 int ret;
5716 struct ocfs2_xattr_bucket *bucket =
5717 (struct ocfs2_xattr_bucket *)para;
5718
5719 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
5720 OCFS2_JOURNAL_ACCESS_WRITE);
5721 if (ret) {
5722 mlog_errno(ret);
5723 return ret;
5724 }
5725
5726 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
5727
5728 return 0;
5729}
5730
5731/*
5732 * Special action we need if the xattr value is refcounted.
5733 *
5734 * 1. If the xattr is refcounted, lock the tree.
5735 * 2. CoW the xattr if we are setting the new value and the value
5736 * will be stored outside.
5737 * 3. In other case, decrease_refcount will work for us, so just
5738 * lock the refcount tree, calculate the meta and credits is OK.
5739 *
5740 * We have to do CoW before ocfs2_init_xattr_set_ctxt since
5741 * currently CoW is a completed transaction, while this function
5742 * will also lock the allocators and let us deadlock. So we will
5743 * CoW the whole xattr value.
5744 */
5745static int ocfs2_prepare_refcount_xattr(struct inode *inode,
5746 struct ocfs2_dinode *di,
5747 struct ocfs2_xattr_info *xi,
5748 struct ocfs2_xattr_search *xis,
5749 struct ocfs2_xattr_search *xbs,
5750 struct ocfs2_refcount_tree **ref_tree,
5751 int *meta_add,
5752 int *credits)
5753{
5754 int ret = 0;
5755 struct ocfs2_xattr_block *xb;
5756 struct ocfs2_xattr_entry *xe;
5757 char *base;
5758 u32 p_cluster, num_clusters;
5759 unsigned int ext_flags;
5760 int name_offset, name_len;
5761 struct ocfs2_xattr_value_buf vb;
5762 struct ocfs2_xattr_bucket *bucket = NULL;
5763 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5764 struct ocfs2_post_refcount refcount;
5765 struct ocfs2_post_refcount *p = NULL;
5766 struct buffer_head *ref_root_bh = NULL;
5767
5768 if (!xis->not_found) {
5769 xe = xis->here;
5770 name_offset = le16_to_cpu(xe->xe_name_offset);
5771 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5772 base = xis->base;
5773 vb.vb_bh = xis->inode_bh;
5774 vb.vb_access = ocfs2_journal_access_di;
5775 } else {
5776 int i, block_off = 0;
5777 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
5778 xe = xbs->here;
5779 name_offset = le16_to_cpu(xe->xe_name_offset);
5780 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5781 i = xbs->here - xbs->header->xh_entries;
5782
5783 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
Tao Mafd68a892009-08-18 11:43:21 +08005784 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Tao Ma492a8a32009-08-18 11:43:17 +08005785 bucket_xh(xbs->bucket),
5786 i, &block_off,
5787 &name_offset);
5788 if (ret) {
5789 mlog_errno(ret);
5790 goto out;
5791 }
5792 base = bucket_block(xbs->bucket, block_off);
5793 vb.vb_bh = xbs->bucket->bu_bhs[block_off];
5794 vb.vb_access = ocfs2_journal_access;
5795
5796 if (ocfs2_meta_ecc(osb)) {
5797 /*create parameters for ocfs2_post_refcount. */
5798 bucket = xbs->bucket;
5799 refcount.credits = bucket->bu_blocks;
5800 refcount.para = bucket;
5801 refcount.func =
5802 ocfs2_xattr_bucket_post_refcount;
5803 p = &refcount;
5804 }
5805 } else {
5806 base = xbs->base;
5807 vb.vb_bh = xbs->xattr_bh;
5808 vb.vb_access = ocfs2_journal_access_xb;
5809 }
5810 }
5811
5812 if (ocfs2_xattr_is_local(xe))
5813 goto out;
5814
5815 vb.vb_xv = (struct ocfs2_xattr_value_root *)
5816 (base + name_offset + name_len);
5817
5818 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
5819 &num_clusters, &vb.vb_xv->xr_list,
5820 &ext_flags);
5821 if (ret) {
5822 mlog_errno(ret);
5823 goto out;
5824 }
5825
5826 /*
5827 * We just need to check the 1st extent record, since we always
5828 * CoW the whole xattr. So there shouldn't be a xattr with
5829 * some REFCOUNT extent recs after the 1st one.
5830 */
5831 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
5832 goto out;
5833
5834 ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
5835 1, ref_tree, &ref_root_bh);
5836 if (ret) {
5837 mlog_errno(ret);
5838 goto out;
5839 }
5840
5841 /*
5842 * If we are deleting the xattr or the new size will be stored inside,
5843 * cool, leave it there, the xattr truncate process will remove them
5844 * for us(it still needs the refcount tree lock and the meta, credits).
5845 * And the worse case is that every cluster truncate will split the
5846 * refcount tree, and make the original extent become 3. So we will need
5847 * 2 * cluster more extent recs at most.
5848 */
Joel Becker6b240ff2009-08-14 18:02:52 -07005849 if (!xi->xi_value || xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE) {
Tao Ma492a8a32009-08-18 11:43:17 +08005850
5851 ret = ocfs2_refcounted_xattr_delete_need(inode,
5852 &(*ref_tree)->rf_ci,
5853 ref_root_bh, vb.vb_xv,
5854 meta_add, credits);
5855 if (ret)
5856 mlog_errno(ret);
5857 goto out;
5858 }
5859
5860 ret = ocfs2_refcount_cow_xattr(inode, di, &vb,
5861 *ref_tree, ref_root_bh, 0,
5862 le32_to_cpu(vb.vb_xv->xr_clusters), p);
5863 if (ret)
5864 mlog_errno(ret);
5865
5866out:
5867 brelse(ref_root_bh);
5868 return ret;
5869}
5870
5871/*
Tao Ma01292412009-09-21 13:04:19 +08005872 * Add the REFCOUNTED flags for all the extent rec in ocfs2_xattr_value_root.
5873 * The physical clusters will be added to refcount tree.
5874 */
5875static int ocfs2_xattr_value_attach_refcount(struct inode *inode,
5876 struct ocfs2_xattr_value_root *xv,
5877 struct ocfs2_extent_tree *value_et,
5878 struct ocfs2_caching_info *ref_ci,
5879 struct buffer_head *ref_root_bh,
5880 struct ocfs2_cached_dealloc_ctxt *dealloc,
5881 struct ocfs2_post_refcount *refcount)
5882{
5883 int ret = 0;
5884 u32 clusters = le32_to_cpu(xv->xr_clusters);
5885 u32 cpos, p_cluster, num_clusters;
5886 struct ocfs2_extent_list *el = &xv->xr_list;
5887 unsigned int ext_flags;
5888
5889 cpos = 0;
5890 while (cpos < clusters) {
5891 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
5892 &num_clusters, el, &ext_flags);
5893
5894 cpos += num_clusters;
5895 if ((ext_flags & OCFS2_EXT_REFCOUNTED))
5896 continue;
5897
5898 BUG_ON(!p_cluster);
5899
5900 ret = ocfs2_add_refcount_flag(inode, value_et,
5901 ref_ci, ref_root_bh,
5902 cpos - num_clusters,
5903 p_cluster, num_clusters,
5904 dealloc, refcount);
5905 if (ret) {
5906 mlog_errno(ret);
5907 break;
5908 }
5909 }
5910
5911 return ret;
5912}
5913
5914/*
5915 * Given a normal ocfs2_xattr_header, refcount all the entries which
5916 * have value stored outside.
5917 * Used for xattrs stored in inode and ocfs2_xattr_block.
5918 */
5919static int ocfs2_xattr_attach_refcount_normal(struct inode *inode,
5920 struct ocfs2_xattr_value_buf *vb,
5921 struct ocfs2_xattr_header *header,
5922 struct ocfs2_caching_info *ref_ci,
5923 struct buffer_head *ref_root_bh,
5924 struct ocfs2_cached_dealloc_ctxt *dealloc)
5925{
5926
5927 struct ocfs2_xattr_entry *xe;
5928 struct ocfs2_xattr_value_root *xv;
5929 struct ocfs2_extent_tree et;
5930 int i, ret = 0;
5931
5932 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
5933 xe = &header->xh_entries[i];
5934
5935 if (ocfs2_xattr_is_local(xe))
5936 continue;
5937
5938 xv = (struct ocfs2_xattr_value_root *)((void *)header +
5939 le16_to_cpu(xe->xe_name_offset) +
5940 OCFS2_XATTR_SIZE(xe->xe_name_len));
5941
5942 vb->vb_xv = xv;
5943 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
5944
5945 ret = ocfs2_xattr_value_attach_refcount(inode, xv, &et,
5946 ref_ci, ref_root_bh,
5947 dealloc, NULL);
5948 if (ret) {
5949 mlog_errno(ret);
5950 break;
5951 }
5952 }
5953
5954 return ret;
5955}
5956
5957static int ocfs2_xattr_inline_attach_refcount(struct inode *inode,
5958 struct buffer_head *fe_bh,
5959 struct ocfs2_caching_info *ref_ci,
5960 struct buffer_head *ref_root_bh,
5961 struct ocfs2_cached_dealloc_ctxt *dealloc)
5962{
5963 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
5964 struct ocfs2_xattr_header *header = (struct ocfs2_xattr_header *)
5965 (fe_bh->b_data + inode->i_sb->s_blocksize -
5966 le16_to_cpu(di->i_xattr_inline_size));
5967 struct ocfs2_xattr_value_buf vb = {
5968 .vb_bh = fe_bh,
5969 .vb_access = ocfs2_journal_access_di,
5970 };
5971
5972 return ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
5973 ref_ci, ref_root_bh, dealloc);
5974}
5975
5976struct ocfs2_xattr_tree_value_refcount_para {
5977 struct ocfs2_caching_info *ref_ci;
5978 struct buffer_head *ref_root_bh;
5979 struct ocfs2_cached_dealloc_ctxt *dealloc;
5980};
5981
5982static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
5983 struct ocfs2_xattr_bucket *bucket,
5984 int offset,
5985 struct ocfs2_xattr_value_root **xv,
5986 struct buffer_head **bh)
5987{
5988 int ret, block_off, name_offset;
5989 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5990 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
5991 void *base;
5992
5993 ret = ocfs2_xattr_bucket_get_name_value(sb,
5994 bucket_xh(bucket),
5995 offset,
5996 &block_off,
5997 &name_offset);
5998 if (ret) {
5999 mlog_errno(ret);
6000 goto out;
6001 }
6002
6003 base = bucket_block(bucket, block_off);
6004
6005 *xv = (struct ocfs2_xattr_value_root *)(base + name_offset +
6006 OCFS2_XATTR_SIZE(xe->xe_name_len));
6007
6008 if (bh)
6009 *bh = bucket->bu_bhs[block_off];
6010out:
6011 return ret;
6012}
6013
6014/*
6015 * For a given xattr bucket, refcount all the entries which
6016 * have value stored outside.
6017 */
6018static int ocfs2_xattr_bucket_value_refcount(struct inode *inode,
6019 struct ocfs2_xattr_bucket *bucket,
6020 void *para)
6021{
6022 int i, ret = 0;
6023 struct ocfs2_extent_tree et;
6024 struct ocfs2_xattr_tree_value_refcount_para *ref =
6025 (struct ocfs2_xattr_tree_value_refcount_para *)para;
6026 struct ocfs2_xattr_header *xh =
6027 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6028 struct ocfs2_xattr_entry *xe;
6029 struct ocfs2_xattr_value_buf vb = {
6030 .vb_access = ocfs2_journal_access,
6031 };
6032 struct ocfs2_post_refcount refcount = {
6033 .credits = bucket->bu_blocks,
6034 .para = bucket,
6035 .func = ocfs2_xattr_bucket_post_refcount,
6036 };
6037 struct ocfs2_post_refcount *p = NULL;
6038
6039 /* We only need post_refcount if we support metaecc. */
6040 if (ocfs2_meta_ecc(OCFS2_SB(inode->i_sb)))
6041 p = &refcount;
6042
6043 mlog(0, "refcount bucket %llu, count = %u\n",
6044 (unsigned long long)bucket_blkno(bucket),
6045 le16_to_cpu(xh->xh_count));
6046 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6047 xe = &xh->xh_entries[i];
6048
6049 if (ocfs2_xattr_is_local(xe))
6050 continue;
6051
6052 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket, i,
6053 &vb.vb_xv, &vb.vb_bh);
6054 if (ret) {
6055 mlog_errno(ret);
6056 break;
6057 }
6058
6059 ocfs2_init_xattr_value_extent_tree(&et,
6060 INODE_CACHE(inode), &vb);
6061
6062 ret = ocfs2_xattr_value_attach_refcount(inode, vb.vb_xv,
6063 &et, ref->ref_ci,
6064 ref->ref_root_bh,
6065 ref->dealloc, p);
6066 if (ret) {
6067 mlog_errno(ret);
6068 break;
6069 }
6070 }
6071
6072 return ret;
6073
6074}
6075
6076static int ocfs2_refcount_xattr_tree_rec(struct inode *inode,
6077 struct buffer_head *root_bh,
6078 u64 blkno, u32 cpos, u32 len, void *para)
6079{
6080 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
6081 ocfs2_xattr_bucket_value_refcount,
6082 para);
6083}
6084
6085static int ocfs2_xattr_block_attach_refcount(struct inode *inode,
6086 struct buffer_head *blk_bh,
6087 struct ocfs2_caching_info *ref_ci,
6088 struct buffer_head *ref_root_bh,
6089 struct ocfs2_cached_dealloc_ctxt *dealloc)
6090{
6091 int ret = 0;
6092 struct ocfs2_xattr_block *xb =
6093 (struct ocfs2_xattr_block *)blk_bh->b_data;
6094
6095 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
6096 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
6097 struct ocfs2_xattr_value_buf vb = {
6098 .vb_bh = blk_bh,
6099 .vb_access = ocfs2_journal_access_xb,
6100 };
6101
6102 ret = ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
6103 ref_ci, ref_root_bh,
6104 dealloc);
6105 } else {
6106 struct ocfs2_xattr_tree_value_refcount_para para = {
6107 .ref_ci = ref_ci,
6108 .ref_root_bh = ref_root_bh,
6109 .dealloc = dealloc,
6110 };
6111
6112 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
6113 ocfs2_refcount_xattr_tree_rec,
6114 &para);
6115 }
6116
6117 return ret;
6118}
6119
6120int ocfs2_xattr_attach_refcount_tree(struct inode *inode,
6121 struct buffer_head *fe_bh,
6122 struct ocfs2_caching_info *ref_ci,
6123 struct buffer_head *ref_root_bh,
6124 struct ocfs2_cached_dealloc_ctxt *dealloc)
6125{
6126 int ret = 0;
6127 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6128 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
6129 struct buffer_head *blk_bh = NULL;
6130
6131 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
6132 ret = ocfs2_xattr_inline_attach_refcount(inode, fe_bh,
6133 ref_ci, ref_root_bh,
6134 dealloc);
6135 if (ret) {
6136 mlog_errno(ret);
6137 goto out;
6138 }
6139 }
6140
6141 if (!di->i_xattr_loc)
6142 goto out;
6143
6144 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
6145 &blk_bh);
6146 if (ret < 0) {
6147 mlog_errno(ret);
6148 goto out;
6149 }
6150
6151 ret = ocfs2_xattr_block_attach_refcount(inode, blk_bh, ref_ci,
6152 ref_root_bh, dealloc);
6153 if (ret)
6154 mlog_errno(ret);
6155
6156 brelse(blk_bh);
6157out:
6158
6159 return ret;
6160}
6161
Tao Ma0fe9b662009-08-18 11:47:56 +08006162typedef int (should_xattr_reflinked)(struct ocfs2_xattr_entry *xe);
Tao Ma01292412009-09-21 13:04:19 +08006163/*
Tao Ma2999d122009-08-18 11:43:55 +08006164 * Store the information we need in xattr reflink.
6165 * old_bh and new_bh are inode bh for the old and new inode.
6166 */
6167struct ocfs2_xattr_reflink {
6168 struct inode *old_inode;
6169 struct inode *new_inode;
6170 struct buffer_head *old_bh;
6171 struct buffer_head *new_bh;
6172 struct ocfs2_caching_info *ref_ci;
6173 struct buffer_head *ref_root_bh;
6174 struct ocfs2_cached_dealloc_ctxt *dealloc;
Tao Ma0fe9b662009-08-18 11:47:56 +08006175 should_xattr_reflinked *xattr_reflinked;
Tao Ma2999d122009-08-18 11:43:55 +08006176};
6177
6178/*
6179 * Given a xattr header and xe offset,
6180 * return the proper xv and the corresponding bh.
6181 * xattr in inode, block and xattr tree have different implementaions.
6182 */
6183typedef int (get_xattr_value_root)(struct super_block *sb,
6184 struct buffer_head *bh,
6185 struct ocfs2_xattr_header *xh,
6186 int offset,
6187 struct ocfs2_xattr_value_root **xv,
6188 struct buffer_head **ret_bh,
6189 void *para);
6190
6191/*
6192 * Calculate all the xattr value root metadata stored in this xattr header and
6193 * credits we need if we create them from the scratch.
6194 * We use get_xattr_value_root so that all types of xattr container can use it.
6195 */
6196static int ocfs2_value_metas_in_xattr_header(struct super_block *sb,
6197 struct buffer_head *bh,
6198 struct ocfs2_xattr_header *xh,
6199 int *metas, int *credits,
6200 int *num_recs,
6201 get_xattr_value_root *func,
6202 void *para)
6203{
6204 int i, ret = 0;
6205 struct ocfs2_xattr_value_root *xv;
6206 struct ocfs2_xattr_entry *xe;
6207
6208 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6209 xe = &xh->xh_entries[i];
6210 if (ocfs2_xattr_is_local(xe))
6211 continue;
6212
6213 ret = func(sb, bh, xh, i, &xv, NULL, para);
6214 if (ret) {
6215 mlog_errno(ret);
6216 break;
6217 }
6218
6219 *metas += le16_to_cpu(xv->xr_list.l_tree_depth) *
6220 le16_to_cpu(xv->xr_list.l_next_free_rec);
6221
6222 *credits += ocfs2_calc_extend_credits(sb,
6223 &def_xv.xv.xr_list,
6224 le32_to_cpu(xv->xr_clusters));
6225
6226 /*
6227 * If the value is a tree with depth > 1, We don't go deep
6228 * to the extent block, so just calculate a maximum record num.
6229 */
6230 if (!xv->xr_list.l_tree_depth)
Tao Ma8ff6af82009-12-23 14:31:15 +08006231 *num_recs += le16_to_cpu(xv->xr_list.l_next_free_rec);
Tao Ma2999d122009-08-18 11:43:55 +08006232 else
6233 *num_recs += ocfs2_clusters_for_bytes(sb,
6234 XATTR_SIZE_MAX);
6235 }
6236
6237 return ret;
6238}
6239
6240/* Used by xattr inode and block to return the right xv and buffer_head. */
6241static int ocfs2_get_xattr_value_root(struct super_block *sb,
6242 struct buffer_head *bh,
6243 struct ocfs2_xattr_header *xh,
6244 int offset,
6245 struct ocfs2_xattr_value_root **xv,
6246 struct buffer_head **ret_bh,
6247 void *para)
6248{
6249 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
6250
6251 *xv = (struct ocfs2_xattr_value_root *)((void *)xh +
6252 le16_to_cpu(xe->xe_name_offset) +
6253 OCFS2_XATTR_SIZE(xe->xe_name_len));
6254
6255 if (ret_bh)
6256 *ret_bh = bh;
6257
6258 return 0;
6259}
6260
6261/*
6262 * Lock the meta_ac and caculate how much credits we need for reflink xattrs.
6263 * It is only used for inline xattr and xattr block.
6264 */
6265static int ocfs2_reflink_lock_xattr_allocators(struct ocfs2_super *osb,
6266 struct ocfs2_xattr_header *xh,
6267 struct buffer_head *ref_root_bh,
6268 int *credits,
6269 struct ocfs2_alloc_context **meta_ac)
6270{
6271 int ret, meta_add = 0, num_recs = 0;
6272 struct ocfs2_refcount_block *rb =
6273 (struct ocfs2_refcount_block *)ref_root_bh->b_data;
6274
6275 *credits = 0;
6276
6277 ret = ocfs2_value_metas_in_xattr_header(osb->sb, NULL, xh,
6278 &meta_add, credits, &num_recs,
6279 ocfs2_get_xattr_value_root,
6280 NULL);
6281 if (ret) {
6282 mlog_errno(ret);
6283 goto out;
6284 }
6285
6286 /*
6287 * We need to add/modify num_recs in refcount tree, so just calculate
6288 * an approximate number we need for refcount tree change.
6289 * Sometimes we need to split the tree, and after split, half recs
6290 * will be moved to the new block, and a new block can only provide
6291 * half number of recs. So we multiple new blocks by 2.
6292 */
6293 num_recs = num_recs / ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6294 meta_add += num_recs;
6295 *credits += num_recs + num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6296 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6297 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6298 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6299 else
6300 *credits += 1;
6301
6302 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add, meta_ac);
6303 if (ret)
6304 mlog_errno(ret);
6305
6306out:
6307 return ret;
6308}
6309
6310/*
6311 * Given a xattr header, reflink all the xattrs in this container.
6312 * It can be used for inode, block and bucket.
6313 *
6314 * NOTE:
6315 * Before we call this function, the caller has memcpy the xattr in
6316 * old_xh to the new_xh.
Tao Ma0fe9b662009-08-18 11:47:56 +08006317 *
6318 * If args.xattr_reflinked is set, call it to decide whether the xe should
6319 * be reflinked or not. If not, remove it from the new xattr header.
Tao Ma2999d122009-08-18 11:43:55 +08006320 */
6321static int ocfs2_reflink_xattr_header(handle_t *handle,
6322 struct ocfs2_xattr_reflink *args,
6323 struct buffer_head *old_bh,
6324 struct ocfs2_xattr_header *xh,
6325 struct buffer_head *new_bh,
6326 struct ocfs2_xattr_header *new_xh,
6327 struct ocfs2_xattr_value_buf *vb,
6328 struct ocfs2_alloc_context *meta_ac,
6329 get_xattr_value_root *func,
6330 void *para)
6331{
Tao Ma0fe9b662009-08-18 11:47:56 +08006332 int ret = 0, i, j;
Tao Ma2999d122009-08-18 11:43:55 +08006333 struct super_block *sb = args->old_inode->i_sb;
6334 struct buffer_head *value_bh;
Tao Ma0fe9b662009-08-18 11:47:56 +08006335 struct ocfs2_xattr_entry *xe, *last;
Tao Ma2999d122009-08-18 11:43:55 +08006336 struct ocfs2_xattr_value_root *xv, *new_xv;
6337 struct ocfs2_extent_tree data_et;
6338 u32 clusters, cpos, p_cluster, num_clusters;
6339 unsigned int ext_flags = 0;
6340
6341 mlog(0, "reflink xattr in container %llu, count = %u\n",
6342 (unsigned long long)old_bh->b_blocknr, le16_to_cpu(xh->xh_count));
Tao Ma0fe9b662009-08-18 11:47:56 +08006343
6344 last = &new_xh->xh_entries[le16_to_cpu(new_xh->xh_count)];
6345 for (i = 0, j = 0; i < le16_to_cpu(xh->xh_count); i++, j++) {
Tao Ma2999d122009-08-18 11:43:55 +08006346 xe = &xh->xh_entries[i];
6347
Tao Ma0fe9b662009-08-18 11:47:56 +08006348 if (args->xattr_reflinked && !args->xattr_reflinked(xe)) {
6349 xe = &new_xh->xh_entries[j];
6350
6351 le16_add_cpu(&new_xh->xh_count, -1);
6352 if (new_xh->xh_count) {
6353 memmove(xe, xe + 1,
6354 (void *)last - (void *)xe);
6355 memset(last, 0,
6356 sizeof(struct ocfs2_xattr_entry));
6357 }
6358
6359 /*
6360 * We don't want j to increase in the next round since
6361 * it is already moved ahead.
6362 */
6363 j--;
6364 continue;
6365 }
6366
Tao Ma2999d122009-08-18 11:43:55 +08006367 if (ocfs2_xattr_is_local(xe))
6368 continue;
6369
6370 ret = func(sb, old_bh, xh, i, &xv, NULL, para);
6371 if (ret) {
6372 mlog_errno(ret);
6373 break;
6374 }
6375
Tao Ma0fe9b662009-08-18 11:47:56 +08006376 ret = func(sb, new_bh, new_xh, j, &new_xv, &value_bh, para);
Tao Ma2999d122009-08-18 11:43:55 +08006377 if (ret) {
6378 mlog_errno(ret);
6379 break;
6380 }
6381
6382 /*
6383 * For the xattr which has l_tree_depth = 0, all the extent
6384 * recs have already be copied to the new xh with the
6385 * propriate OCFS2_EXT_REFCOUNTED flag we just need to
6386 * increase the refount count int the refcount tree.
6387 *
6388 * For the xattr which has l_tree_depth > 0, we need
6389 * to initialize it to the empty default value root,
6390 * and then insert the extents one by one.
6391 */
6392 if (xv->xr_list.l_tree_depth) {
6393 memcpy(new_xv, &def_xv, sizeof(def_xv));
6394 vb->vb_xv = new_xv;
6395 vb->vb_bh = value_bh;
6396 ocfs2_init_xattr_value_extent_tree(&data_et,
6397 INODE_CACHE(args->new_inode), vb);
6398 }
6399
6400 clusters = le32_to_cpu(xv->xr_clusters);
6401 cpos = 0;
6402 while (cpos < clusters) {
6403 ret = ocfs2_xattr_get_clusters(args->old_inode,
6404 cpos,
6405 &p_cluster,
6406 &num_clusters,
6407 &xv->xr_list,
6408 &ext_flags);
6409 if (ret) {
6410 mlog_errno(ret);
6411 goto out;
6412 }
6413
6414 BUG_ON(!p_cluster);
6415
6416 if (xv->xr_list.l_tree_depth) {
6417 ret = ocfs2_insert_extent(handle,
6418 &data_et, cpos,
6419 ocfs2_clusters_to_blocks(
6420 args->old_inode->i_sb,
6421 p_cluster),
6422 num_clusters, ext_flags,
6423 meta_ac);
6424 if (ret) {
6425 mlog_errno(ret);
6426 goto out;
6427 }
6428 }
6429
6430 ret = ocfs2_increase_refcount(handle, args->ref_ci,
6431 args->ref_root_bh,
6432 p_cluster, num_clusters,
6433 meta_ac, args->dealloc);
6434 if (ret) {
6435 mlog_errno(ret);
6436 goto out;
6437 }
6438
6439 cpos += num_clusters;
6440 }
6441 }
6442
6443out:
6444 return ret;
6445}
6446
6447static int ocfs2_reflink_xattr_inline(struct ocfs2_xattr_reflink *args)
6448{
6449 int ret = 0, credits = 0;
6450 handle_t *handle;
6451 struct ocfs2_super *osb = OCFS2_SB(args->old_inode->i_sb);
6452 struct ocfs2_dinode *di = (struct ocfs2_dinode *)args->old_bh->b_data;
6453 int inline_size = le16_to_cpu(di->i_xattr_inline_size);
6454 int header_off = osb->sb->s_blocksize - inline_size;
6455 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)
6456 (args->old_bh->b_data + header_off);
6457 struct ocfs2_xattr_header *new_xh = (struct ocfs2_xattr_header *)
6458 (args->new_bh->b_data + header_off);
6459 struct ocfs2_alloc_context *meta_ac = NULL;
6460 struct ocfs2_inode_info *new_oi;
6461 struct ocfs2_dinode *new_di;
6462 struct ocfs2_xattr_value_buf vb = {
6463 .vb_bh = args->new_bh,
6464 .vb_access = ocfs2_journal_access_di,
6465 };
6466
6467 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6468 &credits, &meta_ac);
6469 if (ret) {
6470 mlog_errno(ret);
6471 goto out;
6472 }
6473
6474 handle = ocfs2_start_trans(osb, credits);
6475 if (IS_ERR(handle)) {
6476 ret = PTR_ERR(handle);
6477 mlog_errno(ret);
6478 goto out;
6479 }
6480
6481 ret = ocfs2_journal_access_di(handle, INODE_CACHE(args->new_inode),
6482 args->new_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6483 if (ret) {
6484 mlog_errno(ret);
6485 goto out_commit;
6486 }
6487
6488 memcpy(args->new_bh->b_data + header_off,
6489 args->old_bh->b_data + header_off, inline_size);
6490
6491 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6492 new_di->i_xattr_inline_size = cpu_to_le16(inline_size);
6493
6494 ret = ocfs2_reflink_xattr_header(handle, args, args->old_bh, xh,
6495 args->new_bh, new_xh, &vb, meta_ac,
6496 ocfs2_get_xattr_value_root, NULL);
6497 if (ret) {
6498 mlog_errno(ret);
6499 goto out_commit;
6500 }
6501
6502 new_oi = OCFS2_I(args->new_inode);
6503 spin_lock(&new_oi->ip_lock);
6504 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL | OCFS2_INLINE_XATTR_FL;
6505 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6506 spin_unlock(&new_oi->ip_lock);
6507
6508 ocfs2_journal_dirty(handle, args->new_bh);
6509
6510out_commit:
6511 ocfs2_commit_trans(osb, handle);
6512
6513out:
6514 if (meta_ac)
6515 ocfs2_free_alloc_context(meta_ac);
6516 return ret;
6517}
6518
6519static int ocfs2_create_empty_xattr_block(struct inode *inode,
6520 struct buffer_head *fe_bh,
6521 struct buffer_head **ret_bh,
6522 int indexed)
6523{
6524 int ret;
6525 handle_t *handle;
6526 struct ocfs2_alloc_context *meta_ac;
6527 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6528
6529 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
6530 if (ret < 0) {
6531 mlog_errno(ret);
6532 return ret;
6533 }
6534
6535 handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_CREATE_CREDITS);
6536 if (IS_ERR(handle)) {
6537 ret = PTR_ERR(handle);
6538 mlog_errno(ret);
6539 goto out;
6540 }
6541
6542 mlog(0, "create new xattr block for inode %llu, index = %d\n",
6543 (unsigned long long)fe_bh->b_blocknr, indexed);
6544 ret = ocfs2_create_xattr_block(handle, inode, fe_bh,
6545 meta_ac, ret_bh, indexed);
6546 if (ret)
6547 mlog_errno(ret);
6548
6549 ocfs2_commit_trans(osb, handle);
6550out:
6551 ocfs2_free_alloc_context(meta_ac);
6552 return ret;
6553}
6554
6555static int ocfs2_reflink_xattr_block(struct ocfs2_xattr_reflink *args,
6556 struct buffer_head *blk_bh,
6557 struct buffer_head *new_blk_bh)
6558{
6559 int ret = 0, credits = 0;
6560 handle_t *handle;
6561 struct ocfs2_inode_info *new_oi = OCFS2_I(args->new_inode);
6562 struct ocfs2_dinode *new_di;
6563 struct ocfs2_super *osb = OCFS2_SB(args->new_inode->i_sb);
6564 int header_off = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
6565 struct ocfs2_xattr_block *xb =
6566 (struct ocfs2_xattr_block *)blk_bh->b_data;
6567 struct ocfs2_xattr_header *xh = &xb->xb_attrs.xb_header;
6568 struct ocfs2_xattr_block *new_xb =
6569 (struct ocfs2_xattr_block *)new_blk_bh->b_data;
6570 struct ocfs2_xattr_header *new_xh = &new_xb->xb_attrs.xb_header;
6571 struct ocfs2_alloc_context *meta_ac;
6572 struct ocfs2_xattr_value_buf vb = {
6573 .vb_bh = new_blk_bh,
6574 .vb_access = ocfs2_journal_access_xb,
6575 };
6576
6577 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6578 &credits, &meta_ac);
6579 if (ret) {
6580 mlog_errno(ret);
6581 return ret;
6582 }
6583
6584 /* One more credits in case we need to add xattr flags in new inode. */
6585 handle = ocfs2_start_trans(osb, credits + 1);
6586 if (IS_ERR(handle)) {
6587 ret = PTR_ERR(handle);
6588 mlog_errno(ret);
6589 goto out;
6590 }
6591
6592 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6593 ret = ocfs2_journal_access_di(handle,
6594 INODE_CACHE(args->new_inode),
6595 args->new_bh,
6596 OCFS2_JOURNAL_ACCESS_WRITE);
6597 if (ret) {
6598 mlog_errno(ret);
6599 goto out_commit;
6600 }
6601 }
6602
6603 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(args->new_inode),
6604 new_blk_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6605 if (ret) {
6606 mlog_errno(ret);
6607 goto out_commit;
6608 }
6609
6610 memcpy(new_blk_bh->b_data + header_off, blk_bh->b_data + header_off,
6611 osb->sb->s_blocksize - header_off);
6612
6613 ret = ocfs2_reflink_xattr_header(handle, args, blk_bh, xh,
6614 new_blk_bh, new_xh, &vb, meta_ac,
6615 ocfs2_get_xattr_value_root, NULL);
6616 if (ret) {
6617 mlog_errno(ret);
6618 goto out_commit;
6619 }
6620
6621 ocfs2_journal_dirty(handle, new_blk_bh);
6622
6623 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6624 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6625 spin_lock(&new_oi->ip_lock);
6626 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
6627 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6628 spin_unlock(&new_oi->ip_lock);
6629
6630 ocfs2_journal_dirty(handle, args->new_bh);
6631 }
6632
6633out_commit:
6634 ocfs2_commit_trans(osb, handle);
6635
6636out:
6637 ocfs2_free_alloc_context(meta_ac);
6638 return ret;
6639}
6640
6641struct ocfs2_reflink_xattr_tree_args {
6642 struct ocfs2_xattr_reflink *reflink;
6643 struct buffer_head *old_blk_bh;
6644 struct buffer_head *new_blk_bh;
6645 struct ocfs2_xattr_bucket *old_bucket;
6646 struct ocfs2_xattr_bucket *new_bucket;
6647};
6648
6649/*
6650 * NOTE:
6651 * We have to handle the case that both old bucket and new bucket
6652 * will call this function to get the right ret_bh.
6653 * So The caller must give us the right bh.
6654 */
6655static int ocfs2_get_reflink_xattr_value_root(struct super_block *sb,
6656 struct buffer_head *bh,
6657 struct ocfs2_xattr_header *xh,
6658 int offset,
6659 struct ocfs2_xattr_value_root **xv,
6660 struct buffer_head **ret_bh,
6661 void *para)
6662{
6663 struct ocfs2_reflink_xattr_tree_args *args =
6664 (struct ocfs2_reflink_xattr_tree_args *)para;
6665 struct ocfs2_xattr_bucket *bucket;
6666
6667 if (bh == args->old_bucket->bu_bhs[0])
6668 bucket = args->old_bucket;
6669 else
6670 bucket = args->new_bucket;
6671
6672 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6673 xv, ret_bh);
6674}
6675
6676struct ocfs2_value_tree_metas {
6677 int num_metas;
6678 int credits;
6679 int num_recs;
6680};
6681
6682static int ocfs2_value_tree_metas_in_bucket(struct super_block *sb,
6683 struct buffer_head *bh,
6684 struct ocfs2_xattr_header *xh,
6685 int offset,
6686 struct ocfs2_xattr_value_root **xv,
6687 struct buffer_head **ret_bh,
6688 void *para)
6689{
6690 struct ocfs2_xattr_bucket *bucket =
6691 (struct ocfs2_xattr_bucket *)para;
6692
6693 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6694 xv, ret_bh);
6695}
6696
6697static int ocfs2_calc_value_tree_metas(struct inode *inode,
6698 struct ocfs2_xattr_bucket *bucket,
6699 void *para)
6700{
6701 struct ocfs2_value_tree_metas *metas =
6702 (struct ocfs2_value_tree_metas *)para;
6703 struct ocfs2_xattr_header *xh =
6704 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6705
6706 /* Add the credits for this bucket first. */
6707 metas->credits += bucket->bu_blocks;
6708 return ocfs2_value_metas_in_xattr_header(inode->i_sb, bucket->bu_bhs[0],
6709 xh, &metas->num_metas,
6710 &metas->credits, &metas->num_recs,
6711 ocfs2_value_tree_metas_in_bucket,
6712 bucket);
6713}
6714
6715/*
6716 * Given a xattr extent rec starting from blkno and having len clusters,
6717 * iterate all the buckets calculate how much metadata we need for reflinking
6718 * all the ocfs2_xattr_value_root and lock the allocators accordingly.
6719 */
6720static int ocfs2_lock_reflink_xattr_rec_allocators(
6721 struct ocfs2_reflink_xattr_tree_args *args,
6722 struct ocfs2_extent_tree *xt_et,
6723 u64 blkno, u32 len, int *credits,
6724 struct ocfs2_alloc_context **meta_ac,
6725 struct ocfs2_alloc_context **data_ac)
6726{
6727 int ret, num_free_extents;
6728 struct ocfs2_value_tree_metas metas;
6729 struct ocfs2_super *osb = OCFS2_SB(args->reflink->old_inode->i_sb);
6730 struct ocfs2_refcount_block *rb;
6731
6732 memset(&metas, 0, sizeof(metas));
6733
6734 ret = ocfs2_iterate_xattr_buckets(args->reflink->old_inode, blkno, len,
6735 ocfs2_calc_value_tree_metas, &metas);
6736 if (ret) {
6737 mlog_errno(ret);
6738 goto out;
6739 }
6740
6741 *credits = metas.credits;
6742
6743 /*
6744 * Calculate we need for refcount tree change.
6745 *
6746 * We need to add/modify num_recs in refcount tree, so just calculate
6747 * an approximate number we need for refcount tree change.
6748 * Sometimes we need to split the tree, and after split, half recs
6749 * will be moved to the new block, and a new block can only provide
6750 * half number of recs. So we multiple new blocks by 2.
6751 * In the end, we have to add credits for modifying the already
6752 * existed refcount block.
6753 */
6754 rb = (struct ocfs2_refcount_block *)args->reflink->ref_root_bh->b_data;
6755 metas.num_recs =
6756 (metas.num_recs + ocfs2_refcount_recs_per_rb(osb->sb) - 1) /
6757 ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6758 metas.num_metas += metas.num_recs;
6759 *credits += metas.num_recs +
6760 metas.num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6761 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6762 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6763 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6764 else
6765 *credits += 1;
6766
6767 /* count in the xattr tree change. */
6768 num_free_extents = ocfs2_num_free_extents(osb, xt_et);
6769 if (num_free_extents < 0) {
6770 ret = num_free_extents;
6771 mlog_errno(ret);
6772 goto out;
6773 }
6774
6775 if (num_free_extents < len)
6776 metas.num_metas += ocfs2_extend_meta_needed(xt_et->et_root_el);
6777
6778 *credits += ocfs2_calc_extend_credits(osb->sb,
6779 xt_et->et_root_el, len);
6780
6781 if (metas.num_metas) {
6782 ret = ocfs2_reserve_new_metadata_blocks(osb, metas.num_metas,
6783 meta_ac);
6784 if (ret) {
6785 mlog_errno(ret);
6786 goto out;
6787 }
6788 }
6789
6790 if (len) {
6791 ret = ocfs2_reserve_clusters(osb, len, data_ac);
6792 if (ret)
6793 mlog_errno(ret);
6794 }
6795out:
6796 if (ret) {
6797 if (*meta_ac) {
6798 ocfs2_free_alloc_context(*meta_ac);
6799 meta_ac = NULL;
6800 }
6801 }
6802
6803 return ret;
6804}
6805
6806static int ocfs2_reflink_xattr_buckets(handle_t *handle,
6807 u64 blkno, u64 new_blkno, u32 clusters,
6808 struct ocfs2_alloc_context *meta_ac,
6809 struct ocfs2_alloc_context *data_ac,
6810 struct ocfs2_reflink_xattr_tree_args *args)
6811{
6812 int i, j, ret = 0;
6813 struct super_block *sb = args->reflink->old_inode->i_sb;
6814 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
6815 u32 num_buckets = clusters * bpc;
6816 int bpb = args->old_bucket->bu_blocks;
6817 struct ocfs2_xattr_value_buf vb = {
6818 .vb_access = ocfs2_journal_access,
6819 };
6820
6821 for (i = 0; i < num_buckets; i++, blkno += bpb, new_blkno += bpb) {
6822 ret = ocfs2_read_xattr_bucket(args->old_bucket, blkno);
6823 if (ret) {
6824 mlog_errno(ret);
6825 break;
6826 }
6827
6828 ret = ocfs2_init_xattr_bucket(args->new_bucket, new_blkno);
6829 if (ret) {
6830 mlog_errno(ret);
6831 break;
6832 }
6833
6834 /*
6835 * The real bucket num in this series of blocks is stored
6836 * in the 1st bucket.
6837 */
6838 if (i == 0)
6839 num_buckets = le16_to_cpu(
6840 bucket_xh(args->old_bucket)->xh_num_buckets);
6841
6842 ret = ocfs2_xattr_bucket_journal_access(handle,
6843 args->new_bucket,
6844 OCFS2_JOURNAL_ACCESS_CREATE);
6845 if (ret) {
6846 mlog_errno(ret);
6847 break;
6848 }
6849
6850 for (j = 0; j < bpb; j++)
6851 memcpy(bucket_block(args->new_bucket, j),
6852 bucket_block(args->old_bucket, j),
6853 sb->s_blocksize);
6854
6855 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
6856
6857 ret = ocfs2_reflink_xattr_header(handle, args->reflink,
6858 args->old_bucket->bu_bhs[0],
6859 bucket_xh(args->old_bucket),
6860 args->new_bucket->bu_bhs[0],
6861 bucket_xh(args->new_bucket),
6862 &vb, meta_ac,
6863 ocfs2_get_reflink_xattr_value_root,
6864 args);
6865 if (ret) {
6866 mlog_errno(ret);
6867 break;
6868 }
6869
6870 /*
6871 * Re-access and dirty the bucket to calculate metaecc.
6872 * Because we may extend the transaction in reflink_xattr_header
6873 * which will let the already accessed block gone.
6874 */
6875 ret = ocfs2_xattr_bucket_journal_access(handle,
6876 args->new_bucket,
6877 OCFS2_JOURNAL_ACCESS_WRITE);
6878 if (ret) {
6879 mlog_errno(ret);
6880 break;
6881 }
6882
6883 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
6884 ocfs2_xattr_bucket_relse(args->old_bucket);
6885 ocfs2_xattr_bucket_relse(args->new_bucket);
6886 }
6887
6888 ocfs2_xattr_bucket_relse(args->old_bucket);
6889 ocfs2_xattr_bucket_relse(args->new_bucket);
6890 return ret;
6891}
6892/*
6893 * Create the same xattr extent record in the new inode's xattr tree.
6894 */
6895static int ocfs2_reflink_xattr_rec(struct inode *inode,
6896 struct buffer_head *root_bh,
6897 u64 blkno,
6898 u32 cpos,
6899 u32 len,
6900 void *para)
6901{
6902 int ret, credits = 0;
6903 u32 p_cluster, num_clusters;
6904 u64 new_blkno;
6905 handle_t *handle;
6906 struct ocfs2_reflink_xattr_tree_args *args =
6907 (struct ocfs2_reflink_xattr_tree_args *)para;
6908 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6909 struct ocfs2_alloc_context *meta_ac = NULL;
6910 struct ocfs2_alloc_context *data_ac = NULL;
6911 struct ocfs2_extent_tree et;
6912
6913 ocfs2_init_xattr_tree_extent_tree(&et,
6914 INODE_CACHE(args->reflink->new_inode),
6915 args->new_blk_bh);
6916
6917 ret = ocfs2_lock_reflink_xattr_rec_allocators(args, &et, blkno,
6918 len, &credits,
6919 &meta_ac, &data_ac);
6920 if (ret) {
6921 mlog_errno(ret);
6922 goto out;
6923 }
6924
6925 handle = ocfs2_start_trans(osb, credits);
6926 if (IS_ERR(handle)) {
6927 ret = PTR_ERR(handle);
6928 mlog_errno(ret);
6929 goto out;
6930 }
6931
6932 ret = ocfs2_claim_clusters(osb, handle, data_ac,
6933 len, &p_cluster, &num_clusters);
6934 if (ret) {
6935 mlog_errno(ret);
6936 goto out_commit;
6937 }
6938
6939 new_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cluster);
6940
6941 mlog(0, "reflink xattr buckets %llu to %llu, len %u\n",
6942 (unsigned long long)blkno, (unsigned long long)new_blkno, len);
6943 ret = ocfs2_reflink_xattr_buckets(handle, blkno, new_blkno, len,
6944 meta_ac, data_ac, args);
6945 if (ret) {
6946 mlog_errno(ret);
6947 goto out_commit;
6948 }
6949
6950 mlog(0, "insert new xattr extent rec start %llu len %u to %u\n",
6951 (unsigned long long)new_blkno, len, cpos);
6952 ret = ocfs2_insert_extent(handle, &et, cpos, new_blkno,
6953 len, 0, meta_ac);
6954 if (ret)
6955 mlog_errno(ret);
6956
6957out_commit:
6958 ocfs2_commit_trans(osb, handle);
6959
6960out:
6961 if (meta_ac)
6962 ocfs2_free_alloc_context(meta_ac);
6963 if (data_ac)
6964 ocfs2_free_alloc_context(data_ac);
6965 return ret;
6966}
6967
6968/*
6969 * Create reflinked xattr buckets.
6970 * We will add bucket one by one, and refcount all the xattrs in the bucket
6971 * if they are stored outside.
6972 */
6973static int ocfs2_reflink_xattr_tree(struct ocfs2_xattr_reflink *args,
6974 struct buffer_head *blk_bh,
6975 struct buffer_head *new_blk_bh)
6976{
6977 int ret;
6978 struct ocfs2_reflink_xattr_tree_args para;
6979
6980 memset(&para, 0, sizeof(para));
6981 para.reflink = args;
6982 para.old_blk_bh = blk_bh;
6983 para.new_blk_bh = new_blk_bh;
6984
6985 para.old_bucket = ocfs2_xattr_bucket_new(args->old_inode);
6986 if (!para.old_bucket) {
6987 mlog_errno(-ENOMEM);
6988 return -ENOMEM;
6989 }
6990
6991 para.new_bucket = ocfs2_xattr_bucket_new(args->new_inode);
6992 if (!para.new_bucket) {
6993 ret = -ENOMEM;
6994 mlog_errno(ret);
6995 goto out;
6996 }
6997
6998 ret = ocfs2_iterate_xattr_index_block(args->old_inode, blk_bh,
6999 ocfs2_reflink_xattr_rec,
7000 &para);
7001 if (ret)
7002 mlog_errno(ret);
7003
7004out:
7005 ocfs2_xattr_bucket_free(para.old_bucket);
7006 ocfs2_xattr_bucket_free(para.new_bucket);
7007 return ret;
7008}
7009
7010static int ocfs2_reflink_xattr_in_block(struct ocfs2_xattr_reflink *args,
7011 struct buffer_head *blk_bh)
7012{
7013 int ret, indexed = 0;
7014 struct buffer_head *new_blk_bh = NULL;
7015 struct ocfs2_xattr_block *xb =
7016 (struct ocfs2_xattr_block *)blk_bh->b_data;
7017
7018
7019 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)
7020 indexed = 1;
7021
7022 ret = ocfs2_create_empty_xattr_block(args->new_inode, args->new_bh,
7023 &new_blk_bh, indexed);
7024 if (ret) {
7025 mlog_errno(ret);
7026 goto out;
7027 }
7028
7029 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED))
7030 ret = ocfs2_reflink_xattr_block(args, blk_bh, new_blk_bh);
7031 else
7032 ret = ocfs2_reflink_xattr_tree(args, blk_bh, new_blk_bh);
7033 if (ret)
7034 mlog_errno(ret);
7035
7036out:
7037 brelse(new_blk_bh);
7038 return ret;
7039}
7040
Tao Ma0fe9b662009-08-18 11:47:56 +08007041static int ocfs2_reflink_xattr_no_security(struct ocfs2_xattr_entry *xe)
7042{
7043 int type = ocfs2_xattr_get_type(xe);
7044
7045 return type != OCFS2_XATTR_INDEX_SECURITY &&
7046 type != OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS &&
7047 type != OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
7048}
7049
Tao Ma2999d122009-08-18 11:43:55 +08007050int ocfs2_reflink_xattrs(struct inode *old_inode,
7051 struct buffer_head *old_bh,
7052 struct inode *new_inode,
Tao Ma0fe9b662009-08-18 11:47:56 +08007053 struct buffer_head *new_bh,
7054 bool preserve_security)
Tao Ma2999d122009-08-18 11:43:55 +08007055{
7056 int ret;
7057 struct ocfs2_xattr_reflink args;
7058 struct ocfs2_inode_info *oi = OCFS2_I(old_inode);
7059 struct ocfs2_dinode *di = (struct ocfs2_dinode *)old_bh->b_data;
7060 struct buffer_head *blk_bh = NULL;
7061 struct ocfs2_cached_dealloc_ctxt dealloc;
7062 struct ocfs2_refcount_tree *ref_tree;
7063 struct buffer_head *ref_root_bh = NULL;
7064
7065 ret = ocfs2_lock_refcount_tree(OCFS2_SB(old_inode->i_sb),
7066 le64_to_cpu(di->i_refcount_loc),
7067 1, &ref_tree, &ref_root_bh);
7068 if (ret) {
7069 mlog_errno(ret);
7070 goto out;
7071 }
7072
7073 ocfs2_init_dealloc_ctxt(&dealloc);
7074
7075 args.old_inode = old_inode;
7076 args.new_inode = new_inode;
7077 args.old_bh = old_bh;
7078 args.new_bh = new_bh;
7079 args.ref_ci = &ref_tree->rf_ci;
7080 args.ref_root_bh = ref_root_bh;
7081 args.dealloc = &dealloc;
Tao Ma0fe9b662009-08-18 11:47:56 +08007082 if (preserve_security)
7083 args.xattr_reflinked = NULL;
7084 else
7085 args.xattr_reflinked = ocfs2_reflink_xattr_no_security;
Tao Ma2999d122009-08-18 11:43:55 +08007086
7087 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
7088 ret = ocfs2_reflink_xattr_inline(&args);
7089 if (ret) {
7090 mlog_errno(ret);
7091 goto out_unlock;
7092 }
7093 }
7094
7095 if (!di->i_xattr_loc)
7096 goto out_unlock;
7097
7098 ret = ocfs2_read_xattr_block(old_inode, le64_to_cpu(di->i_xattr_loc),
7099 &blk_bh);
7100 if (ret < 0) {
7101 mlog_errno(ret);
7102 goto out_unlock;
7103 }
7104
7105 ret = ocfs2_reflink_xattr_in_block(&args, blk_bh);
7106 if (ret)
7107 mlog_errno(ret);
7108
7109 brelse(blk_bh);
7110
7111out_unlock:
7112 ocfs2_unlock_refcount_tree(OCFS2_SB(old_inode->i_sb),
7113 ref_tree, 1);
7114 brelse(ref_root_bh);
7115
7116 if (ocfs2_dealloc_has_cluster(&dealloc)) {
7117 ocfs2_schedule_truncate_log_flush(OCFS2_SB(old_inode->i_sb), 1);
7118 ocfs2_run_deallocs(OCFS2_SB(old_inode->i_sb), &dealloc);
7119 }
7120
7121out:
7122 return ret;
7123}
7124
7125/*
Tao Ma0fe9b662009-08-18 11:47:56 +08007126 * Initialize security and acl for a already created inode.
7127 * Used for reflink a non-preserve-security file.
7128 *
7129 * It uses common api like ocfs2_xattr_set, so the caller
7130 * must not hold any lock expect i_mutex.
7131 */
7132int ocfs2_init_security_and_acl(struct inode *dir,
7133 struct inode *inode)
7134{
7135 int ret = 0;
7136 struct buffer_head *dir_bh = NULL;
7137 struct ocfs2_security_xattr_info si = {
7138 .enable = 1,
7139 };
7140
7141 ret = ocfs2_init_security_get(inode, dir, &si);
7142 if (!ret) {
Christoph Hellwig431547b2009-11-13 09:52:56 +00007143 ret = ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY,
7144 si.name, si.value, si.value_len,
7145 XATTR_CREATE);
Tao Ma0fe9b662009-08-18 11:47:56 +08007146 if (ret) {
7147 mlog_errno(ret);
7148 goto leave;
7149 }
7150 } else if (ret != -EOPNOTSUPP) {
7151 mlog_errno(ret);
7152 goto leave;
7153 }
7154
7155 ret = ocfs2_inode_lock(dir, &dir_bh, 0);
7156 if (ret) {
7157 mlog_errno(ret);
7158 goto leave;
7159 }
7160
7161 ret = ocfs2_init_acl(NULL, inode, dir, NULL, dir_bh, NULL, NULL);
7162 if (ret)
7163 mlog_errno(ret);
7164
7165 ocfs2_inode_unlock(dir, 0);
7166 brelse(dir_bh);
7167leave:
7168 return ret;
7169}
7170/*
Tiger Yang923f7f32008-11-14 11:16:27 +08007171 * 'security' attributes support
7172 */
Christoph Hellwig431547b2009-11-13 09:52:56 +00007173static size_t ocfs2_xattr_security_list(struct dentry *dentry, char *list,
Tiger Yang923f7f32008-11-14 11:16:27 +08007174 size_t list_size, const char *name,
Christoph Hellwig431547b2009-11-13 09:52:56 +00007175 size_t name_len, int type)
Tiger Yang923f7f32008-11-14 11:16:27 +08007176{
7177 const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN;
7178 const size_t total_len = prefix_len + name_len + 1;
7179
7180 if (list && total_len <= list_size) {
7181 memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
7182 memcpy(list + prefix_len, name, name_len);
7183 list[prefix_len + name_len] = '\0';
7184 }
7185 return total_len;
7186}
7187
Christoph Hellwig431547b2009-11-13 09:52:56 +00007188static int ocfs2_xattr_security_get(struct dentry *dentry, const char *name,
7189 void *buffer, size_t size, int type)
Tiger Yang923f7f32008-11-14 11:16:27 +08007190{
7191 if (strcmp(name, "") == 0)
7192 return -EINVAL;
Christoph Hellwig431547b2009-11-13 09:52:56 +00007193 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
7194 name, buffer, size);
Tiger Yang923f7f32008-11-14 11:16:27 +08007195}
7196
Christoph Hellwig431547b2009-11-13 09:52:56 +00007197static int ocfs2_xattr_security_set(struct dentry *dentry, const char *name,
7198 const void *value, size_t size, int flags, int type)
Tiger Yang923f7f32008-11-14 11:16:27 +08007199{
7200 if (strcmp(name, "") == 0)
7201 return -EINVAL;
7202
Christoph Hellwig431547b2009-11-13 09:52:56 +00007203 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
7204 name, value, size, flags);
Tiger Yang923f7f32008-11-14 11:16:27 +08007205}
7206
Tiger Yang534eadd2008-11-14 11:16:41 +08007207int ocfs2_init_security_get(struct inode *inode,
7208 struct inode *dir,
7209 struct ocfs2_security_xattr_info *si)
7210{
Tiger Yang38d59ef2008-12-17 10:22:56 +08007211 /* check whether ocfs2 support feature xattr */
7212 if (!ocfs2_supports_xattr(OCFS2_SB(dir->i_sb)))
7213 return -EOPNOTSUPP;
Tiger Yang534eadd2008-11-14 11:16:41 +08007214 return security_inode_init_security(inode, dir, &si->name, &si->value,
7215 &si->value_len);
7216}
7217
7218int ocfs2_init_security_set(handle_t *handle,
7219 struct inode *inode,
7220 struct buffer_head *di_bh,
7221 struct ocfs2_security_xattr_info *si,
7222 struct ocfs2_alloc_context *xattr_ac,
7223 struct ocfs2_alloc_context *data_ac)
7224{
7225 return ocfs2_xattr_set_handle(handle, inode, di_bh,
7226 OCFS2_XATTR_INDEX_SECURITY,
7227 si->name, si->value, si->value_len, 0,
7228 xattr_ac, data_ac);
7229}
7230
Tiger Yang923f7f32008-11-14 11:16:27 +08007231struct xattr_handler ocfs2_xattr_security_handler = {
7232 .prefix = XATTR_SECURITY_PREFIX,
7233 .list = ocfs2_xattr_security_list,
7234 .get = ocfs2_xattr_security_get,
7235 .set = ocfs2_xattr_security_set,
7236};
7237
7238/*
Mark Fasheh99219ae2008-10-07 14:52:59 -07007239 * 'trusted' attributes support
7240 */
Christoph Hellwig431547b2009-11-13 09:52:56 +00007241static size_t ocfs2_xattr_trusted_list(struct dentry *dentry, char *list,
Mark Fasheh99219ae2008-10-07 14:52:59 -07007242 size_t list_size, const char *name,
Christoph Hellwig431547b2009-11-13 09:52:56 +00007243 size_t name_len, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007244{
Tiger Yangceb1eba2008-10-23 16:34:13 +08007245 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
Mark Fasheh99219ae2008-10-07 14:52:59 -07007246 const size_t total_len = prefix_len + name_len + 1;
7247
7248 if (list && total_len <= list_size) {
7249 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
7250 memcpy(list + prefix_len, name, name_len);
7251 list[prefix_len + name_len] = '\0';
7252 }
7253 return total_len;
7254}
7255
Christoph Hellwig431547b2009-11-13 09:52:56 +00007256static int ocfs2_xattr_trusted_get(struct dentry *dentry, const char *name,
7257 void *buffer, size_t size, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007258{
7259 if (strcmp(name, "") == 0)
7260 return -EINVAL;
Christoph Hellwig431547b2009-11-13 09:52:56 +00007261 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
7262 name, buffer, size);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007263}
7264
Christoph Hellwig431547b2009-11-13 09:52:56 +00007265static int ocfs2_xattr_trusted_set(struct dentry *dentry, const char *name,
7266 const void *value, size_t size, int flags, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007267{
7268 if (strcmp(name, "") == 0)
7269 return -EINVAL;
7270
Christoph Hellwig431547b2009-11-13 09:52:56 +00007271 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
7272 name, value, size, flags);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007273}
7274
7275struct xattr_handler ocfs2_xattr_trusted_handler = {
7276 .prefix = XATTR_TRUSTED_PREFIX,
7277 .list = ocfs2_xattr_trusted_list,
7278 .get = ocfs2_xattr_trusted_get,
7279 .set = ocfs2_xattr_trusted_set,
7280};
7281
Mark Fasheh99219ae2008-10-07 14:52:59 -07007282/*
7283 * 'user' attributes support
7284 */
Christoph Hellwig431547b2009-11-13 09:52:56 +00007285static size_t ocfs2_xattr_user_list(struct dentry *dentry, char *list,
Mark Fasheh99219ae2008-10-07 14:52:59 -07007286 size_t list_size, const char *name,
Christoph Hellwig431547b2009-11-13 09:52:56 +00007287 size_t name_len, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007288{
Tiger Yangceb1eba2008-10-23 16:34:13 +08007289 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
Mark Fasheh99219ae2008-10-07 14:52:59 -07007290 const size_t total_len = prefix_len + name_len + 1;
Christoph Hellwig431547b2009-11-13 09:52:56 +00007291 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007292
7293 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7294 return 0;
7295
7296 if (list && total_len <= list_size) {
7297 memcpy(list, XATTR_USER_PREFIX, prefix_len);
7298 memcpy(list + prefix_len, name, name_len);
7299 list[prefix_len + name_len] = '\0';
7300 }
7301 return total_len;
7302}
7303
Christoph Hellwig431547b2009-11-13 09:52:56 +00007304static int ocfs2_xattr_user_get(struct dentry *dentry, const char *name,
7305 void *buffer, size_t size, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007306{
Christoph Hellwig431547b2009-11-13 09:52:56 +00007307 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007308
7309 if (strcmp(name, "") == 0)
7310 return -EINVAL;
7311 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7312 return -EOPNOTSUPP;
Christoph Hellwig431547b2009-11-13 09:52:56 +00007313 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_USER, name,
Mark Fasheh99219ae2008-10-07 14:52:59 -07007314 buffer, size);
7315}
7316
Christoph Hellwig431547b2009-11-13 09:52:56 +00007317static int ocfs2_xattr_user_set(struct dentry *dentry, const char *name,
7318 const void *value, size_t size, int flags, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007319{
Christoph Hellwig431547b2009-11-13 09:52:56 +00007320 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007321
7322 if (strcmp(name, "") == 0)
7323 return -EINVAL;
7324 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7325 return -EOPNOTSUPP;
7326
Christoph Hellwig431547b2009-11-13 09:52:56 +00007327 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_USER,
7328 name, value, size, flags);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007329}
7330
7331struct xattr_handler ocfs2_xattr_user_handler = {
7332 .prefix = XATTR_USER_PREFIX,
7333 .list = ocfs2_xattr_user_list,
7334 .get = ocfs2_xattr_user_get,
7335 .set = ocfs2_xattr_user_set,
7336};